示例#1
0
def unique_signals(msg):
	signal_table = {}
	for line in msg:
		line = line[:-1]
		if match(event_string, line) != -1:
			#print 'line is "' + line + '"'
			symbol = sub(symbol_regex, symbol_regsub, line)
			date = sub(date_regex, date_regsub, line)
			#print 'symbol, date: "' + symbol + '", "' + date + '"'
			ymd = split(date, "/")
			year = 0; month = 0; day = 0
			year = eval(ymd[2])
			month = eval(sub("^0", "", ymd[0]))
			day = eval(sub("^0", "", ymd[1]))
			signal_date = Date(year, month, day)
			signal = Signal(symbol, signal_date, line)
			if signal_table.has_key(symbol):
				#print 'Adding signal ',; print signal
				signal_table[symbol].append(signal)
			else:
				#print 'Putting signal ',; print signal
				signal_table[symbol] = [signal]
	signal_list = []
	for symbol in signal_table.keys():
		signals = signal_table[symbol]
		last_signal = signals[0]
		for i in range(1, len(signals)):
			if signals[i].date > last_signal.date:
				last_signal = signals[i]
		#print '<<<Appending signal ',; print last_signal,;print '>>>'
		signal_list.append(last_signal)
	return signal_list
示例#2
0
def unique_signals(msg):
    signal_table = {}
    for line in msg:
        line = line[:-1]
        if match(event_string, line) != -1:
            #print 'line is "' + line + '"'
            symbol = sub(symbol_regex, symbol_regsub, line)
            date = sub(date_regex, date_regsub, line)
            #print 'symbol, date: "' + symbol + '", "' + date + '"'
            ymd = split(date, "/")
            year = 0
            month = 0
            day = 0
            year = eval(ymd[2])
            month = eval(sub("^0", "", ymd[0]))
            day = eval(sub("^0", "", ymd[1]))
            signal_date = Date(year, month, day)
            signal = Signal(symbol, signal_date, line)
            if signal_table.has_key(symbol):
                #print 'Adding signal ',; print signal
                signal_table[symbol].append(signal)
            else:
                #print 'Putting signal ',; print signal
                signal_table[symbol] = [signal]
    signal_list = []
    for symbol in signal_table.keys():
        signals = signal_table[symbol]
        last_signal = signals[0]
        for i in range(1, len(signals)):
            if signals[i].date > last_signal.date:
                last_signal = signals[i]
        #print '<<<Appending signal ',; print last_signal,;print '>>>'
        signal_list.append(last_signal)
    return signal_list
示例#3
0
def spew(clearol=0, clearul=0):
    global content, body, ollev, ullev

    if content:
        if entityprog.search(content) > -1:
            content = regsub.gsub('&', '&amp;', content)
            content = regsub.gsub('<', '&lt;', content)
            content = regsub.gsub('>', '&gt;', content)

        n = questionprog.match(content)
        if n > 0:
            content = '<em>' + content[n:] + '</em>'
            if ollev:                       # question reference in index
                fragid = regsub.gsub('^ +|\.? +$', '', secnum)
                content = '<a href="#%s">%s</a>' % (fragid, content)

        if element[0] == 'h':               # heading in the main text
            fragid = regsub.gsub('^ +|\.? +$', '', secnum)
            content = secnum + '<a name="%s">%s</a>' % (fragid, content)

        n = answerprog.match(content)
        if n > 0:                           # answer paragraph
            content = regsub.sub(sentprog, '<strong>\\1</strong>', content[n:])

        body.append('<' + element + '>' + content)
        body.append('</' + element + '>')
        content = ''

    while clearol and ollev: upol()
    if clearul and ullev: body.append('</ul>'); ullev = 0
示例#4
0
def spew(clearol=0, clearul=0):
    global content, body, ollev, ullev

    if content:
        if entityprog.search(content) > -1:
            content = regsub.gsub('&', '&amp;', content)
            content = regsub.gsub('<', '&lt;', content)
            content = regsub.gsub('>', '&gt;', content)

        n = questionprog.match(content)
        if n > 0:
            content = '<em>' + content[n:] + '</em>'
            if ollev:  # question reference in index
                fragid = regsub.gsub('^ +|\.? +$', '', secnum)
                content = '<a href="#%s">%s</a>' % (fragid, content)

        if element[0] == 'h':  # heading in the main text
            fragid = regsub.gsub('^ +|\.? +$', '', secnum)
            content = secnum + '<a name="%s">%s</a>' % (fragid, content)

        n = answerprog.match(content)
        if n > 0:  # answer paragraph
            content = regsub.sub(sentprog, '<strong>\\1</strong>', content[n:])

        body.append('<' + element + '>' + content)
        body.append('</' + element + '>')
        content = ''

    while clearol and ollev:
        upol()
    if clearul and ullev:
        body.append('</ul>')
        ullev = 0
示例#5
0
	def getfileinfo(name):
		finfo = FInfo()
		# Quick check for textfile
		fp = open(name)
		data = open(name).read(256)
		for c in data:
			if not c in string.whitespace and (c<' ' or ord(c) > 0177):
				break
		else:
			finfo.Type = 'TEXT'
		fp.seek(0, 2)
		dsize = fp.tell()
		fp.close()
		dir, file = os.path.split(name)
		file = regsub.sub(':', '-', file)
		return file, finfo, dsize, 0
示例#6
0
 def getfileinfo(name):
     finfo = FInfo()
     # Quick check for textfile
     fp = open(name)
     data = open(name).read(256)
     for c in data:
         if not c in string.whitespace and (c < ' ' or ord(c) > 0177):
             break
     else:
         finfo.Type = 'TEXT'
     fp.seek(0, 2)
     dsize = fp.tell()
     fp.close()
     dir, file = os.path.split(name)
     file = regsub.sub(':', '-', file)
     return file, finfo, dsize, 0
