/EMORY_globus_dnaComplete_jointGenotyping_optimized/pymodules/python2.7/lib/python/args-0.1.0-py2.7.egg/args.py

https://gitlab.com/pooja043/Globus_Docker_3
Python | 398 lines | 363 code | 9 blank | 26 comment | 14 complexity | c3d7b89c012ba27760a26a9d4be1373d MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. """
  3. args
  4. ~~~~
  5. This module provides the CLI argument interface for clint.
  6. """
  7. import os
  8. from sys import argv
  9. from glob import glob
  10. from collections import OrderedDict
  11. def _expand_path(path):
  12. """Expands directories and globs in given path."""
  13. paths = []
  14. path = os.path.expanduser(path)
  15. path = os.path.expandvars(path)
  16. if os.path.isdir(path):
  17. for (dir, dirs, files) in os.walk(path):
  18. for file in files:
  19. paths.append(os.path.join(dir, file))
  20. else:
  21. paths.extend(glob(path))
  22. return paths
  23. def _is_collection(obj):
  24. """Tests if an object is a collection. Strings don't count."""
  25. if isinstance(obj, basestring):
  26. return False
  27. return hasattr(obj, '__getitem__')
  28. class ArgsList(object):
  29. """CLI Argument management."""
  30. def __init__(self, args=None, no_argv=False):
  31. if not args:
  32. if not no_argv:
  33. self._args = argv[1:]
  34. else:
  35. self._args = []
  36. else:
  37. self._args = args
  38. def __len__(self):
  39. return len(self._args)
  40. def __repr__(self):
  41. return '<args %s>' % (repr(self._args))
  42. def __getitem__(self, i):
  43. try:
  44. return self.all[i]
  45. except IndexError:
  46. return None
  47. def __contains__(self, x):
  48. return self.first(x) is not None
  49. def get(self, x):
  50. """Returns argument at given index, else none."""
  51. try:
  52. return self.all[x]
  53. except IndexError:
  54. return None
  55. def get_with(self, x):
  56. """Returns first argument that contains given string."""
  57. return self.all[self.first_with(x)]
  58. def remove(self, x):
  59. """Removes given arg (or list thereof) from Args object."""
  60. def _remove(x):
  61. found = self.first(x)
  62. if found is not None:
  63. self._args.pop(found)
  64. if _is_collection(x):
  65. for item in x:
  66. _remove(x)
  67. else:
  68. _remove(x)
  69. def pop(self, x):
  70. """Removes and Returns value at given index, else none."""
  71. try:
  72. return self._args.pop(x)
  73. except IndexError:
  74. return None
  75. def any_contain(self, x):
  76. """Tests if given string is contained in any stored argument."""
  77. return bool(self.first_with(x))
  78. def contains(self, x):
  79. """Tests if given object is in arguments list.
  80. Accepts strings and lists of strings."""
  81. return self.__contains__(x)
  82. def first(self, x):
  83. """Returns first found index of given value (or list of values)"""
  84. def _find( x):
  85. try:
  86. return self.all.index(str(x))
  87. except ValueError:
  88. return None
  89. if _is_collection(x):
  90. for item in x:
  91. found = _find(item)
  92. if found is not None:
  93. return found
  94. return None
  95. else:
  96. return _find(x)
  97. def first_with(self, x):
  98. """Returns first found index containing value (or list of values)"""
  99. def _find(x):
  100. try:
  101. for arg in self.all:
  102. if x in arg:
  103. return self.all.index(arg)
  104. except ValueError:
  105. return None
  106. if _is_collection(x):
  107. for item in x:
  108. found = _find(item)
  109. if found:
  110. return found
  111. return None
  112. else:
  113. return _find(x)
  114. def first_without(self, x):
  115. """Returns first found index not containing value (or list of values)"""
  116. def _find(x):
  117. try:
  118. for arg in self.all:
  119. if x not in arg:
  120. return self.all.index(arg)
  121. except ValueError:
  122. return None
  123. if _is_collection(x):
  124. for item in x:
  125. found = _find(item)
  126. if found:
  127. return found
  128. return None
  129. else:
  130. return _find(x)
  131. def start_with(self, x):
  132. """Returns all arguments beginning with given string (or list thereof)"""
  133. _args = []
  134. for arg in self.all:
  135. if _is_collection(x):
  136. for _x in x:
  137. if arg.startswith(x):
  138. _args.append(arg)
  139. break
  140. else:
  141. if arg.startswith(x):
  142. _args.append(arg)
  143. return ArgsList(_args, no_argv=True)
  144. def contains_at(self, x, index):
  145. """Tests if given [list of] string is at given index."""
  146. try:
  147. if _is_collection(x):
  148. for _x in x:
  149. if (_x in self.all[index]) or (_x == self.all[index]):
  150. return True
  151. else:
  152. return False
  153. else:
  154. return (x in self.all[index])
  155. except IndexError:
  156. return False
  157. def has(self, x):
  158. """Returns true if argument exists at given index.
  159. Accepts: integer.
  160. """
  161. try:
  162. self.all[x]
  163. return True
  164. except IndexError:
  165. return False
  166. def value_after(self, x):
  167. """Returns value of argument after given found argument (or list thereof)."""
  168. try:
  169. try:
  170. i = self.all.index(x)
  171. except ValueError:
  172. return None
  173. return self.all[i + 1]
  174. except IndexError:
  175. return None
  176. @property
  177. def grouped(self):
  178. """Extracts --flag groups from argument list.
  179. Returns {format: Args, ...}
  180. """
  181. collection = OrderedDict(_=ArgsList(no_argv=True))
  182. _current_group = None
  183. for arg in self.all:
  184. if arg.startswith('-'):
  185. _current_group = arg
  186. collection.setdefault(arg, ArgsList(no_argv=True))
  187. else:
  188. if _current_group:
  189. collection[_current_group]._args.append(arg)
  190. else:
  191. collection['_']._args.append(arg)
  192. return collection
  193. @property
  194. def last(self):
  195. """Returns last argument."""
  196. try:
  197. return self.all[-1]
  198. except IndexError:
  199. return None
  200. @property
  201. def all(self):
  202. """Returns all arguments."""
  203. return self._args
  204. def all_with(self, x):
  205. """Returns all arguments containing given string (or list thereof)"""
  206. _args = []
  207. for arg in self.all:
  208. if _is_collection(x):
  209. for _x in x:
  210. if _x in arg:
  211. _args.append(arg)
  212. break
  213. else:
  214. if x in arg:
  215. _args.append(arg)
  216. return ArgsList(_args, no_argv=True)
  217. def all_without(self, x):
  218. """Returns all arguments not containing given string (or list thereof)"""
  219. _args = []
  220. for arg in self.all:
  221. if _is_collection(x):
  222. for _x in x:
  223. if _x not in arg:
  224. _args.append(arg)
  225. break
  226. else:
  227. if x not in arg:
  228. _args.append(arg)
  229. return ArgsList(_args, no_argv=True)
  230. @property
  231. def flags(self):
  232. """Returns Arg object including only flagged arguments."""
  233. return self.start_with('-')
  234. @property
  235. def not_flags(self):
  236. """Returns Arg object excluding flagged arguments."""
  237. return self.all_without('-')
  238. @property
  239. def files(self, absolute=False):
  240. """Returns an expanded list of all valid paths that were passed in."""
  241. _paths = []
  242. for arg in self.all:
  243. for path in _expand_path(arg):
  244. if os.path.exists(path):
  245. if absolute:
  246. _paths.append(os.path.abspath(path))
  247. else:
  248. _paths.append(path)
  249. return _paths
  250. @property
  251. def not_files(self):
  252. """Returns a list of all arguments that aren't files/globs."""
  253. _args = []
  254. for arg in self.all:
  255. if not len(_expand_path(arg)):
  256. if not os.path.exists(arg):
  257. _args.append(arg)
  258. return ArgsList(_args, no_argv=True)
  259. @property
  260. def copy(self):
  261. """Returns a copy of Args object for temporary manipulation."""
  262. return ArgsList(self.all)
  263. args = ArgsList()
  264. get = args.get
  265. get_with = args.get_with
  266. remove = args.remove
  267. pop = args.pop
  268. any_contain = args.any_contain
  269. contains = args.contains
  270. first = args.first
  271. first_with = args.first_with
  272. first_without = args.first_without
  273. start_with = args.start_with
  274. contains_at = args.contains_at
  275. has = args.has
  276. value_after = args.value_after
  277. grouped = args.grouped
  278. last = args.last
  279. all = args.all
  280. all_with = args.all_with
  281. all_without = args.all_without
  282. flags = args.flags
  283. not_flags = args.not_flags
  284. files = args.files
  285. not_files = args.not_files
  286. copy = args.copy