Add server config storage classes

This commit is contained in:
Rafa de la Torre
2016-09-28 17:32:26 +02:00
parent 18f05fbd4f
commit fd2cc21942
6 changed files with 114 additions and 0 deletions
@@ -0,0 +1,11 @@
import abc
class ConfigStorageInterface(object):
"""This is an interface that all config storages must abide to"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def get(self, key):
"""Return a value based on the key supplied from some storage"""
pass
@@ -0,0 +1,26 @@
import json
import cartodb_services
from interfaces import ConfigStorageInterface
class InDbServerConfigStorage(ConfigStorageInterface):
def get(self, key):
sql = "SELECT cdb_dataservices_server.cdb_conf_getconf('{0}') as conf".format(key)
rows = cartodb_services.plpy.execute(sql, 1)
json_output = rows[0]['conf']
if json_output:
return json.loads(json_output)
else:
return None
class InMemoryConfigStorage(ConfigStorageInterface):
def __init__(self, config_hash={}):
self._config_hash = config_hash
def get(self, key):
try:
return self._config_hash[key]
except KeyError:
return None