Add server config storage classes
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
# NOTE: This init function must be called from plpythonu entry points to
|
||||
# initialize cartodb_services module properly. E.g:
|
||||
#
|
||||
# CREATE OR REPLACE FUNCTION cdb_dataservices_server.cdb_isochrone(...)
|
||||
# RETURNS SETOF cdb_dataservices_server.isoline AS $$
|
||||
#
|
||||
# import cartodb_services
|
||||
# cartodb_services.init(plpy, GD)
|
||||
#
|
||||
# # rest of the code here
|
||||
# cartodb_services.GD[key] = val
|
||||
# cartodb_services.plpy.execute('SELECT * FROM ...')
|
||||
#
|
||||
# $$ LANGUAGE plpythonu;
|
||||
|
||||
plpy = None
|
||||
GD = None
|
||||
|
||||
def init(_plpy, _GD):
|
||||
global plpy
|
||||
global GD
|
||||
|
||||
if plpy is None:
|
||||
plpy = _plpy
|
||||
|
||||
if GD is None:
|
||||
GD = _GD
|
||||
|
||||
def _reset():
|
||||
# NOTE: just for testing
|
||||
global plpy
|
||||
global GD
|
||||
|
||||
plpy = None
|
||||
GD = None
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user