<commit_before>from .app import App
from .config import Directive
from .error import ConfigError
from .view import register_view, render_json, render_html
from .model import register_model, register_root
from .traject import Path


class directive(object):
    def __init__(self, name):
        self.name = name

    def __call__(self, directive):
        def method(self, *args, **kw):
            return directive(self, *args, **kw)
        setattr(App, self.name, method)
        return directive


@directive('model')
class ModelDirective(Directive):
    def __init__(self, app,  path, model=None,
                 variables=None, base=None, get_base=None):
        super(ModelDirective, self).__init__(app)
        self.model = model
        self.path = path
        self.variables = variables
        self.base = base
        self.get_base = get_base

    def discriminator(self):
        return [('model', self.model),
                ('path', self.base, Path(self.path).discriminator())]

    def prepare(self, obj):
        # XXX check shared with @root
        if isinstance(obj, type):
            if self.model is not None:
                raise ConfigError(
                    "@model decorates class so cannot "
                    "have explicit model: %s" % self.model)
            self.model = obj
        if self.model is None:
            raise ConfigError(
                "@model does not decorate class and has no explicit model")

        # XXX check whether get_base is there if base is there
        # XXX check whether variables is there if variable is used in
        # path

    def perform(self, obj):
        register_model(self.app, self.model, self.path,
                       self.variables, obj, self.base, self.get_base)


@directive('view')
class ViewDirective(Directive):
    def __init__(self, app, model, name='', render=None, **kw):
        super(ViewDirective, self).__init__(app)
        self.model = model
        self.name = name
        self.render = render
        self.predicates = {
            'name': self.name
            }
        self.predicates.update(kw)

    def discriminator(self):
        predicates_discriminator = tuple(sorted(self.predicates.items()))
        return ('view', self.model, self.name, predicates_discriminator)

    def perform(self, obj):
        register_view(self.app, self.model, obj, self.render,
                      self.predicates)


@directive('json')
class JsonDirective(ViewDirective):
    def __init__(self, app, model, name='', render=render_json, **kw):
        super(JsonDirective, self).__init__(app, model, name, render, **kw)


@directive('html')
class HtmlDirective(ViewDirective):
    def __init__(self, app, model, name='', render=render_html, **kw):
        super(HtmlDirective, self).__init__(app, model, name, render, **kw)


@directive('root')
class RootDirective(Directive):
    def __init__(self, app, model=None):
        super(RootDirective, self).__init__(app)
        self.model = model

    def discriminator(self):
        return ('root')

    def prepare(self, obj):
        if isinstance(obj, type):
            if self.model is not None:
                raise ConfigError(
                    "@root decorates class so cannot have explicit model: %s" %
                    self.model)
            self.model = obj
        if self.model is None:
            raise ConfigError(
                "@root does not decorate class and has no explicit model")

    def perform(self, obj):
        register_root(self.app, self.model, obj)


@directive('function')
class FunctionDirective(Directive):
    def __init__(self, app, target, *sources):
        super(FunctionDirective, self).__init__(app)
        self.target = target
        self.sources = tuple(sources)

    def discriminator(self):
        return ('function', self.target, self.sources)

    def perform(self, obj):
        self.app.register(self.target, self.sources, obj)<commit_msg>Make this a tuple for consistency.<commit_after>from .app import App
from .config import Directive
from .error import ConfigError
from .view import register_view, render_json, render_html
from .model import register_model, register_root
from .traject import Path


class directive(object):
    def __init__(self, name):
        self.name = name

    def __call__(self, directive):
        def method(self, *args, **kw):
            return directive(self, *args, **kw)
        setattr(App, self.name, method)
        return directive


@directive('model')
class ModelDirective(Directive):
    def __init__(self, app,  path, model=None,
                 variables=None, base=None, get_base=None):
        super(ModelDirective, self).__init__(app)
        self.model = model
        self.path = path
        self.variables = variables
        self.base = base
        self.get_base = get_base

    def discriminator(self):
        return [('model', self.model),
                ('path', self.base, Path(self.path).discriminator())]

    def prepare(self, obj):
        # XXX check shared with @root
        if isinstance(obj, type):
            if self.model is not None:
                raise ConfigError(
                    "@model decorates class so cannot "
                    "have explicit model: %s" % self.model)
            self.model = obj
        if self.model is None:
            raise ConfigError(
                "@model does not decorate class and has no explicit model")

        # XXX check whether get_base is there if base is there
        # XXX check whether variables is there if variable is used in
        # path

    def perform(self, obj):
        register_model(self.app, self.model, self.path,
                       self.variables, obj, self.base, self.get_base)


@directive('view')
class ViewDirective(Directive):
    def __init__(self, app, model, name='', render=None, **kw):
        super(ViewDirective, self).__init__(app)
        self.model = model
        self.name = name
        self.render = render
        self.predicates = {
            'name': self.name
            }
        self.predicates.update(kw)

    def discriminator(self):
        predicates_discriminator = tuple(sorted(self.predicates.items()))
        return ('view', self.model, self.name, predicates_discriminator)

    def perform(self, obj):
        register_view(self.app, self.model, obj, self.render,
                      self.predicates)


@directive('json')
class JsonDirective(ViewDirective):
    def __init__(self, app, model, name='', render=render_json, **kw):
        super(JsonDirective, self).__init__(app, model, name, render, **kw)


@directive('html')
class HtmlDirective(ViewDirective):
    def __init__(self, app, model, name='', render=render_html, **kw):
        super(HtmlDirective, self).__init__(app, model, name, render, **kw)


@directive('root')
class RootDirective(Directive):
    def __init__(self, app, model=None):
        super(RootDirective, self).__init__(app)
        self.model = model

    def discriminator(self):
        return ('root',)

    def prepare(self, obj):
        if isinstance(obj, type):
            if self.model is not None:
                raise ConfigError(
                    "@root decorates class so cannot have explicit model: %s" %
                    self.model)
            self.model = obj
        if self.model is None:
            raise ConfigError(
                "@root does not decorate class and has no explicit model")

    def perform(self, obj):
        register_root(self.app, self.model, obj)


@directive('function')
class FunctionDirective(Directive):
    def __init__(self, app, target, *sources):
        super(FunctionDirective, self).__init__(app)
        self.target = target
        self.sources = tuple(sources)

    def discriminator(self):
        return ('function', self.target, self.sources)

    def perform(self, obj):
        self.app.register(self.target, self.sources, obj)
{'fraction_non_alphanumeric': 0.10766515380736258, 'fraction_numerical': 0.0, 'mean_word_length': 2.4267818574514037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1739534', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2189, 'n_words': 644}