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)
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
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="ignore composing of the numbered filename.")
39 parser
.add_option("-b", "--basename", type='string', dest
="basename",
40 help="File basename for reading", metavar
="FILE")
41 parser
.add_option("-e", "--ext", type='string', dest
="ext", metavar
="EXT",
42 help="add an extension after the file basename + number (without leading \".\")")
43 parser
.add_option("-f", "--first", type='int', dest
="first_nr", default
=0,
44 help="first numbered file (default: 0)", metavar
="N")
45 parser
.add_option("-l", "--last", type='int', dest
="last_nr", default
=0,
46 help="last numbered file(default: 0)", metavar
="N")
47 parser
.add_option("-i", "--incr", type='int', dest
="incr_nr", default
=1,
48 help="increment file number (default: 1)", metavar
="N")
49 parser
.add_option("-p", "--print",
50 action
="store_true", dest
="pri", default
=False,
51 help="prints process parameters/variables to stdout")
52 parser
.add_option("--pad", type='int', dest
="pad0", default
=0,
53 help="padding number with zero's (default: 0)", metavar
="N")
54 parser
.add_option("-n", "--none",
55 action
="store_true", dest
="none", default
=False,
56 help="suppresses real execution")
57 parser
.add_option("-x", "--prefix", action
="store_true", dest
="prefix", default
=False,
58 help="prefix numbering to file basename")
60 (options
, args
) = parser
.parse_args()
62 parser
.error("incorrect number of arguments")
66 if not "FNAME" in process
:
67 parser
.error("process does not contain the required string 'FNAME'")
70 #----------- Function definitions
72 def pri_date(msg
= "Time stamp at start of series processing:"):
76 msg -- message to be printed before time stamp
78 if options
.pri
== True:
88 """Counts number of digits from a number
91 nr -- number to be questioned
101 """Created a string for zero padding
104 nr -- number of zeros to be padded
107 for i
in range(0, nr
):
113 def compose_name_nr(name
, nr
, ext
):
114 """Creates proper name from basename and number.
117 nr -- number of filename to be processed
120 ndig
= count_digits(nr
)
121 null_str
= pad0(options
.pad0
- ndig
)
122 nr_str
= null_str
+str(nr
)
130 name
=nr_str
+str(name
)
135 name
=str(name
)+nr_str
137 if str(ext
) != "None":
138 name
=str(name
)+str(".")+str(options
.ext
)
143 def compose_cmd(name
, nr
, ext
):
144 """Creates proper command.
147 name -- complete filename
151 # Eventually, substitutes "-f" with: "nr -f"
153 # if "-f" in command:
154 # command=re.sub("-f", str(nr)+" -f", command)
156 # command=re.sub("FNAME", str(nr)+" FNAME", command)
159 command
=re
.sub("FNAME", str(name
), str(command
))
160 command
=re
.sub("NR", str(nr
), str(command
))
161 command
=re
.sub("EXT", str(ext
), str(command
))
166 def proc_series_ser():
167 """Processes a series on identic numbered files.
169 for i
in range(options
.first_nr
, options
.last_nr
+1, options
.incr_nr
):
170 name_nr
= compose_name_nr(options
.basename
, i
, options
.ext
)
171 command
= compose_cmd(name_nr
, i
, options
.ext
)
173 if options
.pri
== True: print command
174 elif options
.none
== True: print command
175 if options
.none
== False: os
.system(command
)
179 #----------- Calling functions
181 if options
.pri
== True: pri_date()
182 elif options
.none
== True: pri_date()
184 if options
.pri
== True: pri_date(msg
= "Time stamp at end of series processing:")
185 elif options
.none
== True: pri_date(msg
= "Time stamp at end of series processing:")
187 #----------- That's all folks