Accessing context from finalizeFunction

Asked by Yannig Robert on 2017-11-04

Thank you for the appy.pod module, it's really useful.

I am a structural engineer trying to automate some repetitive design calculations. So I have some python classes and functions that do the actual calculations and would like to find a way to generate a writer document documenting the calcs.

I was really happy to find appy.pod, I can make it work for a general document but when I have formula that I normally type with math directly within writer I am stuck as I can't insert fields in there.

Then I realised that each formula was an object inside the odt zippedfile filed in a folder named "Object x" with x a number. I discovered the finalizeFunction and managed to list the relevant directories and files with this function passed to finalizeFunction

 def postprocess(path):
    ...: import glob,fnmatch,os
    ...: for dirobj in (glob.glob(path+"\\Object*")) :
    ...: listxml = fnmatch.filter(os.listdir(dirobj),'*.xml')
    ...: print dirobj , " = ",listxml

I thought well in my formula in the odt template, I will write my variables as $variablename$ and will replace them doing some postprocessing with the finalizeFunction. When I tried to access context to access the variables that I had passed through renderer, I realised that they were not in the scope...

So I am stuck, any idea how I could access the context variables from within this function? Alternatively any way to have variables in my formulae that can be updated by appy.pod ?

Question information

Language:
English Edit question
Status:
Solved
For:
Appy Edit question
Assignee:
No assignee Edit question
Solved by:
Gaëtan Delannay
Solved:
2017-11-05
Last query:
2017-11-05
Last reply:
2017-11-05

Hi,
Thanks !
What I could implement to quickly handle your problem is to give the Renderer as an additional parameter to the "finalize" function. In the future, of couse, support for POD expressions and statements within formulae would be a must.

Could you try to apply this patch in POD, in Renderer.py, within method "finalize". Instead of calling:

self.finalizeFunction(self.unzipFolder)

Call it with:
self.finalizeFunction(self.unzipFolder, self)

Then, all Renderer attributes are available to your function. The original context you gave to the renderer is in:
renderer.originalContext

The real context, potentially updated after execution, is in:
renderer.contentParser.env.context

If this works for you, I will integrate this update in the next release. Please let me know!
Best regards
Gaetan

Yannig Robert (yrobert) said : #2

Thanks Gaëtan Delannay, that solved my question.

Yannig Robert (yrobert) said : #3

Thank you for the really fast reply.
I've done the patch and now I can access the variables that I have passed. I need to do the replacing bit. I don't have time right now but that should be fairly easy.