示例#7
0
文件: fixps.py 项目: 8Banana/py1.0
def main():
    for file in sys.argv[1:]:
        try:
            f = open(file, 'r+')
        except IOError:
            print file, ': can\'t open for update'
            continue
        line = f.readline()
        if regex.match('^#! */usr/local/python', line) < 0:
            print file, ': not a /usr/local/python script'
            f.close()
            continue
        rest = f.read()
        line = regsub.sub('/usr/local/python', \
         '/usr/local/bin/python', line)
        print file, ':', ` line `
        f.seek(0)
        f.write(line)
        f.write(rest)
        f.close()
示例#8
0
def processfile(packname, filename, othermacros=[], timeroutines=0):

    # Open the variable description file
    varfile = open(filename, 'r')

    # Read in the whole thing
    # (Make sure that there is a newline character at the end)
    text = varfile.read() + '\n'
    varfile.close()

    # Check if any other files where included in the list
    textother = []
    for a in othermacros:
        f = open(a, 'r')
        t = f.read()
        f.close()
        textother = textother + [t]

    # Get the package name (first line of file) and strip it from the text
    i = re.search('[\n \t#]', text).start()
    line = text[:i]
    text = text[i+1:].strip()

    # Check to make sure that the package name is correct
    if packname != line:
        print("""

    Warning: the package name in the file does not agree
    with the file name. Make sure that the first line of the
    variable description file is the package name.
    file name = %r
    packagename = %r

    One possible reason this error is that the variable description
    file is in dos format. If this is so, change the format to match
    your system. (This can be done in vi by opening the file and
    typing ":set fileformat=unix" and then saving the file.)

    """%(packname, line))
        return []

    # Remove all line continuation marks
    text = re.sub('\\\\', '', text)

    # Get rid of any initial comments and blank lines
    while len(text) > 0 and text[0] ==  '#':
        text = text[re.search('\n', text).start()+1:].strip()

    # Get macros (everthing between curly braces) from other files and
    # prepend to current file (inside curly braces).
    for t in textother:
        m = t[re.search('\n{', t).start()+2:re.search('\n}', t).start()+1]
        if m:
            if text[0] == '{':
                text = '{' + m + text[1:]
            else:
                text = '{' + m + '}\n' + text

    # Deal with statements in between initial curly braces
    # (Only macro statements are used, everything else is ignored.)
    if len(text) > 0 and text[0] == '{':
        while text[0] != '}':
            if 'a' <= text[0] and text[0] <= 'z' or 'A' <= text[0] and text[0] <= 'Z':
                macro = text[0:re.search('=', text).start()].strip()
                text = text[re.search('=', text).start()+1:].strip()
                value = text[0:re.search('[#\n]', text).start()].strip()
                value = repr(eval(value))
                if sys.version[0] == '1':
                    text = regsub.gsub('\<'+macro+'\>', value, text)
                else:
                    text = re.sub('(?<=\W)'+macro+'(?=\W)', value, text)
                text = re.sub('/'+macro+'/', '/'+value+'/', text)
            text = text[re.search('\n', text).start()+1:].strip()
        text = text[re.search('\n', text).start()+1:].strip()

    # Get rid of any comments and blank lines
    while len(text) > 0 and text[0] ==  '#':
        text = text[re.search('\n', text).start()+1:].strip()

    # Parse rest of file, gathering variables

    # Create blank list to put the variables in.
    vlist = []

    # Create blank list to put the 'hidden' variables in.
    hidden_vlist = []

    # Create list of types
    typelist = []

    group = ''
    attributes = ''

    # The parser needs to distinguish between variable names and type names.
    # When readyfortype is 0, the next string found will be interpreted as a
    # variable name. If 1, then it will be a type name.
    readyfortype = 0

    # Parse rest of file
    hidden = 0
    istype = 0
    while text:
        text = text + '\n'

        # Check if group
        if text[0] == '*':
            istype = 0
            # Check the syntax
            g = re.match('\*+ (\w+)((?:[ \t]+\w+)*):', text)
            if g is None:
                g = re.match('(.*)', text)
                raise SyntaxError('Line defining the group is invalid\n%s'%g.group(1))
            # Then get new group name
            i = re.search(':', text).start()
            g = text[:i].split()
            group = g[1]
            # Include group name as an attribute
            attributes = ' '+' '.join(g[1:])+' '
            # Look for the 'hidden' attribute
            hidden = 0
            if re.search(' hidden ', attributes) != None:
                hidden = 1
            # Strip off group name and any comments
            text = text[i+1:].strip() + '\n'
            while text[0] ==  '#':
                text = text[re.search('\n', text).start()+1:].strip() + '\n'
            # Set i so that nothing is stripped off at end of 'if' block
            i = -1
            readyfortype = 0

        # Check if derived type
        elif text[0] == '%':
            istype = 1
            # Then get new type name
            i = re.search(':', text).start()
            tname = text[:i].split()[1]
            group = tname
            # Include group name as an attribute
            attributes = ' '+tname+' '
            # Create new instance of Ftype and append to the list
            ftype = fvars.Ftype(tname, attributes)
            typelist.append(ftype)
            # Strip off group name and any comments
            text = text[i+1:].strip() + '\n'
            while text[0] ==  '#':
                text = text[re.search('\n', text).start()+1:].strip() + '\n'
            # Set i so that nothing is stripped off at end of 'if' block
            i = -1
            readyfortype = 0

        # Check if variable is dynamic
        elif text[0] == '_':
            v.dynamic = 1
            i = 0

        # Check if type is real
        elif re.match('real[\s/\-\+\$[#]', text):
            v.type = 'real'
            i = 3
            readyfortype = 0

        # Check if type is double
        elif re.match('double[\s/\-\+\$[#]', text):
            v.type = 'double'
            i = 5
            readyfortype = 0

        # Check if type is float
        elif re.match('float[\s/\-\+\$[#]', text):
            v.type = 'float'
            i = 4
            readyfortype = 0

        # Check if type is integer
        elif re.match('integer[\s/\-\+\$[#]', text):
            v.type = 'integer'
            i = 6
            readyfortype = 0

        # Check if type is logical
        elif re.match('logical[\s/\-\+\$[#]', text):
            v.type = 'logical'
            i = 6
            readyfortype = 0

        # Check if type is Filedes (temporary fix for now)
        elif re.match('Filedes[\s/\-\+\$[#]', text):
            v.type = 'integer'
            i = 6
            readyfortype = 0

        # Check if type is Filename (assumed to a string of length 256)
        elif re.match('Filename[\s/\-\+\$[#]', text):
            v.type = 'character'
            i = 7
            v.dims = ['256'] + v.dims
            readyfortype = 0

        # Check if type is character
        elif re.match('character[\s\*/\-\+\$[#]', text):
            v.type = 'character'
            v.array = 1
            i = re.search('[ \t\n/\-\+\$[#]', text).start()
            if text[9] == '*':
                v.dims = [text[10:i]] + v.dims
            readyfortype = 0

        # Check if type is complex
        elif re.match('complex[\s/\-\+\$[#]', text):
            v.type = 'complex'
            i = 6
            readyfortype = 0
           #if not v.dims:
           #  v.dims = ['1']

        # Check if variable is a function
        elif re.match('function[\s\$#]', text):
            v.function = 'fsub'
            v.array = 0
            i = 7
            readyfortype = 0
            if timeroutines:
                # --- Add variable used to accumulate the time
                timerv = fvars.Fvars()
                timerv.name = v.name+'runtime'
                timerv.type = 'real'
                timerv.data = '/0./'
                timerv.unit = 'seconds'
                timerv.comment = 'Run time for function %s'%v.name
                timerv.group = group
                timerv.attr = attributes
                vlist.append(timerv)
        # Look for private remark
        # elif re.match('threadprivate', text):
        #     print('text={}\n'.format(text))
        #     v.threadprivate=1
        # Check if variable is a subroutine
        elif re.match('subroutine[\s\$#]', text):
            v.function = 'fsub'
            v.array = 0
            v.type = 'void'
            i = 9
            readyfortype = 0
            if timeroutines:
                # --- Add variable used to accumulate the time
                timerv = fvars.Fvars()
                timerv.name = v.name+'runtime'
                timerv.type = 'real'
                timerv.data = '/0./'
                timerv.unit = 'seconds'
                timerv.comment = 'Run time for subroutine %s'%v.name
                timerv.group = group
                timerv.attr = attributes
                vlist.append(timerv)

        # Check if variable is a C subroutine (takes C ordered arrays)
        elif re.match('csubroutine[\s\$#]', text):
            v.function = 'csub'
            v.array = 0
            v.type = 'void'
            i = 10
            readyfortype = 0
            if timeroutines:
                # --- Add variable used to accumulate the time
                timerv = fvars.Fvars()
                timerv.name = v.name+'runtime'
                timerv.type = 'real'
                timerv.data = '/0./'
                timerv.unit = 'seconds'
                timerv.comment = 'Run time for C subroutine %s'%v.name
                timerv.group = group
                timerv.attr = attributes
                vlist.append(timerv)

        # Check if variable is a parameter, i.e. not writable
        elif re.match('parameter[\s/\-\+\$[#]', text):
            if v.array or v.function:
                raise SyntaxError('%s: only scalar variables can be a parameter'%v.name)
            v.parameter = 1
            i = 8

        # Check if there are any dimensions
        elif text[0] == '(':
            v.array = 1
            i = findmatchingparenthesis(0, text, v.name)
            v.dimstring = text[0:i+1]
            v.dims = v.dims + convertdimstringtodims(v.dimstring)

        # Look for a data field
        elif text[0] == '/':
            i = 1
            # If the data is a string, skip over the string in case the
            # '/' character appears, as in a date for example.
            if   text[i] == '"': i = re.search('"', text[2:]).start() + 2
            elif text[i] == "'": i = re.search("'", text[2:]).start() + 2
            i = re.search('/', text[i:]).start() + i
            data = text[0:i+1]
            # parse expression for size of data block with brackets
            #Example: 'var(1:N-1) /(N-1)*0/
            if data.count('(')==1 and data.count(')')==1:
                 arg=str(eval(data.split('(')[1].split(')')[0]))
                 data=data.split('(')[0]+arg+data.split(')')[1]
            # Handle the old Basis syntax for initial logical values.
            if data[1:-1] == 'FALSE': data = '/.false./'
            if data[1:-1] == 'TRUE': data = '/.true./'
            v.data = data
            readyfortype = 0

        # Look for a unit field
        elif text[0] == '[':
            i = re.search(']', text).start()
            v.unit = text[1:i]
            readyfortype = 0

        # Look for a limited field
        elif re.match('limited[\s\(/\-\+\$[#]', text):
            j = re.search('\(', text).start()
            i = findmatchingparenthesis(j, text, v.name)
            v.limit = text[j:i+1]
            readyfortype = 0

        # Look for attribute to add
        elif text[0] == '+':
            m = attribute_pat.match(text[1:])
            i = m.end() + 1
            if i != -1:
                if m.group(1) =='threadprivate':
                    v.threadprivate = 1
                else:
                    v.attr = v.attr + m.group(1) + ' '
                
            readyfortype = 0

        # Look for attribute to subtract
        elif text[0] == '-':
            m = attribute_pat.match(text[1:])
            if m != None:
                i = m.end() + 1
                if sys.version[0] == '1':
                    v.attr = regsub.sub('\<'+m.group(1)+'\>', ' ', v.attr)
                else:
                    v.attr = re.sub('(?<=\W)'+m.group(1)+'(?=\W)', ' ', v.attr, count=1)
            else:
                i = 0
            readyfortype = 0

        # Look for a set action flag
        elif re.match('SET[\s/\-\+\$[#]', text):
            v.setaction = 1
            i = 2
            readyfortype = 0

        # Look for a get action flag
        elif re.match('GET[\s/\-\+\$[#]', text):
            v.getaction = 1
            i = 2
            readyfortype = 0

        # Look for comment (and remove extra spaces)
        elif text[0] == '#':
            i = re.search('\n', text).start() - 1
            v.comment = v.comment + text[1:i+2].lstrip()
            readyfortype = 0

        # Look for private remark
        elif text[0] == '$':
            i = re.search('\n', text).start() - 1
            readyfortype = 0
        
        # This only leaves a variable name or a new type
        else:
            if not readyfortype:
                # Create new variable
                v = fvars.Fvars()
                if not hidden and not istype:
                    vlist.append(v)
                elif hidden:
                    hidden_vlist.append(v)
                elif istype:
                    ftype.addvar(v)
                i = re.search('[ (\t\n/\-\+\$[#]', text).start() - 1
                v.name = text[:i+1]
                v.group = group
                v.attr = attributes
                readyfortype = 1
            else:
                readyfortype = 0
                i = re.search('[ \t\n/\-\+\$[#]', text).start() - 1
                v.type = text[:i+1]
                v.derivedtype = 1

        # Strip off field which was just parsed
        try:
            text = text[i+1:].strip()
        except IndexError:
            text = ''

    def processvar(v):
        # Use implicit typing if variable type was not set.
        if v.type == '':
            v.type = 'real'
            if 'i' <= v.name[0] and v.name[0] <= 'n':
                v.type = 'integer'

        # Parse the dimensions of arrays
        if v.array:
            assert v.dims, "%s dimensions not set properly"%v.name
            dims = v.dims
            v.dims = []
            for d in dims:
                sd = d.split(':')
                fd = fvars.Fdims()
                if len(sd) == 1:
                    fd.low = '1'
                    fd.high = sd[0]
                else:
                    fd.low = sd[0]
                    fd.high = sd[1]
                # Check for dimensions which are expressions
                if v.dynamic:
                    if fd.high == '':
                        # Flag the dynamic arrays which do not have dimensions
                        # specified. Give it the fassign attribute since it is
                        # assumed that a shapeless variable will be assigned
                        # to in fortran.
                        v.dynamic = 3
                        v.attr = v.attr + ' fassign '
                    elif (re.search(fd.low , '[/\*\+\-]') != None or
                          re.search(fd.high, '[/\*\+\-]') != None):
                        # Flag the dynamic arrays which have an expression for a dimension
                        v.dynamic = 2
                else:
                    # For static arrays, evaluate the dimensions or remove the
                    # expression
                    if re.search(fd.low , '[/\*\+\-]') != None:
                        fd.low = repr(eval(fd.low))
                    if re.search(fd.high, '[/\*\+\-]') != None:
                        fd.high = repr(eval(fd.high))
                v.dims = v.dims + [fd]
            #v.dims.reverse()

        # Set v.args if variable is a function
        if v.function:
            # Remove a possible blank first argument
            if len(v.dims) > 0 and v.dims[0]=='': del v.dims[0]
            # Extract type from arguments, this includes possible dimension
            # descriptions of arrays
            # --- dimvars holds the list of arguments that are used in array
            # --- dimensions
            v.dimvars = []
            for a in v.dims:
                fa = fvars.Fargs()
                v.args = v.args + [fa]
                # --- parse the pattern name(dim1:dim2,...):type
                # --- Only the name must be given, the other parts are optional
                ma = re.search("[:(]|\Z", a)
                fa.name = a[:ma.start()]
                if ma.group() == '(':
                    i = findmatchingparenthesis(ma.start(), a, v.name)
                    fa.dimstring = a[ma.start():i+1]
                    dimlist = fa.dimstring[1:-1].split(',')
                    fa.dims = processargdimvars(dimlist, v.dimvars)
                    a = a[i+1:]
                    ma = re.search("[:]|\Z", a)
                else:
                    fa.dimstring = ''
                    fa.dims = []
                # --- Set the argument type
                if ma.group() == ':':
                    fa.type = a[ma.start()+1:]
                else:
                    # --- Use implicit typing if the type was not specified
                    fa.type = 'real'
                    if 'i' <= fa.name[0] and fa.name[0] <= 'n':
                        fa.type = 'integer'
            # --- Change the list of dimvars from names of the arguments to
            # --- references to the Fargs instance and position number
            i = 0
            for arg in v.args:
                if arg.name in v.dimvars:
                    assert arg.type in ['integer'], "Error in %s:Variables used in array dimensions for argument list must be integer type"%v.name
                    v.dimvars[v.dimvars.index(arg.name)] = [arg, i]
                i += 1
            # --- Make sure that only names that are arguments are used
            # --- This checks for leftover names that wern't replaced by the
            # --- above loop.
            for var in v.dimvars:
                if type(var) == type(''):
                    raise SyntaxError("%s: Only subroutine arguments can be specified as dimensions. Bad argument name is %s"%(v.name, var))
            # --- Empty out dims which is no longer needed (so it won't cause
            # --- confusion later).
            v.dims = []

        # Clean up the comment, removing extra spaces and replace " with '
        if v.comment:
            v.comment = re.sub(' +', ' ', v.comment)
            v.comment = re.sub('"', "'", v.comment)
            v.comment = v.comment.strip()

    # Do further processing on dims or arguments list, and check variable type
    for v in vlist: processvar(v)
    for t in typelist:
        for v in t.vlist: processvar(v)

    # Return the list
    return (vlist, hidden_vlist, typelist)
