2 from wxPython
.wx
import *
3 from httplib
import HTTP
4 from htmllib
import HTMLParser
9 __doc__
= """This is wxSlash 1.1
11 It's the obligatory Slashdot.org headlines reader that any modern
12 widget set/library must have in order to be taken seriously :-)
14 Usage is quite simple; wxSlash attempts to download the 'ultramode.txt'
15 file from http://slashdot.org, which contains the headlines in a computer
16 friendly format. It then displays said headlines in a wxWindows list control.
18 You can read articles using either Python's html library or an external
19 browser. Uncheck the 'browser->internal' menu item to use the latter option.
20 Use the settings dialog box to set which external browser is started.
22 This code is available under the wxWindows license, see elsewhere. If you
23 modify this code, be aware of the fact that slashdot.org's maintainer,
24 CmdrTaco, explicitly asks 'ultramode.txt' downloaders not to do this
25 automatically more than twice per hour. If this feature is abused, CmdrTaco
26 may remove the ultramode file completely and that will make a *lot* of people
29 I want to thank Alex Shnitman whose slashes.pl (Perl/GTK) script gave me
30 the idea for this applet.
34 Harm van der Heijden (H.v.d.Heijden@phys.tue.nl)
37 class HTMLTextView(wxFrame
):
38 def __init__(self
, parent
, id, title
='HTMLTextView', url
=None):
39 wxFrame
.__init
__(self
, parent
, id, title
, wxPyDefaultPosition
,
42 self
.mainmenu
= wxMenuBar()
45 menu
.Append(201, '&Open URL...', 'Open URL')
46 EVT_MENU(self
, 201, self
.OnFileOpen
)
47 menu
.Append(209, 'E&xit', 'Exit viewer')
48 EVT_MENU(self
, 209, self
.OnFileExit
)
50 self
.mainmenu
.Append(menu
, '&File')
51 self
.SetMenuBar(self
.mainmenu
)
52 self
.CreateStatusBar(1)
54 self
.text
= wxTextCtrl(self
, -1, "", wxPyDefaultPosition
,
55 wxPyDefaultSize
, wxTE_MULTILINE | wxTE_READONLY
)
60 def logprint(self
, x
):
63 def OpenURL(self
, url
):
65 m
= re
.match('file:(\S+)\s*', url
)
67 f
= open(m
.groups()[0],'r')
69 m
= re
.match('http://([^/]+)(/\S*)\s*', url
)
74 m
= re
.match('http://(\S+)\s*', url
)
77 self
.logprint("Invalid or unsupported URL: %s" % (url
))
81 f
= RetrieveAsFile(host
,path
,self
.logprint
)
83 self
.logprint("Could not open %s" % (url
))
85 self
.logprint("Receiving data...")
87 tmp
= open('tmphtml.txt','w')
88 fmt
= formatter
.AbstractFormatter(formatter
.DumbWriter(tmp
))
90 self
.logprint("Parsing data...")
94 tmp
= open('tmphtml.txt', 'r')
95 self
.text
.SetValue(tmp
.read())
99 def OnFileOpen(self
, event
):
100 dlg
= wxTextEntryDialog(self
, "Enter URL to open:", "")
101 if dlg
.ShowModal() == wxID_OK
:
108 def OnFileExit(self
, event
):
111 def OnCloseWindow(self
, event
):
115 def ParseSlashdot(f
):
116 art_sep
= re
.compile('%%\r?\n')
117 line_sep
= re
.compile('\r?\n')
119 list = art_sep
.split(data
)
121 for i
in range(1,len(list)-1):
122 art_list
.append(line_sep
.split(list[i
]))
128 def RetrieveAsFile(host
, path
='', logprint
= myprint
):
132 logprint("Failed to create HTTP connection to %s... is the network available?" % (host
))
134 h
.putrequest('GET',path
)
135 h
.putheader('Accept','text/html')
136 h
.putheader('Accept','text/plain')
138 errcode
, errmsg
, headers
= h
.getreply()
140 logprint("HTTP error code %d: %s" % (errcode
, errmsg
))
143 # f = open('/home/harm/ultramode.txt','r')
147 class AppStatusBar(wxStatusBar
):
148 def __init__(self
, parent
):
149 wxStatusBar
.__init
__(self
,parent
, -1)
150 self
.SetFieldsCount(2)
151 self
.SetStatusWidths([-1, 100])
152 self
.but
= wxButton(self
, 1001, "Refresh")
153 EVT_BUTTON(self
, 1001, parent
.OnViewRefresh
)
156 def logprint(self
,x
):
157 self
.SetStatusText(x
,0)
159 def OnSize(self
, event
):
160 rect
= self
.GetFieldRect(1)
161 self
.but
.SetPosition(wxPoint(rect
.x
+2, rect
.y
+2))
162 self
.but
.SetSize(wxSize(rect
.width
-4, rect
.height
-4))
164 # This is a simple timer class to start a function after a short delay;
165 class QuickTimer(wxTimer
):
166 def __init__(self
, func
, wait
=100):
167 wxTimer
.__init
__(self
)
169 self
.Start(wait
); # wait .1 second (.001 second doesn't work. why?)
172 apply(self
.callback
, ());
174 class AppFrame(wxFrame
):
175 def __init__(self
, parent
, id, title
):
176 wxFrame
.__init
__(self
, parent
, id, title
, wxPyDefaultPosition
,
179 # if the window manager closes the window:
180 EVT_CLOSE(self
, self
.OnCloseWindow
);
182 # Now Create the menu bar and items
183 self
.mainmenu
= wxMenuBar()
186 menu
.Append(209, 'E&xit', 'Enough of this already!')
187 EVT_MENU(self
, 209, self
.OnFileExit
)
188 self
.mainmenu
.Append(menu
, '&File')
190 menu
.Append(210, '&Refresh', 'Refresh headlines')
191 EVT_MENU(self
, 210, self
.OnViewRefresh
)
192 menu
.Append(211, '&Slashdot Index', 'View Slashdot index')
193 EVT_MENU(self
, 211, self
.OnViewIndex
)
194 menu
.Append(212, 'Selected &Article', 'View selected article')
195 EVT_MENU(self
, 212, self
.OnViewArticle
)
196 self
.mainmenu
.Append(menu
, '&View')
198 menu
.Append(220, '&Internal', 'Use internal text browser',TRUE
)
199 menu
.Check(220, true
)
200 self
.UseInternal
= 1;
201 EVT_MENU(self
, 220, self
.OnBrowserInternal
)
202 menu
.Append(222, '&Settings...', 'External browser Settings')
203 EVT_MENU(self
, 222, self
.OnBrowserSettings
)
204 self
.mainmenu
.Append(menu
, '&Browser')
206 menu
.Append(230, '&About', 'Some documentation');
207 EVT_MENU(self
, 230, self
.OnAbout
)
208 self
.mainmenu
.Append(menu
, '&Help')
210 self
.SetMenuBar(self
.mainmenu
)
212 if wxPlatform
== '__WXGTK__':
213 # I like lynx. Also Netscape 4.5 doesn't react to my cmdline opts
214 self
.BrowserSettings
= "xterm -e lynx %s &"
215 elif wxPlatform
== '__WXMSW__':
216 # netscape 4.x likes to hang out here...
217 self
.BrowserSettings
= '\progra~1\Netscape\Communicator\Program\netscape.exe %s'
220 self
.BrowserSettings
= 'netscape %s'
222 # A status bar to tell people what's happening
223 self
.sb
= AppStatusBar(self
)
224 self
.SetStatusBar(self
.sb
)
226 self
.list = wxListCtrl(self
, 1100)
227 self
.list.SetSingleStyle(wxLC_REPORT
)
228 self
.list.InsertColumn(0, 'Subject')
229 self
.list.InsertColumn(1, 'Date')
230 self
.list.InsertColumn(2, 'Posted by')
231 self
.list.InsertColumn(3, 'Comments')
232 self
.list.SetColumnWidth(0, 300)
233 self
.list.SetColumnWidth(1, 150)
234 self
.list.SetColumnWidth(2, 100)
235 self
.list.SetColumnWidth(3, 100)
237 EVT_LIST_ITEM_SELECTED(self
, 1100, self
.OnItemSelected
)
239 self
.logprint("Connecting to slashdot... Please wait.")
240 # wxYield doesn't yet work here. That's why we use a timer
241 # to make sure that we see some GUI stuff before the slashdot
242 # file is transfered.
243 self
.timer
= QuickTimer(self
.DoRefresh
, 1000)
245 def logprint(self
, x
):
248 def OnFileExit(self
, event
):
252 f
= RetrieveAsFile('slashdot.org','/ultramode.txt',self
.sb
.logprint
)
253 art_list
= ParseSlashdot(f
)
254 self
.list.DeleteAllItems()
258 for article
in art_list
:
259 self
.list.InsertStringItem(i
, article
[0])
260 self
.list.SetStringItem(i
, 1, article
[2])
261 self
.list.SetStringItem(i
, 2, article
[3])
262 self
.list.SetStringItem(i
, 3, article
[6])
263 self
.url
.append(article
[1])
265 self
.logprint("File retrieved OK.")
267 def OnViewRefresh(self
, event
):
268 self
.logprint("Connecting to slashdot... Please wait.");
272 def DoViewIndex(self
):
274 self
.view
= HTMLTextView(self
, -1, 'slashdot.org',
275 'http://slashdot.org')
278 self
.logprint(self
.BrowserSettings
% ('http://slashdot.org'))
279 os
.system(self
.BrowserSettings
% ('http://slashdot.org'))
282 def OnViewIndex(self
, event
):
283 self
.logprint("Starting browser... Please wait.")
287 def DoViewArticle(self
):
288 if self
.current
<0: return
289 url
= self
.url
[self
.current
]
291 self
.view
= HTMLTextView(self
, -1, url
, url
)
294 self
.logprint(self
.BrowserSettings
% (url
))
295 os
.system(self
.BrowserSettings
% (url
))
298 def OnViewArticle(self
, event
):
299 self
.logprint("Starting browser... Please wait.")
303 def OnBrowserInternal(self
, event
):
304 if self
.mainmenu
.Checked(220):
309 def OnBrowserSettings(self
, event
):
310 dlg
= wxTextEntryDialog(self
, "Enter command to view URL.\nUse %s as a placeholder for the URL.", "", self
.BrowserSettings
);
311 if dlg
.ShowModal() == wxID_OK
:
312 self
.BrowserSettings
= dlg
.GetValue()
314 def OnAbout(self
, event
):
315 dlg
= wxMessageDialog(self
, __doc__
, "wxSlash", wxOK | wxICON_INFORMATION
)
318 def OnItemSelected(self
, event
):
319 self
.current
= event
.m_itemIndex
320 self
.logprint("URL: %s" % (self
.url
[self
.current
]))
322 def OnCloseWindow(self
, event
):
327 frame
= AppFrame(NULL
, -1, "Slashdot Breaking News")
329 self
.SetTopWindow(frame
)
335 if __name__
== '__main__':