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
|
from classes import PersonalError, RED, GREY
from listcommands import ListCommands
from config import config as co
class Commands(object):
def command(self, what, cmd):
"""
this function is used for execute a command
"""
if what == 'set':
try:
if cmd[1] == 'user' and cmd[2] is not None: #set name for user
if len(cmd[2]) > 10:
raise PersonalError('lunghezza maggiore del consetito. Max 10')
if len(cmd) > ListCommands.info['set'][1]:
raise PersonalError(GREY + 'set user' + RED + ' accetta 1 parametro')
self.user = cmd[2]
with open(co['path'], 'rb') as fin:
listOld = [x.decode('utf-8') for x in fin.readlines()]
del listOld[0]
with open(co['path'], 'wb') as fout:
fout.write((cmd[2] + '\n').encode('utf-8'))
for i in listOld:
fout.write((i).encode('utf-8'))
del listOld
print('Ok')
elif cmd[1] is not ListCommands.commands['set']:
raise KeyError
except (IndexError, KeyError) as e:
ListCommands.err(type(e).__name__)
except PersonalError as e:
ListCommands.err('personal', e.value)
elif what == 'get':
try:
if len(cmd) > ListCommands.info['get'][1]:
raise PersonalError(GREY + 'get ' + RED + 'accetta 2 parametri')
if cmd[1] == 'i': #if first word after get is 'i', there is an info
if cmd[2] == 'user':
#if self.user is empty, print 'nil'
if self.user != '':
with open(co['path'], 'rb') as fin:
print(fin.readline().decode('utf-8'))
else:
print('nil')
elif cmd[2] not in ListCommands.commands['get'][1]:
raise KeyError
else:
print(co[cmd[2]])
elif cmd[1] == 'todo':
with open(co['path'], 'rb') as fout:
try:
lines = fout.readlines()
except Exception as e:
lines = ''
if lines is not None:
todolist = [x.strip() for i, x in enumerate(lines) if i > 0]
else:
todolist = []
if len(todolist) == 0: #if todo is empty
print('nessun todo in lista: goditi una Stout')
else:
for num, i in enumerate(todolist): #num = index, i = value
print('| {} |\t {}'.format(num, i.decode('utf-8')))
elif cmd[1] == 'ctodo':
count = countID()
if count is 0: #if idTODO is None or 0
print('0')
else:
print(count)
else:
raise KeyError
except (IndexError, KeyError) as e:
ListCommands.err(type(e).__name__)
except PersonalError as e:
ListCommands.err('personal', e.value)
elif what == 'add':
try:
msg = ' '.join(cmd[1:]) #join first word after 'add' to last word
try:
with open(co['path'], 'ab') as fout:
fout.seek(1, 1)
fout.write((msg + '\n').encode('utf-8'))
print('Ok')
except:
raise KeyError
except (IndexError, KeyError):
ListCommands.err(type(e).__name__)
elif what == 'del':
try:
if len(cmd) > ListCommands.info['del'][1]:
raise PersonalError(GREY + 'del ' + RED + 'accetta 1 parametro')
count = countID()
if count == 0: #todo list is empty
print('nessun todo in lista: goditi una Stout')
elif int(cmd[1]) >= count:
print('nessun todo con questo id')
else:
try:
with open(co['path'], 'rb') as fin:
lines = [x.strip() for x in fin.readlines()]
del lines[int(cmd[1])+1]
with open(co['path'], 'wb') as fout:
for line in lines:
fout.write(line + ('\n'.encode('utf-8')))
print('Ok')
except Exception as e:
print(e)
except (IndexError, KeyError) as e:
ListCommands.err(type(e).__name__)
def countID():
with open(co['path'], 'rb') as fout:
for i, _ in enumerate(fout.readlines()):
count = i
return count
|