Skip to content

Commit

Permalink
Merge pull request #15 from collove/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
lnxpy authored Aug 1, 2022
2 parents f9c8430 + c10015d commit cb89fd0
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 85 deletions.
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down
137 changes: 81 additions & 56 deletions src/pasteme_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,82 +29,107 @@
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.',
epilog=EPILOG_DESCRIPTION,
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)
18 changes: 13 additions & 5 deletions src/pasteme_cli/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Language choices

LANGUAGES = {
'bash': 'Command Language',
'c': 'C Language',
Expand All @@ -16,7 +15,7 @@
'php': 'PHP Language',
'plaintext': 'PlainText',
'python': 'Python Language',
'rb': 'Ruby Language'
'rb': 'Ruby Language',
}

THEMES = {
Expand All @@ -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'
Expand All @@ -53,4 +61,4 @@
# Information

EPILOG_DESCRIPTION = '''Author -> Sadra Yahyapour (mailto:[email protected])
GitHub -> https://github.com/collove/pasteme-cli'''
GitHub -> https://github.com/collove/pasteme-cli'''
2 changes: 1 addition & 1 deletion src/pasteme_cli/sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .pasteme import Snippet
from .pasteme import Snippet
28 changes: 9 additions & 19 deletions src/pasteme_cli/sdk/pasteme.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

return response
5 changes: 2 additions & 3 deletions tests/test_snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,4 +22,4 @@ def test_push_snippet(self):


if __name__ == '__main__':
unittest.main()
unittest.main()

0 comments on commit cb89fd0

Please sign in to comment.