示例#9
0
			print file, ': too old, skipped'
			continue
		try:
			f = open(file, 'r')
		except IOError, msg:
			print file, ': open failed :', msg
			continue
		head = f.read(HEADSIZE)
		if oldprog.search(head) < 0:
			if newprog.search(head) < 0:
				print file, ': NO COPYRIGHT FOUND'
			else:
				print file, ': (new copyright already there)'
			f.close()
			continue
		newhead = regsub.sub(oldcprt, newcprt, head)
		data = newhead + f.read()
		f.close()
		try:
			f = open(file + '.new', 'w')
		except IOError, msg:
			print file, ': creat failed :', msg
			continue
		f.write(data)
		f.close()
		try:
			os.rename(file, file + '~')
		except IOError, msg:
			print file, ': rename to backup failed :', msg
			continue
		try:
示例#10
0
def processfile(packname, filename, othermacros=[], timeroutines=0):

    # Open the variable description file
    varfile = open(filename, 'r')

    # Read in the whole thing
    # (Make sure that there is a newline character at the end)
    text = varfile.read() + '\n'
    varfile.close()

    # Check if any other files where included in the list
    textother = []
    for a in othermacros:
        f = open(a, 'r')
        t = f.read()
        f.close()
        textother = textother + [t]

    # Get the package name (first line of file) and strip it from the text
    i = re.search('[\n \t#]', text).start()
    line = text[:i]
    text = text[i+1:].strip()

    # Check to make sure that the package name is correct
    if packname != line:
        print """

    Warning: the package name in the file does not agree
    with the file name. Make sure that the first line of the
    variable description file is the package name.
    file name = %r
    packagename = %r

    One possible reason this error is that the variable description
    file is in dos format. If this is so, change the format to match
    your system. (This can be done in vi by opening the file and
    typing ":set fileformat=unix" and then saving the file.)

    """%(packname, line)
        return []

    # Remove all line continuation marks
    text = re.sub('\\\\', '', text)

    # Get rid of any initial comments and blank lines
    while len(text) > 0 and text[0] ==  '#':
        text = text[re.search('\n', text).start()+1:].strip()

    # Get macros (everthing between curly braces) from other files and
    # prepend to current file (inside curly braces).
    for t in textother:
        m = t[re.search('\n{', t).start()+2:re.search('\n}', t).start()+1]
        if m:
            if text[0] == '{':
                text = '{' + m + text[1:]
            else:
                text = '{' + m + '}\n' + text

    # Deal with statements in between initial curly braces
    # (Only macro statements are used, everything else is ignored.)
    if len(text) > 0 and text[0] == '{':
        while text[0] != '}':
            if 'a' <= text[0] and text[0] <= 'z' or 'A' <= text[0] and text[0] <= 'Z':
                macro = text[0:re.search('=', text).start()].strip()
                text = text[re.search('=', text).start()+1:].strip()
                value = text[0:re.search('[#\n]', text).start()].strip()
                value = repr(eval(value))
                if sys.version[0] == '1':
                    text = regsub.gsub('\<'+macro+'\>', value, text)
                else:
                    text = re.sub('(?<=\W)'+macro+'(?=\W)', value, text)
                text = re.sub('/'+macro+'/', '/'+value+'/', text)
            text = text[re.search('\n', text).start()+1:].strip()
        text = text[re.search('\n', text).start()+1:].strip()

    # Get rid of any comments and blank lines
    while len(text) > 0 and text[0] ==  '#':
        text = text[re.search('\n', text).start()+1:].strip()

    # Parse rest of file, gathering variables

    # Create blank list to put the variables in.
    vlist = []

    # Create blank list to put the 'hidden' variables in.
    hidden_vlist = []

    # Create list of types
    typelist = []

    group = ''
    attributes = ''

    # The parser needs to distinguish between variable names and type names.
    # When readyfortype is 0, the next string found will be interpreted as a
    # variable name. If 1, then it will be a type name.
    readyfortype = 0

    # Parse rest of file
    hidden = 0
    istype = 0
    while text:
        text = text + '\n'

        # Check if group
        if text[0] == '*':
            istype = 0
            # Check the syntax
            g = re.match('\*+ (\w+)((?:[ \t]+\w+)*):', text)
            if g is None:
                g = re.match('(.*)', text)
                raise SyntaxError('Line defining the group is invalid\n%s'%g.group(1))
            # Then get new group name
            i = re.search(':', text).start()
            g = text[:i].split()
            group = g[1]
            # Include group name as an attribute
            attributes = ' '+' '.join(g[1:])+' '
            # Look for the 'hidden' attribute
            hidden = 0
            if re.search(' hidden ', attributes) != None:
                hidden = 1
            # Strip off group name and any comments
            text = text[i+1:].strip() + '\n'
            while text[0] ==  '#':
                text = text[re.search('\n', text).start()+1:].strip() + '\n'
            # Set i so that nothing is stripped off at end of 'if' block
            i = -1
            readyfortype = 0

        # Check if derived type
        elif text[0] == '%':
            istype = 1
            # Then get new type name
            i = re.search(':', text).start()
            tname = text[:i].split()[1]
            group = tname
            # Include group name as an attribute
            attributes = ' '+tname+' '
            # Create new instance of Ftype and append to the list
            ftype = fvars.Ftype(tname, attributes)
            typelist.append(ftype)
            # Strip off group name and any comments
            text = text[i+1:].strip() + '\n'
            while text[0] ==  '#':
                text = text[re.search('\n', text).start()+1:].strip() + '\n'
            # Set i so that nothing is stripped off at end of 'if' block
            i = -1
            readyfortype = 0

        # Check if variable is dynamic
        elif text[0] == '_':
            v.dynamic = 1
            i = 0

        # Check if type is real
        elif re.match('real[\s/\-\+\$[#]', text):
            v.type = 'real'
            i = 3
            readyfortype = 0

        # Check if type is double
        elif re.match('double[\s/\-\+\$[#]', text):
            v.type = 'double'
            i = 5
            readyfortype = 0

        # Check if type is float
        elif re.match('float[\s/\-\+\$[#]', text):
            v.type = 'float'
            i = 4
            readyfortype = 0

        # Check if type is integer
        elif re.match('integer[\s/\-\+\$[#]', text):
            v.type = 'integer'
            i = 6
            readyfortype = 0

        # Check if type is logical
        elif re.match('logical[\s/\-\+\$[#]', text):
            v.type = 'logical'
            i = 6
            readyfortype = 0

        # Check if type is Filedes (temporary fix for now)
        elif re.match('Filedes[\s/\-\+\$[#]', text):
            v.type = 'integer'
            i = 6
            readyfortype = 0

        # Check if type is Filename (assumed to a string of length 256)
        elif re.match('Filename[\s/\-\+\$[#]', text):
            v.type = 'character'
            i = 7
            v.dims = ['256'] + v.dims
            readyfortype = 0

        # Check if type is character
        elif re.match('character[\s\*/\-\+\$[#]', text):
            v.type = 'character'
            v.array = 1
            i = re.search('[ \t\n/\-\+\$[#]', text).start()
            if text[9] == '*':
                v.dims = [text[10:i]] + v.dims
            readyfortype = 0

        # Check if type is complex
        elif re.match('complex[\s/\-\+\$[#]', text):
            v.type = 'complex'
            i = 6
            readyfortype = 0
           #if not v.dims:
           #  v.dims = ['1']

        # Check if variable is a function
        elif re.match('function[\s\$#]', text):
            v.function = 'fsub'
            v.array = 0
            i = 7
            readyfortype = 0
            if timeroutines:
                # --- Add variable used to accumulate the time
                timerv = fvars.Fvars()
                timerv.name = v.name+'runtime'
                timerv.type = 'real'
                timerv.data = '/0./'
                timerv.unit = 'seconds'
                timerv.comment = 'Run time for function %s'%v.name
                timerv.group = group
                timerv.attr = attributes
                vlist.append(timerv)

        # Check if variable is a subroutine
        elif re.match('subroutine[\s\$#]', text):
            v.function = 'fsub'
            v.array = 0
            v.type = 'void'
            i = 9
            readyfortype = 0
            if timeroutines:
                # --- Add variable used to accumulate the time
                timerv = fvars.Fvars()
                timerv.name = v.name+'runtime'
                timerv.type = 'real'
                timerv.data = '/0./'
                timerv.unit = 'seconds'
                timerv.comment = 'Run time for subroutine %s'%v.name
                timerv.group = group
                timerv.attr = attributes
                vlist.append(timerv)

        # Check if variable is a C subroutine (takes C ordered arrays)
        elif re.match('csubroutine[\s\$#]', text):
            v.function = 'csub'
            v.array = 0
            v.type = 'void'
            i = 10
            readyfortype = 0
            if timeroutines:
                # --- Add variable used to accumulate the time
                timerv = fvars.Fvars()
                timerv.name = v.name+'runtime'
                timerv.type = 'real'
                timerv.data = '/0./'
                timerv.unit = 'seconds'
                timerv.comment = 'Run time for C subroutine %s'%v.name
                timerv.group = group
                timerv.attr = attributes
                vlist.append(timerv)

        # Check if variable is a parameter, i.e. not writable
        elif re.match('parameter[\s/\-\+\$[#]', text):
            if v.array or v.function:
                raise SyntaxError('%s: only scalar variables can be a parameter'%v.name)
            v.parameter = 1
            i = 8

        # Check if there are any dimensions
        elif text[0] == '(':
            v.array = 1
            i = findmatchingparenthesis(0, text, v.name)
            v.dimstring = text[0:i+1]
            v.dims = v.dims + convertdimstringtodims(v.dimstring)

        # Look for a data field
        elif text[0] == '/':
            i = 1
            # If the data is a string, skip over the string in case the
            # '/' character appears, as in a date for example.
            if   text[i] == '"': i = re.search('"', text[2:]).start() + 2
            elif text[i] == "'": i = re.search("'", text[2:]).start() + 2
            i = re.search('/', text[i:]).start() + i
            data = text[0:i+1]
            # Handle the old Basis syntax for initial logical values.
            if data[1:-1] == 'FALSE': data = '/.false./'
            if data[1:-1] == 'TRUE': data = '/.true./'
            v.data = data
            readyfortype = 0

        # Look for a unit field
        elif text[0] == '[':
            i = re.search(']', text).start()
            v.unit = text[1:i]
            readyfortype = 0

        # Look for a limited field
        elif re.match('limited[\s\(/\-\+\$[#]', text):
            j = re.search('\(', text).start()
            i = findmatchingparenthesis(j, text, v.name)
            v.limit = text[j:i+1]
            readyfortype = 0

        # Look for attribute to add
        elif text[0] == '+':
            m = attribute_pat.match(text[1:])
            i = m.end() + 1
            if i != -1:
                v.attr = v.attr + m.group(1) + ' '
            readyfortype = 0

        # Look for attribute to subtract
        elif text[0] == '-':
            m = attribute_pat.match(text[1:])
            if m != None:
                i = m.end() + 1
                if sys.version[0] == '1':
                    v.attr = regsub.sub('\<'+m.group(1)+'\>', ' ', v.attr)
                else:
                    v.attr = re.sub('(?<=\W)'+m.group(1)+'(?=\W)', ' ', v.attr, count=1)
            else:
                i = 0
            readyfortype = 0

        # Look for a set action flag
        elif re.match('SET[\s/\-\+\$[#]', text):
            v.setaction = 1
            i = 2
            readyfortype = 0

        # Look for a get action flag
        elif re.match('GET[\s/\-\+\$[#]', text):
            v.getaction = 1
            i = 2
            readyfortype = 0

        # Look for comment (and remove extra spaces)
        elif text[0] == '#':
            i = re.search('\n', text).start() - 1
            v.comment = v.comment + text[1:i+2].lstrip()
            readyfortype = 0

        # Look for private remark
        elif text[0] == '$':
            i = re.search('\n', text).start() - 1
            readyfortype = 0

        # This only leaves a variable name or a new type
        else:
            if not readyfortype:
                # Create new variable
                v = fvars.Fvars()
                if not hidden and not istype:
                    vlist.append(v)
                elif hidden:
                    hidden_vlist.append(v)
                elif istype:
                    ftype.addvar(v)
                i = re.search('[ (\t\n/\-\+\$[#]', text).start() - 1
                v.name = text[:i+1]
                v.group = group
                v.attr = attributes
                readyfortype = 1
            else:
                readyfortype = 0
                i = re.search('[ \t\n/\-\+\$[#]', text).start() - 1
                v.type = text[:i+1]
                v.derivedtype = 1

        # Strip off field which was just parsed
        try:
            text = text[i+1:].strip()
        except IndexError:
            text = ''

    def processvar(v):
        # Use implicit typing if variable type was not set.
        if v.type == '':
            v.type = 'real'
            if 'i' <= v.name[0] and v.name[0] <= 'n':
                v.type = 'integer'

        # Parse the dimensions of arrays
        if v.array:
            assert v.dims, "%s dimensions not set properly"%v.name
            dims = v.dims
            v.dims = []
            for d in dims:
                sd = d.split(':')
                fd = fvars.Fdims()
                if len(sd) == 1:
                    fd.low = '1'
                    fd.high = sd[0]
                else:
                    fd.low = sd[0]
                    fd.high = sd[1]
                # Check for dimensions which are expressions
                if v.dynamic:
                    if fd.high == '':
                        # Flag the dynamic arrays which do not have dimensions
                        # specified. Give it the fassign attribute since it is
                        # assumed that a shapeless variable will be assigned
                        # to in fortran.
                        v.dynamic = 3
                        v.attr = v.attr + ' fassign '
                    elif (re.search(fd.low , '[/\*\+\-]') != None or
                          re.search(fd.high, '[/\*\+\-]') != None):
                        # Flag the dynamic arrays which have an expression for a dimension
                        v.dynamic = 2
                else:
                    # For static arrays, evaluate the dimensions or remove the
                    # expression
                    if re.search(fd.low , '[/\*\+\-]') != None:
                        fd.low = repr(eval(fd.low))
                    if re.search(fd.high, '[/\*\+\-]') != None:
                        fd.high = repr(eval(fd.high))
                v.dims = v.dims + [fd]
            #v.dims.reverse()

        # Set v.args if variable is a function
        if v.function:
            # Remove a possible blank first argument
            if len(v.dims) > 0 and v.dims[0]=='': del v.dims[0]
            # Extract type from arguments, this includes possible dimension
            # descriptions of arrays
            # --- dimvars holds the list of arguments that are used in array
            # --- dimensions
            v.dimvars = []
            for a in v.dims:
                fa = fvars.Fargs()
                v.args = v.args + [fa]
                # --- parse the pattern name(dim1:dim2,...):type
                # --- Only the name must be given, the other parts are optional
                ma = re.search("[:(]|\Z", a)
                fa.name = a[:ma.start()]
                if ma.group() == '(':
                    i = findmatchingparenthesis(ma.start(), a, v.name)
                    fa.dimstring = a[ma.start():i+1]
                    dimlist = fa.dimstring[1:-1].split(',')
                    fa.dims = processargdimvars(dimlist, v.dimvars)
                    a = a[i+1:]
                    ma = re.search("[:]|\Z", a)
                else:
                    fa.dimstring = ''
                    fa.dims = []
                # --- Set the argument type
                if ma.group() == ':':
                    fa.type = a[ma.start()+1:]
                else:
                    # --- Use implicit typing if the type was not specified
                    fa.type = 'real'
                    if 'i' <= fa.name[0] and fa.name[0] <= 'n':
                        fa.type = 'integer'
            # --- Change the list of dimvars from names of the arguments to
            # --- references to the Fargs instance and position number
            i = 0
            for arg in v.args:
                if arg.name in v.dimvars:
                    assert arg.type in ['integer'], "Error in %s:Variables used in array dimensions for argument list must be integer type"%v.name
                    v.dimvars[v.dimvars.index(arg.name)] = [arg, i]
                i += 1
            # --- Make sure that only names that are arguments are used
            # --- This checks for leftover names that wern't replaced by the
            # --- above loop.
            for var in v.dimvars:
                if type(var) == type(''):
                    raise SyntaxError("%s: Only subroutine arguments can be specified as dimensions. Bad argument name is %s"%(v.name, var))
            # --- Empty out dims which is no longer needed (so it won't cause
            # --- confusion later).
            v.dims = []

        # Clean up the comment, removing extra spaces and replace " with '
        if v.comment:
            v.comment = re.sub(' +', ' ', v.comment)
            v.comment = re.sub('"', "'", v.comment)
            v.comment = v.comment.strip()

    # Do further processing on dims or arguments list, and check variable type
    for v in vlist: processvar(v)
    for t in typelist:
        for v in t.vlist: processvar(v)

    # Return the list
    return (vlist, hidden_vlist, typelist)
