Next / Previous / Contents / Shipman's homepage

88.2. main()

Here is the main procedure of the script.

dbfstruct
# - - -   m a i n   - - -

def main():
    '''Display the structure of a .dbf file.
    '''

The command line should contain exactly one argument, the name of the .dbf file to be displayed.

dbfstruct
    #-- 1 --
    # [ if sys.argv contains exactly one argument ->
    #      fileName    :=   that argument
    #   else ->
    #      sys.stderr  +:=  error message
    #      stop execution ]
    argList = sys.argv[1:]
    if  len(argList) != 1:
        print >>sys.stderr, "*** One argument, the .dbf file name."
        sys.exit(1)
    else:
        fileName = argList[0]

Next we attempt to open the input file. Any sort of exception here is fatal. See Section 90.1, “Dbf: Database object”.

dbfstruct
    #-- 2 --
    # [ if fileName names a readable, valid .dbf file ->
    #     header  :=  a dbf.DbfHeader object representing the
    #                 header data from that file
    #   else ->
    #     sys.stderr  +:=  error message
    #     stop execution ]
    try:
        table = Dbf(fileName)
        header = table.header
    except Exception, detail:
        print >>sys.stderr,("*** Couldn't open the input file: %s" %
            detail)
        sys.exit(2)

First we write the part of the report that gives general file information. Then we write the report on the field definitions. See Section 88.3, “headerReport(): General information” and Section 88.4, “fieldReport(): Report on field definitions”.

dbfstruct
    #-- 3 --
    # [ sys.stdout  +:=  report on fileName and header ]
    headerReport(fileName, header)

    #-- 4 --
    # [ sys.stdout  +:=  report of field definitions from header ]
    fieldReport(header)