Skip to content

Commit

Permalink
Merge pull request #6 from ongres/ndjson_implementation
Browse files Browse the repository at this point in the history
fix: ndjson file escaped
  • Loading branch information
3manuek authored Jun 13, 2023
2 parents 299284a + c0bfc52 commit 6481de7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions artifact_builder/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import json
import ndjson
import argparse

from re import sub, escape, MULTILINE
from os import remove

sqlDirectory = "../sql/"
global _engine
Expand All @@ -30,11 +31,23 @@ def main():
with open(args.output, 'rb') as f:
data = json.load(f)

with open(args.output_ndjson, 'w+', encoding='utf-8-sig') as f:
"""
Next blocks are doing a nasty thing. They dump into a temporal file the contents of
the data dictionary for escaping the escape character later. This generates an ndjson
compatible with Postgres COPY.
"""
with open(args.output_ndjson + '.temp', 'w+', encoding='utf-8-sig') as f:
writer = ndjson.writer(f)
for key in data:
writer.writerow(data[key])

with open(args.output_ndjson, 'w+', encoding='utf-8-sig') as f:
input = open(args.output_ndjson + '.temp')
f.write(sub(r'\\',r'\\\\',input.read()))
input.close()

remove(args.output_ndjson + '.temp')

if __name__ == "__main__":
main()

2 changes: 1 addition & 1 deletion artifact_builder/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def indexDir(sqlDirectory: str, _engine: str) -> fileMap:

# For now, we ignore READMEs. But, we might furtherly include some documentation
# artifact.
if key not in _fileMap and filename.removesuffix(".md").lower() != 'readme':
if key not in _fileMap and filename.removesuffix(".md").lower() not in ('readme', '.gitkeep'):
_fileMap[key]={'engine': _engine}
_fileMap[key]={'title': sub('[_-]'," ", str(key)).capitalize()}

Expand Down

0 comments on commit 6481de7

Please sign in to comment.