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 try: 56 order = order.lower() 57 except: 58 pass 59 60 if not order in self.DIRECTIONS: 61 raise TypeError("Order must be one of " + str(self.DIRECTIONS) 62 + " - not " + order) 63 self.order = order 64 super(SortOrder, self).__init__(path)
65 - def __str__(self):
66 return self.path + " " + self.order
67 - def to_string(self):
68 return str(self)
69
70 -class SortOrderList(object):
71 """ 72 A container implementation for holding sort orders 73 ================================================== 74 75 This class exists to hold the sort order information for a 76 query. It handles appending elements, and the stringification 77 of the sort order. 78 """
79 - def __init__(self, *sos):
80 self.sort_orders = [] 81 self.append(*sos)
82 - def append(self, *sos):
83 """ 84 Add sort order elements to the sort order list. 85 =============================================== 86 87 Elements can be provided as a SortOrder object or 88 as a tuple of arguments (path, direction). 89 """ 90 for so in sos: 91 if isinstance(so, SortOrder): 92 self.sort_orders.append(so) 93 elif isinstance(so, tuple): 94 self.sort_orders.append(SortOrder(*so)) 95 else: 96 raise TypeError( 97 "Sort orders must be either SortOrder instances," 98 + " or tuples of arguments: I got:" + so + sos)
99 - def __repr__(self):
100 return '<' + self.class__.__name__ + ': [' + str(self) + ']>'
101 - def __str__(self):
102 return " ".join(map(str, self.sort_orders))
103 - def clear(self):
104 self.sort_orders = []
105 - def is_empty(self):
106 return len(self.sort_orders) == 0
107 - def __len__(self):
108 return len(self.sort_orders)
109 - def next(self):
110 return self.sort_orders.next()
111 - def __iter__(self):
112 return iter(self.sort_orders)
113