Exemple #1
0
def changeText(filename, text, newtext=""):
    ''' *Change* (or *remove*) text in this file.
        -- called by changeFiles '''
    filetext = fileText(filename)
    if text in filetext:
        filetext = filetext.replace(text, newtext)
        f = file(filename, 'w')
        f.write(filetext)
        f.close()
        print "file: %s" % filename
Exemple #2
0
def changeText(filename, text, newtext=""):
    """ *Change* (or *remove*) text in this file.
        -- called by changeFiles """
    filetext = fileText(filename)
    if text in filetext:
        filetext = filetext.replace(text, newtext)
        f = open(filename, "w")
        f.write(filetext)
        f.close()
        print("file: %s" % filename)
Exemple #3
0
def changeText(filename, text, newtext=""):
    ''' *Change* (or *remove*) text in this file.
        -- called by changeFiles '''
    filetext = fileText(filename)
    if text in filetext:
        filetext = filetext.replace(text, newtext)
        f = file(filename, 'w')
        f.write(filetext)
        f.close()
        print "file: %s" % filename
Exemple #4
0
def insertNotices(notice_filename, linecount=2, dir=None, ext="py"):
    # params
    if dir is None:
        dir = os.getcwd()
    try:
        notice = fileText(notice_filename)
        notice_start = "\n".join(notice.splitlines()[:7])
    except IOError:
        notice = notice_start = None
    # now, run?
    print("\ndir: %s\next: %s\nlinecount: %s\nfirst text lines:\n%s\n" % (dir, ext, linecount, notice_start))
    answer = raw_input(">>> write notice in files? (y/n)")
    if not (answer.lower() == "y"):
        end()
    # yes, run!
    print("=== file walk ===")
    print(linecount, dir)
    filenames = fileNames(dir, ext)
    print("\n=== writing notice ===")
    for filename in filenames:
        insertNotice(filename, notice, linecount)
Exemple #5
0
def insertNotices(notice_filename, linecount=2, dir=None, ext="py"):
    # params
    if dir is None:
        dir = os.getcwd()
    try:
        notice = fileText(notice_filename)
        notice_start = '\n'.join(notice.splitlines()[:7])
    except IOError:
        notice = notice_start = None
    # now, run?
    print ("\ndir: %s\next: %s\nlinecount: %s\nfirst text lines:\n%s\n"
            % (dir, ext, linecount, notice_start))
    answer = raw_input(">>> write notice in files? (y/n)")
    if not (answer.lower() == 'y'):
        end()
    # yes, run!
    print "=== file walk ==="
    print linecount, dir
    filenames = fileNames(dir, ext)
    print "\n=== writing notice ==="
    for filename in filenames:
        insertNotice(filename, notice, linecount)
Exemple #6
0
def makeParser(grammarText, feedback=False, outputPath=""):
    ''' Write parser code file. '''
    ''' example:
        from pijnu import makeParser, fileText
        parser = makeParser(fileText("foo.pijnu")) '''
    ### parse & process grammar
    #   (using meta-parser 'pijnuParser')
    print "... parsing grammar ..."
    tree = pijnuParser.parse(grammarText)

    ### compose parser code:
    #   ~ copy of grammar original definition inside """..."""
    #   ~ "from pijnu.library import *"
    #   ~ parser object creation
    #   ~ grammar code itself
    #   ~ parser object specification
    print "... composing code ..."
    # major code sections
    grammarTitle = tree.title
    definition = tree.definition
    # parser object definition
    parserName = "%sParser" % grammarTitle
    topPatternName = tree.topPatternName
    filename = "%s.py" % parserName

    code = ('''%(definitionCopy)s
from pijnu.library import *


def make_parser(actions=None):
    """Return a parser.

    The parser's toolset functions are (optionally) augmented (or overridden)
    by a map of additional ones passed in.

    """
    if actions is None:
        actions = {}

    # Start off with the imported pijnu library functions:
    toolset = globals().copy()

    parser = Parser()
    state = parser.state

%(grammarCode)s

    symbols = locals().copy()
    symbols.update(actions)
    parser._recordPatterns(symbols)
    parser._setTopPattern("%(topPatternName)s")
    parser.grammarTitle = "%(grammarTitle)s"
    parser.filename = "%(filename)s"

    return parser\n''' %
    dict(definitionCopy='""" %s\n%s\n"""\n' % (grammarTitle, tree.definition),
         parserName=parserName,
         topPatternName=topPatternName,
         grammarTitle=grammarTitle,
         filename=filename,
         grammarCode='\n    '.join(tree.value.splitlines())))

    ### write parser module --possible feedback on stdout
    print "... writing file ...\n"
    filepath = os.path.join(outputPath, filename)
    writeFile(filepath, code)
    if feedback:
        print fileText(filepath)

    ### return parser object --if ever needed
    try:
        if outputPath:
            sys.path.insert(0, outputPath)
        modulename = filename[:-3]
        module = __import__(modulename)
    finally:
        sys.path.pop(0)
    return module.make_parser
def makeParser(grammarText, feedback=False):
    ''' Write parser code file. '''
    ''' example:
        from pijnu import makeParser, fileText
        parser = makeParser(fileText("foo.pijnu")) '''
    ### parse & process grammar
    #   (using meta-parser 'pijnuParser')
    #print "... parsing grammar ..."
    tree = pijnuParser.parse(grammarText)

    ### compose parser code:
    #   ~ copy of grammar original definition inside """..."""
    #   ~ "from pijnu.library import *"
    #   ~ parser object creation
    #   ~ grammar code itself
    #   ~ parser object specification
    #print "... composing code ..."
    # major code sections
    grammarTitle = tree.title
    definition = tree.definition
    # parser object definition
    parserName = "%sParser" % grammarTitle
    topPatternName = tree.topPatternName
    filename = "%s.py" % parserName

    code = ('''%(definitionCopy)s
from pijnu.library import *


def make_parser(actions=None):
    """Return a parser.

    The parser's toolset functions are (optionally) augmented (or overridden)
    by a map of additional ones passed in.

    """
    if actions is None:
        actions = {}

    # Start off with the imported pijnu library functions:
    toolset = globals().copy()

    parser = Parser()
    state = parser.state

%(grammarCode)s

    symbols = locals().copy()
    symbols.update(actions)
    parser._recordPatterns(symbols)
    parser._setTopPattern("%(topPatternName)s")
    parser.grammarTitle = "%(grammarTitle)s"
    parser.filename = "%(filename)s"

    return parser\n''' %
            dict(definitionCopy='""" %s\n%s\n"""\n' %
                 (grammarTitle, tree.definition),
                 parserName=parserName,
                 topPatternName=topPatternName,
                 grammarTitle=grammarTitle,
                 filename=filename,
                 grammarCode='\n    '.join(tree.value.splitlines())))

    ### write parser module --possible feedback on stdout
    #print "... writing file ...\n"
    writeFile(filename, code)
    if feedback:
        print fileText(filename)

    ### return parser object --if ever needed
    modulename = filename[:-3]
    module = __import__(modulename)
    return module.make_parser