Пример #1
0
    def test_byte_arguments(self):
        """byte arguments should be encoded correctly"""

        if PY2:
            self.assertEqual(type(make_unicode("byte string")), unicode)
        else:
            self.assertEqual(type(make_unicode(b"byte string")), str)
Пример #2
0
    def test_byte_arguments(self):
        """byte arguments should be encoded correctly"""

        if PY2:
            self.assertEqual(type(make_unicode("byte string")), unicode)
        else:
            self.assertEqual(type(make_unicode(b"byte string")), str)
Пример #3
0
    def __init__(self, api_key, **kwargs):
        self.config = dict()

        #cache old searches to avoid hitting the server
        self.search_buffer = dict()

        #Store the path to where we are
        self.path = os.path.abspath(os.path.dirname(__file__))

        if 'force_lang' in kwargs:
            logger.warning(u"'force_lang' keyword argument is deprecated as of version 0.4")

        #extract all argument and store for later use
        self.config['api_key'] = api_key
        self.config['cache_dir'] = kwargs.get("cache_dir",
                                              make_unicode(os.path.join(tempfile.gettempdir(),  __NAME__)))

        self.config['actors'] = kwargs.get('actors', False)
        self.config['banners'] = kwargs.get('banners', False)
        self.config['ignore_case'] = kwargs.get('ignore_case', False)

        #Create the loader object to use
        self.loader = Loader(self.config['cache_dir'])

        #Create the list of available mirrors
        tree = generate_tree(self.loader.load(__mirrors__.format(**self.config)))
        self.mirrors = MirrorList(tree)
Пример #4
0
    def __init__(self, api_key, **kwargs):
        self.config = dict()

        #cache old searches to avoid hitting the server
        self.search_buffer = dict()

        #Store the path to where we are
        self.path = os.path.abspath(os.path.dirname(__file__))

        if 'force_lang' in kwargs:
            logger.warning(
                u"'force_lang' keyword argument is deprecated as of version 0.4"
            )

        #extract all argument and store for later use
        self.config['api_key'] = api_key
        self.config['cache_dir'] = kwargs.get(
            "cache_dir",
            make_unicode(os.path.join(tempfile.gettempdir(), __NAME__)))

        self.config['actors'] = kwargs.get('actors', False)
        self.config['banners'] = kwargs.get('banners', False)
        self.config['ignore_case'] = kwargs.get('ignore_case', False)

        #Create the loader object to use
        self.loader = Loader(self.config['cache_dir'])

        #Create the list of available mirrors
        tree = generate_tree(
            self.loader.load(__mirrors__.format(**self.config)))
        self.mirrors = MirrorList(tree)
Пример #5
0
    def load(self, url, cache=True):
        """
        :param url: The URL to be loaded
        :param cache: Optional. Set if the cache should be ignored or not.
        :return: The content of the url as bytes
        :raise: ConnectionError if the url could not be loaded

        """

        logger.debug(u"Loading data from {0}".format(url))

        header = dict()
        if not cache:
            logger.debug(u"Ignoring cached data.")
            header['cache-control'] = u'no-cache'

        try:
            response, content = self.http.request(url, headers=header)
        except (httplib2.RelativeURIError, httplib2.ServerNotFoundError):
            raise error.ConnectionError(
                u"Unable to connect to {0}".format(url))

        if response.status in [404]:
            raise error.TVDBNotFoundError(u"Data not found")
        elif response.status not in [200, 304]:  # pragma: no cover
            raise error.ConnectionError(
                u"Bad status returned from server. {0}".format(
                    response.status))
        else:
            return make_unicode(content)
Пример #6
0
    def load(self, url, cache=True):
        """
        :param url: The URL to be loaded
        :param cache: Optional. Set if the cache should be ignored or not.
        :return: The content of the url as bytes
        :raise: ConnectionError if the url could not be loaded

        """

        logger.debug(u"Loading data from {0}".format(url))

        header = dict()
        if not cache:
            logger.debug(u"Ignoring cached data.")
            header['cache-control'] = u'no-cache'

        try:
            response, content = self.http.request(url, headers=header)
        except (httplib2.RelativeURIError, httplib2.ServerNotFoundError):
            raise error.ConnectionError(u"Unable to connect to {0}".format(url))

        if response.status in [404]:
            raise error.TVDBNotFoundError(u"Data not found")
        elif response.status not in [200, 304]:  # pragma: no cover
            raise error.ConnectionError(u"Bad status returned from server. {0}".format(response.status))
        else:
            return make_unicode(content)
