@@ -86,22 +86,37 @@ def action_glossary(
8686
8787
8888def action_glossary_create (
89- translator : deepl .Translator , entry_list , file , ** kwargs
89+ translator : deepl .Translator , entry_list , file , csv , ** kwargs
9090):
91+ term_separator = None
9192 if file :
9293 if entry_list :
9394 raise deepl .DeepLException (
9495 "The --file argument cannot be used together with "
9596 "command-line entries"
9697 )
97- file_contents = pathlib .Path (file ).read_text ("UTF-8" )
98- entry_dict = deepl .convert_tsv_to_dict (file_contents )
98+ content = pathlib .Path (file ).read_text ("UTF-8" )
9999 elif entry_list and entry_list [0 ] == "-" :
100- entry_dict = deepl . convert_tsv_to_dict ( sys .stdin .read () )
100+ content = sys .stdin .read ()
101101 else :
102- entry_dict = deepl .convert_tsv_to_dict ("\n " .join (entry_list ), "=" )
102+ content = "\n " .join (entry_list )
103+ term_separator = "="
104+ if csv :
105+ raise Exception (
106+ "csv option is not compatible with command-line entries"
107+ )
108+
109+ if csv :
110+ glossary = translator .create_glossary_from_csv (
111+ csv_data = content , ** kwargs
112+ )
113+ else :
114+ if term_separator :
115+ entry_dict = deepl .convert_tsv_to_dict (content , term_separator )
116+ else :
117+ entry_dict = deepl .convert_tsv_to_dict (content )
118+ glossary = translator .create_glossary (entries = entry_dict , ** kwargs )
103119
104- glossary = translator .create_glossary (entries = entry_dict , ** kwargs )
105120 print (f"Created { glossary } " )
106121 print_glossaries ([glossary ])
107122
@@ -367,8 +382,8 @@ def add_common_arguments(subparser: argparse.ArgumentParser):
367382 parser_glossary_create = glossary_subparsers .add_parser (
368383 "create" ,
369384 help = "create a new glossary" ,
370- description = "create a new glossary using entries specified in "
371- "a TSV file , standard-input, or provided via command-line " ,
385+ description = "create a new glossary using entries provided via command- "
386+ "line , standard-input, or specified in a TSV or CSV file " ,
372387 )
373388 parser_glossary_create .add_argument (
374389 "--name" , required = True , help = "name to be associated with glossary."
@@ -393,17 +408,25 @@ def add_common_arguments(subparser: argparse.ArgumentParser):
393408 type = str ,
394409 metavar = "SOURCE=TARGET" ,
395410 help = "one or more entries to add to glossary, may be repeated. "
396- 'Alternatively, use "-" to read entries from standard-input in TSV '
397- "format (see --file argument). These arguments cannot be used "
398- "together with the --file argument." ,
411+ 'Alternatively, use "-" to read entries from standard-input in TSV or '
412+ "CSV format (see --file argument for formatting information). These "
413+ "arguments cannot be used together with the --file argument." ,
399414 )
400415 parser_glossary_create .add_argument (
401416 "--file" ,
402417 type = str ,
403- help = "file to read glossary entries from. File must be in "
404- "tab-separated values (TSV) format: one entry-pair per line, each "
405- "line contains the source entry, a tab, then the target entry. Empty "
406- "lines are ignored." ,
418+ help = "file to read glossary entries from. Unless --csv is specified, "
419+ "file format is expected to be tab-separated values (TSV) format: one "
420+ "entry-pair per line, each line contains the source entry, a tab, "
421+ "then the target entry. Empty lines are ignored." ,
422+ )
423+ parser_glossary_create .add_argument (
424+ "--csv" ,
425+ action = "store_true" ,
426+ help = "the provided --file option or standard-input should be "
427+ "interpreted as a CSV file. Information about the expected CSV format "
428+ "can be found in the API documentation: "
429+ "https://www.deepl.com/docs-api/managing-glossaries/supported-glossary-formats/." , # noqa
407430 )
408431
409432 parser_glossary_list = glossary_subparsers .add_parser (
0 commit comments