leocursor.py :  » Development » Leo » Leo-4.7.1-final » leo » plugins » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Leo 
Leo » Leo 4.7.1 final » leo » plugins » leocursor.py
#@+leo-ver=4-thin
#@+node:tbrown.20100205200824.5413:@thin leocursor.py
"""A LeoCursor object can walk around on a Leo outline and decode
attributes from nodes.  Node names can be used through . (dot) notation
so ``cursor.Data.Name._B`` for example returns the body text of the
Name node which is a child of the Data node which is a child of the
cursors current location.

See .../plugins/examples/leocursorexample.leo for application.
"""

import re

#@+others
#@+node:tbrown.20100206093439.5452:class AttribManager
class AttribManager(object):

    """Class responsible for reading / writing attributes from
    vnodes for LeoCursor"""

    class NotPresent(Exception):
        pass

    def filterBody(self, v):
        """Return the body string without any parts used to store
        attributes, if this flavor of attribute manager stores attributes
        in the body.  If not, just return the whole body string."""
        raise NotImplemented

    def getAttrib(self, v, what):
        """Get an attribute value from a vnode"""
        raise NotImplemented
#@-node:tbrown.20100206093439.5452:class AttribManager
#@+node:tbrown.20100206093439.5453:class AM_Colon
class AM_Colon(AttribManager):

    """Attributes are in the body text as::

         start-of-line letter letters-or-numbers colon space(s) attribute-value

    Both::

      foo:

    and::

      foo: all this is the value

    are attributes (first one is value==''), but::

      foo:value

    is not (because there's no space after the colon).

    """

    pattern = re.compile(r"^([A-Za-z][A-Za-z0-9_]*:)(\s+(\S.*))*$")

    def filterBody(self, v):
        return '\n'.join(
           [i for i in v.b.split('\n')
            if not self.pattern.match(i)])

    def getAttrib(self, v, what):

        for i in v.b.split('\n'):

            m = self.pattern.match(i)

            if m and '_'+m.group(1) == what+':':
                m = m.group(2)
                if m:
                    m = m.strip()
                else:
                    m = ''  # don't return None
                return m

        raise self.NotPresent
#@-node:tbrown.20100206093439.5453:class AM_Colon
#@+node:tbrown.20100206093439.5455:class AM_CapColon
class AM_CapColon(AM_Colon):

    """Like AM_Colon, but first letter must be capital."""

    pattern = r"^([A-Z][A-Za-z0-9_]*:)(\s+(\S.*))*$"
#@-node:tbrown.20100206093439.5455:class AM_CapColon
#@+node:tbrown.20100206093439.5451:class LeoCursor
class LeoCursor(object):

    """See module docs."""

    #@    @+others
    #@+node:tbrown.20100205200824.5424:__init__
    def __init__(self, v, other=None):

        if other:
            self.__root = other.__root
            self.__attribManagers = list(other.__attribManagers)
        else:
            self.__root = v
            self.__attribManagers = []

        self.__v = v
    #@-node:tbrown.20100205200824.5424:__init__
    #@+node:tbrown.20100205200824.9978:__iter__
    def __iter__(self):

        for i in self.__v.children:
            yield self.__at(i)
    #@-node:tbrown.20100205200824.9978:__iter__
    #@+node:tbrown.20100206093439.5447:__call__
    def __call__(self, path):

        """'Group .*/June/Event .*' - all the events that descend from June
        that dsecend from a Group"""

        stems = [self]
        steps = path.split('/')

        while steps:

            step = steps.pop(0)
            #print 'S',step
            if not step.strip():
                continue

            try:
                step_int = int(step)
            except ValueError:
                step_int = False

            new_stems = []

            for stem in stems:

                if step_int is not False:

                    new_stems.append(self.__at(
                        (stem.__v.children or [])[step_int]))

                else:

                    for i in stem.__v.children or []:
                        #print 'V', step, i.h
                        if re.match(step, i.h):
                            new_stems.append(self.__at(i))
                            #print 'N',i.h

            stems = new_stems

        return stems
    #@-node:tbrown.20100206093439.5447:__call__
    #@+node:tbrown.20100205200824.5425:__getattr__
    def __getattr__(self, what):

        if what == '_H':

            return self.__v.h

        elif what == '_B':

            return self.__body()

        elif what == '_U':

            return self.__v.u

        elif what == '_GNX':  # or _G?

            return self.__v.gnx

        elif what == '_R':
            return self.__at(self.__root)


        for child in self.__v.children:
            if child.h == what:
                return self.__at(child)

        for i in self.__attribManagers:
            try:
                return i.getAttrib(self.__v, what)
            except AttribManager.NotPresent:
                pass

    #@-node:tbrown.20100205200824.5425:__getattr__
    #@+node:tbrown.20100208110238.12228:__getitem__
    def __getitem__(self, what):
        """what can be a slice object, we let builtin list take care of it"""

        if isinstance(what, int):
            return self.__at(self.__v.children[what])
        else:
            return [self.__at(i) for i in self.__v.children[what]]
    #@-node:tbrown.20100208110238.12228:__getitem__
    #@+node:tbrown.20100206093439.5449:__body
    def __body(self):

        b = self.__v.b

        for i in self.__attribManagers:
            b = i.filterBody(self.__v)

        return b
    #@-node:tbrown.20100206093439.5449:__body
    #@+node:tbrown.20100206093439.5450:__at
    def __at(self, v):

        return LeoCursor(v, self)
    #@-node:tbrown.20100206093439.5450:__at
    #@+node:tbrown.20100206093439.5457:_setAttribManagers
    def _setAttribManagers(self, mngrs):

        self.__attribManagers = list(mngrs)
    #@-node:tbrown.20100206093439.5457:_setAttribManagers
    #@-others
#@nonl
#@-node:tbrown.20100206093439.5451:class LeoCursor
#@-others
#@-node:tbrown.20100205200824.5413:@thin leocursor.py
#@-leo
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.