Пример #7
0
    def test_xml_error(self):
        """
        The tree generator should raise BadData error when passed bad xml data
        """

        data = StringIO(make_unicode('<?xml version="1.0" encoding="UTF-8" ?>\n<data>'))

        self.assertRaises(error.BadData, generate_tree, data)
Пример #8
0
def parse_xml(etree, element):
    """
    :param etree:
    :param element:
    :return: A list of dictionaries containing the data of the format tag:value

    Parses the element tree for elements of type *element* and converts the
    data into a dictionary.

    It will attempt to convert the data into native Python
    types. The following conversions will be applied.

      * yyyy-mm-dd will be converted into a datetime.date object.
      * Integers will be converted to int
      * Floats will be converted to float
      * Lists separated by | will be converted into a list. Eg. |foo|bar|
      will be converted into ['foo', 'bar']. Note that even if there is only
      one element it will be converted into a one element list.
    """

    logger.debug(u"Parsing element tree for {0}".format(element))

    _list = list()
    for item in etree.findall(element):

        data = dict()
        for child in list(item):
            tag, value = child.tag, make_unicode(child.text)

            if value:
                value = value.strip()
            else:
                value = u""

            try:  # Try to format as a datetime object
                value = datetime.datetime.strptime(value, "%Y-%m-%d").date()
            except ValueError:
                if '|' in value:  # Split piped values into a list
                    value = value.strip("|").split("|")
                    value = [s.strip() for s in value]
                else:
                    if re.match(r"^\d+\.\d+$", value):  # Convert float
                        value = float(value)
                    elif re.match(r"^\d+$", value):  # Convert integer
                        value = int(value)

            data[tag] = value
        _list.append(data)
    logger.debug(u"Found {0} element(s)".format(len(_list)))
    return _list
Пример #9
0
def parse_xml(etree, element):
    """
    :param etree:
    :param element:
    :return: A list of dictionaries containing the data of the format tag:value

    Parses the element tree for elements of type *element* and converts the
    data into a dictionary.

    It will attempt to convert the data into native Python
    types. The following conversions will be applied.

      * yyyy-mm-dd will be converted into a datetime.date object.
      * Integers will be converted to int
      * Floats will be converted to float
      * Lists separated by | will be converted into a list. Eg. |foo|bar|
      will be converted into ['foo', 'bar']. Note that even if there is only
      one element it will be converted into a one element list.
    """

    logger.debug(u"Parsing element tree for {0}".format(element))

    _list = list()
    for item in etree.findall(element):

        data = dict()
        for child in list(item):
            tag, value = child.tag, make_unicode(child.text)

            if value:
                value = value.strip()
            else:
                value = u""

            try:  # Try to format as a datetime object
                value = datetime.datetime.strptime(value, "%Y-%m-%d").date()
            except ValueError:
                if '|' in value:  # Split piped values into a list
                    value = value.strip("|").split("|")
                    value = [s.strip() for s in value]
                else:
                    if re.match(r"^\d+\.\d+$", value):  # Convert float
                        value = float(value)
                    elif re.match(r"^\d+$", value):  # Convert integer
                        value = int(value)

            data[tag] = value
        _list.append(data)
    logger.debug(u"Found {0} element(s)".format(len(_list)))
    return _list
