Submitted by : ktenney at: 2008-12-04T07:57:31-08:00 (10 years ago)
Name :
Category : Severity : Status :
Optional subject :  
Optional comment :

import os

def is_at_path(node):
    """Determine if this node declares a @path element.

    I'm not sure of a couple assumptions regarding legal @path statements

    - they must begin the first line of node body or
    - they must begin the headline

    This code also allows @path declarations in headline and body
    if they are the same
    """
    head = node.headString()
    body = node.bodyString().split('\n')[0]
    path_in_body = ''
    path_in_head = ''
    if head.startswith('@path'):
        path_in_head = head.split()[1]
    if body.startswith('@path'):
        path_in_body = body.split()[1]
    if path_in_head is not '':
        # @path in headline
        if path_in_body is '':
            return path_in_head
        else:
            # allow same @path declaration in headline and body
            if path_in_head == path_in_body:
                return path_in_head
            else:
                # XXX should raise exception?
                g.es('mismatched @path in body and headline of %s' % head)
                return ''
    if path_in_body is not '':
        # @path declaration in body
        return path_in_body
    return ''

def ascend(start_node):
    """A generator which climbs the node tree, returns file name
    and path elements.
    """
    pathlist = []
    for node in start_node.self_and_parents_iter():
        fname = node.anyAtFileNodeName()
        if fname != '':
            yield fname
        at_path = is_at_path(node)
        if at_path != '':
            yield at_path

def get_ext_fname():
    pathlist = []
    fname = ''
    for path in ascend(p):
        pathlist.insert(0, path)
    if len(pathlist) > 0: # in a file system tree
        fname = pathlist.pop(0)
    else: # not in a fs tree
        # return os.path.dirname(c.fileName())
        return ''

    while len(pathlist) > 0:
        fname = os.path.join(fname, pathlist.pop(0))

    if not os.path.isabs(fname):
        leo_file_dir = os.path.dirname(c.fileName())
        fname = os.path.join(leo_file_dir, fname)

    return fname

g.es(get_ext_fname())

... --edreamleo, Thu, 05 Mar 2009 10:12:58 -0800 reply

Status: open => closed