model.py 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633
  1. """
  2. @package gmodeler.model
  3. @brief wxGUI Graphical Modeler (base classes & read/write)
  4. Classes:
  5. - model::Model
  6. - model::ModelObject
  7. - model::ModelAction
  8. - model::ModelData
  9. - model::ModelRelation
  10. - model::ModelItem
  11. - model::ModelLoop
  12. - model::ModelCondition
  13. - model::ModelComment
  14. - model::ProcessModelFile
  15. - model::WriteModelFile
  16. - model::WritePythonFile
  17. - model::ModelParamDialog
  18. (C) 2010-2014 by the GRASS Development Team
  19. This program is free software under the GNU General Public License
  20. (>=v2). Read the file COPYING that comes with GRASS for details.
  21. @author Martin Landa <landa.martin gmail.com>
  22. """
  23. import os
  24. import getpass
  25. import copy
  26. import re
  27. import mimetypes
  28. import time
  29. try:
  30. import xml.etree.ElementTree as etree
  31. except ImportError:
  32. import elementtree.ElementTree as etree # Python <= 2.4
  33. import xml.sax.saxutils as saxutils
  34. import wx
  35. from wx.lib import ogl
  36. from core import globalvar
  37. from core import utils
  38. from core.utils import _
  39. from core.gcmd import GMessage, GException, GError, RunCommand, EncodeString, GWarning, GetDefaultEncoding
  40. from core.settings import UserSettings
  41. from gui_core.forms import GUI, CmdPanel
  42. from gui_core.widgets import GNotebook
  43. from gmodeler.giface import GraphicalModelerGrassInterface
  44. from grass.script import core as grass
  45. from grass.script import task as gtask
  46. class Model(object):
  47. """Class representing the model"""
  48. def __init__(self, canvas = None):
  49. self.items = list() # list of ordered items (action/loop/condition)
  50. # model properties
  51. self.properties = { 'name' : _("model"),
  52. 'description' : _("Script generated by wxGUI Graphical Modeler."),
  53. 'author' : getpass.getuser() }
  54. # model variables
  55. self.variables = dict()
  56. self.variablesParams = dict()
  57. self.canvas = canvas
  58. def GetCanvas(self):
  59. """Get canvas or None"""
  60. return self.canvas
  61. def GetItems(self, objType = None):
  62. """Get list of model items
  63. :param objType: Object type to filter model objects
  64. """
  65. if not objType:
  66. return self.items
  67. result = list()
  68. for item in self.items:
  69. if isinstance(item, objType):
  70. result.append(item)
  71. return result
  72. def GetItem(self, aId, objType=None):
  73. """Get item of given id
  74. :param aId: item id
  75. :return: Model* instance
  76. :return: None if no item found
  77. """
  78. ilist = self.GetItems(objType)
  79. for item in ilist:
  80. if item.GetId() == aId:
  81. return item
  82. return None
  83. def GetItemIndex(self, item):
  84. """Return list index of given item"""
  85. return self.items.index(item)
  86. def GetNumItems(self, actionOnly = False):
  87. """Get number of items"""
  88. if actionOnly:
  89. return len(self.GetItems(objType = ModelAction))
  90. return len(self.GetItems())
  91. def ReorderItems(self, idxList):
  92. items = list()
  93. for oldIdx, newIdx in idxList.iteritems():
  94. item = self.items.pop(oldIdx)
  95. items.append(item)
  96. self.items.insert(newIdx, item)
  97. nextItem = self.items[newIdx+1]
  98. items.append(nextItem)
  99. x = item.GetX()
  100. y = item.GetY()
  101. item.SetX(nextItem.GetX())
  102. item.SetY(nextItem.GetY())
  103. nextItem.SetX(x)
  104. nextItem.SetY(y)
  105. dc = wx.ClientDC(self.canvas)
  106. for item in items:
  107. item.MoveLinks(dc)
  108. for mo in item.GetBlock():
  109. if isinstance(mo, ModelLoop):
  110. self.canvas.parent.DefineLoop(mo)
  111. elif isinstance(mo, ModelCondition):
  112. self.canvas.parent.DefineCondition(mo)
  113. def Normalize(self):
  114. # check for inconsistecies
  115. for idx in range(1, len(self.items)):
  116. if not self.items[idx].GetBlock() and \
  117. isinstance(self.items[idx-1], ModelLoop):
  118. # swap action not-in-block with previously defined
  119. # loop
  120. itemPrev = self.items[idx-1]
  121. self.items[idx-1] = self.items[idx]
  122. self.items[idx] = itemPrev
  123. # update ids
  124. iId = 1
  125. for item in self.items:
  126. item.SetId(iId)
  127. item.SetLabel()
  128. iId += 1
  129. def GetNextId(self):
  130. """Get next id (data ignored)
  131. :return: next id to be used (default: 1)
  132. """
  133. if len(self.items) < 1:
  134. return 1
  135. currId = self.items[-1].GetId()
  136. if currId > 0:
  137. return currId + 1
  138. return 1
  139. def GetProperties(self):
  140. """Get model properties"""
  141. return self.properties
  142. def GetVariables(self, params = False):
  143. """Get model variables"""
  144. if params:
  145. return self.variablesParams
  146. return self.variables
  147. def SetVariables(self, data):
  148. """Set model variables"""
  149. self.variables = data
  150. def Reset(self):
  151. """Reset model"""
  152. self.items = list()
  153. def RemoveItem(self, item):
  154. """Remove item from model
  155. :return: list of related items to remove/update
  156. """
  157. relList = list()
  158. upList = list()
  159. self.items.remove(item)
  160. if isinstance(item, ModelAction):
  161. for rel in item.GetRelations():
  162. relList.append(rel)
  163. data = rel.GetData()
  164. if len(data.GetRelations()) < 2:
  165. relList.append(data)
  166. else:
  167. upList.append(data)
  168. elif isinstance(item, ModelData):
  169. for rel in item.GetRelations():
  170. relList.append(rel)
  171. if rel.GetFrom() == self:
  172. relList.append(rel.GetTo())
  173. else:
  174. relList.append(rel.GetFrom())
  175. elif isinstance(item, ModelLoop):
  176. for rel in item.GetRelations():
  177. relList.append(rel)
  178. for action in self.GetItems():
  179. action.UnSetBlock(item)
  180. return relList, upList
  181. def FindAction(self, aId):
  182. """Find action by id"""
  183. alist = self.GetItems(objType = ModelAction)
  184. for action in alist:
  185. if action.GetId() == aId:
  186. return action
  187. return None
  188. def GetMaps(self, prompt):
  189. """Get list of maps of selected type
  190. :param prompt: to filter maps"""
  191. maps = list()
  192. for data in self.GetData():
  193. if prompt == data.GetPrompt():
  194. mapName = data.GetValue()
  195. if not mapName or mapName[0] is '%':
  196. continue # skip variables
  197. maps.append(mapName)
  198. return maps
  199. def GetData(self):
  200. """Get list of data items"""
  201. result = list()
  202. dataItems = self.GetItems(objType = ModelData)
  203. for action in self.GetItems(objType = ModelAction):
  204. for rel in action.GetRelations():
  205. dataItem = rel.GetData()
  206. if dataItem not in result:
  207. result.append(dataItem)
  208. if dataItem in dataItems:
  209. dataItems.remove(dataItem)
  210. # standalone data
  211. if dataItems:
  212. result += dataItems
  213. return result
  214. def FindData(self, value, prompt):
  215. """Find data item in the model
  216. :param value: value
  217. :param prompt: prompt
  218. :return: ModelData instance
  219. :return: None if not found
  220. """
  221. for data in self.GetData():
  222. if data.GetValue() == value and \
  223. data.GetPrompt() == prompt:
  224. return data
  225. return None
  226. def LoadModel(self, filename):
  227. """Load model definition stored in GRASS Model XML file (gxm)
  228. .. todo::
  229. Validate against DTD
  230. Raise exception on error.
  231. """
  232. dtdFilename = os.path.join(globalvar.WXGUIDIR, "xml", "grass-gxm.dtd")
  233. # parse workspace file
  234. try:
  235. gxmXml = ProcessModelFile(etree.parse(filename))
  236. except StandardError as e:
  237. raise GException(e)
  238. if self.canvas:
  239. win = self.canvas.parent
  240. if gxmXml.pos:
  241. win.SetPosition(gxmXml.pos)
  242. if gxmXml.size:
  243. win.SetSize(gxmXml.size)
  244. # load properties
  245. self.properties = gxmXml.properties
  246. self.variables = gxmXml.variables
  247. # load actions
  248. for action in gxmXml.actions:
  249. actionItem = ModelAction(parent = self,
  250. x = action['pos'][0],
  251. y = action['pos'][1],
  252. width = action['size'][0],
  253. height = action['size'][1],
  254. task = action['task'],
  255. id = action['id'],
  256. label = action['label'],
  257. comment = action['comment'])
  258. if action['disabled']:
  259. actionItem.Enable(False)
  260. self.AddItem(actionItem, pos = actionItem.GetId()-1)
  261. actionItem.SetValid(actionItem.GetTask().get_options())
  262. actionItem.GetLog() # substitute variables (-> valid/invalid)
  263. # load data & relations
  264. for data in gxmXml.data:
  265. dataItem = ModelData(parent = self,
  266. x = data['pos'][0],
  267. y = data['pos'][1],
  268. width = data['size'][0],
  269. height = data['size'][1],
  270. prompt = data['prompt'],
  271. value = data['value'])
  272. dataItem.SetIntermediate(data['intermediate'])
  273. for rel in data['rels']:
  274. actionItem = self.FindAction(rel['id'])
  275. if rel['dir'] == 'from':
  276. relation = ModelRelation(parent = self, fromShape = dataItem,
  277. toShape = actionItem, param = rel['name'])
  278. else:
  279. relation = ModelRelation(parent = self, fromShape = actionItem,
  280. toShape = dataItem, param = rel['name'])
  281. relation.SetControlPoints(rel['points'])
  282. actionItem.AddRelation(relation)
  283. dataItem.AddRelation(relation)
  284. if self.canvas:
  285. dataItem.Update()
  286. # load loops
  287. for loop in gxmXml.loops:
  288. loopItem = ModelLoop(parent = self,
  289. x = loop['pos'][0],
  290. y = loop['pos'][1],
  291. width = loop['size'][0],
  292. height = loop['size'][1],
  293. label = loop['text'],
  294. id = loop['id'])
  295. self.AddItem(loopItem, pos = loopItem.GetId()-1)
  296. # load conditions
  297. for condition in gxmXml.conditions:
  298. conditionItem = ModelCondition(parent = self,
  299. x = condition['pos'][0],
  300. y = condition['pos'][1],
  301. width = condition['size'][0],
  302. height = condition['size'][1],
  303. label = condition['text'],
  304. id = condition['id'])
  305. self.AddItem(conditionItem, pos = conditionItem.GetId()-1)
  306. # define loops & if/else items
  307. for loop in gxmXml.loops:
  308. loopItem = self.GetItem(loop['id'], objType=ModelLoop)
  309. loopItem.SetItems(loop['items'])
  310. for idx in loop['items']:
  311. action = self.GetItem(idx, objType=ModelAction)
  312. action.SetBlock(loopItem)
  313. for condition in gxmXml.conditions:
  314. conditionItem = self.GetItem(condition['id'])
  315. for b in condition['items'].keys():
  316. alist = list()
  317. for aId in condition['items'][b]:
  318. action = self.GetItem(aId)
  319. alist.append(action)
  320. conditionItem.SetItems(alist, branch = b)
  321. items = conditionItem.GetItems()
  322. for b in items.keys():
  323. for action in items[b]:
  324. action.SetBlock(conditionItem)
  325. # load comments
  326. for comment in gxmXml.comments:
  327. commentItem = ModelComment(parent = self,
  328. x = comment['pos'][0],
  329. y = comment['pos'][1],
  330. width = comment['size'][0],
  331. height = comment['size'][1],
  332. id = comment['id'],
  333. label = comment['text'])
  334. self.AddItem(commentItem, pos = commentItem.GetId()-1)
  335. def AddItem(self, newItem, pos = -1):
  336. """Add item to the list"""
  337. if pos != -1:
  338. self.items.insert(pos, newItem)
  339. else:
  340. self.items.append(newItem)
  341. # i = 1
  342. # for item in self.items:
  343. # item.SetId(i)
  344. # i += 1
  345. def IsValid(self):
  346. """Return True if model is valid"""
  347. if self.Validate():
  348. return False
  349. return True
  350. def Validate(self):
  351. """Validate model, return None if model is valid otherwise
  352. error string"""
  353. errList = list()
  354. variables = self.GetVariables().keys()
  355. pattern = re.compile(r'(.*)(%.+\s?)(.*)')
  356. for action in self.GetItems(objType = ModelAction):
  357. cmd = action.GetLog(string = False)
  358. task = GUI(show = None).ParseCommand(cmd = cmd)
  359. errList += map(lambda x: cmd[0] + ': ' + x, task.get_cmd_error())
  360. # check also variables
  361. for opt in cmd[1:]:
  362. if '=' not in opt:
  363. continue
  364. key, value = opt.split('=', 1)
  365. sval = pattern.search(value)
  366. if sval:
  367. var = sval.group(2).strip()[1:] # ignore '%'
  368. if var not in variables:
  369. report = True
  370. for item in filter(lambda x: isinstance(x, ModelLoop), action.GetBlock()):
  371. if var in item.GetLabel():
  372. report = False
  373. break
  374. if report:
  375. errList.append(cmd[0] + ": " + _("undefined variable '%s'") % var)
  376. ### TODO: check variables in file only optionally
  377. ### errList += self._substituteFile(action, checkOnly = True)
  378. return errList
  379. def _substituteFile(self, item, params = None, checkOnly = False):
  380. """Subsitute variables in command file inputs
  381. :param bool checkOnly: tuble - True to check variable, don't touch files
  382. :return: list of undefined variables
  383. """
  384. errList = list()
  385. self.fileInput = dict()
  386. # collect ascii inputs
  387. for p in item.GetParams()['params']:
  388. if p.get('element', '') == 'file' and \
  389. p.get('prompt', '') == 'input' and \
  390. p.get('age', '') == 'old':
  391. filename = p.get('value', p.get('default', ''))
  392. if filename and \
  393. mimetypes.guess_type(filename)[0] == 'text/plain':
  394. self.fileInput[filename] = None
  395. for finput in self.fileInput:
  396. # read lines
  397. fd = open(finput, "r")
  398. try:
  399. data = self.fileInput[finput] = fd.read()
  400. finally:
  401. fd.close()
  402. # substitute variables
  403. write = False
  404. variables = self.GetVariables()
  405. for variable in variables:
  406. pattern = re.compile('%' + variable)
  407. value = ''
  408. if params and 'variables' in params:
  409. for p in params['variables']['params']:
  410. if variable == p.get('name', ''):
  411. if p.get('type', 'string') == 'string':
  412. value = p.get('value', '')
  413. else:
  414. value = str(p.get('value', ''))
  415. break
  416. if not value:
  417. value = variables[variable].get('value', '')
  418. data = pattern.sub(value, data)
  419. if not checkOnly:
  420. write = True
  421. pattern = re.compile(r'(.*)(%.+\s?)(.*)')
  422. sval = pattern.search(data)
  423. if sval:
  424. var = sval.group(2).strip()[1:] # ignore '%'
  425. cmd = item.GetLog(string = False)[0]
  426. errList.append(cmd + ": " + _("undefined variable '%s'") % var)
  427. if not checkOnly:
  428. if write:
  429. fd = open(finput, "w")
  430. try:
  431. fd.write(data)
  432. finally:
  433. fd.close()
  434. else:
  435. self.fileInput[finput] = None
  436. return errList
  437. def OnPrepare(self, item, params):
  438. self._substituteFile(item, params, checkOnly = False)
  439. def RunAction(self, item, params, log, onDone, onPrepare = None, statusbar = None):
  440. """Run given action
  441. :param item: action item
  442. :param params: parameters dict
  443. :param log: logging window
  444. :param onDone: on-done method
  445. :param onPrepare: on-prepare method
  446. :param statusbar: wx.StatusBar instance or None
  447. """
  448. name = item.GetLabel()
  449. if name in params:
  450. paramsOrig = item.GetParams(dcopy = True)
  451. item.MergeParams(params[name])
  452. if statusbar:
  453. statusbar.SetStatusText(_('Running model...'), 0)
  454. data = { 'item' : item,
  455. 'params' : copy.deepcopy(params) }
  456. log.RunCmd(command = item.GetLog(string = False, substitute = params),
  457. onDone = onDone, onPrepare = self.OnPrepare, userData = data)
  458. if name in params:
  459. item.SetParams(paramsOrig)
  460. def Run(self, log, onDone, parent = None):
  461. """Run model
  462. :param log: logging window (see gconsole.GConsole)
  463. :param onDone: on-done method
  464. :param parent: window for messages or None
  465. """
  466. if self.GetNumItems() < 1:
  467. GMessage(parent = parent,
  468. message = _('Model is empty. Nothing to run.'))
  469. return
  470. statusbar = None
  471. if isinstance(parent, wx.Frame):
  472. statusbar = parent.GetStatusBar()
  473. # validation
  474. if statusbar:
  475. statusbar.SetStatusText(_('Validating model...'), 0)
  476. errList = self.Validate()
  477. if statusbar:
  478. statusbar.SetStatusText('', 0)
  479. if errList:
  480. dlg = wx.MessageDialog(parent = parent,
  481. message = _('Model is not valid. Do you want to '
  482. 'run the model anyway?\n\n%s') % '\n'.join(errList),
  483. caption = _("Run model?"),
  484. style = wx.YES_NO | wx.NO_DEFAULT |
  485. wx.ICON_QUESTION | wx.CENTRE)
  486. ret = dlg.ShowModal()
  487. dlg.Destroy()
  488. if ret != wx.ID_YES:
  489. return
  490. # parametrization
  491. params = self.Parameterize()
  492. delInterData = False
  493. if params:
  494. dlg = ModelParamDialog(parent = parent,
  495. model = self,
  496. params = params)
  497. dlg.CenterOnParent()
  498. ret = dlg.ShowModal()
  499. if ret != wx.ID_OK:
  500. dlg.Destroy()
  501. return
  502. err = dlg.GetErrors()
  503. delInterData = dlg.DeleteIntermediateData()
  504. dlg.Destroy()
  505. if err:
  506. GError(parent = parent, message = unicode('\n'.join(err)))
  507. return
  508. err = list()
  509. for key, item in params.iteritems():
  510. for p in item['params']:
  511. if p.get('value', '') == '':
  512. err.append((key, p.get('name', ''), p.get('description', '')))
  513. if err:
  514. GError(parent = parent,
  515. message = _("Variables below not defined:") + \
  516. "\n\n" + unicode('\n'.join(map(lambda x: "%s: %s (%s)" % (x[0], x[1], x[2]), err))))
  517. return
  518. log.cmdThread.SetId(-1)
  519. for item in self.GetItems():
  520. if not item.IsEnabled():
  521. continue
  522. if isinstance(item, ModelAction):
  523. if item.GetBlockId():
  524. continue
  525. self.RunAction(item, params, log, onDone)
  526. elif isinstance(item, ModelLoop):
  527. cond = item.GetLabel()
  528. # substitute variables in condition
  529. variables = self.GetVariables()
  530. for variable in variables:
  531. pattern = re.compile('%' + variable)
  532. if pattern.search(cond):
  533. value = ''
  534. if params and 'variables' in params:
  535. for p in params['variables']['params']:
  536. if variable == p.get('name', ''):
  537. value = p.get('value', '')
  538. break
  539. if not value:
  540. value = variables[variable].get('value', '')
  541. if not value:
  542. continue
  543. vtype = variables[variable].get('type', 'string')
  544. if vtype == 'string':
  545. value = '"' + value + '"'
  546. cond = pattern.sub(value, cond)
  547. # split condition
  548. ### TODO: this part needs some better solution
  549. condVar, condText = map(lambda x: x.strip(), re.split('\s* in \s*', cond))
  550. pattern = re.compile('%' + condVar)
  551. ### for vars()[condVar] in eval(condText): ?
  552. vlist = list()
  553. if condText[0] == '`' and condText[-1] == '`':
  554. # run command
  555. cmd, dcmd = utils.CmdToTuple(condText[1:-1].split(' '))
  556. ret = RunCommand(cmd,
  557. read = True,
  558. **dcmd)
  559. if ret:
  560. vlist = ret.splitlines()
  561. else:
  562. vlist = eval(condText)
  563. if 'variables' not in params:
  564. params['variables'] = { 'params' : [] }
  565. varDict = { 'name' : condVar, 'value' : '' }
  566. params['variables']['params'].append(varDict)
  567. for var in vlist:
  568. for action in item.GetItems(self.GetItems()):
  569. if not action.IsEnabled():
  570. continue
  571. varDict['value'] = var
  572. self.RunAction(item = action, params = params,
  573. log = log, onDone = onDone)
  574. params['variables']['params'].remove(varDict)
  575. if delInterData:
  576. self.DeleteIntermediateData(log)
  577. # discard values
  578. if params:
  579. for item in params.itervalues():
  580. for p in item['params']:
  581. p['value'] = ''
  582. def DeleteIntermediateData(self, log):
  583. """Detele intermediate data"""
  584. rast, vect, rast3d, msg = self.GetIntermediateData()
  585. if rast:
  586. log.RunCmd(['g.remove', '-f', 'type=raster', 'name=%s' %','.join(rast)])
  587. if rast3d:
  588. log.RunCmd(['g.remove', '-f', 'type=raster_3d', 'name=%s' %','.join(rast3d)])
  589. if vect:
  590. log.RunCmd(['g.remove', '-f', 'type=vector', 'name=%s' %','.join(vect)])
  591. def GetIntermediateData(self):
  592. """Get info about intermediate data"""
  593. rast = list()
  594. rast3d = list()
  595. vect = list()
  596. for data in self.GetData():
  597. if not data.IsIntermediate():
  598. continue
  599. name = data.GetValue()
  600. prompt = data.GetPrompt()
  601. if prompt == 'raster':
  602. rast.append(name)
  603. elif prompt == 'vector':
  604. vect.append(name)
  605. elif prompt == 'raster_3d':
  606. rast3d.append(name)
  607. msg = ''
  608. if rast:
  609. msg += '\n\n%s: ' % _('Raster maps')
  610. msg += ', '.join(rast)
  611. if rast3d:
  612. msg += '\n\n%s: ' % _('3D raster maps')
  613. msg += ', '.join(rast3d)
  614. if vect:
  615. msg += '\n\n%s: ' % _('Vector maps')
  616. msg += ', '.join(vect)
  617. return rast, vect, rast3d, msg
  618. def Update(self):
  619. """Update model"""
  620. for item in self.items:
  621. item.Update()
  622. def IsParameterized(self):
  623. """Return True if model is parameterized"""
  624. if self.Parameterize():
  625. return True
  626. return False
  627. def Parameterize(self):
  628. """Return parameterized options"""
  629. result = dict()
  630. idx = 0
  631. if self.variables:
  632. params = list()
  633. result["variables"] = { 'flags' : list(),
  634. 'params' : params,
  635. 'idx' : idx }
  636. for name, values in self.variables.iteritems():
  637. gtype = values.get('type', 'string')
  638. if gtype in ('raster', 'vector', 'mapset', 'file', 'region', 'dir'):
  639. gisprompt = True
  640. prompt = gtype
  641. if gtype == 'raster':
  642. element = 'cell'
  643. else:
  644. element = gtype
  645. ptype = 'string'
  646. else:
  647. gisprompt = False
  648. prompt = None
  649. element = None
  650. ptype = gtype
  651. params.append({ 'gisprompt' : gisprompt,
  652. 'multiple' : False,
  653. 'description' : values.get('description', ''),
  654. 'guidependency' : '',
  655. 'default' : '',
  656. 'age' : None,
  657. 'required' : True,
  658. 'value' : values.get('value', ''),
  659. 'label' : '',
  660. 'guisection' : '',
  661. 'key_desc' : '',
  662. 'values' : list(),
  663. 'parameterized' : False,
  664. 'values_desc' : list(),
  665. 'prompt' : prompt,
  666. 'element' : element,
  667. 'type' : ptype,
  668. 'name' : name })
  669. idx += 1
  670. for action in self.GetItems(objType = ModelAction):
  671. if not action.IsEnabled():
  672. continue
  673. name = action.GetLabel()
  674. params = action.GetParams()
  675. for f in params['flags']:
  676. if f.get('parameterized', False):
  677. if name not in result:
  678. result[name] = { 'flags' : list(),
  679. 'params': list(),
  680. 'idx' : idx }
  681. result[name]['flags'].append(f)
  682. for p in params['params']:
  683. if p.get('parameterized', False):
  684. if name not in result:
  685. result[name] = { 'flags' : list(),
  686. 'params': list(),
  687. 'idx' : idx }
  688. result[name]['params'].append(p)
  689. if name in result:
  690. idx += 1
  691. self.variablesParams = result # record parameters
  692. return result
  693. class ModelObject(object):
  694. def __init__(self, id = -1, label = ''):
  695. self.id = id # internal id, should be not changed
  696. self.label = ''
  697. self.rels = list() # list of ModelRelations
  698. self.isEnabled = True
  699. self.inBlock = list() # list of related loops/conditions
  700. def __del__(self):
  701. pass
  702. def GetLabel(self):
  703. """Get label"""
  704. return self.label
  705. def SetLabel(self, label=''):
  706. """Set label"""
  707. self.label = label
  708. def GetId(self):
  709. """Get id"""
  710. return self.id
  711. def SetId(self, newId):
  712. """Set id"""
  713. if self.inBlock:
  714. for loop in self.inBlock:
  715. # update block item
  716. loop.UpdateItem(self.id, newId)
  717. self.id = newId
  718. def AddRelation(self, rel):
  719. """Record new relation
  720. """
  721. self.rels.append(rel)
  722. def GetRelations(self, fdir = None):
  723. """Get list of relations
  724. :param bool fdir: True for 'from'
  725. """
  726. if fdir is None:
  727. return self.rels
  728. result = list()
  729. for rel in self.rels:
  730. if fdir == 'from':
  731. if rel.GetFrom() == self:
  732. result.append(rel)
  733. else:
  734. if rel.GetTo() == self:
  735. result.append(rel)
  736. return result
  737. def IsEnabled(self):
  738. """Get True if action is enabled, otherwise False"""
  739. return self.isEnabled
  740. def Enable(self, enabled = True):
  741. """Enable/disable action"""
  742. self.isEnabled = enabled
  743. self.Update()
  744. def Update(self):
  745. pass
  746. def SetBlock(self, item):
  747. """Add object to the block (loop/condition)
  748. :param item: reference to ModelLoop or ModelCondition which
  749. defines loops/condition
  750. """
  751. if item not in self.inBlock:
  752. self.inBlock.append(item)
  753. def UnSetBlock(self, item):
  754. """Remove object from the block (loop/consition)
  755. :param item: reference to ModelLoop or ModelCondition which
  756. defines loops/codition
  757. """
  758. if item in self.inBlock:
  759. self.inBlock.remove(item)
  760. def GetBlock(self):
  761. """Get list of related ModelObject(s) which defines block
  762. (loop/condition)
  763. :return: list of ModelObjects
  764. """
  765. return self.inBlock
  766. def GetBlockId(self):
  767. """Get list of related ids which defines block
  768. :return: list of ids
  769. """
  770. ret = list()
  771. for mo in self.inBlock:
  772. ret.append(mo.GetId())
  773. return ret
  774. class ModelAction(ModelObject, ogl.DividedShape):
  775. """Action class (GRASS module)"""
  776. def __init__(self, parent, x, y, id = -1, cmd = None, task = None,
  777. width = None, height = None, label = None, comment = ''):
  778. ModelObject.__init__(self, id, label)
  779. self.parent = parent
  780. self.task = task
  781. self.comment = comment
  782. if not width:
  783. width = UserSettings.Get(group='modeler', key='action', subkey=('size', 'width'))
  784. if not height:
  785. height = UserSettings.Get(group='modeler', key='action', subkey=('size', 'height'))
  786. if cmd:
  787. self.task = GUI(show = None).ParseCommand(cmd = cmd)
  788. else:
  789. if task:
  790. self.task = task
  791. else:
  792. self.task = None
  793. self.propWin = None
  794. self.data = list() # list of connected data items
  795. self.isValid = False
  796. self.isParameterized = False
  797. if self.parent.GetCanvas():
  798. ogl.DividedShape.__init__(self, width, height)
  799. self.regionLabel = ogl.ShapeRegion()
  800. self.regionLabel.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ | ogl.FORMAT_CENTRE_VERT)
  801. self.AddRegion(self.regionLabel)
  802. self.regionComment = None
  803. self.SetCanvas(self.parent)
  804. self.SetX(x)
  805. self.SetY(y)
  806. self._setPen()
  807. self._setBrush()
  808. self.SetLabel(label)
  809. if comment:
  810. self.SetComment(comment)
  811. self.SetRegionSizes()
  812. self.ReformatRegions()
  813. if self.task:
  814. self.SetValid(self.task.get_options())
  815. def _setBrush(self, running = False):
  816. """Set brush"""
  817. if running:
  818. color = UserSettings.Get(group='modeler', key='action',
  819. subkey=('color', 'running'))
  820. elif not self.isEnabled:
  821. color = UserSettings.Get(group='modeler', key='disabled',
  822. subkey='color')
  823. elif self.isValid:
  824. color = UserSettings.Get(group='modeler', key='action',
  825. subkey=('color', 'valid'))
  826. else:
  827. color = UserSettings.Get(group='modeler', key='action',
  828. subkey=('color', 'invalid'))
  829. wxColor = wx.Colour(color[0], color[1], color[2])
  830. self.SetBrush(wx.Brush(wxColor))
  831. def _setPen(self):
  832. """Set pen"""
  833. if self.isParameterized:
  834. width = int(UserSettings.Get(group='modeler', key='action',
  835. subkey=('width', 'parameterized')))
  836. else:
  837. width = int(UserSettings.Get(group='modeler', key='action',
  838. subkey=('width', 'default')))
  839. if self.isEnabled:
  840. style = wx.SOLID
  841. else:
  842. style = wx.DOT
  843. pen = wx.Pen(wx.BLACK, width, style)
  844. self.SetPen(pen)
  845. def ReformatRegions(self):
  846. rnum = 0
  847. canvas = self.parent.GetCanvas()
  848. dc = wx.ClientDC(canvas) # used for measuring
  849. for region in self.GetRegions():
  850. text = region.GetText()
  851. self.FormatText(dc, text, rnum)
  852. rnum += 1
  853. def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
  854. ogl.DividedShape.OnSizingEndDragLeft(self, pt, x, y, keys, attch)
  855. self.SetRegionSizes()
  856. self.ReformatRegions()
  857. self.GetCanvas().Refresh()
  858. def SetLabel(self, label=None):
  859. """Set label
  860. :param label: if None use command string instead
  861. """
  862. if label:
  863. self.label = label
  864. elif self.label:
  865. label = self.label
  866. else:
  867. try:
  868. label = self.task.get_cmd(ignoreErrors = True)[0]
  869. except:
  870. label = _("unknown")
  871. idx = self.GetId()
  872. self.regionLabel.SetText('(%d) %s' % (idx, label))
  873. self.SetRegionSizes()
  874. self.ReformatRegions()
  875. def SetComment(self, comment):
  876. """Set comment"""
  877. if self.regionComment is None:
  878. self.regionComment = ogl.ShapeRegion()
  879. self.regionComment.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ)
  880. font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
  881. font.SetStyle(wx.ITALIC)
  882. self.regionComment.SetFont(font)
  883. self.AddRegion(self.regionComment)
  884. self.regionLabel.SetProportions(0.0, 0.4)
  885. self.comment = comment
  886. self.regionComment.SetText(comment)
  887. self.SetRegionSizes()
  888. self.ReformatRegions()
  889. def GetComment(self):
  890. """Get comment"""
  891. return self.comment
  892. def SetProperties(self, params, propwin):
  893. """Record properties dialog"""
  894. self.task.params = params['params']
  895. self.task.flags = params['flags']
  896. self.propWin = propwin
  897. def GetPropDialog(self):
  898. """Get properties dialog"""
  899. return self.propWin
  900. def GetLog(self, string = True, substitute = None):
  901. """Get logging info
  902. :param string: True to get cmd as a string otherwise a list
  903. :param substitute: dictionary of parameter to substitute or None
  904. """
  905. cmd = self.task.get_cmd(ignoreErrors = True, ignoreRequired = True,
  906. ignoreDefault = False)
  907. # substitute variables
  908. if substitute:
  909. variables = []
  910. if 'variables' in substitute:
  911. for p in substitute['variables']['params']:
  912. variables.append(p.get('name', ''))
  913. else:
  914. variables = self.parent.GetVariables()
  915. # order variables by length
  916. for variable in sorted(variables, key=len, reverse=True):
  917. pattern= re.compile('%' + variable)
  918. value = ''
  919. if substitute and 'variables' in substitute:
  920. for p in substitute['variables']['params']:
  921. if variable == p.get('name', ''):
  922. if p.get('type', 'string') == 'string':
  923. value = p.get('value', '')
  924. else:
  925. value = str(p.get('value', ''))
  926. break
  927. if not value:
  928. value = variables[variable].get('value', '')
  929. if not value:
  930. continue
  931. for idx in range(len(cmd)):
  932. if pattern.search(cmd[idx]):
  933. cmd[idx] = pattern.sub(value, cmd[idx])
  934. break
  935. idx += 1
  936. if string:
  937. if cmd is None:
  938. return ''
  939. else:
  940. return ' '.join(cmd)
  941. return cmd
  942. def GetLabel(self):
  943. """Get name"""
  944. if self.label:
  945. return self.label
  946. cmd = self.task.get_cmd(ignoreErrors = True)
  947. if cmd and len(cmd) > 0:
  948. return cmd[0]
  949. return _('unknown')
  950. def GetParams(self, dcopy = False):
  951. """Get dictionary of parameters"""
  952. if dcopy:
  953. return copy.deepcopy(self.task.get_options())
  954. return self.task.get_options()
  955. def GetTask(self):
  956. """Get grassTask instance"""
  957. return self.task
  958. def SetParams(self, params):
  959. """Set dictionary of parameters"""
  960. self.task.params = params['params']
  961. self.task.flags = params['flags']
  962. def MergeParams(self, params):
  963. """Merge dictionary of parameters"""
  964. if 'flags' in params:
  965. for f in params['flags']:
  966. self.task.set_flag(f['name'],
  967. f.get('value', False))
  968. if 'params' in params:
  969. for p in params['params']:
  970. self.task.set_param(p['name'],
  971. p.get('value', ''))
  972. def SetValid(self, options):
  973. """Set validity for action
  974. :param options: dictionary with flags and params (gtask)
  975. """
  976. self.isValid = True
  977. for f in options['flags']:
  978. if f.get('parameterized', False):
  979. self.isParameterized = True
  980. break
  981. for p in options['params']:
  982. if self.isValid and p.get('required', False) and \
  983. p.get('value', '') == '' and \
  984. p.get('default', '') == '':
  985. self.isValid = False
  986. if not self.isParameterized and p.get('parameterized', False):
  987. self.isParameterized = True
  988. if self.parent.GetCanvas():
  989. self._setBrush()
  990. self._setPen()
  991. def IsValid(self):
  992. """Check validity (all required parameters set)"""
  993. return self.isValid
  994. def IsParameterized(self):
  995. """Check if action is parameterized"""
  996. return self.isParameterized
  997. def GetParameterizedParams(self):
  998. """Return parameterized flags and options"""
  999. param = { 'flags': [], 'params' : [] }
  1000. options = self.GetParams()
  1001. for f in options['flags']:
  1002. if f.get('parameterized', False):
  1003. param['flags'].append(f)
  1004. for p in options['params']:
  1005. if p.get('parameterized', False):
  1006. param['params'].append(p)
  1007. return param
  1008. def FindData(self, name):
  1009. """Find data item by name"""
  1010. for rel in self.GetRelations():
  1011. data = rel.GetData()
  1012. if name == rel.GetLabel() and name in data.GetLabel():
  1013. return data
  1014. return None
  1015. def Update(self, running = False):
  1016. """Update action"""
  1017. if running:
  1018. self._setBrush(running = True)
  1019. else:
  1020. self._setBrush()
  1021. self._setPen()
  1022. def OnDraw(self, dc):
  1023. """Draw action in canvas"""
  1024. self._setBrush()
  1025. self._setPen()
  1026. ogl.RectangleShape.Recentre(self, dc) # re-center text
  1027. ogl.RectangleShape.OnDraw(self, dc)
  1028. class ModelData(ModelObject, ogl.EllipseShape):
  1029. def __init__(self, parent, x, y, value = '', prompt = '', width = None, height = None):
  1030. """Data item class
  1031. :param parent: window parent
  1032. :param x, y: position of the shape
  1033. :param fname, tname: list of parameter names from / to
  1034. :param value: value
  1035. :param prompt: type of GIS element
  1036. :param width, height: dimension of the shape
  1037. """
  1038. ModelObject.__init__(self)
  1039. self.parent = parent
  1040. self.value = value
  1041. self.prompt = prompt
  1042. self.intermediate = False
  1043. self.propWin = None
  1044. if not width:
  1045. width = UserSettings.Get(group='modeler', key='data', subkey=('size', 'width'))
  1046. if not height:
  1047. height = UserSettings.Get(group='modeler', key='data', subkey=('size', 'height'))
  1048. if self.parent.GetCanvas():
  1049. ogl.EllipseShape.__init__(self, width, height)
  1050. self.SetCanvas(self.parent)
  1051. self.SetX(x)
  1052. self.SetY(y)
  1053. self._setPen()
  1054. self._setBrush()
  1055. self.SetLabel()
  1056. def IsIntermediate(self):
  1057. """Checks if data item is intermediate"""
  1058. return self.intermediate
  1059. def SetIntermediate(self, im):
  1060. """Set intermediate flag"""
  1061. self.intermediate = im
  1062. def OnDraw(self, dc):
  1063. self._setPen()
  1064. ogl.EllipseShape.OnDraw(self, dc)
  1065. def GetLog(self, string = True):
  1066. """Get logging info"""
  1067. name = list()
  1068. for rel in self.GetRelations():
  1069. name.append(rel.GetLabel())
  1070. if name:
  1071. return '/'.join(name) + '=' + self.value + ' (' + self.prompt + ')'
  1072. else:
  1073. return self.value + ' (' + self.prompt + ')'
  1074. def GetLabel(self):
  1075. """Get list of names"""
  1076. name = list()
  1077. for rel in self.GetRelations():
  1078. name.append(rel.GetLabel())
  1079. return name
  1080. def GetPrompt(self):
  1081. """Get prompt"""
  1082. return self.prompt
  1083. def SetPrompt(self, prompt):
  1084. """Set prompt
  1085. :param prompt:
  1086. """
  1087. self.prompt = prompt
  1088. def GetValue(self):
  1089. """Get value"""
  1090. return self.value
  1091. def SetValue(self, value):
  1092. """Set value
  1093. :param value:
  1094. """
  1095. self.value = value
  1096. self.SetLabel()
  1097. for direction in ('from', 'to'):
  1098. for rel in self.GetRelations(direction):
  1099. if direction == 'from':
  1100. action = rel.GetTo()
  1101. else:
  1102. action = rel.GetFrom()
  1103. task = GUI(show = None).ParseCommand(cmd = action.GetLog(string = False))
  1104. task.set_param(rel.GetLabel(), self.value)
  1105. action.MergeParams(task.get_options())
  1106. def GetPropDialog(self):
  1107. """Get properties dialog"""
  1108. return self.propWin
  1109. def SetPropDialog(self, win):
  1110. """Get properties dialog"""
  1111. self.propWin = win
  1112. def _setBrush(self):
  1113. """Set brush"""
  1114. if self.prompt == 'raster':
  1115. color = UserSettings.Get(group = 'modeler', key = 'data',
  1116. subkey = ('color', 'raster'))
  1117. elif self.prompt == 'raster3d':
  1118. color = UserSettings.Get(group = 'modeler', key = 'data',
  1119. subkey = ('color', 'raster3d'))
  1120. elif self.prompt == 'vector':
  1121. color = UserSettings.Get(group = 'modeler', key = 'data',
  1122. subkey = ('color', 'vector'))
  1123. else:
  1124. color = UserSettings.Get(group = 'modeler', key = 'action',
  1125. subkey = ('color', 'invalid'))
  1126. wxColor = wx.Colour(color[0], color[1], color[2])
  1127. self.SetBrush(wx.Brush(wxColor))
  1128. def _setPen(self):
  1129. """Set pen"""
  1130. isParameterized = False
  1131. for rel in self.GetRelations('from'):
  1132. if rel.GetTo().IsParameterized():
  1133. isParameterized = True
  1134. break
  1135. if not isParameterized:
  1136. for rel in self.GetRelations('to'):
  1137. if rel.GetFrom().IsParameterized():
  1138. isParameterized = True
  1139. break
  1140. if isParameterized:
  1141. width = int(UserSettings.Get(group = 'modeler', key = 'action',
  1142. subkey = ('width', 'parameterized')))
  1143. else:
  1144. width = int(UserSettings.Get(group = 'modeler', key = 'action',
  1145. subkey = ('width', 'default')))
  1146. if self.intermediate:
  1147. style = wx.DOT
  1148. else:
  1149. style = wx.SOLID
  1150. pen = wx.Pen(wx.BLACK, width, style)
  1151. self.SetPen(pen)
  1152. def SetLabel(self):
  1153. """Update text"""
  1154. self.ClearText()
  1155. name = []
  1156. for rel in self.GetRelations():
  1157. name.append(rel.GetLabel())
  1158. self.AddText('/'.join(name))
  1159. if self.value:
  1160. self.AddText(self.value)
  1161. else:
  1162. self.AddText(_('<not defined>'))
  1163. def Update(self):
  1164. """Update action"""
  1165. self._setBrush()
  1166. self._setPen()
  1167. self.SetLabel()
  1168. class ModelRelation(ogl.LineShape):
  1169. """Data - action relation"""
  1170. def __init__(self, parent, fromShape, toShape, param = ''):
  1171. self.fromShape = fromShape
  1172. self.toShape = toShape
  1173. self.param = param
  1174. self.parent = parent
  1175. self._points = None
  1176. if self.parent.GetCanvas():
  1177. ogl.LineShape.__init__(self)
  1178. def __del__(self):
  1179. if self in self.fromShape.rels:
  1180. self.fromShape.rels.remove(self)
  1181. if self in self.toShape.rels:
  1182. self.toShape.rels.remove(self)
  1183. def GetFrom(self):
  1184. """Get id of 'from' shape"""
  1185. return self.fromShape
  1186. def GetTo(self):
  1187. """Get id of 'to' shape"""
  1188. return self.toShape
  1189. def GetData(self):
  1190. """Get related ModelData instance
  1191. :return: ModelData instance
  1192. :return: None if not found
  1193. """
  1194. if isinstance(self.fromShape, ModelData):
  1195. return self.fromShape
  1196. elif isinstance(self.toShape, ModelData):
  1197. return self.toShape
  1198. return None
  1199. def GetLabel(self):
  1200. """Get parameter name"""
  1201. return self.param
  1202. def ResetShapes(self):
  1203. """Reset related objects"""
  1204. self.fromShape.ResetControlPoints()
  1205. self.toShape.ResetControlPoints()
  1206. self.ResetControlPoints()
  1207. def SetControlPoints(self, points):
  1208. """Set control points"""
  1209. self._points = points
  1210. def GetControlPoints(self):
  1211. """Get list of control points"""
  1212. return self._points
  1213. def _setPen(self):
  1214. """Set pen"""
  1215. pen = wx.Pen(wx.BLACK, 1, wx.SOLID)
  1216. self.SetPen(pen)
  1217. def OnDraw(self, dc):
  1218. """Draw relation"""
  1219. self._setPen()
  1220. ogl.LineShape.OnDraw(self, dc)
  1221. def SetName(self, param):
  1222. self.param = param
  1223. class ModelItem(ModelObject):
  1224. def __init__(self, parent, x, y, id = -1, width = None, height = None, label = '', items = []):
  1225. """Abstract class for loops and conditions"""
  1226. ModelObject.__init__(self, id, label)
  1227. self.parent = parent
  1228. def _setPen(self):
  1229. """Set pen"""
  1230. if self.isEnabled:
  1231. style = wx.SOLID
  1232. else:
  1233. style = wx.DOT
  1234. pen = wx.Pen(wx.BLACK, 1, style)
  1235. self.SetPen(pen)
  1236. def SetId(self, id):
  1237. """Set loop id"""
  1238. self.id = id
  1239. def SetLabel(self, label=''):
  1240. """Set loop text (condition)"""
  1241. if label:
  1242. self.label = label
  1243. self.ClearText()
  1244. self.AddText('(' + str(self.id) + ') ' + self.label)
  1245. def GetLog(self):
  1246. """Get log info"""
  1247. if self.label:
  1248. return _("Condition: ") + self.label
  1249. else:
  1250. return _("Condition: not defined")
  1251. def AddRelation(self, rel):
  1252. """Record relation"""
  1253. self.rels.append(rel)
  1254. def Clear(self):
  1255. """Clear object, remove rels"""
  1256. self.rels = list()
  1257. class ModelLoop(ModelItem, ogl.RectangleShape):
  1258. def __init__(self, parent, x, y, id=-1, idx=-1, width = None, height = None, label = '', items = []):
  1259. """Defines a loop"""
  1260. ModelItem.__init__(self, parent, x, y, id, width, height, label, items)
  1261. self.itemIds = list() # unordered
  1262. if not width:
  1263. width = UserSettings.Get(group='modeler', key='loop', subkey=('size', 'width'))
  1264. if not height:
  1265. height = UserSettings.Get(group='modeler', key='loop', subkey=('size', 'height'))
  1266. if self.parent.GetCanvas():
  1267. ogl.RectangleShape.__init__(self, width, height)
  1268. self.SetCanvas(self.parent)
  1269. self.SetX(x)
  1270. self.SetY(y)
  1271. self._setPen()
  1272. self._setBrush()
  1273. self.SetCornerRadius(100)
  1274. self.SetLabel(label)
  1275. def _setBrush(self):
  1276. """Set brush"""
  1277. if not self.isEnabled:
  1278. color = UserSettings.Get(group='modeler', key='disabled',
  1279. subkey='color')
  1280. else:
  1281. color = UserSettings.Get(group='modeler', key='loop',
  1282. subkey=('color', 'valid'))
  1283. wxColor = wx.Colour(color[0], color[1], color[2])
  1284. self.SetBrush(wx.Brush(wxColor))
  1285. def Enable(self, enabled = True):
  1286. """Enable/disable action"""
  1287. for idx in self.itemIds:
  1288. item = self.parent.FindAction(idx)
  1289. if item:
  1290. item.Enable(enabled)
  1291. ModelObject.Enable(self, enabled)
  1292. self.Update()
  1293. def Update(self):
  1294. self._setPen()
  1295. self._setBrush()
  1296. def GetItems(self, items):
  1297. """Get sorted items by id"""
  1298. result = list()
  1299. for item in items:
  1300. if item.GetId() in self.itemIds:
  1301. result.append(item)
  1302. return result
  1303. def SetItems(self, items):
  1304. """Set items (id)"""
  1305. self.itemIds = items
  1306. def UpdateItem(self, oldId, newId):
  1307. """Update item in the list"""
  1308. idx = self.itemIds.index(oldId)
  1309. if idx != -1:
  1310. self.itemIds[idx] = newId
  1311. def OnDraw(self, dc):
  1312. """Draw loop in canvas"""
  1313. self._setBrush()
  1314. ogl.RectangleShape.Recentre(self, dc) # re-center text
  1315. ogl.RectangleShape.OnDraw(self, dc)
  1316. class ModelCondition(ModelItem, ogl.PolygonShape):
  1317. def __init__(self, parent, x, y, id = -1, width = None, height = None, label = '',
  1318. items = { 'if' : [], 'else' : [] }):
  1319. """Defines a if-else condition"""
  1320. ModelItem.__init__(self, parent, x, y, id, width, height, label, items)
  1321. self.itemIds = {'if' : [], 'else': []}
  1322. if not width:
  1323. self.width = UserSettings.Get(group='modeler', key='if-else', subkey=('size', 'width'))
  1324. else:
  1325. self.width = width
  1326. if not height:
  1327. self.height = UserSettings.Get(group='modeler', key='if-else', subkey=('size', 'height'))
  1328. else:
  1329. self.height = height
  1330. if self.parent.GetCanvas():
  1331. ogl.PolygonShape.__init__(self)
  1332. points = [(0, - self.height / 2),
  1333. (self.width / 2, 0),
  1334. (0, self.height / 2),
  1335. (- self.width / 2, 0)]
  1336. self.Create(points)
  1337. self.SetCanvas(self.parent)
  1338. self.SetX(x)
  1339. self.SetY(y)
  1340. self.SetPen(wx.BLACK_PEN)
  1341. if label:
  1342. self.AddText('(' + str(self.id) + ') ' + label)
  1343. else:
  1344. self.AddText('(' + str(self.id) + ')')
  1345. def GetLabel(self):
  1346. """Get name"""
  1347. return _("if-else")
  1348. def GetWidth(self):
  1349. """Get object width"""
  1350. return self.width
  1351. def GetHeight(self):
  1352. """Get object height"""
  1353. return self.height
  1354. def GetItems(self, items):
  1355. """Get sorted items by id"""
  1356. result = {'if' : [], 'else': []}
  1357. for item in items:
  1358. if item.GetId() in self.itemIds['if']:
  1359. result['if'].append(item)
  1360. elif item.GetId() in self.itemIds['else']:
  1361. result['else'].append(item)
  1362. return result
  1363. def SetItems(self, items, branch = 'if'):
  1364. """Set items (id)
  1365. :param items: list of items
  1366. :param branch: 'if' / 'else'
  1367. """
  1368. if branch in ['if', 'else']:
  1369. self.itemIds[branch] = items
  1370. class ModelComment(ModelObject, ogl.RectangleShape):
  1371. def __init__(self, parent, x, y, id = -1, width = None, height = None, label = ''):
  1372. """Defines a model comment"""
  1373. ModelObject.__init__(self, id, label)
  1374. if not width:
  1375. width = UserSettings.Get(group='modeler', key='comment', subkey=('size', 'width'))
  1376. if not height:
  1377. height = UserSettings.Get(group='modeler', key='comment', subkey=('size', 'height'))
  1378. if parent.GetCanvas():
  1379. ogl.RectangleShape.__init__(self, width, height)
  1380. self.SetCanvas(parent)
  1381. self.SetX(x)
  1382. self.SetY(y)
  1383. font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
  1384. font.SetStyle(wx.ITALIC)
  1385. self.SetFont(font)
  1386. self._setPen()
  1387. self._setBrush()
  1388. self.SetLabel(label)
  1389. def _setBrush(self, running = False):
  1390. """Set brush"""
  1391. color = UserSettings.Get(group='modeler', key='comment',
  1392. subkey='color')
  1393. wxColor = wx.Colour(color[0], color[1], color[2])
  1394. self.SetBrush(wx.Brush(wxColor))
  1395. def _setPen(self):
  1396. """Set pen"""
  1397. pen = wx.Pen(wx.BLACK, 1, wx.DOT)
  1398. self.SetPen(pen)
  1399. def SetLabel(self, label=None):
  1400. """Set label
  1401. :param label: if None use command string instead
  1402. """
  1403. if label:
  1404. self.label = label
  1405. elif self.label:
  1406. label = self.label
  1407. else:
  1408. label = ''
  1409. idx = self.GetId()
  1410. self.ClearText()
  1411. self.AddText('(%d) %s' % (idx, label))
  1412. def GetComment(self):
  1413. return self.GetLabel()
  1414. def SetComment(self, comment):
  1415. self.SetLabel(comment)
  1416. self.GetCanvas().Refresh()
  1417. class ProcessModelFile:
  1418. """Process GRASS model file (gxm)"""
  1419. def __init__(self, tree):
  1420. """A ElementTree handler for the GXM XML file, as defined in
  1421. grass-gxm.dtd.
  1422. """
  1423. self.tree = tree
  1424. self.root = self.tree.getroot()
  1425. # list of actions, data
  1426. self.properties = dict()
  1427. self.variables = dict()
  1428. self.actions = list()
  1429. self.data = list()
  1430. self.loops = list()
  1431. self.conditions = list()
  1432. self.comments = list()
  1433. self._processWindow()
  1434. self._processProperties()
  1435. self._processVariables()
  1436. self._processItems()
  1437. self._processData()
  1438. def _filterValue(self, value):
  1439. """Filter value
  1440. :param value:
  1441. """
  1442. value = value.replace('&lt;', '<')
  1443. value = value.replace('&gt;', '>')
  1444. return value
  1445. def _getNodeText(self, node, tag, default = ''):
  1446. """Get node text"""
  1447. p = node.find(tag)
  1448. if p is not None:
  1449. if p.text:
  1450. return utils.normalize_whitespace(p.text)
  1451. else:
  1452. return ''
  1453. return default
  1454. def _processWindow(self):
  1455. """Process window properties"""
  1456. node = self.root.find('window')
  1457. if node is None:
  1458. self.pos = self.size = None
  1459. return
  1460. self.pos, self.size = self._getDim(node)
  1461. def _processProperties(self):
  1462. """Process model properties"""
  1463. node = self.root.find('properties')
  1464. if node is None:
  1465. return
  1466. for key in ('name', 'description', 'author'):
  1467. self._processProperty(node, key)
  1468. for f in node.findall('flag'):
  1469. name = f.get('name', '')
  1470. if name == 'overwrite':
  1471. self.properties['overwrite'] = True
  1472. def _processProperty(self, pnode, name):
  1473. """Process given property"""
  1474. node = pnode.find(name)
  1475. if node is not None:
  1476. self.properties[name] = node.text
  1477. else:
  1478. self.properties[name] = ''
  1479. def _processVariables(self):
  1480. """Process model variables"""
  1481. vnode = self.root.find('variables')
  1482. if vnode is None:
  1483. return
  1484. for node in vnode.findall('variable'):
  1485. name = node.get('name', '')
  1486. if not name:
  1487. continue # should not happen
  1488. self.variables[name] = { 'type' : node.get('type', 'string') }
  1489. for key in ('description', 'value'):
  1490. self._processVariable(node, name, key)
  1491. def _processVariable(self, pnode, name, key):
  1492. """Process given variable"""
  1493. node = pnode.find(key)
  1494. if node is not None:
  1495. if node.text:
  1496. self.variables[name][key] = node.text
  1497. def _processItems(self):
  1498. """Process model items (actions, loops, conditions)"""
  1499. self._processActions()
  1500. self._processLoops()
  1501. self._processConditions()
  1502. self._processComments()
  1503. def _processActions(self):
  1504. """Process model file"""
  1505. for action in self.root.findall('action'):
  1506. pos, size = self._getDim(action)
  1507. disabled = False
  1508. task = action.find('task')
  1509. if task is not None:
  1510. if task.find('disabled') is not None:
  1511. disabled = True
  1512. task = self._processTask(task)
  1513. else:
  1514. task = None
  1515. aId = int(action.get('id', -1))
  1516. label = action.get('name')
  1517. comment = action.find('comment')
  1518. if comment is not None:
  1519. commentString = comment.text
  1520. else:
  1521. commentString = ''
  1522. self.actions.append({ 'pos' : pos,
  1523. 'size' : size,
  1524. 'task' : task,
  1525. 'id' : aId,
  1526. 'disabled' : disabled,
  1527. 'label' : label,
  1528. 'comment' : commentString})
  1529. def _getDim(self, node):
  1530. """Get position and size of shape"""
  1531. pos = size = None
  1532. posAttr = node.get('pos', None)
  1533. if posAttr:
  1534. posVal = map(int, posAttr.split(','))
  1535. try:
  1536. pos = (posVal[0], posVal[1])
  1537. except:
  1538. pos = None
  1539. sizeAttr = node.get('size', None)
  1540. if sizeAttr:
  1541. sizeVal = map(int, sizeAttr.split(','))
  1542. try:
  1543. size = (sizeVal[0], sizeVal[1])
  1544. except:
  1545. size = None
  1546. return pos, size
  1547. def _processData(self):
  1548. """Process model file"""
  1549. for data in self.root.findall('data'):
  1550. pos, size = self._getDim(data)
  1551. param = data.find('data-parameter')
  1552. prompt = value = None
  1553. if param is not None:
  1554. prompt = param.get('prompt', None)
  1555. value = self._filterValue(self._getNodeText(param, 'value'))
  1556. if data.find('intermediate') is None:
  1557. intermediate = False
  1558. else:
  1559. intermediate = True
  1560. rels = list()
  1561. for rel in data.findall('relation'):
  1562. defrel = { 'id' : int(rel.get('id', -1)),
  1563. 'dir' : rel.get('dir', 'to'),
  1564. 'name' : rel.get('name', '') }
  1565. points = list()
  1566. for point in rel.findall('point'):
  1567. x = self._filterValue(self._getNodeText(point, 'x'))
  1568. y = self._filterValue(self._getNodeText(point, 'y'))
  1569. points.append((float(x), float(y)))
  1570. defrel['points'] = points
  1571. rels.append(defrel)
  1572. self.data.append({ 'pos' : pos,
  1573. 'size': size,
  1574. 'prompt' : prompt,
  1575. 'value' : value,
  1576. 'intermediate' : intermediate,
  1577. 'rels' : rels })
  1578. def _processTask(self, node):
  1579. """Process task
  1580. :return: grassTask instance
  1581. :return: None on error
  1582. """
  1583. cmd = list()
  1584. parameterized = list()
  1585. name = node.get('name', None)
  1586. if not name:
  1587. return None
  1588. cmd.append(name)
  1589. # flags
  1590. for f in node.findall('flag'):
  1591. flag = f.get('name', '')
  1592. if f.get('parameterized', '0') == '1':
  1593. parameterized.append(('flag', flag))
  1594. if f.get('value', '1') == '0':
  1595. continue
  1596. if len(flag) > 1:
  1597. cmd.append('--' + flag)
  1598. else:
  1599. cmd.append('-' + flag)
  1600. # parameters
  1601. for p in node.findall('parameter'):
  1602. name = p.get('name', '')
  1603. if p.find('parameterized') is not None:
  1604. parameterized.append(('param', name))
  1605. cmd.append('%s=%s' % (name,
  1606. self._filterValue(self._getNodeText(p, 'value'))))
  1607. task, err = GUI(show = None, checkError = True).ParseCommand(cmd = cmd)
  1608. if err:
  1609. GWarning(os.linesep.join(err))
  1610. for opt, name in parameterized:
  1611. if opt == 'flag':
  1612. task.set_flag(name, True, element = 'parameterized')
  1613. else:
  1614. task.set_param(name, True, element = 'parameterized')
  1615. return task
  1616. def _processLoops(self):
  1617. """Process model loops"""
  1618. for node in self.root.findall('loop'):
  1619. pos, size = self._getDim(node)
  1620. text = self._filterValue(self._getNodeText(node, 'condition')).strip()
  1621. aid = list()
  1622. for anode in node.findall('item'):
  1623. try:
  1624. aid.append(int(anode.text))
  1625. except ValueError:
  1626. pass
  1627. self.loops.append({ 'pos' : pos,
  1628. 'size' : size,
  1629. 'text' : text,
  1630. 'id' : int(node.get('id', -1)),
  1631. 'items' : aid })
  1632. def _processConditions(self):
  1633. """Process model conditions"""
  1634. for node in self.root.findall('if-else'):
  1635. pos, size = self._getDim(node)
  1636. text = self._filterValue(self._getNodeText(node, 'condition')).strip()
  1637. aid = { 'if' : list(),
  1638. 'else' : list() }
  1639. for b in aid.keys():
  1640. bnode = node.find(b)
  1641. if bnode is None:
  1642. continue
  1643. for anode in bnode.findall('item'):
  1644. try:
  1645. aid[b].append(int(anode.text))
  1646. except ValueError:
  1647. pass
  1648. self.conditions.append({ 'pos' : pos,
  1649. 'size' : size,
  1650. 'text' : text,
  1651. 'id' : int(node.get('id', -1)),
  1652. 'items' : aid })
  1653. def _processComments(self):
  1654. """Process model comments"""
  1655. for node in self.root.findall('comment'):
  1656. pos, size = self._getDim(node)
  1657. text = self._filterValue(node.text)
  1658. self.comments.append({ 'pos' : pos,
  1659. 'size' : size,
  1660. 'text' : text,
  1661. 'id' : int(node.get('id', -1)),
  1662. 'text' : text })
  1663. class WriteModelFile:
  1664. """Generic class for writing model file"""
  1665. def __init__(self, fd, model):
  1666. self.fd = fd
  1667. self.model = model
  1668. self.properties = model.GetProperties()
  1669. self.variables = model.GetVariables()
  1670. self.items = model.GetItems()
  1671. self.indent = 0
  1672. self._header()
  1673. self._window()
  1674. self._properties()
  1675. self._variables()
  1676. self._items()
  1677. dataList = list()
  1678. for action in model.GetItems(objType = ModelAction):
  1679. for rel in action.GetRelations():
  1680. dataItem = rel.GetData()
  1681. if dataItem not in dataList:
  1682. dataList.append(dataItem)
  1683. self._data(dataList)
  1684. self._footer()
  1685. def _filterValue(self, value):
  1686. """Escapes value to be stored in XML.
  1687. :param value: string to be escaped as XML
  1688. :return: a XML-valid string
  1689. """
  1690. value = saxutils.escape(value)
  1691. return value
  1692. def _header(self):
  1693. """Write header"""
  1694. self.fd.write('<?xml version="1.0" encoding="%s"?>\n' % GetDefaultEncoding(forceUTF8 = True))
  1695. self.fd.write('<!DOCTYPE gxm SYSTEM "grass-gxm.dtd">\n')
  1696. self.fd.write('%s<gxm>\n' % (' ' * self.indent))
  1697. self.indent += 4
  1698. def _footer(self):
  1699. """Write footer"""
  1700. self.indent -= 4
  1701. self.fd.write('%s</gxm>\n' % (' ' * self.indent))
  1702. def _window(self):
  1703. """Write window properties"""
  1704. canvas = self.model.GetCanvas()
  1705. if canvas is None:
  1706. return
  1707. win = canvas.parent
  1708. pos = win.GetPosition()
  1709. size = win.GetSize()
  1710. self.fd.write('%s<window pos="%d,%d" size="%d,%d" />\n' % \
  1711. (' ' * self.indent, pos[0], pos[1], size[0], size[1]))
  1712. def _properties(self):
  1713. """Write model properties"""
  1714. self.fd.write('%s<properties>\n' % (' ' * self.indent))
  1715. self.indent += 4
  1716. if self.properties['name']:
  1717. self.fd.write('%s<name>%s</name>\n' % (' ' * self.indent, EncodeString(self.properties['name'])))
  1718. if self.properties['description']:
  1719. self.fd.write('%s<description>%s</description>\n' % (' ' * self.indent,
  1720. EncodeString(self.properties['description'])))
  1721. if self.properties['author']:
  1722. self.fd.write('%s<author>%s</author>\n' % (' ' * self.indent,
  1723. EncodeString(self.properties['author'])))
  1724. if 'overwrite' in self.properties and \
  1725. self.properties['overwrite']:
  1726. self.fd.write('%s<flag name="overwrite" />\n' % (' ' * self.indent))
  1727. self.indent -= 4
  1728. self.fd.write('%s</properties>\n' % (' ' * self.indent))
  1729. def _variables(self):
  1730. """Write model variables"""
  1731. if not self.variables:
  1732. return
  1733. self.fd.write('%s<variables>\n' % (' ' * self.indent))
  1734. self.indent += 4
  1735. for name, values in self.variables.iteritems():
  1736. self.fd.write('%s<variable name="%s" type="%s">\n' % \
  1737. (' ' * self.indent, EncodeString(name), values['type']))
  1738. self.indent += 4
  1739. if 'value' in values:
  1740. self.fd.write('%s<value>%s</value>\n' % \
  1741. (' ' * self.indent, EncodeString(values['value'])))
  1742. if 'description' in values:
  1743. self.fd.write('%s<description>%s</description>\n' % \
  1744. (' ' * self.indent, EncodeString(values['description'])))
  1745. self.indent -= 4
  1746. self.fd.write('%s</variable>\n' % (' ' * self.indent))
  1747. self.indent -= 4
  1748. self.fd.write('%s</variables>\n' % (' ' * self.indent))
  1749. def _items(self):
  1750. """Write actions/loops/conditions"""
  1751. for item in self.items:
  1752. if isinstance(item, ModelAction):
  1753. self._action(item)
  1754. elif isinstance(item, ModelLoop):
  1755. self._loop(item)
  1756. elif isinstance(item, ModelCondition):
  1757. self._condition(item)
  1758. elif isinstance(item, ModelComment):
  1759. self._comment(item)
  1760. def _action(self, action):
  1761. """Write actions"""
  1762. self.fd.write('%s<action id="%d" name="%s" pos="%d,%d" size="%d,%d">\n' % \
  1763. (' ' * self.indent, action.GetId(), EncodeString(action.GetLabel()), action.GetX(), action.GetY(),
  1764. action.GetWidth(), action.GetHeight()))
  1765. self.indent += 4
  1766. comment = action.GetComment()
  1767. if comment:
  1768. self.fd.write('%s<comment>%s</comment>\n' % (' ' * self.indent, EncodeString(comment)))
  1769. self.fd.write('%s<task name="%s">\n' % (' ' * self.indent, action.GetLog(string = False)[0]))
  1770. self.indent += 4
  1771. if not action.IsEnabled():
  1772. self.fd.write('%s<disabled />\n' % (' ' * self.indent))
  1773. for key, val in action.GetParams().iteritems():
  1774. if key == 'flags':
  1775. for f in val:
  1776. if f.get('value', False) or f.get('parameterized', False):
  1777. if f.get('parameterized', False):
  1778. if f.get('value', False) == False:
  1779. self.fd.write('%s<flag name="%s" value="0" parameterized="1" />\n' %
  1780. (' ' * self.indent, f.get('name', '')))
  1781. else:
  1782. self.fd.write('%s<flag name="%s" parameterized="1" />\n' %
  1783. (' ' * self.indent, f.get('name', '')))
  1784. else:
  1785. self.fd.write('%s<flag name="%s" />\n' %
  1786. (' ' * self.indent, f.get('name', '')))
  1787. else: # parameter
  1788. for p in val:
  1789. if not p.get('value', '') and not p.get('parameterized', False):
  1790. continue
  1791. self.fd.write('%s<parameter name="%s">\n' %
  1792. (' ' * self.indent, p.get('name', '')))
  1793. self.indent += 4
  1794. if p.get('parameterized', False):
  1795. self.fd.write('%s<parameterized />\n' % (' ' * self.indent))
  1796. self.fd.write('%s<value>%s</value>\n' %
  1797. (' ' * self.indent, self._filterValue(p.get('value', ''))))
  1798. self.indent -= 4
  1799. self.fd.write('%s</parameter>\n' % (' ' * self.indent))
  1800. self.indent -= 4
  1801. self.fd.write('%s</task>\n' % (' ' * self.indent))
  1802. self.indent -= 4
  1803. self.fd.write('%s</action>\n' % (' ' * self.indent))
  1804. def _data(self, dataList):
  1805. """Write data"""
  1806. for data in dataList:
  1807. self.fd.write('%s<data pos="%d,%d" size="%d,%d">\n' % \
  1808. (' ' * self.indent, data.GetX(), data.GetY(),
  1809. data.GetWidth(), data.GetHeight()))
  1810. self.indent += 4
  1811. self.fd.write('%s<data-parameter prompt="%s">\n' % \
  1812. (' ' * self.indent, data.GetPrompt()))
  1813. self.indent += 4
  1814. self.fd.write('%s<value>%s</value>\n' %
  1815. (' ' * self.indent, self._filterValue(data.GetValue())))
  1816. self.indent -= 4
  1817. self.fd.write('%s</data-parameter>\n' % (' ' * self.indent))
  1818. if data.IsIntermediate():
  1819. self.fd.write('%s<intermediate />\n' % (' ' * self.indent))
  1820. # relations
  1821. for ft in ('from', 'to'):
  1822. for rel in data.GetRelations(ft):
  1823. if ft == 'from':
  1824. aid = rel.GetTo().GetId()
  1825. else:
  1826. aid = rel.GetFrom().GetId()
  1827. self.fd.write('%s<relation dir="%s" id="%d" name="%s">\n' % \
  1828. (' ' * self.indent, ft, aid, rel.GetLabel()))
  1829. self.indent += 4
  1830. for point in rel.GetLineControlPoints()[1:-1]:
  1831. self.fd.write('%s<point>\n' % (' ' * self.indent))
  1832. self.indent += 4
  1833. x, y = point.Get()
  1834. self.fd.write('%s<x>%d</x>\n' % (' ' * self.indent, int(x)))
  1835. self.fd.write('%s<y>%d</y>\n' % (' ' * self.indent, int(y)))
  1836. self.indent -= 4
  1837. self.fd.write('%s</point>\n' % (' ' * self.indent))
  1838. self.indent -= 4
  1839. self.fd.write('%s</relation>\n' % (' ' * self.indent))
  1840. self.indent -= 4
  1841. self.fd.write('%s</data>\n' % (' ' * self.indent))
  1842. def _loop(self, loop):
  1843. """Write loops"""
  1844. self.fd.write('%s<loop id="%d" pos="%d,%d" size="%d,%d">\n' % \
  1845. (' ' * self.indent, loop.GetId(), loop.GetX(), loop.GetY(),
  1846. loop.GetWidth(), loop.GetHeight()))
  1847. self.indent += 4
  1848. cond = loop.GetLabel()
  1849. if cond:
  1850. self.fd.write('%s<condition>%s</condition>\n' %
  1851. (' ' * self.indent, self._filterValue(cond)))
  1852. for item in loop.GetItems(self.model.GetItems(objType=ModelAction)):
  1853. self.fd.write('%s<item>%d</item>\n' %
  1854. (' ' * self.indent, item.GetId()))
  1855. self.indent -= 4
  1856. self.fd.write('%s</loop>\n' % (' ' * self.indent))
  1857. def _condition(self, condition):
  1858. """Write conditions"""
  1859. bbox = condition.GetBoundingBoxMin()
  1860. self.fd.write('%s<if-else id="%d" pos="%d,%d" size="%d,%d">\n' % \
  1861. (' ' * self.indent, condition.GetId(), condition.GetX(), condition.GetY(),
  1862. bbox[0], bbox[1]))
  1863. text = condition.GetLabel()
  1864. self.indent += 4
  1865. if text:
  1866. self.fd.write('%s<condition>%s</condition>\n' %
  1867. (' ' * self.indent, self._filterValue(text)))
  1868. items = condition.GetItems()
  1869. for b in items.keys():
  1870. if len(items[b]) < 1:
  1871. continue
  1872. self.fd.write('%s<%s>\n' % (' ' * self.indent, b))
  1873. self.indent += 4
  1874. for item in items[b]:
  1875. self.fd.write('%s<item>%d</item>\n' %
  1876. (' ' * self.indent, item.GetId()))
  1877. self.indent -= 4
  1878. self.fd.write('%s</%s>\n' % (' ' * self.indent, b))
  1879. self.indent -= 4
  1880. self.fd.write('%s</if-else>\n' % (' ' * self.indent))
  1881. def _comment(self, comment):
  1882. """Write comment"""
  1883. self.fd.write('%s<comment id="%d" pos="%d,%d" size="%d,%d">%s</comment>\n' % \
  1884. (' ' * self.indent, comment.GetId(), comment.GetX(), comment.GetY(),
  1885. comment.GetWidth(), comment.GetHeight(), EncodeString(comment.GetLabel())))
  1886. class WritePythonFile:
  1887. def __init__(self, fd, model):
  1888. """Class for exporting model to Python script
  1889. :param fd: file descriptor
  1890. """
  1891. self.fd = fd
  1892. self.model = model
  1893. self.indent = 4
  1894. self._writePython()
  1895. def _getStandardizedOption(self, string):
  1896. if string == 'raster':
  1897. return 'G_OPT_R_MAP'
  1898. elif string == 'vector':
  1899. return 'G_OPT_V_MAP'
  1900. elif string == 'mapset':
  1901. return 'G_OPT_M_MAPSET'
  1902. elif string == 'file':
  1903. return 'G_OPT_F_INPUT'
  1904. elif string == 'dir':
  1905. return 'G_OPT_M_DIR'
  1906. elif string == 'region':
  1907. return 'G_OPT_M_REGION'
  1908. return ''
  1909. def _writePython(self):
  1910. """Write model to file"""
  1911. properties = self.model.GetProperties()
  1912. # header
  1913. self.fd.write(
  1914. r"""#!/usr/bin/env python
  1915. #
  1916. #%s
  1917. #
  1918. # MODULE: %s
  1919. #
  1920. # AUTHOR(S): %s
  1921. #
  1922. # PURPOSE: %s
  1923. #
  1924. # DATE: %s
  1925. #
  1926. #%s
  1927. """% ('#' * 77,
  1928. EncodeString(properties['name']),
  1929. EncodeString(properties['author']),
  1930. EncodeString('\n# '.join(properties['description'].splitlines())),
  1931. time.asctime(),
  1932. '#' * 77))
  1933. # UI
  1934. self.fd.write(
  1935. r"""
  1936. #%%module
  1937. #%% description: %s
  1938. #%%end
  1939. """% (EncodeString(' '.join(properties['description'].splitlines()))))
  1940. variables = self.model.GetVariables()
  1941. for key, data in variables.iteritems():
  1942. otype = self._getStandardizedOption(data['type'])
  1943. self.fd.write(
  1944. r"""
  1945. #%%option %s
  1946. #%% key: %s
  1947. #%% description: %s
  1948. #%% required: yes
  1949. """% (otype, key, data['description']))
  1950. if 'value' in data:
  1951. self.fd.write("#%% answer: %s\n" % data['value'])
  1952. self.fd.write("#% end\n")
  1953. # import modules
  1954. self.fd.write(
  1955. r"""
  1956. import sys
  1957. import os
  1958. import atexit
  1959. from grass.script import parser, run_command
  1960. """)
  1961. # cleanup()
  1962. rast, vect, rast3d, msg = self.model.GetIntermediateData()
  1963. self.fd.write(
  1964. r"""
  1965. def cleanup():
  1966. """)
  1967. if rast:
  1968. self.fd.write(
  1969. r""" run_command('g.remove', flags='f', type='raster',
  1970. name=%s)
  1971. """% ','.join(map(lambda x: "'" + x + "'", rast)))
  1972. if vect:
  1973. self.fd.write(
  1974. r""" run_command('g.remove', flags='f', type='vector',
  1975. name=%s)
  1976. """% ','.join(map(lambda x: "'" + x + "'", vect)))
  1977. if rast3d:
  1978. self.fd.write(
  1979. r""" run_command('g.remove', flags='f', type='raster_3d',
  1980. name=%s)
  1981. """% ','.join(map(lambda x: "'" + x + "'", rast3d)))
  1982. if not rast and not vect and not rast3d:
  1983. self.fd.write(' pass\n')
  1984. self.fd.write("\ndef main():\n")
  1985. for item in self.model.GetItems():
  1986. self._writePythonItem(item, variables = self.model.GetVariables())
  1987. self.fd.write("\n return 0\n")
  1988. self.fd.write(
  1989. r"""
  1990. if __name__ == "__main__":
  1991. options, flags = parser()
  1992. atexit.register(cleanup)
  1993. sys.exit(main())
  1994. """)
  1995. def _writePythonItem(self, item, ignoreBlock = True, variables = {}):
  1996. """Write model object to Python file"""
  1997. if isinstance(item, ModelAction):
  1998. if ignoreBlock and item.GetBlockId(): # ignore items in loops of conditions
  1999. return
  2000. self._writePythonAction(item, variables = variables)
  2001. elif isinstance(item, ModelLoop) or isinstance(item, ModelCondition):
  2002. # substitute condition
  2003. cond = item.GetLabel()
  2004. for variable in self.model.GetVariables():
  2005. pattern = re.compile('%' + variable)
  2006. if pattern.search(cond):
  2007. value = variables[variable].get('value', '')
  2008. if variables[variable].get('type', 'string') == 'string':
  2009. value = '"' + value + '"'
  2010. cond = pattern.sub(value, cond)
  2011. if isinstance(item, ModelLoop):
  2012. condVar, condText = map(lambda x: x.strip(), re.split('\s* in \s*', cond))
  2013. cond = "%sfor %s in " % (' ' * self.indent, condVar)
  2014. if condText[0] == '`' and condText[-1] == '`':
  2015. task = GUI(show = None).ParseCommand(cmd = utils.split(condText[1:-1]))
  2016. cond += "grass.read_command("
  2017. cond += self._getPythonActionCmd(task, len(cond), variables = [condVar]) + ".splitlines()"
  2018. else:
  2019. cond += condText
  2020. self.fd.write('%s:\n' % cond)
  2021. self.indent += 4
  2022. variablesLoop = variables.copy()
  2023. variablesLoop[condVar] = None
  2024. for action in item.GetItems(self.model.GetItems(objType=ModelAction)):
  2025. self._writePythonItem(action, ignoreBlock = False, variables = variablesLoop)
  2026. self.indent -= 4
  2027. if isinstance(item, ModelCondition):
  2028. self.fd.write('%sif %s:\n' % (' ' * self.indent, cond))
  2029. self.indent += 4
  2030. condItems = item.GetItems()
  2031. for action in condItems['if']:
  2032. self._writePythonItem(action, ignoreBlock = False)
  2033. if condItems['else']:
  2034. self.indent -= 4
  2035. self.fd.write('%selse:\n' % (' ' * self.indent))
  2036. self.indent += 4
  2037. for action in condItems['else']:
  2038. self._writePythonItem(action, ignoreBlock = False)
  2039. self.indent += 4
  2040. self.fd.write('\n')
  2041. if isinstance(item, ModelComment):
  2042. self._writePythonComment(item)
  2043. def _writePythonAction(self, item, variables = {}):
  2044. """Write model action to Python file"""
  2045. task = GUI(show = None).ParseCommand(cmd = item.GetLog(string = False))
  2046. strcmd = "%srun_command(" % (' ' * self.indent)
  2047. self.fd.write(strcmd + self._getPythonActionCmd(task, len(strcmd), variables) + '\n')
  2048. def _getPythonActionCmd(self, task, cmdIndent, variables = {}):
  2049. opts = task.get_options()
  2050. ret = ''
  2051. flags = ''
  2052. params = list()
  2053. for f in opts['flags']:
  2054. if f.get('value', False):
  2055. name = f.get('name', '')
  2056. if len(name) > 1:
  2057. params.append('%s = True' % name)
  2058. else:
  2059. flags += name
  2060. for p in opts['params']:
  2061. name = p.get('name', None)
  2062. value = p.get('value', None)
  2063. if name and value:
  2064. ptype = p.get('type', 'string')
  2065. foundVar = False
  2066. for var in sorted(variables, key=len, reverse=True):
  2067. data = variables[var]
  2068. if '%' + var in value:
  2069. value = self._substituteVariable(value, var, data)
  2070. foundVar = True
  2071. if foundVar or ptype != 'string':
  2072. params.append("%s = %s" % (name, value))
  2073. else:
  2074. params.append('%s = "%s"' % (name, value))
  2075. ret += '"%s"' % task.get_name()
  2076. if flags:
  2077. ret += ",\n%sflags = '%s'" % (' ' * cmdIndent, flags)
  2078. if len(params) > 0:
  2079. ret += ",\n"
  2080. for opt in params[:-1]:
  2081. ret += "%s%s,\n" % (' ' * cmdIndent, opt)
  2082. ret += "%s%s)" % (' ' * cmdIndent, params[-1])
  2083. else:
  2084. ret += ")"
  2085. return ret
  2086. def _writePythonComment(self, item):
  2087. """Write model comment to Python file"""
  2088. for line in item.GetLabel().splitlines():
  2089. self.fd.write('#' + line + '\n')
  2090. def _substituteVariable(self, string, variable, data):
  2091. """Substitute variable in the string
  2092. :param string: string to be modified
  2093. :param variable: variable to be substituted
  2094. :param data: data related to the variable
  2095. :return: modified string
  2096. """
  2097. result = ''
  2098. ss = re.split("\w*(%"+variable+")w*", string)
  2099. if not ss[0]:
  2100. if data:
  2101. return "options['%s']" % variable
  2102. else:
  2103. return variable
  2104. for s in ss:
  2105. if s == '"':
  2106. continue
  2107. if s == '%' + variable:
  2108. if data:
  2109. result += "+options['%s']+" % variable
  2110. else:
  2111. result += '+%s+' % variable
  2112. else:
  2113. result += '"' + s + '"'
  2114. return result.strip('+')
  2115. class ModelParamDialog(wx.Dialog):
  2116. def __init__(self, parent, model, params, id = wx.ID_ANY, title = _("Model parameters"),
  2117. style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER, **kwargs):
  2118. """Model parameters dialog
  2119. """
  2120. self.parent = parent
  2121. self._model = model
  2122. self.params = params
  2123. self.tasks = list() # list of tasks/pages
  2124. wx.Dialog.__init__(self, parent = parent, id = id, title = title, style = style, **kwargs)
  2125. self.notebook = GNotebook(parent = self,
  2126. style = globalvar.FNPageDStyle)
  2127. panel = self._createPages()
  2128. wx.CallAfter(self.notebook.SetSelection, 0)
  2129. # intermediate data?
  2130. self.interData = wx.CheckBox(parent = self, label = _("Delete intermediate data when finish"))
  2131. self.interData.SetValue(True)
  2132. rast, vect, rast3d, msg = self._model.GetIntermediateData()
  2133. if not rast and not vect and not rast3d:
  2134. self.interData.Hide()
  2135. self.btnCancel = wx.Button(parent = self, id = wx.ID_CANCEL)
  2136. self.btnRun = wx.Button(parent = self, id = wx.ID_OK,
  2137. label = _("&Run"))
  2138. self.btnRun.SetDefault()
  2139. self._layout()
  2140. size = self.GetBestSize()
  2141. self.SetMinSize(size)
  2142. self.SetSize((size.width, size.height +
  2143. panel.constrained_size[1] -
  2144. panel.panelMinHeight))
  2145. def _layout(self):
  2146. btnSizer = wx.StdDialogButtonSizer()
  2147. btnSizer.AddButton(self.btnCancel)
  2148. btnSizer.AddButton(self.btnRun)
  2149. btnSizer.Realize()
  2150. mainSizer = wx.BoxSizer(wx.VERTICAL)
  2151. mainSizer.Add(item = self.notebook, proportion = 1,
  2152. flag = wx.EXPAND)
  2153. if self.interData.IsShown():
  2154. mainSizer.Add(item = self.interData, proportion = 0,
  2155. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  2156. mainSizer.Add(item = wx.StaticLine(parent = self, id = wx.ID_ANY,
  2157. style = wx.LI_HORIZONTAL),
  2158. proportion = 0,
  2159. flag = wx.EXPAND | wx.LEFT | wx.RIGHT, border = 5)
  2160. mainSizer.Add(item = btnSizer, proportion = 0,
  2161. flag = wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border = 5)
  2162. self.SetSizer(mainSizer)
  2163. mainSizer.Fit(self)
  2164. def _createPages(self):
  2165. """Create for each parameterized module its own page"""
  2166. nameOrdered = [''] * len(self.params.keys())
  2167. for name, params in self.params.iteritems():
  2168. nameOrdered[params['idx']] = name
  2169. for name in nameOrdered:
  2170. params = self.params[name]
  2171. panel = self._createPage(name, params)
  2172. if name == 'variables':
  2173. name = _('Variables')
  2174. self.notebook.AddPage(page = panel, text = name)
  2175. return panel
  2176. def _createPage(self, name, params):
  2177. """Define notebook page"""
  2178. if name in globalvar.grassCmd:
  2179. task = gtask.grassTask(name)
  2180. else:
  2181. task = gtask.grassTask()
  2182. task.flags = params['flags']
  2183. task.params = params['params']
  2184. panel = CmdPanel(parent = self, id = wx.ID_ANY, task = task,
  2185. giface = GraphicalModelerGrassInterface(self._model))
  2186. self.tasks.append(task)
  2187. return panel
  2188. def GetErrors(self):
  2189. """Check for errors, get list of messages"""
  2190. errList = list()
  2191. for task in self.tasks:
  2192. errList += task.get_cmd_error()
  2193. return errList
  2194. def DeleteIntermediateData(self):
  2195. """Check if to detele intermediate data"""
  2196. if self.interData.IsShown() and self.interData.IsChecked():
  2197. return True
  2198. return False