From 30f7340f145a3d3771a262b10d4fb2c9a3a134ca Mon Sep 17 00:00:00 2001 From: Farhad Date: Mon, 1 Aug 2022 11:22:22 +0430 Subject: [PATCH 1/2] UPDATE: Files Formatted via Black --- setup.py | 5 +- src/pasteme_cli/cli.py | 119 +++++++++++++++++--------------- src/pasteme_cli/constants.py | 4 +- src/pasteme_cli/sdk/__init__.py | 2 +- src/pasteme_cli/sdk/pasteme.py | 25 ++----- tests/test_snippet.py | 4 +- 6 files changed, 78 insertions(+), 81 deletions(-) diff --git a/setup.py b/setup.py index dcf0a9c..d2c0a75 100755 --- a/setup.py +++ b/setup.py @@ -65,7 +65,10 @@ def read(*names, **kwargs): 'Issue Tracker': 'https://github.com/collove/pasteme-cli/issues', }, keywords=[ - 'pastebin', 'cli', 'tool', 'linux', + 'pastebin', + 'cli', + 'tool', + 'linux', ], python_requires='>=3.6', install_requires=[ diff --git a/src/pasteme_cli/cli.py b/src/pasteme_cli/cli.py index 21d872c..91087e9 100644 --- a/src/pasteme_cli/cli.py +++ b/src/pasteme_cli/cli.py @@ -36,75 +36,82 @@ formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument( - '-t', '--title', - metavar='', - type=str, - help='title/description of snippet', + '-t', + '--title', + metavar='', + type=str, + help='title/description of snippet', ) parser.add_argument( - '-l', '--language', - metavar='', - default='plaintext', - type=str, - choices=LANGUAGES.keys(), - help=LANGUAGES_HINT, + '-l', + '--language', + metavar='', + default='plaintext', + type=str, + choices=LANGUAGES.keys(), + help=LANGUAGES_HINT, ) parser.add_argument( - '-T', '--theme', - metavar='', - default='default', - type=str, - choices=THEMES.keys(), - help=THEMES_HINT, + '-T', + '--theme', + metavar='', + default='default', + type=str, + choices=THEMES.keys(), + help=THEMES_HINT, ) parser.add_argument( - "-v", "--verbose", - action = "store_true", + "-v", + "--verbose", + action="store_true", help="verbosity for post data and response", ) parser.add_argument( - '-s', '--start', - metavar='', - type=int, - default=1, - help='select lines from (default: first line of the file)', + '-s', + '--start', + metavar='', + type=int, + default=1, + help='select lines from (default: first line of the file)', ) parser.add_argument( - '-e', '--end', - metavar='', - type=int, - help='select lines till (default: end of the file)', + '-e', + '--end', + metavar='', + type=int, + help='select lines till (default: end of the file)', ) parser.add_argument( - 'file', - type=open, - help='script file', + 'file', + type=open, + help='script file', ) + def main(args=None): - args = parser.parse_args(args=args) - - with args.file as source_code: - code_lines = source_code.readlines() - - args.end = args.end if args.end else len(code_lines) - - code_lines = code_lines[int(args.start)-1:int(args.end)] - - if not code_lines: - sys.exit(f'Make sure ({args.start}-{args.end}) range is available in your source code file.') - - context = { - 'title': args.title, - 'body': ''.join(code_lines), - 'language': args.language, - 'theme': args.theme, - } - - try: - snippet = Snippet(**context) - context = snippet.push(PASTEME_API_URL, args.verbose).json() - print(f'PASTE --> {context["url"]}') - sys.exit() - except ConnectionError: - sys.exit(CONNECTION_ISSUE_HINT) + args = parser.parse_args(args=args) + + with args.file as source_code: + code_lines = source_code.readlines() + + args.end = args.end if args.end else len(code_lines) + + code_lines = code_lines[int(args.start) - 1 : int(args.end)] + + if not code_lines: + sys.exit(f'Make sure ({args.start}-{args.end}) range is available in your source code file.') + + context = { + 'title': args.title, + 'body': ''.join(code_lines), + 'language': args.language, + 'theme': args.theme, + } + + try: + snippet = Snippet(**context) + context = snippet.push(PASTEME_API_URL, args.verbose).json() + print(f'PASTE --> {context["url"]}') + sys.exit() + except ConnectionError: + sys.exit(CONNECTION_ISSUE_HINT) diff --git a/src/pasteme_cli/constants.py b/src/pasteme_cli/constants.py index d9c6f3a..b4d1f31 100644 --- a/src/pasteme_cli/constants.py +++ b/src/pasteme_cli/constants.py @@ -16,7 +16,7 @@ 'php': 'PHP Language', 'plaintext': 'PlainText', 'python': 'Python Language', - 'rb': 'Ruby Language' + 'rb': 'Ruby Language', } THEMES = { @@ -53,4 +53,4 @@ # Information EPILOG_DESCRIPTION = '''Author -> Sadra Yahyapour (mailto:lnxpylnxpy@gmail.com) -GitHub -> https://github.com/collove/pasteme-cli''' \ No newline at end of file +GitHub -> https://github.com/collove/pasteme-cli''' diff --git a/src/pasteme_cli/sdk/__init__.py b/src/pasteme_cli/sdk/__init__.py index 9bea9c6..574c976 100644 --- a/src/pasteme_cli/sdk/__init__.py +++ b/src/pasteme_cli/sdk/__init__.py @@ -1 +1 @@ -from .pasteme import Snippet \ No newline at end of file +from .pasteme import Snippet diff --git a/src/pasteme_cli/sdk/pasteme.py b/src/pasteme_cli/sdk/pasteme.py index 9b7310a..a54c476 100644 --- a/src/pasteme_cli/sdk/pasteme.py +++ b/src/pasteme_cli/sdk/pasteme.py @@ -8,8 +8,8 @@ JSON_TEMPLATE = '''-> {} {}''' -class Snippet: +class Snippet: def __init__(self, title, body, language, theme) -> None: self.snippet = { 'title': title, @@ -19,24 +19,13 @@ def __init__(self, title, body, language, theme) -> None: } def push(self, url, is_verbose=False) -> requests.Response: - response = requests.post( - url=url, - data=self.snippet - ) - + response = requests.post(url=url, data=self.snippet) + if is_verbose: response_json = response.json() - sent_data = highlight( - json.dumps(self.snippet, indent=3), - lexers.JsonLexer(), - formatters.TerminalFormatter() - ) - response_data = highlight( - json.dumps(response_json, indent=3), - lexers.JsonLexer(), - formatters.TerminalFormatter() - ) + sent_data = highlight(json.dumps(self.snippet, indent=3), lexers.JsonLexer(), formatters.TerminalFormatter()) + response_data = highlight(json.dumps(response_json, indent=3), lexers.JsonLexer(), formatters.TerminalFormatter()) print(JSON_TEMPLATE.format('Posted Data', sent_data)) print(JSON_TEMPLATE.format('Returned Payload', response_data)) - - return response \ No newline at end of file + + return response diff --git a/tests/test_snippet.py b/tests/test_snippet.py index a1f0897..68b3f84 100644 --- a/tests/test_snippet.py +++ b/tests/test_snippet.py @@ -5,8 +5,6 @@ class SnippetTestCase(unittest.TestCase): - - def setUp(self) -> None: self.sample = { 'title': 'Paste Title', @@ -23,4 +21,4 @@ def test_push_snippet(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() From 15fcd318389bf2c73af9c4e95d1589b58dfa6fa5 Mon Sep 17 00:00:00 2001 From: Farhad Date: Mon, 1 Aug 2022 12:50:39 +0430 Subject: [PATCH 2/2] ADD: Expiry Option --- src/pasteme_cli/cli.py | 18 ++++++++++++++++++ src/pasteme_cli/constants.py | 14 +++++++++++--- src/pasteme_cli/sdk/pasteme.py | 3 ++- tests/test_snippet.py | 1 + 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/pasteme_cli/cli.py b/src/pasteme_cli/cli.py index 91087e9..e679c70 100644 --- a/src/pasteme_cli/cli.py +++ b/src/pasteme_cli/cli.py @@ -29,6 +29,8 @@ from .constants import PASTEME_SERVICE_URL from .constants import THEMES from .constants import THEMES_HINT +from .constants import EXPIRY_TIME +from .constants import EXPIRY_TIME_HINT parser = argparse.ArgumentParser( description=f'A CLI pastebin tool interacting with PasteMe ({PASTEME_SERVICE_URL}) RESTful APIs.', @@ -51,6 +53,15 @@ choices=LANGUAGES.keys(), help=LANGUAGES_HINT, ) +parser.add_argument( + '-x', + '--expiry-time', + metavar='', + default='1d', + type=str, + choices=EXPIRY_TIME.keys(), + help=EXPIRY_TIME_HINT, +) parser.add_argument( '-T', '--theme', @@ -101,11 +112,18 @@ def main(args=None): if not code_lines: sys.exit(f'Make sure ({args.start}-{args.end}) range is available in your source code file.') + expiry_days = { + "1d": 1, + "1w": 7, + "1m": 30, + } + context = { 'title': args.title, 'body': ''.join(code_lines), 'language': args.language, 'theme': args.theme, + 'expiry_time': expiry_days[args.expiry_time], } try: diff --git a/src/pasteme_cli/constants.py b/src/pasteme_cli/constants.py index b4d1f31..a5904f4 100644 --- a/src/pasteme_cli/constants.py +++ b/src/pasteme_cli/constants.py @@ -1,5 +1,4 @@ # Language choices - LANGUAGES = { 'bash': 'Command Language', 'c': 'C Language', @@ -28,18 +27,27 @@ 'github-dark': 'Github Dark', } -# Hint for using the available languages +EXPIRY_TIME = { + '1d': '1 Day', + '1w': '1 Week', + '1m': '1 Month', +} +# Hint for using the available languages LANGUAGES_HINT = f'''snippet language (available languages: {", ".join([_ for _ in LANGUAGES.keys()])}) ''' # Hint for using the available themes - THEMES_HINT = f'''theme (available themes: {", ".join([_ for _ in THEMES.keys()])}) ''' +# Hint for using the available expiry times +EXPIRY_TIME_HINT = f'''expiry time (available expiry times: +{", ".join([_ for _ in EXPIRY_TIME.keys()])}) +''' + # Actual service information PASTEME_SERVICE_URL = 'https://pasteme.pythonanywhere.com' diff --git a/src/pasteme_cli/sdk/pasteme.py b/src/pasteme_cli/sdk/pasteme.py index a54c476..7215df2 100644 --- a/src/pasteme_cli/sdk/pasteme.py +++ b/src/pasteme_cli/sdk/pasteme.py @@ -10,12 +10,13 @@ class Snippet: - def __init__(self, title, body, language, theme) -> None: + def __init__(self, title, body, language, theme, expiry_time) -> None: self.snippet = { 'title': title, 'body': body, 'language': language, 'theme': theme, + 'expires_in': expiry_time, } def push(self, url, is_verbose=False) -> requests.Response: diff --git a/tests/test_snippet.py b/tests/test_snippet.py index 68b3f84..f886de3 100644 --- a/tests/test_snippet.py +++ b/tests/test_snippet.py @@ -11,6 +11,7 @@ def setUp(self) -> None: 'body': 'print("Hello")', 'language': 'bash', 'theme': 'default', + 'expiry_time': '7', } # TODO: Using mocks