/
shoppingList.py
executable file
·266 lines (226 loc) · 6.9 KB
/
shoppingList.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python
# -*- encoding:utf-8 -*-
from sys import argv
import cPickle
from urllib2 import urlopen
import updateChecker
# -----------------------------------------------------------
# --------------------- VARIABLES ---------------------------
# -----------------------------------------------------------
# -----------------------------------------------------------
version = 1
serverList = []
usr = ""
filepath = "./pickle"
CONFIG = "./sconfig"
password = ""
server = ""
checked = False
serverPicklePath = "./sPickle"
# Try to open file, to see if it exist, and
# if it fail it will make the file.
list = []
try:
file = open(filepath,"r")
list = cPickle.load(file)
file.close()
except IOError:
file = open(filepath,"w")
file.close()
except EOFError:
print "The program met an unexpected end, which probably means this is the first time running this program, or that the file is corrupted."
try:
config = open(CONFIG,"r")
config.close()
except IOError:
config = open(CONFIG,"w")
config.close()
# -----------------------------------------------------------
# --------------------- UPDATE RELATED ---------------------
# -----------------------------------------------------------
# -----------------------------------------------------------
def versionCheck():
print "You are using version:"+str(version)
if updateChecker.shoppingCheck(version) == 1:
print "ShoppingList is up to date"
elif updateChecker.shoppingCheck(version) == 0:
print "The shoppingList is outdated"
# -----------------------------------------------------------
# ---------------- SERVER RELATED FUNCTIONS -----------------
# -----------------------------------------------------------
# -----------------------------------------------------------
# A function for loading settings from a config file:
def getSettings(test=False):
config = open(CONFIG,"r")
global usr
global password
global server
global checked
for setting in config:
parts = setting.split("=")
if parts[0] == "USER":
usr = parts[1].rstrip("\n")
elif parts[0] == "PASS":
password = parts[1].rstrip("\n")
elif parts[0] == "SERVER":
server = parts[1].rstrip("\n")
elif parts[0] == "CHECK":
if parts[1].rstrip("\n") == "1":
checked = True
elif parts[1].rstrip("\n") == "0":
checked = False
if (usr == "" or password == "" or server == "") and test == False:
if usr == "":
print "Set usr"
if password == "":
print "Set password"
if server == "":
print "Set server"
exit()
#TODO
# A function for getting the pickle from the server.
def getPickleServer():
getSettings()
address = server+"?who="+usr+"&pass="+password+"&cmd=REQ"
response = urlopen(address)
serverPickle = open(serverPicklePath,"w")
serverPickle.write(response.read())
serverPickle.close()
#TODO - finish up
def makeNewConfig():
config = open(CONFIG,"w")
config.write("SERVER="+server+"\n")
config.write("USER="+usr+"\n")
config.write("PASS="+password+"\n")
config.write("CHECK="+str(checked))
config.close()
def defineServer(name):
getSettings(True)
global server
server = name
makeNewConfig()
def defineUser(name):
getSettings(True)
global usr
usr = name
makeNewConfig()
def definePassword(name):
getSettings(True)
global password
password = name
makeNewConfig()
def defineChecked(value):
getSettings(True)
global checked
checked = value
makeNewConfig()
def printPickleServer():
getPickleServer()
serverPickle = open(serverPicklePath,"r")
global serverList
serverList = cPickle.load(serverPickle)
printList(serverList)
serverPickle.close()
def addPickleServer(item):
getSettings()
address = server+"?who="+usr+"&pass="+password+"&cmd=ADD&item="+item
response = urlopen(address)
def removePickleServer(item):
getSettings()
address = server+"?who="+usr+"&pass="+password+"&cmd=REM&item="+item
response = urlopen(address)
# -----------------------------------------------------------
# ---------------- INTERNAL FUNCTIONS -----------------------
# -----------------------------------------------------------
# -----------------------------------------------------------
# A fuction to add items to the shopping list
def addItem(x):
list.append(x)
print x,"is added to the shopping list"
#function to remove items
def removeItems(name_of_item):
# Goes through list looking for a match
# if no match found it will give a
# message saying that the item was
# not found.
for i in list:
if name_of_item == i:
list.remove(name_of_item)
print "Success"
return True
print "The item,",name_of_item,"was not found in the list."
# Simply prints out the items on the list.
def printList(thisList):
for i in thisList:
print i
# Print help menu
def printHelp():
print "Options\n----------------\n -h or --help - displays this.\n -a - add the following string to the shopping list. \n -r - remove the following string from the list, if found.\n -p or --print - prints the items currently on the list\n"
print " --user - set username for external server\n --password - set password for external server\n --server - set server address\n"
print " --sadd - add item to server shoppingList\n --srem - remove item from server shoppingList\n --serverList - print the list from the server\n"
print " --version or -v - print if the version is up to date\n"
# -----------------------------------------------------------
# ----------------------- MAIN BODY -------------------------
# -----------------------------------------------------------
# -----------------------------------------------------------
def eventHandler():
valid = False
if len(argv) == 3:
# If sys holds 3 arguments it will check if the second
# hold one of the 4 commands that match.
if(argv[1] == "-a"):
addItem(argv[2])
valid = True
elif(argv[1] == "-r"):
removeItems(argv[2])
valid = True
elif(argv[1] == "--sadd"):
addPickleServer(argv[2])
elif(argv[1] == "--srem"):
removePickleServer(argv[2])
elif(argv[1] == "--server"):
defineServer(argv[2])
elif(argv[1] == "--user"):
defineUser(argv[2])
elif(argv[1] == "--password"):
definePassword(argv[2])
else:
printHelp()
if valid:
try:
file = open(filepath,"w")
cPickle.dump(list,file)
file.close()
except IOError:
print "Something went wrong, when writing or opening the storage file."
elif len(argv) == 2:
if argv[1] == "-p" or argv[1] == "--print":
printList(list)
elif argv[1] == "-h" or argv[1] == "--help":
printHelp()
elif argv[1] == "--clear":
clearList()
elif(argv[1] == "--serverList"):
printPickleServer()
elif(argv[1] == "--version") or (argv[1] == "-v"):
versionCheck()
else:
printHelp()
if len(argv) > 3:
printHelp()
def clearList():
file = open(filepath,"w")
file.close();
def guiAddItem(thisX):
list.append(thisX)
file=open(filepath,"w")
cPickle.dump(list,file)
file.close()
def guiRemoveItem(thisX):
removeItems(thisX)
file=open(filepath,"w")
cPickle.dump(list,file)
file.close()
# Start eventHandler
getSettings(True)
eventHandler()