-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsoleParser.py
More file actions
84 lines (59 loc) · 1.54 KB
/
consoleParser.py
File metadata and controls
84 lines (59 loc) · 1.54 KB
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
class Header:
filename = ""
filesize = 0
class MsgPacket:
username = ""
msg = ""
def parsear(com):
######### The structure of a command is: COMMAND PARAMETER1 PARAMETER2 [...] PARAMETER-N
######### The user can also use "" to simbolize that spaces are considered part of a parameter, like this: COMMAND PARAMETER1 "STRING WITH SPACES AS PARAMETER2" PARAMETER3 [...]
com += " "
acumulador = ""
palabras = []
onsameword = False
for caracter in com:
if caracter == '"':
onsameword = not onsameword
continue
if onsameword: acumulador += caracter
elif caracter != " ": acumulador += caracter
else:
palabras.append(acumulador)
acumulador = ""
return palabras
def parseHeader(packHead):
######### The structure of a header is: <FILENAME|FILESIZE>
retMe = Header()
retMe.filename = ""
infilesize = False
auxcount = 0
auxstring = ""
if packHead[0] != '<':
return retMe
for x in range(1, len(packHead)):
if packHead[x] == '|':
break
retMe.filename += packHead[x]
auxcount = x + 1
if auxcount >= len(packHead):
return retMe
if packHead[auxcount] != '|':
return retMe
for r in range(auxcount+1, len(packHead)):
if packHead[r] == '>': break
auxstring += packHead[r]
retMe.filesize = int(auxstring)
return retMe
def parseMessage(message):
######### The structure of a message is: USERNAME|MESSAGE
retMe = MsgPacket()
inusername = True
for char in message:
if inusername:
if char == "|":
inusername = False
continue
retMe.username += char
else:
retMe.msg += char
return retMe