toolbox.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. test_config = """
  2. <qgisgrass name="Test config">
  3. <modules>
  4. <section label="First Section">
  5. <grass name="g.gisenv"/>
  6. <grass name="d.vect"/>
  7. </section>
  8. <section label="Second Section">
  9. <grass name="r.to.vect.line"/>
  10. <grass name="r.to.vect.area"/>
  11. </section>
  12. <grass name="v.buffer"/>
  13. </modules>
  14. </qgisgrass>
  15. """
  16. test_modules = {"g.gisenv":"""
  17. <qgisgrassmodule label="GRASS environment variables" module="g.gisenv">
  18. </qgisgrassmodule>
  19. ""","r.to.vect.area":"""
  20. <qgisgrassmodule label="Convert a raster to vector areas" module="r.to.vect">
  21. <option key="input" />
  22. <option key="output" />
  23. <option key="feature" answer="area" hidden="yes" />
  24. <flag key="s" answer="on" hidden="yes" />
  25. </qgisgrassmodule>
  26. ""","r.to.vect.line":"""
  27. <qgisgrassmodule label="Convert a raster to vector areas" module="r.to.vect">
  28. <option key="input" />
  29. <option key="output" />
  30. <option key="feature" answer="line" hidden="yes" />
  31. <flag key="s" answer="on" hidden="yes" />
  32. </qgisgrassmodule>
  33. ""","v.surf.idw":"""
  34. <qgisgrassmodule label="Interpolate attribute values (IDW)" module="v.surf.idw">
  35. <option key="input" layeroption="layer" typemask="point,line" id="input" />
  36. <field key="column" layerid="input" type="integer,double" label="Attribute field (interpolated values)" />
  37. <option key="npoints" />
  38. <option key="output" />
  39. </qgisgrassmodule>
  40. """
  41. }
  42. import xml.dom.minidom
  43. import menuform
  44. class handleQgisGrass:
  45. def __init__(self, qgisgrass):
  46. modules = qgisgrass.childNodes[0].GetElementsByTagName('modules')[0]
  47. for child in modules.childNodes:
  48. if child.localName == 'grass':
  49. self.handleGrass( child )
  50. elif child.localName == 'section':
  51. self.handleSection( child )
  52. def handleSection( self,section ):
  53. for child in section.GetElementsByTagName('grass'):
  54. self.handleGrass( child )
  55. def handleGrass( self, grass ):
  56. raise NotImplementedError
  57. class printQgisGrass( handleQgisGrass ):
  58. def __init__(self, qgisgrass):
  59. print "in qgisgrass"
  60. handleQgisGrass.__init__( self, qgisgrass )
  61. def handleSection( self,section ):
  62. print "Section:",section.getAttribute('label')
  63. handleQgisGrass.handleSection( self, section )
  64. def handleGrass( self, grass ):
  65. print "Command:",grass.getAttribute('name')
  66. class wxQgisGrass( handleQgisGrass ):
  67. pass
  68. class handleQgisGrassModule:
  69. def __init__( self, qgisgrassmodule):
  70. qgisgrassm = qgisgrassmodule.GetElementsByTagName( "qgisgrassmodule" )[0]
  71. self.handleAttributes( qgisgrassm )
  72. for inner in qgisgrassm.childNodes:
  73. it = inner.localName
  74. if it == 'option':
  75. self.handleOption( inner )
  76. elif it == 'flag':
  77. self.handleFlag( inner )
  78. elif it is not None:
  79. self.handleOther( inner )
  80. def handleAttributes( self, node ):
  81. for (l,a) in node.attributes.items():
  82. self.handleAttribute( l, a, node )
  83. def handleAttribute( self, label, value, parent ):
  84. raise NotImplementedError
  85. def handleOption( self, option ):
  86. self.handleAttributes( option )
  87. def handleFlag( self, flag ):
  88. self.handleAttributes( flag )
  89. def handleOther( self, other ):
  90. self.handleAttributes( other )
  91. class printQgisGrassModule( handleQgisGrassModule ):
  92. def __init__( self, qgisgrassmodule):
  93. self.handleOption = self.printHandler
  94. self.handleFlag = self.printHandler
  95. self.handleOther = self.printHandler
  96. print "in qgisgrassmodule"
  97. handleQgisGrassModule.__init__( self, qgisgrassmodule )
  98. def printHandler( self, opt ):
  99. print opt.localName
  100. handleQgisGrassModule.handleOther( self, opt )
  101. print
  102. def handleAttribute( self, label, value, option ):
  103. print "%s:%s" % (label, value)
  104. class wxQgisGrassModule( handleQgisGrassModule ):
  105. def __init__(self, qgisgrassmodule, label='' ):
  106. """qgisGrassModule is a string containing the .qgm xml file"""
  107. self.task = None
  108. self.label = label
  109. self.description = ''
  110. handleQgisGrassModule.__init__( self, qgisgrassmodule )
  111. self.task.description = self.description
  112. menuform.GrassGUIApp( self.task ).MainLoop()
  113. def handleOption( self, opt, getit = menuform.grassTask.get_param, **other ):
  114. a = dict(opt.attributes.items())
  115. p = getit( self.task, a['key'] )
  116. # visibility:
  117. p['guisection'] = _( 'Main' ) # this should be the only tab present
  118. p['hidden'] = 'no' # unhide params ...
  119. if a.get('hidden','no') == 'yes': p['hidden'] = 'yes' # ...except when explicitly hidden
  120. # overrides:
  121. if a.has_key( 'answer' ): p['value'] = a['answer']
  122. if a.has_key( 'label' ): p['description'] = a['label']
  123. for (k,i) in other.items():
  124. p[k] = i
  125. # exclusions:
  126. if a.has_key('exclude'):
  127. vals = p['value'].split(',')
  128. exclusions = a['exclude'].split( ',' )
  129. for excluded in exclusions:
  130. if excluded in vals:
  131. idx = vals.index(excluded)
  132. vals[ idx:idx+1 ] = []
  133. p['value'] = ','.join( vals )
  134. if p.get('default','') in exclusions: p['default'] = ''
  135. def handleOther( self, opt ):
  136. tag = opt.localName
  137. att = dict( opt.attributes.items() )
  138. if tag == 'field':
  139. pass
  140. elif tag == 'file':
  141. filters = dict(opt.attributes.items()).get('filters','(*.*)')
  142. try:
  143. glob = re.match(r'\((.+)\)').groups(0)
  144. except KeyError:
  145. glob = '*.*'
  146. self.handleOption( opt, gisprompt=True, element='file', filemask=glob )
  147. elif tag == 'selection':
  148. pass
  149. def handleFlag( self, flag ):
  150. self.handleOption( flag, getit = menuform.grassTask.get_flag )
  151. def handleAttribute( self, label, value, option ):
  152. if option.localName == 'qgisgrassmodule':
  153. if label=='module':
  154. self.task = menuform.grassTask( grassModule = value )
  155. for pf in self.task.params + self.task.flags:
  156. pf['hidden'] = 'yes' # hide eveything initially
  157. if label=='label':
  158. self.description = value
  159. from sys import argv
  160. if __name__ == '__main__':
  161. if len( argv ) != 2:
  162. print "Usage: %s <toolbox command>" % sys.argv[0]
  163. else:
  164. the_module = argv[1]
  165. if the_module != 'test':
  166. qgm = open( the_module ).read()
  167. x = wxQgisGrassModule( xml.dom.minidom.parseString( qgm ), label = the_module )
  168. else:
  169. # self test
  170. config = xml.dom.minidom.parseString( test_config )
  171. printQgisGrass( config )
  172. print
  173. for m in test_modules.keys():
  174. print m
  175. module = xml.dom.minidom.parseString( test_modules[m] )
  176. printQgisGrassModule( module )
  177. print "----------------"
  178. m = "r.to.vect.area"
  179. x = wxQgisGrassModule( xml.dom.minidom.parseString( test_modules[ m ] ), label = m )