<commit_before>import sys, argparse, json

from bottle import route, run, Response, request, abort

from openehr.utils import get_logger
from openehr.utils.services import get_service_configuration
from openehr.ehr.services.dbmanager.dbservices import DBServices
from openehr.ehr.services.dbmanager.dbservices.wrappers import PatientRecord, ClinicalRecord
import openehr.ehr.services.dbmanager.errors as pyehr_errors


class DBService(object):

    def __init__(self, driver, host, database, patients_repository=None,
                 ehr_repository=None, port=None, user=None, passwd=None):
        self.logger = get_logger('db_service_daemon')
        self.dbs = DBServices(driver, host, database, patients_repository,
                              ehr_repository, port, user, passwd, self.logger)
        #######################################################
        # Web Service methods
        #######################################################
        route('/add_patient')(self.save_patient)

    def _error(self, msg, error_code):
        self.logger.error(msg)
        body = {
            'SUCCESS': False,
            'MESSAGE': msg
        }
        abort(error_code, json.dumps(body))

    def _success(self, body, return_code=200):
        return Response(json.dumps(body), return_code)

    def _get_bool(self, str_val):
        if str_val.upper() == 'TRUE':
            return True
        elif str_val.upper() == 'FALSE':
            return False
        else:
            raise ValueError('Can\'t convert to boolean value %s' % str_val)

    def save_patient(self):
        """
        Create a new PatientRecord from the given values. Returns the saved record in
        its JSON encoding
        """
        params = request.query
        try:
            record_id = params.get('patient_id')
            if not record_id:
                msg = 'Missing record ID, cannot create a patient record'
                return self._error(msg, 500)
            prec_conf = {
                'record_id': record_id,
            }
            if params.get('creation_time'):
                prec_conf['creation_time'] = float(params.get('creation_time'))
            if params.get('active'):
                prec_conf['active'] = self._get_bool(params.get('active'))
            prec = PatientRecord(**prec_conf)
            prec = self.dbs.save_patient(prec)
        except pyehr_errors.DBManagerNotConnectedError:
            msg = 'Unable to connect to backend engine'
            return self._error(msg, 500)
        except pyehr_errors.DuplicatedKeyError:
            msg = 'Duplicated key error for PatientRecord with ID %s' % prec.record_id
            return self._error(msg, 500)
        except pyehr_errors.InvalidRecordTypeError:
            msg = 'Invalid PatientRecord, unable to save'
            return self._error(msg, 500)
        except pyehr_errors.UnknownDriverError, ude:
            return self._error(str(ude), 500)
        except ValueError, ve:
            return self._error(str(ve), 500)
        except Exception, e:
            msg = 'Unexpected error: %s' % e.message
            return self._error(msg, 500)
        response_body = {
            'SUCCESS': True,<commit_msg>hide record callback and moved common exceptions handling to a decorator<commit_after>import sys, argparse, json
from functools import wraps

from bottle import route, run, Response, request, abort, HTTPError

from openehr.utils import get_logger
from openehr.utils.services import get_service_configuration
from openehr.ehr.services.dbmanager.dbservices import DBServices
from openehr.ehr.services.dbmanager.dbservices.wrappers import PatientRecord, ClinicalRecord
import openehr.ehr.services.dbmanager.errors as pyehr_errors


class DBService(object):

    def __init__(self, driver, host, database, patients_repository=None,
                 ehr_repository=None, port=None, user=None, passwd=<PASSWORD>):
        self.logger = get_logger('db_service_daemon')
        self.dbs = DBServices(driver, host, database, patients_repository,
                              ehr_repository, port, user, passwd, self.logger)
        #######################################################
        # Web Service methods
        #######################################################
        route('/patient/add')(self.save_patient)
        route('/patient/hide')(self.hide_patient)

    def exceptions_handler(f):
        @wraps(f)
        def wrapper(inst, *args, **kwargs):
            try:
                return f(inst, *args, **kwargs)
            except pyehr_errors.DBManagerNotConnectedError:
                msg = 'Unable to connect to backend engine'
                inst._error(msg, 500)
            except pyehr_errors.UnknownDriverError, ude:
                inst._error(str(ude), 500)
            except HTTPError:
                #if an abort was called in wrapped function, raise the generated HTTPError
                raise
            except Exception, e:
                msg = 'Unexpected error: %s' % e.message
                inst._error(msg, 500)
        return wrapper

    def _error(self, msg, error_code):
        self.logger.error(msg)
        body = {
            'SUCCESS': False,
            'ERROR': msg
        }
        abort(error_code, json.dumps(body))

    def _success(self, body, return_code=200):
        return Response(json.dumps(body), return_code)

    def _get_bool(self, str_val):
        if str_val.upper() == 'TRUE':
            return True
        elif str_val.upper() == 'FALSE':
            return False
        else:
            raise ValueError('Can\'t convert to boolean value %s' % str_val)

    @exceptions_handler
    def save_patient(self):
        """
        Create a new PatientRecord from the given values. Returns the saved record in
        its JSON encoding
        """
        params = request.query
        try:
            record_id = params.get('patient_id')
            if not record_id:
                msg = 'Missing record ID, cannot create a patient record'
                self._error(msg, 500)
            prec_conf = {
                'record_id': record_id,
            }
            if params.get('creation_time'):
                prec_conf['creation_time'] = float(params.get('creation_time'))
            if params.get('active'):
                prec_conf['active'] = self._get_bool(params.get('active'))
            prec = PatientRecord(**prec_conf)
            prec = self.dbs.save_patient(prec)
            response_body = {
                'SUCCESS': True,
                'RECORD': prec.to_json()
            }
            return self._success(response_body)
        except pyehr_errors.DuplicatedKeyError:
            msg = 'Duplicated key error for PatientRecord with ID %s' % prec.record_id
            self._error(msg, 500)
        except pyehr_errors.InvalidRecordTypeError:
            msg = 'Invalid PatientRecord, unable to save'
            self._error(msg, 500)
        except ValueError, ve:
            self._error(str(ve), 500)

    @exceptions_handler
    def hide_patient(self):
        """
        Hide a patient record and related EHR records
        """
        params = request.query
        patient_id = params.get('patient_id')
        if not patient_id:
            msg = 'Missing patient ID, cannot hide record'
            self._error(msg, 500)
        prec = self.dbs.get_patient(patient_id)
        if not prec:
            # TODO: check if an error is a better solution here
            body = {
                'SUCCESS': False,
                'ERROR': 'There is no patient record with ID %s' % patient_id
            }
            return self._success(body)
        prec = self.dbs.hide_patient(prec)
        response_body = {
            'SUCCESS': True,
{'fraction_non_alphanumeric': 0.1166195735936296, 'fraction_numerical': 0.006550218340611353, 'mean_word_length': 2.1744802282918876, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6267652', 'n_tokens_mistral': 2153, 'n_tokens_neox': 2062, 'n_words': 545}