-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[refactor] Use the csv library to generate properly escaped TSV files #116
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import csv | ||
from wireviz import wv_helper | ||
from wireviz.wv_helper import open_file_write | ||
|
||
EXCEL_CSV = csv.excel | ||
EXCEL_TSV = csv.excel_tab | ||
UNIX_CSV = csv.unix_dialect | ||
WIREVIZ_TSV = type('Wireviz BOM', (csv.Dialect, object), dict( | ||
delimiter='\t', | ||
doublequote=True, | ||
escapechar=None, | ||
lineterminator='\n', | ||
quoting=0, | ||
skipinitialspace=False, | ||
strict=False, | ||
quotechar='"' | ||
)) | ||
csv.register_dialect('Wireviz BOM', WIREVIZ_TSV) | ||
|
||
_csv_formats = { EXCEL_CSV, UNIX_CSV } | ||
_tsv_formats = { EXCEL_TSV, WIREVIZ_TSV } | ||
|
||
_csv_ext = '.bom.csv' | ||
_tsv_ext = '.bom.tsv' | ||
|
||
def generate_bom_outputs(base_filename, bomdata, *argv): | ||
expanded_csv_names = len(_csv_formats.intersection(set(argv))) > 1 | ||
expanded_tsv_names = len(_tsv_formats.intersection(set(argv))) > 1 | ||
for fmt in argv: | ||
if fmt in _csv_formats: | ||
file = csv.writer(open_file_write(base_filename + ("_" + fmt.__name__ if expanded_csv_names else "") + _csv_ext, fmt.lineterminator), fmt) | ||
|
||
elif fmt in _tsv_formats: | ||
file = csv.writer(open_file_write(base_filename + ("_"+fmt.__name__ if expanded_tsv_names else "") + _tsv_ext, fmt.lineterminator), fmt) | ||
else: | ||
raise KeyError("Unknown BOM Format Specified") | ||
file.writerows(wv_helper.flatten2d(bomdata)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The My proposal: Call the function with a list as the third argument:
and then iterate over the list.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That could work well! I will also check briefly to see if the Excel TSV format is essentially compatible with the one wireviz natively generates (and probably have the OS choose if new lines are unix or windows style), and then the csv formats can be directly passed through. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, excuse any python style issues, I will try to fix them whenever pointed out. I'm a Java / C / MATLAB developer, primarily, who has wanted to learn python for far too long! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You're not alone, I've done mostly embedded programming in C++ / Arduino before. |
||
|
||
# TODO: Possibly refactor other BOM output operations, such as HTML, into here? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but in a separate PR :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the discussion in #123 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood, leaving comment for future reference, unless requested otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment in
bom_helper.py
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood, leaving comment for future reference, unless requested otherwise.