Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6b7979

Browse files
committed
Quentin's latest version
1 parent b25c402 commit e6b7979

1 file changed

Lines changed: 119 additions & 70 deletions

File tree

Demo/scripts/newslist.py

Lines changed: 119 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212
# The -a option causes a complete list of all groups to be read from
1313
# the server rather than just the ones which have appeared since last
1414
# execution. This recreates the local list from scratch. Use this on
15-
# the first invocation of the program.
15+
# the first invocation of the program, and from time to time thereafter.
16+
# When new groups are first created they may appear on your server as
17+
# empty groups. By default, empty groups are ignored by the -a option.
18+
# However, these new groups will not be created again, and so will not
19+
# appear in the server's list of 'new groups' at a later date. Hence it
20+
# won't appear until you do a '-a' after some articles have appeared.
21+
#
22+
# I should really keep a list of ignored empty groups and re-check them
23+
# for articles on every run, but I haven't got around to it yet.
1624
#
1725
# This assumes an NNTP news feed.
1826
#
@@ -31,25 +39,23 @@
3139

3240
# Top directory.
3341
# Filenames which don't start with / are taken as being relative to this.
34-
##topdir='/anfs/qsbigdisc/web/html/newspage'
35-
topdir = '/hosts/buizerd/ufs/www/cwi/cwionly/newstree'
42+
topdir='/anfs/qsbigdisc/web/html/newspage'
3643

3744
# The name of your NNTP host
3845
# eg.
39-
# newshost = 'nntp-serv.cam.ac.uk'
46+
# newshost = 'nntp-serv.cl.cam.ac.uk'
4047
# or use following to get the name from the NNTPSERVER environment
4148
# variable:
42-
##newshost = posix.environ['NNTPSERVER']
43-
newshost = 'charon.cwi.nl'
49+
# newshost = posix.environ['NNTPSERVER']
50+
newshost = 'nntp-serv.cl.cam.ac.uk'
4451

4552
# The filename for a local cache of the newsgroup list
4653
treefile = 'grouptree'
4754

4855
# The filename for descriptions of newsgroups
4956
# I found a suitable one at ftp.uu.net in /uunet-info/newgroups.gz
5057
# You can set this to '' if you don't wish to use one.
51-
##descfile = 'newsgroups'
52-
descfile = '/usr/lib/news/newsgroups'
58+
descfile = 'newsgroups'
5359

5460
# The directory in which HTML pages should be created
5561
# eg.
@@ -60,19 +66,22 @@
6066
# The html prefix which will refer to this directory
6167
# eg.
6268
# httppref = '/newspage/',
63-
# or leave blank for relative links
64-
# between pages. (Recommended)
69+
# or leave blank for relative links between pages: (Recommended)
70+
# httppref = ''
6571
httppref = ''
6672

6773
# The name of the 'root' news page in this directory.
6874
# A .html suffix will be added.
69-
##rootpage = 'root'
70-
rootpage = 'index'
75+
rootpage = 'root'
7176

7277
# Set skipempty to 0 if you wish to see links to empty groups as well.
7378
# Only affects the -a option.
74-
##skipempty = 1
75-
skipempty = 0
79+
skipempty = 1
80+
81+
# pagelinkicon can contain html to put an icon after links to
82+
# further pages. This helps to make important links stand out.
83+
# Set to '' if not wanted, or '...' is quite a good one.
84+
pagelinkicon='... <img src="http://pelican.cl.cam.ac.uk/icons/page.xbm"> '
7685

7786
# ---------------------------------------------------------------------
7887
# Less important personal preferences:
@@ -150,8 +159,8 @@ def printtree(f, tree, indent, p):
150159
if l > sublistsize and indent>0:
151160
# Create a new page and a link to it
152161
f.write('<LI><B><A HREF="'+httppref+p[1:]+'.html">')
153-
f.write(p[1:]+'.* ...')
154-
f.write('</A></B>\n')
162+
f.write(p[1:]+'.*')
163+
f.write('</A></B>'+pagelinkicon+'\n')
155164
createpage(p[1:], tree, p)
156165
return
157166

@@ -161,7 +170,7 @@ def printtree(f, tree, indent, p):
161170
kl.sort()
162171
if indent > 0:
163172
# Create a sub-list
164-
f.write('<LI><B>'+p[1:]+'</B>\n<UL>')
173+
f.write('<LI>'+p[1:]+'\n<UL>')
165174
else:
166175
# Create a main list
167176
f.write('<UL>')
@@ -186,7 +195,7 @@ def printtree(f, tree, indent, p):
186195

187196
# This returns an array mapping group name to its description
188197

189-
def readdesc():
198+
def readdesc(descfile):
190199
global desc
191200

192201
desc = {}
@@ -212,15 +221,10 @@ def readdesc():
212221
pass
213222
l = d.readline()
214223

215-
# Now the main program --------------------------------------------
216-
217-
def main():
218-
global desc
219-
220-
connected = 0
221-
tree={}
224+
# Check that ouput directory exists, ------------------------------
225+
# and offer to create it if not
222226

