-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathqueryableadd.py
66 lines (52 loc) · 2.38 KB
/
queryableadd.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
#!/usr/bin/env python
# coding: utf-8
import queryable
class queryableadd(queryable.queryable):
# Inheriting classes must implement an add method
def setsingular(self, getmethod, addmethod, *args, **kwargs):
'''Internal: Generalized method for setting one thing at a time.'''
setvalue, setargs = queryableadd.argsset(*args, **kwargs)
applytoken = getmethod(exact_value=setvalue)
if applytoken is None:
return addmethod(value=setvalue, args=setargs)
else:
applytoken.args.reset(setargs)
return applytoken
def setplural(self, getmethod, *args, **kwargs):
'''Internal: Generalized method for setting several things at a time.'''
setvalue, setargs = queryableadd.argsset(*args, **kwargs)
settoken = token.token.autosingular(*args, **kwargs)
applytokens = getmethod(exact_value=setvalue)
for applytoken in applytokens: applytoken.args.reset(setargs)
return applytokens
@staticmethod
def argsset(*args, **kwargs):
'''Internal: Utility method for handling arguments to set methods.'''
if len(args) == 2:
setvalue = args[0]
setargs = args[1]
if isinstance(setargs, basestring) or not (hasattr(setargs, '__iter__') or hasattr(setargs, '__getitem__')):
setargs = (setargs,)
else:
settoken = token.token.autosingular(*args, **kwargs)
setvalue = settoken.value
setargs = settoken.args
return setvalue, setargs
def set(self, *args, **kwargs):
'''
If a token with a matching value exists then set the first match's
arguments. Otherwise add a new token with the desired value and
arguments.
'''
return self.setsingular(self.get, self.add, *args, **kwargs)
def setlast(self, *args, **kwargs):
'''
If a token with a matching value exists then set the last match's
arguments. Otherwise add a new token with the desired value and
arguments.
'''
return self.setsingular(self.last, self.add, *args, **kwargs)
def setall(self, *args, **kwargs):
'''Set the arguments of all tokens with a matching value.'''
return self.setplural(self.all, *args, **kwargs)
import token