-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
models: extract blobs functions to a separate file
They will be used by migrations, and we don't want to import api.models there.
- Loading branch information
Showing
2 changed files
with
41 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2016, 2018 Red Hat, Inc. | ||
# | ||
# Authors: | ||
# Fam Zheng <[email protected]> | ||
# Paolo Bonzini <[email protected]> | ||
# | ||
# This work is licensed under the MIT License. Please see the LICENSE file or | ||
# http://opensource.org/licenses/MIT. | ||
|
||
|
||
import os | ||
import json | ||
import uuid | ||
import logging | ||
|
||
from django.conf import settings | ||
import lzma | ||
|
||
def save_blob(data, name=None): | ||
if not name: | ||
name = str(uuid.uuid4()) | ||
fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz") | ||
lzma.open(fn, 'w').write(data.encode("utf-8")) | ||
return name | ||
|
||
def load_blob(name): | ||
fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz") | ||
return lzma.open(fn, 'r').read().decode("utf-8") | ||
|
||
def load_blob_json(name): | ||
try: | ||
return json.loads(load_blob(name)) | ||
except json.decoder.JSONDecodeError as e: | ||
logging.error('Failed to load blob %s: %s' %(name, e)) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters