utils.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. # -*- coding: utf-8 -*-
  2. """
  3. Useful functions to be used in Python scripts.
  4. Usage:
  5. ::
  6. from grass.script import utils as gutils
  7. (C) 2014-2016 by the GRASS Development Team
  8. This program is free software under the GNU General Public
  9. License (>=v2). Read the file COPYING that comes with GRASS
  10. for details.
  11. .. sectionauthor:: Glynn Clements
  12. .. sectionauthor:: Martin Landa <landa.martin gmail.com>
  13. .. sectionauthor:: Anna Petrasova <kratochanna gmail.com>
  14. """
  15. import os
  16. import sys
  17. import shutil
  18. import locale
  19. import shlex
  20. import re
  21. if sys.version_info.major == 3:
  22. unicode = str
  23. def float_or_dms(s):
  24. """Convert DMS to float.
  25. >>> round(float_or_dms('26:45:30'), 5)
  26. 26.75833
  27. >>> round(float_or_dms('26:0:0.1'), 5)
  28. 26.00003
  29. :param s: DMS value
  30. :return: float value
  31. """
  32. if s[-1] in ['E', 'W', 'N', 'S']:
  33. s = s[:-1]
  34. return sum(float(x) / 60 ** n for (n, x) in enumerate(s.split(':')))
  35. def separator(sep):
  36. """Returns separator from G_OPT_F_SEP appropriately converted
  37. to character.
  38. >>> separator('pipe')
  39. '|'
  40. >>> separator('comma')
  41. ','
  42. If the string does not match any of the separator keywords,
  43. it is returned as is:
  44. >>> separator(', ')
  45. ', '
  46. :param str separator: character or separator keyword
  47. :return: separator character
  48. """
  49. if sep == "pipe":
  50. return "|"
  51. elif sep == "comma":
  52. return ","
  53. elif sep == "space":
  54. return " "
  55. elif sep == "tab" or sep == "\\t":
  56. return "\t"
  57. elif sep == "newline" or sep == "\\n":
  58. return "\n"
  59. return sep
  60. def diff_files(filename_a, filename_b):
  61. """Diffs two text files and returns difference.
  62. :param str filename_a: first file path
  63. :param str filename_b: second file path
  64. :return: list of strings
  65. """
  66. import difflib
  67. differ = difflib.Differ()
  68. fh_a = open(filename_a, 'r')
  69. fh_b = open(filename_b, 'r')
  70. result = list(differ.compare(fh_a.readlines(),
  71. fh_b.readlines()))
  72. return result
  73. def try_remove(path):
  74. """Attempt to remove a file; no exception is generated if the
  75. attempt fails.
  76. :param str path: path to file to remove
  77. """
  78. try:
  79. os.remove(path)
  80. except:
  81. pass
  82. def try_rmdir(path):
  83. """Attempt to remove a directory; no exception is generated if the
  84. attempt fails.
  85. :param str path: path to directory to remove
  86. """
  87. try:
  88. os.rmdir(path)
  89. except:
  90. shutil.rmtree(path, ignore_errors=True)
  91. def basename(path, ext=None):
  92. """Remove leading directory components and an optional extension
  93. from the specified path
  94. :param str path: path
  95. :param str ext: extension
  96. """
  97. name = os.path.basename(path)
  98. if not ext:
  99. return name
  100. fs = name.rsplit('.', 1)
  101. if len(fs) > 1 and fs[1].lower() == ext:
  102. name = fs[0]
  103. return name
  104. class KeyValue(dict):
  105. """A general-purpose key-value store.
  106. KeyValue is a subclass of dict, but also allows entries to be read and
  107. written using attribute syntax. Example:
  108. >>> reg = KeyValue()
  109. >>> reg['north'] = 489
  110. >>> reg.north
  111. 489
  112. >>> reg.south = 205
  113. >>> reg['south']
  114. 205
  115. """
  116. def __getattr__(self, key):
  117. return self[key]
  118. def __setattr__(self, key, value):
  119. self[key] = value
  120. def _get_encoding():
  121. encoding = locale.getdefaultlocale()[1]
  122. if not encoding:
  123. encoding = 'UTF-8'
  124. return encoding
  125. def decode(bytes_, encoding=None):
  126. """Decode bytes with default locale and return (unicode) string
  127. No-op if parameter is not bytes (assumed unicode string).
  128. :param bytes bytes_: the bytes to decode
  129. :param encoding: encoding to be used, default value is None
  130. Example
  131. -------
  132. >>> decode(b'S\xc3\xbcdtirol')
  133. u'Südtirol'
  134. >>> decode(u'Südtirol')
  135. u'Südtirol'
  136. >>> decode(1234)
  137. u'1234'
  138. """
  139. if isinstance(bytes_, unicode):
  140. return bytes_
  141. if isinstance(bytes_, bytes):
  142. if encoding is None:
  143. enc = _get_encoding()
  144. else:
  145. enc = encoding
  146. return bytes_.decode(enc)
  147. # if something else than text
  148. if sys.version_info.major >= 3:
  149. # only text should be used
  150. raise TypeError("can only accept types str and bytes")
  151. else:
  152. # for backwards compatibility
  153. return unicode(bytes_)
  154. def encode(string, encoding=None):
  155. """Encode string with default locale and return bytes with that encoding
  156. No-op if parameter is bytes (assumed already encoded).
  157. This ensures garbage in, garbage out.
  158. :param str string: the string to encode
  159. :param encoding: encoding to be used, default value is None
  160. Example
  161. -------
  162. >>> encode(b'S\xc3\xbcdtirol')
  163. b'S\xc3\xbcdtirol'
  164. >>> decode(u'Südtirol')
  165. b'S\xc3\xbcdtirol'
  166. >>> decode(1234)
  167. b'1234'
  168. """
  169. if isinstance(string, bytes):
  170. return string
  171. # this also tests str in Py3:
  172. if isinstance(string, unicode):
  173. if encoding is None:
  174. enc = _get_encoding()
  175. else:
  176. enc = encoding
  177. return string.encode(enc)
  178. # if something else than text
  179. if sys.version_info.major >= 3:
  180. # only text should be used
  181. raise TypeError("can only accept types str and bytes")
  182. else:
  183. # for backwards compatibility
  184. return bytes(string)
  185. def text_to_string(text, encoding=None):
  186. """Convert text to str. Useful when passing text into environments,
  187. in Python 2 it needs to be bytes on Windows, in Python 3 in needs unicode.
  188. """
  189. if sys.version[0] == '2':
  190. # Python 2
  191. return encode(text, encoding=encoding)
  192. else:
  193. # Python 3
  194. return decode(text, encoding=encoding)
  195. def parse_key_val(s, sep='=', dflt=None, val_type=None, vsep=None):
  196. """Parse a string into a dictionary, where entries are separated
  197. by newlines and the key and value are separated by `sep` (default: `=`)
  198. >>> parse_key_val('min=20\\nmax=50') == {'min': '20', 'max': '50'}
  199. True
  200. >>> parse_key_val('min=20\\nmax=50',
  201. ... val_type=float) == {'min': 20, 'max': 50}
  202. True
  203. :param str s: string to be parsed
  204. :param str sep: key/value separator
  205. :param dflt: default value to be used
  206. :param val_type: value type (None for no cast)
  207. :param vsep: vertical separator (default is Python 'universal newlines' approach)
  208. :return: parsed input (dictionary of keys/values)
  209. """
  210. result = KeyValue()
  211. if not s:
  212. return result
  213. if isinstance(s, bytes):
  214. sep = encode(sep)
  215. vsep = encode(vsep) if vsep else vsep
  216. if vsep:
  217. lines = s.split(vsep)
  218. try:
  219. lines.remove('\n')
  220. except ValueError:
  221. pass
  222. else:
  223. lines = s.splitlines()
  224. for line in lines:
  225. kv = line.split(sep, 1)
  226. k = decode(kv[0].strip())
  227. if len(kv) > 1:
  228. v = decode(kv[1].strip())
  229. else:
  230. v = dflt
  231. if val_type:
  232. result[k] = val_type(v)
  233. else:
  234. result[k] = v
  235. return result
  236. def get_num_suffix(number, max_number):
  237. """Returns formatted number with number of padding zeros
  238. depending on maximum number, used for creating suffix for data series.
  239. Does not include the suffix separator.
  240. :param number: number to be formatted as map suffix
  241. :param max_number: maximum number of the series to get number of digits
  242. >>> get_num_suffix(10, 1000)
  243. '0010'
  244. >>> get_num_suffix(10, 10)
  245. '10'
  246. """
  247. return '{number:0{width}d}'.format(width=len(str(max_number)),
  248. number=number)
  249. def split(s):
  250. """!Platform specific shlex.split"""
  251. if sys.version_info >= (2, 6):
  252. return shlex.split(s, posix = (sys.platform != "win32"))
  253. elif sys.platform == "win32":
  254. return shlex.split(s.replace('\\', r'\\'))
  255. else:
  256. return shlex.split(s)
  257. # source:
  258. # http://stackoverflow.com/questions/4836710/
  259. # does-python-have-a-built-in-function-for-string-natural-sort/4836734#4836734
  260. def natural_sort(l):
  261. """Returns sorted strings using natural sort
  262. """
  263. convert = lambda text: int(text) if text.isdigit() else text.lower()
  264. alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
  265. return sorted(l, key=alphanum_key)
  266. def get_lib_path(modname, libname=None):
  267. """Return the path of the libname contained in the module.
  268. """
  269. from os.path import isdir, join, sep
  270. from os import getenv
  271. if isdir(join(getenv('GISBASE'), 'etc', modname)):
  272. path = join(os.getenv('GISBASE'), 'etc', modname)
  273. elif getenv('GRASS_ADDON_BASE') and libname and \
  274. isdir(join(getenv('GRASS_ADDON_BASE'), 'etc', modname, libname)):
  275. path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname)
  276. elif getenv('GRASS_ADDON_BASE') and \
  277. isdir(join(getenv('GRASS_ADDON_BASE'), 'etc', modname)):
  278. path = join(getenv('GRASS_ADDON_BASE'), 'etc', modname)
  279. elif getenv('GRASS_ADDON_BASE') and \
  280. isdir(join(getenv('GRASS_ADDON_BASE'), modname, modname)):
  281. path = join(os.getenv('GRASS_ADDON_BASE'), modname, modname)
  282. else:
  283. # used by g.extension compilation process
  284. cwd = os.getcwd()
  285. idx = cwd.find(modname)
  286. if idx < 0:
  287. return None
  288. path = '{cwd}{sep}etc{sep}{modname}'.format(cwd=cwd[:idx+len(modname)],
  289. sep=sep,
  290. modname=modname)
  291. if libname:
  292. path += '{pathsep}{cwd}{sep}etc{sep}{modname}{sep}{libname}'.format(
  293. cwd=cwd[:idx+len(modname)],
  294. sep=sep,
  295. modname=modname, libname=libname,
  296. pathsep=os.pathsep
  297. )
  298. return path
  299. def set_path(modulename, dirname=None, path='.'):
  300. """Set sys.path looking in the the local directory GRASS directories.
  301. :param modulename: string with the name of the GRASS module
  302. :param dirname: string with the directory name containing the python
  303. libraries, default None
  304. :param path: string with the path to reach the dirname locally.
  305. Example
  306. --------
  307. "set_path" example working locally with the source code of a module
  308. (r.green) calling the function with all the parameters. Below it is
  309. reported the directory structure on the r.green module.
  310. ::
  311. grass_prompt> pwd
  312. ~/Download/r.green/r.green.hydro/r.green.hydro.financial
  313. grass_prompt> tree ../../../r.green
  314. ../../../r.green
  315. |-- ...
  316. |-- libgreen
  317. | |-- pyfile1.py
  318. | +-- pyfile2.py
  319. +-- r.green.hydro
  320. |-- Makefile
  321. |-- libhydro
  322. | |-- pyfile1.py
  323. | +-- pyfile2.py
  324. |-- r.green.hydro.*
  325. +-- r.green.hydro.financial
  326. |-- Makefile
  327. |-- ...
  328. +-- r.green.hydro.financial.py
  329. 21 directories, 125 files
  330. in the source code the function is called with the following parameters: ::
  331. set_path('r.green', 'libhydro', '..')
  332. set_path('r.green', 'libgreen', os.path.join('..', '..'))
  333. when we are executing the module: r.green.hydro.financial locally from
  334. the command line: ::
  335. grass_prompt> python r.green.hydro.financial.py --ui
  336. In this way we are executing the local code even if the module was already
  337. installed as grass-addons and it is available in GRASS standards path.
  338. The function is cheching if the dirname is provided and if the
  339. directory exists and it is available using the path
  340. provided as third parameter, if yes add the path to sys.path to be
  341. importable, otherwise it will check on GRASS GIS standard paths.
  342. """
  343. import sys
  344. # TODO: why dirname is checked first - the logic should be revised
  345. pathlib = None
  346. if dirname:
  347. pathlib = os.path.join(path, dirname)
  348. if pathlib and os.path.exists(pathlib):
  349. # we are running the script from the script directory, therefore
  350. # we add the path to sys.path to reach the directory (dirname)
  351. sys.path.append(os.path.abspath(path))
  352. else:
  353. # running from GRASS GIS session
  354. path = get_lib_path(modulename, dirname)
  355. if path is None:
  356. pathname = os.path.join(modulename, dirname) if dirname else modulename
  357. raise ImportError("Not able to find the path '%s' directory "
  358. "(current dir '%s')." % (pathname, os.getcwd()))
  359. sys.path.insert(0, path)