Exemple #1
0
    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'])
Exemple #2
0
    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
Exemple #3
0
    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
Exemple #4
0
    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
Exemple #5
0
    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
Exemple #6
0
    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
Exemple #7
0
 def test_auto_type_str(self):
     self.assertIsInstance(utils.auto_type('forty-two'), str)
Exemple #8
0
 def test_auto_type_float(self):
     self.assertIsInstance(utils.auto_type('4.2'), float)
Exemple #9
0
 def test_auto_type_int(self):
     self.assertIsInstance(utils.auto_type('42'), int)
Exemple #10
0
 def __setitem__(self, k, v):
     self._a_tags[k] = utils.auto_type(v)
Exemple #11
0
 def test_auto_type_str(self):
     self.assertIsInstance(utils.auto_type('forty-two'), str)
Exemple #12
0
 def test_auto_type_float(self):
     self.assertIsInstance(utils.auto_type('4.2'), float)
Exemple #13
0
 def test_auto_type_int(self):
     self.assertIsInstance(utils.auto_type('42'), int)
Exemple #14
0
 def test_auto_type_none(self):
     self.assertEqual(utils.auto_type(None), '')
Exemple #15
0
 def __setitem__(self, k, v):
     self._a_tags[k] = utils.auto_type(v)
Exemple #16
0
 def test_auto_type_bool(self):
     self.assertIsInstance(utils.auto_type('TRUE'), bool)
     self.assertIsInstance(utils.auto_type('FALSE'), bool)
Exemple #17
0
 def test_auto_type_none(self):
     self.assertEqual(utils.auto_type(None), '')
Exemple #18
0
 def test_auto_type_bool(self):
     self.assertIsInstance(utils.auto_type('TRUE'), bool)
     self.assertIsInstance(utils.auto_type('FALSE'), bool)