Пример #1
0
def get_caller_addon(depth=0, max_depth=5):
    """Guess the caller addon.

    :param depth: Skip that many levels in the call stack.

    :param max_depth: Max level to look in the call stack.

    Technically, we look in the globals of the *calling* stack frame for the
    ``__name__`` and, its matches the format with 'oddo.addons.<addon>.*',
    return the addon name; otherwise, look up in the frame stack until
    `max_depth` is reached.  No addon name is found, return None.

    .. versionchanged:: 0.51.0 Added `max_depth` argument.

    """
    res = None
    frame = sys._getframe(1 + depth)
    while depth < max_depth and frame is not None and not res:
        module = frame.f_globals["__name__"]
        if module.startswith("odoo.addons."):
            module = cut_prefix(module, "odoo.addons.")
            res = module.split(".", 1)[0]
        elif module.startswith("openerp.addons."):
            module = cut_prefix(module, "openerp.addons.")
            res = module.split(".", 1)[0]
        depth += 1
        frame = frame.f_back
    return res
Пример #2
0
    def __init__(self, enumclass, *args, **kwargs):
        selection_field_kwargs = kwargs.pop("selection_field_kwargs", {})
        if not selection_field_kwargs:
            SELECTION_FIELD_PREFIX = "selection_field_"
            selection_field_kwargs = {
                cut_prefix(kw, SELECTION_FIELD_PREFIX): value
                for kw, value in kwargs.items()
                if kw.startswith(SELECTION_FIELD_PREFIX)
            }
            compute_member_string = kwargs.pop("compute_member_string", None)
            if compute_member_string:
                selection_field_kwargs["compute_member_string"] = compute_member_string

        super(Enumeration, self).__init__(
            enumclass=enumclass,
            selection_field_kwargs=selection_field_kwargs,
            *args,
            **kwargs
        )
        self.enumclass = enumclass
        self.selection_field_kwargs = selection_field_kwargs
Пример #3
0
def test_cutting_is_stable(s, p):
    from xoutil.string import cut_prefix, cut_suffix
    if not s.startswith(p):
        assert cut_prefix(s, p) == s == cut_prefix(cut_prefix(s, p), p)
    if not s.endswith(p):
        assert cut_suffix(s, p) == s == cut_suffix(cut_suffix(s, p), p)
Пример #4
0
def test_cutting_is_inverse_to_adding(s, p):
    from xoutil.string import cut_prefix, cut_suffix
    assert cut_prefix(p + s, p) == s
    assert cut_suffix(s + p, p) == s
    assert cut_suffix(s, '') == s
    assert cut_prefix(s, '') == s