예제 #1
0
class TextSpanAbstractView(_TextAbstractView):
    def __init__(self, worksheet, model):
        super(TextSpanAbstractView, self).__init__(worksheet, model)
        self._styleMapLive = LiveFunction(self.__compute_style_map)

    def getStyleAttrs(self):
        return self._styleMapLive.getValue()[0]

    def getStyleSheet(self):
        return self._styleMapLive.getValue()[1]

    _styleMap = {}
    _styleMap['italic'] = lambda value: Primitive.fontItalic(bool(value))
    _styleMap['bold'] = lambda value: Primitive.fontBold(bool(value))
    _styleMap['underline'] = lambda value: Primitive.fontUnderline(bool(value))
    _styleMap['strikethrough'] = lambda value: Primitive.fontStrikethrough(
        bool(value))

    def __compute_style_map(self):
        attrs = self._model['styleAttrs']

        m = {}
        styles = []

        for a in attrs:
            key = a['name']
            value = a['value']

            m[key] = value
            styles.append(self._styleMap[key](value))

        return m, StyleSheet.style(*styles)
 def __init__(self, contents=None):
     super(ProjectContainer, self).__init__()
     self._contents_ = []
     self._prevContents = []
     self._contentsMapLive = LiveFunction(self._computeContentsMap)
     if contents is not None:
         self[:] = contents
    def __setstate__(self, state):
        super(ProjectContainer, self).__setstate__(state)
        self._contents_ = state['contents']
        self._prevContents = self._contents[:]
        self._contentsMapLive = LiveFunction(self._computeContentsMap)

        for x in self._contents_:
            x._setParent(self, True)
예제 #4
0
    def __init__(self, underlying, map_fns, inverse_map_fns):
        self.__underlying = underlying
        self.__map_fns = map_fns
        self.__inverse_map_fns = inverse_map_fns

        def _apply():
            return [self.__apply(x) for x in self.__underlying]

        self.__live = LiveFunction(_apply)
예제 #5
0
    def __init__(self, underlying, filter_fns):
        self.__underlying = underlying
        self.__filter_fns = filter_fns

        def _apply():
            ii = zip(*[(i, x) for i, x in enumerate(self.__underlying)
                       if self.__test(x)])
            if len(ii) == 2:
                return list(ii[0]), list(ii[1])
            else:
                return [], []

        self.__live = LiveFunction(_apply)

        def _live_listener(inc):
            if self.__change_listener is not None:
                self.__change_listener()

        self.__change_listener = None
예제 #6
0
class BodyAbstractView(NodeAbstractView):
    def __init__(self, worksheet, model):
        super(BodyAbstractView, self).__init__(worksheet, model)
        self._contentsLive = LiveFunction(self._computeContents)

    def refreshResults(self, module):
        for v in self.getContents():
            v._refreshResults(module)

    def getContents(self):
        return self._contentsLive.getValue()

    def _computeContents(self):
        return [self._viewOf(x) for x in self._model['contents']]
예제 #7
0
class _TextAbstractView(NodeAbstractView):
    def __init__(self, worksheet, model):
        super(_TextAbstractView, self).__init__(worksheet, model)
        self._textLive = LiveFunction(self._computeText)

    def getText(self):
        return self._textLive.getValue()

    def _refreshResults(self, module):
        for x in self.getText():
            if not isinstance(x, str) and not isinstance(x, unicode):
                x._refreshResults(module)

    def _computeText(self):
        return [
            x if isinstance(x, str) or isinstance(x, unicode) else
            self._viewOf(x) for x in self._model['text']
        ]

    @staticmethod
    def _textToModel(text):
        return [(x if isinstance(x, str) or isinstance(x, unicode) else
                 x.getModel()) for x in text]
예제 #8
0
 def __init__(self, worksheet, model):
     super(_TextAbstractView, self).__init__(worksheet, model)
     self._textLive = LiveFunction(self._computeText)
예제 #9
0
 def __init__(self, worksheet, model):
     super(BodyAbstractView, self).__init__(worksheet, model)
     self._contentsLive = LiveFunction(self._computeContents)
예제 #10
0
 def __init__(self, worksheet, model):
     super(TextSpanAbstractView, self).__init__(worksheet, model)
     self._styleMapLive = LiveFunction(self.__compute_style_map)
