Quest for Massage, Part 4.1
As I noted, the 'tidy' function in the mx extentions requires a true file object as its input. What I want to give it is the results of urlopen. My initial solution was to use a temporary file. It worked, but I didn't like the explicit steps I had to go through.
When you have an X, and you need a Y, you are looking at a use for the Adapter Pattern. So I started working on a adaptor that could take a file-like object, and give me a true file object. Here's my initial look at it: import types import os class FileAdapter: """A FileAdapter instance takes a 'file-like' object having at least a 'read' method and, via the file method, returns a true file object.""" def __init__(self, fileObj): self.fileObj = fileObj self.chunksize = 1024 * 10 if type(self.fileObj) != types.FileType: if not hasattr(fileObj, "read"): raise ValueError, "not a file-like object" self.tmpFileObj = os.tmpfile() while True: data = fileObj.read(self.chunksize) if len(data) == 0: break self.tmpFileObj.write(data) del data self.tmpFileObj.flush() self.tmpFileObj.seek(0, 0) self.fileObj = self.tmpFileObj return def file(self): return self.fileObj
Note that I'm still using a temporary file, but now my interface to it is much cleaner. I can use it like this:
realFileObj = FileAdapter(urlopen("http://www.cnn.com")).file()
This takes the file-like object returned by urlopen, reads in all of its data to a temporary file, and returns that file as the real file object.
This gives me what I need to make a url file-like object play nice with tidy. But you know, maybe I can do better. It's pretty obvious that tidy works like a filter. I'd like to treat it like one, from within my program. That bears thought.
9:06:50 PM
|