コード例 #1
0
    def edit(self, id):
        """Return a morphological parser and the data needed to update it.

        :URL: ``GET /morphologicalparsers/id/edit``
        :param str id: the ``id`` value of the morphological parser that will be updated.
        :returns: a dictionary of the form::

                {"morphological_parser": {...}, "data": {...}}

            where the value of the ``morphological_parser`` key is a dictionary
            representation of the morphological_parser and the value of the ``data`` key
            is the data structure returned by the ``new`` action, i.e., a representation of
            the corpora, phonologies and morphologies in the system.

        """
        morphological_parser = h.eagerload_morphological_parser(
            Session.query(MorphologicalParser)).get(id)
        if morphological_parser:
            return {
                'data': get_data_for_new_edit(dict(request.GET)),
                'morphological_parser': morphological_parser
            }
        else:
            response.status_int = 404
            return {
                'error': 'There is no morphological parser with id %s' % id
            }
コード例 #2
0
ファイル: morphologicalparsers.py プロジェクト: jrwdunham/old
    def update(self, id):
        """Update a morphological parser and return it.

        :URL: ``PUT /morphologicalparsers/id``
        :Request body: JSON object representing the morphological parser with updated attribute values.
        :param str id: the ``id`` value of the morphological parser to be updated.
        :returns: the updated morphological parser model.

        """
        morphological_parser = h.eagerload_morphological_parser(Session.query(MorphologicalParser)).get(int(id))
        if morphological_parser:
            try:
                schema = MorphologicalParserSchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.id = id
                data = schema.to_python(values, state)
                morphological_parser_dict = morphological_parser.get_dict()
                morphological_parser = update_morphological_parser(morphological_parser, data)
                # morphological_parser will be False if there are no changes (cf. update_morphological_parser).
                if morphological_parser:
                    backup_morphological_parser(morphological_parser_dict)
                    Session.add(morphological_parser)
                    Session.commit()
                    return morphological_parser
                else:
                    response.status_int = 400
                    return {'error': u'The update request failed because the submitted data were not new.'}
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
コード例 #3
0
    def show(self, id):
        """Return a morphological parser.

        :URL: ``GET /morphologicalparsers/id``
        :param str id: the ``id`` value of the morphological parser to be returned.
        :GET param str script: if set to '1', the script will be returned with the morphological parser
        :returns: a morphological parser model object.

        """
        morphological_parser = h.eagerload_morphological_parser(
            Session.query(MorphologicalParser)).get(id)
        if morphological_parser:
            morphological_parser_dict = morphological_parser.get_dict()
            if request.GET.get('script') == u'1':
                morphological_parser_dir_path = h.get_model_directory_path(
                    morphological_parser, config)
                morphological_parser_script_path = h.get_model_file_path(
                    morphological_parser,
                    morphological_parser_dir_path,
                    file_type='script')
                if os.path.isfile(morphological_parser_script_path):
                    morphological_parser_dict['script'] = codecs.open(
                        morphological_parser_script_path,
                        mode='r',
                        encoding='utf8').read()
                else:
                    morphological_parser_dict['script'] = u''
            return morphological_parser_dict
        else:
            response.status_int = 404
            return {
                'error': 'There is no morphological parser with id %s' % id
            }
コード例 #4
0
ファイル: morphologicalparsers.py プロジェクト: jrwdunham/old
    def delete(self, id):
        """Delete an existing morphological parser and return it.

        :URL: ``DELETE /morphologicalparsers/id``
        :param str id: the ``id`` value of the morphological parser to be deleted.
        :returns: the deleted morphological parser model.

        """
        parser = h.eagerload_morphological_parser(Session.query(MorphologicalParser)).get(id)
        if parser:
            parser_dict = parser.get_dict()
            backup_morphological_parser(parser_dict)
            Session.delete(parser)
            Session.commit()
            parser.remove_directory()
            return parser
        else:
            response.status_int = 404
            return {'error': 'There is no morphological parser with id %s' % id}
コード例 #5
0
ファイル: morphologicalparsers.py プロジェクト: jrwdunham/old
    def index(self):
        """Get all morphological parser resources.

        :URL: ``GET /morphologicalparsers`` with optional query string parameters for
            ordering and pagination.
        :returns: a list of all morphological parser resources.

        .. note::

           See :func:`utils.add_order_by` and :func:`utils.add_pagination` for the
           query string parameters that effect ordering and pagination.

        """
        try:
            query = h.eagerload_morphological_parser(Session.query(MorphologicalParser))
            query = h.add_order_by(query, dict(request.GET), self.query_builder)
            return h.add_pagination(query, dict(request.GET))
        except Invalid, e:
            response.status_int = 400
            return {'errors': e.unpack_errors()}
