-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowse.py
175 lines (137 loc) · 4.76 KB
/
browse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from urllib.parse import urlparse
import socketManagement as conn
import tkHyperlinkManager
import constans as const
def linkClick(link_type, link_name, domain, adress, port):
"""Describe how programm will behave after clicking on link."""
raise Exception("Fail while executing callback.")
def parseMenuItem(line):
"""Parse menu item.
Return:
If valid menu item:
(tuple) [0] - link type
[1] - link name
[2] - adress
[3] - domain
[4] - port
Else:
(str)
"""
# Slice the line
menu_item = line.replace("\r\n", "")
menu_item = line.strip(" ")
menu_item_details = []
menu_item_details.append(menu_item[0:1])
menu_item = menu_item[1:]
menu_item_details += menu_item.split(const.TAB)
# Valid menu item consist 5 elements
# type:name:adress:domain:port
if len(menu_item_details) != 5:
return line
return (menu_item_details[0],
menu_item_details[1],
menu_item_details[2],
menu_item_details[3],
menu_item_details[4].replace("\r\n", "")
)
def parseLine(line, hyperlink_manager):
"""Parse line in gopher standards.
Return :
(tuple) [0] - parsed line
[1] - tag
"""
parsed_line = line.decode(const.DECODING_STANDARD, errors="replace")
tag = ""
if not line:
parsed_line = 'Problem while displaying line.'
tag = "error"
return "", tag
# END OF PAGE
if parsed_line.replace(const.CRLF, "") == '.':
return "", tag
# I don`t why I get it sometimes.
# parsed_line = parsed_line.replace("error.host", "")
# parsed_line = parsed_line.replace("null.host", "")
# International message
if parsed_line.startswith("i") and const.TAB in parsed_line:
parsed_line = parsed_line[1:]
parsed_line = parsed_line.split(const.TAB)[0]
parsed_line += const.CRLF
tag = "international"
return const.TAB + parsed_line, tag
# Special line - menu item
if parsed_line.startswith(tuple(const.TYPENAME.keys())):
parsed_line = parseMenuItem(parsed_line)
if type(parsed_line) == tuple:
link_details = [
parsed_line[0], # link type
parsed_line[1], # link name
parsed_line[2], # link adress
parsed_line[3], # link domain
parsed_line[4] # link port
]
tag = hyperlink_manager.add(linkClick, link_details)
parsed_line = const.TYPENAME[parsed_line[0]] \
+ const.TAB + parsed_line[1] + const.CRLF
return parsed_line, tag
return parsed_line, tag
def go(tk_adress_field, tk_browser_field):
"""Run the connection."""
# Prepare information about connection
parsed_url = urlparse(tk_adress_field.get())
scheme = parsed_url.scheme
hostname = parsed_url.hostname
adress = parsed_url.path
port = parsed_url.port
query = parsed_url.query
# Set progresbar handler
conn.progressHandler = progressHandler
# When is not link process to Veronica
if hostname is None:
hostname = "gopher.floodgap.com"
adress = "v2/vs"
port = 70
query = tk_adress_field.get()
# When URL have arguments add needed TAB
if query:
query = const.TAB + query
# Prepare browser screen for new data
hyperlink_manager = tkHyperlinkManager.HyperlinkManager(
tk_browser_field,
downloadFile)
tk_browser_field.config(state="normal")
tk_browser_field.delete('1.0', "end")
# Establish connection and receive data
connection = conn.openConnection(hostname, port)
page_source = conn.getData(connection, adress + query)
# DEBUG
# import os
# os.system('cls')
# Parse received data
for line in page_source.readlines():
# print(line)
parsed_line = parseLine(line, hyperlink_manager)
tk_browser_field.insert("insert", parsed_line[0], parsed_line[1])
tk_browser_field.config(state="disabled")
return 1
def downloadFile(url, name):
"""Download the file and save on disc."""
parsed_url = urlparse(url)
scheme = parsed_url.scheme
hostname = parsed_url.hostname
adress = parsed_url.path
port = parsed_url.port
query = parsed_url.query
# Establish connection and receive data
connection = conn.openConnection(hostname, port)
file_source = conn.getData(connection, adress + query)
# Save received data
with open(name, "wb") as file:
for line in file_source:
file.write(line)
return 1
def progressHandler(state):
"""Progress bar handler.
State can eqaual to 0, n mod 6, 6.
"""
pass