/usr/lib/python3/dist-packages/args.py is in python3-args 0.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | # -*- coding: utf-8 -*-
"""
args
~~~~
This module provides the CLI argument interface for clint.
"""
import os
from sys import argv
from glob import glob
from collections import OrderedDict
def _expand_path(path):
"""Expands directories and globs in given path."""
paths = []
path = os.path.expanduser(path)
path = os.path.expandvars(path)
if os.path.isdir(path):
for (dir, dirs, files) in os.walk(path):
for file in files:
paths.append(os.path.join(dir, file))
else:
paths.extend(glob(path))
return paths
def _is_collection(obj):
"""Tests if an object is a collection. Strings don't count."""
if isinstance(obj, basestring):
return False
return hasattr(obj, '__getitem__')
class ArgsList(object):
"""CLI Argument management."""
def __init__(self, args=None, no_argv=False):
if not args:
if not no_argv:
self._args = argv[1:]
else:
self._args = []
else:
self._args = args
def __len__(self):
return len(self._args)
def __repr__(self):
return '<args %s>' % (repr(self._args))
def __getitem__(self, i):
try:
return self.all[i]
except IndexError:
return None
def __contains__(self, x):
return self.first(x) is not None
def get(self, x):
"""Returns argument at given index, else none."""
try:
return self.all[x]
except IndexError:
return None
def get_with(self, x):
"""Returns first argument that contains given string."""
return self.all[self.first_with(x)]
def remove(self, x):
"""Removes given arg (or list thereof) from Args object."""
def _remove(x):
found = self.first(x)
if found is not None:
self._args.pop(found)
if _is_collection(x):
for item in x:
_remove(x)
else:
_remove(x)
def pop(self, x):
"""Removes and Returns value at given index, else none."""
try:
return self._args.pop(x)
except IndexError:
return None
def any_contain(self, x):
"""Tests if given string is contained in any stored argument."""
return bool(self.first_with(x))
def contains(self, x):
"""Tests if given object is in arguments list.
Accepts strings and lists of strings."""
return self.__contains__(x)
def first(self, x):
"""Returns first found index of given value (or list of values)"""
def _find( x):
try:
return self.all.index(str(x))
except ValueError:
return None
if _is_collection(x):
for item in x:
found = _find(item)
if found is not None:
return found
return None
else:
return _find(x)
def first_with(self, x):
"""Returns first found index containing value (or list of values)"""
def _find(x):
try:
for arg in self.all:
if x in arg:
return self.all.index(arg)
except ValueError:
return None
if _is_collection(x):
for item in x:
found = _find(item)
if found:
return found
return None
else:
return _find(x)
def first_without(self, x):
"""Returns first found index not containing value (or list of values)"""
def _find(x):
try:
for arg in self.all:
if x not in arg:
return self.all.index(arg)
except ValueError:
return None
if _is_collection(x):
for item in x:
found = _find(item)
if found:
return found
return None
else:
return _find(x)
def start_with(self, x):
"""Returns all arguments beginning with given string (or list thereof)"""
_args = []
for arg in self.all:
if _is_collection(x):
for _x in x:
if arg.startswith(x):
_args.append(arg)
break
else:
if arg.startswith(x):
_args.append(arg)
return ArgsList(_args, no_argv=True)
def contains_at(self, x, index):
"""Tests if given [list of] string is at given index."""
try:
if _is_collection(x):
for _x in x:
if (_x in self.all[index]) or (_x == self.all[index]):
return True
else:
return False
else:
return (x in self.all[index])
except IndexError:
return False
def has(self, x):
"""Returns true if argument exists at given index.
Accepts: integer.
"""
try:
self.all[x]
return True
except IndexError:
return False
def value_after(self, x):
"""Returns value of argument after given found argument (or list thereof)."""
try:
try:
i = self.all.index(x)
except ValueError:
return None
return self.all[i + 1]
except IndexError:
return None
@property
def grouped(self):
"""Extracts --flag groups from argument list.
Returns {format: Args, ...}
"""
collection = OrderedDict(_=ArgsList(no_argv=True))
_current_group = None
for arg in self.all:
if arg.startswith('-'):
_current_group = arg
collection.setdefault(arg, ArgsList(no_argv=True))
else:
if _current_group:
collection[_current_group]._args.append(arg)
else:
collection['_']._args.append(arg)
return collection
@property
def last(self):
"""Returns last argument."""
try:
return self.all[-1]
except IndexError:
return None
@property
def all(self):
"""Returns all arguments."""
return self._args
def all_with(self, x):
"""Returns all arguments containing given string (or list thereof)"""
_args = []
for arg in self.all:
if _is_collection(x):
for _x in x:
if _x in arg:
_args.append(arg)
break
else:
if x in arg:
_args.append(arg)
return ArgsList(_args, no_argv=True)
def all_without(self, x):
"""Returns all arguments not containing given string (or list thereof)"""
_args = []
for arg in self.all:
if _is_collection(x):
for _x in x:
if _x not in arg:
_args.append(arg)
break
else:
if x not in arg:
_args.append(arg)
return ArgsList(_args, no_argv=True)
@property
def flags(self):
"""Returns Arg object including only flagged arguments."""
return self.start_with('-')
@property
def not_flags(self):
"""Returns Arg object excluding flagged arguments."""
return self.all_without('-')
@property
def files(self, absolute=False):
"""Returns an expanded list of all valid paths that were passed in."""
_paths = []
for arg in self.all:
for path in _expand_path(arg):
if os.path.exists(path):
if absolute:
_paths.append(os.path.abspath(path))
else:
_paths.append(path)
return _paths
@property
def not_files(self):
"""Returns a list of all arguments that aren't files/globs."""
_args = []
for arg in self.all:
if not len(_expand_path(arg)):
if not os.path.exists(arg):
_args.append(arg)
return ArgsList(_args, no_argv=True)
@property
def copy(self):
"""Returns a copy of Args object for temporary manipulation."""
return ArgsList(self.all)
args = ArgsList()
get = args.get
get_with = args.get_with
remove = args.remove
pop = args.pop
any_contain = args.any_contain
contains = args.contains
first = args.first
first_with = args.first_with
first_without = args.first_without
start_with = args.start_with
contains_at = args.contains_at
has = args.has
value_after = args.value_after
grouped = args.grouped
last = args.last
all = args.all
all_with = args.all_with
all_without = args.all_without
flags = args.flags
not_flags = args.not_flags
files = args.files
not_files = args.not_files
copy = args.copy
|