Beispiel #1
0
def main(args=None, setup_logging=True):
    if setup_logging:
        from guessit import slogging
        slogging.setupLogging()

    if PY2:  # pragma: no cover
        import codecs
        import locale
        import sys

        # see http://bugs.python.org/issue2128
        if os.name == 'nt':
            for i, a in enumerate(sys.argv):
                sys.argv[i] = a.decode(locale.getpreferredencoding())

        # see https://github.com/wackou/guessit/issues/43
        # and http://stackoverflow.com/questions/4545661/unicodedecodeerror-when-redirecting-to-file
        # Wrap sys.stdout into a StreamWriter to allow writing unicode.
        sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)

    if args:
        options, args = option_parser.parse_args(args)
    else:  # pragma: no cover
        options, args = option_parser.parse_args()
    if options.verbose:
        logging.getLogger().setLevel(logging.DEBUG)

    help_required = True
    if options.properties or options.values:
        display_properties(options)
        help_required = False
    elif options.transformers:
        display_transformers()
        help_required = False

    if options.demo:
        run_demo(episodes=True, movies=True, options=vars(options))
        help_required = False
    elif options.submit_bug:
        for filename in args:
            help_required = False
            submit_bug(filename)
    else:
        if args:
            help_required = False
            for filename in args:
                guess_file(filename,
                           info=options.info.split(','),
                           options=vars(options))

    if help_required:  # pragma: no cover
        option_parser.print_help()
Beispiel #2
0
    def checkFields(self,
                    groundTruth,
                    guess_func,
                    remove_type=True,
                    exclude_files=None):
        total = 0
        exclude_files = exclude_files or []

        fails = defaultdict(list)
        additionals = defaultdict(list)

        for filename, required_fields in groundTruth.items():
            filename = u(filename)
            if filename in exclude_files:
                continue

            log.debug('\n' + '-' * 120)
            log.info('Guessing information for file: %s' % filename)

            options = required_fields.pop(
                'options') if 'options' in required_fields else None

            if options:
                args = shlex.split(options)
                options, _ = option_parser.parse_args(args)
                options = vars(options)
            found = guess_func(filename, options)

            total = total + 1

            # no need for these in the unittests
            if remove_type:
                try:
                    del found['type']
                except:
                    pass
            for prop in ('container', 'mimetype'):
                if prop in found:
                    del found[prop]

            # props which are list of just 1 elem should be opened for easier writing of the tests
            for prop in ('language', 'subtitleLanguage', 'other',
                         'episodeDetails'):
                value = found.get(prop, None)
                if isinstance(value, list) and len(value) == 1:
                    found[prop] = value[0]

            # look for missing properties
            for prop, value in required_fields.items():
                if prop not in found:
                    log.debug("Prop '%s' not found in: %s" % (prop, filename))
                    fails[filename].append("'%s' not found in: %s" %
                                           (prop, filename))
                    continue

                # if both properties are strings, do a case-insensitive comparison
                if (isinstance(value, base_text_type)
                        and isinstance(found[prop], base_text_type)):
                    if value.lower() != found[prop].lower():
                        log.debug(
                            "Wrong prop value [str] for '%s': expected = '%s' - received = '%s'"
                            % (prop, u(value), u(found[prop])))
                        fails[filename].append(
                            "'%s': expected = '%s' - received = '%s'" %
                            (prop, u(value), u(found[prop])))

                elif isinstance(value, list) and isinstance(found[prop], list):
                    if found[prop] and isinstance(found[prop][0],
                                                  babelfish.Language):
                        # list of languages
                        s1 = set(Language.fromguessit(s) for s in value)
                        s2 = set(found[prop])
                    else:
                        # by default we assume list of strings and do a case-insensitive
                        # comparison on their elements
                        s1 = set(u(s).lower() for s in value)
                        s2 = set(u(s).lower() for s in found[prop])

                    if s1 != s2:
                        log.debug(
                            "Wrong prop value [list] for '%s': expected = '%s' - received = '%s'"
                            % (prop, u(value), u(found[prop])))
                        fails[filename].append(
                            "'%s': expected = '%s' - received = '%s'" %
                            (prop, u(value), u(found[prop])))

                elif isinstance(found[prop], babelfish.Language):
                    try:
                        if babelfish.Language.fromguessit(
                                value) != found[prop]:
                            raise ValueError
                    except:
                        log.debug(
                            "Wrong prop value [Language] for '%s': expected = '%s' - received = '%s'"
                            % (prop, u(value), u(found[prop])))
                        fails[filename].append(
                            "'%s': expected = '%s' - received = '%s'" %
                            (prop, u(value), u(found[prop])))

                elif isinstance(found[prop], babelfish.Country):
                    try:
                        if babelfish.Country.fromguessit(value) != found[prop]:
                            raise ValueError
                    except:
                        log.debug(
                            "Wrong prop value [Country] for '%s': expected = '%s' - received = '%s'"
                            % (prop, u(value), u(found[prop])))
                        fails[filename].append(
                            "'%s': expected = '%s' - received = '%s'" %
                            (prop, u(value), u(found[prop])))

                # otherwise, just compare their values directly
                else:
                    if found[prop] != value:
                        log.debug(
                            "Wrong prop value for '%s': expected = '%s' [%s] - received = '%s' [%s]"
                            % (prop, u(value), type(value), u(
                                found[prop]), type(found[prop])))
                        fails[filename].append(
                            "'%s': expected = '%s' [%s] - received = '%s' [%s]"
                            % (prop, u(value), type(value), u(
                                found[prop]), type(found[prop])))

            # look for additional properties
            for prop, value in found.items():
                if prop not in required_fields:
                    log.debug("Found additional info for prop = '%s': '%s'" %
                              (prop, u(value)))
                    additionals[filename].append("'%s': '%s'" %
                                                 (prop, u(value)))

        correct = total - len(fails)
        log.info('SUMMARY: Guessed correctly %d out of %d filenames' %
                 (correct, total))

        for failed_entry, failed_properties in fails.items():
            log.error('---- ' + failed_entry + ' ----')
            for failed_property in failed_properties:
                log.error("FAILED: " + failed_property)

        for additional_entry, additional_properties in additionals.items():
            log.warn('---- ' + additional_entry + ' ----')
            for additional_property in additional_properties:
                log.warn("ADDITIONAL: " + additional_property)

        self.assertTrue(correct == total,
                        msg='Correct: %d < Total: %d' % (correct, total))
