core.py 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. """!@package grass.script.core
  2. @brief GRASS Python scripting module (core functions)
  3. Core functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. from grass.script import core as grass
  7. grass.parser()
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Glynn Clements
  15. @author Martin Landa <landa.martin gmail.com>
  16. @author Michael Barton <michael.barton asu.edu>
  17. """
  18. import os
  19. import sys
  20. import types
  21. import re
  22. import atexit
  23. import subprocess
  24. import shutil
  25. import locale
  26. import codecs
  27. # i18N
  28. import gettext
  29. gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
  30. # subprocess wrapper that uses shell on Windows
  31. class Popen(subprocess.Popen):
  32. def __init__(self, args, bufsize = 0, executable = None,
  33. stdin = None, stdout = None, stderr = None,
  34. preexec_fn = None, close_fds = False, shell = None,
  35. cwd = None, env = None, universal_newlines = False,
  36. startupinfo = None, creationflags = 0):
  37. if shell == None:
  38. shell = (sys.platform == "win32")
  39. subprocess.Popen.__init__(self, args, bufsize, executable,
  40. stdin, stdout, stderr,
  41. preexec_fn, close_fds, shell,
  42. cwd, env, universal_newlines,
  43. startupinfo, creationflags)
  44. PIPE = subprocess.PIPE
  45. STDOUT = subprocess.STDOUT
  46. class ScriptError(Exception):
  47. def __init__(self, msg):
  48. self.value = msg
  49. def __str__(self):
  50. return repr(self.value)
  51. raise_on_error = False # raise exception instead of calling fatal()
  52. debug_level = 0 # DEBUG level
  53. def call(*args, **kwargs):
  54. return Popen(*args, **kwargs).wait()
  55. # GRASS-oriented interface to subprocess module
  56. _popen_args = ["bufsize", "executable", "stdin", "stdout", "stderr",
  57. "preexec_fn", "close_fds", "cwd", "env",
  58. "universal_newlines", "startupinfo", "creationflags"]
  59. def _decode(string):
  60. enc = locale.getdefaultlocale()[1]
  61. if enc:
  62. return string.decode(enc)
  63. return string
  64. def _make_val(val):
  65. if isinstance(val, types.StringType) or \
  66. isinstance(val, types.UnicodeType):
  67. return val
  68. if isinstance(val, types.ListType):
  69. return ",".join(map(_make_val, val))
  70. if isinstance(val, types.TupleType):
  71. return _make_val(list(val))
  72. return str(val)
  73. def make_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **options):
  74. """!Return a list of strings suitable for use as the args parameter to
  75. Popen() or call(). Example:
  76. @code
  77. >>> grass.make_command("g.message", flags = 'w', message = 'this is a warning')
  78. ['g.message', '-w', 'message=this is a warning']
  79. @endcode
  80. @param prog GRASS module
  81. @param flags flags to be used (given as a string)
  82. @param overwrite True to enable overwriting the output (<tt>--o</tt>)
  83. @param quiet True to run quietly (<tt>--q</tt>)
  84. @param verbose True to run verbosely (<tt>--v</tt>)
  85. @param options module's parameters
  86. @return list of arguments
  87. """
  88. args = [prog]
  89. if overwrite:
  90. args.append("--o")
  91. if quiet:
  92. args.append("--q")
  93. if verbose:
  94. args.append("--v")
  95. if flags:
  96. args.append("-%s" % flags)
  97. for opt, val in options.iteritems():
  98. if val != None:
  99. if opt[0] == '_':
  100. opt = opt[1:]
  101. args.append("%s=%s" % (opt, _make_val(val)))
  102. return args
  103. def start_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, **kwargs):
  104. """!Returns a Popen object with the command created by make_command.
  105. Accepts any of the arguments which Popen() accepts apart from "args"
  106. and "shell".
  107. \code
  108. >>> p = grass.start_command("g.gisenv", stdout = subprocess.PIPE)
  109. >>> print p
  110. <subprocess.Popen object at 0xb7c12f6c>
  111. >>> print p.communicate()[0]
  112. GISDBASE='/opt/grass-data';
  113. LOCATION_NAME='spearfish60';
  114. MAPSET='glynn';
  115. GRASS_DB_ENCODING='ascii';
  116. GRASS_GUI='text';
  117. MONITOR='x0';
  118. \endcode
  119. @param prog GRASS module
  120. @param flags flags to be used (given as a string)
  121. @param overwrite True to enable overwriting the output (<tt>--o</tt>)
  122. @param quiet True to run quietly (<tt>--q</tt>)
  123. @param verbose True to run verbosely (<tt>--v</tt>)
  124. @param kwargs module's parameters
  125. @return Popen object
  126. """
  127. options = {}
  128. popts = {}
  129. for opt, val in kwargs.iteritems():
  130. if opt in _popen_args:
  131. popts[opt] = val
  132. else:
  133. options[opt] = val
  134. args = make_command(prog, flags, overwrite, quiet, verbose, **options)
  135. global debug_level
  136. if debug_level > 0:
  137. sys.stderr.write("D1/%d: %s.start_command(): %s\n" % (debug_level, __name__, ' '.join(args)))
  138. sys.stderr.flush()
  139. return Popen(args, **popts)
  140. def run_command(*args, **kwargs):
  141. """!Passes all arguments to start_command(), then waits for the process to
  142. complete, returning its exit code. Similar to subprocess.call(), but
  143. with the make_command() interface.
  144. @param args list of unnamed arguments (see start_command() for details)
  145. @param kwargs list of named arguments (see start_command() for details)
  146. @return exit code (0 for success)
  147. """
  148. ps = start_command(*args, **kwargs)
  149. return ps.wait()
  150. def pipe_command(*args, **kwargs):
  151. """!Passes all arguments to start_command(), but also adds
  152. "stdout = PIPE". Returns the Popen object.
  153. \code
  154. >>> p = grass.pipe_command("g.gisenv")
  155. >>> print p
  156. <subprocess.Popen object at 0xb7c12f6c>
  157. >>> print p.communicate()[0]
  158. GISDBASE='/opt/grass-data';
  159. LOCATION_NAME='spearfish60';
  160. MAPSET='glynn';
  161. GRASS_DB_ENCODING='ascii';
  162. GRASS_GUI='text';
  163. MONITOR='x0';
  164. \endcode
  165. @param args list of unnamed arguments (see start_command() for details)
  166. @param kwargs list of named arguments (see start_command() for details)
  167. @return Popen object
  168. """
  169. kwargs['stdout'] = PIPE
  170. return start_command(*args, **kwargs)
  171. def feed_command(*args, **kwargs):
  172. """!Passes all arguments to start_command(), but also adds
  173. "stdin = PIPE". Returns the Popen object.
  174. @param args list of unnamed arguments (see start_command() for details)
  175. @param kwargs list of named arguments (see start_command() for details)
  176. @return Popen object
  177. """
  178. kwargs['stdin'] = PIPE
  179. return start_command(*args, **kwargs)
  180. def read_command(*args, **kwargs):
  181. """!Passes all arguments to pipe_command, then waits for the process to
  182. complete, returning its stdout (i.e. similar to shell `backticks`).
  183. @param args list of unnamed arguments (see start_command() for details)
  184. @param kwargs list of named arguments (see start_command() for details)
  185. @return stdout
  186. """
  187. ps = pipe_command(*args, **kwargs)
  188. return _decode(ps.communicate()[0])
  189. def parse_command(*args, **kwargs):
  190. """!Passes all arguments to read_command, then parses the output by
  191. parse_key_val().
  192. Parsing function can be optionally given by <b>parse</b> parameter
  193. including its arguments, e.g.
  194. @code
  195. parse_command(..., parse = (grass.parse_key_val, { 'sep' : ':' }))
  196. @endcode
  197. @param args list of unnamed arguments (see start_command() for details)
  198. @param kwargs list of named arguments (see start_command() for details)
  199. @return parsed module output
  200. """
  201. parse = None
  202. if kwargs.has_key('parse'):
  203. if type(kwargs['parse']) is types.TupleType:
  204. parse = kwargs['parse'][0]
  205. parse_args = kwargs['parse'][1]
  206. del kwargs['parse']
  207. if not parse:
  208. parse = parse_key_val # use default fn
  209. parse_args = {}
  210. res = read_command(*args, **kwargs)
  211. return parse(res, **parse_args)
  212. def write_command(*args, **kwargs):
  213. """!Passes all arguments to feed_command, with the string specified
  214. by the 'stdin' argument fed to the process' stdin.
  215. @param args list of unnamed arguments (see start_command() for details)
  216. @param kwargs list of named arguments (see start_command() for details)
  217. @return return code
  218. """
  219. stdin = kwargs['stdin']
  220. p = feed_command(*args, **kwargs)
  221. p.stdin.write(stdin)
  222. p.stdin.close()
  223. return p.wait()
  224. def exec_command(prog, flags = "", overwrite = False, quiet = False, verbose = False, env = None, **kwargs):
  225. """!Interface to os.execvpe(), but with the make_command() interface.
  226. @param prog GRASS module
  227. @param flags flags to be used (given as a string)
  228. @param overwrite True to enable overwriting the output (<tt>--o</tt>)
  229. @param quiet True to run quietly (<tt>--q</tt>)
  230. @param verbose True to run verbosely (<tt>--v</tt>)
  231. @param env directory with enviromental variables
  232. @param kwargs module's parameters
  233. """
  234. args = make_command(prog, flags, overwrite, quiet, verbose, **kwargs)
  235. if env == None:
  236. env = os.environ
  237. os.execvpe(prog, args, env)
  238. # interface to g.message
  239. def message(msg, flag = None):
  240. """!Display a message using `g.message`
  241. @param msg message to be displayed
  242. @param flag flags (given as string)
  243. """
  244. run_command("g.message", flags = flag, message = msg)
  245. def debug(msg, debug = 1):
  246. """!Display a debugging message using `g.message -d`
  247. @param msg debugging message to be displayed
  248. @param debug debug level (0-5)
  249. """
  250. run_command("g.message", flags = 'd', message = msg, debug = debug)
  251. def verbose(msg):
  252. """!Display a verbose message using `g.message -v`
  253. @param msg verbose message to be displayed
  254. """
  255. message(msg, flag = 'v')
  256. def info(msg):
  257. """!Display an informational message using `g.message -i`
  258. @param msg informational message to be displayed
  259. """
  260. message(msg, flag = 'i')
  261. def percent(i, n, s):
  262. """!Display a progress info message using `g.message -p`
  263. @code
  264. message(_("Percent complete..."))
  265. n = 100
  266. for i in range(n):
  267. percent(i, n, 1)
  268. percent(1, 1, 1)
  269. @endcode
  270. @param i current item
  271. @param n total number of items
  272. @param s increment size
  273. """
  274. message("%d %d %d" % (i, n, s), flag = 'p')
  275. def warning(msg):
  276. """!Display a warning message using `g.message -w`
  277. @param msg warning message to be displayed
  278. """
  279. message(msg, flag = 'w')
  280. def error(msg):
  281. """!Display an error message using `g.message -e`
  282. Raise exception when on_error is 'raise'.
  283. @param msg error message to be displayed
  284. """
  285. global raise_on_error
  286. if raise_on_error:
  287. raise ScriptError(msg)
  288. else:
  289. message(msg, flag = 'e')
  290. def fatal(msg):
  291. """!Display an error message using `g.message -e`, then abort
  292. @param msg error message to be displayed
  293. """
  294. error(msg)
  295. sys.exit(1)
  296. def set_raise_on_error(raise_exp = True):
  297. """!Define behaviour on error (error() called)
  298. @param raise_exp True to raise ScriptError instead of calling
  299. error()
  300. @return current status
  301. """
  302. global raise_on_error
  303. tmp_raise = raise_on_error
  304. raise_on_error = raise_exp
  305. # interface to g.parser
  306. def _parse_opts(lines):
  307. options = {}
  308. flags = {}
  309. for line in lines:
  310. line = line.rstrip('\r\n')
  311. if not line:
  312. break
  313. try:
  314. [var, val] = line.split('=', 1)
  315. except:
  316. raise SyntaxError("invalid output from g.parser: %s" % line)
  317. if var.startswith('flag_'):
  318. flags[var[5:]] = bool(int(val))
  319. elif var.startswith('opt_'):
  320. options[var[4:]] = val
  321. elif var in ['GRASS_OVERWRITE', 'GRASS_VERBOSE']:
  322. os.environ[var] = val
  323. else:
  324. raise SyntaxError("invalid output from g.parser: %s" % line)
  325. return (options, flags)
  326. def parser():
  327. """!Interface to g.parser, intended to be run from the top-level, e.g.:
  328. @code
  329. if __name__ == "__main__":
  330. options, flags = grass.parser()
  331. main()
  332. @endcode
  333. Thereafter, the global variables "options" and "flags" will be
  334. dictionaries containing option/flag values, keyed by lower-case
  335. option/flag names. The values in "options" are strings, those in
  336. "flags" are Python booleans.
  337. """
  338. if not os.getenv("GISBASE"):
  339. print >> sys.stderr, "You must be in GRASS GIS to run this program."
  340. sys.exit(1)
  341. cmdline = [basename(sys.argv[0])]
  342. cmdline += ['"' + arg + '"' for arg in sys.argv[1:]]
  343. os.environ['CMDLINE'] = ' '.join(cmdline)
  344. argv = sys.argv[:]
  345. name = argv[0]
  346. if not os.path.isabs(name):
  347. if os.sep in name or (os.altsep and os.altsep in name):
  348. argv[0] = os.path.abspath(name)
  349. else:
  350. argv[0] = os.path.join(sys.path[0], name)
  351. p = Popen(['g.parser', '-s'] + argv, stdout = PIPE)
  352. s = p.communicate()[0]
  353. lines = s.splitlines()
  354. if not lines or lines[0].rstrip('\r\n') != "@ARGS_PARSED@":
  355. sys.stdout.write(s)
  356. sys.exit(1)
  357. return _parse_opts(lines[1:])
  358. # interface to g.tempfile
  359. def tempfile():
  360. """!Returns the name of a temporary file, created with g.tempfile."""
  361. return read_command("g.tempfile", pid = os.getpid()).strip()
  362. def tempdir():
  363. """!Returns the name of a temporary dir, created with g.tempfile."""
  364. tmp = read_command("g.tempfile", pid = os.getpid()).strip()
  365. try_remove(tmp)
  366. os.mkdir(tmp)
  367. return tmp
  368. # key-value parsers
  369. def parse_key_val(s, sep = '=', dflt = None, val_type = None, vsep = None):
  370. """!Parse a string into a dictionary, where entries are separated
  371. by newlines and the key and value are separated by `sep' (default: `=')
  372. @param s string to be parsed
  373. @param sep key/value separator
  374. @param dflt default value to be used
  375. @param val_type value type (None for no cast)
  376. @param vsep vertical separator (default os.linesep)
  377. @return parsed input (dictionary of keys/values)
  378. """
  379. result = {}
  380. if not s:
  381. return result
  382. if vsep:
  383. lines = s.split(vsep)
  384. try:
  385. lines.remove('\n')
  386. except ValueError:
  387. pass
  388. else:
  389. lines = s.splitlines()
  390. for line in lines:
  391. kv = line.split(sep, 1)
  392. k = kv[0].strip()
  393. if len(kv) > 1:
  394. v = kv[1]
  395. else:
  396. v = dflt
  397. if val_type:
  398. result[k] = val_type(v)
  399. else:
  400. result[k] = v
  401. return result
  402. # interface to g.gisenv
  403. def gisenv():
  404. """!Returns the output from running g.gisenv (with no arguments), as a
  405. dictionary. Example:
  406. \code
  407. >>> env = grass.gisenv()
  408. >>> print env['GISDBASE']
  409. /opt/grass-data
  410. \endcode
  411. @return list of GRASS variables
  412. """
  413. s = read_command("g.gisenv", flags='n')
  414. return parse_key_val(s)
  415. # interface to g.region
  416. def region():
  417. """!Returns the output from running "g.region -g", as a
  418. dictionary. Example:
  419. \code
  420. >>> region = grass.region()
  421. >>> [region[key] for key in "nsew"]
  422. [228500.0, 215000.0, 645000.0, 630000.0]
  423. >>> (region['nsres'], region['ewres'])
  424. (10.0, 10.0)
  425. \endcode
  426. @return dictionary of region values
  427. """
  428. s = read_command("g.region", flags='g')
  429. reg = parse_key_val(s, val_type = float)
  430. for k in ['rows', 'cols']:
  431. reg[k] = int(reg[k])
  432. return reg
  433. def use_temp_region():
  434. """!Copies the current region to a temporary region with "g.region save=",
  435. then sets WIND_OVERRIDE to refer to that region. Installs an atexit
  436. handler to delete the temporary region upon termination.
  437. """
  438. name = "tmp.%s.%d" % (os.path.basename(sys.argv[0]), os.getpid())
  439. run_command("g.region", save = name)
  440. os.environ['WIND_OVERRIDE'] = name
  441. atexit.register(del_temp_region)
  442. def del_temp_region():
  443. """!Unsets WIND_OVERRIDE and removes any region named by it."""
  444. try:
  445. name = os.environ.pop('WIND_OVERRIDE')
  446. run_command("g.remove", quiet = True, region = name)
  447. except:
  448. pass
  449. # interface to g.findfile
  450. def find_file(name, element = 'cell', mapset = None):
  451. """!Returns the output from running g.findfile as a
  452. dictionary. Example:
  453. \code
  454. >>> result = grass.find_file('fields', element = 'vector')
  455. >>> print result['fullname']
  456. fields@PERMANENT
  457. >>> print result['file']
  458. /opt/grass-data/spearfish60/PERMANENT/vector/fields
  459. \endcode
  460. @param name file name
  461. @param element element type (default 'cell')
  462. @param mapset mapset name (default all mapsets in search path)
  463. @return parsed output of g.findfile
  464. """
  465. s = read_command("g.findfile", flags='n', element = element, file = name, mapset = mapset)
  466. return parse_key_val(s)
  467. # interface to g.list
  468. def list_grouped(type):
  469. """!List elements grouped by mapsets.
  470. Returns the output from running g.list, as a dictionary where the
  471. keys are mapset names and the values are lists of maps in that
  472. mapset. Example:
  473. @code
  474. >>> grass.list_grouped('rast')['PERMANENT']
  475. ['aspect', 'erosion1', 'quads', 'soils', 'strm.dist', ...
  476. @endcode
  477. @param type element type (rast, vect, rast3d, region, ...)
  478. @return directory of mapsets/elements
  479. """
  480. dashes_re = re.compile("^----+$")
  481. mapset_re = re.compile("<(.*)>")
  482. result = {}
  483. mapset = None
  484. for line in read_command("g.list", type = type).splitlines():
  485. if line == "":
  486. continue
  487. if dashes_re.match(line):
  488. continue
  489. m = mapset_re.search(line)
  490. if m:
  491. mapset = m.group(1)
  492. result[mapset] = []
  493. continue
  494. if mapset:
  495. result[mapset].extend(line.split())
  496. return result
  497. def _concat(xs):
  498. result = []
  499. for x in xs:
  500. result.extend(x)
  501. return result
  502. def list_pairs(type):
  503. """!List of elements as tuples.
  504. Returns the output from running g.list, as a list of (map, mapset)
  505. pairs. Example:
  506. @code
  507. >>> grass.list_pairs('rast')
  508. [('aspect', 'PERMANENT'), ('erosion1', 'PERMANENT'), ('quads', 'PERMANENT'), ...
  509. @endcode
  510. @param type element type (rast, vect, rast3d, region, ...)
  511. @return list of tuples (map, mapset)
  512. """
  513. return _concat([[(map, mapset) for map in maps]
  514. for mapset, maps in list_grouped(type).iteritems()])
  515. def list_strings(type):
  516. """!List of elements as strings.
  517. Returns the output from running g.list, as a list of qualified
  518. names. Example:
  519. @code
  520. >>> grass.list_strings('rast')
  521. ['aspect@PERMANENT', 'erosion1@PERMANENT', 'quads@PERMANENT', 'soils@PERMANENT', ...
  522. @endcode
  523. @param type element type
  524. @return list of strings ('map@@mapset')
  525. """
  526. return ["%s@%s" % pair for pair in list_pairs(type)]
  527. # interface to g.mlist
  528. def mlist(type, pattern = None, mapset = None):
  529. """!List of elements
  530. @param type element type (rast, vect, rast3d, region, ...)
  531. @param pattern pattern string
  532. @param mapset mapset name (if not given use search path)
  533. @return list of elements
  534. """
  535. result = list()
  536. for line in read_command("g.mlist",
  537. type = type,
  538. pattern = pattern,
  539. mapset = mapset).splitlines():
  540. result.append(line.strip())
  541. return result
  542. def mlist_grouped(type, pattern = None):
  543. """!List of elements grouped by mapsets.
  544. Returns the output from running g.mlist, as a dictionary where the
  545. keys are mapset names and the values are lists of maps in that
  546. mapset. Example:
  547. @code
  548. >>> grass.mlist_grouped('rast', pattern='r*')['PERMANENT']
  549. ['railroads', 'roads', 'rstrct.areas', 'rushmore']
  550. @endcode
  551. @param type element type (rast, vect, rast3d, region, ...)
  552. @param pattern pattern string
  553. @return directory of mapsets/elements
  554. """
  555. result = dict()
  556. mapset_element = None
  557. for line in read_command("g.mlist", flags = "m",
  558. type = type, pattern = pattern).splitlines():
  559. try:
  560. map, mapset_element = line.split('@')
  561. except ValueError:
  562. warning(_("Invalid element '%s'") % line)
  563. continue
  564. if result.has_key(mapset_element):
  565. result[mapset_element].append(map)
  566. else:
  567. result[mapset_element] = [map, ]
  568. return result
  569. # color parsing
  570. named_colors = {
  571. "white": (1.00, 1.00, 1.00),
  572. "black": (0.00, 0.00, 0.00),
  573. "red": (1.00, 0.00, 0.00),
  574. "green": (0.00, 1.00, 0.00),
  575. "blue": (0.00, 0.00, 1.00),
  576. "yellow": (1.00, 1.00, 0.00),
  577. "magenta": (1.00, 0.00, 1.00),
  578. "cyan": (0.00, 1.00, 1.00),
  579. "aqua": (0.00, 0.75, 0.75),
  580. "grey": (0.75, 0.75, 0.75),
  581. "gray": (0.75, 0.75, 0.75),
  582. "orange": (1.00, 0.50, 0.00),
  583. "brown": (0.75, 0.50, 0.25),
  584. "purple": (0.50, 0.00, 1.00),
  585. "violet": (0.50, 0.00, 1.00),
  586. "indigo": (0.00, 0.50, 1.00)}
  587. def parse_color(val, dflt = None):
  588. """!Parses the string "val" as a GRASS colour, which can be either one of
  589. the named colours or an R:G:B tuple e.g. 255:255:255. Returns an
  590. (r,g,b) triple whose components are floating point values between 0
  591. and 1. Example:
  592. \code
  593. >>> grass.parse_color("red")
  594. (1.0, 0.0, 0.0)
  595. >>> grass.parse_color("255:0:0")
  596. (1.0, 0.0, 0.0)
  597. \endcode
  598. @param val color value
  599. @param dflt default color value
  600. @return tuple RGB
  601. """
  602. if val in named_colors:
  603. return named_colors[val]
  604. vals = val.split(':')
  605. if len(vals) == 3:
  606. return tuple(float(v) / 255 for v in vals)
  607. return dflt
  608. # check GRASS_OVERWRITE
  609. def overwrite():
  610. """!Return True if existing files may be overwritten"""
  611. owstr = 'GRASS_OVERWRITE'
  612. return owstr in os.environ and os.environ[owstr] != '0'
  613. # check GRASS_VERBOSE
  614. def verbosity():
  615. """!Return the verbosity level selected by GRASS_VERBOSE"""
  616. vbstr = os.getenv('GRASS_VERBOSE')
  617. if vbstr:
  618. return int(vbstr)
  619. else:
  620. return 2
  621. ## various utilities, not specific to GRASS
  622. # basename inc. extension stripping
  623. def basename(path, ext = None):
  624. """!Remove leading directory components and an optional extension
  625. from the specified path
  626. @param path path
  627. @param ext extension
  628. """
  629. name = os.path.basename(path)
  630. if not ext:
  631. return name
  632. fs = name.rsplit('.', 1)
  633. if len(fs) > 1 and fs[1].lower() == ext:
  634. name = fs[0]
  635. return name
  636. # find a program (replacement for "which")
  637. def find_program(pgm, args = []):
  638. """!Attempt to run a program, with optional arguments.
  639. @param pgm program name
  640. @param args list of arguments
  641. @return False if the attempt failed due to a missing executable
  642. @return True otherwise
  643. """
  644. nuldev = file(os.devnull, 'w+')
  645. try:
  646. ret = call([pgm] + args, stdin = nuldev, stdout = nuldev, stderr = nuldev)
  647. if ret == 0:
  648. found = True
  649. else:
  650. found = False
  651. except:
  652. found = False
  653. nuldev.close()
  654. return found
  655. # try to remove a file, without complaints
  656. def try_remove(path):
  657. """!Attempt to remove a file; no exception is generated if the
  658. attempt fails.
  659. @param path path to file to remove
  660. """
  661. try:
  662. os.remove(path)
  663. except:
  664. pass
  665. # try to remove a directory, without complaints
  666. def try_rmdir(path):
  667. """!Attempt to remove a directory; no exception is generated if the
  668. attempt fails.
  669. @param path path to directory to remove
  670. """
  671. try:
  672. os.rmdir(path)
  673. except:
  674. shutil.rmtree(path, ignore_errors = True)
  675. def float_or_dms(s):
  676. """!Convert DMS to float.
  677. @param s DMS value
  678. @return float value
  679. """
  680. return sum(float(x) / 60 ** n for (n, x) in enumerate(s.split(':')))
  681. def command_info(cmd):
  682. """!Returns 'help' information for any command as dictionary with entries
  683. for description, keywords, usage, flags, and parameters"""
  684. cmdinfo = {}
  685. s = start_command(cmd, 'help', stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  686. out, err = s.communicate()
  687. sections = err.split('\n\n')
  688. #Description
  689. first, desc = sections[0].split(':\n', 1)
  690. desclines = desc.splitlines()
  691. for line in desclines:
  692. line = line.strip()+' '
  693. # Keywords
  694. first, keywords = sections[1].split(':\n', 1)
  695. keylines = keywords.splitlines()
  696. list = []
  697. list = keywords.strip().split(',')
  698. cmdinfo['keywords'] = list
  699. cmdinfo['description'] = ''.join(desclines).strip()
  700. # Usage
  701. first, usage = sections[2].split(':\n', 1)
  702. usagelines = usage.splitlines()
  703. list = []
  704. for line in usagelines:
  705. line = line.strip()
  706. if line == '': continue
  707. line = line+' '
  708. list.append(line)
  709. cmdinfo['usage'] = ''.join(list).strip()
  710. # Flags
  711. first, flags = sections[3].split(':\n', 1)
  712. flaglines = flags.splitlines()
  713. dict = {}
  714. for line in flaglines:
  715. line = line.strip()
  716. if line == '': continue
  717. item = line.split(' ',1)[0].strip()
  718. val = line.split(' ',1)[1].strip()
  719. dict[item] = val
  720. cmdinfo['flags'] = dict
  721. # Parameters
  722. first, params = err.rsplit(':\n', 1)
  723. paramlines = params.splitlines()
  724. dict = {}
  725. for line in paramlines:
  726. line = line.strip()
  727. if line == '': continue
  728. item = line.split(' ',1)[0].strip()
  729. val = line.split(' ',1)[1].strip()
  730. dict[item] = val
  731. cmdinfo['parameters'] = dict
  732. return cmdinfo
  733. # interface to g.mapsets
  734. def mapsets(accessible = True):
  735. """!List accessible mapsets (mapsets in search path)
  736. @param accessible False to list all mapsets in the location
  737. @return list of mapsets
  738. """
  739. if accessible:
  740. flags = 'p'
  741. else:
  742. flags = 'l'
  743. mapsets = read_command('g.mapsets',
  744. flags = flags,
  745. fs = 'newline',
  746. quiet = True)
  747. if not mapsets:
  748. fatal(_("Unable to list mapsets"))
  749. return mapsets.splitlines()
  750. # interface to `g.proj -c`
  751. def create_location(dbase, location,
  752. epsg = None, proj4 = None, filename = None, wkt = None,
  753. datum = None, desc = None):
  754. """!Create new location
  755. Raise ScriptError on error.
  756. @param dbase path to GRASS database
  757. @param location location name to create
  758. @param epgs if given create new location based on EPSG code
  759. @param proj4 if given create new location based on Proj4 definition
  760. @param filename if given create new location based on georeferenced file
  761. @param wkt if given create new location based on WKT definition (path to PRJ file)
  762. @param datum datum transformation parameters (used for epsg and proj4)
  763. @param desc description of the location (creates MYNAME file)
  764. """
  765. gisdbase = None
  766. if epsg or proj4 or filename or wkt:
  767. gisdbase = gisenv()['GISDBASE']
  768. run_command('g.gisenv',
  769. set = 'GISDBASE=%s' % dbase)
  770. if not os.path.exists(dbase):
  771. os.mkdir(dbase)
  772. kwargs = dict()
  773. if datum:
  774. kwargs['datum'] = datum
  775. if epsg:
  776. ps = pipe_command('g.proj',
  777. quiet = True,
  778. flags = 'c',
  779. epsg = epsg,
  780. location = location,
  781. stderr = PIPE,
  782. **kwargs)
  783. elif proj4:
  784. ps = pipe_command('g.proj',
  785. quiet = True,
  786. flags = 'c',
  787. proj4 = proj4,
  788. location = location,
  789. stderr = PIPE,
  790. **kwargs)
  791. elif filename:
  792. ps = pipe_command('g.proj',
  793. quiet = True,
  794. flags = 'c',
  795. georef = filename,
  796. location = location,
  797. stderr = PIPE)
  798. elif wkt:
  799. ps = pipe_command('g.proj',
  800. quiet = True,
  801. flags = 'c',
  802. wkt = wktfile,
  803. location = location,
  804. stderr = PIPE)
  805. else:
  806. _create_location_xy(dbase, location)
  807. if epsg or proj4 or filename or wkt:
  808. error = ps.communicate()[1]
  809. run_command('g.gisenv',
  810. set = 'GISDBASE=%s' % gisdbase)
  811. if ps.returncode != 0 and error:
  812. raise ScriptError(repr(error))
  813. try:
  814. fd = codecs.open(os.path.join(dbase, location,
  815. 'PERMANENT', 'MYNAME'),
  816. encoding = 'utf-8', mode = 'w')
  817. if desc:
  818. fd.write(desc + os.linesep)
  819. else:
  820. fd.write(os.linesep)
  821. fd.close()
  822. except OSError, e:
  823. raise ScriptError(repr(e))
  824. def _create_location_xy(database, location):
  825. """!Create unprojected location
  826. Raise ScriptError on error.
  827. @param database GRASS database where to create new location
  828. @param location location name
  829. """
  830. cur_dir = os.getcwd()
  831. try:
  832. os.chdir(database)
  833. os.mkdir(location)
  834. os.mkdir(os.path.join(location, 'PERMANENT'))
  835. # create DEFAULT_WIND and WIND files
  836. regioninfo = ['proj: 0',
  837. 'zone: 0',
  838. 'north: 1',
  839. 'south: 0',
  840. 'east: 1',
  841. 'west: 0',
  842. 'cols: 1',
  843. 'rows: 1',
  844. 'e-w resol: 1',
  845. 'n-s resol: 1',
  846. 'top: 1',
  847. 'bottom: 0',
  848. 'cols3: 1',
  849. 'rows3: 1',
  850. 'depths: 1',
  851. 'e-w resol3: 1',
  852. 'n-s resol3: 1',
  853. 't-b resol: 1']
  854. defwind = open(os.path.join(location,
  855. "PERMANENT", "DEFAULT_WIND"), 'w')
  856. for param in regioninfo:
  857. defwind.write(param + '%s' % os.linesep)
  858. defwind.close()
  859. shutil.copy(os.path.join(location, "PERMANENT", "DEFAULT_WIND"),
  860. os.path.join(location, "PERMANENT", "WIND"))
  861. os.chdir(cur_dir)
  862. except OSError, e:
  863. raise ScriptError(repr(e))
  864. # interface to g.version
  865. def version():
  866. """!Get GRASS version as dictionary
  867. @code
  868. version()
  869. {'date': '2011', 'libgis_revision': '45093 ', 'version': '7.0.svn',
  870. 'libgis_date': '2011-01-20 13:10:50 +0100 (Thu, 20 Jan 2011) ', 'revision': '45136M'}
  871. @endcode
  872. """
  873. return parse_command('g.version',
  874. flags = 'rg')
  875. # get debug_level
  876. if find_program('g.gisenv', ['--help']):
  877. debug_level = int(gisenv().get('DEBUG', 0))