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
|
|
|
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 |
|
cwd = cwd if cwd else os.getcwd() |
39
|
1 |
|
target = cwd + '/' + target |
40
|
|
|
|
41
|
1 |
|
self.__make_parent_dir(target) |
42
|
|
|
|
43
|
1 |
|
return target |
44
|
|
|
|
45
|
1 |
|
def __make_parent_dir(self, target): |
|
|
|
|
46
|
1 |
|
parent_dir = os.path.dirname(target) |
47
|
1 |
|
if not os.path.exists(parent_dir): |
48
|
1 |
|
os.makedirs(parent_dir, 493) # 493 in decimal as 755 in octal |
49
|
|
|
|
50
|
1 |
|
def __create_infos_to_dump(self, infos, namespace=None): |
|
|
|
|
51
|
1 |
|
to_dump = infos |
52
|
1 |
|
if namespace is not None and namespace != '': |
53
|
1 |
|
namespaces = namespace.split('.') |
54
|
1 |
|
namespaces.reverse() |
55
|
1 |
|
for name in namespaces: |
56
|
1 |
|
to_dump = {name: to_dump} |
57
|
|
|
|
58
|
1 |
|
return to_dump |
59
|
|
|
|
60
|
1 |
|
def __encode(self, item, encoding='utf-8'): |
|
|
|
|
61
|
1 |
|
if isinstance(item, unicode): |
|
|
|
|
62
|
1 |
|
return item.encode(encoding) |
63
|
|
|
else: |
64
|
1 |
|
return item |
65
|
|
|
|
66
|
1 |
|
def __flatten(self, item): |
|
|
|
|
67
|
1 |
|
if isinstance(item, list): |
68
|
1 |
|
flattened_list = '' |
69
|
1 |
|
if len(item): |
70
|
1 |
|
flattened_list = "'{}'".format("', '".join(item)) |
71
|
1 |
|
return "[{}]".format(flattened_list) |
72
|
|
|
else: |
73
|
1 |
|
return item |
74
|
|
|
|
75
|
1 |
|
def dump_ini(self, data, target, namespace=None): |
|
|
|
|
76
|
1 |
|
target = target + '.ini' |
77
|
|
|
|
78
|
1 |
|
if namespace is None or namespace == '': |
79
|
|
|
namespace = 'app_version' |
80
|
|
|
|
81
|
1 |
|
ini = configparser.RawConfigParser() |
82
|
1 |
|
ini.add_section(namespace) |
83
|
|
|
|
84
|
1 |
|
for key, val in data.items(): |
85
|
1 |
|
if not PY3: |
86
|
1 |
|
val = self.__encode(self.__flatten(val)) |
87
|
|
|
|
88
|
1 |
|
ini.set(namespace, key, val) |
89
|
|
|
|
90
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
91
|
1 |
|
ini.write(fp) |
92
|
|
|
|
93
|
1 |
|
return target |
94
|
|
|
|
95
|
1 |
|
def dump_xml(self, data, target, namespace=None): |
|
|
|
|
96
|
1 |
|
target = target + '.xml' |
97
|
1 |
|
if namespace is None or namespace == '': |
98
|
1 |
|
namespace = 'app_version' |
99
|
|
|
|
100
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
101
|
1 |
|
xml = xmltodict.unparse( |
102
|
|
|
self.__create_infos_to_dump( |
103
|
|
|
data, |
104
|
|
|
namespace), |
105
|
|
|
encoding='utf-8', |
106
|
|
|
pretty=True, |
107
|
|
|
indent=' ') |
108
|
1 |
|
if not PY3: |
109
|
1 |
|
xml = xml.encode('utf-8') |
110
|
|
|
|
111
|
1 |
|
fp.write(xml) |
112
|
|
|
|
113
|
1 |
|
return target |
114
|
|
|
|
115
|
1 |
|
def dump_json(self, data, target, namespace=None): |
|
|
|
|
116
|
1 |
|
target = target + '.json' |
117
|
|
|
|
118
|
1 |
|
data1 = self.__create_infos_to_dump(data, namespace) |
119
|
|
|
|
120
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
121
|
1 |
|
json.dump(data1, fp, indent=2) |
122
|
|
|
|
123
|
1 |
|
return target |
124
|
|
|
|
125
|
1 |
|
def dump_yaml(self, data, target, namespace=None): |
|
|
|
|
126
|
1 |
|
target = target + '.yml' |
127
|
|
|
|
128
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
129
|
1 |
|
if not data: |
130
|
1 |
|
fp.write("---\n") |
131
|
|
|
else: |
132
|
1 |
|
yaml.safe_dump( |
133
|
|
|
self.__create_infos_to_dump(data, namespace), |
134
|
|
|
fp, |
135
|
|
|
default_flow_style=False, |
136
|
|
|
explicit_start=True, |
137
|
|
|
allow_unicode=True, |
138
|
|
|
# force quoting |
139
|
|
|
# to prevent abbrev_commit to be read as a float |
140
|
|
|
default_style='\'' |
141
|
|
|
) |
142
|
|
|
|
143
|
|
|
return target |
144
|
|
|
|
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.