|
13 | 13 | # All configuration values have a default; values that are commented out |
14 | 14 | # serve to show the default. |
15 | 15 |
|
16 | | -import ast |
17 | | -import collections |
18 | | -import os.path |
19 | | -import sys |
20 | | - |
21 | | -PY3 = sys.version_info[:2] > (2, 7) |
22 | | -PY34 = sys.version_info[:2] > (3, 3) |
23 | | - |
24 | | -with open( |
25 | | - os.path.join( |
26 | | - os.path.dirname(__file__), '..', '..', |
27 | | - 'exec_helpers', '__init__.py' |
28 | | - ) |
29 | | -) as f: |
30 | | - source = f.read() |
31 | | - |
32 | | - |
33 | | -# noinspection PyUnresolvedReferences |
34 | | -def get_simple_vars_from_src(src): |
35 | | - """Get simple (string/number/boolean and None) assigned values from source. |
36 | | -
|
37 | | - :param src: Source code |
38 | | - :type src: str |
39 | | - :returns: OrderedDict with keys, values = variable names, values |
40 | | - :rtype: typing.Dict[ |
41 | | - str, |
42 | | - typing.Union[ |
43 | | - str, bytes, |
44 | | - int, float, complex, |
45 | | - list, set, dict, tuple, |
46 | | - None, |
47 | | - ] |
48 | | - ] |
49 | | -
|
50 | | - Limitations: Only defined from scratch variables. |
51 | | - Not supported by design: |
52 | | - * Imports |
53 | | - * Executable code, including string formatting and comprehensions. |
54 | | -
|
55 | | - Examples: |
56 | | -
|
57 | | - >>> string_sample = "a = '1'" |
58 | | - >>> get_simple_vars_from_src(string_sample) |
59 | | - OrderedDict([('a', '1')]) |
60 | | -
|
61 | | - >>> int_sample = "b = 1" |
62 | | - >>> get_simple_vars_from_src(int_sample) |
63 | | - OrderedDict([('b', 1)]) |
64 | | -
|
65 | | - >>> list_sample = "c = [u'1', b'1', 1, 1.0, 1j, None]" |
66 | | - >>> result = get_simple_vars_from_src(list_sample) |
67 | | - >>> result == collections.OrderedDict( |
68 | | - ... [('c', [u'1', b'1', 1, 1.0, 1j, None])] |
69 | | - ... ) |
70 | | - True |
71 | | -
|
72 | | - >>> iterable_sample = "d = ([1], {1: 1}, {1})" |
73 | | - >>> get_simple_vars_from_src(iterable_sample) |
74 | | - OrderedDict([('d', ([1], {1: 1}, {1}))]) |
75 | | -
|
76 | | - >>> multiple_assign = "e = f = g = 1" |
77 | | - >>> get_simple_vars_from_src(multiple_assign) |
78 | | - OrderedDict([('e', 1), ('f', 1), ('g', 1)]) |
79 | | - """ |
80 | | - ast_data = ( |
81 | | - ast.Str, ast.Num, |
82 | | - ast.List, ast.Set, ast.Dict, ast.Tuple |
83 | | - ) |
84 | | - if PY3: |
85 | | - ast_data += (ast.Bytes,) |
86 | | - if PY34: |
87 | | - ast_data += (ast.NameConstant,) |
88 | | - |
89 | | - tree = ast.parse(src) |
90 | | - |
91 | | - result = collections.OrderedDict() |
92 | | - |
93 | | - for node in ast.iter_child_nodes(tree): |
94 | | - if not isinstance(node, ast.Assign): # We parse assigns only |
95 | | - continue |
96 | | - try: |
97 | | - if isinstance(node.value, ast_data): |
98 | | - value = ast.literal_eval(node.value) |
99 | | - elif isinstance( # NameConstant in python < 3.4 |
100 | | - node.value, ast.Name |
101 | | - ) and isinstance( |
102 | | - node.value.ctx, ast.Load # Read constant |
103 | | - ): |
104 | | - value = ast.literal_eval(node.value) |
105 | | - else: |
106 | | - continue |
107 | | - except ValueError: |
108 | | - continue |
109 | | - for tgt in node.targets: |
110 | | - if isinstance( |
111 | | - tgt, ast.Name |
112 | | - ) and isinstance( |
113 | | - tgt.ctx, ast.Store |
114 | | - ): |
115 | | - result[tgt.id] = value |
116 | | - return result |
117 | | - |
118 | | - |
119 | | -variables = get_simple_vars_from_src(source) |
| 16 | +import pkg_resources |
| 17 | + |
| 18 | +release = pkg_resources.get_distribution("exec-helpers").version |
| 19 | +version = '.'.join(release.split('.')[:2]) |
| 20 | + |
120 | 21 |
|
121 | 22 | # If extensions (or modules to document with autodoc) are in another directory, |
122 | 23 | # add these directories to sys.path here. If the directory is relative to the |
@@ -168,9 +69,8 @@ def get_simple_vars_from_src(src): |
168 | 69 | # built documents. |
169 | 70 | # |
170 | 71 | # The short X.Y version. |
171 | | -version = variables['__version__'] |
| 72 | + |
172 | 73 | # The full version, including alpha/beta/rc tags. |
173 | | -release = variables['__version__'] |
174 | 74 |
|
175 | 75 | # The language for content autogenerated by Sphinx. Refer to documentation |
176 | 76 | # for a list of supported languages. |
|
0 commit comments