-
Notifications
You must be signed in to change notification settings - Fork 0
/
createFile.py
executable file
·56 lines (44 loc) · 1.92 KB
/
createFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3
# @todo add pytest
import json
import re
import subprocess
from pathlib import Path
import argparse
import logging
logger = logging.getLogger(__name__)
def get_supported_langs() -> [str]:
return [f.stem for f in Path("languages").glob("*") if f.is_file()]
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--force", action="store_true", help="Overwrite existing files.")
parser.add_argument('-l', '--language', default='cpp',
help='The language to use: %s. default: cpp' % "|".join(get_supported_langs()))
parser.add_argument("-r", "--root", default=".",
help="the root directory to store generated code files. default: '.'")
# parser.add_argument("params", help="The json data from bookmarklet")
args = parser.parse_args()
# import languages specific render
try:
language = __import__("languages.%s" % args.language, fromlist=["languages"])
except ImportError as e:
logger.fatal("not supported language: %s" % args.language)
logger.error(e.msg)
exit(1)
# read from clipboard
result = subprocess.run("xclip -selection clipboard -o", stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
params = json.loads(result.stdout)
# cleaning first
# params = json.loads(args.params)
params["code"] = re.sub(r"\xa0", " ", params["code"]) # replace to ' '
render = language.Render(params)
filepath = Path(args.root) / render.get_file_path()
if not args.force and filepath.exists():
logger.fatal("destination(%s) exists. use -f to overwrite." % filepath)
exit(1)
# create dir(Easy/Medium/Hard) to store generated code file
filepath.parent.mkdir(exist_ok=True)
with open(filepath, "w") as f:
f.write(render.render())
print("written to file %s" % filepath)
render.post_render(args.root)