utils.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. """
  2. Useful functions to be used in Python scripts.
  3. Usage:
  4. ::
  5. from grass.script import utils as gutils
  6. (C) 2014 by the GRASS Development Team
  7. This program is free software under the GNU General Public
  8. License (>=v2). Read the file COPYING that comes with GRASS
  9. for details.
  10. .. sectionauthor:: Glynn Clements
  11. .. sectionauthor:: Martin Landa <landa.martin gmail.com>
  12. .. sectionauthor:: Anna Petrasova <kratochanna gmail.com>
  13. """
  14. import os
  15. import shutil
  16. import locale
  17. import re
  18. def float_or_dms(s):
  19. """Convert DMS to float.
  20. >>> round(float_or_dms('26:45:30'), 5)
  21. 26.75833
  22. >>> round(float_or_dms('26:0:0.1'), 5)
  23. 26.00003
  24. :param s: DMS value
  25. :return: float value
  26. """
  27. return sum(float(x) / 60 ** n for (n, x) in enumerate(s.split(':')))
  28. def separator(sep):
  29. """Returns separator from G_OPT_F_SEP appropriately converted
  30. to character.
  31. >>> separator('pipe')
  32. '|'
  33. >>> separator('comma')
  34. ','
  35. If the string does not match any of the spearator keywords,
  36. it is returned as is:
  37. >>> separator(', ')
  38. ', '
  39. :param str separator: character or separator keyword
  40. :return: separator character
  41. """
  42. if sep == "pipe":
  43. return "|"
  44. elif sep == "comma":
  45. return ","
  46. elif sep == "space":
  47. return " "
  48. elif sep == "tab" or sep == "\\t":
  49. return "\t"
  50. elif sep == "newline" or sep == "\\n":
  51. return "\n"
  52. return sep
  53. def diff_files(filename_a, filename_b):
  54. """Diffs two text files and returns difference.
  55. :param str filename_a: first file path
  56. :param str filename_b: second file path
  57. :return: list of strings
  58. """
  59. import difflib
  60. differ = difflib.Differ()
  61. fh_a = open(filename_a, 'r')
  62. fh_b = open(filename_b, 'r')
  63. result = list(differ.compare(fh_a.readlines(),
  64. fh_b.readlines()))
  65. return result
  66. def try_remove(path):
  67. """Attempt to remove a file; no exception is generated if the
  68. attempt fails.
  69. :param str path: path to file to remove
  70. """
  71. try:
  72. os.remove(path)
  73. except:
  74. pass
  75. def try_rmdir(path):
  76. """Attempt to remove a directory; no exception is generated if the
  77. attempt fails.
  78. :param str path: path to directory to remove
  79. """
  80. try:
  81. os.rmdir(path)
  82. except:
  83. shutil.rmtree(path, ignore_errors=True)
  84. def basename(path, ext=None):
  85. """Remove leading directory components and an optional extension
  86. from the specified path
  87. :param str path: path
  88. :param str ext: extension
  89. """
  90. name = os.path.basename(path)
  91. if not ext:
  92. return name
  93. fs = name.rsplit('.', 1)
  94. if len(fs) > 1 and fs[1].lower() == ext:
  95. name = fs[0]
  96. return name
  97. class KeyValue(dict):
  98. """A general-purpose key-value store.
  99. KeyValue is a subclass of dict, but also allows entries to be read and
  100. written using attribute syntax. Example:
  101. >>> reg = KeyValue()
  102. >>> reg['north'] = 489
  103. >>> reg.north
  104. 489
  105. >>> reg.south = 205
  106. >>> reg['south']
  107. 205
  108. """
  109. def __getattr__(self, key):
  110. return self[key]
  111. def __setattr__(self, key, value):
  112. self[key] = value
  113. def decode(string):
  114. """Decode string with defualt locale
  115. :param str string: the string to decode
  116. """
  117. enc = locale.getdefaultlocale()[1]
  118. if enc:
  119. if hasattr(string, 'decode'):
  120. return string.decode(enc)
  121. return string
  122. def encode(string):
  123. """Encode string with defualt locale
  124. :param str string: the string to encode
  125. """
  126. enc = locale.getdefaultlocale()[1]
  127. if enc:
  128. if hasattr(string, 'encode'):
  129. return string.encode(enc)
  130. return string
  131. def parse_key_val(s, sep='=', dflt=None, val_type=None, vsep=None):
  132. """Parse a string into a dictionary, where entries are separated
  133. by newlines and the key and value are separated by `sep` (default: `=`)
  134. >>> parse_key_val('min=20\\nmax=50') == {'min': '20', 'max': '50'}
  135. True
  136. >>> parse_key_val('min=20\\nmax=50',
  137. ... val_type=float) == {'min': 20, 'max': 50}
  138. True
  139. :param str s: string to be parsed
  140. :param str sep: key/value separator
  141. :param dflt: default value to be used
  142. :param val_type: value type (None for no cast)
  143. :param vsep: vertical separator (default is Python 'universal newlines' approach)
  144. :return: parsed input (dictionary of keys/values)
  145. """
  146. result = KeyValue()
  147. if not s:
  148. return result
  149. if isinstance(s, bytes):
  150. sep = encode(sep)
  151. vsep = encode(vsep) if vsep else vsep
  152. if vsep:
  153. lines = s.split(vsep)
  154. try:
  155. lines.remove('\n')
  156. except ValueError:
  157. pass
  158. else:
  159. lines = s.splitlines()
  160. for line in lines:
  161. kv = line.split(sep, 1)
  162. k = decode(kv[0].strip())
  163. if len(kv) > 1:
  164. v = decode(kv[1].strip())
  165. else:
  166. v = dflt
  167. if val_type:
  168. result[k] = val_type(v)
  169. else:
  170. result[k] = v
  171. return result
  172. def get_num_suffix(number, max_number):
  173. """Returns formatted number with number of padding zeros
  174. depending on maximum number, used for creating suffix for data series.
  175. Does not include the suffix separator.
  176. :param number: number to be formated as map suffix
  177. :param max_number: maximum number of the series to get number of digits
  178. >>> get_num_suffix(10, 1000)
  179. '0010'
  180. >>> get_num_suffix(10, 10)
  181. '10'
  182. """
  183. return '{number:0{width}d}'.format(width=len(str(max_number)),
  184. number=number)
  185. # source:
  186. # http://stackoverflow.com/questions/4836710/
  187. # does-python-have-a-built-in-function-for-string-natural-sort/4836734#4836734
  188. def natural_sort(l):
  189. """Returns sorted strings using natural sort
  190. """
  191. convert = lambda text: int(text) if text.isdigit() else text.lower()
  192. alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
  193. return sorted(l, key=alphanum_key)