[GRASS-SVN] r53845 - grass/trunk/lib/python/pygrass/modules
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Nov 16 06:26:38 PST 2012
Author: zarch
Date: 2012-11-16 06:26:37 -0800 (Fri, 16 Nov 2012)
New Revision: 53845
Modified:
grass/trunk/lib/python/pygrass/modules/__init__.py
Log:
Fix flags support of the modules, and add the capability to export the Module object into a bash and python string
Modified: grass/trunk/lib/python/pygrass/modules/__init__.py
===================================================================
--- grass/trunk/lib/python/pygrass/modules/__init__.py 2012-11-16 10:52:10 UTC (rev 53844)
+++ grass/trunk/lib/python/pygrass/modules/__init__.py 2012-11-16 14:26:37 UTC (rev 53845)
@@ -11,12 +11,12 @@
try:
from collections import OrderedDict
-except:
+except ImportError:
from pygrass.orderdict import OrderedDict
from itertools import izip_longest
from xml.etree.ElementTree import fromstring
-import numpy as np
+
import grass
@@ -185,13 +185,21 @@
# in this case we define which function must be use to get/set the value
value = property(fget=_get_value, fset=_set_value)
- def __str__(self):
+ def get_bash(self):
if isinstance(self._value, list) or isinstance(self._value, tuple):
value = ','.join([str(v) for v in self._value])
else:
value = str(self._value)
return """%s=%s""" % (self.name, value)
+ def get_python(self):
+ if not self.value:
+ return ''
+ return """%s=%r""" % (self.name, self._value)
+
+ def __str__(self):
+ return self.get_bash()
+
def __repr__(self):
str_repr = "Parameter <%s> (required:%s, type:%s, multiple:%s)"
return str_repr % (self.name,
@@ -251,7 +259,7 @@
self.default = diz.get('default', None)
self.guisection = diz.get('guisection', None)
- def __str__(self):
+ def get_bash(self):
if self.value:
if self.special:
return '--%s' % self.name[0]
@@ -260,6 +268,18 @@
else:
return ''
+ def get_python(self):
+ if self.value:
+ if self.special:
+ return '%s=True' % self.name
+ else:
+ return self.name
+ else:
+ return ''
+
+ def __str__(self):
+ return self.get_bash()
+
def __repr__(self):
return "Flag <%s> (%s)" % (self.name, self.description)
@@ -353,6 +373,7 @@
self.inputs = TypeDict(Parameter)
self.outputs = TypeDict(Parameter)
self.required = []
+
# Insert parameters into input/output and required
for par in self.params_list:
if par.input:
@@ -373,7 +394,6 @@
#
# Add new attributes to the class
#
- self._flags = ''
self.run_ = True
self.finish_ = True
self.stdin_ = None
@@ -385,18 +405,26 @@
self.__call__(*args, **kargs)
def _get_flags(self):
- return self._flags
+ return ''.join([flg.get_python() for flg in self.flags_dict.values()
+ if not flg.special])
def _set_flags(self, value):
if isinstance(value, str):
- flgs = [flg for flg in self.flags_dict
- if not self.flags_dict[flg].special]
- # we need to check if the flag is valid, special flags are not
- # allow
- if value in flgs:
- self._flags = value
+ if value == '':
+ for flg in self.flags_dict.values():
+ if not flg.special:
+ flg.value = False
else:
- raise ValueError('Flag not valid, valid flag are: %r' % flgs)
+ flgs = [flg.name for flg in self.flags_dict.values()
+ if not flg.special]
+ # we need to check if the flag is valid, special flags are not
+ # allow
+ for val in value:
+ if val in flgs:
+ self.flags_dict[val].value = True
+ else:
+ str_err = 'Flag not valid: %r, valid flag are: %r'
+ raise ValueError(str_err % (val, flgs))
else:
raise TypeError('The flags attribute must be a string')
@@ -462,7 +490,7 @@
if flag in self.flags_dict:
self.flags_dict[flag].value = True
else:
- raise FlagError('Flag "%s" not valid.')
+ raise FlagError('Flag "%s" not valid.' % flag)
#
# check if execute
@@ -470,6 +498,28 @@
if self.run_:
self.run()
+ def get_bash(self):
+ return ' '.join(self.make_cmd())
+
+ def get_python(self):
+ prefix = self.name.split('.')[0]
+ name = '_'.join(self.name.split('.')[1:])
+ params = ', '.join([par.get_python() for par in self.params_list
+ if par.get_python() != ''])
+ special = ', '.join([flg.get_python()
+ for flg in self.flags_dict.values()
+ if flg.special and flg.get_python() != ''])
+ # pre name par flg special
+ if self.flags and special:
+ return "%s.%s(%s, flags=%r, %s)" % (prefix, name, params,
+ self.flags, special)
+ elif self.flags:
+ return "%s.%s(%s, flags=%r)" % (prefix, name, params, self.flags)
+ elif special:
+ return "%s.%s(%s, %s)" % (prefix, name, params, special)
+ else:
+ return "%s.%s(%s)" % (prefix, name, params)
+
def __str__(self):
return ' '.join(self.make_cmd())
@@ -495,13 +545,12 @@
if par.value is not None:
args.append(str(par))
for flg in self.flags_dict:
- if self.flags_dict[flg].value is not None:
+ if self.flags_dict[flg].value:
args.append(str(self.flags_dict[flg]))
return args
def run(self, node=None):
cmd = self.make_cmd()
- #print(repr(cmd))
self.popen = subprocess.Popen(cmd, stdin=self.stdin_,
stdout=self.stdout_,
stderr=self.stderr_)
More information about the grass-commit
mailing list