-
Notifications
You must be signed in to change notification settings - Fork 4
/
state.py
170 lines (121 loc) · 4.44 KB
/
state.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
"""
.. module:: facio.state
:synopsis: Facio state module, for accessing and maintaining state during
the template generation process.
"""
import os
from facio.base import BaseFacio
from sh import pwd
from six.moves import builtins
class State(BaseFacio):
def __init__(self):
""" State is stored in a __facio__ super global set at the moment
this class is instantiated but only if not already set.
This class is basically a proxy class for interfacing with __facio__
super global variable. All state is set and retrieved from __facio__.
"""
try:
self.state = builtins.__facio__
except AttributeError:
builtins.__facio__ = self
self.state = builtins.__facio__
def set_project_name(self, name):
""" Set the project name to the state.
:param name: The project name from facio.config.CommandLineInterface
:type name: str
"""
self.update_context_variables({'PROJECT_NAME': name})
self.state.project_name = name
def get_project_name(self):
""" Return the project name stored in the state.
:returns: str
"""
return self.state.project_name
def get_working_directory(self):
""" Use the ``sh`` library to return the current working directory
using the unix command ``pwd``.
:returns: str
"""
return '{0}'.format(pwd()).strip()
def get_project_root(self):
""" Return the project root, which is the current working directory
plus the project name.
:returns: str
"""
return os.path.join(self.get_working_directory(),
self.get_project_name())
def update_context_variables(self, dictionary):
""" Update the context varaibles dict with new values.
** Usage: **
.. code-block:: python
from facio.state import state
dictionary = {
'bar': 'baz',
'fib': 'fab',
}
state.update_context_variables(dictionary)
:param dictionary: Dictionary of new key values
:type dictionary: dict
"""
try:
dict1 = self.state.context_variables
except AttributeError:
self.state.context_variables = {}
dict1 = self.state.context_variables
dict2 = dictionary
if isinstance(dict1, dict) and isinstance(dict2, dict):
dict1.update(dict2)
self.state.context_variables = dict1
else:
self.warning('Failed to update context variables with {0}'.format(
dict2))
def get_context_variables(self):
""" Returns the current context variables at time of call.
:retutns: dict
"""
try:
return self.state.context_variables
except AttributeError:
return {}
def get_context_variable(self, name):
""" Return a specific context variable value.
:param name: Context variable name
:type name: str
:returns: str or None -- None if name not found in var list
"""
variables = self.get_context_variables()
return variables.get(name, None)
def get_hook_call(self, module_path):
""" Returns a hook call result, else returns false if the module
path is not in the hook call list.
:param module_path: The python dotted path to the module
:type module_path: str
:returns: Call result
"""
try:
calls = self.state.hook_calls
except AttributeError:
calls = []
try:
module, result = [(m, r) for m, r in calls if m == module_path][0]
except IndexError:
return None
return result
def save_hook_call(self, module_path, result):
""" Saves a hook call to state
:param module_path: The python dotted path to the module
:type module_path: str
:param result: The result of the module run() function
:type result: Anything
:returns: list -- The call list or tuples
"""
try:
calls = self.state.hook_calls
except AttributeError:
calls = []
if not self.get_hook_call(module_path):
calls.append((module_path, result))
self.state.hook_calls = calls
return calls
state = State()