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.

98 lines
2.9 KiB

  1. from config import ROOT
  2. import copy
  3. import json
  4. import os
  5. import shutil
  6. class ArgumentHandler:
  7. def __init__(self,args_file):
  8. self.args_file = args_file
  9. self.exts = set()
  10. self.reload()
  11. for _ext in self.data['exts']:
  12. ext = _ext['ext']
  13. self.exts.add(ext)
  14. args = _ext['opts']
  15. self.set(ext,ArgumentContainer(args,self.data['prefix_char']))
  16. for key,value in self.data.items():
  17. if key != 'exts' and isinstance(value,list):
  18. self.set(key,ArgumentContainer(value,self.data['prefix_char']))
  19. self.prefix_char = self.data['prefix_char']
  20. def get(self,item):
  21. return self.__dict__[item]
  22. def set(self,item,value):
  23. self.__dict__[item] = value
  24. def add_ext(self,ext):
  25. self.set(ext,ArgumentContainer([]))
  26. self.exts.add(ext)
  27. def reload(self):
  28. with open(self.args_file) as file:
  29. self.data = json.load(file)
  30. def save(self):
  31. data = []
  32. for ext in self.exts:
  33. data.append({'ext':ext,'opts':self.get(ext).to_dict()})
  34. self.data['exts'] = data
  35. for key in ['bools', 'exe', 'exts', 'prefix', 'prefix_char']:
  36. value = self.get(key)
  37. if isinstance(value,str):
  38. self.data[key] = value
  39. elif isinstance(value,ArgumentContainer):
  40. self.data[key] = value.to_dict()
  41. def commit(self):
  42. backup_name = self.args_file + '.bak'
  43. if os.path.exists(backup_name):
  44. os.remove(backup_name)
  45. shutil.copy(self.args_file,backup_name)
  46. with open(self.args_file,'w') as file:
  47. json.dump(self.data,file)
  48. def revert(self):
  49. backup_name = self.args_file + '.bak'
  50. if os.path.exists(backup_name):
  51. os.remove(self.args_file)
  52. os.rename(backup_name,self.args_file)
  53. class ArgumentContainer:
  54. def __init__(self,l_of_args,prefix):
  55. self.args = l_of_args
  56. self._argnames = None
  57. self.prefix = prefix
  58. def add_argument(self,*args,**kwargs):
  59. new_arg = {'ostring': args,}
  60. new_arg.update(kwargs)
  61. self.args.append(new_arg)
  62. self._argnames = None
  63. @property
  64. def argnames(self):
  65. if self._argnames is None:
  66. self._argnames = set(max(arg['ostring'],key=len).lstrip(self.prefix).replace('-','_') for arg in self.args)
  67. return self._argnames
  68. def to_dict(self):
  69. return copy.deepcopy(self.args)
  70. def __str__(self):
  71. ostrings = (opt['ostring'] for opt in self.args)
  72. f_ostrings = ('({})'.format(', '.join(ostring)) for ostring in ostrings)
  73. return '[{}]'.format(', '.join(f_ostrings))
  74. def __repr__(self):
  75. return str(self)
  76. cur = ArgumentHandler(os.path.join(ROOT,'opts.json'))
  77. globals().update((key,cur.data[key]) for key in ['bools', 'exe', 'exts', 'other', 'prefix', 'prefix_char'])
  78. common = bools+other
  79. exe += bools