guiutils.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 wx
  13. from grass.grassdb.checks import (
  14. is_mapset_locked,
  15. get_mapset_lock_info,
  16. is_mapset_name_valid,
  17. is_location_name_valid,
  18. get_mapset_name_invalid_reason,
  19. get_location_name_invalid_reason,
  20. get_reason_mapset_not_removable,
  21. get_reasons_mapsets_not_removable,
  22. get_reasons_location_not_removable,
  23. get_reasons_locations_not_removable,
  24. get_reasons_grassdb_not_removable,
  25. is_fallback_session,
  26. )
  27. import grass.grassdb.config as cfg
  28. from grass.grassdb.create import create_mapset, get_default_mapset_name
  29. from grass.grassdb.manage import (
  30. delete_mapset,
  31. delete_location,
  32. delete_grassdb,
  33. rename_mapset,
  34. rename_location,
  35. )
  36. from grass.script.core import create_environment
  37. from grass.script.utils import try_remove
  38. from grass.script import gisenv
  39. from core.gcmd import GError, GMessage, RunCommand
  40. from gui_core.dialogs import TextEntryDialog
  41. from location_wizard.dialogs import RegionDef
  42. from gui_core.widgets import GenericValidator
  43. class MapsetDialog(TextEntryDialog):
  44. def __init__(
  45. self,
  46. parent=None,
  47. default=None,
  48. message=None,
  49. caption=None,
  50. database=None,
  51. location=None,
  52. ):
  53. self.database = database
  54. self.location = location
  55. validator = GenericValidator(
  56. self._isMapsetNameValid, self._showMapsetNameInvalidReason
  57. )
  58. TextEntryDialog.__init__(
  59. self,
  60. parent=parent,
  61. message=message,
  62. caption=caption,
  63. defaultValue=default,
  64. validator=validator,
  65. )
  66. def _showMapsetNameInvalidReason(self, ctrl):
  67. message = get_mapset_name_invalid_reason(
  68. self.database, self.location, ctrl.GetValue()
  69. )
  70. GError(parent=self, message=message, caption=_("Invalid mapset name"))
  71. def _isMapsetNameValid(self, text):
  72. """Check whether user's input location is valid or not."""
  73. return is_mapset_name_valid(self.database, self.location, text)
  74. class LocationDialog(TextEntryDialog):
  75. def __init__(
  76. self, parent=None, default=None, message=None, caption=None, database=None
  77. ):
  78. self.database = database
  79. validator = GenericValidator(
  80. self._isLocationNameValid, self._showLocationNameInvalidReason
  81. )
  82. TextEntryDialog.__init__(
  83. self,
  84. parent=parent,
  85. message=message,
  86. caption=caption,
  87. defaultValue=default,
  88. validator=validator,
  89. )
  90. def _showLocationNameInvalidReason(self, ctrl):
  91. message = get_location_name_invalid_reason(self.database, ctrl.GetValue())
  92. GError(parent=self, message=message, caption=_("Invalid location name"))
  93. def _isLocationNameValid(self, text):
  94. """Check whether user's input location is valid or not."""
  95. return is_location_name_valid(self.database, text)
  96. def create_mapset_interactively(guiparent, grassdb, location):
  97. """
  98. Create new mapset
  99. """
  100. dlg = MapsetDialog(
  101. parent=guiparent,
  102. default=get_default_mapset_name(),
  103. message=_("Name for the new mapset:"),
  104. caption=_("Create new mapset"),
  105. database=grassdb,
  106. location=location,
  107. )
  108. mapset = None
  109. if dlg.ShowModal() == wx.ID_OK:
  110. mapset = dlg.GetValue()
  111. try:
  112. create_mapset(grassdb, location, mapset)
  113. except OSError as err:
  114. mapset = None
  115. GError(
  116. parent=guiparent,
  117. message=_("Unable to create new mapset: {}").format(err),
  118. showTraceback=False,
  119. )
  120. dlg.Destroy()
  121. return mapset
  122. def create_location_interactively(guiparent, grassdb):
  123. """
  124. Create new location using Location Wizard.
  125. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  126. by default or another mapset a user created and may want to switch to.
  127. """
  128. from location_wizard.wizard import LocationWizard
  129. gWizard = LocationWizard(parent=guiparent, grassdatabase=grassdb)
  130. if gWizard.location is None:
  131. gWizard_output = (None, None, None)
  132. # Returns Nones after Cancel
  133. return gWizard_output
  134. if gWizard.georeffile:
  135. message = _(
  136. "Do you want to import {} " "to the newly created location?"
  137. ).format(gWizard.georeffile)
  138. dlg = wx.MessageDialog(
  139. parent=guiparent,
  140. message=message,
  141. caption=_("Import data?"),
  142. style=wx.YES_NO | wx.YES_DEFAULT | wx.ICON_QUESTION,
  143. )
  144. dlg.CenterOnParent()
  145. if dlg.ShowModal() == wx.ID_YES:
  146. gisrc_file, env = create_environment(
  147. gWizard.grassdatabase, gWizard.location, "PERMANENT"
  148. )
  149. import_file(guiparent, gWizard.georeffile, env)
  150. try_remove(gisrc_file)
  151. dlg.Destroy()
  152. if gWizard.default_region:
  153. defineRegion = RegionDef(guiparent, location=gWizard.location)
  154. defineRegion.CenterOnParent()
  155. defineRegion.ShowModal()
  156. defineRegion.Destroy()
  157. if gWizard.user_mapset:
  158. mapset = create_mapset_interactively(
  159. guiparent, gWizard.grassdatabase, gWizard.location
  160. )
  161. # Returns database and location created by user
  162. # and a mapset user may want to switch to
  163. gWizard_output = (gWizard.grassdatabase, gWizard.location, mapset)
  164. else:
  165. # Returns PERMANENT mapset when user mapset not defined
  166. gWizard_output = (gWizard.grassdatabase, gWizard.location, "PERMANENT")
  167. return gWizard_output
  168. def rename_mapset_interactively(guiparent, grassdb, location, mapset):
  169. """Rename mapset with user interaction.
  170. Exceptions during renaming are handled in get_reason_mapset_not_removable
  171. function.
  172. Returns newmapset if there was a change or None if the mapset cannot be
  173. renamed (see reasons given by get_reason_mapset_not_removable
  174. function) or if another error was encountered.
  175. """
  176. newmapset = None
  177. # Check selected mapset
  178. message = get_reason_mapset_not_removable(
  179. grassdb, location, mapset, check_permanent=True
  180. )
  181. if message:
  182. dlg = wx.MessageDialog(
  183. parent=guiparent,
  184. message=_(
  185. "Cannot rename mapset <{mapset}> for the following reason:\n\n"
  186. "{reason}\n\n"
  187. "No mapset will be renamed."
  188. ).format(mapset=mapset, reason=message),
  189. caption=_("Unable to rename selected mapset"),
  190. style=wx.OK | wx.ICON_WARNING,
  191. )
  192. dlg.ShowModal()
  193. dlg.Destroy()
  194. return newmapset
  195. # Display question dialog
  196. dlg = MapsetDialog(
  197. parent=guiparent,
  198. default=mapset,
  199. message=_("Current name: {}\n\nEnter new name:").format(mapset),
  200. caption=_("Rename selected mapset"),
  201. database=grassdb,
  202. location=location,
  203. )
  204. if dlg.ShowModal() == wx.ID_OK:
  205. newmapset = dlg.GetValue()
  206. try:
  207. rename_mapset(grassdb, location, mapset, newmapset)
  208. except OSError as err:
  209. newmapset = None
  210. wx.MessageBox(
  211. parent=guiparent,
  212. caption=_("Error"),
  213. message=_("Unable to rename mapset.\n\n{}").format(err),
  214. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  215. )
  216. dlg.Destroy()
  217. return newmapset
  218. def rename_location_interactively(guiparent, grassdb, location):
  219. """Rename location with user interaction.
  220. Exceptions during renaming are handled in get_reasons_location_not_removable
  221. function.
  222. Returns newlocation if there was a change or None if the location cannot be
  223. renamed (see reasons given by get_reasons_location_not_removable
  224. function) or if another error was encountered.
  225. """
  226. newlocation = None
  227. # Check selected location
  228. messages = get_reasons_location_not_removable(grassdb, location)
  229. if messages:
  230. dlg = wx.MessageDialog(
  231. parent=guiparent,
  232. message=_(
  233. "Cannot rename location <{location}> for the following reasons:\n\n"
  234. "{reasons}\n\n"
  235. "No location will be renamed."
  236. ).format(location=location, reasons="\n".join(messages)),
  237. caption=_("Unable to rename selected location"),
  238. style=wx.OK | wx.ICON_WARNING,
  239. )
  240. dlg.ShowModal()
  241. dlg.Destroy()
  242. return newlocation
  243. # Display question dialog
  244. dlg = LocationDialog(
  245. parent=guiparent,
  246. default=location,
  247. message=_("Current name: {}\n\nEnter new name:").format(location),
  248. caption=_("Rename selected location"),
  249. database=grassdb,
  250. )
  251. if dlg.ShowModal() == wx.ID_OK:
  252. newlocation = dlg.GetValue()
  253. try:
  254. rename_location(grassdb, location, newlocation)
  255. except OSError as err:
  256. newlocation = None
  257. wx.MessageBox(
  258. parent=guiparent,
  259. caption=_("Error"),
  260. message=_("Unable to rename location.\n\n{}").format(err),
  261. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  262. )
  263. dlg.Destroy()
  264. return newlocation
  265. def download_location_interactively(guiparent, grassdb):
  266. """
  267. Download new location using Location Wizard.
  268. Returns tuple (database, location, mapset) where mapset is "PERMANENT"
  269. by default or in future it could be the mapset the user may want to
  270. switch to.
  271. """
  272. from startup.locdownload import LocationDownloadDialog
  273. result = (None, None, None)
  274. loc_download = LocationDownloadDialog(parent=guiparent, database=grassdb)
  275. loc_download.Centre()
  276. loc_download.ShowModal()
  277. if loc_download.GetLocation() is not None:
  278. # Returns database and location created by user
  279. # and a mapset user may want to switch to
  280. result = (grassdb, loc_download.GetLocation(), "PERMANENT")
  281. loc_download.Destroy()
  282. return result
  283. def delete_mapset_interactively(guiparent, grassdb, location, mapset):
  284. """Delete one mapset with user interaction.
  285. This is currently just a convenience wrapper for delete_mapsets_interactively().
  286. """
  287. mapsets = [(grassdb, location, mapset)]
  288. return delete_mapsets_interactively(guiparent, mapsets)
  289. def delete_mapsets_interactively(guiparent, mapsets):
  290. """Delete multiple mapsets with user interaction.
  291. Parameter *mapsets* is a list of tuples (database, location, mapset).
  292. Exceptions during deletation are handled in get_reasons_mapsets_not_removable
  293. function.
  294. Returns True if there was a change, i.e., all mapsets were successfully
  295. deleted or at least one mapset was deleted.
  296. Returns False if one or more mapsets cannot be deleted (see reasons given
  297. by get_reasons_mapsets_not_removable function) or if an error was
  298. encountered when deleting the first mapset in the list.
  299. """
  300. deletes = []
  301. modified = False
  302. # Check selected mapsets
  303. messages = get_reasons_mapsets_not_removable(mapsets, check_permanent=True)
  304. if messages:
  305. dlg = wx.MessageDialog(
  306. parent=guiparent,
  307. message=_(
  308. "Cannot delete one or more mapsets for the following reasons:\n\n"
  309. "{reasons}\n\n"
  310. "No mapsets will be deleted."
  311. ).format(reasons="\n".join(messages)),
  312. caption=_("Unable to delete selected mapsets"),
  313. style=wx.OK | wx.ICON_WARNING,
  314. )
  315. dlg.ShowModal()
  316. dlg.Destroy()
  317. return modified
  318. # No error occurs, create list of mapsets for deleting
  319. for grassdb, location, mapset in mapsets:
  320. mapset_path = os.path.join(grassdb, location, mapset)
  321. deletes.append(mapset_path)
  322. # Display question dialog
  323. dlg = wx.MessageDialog(
  324. parent=guiparent,
  325. message=_(
  326. "Do you want to continue with deleting"
  327. " one or more of the following mapsets?\n\n"
  328. "{deletes}\n\n"
  329. "All maps included in these mapsets will be permanently deleted!"
  330. ).format(deletes="\n".join(deletes)),
  331. caption=_("Delete selected mapsets"),
  332. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  333. )
  334. if dlg.ShowModal() == wx.ID_YES:
  335. try:
  336. for grassdb, location, mapset in mapsets:
  337. delete_mapset(grassdb, location, mapset)
  338. modified = True
  339. dlg.Destroy()
  340. return modified
  341. except OSError as error:
  342. wx.MessageBox(
  343. parent=guiparent,
  344. caption=_("Error when deleting mapsets"),
  345. message=_(
  346. "The following error occured when deleting mapset <{path}>:"
  347. "\n\n{error}\n\n"
  348. "Deleting of mapsets was interrupted."
  349. ).format(
  350. path=os.path.join(grassdb, location, mapset),
  351. error=error,
  352. ),
  353. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  354. )
  355. dlg.Destroy()
  356. return modified
  357. def delete_location_interactively(guiparent, grassdb, location):
  358. """Delete one location with user interaction.
  359. This is currently just a convenience wrapper for delete_locations_interactively().
  360. """
  361. locations = [(grassdb, location)]
  362. return delete_locations_interactively(guiparent, locations)
  363. def delete_locations_interactively(guiparent, locations):
  364. """Delete multiple locations with user interaction.
  365. Parameter *locations* is a list of tuples (database, location).
  366. Exceptions during deletation are handled in get_reasons_locations_not_removable
  367. function.
  368. Returns True if there was a change, i.e., all locations were successfully
  369. deleted or at least one location was deleted.
  370. Returns False if one or more locations cannot be deleted (see reasons given
  371. by get_reasons_locations_not_removable function) or if an error was
  372. encountered when deleting the first location in the list.
  373. """
  374. deletes = []
  375. modified = False
  376. # Check selected locations
  377. messages = get_reasons_locations_not_removable(locations)
  378. if messages:
  379. dlg = wx.MessageDialog(
  380. parent=guiparent,
  381. message=_(
  382. "Cannot delete one or more locations for the following reasons:\n\n"
  383. "{reasons}\n\n"
  384. "No locations will be deleted."
  385. ).format(reasons="\n".join(messages)),
  386. caption=_("Unable to delete selected locations"),
  387. style=wx.OK | wx.ICON_WARNING,
  388. )
  389. dlg.ShowModal()
  390. dlg.Destroy()
  391. return modified
  392. # No error occurs, create list of locations for deleting
  393. for grassdb, location in locations:
  394. location_path = os.path.join(grassdb, location)
  395. deletes.append(location_path)
  396. # Display question dialog
  397. dlg = wx.MessageDialog(
  398. parent=guiparent,
  399. message=_(
  400. "Do you want to continue with deleting"
  401. " one or more of the following locations?\n\n"
  402. "{deletes}\n\n"
  403. "All mapsets included in these locations will be permanently deleted!"
  404. ).format(deletes="\n".join(deletes)),
  405. caption=_("Delete selected locations"),
  406. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  407. )
  408. if dlg.ShowModal() == wx.ID_YES:
  409. try:
  410. for grassdb, location in locations:
  411. delete_location(grassdb, location)
  412. modified = True
  413. dlg.Destroy()
  414. return modified
  415. except OSError as error:
  416. wx.MessageBox(
  417. parent=guiparent,
  418. caption=_("Error when deleting locations"),
  419. message=_(
  420. "The following error occured when deleting location <{path}>:"
  421. "\n\n{error}\n\n"
  422. "Deleting of locations was interrupted."
  423. ).format(
  424. path=os.path.join(grassdb, location),
  425. error=error,
  426. ),
  427. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  428. )
  429. dlg.Destroy()
  430. return modified
  431. def delete_grassdb_interactively(guiparent, grassdb):
  432. """
  433. Delete grass database if could be deleted.
  434. If current grass database found, desired operation cannot be performed.
  435. Exceptions during deleting are handled in this function.
  436. Returns True if grass database is deleted from the disk. Returns None if
  437. cannot be deleted (see above the possible reasons).
  438. """
  439. deleted = False
  440. # Check selected grassdb
  441. messages = get_reasons_grassdb_not_removable(grassdb)
  442. if messages:
  443. dlg = wx.MessageDialog(
  444. parent=guiparent,
  445. message=_(
  446. "Cannot delete GRASS database from disk for the following reason:\n\n"
  447. "{reasons}\n\n"
  448. "GRASS database will not be deleted."
  449. ).format(reasons="\n".join(messages)),
  450. caption=_("Unable to delete selected GRASS database"),
  451. style=wx.OK | wx.ICON_WARNING,
  452. )
  453. dlg.ShowModal()
  454. else:
  455. dlg = wx.MessageDialog(
  456. parent=guiparent,
  457. message=_(
  458. "Do you want to delete"
  459. " the following GRASS database from disk?\n\n"
  460. "{grassdb}\n\n"
  461. "The directory will be permanently deleted!"
  462. ).format(grassdb=grassdb),
  463. caption=_("Delete selected GRASS database"),
  464. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  465. )
  466. if dlg.ShowModal() == wx.ID_YES:
  467. try:
  468. delete_grassdb(grassdb)
  469. deleted = True
  470. dlg.Destroy()
  471. return deleted
  472. except OSError as error:
  473. wx.MessageBox(
  474. parent=guiparent,
  475. caption=_("Error when deleting GRASS database"),
  476. message=_(
  477. "The following error occured when deleting database <{path}>:"
  478. "\n\n{error}\n\n"
  479. "Deleting of GRASS database was interrupted."
  480. ).format(
  481. path=grassdb,
  482. error=error,
  483. ),
  484. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  485. )
  486. dlg.Destroy()
  487. return deleted
  488. def can_switch_mapset_interactive(guiparent, grassdb, location, mapset):
  489. """
  490. Checks if mapset is locked and offers to remove the lock file.
  491. Returns True if user wants to switch to the selected mapset in spite of
  492. removing lock. Returns False if a user wants to stay in the current
  493. mapset or if an error was encountered.
  494. """
  495. can_switch = True
  496. mapset_path = os.path.join(grassdb, location, mapset)
  497. if is_mapset_locked(mapset_path):
  498. info = get_mapset_lock_info(mapset_path)
  499. user = info["owner"] if info["owner"] else _("unknown")
  500. lockpath = info["lockpath"]
  501. timestamp = info["timestamp"]
  502. dlg = wx.MessageDialog(
  503. parent=guiparent,
  504. message=_(
  505. "User {user} is already running GRASS in selected mapset "
  506. "<{mapset}>\n (file {lockpath} created {timestamp} "
  507. "found).\n\n"
  508. "Concurrent use not allowed.\n\n"
  509. "Do you want to stay in the current mapset or remove "
  510. ".gislock and switch to selected mapset?"
  511. ).format(user=user, mapset=mapset, lockpath=lockpath, timestamp=timestamp),
  512. caption=_("Mapset is in use"),
  513. style=wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION,
  514. )
  515. dlg.SetYesNoLabels("S&witch to selected mapset", "S&tay in current mapset")
  516. if dlg.ShowModal() == wx.ID_YES:
  517. # Remove lockfile
  518. try:
  519. os.remove(lockpath)
  520. except IOError as e:
  521. wx.MessageBox(
  522. parent=guiparent,
  523. caption=_("Error when removing lock file"),
  524. message=_(
  525. "Unable to remove {lockpath}.\n\n Details: {error}."
  526. ).format(lockpath=lockpath, error=e),
  527. style=wx.OK | wx.ICON_ERROR | wx.CENTRE,
  528. )
  529. can_switch = False
  530. else:
  531. can_switch = False
  532. dlg.Destroy()
  533. return can_switch
  534. def import_file(guiparent, filePath, env):
  535. """Tries to import file as vector or raster.
  536. If successful sets default region from imported map.
  537. """
  538. RunCommand("db.connect", flags="c", env=env)
  539. mapName = os.path.splitext(os.path.basename(filePath))[0]
  540. vectors = RunCommand("v.in.ogr", input=filePath, flags="l", read=True, env=env)
  541. wx.BeginBusyCursor()
  542. wx.GetApp().Yield()
  543. if vectors:
  544. # vector detected
  545. returncode, error = RunCommand(
  546. "v.in.ogr", input=filePath, output=mapName, getErrorMsg=True, env=env
  547. )
  548. if returncode == 0:
  549. RunCommand("g.region", flags="s", vector=mapName, env=env)
  550. else:
  551. returncode, error = RunCommand(
  552. "r.in.gdal", input=filePath, output=mapName, getErrorMsg=True, env=env
  553. )
  554. if returncode == 0:
  555. RunCommand("g.region", flags="s", raster=mapName, env=env)
  556. wx.EndBusyCursor()
  557. if returncode != 0:
  558. GError(
  559. parent=guiparent,
  560. message=_("Import of <%(name)s> failed.\n" "Reason: %(msg)s")
  561. % ({"name": filePath, "msg": error}),
  562. )
  563. else:
  564. GMessage(
  565. message=_(
  566. "Data file <%(name)s> imported successfully. "
  567. "The location's default region was set from "
  568. "this imported map."
  569. )
  570. % {"name": filePath},
  571. parent=guiparent,
  572. )
  573. def switch_mapset_interactively(
  574. guiparent, giface, dbase, location, mapset, show_confirmation=False
  575. ):
  576. """Switch current mapset. Emits giface.currentMapsetChanged signal."""
  577. # Decide if a user is in a fallback session
  578. fallback_session = is_fallback_session()
  579. if dbase:
  580. if (
  581. RunCommand(
  582. "g.mapset",
  583. parent=guiparent,
  584. location=location,
  585. mapset=mapset,
  586. dbase=dbase,
  587. )
  588. == 0
  589. ):
  590. if show_confirmation:
  591. GMessage(
  592. parent=guiparent,
  593. message=_(
  594. "Current GRASS database is <%(dbase)s>.\n"
  595. "Current location is <%(loc)s>.\n"
  596. "Current mapset is <%(mapset)s>."
  597. )
  598. % {"dbase": dbase, "loc": location, "mapset": mapset},
  599. )
  600. giface.currentMapsetChanged.emit(
  601. dbase=dbase, location=location, mapset=mapset
  602. )
  603. elif location:
  604. if (
  605. RunCommand("g.mapset", parent=guiparent, location=location, mapset=mapset)
  606. == 0
  607. ):
  608. if show_confirmation:
  609. GMessage(
  610. parent=guiparent,
  611. message=_(
  612. "Current location is <%(loc)s>.\n"
  613. "Current mapset is <%(mapset)s>."
  614. )
  615. % {"loc": location, "mapset": mapset},
  616. )
  617. giface.currentMapsetChanged.emit(
  618. dbase=None, location=location, mapset=mapset
  619. )
  620. else:
  621. if RunCommand("g.mapset", parent=guiparent, mapset=mapset) == 0:
  622. if show_confirmation:
  623. GMessage(
  624. parent=guiparent, message=_("Current mapset is <%s>.") % mapset
  625. )
  626. giface.currentMapsetChanged.emit(dbase=None, location=None, mapset=mapset)
  627. if fallback_session:
  628. tmp_dbase = os.environ["TMPDIR"]
  629. tmp_loc = cfg.temporary_location
  630. if tmp_dbase != gisenv()["GISDBASE"]:
  631. # Delete temporary location
  632. delete_location(tmp_dbase, tmp_loc)
  633. # Remove useless temporary grassdb node
  634. giface.grassdbChanged.emit(
  635. location=location, grassdb=tmp_dbase, action="delete", element="grassdb"
  636. )