1313
1414
1515class 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
0 commit comments