예제 #11
0
class FilterProjectedList(_ProjectedList):
    def __init__(self, underlying, filter_fns):
        self.__underlying = underlying
        self.__filter_fns = filter_fns

        def _apply():
            ii = zip(*[(i, x) for i, x in enumerate(self.__underlying)
                       if self.__test(x)])
            if len(ii) == 2:
                return list(ii[0]), list(ii[1])
            else:
                return [], []

        self.__live = LiveFunction(_apply)

        def _live_listener(inc):
            if self.__change_listener is not None:
                self.__change_listener()

        self.__change_listener = None

    @property
    def change_listener(self):
        return self.__change_listener

    @change_listener.setter
    def change_listener(self, x):
        self.__change_listener = x

    def __test(self, item):
        for f in self.__filter_fns:
            if not f(item):
                return False
        return True

    @property
    def _indices_and_items(self):
        return self.__live.getValue()

    @property
    def _indices(self):
        return self.__live.getValue()[0]

    @property
    def _items(self):
        return self.__live.getValue()[1]

    def filter(self, fn):
        return FilterProjectedList(self.__underlying, self.__filter_fns + [fn])

    def __iter__(self):
        return iter(self._items)

    def __contains__(self, x):
        return x in self._items

    def __add__(self, xs):
        return self._items + xs

    def __mul__(self, x):
        return self._items * x

    def __rmul__(self, x):
        return x * self._items

    def __getitem__(self, index):
        return self._items[index]

    def __len__(self):
        return len(self._items)

    def index(self, x, i=None, j=None):
        if i is None:
            return self._items.index(x)
        elif j is None:
            return self._items.index(x, i)
        else:
            return self._items.index(x, i, j)

    def count(self, x):
        return self._items.count(x)

    def __setitem__(self, index, x):
        if isinstance(index, int) or isinstance(index, long):
            self.__underlying[self._indices[index]] = x
        else:
            indices = self._indices[index]
            for i, a in zip(indices, x):
                self.__underlying[i] = a
            if len(x) > len(indices):
                i = indices[-1] + 1 if len(indices) > 0 else len(
                    self.__underlying)
                for a in x[len(indices):]:
                    self.__underlying.insert(i, a)
                    i += 1
            elif len(x) < len(indices):
                for i in reversed(indices[len(x):]):
                    del self.__underlying[i]

    def __delitem__(self, index):
        if isinstance(index, int) or isinstance(index, long):
            del self.__underlying[self._indices[index]]
        else:
            indices = self._indices[index]
            for i in reversed(indices):
                del self.__underlying[i]

    def append(self, x):
        self.__underlying.append(x)

    def extend(self, xs):
        self.__underlying.extend(xs)

    def insert(self, i, x):
        indices = self._indices
        l = len(indices)
        i = min(max(i, -l), l - 1)
        n = self._indices[i]
        self.__underlying.insert(n, x)

    def pop(self):
        i = self._indices[-1]
        x = self.__underlying[i]
        del self.__underlying[i]
        return x

    def remove(self, x):
        i = self._items.index(x)
        n = self._indices[i]
        del self.__underlying[n]

    def reverse(self):
        raise TypeError, 'Invalid operation'

    def sort(self, cmp=None, key=None, reverse=None):
        raise TypeError, 'Invalid operation'
