Difference between revisions of "Python:Template"

From wiki
Jump to navigation Jump to search
(Created page with "Template for Python programs. Creating logfiles, standard modules to load, opening files, user configuration. Code for mailing is on this wiki too. ToDO: * Parsin...")
 
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Python]]
 
Template for Python programs. Creating logfiles, standard modules to load, opening files,  user configuration.
 
Template for Python programs. Creating logfiles, standard modules to load, opening files,  user configuration.
  
[[Mail|Code for mailing]] is on this wiki too.
+
[[Mail|Code for mailing is on this wiki too]].
  
 
ToDO:  
 
ToDO:  
Line 7: Line 8:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 +
#!/usr/bin/env python
  
 
def usage():
 
def usage():
 
     print ("Usage: "+__file__)
 
     print ("Usage: "+__file__)
     print("Open and write to logfiles")
+
     print("Template for python programs")
 +
    version = 'R1A, 20181010, Initial release'
 +
    version = 'v02, 20211229, Last update (counters added)'
 +
    print("Version: "+version)
 
     sys.exit(1)
 
     sys.exit(1)
  
  
# USER CONFIGURABLE ITEMS
+
# USER CONFIGURABLE ITEMS (use this or a config file (see getconfig function))
  
 +
testing = 0
 +
warning = 'If you need to modify these variables anywhere else you have to declare it global there'
 
filespec =  '*.*'
 
filespec =  '*.*'
 +
logfile2name = '<apath>_timestamp.txt'
 +
 +
# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE
  
  
# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE
+
if testing:
 +
    print('Running {} in testing mode'.format(__file))
  
import os,sys,re,glob
+
import os
 +
import sys
 +
import re
 +
import glob
 +
import logging
 +
from collections import defaultdict
 
from datetime import datetime
 
from datetime import datetime
timestamp = datetime.now().strftime("%y%m%d_%H%M%S")
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
 +
 
 +
logfile2name = logfile2name.replace('timestamp', timestamp)
 +
counters = defaultdict(int)
  
 
def main():
 
def main():
 
+
    config = get_config(scriptname = __file__)
     logfile,logfilename = openlog()
+
     logfilename = openlog()
     logfile2,dummy = openlog(<apath>)
+
     openlog(<apath>)
     writelog(logfile,"This is in the logfile with generated name")
