def get_services(pyfile_settings, **options): pyfile_storage = get_storage(pyfile_settings.BACKEND, pyfile_settings.BACKEND_OPTIONS) return { 'pyfile_storage': pyfile_storage, 'http': requests.Session(), }
def disk_storage(): """Disk storage""" backend = "pyfiles.storages.diskstorage.DiskStorage" storage_path = tempfile.mkdtemp() options = {"basepath": storage_path, "base_url": "file://"} yield get_storage(backend, options) shutil.rmtree(storage_path)
def versions(namespace: "file namespace", filename: "filename"): """ Show available versions for a specific file """ loop = asyncio.get_event_loop() settings.init_settings() storage = get_storage(settings.BACKEND, settings.BACKEND_OPTIONS) result = loop.run_until_complete( storage.versions(namespace=namespace, filename=filename)) print("Avaible version(s) for this file:") [print(v) for v in result]
def serve(host: "Web api host" = "localhost", port: "Listen port" = 8080): from pyfiles.server import get_app loop = asyncio.get_event_loop() settings.init_settings() storage = get_storage(settings.BACKEND, settings.BACKEND_OPTIONS) app = get_app(storage) import webbrowser webbrowser.open(f"http://{host}:{port}") app.run(host=host, port=port)
def search(namespace: "file namespace", filename: "filename", revision="latest"): """ Search for an entry """ loop = asyncio.get_event_loop() settings.init_settings() storage = get_storage(settings.BACKEND, settings.BACKEND_OPTIONS) result = loop.run_until_complete( storage.search(namespace=namespace, filename=filename, version=revision)) print(f"""url: {result['url']} version: {result['version']}""")
def delete( namespace: "Namespace to store", filename: "File name to delete", revision: "File version", ): """ Delete file from storage """ loop = asyncio.get_event_loop() settings.init_settings() storage = get_storage(settings.BACKEND, settings.BACKEND_OPTIONS) print("Delete in progress...") loop.run_until_complete( storage.delete(namespace=namespace, filename=filename, version=revision)) print("Deleted!")
def store( path: "file path to upload", namespace: "Namespace to store", filename: "File name to store", revision: "File version", ): """ Store or update file in storage """ loop = asyncio.get_event_loop() settings.init_settings() storage = get_storage(settings.BACKEND, settings.BACKEND_OPTIONS) with open(path, "rb") as fin: print("Storage in progress...") loop.run_until_complete( storage.store(stream=fin, namespace=namespace, filename=filename, version=revision)) print("Stored!")