You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.9 KiB
99 lines
2.9 KiB
from config import ROOT
|
|
import copy
|
|
import json
|
|
import os
|
|
import shutil
|
|
|
|
class ArgumentHandler:
|
|
def __init__(self,args_file):
|
|
|
|
self.args_file = args_file
|
|
self.exts = set()
|
|
self.reload()
|
|
|
|
for _ext in self.data['exts']:
|
|
|
|
ext = _ext['ext']
|
|
self.exts.add(ext)
|
|
args = _ext['opts']
|
|
self.set(ext,ArgumentContainer(args,self.data['prefix_char']))
|
|
|
|
for key,value in self.data.items():
|
|
if key != 'exts' and isinstance(value,list):
|
|
self.set(key,ArgumentContainer(value,self.data['prefix_char']))
|
|
|
|
self.prefix_char = self.data['prefix_char']
|
|
|
|
def get(self,item):
|
|
return self.__dict__[item]
|
|
|
|
def set(self,item,value):
|
|
self.__dict__[item] = value
|
|
|
|
def add_ext(self,ext):
|
|
self.set(ext,ArgumentContainer([]))
|
|
self.exts.add(ext)
|
|
|
|
def reload(self):
|
|
with open(self.args_file) as file:
|
|
self.data = json.load(file)
|
|
|
|
def save(self):
|
|
data = []
|
|
for ext in self.exts:
|
|
data.append({'ext':ext,'opts':self.get(ext).to_dict()})
|
|
self.data['exts'] = data
|
|
for key in ['bools', 'exe', 'exts', 'prefix', 'prefix_char']:
|
|
value = self.get(key)
|
|
if isinstance(value,str):
|
|
self.data[key] = value
|
|
elif isinstance(value,ArgumentContainer):
|
|
self.data[key] = value.to_dict()
|
|
|
|
def commit(self):
|
|
backup_name = self.args_file + '.bak'
|
|
if os.path.exists(backup_name):
|
|
os.remove(backup_name)
|
|
shutil.copy(self.args_file,backup_name)
|
|
with open(self.args_file,'w') as file:
|
|
json.dump(self.data,file)
|
|
|
|
def revert(self):
|
|
backup_name = self.args_file + '.bak'
|
|
if os.path.exists(backup_name):
|
|
os.remove(self.args_file)
|
|
os.rename(backup_name,self.args_file)
|
|
|
|
class ArgumentContainer:
|
|
def __init__(self,l_of_args,prefix):
|
|
self.args = l_of_args
|
|
self._argnames = None
|
|
self.prefix = prefix
|
|
|
|
def add_argument(self,*args,**kwargs):
|
|
new_arg = {'ostring': args,}
|
|
new_arg.update(kwargs)
|
|
self.args.append(new_arg)
|
|
self._argnames = None
|
|
|
|
@property
|
|
def argnames(self):
|
|
if self._argnames is None:
|
|
self._argnames = set(max(arg['ostring'],key=len).lstrip(self.prefix).replace('-','_') for arg in self.args)
|
|
return self._argnames
|
|
|
|
def to_dict(self):
|
|
return copy.deepcopy(self.args)
|
|
|
|
def __str__(self):
|
|
ostrings = (opt['ostring'] for opt in self.args)
|
|
f_ostrings = ('({})'.format(', '.join(ostring)) for ostring in ostrings)
|
|
return '[{}]'.format(', '.join(f_ostrings))
|
|
|
|
def __repr__(self):
|
|
return str(self)
|
|
|
|
cur = ArgumentHandler(os.path.join(ROOT,'opts.json'))
|
|
globals().update((key,cur.data[key]) for key in ['bools', 'exe', 'exts', 'other', 'prefix', 'prefix_char'])
|
|
common = bools+other
|
|
exe += bools
|