コード例 #6
0
ファイル: morphologicalparsers.py プロジェクト: jrwdunham/old
    def edit(self, id):
        """Return a morphological parser and the data needed to update it.

        :URL: ``GET /morphologicalparsers/id/edit``
        :param str id: the ``id`` value of the morphological parser that will be updated.
        :returns: a dictionary of the form::

                {"morphological_parser": {...}, "data": {...}}

            where the value of the ``morphological_parser`` key is a dictionary
            representation of the morphological_parser and the value of the ``data`` key
            is the data structure returned by the ``new`` action, i.e., a representation of
            the corpora, phonologies and morphologies in the system.

        """
        morphological_parser = h.eagerload_morphological_parser(Session.query(MorphologicalParser)).get(id)
        if morphological_parser:
            return {'data': get_data_for_new_edit(dict(request.GET)), 'morphological_parser': morphological_parser}
        else:
            response.status_int = 404
            return {'error': 'There is no morphological parser with id %s' % id}
コード例 #7
0
    def delete(self, id):
        """Delete an existing morphological parser and return it.

        :URL: ``DELETE /morphologicalparsers/id``
        :param str id: the ``id`` value of the morphological parser to be deleted.
        :returns: the deleted morphological parser model.

        """
        parser = h.eagerload_morphological_parser(
            Session.query(MorphologicalParser)).get(id)
        if parser:
            parser_dict = parser.get_dict()
            backup_morphological_parser(parser_dict)
            Session.delete(parser)
            Session.commit()
            parser.remove_directory()
            return parser
        else:
            response.status_int = 404
            return {
                'error': 'There is no morphological parser with id %s' % id
            }
コード例 #8
0
    def update(self, id):
        """Update a morphological parser and return it.

        :URL: ``PUT /morphologicalparsers/id``
        :Request body: JSON object representing the morphological parser with updated attribute values.
        :param str id: the ``id`` value of the morphological parser to be updated.
        :returns: the updated morphological parser model.

        """
        morphological_parser = h.eagerload_morphological_parser(
            Session.query(MorphologicalParser)).get(int(id))
        if morphological_parser:
            try:
                schema = MorphologicalParserSchema()
                values = json.loads(unicode(request.body, request.charset))
                state = h.get_state_object(values)
                state.id = id
                data = schema.to_python(values, state)
                morphological_parser_dict = morphological_parser.get_dict()
                morphological_parser = update_morphological_parser(
                    morphological_parser, data)
                # morphological_parser will be False if there are no changes (cf. update_morphological_parser).
                if morphological_parser:
                    backup_morphological_parser(morphological_parser_dict)
                    Session.add(morphological_parser)
                    Session.commit()
                    return morphological_parser
                else:
                    response.status_int = 400
                    return {
                        'error':
                        u'The update request failed because the submitted data were not new.'
                    }
            except h.JSONDecodeError:
                response.status_int = 400
                return h.JSONDecodeErrorResponse
            except Invalid, e:
                response.status_int = 400
                return {'errors': e.unpack_errors()}
コード例 #9
0
    def index(self):
        """Get all morphological parser resources.

        :URL: ``GET /morphologicalparsers`` with optional query string parameters for
            ordering and pagination.
        :returns: a list of all morphological parser resources.

        .. note::

           See :func:`utils.add_order_by` and :func:`utils.add_pagination` for the
           query string parameters that effect ordering and pagination.

        """
        try:
            query = h.eagerload_morphological_parser(
                Session.query(MorphologicalParser))
            query = h.add_order_by(query, dict(request.GET),
                                   self.query_builder)
            return h.add_pagination(query, dict(request.GET))
        except Invalid, e:
            response.status_int = 400
            return {'errors': e.unpack_errors()}
コード例 #10
0
ファイル: morphologicalparsers.py プロジェクト: jrwdunham/old
    def show(self, id):
        """Return a morphological parser.

        :URL: ``GET /morphologicalparsers/id``
        :param str id: the ``id`` value of the morphological parser to be returned.
        :GET param str script: if set to '1', the script will be returned with the morphological parser
        :returns: a morphological parser model object.

        """
        morphological_parser = h.eagerload_morphological_parser(Session.query(MorphologicalParser)).get(id)
        if morphological_parser:
            morphological_parser_dict = morphological_parser.get_dict()
            if request.GET.get('script') == u'1':
                morphological_parser_dir_path = h.get_model_directory_path(morphological_parser, config)
                morphological_parser_script_path = h.get_model_file_path(morphological_parser, morphological_parser_dir_path, file_type='script')
                if os.path.isfile(morphological_parser_script_path):
                    morphological_parser_dict['script'] = codecs.open(morphological_parser_script_path, mode='r', encoding='utf8').read()
                else:
                    morphological_parser_dict['script'] = u''
            return morphological_parser_dict
        else:
            response.status_int = 404
            return {'error': 'There is no morphological parser with id %s' % id}