-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery refactored master branch #18
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,7 @@ def populate(parentNode, request): | |
window.FindElement('-QUERY-').update(request.url()) | ||
window.FindElement('-LOADING-').update(visible=True) | ||
|
||
if not parentNode in openNodes: | ||
if parentNode not in openNodes: | ||
passes = 0 | ||
from_cache = False | ||
try: | ||
|
@@ -80,25 +80,34 @@ def populate(parentNode, request): | |
cache[request.url()] = resp | ||
passes += 1 | ||
except: | ||
sg.popup("We're sorry!", request.url() + ' could not be fetched. Try again later.') | ||
sg.popup( | ||
"We're sorry!", | ||
f'{request.url()} could not be fetched. Try again later.', | ||
) | ||
if passes == 1: | ||
try: | ||
menu = trim_menu(resp.menu()) | ||
passes += 1 | ||
except: | ||
sg.popup("We're sorry!", request.url() + ' could not be parsed as a menu for one reason or another.') | ||
sg.popup( | ||
"We're sorry!", | ||
f'{request.url()} could not be parsed as a menu for one reason or another.', | ||
) | ||
if passes == 2: | ||
if from_cache: | ||
gophertree.insert(parentNode, request.url() + ' <cached>', text='- This is a cached menu, double click to go to the live version -', values=[], icon=icons['cache']) | ||
gophertree.insert( | ||
parentNode, | ||
f'{request.url()} <cached>', | ||
text='- This is a cached menu, double click to go to the live version -', | ||
values=[], | ||
icon=icons['cache'], | ||
) | ||
for item in menu: | ||
if not item.request().url() in openNodes: | ||
if item.request().url() not in openNodes: | ||
sub_url = item.request().url() | ||
if item.path.startswith("URL:"): | ||
sub_url = item.path[4:] | ||
if item.type in icons: | ||
icon = icons[item.type] | ||
else: | ||
icon = icons['9'] | ||
icon = icons[item.type] if item.type in icons else icons['9'] | ||
if item.type == 'i': | ||
gophertree.insert(parentNode, sub_url, | ||
text=item.text, values=[], icon=icon) | ||
|
@@ -118,17 +127,21 @@ def download_thread(req, dlpath, gui_queue): # This uses Pituophis' Request( | |
with open(dlpath, "wb") as dl: | ||
remote_file = req.stream().makefile('rb') | ||
while True: | ||
piece = remote_file.read(1024) | ||
if not piece: | ||
if piece := remote_file.read(1024): | ||
dl.write(piece) | ||
else: | ||
break | ||
dl.write(piece) | ||
Comment on lines
-121
to
-124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Walrus (:=) requires Python 3.8+ |
||
gui_queue.put(dlpath) # put a message into queue for GUI | ||
|
||
history = [] | ||
|
||
def dlPopup(url): | ||
return sg.popup_get_file('Where to save this file?', 'Download {}'.format( | ||
url), default_path=url.split('/')[-1], save_as=True) | ||
return sg.popup_get_file( | ||
'Where to save this file?', | ||
f'Download {url}', | ||
default_path=url.split('/')[-1], | ||
save_as=True, | ||
) | ||
Comment on lines
-130
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def go(url): | ||
global gophertree, openNodes, loadedTextURL | ||
|
@@ -152,19 +165,17 @@ def go(url): | |
loadedTextURL = req.url() | ||
window.FindElement('-OUTPUT-').update(resp.text()) | ||
except: | ||
sg.popup("We're sorry!", req.url() + ' could not be fetched. Try again later.') | ||
sg.popup("We're sorry!", f'{req.url()} could not be fetched. Try again later.') | ||
else: | ||
dlpath = dlPopup(req.url()) | ||
if not dlpath is None: | ||
window.FindElement('-DOWNLOADS-').update(value='Downloading {}'.format(dlpath)) | ||
if dlpath is not None: | ||
window.FindElement('-DOWNLOADS-').update(value=f'Downloading {dlpath}') | ||
Comment on lines
-155
to
+172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
threading.Thread(target=download_thread, args=(req, dlpath, gui_queue), daemon=True).start() | ||
|
||
window.FindElement('-LOADING-').update(visible=False) | ||
|
||
def plural(x): | ||
if x > 1 or x < 1: | ||
return 's' | ||
return '' | ||
return 's' if x > 1 or x < 1 else '' | ||
Comment on lines
-165
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
previousvalue = None | ||
|
||
|
@@ -185,26 +196,25 @@ def plural(x): | |
url = url[:-9] | ||
del cache[url] | ||
go(url) | ||
else: | ||
if url.startswith('gopher'): | ||
req = pituophis.parse_url(url) | ||
if req.type == '1': | ||
parentNode = url | ||
if value['-USETREE-']: | ||
populate(parentNode, req) | ||
else: | ||
go(parentNode) | ||
elif req.type == '7': | ||
q = sg.popup_get_text('Search on ' + req.host, '') | ||
if not q is None: | ||
req.query = q | ||
go(req.url()) | ||
elif req.type != 'i': | ||
elif url.startswith('gopher'): | ||
req = pituophis.parse_url(url) | ||
if req.type == '1': | ||
parentNode = url | ||
if value['-USETREE-']: | ||
populate(parentNode, req) | ||
else: | ||
go(parentNode) | ||
elif req.type == '7': | ||
q = sg.popup_get_text(f'Search on {req.host}', '') | ||
if q is not None: | ||
req.query = q | ||
go(req.url()) | ||
elif req.type != 'i': | ||
go(req.url()) | ||
|
||
window.FindElement('-LOADING-').update(visible=False) | ||
else: | ||
os.startfile(url) | ||
window.FindElement('-LOADING-').update(visible=False) | ||
else: | ||
os.startfile(url) | ||
Comment on lines
+199
to
+217
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
previousvalue = value | ||
elif event == 'Go': | ||
go(value['-QUERY-'].rstrip()) | ||
|
@@ -223,7 +233,7 @@ def plural(x): | |
pyperclip.copy(loadedTextURL) | ||
elif event == 'Save...': | ||
dlpath = dlPopup(loadedTextURL) | ||
if not dlpath is None: | ||
if dlpath is not None: | ||
with open(dlpath, 'w') as f: | ||
f.write(value['-OUTPUT-']) | ||
|
||
|
@@ -236,7 +246,11 @@ def plural(x): | |
# if message received from queue, display the message in the Window | ||
if message: | ||
window.FindElement('-DOWNLOADS-').update(value='') | ||
if sg.popup_yes_no('Finished downloading {}. Would you like to open the downloaded file?'.format(message)): | ||
if sg.popup_yes_no( | ||
f'Finished downloading {message}. Would you like to open the downloaded file?' | ||
): | ||
os.startfile(message) | ||
window.FindElement('-CACHE-').update(value='{} menu{} in cache.'.format(len(cache), plural(len(cache)))) | ||
window.FindElement('-CACHE-').update( | ||
value=f'{len(cache)} menu{plural(len(cache))} in cache.' | ||
) | ||
window.close() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,13 +149,11 @@ def url(self): | |
""" | ||
protocol = 'gopher' | ||
path = self.path | ||
query = '' | ||
if not (self.query == ''): | ||
query = '%09' + self.query | ||
query = f'%09{self.query}' if self.query != '' else '' | ||
hst = self.host | ||
if not self.port == 70: | ||
hst += ':{}'.format(self.port) | ||
return '{}://{}/{}{}{}'.format(protocol, hst, self.type, path, query) | ||
if self.port != 70: | ||
hst += f':{self.port}' | ||
return f'{protocol}://{hst}/{self.type}{path}{query}' | ||
Comment on lines
-152
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
class Item: | ||
|
@@ -227,8 +225,7 @@ def parse_menu(source): | |
line) > 4: # discard Gopher+ and other naughty stuff | ||
line = line[:-1] | ||
line = '\t'.join(line) | ||
matches = re.match(r'^(.)(.*)\t(.*)\t(.*)\t(.*)', line) | ||
if matches: | ||
if matches := re.match(r'^(.)(.*)\t(.*)\t(.*)\t(.*)', line): | ||
Comment on lines
-230
to
+228
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
item.type = matches[1] | ||
item.text = matches[2] | ||
item.path = matches[3] | ||
|
@@ -251,12 +248,12 @@ def parse_url(url): | |
up = urlparse(url) | ||
|
||
if up.scheme == '': | ||
up = urlparse('gopher://' + url) | ||
up = urlparse(f'gopher://{url}') | ||
|
||
req.path = up.path | ||
if up.query: | ||
req.path += '?{}'.format(up.query) # NOT to be confused with actual gopher queries, which use %09 | ||
# this just combines them back into one string | ||
req.path += f'?{up.query}' | ||
# this just combines them back into one string | ||
Comment on lines
-254
to
+256
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
req.host = up.hostname | ||
req.port = up.port | ||
if up.port is None: | ||
|
@@ -347,7 +344,7 @@ def parse_gophermap(source, def_host='127.0.0.1', def_port='70', | |
if not path.startswith('URL:'): | ||
# fix relative path | ||
if not path.startswith('/'): | ||
path = realpath(gophermap_dir + '/' + path) | ||
path = realpath(f'{gophermap_dir}/{path}') | ||
Comment on lines
-350
to
+347
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
# globbing | ||
if '*' in path: | ||
|
@@ -387,10 +384,12 @@ def parse_gophermap(source, def_host='127.0.0.1', def_port='70', | |
while '' in splt: | ||
splt.remove('') | ||
s.text = splt[len(splt) - 1] | ||
if os.path.exists(file + '/gophertag'): | ||
s.text = ''.join(list(open( | ||
file + '/gophertag'))).replace( | ||
'\r\n', '').replace('\n', '') | ||
if os.path.exists(f'{file}/gophertag'): | ||
s.text = ( | ||
''.join(list(open(f'{file}/gophertag'))) | ||
.replace('\r\n', '') | ||
.replace('\n', '') | ||
) | ||
s.path = file.replace(pub_dir, '/', 1) | ||
s.path = re.sub(r'/{2}', r'/', s.path) | ||
s.host = host | ||
|
@@ -399,15 +398,12 @@ def parse_gophermap(source, def_host='127.0.0.1', def_port='70', | |
s.path = '' | ||
s.host = '' | ||
s.port = '0' | ||
if s.type == '1': | ||
d = 0 | ||
else: | ||
d = 1 | ||
if not s.path.endswith('gophermap'): | ||
if not s.path.endswith( | ||
'gophertag'): | ||
listing.append( | ||
[file, s, s.text, d]) | ||
d = 0 if s.type == '1' else 1 | ||
if not s.path.endswith( | ||
'gophermap' | ||
) and not s.path.endswith('gophertag'): | ||
listing.append( | ||
[file, s, s.text, d]) | ||
|
||
listing = natsorted(listing, | ||
key=itemgetter(0)) | ||
|
@@ -416,8 +412,7 @@ def parse_gophermap(source, def_host='127.0.0.1', def_port='70', | |
listing = natsorted(listing, | ||
key=itemgetter(3)) | ||
|
||
for item in listing: | ||
new_menu.append(item[1]) | ||
new_menu.extend(item[1] for item in listing) | ||
else: | ||
new_menu.append(errors['403_glob']) | ||
|
||
|
@@ -438,10 +433,7 @@ def parse_gophermap(source, def_host='127.0.0.1', def_port='70', | |
mime = mimetypes.guess_type( | ||
pub_dir + path)[0] | ||
if mime is None: # is directory or binary | ||
if os.path.isdir(file): | ||
s.type = '1' | ||
else: | ||
s.type = '9' | ||
s.type = '1' if os.path.isdir(file) else '9' | ||
else: | ||
for sw in mime_starts_with.keys(): | ||
if mime.startswith(sw): | ||
|
@@ -450,7 +442,7 @@ def parse_gophermap(source, def_host='127.0.0.1', def_port='70', | |
|
||
new_menu.append(item.source()) | ||
else: | ||
item = 'i' + item + '\t\t\t0' | ||
item = f'i{item}' + '\t\t\t0' | ||
new_menu.append(item) | ||
return new_menu | ||
|
||
|
@@ -492,32 +484,25 @@ def handle(request): | |
if os.path.isdir(res_path): | ||
# is directory | ||
if os.path.exists(res_path): | ||
if os.path.isfile(res_path + '/gophermap'): | ||
in_file = open(res_path + '/gophermap', "r+") | ||
gmap = in_file.read() | ||
in_file.close() | ||
menu = parse_gophermap(source=gmap, | ||
def_host=request.host, | ||
def_port=request.advertised_port, | ||
gophermap_dir=request.path, | ||
pub_dir=pub_dir) | ||
if os.path.isfile(f'{res_path}/gophermap'): | ||
with open(f'{res_path}/gophermap', "r+") as in_file: | ||
gmap = in_file.read() | ||
else: | ||
gmap = '?*\t\r\n' | ||
menu = parse_gophermap(source=gmap, | ||
def_host=request.host, | ||
def_port=request.advertised_port, | ||
gophermap_dir=request.path, | ||
pub_dir=pub_dir) | ||
return menu | ||
return parse_gophermap( | ||
source=gmap, | ||
def_host=request.host, | ||
def_port=request.advertised_port, | ||
gophermap_dir=request.path, | ||
pub_dir=pub_dir, | ||
) | ||
elif os.path.isfile(res_path): | ||
in_file = open(res_path, "rb") | ||
data = in_file.read() | ||
in_file.close() | ||
with open(res_path, "rb") as in_file: | ||
data = in_file.read() | ||
return data | ||
|
||
if request.alt_handler: | ||
alt = request.alt_handler(request) | ||
if alt: | ||
if alt := request.alt_handler(request): | ||
Comment on lines
-495
to
+505
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return alt | ||
|
||
e = errors['404'] | ||
|
@@ -536,7 +521,7 @@ def serve(host="127.0.0.1", port=70, advertised_port=None, | |
""" | ||
if pub_dir is None or pub_dir == '': | ||
pub_dir = '.' | ||
print('Gopher server is now running on', host + ':' + str(port) + '.') | ||
print('Gopher server is now running on', f'{host}:{str(port)}.') | ||
Comment on lines
-539
to
+524
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
class GopherProtocol(asyncio.Protocol): | ||
def connection_made(self, transport): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,9 @@ def go(url, itype=''): | |
# req.type = '1' parse_url() now does this in Pituophis 1.0 | ||
if itype == '7': | ||
req.type = itype | ||
print(bold('URL: ' + req.url())) | ||
if req.type == '7': | ||
if req.query == '': | ||
req.query = input(bold('Search term: ')) | ||
print(bold(f'URL: {req.url()}')) | ||
if req.type == '7' and req.query == '': | ||
req.query = input(bold('Search term: ')) | ||
Comment on lines
-32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if req.type in compatibleTypes: | ||
resp = req.get() | ||
if req.type in menuTypes: | ||
|
@@ -42,13 +41,13 @@ def go(url, itype=''): | |
text = typeIcons['9'] | ||
if selector.type in typeIcons: | ||
text = typeIcons[selector.type] | ||
text = text + ' ' + selector.text | ||
text = f'{text} {selector.text}' | ||
if selector.type not in noLinkTypes: | ||
items += 1 | ||
requests[items] = selector.request() | ||
text = text + ' (' + requests[items].url() + ') ' + bold('[#' + str(items) + ']') | ||
text = f"{text} ({requests[items].url()}) {bold(f'[#{items}]')}" | ||
if selector.path.startswith('URL:'): | ||
text = text + ' (' + selector.path.split('URL:')[1] + ')' | ||
text = f'{text} (' + selector.path.split('URL:')[1] + ')' | ||
print(text) | ||
elif req.type == '0': | ||
print(resp.text()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
populate
refactored with the following changes:de-morgan
)use-fstring-for-concatenation
)assign-if-exp
)