Skip to content

Commit fd14074

Browse files
authored
Merge pull request #270 from kobotoolbox/268-black
Apply `black` formatting
2 parents 611735b + c418c5d commit fd14074

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3529
-2719
lines changed

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[tool.black]
2+
verbose = true
3+
line-length = 80
4+
skip-string-normalization = true

src/formpack/__main__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ def print_xls(filename, expand=False, flatten=False, xml=False):
3030
if xml:
3131
print(FormPack({'content': content}, **settings)[0].to_xml())
3232
else:
33-
print(json.dumps(content,
34-
indent=2))
33+
print(json.dumps(content, indent=2))
3534
except EnvironmentError as e:
36-
sys.exit('error trying to read input as xls file? {}: {}'.format(
37-
filename, e))
35+
sys.exit(
36+
'error trying to read input as xls file? {}: {}'.format(filename, e)
37+
)
3838

3939

4040
@begin.subcommand

src/formpack/errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
34
class TranslationError(ValueError):
45
pass
56

src/formpack/pack.py

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313

1414

1515
class FormPack:
16-
17-
def __init__(self, versions=None, title='Submissions', id_string=None,
18-
default_version_id_key='__version__',
19-
strict_schema=False,
20-
root_node_name='data',
21-
asset_type=None, submissions_xml=None):
16+
def __init__(
17+
self,
18+
versions=None,
19+
title='Submissions',
20+
id_string=None,
21+
default_version_id_key='__version__',
22+
strict_schema=False,
23+
root_node_name='data',
24+
asset_type=None,
25+
submissions_xml=None,
26+
):
2227
"""
2328
2429
@@ -54,7 +59,7 @@ def __init__(self, versions=None, title='Submissions', id_string=None,
5459
# FIXME: Find a safe way to use this. Wrapping with try/except isn't enough
5560
# to fix https://github.com/kobotoolbox/formpack/issues/150
5661
#
57-
#def __repr__(self):
62+
# def __repr__(self):
5863
# return '<FormPack %s>' % self._stats()
5964

6065
def version_id_keys(self, _versions=None):
@@ -101,8 +106,9 @@ def _stats(self):
101106
_stats['versions'] = len(self.versions)
102107
# _stats['submissions'] = self.submissions_count()
103108
if self.versions:
104-
_stats['row_count'] = len(self[-1].schema.get('content', {})
105-
.get('survey', []))
109+
_stats['row_count'] = len(
110+
self[-1].schema.get('content', {}).get('survey', [])
111+
)
106112
# returns stats in the format [ key="value" ]
107113
return '\n\t'.join('%s="%s"' % item for item in _stats.items())
108114

@@ -111,20 +117,20 @@ def load_all_versions(self, versions):
111117
self.load_version(deepcopy(schema))
112118

113119
def load_version(self, schema):
114-
""" Load one version and attach it to this Formpack
120+
"""Load one version and attach it to this Formpack
115121
116-
All the metadata parsing is delegated to the FormVersion class,
117-
hence several attributes for FormPack are populated on the fly
118-
while getting versions loaded:
122+
All the metadata parsing is delegated to the FormVersion class,
123+
hence several attributes for FormPack are populated on the fly
124+
while getting versions loaded:
119125
120-
- title : the human readable name of the form. Match the one
121-
from the most recent version.
122-
- id_string : the human readable id of the form. The same for
123-
all versions of the same FormPack.
126+
- title : the human readable name of the form. Match the one
127+
from the most recent version.
128+
- id_string : the human readable id of the form. The same for
129+
all versions of the same FormPack.
124130
125-
Each version can be distinguish by its version_id, which is
126-
unique accross an entire FormPack. It can be None, but only for
127-
one version in the FormPack.
131+
Each version can be distinguish by its version_id, which is
132+
unique accross an entire FormPack. It can be None, but only for
133+
one version in the FormPack.
128134
"""
129135
replace_aliases(schema['content'], in_place=True)
130136
expand_content(schema['content'], in_place=True)
@@ -140,20 +146,27 @@ def load_version(self, schema):
140146
# Avoid duplicate versions id
141147
if form_version.id in self.versions:
142148
if form_version.id is None:
143-
raise ValueError('cannot have two versions without '
144-
'a "version" id specified')
149+
raise ValueError(
150+
'cannot have two versions without '
151+
'a "version" id specified'
152+
)
145153

