from __future__ import absolute_import
from datetime import datetime
import config
from models import Nick, Phrase, History, Response
from db import session
import logging

logger = logging.getLogger('snarkbot-memory')

class PhraseNotFound(Exception):
    pass

class ResponseNotFound(Exception):
    pass

# just a wrapper around some common operations in sql
class Memory(object):
    def __init__(self,history=None):
        if history is None:
            history = config.max_history_lines

        if history < 1:
            logger.warning('Max history is not defined. Defaulting to 1000 lines')
            history = 1000
        
        self.history_max_lines = int(history)
        self.history = list( session.query(History).order_by(History.created.desc()).limit(self.history_max_lines).all() )

    def get_phrase(self,phrase_id):
        return session.query(Phrase).get(phrase_id)

    def get_phrase_by_text(self,text):
        return session.query(Phrase).filter_by(phrase=text)

    def get_response(self,response_id):
        return session.query(Response).get(response_id)

    def get_response_by_text(self,text):
        return session.query(Response).filter(
            Response.response.like('%{}%'.format(text))).one_or_none()
        

    def remember(self,keyvalue,response_text,added_by):
        phrase = self.get_phrase_by_text(keyvalue)
        if phrase:
            response = self.get_response_by_text(response_text)
        else:
            response = Response(response=response_text, added_by_id=added_by.id)
            session.add(response)

        if response not in phrase.responses:
            phrase.responses.append(response)

        session.commit()

        return response

    def add_history(self, nick, history_line ):
        if len(self.history) > self.history_max_lines:
            try:
                session.delete(self.history[0])
                del self.history[0]
            except IndexError:
                pass

        hist = History(
            nick=nick,
            line=history_line, 
            created=datetime.now()
        )
        db.session.add(hist)
        self.history.append(hist)
        
    def commit_history(self):
        # this just commits the database session
        session.commit()

    def recall_history(self,lines=1):
        # get lines number of lines of previous history
        start = len(self.history) - lines
        if start < 0:
            start = 0

        hslice = self.history[start:len(self.history)]
        return hslice

    def find_in_history(self,nick=None,lines=1,phrase=None):
        # find the last lines said by [nick]
        # optionally add a phrase

        q = session.query(History)
        if nick is not None:
            q = q.filter_by(nick=nick)

        if phrase is not None:
            q = q.filter(History.line.like('%{}%'.format(phrase)))

        lines = q.order_by(History.created.desc()).limit(lines).all()

        return lines

cfbc87ffec9fafc7d4abf6f60d8177b2285927b5
<mask token>


class Memory(object):

    def __init__(self, history=None):
        if history is None:
            history = config.max_history_lines
        if history < 1:
            logger.warning(
                'Max history is not defined. Defaulting to 1000 lines')
            history = 1000
        self.history_max_lines = int(history)
        self.history = list(session.query(History).order_by(History.created
            .desc()).limit(self.history_max_lines).all())

    def get_phrase(self, phrase_id):
        return session.query(Phrase).get(phrase_id)

    def get_phrase_by_text(self, text):
        return session.query(Phrase).filter_by(phrase=text)
    <mask token>

    def get_response_by_text(self, text):
        return session.query(Response).filter(Response.response.like('%{}%'
            .format(text))).one_or_none()

    def remember(self, keyvalue, response_text, added_by):
        phrase = self.get_phrase_by_text(keyvalue)
        if phrase:
            response = self.get_response_by_text(response_text)
        else:
            response = Response(response=response_text, added_by_id=added_by.id
                )
            session.add(response)
        if response not in phrase.responses:
            phrase.responses.append(response)
        session.commit()
        return response
    <mask token>

    def commit_history(self):
        session.commit()
    <mask token>

    def find_in_history(self, nick=None, lines=1, phrase=None):
        q = session.query(History)
        if nick is not None:
            q = q.filter_by(nick=nick)
        if phrase is not None:
            q = q.filter(History.line.like('%{}%'.format(phrase)))
        lines = q.order_by(History.created.desc()).limit(lines).all()
        return lines

<mask token>


