1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
1 |
|
import os |
4
|
1 |
|
import json |
5
|
|
|
|
6
|
1 |
|
import xmltodict |
|
|
|
|
7
|
1 |
|
from git_app_version.helper.pyversion import PY3 |
8
|
|
|
|
9
|
1 |
|
try: |
10
|
1 |
|
import ConfigParser as configparser |
11
|
|
|
except ImportError: |
12
|
|
|
import configparser |
13
|
|
|
|
14
|
1 |
|
import yaml |
|
|
|
|
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
|
|
|
else: |
34
|
1 |
|
return self.dump_json(data, target, namespace) |
35
|
|
|
|
36
|
1 |
|
def __check_target(self, target, cwd=None): |
37
|
1 |
|
if not os.path.isabs(target): |
38
|
1 |
|
if cwd is None or not os.path.exists(cwd): |
39
|
|
|
cwd = os.getcwd() |
40
|
|
|
|
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
|
|
|
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 |
|
if len(item): |
71
|
1 |
|
return "['{}']".format("', '".join(item)) |
72
|
|
|
return None |
73
|
|
|
else: |
74
|
1 |
|
return item |
75
|
|
|
|
76
|
1 |
|
def dump_ini(self, data, target, namespace=None): |
|
|
|
|
77
|
1 |
|
target = target + '.ini' |
78
|
1 |
|
namespace = 'app_version' if namespace is None or namespace == '' else namespace |
79
|
|
|
|
80
|
1 |
|
ini = configparser.RawConfigParser() |
81
|
1 |
|
ini.add_section(namespace) |
82
|
|
|
|
83
|
1 |
|
for key, val in data.items(): |
84
|
1 |
|
if PY3: |
85
|
|
|
ini.set(namespace, key, val) |
86
|
|
|
else: |
87
|
1 |
|
ini.set(namespace, key, self.__encode(self.__flatten(val))) |
88
|
|
|
|
89
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
90
|
1 |
|
ini.write(fp) |
91
|
|
|
|
92
|
1 |
|
return target |
93
|
|
|
|
94
|
1 |
|
def dump_xml(self, data, target, namespace=None): |
|
|
|
|
95
|
1 |
|
target = target + '.xml' |
96
|
1 |
|
namespace = 'app_version' if namespace is None or namespace == '' else namespace |
97
|
|
|
|
98
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
99
|
1 |
|
xml = xmltodict.unparse( |
100
|
|
|
self.__create_infos_to_dump( |
101
|
|
|
data, |
102
|
|
|
namespace), |
103
|
|
|
encoding='utf-8', |
104
|
|
|
pretty=True, |
105
|
|
|
indent=' ') |
106
|
1 |
|
if PY3: |
107
|
|
|
fp.write(xml) |
108
|
|
|
else: |
109
|
1 |
|
fp.write(xml.encode('utf-8')) |
110
|
|
|
|
111
|
1 |
|
return target |
112
|
|
|
|
113
|
1 |
|
def dump_json(self, data, target, namespace=None): |
|
|
|
|
114
|
1 |
|
target = target + '.json' |
115
|
|
|
|
116
|
1 |
|
data1 = self.__create_infos_to_dump(data, namespace) |
117
|
|
|
|
118
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
119
|
1 |
|
json.dump(data1, fp, indent=2) |
120
|
|
|
|
121
|
1 |
|
return target |
122
|
|
|
|
123
|
1 |
|
def dump_yaml(self, data, target, namespace=None): |
|
|
|
|
124
|
1 |
|
target = target + '.yml' |
125
|
|
|
|
126
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
127
|
1 |
|
if not data: |
128
|
1 |
|
fp.write("---\n") |
129
|
|
|
else: |
130
|
1 |
|
yaml.safe_dump( |
131
|
|
|
self.__create_infos_to_dump(data, namespace), |
132
|
|
|
fp, |
133
|
|
|
default_flow_style=False, |
134
|
|
|
explicit_start=True, |
135
|
|
|
allow_unicode=True, |
136
|
|
|
default_style='\'' # force quoting to prevent abbrev_commit to be read as a float |
|
|
|
|
137
|
|
|
) |
138
|
|
|
|
139
|
|
|
return target |
140
|
|
|
|
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.