Exemplo n.º 1
0
    def makePGTSComparisonYAML(self):
        import codecs
        outfile = codecs.open('outfile.yaml', 'w', 'utf-8')
        print >> outfile, "test_cases:"

        yamlFile = open(
            os.path.join(TEST_RESOURCES_DIR, 'pgts_browser_list.yaml'))
        yamlContents = yaml.load(yamlFile)
        yamlFile.close()

        for test_case in yamlContents['test_cases']:
            user_agent_string = test_case['user_agent_string']
            kwds = {}
            if 'js_ua' in test_case:
                kwds = eval(test_case['js_ua'])

            (family, major, minor,
             patch) = user_agent_parser.ParseUserAgent(user_agent_string,
                                                       **kwds)

            # Escape any double-quotes in the UA string
            user_agent_string = re.sub(r'"', '\\"', user_agent_string)
            print >> outfile, '    - user_agent_string: "' + unicode(user_agent_string) + '"' + "\n" +\
                              '      family: "' + family + "\"\n" +\
                              "      major: " + ('' if (major is None) else "'" + major + "'") + "\n" +\
                              "      minor: " + ('' if (minor is None) else "'" + minor + "'") + "\n" +\
                              "      patch: " + ('' if (patch is None) else "'" + patch + "'")
        outfile.close()
Exemplo n.º 2
0
    def runOSTestsFromYAML(self, file_name):
        yamlFile = open(os.path.join(TEST_RESOURCES_DIR, file_name))
        yamlContents = yaml.load(yamlFile)
        yamlFile.close()

        for test_case in yamlContents['test_cases']:
            # Inputs to Parse()
            user_agent_string = test_case['user_agent_string']
            kwds = {}
            if 'js_ua' in test_case:
                kwds = eval(test_case['js_ua'])

            # The expected results
            expected = {
                'family': test_case['family'],
                'major': test_case['major'],
                'minor': test_case['minor'],
                'patch': test_case['patch'],
                'patch_minor': test_case['patch_minor']
            }

            result = user_agent_parser.ParseOS(user_agent_string, **kwds)
            self.assertEqual(result, expected,
                    u"UA: {0}\n expected<{1} {2} {3} {4} {5}> != actual<{6} {7} {8} {9} {10}>".format(\
                            user_agent_string,
                            expected['family'],
                            expected['major'],
                            expected['minor'],
                            expected['patch'],
                            expected['patch_minor'],
                            result['family'],
                            result['major'],
                            result['minor'],
                            result['patch'],
                            result['patch_minor']))
Exemplo n.º 3
0
    def makePGTSComparisonYAML(self):
        import codecs
        outfile = codecs.open('outfile.yaml', 'w', 'utf-8')
        print >> outfile, "test_cases:"

        yamlFile = open(os.path.join(TEST_RESOURCES_DIR,
                                     'pgts_browser_list.yaml'))
        yamlContents = yaml.load(yamlFile)
        yamlFile.close()

        for test_case in yamlContents['test_cases']:
            user_agent_string = test_case['user_agent_string']
            kwds = {}
            if 'js_ua' in test_case:
                kwds = eval(test_case['js_ua'])

            (family, major, minor, patch) = user_agent_parser.ParseUserAgent(user_agent_string, **kwds)

            # Escape any double-quotes in the UA string
            user_agent_string = re.sub(r'"', '\\"', user_agent_string)
            print >> outfile, '    - user_agent_string: "' + unicode(user_agent_string) + '"' + "\n" +\
                              '      family: "' + family + "\"\n" +\
                              "      major: " + ('' if (major is None) else "'" + major + "'") + "\n" +\
                              "      minor: " + ('' if (minor is None) else "'" + minor + "'") + "\n" +\
                              "      patch: " + ('' if (patch is None) else "'" + patch + "'")
        outfile.close()
