[GRASS-dev] Using a dynamic text in module header for parser

Michel Wortmann wortmann at pik-potsdam.de
Tue Oct 13 06:33:27 PDT 2015


Hi Stefan,
I have been meaning to implement something like this too and got your 
example to work with the additional step of setting the environment 
variable GRASS_ADDON_PATH (and under unix setting the permissions). Here 
is my example just parsing the first mapset it finds:

import os, tempfile, stat
import grass.script as grass

module = 'subbasins.py'

if __name__=='__main__':
     answer = grass.mapsets(search_path = True)
     available_mapsets = grass.mapsets()

     script=file(module,'r').read()

     with tempfile.NamedTemporaryFile(delete = False) as s:
         s.write('''#!/usr/bin/env python
# -*- coding: utf-8 -*-

{script}

#%Option
#% key:{option}
#%end

'''.format(script=script,option=available_mapsets[0].lower()))

     startcmd = 'python '+s.name
     # set env variable
     aoev='GRASS_ADDON_PATH'
     tmpdir = os.path.basename(s.name)
     if aoev in os.environ:
         os.environ[aoev]=[tmpdir]+os.environ[aoev]
     else:
         os.environ[aoev]=[tmpdir]
     # set permissions
     os.chmod(s.name, os.stat(s.name).st_mode | stat.S_IEXEC)
     # start module
     os.system(startcmd)
     os.remove(s.name)

Let me know if this works under Windows too.
Michel

On 10/12/2015 12:55 PM, Blumentrath, Stefan wrote:
>
> Hi again,
>
> Now I found out that the parser section in the inner python script was 
> not formated properly.
>
> However, when I now call the inner script with ‘--ui’ I get an error:
>
> Unable to fetch interface description for command ‘tmpjksdfjiol’
>
> Details:
>
> Try to set up GRASS_ADDON_PATH or GRASS_ADDON_BASE variable.
>
> It was neither possible to run the script using grass.run_command() 
> (on Windows).
>
> Cheers
>
> Stefan
>
> *From:*grass-dev [mailto:grass-dev-bounces at lists.osgeo.org] *On Behalf 
> Of *Blumentrath, Stefan
> *Sent:* 12. oktober 2015 11:36
> *To:* GRASS developers list (grass-dev at lists.osgeo.org) 
> <grass-dev at lists.osgeo.org>
> *Subject:* [GRASS-dev] Using a dynamic text in module header for parser
>
> Hi,
>
> I would like to fill:
>
> #% options:
>
> and
>
> #% answer
>
> in a parser option for a python script dynamically.  In particular I 
> want to have tickboxes for available mapsets in the module GUI…
>
> Meaning something like this  (but less complex / not interactive):
>
> http://permalink.gmane.org/gmane.comp.gis.grass.gui/667
>
> My amateur programming skills do unfortunately not allow me to really 
> understand how to accomplish what Glynn describes in the post above…
>
> I tried to generate and run a temporary python script from within my 
> script like this:
>
> def main():
>
>     answer = grass.mapsets(search_path = True)
>
>     available_mapsets = str(grass.mapsets())
>
>     with tempfile.NamedTemporaryFile(delete = False) as s:
>
>         s.write(''' #!/usr/bin/env python
>
> # -*- coding: utf-8 -*-
>
> # Here comes the full module with header for the parser
>
> #% options: ''' + str(','.join(available_mapsets))
>
>>
> ’’’)
>
>     startcmd = 'python ' + s.name
>
>     os.system(startcmd)
>
>     os.remove(s.name)
>
> But that way the GUI never starts, when I call the outer script from 
> GRASS and it seems that option never get parsed…
>
> Any hints how to proceed?
>
> Thanks for helping in advance.
>
> Stefan
>
>
>
> _______________________________________________
> grass-dev mailing list
> grass-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-dev/attachments/20151013/5d28ae85/attachment.html>


More information about the grass-dev mailing list