Beispiel #1
0
def get_current_audit():
    try:
        history = get_history()
        current = history.current
    except FileNotFoundError:
        current = datetime.date.today().isoformat()
        log.error(errors.get_log_event())
    return current
def read_json(path, default="", force_renew=False):
    content = cache_read(path, force_renew)
    try:
        log.debug(f"String content starts: {content[0:10]}")
        parsed = Dict(json.loads(content))
        log.debug(f"Parsed content starts: " + json.dumps(parsed)[0:10])
    except json.decoder.JSONDecodeError:
        print("Not decodable - returning default content", sys.stderr)
        parsed = default
    except Exception:
        log.error(errors.get_log_event())
    return parsed
def read_s3(path):
    try:
        log.debug(f"Read from S3: {path}")
        s3 = get_s3_client()
        response = s3.get_object(Bucket=OPTIONS.location, Key=path)
        bytes_content = response["Body"].read()
        content = bytes_content.decode()
        # remove newlines
        content = " ".join(content.splitlines())
        log.debug(f"Content starts: {content[0:10]}")
    except Exception as err:
        # TODO error handling
        log.error(f"Unable to load: {path}")
        log.error(errors.get_log_event())
        content = ""

    return content
def save_s3(path, content):
    try:
        log.debug(f"Save to S3: {path}")
        log.debug(f"Content starts: {content[0:10]}")
        s3 = get_s3_client()
        bytes_content = str.encode(content)
        bucket_name = OPTIONS.location
        response = s3.put_object(Bucket=bucket_name,
                                 Key=path,
                                 Body=bytes_content)
        tag = response["ETag"]
        log.debug(f"Saved object tag: {tag} to bucket")
        status = True
    except Exception as err:
        # TODO error handling
        log.error(errors.get_log_event())
        status = False
    return status