223-
# Check that the output directory exists
227+
def checkopdir(pagedir):
224228
if not os.path.isdir(pagedir):
225229
print 'Directory '+pagedir+' does not exist.'
226230
print 'Shall I create it for you? (y/n)'
@@ -234,37 +238,11 @@ def main():
234238
print 'OK. Exiting.'
235239
sys.exit(1)
236240

237-
try:
238-
print 'Connecting to '+newshost+'...'
239-
if sys.version[0] == '0':
240-
s = NNTP.init(newshost)
241-
else:
242-
s = NNTP(newshost)
243-
connected = 1
244-
except (nntplib.error_temp, nntplib.error_perm), x:
245-
print 'Error connecting to host:'
246-
print x
247-
print 'I\'ll try to use just the local list.'
248-
249-
# If -a is specified, read the full list of groups from server
250-
if connected and len(sys.argv) > 1 and sys.argv[1] == '-a':
251-
print 'Getting list of all groups...'
252-
treedate='010101'
253-
info = s.list()[1]
254-
groups = []
255-
print 'Processing...'
256-
if skipempty:
257-
print '\nIgnoring following empty groups:'
258-
for i in info:
259-
if skipempty and string.atoi(i[1]) < string.atoi(i[2]):
260-
print i[0]+' ',
261-
else:
262-
groups.append(i[0])
263-
print '\n(End of empty groups)'
241+
# Read and write current local tree ----------------------------------
264242

265-
# Otherwise just read groups created since local file last modified.
266-
else:
243+
def readlocallist(treefile):
267244
print 'Reading current local group list...'
245+
tree = {}
268246
try:
269247
treetime = time.localtime(os.stat(treefile)[ST_MTIME])
270248
except:
@@ -279,25 +257,96 @@ def main():
279257
dump.close()
280258
except (IOError):
281259
print 'Cannot open local group list ' + treefile
282-
283-
if connected:
284-
print 'Getting list of new groups since start of '+treedate+'...',
285-
groups = s.newgroups(treedate,'000001')[1]
286-
print 'got '+`len(groups)`+'.'
260+
return (tree, treedate)
261+
262+
def writelocallist(treefile, tree):
263+
try:
264+
dump = open(treefile,'w')
265+
groups = marshal.dump(tree,dump)
266+
dump.close()
267+
print 'Saved list to '+treefile+'\n'
268+
except:
269+
print 'Sorry - failed to write to local group cache '+treefile
270+
print 'Does it (or its directory) have the correct permissions?'
271+
sys.exit(1)
272+
273+
# Return list of all groups on server -----------------------------
274+
275+
def getallgroups(server):
276+
print 'Getting list of all groups...'
277+
treedate='010101'
278+
info = server.list()[1]
279+
groups = []
280+
print 'Processing...'
281+
if skipempty:
282+
print '\nIgnoring following empty groups:'
283+
for i in info:
284+
grpname = string.split(i[0])[0]
285+
if skipempty and string.atoi(i[1]) < string.atoi(i[2]):
286+
print grpname+' ',
287+
else:
288+
groups.append(grpname)
289+
print '\n'
290+
if skipempty:
291+
print '(End of empty groups)'
292+
return groups
293+
294+
# Return list of new groups on server -----------------------------
295+
296+
def getnewgroups(server, treedate):
297+
print 'Getting list of new groups since start of '+treedate+'...',
298+
info = server.newgroups(treedate,'000001')[1]
299+
print 'got '+`len(info)`+'.'
300+
print 'Processing...',
301+
groups = []
302+
for i in info:
303+
grpname = string.split(i)[0]
304+
groups.append(grpname)
305+
print 'Done'
306+
return groups
307+
308+
# Now the main program --------------------------------------------
287309

310+
def main():
311+
global desc
312+
313+
tree={}
314+
315+
# Check that the output directory exists
316+
checkopdir(pagedir);
317+
318+
try:
319+
print 'Connecting to '+newshost+'...'
320+
if sys.version[0] == '0':
321+
s = NNTP.init(newshost)
322+
else:
323+
s = NNTP(newshost)
324+
connected = 1
325+
except (nntplib.error_temp, nntplib.error_perm), x:
326+
print 'Error connecting to host:', x
327+
print 'I\'ll try to use just the local list.'
328+
connected = 0
329+
330+
# If -a is specified, read the full list of groups from server
331+
if connected and len(sys.argv) > 1 and sys.argv[1] == '-a':
332+
333+
groups = getallgroups(s)
334+
335+
# Otherwise just read the local file and then add
336+
# groups created since local file last modified.
337+
else:
338+
339+
(tree, treedate) = readlocallist(treefile)
340+
if connected:
341+
groups = getnewgroups(s, treedate)
342+
288343
if connected:
289344
addtotree(tree, groups)
290-
try:
291-
dump = open(treefile,'w')
292-
groups = marshal.dump(tree,dump)
293-
dump.close()
294-
print 'Saved list to '+treefile+'\n'
295-
except:
296-
print 'Sorry - failed to write to local group cache '+treefile
297-
print 'Does it (or its directory) have the correct permissions?'
298-
sys.exit(1)
345+
writelocallist(treefile,tree)
346+
347+
# Read group descriptions
348+
readdesc(descfile)
299349

300-
readdesc()
301350
print 'Creating pages...'
302351
createpage(rootpage, tree, '')
303352
print 'Done'

0 commit comments

Comments
 (0)