Next / Previous / Contents / Shipman's homepage

22.4. flattest: Flatten the entire database

This script uses the BirdNoteTree class to read a complete collection of files and output it as a flat file delimited by the “|” character.

flattest
#!/usr/bin/env python
#================================================================
# flattest:  Exercise class BirdNoteTree and FlatSighting
#----------------------------------------------------------------

# - - - - -   I m p o r t s

from __future__ import print_function
import sys
import datetime
import xnomo3
import birdnotes

# - - - - -   M a n i f e s t   c o n s t a n t s

# [ DEFAULT_ROOT  :=  default input root directory ]
DEFAULT_ROOT = "/u/shipman/www/aba/"

# - - -   m a i n

def main():
    """
    """
    root = checkArgs()
    txny = xnomo3.Txny()
    tree = birdnotes.BirdNoteTree(txny, root)
    for monthSet in tree.genMonths():
        report(monthSet)

# - - -   c h e c k A r g s

def checkArgs():
    '''Find the root given (or not) on the command line
    '''
    script, argList = sys.argv[0], sys.argv[1:]
    if len(argList) == 0:
        root = DEFAULT_ROOT
    elif len(argList) == 1:
        root = argList[0]
    else:
        sys.stderr.write("*** Usage: {0} [ROOT_DIR]".format(script))
        raise SystemExit
    
    return root

# - - -   r e p o r t

def report(monthSet):
    '''Show one monthSet
    '''
    for dayNotes in monthSet.genDays():
        for birdForm in dayNotes.genForms():
            for sighting in birdForm.genSightings():
                flat = birdnotes.FlatSighting(sighting)
                print(flat.delimited('|'))

# - - - - -   E p i l o g u e

if __name__ == "__main__":
    main()