-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdflib_gui.py
executable file
·270 lines (222 loc) · 8.09 KB
/
pdflib_gui.py
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/python
import wx
import os
import pyPdf
import subprocess
from pdflib_db import Paper, load_docs_from_db, add_doc_to_db, pdf_repo
icon_path = "tango-icon-theme/32x32/"
PAPER_ADD = 0
PAPER_DEL = 1
PAPER_UPDATE = 2
ADD_ID = 1
BIB_ID = 2
EDIT_ID = 3
MAIN_BORDER= 10
GAP = 5
def ParsePDF(path):
return Paper()
# in the future, figure out how to extract the title, authors, etc
try:
pdf = pyPdf.PdfFileReader(open(path, 'rb'))
except Exception, e:
print e
print 'Exception: no file'
return None
if pdf == None:
print 'None in open: no file'
return None
try:
title = pdf.getDocumentInfo().title
except:
pass
txt = pdf.getPage(0).extractText()
txt = " ".join(txt.replace(u"\xa0", " ").strip().split())
txt = txt.encode("ascii", "ignore")
class DocumentPromptDialog(wx.Dialog):
def __init__(self, parent, id, title, paper = None):
wx.Dialog.__init__(self, parent, id, title)
vbox = wx.BoxSizer(wx.VERTICAL)
input_boxes = [u"Title:", u"Authors:", u"Year:", u"Venue:"]
bttn_sizer = self.CreateButtonSizer(wx.CANCEL|wx.OK)
self.ctrl = {}
for i in input_boxes:
hbox = wx.BoxSizer(wx.HORIZONTAL)
ctrl = wx.TextCtrl(self, -1)
label = wx.StaticText(self, -1, label = i)
hbox.Add(label, 1)
hbox.Add(ctrl, 2)
vbox.Add(hbox, 2, wx.EXPAND|wx.ALL, 5)
self.ctrl[i] = ctrl
vbox.AddSizer(bttn_sizer, proportion = 1, flag = wx.ALIGN_BOTTOM|wx.ALL, border = 5)
self.SetSizer(vbox)
self.__results = None
self.Centre()
self.Show(True)
def GetData(self):
self.__results = {}
self.__results['title'] = ' '.join(self.ctrl[u'Title:'].GetValue().split())
self.__results['authors'] = ' '.join(self.ctrl[u'Authors:'].GetValue().split()).split(',')
self.__results['year'] = int(self.ctrl[u'Year:'].GetValue())
return self.__results
def make_image(path):
return wx.Image(icon_path+path, wx.BITMAP_TYPE_PNG).ConvertToBitmap()
class PDFLibFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
# add the necessary lists, menus, buttons
self.__grid = wx.GridBagSizer(GAP, GAP)
self.__toolbar = wx.ToolBar(self, -1, style = wx.TB_HORIZONTAL)
add_img = make_image("actions/list-add.png")
bib_img = make_image("actions/document-properties.png")
edit_img = make_image("actions/edit-find-replace.png")
self.__toolbar.AddLabelTool(ADD_ID, '', add_img,
shortHelp = 'Add document')
self.__toolbar.AddLabelTool(BIB_ID, '', bib_img,
shortHelp = 'Make a bib')
self.__toolbar.AddLabelTool(EDIT_ID, '', edit_img,
shortHelp = 'Edit entry')
self.__toolbar.Realize()
self.__doc_list = wx.ListBox(self, -1, style = wx.LB_SINGLE | wx.LB_SORT)
self.__doc_properties = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
self.__doc_properties.SetEditable(False)
self.__title_txt = wx.StaticText(self, -1, label = 'Title:')
self.__author_txt = wx.StaticText(self, -1, label = 'Author:')
self.__venue_txt = wx.StaticText(self, -1, label = 'Venue:')
self.__year_txt = wx.StaticText(self, -1, label = 'Year:')
self.__find_title = wx.TextCtrl(self, -1)
self.__find_author = wx.TextCtrl(self, -1)
self.__find_venue = wx.TextCtrl(self, -1)
self.__find_year = wx.TextCtrl(self, -1)
self.__find_bttn = wx.Button(self, wx.ID_FIND)
self.__reset_bttn = wx.Button(self, wx.ID_CLEAR)
self.__grid.Add(self.__toolbar, (0, 0), wx.GBSpan(1, 40), wx.EXPAND)
self.__grid.Add(self.__title_txt, (1, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__find_title, (2, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__author_txt, (3, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__find_author, (4, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__venue_txt, (5, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__find_venue, (6, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__year_txt, (7, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__find_year, (8, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__find_bttn, (9, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__reset_bttn, (10, 0), flag = wx.LEFT,
border = MAIN_BORDER)
self.__grid.Add(self.__doc_list, (1, 1), wx.GBSpan(10, 40),
wx.EXPAND|wx.RIGHT, border=MAIN_BORDER)
self.__grid.Add(self.__doc_properties, (11, 1), (10, 40),
wx.EXPAND|wx.RIGHT|wx.BOTTOM, border=MAIN_BORDER)
self.SetSizer(self.__grid)
self.__grid.Fit(self)
self.Centre()
self.Show(True)
self.InitializeDocList()
self.__doc_list.Bind(wx.EVT_LISTBOX, self.OnDocListBox)
self.__doc_list.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListDblClick)
self.Bind(wx.EVT_TOOL, self.OnAddDocument, id = ADD_ID)
self.Bind(wx.EVT_TOOL, self.OnMakeBib, id = BIB_ID)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.__find_bttn.Bind(wx.EVT_BUTTON, self.OnFind)
self.__reset_bttn.Bind(wx.EVT_BUTTON, self.OnClear)
def InitializeDocList(self):
self.__docs = load_docs_from_db()
if self.__docs == None:
return
for doc in self.__docs:
# set the list entries
self.__doc_list.Append(doc.get_title(), clientData = doc)
def UpdateDocList(self, paper, action = PAPER_ADD):
self.__doc_list.Append(paper.get_title(), clientData = paper)
def OnDocListBox(self, event):
clicked = self.__doc_list.GetClientData(self.__doc_list.GetSelection())
if clicked == None:
return
# set the properties in the text box
self.__doc_properties.Clear()
self.__doc_properties.WriteText("Title: %s\n" % clicked.get_title())
self.__doc_properties.WriteText("Authors: %s\n" % clicked.get_author())
self.__doc_properties.WriteText("Year: %d" % clicked.get_year())
def OnListDblClick(self, event):
clicked = self.__doc_list.GetClientData(self.__doc_list.GetSelection())
if clicked == None:
return
name = u'acroread ' + pdf_repo + u'/' + clicked.get_file()
subprocess.Popen(name.encode('utf_8', 'replace').split(' '))
def OnAddDocument(self, event):
# prompt for the document to add
dialog = wx.FileDialog(None, message = 'Select a file...', style = wx.OPEN, wildcard = '*.pdf')
file_path = None
paper = None
if dialog.ShowModal() == wx.ID_OK:
file_path = dialog.GetPath()
if file_path and len(file_path) > 0:
paper = ParsePDF(file_path)
dialog.Destroy()
if not paper:
return
dialog = DocumentPromptDialog(self, -1, "PDF Information Prompt", paper)
result = dialog.ShowModal()
if result == wx.ID_OK:
info = dialog.GetData()
if info != None:
paper = Paper(title=info['title'], author=info['authors'],\
year=info['year'], filename=file_path)
else:
print "not ok??", result
paper = None
dialog.Destroy()
if not paper:
return
# add the paper to the DB and the list
add_doc_to_db(paper)
self.UpdateDocList(paper)
def OnMakeBib(self, event):
clicked = self.__doc_list.GetClientData(self.__doc_list.GetSelection())
if not clicked:
return
name = clicked.get_title()
year = str(clicked.get_year())
authors = clicked.get_author()
ref_name = authors.split(',')[0].split(' ')[1]+year
author_list = []
for a in authors.split(','):
a = a.strip()
author_names = a.split(' ')
first = author_names[0]
last = author_names[-1]
if not first.endswith('.'):
author_list.append(last + ', ' + first[0] + '.')
else:
author_list.append(last + ', ' + first)
authors_line = ' and '.join(author_list)
msg = '@inproceedings{%s,\n' % ref_name
msg += '\ttitle = {{%s}},\n' % name
msg += '\tauthor = {%s},\n' % authors_line
msg += '\tyear = {%s},\n' % year
msg += '}'
wx.MessageBox(msg, 'bibtex entry for %s' % ref_name, wx.OK)
def OnSize(self, event):
x, y = event.GetSize()
cell_x, cell_y = self.__grid.GetCellSize(1, 0)
max_x_size = x - cell_x - MAIN_BORDER - GAP
max_y_size = (y - cell_y - GAP*2 - MAIN_BORDER)/2
self.__doc_list.SetMaxSize((max_x_size, max_y_size))
self.__doc_properties.SetMaxSize((max_x_size, max_y_size))
self.Layout()
self.Refresh()
def OnFind(self, event):
pass
def OnClear(self, event):
pass
if __name__ == '__main__':
app = wx.App()
PDFLibFrame(None, -1, "PDFLib")
app.MainLoop()