Beispiel #1
0
def insert(data):
    try:
        control.makeFile(control.dataPath)
        cacheFile = os.path.join(control.dataPath, 'regex.db')
        dbcon = database.connect(cacheFile)
        dbcur = dbcon.cursor()
        dbcur.execute("CREATE TABLE IF NOT EXISTS regex ("
                      "regex TEXT, "
                      "response TEXT, "
                      "UNIQUE(regex)"
                      ");")
        for i in data:
            try:
                dbcur.execute("INSERT INTO regex Values (?, ?)",
                              (i['regex'], i['response']))
            except:
                pass
        dbcon.commit()
    except:
        return
Beispiel #2
0
def get(function_, duration, *args, **table):

    try:

        response = None

        f = repr(function_)
        f = re.sub(r'.+\smethod\s|.+function\s|\sat\s.+|\sof\s.+', '', f)

        a = hashlib.md5()
        for i in args:
            a.update(str(i))
        a = str(a.hexdigest())

    except Exception:

        pass

    try:
        table = table['table']
    except Exception:
        table = 'rel_list'

    try:

        control.makeFile(control.dataPath)
        dbcon = db.connect(control.cacheFile)
        dbcur = dbcon.cursor()
        dbcur.execute(
            "SELECT * FROM {tn} WHERE func = '{f}' AND args = '{a}'".format(
                tn=table, f=f, a=a))
        match = dbcur.fetchone()

        try:
            response = evaluate(match[2].encode('utf-8'))
        except AttributeError:
            response = evaluate(match[2])

        t1 = int(match[3])
        t2 = int(time.time())
        update = (abs(t2 - t1) / 3600) >= int(duration)
        if not update:
            return response

    except Exception:

        pass

    try:

        r = function_(*args)
        if (r is None or r == []) and response is not None:
            return response
        elif r is None or r == []:
            return r

    except Exception:
        return

    try:

        r = repr(r)
        t = int(time.time())
        dbcur.execute("CREATE TABLE IF NOT EXISTS {} ("
                      "func TEXT, "
                      "args TEXT, "
                      "response TEXT, "
                      "added TEXT, "
                      "UNIQUE(func, args)"
                      ");".format(table))
        dbcur.execute(
            "DELETE FROM {0} WHERE func = '{1}' AND args = '{2}'".format(
                table, f, a))
        dbcur.execute("INSERT INTO {} Values (?, ?, ?, ?)".format(table),
                      (f, a, r, t))
        dbcon.commit()

    except Exception:
        pass

    try:
        return evaluate(r.encode('utf-8'))
    except Exception:
        return evaluate(r)
Beispiel #3
0
def _get_connection_providers():
    control.makeFile(data_path)
    conn = db.connect(os.path.join(data_path, 'providers.13.db'))
    conn.row_factory = _dict_factory
    return conn
Beispiel #4
0
def _get_connection_search():
    control.makeFile(data_path)
    conn = db.connect(os.path.join(data_path, 'search.1.db'))
    conn.row_factory = _dict_factory
    return conn
Beispiel #5
0
def _get_connection_meta():
    control.makeFile(data_path)
    conn = db.connect(os.path.join(data_path, 'meta.5.db'))
    conn.row_factory = _dict_factory
    return conn