-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_new_scheme.py
113 lines (93 loc) · 3.31 KB
/
create_new_scheme.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import argparse
import os
import glob
import yaml
from config import get_cfg_defaults
from pathlib import Path
from shutil import copy
from datetime import datetime
from textwrap import dedent, wrap
from argparse import RawTextHelpFormatter
def get_parser():
parser = argparse.ArgumentParser(
description="\n".join(wrap(dedent("""\
Makes a new registration scheme based on a previously created scheme.
If no name is specified the name of the new scheme will be the same as the template scheme,
but with an appended datetime suffix. This will copy both the config file and the scheme folder.
Default storage locations will be used."""), 80, expand_tabs=False)),
epilog=dedent('''\
Example usage:
To create a copy of the translation scheme named translationCopy we can call
python create_new_scheme.py --config ./configs/translation.yaml -n translationCopy
'''),
formatter_class=RawTextHelpFormatter
)
parser.add_argument(
"-c",
"--config",
default="./configs/translationDeform.yaml",
metavar="FILE",
help="path to config file",
type=str,
)
parser.add_argument(
"-n", "--name", help="Name of new scheme.", default=None, type=str
)
parser.add_argument(
"opts",
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER,
)
return parser
if __name__ == "__main__":
args = get_parser().parse_args()
cfg = get_cfg_defaults()
cfg.merge_from_file(args.config)
cfg.merge_from_list(args.opts)
cfg.freeze()
reg_folder = (
Path(cfg.BASE_DIR)
/ Path(cfg.REGISTRATION.BASE_DIR)
/ Path(cfg.REGISTRATION.SCHEME)
)
with open(os.path.join(reg_folder, "scheme.yaml"), "r") as f:
scheme = yaml.load(f, Loader=yaml.FullLoader)
with open(args.config, "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
# created_schemes = [scheme_name.lower().replace('.yaml', '') for scheme_name in
# os.listdir(cfg.REGISTRATION.BASE_DIR) + os.listdir('./configs')]
prev_scheme = scheme["Name"]
time = datetime.now().strftime("%y%m%d-%H%M")
scheme["Last updated"] = time
if not (args.name):
try:
valid_check = datetime.strptime(
config["REGISTRATION"]["SCHEME"][-11:], "%y%m%d-%H%M"
)
config["REGISTRATION"]["SCHEME"] = (
config["REGISTRATION"]["SCHEME"][:-11] + time
)
except:
config["REGISTRATION"]["SCHEME"] += time
else:
config["REGISTRATION"]["SCHEME"] = args.name
# Write config
with open(
Path(cfg.BASE_DIR)
/ Path("configs")
/ Path(config["REGISTRATION"]["SCHEME"] + ".yaml"),
"w",
) as f:
yaml.dump(config, f)
new_reg_folder = (
Path(cfg.BASE_DIR)
/ Path(cfg.REGISTRATION.BASE_DIR)
/ Path(config["REGISTRATION"]["SCHEME"])
)
os.makedirs(new_reg_folder)
with open(new_reg_folder / Path("scheme.yaml"), "w") as f:
yaml.dump(scheme, f)
for file in scheme["Scheme"]:
copy(reg_folder / Path(file), new_reg_folder / Path(file))
print(f'New scheme written to {config["REGISTRATION"]["SCHEME"] + ".yaml"}')