core.py 30 KB

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