utils.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. @package startup.utils
  3. @brief General GUI-independent utilities for GUI startup of GRASS GIS
  4. (C) 2017-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. This file should not use (import) anything from GUI code (wx or wxGUI).
  9. This can potentially be part of the Python library (i.e. it needs to
  10. solve the errors etc. in a general manner).
  11. """
  12. import os
  13. def get_possible_database_path():
  14. """Finds a path to what is possibly a GRASS Database.
  15. Looks for directory named grassdata in the usual locations.
  16. Returns the path as a string or None if nothing was found, so the
  17. return value can be used to test if the directory was found.
  18. """
  19. home = os.path.expanduser('~')
  20. # try some common directories for grassdata
  21. # grassdata (lowercase) in home for Linux (first choice)
  22. # Documents and My Documents for Windows
  23. # potential translations (old Windows and some Linux)
  24. # but ~ and ~/Documents should cover most of the cases
  25. # ordered by preference and then likelihood
  26. candidates = [
  27. os.path.join(home, "grassdata"),
  28. os.path.join(home, "Documents", "grassdata"),
  29. os.path.join(home, "My Documents", "grassdata"),
  30. ]
  31. try:
  32. # here goes everything which has potential unicode issues
  33. candidates.append(os.path.join(home, _("Documents"), "grassdata"))
  34. candidates.append(os.path.join(home, _("My Documents"), "grassdata"))
  35. except UnicodeDecodeError:
  36. # just ignore the errors if it doesn't work
  37. pass
  38. path = None
  39. for candidate in candidates:
  40. if os.path.exists(candidate):
  41. path = candidate
  42. break # get the first match
  43. return path
  44. def get_lockfile_if_present(database, location, mapset):
  45. """Return path to lock if present, None otherwise
  46. Returns the path as a string or None if nothing was found, so the
  47. return value can be used to test if the lock is present.
  48. """
  49. lock_name = '.gislock'
  50. lockfile = os.path.join(database, location, mapset, lock_name)
  51. if os.path.isfile(lockfile):
  52. return lockfile
  53. else:
  54. return None