1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
1 |
|
import json |
4
|
1 |
|
import os |
5
|
|
|
|
6
|
1 |
|
import xmltodict |
|
|
|
|
7
|
1 |
|
import yaml |
|
|
|
|
8
|
|
|
|
9
|
1 |
|
from git_app_version.helper.pyversion import PY3 |
10
|
|
|
|
11
|
1 |
|
try: |
12
|
1 |
|
import ConfigParser as configparser |
13
|
|
|
except ImportError: |
14
|
|
|
import configparser |
15
|
|
|
|
16
|
|
|
|
17
|
1 |
|
class FileDumper(object): |
|
|
|
|
18
|
|
|
|
19
|
1 |
|
def dump( |
|
|
|
|
20
|
|
|
self, |
21
|
|
|
data=None, |
22
|
|
|
fileformat='json', |
23
|
|
|
target=None, |
24
|
|
|
cwd=None, |
25
|
|
|
namespace=''): |
26
|
1 |
|
target = self.__check_target(target, cwd) |
27
|
1 |
|
if fileformat == 'yaml' or fileformat == 'yml': |
28
|
1 |
|
return self.dump_yaml(data, target, namespace) |
29
|
1 |
|
elif fileformat == 'xml': |
30
|
1 |
|
return self.dump_xml(data, target, namespace) |
31
|
1 |
|
elif fileformat == 'ini': |
32
|
1 |
|
return self.dump_ini(data, target, namespace) |
33
|
1 |
|
elif fileformat == 'sh': |
34
|
1 |
|
return self.dump_sh(data, target) |
35
|
|
|
else: |
36
|
1 |
|
return self.dump_json(data, target, namespace) |
37
|
|
|
|
38
|
1 |
|
def __check_target(self, target, cwd=None): |
39
|
1 |
|
if not os.path.isabs(target): |
40
|
1 |
|
cwd = cwd if cwd else os.getcwd() |
41
|
1 |
|
target = cwd + '/' + target |
42
|
|
|
|
43
|
1 |
|
self.__make_parent_dir(target) |
44
|
|
|
|
45
|
1 |
|
return target |
46
|
|
|
|
47
|
1 |
|
def __make_parent_dir(self, target): |
|
|
|
|
48
|
1 |
|
parent_dir = os.path.dirname(target) |
49
|
1 |
|
if not os.path.exists(parent_dir): |
50
|
1 |
|
os.makedirs(parent_dir, 493) # 493 in decimal as 755 in octal |
51
|
|
|
|
52
|
1 |
|
def __create_infos_to_dump(self, infos, namespace=None): |
|
|
|
|
53
|
1 |
|
to_dump = infos |
54
|
1 |
|
if namespace is not None and namespace != '': |
55
|
1 |
|
namespaces = namespace.split('.') |
56
|
1 |
|
namespaces.reverse() |
57
|
1 |
|
for name in namespaces: |
58
|
1 |
|
to_dump = {name: to_dump} |
59
|
|
|
|
60
|
1 |
|
return to_dump |
61
|
|
|
|
62
|
1 |
|
def __encode(self, item, encoding='utf-8'): |
|
|
|
|
63
|
1 |
|
if isinstance(item, unicode): |
64
|
1 |
|
return item.encode(encoding) |
65
|
|
|
else: |
66
|
1 |
|
return item |
67
|
|
|
|
68
|
1 |
|
def __flatten(self, item): |
|
|
|
|
69
|
1 |
|
if isinstance(item, list): |
70
|
1 |
|
flattened_list = '' |
71
|
1 |
|
if len(item): |
72
|
1 |
|
flattened_list = "'{}'".format("', '".join(item)) |
73
|
1 |
|
return "[{}]".format(flattened_list) |
74
|
|
|
else: |
75
|
1 |
|
return item |
76
|
|
|
|
77
|
1 |
|
def dump_sh(self, data, target): |
|
|
|
|
78
|
1 |
|
target = target + '.sh' |
79
|
|
|
|
80
|
1 |
|
with open(target, 'w') as fpt: |
81
|
1 |
|
for key, val in data.items(): |
82
|
1 |
|
if not PY3: |
83
|
1 |
|
val = self.__encode(self.__flatten(val)) |
84
|
1 |
|
fpt.write("{}=\"{}\"\n".format(key, val)) |
85
|
|
|
|
86
|
1 |
|
return target |
87
|
|
|
|
88
|
1 |
|
def dump_ini(self, data, target, namespace=None): |
|
|
|
|
89
|
1 |
|
target = target + '.ini' |
90
|
|
|
|
91
|
1 |
|
if namespace is None or namespace == '': |
92
|
1 |
|
namespace = 'app_version' |
93
|
|
|
|
94
|
1 |
|
ini = configparser.RawConfigParser() |
95
|
1 |
|
ini.add_section(namespace) |
96
|
|
|
|
97
|
1 |
|
for key, val in data.items(): |
98
|
1 |
|
if not PY3: |
99
|
1 |
|
val = self.__encode(self.__flatten(val)) |
100
|
|
|
|
101
|
1 |
|
ini.set(namespace, key, val) |
102
|
|
|
|
103
|
1 |
|
with open(target, 'w') as fpt: |
104
|
1 |
|
ini.write(fpt) |
105
|
|
|
|
106
|
1 |
|
return target |
107
|
|
|
|
108
|
1 |
|
def dump_xml(self, data, target, namespace=None): |
|
|
|
|
109
|
1 |
|
target = target + '.xml' |
110
|
1 |
|
if namespace is None or namespace == '': |
111
|
1 |
|
namespace = 'app_version' |
112
|
|
|
|
113
|
1 |
|
with open(target, 'w') as fpt: |
114
|
1 |
|
xml = xmltodict.unparse( |
115
|
|
|
self.__create_infos_to_dump( |
116
|
|
|
data, |
117
|
|
|
namespace), |
118
|
|
|
encoding='utf-8', |
119
|
|
|
pretty=True, |
120
|
|
|
indent=' ') |
121
|
1 |
|
if not PY3: |
122
|
1 |
|
xml = xml.encode('utf-8') |
123
|
|
|
|
124
|
1 |
|
fpt.write(xml) |
125
|
|
|
|
126
|
1 |
|
return target |
127
|
|
|
|
128
|
1 |
|
def dump_json(self, data, target, namespace=None): |
|
|
|
|
129
|
1 |
|
target = target + '.json' |
130
|
|
|
|
131
|
1 |
|
data1 = self.__create_infos_to_dump(data, namespace) |
132
|
|
|
|
133
|
1 |
|
with open(target, 'w') as fpt: |
134
|
1 |
|
json.dump(data1, fpt, indent=2) |
135
|
|
|
|
136
|
1 |
|
return target |
137
|
|
|
|
138
|
1 |
|
def dump_yaml(self, data, target, namespace=None): |
|
|
|
|
139
|
1 |
|
target = target + '.yml' |
140
|
|
|
|
141
|
1 |
|
with open(target, 'w') as fpt: |
142
|
1 |
|
if not data: |
143
|
1 |
|
fpt.write("---\n") |
144
|
|
|
else: |
145
|
1 |
|
yaml.safe_dump( |
146
|
|
|
self.__create_infos_to_dump(data, namespace), |
147
|
|
|
fpt, |
148
|
|
|
default_flow_style=False, |
149
|
|
|
explicit_start=True, |
150
|
|
|
allow_unicode=True, |
151
|
|
|
# force quoting |
152
|
|
|
# to prevent abbrev_commit to be read as a float |
153
|
|
|
default_style='\'' |
154
|
|
|
) |
155
|
|
|
|
156
|
|
|
return target |
157
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.