Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if exc.errno != errno.EEXIST:
raise
pass
if not os.path.exists(CmdManager.config_file):
with open(CmdManager.config_file, 'w') as a_file:
a_file.write("---\n")
out_dict = {'plugin_path': os.path.join(CmdManager.config_dir,
'plugins')}
a_file.write(yaml.dump(out_dict, default_flow_style=False))
# ----------------------------------------------------------------------
# Check version
# ----------------------------------------------------------------------
if os.path.exists(CmdManager.version_file):
cur_version = None
version_fh = open(CmdManager.version_file, "r")
for i in version_fh:
if "__version__" in i:
cur_version = i.split("=")[1]
cur_version = re.sub("[\'\" \n\r]", "", cur_version)
if cur_version != __version__:
cls._create_version_file()
CmdManager.reload = True
else:
cls._create_version_file()
def parse_cmd_args(cls):
""" Parse arguments of all declared commands."""
CmdManager.args = cls.parser.parse_args(None)
args = CmdManager.args
cmd_name = args.command
if cmd_name is None:
message("Please provide a subcommand or argument (e.g. -h)", type="WARNING", force=True)
CmdManager.parser.print_help()
exit(0)
lang = cls.cmd_obj_list[cmd_name].lang
if lang == 'Python':
if args.tmp_dir is not None:
if not os.path.exists(args.tmp_dir):
msg = "Creating directory {d}."
message(msg.format(d=args.tmp_dir), type="INFO")
# ----------------------------------------------------------------------
if os.path.exists(CmdManager.version_file):
cur_version = None
version_fh = open(CmdManager.version_file, "r")
for i in version_fh:
if "__version__" in i:
cur_version = i.split("=")[1]
cur_version = re.sub("[\'\" \n\r]", "", cur_version)
if cur_version != __version__:
cls._create_version_file()
CmdManager.reload = True
else:
cls._create_version_file()
def apropos(keyword="",
notes=False):
"""
Search in all command description files those related to a user-defined keyword.
"""
out_list = set()
for i in CmdManager.cmd_obj_list:
if keyword in CmdManager.cmd_obj_list[i].desc:
out_list.add(i)
if notes:
try:
if keyword in CmdManager.cmd_obj_list[i].notes:
out_list.add(i)
except:
pass
if out_list:
message(">> Keyword '" + keyword + "' was found in the following command:",
force=True)
for i in out_list:
print("\t- " + i + ".")
else:
message(">> Keyword '" + keyword + "' was not found.",
force=True)
def get_completion_script():
import pygtftk.cmd_manager
prg_list = pygtftk.cmd_manager.CmdManager.cmd_obj_list.keys()
prg_str = " ".join(sorted(prg_list))
return COMPLETION_SCRIPT.format(p=prg_str)
try:
del parser.__dict__['_option_string_actions']['--help']
del parser.__dict__['_option_string_actions']['-h']
except:
pass
self.parser = parser
self.fun = fun
self.desc = desc
self.logger = None
self.lang = lang
self.test = test
self.rlib = rlib
if re.search("@test", self.test):
pygtftk.cmd_manager.CmdManager.add_command(self)
else:
pygtftk.utils.message(
"%s command has no test and won't be installed." % name,
type="WARNING")
def check_config_file(cls):
# ----------------------------------------------------------------------
# Config directory and config files
# ----------------------------------------------------------------------
CmdManager.config_dir = os.path.join(os.path.expanduser("~"),
".gtftk",
CmdManager.hash)
CmdManager.config_file = os.path.join(CmdManager.config_dir,
"gtftk.cnf")
CmdManager.dumped_plugin_path = os.path.join(CmdManager.config_dir,
"plugin.pick")
CmdManager.version_file = os.path.join(CmdManager.config_dir,
"version.py")
if os.path.exists(os.path.join(CmdManager.config_dir, "reload")):
CmdManager.reload = True
os.unlink(os.path.join(CmdManager.config_dir, "reload"))
else:
CmdManager.reload = False
# ----------------------------------------------------------------------
def apropos(keyword="",
notes=False):
"""
Search in all command description files those related to a user-defined keyword.
"""
out_list = set()
for i in CmdManager.cmd_obj_list:
if keyword in CmdManager.cmd_obj_list[i].desc:
out_list.add(i)
if notes:
try:
if keyword in CmdManager.cmd_obj_list[i].notes:
out_list.add(i)
except:
pass
if out_list:
message(">> Keyword '" + keyword + "' was found in the following command:",
force=True)
for i in out_list:
print("\t- " + i + ".")
else:
message(">> Keyword '" + keyword + "' was not found.",
def check_config_file(cls):
# ----------------------------------------------------------------------
# Config directory and config files
# ----------------------------------------------------------------------
CmdManager.config_dir = os.path.join(os.path.expanduser("~"),
".gtftk",
CmdManager.hash)
CmdManager.config_file = os.path.join(CmdManager.config_dir,
"gtftk.cnf")
CmdManager.dumped_plugin_path = os.path.join(CmdManager.config_dir,
"plugin.pick")
CmdManager.version_file = os.path.join(CmdManager.config_dir,
"version.py")
if os.path.exists(os.path.join(CmdManager.config_dir, "reload")):
CmdManager.reload = True
os.unlink(os.path.join(CmdManager.config_dir, "reload"))
else:
CmdManager.reload = False
# ----------------------------------------------------------------------
# Load config
# ----------------------------------------------------------------------
def _load_dumped_plugins():
f_handler = open(CmdManager.dumped_plugin_path, "rb")
CmdManager.cmd_obj_list, CmdManager.parser = cloudpickle.load(f_handler)
f_handler.close()
for cur_cmd in sorted(CmdManager.cmd_obj_list):
# fix some issues related to dumping of the parser
for cur_arg in CmdManager.cmd_obj_list[cur_cmd].parser._option_string_actions:
obj = CmdManager.cmd_obj_list[
cur_cmd].parser._option_string_actions[cur_arg]
if obj.default == '==stdin==':
CmdManager.cmd_obj_list[cur_cmd].parser._option_string_actions[
cur_arg].default = sys.stdin
if obj.default == '==SUPPRESS==':
CmdManager.cmd_obj_list[cur_cmd].parser._option_string_actions[
cur_arg].default = argparse.SUPPRESS