Changeset 7710 for nappy


Ignore:
Timestamp:
08/11/10 11:42:54 (9 years ago)
Author:
astephen
Message:

Added in right-stripping of any annotations included in curly braces "{...}" on the
end of any input lines in NASA Ames.

Location:
nappy/trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • nappy/trunk/nappy/nappy_api.py

    r7638 r7710  
    140140import nappy.utils.common_utils 
    141141import nappy.utils.compare_na 
     142import nappy.utils.text_parser 
    142143 
    143144# Bring some utils into the API 
  • nappy/trunk/nappy/utils/common_utils.py

    r5369 r7710  
    1616# Imports from local package 
    1717import parse_config 
     18import text_parser as text_parser 
    1819 
    1920logging.basicConfig() 
     
    3637    the File Format Index (FFI) and return it as an integer. 
    3738    """ 
    38     topline = open(filename).readline() 
    39     return int(topline.split()[-1]) 
     39    fin = open(filename) 
     40    topline = fin.readline() 
     41    fin.close() 
     42 
     43    ffi = text_parser.readItemsFromLine(topline, 2, int)[-1] 
     44    return ffi 
    4045 
    4146 
  • nappy/trunk/nappy/utils/text_parser.py

    r3403 r7710  
    1212""" 
    1313 
     14# Standard library imports 
    1415import re 
    1516import string 
    1617 
     18# Local imports 
     19from nappy.utils.right_strip import * 
     20 
     21# Global variables 
    1722pattnNoQuotes = re.compile("^[\"'].*\1$") 
     23 
    1824 
    1925def readItemFromLine(line, rttype=str): 
    2026    """ 
     27    Reads an item of type ``rttype`` from ``line``. 
     28    """ 
     29    line = rightStripCurlyBraces(line) 
     30    rtitem = pattnNoQuotes.sub("", line.strip()) 
    2131 
    22     """ 
    23     rtitem = pattnNoQuotes.sub("", line.strip()) 
    2432    if rttype is not str: 
    2533        rtitem = rttype(rtitem)    
     
    2836def readItemsFromLine(line, nitems=None, rttype=str): 
    2937    """ 
     38    Reads ``nitems`` items of type ``rttype`` from ``line``. 
     39    """ 
     40    line = rightStripCurlyBraces(line) 
     41    rtitems = re.split(r"\s+", line.strip()) 
    3042 
    31     """ 
    32     rtitems = re.split(r"\s+", line.strip()) 
    3343    if nitems and len(rtitems) != nitems: 
    3444        raise "Incorrect number of items (%s) found in line: \n'%s'" % (nitems, line) 
     
    3949def readItemsFromLines(lines, nitems, rttype=str): 
    4050    """ 
    41  
     51    Reads ``nitems`` items of type ``rttype`` from ``lines`` 
    4252    """ 
     53    lines = [rightStripCurlyBraces(line) for line in lines] 
    4354    rtitems = [] 
    4455    for line in lines: 
     
    5970    rtitems = [] 
    6071    lines = []         
     72 
    6173    if type(object) == type([2,3]):  
    6274 
     
    6476            nextitem = object[0] 
    6577            object = object[1:] 
    66             items = nextitem.strip().split() 
     78            items = rightStripCurlyBraces(nextitem).strip().split() 
    6779            lines.append(items) 
    6880            (rtitems, extras) = (rtitems + items[:nitems], items[nitems:]) 
     
    7082    else: 
    7183        while len(rtitems) < nitems: 
    72             items = object.readline().strip().split() 
     84            items = rightStripCurlyBraces(object.readline()).strip().split() 
    7385            lines.append(items) 
    7486            (rtitems, extras) = (rtitems + items[:nitems], items[nitems:]) 
  • nappy/trunk/setup.py

    r7607 r7710  
    1010setup( 
    1111      name='nappy', 
    12       version='0.9.9', 
     12      version='0.9.9c', 
    1313 
    1414      description = 'NASA Ames Processing in Python', 
  • nappy/trunk/tests/test_na_file_1001.py

    r5072 r7710  
    6363 
    6464 
     65    def test_na1001CurlyWithCurlyBraces(self): 
     66        "Tests an input file with curly braces." 
     67        cb_file = os.path.join(data_files, "1001_cb.na") 
     68        fin = nappy.openNAFile(cb_file) 
     69        fin.readData() 
     70        na_dict = fin.getNADict() 
     71        foutname = os.path.join(test_outputs, "test_1001_cb_rewritten.na") 
     72        fobj = nappy.openNAFile(foutname, mode="w", na_dict=na_dict) 
     73        fobj.write() 
     74        self.failUnless(isinstance(fobj, nappy.na_file.na_file.NAFile)) 
     75 
    6576if __name__ ==  "__main__": 
    6677 
Note: See TracChangeset for help on using the changeset viewer.