/Python/Lib/site-packages/pypm/external/applib/misc.py

https://gitlab.com/orvi2014/rcs-db-ext · Python · 104 lines · 82 code · 13 blank · 9 comment · 8 complexity · 9ca41d2d732e8ced5db68c373791e0f0 MD5 · raw file

  1. # Copyright (c) 2010 ActiveState Software Inc. All rights reserved.
  2. """Miscelleneous utility functions
  3. """
  4. import sys
  5. from os import path
  6. import six
  7. from applib import _cmdln as cmdln
  8. __all__ = ['xjoin', 'existing']
  9. def xjoin(*c):
  10. """Equivalent to normpath(abspath(join(*c)))"""
  11. return path.normpath(path.abspath(path.join(*c)))
  12. def existing(pth):
  13. """Return path, but assert its presence first"""
  14. assert isinstance(pth, (str, unicode)), \
  15. 'not of string type: %s <%s>' % (pth, type(pth))
  16. assert exists(pth), 'file/directory not found: %s' % pth
  17. return pth
  18. def require_option(options, option_name, details=None):
  19. """
  20. >>> require_option('foo-bar')
  21. ...
  22. CmdlnUserError: required option, --foo-bar, is mising
  23. From http://twitter.com/ActiveState/status/19782350475
  24. 'required options' - conforming to unix standard vs being creative with
  25. non-positional arguments. http://bit.ly/d2iiUL #python #optparse ^SR
  26. """
  27. option_var_name = option_name.replace('-', '_')
  28. if not hasattr(options, option_var_name):
  29. raise ValueError(
  30. "require_option: undefined option '%s'" % option_var_name)
  31. if getattr(options, option_var_name) is None:
  32. msg = 'required option "--{0}" is missing'.format(option_name)
  33. if details:
  34. msg = '%s (%s)' % (msg, details)
  35. raise cmdln.CmdlnUserError(msg)
  36. def safe_unicode(obj):
  37. """Return the unicode/text representation of `obj` without throwing UnicodeDecodeError
  38. Returned value is only a *representation*, not necessarily identical.
  39. """
  40. if type(obj) not in (six.text_type, six.binary_type):
  41. obj = six.text_type(obj)
  42. if type(obj) is six.text_type:
  43. return obj
  44. else:
  45. return obj.decode(errors='ignore')
  46. def _hack_unix2win_path_conversion(cmdln_options, option_names):
  47. """Hack to convert Unix paths in cmdln options (via config file) to
  48. Windows specific netshare location
  49. Config file must define the mapping as config var "unix2win_path_mapping"
  50. """
  51. require_option(cmdln_options, 'unix2win_path_mapping')
  52. for opt in option_names:
  53. setattr(
  54. cmdln_options,
  55. opt,
  56. _cmdln_canonical_path(
  57. cmdln_options.unix2win_path_mapping,
  58. getattr(cmdln_options, opt)))
  59. def _cmdln_canonical_path(unix2win_path_mapping, unixpath):
  60. """Given a unix path return the platform-specific path
  61. On Windows, use the given mapping to translate the path. On Unix platforms,
  62. this function essentially returns `unixpath`.
  63. The mapping is simply a buildout.cfg-friendly multiline string that would
  64. get parsed as dictionary which should have path prefixes as keys, and the
  65. translated Windows net share path as the values. See PyPM's
  66. etc/activestate.conf for an example.
  67. The mapping is typically supposed to be defined in the config file under
  68. the cmdln section. This function is used by PyPM and Grail.
  69. """
  70. unix2win_path_mapping = unix2win_path_mapping or ""
  71. # convert buildout.cfg-style multiline mapping to a dict
  72. m = dict([
  73. [x.strip() for x in line.strip().split(None, 1)]
  74. for line in unix2win_path_mapping.splitlines() if line.strip()])
  75. if sys.platform.startswith('win'):
  76. for prefix, netsharepath in m.items():
  77. if unixpath.startswith(prefix):
  78. return netsharepath + unixpath[len(prefix):]
  79. return unixpath