__init__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python2.7
  3. from __future__ import (nested_scopes, generators, division, absolute_import,
  4. with_statement, print_function, unicode_literals)
  5. from os import listdir
  6. from os.path import join, isdir
  7. import shutil
  8. import ctypes as ct
  9. import fnmatch
  10. import grass.lib.gis as libgis
  11. from grass.pygrass.functions import getenv
  12. from grass.pygrass.errors import GrassError
  13. ETYPE = {'rast': libgis.G_ELEMENT_RASTER,
  14. 'rast3d': libgis.G_ELEMENT_RASTER3D,
  15. 'vect': libgis.G_ELEMENT_VECTOR,
  16. 'oldvect': libgis.G_ELEMENT_OLDVECTOR,
  17. 'asciivect': libgis.G_ELEMENT_ASCIIVECTOR,
  18. 'icon': libgis.G_ELEMENT_ICON,
  19. 'labels': libgis.G_ELEMENT_LABEL,
  20. 'sites': libgis.G_ELEMENT_SITE,
  21. 'region': libgis.G_ELEMENT_REGION,
  22. 'region3d': libgis.G_ELEMENT_REGION3D,
  23. 'group': libgis.G_ELEMENT_GROUP,
  24. 'view3d': libgis.G_ELEMENT_3DVIEW}
  25. CHECK_IS = {"GISBASE": libgis.G_is_gisbase,
  26. "GISDBASE": lambda x: True,
  27. "LOCATION_NAME": libgis.G_is_location,
  28. "MAPSET": libgis.G_is_mapset}
  29. def _check(value, path, type):
  30. if value and CHECK_IS[type](join(path, value)):
  31. return value
  32. elif value is '':
  33. return getenv(type)
  34. else:
  35. raise GrassError("%s <%s> not found" % (type.title(),
  36. join(path, value)))
  37. def set_current_mapset(mapset, location=None, gisdbase=None):
  38. """Set the current mapset as working area
  39. """
  40. libgis.G_setenv('MAPSET', mapset)
  41. if location:
  42. libgis.G_setenv('LOCATION_NAME', location)
  43. if gisdbase:
  44. libgis.G_setenv('GISDBASE', gisdbase)
  45. def make_mapset(mapset, location=None, gisdbase=None):
  46. """Create a new mapset"""
  47. res = libgis.G_make_mapset(gisdbase, location, mapset)
  48. if res == -1:
  49. raise GrassError("Cannot create new mapset")
  50. elif res == -2:
  51. raise GrassError("Illegal name")
  52. class Gisdbase(object):
  53. """Return Gisdbase object. ::
  54. >>> from grass.script.core import gisenv
  55. >>> gisdbase = Gisdbase()
  56. >>> gisdbase.name == gisenv()['GISDBASE']
  57. True
  58. ..
  59. """
  60. def __init__(self, gisdbase=''):
  61. self.name = gisdbase
  62. def _get_name(self):
  63. return self._name
  64. def _set_name(self, name):
  65. self._name = _check(name, '', "GISDBASE")
  66. name = property(fget=_get_name, fset=_set_name,
  67. doc="Set or obtain the name of GISDBASE")
  68. def __str__(self):
  69. return self.name
  70. def __repr__(self):
  71. return 'Gisdbase(%s)' % self.name
  72. def __getitem__(self, location):
  73. """Return a Location object. ::
  74. >>> from grass.script.core import gisenv
  75. >>> loc_env = gisenv()['LOCATION_NAME']
  76. >>> gisdbase = Gisdbase()
  77. >>> loc_py = gisdbase[loc_env]
  78. >>> loc_env == loc_py.name
  79. True
  80. ..
  81. """
  82. if location in self.locations():
  83. return Location(location, self.name)
  84. else:
  85. raise KeyError('Location: %s does not exist' % location)
  86. def __iter__(self):
  87. for loc in self.locations():
  88. yield Location(loc, self.name)
  89. # TODO remove or complete this function
  90. def new_location(self):
  91. if libgis.G__make_location() != 0:
  92. raise GrassError("Cannot create new location")
  93. def locations(self):
  94. """Return a list of locations that are available in the gisdbase: ::
  95. >>> gisdbase = Gisdbase()
  96. >>> gisdbase.locations() # doctest: +ELLIPSIS
  97. [...]
  98. ..
  99. """
  100. locations = []
  101. for loc in listdir(self.name):
  102. if libgis.G_is_location(join(self.name, loc)):
  103. locations.append(loc)
  104. locations.sort()
  105. return locations
  106. class Location(object):
  107. """Location object ::
  108. >>> from grass.script.core import gisenv
  109. >>> location = Location()
  110. >>> location # doctest: +ELLIPSIS
  111. Location(...)
  112. >>> location.gisdbase == gisenv()['GISDBASE']
  113. True
  114. >>> location.name == gisenv()['LOCATION_NAME']
  115. True
  116. ..
  117. """
  118. def __init__(self, location='', gisdbase=''):
  119. self.gisdbase = gisdbase
  120. self.name = location
  121. def _get_gisdb(self):
  122. return self._gisdb
  123. def _set_gisdb(self, gisdb):
  124. self._gisdb = _check(gisdb, '', "GISDBASE")
  125. gisdbase = property(fget=_get_gisdb, fset=_set_gisdb,
  126. doc="Set or obtain the name of GISDBASE")
  127. def _get_name(self):
  128. return self._name
  129. def _set_name(self, name):
  130. self._name = _check(name, self._gisdb, "LOCATION_NAME")
  131. name = property(fget=_get_name, fset=_set_name,
  132. doc="Set or obtain the name of LOCATION")
  133. def __getitem__(self, mapset):
  134. if mapset in self.mapsets():
  135. return Mapset(mapset)
  136. else:
  137. raise KeyError('Mapset: %s does not exist' % mapset)
  138. def __iter__(self):
  139. lpath = self.path()
  140. return (m for m in listdir(lpath)
  141. if (isdir(join(lpath, m)) and _check(m, lpath, "MAPSET")))
  142. def __len__(self):
  143. return len(self.mapsets())
  144. def __str__(self):
  145. return self.name
  146. def __repr__(self):
  147. return 'Location(%r)' % self.name
  148. def mapsets(self, pattern=None, permissions=True):
  149. """Return a list of the available mapsets.
  150. :param pattern: the pattern to filter the result
  151. :type pattern: str
  152. :param permissions: check the permission of mapset
  153. :type permissions: bool
  154. :returns: a list of mapset's names
  155. ::
  156. >>> location = Location()
  157. >>> location.mapsets()
  158. ['PERMANENT', 'user1']
  159. ..
  160. """
  161. mapsets = [mapset for mapset in self]
  162. if permissions:
  163. mapsets = [mapset for mapset in mapsets
  164. if libgis.G__mapset_permissions(mapset)]
  165. if pattern:
  166. return fnmatch.filter(mapsets, pattern)
  167. return mapsets
  168. def path(self):
  169. return join(self.gisdbase, self.name)
  170. class Mapset(object):
  171. """Mapset ::
  172. >>> mapset = Mapset()
  173. >>> mapset
  174. Mapset('user1')
  175. >>> mapset.gisdbase # doctest: +ELLIPSIS
  176. '/home/...'
  177. >>> mapset.location
  178. 'nc_basic_spm_grass7'
  179. >>> mapset.name
  180. 'user1'
  181. ..
  182. """
  183. def __init__(self, mapset='', location='', gisdbase=''):
  184. self.gisdbase = gisdbase
  185. self.location = location
  186. self.name = mapset
  187. self.visible = VisibleMapset(self.name, self.location, self.gisdbase)
  188. def _get_gisdb(self):
  189. return self._gisdb
  190. def _set_gisdb(self, gisdb):
  191. self._gisdb = _check(gisdb, '', "GISDBASE")
  192. gisdbase = property(fget=_get_gisdb, fset=_set_gisdb,
  193. doc="Set or obtain the name of GISDBASE")
  194. def _get_loc(self):
  195. return self._loc
  196. def _set_loc(self, loc):
  197. self._loc = _check(loc, self._gisdb, "LOCATION_NAME")
  198. location = property(fget=_get_loc, fset=_set_loc,
  199. doc="Set or obtain the name of LOCATION")
  200. def _get_name(self):
  201. return self._name
  202. def _set_name(self, name):
  203. self._name = _check(name, join(self._gisdb, self._loc), "MAPSET")
  204. name = property(fget=_get_name, fset=_set_name,
  205. doc="Set or obtain the name of MAPSET")
  206. def __str__(self):
  207. return self.name
  208. def __repr__(self):
  209. return 'Mapset(%r)' % self.name
  210. def glist(self, type, pattern=None):
  211. """Return a list of grass types like:
  212. * 'asciivect',
  213. * 'group',
  214. * 'icon',
  215. * 'labels',
  216. * 'oldvect',
  217. * 'rast',
  218. * 'rast3d',
  219. * 'region',
  220. * 'region3d',
  221. * 'sites',
  222. * 'vect',
  223. * 'view3d'
  224. :param type: the type of element to query
  225. :type type: str
  226. :param pattern: the pattern to filter the result
  227. :type pattern: str
  228. ::
  229. >>> mapset = Mapset('PERMANENT')
  230. >>> rast = mapset.glist('rast')
  231. >>> rast.sort()
  232. >>> rast # doctest: +ELLIPSIS
  233. ['basins', 'elevation', ...]
  234. >>> mapset.glist('rast', pattern='el*')
  235. ['elevation_shade', 'elevation']
  236. ..
  237. """
  238. if type not in ETYPE:
  239. str_err = "Type %s is not valid, valid types are: %s."
  240. raise TypeError(str_err % (type, ', '.join(ETYPE.keys())))
  241. clist = libgis.G_list(ETYPE[type], self.gisdbase,
  242. self.location, self.name)
  243. elist = []
  244. for el in clist:
  245. el_name = ct.cast(el, ct.c_char_p).value
  246. if el_name:
  247. elist.append(el_name)
  248. else:
  249. if pattern:
  250. return fnmatch.filter(elist, pattern)
  251. return elist
  252. def is_current(self):
  253. """Check if the MAPSET is the working MAPSET"""
  254. return (self.name == libgis.G_getenv('MAPSET') and
  255. self.location == libgis.G_getenv('LOCATION_NAME') and
  256. self.gisdbase == libgis.G_getenv('GISDBASE'))
  257. def current(self):
  258. """Set the mapset as current"""
  259. set_current_mapset(self.name, self.location, self.gisdbase)
  260. def delete(self):
  261. """Delete the mapset"""
  262. if self.is_current():
  263. raise GrassError('The mapset is in use.')
  264. shutil.rmtree(self.path())
  265. def path(self):
  266. return join(self.gisdbase, self.location, self.name)
  267. class VisibleMapset(object):
  268. """VisibleMapset object::
  269. >>> mapset = VisibleMapset('user1')
  270. >>> mapset
  271. ['user1', 'PERMANENT']
  272. ..
  273. """
  274. def __init__(self, mapset, location='', gisdbase=''):
  275. self.mapset = mapset
  276. self.location = Location(location, gisdbase)
  277. self._list = []
  278. self.spath = join(self.location.path(), self.mapset, 'SEARCH_PATH')
  279. def __repr__(self):
  280. return repr(self.read())
  281. def __iter__(self):
  282. for mapset in self.read():
  283. yield mapset
  284. def read(self):
  285. """Return the mapsets in the search path"""
  286. with open(self.spath, "a+") as f:
  287. lines = f.readlines()
  288. if lines:
  289. return [l.strip() for l in lines]
  290. lns = ['PERMANENT', ]
  291. self.write(lns)
  292. return lns
  293. def _write(self, mapsets):
  294. """Write to SEARCH_PATH file the changes in the search path
  295. :param mapsets: a list of mapset's names
  296. :type mapsets: list
  297. """
  298. with open(self.spath, "w+") as f:
  299. ms = self.location.mapsets()
  300. f.write('%s' % '\n'.join([m for m in mapsets if m in ms]))
  301. def add(self, mapset):
  302. """Add a mapset to the search path
  303. :param mapset: a mapset's name
  304. :type mapset: str
  305. """
  306. if mapset not in self.read() and mapset in self.location:
  307. with open(self.spath, "a+") as f:
  308. f.write('\n%s' % mapset)
  309. else:
  310. raise TypeError('Mapset not found')
  311. def remove(self, mapset):
  312. """Remove mapset to the search path
  313. :param mapset: a mapset's name
  314. :type mapset: str
  315. """
  316. mapsets = self.read()
  317. mapsets.remove(mapset)
  318. self._write(mapsets)
  319. def extend(self, mapsets):
  320. """Add more mapsets to the search path
  321. :param mapsets: a list of mapset's names
  322. :type mapsets: list
  323. """
  324. ms = self.location.mapsets()
  325. final = self.read()
  326. final.extend([m for m in mapsets if m in ms and m not in final])
  327. self._write(final)
  328. def reset(self):
  329. """Reset to the original search path"""
  330. final = [self.mapset, 'PERMANENT']
  331. self._write(final)