How to use the patchy.core.PatchBase function in patchy

To help you get started, we’ve selected a few patchy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ashleywaite / django-more / patchy / core.py View on Github external
# Apply patched value
            setattr(self.target, attr, value)

    def get_attrs(self, source=None, exclude_hidden=True):
        # Get all attributes, except hidden if exclude_hidden
        # but allowing whitelisted attributes (like __all__)
        source = source or self.source
        return (
            (attr, val)
            for attr, val in source.__dict__.items()
            if attr in self.allow
            or not exclude_hidden
            or not attr.startswith('_'))


class PatchModule(PatchBase):
    allow = {'__all__'}

    def __init__(self, target, source=None, module_sep='_'):
        self.target = target
        self.source = source
        self.module_sep = module_sep

    def cls(self, target, source=None):
        if isinstance(target, str):
            target = resolve(target, package=self.target)
        if self.source and source is None:
            with suppress(ImportError):
                source_str = '{mod}.{cls}'.format(
                    mod=target.__module__.replace('.', self.module_sep),
                    cls=target.__name__)
                source = resolve(source_str, package=self.source)
github ashleywaite / django-more / patchy / core.py View on Github external
if isinstance(target, ModuleType):
            if source:
                logger.info('Patching {} using {}'.format(target.__name__, source.__name__))
            return PatchModule(target, source, self.module_sep)

    def get_auto_attrs(self, source=None, exclude_hidden=True):
        # Only auto locally declared objects, or attributes in allow
        return (
            (attr, val)
            for attr, val in self.get_attrs(source, exclude_hidden)
            if (hasattr(val, '__module__') and val.__module__ == source.__name__)
            or attr in self.allow)


class PatchClass(PatchBase):
    allow = {'__init__', '__new__'}

    def __init__(self, target, source):
        self.target = target
        self.source = source

    def mod(self):
        return self.target.__module__

    def get_auto_attrs(self, source=None, exclude_hidden=True):
        # Only auto attributes, locally declared objects, or hiddens in allow
        return (
            (attr, val)
            for attr, val in self.get_attrs(source, exclude_hidden)
            if not hasattr(val, '__module__')
            or val.__module__ == source.__module__