示例#11
0
#  Open the input and output files.
#
inFile = open(inputfile, 'r')
outFile = open(outputfile, 'w')

#
#  Convert each line in the input file to 1 or more lines in the
#  output file.
#
for line in inFile.readlines():
    #
    #  Replace the whitespace with a pipe character between the cluster ID
    #  and the list of cluster member IDs.  Ignore any line that does not
    #  contain the proper whitespace separating the fields.
    #
    line = regsub.sub('[ 	]+', '|', line)
    pipe = regex.search('|', line)
    if (pipe < 0):
        continue

    #
    #  Ignore any line that does not contain a cluster ID.
    #
    if (line[0] == '|'):
        continue

    #
    #  Locate and save the cluster ID.
    #
    cid = line[:pipe]
示例#12
0
文件: bisonparse.py 项目: spchamp/ilu
curstate=None
for l in open(sys.argv[1],"rt").readlines():
    if l[:6]=="state ":
	curstate=string.atoi(l[6:])
	states[curstate]=[]
	continue
    if curstate is None:
	# we have not seen state 0, yet
	continue
    if string.find(l,"->")==-1:
	# inside a state, but this line is not a rule
	continue
    # Now we have a rule. Split off the rule
    l=string.strip(string.split(l,"(rule")[0])
    # the rule might contain funny characters. Not all are supported, here.
    l=regsub.sub('"','\\"',l)
    states[curstate].append(l)