예제 #12
0
class MapProjectedList(_ProjectedList):
    def __init__(self, underlying, map_fns, inverse_map_fns):
        self.__underlying = underlying
        self.__map_fns = map_fns
        self.__inverse_map_fns = inverse_map_fns

        def _apply():
            return [self.__apply(x) for x in self.__underlying]

        self.__live = LiveFunction(_apply)

    def __apply(self, item):
        for m in self.__map_fns:
            item = m(item)
        return item

    def __inv_apply(self, item):
        for m in self.__inverse_map_fns:
            item = m(item)
        return item

    def map(self, fn, inv_fn):
        return MapProjectedList(self.__underlying, self.__map_fns + [fn],
                                [inv_fn] + self.__inverse_map_fns)

    @property
    def _items(self):
        return self.__live.getValue()

    def __iter__(self):
        return iter(self._items)

    def __contains__(self, x):
        return x in self._items

    def __add__(self, xs):
        return self._items + xs

    def __mul__(self, x):
        return self._items * x

    def __rmul__(self, x):
        return x * self._items

    def __getitem__(self, index):
        return self._items[index]

    def __len__(self):
        return len(self._items)

    def index(self, x, i=None, j=None):
        if i is None:
            return self._items.index(x)
        elif j is None:
            return self._items.index(x, i)
        else:
            return self._items.index(x, i, j)

    def count(self, x):
        return self._items.count(x)

    def __setitem__(self, index, x):
        if isinstance(index, int) or isinstance(index, long):
            self.__underlying[index] = self.__inv_apply(x)
        else:
            self.__underlying[index] = [self.__inv_apply(a) for a in x]

    def __delitem__(self, index):
        del self.__underlying[index]

    def append(self, x):
        self.__underlying.append(self.__inv_apply(x))

    def extend(self, xs):
        self.__underlying.extend([self.__inv_apply(x) for x in xs])

    def insert(self, i, x):
        self.__underlying.insert(i, self.__inv_apply(x))

    def pop(self):
        x = self._items[-1]
        self.__underlying.pop()
        return x

    def remove(self, x):
        i = self._items.index(x)
        del self.__underlying[i]

    def reverse(self):
        raise TypeError, 'Invalid operation'

    def sort(self, cmp=None, key=None, reverse=None):
        raise TypeError, 'Invalid operation'
class ProjectContainer(ProjectNode):
    def __init__(self, contents=None):
        super(ProjectContainer, self).__init__()
        self._contents_ = []
        self._prevContents = []
        self._contentsMapLive = LiveFunction(self._computeContentsMap)
        if contents is not None:
            self[:] = contents

    @property
    def moduleNames(self):
        names = []
        for c in self._contents:
            names.extend(c.moduleNames)
        return names

    def __getstate__(self):
        state = super(ProjectContainer, self).__getstate__()
        state['contents'] = self._contents_
        return state

    def __setstate__(self, state):
        super(ProjectContainer, self).__setstate__(state)
        self._contents_ = state['contents']
        self._prevContents = self._contents[:]
        self._contentsMapLive = LiveFunction(self._computeContentsMap)

        for x in self._contents_:
            x._setParent(self, True)

    def _computeContentsMap(self):
        m = {}
        self._incr.onAccess()
        for x in self._contents:
            m[x.name] = x
        return m

    def __len__(self):
        self._incr.onAccess()
        return len(self._contents)

    def __getitem__(self, index):
        self._incr.onAccess()
        return self._contents[index]

    def __iter__(self):
        for x in self._contents:
            self._incr.onAccess()
            yield x

    def __contains__(self, x):
        self._incr.onAccess()
        return x in self._contents

    def indexOfById(self, x):
        for i, y in enumerate(self._contents):
            if x is y:
                return i
        return -1

    def __setitem__(self, index, x):
        self._contents[index] = x

    def __delitem__(self, index):
        del self._contents[index]

    def append(self, x):
        self._contents.append(x)

    def insert(self, i, x):
        self._contents.insert(i, x)

    def remove(self, x):
        self._contents.remove(x)

    @property
    def contentsMap(self):
        return self._contentsMapLive.getValue()

    def exportContents(self, myPath):
        for x in self._contents:
            x.export(myPath)

    def __get_trackable_contents__(self):
        return self._contents.__get_trackable_contents__()

    def _registerRoot(self, root, takePriority):
        super(ProjectContainer, self)._registerRoot(root, takePriority)
        for x in self._contents_:
            x._registerRoot(root, takePriority)

    def _unregisterRoot(self, root):
        super(ProjectContainer, self)._unregisterRoot(root)
        for x in self._contents_:
            x._unregisterRoot(root)

    @TrackedListProperty
    def _contents(self):
        return self._contents_

    @_contents.changeNotificationMethod
    def _contents_changed(self):
        prev = set(self._prevContents)
        cur = set(self._contents_)
        added = cur - prev
        removed = prev - cur
        for x in removed:
            x._clearParent()
        for x in added:
            x._setParent(self, False)
        self._prevContents = self._contents_[:]
        self._incr.onChanged()