Exemplo n.º 4
0
    def runOSTestsFromYAML(self, file_name):
        yamlFile = open(os.path.join(TEST_RESOURCES_DIR, file_name))
        yamlContents = yaml.load(yamlFile)
        yamlFile.close()

        for test_case in yamlContents['test_cases']:
            # Inputs to Parse()
            user_agent_string = test_case['user_agent_string']
            kwds = {}
            if 'js_ua' in test_case:
                kwds = eval(test_case['js_ua'])

            # The expected results
            expected = {
              'family': test_case['family'],
              'major': test_case['major'],
              'minor': test_case['minor'],
              'patch': test_case['patch'],
              'patch_minor': test_case['patch_minor']
            }

            result = user_agent_parser.ParseOS(user_agent_string, **kwds)
            self.assertEqual(result, expected,
                    u"UA: {0}\n expected<{1} {2} {3} {4} {5}> != actual<{6} {7} {8} {9} {10}>".format(\
                            user_agent_string,
                            expected['family'],
                            expected['major'],
                            expected['minor'],
                            expected['patch'],
                            expected['patch_minor'],
                            result['family'],
                            result['major'],
                            result['minor'],
                            result['patch'],
                            result['patch_minor']))
Exemplo n.º 5
0
def load_config(tricks_file_pathname):
    """
    Loads the YAML configuration from the specified file.

    :param tricks_file_path:
        The path to the tricks configuration file.
    :returns:
        A dictionary of configuration information.
    """
    f = open(tricks_file_pathname, 'rb')
    content = f.read()
    f.close()
    config = yaml.load(content)
    return config
Exemplo n.º 6
0
    def runDeviceTestsFromYAML(self, file_name):
        yamlFile = open(os.path.join(TEST_RESOURCES_DIR, file_name))
        yamlContents = yaml.load(yamlFile)
        yamlFile.close()

        for test_case in yamlContents['test_cases']:
            # Inputs to Parse()
            user_agent_string = test_case['user_agent_string']
            kwds = {}
            if 'js_ua' in test_case:
                kwds = eval(test_case['js_ua'])

            # The expected results
            expected = {'family': test_case['family']}

            result = user_agent_parser.ParseDevice(user_agent_string, **kwds)
            self.assertEqual(
                result, expected,
                u"UA: {0}\n expected<{1}> != actual<{2}>".format(
                    user_agent_string, expected['family'], result['family']))
Exemplo n.º 7
0
    def runDeviceTestsFromYAML(self, file_name):
        yamlFile = open(os.path.join(TEST_RESOURCES_DIR, file_name))
        yamlContents = yaml.load(yamlFile)
        yamlFile.close()

        for test_case in yamlContents['test_cases']:
            # Inputs to Parse()
            user_agent_string = test_case['user_agent_string']
            kwds = {}
            if 'js_ua' in test_case:
                kwds = eval(test_case['js_ua'])

            # The expected results
            expected = {
              'family': test_case['family']
            }

            result = user_agent_parser.ParseDevice(user_agent_string, **kwds)
            self.assertEqual(result, expected,
                u"UA: {0}\n expected<{1}> != actual<{2}>".format(
                    user_agent_string,
                    expected['family'],
                    result['family']))
Exemplo n.º 8
0
UA_PARSER_YAML = os.getenv("UA_PARSER_YAML")
regexes = None

if not UA_PARSER_YAML:
    try:
        from pkg_resources import resource_filename
        yamlPath = resource_filename(__name__, 'regexes.yaml')
        json_path = resource_filename(__name__, 'regexes.json')
    except ImportError:
        yamlPath = os.path.join(ROOT_DIR, 'regexes.yaml')
        json_path = os.path.join(ROOT_DIR, 'regexes.json')
else:
    import mitmflib.yaml

    yamlFile = open(UA_PARSER_YAML)
    regexes = yaml.load(yamlFile)
    yamlFile.close()

# If UA_PARSER_YAML is not specified, load regexes from regexes.json before
# falling back to yaml format
if regexes is None:
    try:
        json_file = open(json_path)
        regexes = json.loads(json_file.read())
        json_file.close()
    except IOError:
        import mitmflib.yaml

        yamlFile = open(yamlPath)
        regexes = yaml.load(yamlFile)
        yamlFile.close()