-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-Concatenate_Selected_PDFs
executable file
·168 lines (142 loc) · 5.25 KB
/
3-Concatenate_Selected_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python2
import pygtk
pygtk.require('2.0')
import gtk
import sys, os, logging, re, subprocess
from time import time
#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())
class EntryDialog(gtk.MessageDialog):
def __init__(self, *args, **kwargs):
'''
Creates a new EntryDialog. Takes all the arguments of the usual
MessageDialog constructor plus one optional named argument
"default_value" to specify the initial contents of the entry.
'''
if 'default_value' in kwargs:
default_value = kwargs['default_value']
del kwargs['default_value']
else:
default_value = ''
super(EntryDialog, self).__init__(*args, **kwargs)
entry = gtk.Entry()
entry.set_text(str(default_value))
entry.connect("activate",
lambda ent, dlg, resp: dlg.response(resp),
self, gtk.RESPONSE_OK)
self.vbox.pack_end(entry, True, True, 0)
self.vbox.show_all()
self.entry = entry
def set_value(self, text):
self.entry.set_text(text)
def run(self):
result = super(EntryDialog, self).run()
if (result == gtk.RESPONSE_OK or (result == gtk.RESPONSE_YES)):
text = self.entry.get_text()
elif (result == gtk.RESPONSE_NO):
text = gtk.RESPONSE_NO
else:
text = None
return text
# This is a function I wrote. :)
def getDialogMessage(message, defaultval):
dlg = EntryDialog(None, 0, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
message, default_value=defaultval)
dlg.show_all()
rc = dlg.run()
dlg.destroy()
return rc
# The following message functions were from
# License: Creative Commons Attribution-Share Alike 3.0 License
# http://creativecommons.org/licenses/by-sa/3.0/legalcode
def warningMessage(message):
dlg = gtk.MessageDialog(None, 0, gtk.MESSAGE_WARNING, gtk.BUTTONS_YES_NO,
message)
dlg.show_all()
rc = dlg.run()
dlg.destroy()
if (rc == gtk.RESPONSE_NO):
return gtk.RESPONSE_NO
if (rc == gtk.RESPONSE_DELETE_EVENT):
return gtk.RESPONSE_NO
if (rc == gtk.RESPONSE_CLOSE):
return gtk.RESPONSE_NO
if (rc == gtk.RESPONSE_CANCEL):
return gtk.RESPONSE_NO
return rc
def errorMessage( message):
dlg = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
message)
dlg.show_all()
rc = dlg.run()
dlg.destroy()
return rc
def infoMessage( message):
dlg = gtk.MessageDialog(None, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO,
message)
dlg.show_all()
rc = dlg.run()
dlg.destroy()
return rc
def simpleInfoMessage( message):
dlg = gtk.MessageDialog(None, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
message)
dlg.show_all()
rc = dlg.run()
dlg.destroy()
return rc
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 now():
return int( time() )
def main():
loggerSetup()
pdf_files=""
files = os.getenv('NAUTILUS_SCRIPT_SELECTED_FILE_PATHS')
myfiles = files.split()
for file in myfiles:
#get rid of single quotes if they exist (Thunar workaround)
if file.startswith("'") and file.endswith("'"):
file = file[1:-1]
base = os.path.basename(file)
path, ext = os.path.splitext(file)
dirname = os.path.dirname(file)
if (ext == '.pdf'):
pdf_files += (base + " ")
number_of_pdf_files = len(pdf_files.split())
if (number_of_pdf_files > 1):
auto_name = 'cat' + str(now()) + '.pdf'
os.system("pdftk %s cat output %s" %(pdf_files, auto_name))
msg = "Concatenated %d pdf files.\nRename output file?" %(number_of_pdf_files)
ans = getDialogMessage(msg, auto_name)
if ((ans != gtk.RESPONSE_NO) and (ans != None)):
if os.path.exists(ans):
ans += str(now())
os.rename(auto_name, ans)
ans = infoMessage("Delete orignals?")
if (ans == gtk.RESPONSE_YES):
os.system("rm %s" %(pdf_files))
if __name__ == "__main__":
main()