<commit_before>from __future__ import print_function

import sys

from .util import drop_while
from .predicate import (XidWindowsPredicate,
                        ClassWindowsPredicate,
                        NameWindowsPredicate,
                        PidWindowsPredicate,
                        TypeWindowsPredicate,
                        OffsetWindowsPredicate,
                        AllWindowsPredicate,
                        CurrentWorkspacePredicate,
                        NumberWorkspacePredicate,
                        UnknownPredicate)


class SelectorFactory(object):
    def __new__(klass, selector_expr, expression, model,
                is_global=False):
        selectors = {'%': CurrentWindowSelector,
                     '#': PriorWindowSelector,
                     'g': GlobalSelector,
                     '<': WindowPredicateSelector,
                     '[': WorkspacePredicateSelector,
                     None: NullSelector}
        klass = selectors.get(selector_expr, UnknownSelector)
        return klass(selector_expr, expression, model, is_global)


class GlobalSelector(object):
    def __new__(klass, _old_selector_expr, expression, model, is_global):
        selector_expr = drop_while(expression.get('global'),
                                   lambda e: e == 'g')[0]
        return SelectorFactory(selector_expr, expression, model,
                               is_global=True)


class NullSelector(object):
    def __init__(self, selector_expr, expr, model, is_global):
        pass

    def runWindow(self, modification):
        pass


class UnknownSelector(object):
    def __init__(self, selector_expr, expr, model, is_global):
        self.selector_expr = selector_expr

    def runWindow(self, modification):
        self._print_error()

    def moveTo(self, direction):
        self._print_error()

    def move(self, window):
        self._print_error()

    def _print_error(self):
        print("Unknown selector: %s" % self.selector_expr, file=sys.stderr)


class CurrentWindowSelector(object):
    def __init__(self, selector_expr, expr, model, is_global):
        self.selector_expr = selector_expr
        self.model = model

    def runWindow(self, modification):
        modification(self._window())

    def moveTo(self, obj):
        obj.move(self._window())

    def move(self, window):
        print("Cannot move onto the current window")

    def activate(self):
        self.model.activate_window(self._window())

    def _window(self):
        return self.model.active_window


class PriorWindowSelector(object):
    def __init__(self, selector_expr, expr, model, is_global):
        self.selector_expr = selector_expr
        self.model = model

    def runWindow(self, modification):
        modification(self._window())

    def moveTo(self, obj):
        obj.move(self._window())

    def move(self, window):
        print("Cannot move onto the prior window")

    def activate(self):
        self.model.activate_window(self._window())

    def _window(self):
        return self.model.prior_window


class WindowPredicateSelector(object):
    def __init__(self, selector_expr, expression, model,
                 is_global):
        self.selector_expr = selector_expr
        self.expression = expression
        self.model = model
        self.is_global = is_global

    def runWindow(self, modification):
        for window in self._windows():
            modification(window)

    def activate(self):
        windows = self._windows()
        if not windows:
            pass
        elif len(windows) != 1:
            print("Only window may be activated at a time")
        else:
            self.model.activate_window(windows[0])

    def move(self, window):
        print("Cannot move onto a window predicate")

    def moveTo(self, obj):
        for window in self._windows():
            obj.move(window)

    def _windows(self):
        windows = self._predicate().windows()
        if len(windows) == 0:
            print("No match", file=sys.stderr)
        return windows

    def _predicate(self):
        def predicate_klass():
            if len(self.predicate_expr) == 0:
                return AllWindowsPredicate
            elif self.predicate_expr[0].isdigit():
                return OffsetWindowsPredicate
            else:
                predicates = {'#': XidWindowsPredicate,
                              '.': ClassWindowsPredicate,
                              '@': NameWindowsPredicate,
                              '&': PidWindowsPredicate,
                              '?': TypeWindowsPredicate}
                return predicates.get(self.predicate_expr[0],
                                      UnknownPredicate)

        return predicate_klass()(self.predicate_expr,
                                 self.model,
                                 self.is_global)

    @property
    def predicate_expr(self):
        return self.expression['window'][1:-1]


class WorkspacePredicateSelector(object):

    def __init__(self, selector_expr, expression, model, is_global):
        self.selector_expr = selector_expr
        self.expression = expression
        self.model = model
        self.is_global

    def _workspace(self):
        return self._predicate().workspace()

    def _predicate(self):
        def predicate_klass():
            if len(self.predicate_expr) == 0:<commit_msg>Set attr is_global on WorkplacePredicateSelector<commit_after>from __future__ import print_function

import sys

