from iinfer.app import common, web, feature
import bottle
import json


class DelPipe(feature.WebFeature):
    def __init__(self):
        super().__init__()
    
    def route(self, web:web.Web, app:bottle.Bottle) -> None:
        @app.route('/gui/del_pipe', method='POST')
        def del_pipe():
            if not web.check_signin():
                return common.to_str(dict(warn=f'Please log in to retrieve session.'))
            title = bottle.request.forms.get('title')
            self.del_pipe(web, title)
            bottle.response.content_type = 'application/json'
            return json.dumps({}, default=common.default_json_enc)

    def del_pipe(self, web:web.Web, title:str):
        """
        パイプラインを削除する

        Args:
            web (web.Web): Webオブジェク
            title (str): タイトル
        """
        opt_path = web.pipes_path / f"pipe-{title}.json"
        web.logger.info(f"del_pipe: opt_path={opt_path}")
        opt_path.unlink()
