model.py 93 KB

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