1 | # -*- coding: utf-8 -*- |
||
2 | 1 | ''' |
|
3 | Data dumper to files with several format |
||
4 | ''' |
||
5 | 1 | from __future__ import unicode_literals |
|
6 | |||
7 | 1 | import json |
|
8 | 1 | from builtins import open |
|
9 | |||
10 | 1 | import xmltodict |
|
11 | 1 | import yaml |
|
12 | 1 | from backports import configparser, csv |
|
0 ignored issues
–
show
|
|||
13 | |||
14 | 1 | import git_app_version.helper.tools as tools |
|
15 | |||
16 | |||
17 | 1 | class FileDumper(object): |
|
18 | ''' |
||
19 | Main dumper |
||
20 | ''' |
||
21 | |||
22 | 1 | def dump( |
|
0 ignored issues
–
show
|
|||
23 | self, |
||
0 ignored issues
–
show
|
|||
24 | data=None, |
||
0 ignored issues
–
show
|
|||
25 | fileformat='json', |
||
0 ignored issues
–
show
|
|||
26 | target=None, |
||
0 ignored issues
–
show
|
|||
27 | cwd=None, |
||
0 ignored issues
–
show
|
|||
28 | namespace='', |
||
0 ignored issues
–
show
|
|||
29 | csv_delimiter=',', |
||
0 ignored issues
–
show
|
|||
30 | csv_quote='"', |
||
0 ignored issues
–
show
|
|||
31 | csv_eol='lf' |
||
0 ignored issues
–
show
|
|||
32 | ): |
||
33 | ''' |
||
34 | Agnostic main dump function |
||
35 | ''' |
||
36 | |||
37 | 1 | target = tools.create_parent_dirs(target, cwd) |
|
38 | 1 | if fileformat == 'yaml' or fileformat == 'yml': |
|
39 | 1 | return self.__dump_yaml(data, target, namespace) |
|
40 | 1 | elif fileformat == 'xml': |
|
41 | 1 | return self.__dump_xml(data, target, namespace) |
|
42 | 1 | elif fileformat == 'ini': |
|
43 | 1 | return self.__dump_ini(data, target, namespace) |
|
44 | 1 | elif fileformat == 'sh': |
|
45 | 1 | return self.__dump_sh(data, target) |
|
46 | 1 | elif fileformat == 'csv': |
|
47 | 1 | return self.__dump_csv( |
|
48 | data, |
||
49 | target, |
||
50 | delimiter=csv_delimiter, |
||
51 | quotechar=csv_quote, |
||
52 | eol=csv_eol |
||
53 | ) |
||
54 | else: |
||
55 | 1 | return self.__dump_json(data, target, namespace) |
|
56 | |||
57 | 1 | def __create_infos_to_dump(self, infos, namespace=None): |
|
0 ignored issues
–
show
This method could be written as a function/class method.
If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example class Foo:
def some_method(self, x, y):
return x + y;
could be written as class Foo:
@classmethod
def some_method(cls, x, y):
return x + y;
![]() |
|||
58 | ''' |
||
59 | reorganize data with a namespace if necessary |
||
60 | ''' |
||
61 | |||
62 | 1 | to_dump = infos |
|
63 | 1 | if namespace is not None and namespace != '': |
|
64 | 1 | namespaces = namespace.split('.') |
|
65 | 1 | namespaces.reverse() |
|
66 | 1 | for name in namespaces: |
|
67 | 1 | to_dump = {name: to_dump} |
|
68 | |||
69 | 1 | return to_dump |
|
70 | |||
71 | 1 | def __dump_sh(self, data, target): |
|
0 ignored issues
–
show
This method could be written as a function/class method.
If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example class Foo:
def some_method(self, x, y):
return x + y;
could be written as class Foo:
@classmethod
def some_method(cls, x, y):
return x + y;
![]() |
|||
72 | ''' |
||
73 | dump to Shell variables file |
||
74 | ''' |
||
75 | |||
76 | 1 | target = target + '.sh' |
|
77 | |||
78 | 1 | with open(target, 'w', encoding='utf-8') as fpt: |
|
79 | 1 | for key, val in data.items(): |
|
80 | 1 | fpt.write("{}=\"{}\"\n".format(key, tools.flatten(val))) |
|
81 | |||
82 | 1 | return target |
|
83 | |||
84 | 1 | def __dump_csv(self, data, target, delimiter=',', quotechar='"', eol='lf'): |
|
0 ignored issues
–
show
This method could be written as a function/class method.
If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example class Foo:
def some_method(self, x, y):
return x + y;
could be written as class Foo:
@classmethod
def some_method(cls, x, y):
return x + y;
![]() |
|||
85 | ''' |
||
86 | dump to CSV file (comma separated values) |
||
87 | ''' |
||
88 | |||
89 | 1 | target = target + '.csv' |
|
90 | |||
91 | 1 | eol = '\r\n' if eol == 'crlf' or eol == '\r\n' else '\n' |
|
92 | |||
93 | 1 | with open(target, 'w', encoding='utf-8') as fpt: |
|
94 | 1 | writer = csv.writer( |
|
95 | fpt, |
||
96 | delimiter=delimiter, |
||
97 | lineterminator=eol, |
||
98 | quotechar=quotechar, |
||
99 | quoting=csv.QUOTE_MINIMAL |
||
100 | ) |
||
101 | 1 | for key, val in data.items(): |
|
102 | 1 | writer.writerow((key, tools.flatten(val))) |
|
103 | |||
104 | 1 | return target |
|
105 | |||
106 | 1 | def __dump_ini(self, data, target, namespace=None): |
|
0 ignored issues
–
show
This method could be written as a function/class method.
If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example class Foo:
def some_method(self, x, y):
return x + y;
could be written as class Foo:
@classmethod
def some_method(cls, x, y):
return x + y;
![]() |
|||
107 | ''' |
||
108 | dump to INI file |
||
109 | ''' |
||
110 | |||
111 | 1 | target = target + '.ini' |
|
112 | |||
113 | 1 | if namespace is None or namespace == '': |
|
114 | 1 | namespace = 'app_version' |
|
115 | |||
116 | 1 | ini = configparser.RawConfigParser() |
|
117 | 1 | ini.add_section(namespace) |
|
118 | |||
119 | 1 | for key, val in data.items(): |
|
120 | 1 | ini.set(namespace, key, tools.flatten(val)) |
|
121 | |||
122 | 1 | with open(target, 'w', encoding='utf-8') as fpt: |
|
123 | 1 | ini.write(fpt) |
|
124 | |||
125 | 1 | return target |
|
126 | |||
127 | 1 | def __dump_xml(self, data, target, namespace=None): |
|
128 | ''' |
||
129 | dump to XML file |
||
130 | ''' |
||
131 | |||
132 | 1 | target = target + '.xml' |
|
133 | 1 | if namespace is None or namespace == '': |
|
134 | 1 | namespace = 'app_version' |
|
135 | |||
136 | 1 | with open(target, 'w', encoding='utf-8') as fpt: |
|
137 | 1 | xml = xmltodict.unparse( |
|
138 | self.__create_infos_to_dump(data, namespace), |
||
139 | encoding='utf-8', |
||
140 | pretty=True, |
||
141 | indent=' ' |
||
142 | ) |
||
143 | |||
144 | 1 | fpt.write(xml) |
|
145 | |||
146 | 1 | return target |
|
147 | |||
148 | 1 | def __dump_json(self, data, target, namespace=None): |
|
149 | ''' |
||
150 | dump to JSON file |
||
151 | ''' |
||
152 | |||
153 | 1 | target = target + '.json' |
|
154 | |||
155 | 1 | data1 = self.__create_infos_to_dump(data, namespace) |
|
156 | |||
157 | 1 | with open(target, 'w', encoding='utf-8') as fpt: |
|
158 | 1 | fpt.write(json.dumps(data1, indent=2, ensure_ascii=False)) |
|
159 | |||
160 | 1 | return target |
|
161 | |||
162 | 1 | def __dump_yaml(self, data, target, namespace=None): |
|
163 | ''' |
||
164 | dump to YAML file |
||
165 | ''' |
||
166 | |||
167 | 1 | target = target + '.yml' |
|
168 | |||
169 | 1 | with open(target, 'w', encoding='utf-8') as fpt: |
|
170 | 1 | if not data: |
|
171 | 1 | fpt.write("---\n") |
|
172 | else: |
||
173 | 1 | yaml.safe_dump( |
|
174 | self.__create_infos_to_dump(data, namespace), |
||
175 | fpt, |
||
176 | default_flow_style=False, |
||
177 | explicit_start=True, |
||
178 | allow_unicode=True, |
||
179 | # force quoting |
||
180 | # to prevent abbrev_commit to be read as a float |
||
181 | default_style='\'' |
||
182 | ) |
||
183 | |||
184 | return target |
||
185 |
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.