Beispiel #1
0
 def test_cache_expiration(self):
     cache = ExpiringCache(0.01)
     cache.put('a', 'b')
     time.sleep(0.1)
     assert_is_none(cache.get('a'))
     cache.put('a', 'c')
     assert_equal('c', cache.get('a'))
Beispiel #2
0
 def test_cache_expiration(self):
     cache = ExpiringCache(0.01)
     cache.put('a', 'b')
     time.sleep(0.1)
     assert_is_none(cache.get('a'))
     cache.put('a', 'c')
     assert_equals('c', cache.get('a'))
Beispiel #3
0
 def expire_cache(self):
     self.keyword_cache = ExpiringCache()
     self._lib_cache.expire()
Beispiel #4
0
 def __init__(self, lib_cache, resource_factory):
     self._lib_cache = lib_cache
     self._resource_factory = resource_factory
     self.keyword_cache = ExpiringCache()
     self._default_kws = None
Beispiel #5
0
class DatafileRetriever(object):
    def __init__(self, lib_cache, resource_factory):
        self._lib_cache = lib_cache
        self._resource_factory = resource_factory
        self.keyword_cache = ExpiringCache()
        self._default_kws = None

    def get_all_cached_library_names(self):
        return self._lib_cache.get_all_cached_library_names()

    @property
    def default_kws(self):
        if self._default_kws is None:
            self._default_kws = self._lib_cache.get_default_keywords()
        return self._default_kws

    def expire_cache(self):
        self.keyword_cache = ExpiringCache()
        self._lib_cache.expire()

    def get_keywords_from_several(self, datafiles):
        kws = set()
        kws.update(self.default_kws)
        for df in datafiles:
            kws.update(self.get_keywords_from(df, RetrieverContext()))
        return kws

    def get_keywords_from(self, datafile, ctx):
        self._get_vars_recursive(datafile, ctx)
        ctx.allow_going_through_resources_again()
        return sorted(set(self._get_datafile_keywords(datafile) +\
              self._get_imported_resource_keywords(datafile, ctx) +\
              self._get_imported_library_keywords(datafile, ctx)))

    def is_library_import_ok(self, datafile, imp, ctx):
        self._get_vars_recursive(datafile, ctx)
        return bool(self._lib_kw_getter(imp, ctx))

    def is_variables_import_ok(self, datafile, imp, ctx):
        self._get_vars_recursive(datafile, ctx)
        return self._import_vars(ctx, datafile, imp)

    def _get_datafile_keywords(self, datafile):
        if isinstance(datafile, robotapi.ResourceFile):
            return [ResourceUserKeywordInfo(kw) for kw in datafile.keywords]
        return [TestCaseUserKeywordInfo(kw) for kw in datafile.keywords]

    def _get_imported_library_keywords(self, datafile, ctx):
        return self._collect_kws_from_imports(datafile, robotapi.Library,
                                              self._lib_kw_getter, ctx)

    def _collect_kws_from_imports(self, datafile, instance_type, getter, ctx):
        kws = []
        for imp in self._collect_import_of_type(datafile, instance_type):
            kws.extend(getter(imp, ctx))
        return kws

    def _lib_kw_getter(self, imp, ctx):
        name = ctx.replace_variables(imp.name)
        name = self._convert_to_absolute_path(name, imp)
        args = [ctx.replace_variables(a) for a in imp.args]
        alias = ctx.replace_variables(imp.alias) if imp.alias else None
        return self._lib_cache.get_library_keywords(name, args, alias)

    def _convert_to_absolute_path(self, name, import_):
        full_name = os.path.join(import_.directory, name)
        if os.path.exists(full_name):
            return full_name
        return name

    def _collect_import_of_type(self, datafile, instance_type):
        return [imp for imp in datafile.imports
                if isinstance(imp, instance_type)]

    def _get_imported_resource_keywords(self, datafile, ctx):
        return self._collect_kws_from_imports(datafile, robotapi.Resource,
                                              self._res_kw_recursive_getter,
                                              ctx)

    def _res_kw_recursive_getter(self, imp, ctx):
        kws = []
        res = self._resource_factory.get_resource_from_import(imp, ctx)
        if not res or res in ctx.parsed:
            return kws
        ctx.parsed.add(res)
        ctx.set_variables_from_datafile_variable_table(res)
        for child in self._collect_import_of_type(res, robotapi.Resource):
            kws.extend(self._res_kw_recursive_getter(child, ctx))
        kws.extend(self._get_imported_library_keywords(res, ctx))
        return [ResourceUserKeywordInfo(kw) for kw in res.keywords] + kws

    def get_variables_from(self, datafile, ctx=None):
        return self._get_vars_recursive(datafile,
                                        ctx or RetrieverContext()).vars

    def _get_vars_recursive(self, datafile, ctx):
        ctx.set_variables_from_datafile_variable_table(datafile)
        self._collect_vars_from_variable_files(datafile, ctx)
        self._collect_each_res_import(datafile, ctx, self._var_collector)
        return ctx

    def _collect_vars_from_variable_files(self, datafile, ctx):
        for imp in self._collect_import_of_type(datafile, robotapi.Variables):
            self._import_vars(ctx, datafile, imp)

    def _import_vars(self, ctx, datafile, imp):
        varfile_path = os.path.join(datafile.directory,
                                    ctx.replace_variables(imp.name))
        args = [ctx.replace_variables(a) for a in imp.args]
        try:
            ctx.vars.set_from_file(varfile_path, args)
            return True
        except robotapi.DataError:
            return False  # TODO: log somewhere

    def _var_collector(self, res, ctx, items):
        self._get_vars_recursive(res, ctx)

    def get_keywords_cached(self, datafile, context_factory):
        values = self.keyword_cache.get(datafile.source)
        if not values:
            words = self.get_keywords_from(
                datafile, context_factory.ctx_for_datafile(datafile))
            words.extend(self.default_kws)
            values = _Keywords(words)
            self.keyword_cache.put(datafile.source, values)
        return values

    def _get_user_keywords_from(self, datafile):
        return list(self._get_user_keywords_recursive(datafile,
                                                      RetrieverContext()))

    def _get_user_keywords_recursive(self, datafile, ctx):
        kws = set()
        kws.update(datafile.keywords)
        kws_from_res = self._collect_each_res_import(
            datafile, ctx,
            lambda res, ctx, kws: kws.update(self._get_user_keywords_recursive(res, ctx)))
        kws.update(kws_from_res)
        return kws

    def _collect_each_res_import(self, datafile, ctx, collector):
        items = set()
        ctx.set_variables_from_datafile_variable_table(datafile)
        for imp in self._collect_import_of_type(datafile, robotapi.Resource):
            res = self._resource_factory.get_resource_from_import(imp, ctx)
            if res and res not in ctx.parsed:
                ctx.parsed.add(res)
                collector(res, ctx, items)
        return items

    def get_resources_from(self, datafile):
        resources = list(self._get_resources_recursive(datafile,
                                                       RetrieverContext()))
        resources.sort(key=operator.attrgetter('name'))
        return resources

    def _get_resources_recursive(self, datafile, ctx):
        resources = set()
        res = self._collect_each_res_import(datafile, ctx, self._add_resource)
        resources.update(res)
        for child in datafile.children:
            resources.update(self.get_resources_from(child))
        return resources

    def _add_resource(self, res, ctx, items):
        items.add(res)
        items.update(self._get_resources_recursive(res, ctx))
