3 pdftools.join.cmd - Join PDF documents into a single file.
6 # Copyright 2012 by Hartmut Goebel <h.goebel@goebel-consult.de>
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 __author__ = "Hartmut Goebel <h.goebel@goebel-consult.de>"
23 __copyright__ = "Copyright 2012 by Hartmut Goebel <h.goebel@goebel-consult.de>"
24 __licence__ = "GNU General Public License version 3 (GPL v3)"
26 from . import main, __version__, DecryptionError
33 def make_outname(dirname):
35 Search for a file matching `pdfposter-YYYMMMDD-hhmm.pdf and last
36 modified within the last hour. If this does not exist, create a
39 names = glob.glob(os.path.join(dirname, 'pdfjoin-*.pdf'))
40 # keep only those matching the pattern `pdfposter-YYYMMMDD-hhmm.pdf`
41 names = [(os.path.getmtime(n), n)
43 if re.match('^pdfjoin-\d{8}-\d{4}\.pdf', os.path.basename(n))]
46 mtime, outname = names[-1]
47 if time.time() - mtime < 3600:
49 # no matching file found which has been last motified within the last hour
50 return os.path.join(dirname,time.strftime('pdfjoin-%Y%m%d-%H%M.pdf'))
55 parser = optparse.OptionParser('%prog [options] InputFile OutputFile',
57 parser.add_option('-v', '--verbose', action='count', default=0,
58 help='Be verbose. Can be used more than once to increase the verbosity. ')
59 parser.add_option('-n', '--dry-run', action='store_true',
60 help='Show what would have been done, but do not generate files.')
62 group = parser.add_option_group('Define Target')
63 group.add_option('-o', '--output',
64 help='Specify filename or directory to write file to '
65 '(default: current directory)')
67 opts, args = parser.parse_args()
70 parser.error('requires at least one input filename')
73 outfilename = make_outname('.')
74 elif os.path.isdir(opts.output):
75 outfilename = make_outname(opts.output)
77 outfilename = opts.output
79 if os.path.exists(outfilename):
80 args.insert(0, outfilename)
83 main(opts, outfilename, args)
84 except DecryptionError, e:
85 raise SystemExit(str(e))
88 if __name__ == '__main__':