1
1
import os
2
+ import io
2
3
import sys
3
4
import logging
4
5
import click
7
8
from json_flattener import flatten_to_csv , unflatten_from_csv , GlobalConfig , KeyConfig , Serializer
8
9
9
10
10
- def _get_format (input : str , input_format : str = None ) -> str :
11
+ def _get_format (input : str , input_format : str = None , default_format : str = None ) -> str :
11
12
if input_format is None :
13
+ if input is None :
14
+ if default_format is not None :
15
+ return default_format
16
+ else :
17
+ raise Exception (f'Must pass input file or default format' )
12
18
_ , ext = os .path .splitext (input )
13
19
if ext is not None :
14
20
input_format = ext .replace ('.' , '' )
15
21
else :
16
- raise Exception (f'Must pass format option OR use known file suffix: { input } ' )
22
+ if default_format is not None :
23
+ return default_format
24
+ else :
25
+ raise Exception (f'Must pass format option OR use known file suffix: { input } ' )
17
26
return input_format .lower ()
18
27
19
28
def _is_xsv (fmt : str ) -> bool :
@@ -78,7 +87,7 @@ def _get_config(serializer: str = 'json', serialized_keys = [], multivalued_keys
78
87
"-o" , "--output" , help = "Output file, e.g. a SSSOM tsv file."
79
88
)
80
89
output_format_option = click .option (
81
- "-O " ,
90
+ "-t " ,
82
91
"--output-format" ,
83
92
help = f'Desired output format, e.g. { "," .join (FORMATS )} ' ,
84
93
)
@@ -119,6 +128,16 @@ def _get_config(serializer: str = 'json', serialized_keys = [], multivalued_keys
119
128
multiple = True ,
120
129
help = "Key configuration. Must be of form KEY={yaml,json,flat,multivalued}*" ,
121
130
)
131
+ load_config_option = click .option (
132
+ "-c" ,
133
+ "--load-config" ,
134
+ help = "Path to global configuration file to be loaded" ,
135
+ )
136
+ save_config_option = click .option (
137
+ "-O" ,
138
+ "--save-config" ,
139
+ help = "Path to global configuration file to be saved" ,
140
+ )
122
141
@click .group ()
123
142
@click .option ("-v" , "--verbose" , count = True )
124
143
@click .option ("-q" , "--quiet" )
@@ -154,9 +173,12 @@ def main(verbose: int, quiet: bool):
154
173
@serializer_option
155
174
@serialized_keys_option
156
175
@config_option
176
+ @load_config_option
177
+ @save_config_option
157
178
@key_option
158
179
def flatten (input : str , output : str , input_format : str , output_format : str , key : str ,
159
180
serializer : str , serialized_keys = [], multivalued_keys = [], flatten_keys = [],
181
+ save_config : str = None , load_config : str = None ,
160
182
config_key = []):
161
183
"""Flatten a file to TSV/CSV
162
184
@@ -197,6 +219,9 @@ def flatten(input: str, output: str, input_format: str, output_format: str, key:
197
219
logging .debug (f'CONFIG={ config } ' )
198
220
with open (output , 'w' ) as stream :
199
221
flatten_to_csv (objs , stream , config = config )
222
+ if save_config is not None :
223
+ with open (save_config , 'w' ) as stream :
224
+ yaml .dump (config .as_dict (), stream )
200
225
201
226
202
227
@main .command ()
@@ -209,23 +234,29 @@ def flatten(input: str, output: str, input_format: str, output_format: str, key:
209
234
@serializer_option
210
235
@serialized_keys_option
211
236
@config_option
237
+ @load_config_option
212
238
@key_option
213
239
def unflatten (input : str , output : str , input_format : str , output_format : str , key : str ,
214
- serializer : str , serialized_keys = [], multivalued_keys = [], flatten_keys = [],
215
- config_key = []):
240
+ serializer : str , serialized_keys = [], multivalued_keys = [], flatten_keys = [],
241
+ load_config : str = None ,
242
+ config_key = []):
216
243
"""Unflatten a file from TSV/CSV
217
244
218
245
Example:
219
246
jfl unflatten --input my.tsv --output my.yaml
220
247
221
248
"""
222
249
input_format = _get_format (input , input_format )
223
- output_format = _get_format (output , output_format )
250
+ output_format = _get_format (output , output_format , 'json' )
224
251
config = _get_config (serializer = serializer ,
225
252
serialized_keys = serialized_keys ,
226
253
multivalued_keys = multivalued_keys ,
227
254
flatten_keys = flatten_keys ,
228
255
config_keys = config_key )
256
+ if load_config is not None :
257
+ with open (load_config ) as stream :
258
+ config = GlobalConfig .from_dict (** yaml .safe_load (stream ))
259
+ logging .debug (f'CONFIG={ config } ' )
229
260
with open (input ) as stream :
230
261
if input_format == 'tsv' :
231
262
sep = '\t '
@@ -246,5 +277,6 @@ def unflatten(input: str, output: str, input_format: str, output_format: str, ke
246
277
else :
247
278
json .dump (obj , stream )
248
279
280
+
249
281
if __name__ == "__main__" :
250
282
main ()
0 commit comments