Skip to content

Commit 6481de7

Browse files
authored
Merge pull request #6 from ongres/ndjson_implementation
fix: ndjson file escaped
2 parents 299284a + c0bfc52 commit 6481de7

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

artifact_builder/__main__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import json
44
import ndjson
55
import argparse
6-
6+
from re import sub, escape, MULTILINE
7+
from os import remove
78

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

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

44+
with open(args.output_ndjson, 'w+', encoding='utf-8-sig') as f:
45+
input = open(args.output_ndjson + '.temp')
46+
f.write(sub(r'\\',r'\\\\',input.read()))
47+
input.close()
48+
49+
remove(args.output_ndjson + '.temp')
50+
3851
if __name__ == "__main__":
3952
main()
4053

artifact_builder/indexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def indexDir(sqlDirectory: str, _engine: str) -> fileMap:
1818

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

0 commit comments

Comments
 (0)