Merge pull request #8 from sloumdrone/develop

0.5.9 => Master
This commit is contained in:
Brian 2018-11-03 10:31:35 -07:00 committed by GitHub
commit 3615d47282
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 14 deletions

View File

@ -8,6 +8,7 @@ class connect:
def request(self, resource, host, itemtype, port=70): def request(self, resource, host, itemtype, port=70):
#connects to server and returns list with response type and body #connects to server and returns list with response type and body
socket_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_conn.settimeout(5.0)
try: try:
socket_conn.connect((host, port)) socket_conn.connect((host, port))
@ -17,15 +18,20 @@ class connect:
response = socket_conn.makefile(mode = 'rb', errors = 'ignore') response = socket_conn.makefile(mode = 'rb', errors = 'ignore')
else: else:
response = socket_conn.makefile(mode = 'r', errors = 'ignore') response = socket_conn.makefile(mode = 'r', errors = 'ignore')
except: except socket.timeout:
print('Socket timeout')
socket_conn.close() socket_conn.close()
return False return {'type': '3', 'body': '3ERROR: Server request timed out\tfalse\tnull.host\t1'}
except Exception as e:
print('Misc socket error: ', e)
socket_conn.close()
return {'type': '3', 'body': '3ERROR: Unable to communicate with remote server\tfalse\tnull.host\t1'}
try: try:
self.raw_response = response.read() self.raw_response = response.read()
self.filetype = itemtype self.filetype = itemtype
except UnicodeDecodeError: except UnicodeDecodeError:
self.raw_response = '3Error decoding server response\tfalse\tnull.host\t1' self.raw_response = '3ERROR: Unable to decode server response\tfalse\tnull.host\t1'
self.filetype = '3' self.filetype = '3'
socket_conn.close() socket_conn.close()

30
gui.py
View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import tkinter as tk import tkinter as tk
import tkinter.simpledialog as dialog
from connect import connect as conn from connect import connect as conn
from parser import parser from parser import parser
import time import time
@ -102,6 +103,7 @@ class GUI:
self.site_display.bind("<Button-1>", lambda event: self.site_display.focus_set()) self.site_display.bind("<Button-1>", lambda event: self.site_display.focus_set())
self.entry_url.bind("<Button-1>", lambda event: self.entry_url.focus_set()) self.entry_url.bind("<Button-1>", lambda event: self.entry_url.focus_set())
self.root.protocol('WM_DELETE_WINDOW', self.close_window) self.root.protocol('WM_DELETE_WINDOW', self.close_window)
self.btn_favorite.bind("<Button-1>", self.add_to_favorites)
def pack_geometry(self): def pack_geometry(self):
@ -220,6 +222,7 @@ class GUI:
self.entry_url.insert(tk.END, 'home') self.entry_url.insert(tk.END, 'home')
if event: if event:
self.add_to_history('home') self.add_to_history('home')
data += self.load_favorites()
self.send_to_screen(data, '1') self.send_to_screen(data, '1')
@ -241,13 +244,31 @@ class GUI:
self.handle_request(False, href, False) self.handle_request(False, href, False)
def add_to_favorites(self, event):
favorite_name = dialog.askstring("Add to favorites", "What would you like to title this favorite?")
url = self.entry_url.get()
if not favorite_name or not url:
return False
favorite = {"url": url, "name": favorite_name}
self.config["favorites"].append(favorite)
self.write_config(self.config)
#-------------Start view methods---------------- #-------------Start view methods----------------
def load_favorites(self): def load_favorites(self):
header = 'i#############\tfalse\tnull.host\t1\r\ni manually edit in go.config.json\tfalse\tnull.host\t1\r\n or add using the favorites button\tfalse\tnull.host\t1\r\ni\tfalse\tnull.host\t1\r\n' header = ''
#soon add code to load in favorites here #soon add code to load in favorites here
self.send_to_screen(data=header, clear=False) for x in self.config["favorites"]:
url = self.parser.parse_url(x["url"])
if not url:
continue
entry = '{}{}\t{}\t{}\t{}\n'.format(url['type'], x['name'], url['resource'], url['host'], url['port'])
header += entry
return header
# self.send_to_screen(data=header, clear=False)
def show_search(self): def show_search(self):
text1 = ' __ ___ __ __\n/__` |__ /\ |__) / ` |__|\n.__/ |___ /~~\ | \ \__, | |\n\n\nPlease enter your search terms and press the enter key:\n\n' text1 = ' __ ___ __ __\n/__` |__ /\ |__) / ` |__|\n.__/ |___ /~~\ | \ \__, | |\n\n\nPlease enter your search terms and press the enter key:\n\n'
@ -303,7 +324,6 @@ class GUI:
elif x['type'] == '3': elif x['type'] == '3':
self.site_display.insert(tk.END,' \t\t{}\n'.format(x['description'])) self.site_display.insert(tk.END,' \t\t{}\n'.format(x['description']))
elif x['type'] in types: elif x['type'] in types:
# adapted from: # adapted from:
# https://stackoverflow.com/questions/27760561/tkinter-and-hyperlinks # https://stackoverflow.com/questions/27760561/tkinter-and-hyperlinks
if x['port'] and x['port'][0] != ':': if x['port'] and x['port'][0] != ':':
@ -334,9 +354,11 @@ class GUI:
def show_text(self, data): def show_text(self, data):
if data[-2:] == '\n.':
data = data[:-2]
self.site_display.config(state=tk.NORMAL) self.site_display.config(state=tk.NORMAL)
self.site_display.delete(1.0, tk.END) self.site_display.delete(1.0, tk.END)
self.site_display.insert(tk.END, data[:-2]) self.site_display.insert(tk.END, data)
self.site_display.config(state=tk.DISABLED) self.site_display.config(state=tk.DISABLED)

View File

@ -15,6 +15,8 @@ class parser:
def parse_url(self, url): def parse_url(self, url):
# Take in a URL and output a dict of the url parts # Take in a URL and output a dict of the url parts
if url == 'home':
return False
regex = r'^(?P<protocol>(?:(gopher|http):\/\/)?)?(?P<host>[\w\.\d]+)(?P<port>(?::\d+)?)?(?P<type>(?:\/[\dgIp])?)?(?P<resource>(?:\/.*)?)?$' regex = r'^(?P<protocol>(?:(gopher|http):\/\/)?)?(?P<host>[\w\.\d]+)(?P<port>(?::\d+)?)?(?P<type>(?:\/[\dgIp])?)?(?P<resource>(?:\/.*)?)?$'
@ -23,13 +25,13 @@ class parser:
if not match: if not match:
return False return False
protocol = match.group('protocol') try:
itemtype = match.group('type') protocol = match.group('protocol')
host = match.group('host') itemtype = match.group('type')
port = match.group('port') host = match.group('host')
resource = match.group('resource') port = match.group('port')
resource = match.group('resource')
if not host: except:
return False return False
if not resource: if not resource: