Newer
Older
tmkproject / py / program.py
@tundra tundra on 19 Feb 2014 5 KB More cosmetic cleanup.
#####
# Program Information
#####

PROGNAME = "__FULLNAME__"
PROGENV  = "__PROJECTNAME__".upper()
VERSION  = CVSID.split()[2]
PROGVER  = PROGNAME + " " + VERSION + " - __DESCRIPTION__"


#####
# Copyright Information
#####

COPYRIGHT    = "__COPYRIGHT__"

#####
# List Of All Legal Options - Update This When You Add More!!!!
#####

OPTIONSLIST = '-f:hv'


#----------------------------------------------------------#
#            Variables User Might Change                   #
#----------------------------------------------------------#



#------------------- Nothing Below Here Should Need Changing ------------------#


#----------------------------------------------------------#
#                       Imports                            #
#----------------------------------------------------------#

import getopt
import os
import sys
# import tconfpy # Uncomment this and code below to parse config files


#----------------------------------------------------------#
#                 Aliases & Redefinitions                  #
#----------------------------------------------------------#



#----------------------------------------------------------#
#                Constants & Literals                      #
#----------------------------------------------------------#



#####
# Constants
#####



#####
# Literals
#####



#----------------------------------------------------------#
#              Prompts, & Application Strings              #
#----------------------------------------------------------#


#####
# Debug Messages
#####

DEBUGFLAG     =   "-d"
dDEBUG        =   "DEBUG"
dPROGENV      =   "$" + PROGENV

#####
# Error Messages
#####

eBADARG  =  "Invalid command line: %s!"
eERROR   =  "ERROR"


#####
# Informational Messages
#####

iINFO    =   "INFO"


#####
# Usage Prompts
#####

# Make the options list more human readable

optionslist =  OPTIONSLIST.replace(':', ' arg -').strip()
if optionslist[-1] == '-':
    optionslist = optionslist[:-1].strip()

uTable = [PROGVER,
          "usage:  " + PROGNAME + " [%s]" % optionslist,
          "   where,",
          "          -f file  Configuration file to use",
          "          -h       Print this help information",
          "          -v       Print detailed version information",
          ]


#----------------------------------------------------------#
#          Global Variables & Data Structures              #
#----------------------------------------------------------#

CFGFILE = os.path.join(os.getenv("HOME"), "." + "__PROJECTNAME__")  # conf file


#--------------------------- Code Begins Here ---------------------------------#


#----------------------------------------------------------#
#             Object Base Class Definitions                #
#----------------------------------------------------------#

    

#----------------------------------------------------------#
#             Supporting Function Definitions              #
#----------------------------------------------------------#


def ColumnPad(list, padchar=" ", padwidth=20):

    retval = ""
    for l in list:
        l = str(l)
        retval += l + ((padwidth - len(l)) * padchar)

    return retval.strip()

# End of 'ColumnPad()'


#####
# Print A Debug Message
#####

def DebugMsg(msg):
    PrintStderr(PROGNAME + " " + dDEBUG + ": " + msg)

# End of 'DebugMsg()'


#####
# Dump The State Of The Program
#####

def DumpState():

    # Dump the command line
    DebugMsg(ColumnPad(["Command Line", sys.argv]))

    # Names of all the state variables we want dumped
    state = [
            ]

    for k in state:
        DebugMsg(ColumnPad([k, eval(k)]))

# End of 'DumpState()'


#####
# Print An Error Message
#####

def ErrorMsg(emsg):
    PrintStderr(PROGNAME + " " + eERROR + ": " + emsg)

# End of 'ErrorMsg()'


#####
# Print An Info Message
#####

def InfoMsg(imsg):
    PrintStderr(PROGNAME + " " + iINFO + ": " + imsg)

# End of 'InfoMsg()'


#####
# Print To stderr
#####

def PrintStderr(msg, trailing="\n"):
    sys.stderr.write(msg + trailing)
    sys.stderr.flush()

# End of 'PrintStderr()'


#####
# Print To stdout
#####

def PrintStdout(msg, trailing="\n"):
    sys.stdout.write(msg + trailing)
    sys.stdout.flush()

# End of 'PrintStdout'


#####
# Print Usage Information
#####

def Usage():
    for line in uTable:
        PrintStdout(line)

# End of 'Usage()'


#----------------------------------------------------------#
#                    Program Entry Point                   #
#----------------------------------------------------------#

# Command line processing - Process any options set in the
# environment first, and then those given on the command line

OPTIONS = sys.argv[1:]
envopt = os.getenv(PROGENV)
if envopt:
    OPTIONS = envopt.split() + OPTIONS

try:
    opts, args = getopt.getopt(OPTIONS, OPTIONSLIST)

except getopt.GetoptError, (errmsg, badarg):

    ErrorMsg(eBADARG % errmsg)
    sys.exit(1)

for opt, val in opts:

    if opt == "-f":
        CFGFILE=val

    if opt == "-h":
        Usage()
        sys.exit(0)

    if opt == "-v":
        PrintStdout(CVSID)
        sys.exit(0)

# Processing of the configuration file below requires installation of
# the freely available TundraWare Inc. 'tconfpy' parser and then
# uncommenting the code below.  The parser can be found at:
#
#   http://www.tundraware.com/Software/tconfpy
#
# Process the configuration file
#
#retval = tconfpy.ParseConfig(CFGFILE, CallingProgram="%s %s " % (PROGNAME, VERSION))
#
# Print any errors or warning generated by the parse
#
# for x in (retval.ErrMsgs, retval.WarnMsgs):
#    for y in x:
#        print y
#
# If there were any errors, we're done
#if retval.ErrMsgs:
#    sys.exit(0)