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
|
1 |
|
except ImportError: |
12
|
1 |
|
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 |
|
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
|
|
|
if isinstance(item, unicode): |
|
|
|
|
62
|
|
|
return item.encode(encoding) |
63
|
|
|
else: |
64
|
|
|
return item |
65
|
|
|
|
66
|
1 |
|
def __flatten(self, item): |
|
|
|
|
67
|
|
|
if isinstance(item, list): |
68
|
|
|
flattened_list = '' |
69
|
|
|
if len(item): |
70
|
|
|
flattened_list = "'{}'".format("', '".join(item)) |
71
|
|
|
return "[{}]".format(flattened_list) |
72
|
|
|
else: |
73
|
|
|
return item |
74
|
|
|
|
75
|
1 |
|
def dump_ini(self, data, target, namespace=None): |
|
|
|
|
76
|
1 |
|
target = target + '.ini' |
77
|
1 |
|
namespace = 'app_version' if namespace is None or namespace == '' else namespace |
78
|
|
|
|
79
|
1 |
|
ini = configparser.RawConfigParser() |
80
|
1 |
|
ini.add_section(namespace) |
81
|
|
|
|
82
|
1 |
|
for key, val in data.items(): |
83
|
1 |
|
if not PY3: |
84
|
|
|
val = self.__encode(self.__flatten(val)) |
85
|
|
|
|
86
|
1 |
|
ini.set(namespace, key, val) |
87
|
|
|
|
88
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
89
|
1 |
|
ini.write(fp) |
90
|
|
|
|
91
|
1 |
|
return target |
92
|
|
|
|
93
|
1 |
|
def dump_xml(self, data, target, namespace=None): |
|
|
|
|
94
|
1 |
|
target = target + '.xml' |
95
|
1 |
|
namespace = 'app_version' if namespace is None or namespace == '' else namespace |
96
|
|
|
|
97
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
98
|
1 |
|
xml = xmltodict.unparse( |
99
|
|
|
self.__create_infos_to_dump( |
100
|
|
|
data, |
101
|
|
|
namespace), |
102
|
|
|
encoding='utf-8', |
103
|
|
|
pretty=True, |
104
|
|
|
indent=' ') |
105
|
1 |
|
if not PY3: |
106
|
|
|
xml = xml.encode('utf-8') |
107
|
|
|
|
108
|
1 |
|
fp.write(xml) |
109
|
|
|
|
110
|
1 |
|
return target |
111
|
|
|
|
112
|
1 |
|
def dump_json(self, data, target, namespace=None): |
|
|
|
|
113
|
1 |
|
target = target + '.json' |
114
|
|
|
|
115
|
1 |
|
data1 = self.__create_infos_to_dump(data, namespace) |
116
|
|
|
|
117
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
118
|
1 |
|
json.dump(data1, fp, indent=2) |
119
|
|
|
|
120
|
1 |
|
return target |
121
|
|
|
|
122
|
1 |
|
def dump_yaml(self, data, target, namespace=None): |
|
|
|
|
123
|
1 |
|
target = target + '.yml' |
124
|
|
|
|
125
|
1 |
|
with open(target, 'w') as fp: |
|
|
|
|
126
|
1 |
|
if not data: |
127
|
1 |
|
fp.write("---\n") |
128
|
|
|
else: |
129
|
1 |
|
yaml.safe_dump( |
130
|
|
|
self.__create_infos_to_dump(data, namespace), |
131
|
|
|
fp, |
132
|
|
|
default_flow_style=False, |
133
|
|
|
explicit_start=True, |
134
|
|
|
allow_unicode=True, |
135
|
|
|
default_style='\'' # force quoting to prevent abbrev_commit to be read as a float |
|
|
|
|
136
|
|
|
) |
137
|
|
|
|
138
|
|
|
return target |
139
|
|
|
|
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.