-
Notifications
You must be signed in to change notification settings - Fork 11
/
queryableobj.py
123 lines (104 loc) · 4.62 KB
/
queryableobj.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
#!/usr/bin/env python
# coding: utf-8
import queryable
class queryableobj(queryable.queryable):
# Inheriting classes must implement a getobjheaders method
def getobjheadername(self, type):
version = self if hasattr(self, 'config') or hasattr(self, 'version') else self.dir
if type is None:
return objects.headers(version)
else:
return (objects.headerforobject(type, version),)
def headersfortype(self, type=None, type_in=None):
if type or (type_in is None):
headers = self.getobjheaders(type)
else:
headers = []
if type_in:
for itertype in type_in:
for header in self.getobjheaders(itertype):
if not any(header is h for h in headers): headers.append(header)
return headers
def removeobj(self, *args, **kwargs):
obj = self.getobj(*args, **kwargs)
if obj: obj.removeselfandprops()
return obj
def removeallobj(self, *args, **kwargs):
objects = self.allobj(*args, **kwargs)
for obj in objects: obj.removeselfandprops()
return objects
def getobj(self, pretty=None, type=None, exact_id=None, type_in=None, re_id=None, id_in=None, **kwargs):
'''Get the first object token matching a given type and id.'''
type, exact_id = queryableobj.objpretty(pretty, type, exact_id)
headers = self.headersfortype(type, type_in)
if type is None and type_in is None: type_in = objects.objects()
for objecttoken in headers:
obj = objecttoken.get(
exact_value = type,
value_in = type_in,
exact_args = (exact_id,) if exact_id else None,
re_args = (re_id,) if re_id else None,
arg_in = ((0, id_in),) if id_in else None,
args_count = 1,
**kwargs
)
if obj: return obj
return None
def lastobj(self, pretty=None, type=None, exact_id=None, type_in=None, re_id=None, id_in=None, **kwargs):
'''Get the last object token matching a given type and id.'''
type, exact_id = queryableobj.objpretty(pretty, type, exact_id)
headers = self.headersfortype(type, type_in)
if type is None and type_in is None: type_in = objects.objects()
for objecttoken in headers:
obj = objecttoken.last(
exact_value = type,
value_in = type_in,
exact_args = (exact_id,) if exact_id else None,
re_args = (re_id,) if re_id else None,
arg_in = ((0, id_in),) if id_in else None,
args_count = 1,
**kwargs
)
if obj: return obj
return None
def allobj(self, pretty=None, type=None, exact_id=None, type_in=None, re_id=None, id_in=None, **kwargs):
'''Get all object tokens matching a given type and id.'''
type, exact_id = queryableobj.objpretty(pretty, type, exact_id)
results = tokenlist.tokenlist()
headers = self.headersfortype(type, type_in)
if type is None and type_in is None: type_in = objects.objects()
for objecttoken in headers:
for result in objecttoken.all(
exact_value = type,
value_in = type_in,
exact_args = (exact_id,) if exact_id else None,
re_args = (re_id,) if re_id else None,
arg_in = ((0, id_in),) if id_in else None,
args_count = 1,
**kwargs
):
results.append(result)
return results
def objdict(self, *args, **kwargs):
'''
Calls allobj with the same arguments then adds each result to a
dictionary associating object IDs with the tokens where they're
declared.
'''
return {token.args[0]: token for token in self.allobj(*args, **kwargs)}
@staticmethod
def objpretty(pretty, type, id):
'''Internal: Used for handling getobj/allobj arguments.'''
if pretty is not None:
if ':' in pretty:
parts = pretty.split(':')
if len(parts) != 2: raise ValueError('Failed to parse argument because there were too many or too few colons, there ought to be be just one.')
return parts[0], parts[1]
elif type is None:
return pretty, id
elif id is None:
return pretty, type
else:
return type, id
import objects
import tokenlist