class Memory(object):

    def __init__(self, history=None):
        if history is None:
            history = config.max_history_lines
        if history < 1:
            logger.warning(
                'Max history is not defined. Defaulting to 1000 lines')
            history = 1000
        self.history_max_lines = int(history)
        self.history = list(session.query(History).order_by(History.created
            .desc()).limit(self.history_max_lines).all())

    def get_phrase(self, phrase_id):
        return session.query(Phrase).get(phrase_id)

    def get_phrase_by_text(self, text):
        return session.query(Phrase).filter_by(phrase=text)

    def get_response(self, response_id):
        return session.query(Response).get(response_id)

    def get_response_by_text(self, text):
        return session.query(Response).filter(Response.response.like('%{}%'
            .format(text))).one_or_none()

    def remember(self, keyvalue, response_text, added_by):
        phrase = self.get_phrase_by_text(keyvalue)
        if phrase:
            response = self.get_response_by_text(response_text)
        else:
            response = Response(response=response_text, added_by_id=added_by.id
                )
            session.add(response)
        if response not in phrase.responses:
            phrase.responses.append(response)
        session.commit()
        return response
    <mask token>

    def commit_history(self):
        session.commit()
    <mask token>

    def find_in_history(self, nick=None, lines=1, phrase=None):
        q = session.query(History)
        if nick is not None:
            q = q.filter_by(nick=nick)
        if phrase is not None:
            q = q.filter(History.line.like('%{}%'.format(phrase)))
        lines = q.order_by(History.created.desc()).limit(lines).all()
        return lines

<mask token>


class Memory(object):

    def __init__(self, history=None):
        if history is None:
            history = config.max_history_lines
        if history < 1:
            logger.warning(
                'Max history is not defined. Defaulting to 1000 lines')
            history = 1000
        self.history_max_lines = int(history)
        self.history = list(session.query(History).order_by(History.created
            .desc()).limit(self.history_max_lines).all())

    def get_phrase(self, phrase_id):
        return session.query(Phrase).get(phrase_id)

    def get_phrase_by_text(self, text):
        return session.query(Phrase).filter_by(phrase=text)

    def get_response(self, response_id):
        return session.query(Response).get(response_id)

    def get_response_by_text(self, text):
        return session.query(Response).filter(Response.response.like('%{}%'
            .format(text))).one_or_none()

    def remember(self, keyvalue, response_text, added_by):
        phrase = self.get_phrase_by_text(keyvalue)
        if phrase:
            response = self.get_response_by_text(response_text)
        else:
            response = Response(response=response_text, added_by_id=added_by.id
                )
            session.add(response)
        if response not in phrase.responses:
            phrase.responses.append(response)
        session.commit()
        return response

    def add_history(self, nick, history_line):
        if len(self.history) > self.history_max_lines:
            try:
                session.delete(self.history[0])
                del self.history[0]
            except IndexError:
                pass
        hist = History(nick=nick, line=history_line, created=datetime.now())
        db.session.add(hist)
        self.history.append(hist)

    def commit_history(self):
        session.commit()

    def recall_history(self, lines=1):
        start = len(self.history) - lines
        if start < 0:
            start = 0
        hslice = self.history[start:len(self.history)]
        return hslice

    def find_in_history(self, nick=None, lines=1, phrase=None):
        q = session.query(History)
        if nick is not None:
            q = q.filter_by(nick=nick)
        if phrase is not None:
            q = q.filter(History.line.like('%{}%'.format(phrase)))
        lines = q.order_by(History.created.desc()).limit(lines).all()
        return lines

from __future__ import absolute_import
from datetime import datetime
import config
from models import Nick, Phrase, History, Response
from db import session
import logging
logger = logging.getLogger('snarkbot-memory')


class PhraseNotFound(Exception):
    pass


class ResponseNotFound(Exception):
    pass


