guiutils.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. """
  2. @package startup.guiutils
  3. @brief General GUI-dependent utilities for GUI startup of GRASS GIS
  4. (C) 2018 by Vaclav Petras the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. @author Vaclav Petras <wenzeslaus gmail com>
  8. @author Linda Kladivova <l.kladivova@seznam.cz>
  9. This is for code which depend on something from GUI (wx or wxGUI).
  10. """
  11. import os
  12. import sys
  13. import wx
  14. import grass.script as gs
  15. from grass.script import gisenv
  16. from grass.grassdb.checks import mapset_exists, location_exists
  17. from grass.grassdb.create import create_mapset, get_default_mapset_name
  18. from grass.grassdb.manage import (
  19. delete_mapset,
  20. delete_location,
  21. rename_mapset,
  22. rename_location,
  23. )
  24. from core import globalvar
  25. from core.gcmd import GError, GMessage, DecodeString, RunCommand
  26. from gui_core.dialogs import TextEntryDialog
  27. from location_wizard.dialogs import RegionDef
  28. from gui_core.widgets import GenericMultiValidator
  29. def SetSessionMapset(database, location, mapset):
  30. """Sets database, location and mapset for the current session"""
  31. RunCommand("g.gisenv", set="GISDBASE=%s" % database)
  32. RunCommand("g.gisenv", set="LOCATION_NAME=%s" % location)
  33. RunCommand("g.gisenv", set="MAPSET=%s" % mapset)
  34. class MapsetDialog(TextEntryDialog):
  35. def __init__(self, parent=None, default=None, message=None, caption=None,
  36. database=None, location=None):
  37. self.database = database
  38. self.location = location
  39. # list of tuples consisting of conditions and callbacks
  40. checks = [(gs.legal_name, self._nameValidationFailed),
  41. (self._checkMapsetNotExists, self._mapsetAlreadyExists),
  42. (self._checkOGR, self._reservedMapsetName)]
  43. validator = GenericMultiValidator(checks)
  44. TextEntryDialog.__init__(
  45. self, parent=parent,
  46. message=message,
  47. caption=caption,
  48. defaultValue=default,
  49. validator=validator,
  50. )
  51. def _nameValidationFailed(self, ctrl):
  52. message = _(
  53. "Name '{}' is not a valid name for location or mapset. "
  54. "Please use only ASCII characters excluding characters {} "
  55. "and space.").format(ctrl.GetValue(), '/"\'@,=*~')
  56. GError(parent=self, message=message, caption=_("Invalid name"))
  57. def _checkOGR(self, text):
  58. """Check user's input for reserved mapset name."""
  59. if text.lower() == 'ogr':
  60. return False
  61. return True
  62. def _reservedMapsetName(self, ctrl):
  63. message = _(
  64. "Name '{}' is reserved for direct "
  65. "read access to OGR layers. Please use "
  66. "another name for your mapset.").format(ctrl.GetValue())
  67. GError(parent=self, message=message,
  68. caption=_("Reserved mapset name"))
  69. def _checkMapsetNotExists(self, text):
  70. """Check whether user's input mapset exists or not."""
  71. if mapset_exists(self.database, self.location, text):
  72. return False
  73. return True
  74. def _mapsetAlreadyExists(self, ctrl):
  75. message = _(
  76. "Mapset '{}' already exists. Please consider using "
  77. "another name for your mapset.").format(ctrl.GetValue())
  78. GError(parent=self, message=message,
  79. caption=_("Existing mapset path"))
  80. class LocationDialog(TextEntryDialog):
  81. def __init__(self, parent=None, default=None, message=None, caption=None,
  82. database=None):
  83. self.database = database
  84. # list of tuples consisting of conditions and callbacks
  85. checks = [(gs.legal_name, self._nameValidationFailed),
  86. (self._checkLocationNotExists, self._locationAlreadyExists)]
  87. validator = GenericMultiValidator(checks)
  88. TextEntryDialog.__init__(
  89. self, parent=parent,
  90. message=message,
  91. caption=caption,
  92. defaultValue=default,
  93. validator=validator,
  94. )
  95. def _nameValidationFailed(self, ctrl):
  96. message = _(
  97. "Name '{}' is not a valid name for location or mapset. "
  98. "Please use only ASCII characters excluding characters {} "
  99. "and space.").format(ctrl.GetValue(), '/"\'@,=*~')
  100. GError(parent=self, message=message, caption=_("Invalid name"))
  101. def _checkLocationNotExists(self, text):
  102. """Check whether user's input location exists or not."""
  103. if location_exists(self.database, text):
  104. return False
  105. return True
  106. def _locationAlreadyExists(self, ctrl):
  107. message = _(
  108. "Location '{}' already exists. Please consider using "
  109. "another name for your location.").format(ctrl.GetValue())
  110. GError(parent=self, message=message,
  111. caption=_("Existing location path"))
  112. # TODO: similar to (but not the same as) read_gisrc function in grass.py
  113. def read_gisrc():
  114. """Read variables from a current GISRC file
  115. Returns a dictionary representation of the file content.
  116. """
  117. grassrc = {}
  118. gisrc = os.getenv("GISRC")
  119. if gisrc and os.path.isfile(gisrc):
  120. try:
  121. rc = open(gisrc, "r")
  122. for line in rc.readlines():
  123. try:
  124. key, val = line.split(":", 1)
  125. except ValueError as e:
  126. sys.stderr.write(
  127. _('Invalid line in GISRC file (%s):%s\n' % (e, line)))
  128. grassrc[key.strip()] = DecodeString(val.strip())
  129. finally:
  130. rc.close()
  131. return grassrc
  132. def GetVersion():
  133. """Gets version and revision
  134. Returns tuple `(version, revision)`. For standard releases revision
  135. is an empty string.
  136. Revision string is currently wrapped in parentheses with added
  137. leading space. This is an implementation detail and legacy and may
  138. change anytime.
  139. """
  140. versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  141. versionLine = versionFile.readline().rstrip('\n')
  142. versionFile.close()
  143. try:
  144. grassVersion, grassRevision = versionLine.split(' ', 1)
  145. if grassVersion.endswith('dev'):
  146. grassRevisionStr = ' (%s)' % grassRevision
  147. else:
  148. grassRevisionStr = ''
  149. except ValueError:
  150. grassVersion = versionLine
  151. grassRevisionStr = ''
  152. return (grassVersion, grassRevisionStr)
  153. def create_mapset_interactively(guiparent, grassdb, location):
  154. """
  155. Create new mapset
  156. """
  157. dlg = MapsetDialog(
  158. parent=guiparent,
  159. default=get_default_mapset_name(),
  160. message=_("Name for the new mapset:"),
  161. caption=_("Create new mapset"),
  162. database=grassdb,
  163. location=location,
  164. )
  165. mapset = None
  166. if dlg.ShowModal() == wx.ID_OK:
  167. mapset = dlg.GetValue()
  168. try:
  169. create_mapset(grassdb, location, mapset)
  170. except OSError as err:
  171. mapset = None
  172. GError(
  173. parent=guiparent,
  174. message=_("Unable to create new mapset: {}").format(err),
  175. showTraceback=False,
  176. )
  177. dlg.Destroy()
  178. return mapset
  179. def create_location_interactively(guiparent, grassdb):
  180. """
  181. Create new location using Location Wizard.
  182. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  183. by default or another mapset a user created and may want to switch to.
  184. """
  185. from location_wizard.wizard import LocationWizard
  186. gWizard = LocationWizard(parent=guiparent,
  187. grassdatabase=grassdb)
  188. if gWizard.location is None:
  189. gWizard_output = (None, None, None)
  190. # Returns Nones after Cancel
  191. return gWizard_output
  192. if gWizard.georeffile:
  193. message = _(
  194. "Do you want to import {}"
  195. "to the newly created location?"
  196. ).format(gWizard.georeffile)
  197. dlg = wx.MessageDialog(parent=guiparent,
  198. message=message,
  199. caption=_("Import data?"),
  200. style=wx.YES_NO | wx.YES_DEFAULT |
  201. wx.ICON_QUESTION)
  202. dlg.CenterOnParent()
  203. if dlg.ShowModal() == wx.ID_YES:
  204. import_file(guiparent, gWizard.georeffile)
  205. dlg.Destroy()
  206. if gWizard.default_region:
  207. defineRegion = RegionDef(guiparent, location=gWizard.location)
  208. defineRegion.CenterOnParent()
  209. defineRegion.ShowModal()
  210. defineRegion.Destroy()
  211. if gWizard.user_mapset:
  212. mapset = create_mapset_interactively(guiparent,
  213. gWizard.grassdatabase,
  214. gWizard.location)
  215. # Returns database and location created by user
  216. # and a mapset user may want to switch to
  217. gWizard_output = (gWizard.grassdatabase, gWizard.location,
  218. mapset)
  219. else:
  220. # Returns PERMANENT mapset when user mapset not defined
  221. gWizard_output = (gWizard.grassdatabase, gWizard.location,
  222. "PERMANENT")
  223. return gWizard_output
  224. def rename_mapset_interactively(guiparent, grassdb, location, mapset):
  225. """Rename mapset with user interaction.
  226. If PERMANENT or current mapset found, rename operation is not performed.
  227. Exceptions during renaming are handled in this function.
  228. Returns newmapset if there was a change or None if the mapset cannot be
  229. renamed (see above the possible reasons) or if another error was encountered.
  230. """
  231. genv = gisenv()
  232. # Check selected mapset and remember issue.
  233. # Each error is reported only once (using elif).
  234. mapset_path = os.path.join(grassdb, location, mapset)
  235. newmapset = None
  236. issue = None
  237. # Check for permanent mapsets
  238. if mapset == "PERMANENT":
  239. issue = _("<{}> is required for a valid location.").format(mapset_path)
  240. # Check for current mapset
  241. elif (
  242. grassdb == genv['GISDBASE'] and
  243. location == genv['LOCATION_NAME'] and
  244. mapset == genv['MAPSET']
  245. ):
  246. issue = _("<{}> is the current mapset.").format(mapset_path)
  247. # If an issue, display the warning message and do not rename mapset
  248. if issue:
  249. dlg = wx.MessageDialog(
  250. parent=guiparent,
  251. message=_(
  252. "Cannot rename selected mapset for the following reason:\n\n"
  253. "{}\n\n"
  254. "No mapset will be renamed."
  255. ).format(issue),
  256. caption=_("Unable to rename selected mapset"),
  257. style=wx.OK | wx.ICON_WARNING
  258. )
  259. dlg.ShowModal()
  260. else:
  261. dlg = MapsetDialog(
  262. parent=guiparent,
  263. default=mapset,
  264. message=_("Current name: {}\n\nEnter new name:").format(mapset),
  265. caption=_("Rename selected mapset"),
  266. database=grassdb,
  267. location=location,
  268. )
  269. if dlg.ShowModal() == wx.ID_OK:
  270. newmapset = dlg.GetValue()
  271. try:
  272. rename_mapset(grassdb, location, mapset, newmapset)
  273. except OSError as err:
  274. newmapset = None
  275. wx.MessageBox(
  276. parent=guiparent,
  277. caption=_("Error"),
  278. message=_("Unable to rename mapset.\n\n{}").format(err),
  279. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  280. )
  281. dlg.Destroy()
  282. return newmapset
  283. def rename_location_interactively(guiparent, grassdb, location):
  284. """Rename location with user interaction.
  285. If current location found, rename operation is not performed.
  286. Exceptions during renaming are handled in this function.
  287. Returns newlocation if there was a change or None if the location cannot be
  288. renamed (see above the possible reasons) or if another error was encountered.
  289. """
  290. genv = gisenv()
  291. # Check selected location and remember issue.
  292. # Each error is reported only once (using elif).
  293. location_path = os.path.join(grassdb, location)
  294. newlocation = None
  295. issue = None
  296. # Check for current location
  297. if (
  298. grassdb == genv['GISDBASE'] and
  299. location == genv['LOCATION_NAME']
  300. ):
  301. issue = _("<{}> is the current location.").format(location_path)
  302. # If an issue, display the warning message and do not rename location
  303. if issue:
  304. dlg = wx.MessageDialog(
  305. parent=guiparent,
  306. message=_(
  307. "Cannot rename selected location for the following reason:\n\n"
  308. "{}\n\n"
  309. "No location will be renamed."
  310. ).format(issue),
  311. caption=_("Unable to rename selected location"),
  312. style=wx.OK | wx.ICON_WARNING
  313. )
  314. dlg.ShowModal()
  315. else:
  316. dlg = LocationDialog(
  317. parent=guiparent,
  318. default=location,
  319. message=_("Current name: {}\n\nEnter new name:").format(location),
  320. caption=_("Rename selected location"),
  321. database=grassdb,
  322. )
  323. if dlg.ShowModal() == wx.ID_OK:
  324. newlocation = dlg.GetValue()
  325. try:
  326. rename_location(grassdb, location, newlocation)
  327. except OSError as err:
  328. newlocation = None
  329. wx.MessageBox(
  330. parent=guiparent,
  331. caption=_("Error"),
  332. message=_("Unable to rename location.\n\n{}").format(err),
  333. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  334. )
  335. dlg.Destroy()
  336. return newlocation
  337. def download_location_interactively(guiparent, grassdb):
  338. """
  339. Download new location using Location Wizard.
  340. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  341. by default or in future it could be the mapset the user may want to
  342. switch to.
  343. """
  344. from startup.locdownload import LocationDownloadDialog
  345. result = (None, None, None)
  346. loc_download = LocationDownloadDialog(parent=guiparent,
  347. database=grassdb)
  348. loc_download.Centre()
  349. loc_download.ShowModal()
  350. if loc_download.GetLocation() is not None:
  351. # Returns database and location created by user
  352. # and a mapset user may want to switch to
  353. result = (grassdb, loc_download.GetLocation(), "PERMANENT")
  354. loc_download.Destroy()
  355. return result
  356. def delete_mapset_interactively(guiparent, grassdb, location, mapset):
  357. """Delete one mapset with user interaction.
  358. This is currently just a convenience wrapper for delete_mapsets_interactively().
  359. """
  360. mapsets = [(grassdb, location, mapset)]
  361. return delete_mapsets_interactively(guiparent, mapsets)
  362. def delete_mapsets_interactively(guiparent, mapsets):
  363. """Delete multiple mapsets with user interaction.
  364. Parameter *mapsets* is a list of tuples (database, location, mapset).
  365. If PERMANENT or current mapset found, delete operation is not performed.
  366. Exceptions during deletation are handled in this function.
  367. Returns True if there was a change, i.e., all mapsets were successfuly deleted
  368. or at least one mapset was deleted. Returns False if one or more mapsets cannot be
  369. deleted (see above the possible reasons) or if an error was encountered when
  370. deleting the first mapset in the list.
  371. """
  372. genv = gisenv()
  373. issues = []
  374. deletes = []
  375. # Check selected mapsets and remember issue.
  376. # Each error is reported only once (using elif).
  377. for grassdb, location, mapset in mapsets:
  378. mapset_path = os.path.join(grassdb, location, mapset)
  379. # Check for permanent mapsets
  380. if mapset == "PERMANENT":
  381. issue = _("<{}> is required for a valid location.").format(mapset_path)
  382. issues.append(issue)
  383. # Check for current mapset
  384. elif (
  385. grassdb == genv['GISDBASE'] and
  386. location == genv['LOCATION_NAME'] and
  387. mapset == genv['MAPSET']
  388. ):
  389. issue = _("<{}> is the current mapset.").format(mapset_path)
  390. issues.append(issue)
  391. # No issue detected
  392. else:
  393. deletes.append(mapset_path)
  394. modified = False # True after first successful delete
  395. # If any issues, display the warning message and do not delete anything
  396. if issues:
  397. issues = "\n".join(issues)
  398. dlg = wx.MessageDialog(
  399. parent=guiparent,
  400. message=_(
  401. "Cannot delete one or more mapsets for the following reasons:\n\n"
  402. "{}\n\n"
  403. "No mapsets will be deleted."
  404. ).format(issues),
  405. caption=_("Unable to delete selected mapsets"),
  406. style=wx.OK | wx.ICON_WARNING
  407. )
  408. dlg.ShowModal()
  409. else:
  410. deletes = "\n".join(deletes)
  411. dlg = wx.MessageDialog(
  412. parent=guiparent,
  413. message=_(
  414. "Do you want to continue with deleting"
  415. " one or more of the following mapsets?\n\n"
  416. "{}\n\n"
  417. "All maps included in these mapsets will be permanently deleted!"
  418. ).format(deletes),
  419. caption=_("Delete selected mapsets"),
  420. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  421. )
  422. if dlg.ShowModal() == wx.ID_YES:
  423. try:
  424. for grassdb, location, mapset in mapsets:
  425. delete_mapset(grassdb, location, mapset)
  426. modified = True
  427. dlg.Destroy()
  428. return modified
  429. except OSError as error:
  430. wx.MessageBox(
  431. parent=guiparent,
  432. caption=_("Error when deleting mapsets"),
  433. message=_(
  434. "The following error occured when deleting mapset <{path}>:"
  435. "\n\n{error}\n\n"
  436. "Deleting of mapsets was interrupted."
  437. ).format(
  438. path=os.path.join(grassdb, location, mapset),
  439. error=error,
  440. ),
  441. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  442. )
  443. dlg.Destroy()
  444. return modified
  445. def delete_location_interactively(guiparent, grassdb, location):
  446. """Delete a location with user interaction.
  447. If current location found, delete operation is not performed.
  448. Exceptions during deletation are handled in this function.
  449. Returns True if there was a change, returns False if a location cannot be
  450. deleted (see above the possible reasons) or if another error was encountered.
  451. """
  452. genv = gisenv()
  453. issue = None
  454. # Check selected location and remember issue.
  455. # Each error is reported only once (using elif).
  456. location_path = os.path.join(grassdb, location)
  457. # Check for current location
  458. if (
  459. grassdb == genv['GISDBASE'] and
  460. location == genv['LOCATION_NAME']
  461. ):
  462. issue = _("<{}> is the current location.").format(location_path)
  463. modified = False # True after first successful delete
  464. # If any issues, display the warning message and do not delete anything
  465. if issue:
  466. dlg = wx.MessageDialog(
  467. parent=guiparent,
  468. message=_(
  469. "Cannot delete selected location for the following reasons:\n\n"
  470. "{}\n\n"
  471. "No location will be deleted."
  472. ).format(issue),
  473. caption=_("Unable to delete selected location"),
  474. style=wx.OK | wx.ICON_WARNING
  475. )
  476. dlg.ShowModal()
  477. else:
  478. dlg = wx.MessageDialog(
  479. parent=guiparent,
  480. message=_(
  481. "Do you want to continue with deleting"
  482. "the following location?\n\n"
  483. "{}\n\n"
  484. "All mapsets included in this location will be permanently deleted!"
  485. ).format(location_path),
  486. caption=_("Delete selected location"),
  487. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  488. )
  489. if dlg.ShowModal() == wx.ID_YES:
  490. try:
  491. delete_location(grassdb, location)
  492. modified = True
  493. dlg.Destroy()
  494. return modified
  495. except OSError as error:
  496. wx.MessageBox(
  497. parent=guiparent,
  498. caption=_("Error when deleting location"),
  499. message=_(
  500. "The following error occured when deleting location <{path}>:"
  501. "\n\n{error}\n\n"
  502. "Deleting was interrupted."
  503. ).format(
  504. path=os.path.join(grassdb, location),
  505. error=error,
  506. ),
  507. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  508. )
  509. dlg.Destroy()
  510. return modified
  511. def import_file(guiparent, filePath):
  512. """Tries to import file as vector or raster.
  513. If successfull sets default region from imported map.
  514. """
  515. RunCommand('db.connect', flags='c')
  516. mapName = os.path.splitext(os.path.basename(filePath))[0]
  517. vectors = RunCommand('v.in.ogr', input=filePath, flags='l',
  518. read=True)
  519. wx.BeginBusyCursor()
  520. wx.GetApp().Yield()
  521. if vectors:
  522. # vector detected
  523. returncode, error = RunCommand(
  524. 'v.in.ogr', input=filePath, output=mapName, flags='e',
  525. getErrorMsg=True)
  526. else:
  527. returncode, error = RunCommand(
  528. 'r.in.gdal', input=filePath, output=mapName, flags='e',
  529. getErrorMsg=True)
  530. wx.EndBusyCursor()
  531. if returncode != 0:
  532. GError(
  533. parent=guiparent,
  534. message=_(
  535. "Import of <%(name)s> failed.\n"
  536. "Reason: %(msg)s") % ({
  537. 'name': filePath,
  538. 'msg': error}))
  539. else:
  540. GMessage(
  541. message=_(
  542. "Data file <%(name)s> imported successfully. "
  543. "The location's default region was set from "
  544. "this imported map.") % {
  545. 'name': filePath},
  546. parent=guiparent)