Ignore /build.
[coe.git] / coe / data.py
CommitLineData
e8a122ff
FT
1class symbol(object):
2 isafe = set("abcdefghijklmnopqrstuvwxyz" +
3 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
4 "$%^&*_=/.")
5 qsafe = isafe | set("0123456789-+")
6
7 def __eq__(s, o):
8 if not isinstance(o, symbol):
9 return False
10 return (s.name == o.name) and (s.ns == o.ns)
11
12 def __hash__(self):
13 return (hash(self.ns) * 31) + hash(self.name)
14
15 def __lt__(s, o):
16 ns, nm = ("", o) if isinstance(o, str) else (s.ns, s.name)
17 return s.ns < ns or s.name < nm
18 def __gt__(s, o):
19 ns, nm = ("", o) if isinstance(o, str) else (s.ns, s.name)
20 return s.ns > ns or s.name > nm
21
22 @staticmethod
23 def quote(st):
24 ret = ""
25 q = False
26 cset = symbol.isafe
27 for c in st:
28 if c not in cset:
29 q = True
30 ret += "\\" + c
31 else:
32 ret += c
33 cset = symbol.qsafe
34 if q:
35 ret = "|" + ret + "|"
36 return ret
37
38 @staticmethod
39 def get(ns, name):
40 if ns == "":
41 return usymbol(name)
42 return nssymbol(ns, name)
43
44 def __repr__(self):
45 if self.ns == "":
46 return self.quote(self.name)
47 return "%s:%s" % (self.quote(self.ns), self.quote(self.name))
48
49class usymbol(symbol):
50 ns = ""
51 def __init__(self, name):
52 self.name = name
53
54 def __hash__(self):
55 return hash(self.name)
56 def __eq__(s, o):
57 if isinstance(o, str):
58 return s.name == o
59 return super().__eq__(o)
60
61class nssymbol(symbol):
62 def __init__(self, ns, name, *args, **kw):
63 super().__init__(*args, **kw)
64 self.name = name
65 self.ns = ns
66
67class obj(object):
68 def __getitem__(self, key):
69 return self.__dict__[key]
70 def __setitem__(self, key, val):
71 self.__dict__[key] = val
72 def __delitem__(self, key):
73 del self.__dict__[key]