-
Notifications
You must be signed in to change notification settings - Fork 16
/
dblimits.py
72 lines (55 loc) · 1.82 KB
/
dblimits.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
class DBLimits:
def __init__(self, limits):
self.default = True
self.databases = []
self.tables = []
d = {}
for limit in limits:
if limit[0] == '-':
limit = limit[1:]
sign = False
else:
sign = True
self.default = False
if '/' in limit:
database, table = limit.split('/')
self.tables.append((database, table))
d[(database, table)] = sign
if sign:
self.databases.append(database)
else:
database = limit
d[database] = sign
self.d = d
def __contains__(self, val):
"""Tests if <val> is within the defined Database limits
<val> can be:
1) a (database, table) tuple
2) a database string
3) database/table
"""
if '/' in val:
database, table = val.split('/')
val = (database, table)
if isinstance(val, type(())):
database, table = val
if (database, table) in self.d:
return self.d[(database, table)]
if database in self.d:
return self.d[database]
return self.default
else:
database = val
if database in self.d:
return self.d[database]
if database in self.databases:
return True
return self.default
def __getitem__(self, database_limit):
table_limits = []
for database, table in self.tables:
if database != database_limit:
continue
sign = self.d[(database, table)]
table_limits.append((table, sign))
return table_limits