Rendering GSP as Strings in Grails

In grails if you want to store GSP pages and render them as strings, you can! but its a little tricksy.

The process to do so does not seem to be very well documented, though it has been talked about recently and implemented in the mail plugin. If your goal is simply to render gsp directly to the response then it is much simpler but if you want to render the contents to a string, it gets a little weirder. Just spent the better half of 2 hours trying to figure this one out with (alot of) help from the bobbytek and mikenicholson over at #grails on freenode. It goes like this:

import org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.web.context.request.RequestContextHolder
import groovy.text.Template

class WorkshopsController {
	GroovyPagesTemplateEngine groovyPagesTemplateEngine
	private String renderTemplate(String content, Map model)
	{
		// create a template using the content, "somepage" is arbitrary
		def template = groovyPagesTemplateEngine.createTemplate(content, "somepage.gsp")

		// Get the current request and create a new output stream
		def requestAttributes = RequestContextHolder.getRequestAttributes()
		def strWriter = new StringWriter()

		// Replace the requests output stream with the new one, remember the old one!
		def originalOut = requestAttributes.getOut()
		requestAttributes.setOut(strWriter)

		// Render that bad boy, note the "writeTo" basically ignores the stream you give it
		// It renders straight to the response, thats what that stuff before was for
		try {
			def renderedTemplate = template.make(model)
			renderedTemplate.writeTo(strWriter)
		}
		catch(Exception e) {}
		finally {requestAttributes.setOut(originalOut)}

		return strWriter.toString()
	}
	def index = {
		def myStr = renderTemplate( "  asdasdas  ", [:])

		// &lt; myStr now ==  <a href="/file/download"> asdasdas </a>

		render myStr // Works as expected
	}
}

The comments and the links i’ve pasted above should get you most of the way to understanding whats going on here. The problem boils down to the writeTo function in the renderedTemplate. It seems to write directly to the request’s output stream. This means that if we want to see the results before they are rendered in a string we have to be a little sneaky and switch the output stream temporarily with our own.

Horrible… Just horrible

But it works!

enjoy :-)


Installing ipython properly on Snow Leopard

After following this awesome guide I correctly built and installed numpy and scipy on snow leopard. A very important factor in having snow leopard on my laptop as most of my PhD is heavily dependant on scipy’s magic.

However, I also use ipython. Rather alot actually. Through a combination of nose and a script called nosy I run most of my python programs constantly in the background as I program. The logical progression of this for me was being able to properly poke around variables and code as the script was running. I do this using ipython, which lets me arbitrarily stop the flow of the program and drops me into a shell with all the local/global variables as they should be at that point in the program. I’m heavily dependant on this form of programming in fact…

Which made it rather painful when it broke in snow leopard. The problem stems from a hack, as most problems do. IPython heavily uses a unix library called readline. However, Mac OS 10.* does not have libreadline, it has libedit, which is apparently something which looks a little like readline but isn’t the same. They clundged it so ipython runs without crashing, but doesn’t work correctly. In my case this manifests itself as weird rendering errors when going through previous lines of code.

So you need to install pyreadline. In 10.5 this is a matter of going

	sudo easy_install readline

On 10.6 it is not. This is how i went about fixing it. Firstly, run the command above. Apparently you can pass easy_install –editable to do the next step, but instead just look for URL from which easy_install trys to download readline, and get the tar.gz yourself:

	wget [URL from easy_install]
	tar xvfz readline-2.5.1.tar.gz
	cd readline-2.5.1

Right! Now you have the python package which easy_install usually builds and installs for you. You’re gonna edit the build steps a bit and build and install it manually. Firstly edit the file called rl/build.sh. From the rl directory, manually run the commands between tar and before configure. like this:


	cd rl
	tar xzvf readline-5.2.tar.gz
	mv readline-5.2 readline-lib
	cd readline-lib
	patch -p0 < ../readline52-001
	patch -p0 < ../readline52-002
	patch -p0 < ../readline52-003
	patch -p0 < ../readline52-004
	patch -p0 < ../readline52-005
	patch -p0 < ../readline52-006
	patch -p0 < ../readline52-007
	patch -p0 < ../readline52-008
	patch -p0 < ../readline52-009
	patch -p0 < ../readline52-010
	patch -p0 < ../readline52-011
	patch -p0 < ../readline52-012

