Skip to content

Commit

Permalink
refactor: Enhance naming conventions for cmproved clarity and consist…
Browse files Browse the repository at this point in the history
…ency (osl-incubator#49)

* chore(modules): Rename error module to errors

* refactor(modules): Move __main__.py content to cli.py

* fix(modules): Change makim import to absolute path

* chore(linting): Resolve mypy blank line warnings in the modules
  • Loading branch information
esloch authored Jun 7, 2023
1 parent bbf8d6c commit 161442d
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 192 deletions.
191 changes: 1 addition & 190 deletions makim/__main__.py
Original file line number Diff line number Diff line change
@@ -1,193 +1,4 @@
import argparse
import os
import sys
from pathlib import Path

from makim import Makim, __version__


class CustomHelpFormatter(argparse.RawTextHelpFormatter):
"""Formatter for generating usage messages and argument help strings.
Only the name of this class is considered a public API. All the methods
provided by the class are considered an implementation detail.
"""

def __init__(
self,
prog,
indent_increment=2,
max_help_position=4,
width=None,
**kwargs,
):
super().__init__(
prog,
indent_increment=indent_increment,
max_help_position=max_help_position,
width=width,
**kwargs,
)


makim = Makim()


def _get_args():
"""
note: when added new flags, update the list of flags to be
skipped at extract_makim_args function.
"""
makim_file_default = str(Path(os.getcwd()) / '.makim.yaml')

parser = argparse.ArgumentParser(
prog='MakIm',
description=(
'MakIm is a tool that helps you to organize '
'and simplify your helper commands.'
),
epilog=(
'If you have any problem, open an issue at: '
'https://github.com/osl-incubator/makim'
),
add_help=False,
formatter_class=CustomHelpFormatter,
)
parser.add_argument(
'--help',
'-h',
action='store_true',
help='Show the help menu',
)

parser.add_argument(
'--version',
action='store_true',
help='Show the version of the installed MakIm tool.',
)

parser.add_argument(
'--verbose',
action='store_true',
help='Show the commands to be executed.',
)

parser.add_argument(
'--dry-run',
action='store_true',
help="Show the commands but don't execute them.",
)

parser.add_argument(
'--makim-file',
type=str,
default=makim_file_default,
help='Specify a custom location for the makim file.',
)

try:
idx = sys.argv.index('--makim-file')
makim_file = sys.argv[idx + 1]
except ValueError:
makim_file = makim_file_default

makim.load(makim_file)
target_help = []
groups = makim.global_data.get('groups', [])
for group in groups:
for target_name, target_data in groups[group]['targets'].items():
target_name_qualified = f'{group}.{target_name}'
help_text = target_data['help'] if 'help' in target_data else ''
target_help.append(f' {target_name_qualified} => {help_text}')

if 'args' in target_data:
target_help.append(' ARGS:')

for arg_name, arg_data in target_data['args'].items():
target_help.append(
f' --{arg_name}: ({arg_data["type"]}) '
f'{arg_data["help"]}'
)

parser.add_argument(
'target',
nargs='?',
default=None,
help=(
'Specify the target command to be performed. '
'\nOptions are:\n' + '\n'.join(target_help)
),
)

return parser


def show_version():
print(__version__)


def extract_makim_args():
makim_args = {}
index_to_remove = []
for ind, arg in enumerate(list(sys.argv)):
if arg in [
'--help',
'--version',
'--verbose',
'--makim-file',
'--dry-run',
]:
continue

if not arg.startswith('--'):
continue

index_to_remove.append(ind)

arg_name = None
arg_value = None

next_ind = ind + 1

arg_name = sys.argv[ind]

if (
len(sys.argv) == next_ind
or len(sys.argv) > next_ind
and sys.argv[next_ind].startswith('--')
):
arg_value = True
else:
arg_value = sys.argv[next_ind]
index_to_remove.append(next_ind)

makim_args[arg_name] = arg_value

# remove exclusive makim flags from original sys.argv
for ind in sorted(index_to_remove, reverse=True):
sys.argv.pop(ind)

return makim_args


def app():
makim_args = extract_makim_args()
args_parser = _get_args()
args = args_parser.parse_args()

if args.version:
return show_version()

if not args.target or args.help:
return args_parser.print_help()

if args.help:
return args_parser.print_help()

makim.load(args.makim_file)
makim_args.update(dict(args._get_kwargs()))
return makim.run(makim_args)

from makim.cli import app

if __name__ == '__main__':
app()
189 changes: 189 additions & 0 deletions makim/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import argparse
import os
import sys
from pathlib import Path

from makim import Makim, __version__


class CustomHelpFormatter(argparse.RawTextHelpFormatter):
"""Formatter for generating usage messages and argument help strings.
Only the name of this class is considered a public API. All the methods
provided by the class are considered an implementation detail.
"""

def __init__(
self,
prog,
indent_increment=2,
max_help_position=4,
width=None,
**kwargs,
):
super().__init__(
prog,
indent_increment=indent_increment,
max_help_position=max_help_position,
width=width,
**kwargs,
)


makim = Makim()


def _get_args():
"""
note: when added new flags, update the list of flags to be
skipped at extract_makim_args function.
"""
makim_file_default = str(Path(os.getcwd()) / '.makim.yaml')

parser = argparse.ArgumentParser(
prog='MakIm',
description=(
'MakIm is a tool that helps you to organize '
'and simplify your helper commands.'
),
epilog=(
'If you have any problem, open an issue at: '
'https://github.com/osl-incubator/makim'
),
add_help=False,
formatter_class=CustomHelpFormatter,
)
parser.add_argument(
'--help',
'-h',
action='store_true',
help='Show the help menu',
)

parser.add_argument(
'--version',
action='store_true',
help='Show the version of the installed MakIm tool.',
)

parser.add_argument(
'--verbose',
action='store_true',
help='Show the commands to be executed.',
)

parser.add_argument(
'--dry-run',
action='store_true',
help="Show the commands but don't execute them.",
)

parser.add_argument(
'--makim-file',
type=str,
default=makim_file_default,
help='Specify a custom location for the makim file.',
)

try:
idx = sys.argv.index('--makim-file')
makim_file = sys.argv[idx + 1]
except ValueError:
makim_file = makim_file_default

makim.load(makim_file)
target_help = []
groups = makim.global_data.get('groups', [])
for group in groups:
for target_name, target_data in groups[group]['targets'].items():
target_name_qualified = f'{group}.{target_name}'
help_text = target_data['help'] if 'help' in target_data else ''
target_help.append(f' {target_name_qualified} => {help_text}')

if 'args' in target_data:
target_help.append(' ARGS:')

for arg_name, arg_data in target_data['args'].items():
target_help.append(
f' --{arg_name}: ({arg_data["type"]}) '
f'{arg_data["help"]}'
)

parser.add_argument(
'target',
nargs='?',
default=None,
help=(
'Specify the target command to be performed. '
'\nOptions are:\n' + '\n'.join(target_help)
),
)

return parser


def show_version():
print(__version__)


def extract_makim_args():
makim_args = {}
index_to_remove = []
for ind, arg in enumerate(list(sys.argv)):
if arg in [
'--help',
'--version',
'--verbose',
'--makim-file',
'--dry-run',
]:
continue

if not arg.startswith('--'):
continue

index_to_remove.append(ind)

arg_name = None
arg_value = None

next_ind = ind + 1

arg_name = sys.argv[ind]

if (
len(sys.argv) == next_ind
or len(sys.argv) > next_ind
and sys.argv[next_ind].startswith('--')
):
arg_value = True
else:
arg_value = sys.argv[next_ind]
index_to_remove.append(next_ind)

makim_args[arg_name] = arg_value

# remove exclusive makim flags from original sys.argv
for ind in sorted(index_to_remove, reverse=True):
sys.argv.pop(ind)

return makim_args


def app():
makim_args = extract_makim_args()
args_parser = _get_args()
args = args_parser.parse_args()

if args.version:
return show_version()

if not args.target or args.help:
return args_parser.print_help()

if args.help:
return args_parser.print_help()

makim.load(args.makim_file)
makim_args.update(dict(args._get_kwargs()))
return makim.run(makim_args)
File renamed without changes.
2 changes: 1 addition & 1 deletion makim/makim.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from colorama import Fore
from jinja2 import Template

from makim.error import MakimError
from makim.errors import MakimError


def escape_template_tag(v: str) -> str:
Expand Down
Loading

0 comments on commit 161442d

Please sign in to comment.