__init__.py 13 KB

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