示例#1
0
def get_value(key, default_value=None, constructor=None):
    """Get the value for a key."""
    value_path = get_value_file_path(key)

    if not os.path.exists(value_path):
        return default_value

    try:
        with open(value_path, 'rb') as f:
            value_str = f.read()
    except IOError:
        logs.log_error('Failed to read %s from persistent cache.' % key)
        value_str = None

    if value_str is None:
        return None

    try:
        value = json_utils.loads(value_str)
    except Exception:
        logs.log_error('Non-serializable value read from cache key %s: "%s"' %
                       (key, value_str))
        value = default_value

    if constructor:
        try:
            value = constructor(value)
        except Exception:
            logs.log_warn(
                'Failed to construct value "%s" using %s '
                'and key "%s" in persistent cache. Using default value %s.' %
                (value, constructor, key, default_value))
            value = default_value

    return value
示例#2
0
    def get_metadata(self, key=None, default=None):
        """Emulate Testcase's get_metadata function."""
        metadata = json_utils.loads(self.additional_metadata)
        if not key:
            return metadata

        try:
            return self.metadata[key]
        except KeyError:
            return default
示例#3
0
def _get_testcase(testcase_id):
    """Retrieve the json representation of the test case with the given id."""
    response, content = _http_request(TESTCASE_INFO_URL,
                                      body={'testcaseId': testcase_id})

    if response.status != 200:
        raise ReproduceToolException('Unable to fetch test case information.')

    testcase_map = json_utils.loads(content)
    return SerializedTestcase(testcase_map)
示例#4
0
    def __init__(self, testcase_url):
        testcase_url_parts = parse.urlparse(testcase_url)
        config_url = testcase_url_parts._replace(
            path=REPRODUCE_TOOL_CONFIG_HANDLER).geturl()
        response, content = http_utils.request(config_url, body={})
        if response.status != 200:
            raise errors.ReproduceToolUnrecoverableError(
                'Failed to access server.')

        self._config = json_utils.loads(content)
示例#5
0
    def _ensure_metadata_is_cached(self):
        """Ensure that the metadata for this has been cached."""
        if hasattr(self, 'metadata_cache'):
            return

        try:
            cache = json_utils.loads(self.additional_metadata)
        except (TypeError, ValueError):
            cache = {}

        setattr(self, 'metadata_cache', cache)
示例#6
0
def _get_testcase(testcase_id, configuration):
    """Retrieve the json representation of the test case with the given id."""
    response, content = http_utils.request(
        configuration.get('testcase_info_url'),
        body={'testcaseId': testcase_id},
        configuration=configuration)

    if response.status != 200:
        raise errors.ReproduceToolUnrecoverableError(
            'Unable to fetch test case information.')

    testcase_map = json_utils.loads(content)
    return SerializedTestcase(testcase_map)
示例#7
0
    def test(self):
        """Tests json_utils.loads with various primitive data types."""
        self.assertEqual(json_utils.loads('"string"'), 'string')

        self.assertEqual(json_utils.loads('true'), True)
        self.assertEqual(json_utils.loads('false'), False)

        self.assertEqual(json_utils.loads('-5'), -5)
        self.assertEqual(json_utils.loads('0'), 0)
        self.assertEqual(json_utils.loads('10'), 10)
        self.assertEqual(json_utils.loads('12.0'), 12.0)

        self.assertEqual(
            json_utils.loads(
                '{"second": 3, "microsecond": 9, "hour": 1, "year": 2018, '
                '"__type__": "datetime", "day": 4, "minute": 2, "month": 2}'),
            datetime.datetime(2018, 2, 4, 1, 2, 3, 9))

        self.assertEqual(
            json_utils.loads(
                '{"__type__": "date", "day": 20, "month": 3, "year": 2018}'),
            datetime.date(2018, 3, 20))