def get_preference(self, pref_name): """ Gets a single named preference :returns: the value, typed to str/bool/int/float regarding its content. """ resp = self.request_single('GetPrefs', {'pref': {'name': pref_name}}) return utils.auto_type(resp['_content'])
def _parse_a_tags(cls, dic): """ Iterates over all <a> tags and builds a dict with those. If a tag with same "n" attributes appears several times, the dict value is a list with the tags values, else it's a string. :param: dic the dict describing the tag :returns: a dict """ props = {} if "a" in dic: childs = dic["a"] # If there is only one "a", it's not presentend as # a list, that's why we put it in a list if not isinstance(childs, (list, tuple)): childs = [childs] else: childs = [] for child in childs: k = child[cls.ATTRNAME_PROPERTY] try: v = child["_content"] except KeyError: v = None try: v = utils.auto_type(str(v)) except UnicodeEncodeError: # Some times, str() fails because of accents... v = utils.auto_type(v) if k in props: prev_v = props[k] if type(prev_v) != list: props[k] = [prev_v] props[k].append(v) else: props[k] = v return props
def _parse_a_tags(cls, dic): """ Iterates over all <a> tags and builds a dict with those. If a tag with same "n" attributes appears several times, the dict value is a list with the tags values, else it's a string. :param: dic the dict describing the tag :returns: a dict """ props = {} if 'a' in dic: childs = dic['a'] # If there is only one "a", it's not presentend as # a list, that's why we put it in a list if not isinstance(childs, (list, tuple)): childs = [childs] else: childs = [] for child in childs: k = child[cls.ATTRNAME_PROPERTY] try: v = child['_content'] except KeyError: v = None try: v = utils.auto_type(str(v)) except UnicodeEncodeError: # Some times, str() fails because of accents... v = utils.auto_type(v) if k in props: prev_v = props[k] if type(prev_v) != list: props[k] = [prev_v] props[k].append(v) else: props[k] = v return props
def _unparse_a_tags(cls, attrs_dict): """ Iterates over the dictionary :param: attrs_dict a dict of attributes :returns: a SimpleXMLElement list containing <a> tags """ prop_tags = [] for k, v in attrs_dict.items(): node = {cls.ATTRNAME_PROPERTY: k, "_content": utils.auto_type(v)} prop_tags.append(node) return prop_tags
def get_preferences(self): """ Gets all the preferences of the current user :returns: a dict presenting the preferences by name, values are typed to str/bool/int/float regarding their content. """ pref_list = self.request('GetPrefs')['pref'] out = {} for pref in pref_list: out[pref['name']] = utils.auto_type(pref['_content']) return out
def _unparse_a_tags(cls, attrs_dict): """ Iterates over the dictionary :param: attrs_dict a dict of attributes :returns: a SimpleXMLElement list containing <a> tags """ prop_tags = [] for k, v in attrs_dict.items(): node = {cls.ATTRNAME_PROPERTY: k, '_content': utils.auto_type(v)} prop_tags.append(node) return prop_tags
def test_auto_type_str(self): self.assertIsInstance(utils.auto_type('forty-two'), str)
def test_auto_type_float(self): self.assertIsInstance(utils.auto_type('4.2'), float)
def test_auto_type_int(self): self.assertIsInstance(utils.auto_type('42'), int)
def __setitem__(self, k, v): self._a_tags[k] = utils.auto_type(v)
def test_auto_type_none(self): self.assertEqual(utils.auto_type(None), '')
def test_auto_type_bool(self): self.assertIsInstance(utils.auto_type('TRUE'), bool) self.assertIsInstance(utils.auto_type('FALSE'), bool)