+
     logging.info("This is in the logfile with generated name")
    writelog(logfile2,"This is in the logfile with specified name")
 
 
     openfile(filespec)
 
     openfile(filespec)
 +
    for counter, value in counters.items():
 +
        print('{:10s} {:5d}'.format(counter, value)
  
 
     return
 
     return
 +
  
 
def openfile(filespec):
 
def openfile(filespec):
Line 41: Line 62:
 
             with open (filename,"r") as file:
 
             with open (filename,"r") as file:
 
                 for line in file:
 
                 for line in file:
 +
                    #remove newline characters
 +
                    line.rstrip('\r\n')
 
                     <codeblock>
 
                     <codeblock>
 
     return
 
     return
  
  
def writelog (logfile,message):
+
def openlog (logfilename = ''):
     try:
+
    if logfilename == '':
         logfile.name
+
        logfilename = __file__.replace('/bin/', '/log/')
     except AttributeError:
+
        logfilename += '_'+timestamp+'.log'
         print("ERROR: Logfile is not open")
+
    if testing:
        usage()
+
        logfilename = logfilename+'_testing'
 +
    logging.basicConfig(filename=logfilename, level=logging.DEBUG,format='%(asctime)s %(levelname)7s %(message)s')
 +
    logging.info(logfilename+" open.")
 +
    return(logfilename)
 +
 
 +
 
 +
def get_config(scriptname = '' , rcfile = None):
 +
    '''Read configuration from a given rcfile or from a configuration file in ./, ../etc, ./etc or /etc '''
 +
    import os
 +
    import re   
 +
    config = {}
 +
    if rcfile:
 +
        pass
 +
     else:
 +
        rcfile = re.sub('\.py$', '.rc', scriptname)
 +
        if os.path.isfile(rcfile):
 +
            pass
 +
         else:
 +
            rcfile = rcfile.replace('/bin/', '/etc/')
 +
            if os.path.isfile(rcfile):
 +
                pass
 +
            else:
 +
                scriptpath = os.path.dirname(scriptname)
 +
                rcfilename = os.path.basename(rcfile)
 +
                rcfile = scriptpath+'/etc/'+rcfilename
 +
                if os.path.isfile(rcfile):
 +
                    pass
 +
                else:
 +
                    rcfile = '/etc/'+rcfilename
 +
     if os.path.isfile(rcfile):
 +
         with open ( rcfile, "r") as file:
 +
            for line in file:
 +
                line = re.sub('\#.*$', '', line).strip()
 +
                s = re.search('\s*=+\s*', line)
 +
                if s:
 +
                    parameter = line[:s.start()]
 +
                    value = line[s.end():]
 +
                    if value[0] == '[' and value[-1] == ']':
 +
                        tmplist = list(value[1:-1].split(','))
 +
                        value = []
 +
                        for val in tmplist:
 +
                            val = val.strip(' \'\"')
 +
                            value.append(val)
 +
                    config[parameter] = value
 
     else:
 
     else:
         logfile.write(datetime.now().strftime("%y%m%d_%H%M")+" "+message+"\n")
+
         print("No configuration file found")
    return
 
  
 +
    return config
  
def openlog (logfilename = ''):
 
    if logfilename == '':
 
        logfilename = re.sub('/bin/','/log/',__file__)
 
        logfilename += '_'+datetime.now().strftime("%y%m%d_%H%M")+'.log'
 
    try:
 
        logfile.name
 
    except UnboundLocalError:
 
        logfile = open (logfilename,"a")
 
    return(logfile,logfilename)
 
  
 
main()
 
main()
 +
</syntaxhighlight>
 +
 +
Logging suggestion to test:
 +
<syntaxhighlight lang=python>
 +
def logfunc(logFile): 
 +
    logging.basicConfig(format = '%(asctime)s %(filename)25s %(levelname)6s : %(message)s' , level=logging.INFO) 
 +
    log = logging.getLogger() 
 +
    fh = logging.FileHandler(logFile) 
 +
    formatter = logging.Formatter('%(asctime)s %(filename)25s %(levelname)6s : %(message)s') 
 +
    fh.setFormatter(formatter) 
 +
    log.addHandler(fh) 
 +
    return log
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 11:44, 29 December 2021

Template for Python programs. Creating logfiles, standard modules to load, opening files, user configuration.

Code for mailing is on this wiki too.

ToDO:

  • Parsing commandline options
#!/usr/bin/env python

def usage():
    print ("Usage: "+__file__)
    print("Template for python programs")
    version = 'R1A, 20181010, Initial release'
    version = 'v02, 20211229, Last update (counters added)'
    print("Version: "+version)
    sys.exit(1)


# USER CONFIGURABLE ITEMS (use this or a config file (see getconfig function))

testing = 0
warning = 'If you need to modify these variables anywhere else you have to declare it global there'
filespec =  '*.*'
logfile2name = '<apath>_timestamp.txt'

# END USER CONFIGURABLE ITEMS, DO NOT CHANGE ANYTHING BELOW THIS LINE


if testing:
    print('Running {} in testing mode'.format(__file))

import os
import sys
import re
import glob
import logging
from collections import defaultdict
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")

logfile2name = logfile2name.replace('timestamp', timestamp)
counters = defaultdict(int)

def main():
    config = get_config(scriptname = __file__)
    logfilename = openlog()
    openlog(<apath>)
    logging.info("This is in the logfile with generated name")
    openfile(filespec)
    for counter, value in counters.items():
        print('{:10s} {:5d}'.format(counter, value)

    return


def openfile(filespec):
    filelist = glob.glob(filespec)
    for filename in filelist:
        if os.path.isfile(filename):
            with open (filename,"r") as file:
                for line in file:
                    #remove newline characters
                    line.rstrip('\r\n')
                    <codeblock>
    return


def openlog (logfilename = ''):
    if logfilename == '':
        logfilename = __file__.replace('/bin/', '/log/')
        logfilename += '_'+timestamp+'.log'
    if testing:
        logfilename = logfilename+'_testing'
    logging.basicConfig(filename=logfilename, level=logging.DEBUG,format='%(asctime)s %(levelname)7s %(message)s')
    logging.info(logfilename+" open.")
    return(logfilename)


def get_config(scriptname = '' , rcfile = None):
    '''Read configuration from a given rcfile or from a configuration file in ./, ../etc, ./etc or /etc '''
    import os
    import re    
    config = {}
    if rcfile:
        pass
    else:
        rcfile = re.sub('\.py$', '.rc', scriptname)
        if os.path.isfile(rcfile):
            pass
        else:
            rcfile = rcfile.replace('/bin/', '/etc/')
            if os.path.isfile(rcfile):
                pass
            else:
                scriptpath = os.path.dirname(scriptname)
                rcfilename = os.path.basename(rcfile)
                rcfile = scriptpath+'/etc/'+rcfilename
                if os.path.isfile(rcfile):
                    pass
                else:
                    rcfile = '/etc/'+rcfilename
    if os.path.isfile(rcfile):
        with open ( rcfile, "r") as file:
            for line in file:
                line = re.sub('\#.*$', '', line).strip()
                s = re.search('\s*=+\s*', line)
                if s:
                    parameter = line[:s.start()]
                    value = line[s.end():]
                    if value[0] == '[' and value[-1] == ']':
                        tmplist = list(value[1:-1].split(','))
                        value = []
                        for val in tmplist:
                            val = val.strip(' \'\"')
                            value.append(val)
                    config[parameter] = value
    else:
        print("No configuration file found")

    return config


main()

Logging suggestion to test:

def logfunc(logFile):  
    logging.basicConfig(format = '%(asctime)s %(filename)25s %(levelname)6s : %(message)s' , level=logging.INFO)  
    log = logging.getLogger()  
    fh = logging.FileHandler(logFile)  
    formatter = logging.Formatter('%(asctime)s %(filename)25s %(levelname)6s : %(message)s')  
    fh.setFormatter(formatter)  
    log.addHandler(fh)  
    return log