From e3f2a894093bf1207399a7794f954c13799073b2 Mon Sep 17 00:00:00 2001 From: karolina <amvdw95@gmail.com> Date: Wed, 8 May 2013 06:31:53 +0000 Subject: [PATCH] ordered imports (mainly moved to the top of the module) --- da/analysis/expand_fluxes.py | 41 ++-- da/analysis/expand_mixingratios.py | 21 +- da/analysis/tools_country.py | 10 +- da/analysis/tools_regions.py | 17 +- da/analysis/tools_time.py | 13 +- da/ct/obspack.py | 18 +- da/ct/statevector.py | 10 +- da/platform/capegrim.py | 45 ++-- da/platform/huygens.py | 41 ++-- da/platform/jet.py | 8 +- da/platform/maunaloa.py | 1 - da/tm5/observationoperator.py | 14 +- da/tools/initexit.py | 13 +- da/tools/io4.py | 5 +- da/tools/rc.py | 276 +++++++++++------------ da/tools/rcn.py | 343 ++++++++++++++--------------- 16 files changed, 411 insertions(+), 465 deletions(-) diff --git a/da/analysis/expand_fluxes.py b/da/analysis/expand_fluxes.py index d01ec4e..46b3a87 100755 --- a/da/analysis/expand_fluxes.py +++ b/da/analysis/expand_fluxes.py @@ -3,13 +3,23 @@ import sys sys.path.append('../../') import os -import getopt from datetime import datetime, timedelta -from da.tools.general import create_dirs -from da.analysis.tools_country import countryinfo # needed here + import logging import numpy as np from pylab import date2num, num2date +import da.tools.io4 as io +from da.analysis.tools_regions import globarea, state_to_grid +from da.tools.general import create_dirs +from da.analysis.tools_country import countryinfo # needed here +from da.analysis.tools_transcom import transcommask, ExtendedTCRegions + + +import da.analysis.tools_transcom as tc +import da.analysis.tools_country as ct +import da.analysis.tools_time as timetools + + """ Author: Wouter Peters (Wouter.Peters@noaa.gov) @@ -38,10 +48,6 @@ def save_weekly_avg_1x1_data(DaCycle, StateVector): :param StateVector: a :class:`~da.baseclasses.statevector.StateVector` :rtype: None """ - - import da.tools.io4 as io - - from da.analysis.tools_regions import globarea # dirname = create_dirs(os.path.join(DaCycle['dir.analysis'], 'data_flux1x1_weekly')) # @@ -217,10 +223,6 @@ def save_weekly_avg_state_data(DaCycle, StateVector): :param StateVector: a :class:`~da.baseclasses.statevector.StateVector` :rtype: None """ - - import da.tools.io4 as io - import logging - from da.analysis.tools_regions import globarea dirname = create_dirs(os.path.join(DaCycle['dir.analysis'], 'data_state_weekly')) # @@ -452,9 +454,7 @@ def save_weekly_avg_tc_data(DaCycle, StateVector): these with the parameters in the StateVector. This creates posterior fluxes, and the posterior covariance for the complete StateVector in units of mol/box/s which we then turn into TC fluxes and covariances. """ - from da.analysis.tools_regions import globarea - from da.analysis.tools_transcom import transcommask - import da.tools.io4 as io + # dirname = create_dirs(os.path.join(DaCycle['dir.analysis'], 'data_tc_weekly')) # @@ -584,9 +584,7 @@ def save_weekly_avg_ext_tc_data(DaCycle): *** Example *** ./expand_savestate project=enkf_release sd=20000101 ed=20010101 """ - from da.analysis.tools_transcom import ExtendedTCRegions - import da.tools.io4 as io - import logging + # dirname = create_dirs(os.path.join(DaCycle['dir.analysis'], 'data_tc_weekly')) # @@ -704,11 +702,7 @@ def save_weekly_avg_agg_data(DaCycle, region_aggregate='olson'): these with the parameters in the StateVector. This creates posterior fluxes, and the posterior covariance for the complete StateVector in units of mol/box/s which we then turn into TC fluxes and covariances. """ - from da.analysis.tools_regions import globarea, state_to_grid - import da.analysis.tools_transcom as tc - import da.analysis.tools_country as ct - import da.tools.io4 as io - import logging + # dirname = create_dirs(os.path.join(DaCycle['dir.analysis'], 'data_%s_weekly' % region_aggregate)) # @@ -918,9 +912,6 @@ def save_time_avg_data(DaCycle, infile, avg='monthly'): *** Example *** ./expand_savestate project=enkf_release sd=20000101 ed=20010101 """ - import da.analysis.tools_time as timetools - import da.tools.io4 as io - if 'weekly' in infile: intime = 'weekly' if 'monthly' in infile: diff --git a/da/analysis/expand_mixingratios.py b/da/analysis/expand_mixingratios.py index c1a5c00..7e0154c 100755 --- a/da/analysis/expand_mixingratios.py +++ b/da/analysis/expand_mixingratios.py @@ -1,13 +1,19 @@ #!/usr/bin/env python # expand_fluxes.py import sys -sys.path.append('../../') import os import getopt -from datetime import datetime, timedelta -from da.tools.general import create_dirs +import shutil +import logging +import netCDF4 import numpy as np +from string import join +from datetime import datetime, timedelta from pylab import date2num, num2date +sys.path.append('../../') +from da.tools.general import create_dirs +import da.tools.io4 as io + """ Author: Wouter Peters (Wouter.Peters@wur.nl) @@ -33,15 +39,8 @@ def write_mixing_ratios(DaCycle): (4) Open the copied file, find the index of each observation, fill in the simulated data """ - import da.tools.io4 as io - from string import join - import shutil - import logging - import netCDF4 - - dirname = 'data_molefractions' - dirname = create_dirs(os.path.join(DaCycle['dir.analysis'], dirname)) + dirname = create_dirs(os.path.join(DaCycle['dir.analysis'], 'data_molefractions')) # # Some help variables # diff --git a/da/analysis/tools_country.py b/da/analysis/tools_country.py index 1fa7418..1624ec8 100755 --- a/da/analysis/tools_country.py +++ b/da/analysis/tools_country.py @@ -23,14 +23,18 @@ by oceans, sea, or open water. The aggregation will thus work best on arrays tha """ import sys -sys.path.append('../../') +import cPickle +import os + +from numpy import sum, array try: from dbfpy import dbf except: print "the python DBF lib might be needed, please install from:" print "http://dbfpy.sourceforge.net/" print " Trying to complete anyway..." -from numpy import sum, array + +sys.path.append('../../') from da.analysis.tools_regions import globarea EU25 = ["Austria", "Belgium", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Poland", "Portugal", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom"] @@ -129,8 +133,6 @@ def fix_eu(rec): def get_countrydict(): """ Create a dictionary with grid-to-country information from a dbf file""" - import cPickle - import os countrydict = countryinfo('Test') diff --git a/da/analysis/tools_regions.py b/da/analysis/tools_regions.py index 1f3b990..5753386 100755 --- a/da/analysis/tools_regions.py +++ b/da/analysis/tools_regions.py @@ -1,7 +1,7 @@ #!/usr/bin/env python -import commands -import os -import sys + +import numpy as np +import cPickle from da.analysis.tools_transcom import * # Aggregated olson ecosystem regions for CT Europe @@ -31,21 +31,15 @@ def state_to_grid(values, regionmap, reverse=False, avg=False, mapname=None): can subsequently be used in the transport model code to multiply/manipulate fluxes """ - import numpy as np - import cPickle - nregions = regionmap.max() - try: if not mapname: raise Exception regionselect = cPickle.load(open('%s_regiondict.pickle' % mapname, 'rb')) - except: # dictionary for region <-> map conversions - regs = {} for r in np.arange(1, nregions + 1): sel = (regionmap.flat == r).nonzero() @@ -53,14 +47,11 @@ def state_to_grid(values, regionmap, reverse=False, avg=False, mapname=None): regs[r] = sel regionselect = regs - cPickle.dump(regionselect, open('%s_regiondict.pickle' % mapname, 'wb'), -1) print 'Pickling region map' - if reverse: - """ project 1x1 degree map onto ecoregions """ result = np.zeros(nregions, float) @@ -72,7 +63,6 @@ def state_to_grid(values, regionmap, reverse=False, avg=False, mapname=None): return result else: - """ project ecoregion properties onto 1x1 degree map """ result = np.zeros((180, 360,), float) @@ -83,7 +73,6 @@ def state_to_grid(values, regionmap, reverse=False, avg=False, mapname=None): def globarea(im=360, jm=180, silent=True): """ Function calculates the surface area according to TM5 definitions""" - import numpy as np radius = 6.371e6 # the earth radius in meters deg2rad = np.pi / 180. diff --git a/da/analysis/tools_time.py b/da/analysis/tools_time.py index cd4c280..7dd8756 100755 --- a/da/analysis/tools_time.py +++ b/da/analysis/tools_time.py @@ -1,10 +1,12 @@ #! /usr/bin/env python import sys -from datetime import datetime, timedelta import calendar -from pylab import floor +import copy +from datetime import datetime, timedelta from matplotlib.dates import date2num, num2date -from numpy import array, zeros, newaxis +from numpy import array, zeros, newaxis, logical_and, arange +from pylab import floor, drange, num2date, date2num + def Fromdatetime(date): dt = date.timetuple() @@ -111,11 +113,6 @@ def nextmonth(dd): def in_interval(start, stop, times_in): """ returns a list of fractions in time interval """ - from numpy import logical_and, arange - from pylab import drange, num2date, date2num - - import copy - times = copy.copy(times_in) interval = times[1] - times[0] diff --git a/da/ct/obspack.py b/da/ct/obspack.py index 31783f0..1677dd3 100755 --- a/da/ct/obspack.py +++ b/da/ct/obspack.py @@ -11,14 +11,21 @@ File created on 28 Jul 2010. import os import sys import logging +import datetime as dtm +from string import strip +from numpy import array, logical_and + sys.path.append(os.getcwd()) sys.path.append('../../') +import da.tools.io4 as io +from da.baseclasses.obs import Observation + identifier = 'CarbonTracker CO2 mixing ratios' version = '0.0' -from da.baseclasses.obs import Observation + ################### Begin Class ObsPackObservations ################### @@ -50,10 +57,7 @@ class ObsPackObservations(Observation): We will loop over all site files in the ObsPackage, and subset each to our needs """ - import da.tools.io4 as io - import datetime as dtm - from string import strip - from numpy import array, logical_and + # Step 1: Read list of available site files in package @@ -111,8 +115,6 @@ class ObsPackObservations(Observation): def add_simulations(self, filename, silent=False): """ Adds model simulated values to the mixing ratio objects """ - import da.tools.io4 as io - if not os.path.exists(filename): msg = "Sample output filename for observations could not be found : %s" % filename logging.error(msg) @@ -152,7 +154,6 @@ class ObsPackObservations(Observation): Write the information needed by the observation operator to a file. Return the filename that was written for later use """ - import da.tools.io4 as io obsinputfile = os.path.join(dirinput, 'observations_%s.nc' % timestamp) @@ -344,7 +345,6 @@ class ObsPackObservations(Observation): Write selected information contained in the Observation object to a file. """ - import da.tools.io4 as io outfile = os.path.join(outdir, 'sampleinfo_%s.nc' % timestamp) diff --git a/da/ct/statevector.py b/da/ct/statevector.py index 58fb3dd..65163f8 100755 --- a/da/ct/statevector.py +++ b/da/ct/statevector.py @@ -14,8 +14,10 @@ import sys sys.path.append(os.getcwd()) import logging -from da.baseclasses.statevector import StateVector import numpy as np +from da.baseclasses.statevector import StateVector, EnsembleMember + +import da.tools.io4 as io identifier = 'CarbonTracker Statevector ' version = '0.0' @@ -30,8 +32,6 @@ class CtStateVector(StateVector): Note that lag=1 means an index of 0 in python, hence the notation lag-1 in the indexing below. The argument is thus referring to the lagged state vector as [1,2,3,4,5,..., nlag] """ - - import da.tools.io4 as io try: import matplotlib.pyplot as plt except: @@ -109,9 +109,7 @@ class CtStateVector(StateVector): :meth:`~da.baseclasses.statevector.StateVector.write_to_file` """ - - import da.tools.io4 as io - from da.baseclasses.statevector import EnsembleMember + f = io.CT_Read(filename, 'read') diff --git a/da/platform/capegrim.py b/da/platform/capegrim.py index f8d952d..d658297 100755 --- a/da/platform/capegrim.py +++ b/da/platform/capegrim.py @@ -9,18 +9,15 @@ File created on 06 Sep 2010. """ -import sys -import os import logging -import subprocess from da.baseclasses.platform import PlatForm, std_joboptions class CapeGrimPlatForm(PlatForm): def __init__(self): - self.Identifier = 'WU capegrim' # the identifier gives the platform name - self.Version = '1.0' # the platform version used + self.Identifier = 'WU capegrim' # the identifier gives the platform name + self.Version = '1.0' # the platform version used def give_blocking_flag(self): return "" @@ -28,7 +25,7 @@ class CapeGrimPlatForm(PlatForm): def give_queue_type(self): return "foreground" - def get_job_template(self,joboptions={},block=False): + def get_job_template(self, joboptions={}, block=False): """ Returns the job template for a given computing system, and fill it with options from the dictionary provided as argument. The job template should return the preamble of a job that can be submitted to a queue on your platform, @@ -46,38 +43,38 @@ class CapeGrimPlatForm(PlatForm): job until the submitted job in this template has been completed fully. """ - template = """## \n"""+ \ - """## This is a set of dummy names, to be replaced by values from the dictionary \n"""+ \ - """## Please make your own platform specific template with your own keys and place it in a subfolder of the da package.\n """+ \ - """## \n"""+ \ - """ \n"""+ \ - """#$ jobname \n"""+ \ - """#$ jobaccount \n"""+ \ - """#$ jobnodes \n"""+ \ - """#$ jobtime \n"""+ \ - """#$ jobshell \n"""+ \ - """\n"""+ \ - """source /usr/local/Modules/3.2.8/init/sh\n"""+ \ - """module load python\n"""+ \ + template = """## \n""" + \ + """## This is a set of dummy names, to be replaced by values from the dictionary \n""" + \ + """## Please make your own platform specific template with your own keys and place it in a subfolder of the da package.\n """ + \ + """## \n""" + \ + """ \n""" + \ + """#$ jobname \n""" + \ + """#$ jobaccount \n""" + \ + """#$ jobnodes \n""" + \ + """#$ jobtime \n""" + \ + """#$ jobshell \n""" + \ + """\n""" + \ + """source /usr/local/Modules/3.2.8/init/sh\n""" + \ + """module load python\n""" + \ """\n""" if 'depends' in joboptions: template += """#$ -hold_jid depends \n""" # First replace from passed dictionary - for k,v in joboptions.iteritems(): + for k, v in joboptions.iteritems(): while k in template: - template = template.replace(k,v) + template = template.replace(k, v) # Fill remaining values with std_options - for k,v in std_joboptions.iteritems(): + for k, v in std_joboptions.iteritems(): while k in template: - template = template.replace(k,v) + template = template.replace(k, v) return template - msg1 = 'Platform initialized: %s'%self.Identifier ; logging.info(msg1) + msg1 = 'Platform initialized: %s' % self.Identifier ; logging.info(msg1) #msg2 = '%s version: %s'%(self.Identifier,self.Version) ; logging.info(msg2) diff --git a/da/platform/huygens.py b/da/platform/huygens.py index ba198c0..1961886 100755 --- a/da/platform/huygens.py +++ b/da/platform/huygens.py @@ -62,20 +62,20 @@ class HuygensPlatForm(PlatForm): job until the submitted job in this template has been completed fully. """ - # template = """## \n"""+ \ - # """## This is a set of dummy names, to be replaced by values from the dictionary \n"""+ \ - # """## Please make your own platform specific template with your own keys and place it in a subfolder of the da package.\n """+ \ - # """## \n"""+ \ - # """ \n"""+ \ - # """#$ jobname \n"""+ \ - # """#$ jobaccount \n"""+ \ - # """#$ jobnodes \n"""+ \ - # """#$ jobtime \n"""+ \ - # """#$ jobshell \n"""+ \ - # """\n"""+ \ - # """source /usr/bin/sh\n"""+ \ - # """module load python\n"""+ \ - # """\n""" + #template = """## \n"""+ \ + # """## This is a set of dummy names, to be replaced by values from the dictionary \n"""+ \ + # """## Please make your own platform specific template with your own keys and place it in a subfolder of the da package.\n """+ \ + # """## \n"""+ \ + # """ \n"""+ \ + # """#$ jobname \n"""+ \ + # """#$ jobaccount \n"""+ \ + # """#$ jobnodes \n"""+ \ + # """#$ jobtime \n"""+ \ + # """#$ jobshell \n"""+ \ + # """\n"""+ \ + # """source /usr/bin/sh\n"""+ \ + # """module load python\n"""+ \ + # """\n""" template = """#!/bin/bash \n""" + \ @@ -150,19 +150,20 @@ class HuygensPlatForm(PlatForm): """ This method submits a jobfile to the queue, and returns the queue ID """ - # cmd = ["llsubmit","-s",jobfile] - # msg = "A new task will be started (%s)"%cmd ; logging.info(msg) + #cmd = ["llsubmit","-s",jobfile] + #msg = "A new task will be started (%s)"%cmd ; logging.info(msg) if block: cmd = ["llsubmit", "-s", jobfile] - msg = "A new task will be started (%s)" % cmd ; logging.info(msg) - output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] ; logging.info(output) + logging.info("A new task will be started (%s)" % cmd) + output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] + logging.info(output) print 'output', output jobid = output.split()[3] print 'jobid', jobid else: cmd = ["llsubmit", jobfile] - msg = "A new task will be started (%s)" % cmd ; logging.info(msg) + logging.info("A new task will be started (%s)" % cmd) output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] ; logging.info(output) jobid = output.split()[3] @@ -177,7 +178,7 @@ class HuygensPlatForm(PlatForm): # jobid = output.split()[2] # retcode = output.split()[-1] # - #for huygens +# #for huygens # print 'output', output # test = output.split()[3] # dummy, jobid =test.split('nl.') diff --git a/da/platform/jet.py b/da/platform/jet.py index b438036..d6ed747 100755 --- a/da/platform/jet.py +++ b/da/platform/jet.py @@ -65,20 +65,18 @@ class JetPlatForm(PlatForm): def submit_job(self, jobfile, joblog=None, block=False): """ This method submits a jobfile to the queue, and returns the queue ID """ - - + cmd = ["qsub", jobfile] logging.info("A new task will be started (%s)" % cmd) output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] logging.info(output) - jobid = output.split()[2] + #jobid = output.split()[2] retcode = output.split()[-1] return retcode def kill_job(self, jobid): - """ This method kills a running job """ - + """ This method kills a running job """ output = subprocess.Popen(['qdel', jobid], stdout=subprocess.PIPE).communicate()[0] logging.info(output) return output diff --git a/da/platform/maunaloa.py b/da/platform/maunaloa.py index 260a919..e3abd32 100755 --- a/da/platform/maunaloa.py +++ b/da/platform/maunaloa.py @@ -22,7 +22,6 @@ class MaunaloaPlatForm(PlatForm): return "" def give_queue_type(self): - return "foreground" def get_job_template(self, joboptions={}, block=False): diff --git a/da/tm5/observationoperator.py b/da/tm5/observationoperator.py index 39ed8df..778dea5 100755 --- a/da/tm5/observationoperator.py +++ b/da/tm5/observationoperator.py @@ -26,10 +26,13 @@ import logging import shutil import datetime import subprocess - +from string import join sys.path.append(os.getcwd()) sys.path.append("../../") +from da.tools.general import create_dirs, to_datetime +from da.baseclasses.observationoperator import ObservationOperator + identifier = 'TM5' version = 'release 3.0' mpi_shell_filename = 'tm5_mpi_wrapper' @@ -38,7 +41,7 @@ mpi_shell_location = 'da/bin/' ################### Begin Class TM5 ################### -from da.baseclasses.observationoperator import ObservationOperator + class TM5ObservationOperator(ObservationOperator): """ This class holds methods and variables that are needed to run the TM5 model. It is initiated with as only argument a TM5 rc-file @@ -87,7 +90,7 @@ class TM5ObservationOperator(ObservationOperator): Execute all steps needed to prepare the ObsOperator for use inside CTDAS, only done at the very first cycle normally """ - from da.tools.general import create_dirs + if self.DaCycle['time.restart'] == False : logging.info('First time step, setting up and compiling the TM5 model before proceeding!') @@ -130,8 +133,7 @@ class TM5ObservationOperator(ObservationOperator): """ Compile TM5 model using setup_tm5 and the modified rc-file """ - import subprocess - from string import join + DaPlatForm = self.DaCycle.DaPlatForm if self.DaCycle.has_key('da.obsoperator.home'): @@ -228,7 +230,7 @@ class TM5ObservationOperator(ObservationOperator): Validate the contents of the tm_settings dictionary and add extra values. The required items for the TM5 rc-file are specified in the tm5_tools module, as dictionary variable "needed_rc_items". """ - from da.tools.general import to_datetime + if self.RcFileType == 'pycasso': self.projectkey = 'my.project.dir' diff --git a/da/tools/initexit.py b/da/tools/initexit.py index 7e77cb9..e152fb3 100755 --- a/da/tools/initexit.py +++ b/da/tools/initexit.py @@ -59,10 +59,13 @@ import os import sys import shutil import copy +import getopt +import cPickle +import numpy as np from string import join import da.tools.rc as rc -from da.tools.general import create_dirs +from da.tools.general import create_dirs, to_datetime, advance_time needed_da_items = [ 'time.start', @@ -131,7 +134,6 @@ class CycleControl(dict): Validate the contents of the rc-file given a dictionary of required keys. Currently required keys are :attr:`~da.tools.initexit.needed_da_items` """ - from da.tools.general import to_datetime for k, v in self.iteritems(): if v in ['True', 'true', 't', 'T', 'y', 'yes']: @@ -161,7 +163,6 @@ class CycleControl(dict): """ Parse time related parameters into datetime objects for later use """ - from da.tools.general import advance_time startdate = self['time.start'] finaldate = self['time.finish'] @@ -217,7 +218,6 @@ class CycleControl(dict): """ Advance sampling start and end time by one cycle interval """ - from da.tools.general import advance_time days = self['cyclelength'].days @@ -229,7 +229,6 @@ class CycleControl(dict): """ Advance cycle start and end time by one cycle interval """ - from da.tools.general import advance_time days = self['cyclelength'].days @@ -249,8 +248,7 @@ class CycleControl(dict): in a python :mod:`pickle` file, residing in the ``exec`` directory """ - import cPickle - import numpy as np + filename = os.path.join(self['dir.exec'], 'randomseed.pickle') @@ -659,7 +657,6 @@ def parse_options(): ======== ======= """ - import getopt # Parse keywords, the only option accepted so far is the "-h" flag for help diff --git a/da/tools/io4.py b/da/tools/io4.py index 7ee9732..3ba1e3d 100755 --- a/da/tools/io4.py +++ b/da/tools/io4.py @@ -12,9 +12,10 @@ File modified for CT data assimilation system in July 2010, Wouter Peters import standardvariables import pycdf as CDF import datetime as dt -from numpy import array +from numpy import array, arange import os + disclaimer = "This data belongs to the CarbonTracker project" email = "wouter.peters@wur.nl" url = "http://carbontracker.wur.nl" @@ -79,7 +80,6 @@ class CT_CDF(CDF.CDF): return (dimobs,) def add_latlon_dim(self, istart=0, iend=360, jstart=0, jend=180): - from numpy import arange, float64 if 'latitude' in self.dimensions(): return (self.dim('latitude'), self.dim('longitude')) # already exists @@ -140,7 +140,6 @@ class CT_CDF(CDF.CDF): def standard_var(self, varname): """ return properties of standard variables """ - import standardvariables if varname in standardvariables.standard_variables.keys(): return standardvariables.standard_variables[varname] diff --git a/da/tools/rc.py b/da/tools/rc.py index 02a1047..080217b 100755 --- a/da/tools/rc.py +++ b/da/tools/rc.py @@ -210,6 +210,11 @@ HISTORY """ +import re +import os +import sys +import logging + # ------------------------------------------------ # classes # ------------------------------------------------ @@ -221,7 +226,7 @@ class RcFile(object) : Class to store settings read from a rcfile. """ - def __init__( self, filename, silent=False, marks=('${','}') ) : + def __init__(self, filename, silent=False, marks=('${', '}')) : """ @@ -236,15 +241,9 @@ class RcFile(object) : <mark1>...<mark2> """ - - # external: - import re - import os - import sys - import logging # info ... - logging.debug( 'reading rcfile %s ...' % filename ) + logging.debug('reading rcfile %s ...' % filename) # check ... if not os.path.exists(filename) : @@ -264,7 +263,7 @@ class RcFile(object) : self.values = {} # open the specified rc-file: - f = open(filename,'r') + f = open(filename, 'r') # store all lines in a list: inpfile = f.readlines() # close: @@ -336,7 +335,7 @@ class RcFile(object) : # skip comment: if inpline.startswith('!') : # add copy to output file: - self.outfile.append( '%s\n' % inpline ) + self.outfile.append('%s\n' % inpline) # next will be a new line: line = '' # next input line: @@ -351,7 +350,7 @@ class RcFile(object) : # then add this input line: if line.endswith('\\') : # remove continuation character, add input line: - line = line[:-1]+inpline + line = line[:-1] + inpline else : # bright new line: line = inpline @@ -365,7 +364,7 @@ class RcFile(object) : # # line number and text for messages: - line_info = '%6i | %s' % (iline+1,line) + line_info = '%6i | %s' % (iline + 1, line) # # conditional settings (1) @@ -375,7 +374,7 @@ class RcFile(object) : mark = '#if' if line.startswith(mark) : # push temporary flag to stack, will be replaced after evaluation of condition: - ifstack.append( ( False, True, False, line_info ) ) + ifstack.append((False, True, False, line_info)) # debug ... #print 'xxx1 ', ifstack #endif @@ -384,14 +383,14 @@ class RcFile(object) : if line.startswith(mark) : # check ... if len(ifstack) == 0 : - logging.error( 'found orphin elif in rcfile on line :' ) - logging.error( ' %s' % line_info ) + logging.error('found orphin elif in rcfile on line :') + logging.error(' %s' % line_info) raise Exception #endif # remove current top from stack: - resolved,flag,anyflag,msg = ifstack.pop() + resolved, flag, anyflag, msg = ifstack.pop() # push temporary flag to stack, will be replaced after evaluation of condition: - ifstack.append( ( resolved, True, anyflag, line_info ) ) + ifstack.append((resolved, True, anyflag, line_info)) # debug ... #print 'xxx1 ', ifstack #endif @@ -400,26 +399,26 @@ class RcFile(object) : if line.startswith(mark) : # check ... if len(ifstack) == 0 : - logging.error( 'found lonely else in rcfile on line :' ) - logging.error( ' %s' % line_info ) + logging.error('found lonely else in rcfile on line :') + logging.error(' %s' % line_info) raise Exception #endif # remove current top from stack: - resolved,flag,anyflag,msg = ifstack.pop() + resolved, flag, anyflag, msg = ifstack.pop() # get higher level settings: if len(ifstack) > 0 : - resolved_prev,flag_prev,anyflag_prev,msg_prev = ifstack[-1] + resolved_prev, flag_prev, anyflag_prev, msg_prev = ifstack[-1] else : flag_prev = True #endif # should next lines be included ? new_flag = (not flag) and (not anyflag) and flag_prev # add else block with reversed flag, take into acount higher level - ifstack.append( ( resolved, new_flag, False, line_info ) ) + ifstack.append((resolved, new_flag, False, line_info)) # debug ... #print 'xxx1 ', ifstack # copy to output: - self.outfile.append( '%s\n' % line ) + self.outfile.append('%s\n' % line) # next input line: continue #endif @@ -429,14 +428,14 @@ class RcFile(object) : if line.startswith(mark) : # check ... if len(ifstack) == 0 : - logging.error( 'found lonely endif in rcfile on line :' ) - logging.error( ' %s' % line_info ) + logging.error('found lonely endif in rcfile on line :') + logging.error(' %s' % line_info) raise Exception #endif # remove top from stack: top = ifstack.pop() # copy to output: - self.outfile.append( '%s\n' % line ) + self.outfile.append('%s\n' % line) # next input line: continue #endif @@ -444,11 +443,11 @@ class RcFile(object) : # within if-statements ? if len(ifstack) > 0 : # extract current top: - resolved,flag,anyflag,msg = ifstack[-1] + resolved, flag, anyflag, msg = ifstack[-1] # already resolved ? then check if this line should be skipped: if resolved and (not flag) : # skip this line, but include commented version in output: - self.outfile.append( '!%s\n' % line ) + self.outfile.append('!%s\n' % line) # read the next input line: continue #endif @@ -462,11 +461,11 @@ class RcFile(object) : if line.startswith(mark): # info .. if not warned_for_eval : - if not silent: logging.warning( 'the #eval statements in rc-files are deprecated, use {key:value} pairs instead' ) + if not silent: logging.warning('the #eval statements in rc-files are deprecated, use {key:value} pairs instead') warned_for_eval = True #endif # add commented copy to output: - self.outfile.append( '!evaluated>>> '+line ) + self.outfile.append('!evaluated>>> ' + line) # remove leading mark: line = line.lstrip(mark) # multiple settings are seperated by ';' : @@ -474,9 +473,9 @@ class RcFile(object) : # insert in output file: for k in range(len(evals)) : # split in key and value: - key,value = evals[k].split('=') + key, value = evals[k].split('=') # insert: - self.outfile.append( '%s : %s' % (key,value) ) + self.outfile.append('%s : %s' % (key, value)) #endfor # next input line: continue @@ -487,17 +486,17 @@ class RcFile(object) : # # ensure that common marks are evaluated correctly: - start_mark = marks[0].replace('{','\{').replace('<','\<').replace('$','\$') - close_mark = marks[1].replace('}','\}').replace('>','\>') + start_mark = marks[0].replace('{', '\{').replace('<', '\<').replace('$', '\$') + close_mark = marks[1].replace('}', '\}').replace('>', '\>') # set syntax of keywords to be matched, e.g. '${...}' : - pattern = start_mark+'[A-Za-z0-9_.]+'+close_mark + pattern = start_mark + '[A-Za-z0-9_.]+' + close_mark # make a regular expression that matches all variables: - rc_varpat = re.compile( pattern ) + rc_varpat = re.compile(pattern) # search all matching paterns: - pats = re.findall(rc_varpat,line) + pats = re.findall(rc_varpat, line) # counter for unexpanded substitutions: ntobedone = 0 # loop over matches: @@ -509,28 +508,28 @@ class RcFile(object) : # get previously defined value: val = self.values[key] # substitute value: - line = line.replace(pat,val) + line = line.replace(pat, val) # set flag: something_done = True elif os.environ.has_key(key) : # get value from environment: val = os.environ[key] # substitute value: - line = line.replace(pat,val) + line = line.replace(pat, val) # set flag: something_done = True elif key == 'pid' : # special value: process id; convert to character: val = '%i' % os.getpid() # substitute value: - line = line.replace(pat,val) + line = line.replace(pat, val) # set flag: something_done = True elif key == 'script' : # special value: base name of the calling script, without extension: - script,ext = os.path.splitext(os.path.basename(sys.argv[0])) + script, ext = os.path.splitext(os.path.basename(sys.argv[0])) # substitute value: - line = line.replace(pat,script) + line = line.replace(pat, script) # set flag: something_done = True else : @@ -562,22 +561,22 @@ class RcFile(object) : inc_file = line.lstrip(mark).strip() # check ... if not os.path.exists(inc_file) : - inc_file = os.path.join(self.rootdir,inc_file) - logging.debug( 'Added rootdir to requested include: %s' % inc_file ) + inc_file = os.path.join(self.rootdir, inc_file) + logging.debug('Added rootdir to requested include: %s' % inc_file) if not os.path.exists(inc_file) : - logging.error( 'include file not found : %s' % inc_file ) - msg = 'ERROR - include file not found : %s' % inc_file - raise IOError,msg + logging.error('include file not found : %s' % inc_file) + msg = 'ERROR - include file not found : %s' % inc_file + raise IOError, msg #endif # read content: - inc_f = open( inc_file, 'r' ) + inc_f = open(inc_file, 'r') inc_rc = inc_f.readlines() inc_f.close() # add extra comment for output file: - self.outfile.append( '! >>> %s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n' % inc_file ) - self.outfile.extend( inc_rc ) - self.outfile.append( '! <<< %s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n' % inc_file ) + self.outfile.append('! >>> %s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n' % inc_file) + self.outfile.extend(inc_rc) + self.outfile.append('! <<< %s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n' % inc_file) # set flag: something_done = True # a new pass is needed: @@ -602,19 +601,19 @@ class RcFile(object) : if expression.endswith(':') : expression = expression.rstrip(':').strip() # evaluate: try : - flag = eval( expression ) + flag = eval(expression) except : - logging.error( 'could not evaluate expression:' ) - logging.error( ' %s' % expression ) - logging.error( 'on line:' ) - logging.error( line_info ) + logging.error('could not evaluate expression:') + logging.error(' %s' % expression) + logging.error('on line:') + logging.error(line_info) sys.exit(1) #endtry # remove temporary top added before during this pass: - tmp_statement,tmp_flag,tmp_anyflag,tmp_msg = ifstack.pop() + tmp_statement, tmp_flag, tmp_anyflag, tmp_msg = ifstack.pop() # extract current top if necessary: if len(ifstack) > 0 : - dummy_statement,prev_flag,dummy_anyflag,dummy_msg = ifstack[-1] + dummy_statement, prev_flag, dummy_anyflag, dummy_msg = ifstack[-1] else : prev_flag = True #endif @@ -623,11 +622,11 @@ class RcFile(object) : # any if/elif evaluated to true in this sequence ? new_anyflag = tmp_anyflag or new_flag # add to stack, now resolved, take into accout current flag: - ifstack.append( ( True, new_flag, new_anyflag, line_info ) ) + ifstack.append((True, new_flag, new_anyflag, line_info)) # debug ... #print 'xxx2 ', ifstack # copy to output: - self.outfile.append( '%s\n' % line ) + self.outfile.append('%s\n' % line) # next input line: continue #endif @@ -642,10 +641,10 @@ class RcFile(object) : # remove leading mark, what remains is error message: msg = line.lstrip(mark).strip() # display: - logging.error( msg ) + logging.error(msg) # add info: - logging.error( 'error message found on line:' ) - logging.error( line_info ) + logging.error('error message found on line:') + logging.error(line_info) # stop: raise Exception #endif @@ -656,18 +655,18 @@ class RcFile(object) : # common mistake ... if line.startswith('#') : - logging.error( 'line in rcfile starts with "#" but is not an "#include" or other special line;' ) - logging.error( 'if it is supposed to be comment, please start with "!" ...' ) - logging.error( ' rcfile : %s' % filename ) - logging.error( ' line : %s' % line ) + logging.error('line in rcfile starts with "#" but is not an "#include" or other special line;') + logging.error('if it is supposed to be comment, please start with "!" ...') + logging.error(' rcfile : %s' % filename) + logging.error(' line : %s' % line) raise IOError #endif # check ... if ':' not in line : - logging.error( 'key/value line should contain a ":"' ) - logging.error( ' rcfile : %s' % filename ) - logging.error( ' line : %s' % line ) + logging.error('key/value line should contain a ":"') + logging.error(' rcfile : %s' % filename) + logging.error(' line : %s' % line) raise IOError #endif @@ -676,7 +675,7 @@ class RcFile(object) : # # add line to output: - self.outfile.append( '%s\n' % line ) + self.outfile.append('%s\n' % line) # # add key/value pair @@ -685,21 +684,21 @@ class RcFile(object) : # not if inside an unresolved if-statement ... if len(ifstack) > 0 : # get top values: - resolved,flag,anyflag,msg = ifstack[-1] + resolved, flag, anyflag, msg = ifstack[-1] # not resolved yet ? then continue: if not resolved : continue #endif # split in key and value; # value might contain ':' too, so at maximum 1 split: - key,val = line.split(':',1) + key, val = line.split(':', 1) # remove comment from value: if '!' in val : # not if '\!' is in the value ... - if not '\!' in val : val,comment = val.split('!') + if not '\!' in val : val, comment = val.split('!') # replace all slash-comments: - val = val.replace('\!','!') + val = val.replace('\!', '!') #endif # remove spaces: @@ -710,9 +709,9 @@ class RcFile(object) : if self.values.has_key(key) : # no problem if values are the same, but otherwise ... if self.values[key] != val : - logging.error( 'key found twice in "%s" :' % filename ) - logging.error( ' %s : %s' % (key,str(self.values[key])) ) - logging.error( ' %s : %s' % (key,str(val)) ) + logging.error('key found twice in "%s" :' % filename) + logging.error(' %s : %s' % (key, str(self.values[key]))) + logging.error(' %s : %s' % (key, str(val))) raise Exception #endif else : @@ -739,9 +738,9 @@ class RcFile(object) : # check ... if len(ifstack) > 0 : - logging.error( 'unterminated if-statement ; current stack:' ) - for resolved,flag,anyflag,msg in ifstack : logging.error( msg ) - logging.error( 'please fix the rcfile or debug this script ...' ) + logging.error('unterminated if-statement ; current stack:') + for resolved, flag, anyflag, msg in ifstack : logging.error(msg) + logging.error('please fix the rcfile or debug this script ...') raise Exception #endif @@ -749,9 +748,9 @@ class RcFile(object) : if something_to_be_done : # check for unterminated loop ... if not something_done : - logging.error( 'could not resolve the following lines in rcfile:' ) - for uline in unresolved : logging.error( ' %s' % uline ) - logging.error( 'please fix the rcfile or debug this script ...' ) + logging.error('could not resolve the following lines in rcfile:') + for uline in unresolved : logging.error(' %s' % uline) + logging.error('please fix the rcfile or debug this script ...') raise Exception #endif else : @@ -761,7 +760,7 @@ class RcFile(object) : # for safety ... if ipass == 100 : - logging.error( 'resolving rc file has reached pass %i ; something wrong ?' % ipass ) + logging.error('resolving rc file has reached pass %i ; something wrong ?' % ipass) raise Exception #endif @@ -778,7 +777,7 @@ class RcFile(object) : # *** - def has_key( self, key ) : + def has_key(self, key) : # from dictionairy: return self.values.has_key(key) @@ -789,7 +788,7 @@ class RcFile(object) : # *** - def keys( self ) : + def keys(self) : # from dictionairy: return self.values.keys() @@ -800,7 +799,7 @@ class RcFile(object) : # *** - def get( self, key, totype='', default=None, verbose=False ) : + def get(self, key, totype='', default=None, verbose=False) : """ rcf.get( 'my.value' [,default=None] ) @@ -816,9 +815,6 @@ class RcFile(object) : for other values, an exception will be raised. """ - # external: - import logging - # element found ? if self.values.has_key(key) : # copy value: @@ -826,30 +822,30 @@ class RcFile(object) : # convert ? if totype == 'bool' : # convert to boolean: - if value in ['T','True','yes','1'] : + if value in ['T', 'True', 'yes', '1'] : value = True - elif value in ['F','False','no','0'] : + elif value in ['F', 'False', 'no', '0'] : value = False else : - logging.error( "value of key '%s' is not a boolean : %s" % (key,str(value)) ) + logging.error("value of key '%s' is not a boolean : %s" % (key, str(value))) raise Exception #endif elif len(totype) > 0 : # convert to other type ... - value = eval( '%s(%s)' % (totype,value) ) + value = eval('%s(%s)' % (totype, value)) #endif # for debugging ... - if verbose : logging.debug( 'rc setting "%s" : "%s"' % (key,str(value)) ) + if verbose : logging.debug('rc setting "%s" : "%s"' % (key, str(value))) else : # default value specified ? if default != None : # copy default: value = default # for debugging ... - if verbose : logging.debug( 'rc setting "%s" : "%s" (deault)' % (key,str(value)) ) + if verbose : logging.debug('rc setting "%s" : "%s" (deault)' % (key, str(value))) else : # something wrong ... - logging.error( "key '%s' not found in '%s' and no default specified" % (key,self.filename) ) + logging.error("key '%s' not found in '%s' and no default specified" % (key, self.filename)) raise Exception #endif #endif @@ -863,15 +859,12 @@ class RcFile(object) : # *** - def replace( self, key, val ) : + def replace(self, key, val) : """ Replace a key by a new value. """ - # external: - import logging - # search for a line '<key> : <val>' # loop over lines in output file: found = False @@ -881,11 +874,11 @@ class RcFile(object) : # skip lines that are no key:value pair for sure ... if ':' not in line : continue # split once at first ':' - k,v = line.split(':',1) + k, v = line.split(':', 1) # match ? if k.strip() == key : # replace line in original file: - self.outfile[iline] = '%s : %s\n' % (k,str(val)) + self.outfile[iline] = '%s : %s\n' % (k, str(val)) # replace value: self.values[key] = val # set flag: @@ -896,7 +889,7 @@ class RcFile(object) : #endfor # lines # not found ? if not found : - logging.error( 'could not replace key : %s' % key ) + logging.error('could not replace key : %s' % key) raise Exception #endif @@ -909,14 +902,14 @@ class RcFile(object) : # *** - def add( self, key, val, comment='' ) : + def add(self, key, val, comment='') : """Add a new key/value pair.""" # add lines: - self.outfile.append( '\n' ) - if len(comment) > 0 : self.outfile.append( '! %s\n' % comment ) - self.outfile.append( '%s : %s\n' % (key,str(val)) ) + self.outfile.append('\n') + if len(comment) > 0 : self.outfile.append('! %s\n' % comment) + self.outfile.append('%s : %s\n' % (key, str(val))) # add to dictionairy: self.values[key] = val @@ -930,7 +923,7 @@ class RcFile(object) : # *** - def substitute( self, line, marks=('${','}') ) : + def substitute(self, line, marks=('${', '}')) : """ Return a line with all '${..}' parts replaced by the corresponding rcfile values. @@ -939,21 +932,18 @@ class RcFile(object) : <mark1>...<mark2> """ - # external: - import re - # ensure that common marks are evaluated correctly: - start_mark = marks[0].replace('{','\{').replace('<','\<').replace('$','\$') - close_mark = marks[1].replace('}','\}').replace('>','\>') + start_mark = marks[0].replace('{', '\{').replace('<', '\<').replace('$', '\$') + close_mark = marks[1].replace('}', '\}').replace('>', '\>') # set syntax of keywords to be matched, e.g. '${...}' : - pattern = start_mark+'[A-Za-z0-9_.]+'+close_mark + pattern = start_mark + '[A-Za-z0-9_.]+' + close_mark # make a regular expression that matches all variables: - rc_varpat = re.compile( pattern ) + rc_varpat = re.compile(pattern) # search all matching paterns: - pats = re.findall(rc_varpat,line) + pats = re.findall(rc_varpat, line) # loop over matches: for pat in pats : # remove enclosing characters: @@ -963,7 +953,7 @@ class RcFile(object) : # get previously defined value: val = self.values[key] # substitute value: - line = line.replace(pat,val) + line = line.replace(pat, val) #endif #endfor # matched patterns @@ -976,12 +966,12 @@ class RcFile(object) : # *** - def WriteFile( self, filename ) : + def WriteFile(self, filename) : """ write the dictionary to file""" # open file for writing: - f = open(filename,'w') + f = open(filename, 'w') ## loop over key/value pairs: #for k,v in self.iteritems(): @@ -991,7 +981,7 @@ class RcFile(object) : ##endfor # write processed input: - f.writelines( self.outfile ) + f.writelines(self.outfile) # close file: f.close() @@ -1001,30 +991,30 @@ class RcFile(object) : #endclass # RcFile -def read(rcfilename,silent=False): +def read(rcfilename, silent=False): """ This method reads an rc-file by making an instance of the RcFile class, and then returns the dictionary of values only. This makes it backwards compatible with older implementations of the rc.py module """ - rcdict = RcFile(rcfilename,silent=silent) + rcdict = RcFile(rcfilename, silent=silent) return rcdict.values -def write( filename, rcdict): +def write(filename, rcdict): """ This method writes an rc-file dictionary. This is included to make this module backwards compatible with older implementations of the rc.py module """ # open file for writing: - f = open(filename,'w') + f = open(filename, 'w') # loop over key/value pairs: - for k,v in rcdict.items(): + for k, v in rcdict.items(): # add line; at least the specified number of characters # is used for the key: - f.write( '%-20s:%s\n' % (k,v) ) + f.write('%-20s:%s\n' % (k, v)) #endfor # close file: @@ -1040,9 +1030,9 @@ def write( filename, rcdict): if __name__ == '__main__': # external ... - import sys + import optparse - import logging + # extract arguments from sys.argv array: # 0 = name of calling script, 1: = actual arguments @@ -1055,21 +1045,21 @@ if __name__ == '__main__': parser = optparse.OptionParser(usage=usage) # define options: - parser.add_option( "-d", "--doc", + parser.add_option("-d", "--doc", help="print documentation", - dest="doc", action="store_true", default=False ) - parser.add_option( "-b", "--bool", + dest="doc", action="store_true", default=False) + parser.add_option("-b", "--bool", help="return 'True' for values 'T', 'True', 'yes', or '1', and 'False' for 'F', 'False', 'no', or '0'", - dest="boolean", action="store_true", default=False ) - parser.add_option( "--default", + dest="boolean", action="store_true", default=False) + parser.add_option("--default", help="default value returned if key is not found", - dest="default", action="store" ) - parser.add_option( "-w", "--write", + dest="default", action="store") + parser.add_option("-w", "--write", help="write pre-processed rcfile", - dest="write", action="store_true", default=False ) + dest="write", action="store_true", default=False) # now parse the actual arguments: - opts,args = parser.parse_args( args=args ) + opts, args = parser.parse_args(args=args) # print documentation ? if opts.doc : @@ -1088,7 +1078,7 @@ if __name__ == '__main__': try : rcf = RcFile(rcfile) except : - logging.error( sys.exc_info()[1] ) + logging.error(sys.exc_info()[1]) sys.exit(1) #endtry @@ -1103,7 +1093,7 @@ if __name__ == '__main__': parser.error("no name of rckey provided\n") #endif # extract: - rckey = args[1] + rckey = args[1] # key present ? if rcf.has_key(rckey) : @@ -1111,7 +1101,7 @@ if __name__ == '__main__': # print requested value: if opts.boolean : # extract value: - flag = rcf.get(rckey,'bool') + flag = rcf.get(rckey, 'bool') # print result: if flag : print 'True' @@ -1132,7 +1122,7 @@ if __name__ == '__main__': # display: print opts.default else : - print 'ERROR - key "%s" not found in rcfile "%s" and no default specified' % (rckey,rcfile) + print 'ERROR - key "%s" not found in rcfile "%s" and no default specified' % (rckey, rcfile) sys.exit(1) #endif diff --git a/da/tools/rcn.py b/da/tools/rcn.py index 8e9c742..29d646c 100755 --- a/da/tools/rcn.py +++ b/da/tools/rcn.py @@ -233,20 +233,24 @@ HISTORY Identify duplicate keys by checking on different source lines instead of checking if the values are different. """ - +import re +import os +import sys +import logging + # ------------------------------------------------ # classes # ------------------------------------------------ -class RcFile( object ) : +class RcFile(object) : """ Class to store settings read from a rcfile. """ - def __init__( self, filename, silent=False, marks=('${','}') ) : + def __init__(self, filename, silent=False, marks=('${', '}')) : """ @@ -262,14 +266,8 @@ class RcFile( object ) : """ - # external: - import re - import os - import sys - import logging - # info ... - logging.debug( 'reading rcfile %s ...' % filename ) + logging.debug('reading rcfile %s ...' % filename) # check ... if not os.path.exists(filename) : @@ -292,7 +290,7 @@ class RcFile( object ) : self.sources = {} # open the specified rc-file: - f = open(filename,'r') + f = open(filename, 'r') # store all lines in a list: inpfile = f.readlines() # close: @@ -301,7 +299,7 @@ class RcFile( object ) : # create traceback info: inptrace = [] for jline in range(len(inpfile)) : - inptrace.append( '"%s", line %-10s' % (filename,str(jline+1)) ) + inptrace.append('"%s", line %-10s' % (filename, str(jline + 1))) #endfor # flags: @@ -320,15 +318,15 @@ class RcFile( object ) : # init current line: line = '' # assume nothing has to be done after this loop: - something_done = False - something_to_be_done = False + something_done = False + something_to_be_done = False # maintain list with unresolved lines (with keys that could not be evaluated yet): - unresolved_lines = [] + unresolved_lines = [] # maintain list with keys from which the value could not be resolved yet: keys_with_unresolved_value = [] # maintain list with undefined keys; # some might be part of the keys_with_unresolved_value list: - undefined_keys = [] + undefined_keys = [] # stack for conditional evaluation; # each element is a tuple with elements: @@ -357,14 +355,14 @@ class RcFile( object ) : # set full traceback info: if line.endswith('\\') : # current input line is a continuation; combine: - qfile,qlinenrs = linetrace.split(',') - qnrs = qlinenrs.replace('lines','').replace('line','') + qfile, qlinenrs = linetrace.split(',') + qnrs = qlinenrs.replace('lines', '').replace('line', '') if '-' in qnrs : - qnr1,qnr2 = qnrs.split('-') + qnr1, qnr2 = qnrs.split('-') else : - qnr1,qnr2 = qnrs,qnrs + qnr1, qnr2 = qnrs, qnrs #endif - linetrace = '%s, lines %-9s' % ( qfile, '%i-%i' % (int(qnr1),int(qnr2)+1) ) + linetrace = '%s, lines %-9s' % (qfile, '%i-%i' % (int(qnr1), int(qnr2) + 1)) else : # just copy: linetrace = linetrace_curr @@ -386,7 +384,7 @@ class RcFile( object ) : # add empty line to output: self.outfile.append('\n') # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # next will be a new line: line = '' # next input line: @@ -400,9 +398,9 @@ class RcFile( object ) : # skip comment: if inpline.startswith('!') : # add copy to output file: - self.outfile.append( '%s\n' % inpline ) + self.outfile.append('%s\n' % inpline) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # next will be a new line: line = '' # next input line: @@ -417,7 +415,7 @@ class RcFile( object ) : # then add this input line: if line.endswith('\\') : # remove continuation character, add input line: - line = line[:-1]+inpline + line = line[:-1] + inpline else : # bright new line: line = inpline @@ -437,7 +435,7 @@ class RcFile( object ) : mark = '#if' if line.startswith(mark) : # push temporary flag to stack, will be replaced after evaluation of condition: - ifstack.append( ( False, True, False, linetrace ) ) + ifstack.append((False, True, False, linetrace)) # debug ... #print 'xxx1 ', ifstack #endif @@ -446,24 +444,24 @@ class RcFile( object ) : if line.startswith(mark) : # check ... if len(ifstack) == 0 : - logging.error( 'found orphin "%s" in %s' % (mark,linetrace) ) + logging.error('found orphin "%s" in %s' % (mark, linetrace)) raise Exception #endif # remove current top from stack: - resolved,flag,anyflag,msg = ifstack.pop() + resolved, flag, anyflag, msg = ifstack.pop() # did one of the previous #if/#elif evaluate to True already ? if resolved and anyflag : # push to stack that this line resolved to False : - ifstack.append( ( True, False, anyflag, linetrace ) ) + ifstack.append((True, False, anyflag, linetrace)) # copy to output: - self.outfile.append( '%s\n' % line ) + self.outfile.append('%s\n' % line) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # next input line: continue else : # push temporary flag to stack, will be replaced after evaluation of condition: - ifstack.append( ( False, True, anyflag, linetrace ) ) + ifstack.append((False, True, anyflag, linetrace)) #endif ## debug ... #print 'xxx2 ', ifstack @@ -473,14 +471,14 @@ class RcFile( object ) : if line.startswith(mark) : # check ... if len(ifstack) == 0 : - logging.error( 'found orphin "%s" in %s' % (mark,linetrace) ) + logging.error('found orphin "%s" in %s' % (mark, linetrace)) raise Exception #endif # remove current top from stack: - resolved,flag,anyflag,msg = ifstack.pop() + resolved, flag, anyflag, msg = ifstack.pop() # get higher level settings: if len(ifstack) > 0 : - resolved_prev,flag_prev,anyflag_prev,msg_prev = ifstack[-1] + resolved_prev, flag_prev, anyflag_prev, msg_prev = ifstack[-1] else : flag_prev = True #endif @@ -488,13 +486,13 @@ class RcFile( object ) : # reverse flag, take into acount higher level: new_flag = (not flag) and (not anyflag) and flag_prev # push to stack: - ifstack.append( ( resolved, new_flag, False, linetrace ) ) + ifstack.append((resolved, new_flag, False, linetrace)) # debug ... #print 'xxx3 ', ifstack # copy to output: - self.outfile.append( '%s\n' % line ) + self.outfile.append('%s\n' % line) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # next input line: continue #endif @@ -504,15 +502,15 @@ class RcFile( object ) : if line.startswith(mark) : # check ... if len(ifstack) == 0 : - logging.error( 'found orphin "%s" in %s' % (mark,linetrace) ) + logging.error('found orphin "%s" in %s' % (mark, linetrace)) raise Exception #endif # remove top from stack: top = ifstack.pop() # copy to output: - self.outfile.append( '%s\n' % line ) + self.outfile.append('%s\n' % line) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # next input line: continue #endif @@ -520,13 +518,13 @@ class RcFile( object ) : # within if-statements ? if len(ifstack) > 0 : # extract current top: - resolved,flag,anyflag,msg = ifstack[-1] + resolved, flag, anyflag, msg = ifstack[-1] # already resolved ? then check if this line should be skipped: if resolved and (not flag) : # skip this line, but include commented version in output: - self.outfile.append( '!%s\n' % line ) + self.outfile.append('!%s\n' % line) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # read the next input line: continue #endif @@ -540,13 +538,13 @@ class RcFile( object ) : if line.startswith(mark): # info .. if not warned_for_eval : - if not silent: logging.warning( 'the #eval statements in rc-files are deprecated, use {key:value} pairs instead' ) + if not silent: logging.warning('the #eval statements in rc-files are deprecated, use {key:value} pairs instead') warned_for_eval = True #endif # add commented copy to output: - self.outfile.append( '!evaluated>>> '+line ) + self.outfile.append('!evaluated>>> ' + line) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # remove leading mark: line = line.lstrip(mark) # multiple settings are seperated by ';' : @@ -554,11 +552,11 @@ class RcFile( object ) : # insert in output file: for k in range(len(evals)) : # split in key and value: - key,value = evals[k].split('=') + key, value = evals[k].split('=') # insert: - self.outfile.append( '%s : %s' % (key,value) ) + self.outfile.append('%s : %s' % (key, value)) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) #endfor # next input line: continue @@ -569,17 +567,17 @@ class RcFile( object ) : # # ensure that common marks are evaluated correctly: - start_mark = marks[0].replace('{','\{').replace('<','\<').replace('$','\$') - close_mark = marks[1].replace('}','\}').replace('>','\>') + start_mark = marks[0].replace('{', '\{').replace('<', '\<').replace('$', '\$') + close_mark = marks[1].replace('}', '\}').replace('>', '\>') # set syntax of keywords to be matched, e.g. '${...}' : - pattern = start_mark+'[A-Za-z0-9_.]+'+close_mark + pattern = start_mark + '[A-Za-z0-9_.]+' + close_mark # make a regular expression that matches all variables: - rc_varpat = re.compile( pattern ) + rc_varpat = re.compile(pattern) # search all matching paterns: - pats = re.findall(rc_varpat,line) + pats = re.findall(rc_varpat, line) # counter for unexpanded substitutions: ntobedone = 0 # loop over matches: @@ -591,28 +589,28 @@ class RcFile( object ) : # get previously defined value: val = self.values[key] # substitute value: - line = line.replace(pat,val) + line = line.replace(pat, val) # set flag: something_done = True elif os.environ.has_key(key) : # get value from environment: val = os.environ[key] # substitute value: - line = line.replace(pat,val) + line = line.replace(pat, val) # set flag: something_done = True elif key == 'pid' : # special value: process id; convert to character: val = '%i' % os.getpid() # substitute value: - line = line.replace(pat,val) + line = line.replace(pat, val) # set flag: something_done = True elif key == 'script' : # special value: base name of the calling script, without extension: - script,ext = os.path.splitext(os.path.basename(sys.argv[0])) + script, ext = os.path.splitext(os.path.basename(sys.argv[0])) # substitute value: - line = line.replace(pat,script) + line = line.replace(pat, script) # set flag: something_done = True else : @@ -629,15 +627,15 @@ class RcFile( object ) : # add line to output: self.outfile.append(line) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # a new pass is needed: something_to_be_done = True # store for info messages: - unresolved_lines.append( '%s | %s' % (linetrace,line) ) + unresolved_lines.append('%s | %s' % (linetrace, line)) # could this be a 'key : value' line ? if ':' in line : # split, remove leading and end space: - qkey,qvalue = line.split(':',1) + qkey, qvalue = line.split(':', 1) qkey = qkey.strip() # assume it is indeed a key if it does not contain whitespace, # no start mark, and does not start wiht '#' : @@ -659,28 +657,28 @@ class RcFile( object ) : inc_file = line.lstrip(mark).strip() # check ... if not os.path.exists(inc_file) : - inc_file = os.path.join(self.rootdir,inc_file) - logging.debug( 'Added rootdir to requested include: %s' % inc_file ) + inc_file = os.path.join(self.rootdir, inc_file) + logging.debug('Added rootdir to requested include: %s' % inc_file) #endif if not os.path.exists(inc_file) : - logging.error( 'include file not found : %s' % inc_file ) - logging.error( linetrace ) + logging.error('include file not found : %s' % inc_file) + logging.error(linetrace) raise IOError, 'include file not found : %s' % inc_file #endif # read content: - inc_f = open( inc_file, 'r' ) + inc_f = open(inc_file, 'r') inc_rc = inc_f.readlines() inc_f.close() # add extra comment for output file: - self.outfile.append( '! >>> %s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n' % inc_file ) - self.outfile.extend( inc_rc ) - self.outfile.append( '! <<< %s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n' % inc_file ) + self.outfile.append('! >>> %s >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n' % inc_file) + self.outfile.extend(inc_rc) + self.outfile.append('! <<< %s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n' % inc_file) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) for jline in range(len(inc_rc)) : - self.trace.append( '"%s", line %-10s' % (inc_file,str(jline+1)) ) + self.trace.append('"%s", line %-10s' % (inc_file, str(jline + 1))) #endfor - self.trace.append( linetrace ) + self.trace.append(linetrace) # set flag: something_done = True # a new pass is needed: @@ -705,18 +703,18 @@ class RcFile( object ) : if expression.endswith(':') : expression = expression.rstrip(':').strip() # evaluate: try : - flag = eval( expression ) + flag = eval(expression) except : - logging.error( 'could not evaluate expression:' ) - logging.error( ' %s' % expression ) - logging.error( 'in %s' % linetrace ) + logging.error('could not evaluate expression:') + logging.error(' %s' % expression) + logging.error('in %s' % linetrace) raise Exception #endtry # remove temporary top added before during this pass: - tmp_statement,tmp_flag,tmp_anyflag,tmp_msg = ifstack.pop() + tmp_statement, tmp_flag, tmp_anyflag, tmp_msg = ifstack.pop() # extract current top if necessary: if len(ifstack) > 0 : - dummy_statement,prev_flag,dummy_anyflag,dummy_msg = ifstack[-1] + dummy_statement, prev_flag, dummy_anyflag, dummy_msg = ifstack[-1] else : prev_flag = True #endif @@ -725,13 +723,13 @@ class RcFile( object ) : # any if/elif evaluated to true in this sequence ? new_anyflag = tmp_anyflag or new_flag # add to stack, now resolved, take into accout current flag: - ifstack.append( ( True, new_flag, new_anyflag, linetrace ) ) + ifstack.append((True, new_flag, new_anyflag, linetrace)) # debug ... #print 'xxx2 ', ifstack # copy to output: - self.outfile.append( '%s\n' % line ) + self.outfile.append('%s\n' % line) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # next input line: continue #endif @@ -746,9 +744,9 @@ class RcFile( object ) : # remove leading mark, what remains is error message: msg = line.lstrip(mark).strip() # display: - logging.error( msg ) + logging.error(msg) # add info: - logging.error( 'error message in %s' % linetrace ) + logging.error('error message in %s' % linetrace) # stop: raise Exception #endif @@ -759,17 +757,17 @@ class RcFile( object ) : # common mistake ... if line.startswith('#') : - logging.error( 'line in rcfile starts with "#" but is not an "#include" or other special line;' ) - logging.error( 'if it is supposed to be comment, please start with "!" ...' ) - logging.error( ' %s' % line ) - logging.error( '%s' % linetrace ) + logging.error('line in rcfile starts with "#" but is not an "#include" or other special line;') + logging.error('if it is supposed to be comment, please start with "!" ...') + logging.error(' %s' % line) + logging.error('%s' % linetrace) raise IOError #endif # check ... if ':' not in line : - logging.error( 'key/value line should contain a ":"' ) - logging.error( '%s' % linetrace ) + logging.error('key/value line should contain a ":"') + logging.error('%s' % linetrace) raise IOError #endif @@ -778,9 +776,9 @@ class RcFile( object ) : # # add line to output: - self.outfile.append( '%s\n' % line ) + self.outfile.append('%s\n' % line) # add traceback info: - self.trace.append( linetrace ) + self.trace.append(linetrace) # # add key/value pair @@ -789,21 +787,21 @@ class RcFile( object ) : # not if inside an unresolved if-statement ... if len(ifstack) > 0 : # get top values: - resolved,flag,anyflag,msg = ifstack[-1] + resolved, flag, anyflag, msg = ifstack[-1] # not resolved yet ? then continue: if not resolved : continue #endif # split in key and value; # value might contain ':' too, so at maximum 1 split: - key,val = line.split(':',1) + key, val = line.split(':', 1) # remove comment from value: if '!' in val : # not if '\!' is in the value ... - if not '\!' in val : val,comment = val.split('!') + if not '\!' in val : val, comment = val.split('!') # replace all slash-comments: - val = val.replace('\!','!') + val = val.replace('\!', '!') #endif # remove spaces: @@ -817,11 +815,11 @@ class RcFile( object ) : # therefore, only complain if this definition is read # from a different line : if linetrace != self.sources[key] : - logging.error( 'duplicated key \"%s\" found:' % key) - logging.error( 'first definition in %s is:' % self.sources[key]) - logging.error( ' %s : %s' % (key,str(self.values[key])) ) - logging.error( 'second definition in %s is:' % linetrace.strip() ) - logging.error( ' %s : %s' % (key,str(val)) ) + logging.error('duplicated key \"%s\" found:' % key) + logging.error('first definition in %s is:' % self.sources[key]) + logging.error(' %s : %s' % (key, str(self.values[key]))) + logging.error('second definition in %s is:' % linetrace.strip()) + logging.error(' %s : %s' % (key, str(val))) raise Exception #endif else : @@ -849,9 +847,9 @@ class RcFile( object ) : # check ... if len(ifstack) > 0 : - logging.error( 'unterminated if-statement ; current stack:' ) - for resolved,flag,anyflag,msg in ifstack : logging.error( msg ) - logging.error( 'please fix the rcfile or debug this script ...' ) + logging.error('unterminated if-statement ; current stack:') + for resolved, flag, anyflag, msg in ifstack : logging.error(msg) + logging.error('please fix the rcfile or debug this script ...') raise Exception #endif @@ -860,32 +858,32 @@ class RcFile( object ) : # check for unterminated loop ... if not something_done : # list all unresolved lines: - logging.error( 'Could not resolve the following lines in rcfile(s):' ) - logging.error( '' ) + logging.error('Could not resolve the following lines in rcfile(s):') + logging.error('') for uline in unresolved_lines : - logging.error( ' %s' % uline ) + logging.error(' %s' % uline) #endfor - logging.error( '' ) + logging.error('') # list all undefined keys: - logging.error( ' Undefined key(s):' ) - logging.error( '' ) + logging.error(' Undefined key(s):') + logging.error('') for ukey in undefined_keys : # do not list them if they are undefined because the value # depends on other undefined keys: if ukey not in keys_with_unresolved_value : # display: - logging.error( ' %s' % ukey ) + logging.error(' %s' % ukey) # loop over unresolved lines to see in which the key is used: for uline in unresolved_lines : # search for '${key}' pattern: - if marks[0]+ukey+marks[1] in uline : - logging.error( ' %s' % uline ) + if marks[0] + ukey + marks[1] in uline : + logging.error(' %s' % uline) #endif #endfor - logging.error( '' ) + logging.error('') #endif #endfor - logging.error( 'please fix the rcfile(s) or debug this script ...' ) + logging.error('please fix the rcfile(s) or debug this script ...') raise Exception #endif else : @@ -895,7 +893,7 @@ class RcFile( object ) : # for safety ... if ipass == 100 : - logging.error( 'resolving rc file has reached pass %i ; something wrong ?' % ipass ) + logging.error('resolving rc file has reached pass %i ; something wrong ?' % ipass) raise Exception #endif @@ -914,7 +912,7 @@ class RcFile( object ) : # *** - def has_key( self, key ) : + def has_key(self, key) : # from dictionairy: return self.values.has_key(key) @@ -925,7 +923,7 @@ class RcFile( object ) : # *** - def keys( self ) : + def keys(self) : # from dictionairy: return self.values.keys() @@ -936,7 +934,7 @@ class RcFile( object ) : # *** - def get( self, key, totype='', default=None, verbose=False ) : + def get(self, key, totype='', default=None, verbose=False) : """ rcf.get( 'my.value' [,default=None] ) @@ -952,9 +950,6 @@ class RcFile( object ) : for other values, an exception will be raised. """ - # external: - import logging - # element found ? if self.values.has_key(key) : # copy value: @@ -962,30 +957,30 @@ class RcFile( object ) : # convert ? if totype == 'bool' : # convert to boolean: - if value in ['T','True','yes','1'] : + if value in ['T', 'True', 'yes', '1'] : value = True - elif value in ['F','False','no','0'] : + elif value in ['F', 'False', 'no', '0'] : value = False else : - logging.error( "value of key '%s' is not a boolean : %s" % (key,str(value)) ) + logging.error("value of key '%s' is not a boolean : %s" % (key, str(value))) raise Exception #endif elif len(totype) > 0 : # convert to other type ... - value = eval( '%s(%s)' % (totype,value) ) + value = eval('%s(%s)' % (totype, value)) #endif # for debugging ... - if verbose : logging.debug( 'rc setting "%s" : "%s"' % (key,str(value)) ) + if verbose : logging.debug('rc setting "%s" : "%s"' % (key, str(value))) else : # default value specified ? if default != None : # copy default: value = default # for debugging ... - if verbose : logging.debug( 'rc setting "%s" : "%s" (deault)' % (key,str(value)) ) + if verbose : logging.debug('rc setting "%s" : "%s" (deault)' % (key, str(value))) else : # something wrong ... - logging.error( "key '%s' not found in '%s' and no default specified" % (key,self.filename) ) + logging.error("key '%s' not found in '%s' and no default specified" % (key, self.filename)) raise Exception #endif #endif @@ -999,15 +994,12 @@ class RcFile( object ) : # *** - def replace( self, key, val ) : + def replace(self, key, val) : """ Replace a key by a new value. """ - # external: - import logging - # search for a line '<key> : <val>' # loop over lines in output file: found = False @@ -1017,11 +1009,11 @@ class RcFile( object ) : # skip lines that are no key:value pair for sure ... if ':' not in line : continue # split once at first ':' - k,v = line.split(':',1) + k, v = line.split(':', 1) # match ? if k.strip() == key : # replace line in original file: - self.outfile[iline] = '%s : %s\n' % (k,str(val)) + self.outfile[iline] = '%s : %s\n' % (k, str(val)) # replace value: self.values[key] = val # set flag: @@ -1032,7 +1024,7 @@ class RcFile( object ) : #endfor # lines # not found ? if not found : - logging.error( 'could not replace key : %s' % key ) + logging.error('could not replace key : %s' % key) raise Exception #endif @@ -1045,14 +1037,14 @@ class RcFile( object ) : # *** - def add( self, key, val, comment='' ) : + def add(self, key, val, comment='') : """Add a new key/value pair.""" # add lines: - self.outfile.append( '\n' ) - if len(comment) > 0 : self.outfile.append( '! %s\n' % comment ) - self.outfile.append( '%s : %s\n' % (key,str(val)) ) + self.outfile.append('\n') + if len(comment) > 0 : self.outfile.append('! %s\n' % comment) + self.outfile.append('%s : %s\n' % (key, str(val))) # add to dictionairy: self.values[key] = val @@ -1066,7 +1058,7 @@ class RcFile( object ) : # *** - def substitute( self, line, marks=('${','}') ) : + def substitute(self, line, marks=('${', '}')) : """ Return a line with all '${..}' parts replaced by the corresponding rcfile values. @@ -1075,21 +1067,18 @@ class RcFile( object ) : <mark1>...<mark2> """ - # external: - import re - # ensure that common marks are evaluated correctly: - start_mark = marks[0].replace('{','\{').replace('<','\<').replace('$','\$') - close_mark = marks[1].replace('}','\}').replace('>','\>') + start_mark = marks[0].replace('{', '\{').replace('<', '\<').replace('$', '\$') + close_mark = marks[1].replace('}', '\}').replace('>', '\>') # set syntax of keywords to be matched, e.g. '${...}' : - pattern = start_mark+'[A-Za-z0-9_.]+'+close_mark + pattern = start_mark + '[A-Za-z0-9_.]+' + close_mark # make a regular expression that matches all variables: - rc_varpat = re.compile( pattern ) + rc_varpat = re.compile(pattern) # search all matching paterns: - pats = re.findall(rc_varpat,line) + pats = re.findall(rc_varpat, line) # loop over matches: for pat in pats : # remove enclosing characters: @@ -1099,7 +1088,7 @@ class RcFile( object ) : # get previously defined value: val = self.values[key] # substitute value: - line = line.replace(pat,val) + line = line.replace(pat, val) #endif #endfor # matched patterns @@ -1112,12 +1101,12 @@ class RcFile( object ) : # *** - def WriteFile( self, filename ) : + def WriteFile(self, filename) : """ write the dictionary to file""" # open file for writing: - f = open(filename,'w') + f = open(filename, 'w') ## loop over key/value pairs: #for k,v in self.iteritems(): @@ -1127,7 +1116,7 @@ class RcFile( object ) : ##endfor # write processed input: - f.writelines( self.outfile ) + f.writelines(self.outfile) # close file: f.close() @@ -1141,7 +1130,7 @@ class RcFile( object ) : # *** -def read( rcfilename, silent=False ) : +def read(rcfilename, silent=False) : """ This method reads an rc-file by making an instance of the RcFile class, @@ -1149,7 +1138,7 @@ def read( rcfilename, silent=False ) : This makes it backwards compatible with older implementations of the rc.py module """ - rcdict = RcFile( rcfilename, silent=silent ) + rcdict = RcFile(rcfilename, silent=silent) return rcdict.values @@ -1159,7 +1148,7 @@ def read( rcfilename, silent=False ) : # *** -def write( filename, rcdict ) : +def write(filename, rcdict) : """ This method writes an rc-file dictionary. @@ -1167,13 +1156,13 @@ def write( filename, rcdict ) : """ # open file for writing: - f = open(filename,'w') + f = open(filename, 'w') # loop over key/value pairs: - for k,v in rcdict.items(): + for k, v in rcdict.items(): # add line; at least the specified number of characters # is used for the key: - f.write( '%-20s:%s\n' % (k,v) ) + f.write('%-20s:%s\n' % (k, v)) #endfor # close file: @@ -1191,9 +1180,7 @@ def write( filename, rcdict ) : if __name__ == '__main__': # external ... - import sys import optparse - import logging import traceback # extract arguments from sys.argv array: @@ -1207,24 +1194,24 @@ if __name__ == '__main__': parser = optparse.OptionParser(usage=usage) # define options: - parser.add_option( "-d", "--doc", + parser.add_option("-d", "--doc", help="print documentation", - dest="doc", action="store_true", default=False ) - parser.add_option( "-v", "--verbose", + dest="doc", action="store_true", default=False) + parser.add_option("-v", "--verbose", help="print information messages", - dest="verbose", action="store_true", default=False ) - parser.add_option( "-b", "--bool", + dest="verbose", action="store_true", default=False) + parser.add_option("-b", "--bool", help="return 'True' for values 'T', 'True', 'yes', or '1', and 'False' for 'F', 'False', 'no', or '0'", - dest="boolean", action="store_true", default=False ) - parser.add_option( "--default", + dest="boolean", action="store_true", default=False) + parser.add_option("--default", help="default value returned if key is not found", - dest="default", action="store" ) - parser.add_option( "-w", "--write", + dest="default", action="store") + parser.add_option("-w", "--write", help="write pre-processed rcfile", - dest="write", action="store_true", default=False ) + dest="write", action="store_true", default=False) # now parse the actual arguments: - opts,args = parser.parse_args( args=args ) + opts, args = parser.parse_args(args=args) # print documentation ? if opts.doc : @@ -1241,9 +1228,9 @@ if __name__ == '__main__': # read rcfile in dictionary: try : - rcf = RcFile( rcfile, silent=(not opts.verbose) ) + rcf = RcFile(rcfile, silent=(not opts.verbose)) except : - if opts.verbose : logging.error( traceback.format_exc() ) + if opts.verbose : logging.error(traceback.format_exc()) sys.exit(1) #endtry @@ -1258,7 +1245,7 @@ if __name__ == '__main__': parser.error("no name of rckey provided\n") #endif # extract: - rckey = args[1] + rckey = args[1] # key present ? if rcf.has_key(rckey) : @@ -1266,7 +1253,7 @@ if __name__ == '__main__': # print requested value: if opts.boolean : # extract value: - flag = rcf.get(rckey,'bool') + flag = rcf.get(rckey, 'bool') # print result: if flag : print 'True' @@ -1287,7 +1274,7 @@ if __name__ == '__main__': # display: print opts.default else : - print 'ERROR - key "%s" not found in rcfile "%s" and no default specified' % (rckey,rcfile) + print 'ERROR - key "%s" not found in rcfile "%s" and no default specified' % (rckey, rcfile) sys.exit(1) #endif -- GitLab