source: TI04-geosplat/trunk/gss/scripts/addGeosplatTemplates.py @ 798

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI04-geosplat/trunk/gss/scripts/addGeosplatTemplates.py@798
Revision 798, 2.0 KB checked in by astephen, 14 years ago (diff)

Latest working version with install method.
Can accept more than one file but doesn't combine variables yet.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3"""
4addGeosplatTemplates.py
5=======================
6
7Adds all the GeoSPlAT VCS templates in this directory to the
8PCMDI_GRAPHICS/initial.attributes file specified as the only
9command-line argument.
10
11Usage:
12======
13
14    addGeosplatTemplates.py <initial_attributes_file>
15
16"""
17
18# import standard library modules
19import sys
20import os
21import re
22import glob
23import shutil
24
25templateDir="../plotTemplates"
26
27def getTemplateNames():
28    """
29    Function to return a list of the GeoSPlAT templates in the
30    local directory.
31    """
32    cwd=os.getcwd()
33    os.chdir(templateDir)
34    templates=glob.glob("GEOSPLAT_???.txt")
35    os.chdir(cwd)
36    return templates
37
38def readInitialAttsFile(attsfile):
39    """
40    Function to read the initial.attributes file into memory and
41    back up a copy to <initial.attributes.bak> in the directory of
42    the main file.
43    """
44    atts=open(attsfile).readlines()
45    shutil.copy(attsfile, attsfile+".bak")
46    return atts
47
48def addTemplatesToFile(attsfile):
49    """
50    Function to control the adding of templates to the attributes
51    file.
52    """
53    atts=readInitialAttsFile(attsfile)
54    templates=getTemplateNames()
55    # Check which are already in the file
56    templatesToAdd=""
57
58    for temp in templates:
59        trimtemp=temp[:-4]
60        found="no"
61        for line in atts:
62            if line.find(trimtemp)>-1:
63                print "Template already found in file:", trimtemp
64                found="yes" 
65        if found=="no": 
66            print "Template to be added to file:", trimtemp
67            templatesToAdd=templatesToAdd+"\n"+open(os.path.join(templateDir, temp)).read()
68
69    if templatesToAdd=="": 
70        print "No new templates need adding."
71        sys.exit()
72
73    outfile=open(attsfile, 'w')
74    for line in atts:
75        if line.find("Gi_quick(")>-1:
76            outfile.write(templatesToAdd)
77        outfile.write(line)
78    outfile.close()
79
80    print "File '%s' has been updated with new templates." % attsfile
81 
82 
83if __name__=="__main__":
84
85    addTemplatesToFile(sys.argv[1])
Note: See TracBrowser for help on using the repository browser.