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 -class SortOrderList(object):
65 - def __init__(self, *sos):
66 self.sort_orders = [] 67 self.append(*sos)
68 - def append(self, *sos):
69 for so in sos: 70 if isinstance(so, SortOrder): 71 self.sort_orders.append(so) 72 elif isinstance(so, tuple): 73 self.sort_orders.append(SortOrder(*so)) 74 else: 75 raise TypeError( 76 "Sort orders must be either SortOrder instances," 77 + " or tuples of arguments: I got:" + so + sos)
78 - def __repr__(self):
79 return '<' + self.class__.__name__ + ': [' + str(self) + ']>'
80 - def __str__(self):
81 return ",".join(map(str, self.sort_orders))
82 - def clear(self):
83 self.sort_orders = []
84 - def is_empty(self):
85 return len(self.sort_orders) == 0
86 - def next(self):
87 return self.sort_orders.next()
88 - def __iter__(self):
89 return iter(self.sort_orders)
90