forked from nst/gmap_tiles
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sources.py
executable file
·111 lines (99 loc) · 3.1 KB
/
sources.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
import json
def savejson(filename, struct):
try:
for key in struct["sources"]:
tmp = struct["sources"][key]
newkey = str(hash(json.dumps(tmp)))
del struct["sources"][key]
struct["sources"][newkey] = tmp
except:
pass
with open(filename,'w') as fd:
fd.write( json.dumps(struct, indent=4, sort_keys=True) )
def openjson(filename):
with open(filename, 'r') as fd:
t = fd.read()
s = json.loads(t)
if len(s) <= 0: return {}
return s
def ppjson(inputs):
"""
Pretty prints inputs which are assumed json filename, text, or structure.
"""
try:
with open(inputs, 'r') as fd:
print json.dumps(json.loads(fd.read()), indent=4, sort_keys=True)
except:
try:
print json.dumps(json.loads(inputs), indent=4, sort_keys=True)
except:
print json.dumps(inputs, indent=4, sort_keys=True)
def addSource(filename, type,name,prefix,postfix,x,y,zoom,notes="",ext='png', DEBUG=False):
try:
struct = openjson(filename)
except:
if DEBUG: print 'unable to save json contents'
return False
new = {}
new["type"] = str(type)
new["name"] = str(name)
new["prefix"] = str(prefix)
new["postfix"] = str(postfix)
new["x"] = str(x)
new["y"] = str(y)
new["zoom"] = str(zoom)
new["ext"] = str(ext)
new["notes"] = str(notes)
uid = str(hash( json.dumps(new) ))
try:
len(struct["sources"])
except:
struct["sources"] = {}
struct["sources"][uid] = new
try:
savejson(filename, struct)
except:
if DEBUG: print 'unable to save json contents'
return False
return True
def searchSource(filename, search={}, DEBUG=False):
try:
struct = openjson(filename)
except:
struct = {"sources":{}}
output = {"sources":{}}
if DEBUG: print json.dumps(struct, indent=4, sort_keys=True)
if DEBUG: print search.keys()
for uid in struct["sources"].keys():
for attrib in search.keys():
try:
val = struct["sources"][uid][attrib]
test = (val.find( search[attrib] )>=0)
if DEBUG: print [uid,attrib,val,search[attrib], test]
if test:
output["sources"][uid] = struct["sources"][uid]
except:
pass
return output["sources"]
def rmSource(filename, uid):
try:
struct = openjson(filename)
del struct["sources"][uid]
savejson(filename, struct)
except:
return False
return True
def main():
print '-- Insert Test Source'
fname='sources.json'
addSource(fname, 'Satellite','Test Source','www.google.com/','&fetch=True','&x=','&y=','&z=')
ppjson(fname)
print '-- Search For Test Source'
found = searchSource(fname, search={"name":"ource"})
ppjson(found)
print '-- Remove Test Source'
for key in found.keys():
print '--- Removing: ', key, rmSource(fname, key)
ppjson(fname)
if __name__ == '__main__':
main()