Пример #10
0
    def __init__(self, api_key, **kwargs):
        self.config = dict()

        # cache old searches to avoid hitting the server
        self.search_buffer = dict()

        # extract all argument and store for later use
        self.config['api_key'] = api_key
        self.config['cache_dir'] = kwargs.get("cache_dir",
                                              make_unicode(os.path.join(tempfile.gettempdir(), __NAME__)))

        self.config['actors'] = kwargs.get('actors', False)
        self.config['banners'] = kwargs.get('banners', False)
        self.config['ignore_case'] = kwargs.get('ignore_case', False)

        # Create the loader object to use
        self.loader = Loader(self.config['cache_dir'], timeout=kwargs.get('timeout', None))

        # Create the list of available mirrors
        tree = generate_tree(self.loader.load(mirrors.format(**self.config)))
        self.mirrors = MirrorList(tree)
Пример #11
0
    def __init__(self, api_key, **kwargs):
        self.config = dict()

        # cache old searches to avoid hitting the server
        self.search_buffer = dict()

        # extract all argument and store for later use
        self.config['api_key'] = api_key
        self.config['cache_dir'] = kwargs.get(
            "cache_dir",
            make_unicode(os.path.join(tempfile.gettempdir(), __NAME__)))

        self.config['actors'] = kwargs.get('actors', False)
        self.config['banners'] = kwargs.get('banners', False)
        self.config['ignore_case'] = kwargs.get('ignore_case', False)

        # Create the loader object to use
        self.loader = Loader(self.config['cache_dir'],
                             timeout=kwargs.get('timeout', None))

        # Create the list of available mirrors
        tree = generate_tree(self.loader.load(mirrors.format(**self.config)))
        self.mirrors = MirrorList(tree)
Пример #12
0
    def load(self, url, cache=True):
        """
        :param url: The URL to be loaded
        :param cache: Optional. Set if the cache should be ignored or not.
        :return: The content of the url as bytes
        :raise: ConnectionError if the url could not be loaded

        """

        logger.debug(u"Loading data from {0}".format(url))

        header = dict()
        if not cache:
            logger.debug(u"Ignoring cached data.")
            header['cache-control'] = u'no-cache'

        try:
            response, content = self.http.request(url, headers=header)
        except (httplib2.RelativeURIError, httplib2.ServerNotFoundError):
            raise error.ConnectionError(u"Unable to connect to {0}".format(url))

        if response.status in [404]:
            raise error.TVDBNotFoundError(u"Data not found")
        elif response.status not in [200, 304]:  # pragma: no cover
            raise error.ConnectionError(u"Bad status returned from server. {0}".format(response.status))

        data = make_unicode(content)

        if response['content-type'] == "application/zip":
            zd = StringIO()
            zd.write(data)
            zf = zipfile.ZipFile(zd)
            data = zf.open(
                '{0}.xml'.format(os.path.basename(url)[:-4])
            ).read()

        return data
Пример #13
0
    def test_non_byte_arguments(self):
        """Passing non byte arguments should return them unchanged"""

        self.assertEqual(type(make_unicode(2)), int)
        self.assertEqual(type(make_unicode(2.2)), float)
Пример #14
0
    def test_unicode(self):
        """Already encoded strings should be returned unchanged"""
        _input = u"Python 2 String" if PY2 else "Python 3 String"

        self.assertEqual(type(make_unicode(_input)), type(_input))
        self.assertEqual(make_unicode(_input), _input)
Пример #15
0
 def __wrapper__(*args, **kwargs):
     args = [make_unicode(arg) for arg in args]
     kwargs = dict((k, make_unicode(v)) for (k, v) in kwargs.items())
     return func(*args, **kwargs)
Пример #16
0
 def test_none(self):
     """Passing None should return None"""
     self.assertEqual(make_unicode(None), None)
Пример #17
0
 def test_none(self):
     """Passing None should return None"""
     self.assertEqual(make_unicode(None), None)
Пример #18
0
    def test_non_byte_arguments(self):
        """Passing non byte arguments should return them unchanged"""

        self.assertEqual(type(make_unicode(2)), int)
        self.assertEqual(type(make_unicode(2.2)), float)
Пример #19
0
    def test_unicode(self):
        """Already encoded strings should be returned unchanged"""
        _input = u"Python 2 String" if PY2 else "Python 3 String"

        self.assertEqual(type(make_unicode(_input)), type(_input))
        self.assertEqual(make_unicode(_input), _input)