added 'Substitute all FNAME occurrences' option
[gpivtools.git] / src / misc / series_ser.py
blob6aa690c1ed7becf974ee1599368d967ce505c5d3
1 #!/usr/bin/env python
3 # gpiv_series - Processes a set of numbered input data
5 # Copyright (C) 2008 Gerber van der Graaf
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software Foundation,
19 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #--------------------------------------------------------------------
25 # This version is for serial processing
27 import os, re
30 #----------- Command line arguments parser
32 from optparse import OptionParser
34 usage = "%prog [options] \"process\""
35 parser = OptionParser(usage)
36 parser.add_option("-a", "--arg_n",
37 action="store_true", dest="arg_n", default=False,
38 help="if the process needs the current number in its \
39 argument list instead of prepending/appending it to the \
40 filebase name, the number will be put before (-f) \
41 \"filename\" in the \"process\" string.")
42 parser.add_option("-b", "--basename", type='string', dest="basename",
43 help="File basename for reading", metavar="FILE")
44 parser.add_option("-e", "--ext", type='string', dest="ext", metavar="EXT",
45 help="add an extension after the file basename + number (without leading \".\")")
46 parser.add_option("-f", "--first", type='int', dest="first_nr", default=0,
47 help="first numbered file (default: 0)", metavar="N")
48 parser.add_option("-l", "--last", type='int', dest="last_nr", default=0,
49 help="last numbered file(default: 0)", metavar="N")
50 parser.add_option("-i", "--incr", type='int', dest="incr_nr", default=1,
51 help="increment file number (default: 1)", metavar="N")
52 parser.add_option("-p", "--print",
53 action="store_true", dest="pri", default=False,
54 help="prints process parameters/variables to stdout")
55 parser.add_option("--pad", type='int', dest="pad0", default=0,
56 help="padding number with zero's (default: 0)", metavar="N")
57 parser.add_option("-s", "--subst_fname",
58 action="store_true", dest="subst_fname", default=False,
59 help="substitutes all occurences \"FNAME\" in the \"process\" string by the actual \
60 file basename. The option --arg_n will not have effect")
61 parser.add_option("-n", "--none",
62 action="store_true", dest="none", default=False,
63 help="suppresses real execution")
64 parser.add_option("-x", "--prefix", action="store_true", dest="prefix", default=False,
65 help="prefix numbering to file basename")
67 (options, args) = parser.parse_args()
68 if len(args) != 1:
69 parser.error("incorrect number of arguments")
70 else:
71 process = args[0]
75 #----------- Function definitions
77 def pri_date(msg = "Time stamp at start of series processing:"):
78 """Prints time stamp.
80 Keyword arguments:
81 msg -- message to be printed before time stamp
82 """
83 if options.pri == True:
84 print msg
85 os.system('date')
86 elif options.none:
87 print msg
88 os.system('date')
92 def count_digits(nr):
93 """Counts number of digits from a number
95 Keyword arguments:
96 nr -- number to be questioned
97 """
98 count=0
99 while nr/10 !=0:
100 nr=nr/10
101 count=count+1
102 return count
105 def pad0(nr):
106 """Created a string for zero padding
108 Keyword arguments:
109 nr -- number of zeros to be padded
111 pd0=""
112 for i in range(0, nr):
113 pd0 = str(pd0)+"0"
115 return pd0
118 def compose_name_nr(nr):
119 """Creates proper name from basename and number.
121 Keyword arguments:
122 nr -- number of filename to be processed
124 if options.pad0 > 0:
125 ndig=count_digits(nr)
126 null_str=pad0(options.pad0 - ndig)
127 nr_str=null_str+str(nr)
128 else:
129 nr_str=str(nr)
131 if options.prefix:
132 if options.arg_n:
133 name=str(options.basename)
134 else:
135 name=nr_str+str(options.basename)
136 else:
137 if options.arg_n:
138 name=str(options.basename)
139 else:
140 name=str(options.basename)+nr_str
142 if str(options.ext) != "None":
143 name=str(name)+str(".")+str(options.ext)
145 return(name)
148 def compose_cmd(name, nr):
149 """Creates proper command.
151 Keyword arguments:
152 name -- complete filename
154 command=str(process)
155 # Eventually, substitutes "-f" with: "nr -f"
156 if options.arg_n:
157 command_tmp=re.sub("-f", str(nr)+" -f", command)
158 if command_tmp != command:
159 command = str(command_tmp)+" "+str(name)
160 else:
161 command = str(command_tmp)+" "+str(nr)+" "+str(name)
162 else:
163 # Eventually, substitutes "FNAME" with the file base name
164 if options.subst_fname:
165 command=re.sub("FNAME", str(name), str(process))
166 else:
167 command=str(command)+" "+str(name)
169 return command
172 def proc_series_ser():
173 """Processes a series on identic numbered files.
175 for i in range(options.first_nr, options.last_nr+1, options.incr_nr):
176 name_nr = compose_name_nr(i)
177 command = compose_cmd(name_nr, i)
179 if options.pri == True: print command
180 elif options.none == True: print command
181 if options.none == False: os.system(command)
185 #----------- Calling functions
187 if options.pri == True: pri_date()
188 elif options.none == True: pri_date()
189 proc_series_ser()
190 if options.pri == True: pri_date(msg = "Time stamp at end of series processing:")
191 elif options.none == True: pri_date(msg = "Time stamp at end of series processing:")
193 #----------- That's all folks