Package mobi :: Package mtld :: Package da :: Package device :: Module post_walk_rules
[hide private]
[frames] | no frames]

Source Code for Module mobi.mtld.da.device.post_walk_rules

  1  #!/usr/bin/env python 
  2   
  3  """ 
  4  @copyright: 
  5   Copyright (c) 2014 by mTLD Top Level Domain Limited. All rights reserved.\n 
  6   Portions copyright (c) 2008 by Argo Interactive Limited.\n 
  7   Portions copyright (c) 2008 by Nokia Inc.\n 
  8   Portions copyright (c) 2008 by Telecom Italia Mobile S.p.A.\n 
  9   Portions copyright (c) 2008 by Volantis Systems Limited.\n 
 10   Portions copyright (c) 2002-2008 by Andreas Staeding.\n 
 11   Portions copyright (c) 2008 by Zandan.\n 
 12  @author: dotMobi 
 13  """ 
 14   
 15  from mobi.mtld.da.exception.device_atlas_exception import DeviceAtlasException 
 16   
17 -class PostWalkRules(object):
18
19 - class PostWalkRulesException(DeviceAtlasException):
20 """ 21 The PostWalkRulesException exception is raised by this class when the 22 init_get_matcher_propery_ids() or init_rule_sets() are called before they 23 have been replaced by any new implementation from a child class. 24 This helps us to simulate abstract methods, which does not exist in Ruby. 25 """ 26 pass
27 28 # Constants to make keys more readable 29 KEY_PROPERTY_MATCHER = 'p' 30 KEY_PROPERTY_VALUE = 'v' 31 KEY_OPERATOR = 'o' 32 KEY_MATCHER_PROP_IDS_IN_USE = 'mpids' 33 KEY_RULE_ARR = 'r' 34 KEY_RULE_PROP_IDS_IN_USE = 'rpids' 35 KEY_RULE_GROUPS = 'rg' 36 KEY_RULE_SET = 't' 37 KEY_RULE_SET_COUNT = 'tc' 38 39 tree_provider = None 40 branch = {} 41 __prop_matcher_ids_in_use = [] 42 __rule_prop_ids_in_use = [] 43 44
45 - def __init__(self, tree_provider, type):
46 self.tree_provider = tree_provider 47 48 self.branch = tree_provider.tree[type] # main branch 49 50 rg = self.branch[self.KEY_RULE_GROUPS] 51 52 for group in rg: 53 # We want to keep a list of all the properties that are used because when 54 # a user calls DeviceApi.properties we need to fetch additional properties 55 # other than the property they want to optimize the User-Agent string 56 # rules. 57 self._init_get_matcher_propery_ids(group, self.__prop_matcher_ids_in_use) 58 59 sets = self._init_rule_sets(group) 60 61 # Also keep a list of all the property IDs that can be output 62 self.__rule_prop_ids_in_use = self.__init_get_rule_property_ids(sets, 63 self.__rule_prop_ids_in_use)
64 65 # Protected 66
67 - def _init_get_matcher_propery_ids(self, group, prop_ids):
68 """ 69 Find all the properties that are used for matching. This is needed in case 70 the DeviceApi.properties function is called as we need these properties 71 for the rules to work correctly. 72 73 @param group: The rule group that can contain a property matcher. 74 @param prop_ids: The list of found property IDs. 75 @return: an updated set of property IDs. 76 """ 77 raise PostWalkRulesException('This is an abstract method that has not been ' + 78 'implemented.')
79
80 - def _init_rule_sets(self, group):
81 """ 82 Prepare the rule set 83 @param group: The current parent group. 84 @return: a list of all rule sets. 85 """ 86 raise PostWalkRulesException('This is an abstract method that has not been ' + 87 'implemented.')
88 89 # Private 90
91 - def __init_get_rule_property_ids(self, sets, rule_prop_ids):
92 """ 93 Find all the properties that are used in the final rules. This is needed to 94 optimize the Api.getProperty() function. 95 96 @param sets: The rule set from the main rule group. 97 @param rule_prop_ids: The list of found property IDs. 98 @return: an updated set of property IDs. 99 """ 100 101 # loop over all items in the rule set and find all the property ids 102 # used in the rules 103 104 for items in sets: 105 rules = items[self.KEY_RULE_ARR] 106 107 if rules is None: 108 return rule_prop_ids 109 110 # now loop over the actual rule array 111 for rule_details in rules: 112 113 prop_id = rule_details[self.KEY_PROPERTY_MATCHER] 114 115 if prop_id not in rule_prop_ids: 116 117 rule_prop_ids.append(prop_id) 118 119 return rule_prop_ids
120