Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
15 changes: 15 additions & 0 deletions homeworks/relby/5/my_awesome_script/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Final

from my_awesome_script.parser import create_parser

PROG_NAME: Final = 'my_awesome_script'


def main() -> None:
parser = create_parser(PROG_NAME)
args = parser.parse_args()
args.func(args)


if __name__ == '__main__':
main()
18 changes: 18 additions & 0 deletions homeworks/relby/5/my_awesome_script/classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from argparse import Namespace, _SubParsersAction # noqa: WPS450


class Command(object):
name: str
help: str
arguments: list[tuple[str, str]]

@classmethod
def add_to_subparsers(cls, subparsers: _SubParsersAction) -> None:
subparser = subparsers.add_parser(cls.name, help=cls.help)
for arg_name, arg_help in cls.arguments:
subparser.add_argument(arg_name, help=arg_help)
subparser.set_defaults(func=cls.perform)

@classmethod
def perform(cls, args: Namespace) -> None:
raise NotImplementedError
57 changes: 57 additions & 0 deletions homeworks/relby/5/my_awesome_script/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from argparse import Namespace
from datetime import datetime

from cowpy.cow import Cowacter
from pygments import highlight as hl
from pygments.formatters import TerminalFormatter
from pygments.lexers import PythonLexer
from pytz import timezone
from pytz.exceptions import UnknownTimeZoneError

from my_awesome_script.classes import Command


class HighlightCommand(Command):
name = 'highlight'
help = 'Hightlights python code in your terminal'
arguments = [('code', 'The python code to highlight')]

@classmethod
def perform(cls, args: Namespace) -> None:
code = args.code

print(hl(
code,
PythonLexer(),
TerminalFormatter(),
))


class CowsayCommand(Command):
name = 'cowsay'
help = 'A cow will say everything you want'
arguments = [('phrase', 'The phrase of a cow')]

@classmethod
def perform(cls, args: Namespace) -> None:
phrase = args.phrase

cow = Cowacter(eyes='stoned')
print(cow.milk(phrase))


class TimeCommand(Command):
name = 'time'
help = 'Get time of the specific region'
arguments = [('region', 'The region to get time of')]

@classmethod
def perform(cls, args: Namespace) -> None:
region = args.region

try:
tz = timezone(region)
except UnknownTimeZoneError:
print('Region is not found')
else:
print(datetime.now(tz).strftime('%H:%M:%S'))
37 changes: 37 additions & 0 deletions homeworks/relby/5/my_awesome_script/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import inspect
from argparse import ArgumentParser, _SubParsersAction # noqa: WPS450
from importlib import import_module

from my_awesome_script.classes import Command


def get_commands() -> list[type[Command]]:
module_members = inspect.getmembers(
import_module('.commands', 'my_awesome_script'),
)
commands: list[type[Command]] = []
for _, module_member in module_members:
if not inspect.isclass(module_member):
continue
if module_member is not Command and issubclass(module_member, Command):
commands.append(module_member)
return commands


def init_commands(subparsers: _SubParsersAction) -> None:
commands = get_commands()
for command in commands:
command.add_to_subparsers(subparsers)


def create_parser(prog_name: str) -> ArgumentParser:
parser = ArgumentParser(prog=prog_name)

subparsers = parser.add_subparsers(
required=True,
title='commands',
)

init_commands(subparsers)

return parser
2 changes: 2 additions & 0 deletions homeworks/relby/5/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mypy]
ignore_missing_imports = true
Loading