from .util import drop_while
from .predicate import (XidWindowsPredicate,
                        ClassWindowsPredicate,
                        NameWindowsPredicate,
                        PidWindowsPredicate,
                        TypeWindowsPredicate,
                        OffsetWindowsPredicate,
                        AllWindowsPredicate,
                        CurrentWorkspacePredicate,
                        NumberWorkspacePredicate,
                        UnknownPredicate)


class SelectorFactory(object):
    def __new__(klass, selector_expr, expression, model,
                is_global=False):
        selectors = {'%': CurrentWindowSelector,
                     '#': PriorWindowSelector,
                     'g': GlobalSelector,
                     '<': WindowPredicateSelector,
                     '[': WorkspacePredicateSelector,
                     None: NullSelector}
        klass = selectors.get(selector_expr, UnknownSelector)
        return klass(selector_expr, expression, model, is_global)


class GlobalSelector(object):
    def __new__(klass, _old_selector_expr, expression, model, is_global):
        selector_expr = drop_while(expression.get('global'),
                                   lambda e: e == 'g')[0]
        return SelectorFactory(selector_expr, expression, model,
                               is_global=True)


class NullSelector(object):
    def __init__(self, selector_expr, expr, model, is_global):
        pass

    def runWindow(self, modification):
        pass


class UnknownSelector(object):
    def __init__(self, selector_expr, expr, model, is_global):
        self.selector_expr = selector_expr

    def runWindow(self, modification):
        self._print_error()

    def moveTo(self, direction):
        self._print_error()

    def move(self, window):
        self._print_error()

    def _print_error(self):
        print("Unknown selector: %s" % self.selector_expr, file=sys.stderr)


class CurrentWindowSelector(object):
    def __init__(self, selector_expr, expr, model, is_global):
        self.selector_expr = selector_expr
        self.model = model

    def runWindow(self, modification):
        modification(self._window())

    def moveTo(self, obj):
        obj.move(self._window())

    def move(self, window):
        print("Cannot move onto the current window")

    def activate(self):
        self.model.activate_window(self._window())

    def _window(self):
        return self.model.active_window


class PriorWindowSelector(object):
    def __init__(self, selector_expr, expr, model, is_global):
        self.selector_expr = selector_expr
        self.model = model

    def runWindow(self, modification):
        modification(self._window())

    def moveTo(self, obj):
        obj.move(self._window())

    def move(self, window):
        print("Cannot move onto the prior window")

    def activate(self):
        self.model.activate_window(self._window())

    def _window(self):
        return self.model.prior_window


class WindowPredicateSelector(object):
    def __init__(self, selector_expr, expression, model,
                 is_global):
        self.selector_expr = selector_expr
        self.expression = expression
        self.model = model
        self.is_global = is_global

    def runWindow(self, modification):
        for window in self._windows():
            modification(window)

    def activate(self):
        windows = self._windows()
        if not windows:
            pass
        elif len(windows) != 1:
            print("Only window may be activated at a time")
        else:
            self.model.activate_window(windows[0])

    def move(self, window):
        print("Cannot move onto a window predicate")

    def moveTo(self, obj):
        for window in self._windows():
            obj.move(window)

    def _windows(self):
        windows = self._predicate().windows()
        if len(windows) == 0:
            print("No match", file=sys.stderr)
        return windows

    def _predicate(self):
        def predicate_klass():
            if len(self.predicate_expr) == 0:
                return AllWindowsPredicate
            elif self.predicate_expr[0].isdigit():
                return OffsetWindowsPredicate
            else:
                predicates = {'#': XidWindowsPredicate,
                              '.': ClassWindowsPredicate,
                              '@': NameWindowsPredicate,
                              '&': PidWindowsPredicate,
                              '?': TypeWindowsPredicate}
                return predicates.get(self.predicate_expr[0],
                                      UnknownPredicate)

        return predicate_klass()(self.predicate_expr,
                                 self.model,
                                 self.is_global)

    @property
    def predicate_expr(self):
        return self.expression['window'][1:-1]


class WorkspacePredicateSelector(object):

    def __init__(self, selector_expr, expression, model, is_global):
        self.selector_expr = selector_expr
        self.expression = expression
        self.model = model
        self.is_global = is_global

    def _workspace(self):
        return self._predicate().workspace()

    def _predicate(self):
        def predicate_klass():
            if len(self.predicate_expr) == 0:
{'fraction_non_alphanumeric': 0.08796168370636456, 'fraction_numerical': 0.00184212950170397, 'mean_word_length': 2.006923289947383, 'pattern_counts': {'":': 0, '<': 5, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7081578', 'n_tokens_mistral': 2831, 'n_tokens_neox': 2718, 'n_words': 630}