-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpinotate-gui.py
executable file
·88 lines (70 loc) · 2.8 KB
/
pinotate-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
#!/usr/bin/env python3
__author__ = 'Galarius'
__copyright__ = 'Copyright 2020, Galarius'
import platform
import sys
if platform.python_version().startswith("2."):
print('Python3 is required')
sys.exit(1)
import wx
import wx.html2
import os
from markdown import markdown
from core import generate_md, valid_filename
from core import IBooksWorker
wxID_FRAME_LISTBOX_TITLES = wx.NewIdRef(count=1)
wxID_FRAME_BUTTON_EXPORT = wx.NewIdRef(count=1)
class Window(wx.Frame):
def __init__(self, parent, title):
super(Window, self).__init__(parent, title = title, size = (640,480))
self.worker = IBooksWorker()
self.InitUI()
self.CreateStatusBar()
self.Centre()
self.Show()
self.SetAutoLayout(True)
self.Layout()
def InitUI(self):
titles = self.worker.titles()
wxVbox = wx.BoxSizer(wx.VERTICAL)
wxText = wx.TextCtrl(self, style=wx.TE_READONLY)
wxText.SetValue("iBooks Library")
wxVbox.Add(wxText, 0, wx.EXPAND)
self.wxListBox = wx.ListBox(choices=titles, id=wxID_FRAME_LISTBOX_TITLES, parent=self)
self.wxListBox.Bind(wx.EVT_LISTBOX, self.OnRowSelected, id=wxID_FRAME_LISTBOX_TITLES)
wxVbox.Add(self.wxListBox, 0, wx.EXPAND)
self.browser = wx.html2.WebView.New(self)
wxVbox.Add(self.browser, 1, wx.EXPAND)
self.SetSizer(wxVbox)
menu = wx.Menu()
exportMd = menu.Append(wx.ID_ANY,"Export...", "Export highlights.")
aboutItem = menu.Append(wx.ID_ABOUT,"About...", "About Pinotate.")
self.Bind(wx.EVT_MENU, self.OnExportMd, exportMd)
self.Bind(wx.EVT_MENU, self.OnAbout, aboutItem)
bar = wx.MenuBar()
bar.Append(menu, "File")
self.SetMenuBar(bar)
def OnRowSelected(self, event):
self.content = ""
self.title = self.wxListBox.GetStringSelection()
self.SetStatusText("{}".format(self.title))
asset_id = self.worker.asset_id(self.title)
highlights = self.worker.highlights(asset_id)
if highlights:
self.content = generate_md(self.title, highlights)
self.browser.SetPage(markdown(self.content), "")
def OnExportMd(self, event):
if len(self.content) == 0:
return
dialog = wx.FileDialog(None, "Choose a file", os.getcwd(), valid_filename(self.title), "Markdown files (*.md)|*.md|", wx.FD_SAVE)
if dialog.ShowModal() == wx.ID_OK:
filepath = dialog.GetPath()
with open(filepath, 'w') as f:
f.write(self.content)
dialog.Destroy()
def OnAbout(self, e):
aboutDlg = wx.MessageDialog(self, "Export iBooks highlights","About Pinotate", wx.OK)
aboutDlg.ShowModal()
app = wx.App()
wnd = Window(None, "Pinotate")
app.MainLoop()