-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-Make_Duplex_PDFs
executable file
·114 lines (94 loc) · 3.71 KB
/
2-Make_Duplex_PDFs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python2
import pygtk
pygtk.require('2.0')
import gtk
import sys, os, logging, re, subprocess
class filegroup:
count = 0
prefix = ""
#Simple alert script from:
#http://www.ibm.com/developerworks/linux/library/l-script-linux-desktop-2/index.html
def alert(msg):
"""Show a dialog with a simple message."""
dialog = gtk.MessageDialog()
dialog.set_markup(msg)
dialog.run()
#cool logging class from
#http://www.electricmonk.nl/log/2011/08/14/redirect-stdout-and-stderr-to-a-logger-in-python/
class StreamToLogger(object):
"""
Fake file-like stream object that redirects writes to a logger instance.
"""
def __init__(self, logger, log_level=logging.INFO):
self.logger = logger
self.log_level = log_level
self.linebuf = ''
def write(self, buf):
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())
def loggerSetup():
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
filename="out.log",
filemode='a'
)
stdout_logger = logging.getLogger('STDOUT')
sl = StreamToLogger(stdout_logger, logging.INFO)
sys.stdout = sl
stderr_logger = logging.getLogger('STDERR')
sl = StreamToLogger(stderr_logger, logging.ERROR)
sys.stderr = sl
def main():
loggerSetup()
mydir = os.getenv('NAUTILUS_SCRIPT_CURRENT_URI')
files = os.getenv('NAUTILUS_SCRIPT_SELECTED_FILE_PATHS')
myfiles = files.split()
print myfiles
filevers = {}
for file in myfiles:
#get rid of single quotes if they exist (Thunar workaround)
if file.startswith("'") and file.endswith("'"):
file = file[1:-1]
basename = os.path.basename(file)
dirname = os.path.dirname(file)
m = re.match('(?P<prefix>rotate-scan|scan)(?P<time>\d+)-(?P<num>\d+)\.pdf', basename)
if (m != None):
prefix = m.group('prefix')
time = int(m.group('time'))
num = int(m.group('num'))
#all this does is count up how many files we have for a specific time
filevers[time] = filevers.setdefault(time, filegroup())
filevers[time].count += 1
filevers[time].prefix = prefix
else:
alert('At least one of the files you have chosen does not have the\n\
expected filename: scanxxxxxxxxxx-x.pdf')
print('exit')
sys.exit()
groups = filevers.keys() #this should return two values, the time from filegroup 1 and the time from filegroup 2
groups.sort() #put them in order from low --> high
# --------------------- ERROR CHECKING ---------------------------
if (len(groups) == 2):
#Check that the size of the groups is the same
if (filevers[groups[0]].count != filevers[groups[1]].count):
alert('Different sized scan groups: %d vs %d' %(filevers[groups[0]].count, filevers[groups[1]].count))
sys.exit()
else:
alert('You have selected more or less than two scan groups.')
sys.exit()
# --------------------- MATCH THEM UP ---------------------------
num = filevers[groups[0]].count #The number of files to process
oddpages = 1
evenpages = num
for i in range(num):
firstfile = dirname+"/%s%d-%d.pdf" %(filevers[groups[0]].prefix, groups[0], oddpages)
secondfile = dirname+"/%s%d-%d.pdf" %(filevers[groups[1]].prefix, groups[1], evenpages)
finalfile = dirname+"/dplx-scan%d-%02d-%02d.pdf" %(groups[0], oddpages, evenpages)
print firstfile, secondfile, finalfile
os.system("pdftk %s %s cat output %s" %(firstfile, secondfile, finalfile))
oddpages += 1
evenpages -= 1
alert('Created duplex scans for %d documents' %(num))
if __name__ == "__main__":
main()