model.py 92 KB

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