146-
raise ValueError('cannot have duplicate version id: %s'
147-
% form_version.id)
154+
raise ValueError(
155+
'cannot have duplicate version id: %s' % form_version.id
156+
)
148157

149158
# If the form pack doesn't have an id_string, we get it from the
150159
# first form version. We also avoid heterogenenous id_string in versions
151160
if form_version.id_string:
152161
if self.id_string and self.id_string != form_version.id_string:
153-
raise ValueError('Versions must of the same form must '
154-
'share an id_string: %s != %s' % (
155-
self.id_string, form_version.id_string,
156-
))
162+
raise ValueError(
163+
'Versions must of the same form must '
164+
'share an id_string: %s != %s'
165+
% (
166+
self.id_string,
167+
form_version.id_string,
168+
)
169+
)
157170

158171
self.id_string = form_version.id_string
159172

@@ -169,16 +182,16 @@ def version_diff(self, vn1, vn2):
169182
v2 = self.versions[vn2]
170183

171184
def summr(v):
172-
return json.dumps(v.schema.get('content'),
173-
indent=4,
174-
sort_keys=True,
175-
).splitlines(1)
185+
return json.dumps(
186+
v.schema.get('content'),
187+
indent=4,
188+
sort_keys=True,
189+
).splitlines(1)
190+
176191
out = []
177-
for line in difflib.unified_diff(summr(v1),
178-
summr(v2),
179-
fromfile=vn1,
180-
tofile=vn2,
181-
n=1):
192+
for line in difflib.unified_diff(
193+
summr(v1), summr(v2), fromfile=vn1, tofile=vn2, n=1
194+
):
182195
out.append(line)
183196
return ''.join(out)
184197

@@ -268,12 +281,15 @@ def get_fields_for_versions(self, versions=-1, data_types=None):
268281
if not isinstance(field_object, CopyField):
269282
if section_field_name in positions:
270283
position = positions[section_field_name]
271-
latest_field_object = tmp2d[position[0]][position[1]]
284+
latest_field_object = tmp2d[position[0]][
285+
position[1]
286+
]
272287
# Because versions_desc are ordered from latest to oldest,
273288
# we use current field object as the old one and the one already
274289
# in position as the latest one.
275290
new_object = self._combine_field_choices(
276-
field_object, latest_field_object)
291+
field_object, latest_field_object
292+
)
277293
tmp2d[position[0]][position[1]] = new_object
278294
else:
279295
try:
@@ -286,7 +302,10 @@ def get_fields_for_versions(self, versions=-1, data_types=None):
286302
# it can happen when current version has more items than newest one.
287303
index = len(tmp2d) - 1
288304

289-
positions[section_field_name] = (index, len(tmp2d[index]) - 1)
305+
positions[section_field_name] = (
306+
index,
307+
len(tmp2d[index]) - 1,
308+
)
290309

291310
index += 1
292311

@@ -367,7 +386,7 @@ def export(
367386
tag_cols_for_header=tag_cols_for_header,
368387
filter_fields=filter_fields,
369388
xls_types_as_text=xls_types_as_text,
370-
include_media_url=include_media_url
389+
include_media_url=include_media_url,
371390
)
372391

373392
def autoreport(self, versions=-1):
@@ -381,7 +400,7 @@ def _get_versions(self, versions):
381400
if versions is None:
382401
versions = -1
383402

384-
if isinstance(versions, (str, int,)):
403+
if isinstance(versions, (str, int)):
385404
versions = [versions]
386405
versions = [self[key] for key in versions]
387406

src/formpack/reporting/autoreport.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99

