libgpiv so version 4: GpivFt. Show git hash (if available) in output and
[gpivtools.git] / src / misc / series_ser.py
blobafdccb87ee419ea3012e802bb9c76d45dd6083c1
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="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()
61 if len(args) != 1:
62 parser.error("incorrect number of arguments")
63 else:
64 process = args[0]
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:"):
73 """Prints time stamp.
75 Keyword arguments:
76 msg -- message to be printed before time stamp
77 """
78 if options.pri == True:
79 print msg
80 os.system('date')
81 elif options.none:
82 print msg
83 os.system('date')
87 def count_digits(nr):
88 """Counts number of digits from a number
90 Keyword arguments:
91 nr -- number to be questioned
92 """
93 count=0
94 while nr/10 !=0:
95 nr=nr/10
96 count=count+1
97 return count
100 def pad0(nr):
101 """Created a string for zero padding
103 Keyword arguments:
104 nr -- number of zeros to be padded
106 pd0=""
107 for i in range(0, nr):
108 pd0 = str(pd0)+"0"
110 return pd0
113 def compose_name_nr(name, nr, ext):
114 """Creates proper name from basename and number.
116 Keyword arguments:
117 nr -- number of filename to be processed
119 if options.pad0 > 0:
120 ndig = count_digits(nr)
121 null_str = pad0(options.pad0 - ndig)
122 nr_str = null_str+str(nr)
123 else:
124 nr_str = str(nr)
126 if options.prefix:
127 if options.arg_n:
128 name=str(name)
129 else:
130 name=nr_str+str(name)
131 else:
132 if options.arg_n:
133 name=str(name)
134 else:
135 name=str(name)+nr_str
137 if str(ext) != "None":
138 name=str(name)+str(".")+str(options.ext)
140 return(name)
143 def compose_cmd(name, nr, ext):
144 """Creates proper command.
146 Keyword arguments:
147 name -- complete filename
149 command=str(process)
151 # Eventually, substitutes "-f" with: "nr -f"
152 #if options.arg_n:
153 # if "-f" in command:
154 # command=re.sub("-f", str(nr)+" -f", command)
155 # else:
156 # command=re.sub("FNAME", str(nr)+" FNAME", command)
158 # Substitutes macros
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))
163 return 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()
183 proc_series_ser()
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