-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
49 lines (37 loc) · 1.36 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from django.shortcuts import render
from django.template.loader import render_to_string
from django.utils.encoding import smart_str
from django.utils.safestring import mark_safe
from django.http import JsonResponse
from django.conf import settings
import random
import string
import importlib
import re
IGNORE = ("id", "template_name", "request")
def get_vars(instance):
props = set()
for prop in dir(instance):
if not callable(getattr(instance, prop)) and \
not "__" in prop and \
not prop in IGNORE \
and not prop.startswith("_LivewireComponent"):
props.add(prop)
return props
def get_id():
return ''.join(random.choice(string.ascii_lowercase) for i in range(20))
def snakecase(name):
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
def get_component_name(name):
name = name.replace("_", " ")
return name.title().replace(" ", "")
def instance_class(component_name, **kwargs):
path = getattr(settings, "LIVEWIRE_COMPONENTS_PREFIX")
if not path:
assert False, "LIVEWIRE_COMPONENTS_PREFIX missing"
module = importlib.import_module(path)
class_name = get_component_name(component_name)
class_livewire = getattr(module, '{}Livewire'.format(class_name))
inst = class_livewire()
return inst