/usr/lib/python3/dist-packages/pyutilib/misc/misc.py is in python3-pyutilib 5.3.5-1.
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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | # _________________________________________________________________________
#
# PyUtilib: A Python utility library.
# Copyright (c) 2008 Sandia Corporation.
# This software is distributed under the BSD License.
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
# _________________________________________________________________________
import fnmatch
import errno
import linecache
import os
import re
import shutil
import stat
import sys
import warnings
if (sys.platform[0:3] == "win"): #pragma:nocover
executable_extension=".exe"
else: #pragma:nocover
executable_extension=""
import six
def deprecated ( deprecated_function ):
""" Code slightly adapted from the Python Decorator Library
This is a decorator which can be used to mark functions as deprecated.
It will result in a warning being emitted when the function is used.
Example use:
@deprecated
def some_func ( ):
...
"""
def wrapper_function ( *args, **kwargs ):
warnings.warn_explicit(
"Use of deprecated function '%s'." % deprecated_function.__name__,
category = DeprecationWarning,
filename = deprecated_function.__code__.co_filename,
lineno = deprecated_function.__code__.co_firstlineno + 1
)
return deprecated_function( *args, **kwargs )
return wrapper_function
def tostr(array):
""" Create a string from an array of numbers """
tmpstr = ""
for val in array:
tmpstr = tmpstr + " " + repr(val)
return tmpstr.strip()
def flatten(x):
"""Flatten nested iterables"""
def _flatten(x, ans_):
for el in x:
if not type(el) is str and hasattr(el, "__iter__"):
# NB: isinstance can be SLOW if it is going to return false,
# so we will do one extra hasattr() check that will pretty
# much assure that it will be True
# NB: we will flatten anything that looks iterable, except strings
try:
el_it = iter(el)
_flatten(el_it, ans_)
except:
ans_.append(el)
else:
ans_.append(el)
# flatten() is really just a recursive routine; however, if we do a
# naive recursive call, we end up creating small temporary lists and
# throwing them away. We could add an optional second argument
# (like _flatten), but then we need to do an "if ans is None: ans =
# []" check. This dual-function approach appears to be the most
# efficient.
if type(x) is str or not hasattr(x, "__iter__"):
return x
ans = []
_flatten(x, ans)
return ans
def flatten_list(x):
"""Flatten nested lists"""
if type(x) is not list:
return x
x_len = len(x)
i = 0
while i < x_len:
if type(x[i]) is list:
x_len += len(x[i]) - 1
x[i:i+1] = x[i]
else:
i += 1
return x
def recursive_flatten_tuple(val):
""" Flatten nested tuples """
if type(val) is not tuple:
return val
rv = ()
for i in val:
if type(i) is tuple:
rv = rv + flatten_tuple(i)
else:
rv = rv + (i,)
return rv
def flatten_tuple(x):
""" Flatten nested tuples """
if type(x) is not tuple:
return x
x_len = len(x)
i = 0
while i < x_len:
if type(x[i]) is tuple:
x_len += len(x[i]) - 1
x = x[:i] + x[i] + x[i+1:]
else:
i += 1
return x
#
# A better method for removing directory trees, which handles MSWindows errors
# that are associated with read-only files.
#
def handleRemoveReadonly(func, path, exc):
excvalue = exc[1]
if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
func(path)
else:
raise
def rmtree(dir):
if not os.path.exists(dir):
return
shutil.rmtree(dir, ignore_errors=False, onerror=handleRemoveReadonly)
whitespace_re = re.compile('\s+')
def quote_split(regex_str, src=None):
"""
Split a string, but do not split the string between quotes. If only
one argument is provided (the string to be split), regex_str
defaults to '\s+'. In addition, regex_str can be either a regular
expression string, or a compiled expression.
"""
if src is None:
src = regex_str
regex = whitespace_re
elif 'match' in dir(regex_str):
regex = regex_str
else:
regex = re.compile(regex_str)
# We need to figure out where the quoted strings are. Given that
# lots of things may be escaped (e.g., '\\\"'), we can only find the
# "real" quotes by walking the entire string. <sigh>.
tokens = []
start = 0
inQuote = ''
escaping = False
for idx, char in enumerate(src):
if escaping:
escaping = False
elif char == inQuote:
inQuote = ''
else:
if not inQuote and start <= idx:
g = regex.match(src[idx:])
if g:
tokens.append(src[start:idx])
start = idx + len(g.group())
# NB: we still want to parse the remainder of the patern
# for things like escape characters so that we correctly
# parse the entire source string; hence, no 'elif'
if char == '\\':
escaping = True
elif not inQuote and ( char == '"' or char == "'" ):
inQuote = char
if inQuote:
raise ValueError("ERROR: unterminated quotation found in quote_split()")
tokens.append(src[start:])
return tokens
def traceit(frame, event, arg): #pragma:nocover
"""
A utility for tracing Python executions. Use this function by
executing:
sys.settrace(traceit)
"""
if event == "line":
lineno = frame.f_lineno
try:
filename = frame.f_globals["__file__"]
except:
return traceit
if (filename.endswith(".pyc") or
filename.endswith(".pyo")):
filename = filename[:-1]
name = frame.f_globals["__name__"]
line = linecache.getline(filename, lineno)
print("%s:%s: %s" % (name, lineno, line.rstrip()))
return traceit
def tuplize(dlist, d, name):
"""
Convert a list into a list of tuples.
"""
if len(dlist) % d != 0:
raise ValueError("Cannot tuplize data for set "+str(name)+" because its length " + str(len(dlist)) + " is not a multiple of dimen " + str(d))
j = 0
t = []
rv = []
for i in dlist:
t.append(i)
j += 1
if j == d:
rv.append(tuple(t))
t = []
j = 0
return rv
def find_files(directory, *args):
for root, dirs, files in os.walk(directory):
for basename in files:
for pattern in args:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
def search_file(filename,
search_path=None,
implicitExt=executable_extension,
executable=False,
isfile=True,
validate=None):
"""
Given a search path, find a file.
Can specify the following options:
search_path - A list of directories that are searched, or the
name of a single directory to search. If unspecified,
then all directories in the PATH defined for the current
environment will be searched.
executable_extension - This string is used to see if there is an
implicit extension in the filename
executable - Test if the file is an executable (default=False)
isfile - Test if the file is a file (default=True)
"""
if search_path is None:
#
# Use the PATH environment if it is defined and not empty
#
if "PATH" in os.environ and os.environ["PATH"] != "":
search_path = os.environ['PATH'].split(os.pathsep)
else:
search_path = os.defpath.split(os.pathsep)
else:
if isinstance(search_path, six.string_types):
search_path = (search_path,)
for path in search_path:
for ext in ('', implicitExt):
test_fname = os.path.join(path, filename+ext)
if os.path.exists(test_fname) \
and (not isfile or os.path.isfile(test_fname)) \
and (not executable or os.access(test_fname, os.X_OK)):
file = os.path.abspath(test_fname)
if validate is None or validate(file):
return file
return None
def sort_index(l):
"""Returns a list, where the i-th value is the index of the i-th smallest
value in the data 'l'"""
return list(index for index, item in sorted(enumerate(l), key=lambda
item: item[1]))
def count_lines(file):
"""Returns the number of lines in a file."""
count = 0
for line in open(file,"r"):
count = count + 1
return count
class Bunch(dict):
"""
A class that can be used to store a bunch of data dynamically
foo = Bunch(data=y, sq=y*y, val=2)
print foo.data
print foo.sq
print foo.val
Adapted from code developed by Alex Martelli and submitted to
the ActiveState Programmer Network http://aspn.activestate.com
"""
def __init__(self, **kw):
dict.__init__(self,kw)
self.__dict__.update(kw)
class Container(dict):
"""
A generalization of Bunch. This class allows all other attributes to have a
default value of None. This borrows the output formatting ideas from the
ActiveState Code Container (recipe 496697).
"""
def __init__(self, *args, **kw):
for arg in args:
for item in quote_split('[ \t]+',arg):
r = item.find('=')
if r != -1:
try:
val = eval(item[r+1:])
except:
val = item[r+1:]
kw[item[:r]] = val
dict.__init__(self,kw)
self.__dict__.update(kw)
if not '_name_' in kw:
self._name_ = self.__class__.__name__
def update(self, d):
"""
The update is specialized for JSON-like data. This
recursively replaces dictionaries with Container objects.
"""
for k in d:
if type(d[k]) is dict:
tmp = Container()
tmp.update(d[k])
self.__setattr__(k, tmp)
elif type(d[k]) is list:
val = []
for i in d[k]:
if type(i) is dict:
tmp = Container()
tmp.update(i)
val.append(tmp)
else:
val.append(i)
self.__setattr__(k, val)
else:
self.__setattr__(k, d[k])
def set_name(self, name):
self._name_ = name
def __setitem__(self, name, val):
self.__setattr__(name,val)
def __getitem__(self, name):
return self.__getattr__(name)
def __setattr__(self, name, val):
if name[0] != '_':
dict.__setitem__(self, name, val)
self.__dict__[name] = val
def __getattr__(self, name):
try:
return dict.__getitem__(self, name)
except:
if name[0] == '_':
raise AttributeError("Unknown attribute %s" % name)
return None
def __repr__(self):
attrs = sorted("%s = %r" % (k, v) for k, v in self.__dict__.items() if not k.startswith("_"))
return "%s(%s)" % (self.__class__.__name__, ", ".join(attrs))
def __str__(self):
return self.as_string()
def __str__(self, nesting = 0, indent=''):
attrs = []
indentation = indent+" " * nesting
for k, v in self.__dict__.items():
if not k.startswith("_"):
text = [indentation, k, ":"]
if isinstance(v, Container):
if len(v) > 0:
text.append('\n')
text.append(v.__str__(nesting + 1))
elif isinstance(v, list):
if len(v) == 0:
text.append(' []')
else:
for v_ in v:
text.append('\n'+indentation+"-")
if isinstance(v_, Container):
text.append('\n'+v_.__str__(nesting+1))
else:
text.append(" "+repr(v_))
else:
text.append(' '+repr(v))
attrs.append("".join(text))
attrs.sort()
return "\n".join(attrs)
class Options(Container):
"""
This is a convenience class. A common use of the Container class is to
manage options that are passed into a class/solver/framework. Thus,
it's convenient to call this an Options object.
"""
def __init__(self, *args, **kw):
Container.__init__(self, *args, **kw)
self.set_name('Options')
def create_hardlink(src, dst):
"""
Create a hard link where dst points to src.
"""
if os.name == 'nt':
# Windows
import ctypes
if not ctypes.windll.kernel32.CreateHardLinkA(dst, src, 0):
raise OSError("Failed to create hard link for file %s"
% (src))
else:
# Unix
os.link(src, dst)
|