model.py 91 KB

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