Writing a resource like PythonScript

I'm trying to write a resource class like the twisted.web.script.PythonScript class. As an example, I've got this in a file named TestResource.py: from twisted.web.resource import Resource class TestScript(resource.Resource): isLeaf = 1 def __init__(self, filename, registry): self.filename = filename self.registry = registry def render(self, request): request.setHeader("x-powered-by","Twisted/%s" % copyright.version) request.write("We've arrived in TestScript.render") request.finish() return server.NOT_DONE_YET However, when I go to create a .tap file using the command: mktap web --path=webroot --processor=".cht=TestResource.TestScript" --port=8880 I get the error: NameError: name 'resource' is not defined Now I've seen that almost all (if not all) the examples of deriving from Resource have a line like "resource = MyResource()", the PythonScript class is conspicuous in that it doesn't have such a line, and in fact is in the middle of a file with other Resource classes where trying to assign some class instance to a variable named "resource" would be ambiguous. And in fact I've grepped the source of the twisted.web directory structure and can't find anyplace a PythonScript is explicitly instantiated. But I know it works, as I've created a .epy file which does what I'd expect in a basic web.tap application. So my question is, how can I create a class like the PythonScript class, which doesn't explicitly declare a "resource" variable, can get associated with a file extension via the "mktap --processor" command, and then behaves intuitively at runtime? Many thanks, Donnie P.S. Somewhere in the docs there should be a comment that if you use the --processor flag for mktap on windows, you need to surround the "ext=processor" part of the parameters with double-quotes (see above). If you don't do that, mktap will complain about the options not having the right number of values to unpack. That took a while to figure out.

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
from twisted.web.resource import Resource
class TestScript(resource.Resource): ... I get the error:
NameError: name 'resource' is not defined
That won't fly. Either you do: from twisted.web.resource import Resource class TestScript(Resource): or you do: from twisted.web import resource class TestScript(resource.Resource): I prefer the latter. - -- Nicola Larosa - nico@tekNico.net "When a student asks why case matters, simply ask why it shouldn't matter. If they think it would be easier to use Python if they can be inconsistent in their use of case, then they have the answer. Python helps teach that consistency matters." -- Michael McLay, April 2004 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQFA+MTnXv0hgDImBm4RAkA3AKC2erojphiRF2IZkD3uDC/HlH2urwCgk0k4 f/SPpAw+ukk/xZGoVrpY/XE= =rbXc -----END PGP SIGNATURE-----

Thanks for the response - sorry about the user error. I have it doing pretty much what I want at this point. Donnie -----Original Message----- From: twisted-web-bounces@twistedmatrix.com [mailto:twisted-web-bounces@twistedmatrix.com] On Behalf Of Nicola Larosa Sent: Saturday, July 17, 2004 2:19 AM To: twisted-web@twistedmatrix.com Subject: [Twisted-web] Re: Writing a resource like PythonScript -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
from twisted.web.resource import Resource
class TestScript(resource.Resource): ... I get the error:
NameError: name 'resource' is not defined
That won't fly. Either you do: from twisted.web.resource import Resource class TestScript(Resource): or you do: from twisted.web import resource class TestScript(resource.Resource): I prefer the latter. - -- Nicola Larosa - nico@tekNico.net "When a student asks why case matters, simply ask why it shouldn't matter. If they think it would be easier to use Python if they can be inconsistent in their use of case, then they have the answer. Python helps teach that consistency matters." -- Michael McLay, April 2004 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (GNU/Linux) iD8DBQFA+MTnXv0hgDImBm4RAkA3AKC2erojphiRF2IZkD3uDC/HlH2urwCgk0k4 f/SPpAw+ukk/xZGoVrpY/XE= =rbXc -----END PGP SIGNATURE----- _______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
participants (2)
-
Donnie Hale
-
Nicola Larosa