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