out=open(base+"-output.c","w")
out.write("char *error_message[]={\n")

for i in xrange(0,max(states.keys())+1):
    out.write('"')
    if states.has_key(i):
	out.write(string.join(states[i],"\\n"))
    out.write('",\n');

out.write("0};\n");
out.close()
    
示例#13
0
import regsub

text = "Well, there's spam, egg, sausage and spam."

print regsub.sub("spam", "ham", text) # just the first
print regsub.gsub("spam", "bacon", text) # all of them

## Well, there's ham, egg, sausage and spam.
## Well, there's bacon, egg, sausage and bacon.
示例#14
0
import regsub

text = "Well, there's spam, egg, sausage and spam."

print(regsub.sub("spam", "ham", text)) # just the first
print(regsub.gsub("spam", "bacon", text)) # all of them

## Well, there's ham, egg, sausage and spam.
## Well, there's bacon, egg, sausage and bacon.
示例#15
0
def main():
    print 'Reading lines...'
    lines = open(FAQ, 'r').readlines()
    print 'Renumbering in memory...'
    oldlines = lines[:]
    after_blank = 1
    chapter = 0
    question = 0
    chapters = ['<OL>']
    questions = ['<OL>']
    for i in range(len(lines)):
        line = lines[i]
        if after_blank:
            n = chapterprog.match(line)
            if n >= 0:
                chapter = chapter + 1
                if chapter != 1:
                    questions.append('</UL>\n')
                question = 0
                lines[i] = '<H2>' + line[n:-1] + '</H2>\n'
                chapters.append('<LI> ' + line[n:])
                questions.append('<LI> ' + line[n:])
                questions.append('<UL>\n')
                afterblank = 0
                continue
            n = underlineprog.match(line)
            if n >= 0:
                lines[i] = ''
                continue
            n = questionprog.match(line)
            if n < 0: n = newquestionprog.match(line) - 3
            if n >= 0:
                question = question + 1
                number = '%d.%d' % (chapter, question)
                lines[i] = '<A NAME="' + number + '"><H3>' + line[n:]
                questions.append('<LI><A HREF="#' + \
                   number + '">' + line[n:])
                # Add up to 4 continuations of the question
                n = len(number)
                for j in range(i + 1, i + 5):
                    if blankprog.match(lines[j]) >= 0:
                        lines[j - 1] = lines[j - 1] + '</H3></A>'
                        questions[-1] = \
                              questions[-1][:-1] + '</A>\n'
                        break
                    questions.append(' ' * (n + 2) + lines[j])
                afterblank = 0
                continue
        afterblank = (blankprog.match(line) >= 0)
    print 'Inserting list of chapters...'
    chapters.append('</OL>\n')
    for i in range(len(lines)):
        line = lines[i]
        if regex.match('^This FAQ is divided in the following chapters',
                       line) >= 0:
            i = i + 1
            while 1:
                line = lines[i]
                if indentedorblankprog.match(line) < 0:
                    break
                del lines[i]
            lines[i:i] = chapters
            break
    else:
        print '*** Can\'t find header for list of chapters'
        print '*** Chapters found:'
        for line in chapters:
            print line,
    print 'Inserting list of questions...'
    questions.append('</UL></OL>\n')
    for i in range(len(lines)):
        line = lines[i]
        if regex.match('^Here.s an overview of the questions', line) >= 0:
            i = i + 1
            while 1:
                line = lines[i]
                if indentedorblankprog.match(line) < 0:
                    break
                del lines[i]
            lines[i:i] = questions
            break
    else:
        print '*** Can\'t find header for list of questions'
        print '*** Questions found:'
        for line in questions:
            print line,
    # final cleanup
    print "Final cleanup..."
    doingpre = 0
    for i in range(len(lines)):
        # set lines indented by >= 8 spaces using PRE
        # blank lines either terminate PRE or separate paragraphs
        n = eightblanksprog.match(lines[i])
        if n < 0: n = mailheaderprog.match(lines[i])
        if n >= 0:
            if versionprog.match(lines[i]) > 0:
                version = string.split(lines[i])[1]
            if doingpre == 0:
                lines[i] = '<PRE>\n' + lines[i]
                doingpre = 1
                continue
        n = blankprog.match(lines[i])
        if n >= 0:
            # print '*** ', lines[i-1], doingpre
            if doingpre == 1:
                lines[i] = '</PRE><P>\n'
                doingpre = 0
            else:
                lines[i] = '<P>\n'
            continue

        # & -> &amp;
        n = ampprog.search(lines[i])
        if n >= 0:
            lines[i] = regsub.gsub(ampprog, '&amp;', lines[i])
            # no continue - there might be other changes to the line...

        # zap all the 'Q.' and 'A.' leaders - what happened to the
        # last couple?
        n = qprog.search(lines[i])
        if n >= 0:
            lines[i] = regsub.sub(qprog, '>', lines[i])
            # no continue - there might be other changes to the line...

        n = aprog.search(lines[i])
        if n >= 0:
            lines[i] = regsub.sub(aprog, '', lines[i])
            # no continue - there might be other changes to the line...

        # patch up hard refs to questions
        n = qrefprog.search(lines[i])
        if n >= 0:
            lines[i] = regsub.sub(qrefprog, '<A HREF="#\\1">question \\1</A>',
                                  lines[i])
            # no continue - there might be other changes to the line...

        # make <URL:...> into actual links
        n = urlprog.search(lines[i])
        if n >= 0:
            lines[i] = regsub.gsub(urlprog, '<A HREF="\\1">\\1</A>', lines[i])
            # no continue - there might be other changes to the line...

        # make <*****@*****.**> into <mailto:...> links
        n = emailprog.search(lines[i])
        if n >= 0:
            lines[i] = regsub.gsub(emailprog, '<A HREF="mailto:\\1">\\1</A>',
                                   lines[i])
            # no continue - there might be other changes to the line...

    lines[0:0] = [
        '<HTML><HEAD><TITLE>Python Frequently Asked Questions v', version,
        '</TITLE>\n', '</HEAD><body>\n', '(This file was generated using\n',
        '<A HREF="faq2html.py">faq2html.py</A>.)<P>\n'
    ]
    lines.append('<P></BODY></HTML>\n')

    print 'Writing html file...'
    f = open(FAQ + '.html', 'w')
    for line in lines:
        f.write(line)
    f.close()
    print 'Done.'