Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[obsolete] pyDKB storages #244

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions Utils/Dataflow/pyDKB/storages/Storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
"""
pyDKB.storages.Storage
"""

import sys
from datetime import datetime

from exceptions import (StorageNotConfigured,
QueryError)


class Storage(object):
""" Interface class for external and internal DKB storages. """

# Storage name (identifier)
name = None

# Storage type (storageType member)
type = None

# Storage client
c = None

# Stored queries
stored_queries = {}

def __init__(self, name):
""" Initialize Storage object.

Raise ``StorageException`` in case of error.

:param name: storage identifier
:type name: str
"""
self.name = name

def log(self, level, message):
""" Output log message. """
if level not in ('TRACE', 'DEBUG', 'INFO', 'WARN', 'WARNING', 'ERROR',
'CRITICAL'):
level = 'INFO'
dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
sys.stderr.write('(%s) %s (%s) %s\n' % (level, dt,
self.__class__.__name__,
message))

def configure(self, cfg):
""" Apply storage configuration (initialize client).

:param cfg: configuration parameters
:type cfg: dict
"""
raise NotImplementedError

def client(self):
""" Get storage client.

Raise ``StorageNotConfigured`` if called before configuration.

:return: client object, corresponding given storage type.
:rtype: object
"""
if not self.c:
raise StorageNotConfigured(self.name)
return self.c

def get(self, id, **kwargs):
""" Get object / record from storage by ID.

Raise ``NotFound`` exception if object / record not found.

:param id: object / record identfier
:type id: str, int

:return: record with given ID
:rtype: dict
"""
raise NotImplementedError

def read_query(self, fname, qname=None):
""" Read query from file and save it.

:param fname: file name
:type fname: str
:param qname: query name (for futher usage)
:type qname: str
"""
raise NotImplementedError

def query_is_raw(self, query):
""" Check if given query is not compiled ("raw").

:param query: query body
:type query: obj

:return: True/False
:rtype: bool
"""
raise NotImplementedError

def save_query(self, query, qname=None, raw=False):
""" Save query for further usage.

:param query: query content
:type query: object
:param qname: query name (must not start with '__')
:type qname: str
:param raw: store "raw" (not compiled) version of query
:type raw: bool
"""
if qname and qname.startswith('__'):
raise ValueError("Query name must not start with '__'"
" (reserved for service needs).")
if not raw:
try:
raw = self.query_is_raw(query)
except NotImplementedError:
pass
prefix = ''
if not qname:
qname = '__last'
if raw:
prefix = '__raw'
self.stored_queries[prefix + qname] = query
self.stored_queries[prefix + '__last'] = query

def get_query(self, qname):
""" Get query by name.

Raise ``QueryError`` if query not found.

:param qname: query name (if None, last stored/used query will be used)
:type qnmae: str

:return: stored query
:rtype: object
"""
if not qname:
qname = '__last'
try:
q = self.stored_queries[qname]
self.stored_queries['__last'] = q
except KeyError:
# There still may be raw version of the query
try:
q = self.stored_queries['__raw' + qname]
except KeyError:
raise QueryError("Query used before saving: '%s'"
% qname)
self.stored_queries['__last'] = q
return q

def exec_query(self, qname=None, **kwargs):
""" Execute stored query with given parameters.

:param qname: query name (if None, last used/read
one will be used)
:type qname: str, NoneType
:param kwargs: query parameters (applied with old-style
string formatting operator '%')
:type kwargs: dict

:return: storage response
:rtype: object
"""
raise NotImplementedError

def execute(self, query, **kwargs):
""" Execute query with given parameters.

:param query: query content
:type query: object
:param kwargs: query parameters (applied with old-style
string formatting operator '%')
:type kwargs: dict

:return: storage response
:rtype: object
"""
self.save_query(query)
return self.exec_query(**kwargs)
61 changes: 61 additions & 0 deletions Utils/Dataflow/pyDKB/storages/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
pyDKB.storages
"""

from types import storageType
from Storage import Storage
from es import ES
from exceptions import (StorageAlreadyExists,
StorageNotConfigured)


storageClass = {storageType.ES: ES}
storages = {}


def create(name, stype, cfg=None):
""" Create storage of given type.

Raise ``StorageAlreadyExists`` if storage with given name was created
earlier.

:param name: storage identifier
:type name: str
:param stype: storage type
:type stype: storageType member
:param cfg: storage configuration (if None, won't be applied)
:type cfg: dict, NoneType

:return: Storage object
:rtype: Storage
"""
global storages
if name in storages:
raise StorageAlreadyExists(name)
cls = storageClass.get(stype)
if not cls:
sname = storageType.memberName(stype)
if not sname:
raise ValueError("Unknown storage type: '%s'" % stype)
raise NotImplementedError("Storage class is not implemented for: '%s'"
% storageType.memberName(stype))
storages[name] = cls(name)
if cfg is not None:
storages[name].configure(cfg)
return storages[name]


def get(name):
""" Get storage client by name.

Raise ``StorageNotConfigured`` if the storage was not `create()`d earlier.

:param name: storage name
:type name: str

:return: object representing given storage
:rtype: Storage
"""
if name not in storages:
raise StorageNotConfigured(name)
return storages[name]
Loading