1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
1 |
|
import csv |
4
|
1 |
|
import json |
5
|
1 |
|
import os |
6
|
|
|
|
7
|
1 |
|
import xmltodict |
|
|
|
|
8
|
1 |
|
import yaml |
|
|
|
|
9
|
|
|
|
10
|
1 |
|
from git_app_version.helper.pyversion import PY3 |
11
|
|
|
|
12
|
1 |
|
try: |
13
|
1 |
|
import ConfigParser as configparser |
14
|
1 |
|
except ImportError: |
15
|
1 |
|
import configparser |
16
|
|
|
|
17
|
|
|
|
18
|
1 |
|
class FileDumper(object): |
|
|
|
|
19
|
|
|
|
20
|
1 |
|
def dump( |
|
|
|
|
21
|
|
|
self, |
|
|
|
|
22
|
|
|
data=None, |
|
|
|
|
23
|
|
|
fileformat='json', |
|
|
|
|
24
|
|
|
target=None, |
|
|
|
|
25
|
|
|
cwd=None, |
|
|
|
|
26
|
|
|
namespace='', |
|
|
|
|
27
|
|
|
csv_delimiter=',', |
|
|
|
|
28
|
|
|
csv_quote='"', |
|
|
|
|
29
|
|
|
csv_eol='lf' |
|
|
|
|
30
|
|
|
): |
31
|
1 |
|
target = self.__check_target(target, cwd) |
32
|
1 |
|
if fileformat == 'yaml' or fileformat == 'yml': |
33
|
1 |
|
return self.dump_yaml(data, target, namespace) |
34
|
1 |
|
elif fileformat == 'xml': |
35
|
1 |
|
return self.dump_xml(data, target, namespace) |
36
|
1 |
|
elif fileformat == 'ini': |
37
|
1 |
|
return self.dump_ini(data, target, namespace) |
38
|
1 |
|
elif fileformat == 'sh': |
39
|
1 |
|
return self.dump_sh(data, target) |
40
|
1 |
|
elif fileformat == 'csv': |
41
|
1 |
|
return self.dump_csv(data, target, delimiter=csv_delimiter, |
42
|
|
|
quotechar=csv_quote, eol=csv_eol) |
43
|
|
|
else: |
44
|
1 |
|
return self.dump_json(data, target, namespace) |
45
|
|
|
|
46
|
1 |
|
def __check_target(self, target, cwd=None): |
47
|
1 |
|
if not os.path.isabs(target): |
48
|
1 |
|
cwd = cwd if cwd else os.getcwd() |
49
|
1 |
|
target = cwd + '/' + target |
50
|
|
|
|
51
|
1 |
|
self.__make_parent_dir(target) |
52
|
|
|
|
53
|
1 |
|
return target |
54
|
|
|
|
55
|
1 |
|
def __make_parent_dir(self, target): |
|
|
|
|
56
|
1 |
|
parent_dir = os.path.dirname(target) |
57
|
1 |
|
if not os.path.exists(parent_dir): |
58
|
1 |
|
os.makedirs(parent_dir, 493) # 493 in decimal as 755 in octal |
59
|
|
|
|
60
|
1 |
|
def __create_infos_to_dump(self, infos, namespace=None): |
|
|
|
|
61
|
1 |
|
to_dump = infos |
62
|
1 |
|
if namespace is not None and namespace != '': |
63
|
1 |
|
namespaces = namespace.split('.') |
64
|
1 |
|
namespaces.reverse() |
65
|
1 |
|
for name in namespaces: |
66
|
1 |
|
to_dump = {name: to_dump} |
67
|
|
|
|
68
|
1 |
|
return to_dump |
69
|
|
|
|
70
|
1 |
|
def __encode(self, item, encoding='utf-8'): |
|
|
|
|
71
|
|
|
if isinstance(item, unicode): |
72
|
|
|
return item.encode(encoding) |
73
|
|
|
else: |
74
|
|
|
return item |
75
|
|
|
|
76
|
1 |
|
def __flatten(self, item): |
|
|
|
|
77
|
|
|
if isinstance(item, list): |
78
|
|
|
flattened_list = '' |
79
|
|
|
if len(item): |
80
|
|
|
flattened_list = "'{}'".format("', '".join(item)) |
81
|
|
|
return "[{}]".format(flattened_list) |
82
|
|
|
else: |
83
|
|
|
return item |
84
|
|
|
|
85
|
1 |
|
def dump_sh(self, data, target): |
|
|
|
|
86
|
1 |
|
target = target + '.sh' |
87
|
|
|
|
88
|
1 |
|
with open(target, 'w') as fpt: |
89
|
1 |
|
for key, val in data.items(): |
90
|
1 |
|
if not PY3: |
91
|
|
|
val = self.__encode(self.__flatten(val)) |
92
|
1 |
|
fpt.write("{}=\"{}\"\n".format(key, val)) |
93
|
|
|
|
94
|
1 |
|
return target |
95
|
|
|
|
96
|
1 |
|
def dump_csv(self, data, target, delimiter=',', quotechar='"', eol='lf'): |
|
|
|
|
97
|
1 |
|
target = target + '.csv' |
98
|
|
|
|
99
|
1 |
|
eol = "\r\n" if eol == 'crlf' or eol == "\r\n" else "\n" |
100
|
|
|
|
101
|
1 |
|
csv.register_dialect('custom', delimiter=str(delimiter), |
102
|
|
|
lineterminator=str(eol), quotechar=str(quotechar), |
103
|
|
|
quoting=csv.QUOTE_MINIMAL) |
104
|
|
|
|
105
|
1 |
|
if PY3: |
106
|
1 |
|
with open(target, 'w', encoding='utf-8') as fpt: |
107
|
1 |
|
writer = csv.writer(fpt, dialect='custom') |
108
|
1 |
|
for key, val in data.items(): |
109
|
1 |
|
writer.writerow((key, val)) |
110
|
|
|
else: |
111
|
|
|
with open(target, 'wb') as fpt: |
112
|
|
|
writer = csv.writer(fpt, dialect='custom') |
113
|
|
|
for key, val in data.items(): |
114
|
|
|
val = self.__encode(self.__flatten(val)) |
115
|
|
|
writer.writerow((key, val)) |
116
|
|
|
|
117
|
1 |
|
return target |
118
|
|
|
|
119
|
1 |
|
def dump_ini(self, data, target, namespace=None): |
|
|
|
|
120
|
1 |
|
target = target + '.ini' |
121
|
|
|
|
122
|
1 |
|
if namespace is None or namespace == '': |
123
|
1 |
|
namespace = 'app_version' |
124
|
|
|
|
125
|
1 |
|
ini = configparser.RawConfigParser() |
126
|
1 |
|
ini.add_section(namespace) |
127
|
|
|
|
128
|
1 |
|
for key, val in data.items(): |
129
|
1 |
|
if not PY3: |
130
|
|
|
val = self.__encode(self.__flatten(val)) |
131
|
|
|
|
132
|
1 |
|
ini.set(namespace, key, val) |
133
|
|
|
|
134
|
1 |
|
with open(target, 'w') as fpt: |
135
|
1 |
|
ini.write(fpt) |
136
|
|
|
|
137
|
1 |
|
return target |
138
|
|
|
|
139
|
1 |
|
def dump_xml(self, data, target, namespace=None): |
|
|
|
|
140
|
1 |
|
target = target + '.xml' |
141
|
1 |
|
if namespace is None or namespace == '': |
142
|
1 |
|
namespace = 'app_version' |
143
|
|
|
|
144
|
1 |
|
with open(target, 'w') as fpt: |
145
|
1 |
|
xml = xmltodict.unparse( |
146
|
|
|
self.__create_infos_to_dump( |
147
|
|
|
data, |
148
|
|
|
namespace), |
149
|
|
|
encoding='utf-8', |
150
|
|
|
pretty=True, |
151
|
|
|
indent=' ') |
152
|
1 |
|
if not PY3: |
153
|
|
|
xml = xml.encode('utf-8') |
154
|
|
|
|
155
|
1 |
|
fpt.write(xml) |
156
|
|
|
|
157
|
1 |
|
return target |
158
|
|
|
|
159
|
1 |
|
def dump_json(self, data, target, namespace=None): |
|
|
|
|
160
|
1 |
|
target = target + '.json' |
161
|
|
|
|
162
|
1 |
|
data1 = self.__create_infos_to_dump(data, namespace) |
163
|
|
|
|
164
|
1 |
|
with open(target, 'w') as fpt: |
165
|
1 |
|
json.dump(data1, fpt, indent=2) |
166
|
|
|
|
167
|
1 |
|
return target |
168
|
|
|
|
169
|
1 |
|
def dump_yaml(self, data, target, namespace=None): |
|
|
|
|
170
|
1 |
|
target = target + '.yml' |
171
|
|
|
|
172
|
1 |
|
with open(target, 'w') as fpt: |
173
|
1 |
|
if not data: |
174
|
1 |
|
fpt.write("---\n") |
175
|
|
|
else: |
176
|
1 |
|
yaml.safe_dump( |
177
|
|
|
self.__create_infos_to_dump(data, namespace), |
178
|
|
|
fpt, |
179
|
|
|
default_flow_style=False, |
180
|
|
|
explicit_start=True, |
181
|
|
|
allow_unicode=True, |
182
|
|
|
# force quoting |
183
|
|
|
# to prevent abbrev_commit to be read as a float |
184
|
|
|
default_style='\'' |
185
|
|
|
) |
186
|
|
|
|
187
|
|
|
return target |
188
|
|
|
|
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.