model.py 95 KB

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