mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-21 00:19:42 +02:00
formatting of quotation marks and docstrings
This commit is contained in:
27
jc/cli.py
27
jc/cli.py
@ -101,7 +101,7 @@ if os.path.isdir(local_parsers_dir):
|
|||||||
|
|
||||||
# We only support 2.3.0+, pygments changed color names in 2.4.0.
|
# We only support 2.3.0+, pygments changed color names in 2.4.0.
|
||||||
# startswith is sufficient and avoids potential exceptions from split and int.
|
# startswith is sufficient and avoids potential exceptions from split and int.
|
||||||
if pygments.__version__.startswith("2.3."):
|
if pygments.__version__.startswith('2.3.'):
|
||||||
PYGMENT_COLOR = {
|
PYGMENT_COLOR = {
|
||||||
'black': '#ansiblack',
|
'black': '#ansiblack',
|
||||||
'red': '#ansidarkred',
|
'red': '#ansidarkred',
|
||||||
@ -188,7 +188,7 @@ def set_env_colors():
|
|||||||
|
|
||||||
|
|
||||||
def piped_output():
|
def piped_output():
|
||||||
"""returns False if stdout is a TTY. True if output is being piped to another program"""
|
"""Returns False if stdout is a TTY. True if output is being piped to another program"""
|
||||||
if sys.stdout.isatty():
|
if sys.stdout.isatty():
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
@ -196,34 +196,34 @@ def piped_output():
|
|||||||
|
|
||||||
|
|
||||||
def ctrlc(signum, frame):
|
def ctrlc(signum, frame):
|
||||||
"""exit with error on SIGINT"""
|
"""Exit with error on SIGINT"""
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def parser_shortname(parser_argument):
|
def parser_shortname(parser_argument):
|
||||||
"""short name of the parser with dashes and no -- prefix"""
|
"""Short name of the parser with dashes and no -- prefix"""
|
||||||
return parser_argument[2:]
|
return parser_argument[2:]
|
||||||
|
|
||||||
|
|
||||||
def parser_argument(parser):
|
def parser_argument(parser):
|
||||||
"""short name of the parser with dashes and with -- prefix"""
|
"""Short name of the parser with dashes and with -- prefix"""
|
||||||
return f'--{parser}'
|
return f'--{parser}'
|
||||||
|
|
||||||
|
|
||||||
def parser_mod_shortname(parser):
|
def parser_mod_shortname(parser):
|
||||||
"""short name of the parser's module name (no -- prefix and dashes converted to underscores)"""
|
"""Short name of the parser's module name (no -- prefix and dashes converted to underscores)"""
|
||||||
return parser.replace('--', '').replace('-', '_')
|
return parser.replace('--', '').replace('-', '_')
|
||||||
|
|
||||||
|
|
||||||
def parser_module(parser):
|
def parser_module(parser):
|
||||||
"""import the module just in time and return the module object"""
|
"""Import the module just in time and return the module object"""
|
||||||
shortname = parser_mod_shortname(parser)
|
shortname = parser_mod_shortname(parser)
|
||||||
path = ('jcparsers.' if shortname in local_parsers else 'jc.parsers.')
|
path = ('jcparsers.' if shortname in local_parsers else 'jc.parsers.')
|
||||||
return importlib.import_module(path + shortname)
|
return importlib.import_module(path + shortname)
|
||||||
|
|
||||||
|
|
||||||
def parsers_text(indent=0, pad=0):
|
def parsers_text(indent=0, pad=0):
|
||||||
"""return the argument and description information from each parser"""
|
"""Return the argument and description information from each parser"""
|
||||||
ptext = ''
|
ptext = ''
|
||||||
for parser in parsers:
|
for parser in parsers:
|
||||||
parser_arg = parser_argument(parser)
|
parser_arg = parser_argument(parser)
|
||||||
@ -241,7 +241,7 @@ def parsers_text(indent=0, pad=0):
|
|||||||
|
|
||||||
|
|
||||||
def about_jc():
|
def about_jc():
|
||||||
"""return jc info and the contents of each parser.info as a dictionary"""
|
"""Return jc info and the contents of each parser.info as a dictionary"""
|
||||||
parser_list = []
|
parser_list = []
|
||||||
|
|
||||||
for parser in parsers:
|
for parser in parsers:
|
||||||
@ -271,7 +271,7 @@ def about_jc():
|
|||||||
|
|
||||||
|
|
||||||
def helptext(message):
|
def helptext(message):
|
||||||
"""return the help text with the list of parsers"""
|
"""Return the help text with the list of parsers"""
|
||||||
parsers_string = parsers_text(indent=12, pad=17)
|
parsers_string = parsers_text(indent=12, pad=17)
|
||||||
|
|
||||||
helptext_string = f'''
|
helptext_string = f'''
|
||||||
@ -304,7 +304,7 @@ def helptext(message):
|
|||||||
|
|
||||||
|
|
||||||
def json_out(data, pretty=False, mono=False, piped_out=False):
|
def json_out(data, pretty=False, mono=False, piped_out=False):
|
||||||
|
"""Returns a JSON formatted string. String may include color codes or be pretty printed."""
|
||||||
if not mono and not piped_out:
|
if not mono and not piped_out:
|
||||||
# set colors
|
# set colors
|
||||||
class JcStyle(Style):
|
class JcStyle(Style):
|
||||||
@ -356,7 +356,7 @@ def generate_magic_command(args):
|
|||||||
magic_dict = {}
|
magic_dict = {}
|
||||||
parser_info = about_jc()['parsers']
|
parser_info = about_jc()['parsers']
|
||||||
|
|
||||||
# Create a dictionary of magic_commands to their respective parsers.
|
# create a dictionary of magic_commands to their respective parsers.
|
||||||
for entry in parser_info:
|
for entry in parser_info:
|
||||||
# Update the dict with all of the magic commands for this parser, if they exist.
|
# Update the dict with all of the magic commands for this parser, if they exist.
|
||||||
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', [])})
|
||||||
@ -365,7 +365,7 @@ def generate_magic_command(args):
|
|||||||
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
|
||||||
found_parser = magic_dict.get(two_word_command, magic_dict.get(one_word_command))
|
found_parser = magic_dict.get(two_word_command, magic_dict.get(one_word_command))
|
||||||
|
|
||||||
# construct a new command line using the standard syntax: COMMAND | jc --PARSER -OPTIONS
|
# construct a new command line using the standard syntax: COMMAND | jc --PARSER -OPTIONS
|
||||||
@ -378,6 +378,7 @@ def generate_magic_command(args):
|
|||||||
|
|
||||||
|
|
||||||
def magic():
|
def magic():
|
||||||
|
"""Runs the command generated by generate_magic_command() to support magic syntax"""
|
||||||
valid_command, run_command = generate_magic_command(sys.argv)
|
valid_command, run_command = generate_magic_command(sys.argv)
|
||||||
if valid_command:
|
if valid_command:
|
||||||
os.system(run_command)
|
os.system(run_command)
|
||||||
|
Reference in New Issue
Block a user