1010
class AutoReportStats:
11-
12-
def __init__(self, autoreport, stats, submissions_count,
13-
submission_counts_by_version):
11+
def __init__(
12+
self, autoreport, stats, submissions_count, submission_counts_by_version
13+
):
1414
self.autoreport = autoreport
1515
self.stats = stats
1616
self.submissions_count = submissions_count
@@ -21,7 +21,6 @@ def __iter__(self):
2121

2222

2323
class AutoReport:
24-
2524
def __init__(self, formpack, form_versions):
2625
self.formpack = formpack
2726
self.versions = form_versions
@@ -91,14 +90,22 @@ def _calculate_stats(self, submissions, fields, versions, lang):
9190

9291
def stats_generator():
9392
for field in fields:
94-
yield (field,
95-
field.get_labels(lang)[0],
96-
field.get_stats(metrics[field.name], lang=lang))
97-
98-
return AutoReportStats(self, stats_generator(), submissions_count,
99-
submission_counts_by_version)
93+
yield (
94+
field,
95+
field.get_labels(lang)[0],
96+
field.get_stats(metrics[field.name], lang=lang),
97+
)
98+
99+
return AutoReportStats(
100+
self,
101+
stats_generator(),
102+
submissions_count,
103+
submission_counts_by_version,
104+
)
100105

101-
def _disaggregate_stats(self, submissions, fields, versions, lang, split_by_field):
106+
def _disaggregate_stats(
107+
self, submissions, fields, versions, lang, split_by_field
108+
):
102109

103110
# We want only the most used values so we build a separate counter
104111
# for it to filter them
@@ -168,7 +175,7 @@ def _disaggregate_stats(self, submissions, fields, versions, lang, split_by_fiel
168175
if splitter is not None:
169176
values = split_by_field.parse_values(splitter)
170177
else:
171-
values = (None, )
178+
values = (None,)
172179

173180
splitters_rank.update(values)
174181

@@ -191,14 +198,25 @@ def _disaggregate_stats(self, submissions, fields, versions, lang, split_by_fiel
191198

192199
def stats_generator():
193200
for field in fields:
194-
stats = field.get_disaggregated_stats(metrics[field.name], lang=lang,
195-
top_splitters=top_splitters)
201+
stats = field.get_disaggregated_stats(
202+
metrics[field.name], lang=lang, top_splitters=top_splitters
203+
)
196204
yield (field, field.get_labels(lang)[0], stats)
197205

198-
return AutoReportStats(self, stats_generator(), submissions_count,
199-
submission_counts_by_version)
206+
return AutoReportStats(
207+
self,
208+
stats_generator(),
209+
submissions_count,
210+
submission_counts_by_version,
211+
)
200212

201-
def get_stats(self, submissions, fields=(), lang=UNSPECIFIED_TRANSLATION, split_by=None):
213+
def get_stats(
214+
self,
215+
submissions,
216+
fields=(),
217+
lang=UNSPECIFIED_TRANSLATION,
218+
split_by=None,
219+
):
202220

203221
all_fields = self.formpack.get_fields_for_versions(self.versions)
204222
all_fields = [field for field in all_fields if field.has_stats]
@@ -214,10 +232,12 @@ def get_stats(self, submissions, fields=(), lang=UNSPECIFIED_TRANSLATION, split_
214232
try:
215233
split_by_field = next(f for f in fields if f.name == split_by)
216234
except StopIteration:
217-
raise ValueError('No field matching name "%s" '
218-
'for split_by' % split_by)
235+
raise ValueError(
236+
'No field matching name "%s" ' 'for split_by' % split_by
237+
)
219238

220-
return self._disaggregate_stats(submissions, fields,
221-
self.versions, lang, split_by_field)
239+
return self._disaggregate_stats(
240+
submissions, fields, self.versions, lang, split_by_field
241+
)
222242

223243
return self._calculate_stats(submissions, fields, self.versions, lang)

0 commit comments

Comments
 (0)