Beispiel #3
0
    def checkFields(self, groundTruth, guess_func, remove_type=True,
                    exclude_files=None):
        total = 0
        exclude_files = exclude_files or []

        fails = {}
        additionals = {}

        for filename, required_fields in groundTruth.items():
            filename = u(filename)
            if filename in exclude_files:
                continue

            log.debug('\n' + '-' * 120)
            log.info('Guessing information for file: %s' % filename)

            options = required_fields.pop('options') if 'options' in required_fields else None

            if options:
                args = shlex.split(options)
                options, _ = option_parser.parse_args(args)
                options = vars(options)
            found = guess_func(filename, options)

            total = total + 1

            # no need for these in the unittests
            if remove_type:
                try:
                    del found['type']
                except:
                    pass
            for prop in ('container', 'mimetype'):
                if prop in found:
                    del found[prop]

            # props which are list of just 1 elem should be opened for easier writing of the tests
            for prop in ('language', 'subtitleLanguage', 'other', 'special'):
                value = found.get(prop, None)
                if isinstance(value, list) and len(value) == 1:
                    found[prop] = value[0]

            # look for missing properties
            for prop, value in required_fields.items():
                if prop not in found:
                    log.debug("Prop '%s' not found in: %s" % (prop, filename))
                    if not filename in fails:
                        fails[filename] = [] 
                    fails[filename].append("'%s' not found in: %s" % (prop, filename))
                    continue

                # if both properties are strings, do a case-insensitive comparison
                if (isinstance(value, base_text_type) and
                    isinstance(found[prop], base_text_type)):
                    if value.lower() != found[prop].lower():
                        log.debug("Wrong prop value [str] for '%s': expected = '%s' - received = '%s'" % (prop, u(value), u(found[prop])))
                        if not filename in fails:
                            fails[filename] = [] 
                        fails[filename].append("'%s': expected = '%s' - received = '%s'" % (prop, u(value), u(found[prop])))

                # if both are lists, we assume list of strings and do a case-insensitive
                # comparison on their elements
                elif isinstance(value, list) and isinstance(found[prop], list):
                    s1 = set(u(s).lower() for s in value)
                    s2 = set(u(s).lower() for s in found[prop])
                    if s1 != s2:
                        log.debug("Wrong prop value [list] for '%s': expected = '%s' - received = '%s'" % (prop, u(value), u(found[prop])))
                        if not filename in fails:
                            fails[filename] = [] 
                        fails[filename].append("'%s': expected = '%s' - received = '%s'" % (prop, u(value), u(found[prop])))
                # otherwise, just compare their values directly
                else:
                    if found[prop] != value:
                        log.debug("Wrong prop value for '%s': expected = '%s' [%s] - received = '%s' [%s]" % (prop, u(value), type(value), u(found[prop]), type(found[prop])))
                        if not filename in fails:
                            fails[filename] = [] 
                        fails[filename].append("'%s': expected = '%s' [%s] - received = '%s' [%s]" % (prop, u(value), type(value), u(found[prop]), type(found[prop])))

            # look for additional properties
            for prop, value in found.items():
                if prop not in required_fields:
                    log.debug("Found additional info for prop = '%s': '%s'" % (prop, u(value)))
                    if not filename in additionals:
                        additionals[filename] = [] 
                    additionals[filename].append("'%s': '%s'" % (prop, u(value)))

        correct = total - len(fails)
        log.info('SUMMARY: Guessed correctly %d out of %d filenames' % (correct, total))

        for failed_entry, failed_properties in fails.items():
            log.error('---- ' + failed_entry + ' ----')
            for failed_property in failed_properties:
                log.error("FAILED: " + failed_property)

        for additional_entry, additional_properties in additionals.items():
            log.warn('---- ' + additional_entry + ' ----')
            for additional_property in additional_properties:
                log.warn("ADDITIONAL: " + additional_property)

        self.assertTrue(correct == total,
                        msg='Correct: %d < Total: %d' % (correct, total))