guiutils.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 (
  17. mapset_exists,
  18. location_exists,
  19. is_mapset_locked,
  20. get_mapset_lock_info,
  21. is_different_mapset_owner,
  22. is_mapset_current,
  23. is_location_current
  24. )
  25. from grass.grassdb.create import create_mapset, get_default_mapset_name
  26. from grass.grassdb.manage import (
  27. delete_mapset,
  28. delete_location,
  29. delete_grassdb,
  30. rename_mapset,
  31. rename_location,
  32. )
  33. from core.utils import GetListOfLocations
  34. from core import globalvar
  35. from core.gcmd import GError, GMessage, DecodeString, RunCommand
  36. from gui_core.dialogs import TextEntryDialog
  37. from location_wizard.dialogs import RegionDef
  38. from gui_core.widgets import GenericMultiValidator
  39. def SetSessionMapset(database, location, mapset):
  40. """Sets database, location and mapset for the current session"""
  41. RunCommand("g.gisenv", set="GISDBASE=%s" % database)
  42. RunCommand("g.gisenv", set="LOCATION_NAME=%s" % location)
  43. RunCommand("g.gisenv", set="MAPSET=%s" % mapset)
  44. class MapsetDialog(TextEntryDialog):
  45. def __init__(self, parent=None, default=None, message=None, caption=None,
  46. database=None, location=None):
  47. self.database = database
  48. self.location = location
  49. # list of tuples consisting of conditions and callbacks
  50. checks = [(gs.legal_name, self._nameValidationFailed),
  51. (self._checkMapsetNotExists, self._mapsetAlreadyExists),
  52. (self._checkOGR, self._reservedMapsetName)]
  53. validator = GenericMultiValidator(checks)
  54. TextEntryDialog.__init__(
  55. self, parent=parent,
  56. message=message,
  57. caption=caption,
  58. defaultValue=default,
  59. validator=validator,
  60. )
  61. def _nameValidationFailed(self, ctrl):
  62. message = _(
  63. "Name '{}' is not a valid name for location or mapset. "
  64. "Please use only ASCII characters excluding characters {} "
  65. "and space.").format(ctrl.GetValue(), '/"\'@,=*~')
  66. GError(parent=self, message=message, caption=_("Invalid name"))
  67. def _checkOGR(self, text):
  68. """Check user's input for reserved mapset name."""
  69. if text.lower() == 'ogr':
  70. return False
  71. return True
  72. def _reservedMapsetName(self, ctrl):
  73. message = _(
  74. "Name '{}' is reserved for direct "
  75. "read access to OGR layers. Please use "
  76. "another name for your mapset.").format(ctrl.GetValue())
  77. GError(parent=self, message=message,
  78. caption=_("Reserved mapset name"))
  79. def _checkMapsetNotExists(self, text):
  80. """Check whether user's input mapset exists or not."""
  81. if mapset_exists(self.database, self.location, text):
  82. return False
  83. return True
  84. def _mapsetAlreadyExists(self, ctrl):
  85. message = _(
  86. "Mapset '{}' already exists. Please consider using "
  87. "another name for your mapset.").format(ctrl.GetValue())
  88. GError(parent=self, message=message,
  89. caption=_("Existing mapset path"))
  90. class LocationDialog(TextEntryDialog):
  91. def __init__(self, parent=None, default=None, message=None, caption=None,
  92. database=None):
  93. self.database = database
  94. # list of tuples consisting of conditions and callbacks
  95. checks = [(gs.legal_name, self._nameValidationFailed),
  96. (self._checkLocationNotExists, self._locationAlreadyExists)]
  97. validator = GenericMultiValidator(checks)
  98. TextEntryDialog.__init__(
  99. self, parent=parent,
  100. message=message,
  101. caption=caption,
  102. defaultValue=default,
  103. validator=validator,
  104. )
  105. def _nameValidationFailed(self, ctrl):
  106. message = _(
  107. "Name '{}' is not a valid name for location or mapset. "
  108. "Please use only ASCII characters excluding characters {} "
  109. "and space.").format(ctrl.GetValue(), '/"\'@,=*~')
  110. GError(parent=self, message=message, caption=_("Invalid name"))
  111. def _checkLocationNotExists(self, text):
  112. """Check whether user's input location exists or not."""
  113. if location_exists(self.database, text):
  114. return False
  115. return True
  116. def _locationAlreadyExists(self, ctrl):
  117. message = _(
  118. "Location '{}' already exists. Please consider using "
  119. "another name for your location.").format(ctrl.GetValue())
  120. GError(parent=self, message=message,
  121. caption=_("Existing location path"))
  122. def get_reasons_mapsets_not_removable(mapsets, check_permanent):
  123. """Get reasons why mapsets cannot be removed.
  124. Parameter *mapsets* is a list of tuples (database, location, mapset).
  125. Parameter *check_permanent* is True of False. It depends on whether
  126. we want to check for permanent mapset or not.
  127. Returns messages as list if there were any failed checks, otherwise empty list.
  128. """
  129. messages = []
  130. for grassdb, location, mapset in mapsets:
  131. message = get_reason_mapset_not_removable(grassdb, location,
  132. mapset, check_permanent)
  133. if message:
  134. messages.append(message)
  135. return messages
  136. def get_reason_mapset_not_removable(grassdb, location, mapset, check_permanent):
  137. """Get reason why one mapset cannot be removed.
  138. Parameter *check_permanent* is True of False. It depends on whether
  139. we want to check for permanent mapset or not.
  140. Returns message as string if there was failed check, otherwise None.
  141. """
  142. message = None
  143. mapset_path = os.path.join(grassdb, location, mapset)
  144. # Check if mapset is permanent
  145. if check_permanent and mapset == "PERMANENT":
  146. message = _("Mapset <{mapset}> is required for a valid location.").format(
  147. mapset=mapset_path)
  148. # Check if mapset is current
  149. elif is_mapset_current(grassdb, location, mapset):
  150. message = _("Mapset <{mapset}> is the current mapset.").format(
  151. mapset=mapset_path)
  152. # Check whether mapset is in use
  153. elif is_mapset_locked(mapset_path):
  154. message = _("Mapset <{mapset}> is in use.").format(
  155. mapset=mapset_path)
  156. # Check whether mapset is owned by different user
  157. elif is_different_mapset_owner(mapset_path):
  158. message = _("Mapset <{mapset}> is owned by a different user.").format(
  159. mapset=mapset_path)
  160. return message
  161. def get_reasons_locations_not_removable(locations):
  162. """Get reasons why locations cannot be removed.
  163. Parameter *locations* is a list of tuples (database, location).
  164. Returns messages as list if there were any failed checks, otherwise empty list.
  165. """
  166. messages = []
  167. for grassdb, location in locations:
  168. messages += get_reasons_location_not_removable(grassdb, location)
  169. return messages
  170. def get_reasons_location_not_removable(grassdb, location):
  171. """Get reasons why one location cannot be removed.
  172. Returns messages as list if there were any failed checks, otherwise empty list.
  173. """
  174. messages = []
  175. location_path = os.path.join(grassdb, location)
  176. # Check if location is current
  177. if is_location_current(grassdb, location):
  178. messages.append(_("Location <{location}> is the current location.").format(
  179. location=location_path))
  180. return messages
  181. # Find mapsets in particular location
  182. tmp_gisrc_file, env = gs.create_environment(grassdb, location, 'PERMANENT')
  183. env['GRASS_SKIP_MAPSET_OWNER_CHECK'] = '1'
  184. g_mapsets = gs.read_command(
  185. 'g.mapsets',
  186. flags='l',
  187. separator='comma',
  188. quiet=True,
  189. env=env).strip().split(',')
  190. # Append to the list of tuples
  191. mapsets = []
  192. for g_mapset in g_mapsets:
  193. mapsets.append((grassdb, location, g_mapset))
  194. # Concentenate both checks
  195. messages += get_reasons_mapsets_not_removable(mapsets, check_permanent=False)
  196. gs.try_remove(tmp_gisrc_file)
  197. return messages
  198. def get_reasons_grassdb_not_removable(grassdb):
  199. """Get reasons why one grassdb cannot be removed.
  200. Returns messages as list if there were any failed checks, otherwise empty list.
  201. """
  202. messages = []
  203. genv = gisenv()
  204. # Check if grassdb is current
  205. if grassdb == genv['GISDBASE']:
  206. messages.append(_("GRASS database <{grassdb}> is the current database.").format(
  207. grassdb=grassdb))
  208. return messages
  209. g_locations = GetListOfLocations(grassdb)
  210. # Append to the list of tuples
  211. locations = []
  212. for g_location in g_locations:
  213. locations.append((grassdb, g_location))
  214. messages = get_reasons_locations_not_removable(locations)
  215. return messages
  216. # TODO: similar to (but not the same as) read_gisrc function in grass.py
  217. def read_gisrc():
  218. """Read variables from a current GISRC file
  219. Returns a dictionary representation of the file content.
  220. """
  221. grassrc = {}
  222. gisrc = os.getenv("GISRC")
  223. if gisrc and os.path.isfile(gisrc):
  224. try:
  225. rc = open(gisrc, "r")
  226. for line in rc.readlines():
  227. try:
  228. key, val = line.split(":", 1)
  229. except ValueError as e:
  230. sys.stderr.write(
  231. _('Invalid line in GISRC file (%s):%s\n' % (e, line)))
  232. grassrc[key.strip()] = DecodeString(val.strip())
  233. finally:
  234. rc.close()
  235. return grassrc
  236. def GetVersion():
  237. """Gets version and revision
  238. Returns tuple `(version, revision)`. For standard releases revision
  239. is an empty string.
  240. Revision string is currently wrapped in parentheses with added
  241. leading space. This is an implementation detail and legacy and may
  242. change anytime.
  243. """
  244. versionFile = open(os.path.join(globalvar.ETCDIR, "VERSIONNUMBER"))
  245. versionLine = versionFile.readline().rstrip('\n')
  246. versionFile.close()
  247. try:
  248. grassVersion, grassRevision = versionLine.split(' ', 1)
  249. if grassVersion.endswith('dev'):
  250. grassRevisionStr = ' (%s)' % grassRevision
  251. else:
  252. grassRevisionStr = ''
  253. except ValueError:
  254. grassVersion = versionLine
  255. grassRevisionStr = ''
  256. return (grassVersion, grassRevisionStr)
  257. def create_mapset_interactively(guiparent, grassdb, location):
  258. """
  259. Create new mapset
  260. """
  261. dlg = MapsetDialog(
  262. parent=guiparent,
  263. default=get_default_mapset_name(),
  264. message=_("Name for the new mapset:"),
  265. caption=_("Create new mapset"),
  266. database=grassdb,
  267. location=location,
  268. )
  269. mapset = None
  270. if dlg.ShowModal() == wx.ID_OK:
  271. mapset = dlg.GetValue()
  272. try:
  273. create_mapset(grassdb, location, mapset)
  274. except OSError as err:
  275. mapset = None
  276. GError(
  277. parent=guiparent,
  278. message=_("Unable to create new mapset: {}").format(err),
  279. showTraceback=False,
  280. )
  281. dlg.Destroy()
  282. return mapset
  283. def create_location_interactively(guiparent, grassdb):
  284. """
  285. Create new location using Location Wizard.
  286. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  287. by default or another mapset a user created and may want to switch to.
  288. """
  289. from location_wizard.wizard import LocationWizard
  290. gWizard = LocationWizard(parent=guiparent,
  291. grassdatabase=grassdb)
  292. if gWizard.location is None:
  293. gWizard_output = (None, None, None)
  294. # Returns Nones after Cancel
  295. return gWizard_output
  296. if gWizard.georeffile:
  297. message = _(
  298. "Do you want to import {}"
  299. "to the newly created location?"
  300. ).format(gWizard.georeffile)
  301. dlg = wx.MessageDialog(parent=guiparent,
  302. message=message,
  303. caption=_("Import data?"),
  304. style=wx.YES_NO | wx.YES_DEFAULT |
  305. wx.ICON_QUESTION)
  306. dlg.CenterOnParent()
  307. if dlg.ShowModal() == wx.ID_YES:
  308. import_file(guiparent, gWizard.georeffile)
  309. dlg.Destroy()
  310. if gWizard.default_region:
  311. defineRegion = RegionDef(guiparent, location=gWizard.location)
  312. defineRegion.CenterOnParent()
  313. defineRegion.ShowModal()
  314. defineRegion.Destroy()
  315. if gWizard.user_mapset:
  316. mapset = create_mapset_interactively(guiparent,
  317. gWizard.grassdatabase,
  318. gWizard.location)
  319. # Returns database and location created by user
  320. # and a mapset user may want to switch to
  321. gWizard_output = (gWizard.grassdatabase, gWizard.location,
  322. mapset)
  323. else:
  324. # Returns PERMANENT mapset when user mapset not defined
  325. gWizard_output = (gWizard.grassdatabase, gWizard.location,
  326. "PERMANENT")
  327. return gWizard_output
  328. def rename_mapset_interactively(guiparent, grassdb, location, mapset):
  329. """Rename mapset with user interaction.
  330. Exceptions during renaming are handled in get_reason_mapset_not_removable
  331. function.
  332. Returns newmapset if there was a change or None if the mapset cannot be
  333. renamed (see reasons given by get_reason_mapset_not_removable
  334. function) or if another error was encountered.
  335. """
  336. newmapset = None
  337. # Check selected mapset
  338. message = get_reason_mapset_not_removable(grassdb, location, mapset,
  339. check_permanent=True)
  340. if message:
  341. dlg = wx.MessageDialog(
  342. parent=guiparent,
  343. message=_(
  344. "Cannot rename mapset <{mapset}> for the following reason:\n\n"
  345. "{reason}\n\n"
  346. "No mapset will be renamed."
  347. ).format(mapset=mapset, reason=message),
  348. caption=_("Unable to rename selected mapset"),
  349. style=wx.OK | wx.ICON_WARNING
  350. )
  351. dlg.ShowModal()
  352. dlg.Destroy()
  353. return newmapset
  354. # Display question dialog
  355. dlg = MapsetDialog(
  356. parent=guiparent,
  357. default=mapset,
  358. message=_("Current name: {}\n\nEnter new name:").format(mapset),
  359. caption=_("Rename selected mapset"),
  360. database=grassdb,
  361. location=location,
  362. )
  363. if dlg.ShowModal() == wx.ID_OK:
  364. newmapset = dlg.GetValue()
  365. try:
  366. rename_mapset(grassdb, location, mapset, newmapset)
  367. except OSError as err:
  368. newmapset = None
  369. wx.MessageBox(
  370. parent=guiparent,
  371. caption=_("Error"),
  372. message=_("Unable to rename mapset.\n\n{}").format(err),
  373. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  374. )
  375. dlg.Destroy()
  376. return newmapset
  377. def rename_location_interactively(guiparent, grassdb, location):
  378. """Rename location with user interaction.
  379. Exceptions during renaming are handled in get_reasons_location_not_removable
  380. function.
  381. Returns newlocation if there was a change or None if the location cannot be
  382. renamed (see reasons given by get_reasons_location_not_removable
  383. function) or if another error was encountered.
  384. """
  385. newlocation = None
  386. # Check selected location
  387. messages = get_reasons_location_not_removable(grassdb, location)
  388. if messages:
  389. dlg = wx.MessageDialog(
  390. parent=guiparent,
  391. message=_(
  392. "Cannot rename location <{location}> for the following reasons:\n\n"
  393. "{reasons}\n\n"
  394. "No location will be renamed."
  395. ).format(location=location, reasons="\n".join(messages)),
  396. caption=_("Unable to rename selected location"),
  397. style=wx.OK | wx.ICON_WARNING
  398. )
  399. dlg.ShowModal()
  400. dlg.Destroy()
  401. return newlocation
  402. # Display question dialog
  403. dlg = LocationDialog(
  404. parent=guiparent,
  405. default=location,
  406. message=_("Current name: {}\n\nEnter new name:").format(location),
  407. caption=_("Rename selected location"),
  408. database=grassdb,
  409. )
  410. if dlg.ShowModal() == wx.ID_OK:
  411. newlocation = dlg.GetValue()
  412. try:
  413. rename_location(grassdb, location, newlocation)
  414. except OSError as err:
  415. newlocation = None
  416. wx.MessageBox(
  417. parent=guiparent,
  418. caption=_("Error"),
  419. message=_("Unable to rename location.\n\n{}").format(err),
  420. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  421. )
  422. dlg.Destroy()
  423. return newlocation
  424. def download_location_interactively(guiparent, grassdb):
  425. """
  426. Download new location using Location Wizard.
  427. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  428. by default or in future it could be the mapset the user may want to
  429. switch to.
  430. """
  431. from startup.locdownload import LocationDownloadDialog
  432. result = (None, None, None)
  433. loc_download = LocationDownloadDialog(parent=guiparent,
  434. database=grassdb)
  435. loc_download.Centre()
  436. loc_download.ShowModal()
  437. if loc_download.GetLocation() is not None:
  438. # Returns database and location created by user
  439. # and a mapset user may want to switch to
  440. result = (grassdb, loc_download.GetLocation(), "PERMANENT")
  441. loc_download.Destroy()
  442. return result
  443. def delete_mapset_interactively(guiparent, grassdb, location, mapset):
  444. """Delete one mapset with user interaction.
  445. This is currently just a convenience wrapper for delete_mapsets_interactively().
  446. """
  447. mapsets = [(grassdb, location, mapset)]
  448. return delete_mapsets_interactively(guiparent, mapsets)
  449. def delete_mapsets_interactively(guiparent, mapsets):
  450. """Delete multiple mapsets with user interaction.
  451. Parameter *mapsets* is a list of tuples (database, location, mapset).
  452. Exceptions during deletation are handled in get_reasons_mapsets_not_removable
  453. function.
  454. Returns True if there was a change, i.e., all mapsets were successfuly
  455. deleted or at least one mapset was deleted.
  456. Returns False if one or more mapsets cannot be deleted (see reasons given
  457. by get_reasons_mapsets_not_removable function) or if an error was
  458. encountered when deleting the first mapset in the list.
  459. """
  460. deletes = []
  461. modified = False
  462. # Check selected mapsets
  463. messages = get_reasons_mapsets_not_removable(mapsets, check_permanent=True)
  464. if messages:
  465. dlg = wx.MessageDialog(
  466. parent=guiparent,
  467. message=_(
  468. "Cannot delete one or more mapsets for the following reasons:\n\n"
  469. "{reasons}\n\n"
  470. "No mapsets will be deleted."
  471. ).format(reasons="\n".join(messages)),
  472. caption=_("Unable to delete selected mapsets"),
  473. style=wx.OK | wx.ICON_WARNING
  474. )
  475. dlg.ShowModal()
  476. dlg.Destroy()
  477. return modified
  478. # No error occurs, create list of mapsets for deleting
  479. for grassdb, location, mapset in mapsets:
  480. mapset_path = os.path.join(grassdb, location, mapset)
  481. deletes.append(mapset_path)
  482. # Display question dialog
  483. dlg = wx.MessageDialog(
  484. parent=guiparent,
  485. message=_(
  486. "Do you want to continue with deleting"
  487. " one or more of the following mapsets?\n\n"
  488. "{deletes}\n\n"
  489. "All maps included in these mapsets will be permanently deleted!"
  490. ).format(deletes="\n".join(deletes)),
  491. caption=_("Delete selected mapsets"),
  492. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  493. )
  494. if dlg.ShowModal() == wx.ID_YES:
  495. try:
  496. for grassdb, location, mapset in mapsets:
  497. delete_mapset(grassdb, location, mapset)
  498. modified = True
  499. dlg.Destroy()
  500. return modified
  501. except OSError as error:
  502. wx.MessageBox(
  503. parent=guiparent,
  504. caption=_("Error when deleting mapsets"),
  505. message=_(
  506. "The following error occured when deleting mapset <{path}>:"
  507. "\n\n{error}\n\n"
  508. "Deleting of mapsets was interrupted."
  509. ).format(
  510. path=os.path.join(grassdb, location, mapset),
  511. error=error,
  512. ),
  513. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  514. )
  515. dlg.Destroy()
  516. return modified
  517. def delete_location_interactively(guiparent, grassdb, location):
  518. """Delete one location with user interaction.
  519. This is currently just a convenience wrapper for delete_locations_interactively().
  520. """
  521. locations = [(grassdb, location)]
  522. return delete_locations_interactively(guiparent, locations)
  523. def delete_locations_interactively(guiparent, locations):
  524. """Delete multiple locations with user interaction.
  525. Parameter *locations* is a list of tuples (database, location).
  526. Exceptions during deletation are handled in get_reasons_locations_not_removable
  527. function.
  528. Returns True if there was a change, i.e., all locations were successfuly
  529. deleted or at least one location was deleted.
  530. Returns False if one or more locations cannot be deleted (see reasons given
  531. by get_reasons_locations_not_removable function) or if an error was
  532. encountered when deleting the first location in the list.
  533. """
  534. deletes = []
  535. modified = False
  536. # Check selected locations
  537. messages = get_reasons_locations_not_removable(locations)
  538. if messages:
  539. dlg = wx.MessageDialog(
  540. parent=guiparent,
  541. message=_(
  542. "Cannot delete one or more locations for the following reasons:\n\n"
  543. "{reasons}\n\n"
  544. "No locations will be deleted."
  545. ).format(reasons="\n".join(messages)),
  546. caption=_("Unable to delete selected locations"),
  547. style=wx.OK | wx.ICON_WARNING
  548. )
  549. dlg.ShowModal()
  550. dlg.Destroy()
  551. return modified
  552. # No error occurs, create list of locations for deleting
  553. for grassdb, location in locations:
  554. location_path = os.path.join(grassdb, location)
  555. deletes.append(location_path)
  556. # Display question dialog
  557. dlg = wx.MessageDialog(
  558. parent=guiparent,
  559. message=_(
  560. "Do you want to continue with deleting"
  561. " one or more of the following locations?\n\n"
  562. "{deletes}\n\n"
  563. "All mapsets included in these locations will be permanently deleted!"
  564. ).format(deletes="\n".join(deletes)),
  565. caption=_("Delete selected locations"),
  566. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  567. )
  568. if dlg.ShowModal() == wx.ID_YES:
  569. try:
  570. for grassdb, location in locations:
  571. delete_location(grassdb, location)
  572. modified = True
  573. dlg.Destroy()
  574. return modified
  575. except OSError as error:
  576. wx.MessageBox(
  577. parent=guiparent,
  578. caption=_("Error when deleting locations"),
  579. message=_(
  580. "The following error occured when deleting location <{path}>:"
  581. "\n\n{error}\n\n"
  582. "Deleting of locations was interrupted."
  583. ).format(
  584. path=os.path.join(grassdb, location),
  585. error=error,
  586. ),
  587. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  588. )
  589. dlg.Destroy()
  590. return modified
  591. def delete_grassdb_interactively(guiparent, grassdb):
  592. """
  593. Delete grass database if could be deleted.
  594. If current grass database found, desired operation cannot be performed.
  595. Exceptions during deleting are handled in this function.
  596. Returns True if grass database is deleted from the disk. Returns None if
  597. cannot be deleted (see above the possible reasons).
  598. """
  599. deleted = False
  600. # Check selected grassdb
  601. messages = get_reasons_grassdb_not_removable(grassdb)
  602. if messages:
  603. dlg = wx.MessageDialog(
  604. parent=guiparent,
  605. message=_(
  606. "Cannot delete GRASS database from disk for the following reason:\n\n"
  607. "{reasons}\n\n"
  608. "GRASS database will not be deleted."
  609. ).format(reasons="\n".join(messages)),
  610. caption=_("Unable to delete selected GRASS database"),
  611. style=wx.OK | wx.ICON_WARNING
  612. )
  613. dlg.ShowModal()
  614. else:
  615. dlg = wx.MessageDialog(
  616. parent=guiparent,
  617. message=_(
  618. "Do you want to delete"
  619. " the following GRASS database from disk?\n\n"
  620. "{grassdb}\n\n"
  621. "The directory will be permanently deleted!"
  622. ).format(grassdb=grassdb),
  623. caption=_("Delete selected GRASS database"),
  624. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  625. )
  626. if dlg.ShowModal() == wx.ID_YES:
  627. try:
  628. delete_grassdb(grassdb)
  629. deleted = True
  630. dlg.Destroy()
  631. return deleted
  632. except OSError as error:
  633. wx.MessageBox(
  634. parent=guiparent,
  635. caption=_("Error when deleting GRASS database"),
  636. message=_(
  637. "The following error occured when deleting database <{path}>:"
  638. "\n\n{error}\n\n"
  639. "Deleting of GRASS database was interrupted."
  640. ).format(
  641. path=grassdb,
  642. error=error,
  643. ),
  644. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  645. )
  646. dlg.Destroy()
  647. return deleted
  648. def can_switch_mapset_interactive(guiparent, grassdb, location, mapset):
  649. """
  650. Checks if mapset is locked and offers to remove the lock file.
  651. Returns True if user wants to switch to the selected mapset in spite of
  652. removing lock. Returns False if a user wants to stay in the current
  653. mapset or if an error was encountered.
  654. """
  655. can_switch = True
  656. mapset_path = os.path.join(grassdb, location, mapset)
  657. if is_mapset_locked(mapset_path):
  658. info = get_mapset_lock_info(mapset_path)
  659. user = info['owner'] if info['owner'] else _('unknown')
  660. lockpath = info['lockpath']
  661. timestamp = info['timestamp']
  662. dlg = wx.MessageDialog(
  663. parent=guiparent,
  664. message=_("User {user} is already running GRASS in selected mapset "
  665. "<{mapset}>\n (file {lockpath} created {timestamp} "
  666. "found).\n\n"
  667. "Concurrent use not allowed.\n\n"
  668. "Do you want to stay in the current mapset or remove "
  669. ".gislock and switch to selected mapset?"
  670. ).format(user=user,
  671. mapset=mapset,
  672. lockpath=lockpath,
  673. timestamp=timestamp),
  674. caption=_("Mapset is in use"),
  675. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  676. )
  677. dlg.SetYesNoLabels("S&witch to selected mapset",
  678. "S&tay in current mapset")
  679. if dlg.ShowModal() == wx.ID_YES:
  680. # Remove lockfile
  681. try:
  682. os.remove(lockpath)
  683. except IOError as e:
  684. wx.MessageBox(
  685. parent=guiparent,
  686. caption=_("Error when removing lock file"),
  687. message=_("Unable to remove {lockpath}.\n\n Details: {error}."
  688. ).format(lockpath=lockpath,
  689. error=e),
  690. style=wx.OK | wx.ICON_ERROR | wx.CENTRE
  691. )
  692. can_switch = False
  693. else:
  694. can_switch = False
  695. dlg.Destroy()
  696. return can_switch
  697. def import_file(guiparent, filePath):
  698. """Tries to import file as vector or raster.
  699. If successfull sets default region from imported map.
  700. """
  701. RunCommand('db.connect', flags='c')
  702. mapName = os.path.splitext(os.path.basename(filePath))[0]
  703. vectors = RunCommand('v.in.ogr', input=filePath, flags='l',
  704. read=True)
  705. wx.BeginBusyCursor()
  706. wx.GetApp().Yield()
  707. if vectors:
  708. # vector detected
  709. returncode, error = RunCommand(
  710. 'v.in.ogr', input=filePath, output=mapName, flags='e',
  711. getErrorMsg=True)
  712. else:
  713. returncode, error = RunCommand(
  714. 'r.in.gdal', input=filePath, output=mapName, flags='e',
  715. getErrorMsg=True)
  716. wx.EndBusyCursor()
  717. if returncode != 0:
  718. GError(
  719. parent=guiparent,
  720. message=_(
  721. "Import of <%(name)s> failed.\n"
  722. "Reason: %(msg)s") % ({
  723. 'name': filePath,
  724. 'msg': error}))
  725. else:
  726. GMessage(
  727. message=_(
  728. "Data file <%(name)s> imported successfully. "
  729. "The location's default region was set from "
  730. "this imported map.") % {
  731. 'name': filePath},
  732. parent=guiparent)
  733. def switch_mapset_interactively(guiparent, giface, dbase, location, mapset):
  734. """Switch current mapset. Emits giface.currentMapsetChanged signal."""
  735. if dbase:
  736. if RunCommand('g.mapset', parent=guiparent,
  737. location=location,
  738. mapset=mapset,
  739. dbase=dbase) == 0:
  740. GMessage(parent=guiparent,
  741. message=_("Current GRASS database is <%(dbase)s>.\n"
  742. "Current location is <%(loc)s>.\n"
  743. "Current mapset is <%(mapset)s>."
  744. ) %
  745. {'dbase': dbase, 'loc': location, 'mapset': mapset})
  746. giface.currentMapsetChanged.emit(dbase=dbase,
  747. location=location,
  748. mapset=mapset)
  749. elif location:
  750. if RunCommand('g.mapset', parent=guiparent,
  751. location=location,
  752. mapset=mapset) == 0:
  753. GMessage(parent=guiparent,
  754. message=_("Current location is <%(loc)s>.\n"
  755. "Current mapset is <%(mapset)s>.") %
  756. {'loc': location, 'mapset': mapset})
  757. giface.currentMapsetChanged.emit(dbase=None,
  758. location=location,
  759. mapset=mapset)
  760. else:
  761. if RunCommand('g.mapset',
  762. parent=guiparent,
  763. mapset=mapset) == 0:
  764. GMessage(parent=guiparent,
  765. message=_("Current mapset is <%s>.") % mapset)
  766. giface.currentMapsetChanged.emit(dbase=None, location=None, mapset=mapset)