def load_scripts_in_memory(self): db = pyscripts_db_api.get_instance() scripts_uuid_list = db.list_scripts() # Purge old entries scripts_to_purge = [] for script_uuid in self._scripts.keys(): if script_uuid not in scripts_uuid_list: scripts_to_purge.append(script_uuid) for script_uuid in scripts_to_purge: del self._scripts[script_uuid] # Load or update script for script_uuid in scripts_uuid_list: script_db = db.get_script(uuid=script_uuid) name = script_db.name checksum = script_db.checksum if name not in self._scripts: self._scripts[script_uuid] = {} script = self._scripts[script_uuid] # NOTE(sheeprine): We're doing this the easy way, we might want to # store the context and call functions in future if script.get(checksum, '') != checksum: code = compile( script_db.data, '<PyScripts: {name}>'.format(name=name), 'exec') script.update({ 'name': name, 'code': code, 'checksum': checksum})
def load_scripts_in_memory(self): db = pyscripts_db_api.get_instance() scripts_uuid_list = db.list_scripts() # Purge old entries scripts_to_purge = [] for script_uuid in self._scripts.keys(): if script_uuid not in scripts_uuid_list: scripts_to_purge.append(script_uuid) for script_uuid in scripts_to_purge: del self._scripts[script_uuid] # Load or update script for script_uuid in scripts_uuid_list: script_db = db.get_script(uuid=script_uuid) name = script_db.name checksum = script_db.checksum if name not in self._scripts: self._scripts[script_uuid] = {} script = self._scripts[script_uuid] # NOTE(sheeprine): We're doing this the easy way, we might want to # store the context and call functions in future if script.get(checksum, '') != checksum: code = compile(script_db.data, '<PyScripts: {name}>'.format(name=name), 'exec') script.update({ 'name': name, 'code': code, 'checksum': checksum })
def delete(self, script_id): """Delete the script. :param script_id: UUID of the script to delete. """ pyscripts = db_api.get_instance() try: pyscripts.delete_script(uuid=script_id) except db_api.NoSuchScript as e: pecan.abort(400, six.text_type(e))
def delete(self, script_id): """Delete the script. :param script_id: UUID of the script to delete. """ pyscripts = db_api.get_instance() try: pyscripts.delete_script(uuid=script_id) except db_api.NoSuchScript as e: pecan.abort(404, six.text_type(e))
def get_one(self, script_id): """Return a script. :param script_id: UUID of the script to filter on. """ pyscripts = db_api.get_instance() try: script_db = pyscripts.get_script(uuid=script_id) return script_models.Script(**script_db.export_model()) except db_api.NoSuchScript as e: pecan.abort(400, six.text_type(e))
def get_one(self, script_id): """Return a script. :param script_id: UUID of the script to filter on. """ pyscripts = db_api.get_instance() try: script_db = pyscripts.get_script(uuid=script_id) return script_models.Script(**script_db.export_model()) except db_api.NoSuchScript as e: pecan.abort(404, six.text_type(e))
def post(self, script_data): """Create pyscripts script. :param script_data: Informations about the script to create. """ pyscripts = db_api.get_instance() try: data = self.normalize_data(script_data.data) script_db = pyscripts.create_script(script_data.name, data) pecan.response.location = pecan.request.path_url if pecan.response.location[-1] != '/': pecan.response.location += '/' pecan.response.location += script_db.script_id return script_models.Script(**script_db.export_model()) except db_api.ScriptAlreadyExists as e: pecan.abort(409, six.text_type(e))
def get_all(self, no_data=False): """Get the script list :param no_data: Set to True to remove script data from output. :return: List of every scripts. """ pyscripts = db_api.get_instance() script_list = [] script_uuid_list = pyscripts.list_scripts() for script_uuid in script_uuid_list: script_db = pyscripts.get_script(uuid=script_uuid) script = script_db.export_model() if no_data: del script['data'] script_list.append(script_models.Script(**script)) res = script_models.ScriptCollection(scripts=script_list) return res
def post(self, script_data): """Create pyscripts script. :param script_data: Informations about the script to create. """ pyscripts = db_api.get_instance() try: data = self.normalize_data(script_data.data) script_db = pyscripts.create_script(script_data.name, data) pecan.response.location = pecan.request.path_url if pecan.response.location[-1] != '/': pecan.response.location += '/' pecan.response.location += script_db.script_id return script_models.Script( **script_db.export_model()) except db_api.ScriptAlreadyExists as e: pecan.abort(409, six.text_type(e))
def get_all(self, no_data=False): """Get the script list :param no_data: Set to True to remove script data from output. :return: List of every scripts. """ pyscripts = db_api.get_instance() script_list = [] script_uuid_list = pyscripts.list_scripts() for script_uuid in script_uuid_list: script_db = pyscripts.get_script(uuid=script_uuid) script = script_db.export_model() if no_data: del script['data'] script_list.append(script_models.Script( **script)) res = script_models.ScriptCollection(scripts=script_list) return res
def put(self, script_id, script_data): """Update pyscripts script. :param script_id: UUID of the script to update. :param script_data: Script data to update. """ pyscripts = db_api.get_instance() try: data = self.normalize_data(script_data.data) script_db = pyscripts.update_script(script_id, name=script_data.name, data=data) pecan.response.location = pecan.request.path_url if pecan.response.location[-1] != '/': pecan.response.location += '/' pecan.response.location += script_db.script_id return script_models.Script(**script_db.export_model()) except db_api.NoSuchScript as e: pecan.abort(400, six.text_type(e))
def put(self, script_id, script_data): """Update pyscripts script. :param script_id: UUID of the script to update. :param script_data: Script data to update. """ pyscripts = db_api.get_instance() try: data = self.normalize_data(script_data.data) script_db = pyscripts.update_script(script_id, name=script_data.name, data=data) pecan.response.location = pecan.request.path_url if pecan.response.location[-1] != '/': pecan.response.location += '/' pecan.response.location += script_db.script_id return script_models.Script( **script_db.export_model()) except db_api.NoSuchScript as e: pecan.abort(404, six.text_type(e))
def start_fixture(self): super(PyScriptsConfigFixture, self).start_fixture() self.conn = pyscripts_db.get_instance() migration = self.conn.get_migration() migration.upgrade('head')
class PyScripts(rating.RatingProcessorBase): """PyScripts rating module. PyScripts is a module made to execute custom made python scripts to create rating policies. """ module_name = 'pyscripts' description = 'PyScripts rating module.' hot_config = True config_controller = root_api.PyScriptsConfigController db_api = pyscripts_db_api.get_instance() def __init__(self, tenant_id=None): self._scripts = {} super(PyScripts, self).__init__(tenant_id) def load_scripts_in_memory(self): db = pyscripts_db_api.get_instance() scripts_uuid_list = db.list_scripts() # Purge old entries scripts_to_purge = [] for script_uuid in self._scripts.keys(): if script_uuid not in scripts_uuid_list: scripts_to_purge.append(script_uuid) for script_uuid in scripts_to_purge: del self._scripts[script_uuid] # Load or update script for script_uuid in scripts_uuid_list: script_db = db.get_script(uuid=script_uuid) name = script_db.name checksum = script_db.checksum if name not in self._scripts: self._scripts[script_uuid] = {} script = self._scripts[script_uuid] # NOTE(sheeprine): We're doing this the easy way, we might want to # store the context and call functions in future if script.get(checksum, '') != checksum: code = compile(script_db.data, '<PyScripts: {name}>'.format(name=name), 'exec') script.update({ 'name': name, 'code': code, 'checksum': checksum }) def reload_config(self): """Reload the module's configuration. """ self.load_scripts_in_memory() def start_script(self, code, data): context = {'data': data} exec(code, context) return data def process(self, data): for script in self._scripts.values(): self.start_script(script['code'], data) return data