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..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.', @@ -36,75 +38,98 @@ 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, + '-x', + '--expiry-time', + metavar='', + default='1d', + type=str, + choices=EXPIRY_TIME.keys(), + help=EXPIRY_TIME_HINT, ) parser.add_argument( - "-v", "--verbose", - action = "store_true", + '-T', + '--theme', + metavar='', + default='default', + type=str, + choices=THEMES.keys(), + help=THEMES_HINT, +) +parser.add_argument( + "-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.') + + 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: + 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..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', @@ -16,7 +15,7 @@ 'php': 'PHP Language', 'plaintext': 'PlainText', 'python': 'Python Language', - 'rb': 'Ruby Language' + 'rb': 'Ruby Language', } THEMES = { @@ -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' @@ -53,4 +61,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..7215df2 100644 --- a/src/pasteme_cli/sdk/pasteme.py +++ b/src/pasteme_cli/sdk/pasteme.py @@ -8,35 +8,25 @@ JSON_TEMPLATE = '''-> {} {}''' -class Snippet: - def __init__(self, title, body, language, theme) -> None: +class Snippet: + 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: - 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..f886de3 100644 --- a/tests/test_snippet.py +++ b/tests/test_snippet.py @@ -5,14 +5,13 @@ class SnippetTestCase(unittest.TestCase): - - def setUp(self) -> None: self.sample = { 'title': 'Paste Title', 'body': 'print("Hello")', 'language': 'bash', 'theme': 'default', + 'expiry_time': '7', } # TODO: Using mocks @@ -23,4 +22,4 @@ def test_push_snippet(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()