From: Greg Ercolano <erco@(email surpressed)>
Subject: Re: line of rush.submit too long!
   Date: Tue, 10 Jul 2012 11:48:22 -0400
Msg# 2254
View Complete Thread (23 articles) | All Threads
Last Next
On 07/10/12 08:07, Kevin Sallee wrote:
> Hey greg thanks for all the useful info!
> I think I will write an ASCII file also I prefer it that way too.

	If you prefer the Key: Value format, here's a couple of routines
	I use for that. I use 16 as padding to keep my key names lined up.

	You end up with data files that look like:

--- snip
          Frames: 1-50
 LicenseBehavior: Fail
        LogFlags: Overwrite
      MaxLogSize: 0
         MaxTime: 00:00:00
    MaxTimeState: Que
      OutputPath: //meade/net/tmp/images/SpiroBlue.[####].png
PrintEnvironment: off
    QTGeneration: yes
            Cpus: tahoe erie superior
            Cpus: spirit waccabuc crystal
            Cpus: sepulveda canyon huron
--- snip

	Here's a snippet of python code I use for load/saving a
	one dimensional dict in the above format.

--- snip

import os,sys,re

def ParseKeyValue(s, fields, stripflag=1):
    '''HANDLE PARSING KEY/VALUE PAIRS FROM A STRING OF TEXT
       Ignores blank or commented out lines.
       Input:
           s         -- string from file or eg. 'rush -ljf' output
           fields    -- dict being modified/updated with parsed info
           stripflag -- [optional] strip()s the string before parsing
                                   to remove trailing white space
       Returns:
           1 -- if data was parsed, fields[] contains new data
           0 -- if line was blank or empty
          -1 -- data was present, but not in Key:Value format
    '''
    # Handle input line stripping
    if stripflag:
        s = s.strip()
    else:
        # Remove only leading white and trailing crlfs
        #     (Carefully avoid removing trailing white)
        #
        s = s.lstrip()              # "  Key: val\n" -> "Key: val\n"
        s = re.sub("[\r\n]*$","",s) # "Key: val \n" -> "Key: val "
    # Empty or comment line?
    if ( s == "" or s[0] == '#' ):
        return 0
    # Try to parse the line
    try:
        # parse "Key: Value" pairs
        (key,val) = re.search("[\s]*([^:]*):[\s]*(.*)",s).groups()
    except:
        return -1

    # Parsed OK, update fields[]
    if ( fields.has_key(key)):
        fields[key] = fields[key] + "\n" + val
    else:
        fields[key] = val
    return 1

def LoadFields(filename, fields, stripflag=1):
    '''LOAD KEY/VALUE PAIRS FROM FILE
           filename  -- the file containing the "Key: Value" pairs
           fields[]  -- returns a dictionary of key value pairs as:
                        fields[<Key>] = <Value>
           stripflag -- optional flag indicates if values parsed
                        should have leading/trailing whitespace removed
                        (default on)
       Returns:
           Success: returns fields[] containing the loaded key/value pairs
           Failure: raises RuntimeError with error message.
    '''
    try:
        fp = open(filename,"r")
    except IOError,e:
        raise RuntimeError("could not open '%s': %s" % (filename,e.strerror))
    for s in fp:
        ParseKeyValue(s, fields, stripflag)
    fp.close()
    return None

def WriteFields(fd, fields):
    '''WRITE KEY/VALUE PAIRS TO AN ALREADY OPEN FILE
           fd       -- the file descriptor of a file already open for write
           fields[] -- the dictionary of key value pairs to write: fields[<Key>] = <Value>
    Returns:
        Success: returns with fields[] written to fd
        Failure: raises RuntimeError with error message.
    '''
    keys = fields.keys()
    keys.sort()
    for key in keys:
        val = str(fields[key])
        if ( val.find("\n") ):
            for line in val.split("\n"):
                try:
                    print >> fd, "%16s: %s" % (key,line)
                except IOError,e:
                    raise "write error: %s" % e
        else:
            try:
                print >> fd, "%16s: %s" % (key,val)
            except IOError,e:
                raise "write error: %s" % e

def SaveFields(filename, fields):
    '''SAVE FIELDS TO FILE
           filename -- file to write the fields[] to as "Key: Value" pairs
           fields[] -- the array (dictionary) of fields[] to be saved.
                       (Fields will be saved sorted by their key name)
       Returns:
          Success: returns fields[] containing the loaded key/value pairs
          Failure: raises RuntimeError with error message.
    '''
    try:
        fp = open(filename,"w")
    except IOError,e:
        raise RuntimeError("%s: can't open for write: %s" % (filename,e.strerror))
    WriteFields(fp, fields)
    fp.close()

--- snip

	Here's an example showing how to save a file using the above:

fields = {}
fields["Aaa"] = "a a a"			# single line of data
fields["Bbb"] = "b b b\nbb bb bb"	# multi-line data
SaveFields("/tmp/foo.dat", fields)

	..and here's how to load a file using the above routines:

fields = {}
LoadFields("/tmp/foo.dat",fields)

	..and to print the loaded data:

for i in fields:
    print "FIELD[" + i + "]: '" + fields[i] + "'"

-- 
Greg Ercolano, erco@(email surpressed)
Seriss Corporation
Rush Render Queue, http://seriss.com/rush/
Tel: (Tel# suppressed)ext.23
Fax: (Tel# suppressed)
Cel: (Tel# suppressed)


Last Next