Package intermine :: Module pathfeatures
[hide private]
[frames] | no frames]

Source Code for Module intermine.pathfeatures

  1  import re 
  2   
  3  PATTERN_STR = "^(?:\w+\.)*\w+$" 
  4  PATH_PATTERN = re.compile(PATTERN_STR) 
5 6 -class PathFeature(object):
7 - def __init__(self, path):
8 if not PATH_PATTERN.match(path): 9 raise TypeError( 10 "Path '" + path + "' does not match expected pattern" + PATTERN_STR) 11 self.path = path
12 - def __repr__(self):
13 return "<" + self.__class__.__name__ + ": " + self.to_string() + ">"
14 - def to_string(self):
15 return str(self.path)
16 - def to_dict(self):
17 return { 'path' : self.path }
18 @property
19 - def child_type(self):
20 raise AttributeError()
21
22 -class Join(PathFeature):
23 valid_join_styles = ['OUTER', 'INNER'] 24 INNER = "INNER" 25 OUTER = "OUTER" 26 child_type = 'join'
27 - def __init__(self, path, style='OUTER'):
28 if style.upper() not in Join.valid_join_styles: 29 raise TypeError("Unknown join style: " + style) 30 self.style = style.upper() 31 super(Join, self).__init__(path)
32 - def to_dict(self):
33 d = super(Join, self).to_dict() 34 d.update(style=self.style) 35 return d
36 - def __repr__(self):
37 return('<' + self.__class__.__name__ 38 + ' '.join([':', self.path, self.style]) + '>')
39
40 -class PathDescription(PathFeature):
41 child_type = 'pathDescription'
42 - def __init__(self, path, description):
43 self.description = description 44 super(PathDescription, self).__init__(path)
45 - def to_dict(self):
46 d = super(PathDescription, self).to_dict() 47 d.update(description=self.description) 48 return d
49
50 -class SortOrder(PathFeature):
51 ASC = "asc" 52 DESC = "desc" 53 DIRECTIONS = frozenset(["asc", "desc"])
54 - def __init__(self, path, order):
55 if not order in self.DIRECTIONS: 56 raise TypeError("Order must be one of " + str(self.DIRECTIONS) 57 + " - not " + order) 58 self.order = order 59 super(SortOrder, self).__init__(path)
60 - def __str__(self):
61 return self.path + " " + self.order
62 - def to_string(self):
63 return str(self)
64
65 -class SortOrderList(object):
66 """ 67 A container implementation for holding sort orders 68 ================================================== 69 70 This class exists to hold the sort order information for a 71 query. It handles appending elements, and the stringification 72 of the sort order. 73 """
74 - def __init__(self, *sos):
75 self.sort_orders = [] 76 self.append(*sos)
77 - def append(self, *sos):
78 """ 79 Add sort order elements to the sort order list. 80 =============================================== 81 82 Elements can be provided as a SortOrder object or 83 as a tuple of arguments (path, direction). 84 """ 85 for so in sos: 86 if isinstance(so, SortOrder): 87 self.sort_orders.append(so) 88 elif isinstance(so, tuple): 89 self.sort_orders.append(SortOrder(*so)) 90 else: 91 raise TypeError( 92 "Sort orders must be either SortOrder instances," 93 + " or tuples of arguments: I got:" + so + sos)
94 - def __repr__(self):
95 return '<' + self.class__.__name__ + ': [' + str(self) + ']>'
96 - def __str__(self):
97 return ",".join(map(str, self.sort_orders))
98 - def clear(self):
99 self.sort_orders = []
100 - def is_empty(self):
101 return len(self.sort_orders) == 0
102 - def __len__(self):
103 return len(self.sort_orders)
104 - def next(self):
105 return self.sort_orders.next()
106 - def __iter__(self):
107 return iter(self.sort_orders)
108