1import importlib
2import imp
3
4from .browsers import product_list
5
6
7def products_enabled(config):
8    names = config.get("products", {}).keys()
9    if not names:
10        return product_list
11    else:
12        return names
13
14
15def product_module(config, product):
16    if product not in products_enabled(config):
17        raise ValueError("Unknown product %s" % product)
18
19    path = config.get("products", {}).get(product, None)
20    if path:
21        module = imp.load_source('wptrunner.browsers.' + product, path)
22    else:
23        module = importlib.import_module("wptrunner.browsers." + product)
24
25    if not hasattr(module, "__wptrunner__"):
26        raise ValueError("Product module does not define __wptrunner__ variable")
27
28    return module
29
30
31class Product(object):
32    def __init__(self, config, product):
33        module = product_module(config, product)
34        data = module.__wptrunner__
35        self.name = product
36        if isinstance(data["browser"], str):
37            self._browser_cls = {None: getattr(module, data["browser"])}
38        else:
39            self._browser_cls = {key: getattr(module, value)
40                                 for key, value in data["browser"].items()}
41        self.check_args = getattr(module, data["check_args"])
42        self.get_browser_kwargs = getattr(module, data["browser_kwargs"])
43        self.get_executor_kwargs = getattr(module, data["executor_kwargs"])
44        self.env_options = getattr(module, data["env_options"])()
45        self.get_env_extras = getattr(module, data["env_extras"])
46        self.run_info_extras = (getattr(module, data["run_info_extras"])
47                                if "run_info_extras" in data else lambda **kwargs:{})
48        self.get_timeout_multiplier = getattr(module, data["timeout_multiplier"])
49
50        self.executor_classes = {}
51        for test_type, cls_name in data["executor"].items():
52            cls = getattr(module, cls_name)
53            self.executor_classes[test_type] = cls
54
55    def get_browser_cls(self, test_type):
56        if test_type in self._browser_cls:
57            return self._browser_cls[test_type]
58        return self._browser_cls[None]
59
60
61def load_product_update(config, product):
62    """Return tuple of (property_order, boolean_properties) indicating the
63    run_info properties to use when constructing the expectation data for
64    this product. None for either key indicates that the default keys
65    appropriate for distinguishing based on platform will be used."""
66
67    module = product_module(config, product)
68    data = module.__wptrunner__
69
70    update_properties = (getattr(module, data["update_properties"])()
71                         if "update_properties" in data else {})
72
73    return update_properties
74