def readYAML(filename): try: stream = open(filename, "r") contents = yaml.load(stream) return contents except yaml.YAMLError as exc: msg = "Syntax error in file %s" if hasattr(exc, 'problem_mark'): #pylint: disable=E1101 mark = exc.problem_mark msg += " Error position: (%s:%s)" % (mark.line + 1, mark.column + 1) raise ConfigSyntaxError(msg) except Exception as err: raise FileSystemError("Failed to read configuration file %s : %s" % (filename, err))
def writeYAML(filename, data): try: with open(filename, "w") as fileh: fileh.write(yaml.dump(data, default_flow_style=False)) except Exception as err: raise FileSystemError("Failed to write to %s : %s" % (filename, err))
def writeToFile(data, filename): try: with open(filename, "wb") as fh: fh.write(data) except Exception as err: raise FileSystemError("Failed to write to %s : %s" % (filename, err))
def readYAML(filename): try: stream = open(filename, "r") contents = yaml.load(stream) return contents except yaml.YAMLError, exc: msg = "Syntax error in file %s" if hasattr(exc, 'problem_mark'): mark = exc.problem_mark msg += " Error position: (%s:%s)" % (mark.line + 1, mark.column + 1) raise ConfigSyntaxError(msg) except Exception as err: raise FileSystemError("Failed to read configuration file %s : %s" % (filename, err)) def humanReadableBytes(num): for x in ['bytes', 'KB', 'MB', 'GB']: if num < 1024.0: return "%3.1f%s" % (num, x) num /= 1024.0 return "%3.1f%s" % (num, 'TB') def sha1sum(filename): sha = hashlib.sha1() bufsize = 65536 with open(filename, 'rb') as fd: while True:
def rmFile(path): if os.path.isdir(path): return Fail(FileSystemError("%s is a directory.") % path) return OK(os.remove(path))
def pathExists(path): return OK(path) if os.path.exists(path) else Fail( FileSystemError("Path %s does not exist." % path))