diff --git a/homeworks/At0mn1yIvan/5/__init__.py b/homeworks/At0mn1yIvan/5/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/homeworks/At0mn1yIvan/5/__main__.py b/homeworks/At0mn1yIvan/5/__main__.py new file mode 100644 index 0000000..61bc2da --- /dev/null +++ b/homeworks/At0mn1yIvan/5/__main__.py @@ -0,0 +1,16 @@ +from tskInput import com_input + + +def main(): + try: + com_input() + except Exception as ex: + print('Something went wrong.', ex) + + +if __name__ == '__main__': + try: + main() + except KeyboardInterrupt: + print() + print('Shutting down, bye!') diff --git a/homeworks/At0mn1yIvan/5/cmnds.py b/homeworks/At0mn1yIvan/5/cmnds.py new file mode 100644 index 0000000..e73edda --- /dev/null +++ b/homeworks/At0mn1yIvan/5/cmnds.py @@ -0,0 +1,37 @@ +from datetime import datetime + +import pytz +from cowpy import cow +from pygments import highlight +from pygments.formatters import TerminalFormatter +from pygments.lexers import PythonLexer + + +def task(action: str, text: str = None) -> None: + if action == 'help': + functions[action]() + else: + functions[action](text) + + +def Highlight(code: str) -> None: + print(highlight(code, PythonLexer(), TerminalFormatter())) + + +def Cowsay(text: str) -> None: + print(cow.milk_random_cow(text)) + + +def Time(timezone: str) -> None: + local_date = datetime.now(pytz.timezone(timezone)) + print("{0} {1}".format(local_date.date(), local_date.time())) + + +def Help() -> None: + print("EXAMPLE COMMAND:") + print(" highlight 'any text'") + print(" cowsay 'any text'") + print(" time 'Region/City'") + + +functions = {'highlight': Highlight, 'cowsay': Cowsay, 'time': Time, 'help': Help} # noqa: E501 diff --git a/homeworks/At0mn1yIvan/5/tskInput.py b/homeworks/At0mn1yIvan/5/tskInput.py new file mode 100644 index 0000000..68b4d97 --- /dev/null +++ b/homeworks/At0mn1yIvan/5/tskInput.py @@ -0,0 +1,16 @@ +import argparse + +from cmnds import functions, task + + +def com_input(): + parser = argparse.ArgumentParser() + parser.add_argument('action', help='Input action.', choices=functions) + parser.add_argument('text', help="highlight 'any text'/cowsay 'any text'/time 'Region/City'") # noqa: E501 + args = parser.parse_args() + + try: + task(args.action, args.text) + except Exception as ex: + task('help') + raise ex