[Twisted-Python] Is it just me ....

Moshe Zadka m at moshez.org
Thu Jul 10 06:00:20 MDT 2003


On Thu, 10 Jul 2003, Neil Blakey-Milner <nbm at mithrandr.moria.org> wrote:

> tnntprss is at http://mithrandr.moria.org/code/tnntprss/ and it creates
> a NNTP news server with newsgroups and content from RSS feeds.
> Configuration of RSS feeds is probably the main issue here - I didn't
> want to use web interfaces without understanding woven.guard/cred
> better.  Understanding whether I'm using getServiceNamed in an
> acceptable way would be nice.  I remember pynfo used adapters for
> something similar - would that be more Twistedish (Twistedic?).

Well, if you have two services that rely on each other, a nice way to
deal with that is MultiService. Have a convenicence function which
returns a MultiService, all loaded up with both services, created
from arguments to this function.

def getTNNTPService(newsDir, syndDir, name):
    s = app.MultiService(name)
    NewsService(newsDir, "news", s) 
    SyndicationService(syndDir, "syndication", s) 
    return s

Then a user can use the following to run your code:

'''
# File: tnntp.tpy
#
# Location of news
newsDir = "/tmp/newsdb"
# Location of syndication
syndDir = "/tmp/synddb"

from tnntprss import getTNNTPService
from twisted.internetet import app

application = app.Application("tnntprss")
getTNNTPService(newsDir, syndDir, "tnntp).setServiceParent(application)
'''

Then,

% twistd -y tnntp.tpy

works.

Or, you might want to support an even more extreme method of hiding
implementation details from users:

def simpGetTNNTPService(storage, name):
    s = app.MultiService(name)
    NewsService(os.path.join(storage, "newsdb"), "news", s) 
    SyndicationService(os.path.join(storage, "synddb"), "syndication", s) 
    return s

And have config files that look like

'''
# File: tnntp2.tpy
#
# Location of internal storage
storage = "/var/lib/tnntp"

from tnntprss import simpGetTNNTPService
from twisted.internetet import app

application = app.Application("tnntprss")
simpGetTNNTPService(storage, "tnntp").setServiceParent(application)
'''

And again

% twistd -y tnntp2.tpy

works.

Another thing you can do when you convert to a package [and you should,
regardless!] is to supply tap plugins that would look like

# tnntprss/tap.py
from twisted.python import usage
from tnntprss import simpGetTNNTPService

class Options(usage.Options):

    optParameters = [['storage', 's', '/var/lib/tnntp', "Where to store files"]]

def updateApplication(application, config):
    simpGetTNNTPService(config['storage'], "tnntp"
    ).setServiceParent(application)

and then put a file like

# tntprss/plugins.tml
register("TNNTPRss", "tnntprss.tap", type='tap', tapname='tnntprss')

So that the user can

% mktap tnntprss --storage=/home/moshez/.temp
% twistd -f tnntprss.tap

Other remarks:

* startScheduler should be called from startService, not __init__, to
  support persistence
* similarily, all reactor.callLater() results should be saved from
  an attribute of the SyndicationService, which is then .cancelled()
  in stopService
* 9119 is a hardcoded number. Probably should be given in an argument
  to news service. Of course, the above get...Service() calls should
  be modified to accept that argument, blah blah blah.
* I'd prefer it if .listenTCP was not called from __init__, but instead,
  an easy way to get the right factory was supplied:

class NewsService(...):
    ...
    def getFactory(self):
        return news.UsenetServerFactory(self.backend, None)

Then, in the configurator:

s = simpGetTNNTPService(storage, "tnntp")
s.setServiceParent(application)
application.listenTCP(119, s.getServiceNamed("news").getFactory())
[and again, mutatis mutandis for the other options]

Rationale: easier support for non-common listening options [what if you
want to only listen on 127.0.0.1, so that only you can read the news?
what if you wanted to listen on several ports to defeat firewalls?]

* Nit: rssparser.py is a DOS file, all other are unix files.

I hope that matches your expectations of how thoroughly we can pick
on you :)
-- 
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/




More information about the Twisted-Python mailing list