I'm not completely sure this is the right place to talk about paste-
script, but I didn't find anything else...
Anyway we thought to adopt paste-script in our company to generate and
eventually manipulate our python application (since there is quite a
lot of boiler-plate).
Paste-script seems really to be the right tool, the only thing is that
the documentation is a bit lacking and I'm a bit lost on a few
things...
1. when I create a template with a few variables, I would like to make
one of the variables depending on another one.
So if I do simply
var('project'...), var('camelized_project'), the second one should
be assigne d to the camelized version of the first.
Reading also the code it doesn't seem so easy.
So I thought to do it with cheetah, and I created and imported a
function.
I can't, however, import the function (which path should I use),
the name of the module is not in the namespace...
2. a project template should be composed by many smaller templates
(which can be single files or a bunch of files).
I think that I should use "required_templates" for that, or is
there any other way?
But the mechanism doesn't look very dynamic too...
Any advise or some nice example code that I could take a look at?
Thanks,
Andrea
> I'm not completely sure this is the right place to talk about paste- > script, but I didn't find anything else...
> Anyway we thought to adopt paste-script in our company to generate and > eventually manipulate our python application (since there is quite a > lot of boiler-plate).
> Paste-script seems really to be the right tool, the only thing is that > the documentation is a bit lacking and I'm a bit lost on a few > things...
> 1. when I create a template with a few variables, I would like to make > one of the variables depending on another one. > So if I do simply > var('project'...), var('camelized_project'), the second one should > be assigne d to the camelized version of the first. > Reading also the code it doesn't seem so easy.
You should probably override the Template.check_vars() method and just handle it manually.
> So I thought to do it with cheetah, and I created and imported a > function. > I can't, however, import the function (which path should I use), > the name of the module is not in the namespace...
With Tempita you can also always do:
{{py: if not camelized_project: camelized_project = camelize(project)
}}
But it would have to be in each .tmpl file I think. Better to use check_vars.
> 2. a project template should be composed by many smaller templates > (which can be single files or a bunch of files). > I think that I should use "required_templates" for that, or is > there any other way? > But the mechanism doesn't look very dynamic too...
No, unfortunately it is not, though you could override .run() and import and execute other templates manually, instead of having pastescript try to figure out what templates to run through required_templates.
> Any advise or some nice example code that I could take a look at?
ZopeSkel has the largest collection of templates, I think, but I haven't looked at how specifically they've implemented them.
> You should probably override the Template.check_vars() method and just > handle it manually.
Makes sense, but how would I do this anyway? I mean I still have to declare somehow the dependencies in the variables.
The only reasonable way that I see is to modify the "var" class adding def __init__(self,.... other_var=None, transform_function=None)
And then passing the right function to transform one variable into the other. The problem them is that I would have to modify the code in place, (unless maybe I make a "var" wrapper overriding __init__), which is a bit hackish solution.
What other possibility would I have?
> 2. a project template should be composed by many smaller templates > (which can be single files or a bunch of files). > I think that I should use "required_templates" for that, or is > there any other way? > But the mechanism doesn't look very dynamic too...
> No, unfortunately it is not, though you could override .run() and > import and execute other templates manually, instead of having > pastescript try to figure out what templates to run through > required_templates.
Mmm that's a bit sad, required_template itself might also actually be fine in theory, but then I would have to find a way to extract a sub-template in the right (generated) sub-directory for example, which is also a bit hard to do I reckon...
> You should probably override the Template.check_vars() method and just > handle it manually.
> Makes sense, but how would I do this anyway? > I mean I still have to declare somehow the dependencies in the variables.
> The only reasonable way that I see is to modify the "var" class adding > def __init__(self,.... other_var=None, transform_function=None)
> And then passing the right function to transform one variable into the > other. > The problem them is that I would have to modify the code in place, (unless > maybe I make a "var" wrapper overriding __init__), which is a bit hackish > solution.
> 2. a project template should be composed by many smaller templates >> (which can be single files or a bunch of files). >> I think that I should use "required_templates" for that, or is >> there any other way? >> But the mechanism doesn't look very dynamic too...
> No, unfortunately it is not, though you could override .run() and import > and execute other templates manually, instead of having pastescript try to > figure out what templates to run through required_templates.
> Mmm that's a bit sad, required_template itself might also actually be fine > in theory, but then I would have to find a way to extract a sub-template in > the right (generated) sub-directory for example, which is also a bit hard to > do I reckon...
Ok then I followed your advises (I hope) and I did something as below.
So in the run I instantiate three subtemplates and I iterate over them. Then check_vars has a new variable which is the camelized version.
There are two problems: - the camelized variable seems as the camelized of the default variable, and doesn't change after I set a value. I have the impression that without adding some intelligence to "var" is not going to work in this way...
- the template expansion gets stuck here: File "/usr/lib/python2.7/site-packages/paste/script/command.py", line 218, in run result = self.command() File "/usr/lib/python2.7/site-packages/paste/script/create_distro.py", line 133, in command vars['egg_plugins'] = egg_plugins TypeError: 'NoneType' object does not support item assignment
And debugging I see that *vars* is in fact None, but I don't get why, because right before I call the "super" in check_vars it's actually still a dictionary...
.... class PSIEgg(Template): summary = 'basic egg template' _template_dir = 'psi_egg' vars = []
class PSIProject(Template): """Template for a new PSI project """
egg_plugins = ['PSIManage'] summary = 'Template for creating a basic PSI application structure' _template_dir = 'template' use_cheetah = True # the vars should be asked and filled in automatically vars = [ var('application', 'Application Name', default='PSI application'), #TODO: another transformation for the plugin name var('plugin_name', 'Name of the plugin', default='PSI Plugin') ]
# override the "run" method # + copy variables # + run all the subtemplates in the sub_directory def run(self, command, output_dir, vars): subtemplates = [PSIApp(), PSIEgg(), PSIProject()]
new_vars = vars.copy() new_vars['something'] = 'another thing' # TODO: check that it's actually expanding in the right place for sub in subtemplates: sub.run(command, output_dir, new_vars)