create.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """
  2. Create objects in GRASS GIS Spatial Database
  3. (C) 2020 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. .. sectionauthor:: Vaclav Petras <wenzeslaus gmail com>
  8. """
  9. import os
  10. import shutil
  11. import getpass
  12. def create_mapset(database, location, mapset):
  13. """Creates a mapset in a specified location"""
  14. location_path = os.path.join(database, location)
  15. mapset_path = os.path.join(location_path, mapset)
  16. # create an empty directory
  17. os.mkdir(mapset_path)
  18. # copy DEFAULT_WIND file and its permissions from PERMANENT
  19. # to WIND in the new mapset
  20. region_path1 = os.path.join(location_path, "PERMANENT", "DEFAULT_WIND")
  21. region_path2 = os.path.join(location_path, mapset, "WIND")
  22. shutil.copy(region_path1, region_path2)
  23. # set permissions to u+rw,go+r (disabled; why?)
  24. # os.chmod(os.path.join(database,location,mapset,'WIND'), 0644)
  25. def get_default_mapset_name():
  26. """Returns default name for mapset."""
  27. try:
  28. result = getpass.getuser()
  29. # Raise error if not ascii (not valid mapset name).
  30. result.encode("ascii")
  31. except UnicodeEncodeError:
  32. # Fall back to fixed name.
  33. result = "user"
  34. return result