Exemplo n.º 1
0
def init_bridge():
    """Parse the configuration file and set relevant variables."""
    conf_path = os.path.abspath(os.getenv('WAT_CONF', ''))

    if not conf_path or not os.path.isfile(conf_path):
        sys.exit('Could not find configuration file')

    parser = configparser.ConfigParser()
    parser.read(conf_path)

    # Whatsapp settings
    SETTINGS['wa_phone'] = parser.get('wa', 'phone')
    SETTINGS['wa_password'] = parser.get('wa', 'password')

    # Telegram settings
    SETTINGS['owner'] = parser.getint('tg', 'owner')
    SETTINGS['tg_token'] = parser.get('tg', 'token')

    SETTINGS['public_path'] = parser.get('public', 'path')
    SETTINGS['public_reachable'] = parser.get('public', 'address')

    # TinyDB
    global DB
    DB = TinyDB(parser.get('db', 'path'))
    DB.table_class = SmartCacheTable
Exemplo n.º 2
0
def db_smartcache():
    db_ = TinyDB(storage=MemoryStorage)
    db_.purge_tables()

    db_.table_class = SmartCacheTable
    db_ = db_.table('_default')

    db_.insert_multiple({'int': 1, 'char': c} for c in 'abc')
    return db_
Exemplo n.º 3
0
def db_smartcache():
    db_ = TinyDB(storage=MemoryStorage)
    db_.purge_tables()

    db_.table_class = SmartCacheTable
    db_ = db_.table('_default')

    db_.insert_multiple({'int': 1, 'char': c} for c in 'abc')
    return db_
Exemplo n.º 4
0
def get_db(smart_cache=False):
    db_ = TinyDB(storage=MemoryStorage)
    db_.purge_tables()

    if smart_cache:
        db_.table_class = SmartCacheTable
        db_ = db_.table('_default')

    db_.insert_multiple({'int': 1, 'char': c} for c in 'abc')
    return db_
Exemplo n.º 5
0
from tinydb import TinyDB, Query  # database module
from tinydb_smartcache import SmartCacheTable

# ================================================ Start of User Funcs ================================================

user_db = TinyDB('database/user/db.json')
user_db.table_class = SmartCacheTable
user_table = user_db.table('_default')

favs_db = TinyDB('database/user/user_favs/db.json')
favs_db.table_class = SmartCacheTable
favs_table = favs_db.table('_default')


def new_user(from_user_username, from_user_id, from_user_language):
    # Function to insert user on database. It must fill all the four arguments, or things could go bad on the bot
    user_table.insert({
        'id': from_user_id,
        'username': from_user_username,
        'language': from_user_language,
        'nsfw': "No",
        'notif': "Yes",
        'user_is_premium': "No",
        'user_blocked': "No"
    })
    favs_table.insert({'id': from_user_id, 'limit': 500, 'favorites': []})


def user_search(from_user_id):
    # Func to search user on database. Returns the dict if the user exists, and None if doesn't match
    # with anything. If the dict is empty, use new_user() func to register the user in the database.
Exemplo n.º 6
0
def main():
    # Our database
    dbpath = 't1.json'
    serializer = SerializationMiddleware(JSONStorage)
    serializer.register_serializer(DateTimeSerializer(), 'TinyDate')

    # Usando tinykit
    #dbk = Database(dbpath, storage=serializer)
    #dbk.table_class = SmartCacheTable
    dbk = Database(dbpath)

    # Usando tinyDb puro
    db = TinyDB(dbpath, storage=serializer)
    db.table_class = SmartCacheTable

    class ConfigModel(BaseModel):
        __tablename__ = "config"

        name = fields.StringField()
        key = fields.StringField()

        # this attribute wont be saved because it's not a field
        address = "this attribute will not be saved"

        class Meta:
            database = dbk

    model = ConfigModel()
    model.name = "original"
    model.key = "test"
    model.created_at = datetime.utcnow()

    # TinyDb puro
    #table = db.table(model.__tablename__)
    #model.tinsert()

    # Tinydb com tinykit
    table = dbk.table(model.__tablename__)
    model.insert()

    allrec = table.all()
    qlist = []
    qlist = [ConfigModel(eid=row.eid, **row) for row in allrec]
    for rec in qlist:
        print("Rec: ", rec.id, rec.key, rec.name)

    # tinydbk - create model from table's data
    row = table.get(dbk.where("key") == "test")
    new_model = ConfigModel(eid=row.eid, **row)

    print(new_model.id)
    print(new_model.name)
    print(new_model.key)
    print(new_model.created_at)
    print(new_model.address)
    print(new_model.location)
    print(new_model.created_at_datetime())

    new_model.key = 'dev'
    new_model.save()
    print(new_model.id)
    print(new_model.name)
    print(new_model.key)
    print(new_model.created_at)
    print(new_model.address)
    print(new_model.location)
    print(new_model.created_at_datetime())