Next / Previous / Contents / Shipman's homepage

8. main(): Main program

nomcompile3
# - - -   m a i n

def main():
    '''Main program for nomcompile3.

      [ if (command line arguments are valid) and
        (input-files(command line) are readable and valid against
        each other) and
        (output-files(command line) can be created new) ->
          output-files(command line)  :=  representations of
            input-files(command line)
          sys.stderr  +:=  status message(s)
        else ->
          sys.stderr  +:=  status message(s) + error message(s)
          output-files(command line)  :=  (anything) ]
    '''

First open the log file and write the version banner. Then check the command line arguments. The constructor does not return unless they are valid; see Section 14, “class Args: Process command line arguments”.

nomcompile3
    #-- 1 --
    # [ Log()  :=  version message ]
    Log().addLogFile ( LOG_FILE )
    Log().write ( "=== %s %s ===" % (PROGRAM_NAME, EXTERNAL_VERSION ) )

Because we use the optparse module to process the command line arguments, error messages about the command line go directly to sys.stderr, not through the Log() singleton.

nomcompile3
    #-- 2 --
    # [ if the command line arguments are valid for this
    #   program ->
    #     Args()  :=  a singleton Args instance that represents
    #                 those arguments
    #   else ->
    #     sys.stderr  +:=  (usage message) + (error message)
    #     stop execution ]
    Args()
    

Read the three input files: the ranks file, the standard forms file, and the alternate forms file. After each of these files is read, the program checks for errors and terminates if there have been any.

nomcompile3
    #-- 3 --
    # [ if eff-ranks(Args()) names a readable, valid ranks
    #   file ->
    #     hier  :=  a Hier instance representing that file
    #   else ->
    #     Log()  +:=  error message(s)
    #     stop execution ]
    try:
        hier = Hier ( Args().ranksFileName )
    except IOError, detail:
        Log().fatal ( "Errors in ranks file '%s': %s" %
                      (Args().ranksFileName, detail) )

    #-- 4 --
    # [ if file std-file(Args()) and file alt-file(Args())
    #   are readable and valid against each other and (hier) ->
    #       txny  :=  a new Txny instance representing those files
    #   else ->
    #       Log()  +:=  error message(s)
    #       stop execution ]
    try:
        txny = Txny ( hier, Args().stdFileName, Args().altFileName )
    except IOError, detail:
        Log().fatal ( "Input files are not valid: %s" % detail )

At this point the entire structure pictured in Section 3.3, “Principal classes” has been built. All that remains is to write the various output files; see Section 9, “writeProductFiles: Write all product files”

nomcompile3
    #-- 5 --
    # [ if output-files(Args()) can be opened new for writing ->
    #     output-files(Args())  :=  representations of hier and txny
    #   else ->
    #     sys.stderr  +:=  error messages
    #     output-files(Args())  :=  (anything) ]
    writeProductFiles ( txny )