-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.py
58 lines (45 loc) · 1.5 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
50
51
52
53
54
55
56
57
58
def introspect_properties(model_class):
"""
Introspect a :py:class:`elasticgit.models.Model` and
retrieve a suitable mapping to use when indexing instances
of the model in Elasticsearch.
>>> from elasticgit.models import Model, TextField
>>>
>>> class TestModel(Model):
... field = TextField('A text field')
...
>>> from elasticgit.utils import introspect_properties
>>>
>>> sorted(introspect_properties(TestModel).keys()) # doctest: +ELLIPSIS
['_version', 'field', 'uuid']
>>>
"""
return dict([
(field_name, field_object.mapping)
for field_name, field_object in model_class._fields.items()
])
def load_class(class_path):
"""
Load a class by it's class path
:param str class_path:
The dotted.path.to.TheClass
>>> from elasticgit.utils import load_class
>>> load_class('elasticgit.tests.base.TestPerson')
<class 'elasticgit.tests.base.TestPerson'>
>>>
"""
module_name, class_name = class_path.rsplit('.', 1)
mod = __import__(module_name, fromlist=[class_name])
return getattr(mod, class_name)
def fqcn(klass):
"""
Given a class give it's fully qualified class name in dotted notation.
The inverse of `load_class`
:param class klass:
>>> from elasticgit.utils import fqcn
>>> from elasticgit.tests.base import TestPerson
>>> fqcn(TestPerson)
'elasticgit.tests.base.TestPerson'
>>>
"""
return '%s.%s' % (klass.__module__, klass.__name__)