1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
1 |
|
import os |
4
|
1 |
|
import json |
5
|
1 |
|
import xmltodict |
|
|
|
|
6
|
1 |
|
from collections import Iterable |
|
|
|
|
7
|
|
|
|
8
|
1 |
|
try: |
9
|
1 |
|
import configparser |
10
|
|
|
except ImportError: |
11
|
|
|
import ConfigParser as configparser |
|
|
|
|
12
|
|
|
|
13
|
1 |
|
import yaml |
|
|
|
|
14
|
1 |
|
try: |
15
|
1 |
|
from yaml import CDumper as Dumper |
|
|
|
|
16
|
|
|
except ImportError: |
17
|
|
|
from yaml import Dumper |
|
|
|
|
18
|
|
|
|
19
|
1 |
|
from git_app_version.helper.pyversion import PY3 |
20
|
|
|
|
21
|
1 |
|
class Dumper(object): |
|
|
|
|
22
|
|
|
|
23
|
1 |
|
def dump(self, data = {}, format = 'json', target = None, cwd=None, namespace=''): |
|
|
|
|
24
|
1 |
|
target = self.__checkTarget(target, cwd) |
25
|
1 |
|
if format == 'yaml' or format == 'yml': |
26
|
1 |
|
return self.dumpYaml(data, target, namespace) |
27
|
1 |
|
elif format == 'xml': |
28
|
1 |
|
return self.dumpXml(data, target, namespace) |
29
|
1 |
|
elif format == 'ini': |
30
|
1 |
|
return self.dumpIni(data, target, namespace) |
31
|
|
|
else: |
32
|
1 |
|
return self.dumpJson(data, target, namespace) |
33
|
|
|
|
34
|
1 |
|
def __checkTarget(self, target, cwd=None): |
|
|
|
|
35
|
1 |
|
if not os.path.isabs(target): |
36
|
1 |
|
if cwd is None or not os.path.exists(cwd): |
37
|
|
|
cwd = os.getcwd() |
38
|
|
|
|
39
|
1 |
|
target = cwd+'/'+target |
40
|
|
|
|
41
|
1 |
|
self.__makeParentDir(target) |
42
|
|
|
|
43
|
1 |
|
return target |
44
|
|
|
|
45
|
1 |
|
def __makeParentDir(self, target): |
|
|
|
|
46
|
1 |
|
parentDir = os.path.dirname(target) |
|
|
|
|
47
|
1 |
|
if not os.path.exists(parentDir): |
48
|
|
|
os.makedirs(parentDir, 493) # 493 in decimal as 755 in octal |
49
|
|
|
|
50
|
1 |
|
def __createInfosToDump(self, infos, namespace=None): |
|
|
|
|
51
|
1 |
|
toDump = 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 |
|
toDump = { name: toDump } |
|
|
|
|
57
|
|
|
|
58
|
1 |
|
return toDump |
59
|
|
|
|
60
|
1 |
|
def __encode(self, input, encoding='utf-8'): |
|
|
|
|
61
|
|
|
if isinstance(input, dict): |
62
|
|
|
return {self.__encode(key, encoding): self.__encode(value, encoding) for key, value in input.iteritems()} |
|
|
|
|
63
|
|
|
elif isinstance(input, list): |
64
|
|
|
return [self.__encode(element, encoding) for element in input] |
65
|
|
|
elif isinstance(input, unicode): |
|
|
|
|
66
|
|
|
return input.encode(encoding) |
67
|
|
|
else: |
68
|
|
|
return input |
69
|
|
|
|
70
|
1 |
|
def dumpIni(self, data, target, namespace=None): |
|
|
|
|
71
|
1 |
|
target = target+'.ini' |
72
|
1 |
|
namespace = 'app_version' if namespace is None or namespace == '' else namespace |
73
|
|
|
|
74
|
1 |
|
ini = configparser.RawConfigParser() |
75
|
1 |
|
ini.add_section(namespace) |
76
|
|
|
|
77
|
1 |
|
for key,val in data.items(): |
|
|
|
|
78
|
1 |
|
if PY3: |
79
|
1 |
|
ini.set(namespace, key, val) |
80
|
|
|
else: |
81
|
|
|
ini.set(namespace, key, self.__encode(val)) |
82
|
|
|
|
83
|
1 |
|
with open(target, 'w') as f: |
|
|
|
|
84
|
1 |
|
ini.write(f) |
85
|
|
|
|
86
|
1 |
|
return target |
87
|
|
|
|
88
|
1 |
|
def dumpXml(self, data, target, namespace=None): |
|
|
|
|
89
|
1 |
|
target = target+'.xml' |
90
|
1 |
|
namespace = 'app_version' if namespace is None or namespace == '' else namespace |
91
|
|
|
|
92
|
1 |
|
with open(target, 'w') as f: |
|
|
|
|
93
|
1 |
|
xml = xmltodict.unparse(self.__createInfosToDump(data, namespace), encoding='utf-8', pretty=True, indent=' ') |
|
|
|
|
94
|
1 |
|
if PY3: |
95
|
1 |
|
f.write(xml) |
96
|
|
|
else: |
97
|
|
|
f.write(xml.encode('utf-8')) |
98
|
|
|
|
99
|
1 |
|
return target |
100
|
|
|
|
101
|
1 |
|
def dumpJson(self, data, target, namespace=None): |
|
|
|
|
102
|
1 |
|
target = target+'.json' |
103
|
|
|
|
104
|
1 |
|
data1 = self.__createInfosToDump(data, namespace) |
105
|
|
|
|
106
|
1 |
|
with open(target, 'w') as f: |
|
|
|
|
107
|
1 |
|
json.dump(data1, f, indent=2) |
108
|
|
|
|
109
|
1 |
|
return target |
110
|
|
|
|
111
|
1 |
|
def dumpYaml(self, data, target, namespace=None): |
|
|
|
|
112
|
1 |
|
target = target+'.yml' |
113
|
|
|
|
114
|
1 |
|
with open(target, 'w') as f: |
|
|
|
|
115
|
1 |
|
if not data : |
|
|
|
|
116
|
1 |
|
f.write("---\n") |
117
|
|
|
else : |
|
|
|
|
118
|
1 |
|
yaml.safe_dump( |
119
|
|
|
self.__createInfosToDump(data, namespace), |
120
|
|
|
f, |
121
|
|
|
default_flow_style=False, |
122
|
|
|
explicit_start=True, |
123
|
|
|
allow_unicode=True, |
124
|
|
|
default_style='\'' # force quoting to prevent abbrev_commit to be read as a float |
|
|
|
|
125
|
|
|
) |
126
|
|
|
|
127
|
|
|
return target |
128
|
|
|
|
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.