-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_import.py
67 lines (56 loc) · 3.41 KB
/
export_import.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
import ast # Library for parsing string representations of Python objects
# Class to encapsulate each module and the associated methods for storing it on
# disk.
class module_export_import:
def __init__(self, save_filename, load_filename, module):
self.save_filename = save_filename
self.load_filename = load_filename
self.module = module
# This function is for saving all of the variables in the specified module.
def save_params(self):
# Get a list of all the exportable variables in the module. These
# variables are listed in the special __var_list__ variable in the
# module.
module_vars = getattr(self.module, '__var_list__')
# Loop over all variables in the module, get each value, and write it to
# disk if it is a simple object (elementary data type or one-deep
# container of elementary data types).
with open(self.save_filename, 'wt') as f:
for module_var in module_vars:
module_var_val = getattr(self.module, module_var)
if type(module_var_val) is str: # or type(module_var_val) is unicode:
# We need to make sure string values get printed as string
# values or they won't be read properly on reload
f.write(module_var + " = '" + str(module_var_val) + "'\n")
else:
# This a non-string elementary data type, so we write it as-is
f.write(module_var + ' = ' + str(module_var_val) + '\n')
# This function is for setting variables in the specified module using
# previously saved parameters.
def load_params(self):
# Loop over all variables in the file, get each value, and save it to
# the module.
with open(self.load_filename, 'rt') as f:
for line in f:
# This checks for comment and blank lines and skips them
if line.strip().startswith('#') or len(line.strip()) == 0:
continue
# NOTE: Alternatively, if we didn't care about security, we
# could just exec each line (or the whole file) as the lines are
# written in valid Python syntax of 'variable = value'.
# Split on first equal sign only and strip surrounding
# whitespace. Stripping the whitespace is necessary or else we
# will have a variable 'name' or 'value' that is surrounded by
# spaces. When the 'name' is actually ' name ' (note the
# spaces), setattr() will assign the parsed value to a module
# variable named ' name ', not 'name'. If the value has
# surrounding whitespace (at least leading whitespace), the ast
# parser will give an 'unexpected indent' error.
module_var, module_var_val = line.split('=', 1)
module_var = module_var.strip()
module_var_val = module_var_val.strip()
#If a value starts with ' or " then it is a string and ast.literal_eval will return an error
#This can be set directly, otherwise use literal_eval to get the literal from the string
if module_var_val[0] != "'" or module_var_val[0] != '"':
module_var_val = ast.literal_eval(module_var_val)
setattr(self.module, module_var, module_var_val)