Beispiel #6
0
 def expire_cache(self):
     self.keyword_cache = ExpiringCache()
Beispiel #7
0
 def test_cache_hit(self):
     cache = ExpiringCache(0.1)
     cache.put('a', 'b')
     assert_equal('b', cache.get('a'))
Beispiel #8
0
 def expire_cache(self):  #内存终止
     self.keyword_cache = ExpiringCache()
     self._lib_cache.expire()
Beispiel #9
0
 def __init__(self, lib_cache, resource_factory):
     self._lib_cache = lib_cache
     self._resource_factory = resource_factory
     self.keyword_cache = ExpiringCache()
     self._default_kws = None
Beispiel #10
0
class DatafileRetriever(object):  #获取数据文件
    def __init__(self, lib_cache, resource_factory):
        self._lib_cache = lib_cache
        self._resource_factory = resource_factory
        self.keyword_cache = ExpiringCache()
        self._default_kws = None

    def get_all_cached_library_names(self):  #获取所有的内存中库名
        return self._lib_cache.get_all_cached_library_names()

    @property
    def default_kws(self):
        if self._default_kws is None:
            self._default_kws = self._lib_cache.get_default_keywords()
        return self._default_kws

    def expire_cache(self):  #内存终止
        self.keyword_cache = ExpiringCache()
        self._lib_cache.expire()

    def get_keywords_from_several(self, datafiles):  #从各自中获取关键词
        kws = set()
        kws.update(self.default_kws)
        for df in datafiles:
            kws.update(self.get_keywords_from(df, RetrieverContext()))
        return kws

    def get_keywords_from(self, datafile, ctx):
        self._get_vars_recursive(datafile, ctx)
        ctx.allow_going_through_resources_again()
        return sorted(set(self._get_datafile_keywords(datafile) +\
              self._get_imported_resource_keywords(datafile, ctx) +\
              self._get_imported_library_keywords(datafile, ctx)))

    def is_library_import_ok(self, datafile, imp, ctx):  #导入库是否完成
        self._get_vars_recursive(datafile, ctx)
        return bool(self._lib_kw_getter(imp, ctx))

    def is_variables_import_ok(self, datafile, imp, ctx):  #导入变量是否完成
        self._get_vars_recursive(datafile, ctx)
        return self._import_vars(ctx, datafile, imp)

    def _get_datafile_keywords(self, datafile):  #获取数据文件关键字
        if isinstance(datafile, robotapi.ResourceFile):
            return [ResourceUserKeywordInfo(kw) for kw in datafile.keywords]
        return [TestCaseUserKeywordInfo(kw) for kw in datafile.keywords]

    def _get_imported_library_keywords(self, datafile, ctx):  #获取导入的库关键词
        return self._collect_kws_from_imports(datafile, robotapi.Library,
                                              self._lib_kw_getter, ctx)

    def _collect_kws_from_imports(self, datafile, instance_type, getter,
                                  ctx):  #从导入的内容中收集关键词
        kws = []
        for imp in self._collect_import_of_type(datafile, instance_type):
            kws.extend(getter(imp, ctx))
        return kws

    def _lib_kw_getter(self, imp, ctx):
        name = ctx.replace_variables(imp.name)
        name = self._convert_to_absolute_path(name, imp)
        args = [ctx.replace_variables(a) for a in imp.args]
        alias = ctx.replace_variables(imp.alias) if imp.alias else None
        return self._lib_cache.get_library_keywords(name, args, alias)

    def _convert_to_absolute_path(self, name, import_):  #转换绝对路径
        full_name = os.path.join(import_.directory, name)
        if os.path.exists(full_name):
            return full_name
        return name

    def _collect_import_of_type(self, datafile, instance_type):  #收集导入类型
        return [
            imp for imp in datafile.imports if isinstance(imp, instance_type)
        ]

    def _get_imported_resource_keywords(self, datafile, ctx):  #获取导入的资源关键词
        return self._collect_kws_from_imports(datafile, robotapi.Resource,
                                              self._res_kw_recursive_getter,
                                              ctx)

    def _res_kw_recursive_getter(self, imp, ctx):  #循环获取资源关键词
        kws = []
        res = self._resource_factory.get_resource_from_import(imp, ctx)
        if not res or res in ctx.parsed:
            return kws
        ctx.parsed.add(res)
        ctx.set_variables_from_datafile_variable_table(res)
        for child in self._collect_import_of_type(res, robotapi.Resource):
            kws.extend(self._res_kw_recursive_getter(child, ctx))
        kws.extend(self._get_imported_library_keywords(res, ctx))
        return [ResourceUserKeywordInfo(kw) for kw in res.keywords] + kws

    def get_variables_from(self, datafile, ctx=None):  #获取变量
        return self._get_vars_recursive(datafile, ctx
                                        or RetrieverContext()).vars

    def _get_vars_recursive(self, datafile, ctx):  #循环获取变量
        ctx.set_variables_from_datafile_variable_table(datafile)
        self._collect_vars_from_variable_files(datafile, ctx)
        self._collect_each_res_import(datafile, ctx, self._var_collector)
        return ctx

    def _collect_vars_from_variable_files(self, datafile, ctx):  #从变量文件中收集变量
        for imp in self._collect_import_of_type(datafile, robotapi.Variables):
            self._import_vars(ctx, datafile, imp)

    def _import_vars(self, ctx, datafile, imp):  #导入变量
        varfile_path = os.path.join(datafile.directory,
                                    ctx.replace_variables(imp.name))
        args = [ctx.replace_variables(a) for a in imp.args]
        try:
            ctx.vars.set_from_file(varfile_path, args)
            return True
        except robotapi.DataError:
            return False  # TODO: log somewhere

    def _var_collector(self, res, ctx, items):  #变量收集
        self._get_vars_recursive(res, ctx)

    def get_keywords_cached(self, datafile, context_factory):  #获取关键词内存
        values = self.keyword_cache.get(datafile.source)
        if not values:
            words = self.get_keywords_from(
                datafile, context_factory.ctx_for_datafile(datafile))
            words.extend(self.default_kws)
            values = _Keywords(words)
            self.keyword_cache.put(datafile.source, values)
        return values

    def _get_user_keywords_from(self, datafile):  #获取用户关键词
        return list(
            self._get_user_keywords_recursive(datafile, RetrieverContext()))

    def _get_user_keywords_recursive(self, datafile, ctx):  #循环获取用户关键词
        kws = set()
        kws.update(datafile.keywords)
        kws_from_res = self._collect_each_res_import(
            datafile, ctx, lambda res, ctx, kws: kws.update(
                self._get_user_keywords_recursive(res, ctx)))
        kws.update(kws_from_res)
        return kws

    def _collect_each_res_import(self, datafile, ctx, collector):  #收集每一个导入的资源
        items = set()
        ctx.set_variables_from_datafile_variable_table(datafile)
        for imp in self._collect_import_of_type(datafile, robotapi.Resource):
            res = self._resource_factory.get_resource_from_import(imp, ctx)
            if res and res not in ctx.parsed:
                ctx.parsed.add(res)
                collector(res, ctx, items)
        return items

    def get_resources_from(self, datafile):
        resources = list(
            self._get_resources_recursive(datafile, RetrieverContext()))
        resources.sort(key=operator.attrgetter('name'))
        return resources

    def _get_resources_recursive(self, datafile, ctx):  #循环获取资源
        resources = set()
        res = self._collect_each_res_import(datafile, ctx, self._add_resource)
        resources.update(res)
        for child in datafile.children:
            resources.update(self.get_resources_from(child))

        return resources

    def _add_resource(self, res, ctx, items):
        items.add(res)
        items.update(self._get_resources_recursive(res, ctx))
Beispiel #11
0
 def test_cache_hit(self):
     cache = ExpiringCache(0.1)
     cache.put('a', 'b')
     assert_equals('b', cache.get('a'))