And now comment out the whole file. The problem lies with the configuration of readline, and the hacky way it tried to figure out what version of Mac OS X you’re running. Open up the file rl/readline-lib/support/shobj-conf. In this file you should find a line that says something like:

	darwin[89]*)

Go ahead and replace this line with:

	darwin[89]*|darwin10*)

Scary stuff over hopefully! Now just go into the rl/readline-lib directory and run the normal:

	cd readline-lib
	./configure
	make

And finally, go into the base directory again and install the python package

	cd ../../
	python setup.py build
	sudo python setup.py install

And there you go! Now when you run ipython it should no longer tell you that it “found libedit” which basically means “oh god, i’ve not found readline, i’ve ONLY found libedit, FIX THIS!”

Now you might well notice this solution is horrible :-). Well thats because it really is. And i’m sure there will be a new release of readline or patch or something which sorts this out. Perhaps using readline-6.0, which compiles fine first time (their shobj-conf was where i got the change mentioned in this article). But there you go, i have no time to wait for such solutions.

Hope this helps!


Reading zip files from memory

Just starting to learn Ruby. To be honest it scares the crap out of me at the moment. But i have figured something out which i couldn’t find
anywhere else so thought id throw it up here.

Using rubyzip there seems to be no reasonable example explaining how to read a zip file from memory and get stuff from it.

By “from memory” i mean the result of something like

	f = File.open("some.zip")
	allString = f.read

The way forward can be found undocumented in the

Zip::ZipCentralDirectory

class. Specifically:

	c = Zip::ZipCentralDirectory.new
	c.read_from_stream StringIO.new(allString)
	c.entries.detect {|e| e.name.sub(/\/$/, "") == 
		"someFile".to_s.sub(/\/$/, "")} 
	// Where this line is the equivilant of ZipFile.get_entry

Hope that helps someone :)


Actually, I count no more than 28 problems

re: Jay-Z - 99 Problems

I couldn’t help but notice the amount of problems are grossly overestimated.

  1. Overly zealous censors policing his lyrics regarding his love of late 19th century rapid-fire weapons
  2. violent foes
  3. critics
  4. grew up in a bad area
  5. Famous radio stations sullying his name due to contractual obligations he would not honour
  6. Subsequently they don’t play his music
  7. Writers and reviewers in his field displaying his image without paying royalties
  8. Illegal contents in the trunk of his car
  9. Being stopped by police
  10. Young, black and poor choice of haberdasher
  11. Not a mind reader, definitely doesn’t look like one
  12. Speeding
  13. Lost the keys to various storage compartments of his car
  14. Hasn’t passed the bar exam regardless of a seemingly large knowledge base in traffic law procedure
  15. Imminent arrival of seemingly unwelcome dogs
  16. Had to forcefully resolve a situation involving a female.
  17. Not being able to make the distinction between women and weaker men
  18. Paranoid delusions of the existence of a ancestral paternal figure with whom he presumably seeks solace occasionally
  19. Often takes part in fruit fights, presumably low vitamin levels
  20. Paranoid Hallucinations involving membership of early 16th century pirate crew or military unit
  21. Fear of incarceration in confectionary
  22. Superiority complex
  23. Slight fear of mythical creatures scratching his floor quelled only by his deification of the paparazzi, who he assures will dispatch of these fiends
  24. Sexual abuse from high ranking holders of legal office
  25. Loss of 50% of his stake in a local mill due to race
  26. Harassed by various african americans
  27. Savoury behaviour
  28. Equally savoury weaponry

Next New Message in Mail.app

I’ve been annoyed with apple’s Mail app for a while now. For an operating system whose apps usually play so well with each other, Mail is a heaving pile of ass sometimes. There are several keyboard shortcut inconsistencies and wierdnesses. Furthermore, solving these problems is a rather non trivial process seeing as the applescript library for Mail is also thoroughly underpowered. Basically there are things that you want to do in mail without taking your hands off the keyboard that … well… you can’t! Turns out you can though… heres how

(more…)