guiutils.py 27 KB

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