6 Classes and functions defined for convenience. 12 ---------------------------------------------------------------------------------------------- 19 @author: garb_ma [DLR-FA,STM Braunschweig] 20 ---------------------------------------------------------------------------------------------- 32 from future
import standard_library
34 standard_library.install_aliases()
39 from builtins
import str
40 from builtins
import object
44 from past.builtins
import long
45 from past.builtins
import unicode
55 import posixpath, ntpath
68 from contextlib
import contextmanager
70 from contextlib2
import contextmanager
72 from types
import MethodType
73 from ..
import PyXMakePath
79 Context manager for temporarily changing the current working directory. 81 @author: Brian M. Hunt 83 def __init__(self, newPath):
84 self.
newPath = os.path.expanduser(newPath)
86 if not os.path.exists(self.
newPath)
and self.
newPath != os.getcwd():
87 print(
"==================================")
88 print(
"Creating a new scratch folder @:", self.
newPath)
89 print(
"This folder will not be deleted once the job is done!")
90 print(
"==================================")
97 def __exit__(self, etype, value, traceback):
104 Class to convert an arbitrary pickle file (2.x & 3.x) into a readable 109 Get a dictionary from a *.cpd file. 111 @param: self, FileName 113 @type: FileName: string 116 self.
Data = GetDataFromPickle.getDictfromFile(FileName)
122 Open a *.cpd file and extract the dictionary stored within. 125 @type: FileName: string 127 FileIn = open(FileName,
"rb")
128 Dict = cp.load(FileIn)
136 Context manager for update an existing ZIP folder 138 @author: Marc Garbade 140 def __init__(self, zipname, zipdata, outpath=os.getcwd(), exclude=[], update=
True):
145 self.
buffer = open(os.path.join(os.getcwd(), self.
ZipName),
'wb+')
150 self.
Output = io.BytesIO();
158 with zipfile.ZipFile(str(self.
ZipName))
as Input:
164 self.
ExcludeFiles.extend([f
for f
in os.listdir(os.getcwd())
if os.path.isfile(os.path.join(os.getcwd(), f))])
166 def __exit__(self, etype, value, traceback):
168 with zipfile.ZipFile(self.
Output,
"w", zipfile.ZIP_DEFLATED)
as Patch:
169 for dirpath, _, filenames
in os.walk(os.getcwd()):
171 filepath = os.path.join(dirpath,f)
173 if not f.endswith((
".zip",
".obj"))
and f
not in self.
ExcludeFiles:
174 Patch.write(filepath, f)
178 f.write(self.
Output.getvalue())
183 Create a temporary dictionary for use with the "with" statement. Its content is deleted after execution. 186 @type: default: string 189 def Changed(newdir, cleanup=lambda:
True):
191 Local helper function to clean up the directory 193 prevdir = os.getcwd()
194 os.chdir(os.path.expanduser(newdir))
202 dirpath = tempfile.mkdtemp(dir=default)
204 shutil.rmtree(dirpath)
205 with Changed(dirpath, cleanup):
211 Redirect console output to a given file. 213 def fileno(file_or_fd):
215 Small helper function to check the validity of the dump object. 217 fd = getattr(file_or_fd,
'fileno',
lambda: file_or_fd)()
218 if not isinstance(fd, int):
219 raise ValueError(
"Expected a file (`.fileno()`) or a file descriptor")
224 Also flush c stdio buffers on python 3 (if possible) 228 from ctypes.util
import find_library
233 libc = ctypes.cdll.msvcrt
235 libc = ctypes.cdll.LoadLibrary(find_library(
'c'))
238 libc.fflush(ctypes.c_void_p.in_dll(libc,
'stdout'))
239 except (AttributeError, ValueError, IOError):
249 stdout_fd = fileno(stdout)
252 with os.fdopen(os.dup(stdout_fd), stdout.mode)
as copied:
255 os.dup2(fileno(to), stdout_fd)
257 with open(to,
'wb')
as to_file:
258 os.dup2(to_file.fileno(), stdout_fd)
265 os.dup2(copied.fileno(), stdout_fd)
270 Redirect all console outputs to a given stream 273 yield (inp, err, out)
278 Redirect outputs to a given file. 280 if sys.version_info >= (3,4):
283 with open(FileName,
'w', encoding=
"utf-8")
as f, contextlib.redirect_stdout(f),
MergedConsoleRedirect(sys.stdout):
292 Get the PyXMake path from *__init__. 299 Get the underlying machine platform in lower cases. 301 return str(platform.system()).lower()
305 Get the underlying machine architecture. Returns either x86 or x64 which corresponds to 306 32 or 64 bit systems. 308 if struct.calcsize(
"P") * 8 == 64:
316 Create a temporary file name with extension *.cpd by default. Optional argument: Seed for random number generation. 318 if isinstance(arg, (int, long)):
321 randTempInteger = rd.randint(1,1000)
323 TempFileName = filename + str(randTempInteger) + kwargs.get(
"ending",extension)
326 randTempInteger = rd.randint(1,1000)
328 TempFileName = filename + str(randTempInteger) + kwargs.get(
"ending",extension)
333 Walk through an iterable input set and store the results in a list. 336 for i
in range(0,len(Iterable)):
337 AsList.append(Iterable[i])
342 Check whether a string is empty and/or not given. Returns True otherwise. 344 return bool(s
and s.strip())
348 Return s as drive to start an absolute path with path.join(...). 350 if sep != ntpath.sep:
352 drive = posixpath.join(posixpath.sep,s)
355 drive = os.path.join(s+
":",os.path.sep)
360 Run command line string "command" in a separate subprocess. 361 Show output in current console window in dependence of verbosity level: 363 - 1 --> Only show errors 364 - 2 --> Show every command line output. 367 @param: command, verbosity 368 @type: string, integer 370 p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=
True)
371 stdout, stderr = p.communicate()
374 print((stdout.decode(
'utf-8',errors=
'ignore').replace(
'\n',
'')))
376 print((stderr.decode(
'utf-8',errors=
'ignore').replace(
'\n',
'')))
382 Run command line string "command" in a separate SSH client process. 383 Show output in current console window in dependence of verbosity level: 385 - 1 --> Only show errors 386 - 2 --> Show every command line output. 389 @param: command, verbosity 390 @type: string, integer 392 _, stdout, stderr = ssh_client.exec_command(command)
393 sexit = stdout.channel.recv_exit_status()
395 sout = stdout.readlines()
396 serr = stderr.readlines()
398 if verbosity >= 2
and sout:
400 if verbosity >= 1
and serr:
407 Concatenate all files into one. 412 with open(filename,
'wb')
as wfd:
413 for f
in [os.path.join(source,cs)
if IsNotEmpty(str(os.path.splitext(cs)[1]))
414 else os.path.join(source,cs+ending)
for cs
in files]:
417 wfd.write(
"\n\n".encode())
418 with open(f,
'rb')
as fd:
419 shutil.copyfileobj(fd, wfd, 1024*1024*10)
424 Replace all occurrences of replace in filename. 426 Inputfile = os.path.join(source,filename+inend)
427 Outputfile = outname+outend
428 with open(Inputfile)
as infile, open(Outputfile,
'w')
as outfile:
430 for src, target
in replace.items():
431 line = line.replace(src, target)
436 Delete all files from workspace 438 @author: Marc Garbade, 26.02.2018 440 @param: identifier: A tuple specifying the files to remove. 443 for f
in os.listdir(os.getcwd()):
444 if f.endswith(identifier):
449 Bind a function to an existing object. 451 return MethodType(_func, _obj)
455 Prepare a object for pickling and convert all numpy 456 arrays to python defaults (2to3 compatible). 458 _dictobj = _obj.__dict__.copy()
459 _dictobj[
'__np_obj_path'] = []
461 if isinstance(value, (np.ndarray, np.generic)):
463 for step
in path[:-1]:
464 parent = parent[step]
465 parent[path[-1]] = value.tolist()
466 _dictobj[
'__np_obj_path'].append(path[-1])
471 Restore the original dictionary by converting python defaults to their 472 numpy equivalents if required (2to3 compatible). 474 _dictobj = _dict.copy()
476 for key, value
in _dictobj.items():
477 if key
in _dictobj[
'__np_obj_path']:
478 _dictobj[key] = np.float64(value)
479 if len(np.shape(value)) == 2:
480 _dictobj[key] = np.asmatrix(value)
489 Return the last item of an arbitrary path (its leaf). 491 head, tail = ntpath.split(path)
492 return tail
or ntpath.basename(head)
496 Restore the original dictionary by converting python defaults to their 497 numpy equivalents if required (2to3 compatible). 500 if isinstance(i, (list,tuple, np.ndarray)):
508 Walk recursively through path. Check if all files listed in source are present. 509 If True, return them. If False, return all files present in the given path. 514 InputFiles = [f
for f
in os.listdir(path)
if os.path.isfile(os.path.join(path, f))]
517 if all(np.array([len(source),len(InputFiles)]) >= 2):
519 if set([os.path.splitext(x)[0]
for x
in source]).issubset(set([os.path.splitext(f)[0]
for f
in InputFiles])):
524 files.extend(InputFiles)
526 files.extend(InputFiles)
531 def PathWalk(path, exclude=False, startswith=None, endswith=None, contains=None):
533 Walk recursively through path. Exclude both folders and files if requested. 535 def Skip(x, starts, contains, ends):
537 Evaluate skip condition 539 if isinstance(starts, (six.string_types, tuple)):
540 if x.startswith(starts):
542 if isinstance(contains, (six.string_types, tuple, list)):
543 tmp = list([]); tmp.append(contains)
545 if any(s
in x
for s
in tmp):
547 if isinstance(ends, (six.string_types, tuple)):
552 for root, dirs, files
in os.walk(os.path.normpath(path)):
554 files = [f
for f
in files
if not Skip(f,startswith,contains,endswith)]
555 dirs[:] = [d
for d
in dirs
if not Skip(d,startswith,contains,endswith)]
556 yield root, dirs, files
560 Walk recursively through nested python objects. 562 @author: Yaniv Aknin, 13.12.2011 564 @param: obj, path, memo 565 @type: object, list, boolean 567 string_types = (str, unicode)
568 iteritems =
lambda mapping: getattr(mapping,
'iteritems', mapping.items)()
572 if isinstance(obj, dict):
574 elif isinstance(obj, (list, set))
and not isinstance(obj, string_types):
577 if id(obj)
not in memo:
579 for path_component, value
in iterator(obj):
580 for result
in ObjectWalk(value, path + (path_component,), memo):
586 if __name__ ==
'__main__':