-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.py
168 lines (154 loc) · 7.18 KB
/
registry.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
import os, zipfile, StringIO
filepaths = ("data/art", "data/music", "data/sfx", "movies")
ignore = ".hg"
priority = ["png","jpg","gif","bmp","mp3","ogg"]
class File:
def __init__(self,path):
self.path = path
self.filename = self.path.rsplit("/",1)[1]
self.pathtag = self.path
self.filetag = self.filename
self.ext = ""
if "." in self.filename:
self.pathtag = self.path.rsplit(".",1)[0]
self.filetag, self.ext = self.filename.rsplit(".", 1)
self.priority = 12
if self.ext in priority:
self.priority = priority.index(self.ext)
self.filetag = self.filetag.lower()
self.pathtag = self.pathtag.lower()
self.pathtagext = self.pathtag+"."+self.ext
def __repr__(self):
return self.path
def testfile():
a = File("../data/art/port/kristoph2/normal(talk).txt")
b = File("../data/art/port/kristoph2/normal(talk).png")
c = File("../data/art/port/kristoph12/normal(talk).jpg")
d = File("../data/art/port/kristoph2/normal(talk)")
assert a.path=="../data/art/port/kristoph2/normal(talk).txt"
assert b.path=="../data/art/port/kristoph2/normal(talk).png"
assert c.path=="../data/art/port/kristoph12/normal(talk).jpg"
assert d.path=="../data/art/port/kristoph2/normal(talk)"
assert a.filetag==b.filetag==c.filetag==d.filetag
assert a.pathtag==b.pathtag!=c.pathtag
assert b.priority<c.priority,"bpri:%s cpri:%s"%(b.priority,c.priority)
assert c.priority<d.priority
global_registry_cache = {}
class Registry:
def __init__(self,root=None,use_cache=True):
self.map = {}
self.ext_map = {}
self.use_cache = use_cache
if root:
self.build(root)
def open(self,path,mode="rb"):
if ".zip/" in path:
normal,zip = path.split(".zip/",1)
zf = zipfile.ZipFile(normal+".zip")
return StringIO.StringIO(zf.open(zip,"r").read())
return open(path,mode)
def clear_cache(self):
global_registry_cache.clear()
def build(self,root):
if self.use_cache and root in global_registry_cache:
self.map,self.ext_map = global_registry_cache[root]
return
self.root = root
for sub in filepaths:
if os.path.isdir(root+"/"+sub):
self.index(root+"/"+sub)
global_registry_cache[root] = [self.map,self.ext_map]
def list_files(self,path):
if os.path.isdir(path):
return os.listdir(path)
if zipfile.is_zipfile(path):
return zipfile.ZipFile(path,"r").namelist()
def index(self,path):
in_zip = zipfile.is_zipfile(path)
subdirs = []
for sub in self.list_files(path):
if sub==".hg":
continue
elif os.path.isdir(path+"/"+sub) or zipfile.is_zipfile(path+"/"+sub):
subdirs.append(path+"/"+sub)
else:
self.mapfile(path+"/"+sub,in_zip)
for sub in subdirs:
self.index(sub)
def mapfile(self,path,in_zip=False):
file = File(path)
tag = file.pathtag.split(self.root.lower()+"/",1)[1]
if in_zip:
spl = tag.split("/")
spl[-2] = spl[-2].rsplit(".",1)[0]
tag = "/".join(spl)
if tag in self.map:
if file.priority<=self.map[tag].priority:
self.map[tag] = file
else:
self.map[tag] = file
tagext = file.pathtagext.split(self.root.lower()+"/",1)[1]
if tagext in self.ext_map:
if file.priority<=self.ext_map[tagext].priority:
self.ext_map[tagext] = file
else:
self.ext_map[tagext] = file
def lookup(self,thingie,ext=False):
thingie = os.path.normpath(thingie).replace("\\","/")
f = File(thingie)
map = self.map
tag = f.pathtag
if ext:
map = self.ext_map
tag = f.pathtagext
if tag in map:
path = map[tag].path
if "./" in path:
path = path.split("./",1)[1]
return path
def override(self,other_reg):
self.map.update(other_reg.map)
self.ext_map.update(other_reg.ext_map)
def combine_registries(root):
spl = root.split("/")
last = ""
order = []
for x in spl:
if last:
last+="/"+x
else:
last=x
order.append(last)
cur_reg = Registry()
for root in order:
print "building registry for",root
reg = Registry()
reg.build(root)
cur_reg.override(reg)
return cur_reg
def test():
os.chdir("..")
rec = Registry("./games/PW - The Contempt of Court/The Haunted Turnabout")
assert rec.lookup("data/art/port/Maplethorpe/angry(blink).png")=="games/PW - The Contempt of Court/The Haunted Turnabout/data/art/port/Maplethorpe/angry(blink).png"
assert rec.lookup("data/art/port/Maris/angry(blink).png")=="games/PW - The Contempt of Court/The Haunted Turnabout/data/art/port/Maris.zip/angry(blink).png"
assert rec.lookup("data/art/port/Maris/hand1(blink).png")=="games/PW - The Contempt of Court/The Haunted Turnabout/data/art/port/Maris.zip/hand1(blink).png"
reg = Registry("./games/Turnabout Substitution")
assert not reg.lookup("data/art/port/kristoph2/normal(talk)")
assert reg.lookup("data/art/port/apollo/normal(talk)")=="games/Turnabout Substitution/data/art/port/Apollo/normal(talk).png",reg.lookup("data/art/port/apollo/normal(talk)")
base = Registry(".")
assert base.lookup("data/art/port/kristoph2/normal(talk).txt",True)=="data/art/port/kristoph2/normal(talk).txt",base.lookup("data/art/port/kristoph2/normal(talk).txt",True)
assert base.lookup("data/art/port/kristoph2/normal(talk)")=="data/art/port/kristoph2/normal(talk).png",base.lookup("data/art/port/kristoph2/normal(talk)")
assert base.lookup("data/art/port/apollo/normal(talk)")=="data/art/port/apollo/normal(talk).png",base.lookup("data/art/port/apollo/normal(talk)")
assert base.lookup("data/art/fg/../general/logosmall")=="data/art/general/logosmall.png"
base.override(reg)
assert base.lookup("data/art/port/kristoph2/normal(talk)")=="data/art/port/kristoph2/normal(talk).png",base.lookup("data/art/port/kristoph2/normal(talk)")
assert reg.lookup("data/art/port/apollo/normal(talk)")=="games/Turnabout Substitution/data/art/port/Apollo/normal(talk).png",reg.lookup("data/art/port/apollo/normal(talk)")
rec = Registry("examples/rectangles")
assert rec.lookup("data/art/fg/lilmiles-walkeast.txt")=="examples/rectangles/data/art/fg/lilmiles-walkeast.png"
assert rec.lookup("data/art/fg/lilmiles-walkeast.txt",True)=="examples/rectangles/data/art/fg/lilmiles-walkeast.txt"
base.override(rec)
assert rec.lookup("data/art/fg/lilmiles-walkeast.txt")=="examples/rectangles/data/art/fg/lilmiles-walkeast.png"
assert rec.lookup("data/art/fg/lilmiles-walkeast.txt",True)=="examples/rectangles/data/art/fg/lilmiles-walkeast.txt"
if __name__=="__main__":
testfile()
test()