Skip to content

Commit 16101aa

Browse files
committed
hmm... v0.3.0 added proper error handling, unneeded imports and version info, along with __main__
1 parent 0959880 commit 16101aa

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

mystbin/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@
2222
DEALINGS IN THE SOFTWARE.
2323
"""
2424

25+
from collections import namedtuple
26+
2527
from .client import MystbinClient
2628
from .errors import *
29+
30+
__version__ = "0.2.5"
31+
VersionInfo = namedtuple("VersionInfo", "major minor micro releaselevel serial")
32+
version_info = VersionInfo(major=0, minor=2, micro=5, releaselevel='final', serial=0)

mystbin/__main__.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2020 AbstractUmbra
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a
7+
copy of this software and associated documentation files (the "Software"),
8+
to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
and/or sell copies of the Software, and to permit persons to whom the
11+
Software is furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
DEALINGS IN THE SOFTWARE.
23+
"""
24+
25+
import argparse
26+
import platform
27+
import sys
28+
29+
import aiohttp
30+
import pkg_resources
31+
try:
32+
import requests
33+
except ImportError:
34+
requests = None
35+
36+
import mystbin
37+
38+
39+
def show_version():
40+
entries = []
41+
42+
entries.append(
43+
'- Python v{0.major}.{0.minor}.{0.micro}-{0.releaselevel}'.format(sys.version_info))
44+
version_info = mystbin.version_info
45+
entries.append(
46+
'- mystbin.py v{0.major}.{0.minor}.{0.micro}-{0.releaselevel}'.format(version_info))
47+
if version_info.releaselevel != 'final':
48+
pkg = pkg_resources.get_distribution('mystbin.py')
49+
if pkg:
50+
entries.append(
51+
' - mystbin.py pkg_resources: v{0}'.format(pkg.version))
52+
53+
entries.append('- aiohttp v{0.__version__}'.format(aiohttp))
54+
if requests is not None:
55+
entries.append(' - [requests] v{0.__version__}'.format(requests))
56+
uname = platform.uname()
57+
entries.append(
58+
'- system info: {0.system} {0.release} {0.version}'.format(uname))
59+
print('\n'.join(entries))
60+
61+
62+
def parse_args():
63+
parser = argparse.ArgumentParser(
64+
prog='discord', description='Tools for helping with discord.py')
65+
parser.add_argument('-v', '--version', action='store_true',
66+
help='shows the library version')
67+
parser.set_defaults(func=core)
68+
69+
return parser, parser.parse_args()
70+
71+
72+
def core(parser, args):
73+
if args.version:
74+
show_version()
75+
76+
77+
def main():
78+
parser, args = parse_args()
79+
args.func(parser, args)
80+
81+
82+
main()

mystbin/client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626
import json
2727
from typing import TYPE_CHECKING, Awaitable, Callable, Optional, Type, Union
2828

29+
import aiohttp
2930
if TYPE_CHECKING:
3031
import requests
3132

32-
import aiohttp
33-
import yarl
34-
3533
from .constants import *
3634
from .errors import *
3735
from .objects import *
@@ -102,7 +100,7 @@ def _perform_sync_post(self, content: str, syntax: str = None) -> Paste:
102100
""" Sync post request. """
103101
payload = {'meta': [{'index': 0, 'syntax': syntax}]}
104102
response: Type["requests.Response"] = self.session.post(API_BASE_URL, files={
105-
'data': content, 'meta': (None, json.dumps(payload), 'application/json')}, timeout=CLIENT_TIMEOUT)
103+
'data': content, 'meta': (None, json.dumps(payload), 'application/json')}, timeout=CLIENT_TIMEOUT)
106104
if response.status_code not in [200, 201]:
107105
raise APIError(response.status_code, response.text)
108106

@@ -122,7 +120,7 @@ async def _perform_async_post(self, content: str, syntax: str = None) -> Paste:
122120
async with self.session.post(API_BASE_URL, data=multi_part_write) as response:
123121
status_code = response.status
124122
response_text = await response.text()
125-
if status_code not in [200, 201]:
123+
if status_code not in (200, ):
126124
raise APIError(status_code, response_text)
127125
response_data = await response.json()
128126

@@ -149,16 +147,20 @@ def get(self, paste_id: str) -> Union[PasteData, Awaitable]:
149147

150148
def _perform_sync_get(self, paste_id: str, syntax: str = None) -> PasteData:
151149
""" Sync get request. """
152-
response = self.session.get(
150+
response: requests.Response = self.session.get(
153151
f"{API_BASE_URL}/{paste_id}", timeout=CLIENT_TIMEOUT)
152+
if response.status_code not in (200, ):
153+
raise BadPasteID("This is an invalid Mystb.in paste ID.")
154154
paste_data = response.json()
155155
return PasteData(paste_id, paste_data)
156156

157157
async def _perform_async_get(self, paste_id: str, syntax: str = None) -> PasteData:
158158
""" Async get request. """
159159
if not self.session:
160-
self.session = await self._generate_async_session()
160+
self.session: aiohttp.ClientSession = await self._generate_async_session()
161161
async with self.session.get(f"{API_BASE_URL}/{paste_id}", timeout=aiohttp.ClientTimeout(CLIENT_TIMEOUT)) as response:
162+
if response.status not in (200, ):
163+
raise BadPasteID("This is an invalid Mystb.in paste ID.")
162164
paste_data = await response.json()
163165
return PasteData(paste_id, paste_data)
164166

0 commit comments

Comments
 (0)