class Memory(object):

    def __init__(self, history=None):
        if history is None:
            history = config.max_history_lines
        if history < 1:
            logger.warning(
                'Max history is not defined. Defaulting to 1000 lines')
            history = 1000
        self.history_max_lines = int(history)
        self.history = list(session.query(History).order_by(History.created
            .desc()).limit(self.history_max_lines).all())

    def get_phrase(self, phrase_id):
        return session.query(Phrase).get(phrase_id)

    def get_phrase_by_text(self, text):
        return session.query(Phrase).filter_by(phrase=text)

    def get_response(self, response_id):
        return session.query(Response).get(response_id)

    def get_response_by_text(self, text):
        return session.query(Response).filter(Response.response.like('%{}%'
            .format(text))).one_or_none()

    def remember(self, keyvalue, response_text, added_by):
        phrase = self.get_phrase_by_text(keyvalue)
        if phrase:
            response = self.get_response_by_text(response_text)
        else:
            response = Response(response=response_text, added_by_id=added_by.id
                )
            session.add(response)
        if response not in phrase.responses:
            phrase.responses.append(response)
        session.commit()
        return response

    def add_history(self, nick, history_line):
        if len(self.history) > self.history_max_lines:
            try:
                session.delete(self.history[0])
                del self.history[0]
            except IndexError:
                pass
        hist = History(nick=nick, line=history_line, created=datetime.now())
        db.session.add(hist)
        self.history.append(hist)

    def commit_history(self):
        session.commit()

    def recall_history(self, lines=1):
        start = len(self.history) - lines
        if start < 0:
            start = 0
        hslice = self.history[start:len(self.history)]
        return hslice

    def find_in_history(self, nick=None, lines=1, phrase=None):
        q = session.query(History)
        if nick is not None:
            q = q.filter_by(nick=nick)
        if phrase is not None:
            q = q.filter(History.line.like('%{}%'.format(phrase)))
        lines = q.order_by(History.created.desc()).limit(lines).all()
        return lines

from __future__ import absolute_import
from datetime import datetime
import config
from models import Nick, Phrase, History, Response
from db import session
import logging

logger = logging.getLogger('snarkbot-memory')

class PhraseNotFound(Exception):
    pass

class ResponseNotFound(Exception):
    pass

# just a wrapper around some common operations in sql
class Memory(object):
    def __init__(self,history=None):
        if history is None:
            history = config.max_history_lines

        if history < 1:
            logger.warning('Max history is not defined. Defaulting to 1000 lines')
            history = 1000
        
        self.history_max_lines = int(history)
        self.history = list( session.query(History).order_by(History.created.desc()).limit(self.history_max_lines).all() )

    def get_phrase(self,phrase_id):
        return session.query(Phrase).get(phrase_id)

    def get_phrase_by_text(self,text):
        return session.query(Phrase).filter_by(phrase=text)

    def get_response(self,response_id):
        return session.query(Response).get(response_id)

    def get_response_by_text(self,text):
        return session.query(Response).filter(
            Response.response.like('%{}%'.format(text))).one_or_none()
        

    def remember(self,keyvalue,response_text,added_by):
        phrase = self.get_phrase_by_text(keyvalue)
        if phrase:
            response = self.get_response_by_text(response_text)
        else:
            response = Response(response=response_text, added_by_id=added_by.id)
            session.add(response)

        if response not in phrase.responses:
            phrase.responses.append(response)

        session.commit()

        return response

    def add_history(self, nick, history_line ):
        if len(self.history) > self.history_max_lines:
            try:
                session.delete(self.history[0])
                del self.history[0]
            except IndexError:
                pass

        hist = History(
            nick=nick,
            line=history_line, 
            created=datetime.now()
        )
        db.session.add(hist)
        self.history.append(hist)
        
    def commit_history(self):
        # this just commits the database session
        session.commit()

    def recall_history(self,lines=1):
        # get lines number of lines of previous history
        start = len(self.history) - lines
        if start < 0:
            start = 0

        hslice = self.history[start:len(self.history)]
        return hslice

    def find_in_history(self,nick=None,lines=1,phrase=None):
        # find the last lines said by [nick]
        # optionally add a phrase

        q = session.query(History)
        if nick is not None:
            q = q.filter_by(nick=nick)

        if phrase is not None:
            q = q.filter(History.line.like('%{}%'.format(phrase)))

        lines = q.order_by(History.created.desc()).limit(lines).all()

        return lines
