[GRASS-SVN] r65202 - grass/trunk/display/d.mon
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat May 9 12:18:36 PDT 2015
Author: martinl
Date: 2015-05-09 12:18:36 -0700 (Sat, 09 May 2015)
New Revision: 65202
Added:
grass/trunk/display/d.mon/render_cmd.py
Modified:
grass/trunk/display/d.mon/Makefile
grass/trunk/display/d.mon/start.c
Log:
d.mon: store default render command program in a separate file
Modified: grass/trunk/display/d.mon/Makefile
===================================================================
--- grass/trunk/display/d.mon/Makefile 2015-05-09 18:17:06 UTC (rev 65201)
+++ grass/trunk/display/d.mon/Makefile 2015-05-09 19:18:36 UTC (rev 65202)
@@ -2,6 +2,8 @@
PGM = d.mon
+ETCFILES = render_cmd.py
+
LIBES = $(GISLIB) $(DISPLAYLIB)
DEPENDENCIES= $(GISDEP) $(DISPLAYDEP)
Added: grass/trunk/display/d.mon/render_cmd.py
===================================================================
--- grass/trunk/display/d.mon/render_cmd.py (rev 0)
+++ grass/trunk/display/d.mon/render_cmd.py 2015-05-09 19:18:36 UTC (rev 65202)
@@ -0,0 +1,52 @@
+#!/usr/bin/env python
+import os
+import sys
+
+from grass.script import core as grass
+from grass.script import task as gtask
+
+cmd, dcmd = gtask.cmdstring_to_tuple(sys.argv[1])
+if not cmd or cmd == 'd.mon':
+ sys.exit(0)
+
+path = os.path.dirname(os.path.abspath(__file__))
+cmd_file = os.path.join(path, 'cmd')
+env_file = os.path.join(path, 'env')
+
+# read environment variables from file
+fd = open(env_file, 'r')
+if fd is None:
+ grass.fatal("Unable to open file '%s'" % env_file)
+lines = fd.readlines()
+for l in lines:
+ if l.startswith('#'):
+ continue
+ k, v = l.rstrip('\n').split('#', 1)[0].strip().split('=', 1)
+ os.environ[k] = v
+fd.close()
+
+# run display command
+try:
+ grass.run_command(cmd, **dcmd)
+except Exception as e:
+ sys.exit("ERROR: %s" % e)
+
+# update cmd file
+ignoredCmd = ('d.colorlist', 'd.font', 'd.fontlist',
+ 'd.frame', 'd.info', 'd.mon', 'd.out.file',
+ 'd.redraw', 'd.to.rast', 'd.what.rast',
+ 'd.what.vect', 'd.where')
+if cmd not in ignoredCmd:
+ mode = 'w' if cmd == 'd.erase' else 'a'
+ # update cmd file
+ fd = open(cmd_file, mode)
+ if fd is None:
+ grass.fatal("Unable to open file '%s'" % cmd_file)
+ if mode == 'a':
+ fd.write(sys.argv[1])
+ fd.write('\n')
+ else:
+ fd.write('')
+ fd.close()
+
+sys.exit(0)
Property changes on: grass/trunk/display/d.mon/render_cmd.py
___________________________________________________________________
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
Modified: grass/trunk/display/d.mon/start.c
===================================================================
--- grass/trunk/display/d.mon/start.c 2015-05-09 18:17:06 UTC (rev 65201)
+++ grass/trunk/display/d.mon/start.c 2015-05-09 19:18:36 UTC (rev 65202)
@@ -110,8 +110,7 @@
char *mon_path;
char *out_file, *env_file, *cmd_file;
char buf[1024];
- char file_path[GPATH_MAX];
- char *pycode;
+ char file_path[GPATH_MAX], render_cmd_path[GPATH_MAX];
int fd;
if (check_mon(name)) {
@@ -136,55 +135,11 @@
cmd_file = G_store(file_path);
/* create py file (renderer) */
+ sprintf(render_cmd_path, "%s/etc/d.mon/render_cmd.py", getenv("GISBASE"));
G_file_name(file_path, mon_path, "render.py", G_mapset());
G_debug(1, "Monitor name=%s, pyfile = %s", name, file_path);
- fd = creat(file_path, 0666);
- G_asprintf(&pycode,
- "#!/usr/bin/env python\n\n"
- "import os\n"
- "import sys\n\n"
- "from grass.script import core as grass\n"
- "from grass.script import task as gtask\n\n"
- "cmd, dcmd = gtask.cmdstring_to_tuple(sys.argv[1])\n"
- "if not cmd or cmd == 'd.mon':\n"
- " sys.exit(0)\n\n"
- "ignoredCmd = ('d.colorlist', 'd.font', 'd.fontlist',\n"
- " 'd.frame', 'd.info', 'd.mon', 'd.out.file',\n"
- " 'd.redraw', 'd.to.rast', 'd.what.rast',\n"
- " 'd.what.vect', 'd.where')\n"
- "if cmd not in ignoredCmd:\n"
- " mode = 'w' if cmd == 'd.erase' else 'a'\n\n"
- " # update cmd file\n"
- " fd = open('%s', mode)\n"
- " if fd is None:\n"
- " grass.fatal(\"Unable to open file '%s'\")\n"
- " if mode == 'a':\n"
- " fd.write(sys.argv[1])\n"
- " fd.write('\\n')\n"
- " else:\n"
- " fd.write('')\n"
- " fd.close()\n\n"
- "# read env file\n"
- "fd = open('%s', 'r')\n"
- "if fd is None:\n"
- " grass.fatal(\"Unable to open file '%s'\")\n"
- "lines = fd.readlines()\n"
- "for l in lines:\n"
- " if l.startswith('#'):\n"
- " continue\n"
- " k, v = l.rstrip('\\n').split('#', 1)[0].strip().split('=', 1)\n"
- " os.environ[k] = v\n"
- "fd.close()\n\n"
- "# run display command\n"
- "try:\n"
- " grass.run_command(cmd, **dcmd)\n"
- "except:\n"
- " pass\n\n"
- "sys.exit(0)\n",
- cmd_file, cmd_file, env_file, env_file);
- write(fd, pycode, strlen(pycode));
- G_free(pycode);
- close(fd);
+ if (1 != G_copy_file(render_cmd_path, file_path))
+ G_fatal_error(_("Unable to copy render command file"));
/* start monitor */
if (strncmp(name, "wx", 2) == 0)
More information about the grass-commit
mailing list