1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-19 00:17:51 +02:00
This commit is contained in:
Kelly Brazil
2022-10-03 08:58:09 -07:00
parent 094b059aea
commit 83d388613f

View File

@ -96,12 +96,11 @@ class JcCli():
self.bash_comp = False self.bash_comp = False
self.zsh_comp = False self.zsh_comp = False
# Magic options # magic variables
self.valid_command = False
self.run_command = None
self.run_command_str = ''
self.magic_found_parser = None self.magic_found_parser = None
self.magic_options: List[str] = [] self.magic_options: List[str] = []
self.magic_run_command = None
self.magic_run_command_str = ''
self.magic_stdout = None self.magic_stdout = None
self.magic_stderr = None self.magic_stderr = None
self.magic_returncode = 0 self.magic_returncode = 0
@ -442,16 +441,13 @@ Examples:
magic_dict.update({mc: entry['argument'] for mc in entry.get('magic_commands', [])}) magic_dict.update({mc: entry['argument'] for mc in entry.get('magic_commands', [])})
# find the command and parser # find the command and parser
self.magic_run_command = args_given
one_word_command = args_given[0] one_word_command = args_given[0]
two_word_command = ' '.join(args_given[0:2]) two_word_command = ' '.join(args_given[0:2])
# try to get a parser for two_word_command, otherwise get one for one_word_command # try to get a parser for two_word_command, otherwise get one for one_word_command
self.magic_found_parser = magic_dict.get(two_word_command, magic_dict.get(one_word_command)) self.magic_found_parser = magic_dict.get(two_word_command, magic_dict.get(one_word_command))
# set the instance variables
self.valid_command = bool(self.magic_found_parser)
self.run_command = args_given
def open_text_file(self): def open_text_file(self):
with open(self.path_string, 'r') as f: with open(self.path_string, 'r') as f:
return f.read() return f.read()
@ -461,7 +457,7 @@ Examples:
Use subprocess to run the user's command. Returns the STDOUT, STDERR, Use subprocess to run the user's command. Returns the STDOUT, STDERR,
and the Exit Code as a tuple. and the Exit Code as a tuple.
""" """
proc = subprocess.Popen(self.run_command, proc = subprocess.Popen(self.magic_run_command,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=False, # Allows inheriting file descriptors; close_fds=False, # Allows inheriting file descriptors;
@ -492,8 +488,8 @@ Examples:
'timestamp': self.run_timestamp.timestamp() 'timestamp': self.run_timestamp.timestamp()
} }
if self.run_command: if self.magic_run_command:
meta_obj['magic_command'] = self.run_command meta_obj['magic_command'] = self.magic_run_command
meta_obj['magic_command_exit'] = self.magic_returncode meta_obj['magic_command_exit'] = self.magic_returncode
if isinstance(self.data_out, dict): if isinstance(self.data_out, dict):
@ -539,15 +535,12 @@ Examples:
self.args = sys.argv self.args = sys.argv
self.magic_parser() self.magic_parser()
# set colors
self.env_colors = os.getenv('JC_COLORS')
# set magic options if magic syntax was found # set magic options if magic syntax was found
if self.magic_found_parser: if self.magic_found_parser:
self.options.extend(self.magic_options) self.options.extend(self.magic_options)
# find options if magic_parser did not find a command # find options if magic_parser did not find a command
if not self.valid_command: if not self.magic_found_parser:
for opt in self.args: for opt in self.args:
if opt in long_options_map: if opt in long_options_map:
self.options.extend(long_options_map[opt][0]) self.options.extend(long_options_map[opt][0])
@ -555,6 +548,8 @@ Examples:
if opt.startswith('-') and not opt.startswith('--'): if opt.startswith('-') and not opt.startswith('--'):
self.options.extend(opt[1:]) self.options.extend(opt[1:])
self.env_colors = os.getenv('JC_COLORS')
self.about = 'a' in self.options self.about = 'a' in self.options
self.debug = 'd' in self.options self.debug = 'd' in self.options
self.verbose_debug = self.options.count('d') > 1 self.verbose_debug = self.options.count('d') > 1
@ -604,13 +599,13 @@ Examples:
sys.exit(0) sys.exit(0)
# if magic syntax used, try to run the command and error if it's not found, etc. # if magic syntax used, try to run the command and error if it's not found, etc.
if self.run_command: if self.magic_run_command:
try: try:
self.run_command_str = shlex.join(self.run_command) # python 3.8+ self.magic_run_command_str = shlex.join(self.magic_run_command) # python 3.8+
except AttributeError: except AttributeError:
self.run_command_str = ' '.join(self.run_command) # older python versions self.magic_run_command_str = ' '.join(self.magic_run_command) # older python versions
if self.run_command_str.startswith('/proc'): if self.magic_run_command_str.startswith('/proc'):
try: try:
self.magic_found_parser = 'proc' self.magic_found_parser = 'proc'
self.magic_stdout = self.open_text_file() self.magic_stdout = self.open_text_file()
@ -621,7 +616,7 @@ Examples:
error_msg = os.strerror(e.errno) error_msg = os.strerror(e.errno)
utils.error_message([ utils.error_message([
f'"{self.run_command_str}" file could not be opened: {error_msg}.' f'"{self.magic_run_command_str}" file could not be opened: {error_msg}.'
]) ])
self.jc_exit = self.JC_ERROR_EXIT self.jc_exit = self.JC_ERROR_EXIT
sys.exit(self.combined_exit_code()) sys.exit(self.combined_exit_code())
@ -631,12 +626,12 @@ Examples:
raise raise
utils.error_message([ utils.error_message([
f'"{self.run_command_str}" file could not be opened. For details use the -d or -dd option.' f'"{self.magic_run_command_str}" file could not be opened. For details use the -d or -dd option.'
]) ])
self.jc_exit = self.JC_ERROR_EXIT self.jc_exit = self.JC_ERROR_EXIT
sys.exit(self.combined_exit_code()) sys.exit(self.combined_exit_code())
elif self.valid_command: elif self.magic_found_parser:
try: try:
self.run_user_command() self.run_user_command()
if self.magic_stderr: if self.magic_stderr:
@ -648,7 +643,7 @@ Examples:
error_msg = os.strerror(e.errno) error_msg = os.strerror(e.errno)
utils.error_message([ utils.error_message([
f'"{self.run_command_str}" command could not be run: {error_msg}.' f'"{self.magic_run_command_str}" command could not be run: {error_msg}.'
]) ])
self.jc_exit = self.JC_ERROR_EXIT self.jc_exit = self.JC_ERROR_EXIT
sys.exit(self.combined_exit_code()) sys.exit(self.combined_exit_code())
@ -658,13 +653,13 @@ Examples:
raise raise
utils.error_message([ utils.error_message([
f'"{self.run_command_str}" command could not be run. For details use the -d or -dd option.' f'"{self.magic_run_command_str}" command could not be run. For details use the -d or -dd option.'
]) ])
self.jc_exit = self.JC_ERROR_EXIT self.jc_exit = self.JC_ERROR_EXIT
sys.exit(self.combined_exit_code()) sys.exit(self.combined_exit_code())
elif self.run_command is not None: elif self.magic_run_command is not None:
utils.error_message([f'"{self.run_command_str}" cannot be used with Magic syntax. Use "jc -h" for help.']) utils.error_message([f'"{self.magic_run_command_str}" cannot be used with Magic syntax. Use "jc -h" for help.'])
sys.exit(self.combined_exit_code()) sys.exit(self.combined_exit_code())
# find the correct parser # find the correct parser