source: other-projects/bib-stinky/trunk/doi-stinky/util.py@ 36331

Last change on this file since 36331 was 36331, checked in by davidb, 21 months ago

Code tidy up, and refactoring to have a saveJSON function

File size: 1.0 KB
Line 
1from __future__ import print_function
2
3import json
4import re
5import sys
6
7def eprint(*args, **kwargs):
8 print(*args, file=sys.stderr, **kwargs)
9
10
11def readKey(filename):
12
13 try:
14 # read with auto-close
15 with open(filename,"r") as fin:
16 lines = fin.readlines()
17
18 except FileNotFoundError as e:
19 eprint("Failed to find '"+filename+"'")
20 eprint("Have you created your own version of this file based on " + filename + ".in ?")
21 return ""
22
23 key = ""
24 for line in lines:
25 if not re.match(r'^(#.*)|(\s*)$',line):
26 line_stripped = line.strip().rstrip();
27 key = key + line_stripped
28
29 return key
30
31def writeJSON(json_output_filename,returned_json):
32
33 returned_json_prettyprint = json.dumps(returned_json, indent=2, ensure_ascii=False)
34
35 print("Saving returned JSON output to: " + json_output_filename)
36
37 # write output with auto-close file handle
38 with open(json_output_filename, 'w', encoding="utf8") as fout:
39 fout.write(returned_json_prettyprint)
Note: See TracBrowser for help on using the repository browser.