#!/usr/bin/env python # appendall.py - Append Text To Every Non-Comment Line In A File # Copyright (c) 2014 TundraWare Inc., Des Plaines, IL 60018 USA # All Rights Reserved. For Terms Of Use See: appendall-license.txt # For Program Updates See: http://www.tundraware.com/Software/appendall # $Id: appendall.py,v 1.101 2014/11/21 21:00:53 tundra Exp $ # Embed the source control ID string for use by program CVSID='$Id: appendall.py,v 1.101 2014/11/21 21:00:53 tundra Exp $' ##### # Program Information ##### PROGNAME = "appendall.py" PROGENV = "appendall".upper() VERSION = CVSID.split()[2] PROGVER = PROGNAME + " " + VERSION + " - Append Text To Every Non-Comment Line In A File" ##### # Copyright Information ##### COPYRIGHT = "Copyright (c) 2014 TundraWare Inc., Des Plaines, IL 60018 USA" ##### # List Of All Legal Options - Update This When You Add More!!!! ##### OPTIONSLIST = '-hv' #----------------------------------------------------------# # Variables User Might Change # #----------------------------------------------------------# #------------------- Nothing Below Here Should Need Changing ------------------# #----------------------------------------------------------# # Imports # #----------------------------------------------------------# import getopt import os import sys #----------------------------------------------------------# # Aliases & Redefinitions # #----------------------------------------------------------# #----------------------------------------------------------# # Constants & Literals # #----------------------------------------------------------# ##### # Constants ##### ##### # Literals ##### COMMENT = '#' #----------------------------------------------------------# # Prompts, & Application Strings # #----------------------------------------------------------# ##### # Debug Messages ##### DEBUGFLAG = "-d" dDEBUG = "DEBUG" dPROGENV = "$" + PROGENV ##### # Error Messages ##### eBADARG = "Invalid command line: %s!" eERROR = "ERROR" eNOTEXT = "Missing Text To Append!" ##### # 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,", " -h Print this help information", " -v Print detailed version information", ] #----------------------------------------------------------# # Global Variables & Data Structures # #----------------------------------------------------------# CFGFILE = os.path.join(os.getenv("HOME"), "." + "appendall") # conf file #--------------------------- Code Begins Here ---------------------------------# #----------------------------------------------------------# # Object Base Class Definitions # #----------------------------------------------------------# #----------------------------------------------------------# # Supporting Function Definitions # #----------------------------------------------------------# ##### # Print An Error Message ##### def ErrorMsg(emsg): PrintStderr(PROGNAME + " " + eERROR + ": " + emsg) # End of 'ErrorMsg()' ##### # Print To stderr ##### def PrintStderr(msg, trailing="\n"): sys.stderr.write(msg + trailing) # End of 'PrintStderr()' ##### # Print To stdout ##### def PrintStdout(msg, trailing="\n"): sys.stdout.write(msg + trailing) # 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 == "-h": Usage() sys.exit(0) if opt == "-v": PrintStdout(CVSID) sys.exit(0) # Get text to append try: text = sys.argv[1] except: ErrorMsg(eNOTEXT) sys.exit(1) for line in sys.stdin.readlines(): # Get rid of linefeeds - we'll add them back later if line[-1] == "\n": line = line[:-1] # Ignore comments text, and blank lines if line.split(COMMENT)[0].strip(): parts = line.split(COMMENT) if len(parts) == 1: line = parts[0] + text else: line = parts[0] + text + COMMENT + parts[1] PrintStdout(line)