Exemplo n.º 1
1
class TinydbStorage(interface.StorageInterface):

    def __init__(self, db_path):
        super(TinydbStorage, self).__init__()
        self.db = TinyDB(db_path)

    def create_table(self, table_name):
        self._assert_no_table(table_name)
        table = self.db.table(table_name)
        table.insert({'table_exists': True})

    def get_tables(self):
        return self.db.tables()

    def get(self, table_name, key):
        self._assert_table(table_name)
        table = self.db.table(table_name)
        element = Query()
        result = table.search(element.key == key)
        if len(result) == 0:
            raise interface.DataException("Key {} not found in table".format(key))
        return result[0]['val']

    def set(self, table_name, key, val):
        self._assert_table(table_name)
        table = self.db.table(table_name)
        element = Query()
        table.remove(element.key == key)
        table.insert({'key': key, 'val': val})

    def append(self, table_name, list_key, val):
        self._assert_table(table_name)
        table = self.db.table(table_name)
        l = self.get(table_name, list_key)
        #TODO: check if l is a list maybe
        if val in l:
            raise interface.DataException("Value: {0} already exists in list: {1}".format(val, list_key))
        l.append(val)
        element = Query()
        table.update({'val': l}, element.key == list_key)

    def remove(self, table_name, list_key, val):
        #TODO: do
        raise NotImplementedError("Storage module must implement this")

    def _assert_no_table(self, table_name):
        table = self.db.table(table_name)
        if len(table) > 0:
            raise interface.DataException("Table already exists: {}".format(table_name))

    def _assert_table(self, table_name):
        table = self.db.table(table_name)
        if len(table) == 0:
            raise interface.DataException("Table does not exist: {}".format(table_name))
class Base(object):

    def __init__(self, **attributes):
        self.__dict__ = attributes
        self.db = TinyDB(TINY_DB_PATH)
        self.table = self.db.table(self.__class__.__name__)

    def all(self):
        monitors = []
        for elem in self.table.all():
            monitors.append(self.__class__(**elem))
        return monitors

    def load(self, label):
        query = Query()
        self.__dict__.update(self.table.search(query.label == label)[0])

    def save(self):
        attributes = self.get_attributes()
        if self.__new_record__():
            self.table.insert(attributes)
        else:
            query = Query()
            self.table.update(attributes, query.label == self.label)
        return True

    def destroy(self):
        query = Query()
        self.table.remove(eids=[self.table.search(query.label == self.label)[0].eid])

    def get_attributes(self):
        attributes = self.__dict__.copy()
        attributes.pop('db')
        attributes.pop('table')

        return attributes

    def __new_record__(self):
        query = Query()
        return len(self.table.search(query.label == self.label)) == 0
Exemplo n.º 3
0
    def __init__(self):
        self.sentCache = {}
        self.checkingThread = threading.Thread(target = self.startThread)
        #self.checkingThread.daemon = True
        self.keepChecking = True
        self.config = Config()

        if self.config.winning_streak:
            self.winning_streak_messages = self.read_spree_file(self.config.winning_streak_file)
        if self.config.losing_streak:
            self.losing_streak_messages = self.read_spree_file(self.config.losing_streak_file)

        # Debug purposes
        self.printOutput = False

        # Initializing the API
        key = dotamatch.get_key()  # Steam Dev Key (~/.steamapi)
        try:
            self.match_history = MatchHistory(key)
            self.match_details = MatchDetails(key)
            self.account_details = PlayerSummaries(key)
            self.heroes = Heroes(key).heroes()

            # ActualDB
            db = TinyDB(self.config.db_path)
            #db.purge()
            #db.purge_tables()

            self.matches_table = db.table('matches')
            self.matches_info_table = db.table('matches_info')
        except dotamatch.api.ApiError:
            print u"Erro ao conectar à API."
Exemplo n.º 4
0
class EventModel(object):
    def __init__(self, uri):
        self.uri = uri
        self.db = None
        self.reload()

    def reload(self):
        if self.db is not None:
            self.db.close()
        self.db = TinyDB(self.uri, indent=2)
        self.actions = self.db.table('actions')
        self.alarms = self.db.table('alarms')

    def get_action_by_id(self, action_id):
        return self.actions.get(eid=action_id)

    def get_alarm_by_id(self, alarm_id):
        return self.alarms.get(eid=alarm_id)

    def get_actions_by_alarm(self, alarm):
        for action_id in alarm.get('actions', []):
            action = self.get_action_by_id(action_id)
            if action is None: continue
            yield action

    def get_all_alarms(self):
        return self.alarms.all()

    def get_all_actions(self):
        return self.actions.all()

    def get_all_alarms_expanded(self):
        for alarm in self.get_all_alarms():
            for action in self.get_actions_by_alarm(alarm):
                yield alarm, action

    def add_event(self, alarm, actions):
        action_ids = [self.add_action(a) for a in actions]
        alarm['actions'] = action_ids
        return self.alarms.insert(alarm)

    def add_action(self, action):
        return self.actions.insert(action)

    def add_alarm(self, alarm):
        return self.add_event(alarm, [])

    def update_alarm(self, alarmid, new_fields={}):
        return self.alarms.update(new_fields, eids=[alarmid])

    def update_action(self, actionid, new_fields={}):
        return self.actions.update(new_fields, eids=[actionid])

    def delete_alarm(self, alarmid):
        return self.alarms.remove(eids=[alarmid])

    def delete_action(self, actionid):
        return self.actions.remove(eids=[actionid])
Exemplo n.º 5
0
def load_db(bot, conn):
    """Load in our database and the Headers."""
    global CACHE, USERS, LORE, COLLECTIONS, LINKS, DB_Q, HEADERS
    CACHE = TinyDB('destiny.json')
    USERS = CACHE.table('users')
    LORE = CACHE.table('lore')
    COLLECTIONS = CACHE.table('collections')
    LINKS = CACHE.table('links')
    DB_Q = Query()  # This is our query object used. Use it in all queries.
    HEADERS = {'X-API-Key': bot.config.get('api_keys', {}).get('destiny', None)}
class LocalCollectionStore(object):

    def __init__(self):
        if TinyDB.DEFAULT_STORAGE == JSONStorage:
            self._db = TinyDB(path=self.path)
        else:
            self._db = TinyDB()

    def add(self, piece):
        """
        :param Piece piece:
        :return:
        """
        self._piece_table().insert(piece.to_dict())

    def piece_list(self):
        """
        :rtype: list(Piece)
        """

        return [Piece.from_dict(piece_dict) for piece_dict in self._piece_table().all()]

    @contract
    def state_list(self):
        """
        :rtype: list(WatchState)
        """
        state_entry_list = self._state_table().all()
        if not state_entry_list:
            return []

        return jsonpickle.loads(state_entry_list[0]['pickle'])

    def set_state_list(self, state_list):
        """
        :type state_list: list(WatchState)
        """
        state_table = self._state_table()
        state_table.purge()
        self._state_table().insert({'pickle': jsonpickle.dumps(state_list)})

    def _piece_table(self):
        return self._db.table("piece")

    def _state_table(self):
        return self._db.table("state")

    def _state_entry(self):
        Entry = Query()
        entry_search_list = self._state_table().search(Entry.name == "state_list")
        if entry_search_list:
            return entry_search_list[0]
Exemplo n.º 7
0
    def __init__(self, path):

        root_dir = os.path.abspath(path)
        if not os.path.exists(root_dir):
            raise IOError('Path does not exist: %s' % path)

        fs = HashFS(os.path.join(root_dir, 'hashfs'), depth=3,
                    width=2, algorithm='md5')

        # Setup Serialisation for non list/dict objects
        serialization_store = SerializationMiddleware()
        serialization_store.register_serializer(DateTimeSerializer(),
                                                'TinyDate')
        serialization_store.register_serializer(FileSerializer(fs),
                                                'TinyFile')
        if opt.has_numpy:
            serialization_store.register_serializer(NdArraySerializer(),
                                                    'TinyArray')
        if opt.has_pandas:
            serialization_store.register_serializer(DataFrameSerializer(),
                                                    'TinyDataFrame')
            serialization_store.register_serializer(SeriesSerializer(),
                                                    'TinySeries')

        db = TinyDB(os.path.join(root_dir, 'metadata.json'),
                    storage=serialization_store)

        self.db = db
        self.runs = db.table('runs')
        self.fs = fs
Exemplo n.º 8
0
def prelimsearchURLs():
    db = TinyDB('db.json')
    prelimtable = db.table('prelimcasedetails')
    cases = getAllSearchResults()
    prelimtable.purge()
    for idx, case in enumerate(cases):
        prelimtable.insert({'caseid': case.caseid, 'casename': case.casename, 'prelimvideourl': case.videourl,'detailedVideoURL':'0'})
Exemplo n.º 9
0
class ArtemisApplication(Application):
    def __init__(self, db_file, *args, **kwargs):
        super(ArtemisApplication, self).__init__(*args, **kwargs)
        self._db = TinyDB(db_file)

    def merge_host(self, host):
        specs = filter(lambda (hostrx, _): hostrx.pattern == host, self.handlers)
        map(self.handlers.remove, specs)
        self.add_handlers(host, reduce(add, [s for _, s in specs]))

    def table(self, tname):
        return self._db.table(tname)

    def model(self, model_name):
        def _outer(model_cls):
            model_cls.__table__ = self.table(model_name)

            self.add_handlers('.*', [
                (r'/{}'.format(model_name),
                 ModelBaseRequest, dict(model=model_cls)),
                (r'/{}/query'.format(model_name),
                 ModelQueryRequest, dict(model=model_cls)),
                (r'/{}/([a-zA-Z\-_0-9]+)'.format(model_name),
                 ModelIDRequest, dict(model=model_cls)),
                (r'/{}/([a-zA-Z\-_0-9]+)/([a-zA-Z\-_0-9]+)'.format(model_name),
                 ModelMethodRequest, dict(model=model_cls))
            ])

            return model_cls

        return _outer
Exemplo n.º 10
0
def test_purge_table():
    db = TinyDB(storage=MemoryStorage)
    assert [TinyDB.DEFAULT_TABLE] == list(db.tables())

    db.purge_table(TinyDB.DEFAULT_TABLE)
    assert [] == list(db.tables())

    table_name = 'some-other-table'
    db = TinyDB(storage=MemoryStorage)
    db.table(table_name)
    assert set([TinyDB.DEFAULT_TABLE, table_name]) == db.tables()

    db.purge_table(table_name)
    assert set([TinyDB.DEFAULT_TABLE]) == db.tables()

    db.purge_table('non-existent-table-name')
    assert set([TinyDB.DEFAULT_TABLE]) == db.tables()
Exemplo n.º 11
0
class Repository(Singleton):
    USERS_TABLE = 'users'
    USERS_RANK_TABLE = 'users_rank_stat'

    """ Repository class """
    def __init__(self):
        self.database = None

    def init_db(self, dbfile):
        """ Init repository """
        serialization = SerializationMiddleware()
        serialization.register_serializer(DateTimeSerializer(), 'TinyDate')
        self.database = TinyDB(dbfile, storage=serialization)

    def user_exists(self, wg_id):
        """ Check users exists """
        user = Query()
        count = self.database.table(Repository.USERS_TABLE).count(user.wg_id == wg_id)
        return count > 0

    def save_user(self, user_obj):
        """ Update user """
        table = self.database.table(Repository.USERS_TABLE)
        query = Query()
        users = table.search(query.wg_id == user_obj.wg_id)
        if len(users) > 0:
            existing = DbUser.fromdict(users[0])
            existing.last_date = user_obj.last_date
            table.update(existing.__dict__, query.wg_id == existing.wg_id)
        else:
            table.insert(user_obj.__dict__)

    def all_users(self):
        """ Get all users """
        table = self.database.table(Repository.USERS_TABLE)
        users_data = table.all()
        users = list()
        for item in users_data:
            users.append(DbUser.fromdict(item))
        return users

    def rank_history(self, wg_id):
        """ Get all rank history records """
        table = self.database.table(Repository.USERS_RANK_TABLE)
        query = Query()
        return table.search(query.wg_id == wg_id)
Exemplo n.º 12
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.º 13
0
def getdetailedSearchURL():
    db = TinyDB('db.json')
    prelimtable = db.table('prelimcasedetails')
    query = Query()
    elementstoupdate = prelimtable.search(query.detailedVideoURL == '0')
    for iter,element in enumerate(elementstoupdate):
        prelimURL = element['prelimvideourl']
        detailedVideoURL = getvideosourceinfo(prelimURL)
        caseid = element['caseid']
        prelimtable.update({'detailedVideoURL':detailedVideoURL},Query()['caseid'] == caseid)
Exemplo n.º 14
0
def patched_modules():
    db = TinyDB(recipyGui.config.get('tinydb'))
    modules = db.table('patches').all()
    db.close()

    form = SearchForm()

    return render_template('patched_modules.html', form=form,
                           active_page='patched_modules', modules=modules,
                           dbfile=recipyGui.config.get('tinydb'))
Exemplo n.º 15
0
def test_gc(tmpdir):
    # See https://github.com/msiemens/tinydb/issues/92
    path = str(tmpdir.join('db.json'))
    db = TinyDB(path)
    table = db.table('foo')
    table.insert({'something': 'else'})
    table.insert({'int': 13})
    assert len(table.search(where('int') == 13)) == 1
    assert table.all() == [{'something': 'else'}, {'int': 13}]
    db.close()
Exemplo n.º 16
0
class MainView(Frame):
    def __init__(self, parent):
        "docstring"
        Frame.__init__(self, parent)

        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Pycollect")
        self.pack(fill=BOTH)

        menubar = Menu(self.parent)
        self.parent.config(menu=menubar)

        filemenu = Menu(menubar)
        filemenu.add_command(label="Open", command=self.open_database)
        filemenu.add_command(label="Exit", command=self.on_exit)
        menubar.add_cascade(label="File", menu=filemenu)

        frame1 = Frame(self)
        frame1.pack(fill=X)
        self.game_count = StringVar()
        game_count_label = Label(frame1, textvariable=self.game_count).pack()

        frame2 = Frame(self)
        frame2.pack(fill=X, side=LEFT, expand=True)
        self.game_list = Listbox(frame2)
        self.game_list.pack(fill=Y, side=LEFT, expand=True)

        # Events
        self.bind('<<update_game_count>>', self.update_game_count)
        self.bind('<<update_game_list>>', self.update_game_list)

    def on_exit(self):
        self.quit()

    def open_database(self):
        filename = tkFileDialog.askopenfilename(filetypes=[('Database files', '.db'), ('All files', '.*')])
        self.db = TinyDB(filename)
        self.games = self.db.table('games')
        print len(self.games)
        self.event_generate('<<update_game_count>>')
        self.event_generate('<<update_game_list>>')

    def update_game_count(self, args):
        self.game_count.set('Game total: {}'.format(len(self.games)))

    def update_game_list(self, args):
        self.game_list.delete(0, END)
        game_titles = [g['name'] for g in self.games.all()]
        for title in game_titles:
            self.game_list.insert(END, title)
Exemplo n.º 17
0
class Database(object):
    def __init__(self, path):
        self.db = TinyDB(path)

    def get_user_value(self, user, key):
        key = '_' + key
        table = self.db.table('user_values')
        value = table.get(where('id') == user.id)
        try:
            return value[key]
        except KeyError:
            return None

    def set_user_value(self, user, key, value):
        self.insert_else_update_user(user, {key: value})

    def insert_else_update_user(self, user, extra_params=None):
        table = self.db.table('user_values')
        if not table.get(where('id') == user.id):
            table.insert({
                'id': user.id,
                'username': user.username,
                'first_name': user.first_name,
                'last_name': user.last_name
            })
        else:
            table.update({
                'username': user.username,
                'first_name': user.first_name,
                'last_name': user.last_name,
            }, where('id') == user.id)
        if extra_params:
            for k in extra_params:
                new_key = k if k.startswith('_') else '_' + k
                extra_params[new_key] = extra_params.pop(k)
            table.update(extra_params, where('id') == user.id)

    def process_message(self, message):
        if message.from_user:
            self.insert_else_update_user(message.from_user)
Exemplo n.º 18
0
class dal(object):
    def __init__(self, data_base_file):
        if not os.path.isfile(data_base_file):
            open(data_base_file, 'a')
        self.db = TinyDB(data_base_file, default_table='contacts')
        self.users = self.db.table("users")
        self.conversations = self.db.table("conversations")
        self.contacts = self.db.table("contacts")
        self.instance = True

    def add_user(self, element):
        self._insert(self.users, element)

    def remove_user(self):
        pass

    def get_users(self):
        return self.users.all()

    def write_message(self, element):
        self._insert(self.conversations, element)

    def get_user_messages(self, phone):
        conversation = Query()
        return self.db.search(conversation.phone == phone)

    def get_user_contacts(self, phone):
        contact = Query()
        a = self.db.search(contact.phone == phone)
        return a

    def add_contact(self, element):
        self._insert(self.contacts, element)

    def _insert(self, table, element={}):
        try:
            table.insert(element)
        except Exception as ex:
            print(ex)
Exemplo n.º 19
0
    def add_to_db(self, db, table):
        database = TinyDB(str(db))
        tbl = database.table(str(table))

        id = len(tbl) # The id of the formula, auto incrementing

        tbl.insert({
            "id": id,
            "name": self.name,
            "units": self.units,
            "formula": self.formula,
            "explanation": self.explanation,
            "information": {"name_added_by": self.name_added_by, "date": self.date}
        })
Exemplo n.º 20
0
 def _convert_to_sqlitedb(self):
     old_db = TinyDB(DATABASE_SETTINGS['database'])
     self.mem_db = []
     total = 0
     for table in old_db.tables():
         for item in old_db.table(table).all():
             total += 1
     print "MIGRATING DATABASE"
     print "--OLD DATABASE: " + str(total)
     i = 0
     for table in old_db.tables():
         for item in old_db.table(table).all():
             if len(item['accn']) < 15:
                 if int(item['time']) > self.purge_date:
                     self.mem_db.append(item)
                     print "   Converted:   " + str(i) + " of " + str(total)
                     i += 1
                     self.dict_db[str(item['time'])] = item
                 else:
                     print "REMOVING OLD THING"
     print "DATABASE MIGRATION COMPLETE"
     print "COMMITING CHANGES"
     self.get_last_filed()
Exemplo n.º 21
0
def run_details():
    form = SearchForm()
    annotateRunForm = AnnotateRunForm()
    query = request.args.get('query', '')
    run_id = int(request.args.get('id'))

    db = TinyDB(recipyGui.config.get('tinydb'))
    r = db.get(eid=run_id)
    diffs = db.table('filediffs').search(Query().run_id == run_id)

    db.close()

    return render_template('details.html', query=query, form=form,
                           annotateRunForm=annotateRunForm, run=r,
                           dbfile=recipyGui.config.get('tinydb'), diffs=diffs)
Exemplo n.º 22
0
def latest_run():
    form = SearchForm()
    annotateRunForm = AnnotateRunForm()

    db = TinyDB(recipyGui.config.get('tinydb'))

    runs = db.all()
    runs = sorted(runs, key = lambda x: parse(x['date'].replace('{TinyDate}:', '')), reverse=True)
    r = db.get(eid=runs[0].eid)
    diffs = db.table('filediffs').search(Query().run_id == r.eid)

    db.close()

    return render_template('details.html', query='', form=form, run=r,
                           annotateRunForm=annotateRunForm,
                           dbfile=recipyGui.config.get('tinydb'), diffs=diffs,
                           active_page='latest_run')
Exemplo n.º 23
0
def load():
    db = TinyDB('db.json')
    player_stats = db.table(name_input)
    player.time_advance = (player_stats.get(eid=1))['time_advance']
    player.bow_message = (player_stats.get(eid=2))['bow_message']
    player.hunt_message = (player_stats.get(eid=3))['hunt_message']
    player.can_hunt = (player_stats.get(eid=4))['can_hunt']
    player.exploration1_message = (player_stats.get(eid=5))['exploration1_message']
    player.sword_message = (player_stats.get(eid=6))['sword_message']
    player.can_go_to_town = (player_stats.get(eid=7))['can_go_to_town']
    player.health = (player_stats.get(eid=8))['health']
    player.weapon = (player_stats.get(eid=9))['weapon']
    player.energy = (player_stats.get(eid=10))['energy']
    player.exploration_count = (player_stats.get(eid=11))['exploration_count']
    player.town_count = (player_stats.get(eid=12))['town_count']
    player.money = (player_stats.get(eid=13))['money']
    player.inventory = player_stats.get(eid=14)
    print "Your inventory has %r" %player.inventory
Exemplo n.º 24
0
def main():
    db = TinyDB('db.json')
    table = db.table('newsbot')
    while True:
        try:
            news = utility.get_news()
        except:
            news = []
        last = utility.last_new(table)
        for i, (title, url) in enumerate(news):
            if last in url:
                del news[i:]
        if news:
            chat_id = "@mvnews"
            table.insert({'url': str(news[0][1])})
            for n in news:
                text = ('[{}]' + '({})').format(n[0], n[1])
                bot.send_message(chat_id, text, parse_mode='Markdown')
        time.sleep(10)
Exemplo n.º 25
0
def test_custom_table_class():
    from tinydb.database import Table

    class MyTableClass(Table):
        pass

    # Table class for single table
    db = TinyDB(storage=MemoryStorage)
    assert isinstance(TinyDB(storage=MemoryStorage).table(),
                      Table)
    assert isinstance(db.table('my_table', table_class=MyTableClass),
                      MyTableClass)

    # Table class for all tables
    TinyDB.table_class = MyTableClass
    assert isinstance(TinyDB(storage=MemoryStorage).table(),
                      MyTableClass)
    assert isinstance(TinyDB(storage=MemoryStorage).table('my_table'),
                      MyTableClass)

    # Reset default table class
    TinyDB.table_class = Table
Exemplo n.º 26
0
def test_string_key2():
    from tinydb.database import Table, StorageProxy, Document
    from tinydb.storages import MemoryStorage

    class StorageProxy2(StorageProxy):
        def _new_document(self, key, val):
            # Don't convert the key to a number here!
            return Document(val, key)

    class Table2(Table):
        def _init_last_id(self, data):
            if data:
                self._last_id = len(data)
            else:
                self._last_id = 0

        def _get_next_id(self):
            next_id = self._last_id + 1
            data = self._read()
            while str(next_id) in data:
                next_id += 1
            self._last_id = next_id
            return str(next_id)

        def _get_doc_id(self, document):
            if not isinstance(document, dict):
                raise ValueError('Document is not a dictionary')
            return document.get('doc_id') or self._get_next_id()

    TinyDB.storage_proxy_class = StorageProxy2
    db = TinyDB(storage=MemoryStorage, table_class=Table2)
    table = db.table()
    table.insert({'doc_id': 'abc'})
    assert table.get(doc_id='abc')['doc_id'] == 'abc'
    assert table._last_id == 0
    table.insert({'abc': 10})
    assert table.get(doc_id='1')['abc'] == 10
    assert table._last_id == 1
Exemplo n.º 27
0
class DBEntity(object):
    def __init__(self, db_path):
        self._db = TinyDB(db_path)

    def table(self, tname):
        return self._db.table(tname)

    def model(self, model_name):
        def _outer(model_cls):
            model_cls.__table__ = self.table(model_name)
            model_cls.routes = [
                (r'/{}'.format(model_name),
                 ModelBaseRequest, dict(model=model_cls)),
                (r'/{}/query'.format(model_name),
                 ModelQueryRequest, dict(model=model_cls)),
                (r'/{}/([a-zA-Z\-_0-9]+)'.format(model_name),
                 ModelIDRequest, dict(model=model_cls)),
                (r'/{}/([a-zA-Z\-_0-9]+)/([a-zA-Z\-_0-9]+)'.format(model_name),
                 ModelMethodRequest, dict(model=model_cls))
            ]

            return model_cls
        return _outer
Exemplo n.º 28
0
def save():
    db = TinyDB('db.json')
    player_stats = db.table(name_input)
    if db.get(where('name') == name_input):
        player_stats.update({'time_advance' : player.time_advance}, eids=[1])
        player_stats.update({'bow_message': player.bow_message}, eids=[2])
        player_stats.update({'hunt_message': player.hunt_message}, eids=[3])
        player_stats.update({'can_hunt': player.can_hunt}, eids=[4])
        player_stats.update({'exploration1_message': player.exploration1_message}, eids=[5])
        player_stats.update({'sword_message': player.sword_message}, eids=[6])
        player_stats.update({'can_go_to_town': player.can_go_to_town}, eids=[7])
        player_stats.update({'health': player.health}, eids=[8])
        player_stats.update({'weapon': player.weapon}, eids=[9])
        player_stats.update({'energy': player.energy}, eids=[10])
        player_stats.update({'exploration_count': player.exploration_count}, eids=[11])
        player_stats.update({'town_count': player.town_count}, eids=[12])
        player_stats.update({'money': player.money}, eids=[13])
        player_stats.update(player.inventory, eids=[14])
        print "Saved"

    else:
        player_stats.insert({'time_advance' : player.time_advance})
        player_stats.insert({'bow_message': player.bow_message})
        player_stats.insert({'hunt_message': player.hunt_message})
        player_stats.insert({'can_hunt': player.can_hunt})
        player_stats.insert({'exploration1_message': player.exploration1_message})
        player_stats.insert({'sword_message': player.sword_message})
        player_stats.insert({'can_go_to_town': player.can_go_to_town})
        player_stats.insert({'health': player.health})
        player_stats.insert({'weapon': player.weapon})
        player_stats.insert({'energy': player.energy})
        player_stats.insert({'exploration_count': player.exploration_count})
        player_stats.insert({'town_count': player.town_count})
        player_stats.insert({'money': player.money})
        player_stats.insert(player.inventory)
        db.insert({'name' : name_input})
        print "First save"
Exemplo n.º 29
0
def scale(vnfi_id):
    req = request.get_json(force=True)
    cur_flv_id = req['cur_flavor_id']
    dst_flv_id = req['flavor_id']
    vnfi_name = req['vnfi_name']
    vnfd_id = req['vnfd_id']

    # Get VNFCI settings
    db = TinyDB('data/preins.json')
    tbl = db.table('settings_vdus_table')
    q = Query()
    docs = tbl.search(q.vnfi_id == vnfi_id)
    if len(docs) <= 0:
        return Response('', status=403)
    vnfcis = docs[0]['settings']['vnfcs']

    # Get VNFD info
    uri = '/manager/v2/vnf/packages/' + vnfd_id
    status, res_body, msglog = http_send_to_vnfm('GET', uri, '', "vnfm")
    if status == 200:
        pkginfo = json.loads(res_body)

    vnfd_flv = pkginfo['vnfd']['flavors']

    for flv in vnfd_flv:
        if flv['flavor_id'] == cur_flv_id:
            cur_flv_info = flv
        elif flv['flavor_id'] == dst_flv_id:
            dst_flv_info = flv

    scale_body = {
        "vnf": {
            "ns_instance_id": vnfi_id,
            "vnf_descriptor_id": pkginfo['vnfd']['vnfd_id'],
            "name": vnfi_name,
            "flavor_id": dst_flv_id
        }
    }

    if int(cur_flv_info['grade']) > int(
            dst_flv_info['grade']):  # case scale-out
        vnfcs = []
        for c_vdu in cur_flv_info['vdus']:
            for d_vdu in dst_flv_info['vdus']:
                if c_vdu['vdu_id'] == d_vdu['vdu_id']:
                    if int(c_vdu['num_instances']) != int(
                            d_vdu['num_instances']):
                        vdu_id = c_vdu['vdu_id']
                        begin = int(c_vdu['num_instances'])
                        end = int(d_vdu['num_instances'])
                        for vnfci in vnfcis:
                            if vnfci['vdu_id'] == vdu_id:
                                if int(vnfci['sequence']) > begin:
                                    if int(vnfci['sequence']) <= end:
                                        vnfci['sequence'] = str(
                                            int(vnfci['sequence']) - begin)
                                        vnfcs.append(vnfci)
        scale_body['vnf']['vnfcs'] = vnfcs

    uri = '/v2/vnf/instances/' + vnfi_id + '/scale'
    status, res_body, msglog = http_send_to_vnfm('POST', uri,
                                                 json.dumps(scale_body),
                                                 "nfvo")

    job_url = ''
    if status == 202:
        res_json = json.loads(res_body)
        job_url = res_json['job']['id']

    return Response(msglog, status=200, headers={'X-Job-ID': job_url})
Exemplo n.º 30
0
    def __init__(self,
                 config_path=args.config,
                 ws_server=args.host,
                 ws_port=args.port):
        # Setting up logging first and foremost
        self.log = get_logger("midi_to_obs")

        # Internal service variables
        self._action_buffer = []
        self._action_counter = 2
        self._portobjects = []
        self._lastTbarMove = time()
        self._tbarActive = False
        self._tbarDir = 0

        # Feedback blocking
        self.blockcount = 0
        self.block = False

        #load tinydb configuration database
        self.log.debug("Trying to load config file  from %s" % config_path)
        tiny_database = TinyDB(config_path, indent=4)
        tiny_db = tiny_database.table("keys", cache_size=20)
        tiny_devdb = tiny_database.table("devices", cache_size=20)

        #get all mappings and devices
        self._mappings = tiny_db.all()
        self._devices = tiny_devdb.all()

        #open dbj datebase for mapping and clear
        self.mappingdb = dbj("temp-mappingdb.json")
        self.mappingdb.clear()

        #convert database to dbj in-memory
        for _mapping in self._mappings:
            self.mappingdb.insert(_mapping)

        self.log.debug("Mapping database: `%s`" % str(self.mappingdb.getall()))

        if len(self.mappingdb.getall()) < 1:
            self.log.critical("Could not cache device mappings")
            # ENOENT (No such file or directory)
            exit(2)

        self.log.debug("Successfully imported mapping database")

        result = tiny_devdb.all()
        if not result:
            self.log.critical("Config file %s doesn't exist or is damaged" %
                              config_path)
            # ENOENT (No such file or directory)
            exit(2)

        self.log.info("Successfully parsed config file")

        self.log.debug("Retrieved MIDI port name(s) `%s`" % result)
        #create new class with handler and open from there, just create new instances
        for device in result:
            self._portobjects.append(
                (DeviceHandler(device, device.doc_id), device.doc_id))

        self.log.info("Successfully initialized midi port(s)")
        del result

        # close tinydb
        tiny_database.close()

        # setting up a Websocket client
        self.log.debug("Attempting to connect to OBS using websocket protocol")
        self.obs_socket = WebSocketApp("ws://%s:%d" % (ws_server, ws_port))
        self.obs_socket.on_message = lambda ws, message: self.handle_obs_message(
            ws, message)
        self.obs_socket.on_error = lambda ws, error: self.handle_obs_error(
            ws, error)
        self.obs_socket.on_close = lambda ws: self.handle_obs_close(ws)
        self.obs_socket.on_open = lambda ws: self.handle_obs_open(ws)
 def getTable(self):
     db = TinyDB(Constants.DATABASE_ROOT_PATH + Constants.UNITED_DATABASE_FILENAME)
     return db.table(self.TABLE_NAME)
Exemplo n.º 32
0
from flask import Flask, request, send_from_directory
from flask.json import jsonify
from tinydb import TinyDB, Query
import os
from flask_cors import CORS
from Deck import Deck

# Prepare the app and configurations
app = Flask(__name__)
CORS(app)

# Setup the document DB
db = TinyDB('db.json')
deck = db.table('deck')

# Setup the static file directory
static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               './wwwroot')


@app.after_request
def add_header(response):
    response.cache_control.max_age = 86400
    del response.headers['Server']
    return response


@app.route('/', methods=['GET'])
def serve_dir_directory_index():
    ''' Serves the static index file '''
    return send_from_directory(static_file_dir, 'index.html')
#!/usr/bin/env python
from tinydb import TinyDB, Query
from config import SL_HPL2_KEY
import json
import httplib2
http = httplib2.Http()
db = TinyDB('db.json')
sitestable = db.table('sites')
sitestable.purge()
sitesarr = []
url = 'http://api.sl.se/api2/LineData.json?model=site&key=' + SL_HPL2_KEY
resp, content = http.request(url)
sites = json.loads(str(content, 'utf8'))
sites = sites['ResponseData']['Result']
for index, site in enumerate(sites):
    sitename = site['SiteName']
    siteobj = {'name': sitename, 'id': site['SiteId']}
    sitesarr.append(siteobj)
    print(index, site)
sitestable.insert_multiple(sitesarr)
print('finished OK!')
exit()
Exemplo n.º 34
0
from googleapiclient import discovery
from tinydb import TinyDB

db = TinyDB('entities.json')
group_table = db.table('Instance Groups')
template_table = db.table('Instance Templates')
credentials = GoogleCredentials.get_application_default()
from oauth2client.file import Storage

storage = Storage('creds.data')
service = discovery.build('compute', 'v1', credentials=storage.get())
instanceGroups = service.instances()
instanceTemplates = service.instanceTemplates()
zones = service.zones()


def insert_templates():
    projectId = TinyDB('projects.json').table("Project").all()
    request = instanceTemplates.list(project=projectId)
    try:
        while request is not None:
            response = request.execute()
            for instanceTemplate in response['items']:
                template_table.insert(instanceTemplate)
            request = instanceTemplates.list_next(previous_request=request, previous_response=response)
    except KeyError:
        pass


def insert_instance_groups():
    projectId = TinyDB('projects.json').table("Project").all()
Exemplo n.º 35
0
class TinyDBBinding(object):
    def __init__(self, path=None):
        self.path = path
        self.cacheSize = Configuration.get('db.cacheSize')
        Logging.log('Cache Size: %s' % self.cacheSize)

        # create transaction locks
        self.lockResources = Lock()
        self.lockIdentifiers = Lock()
        self.lockSubscriptions = Lock()
        self.lockStatistics = Lock()
        self.lockAppData = Lock()

    def openDB(self):
        if Configuration.get('db.inMemory'):
            Logging.log('DB in memory')
            self.dbResources = TinyDB(storage=MemoryStorage)
            self.dbIdentifiers = TinyDB(storage=MemoryStorage)
            self.dbSubscriptions = TinyDB(storage=MemoryStorage)
            self.dbStatistics = TinyDB(storage=MemoryStorage)
            self.dbAppData = TinyDB(storage=MemoryStorage)
        else:
            Logging.log('DB in file system')
            self.dbResources = TinyDB(self.path + '/resources.json')
            self.dbIdentifiers = TinyDB(self.path + '/identifiers.json')
            self.dbSubscriptions = TinyDB(self.path + '/subscriptions.json')
            self.dbStatistics = TinyDB(self.path + '/statistics.json')
            self.dbAppData = TinyDB(self.path + '/appdata.json')
        self.tabResources = self.dbResources.table('resources',
                                                   cache_size=self.cacheSize)
        self.tabIdentifiers = self.dbIdentifiers.table(
            'identifiers', cache_size=self.cacheSize)
        self.tabSubscriptions = self.dbSubscriptions.table(
            'subsriptions', cache_size=self.cacheSize)
        self.tabStatistics = self.dbStatistics.table('statistics',
                                                     cache_size=self.cacheSize)
        self.tabAppData = self.dbAppData.table('appdata',
                                               cache_size=self.cacheSize)

    def closeDB(self):
        Logging.log('Closing DBs')
        self.dbResources.close()
        self.dbIdentifiers.close()
        self.dbSubscriptions.close()
        self.dbStatistics.close()
        self.dbAppData.close()

    def purgeDB(self):
        Logging.log('Purging DBs')
        self.tabResources.purge()
        self.tabIdentifiers.purge()
        self.tabSubscriptions.purge()
        self.tabStatistics.purge()
        self.tabAppData.purge()

    #
    #	Resources
    #

    def insertResource(self, resource):
        with self.lockResources:
            self.tabResources.insert(resource.json)

    def upsertResource(self, resource):
        with self.lockResources:
            self.tabResources.upsert(
                resource.json,
                Query().ri ==
                resource.ri)  # Update existing or insert new when overwriting

    def updateResource(self, resource):
        ri = resource.ri
        with self.lockResources:
            self.tabResources.update(resource.json, Query().ri == ri)
            # remove nullified fields from db and resource
            for k in list(resource.json):
                if resource.json[k] is None:
                    self.tabResources.update(delete(k), Query().ri == ri)
                    del resource.json[k]
        return resource

    def deleteResource(self, resource):
        with self.lockResources:
            self.tabResources.remove(Query().ri == resource.ri)

    def searchResources(self, ri=None, csi=None, srn=None, pi=None, ty=None):

        # find the ri first and then try again recursively
        if srn is not None:
            if len((identifiers := self.searchIdentifiers(srn=srn))) == 1:
                return self.searchResources(ri=identifiers[0]['ri'])
            return []

        with self.lockResources:
            if ri is not None:
                r = self.tabResources.search(Query().ri == ri)
            elif csi is not None:
                r = self.tabResources.search(Query().csi == csi)
            elif pi is not None and ty is not None:
                r = self.tabResources.search((Query().pi == pi)
                                             & (Query().ty == ty))
            elif pi is not None:
                r = self.tabResources.search(Query().pi == pi)
            elif ty is not None:
                r = self.tabResources.search(Query().ty == ty)
        return r
Exemplo n.º 36
0
 def save_tournament_in_db(self):
     db = TinyDB('database.json')
     table = db.table('tournaments')
     self.tournament.serialize()
     table.insert(self.tournament.params)
Exemplo n.º 37
0
import simplejson as json
from tinydb import TinyDB, Query, where

db = TinyDB('databases/database.db')

users = db.table('users')
todos = db.table('todos')

Users = Query()
Todos = Query()

# 여러가지 조회방법
# print(users.search(Users.id == 7))
# print(users.search(Users['id'] == 7))
# print(users.search(where('id') == 7))
# print(users.search(Query()['id'] == 7))
# print(users.search(where('address')['zipcode'] == '90566-7771'))
# print(users.search(where('address').zipcode == '90566-7771'))

# 고급 쿼리
# print(users.search(Users.email.exists()))   # 특정 키가 있는 row를 다 가져옴

# NOT
# print(users.search(~(Users.username == 'Antonette')))

# OR
# print(users.search( (Users.username == 'Antonette') | (Users.username == 'Kamren') ))

# And
# print(users.search( (Users.username == 'Antonette') & (Users.id == 2) ))
Exemplo n.º 38
0
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.history import FileHistory
from prompt_toolkit.styles import Style
from tinydb import Query, TinyDB, where
from tinydb.operations import delete
from art import text2art

style = Style.from_dict({'': 'gold'})

prompt_symbol = FormattedText([('gold bold', '<< tasker >> ')])

dirname = os.path.dirname(__file__)
datafile = os.path.join(dirname, 'db.json')
db = TinyDB(datafile)
task_table = db.table('tasks')
project_table = db.table('projects')
name_table = db.table('names')

number_of_concurrent_tasks = 3

max_line_length = 25

project_list = []

format_date_str = "%A, %d %b %Y %I:%M:%S %p"

format_delta_str_hours = "%d days %H:%M:%S"
# format_delta_str_days = "%d days %H:%M:%S"

filename_format = '%Y-%m-%d %I%M-%p'
Exemplo n.º 39
0
if (not path.exists(configPath)):
    with open(configPath, "w") as config:
        blankConfigFile = {
            "aws_access_key_id": "",
            "aws_secret_access_key": "",
            "aws_object_name": "",
            "token": "",
            "queue_channels": [],
            "report_channels": [],
            "leaderboard_channel": -1
        }
        json.dump(blankConfigFile, config)

db = TinyDB(dataPath, indent=2)
currQueue = db.table("queue")
activeMatches = db.table("activeMatches")
leaderboard = db.table("leaderboard")


def getDiscordToken() -> str:
    with open(configPath, "r") as config:
        token = json.load(config)["token"]

    return token


def updateDiscordToken(newToken: str) -> str:
    with open(configPath, "r") as configFile:
        config = json.load(configFile)
        config["token"] = newToken
Exemplo n.º 40
0
from asyspider.spider import Spider, Proxy, DBProxy, headers_raw_to_dict
import re
from pyquery import PyQuery as pq
import logging
from tools import log
from pprint import pprint as pp
from pprint import pformat
import argparse
import time
from urllib.parse import urljoin
from tinydb import TinyDB, Query
import os

db = TinyDB(os.path.join(os.path.dirname(__file__), './db.json'))
articles = db.table('articles')
query = Query()

logger = logging.getLogger('demo')
HOST = 'http://www.huntcoco.com/index.php'


class Crawler(Spider):
    status_code = (200, 301, 302)
    platform = 'desktop'
    max_tasks = 15
    sleep_time = None
    timeout = 30
    retries = 10
    check_crawled_urls = True
    update_cookies = True
Exemplo n.º 41
0
from sys import path

TEST_DIR = 'tests/test_db'
test_database = TinyDB(join(TEST_DIR, 'test_database.json'))

with open(join(TEST_DIR, 'data.py'), 'w') as file:
    # test_sample_url_can_be_generated
    data = {'dept': 'test_dept', 'course': 'test_course'}

    file.write(f"test_sample_url_can_be_generated_data = {data}\n")

    # test_get_one_dept
    data = {'dept': 'CS'}

    dept = test_database.table(f"{data['dept']}").all()

    file.write(f"test_get_one_dept_data = {dept}\n")

    # test_get_one_dept_and_course
    data = {'dept': 'CS', 'course': '2A'}

    dept = test_database.table(f"{data['dept']}").all()

    course = next(
        (e[f"{data['course']}"] for e in dept if f"{data['course']}" in e))

    file.write(f"test_get_one_dept_and_course_data = {course}\n")

    # test_get_two_dept
    data = {'courses': [{'dept': 'CS'}, {'dept': 'MATH'}]}
Exemplo n.º 42
0
 def load_tournament_from_db(self):
     db = TinyDB('database.json')
     table = db.table('tournaments')
     for item in table:
         self.create_tournament(item)
Exemplo n.º 43
0
def del_vdus_settings_from_vnfr(vnfi_id):
    db = TinyDB('data/preins.json')
    settbl = db.table('settings_vdus_table')
    q = Query()
    settbl.remove(q.vnfi_id == vnfi_id)
    return
from utils import Conf
from tinydb import TinyDB
from tinydb import where
import argparse
import shutil
import os
# construct the argument parser and parse the arguments
ids = input("Input the id to be deleted")
config_file = "D:/Attendance Management System/config/config.json"
conf = Conf(config_file)
db = TinyDB(conf["db_path"])
studentTable = db.table("student")
student = studentTable.search(where(args["id"]))
student[0][args["id"]][1] = "unenrolled"
studentTable.write_back(student)

# delete the student's data from the dataset
shutil.rmtree(os.path.join(conf["dataset_path"], conf["class"], ids))
print(
    "[INFO] Please extract the embeddings and re-train the face recognition model..."
)
db.close()
Exemplo n.º 45
0
def delete_vnfcis_settings(vnfi_id):
    db = TinyDB('data/preins.json')
    tbl = db.table('settings_vdus_table')
    q = Query()
    tbl.remove(q.vnfi_id == vnfi_id)
Exemplo n.º 46
0
import simplejson as json
from tinydb.storages import MemoryStorage
from tinydb import TinyDB, Query, where

db = TinyDB('g:/python/section2/python_practice/files/database.db')

# db = TinyDB(storage = MemoryStorage, default_table ='users')

users = db.table("users")
todos = db.table("todos")

Users = Query()
Todos = Query()

print(users.search(Users.id == 7))

print(users.search(where('address')['zipcode'] == '90566-7771'))
print(users.search(where('address').zipcode == '90566-7771'))
# users.insert({'name' : 'kim', 'email' : '*****@*****.**'})
# todos.insert({'name' : 'homework', 'complete' : False})

# with open('g:/python/section2/python_practice/files/users.json', 'r') as infile:
#     r = json.loads(infile.read())
#     for u in r:
#         users.insert(u)

# users.purge()
# todos.purge()
Exemplo n.º 47
0
from tinydb import TinyDB, Query

from config import base
base = base.base()
db_path = base['db_path']
db = TinyDB(db_path)
white_list = db.table('white_list', cache_size=0)
in_white = Query()


def add_to_white(id, amis_peer_id, name):
    try:
        if (white_list.search(in_white.id == str(id)) != []):  # 有在裡面就算了
            return True
        white_list.insert({
            'id': str(id),
            'amis_peer_id': str(amis_peer_id),
            'name': str(name)
        })
        return True
    except Exception as e:
        raise e


def rm_from_white(id):
    try:
        if (white_list.search(in_white.id == str(id)) == []):  # 不在裡面就算了
            return True
        white_list.remove(in_white.id == str(id))
        return True
    except Exception as e:
Exemplo n.º 48
0
 def __init__(self):
     db = TinyDB(CODE_DB_FILE)
     self.code = db.table('Code')
root = '/home/lml/smart_city/StreetscapeLocation/'
query = osp.join(root, 'query/query_sencex/*.png')
db_path = '/home/lml/smart_city/StreetscapeLocation/datasets_db/datasetx.db'
search_list = []
value_list = []
for n in range(1, 9):
    s = list(query)
    s[query.rfind("/") - 1] = str(n)
    query = ''.join(s)
    s = list(db_path)
    s[-4] = str(n)
    db_path = ''.join(s)
    print(db_path)
    for img_path in glob.glob(query):
        db = TinyDB(db_path)
        table = db.table('feature')
        cache_features = table.all()[0]
        match_result = image_search(img_path)
        print(match_result)

        query_name = img_path[img_path.rfind("/") + 1:img_path.rfind(".")]
        row1 = [
            query_name, match_result[0][0], match_result[1][0],
            match_result[2][0], match_result[3][0], match_result[4][0],
            match_result[5][0]
        ]
        row2 = [
            query_name, match_result[0][1], match_result[1][1],
            match_result[2][1], match_result[3][1], match_result[4][1],
            match_result[5][1]
        ]
Exemplo n.º 50
0
 def __init__(self):
     db = TinyDB(GS_DB_FILE)
     self.gs_config = db.table('GS_Config')
Exemplo n.º 51
0
from spideradmin.api_app.scrapyd_utils import get_server_status, cancel_all_spider, check_server
from spideradmin.version import VERSION
from puremysql import PureMysql

from tinydb import TinyDB, Query

sys.path.insert(0, os.getcwd())

try:
    import config
except Exception as e:
    from spideradmin import default_config as config

api_app = Blueprint(name="api", import_name=__name__)
db = TinyDB("server.db")
user_server_table = db.table("user_servers")
query = Query()


# 初始化配置文件中的服务器插入db.sqlit
def init_servers():
    if len(user_server_table.all()) == 0:
        # 初始化配置文件中配置的服务器
        [
            user_server_table.insert({
                "server_name": server_name,
                "server_host": server_host
            }) for server_name, server_host in config.SCRAPYD_SERVERS
            if (not all([server_name, server_host])) or (
                check_server(server_host) is False)
        ]
Exemplo n.º 52
0
from tinydb import TinyDB, Query, operations, where

db = TinyDB('./data/db.json')
db_tags = db.table('tags')
db_admin_roles = db.table('admin_roles')
db_user_roles = db.table('user_roles')
db_user_data = db.table('user_data')
db_voice_text = db.table('voice_text')
db_role_data = db.table('role_data')


# functions for tag db
def get_tag(name):
    Tag = Query()
    tags = db_tags.search(Tag.name == name)
    if len(tags) > 0:
        return tags[0]
    else:
        return None


def exists_tag(name):
    tag = get_tag(name)
    return tag is not None


def get_all_tags():
    return db_tags.all()


def add_tag(name, response):
Exemplo n.º 53
0
import os
import json
import falcon
from tinydb import TinyDB, Query

#変数dbにjsonの場所を指定
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DB_PLACE = os.path.join(BASE_DIR, 'db.json')
db = TinyDB(DB_PLACE)
table = db.table('subway')

Data = Query()
msg = []
items = {}


class Search():
    '''パラメータを取得し、その条件を満たしたデータだけを返す'''
    def on_get(self, req, resp):
        # パラメータを取得
        name = req.get_param('name')
        types = req.get_param('type')

        # nameパラメータに値が入っているのかを確認
        if name:
            if types:
                # name,typeパラメータ共に値が存在するとき
                # データをDBから取得
                get_data = table.search((Data.name == name)
                                        & (Data.type == types))
Exemplo n.º 54
0
def pre_instances():
    db = TinyDB('data/preins.json')
    nstbl = db.table('ns_table')
    nss = nstbl.all()
    return render_template('vnf-records.html', site_name=site_name, nss=nss)
Exemplo n.º 55
0
import subprocess
import sys
import io
import datetime

# database
from tinydb import TinyDB
top_database = TinyDB('data/live_mapping.json')
database = top_database.table('data')

from kalman_filter.sensor.nonmetric_map import NonMetricMap
nmap = NonMetricMap(top_database)

from kalman_filter.sensor import SensorData


def schema_to_db(time: 'datetime.datetime', data):
    return {
        'time': {
            'year': time.year,
            'month': time.month,
            'day': time.day,
            'hour': time.hour,
            'minute': time.minute,
            'second': time.second,
            'microsecond': time.microsecond
        },
        'data': data
    }

Exemplo n.º 56
0
class SchedulerBot(discord.Client):
    def __init__(self, discord_token):
        super(SchedulerBot, self).__init__()

        self.discord_token = discord_token

        # Represents very small database inside a json file using TinyDB
        self.db = TinyDB("db.json")

        # Represents all available commands and how to use them.
        # @TODO: Make command classes?!
        #!schedule "Hearthstone Tourney 4" 2017-06-07 7:30PM PST "Bring your best decks!"
        self.commands = {
            "!schedule": {
                "examples": [
                    "!schedule \"Game Night\" 2017-06-01 05:30PM PST \"Bring your own beer.\""
                ]
            },
            "!reply": {
                "examples": ["!reply \"Game Night\" yes"]
            },
            "!events": {
                "examples": ["!events 2017-06-01"]
            },
            "!scheduler-bot": {
                "examples": ["!scheduler-bot"]
            },
            "!delete-event": {
                "examples": ["!delete-event \"Game Night\""]
            },
            "!edit-event": {
                "examples":
                ["!edit-event \"Game Night\" date 2017-06-06 time 5:30PM"]
            }
            #,
            #"!remind":{
            #	"examples": ["!remind \"Game Night\" 30 minutes"]
            #}
        }

    @asyncio.coroutine
    def check_for_reminders(self, task_name, seconds_to_sleep=3):
        while True:
            yield from asyncio.sleep(seconds_to_sleep)
            now = datetime.now().strftime("%Y-%m-%d %I:%M:%p")
            reminders = self.get_data("Reminder", "reminder_datetime", now)
            if len(reminders) > 0:
                yield from self.handle_reminders(reminders)

    def main(self):
        pass
        # @TODO: Commenting this out for reminders.
        # loop = asyncio.get_event_loop()
        # try:
        # asyncio.async(self.check_for_reminders('reminder_task',1))
        # asyncio.async(self.run())
        # loop.run_forever()
        # except KeyboardInterrupt:
        # pass
        # finally:
        # print('step: loop.close()')
        # loop.close()

    def run(self):
        print("superclass being called...")
        # Calling superclass to do discord.Client's run.
        super(SchedulerBot, self).run(self.discord_token)

    # Discord client function that is called when the bot has logged into the registered server.
    @asyncio.coroutine
    def on_ready(self):
        print('Logged in as')
        print(self.user.name)
        print(self.user.id)
        print('------')

    # Bot function that handles the reminders of a certain time and alerts all attendies.
    @asyncio.coroutine
    def handle_reminders(self, reminders):
        names = [reminder["attendie"] for reminder in reminders]
        reminders_by_name = {}
        for reminder in reminders:
            if reminder["attendie"] not in reminders_by_name:
                reminders_by_name[reminder["attendie"]] = []
            reminders_by_name[reminder["attendie"]].append(reminder)

        print("Handling unique reminders for the following events: {}".format(
            set([reminder["event_name"] for reminder in reminders])))
        for i in self.get_all_members():
            if i.name in names:
                user = i
                for reminder in reminders_by_name[i.name]:
                    event_date = self.get_data(
                        table_name="Event",
                        field="name",
                        field_value=reminder["event_name"])[0]["date"]
                    event_time = self.get_data(
                        table_name="Event",
                        field="name",
                        field_value=reminder["event_name"])[0]["time"]
                    event_timezone = self.get_data(
                        table_name="Event",
                        field="name",
                        field_value=reminder["event_name"])[0]["timezone"]
                    yield from self.send_message(
                        user,
                        'Reminding you that {} starts at {} {} {}.'.format(
                            reminder["event_name"], event_date, event_time,
                            event_timezone))
                self.delete_reminder(reminders_by_name[i.name])
                break

    # Delete reminder from db.
    def delete_reminder(self, reminders):
        for reminder in reminders:
            self.db.table("Reminder").remove(eids=[reminder.eid])
        #self.db.table("Reminder").update(delete('eid'), where('eid') == reminder["eid"])
        #self.db.update(delete(), User.name == 'John')

    # Helper function that handles tokens captured within quotations.
    # If there are tokens captured in two quotes, it will treat said tokens as a single token.
    def handle_quotations(self, tokens):
        new_tokens = []

        phrase_found = False
        phrase = ""

        for token in tokens:
            if "\"" in token:
                token_ = token.strip("\"")
                phrase += (token_ + " ")
                if phrase_found or token.count("\"") == 2:
                    new_tokens.append(phrase.strip())
                    phrase = " "
                    phrase_found = False
                else:
                    phrase_found = True
            else:
                if phrase_found:
                    phrase += (token + " ")
                else:
                    new_tokens.append(token.strip())
        return new_tokens

    # Database helper function that strictly pulls events from the database.
    # Pulls events based on desired fields.
    # @param: field="date", field_value="2017-01-06"
    # @TODO: Allow this function to query multiple parameters at once
    # @TODO: Allow this function to look at differ
    def get_data(self, table_name, field=None, field_value=None):
        table = self.db.table(table_name)
        if field:
            all_results = table.search(Query()[field] == field_value)
        else:
            all_results = table.all()
        return all_results

    # Database helper function that gets all the field names from a given table.
    def get_field_names(self, table_name):
        table = self.db.table(table_name)
        all_keys = table.all()[0].keys()
        return all_keys

    # Bot function that creates an event in the database.
    def create_event(self, event_name, event_date, event_time, event_timezone,
                     event_description, event_author):
        table = self.db.table('Event')

        # Checks if the event has already been created.
        # If it has, don't override the event.
        if table.search(Query().name == event_name):
            return "Event {} already created. Cannot override this event.".format(
                event_name)

        # Calculates the date, time, and timezone that the event was created.
        # This helps with logging purposes.
        now_date = time.strftime("%Y-%m-%d")
        now_time = time.strftime("%I:%M%p")
        now_tz = time.tzname[0]
        now_tz_tokens = now_tz.split(" ")
        if len(now_tz_tokens) > 1:
            now_tz = "".join([token[0] for token in now_tz_tokens])

        # Create the dictionary that represents the record in the event table.
        event_record = {
            'name': event_name,
            'date': event_date,
            'time': event_time,
            'timezone': event_timezone,
            'description': event_description,
            'author': event_author,
            'created_date': now_date,
            'created_time': now_time,
            'created_timezone': now_tz
        }

        # Try to insert the record into the table.
        try:
            table.insert(event_record)
            return "{} event successfully recorded. Others may now reply to this event.".format(
                event_name)
        except:
            return "Cannot insert record into the Event table."

    # Bot function that edits an event that has already been created.
    # @format field_values: {"name": "event1", "date": 2017-01-01}
    def edit_event(self, event_name, reply_author, field_values):
        table = self.db.table("Event")

        response = ""

        if table.search(Query().name == event_name):
            if table.search((Query().author == reply_author)
                            & (Query().name == event_name)):
                table.update(field_values, ((Query().author == reply_author) &
                                            (Query().name == event_name)))
                # If date, time, or timezone changed in the event, delete the reminder and make a new one with the updated times.
                #if set(["date","time","timezone"]).intersection(set(field_values.keys())):
                #reminder_data = [(reminder_record['attendie'], reminder_record['time_metric'], reminder_record['diff_value']) for reminder_record in self.get_data('Reminder', 'event_name', event_name)]
                # print("Editing event, reminder_data: {}".format(reminder_data))
                # self.delete_reminders_by_event_name(event_name, reply_author)
                #for reminder in reminder_data:
                # self.create_reminder(event_name, reminder[0], reminder[1], reminder[2])
                response += "Event table has been edited with new values: {}".format(
                    field_values)
            else:
                response += "You do not have permission to edit this event."
        else:
            response += "Event {} does not exist.".format(event_name)

        return response

    # Database function that creates a reminder in the database.
    def create_reminder(self, event_name, attendie, time_metric, diff_value):
        event_data = self.get_data('Event', 'name', event_name)[0]
        event_time = event_data['time']
        event_date = event_data['date']

        reminder_table = self.db.table('Reminder')

        reply_data = [
            reply_record for reply_record in self.get_data('Reply')
            if reply_record['event_name'] == event_name
            and reply_record['author'] == attendie
            and reply_record['status'] == 'yes'
        ]
        if len(reply_data) <= 0:
            return "Attendie did not reply yes to the event."
        if time_metric not in ("minutes", "hours", "days"):
            return "Invalid time metric."
        print("event_date: {}, event_time: {}".format(event_date, event_time))
        event_dt = datetime.strptime(" ".join((event_date, event_time)),
                                     '%Y-%m-%d %I:%M%p')

        if time_metric == "minutes":
            reminder_dt = event_dt - timedelta(minutes=diff_value)
        elif time_metric == "hours":
            reminder_dt = event_dt - timedelta(hours=diff_value)
        elif time_metric == "days":
            reminder_dt = event_dt - timedelta(days=diff_value)

        reminder_dt = reminder_dt.strftime("%Y-%m-%d %I:%M:%p")
        print("reminder_dt: {}".format(reminder_dt))

        reply_data = self.get_data('Reply', 'event_name', event_name)
        print("attendie: {}".format(attendie))
        reminder_record = {
            "event_name": event_name,
            "attendie": attendie,
            "reminder_datetime": reminder_dt,
            "time_metric": time_metric,
            "diff_value": diff_value,
            "is_sent": False
        }
        try:
            reminder_table.insert(reminder_record)
        except:
            return "Reminder not recorded into db. Check connection."

        return "Reminder set. You'll be alerted {} {} before {} begins.".format(
            diff_value, time_metric, event_name)

        # @TODO finish reminder stuff right here.

    # Bot function that creates a reply [to an event] in the database.
    def create_reply(self, event_name, reply_status, reply_author):
        reply_table = self.db.table('Reply')

        # Checks if the event has been created.
        # If it hasn't been created, there is no event to reply to.
        event_table = self.db.table('Event')
        if not event_table.search(Query().name == event_name):
            return "This event hasn\'t been scheduled yet."

        # Checks if the user has already submitted a reply to this event.
        # If they have already replied, the reply is overwritten and the user is notified of it's updated value.
        if reply_table.search((Query().author == reply_author)
                              & (Query().event_name == event_name)):
            reply_table.update({'status': reply_status},
                               ((Query().author == reply_author) &
                                (Query().event_name == event_name)))
            #if reply_status == "yes":
            # print(self.create_reminder(event_name, reply_author, "hours", 1))
            #print(self.create_reminder(event_name, reply_author, "days", 1))
            return "Your old reply has been updated to {}.".format(
                reply_status)

        # Calculates the date, time, and timezone that the event was created.
        # This helps with logging purposes.
        now_date = time.strftime("%Y-%m-%d")
        now_time = time.strftime("%I:%M %p")
        now_tz = time.tzname[0]

        # Create the dictionary that represents the record in the reply table.
        reply_record = {
            'event_name': event_name,
            'status': reply_status,
            'author': reply_author,
            'created_date': now_date,
            'created_time': now_time,
            'created_timezone': now_tz
        }

        # Try to insert the record into the table.
        try:
            reply_table.insert(reply_record)
            #if reply_status == "yes":
            # Create error handling for this. @TODO
            # print(self.create_reminder(event_name, reply_author, "hours", 1))
            #	print(self.create_reminder(event_name, reply_author, "days", 1))
            return "Your reply has been successfully recorded."
        except:
            return "Cannot insert record into the Reply table."

    # Helper function that determines whether or not a string is of the correct date format.
    # @param: date i.e. "2017-06-01"
    def is_date(self, date):
        # Try to convert the date into a struct_time. If it works, it's of a correct format.
        try:
            format_correct = isinstance(time.strptime(date, "%Y-%m-%d"),
                                        time.struct_time)
            return format_correct
        except:
            return False

    # Helper function that determines whether or not a string is of the correct time format.
    # @param: time_str i.e. "5:30PM"
    def is_time(self, time_str):
        time_str = time_str.lower()
        time_period = time_str.split(":")
        time_ = (":".join(time_period[:2]))[:-2]
        period = time_str[-2:]

        # Try to convert the date into a struct_time. If it works, it's of a correct format.
        try:
            time_correct = isinstance(time.strptime(time_, '%H:%M'),
                                      time.struct_time)
            period_correct = period in ("am", "pm")
            return period_correct
        except:
            return False

    # Helper function that determines whether or not a string is a valid time zone abbreviation.
    def is_timezone(self, tz_str):
        known_timezones = [
            "ACDT", "ACST", "ACT", "ACT", "ADT", "AEDT", "AEST", "AFT", "AKDT",
            "AKST", "AMST", "AMT", "AMT", "ART", "AST", "AST", "AWST", "AZOST",
            "AZOT", "AZT", "BDT", "BIOT", "BIT", "BOT", "BRST", "BRT", "BST",
            "BST", "BST", "BTT", "CAT", "CCT", "CDT", "CDT", "CEST", "CET",
            "CHADT", "CHAST", "CHOT", "CHOST", "CHST", "CHUT", "CIST", "CIT",
            "CKT", "CLST", "CLT", "COST", "COT", "CST", "CST", "ACST", "ACDT",
            "CST", "CT", "CVT", "CWST", "CXT", "DAVT", "DDUT", "DFT", "EASST",
            "EAST", "EAT", "ECT", "ECT", "EDT", "AEDT", "EEST", "EET", "EGST",
            "EGT", "EIT", "EST", "AEST", "FET", "FJT", "FKST", "FKT", "FNT",
            "GALT", "GAMT", "GET", "GFT", "GILT", "GIT", "GMT", "GST", "GST",
            "GYT", "HADT", "HAEC", "HAST", "HKT", "HMT", "HOVST", "HOVT",
            "ICT", "IDT", "IOT", "IRDT", "IRKT", "IRST", "IST", "IST", "IST",
            "JST", "KGT", "KOST", "KRAT", "KST", "LHST", "LHST", "LINT",
            "MAGT", "MART", "MAWT", "MDT", "MET", "MEST", "MHT", "MIST", "MIT",
            "MMT", "MSK", "MST", "MST", "MUT", "MVT", "MYT", "NCT", "NDT",
            "NFT", "NPT", "NST", "NT", "NUT", "NZDT", "NZST", "OMST", "ORAT",
            "PDT", "PET", "PETT", "PGT", "PHOT", "PHT", "PKT", "PMDT", "PMST",
            "PONT", "PST", "PST", "PYST", "PYT", "RET", "ROTT", "SAKT", "SAMT",
            "SAST", "SBT", "SCT", "SGT", "SLST", "SRET", "SRT", "SST", "SST",
            "SYOT", "TAHT", "THA", "TFT", "TJT", "TKT", "TLT", "TMT", "TRT",
            "TOT", "TVT", "ULAST", "ULAT", "USZ1", "UTC", "UYST", "UYT", "UZT",
            "VET", "VLAT", "VOLT", "VOST", "VUT", "WAKT", "WAST", "WAT",
            "WEST", "WET", "WIT", "WST", "YAKT", "YEKT"
        ]

        return (tz_str in known_timezones)

    # Helper function that determines whether or not a string has a digit.
    # This is used as quick hack to look for  strings that may have dates.
    def has_digit(self, input_str):
        return any(char.isdigit() for char in input_str)

    # Helper function that determines whether or not an event exists.
    def event_exists(self, event_name):
        table = self.db.table("Event")
        return len(table.search(Query().name == event_name)) > 0

    # String formatter function that determines how the events are displayed in the Discord client.
    def format_events(self, events):
        events_str = "**EVENTS**\n"
        events_str += "```{:12} {:25} {:10} {:6} {:8}\n".format(
            "Host", "Name", "Date", "Time", "Timezone")

        # Sort the events by date.
        date_string = '2009-11-29 03:17 PM'
        events_by_datetime = {
            event["name"]:
            (datetime.strptime(event["date"] + " " + event["time"],
                               '%Y-%m-%d %I:%M%p'))
            for event in events
        }
        sorted_events = sorted(events_by_datetime.items(),
                               key=lambda p: p[1],
                               reverse=False)
        print("events_sorted_by_datetime: {}".format(sorted_events))

        for target_event in sorted_events:
            for event in events:
                if target_event[0] == event["name"]:
                    author = event["author"]
                    name = event["name"]
                    date = event["date"]
                    time = event["time"]
                    timezone = event["timezone"]

                    events_str += "{:12} {:25} {:10} {:6} {:8}\n".format(
                        author, name if len(name) < 25 else name[:22] + "...",
                        date, time, timezone)
                    break

        events_str += "```"

        # Just for debugging purposes.
        print("events_str: {}".format(events_str))

        return events_str

    # String formatter function that determines how single events are displayed in the Discord client.
    def format_single_event(self, event, replies):
        event_str = "**{}**".format(event["name"])
        event_str += "```{:12} {:15} {:10} {:6} {:8}\n".format(
            "Host", "Name", "Date", "Time", "Timezone")

        author = event["author"]
        name = event["name"]
        date = event["date"]
        time = event["time"]
        timezone = event["timezone"]
        desc = event["description"]

        event_str += "{:12} {:15} {:10} {:6} {:8}\n\n{}\n\n".format(
            author, name[:15], date, time, timezone, desc)

        reply_statuses = {"yes": [], "no": [], "maybe": []}

        for reply in replies:
            user = reply["author"]
            status = reply["status"]
            reply_statuses[status].append(user)

        event_str += "Yes: {}\n".format(", ".join(reply_statuses["yes"]))
        event_str += "No: {}\n".format(", ".join(reply_statuses["no"]))
        event_str += "Maybe: {}".format(", ".join(reply_statuses["maybe"]))

        event_str += "```"

        return event_str

    # Bot function that deletes certain reminders from the database based on the event name.
    def delete_reminders_by_event_name(self, event_name, author):
        event_table = self.db.table("Event")

        if not event_table.search(Query().name == event_name):
            return "Event {} not in the table.".format(event_name)
        elif not event_table.search((Query().author == author)
                                    & (Query().name == event_name)):
            return "You do not have permission to delete this event."

        reminder_table = self.db.table("Reminder")

        # Remove all reminders from the reminder table with that event name.
        try:
            reminder_table.remove(Query().event_name == event_name)
        except:
            return "Cannot connect to Reminder table."

        return "Reminders successfully deleted for event {}.".format(
            event_name)

    # Bot function that deletes a certain event from the database.
    def delete_event(self, event_name, reply_author):
        event_table = self.db.table("Event")
        reply_table = self.db.table("Reply")
        #reminder_table = self.db.table("Reminder")

        if not event_table.search(Query().name == event_name):
            return "Event {} not in the table.".format(event_name)
        elif not event_table.search((Query().author == reply_author)
                                    & (Query().name == event_name)):
            return "You do not have permission to delete this event."

        # Remove event from event table.
        try:
            event_table.remove(Query().name == event_name)
        except:
            return "Cannot connect to the Event table."

        # Remove all replies from the reply table with that event name.
        try:
            reply_table.remove(Query().event_name == event_name)
        except:
            return "Cannot connect to the Reply table."

        # Remove all reminders from the reminder table with that event name.
        # try:
        # reminder_table.remove(Query().event_name == event_name)
        #except:
        #return "Cannot connect to Reminder table."

        return "Event successfully deleted."

    # Discord client function that determines how to handle a new message when it appears on the Discord server.
    @asyncio.coroutine
    def on_message(self, message):
        tokens = message.content.split(' ')
        tokens = [str(token) for token in tokens]
        bot_command = tokens[0].lower()
        # !schedule command.
        if bot_command == '!schedule':
            tokens = tokens[1:]

            if len(tokens):
                tokens = self.handle_quotations(tokens)
                if len(tokens) > 5:
                    create_event_response = "Invalid input: too many parameters."
                else:
                    event_name = tokens[0].strip()
                    event_date = tokens[1]
                    event_time = tokens[2]
                    event_timezone = tokens[3]
                    event_description = tokens[4].strip()
                    event_author = message.author.name

                    # Setup input rules to check inputs.
                    date_rule = InputRule(
                        self.is_date,
                        "Invalid date format. Use: YYYY-MM-DD i.e. 2017-01-01")
                    time_rule = InputRule(
                        self.is_time,
                        "Invalid time format. Use: HH:MMPP i.e. 07:58PM")

                    if date_rule.passes(event_date) is False:
                        create_event_response = date_rule.fail_msg
                    elif time_rule.passes(event_time) is False:
                        create_event_response = time_rule.fail_msg
                    else:
                        create_event_response = self.create_event(
                            event_name, event_date, event_time, event_timezone,
                            event_description, event_author)
            else:
                create_event_response = "Invalid input: not enough inputs."

            yield from self.send_message(message.channel,
                                         create_event_response)

        # !reply command.
        elif bot_command == '!reply':
            #	print("message.author: {}, type(message.author): {}".format(message.author, type(message.author)))
            tokens = tokens[1:]
            if len(tokens) >= 2:
                tokens = self.handle_quotations(tokens)

                if len(tokens) == 2:
                    event_name = tokens[0].strip()
                    reply_status = tokens[1].strip().lower()
                    reply_author = message.author.name

                    event_created_rule = InputRule(
                        self.event_exists,
                        "Invalid input. Event not yet created.")
                    reply_rule = InputRule(
                        lambda v1: (v1 in ("yes", "no", "maybe")),
                        "Invalid input. Use: yes, no, or maybe.")
                    if not event_created_rule.passes(event_name):
                        create_reply_response = event_created_rule.fail_msg
                    elif not reply_rule.passes(reply_status):
                        create_reply_response = reply_rule.fail_msg
                    else:
                        create_reply_response = self.create_reply(
                            event_name, reply_status, reply_author)
                else:
                    create_reply_response = "Invalid input: Too many parameters."
            else:
                create_reply_response = "Invalid input: Not enough inputs. Provide event name and reply status."

            yield from self.send_message(message.channel,
                                         create_reply_response)

        # !events command.
        elif bot_command == '!events':
            tokens = tokens[1:]
            if tokens:
                if len(tokens) == 1:
                    date = tokens[0].lower()

                    # Setup input rules to check inputs.
                    date_rule = InputRule(
                        self.is_date,
                        "Invalid date format. Use: YYYY-MM-DD i.e. 2017-01-01")
                    today_tomorrow_rule = InputRule(
                        lambda x: x.lower() in ("today", "tomorrow"),
                        "Invalid day format. Use: today or tomorrow.")

                    if self.has_digit(date):
                        if not date_rule.passes(date):
                            create_events_response = date_rule.fail_msg
                        else:
                            all_events_str = self.format_events(
                                self.get_data("Event", "date", date))
                            create_events_response = all_events_str
                    else:
                        if not today_tomorrow_rule.passes(date):
                            create_events_response = today_tomorrow_rule.fail_msg
                        else:
                            date_ = datetime.now().strftime(
                                "%Y-%m-%d") if date == "today" else (
                                    datetime.now() +
                                    timedelta(days=1)).strftime("%Y-%m-%d")
                            all_events_str = self.format_events(
                                self.get_data("Event", "date", date_))
                            create_events_response = all_events_str
                else:
                    create_events_response = "Invalid input: Too many parameters."
            else:
                create_events_response = self.format_events(
                    self.get_data("Event"))

            yield from self.send_message(message.channel,
                                         create_events_response)

        elif bot_command == '!event':
            tokens = tokens[1:]
            if tokens:
                tokens = self.handle_quotations(tokens)

                if len(tokens) == 1:
                    event_name = tokens[0]
                    all_events = self.get_data("Event", "name", event_name)
                    all_replies = self.get_data("Reply", "event_name",
                                                event_name)

                    if len(all_events) > 0:
                        event_response = self.format_single_event(
                            all_events[0], all_replies)
                    else:
                        event_response = "Invalid input: event not yet created."
                else:
                    event_response = "Invalid input: Too many parameters."
            else:
                event_response = "Invalid input: no event name."

            yield from self.send_message(message.channel, event_response)

        # !scheduler-bot command. (list commands)
        elif bot_command == '!scheduler-bot' and len(tokens) == 1:
            list_commands_response = "**COMMANDS**\n```"
            for command in list(self.commands.keys()):
                list_commands_response += "{}: \n\t {}\n\n".format(
                    command, self.commands[command]["examples"][0])
            list_commands_response += "```"

            yield from self.send_message(message.channel,
                                         list_commands_response)

        # !delete-event command.
        elif bot_command == '!delete-event':
            tokens = tokens[1:]
            if tokens:
                tokens = self.handle_quotations(tokens)

                if len(tokens) == 1:
                    event_name = tokens[0]
                    reply_author = message.author.name
                    all_events = self.get_data("Event", "name", event_name)

                    if len(all_events) > 0:
                        delete_event_response = self.delete_event(
                            event_name, reply_author)
                    else:
                        delete_event_response = "Invalid input: event not yet created."
                else:
                    delete_event_response = "Invalid input: Too many parameters."
            else:
                delete_event_response = "Invalid input: no event name."

            yield from self.send_message(message.channel,
                                         delete_event_response)

        # !remind command.
        # !remind OverwatchNight 2 days
        elif bot_command == "!remind":
            tokens = tokens[1:]
            if tokens:
                if len(tokens) < 3:
                    remind_response = "Invalid input: missing arguments."
                else:
                    tokens = self.handle_quotations(tokens)
                    event_table = self.db.table('Event')
                    reply_table = self.db.table('Reply')
                    if not event_table.search(Query().name == tokens[0]):
                        remind_response = "Invalid input: This event hasn\'t been scheduled yet."
                    elif not tokens[1].isdigit():
                        remind_response = "Invalid input: bad numeric."
                    elif not tokens[2] in ["minutes", "hours", "days"]:
                        remind_response = "Invalid input: did not use 'minutes','hours', or 'days'."
                    elif not reply_table.search(
                        (Query().author == message.author.name)
                            & (Query().event_name == tokens[0])):
                        remind_response = "Invalid input: User has not replied 'yes' to the event."
                    else:
                        remind_response = self.create_reminder(
                            tokens[0], message.author.name, tokens[2],
                            int(tokens[1]))
            else:
                remind_response = "Invalid input: no event name."

            yield from self.send_message(message.channel, remind_response)

        # !edit-event command.
        # !edit OverwatchNight date 1/6/17 time 5:30PM
        # @TODO: InputRule for time and timezone.
        elif bot_command == '!edit-event':
            tokens = tokens[1:]
            if tokens:
                tokens = self.handle_quotations(tokens)
                event_name = tokens[0]
                event_author = message.author.name

                if len(tokens) <= 1:
                    edit_event_response = "No fields given to edit."
                else:
                    tokens = tokens[1:]

                    if len(tokens) % 2 == 0:
                        is_event_field_rule = InputRule(
                            lambda x: x.lower() in self.get_field_names(
                                "Event"), "Field does not exist.")
                        date_rule = InputRule(
                            self.is_date,
                            "Invalid date format. Use: YYYY-MM-DD i.e. 2017-01-01"
                        )
                        time_rule = InputRule(
                            self.is_time,
                            "Invalid time format. Use: HH:MMPP i.e. 07:58PM")
                        timezone_rule = InputRule(
                            self.is_timezone, "Invalid timezone abbreviation.")

                        field_values = {}
                        rule_fail = False
                        for p in range(0, len(tokens), 2):
                            if not is_event_field_rule.passes(tokens[p]):
                                return is_event_field_rule.fail_msg
                            else:
                                # @TODO Rulechecker would fix this awful redundancy.
                                if tokens[p] == "date":
                                    if not date_rule.passes(tokens[p + 1]):
                                        edit_event_response = date_rule.fail_msg
                                        rule_fail = True
                                        break
                                elif tokens[p] == "time":
                                    if not time_rule.passes(tokens[p + 1]):
                                        edit_event_response = time_rule.fail_msg
                                        rule_fail = True
                                        break
                                elif tokens[p] == "timezone":
                                    if not timezone_rule.passes(tokens[p + 1]):
                                        edit_event_response = timezone_rule.fail_msg
                                        rule_fail = True
                                        break

                                field_values[tokens[p]] = tokens[p + 1]

                        if not rule_fail:
                            edit_event_response = self.edit_event(
                                event_name, event_author, field_values)
                    else:
                        edit_event_response = "Invalid input: incorrect number of parameters."
            else:
                edit_event_response = "Invalid input: no event name."

            yield from self.send_message(message.channel, edit_event_response)
 def __init__(self):
     tiny_db = TinyDB(Config.database_file)
     self._task_cache = tiny_db.table('tasks_cache')
     self._log = logging.getLogger(self.__class__.__name__)
Exemplo n.º 58
0
# TinyDBを使うためライブラリのインポート
from tinydb import TinyDB, Query

# データベースに接続
filepath = 'test-tynydb.json'
db = TinyDB(filepath)

# 既存のデータがあれば破棄
#db.purge_table('fruits')

# テーブルを得る
table = db.table('fruits')

# データをデータベースに挿入
table.insert({'name': 'Banana', 'price': 600})
table.insert({'name': 'Orange', 'price': 1200})
table.insert({'name': 'Mango', 'price': 840})

# 全データを抽出して表示
print(table.all())

# 特定のデータを抽出して表示
# Orangeを検索
Item = Query()
res = table.search(Item.name == 'Orange')
print('Orange is', res[0]['price'])

# 値段が800円以上のものを抽出
print('800円以上のもの:')
res = table.search(Item.price >= 800)
for it in res:
Exemplo n.º 59
0
from tinydb import TinyDB, Query
import arrow

reprapDB = TinyDB('reprap.json')
factoids = reprapDB.table('factoids')
#{"id":3027,"item":"vodka","are":0,"value":"what comes out of russia's tap","nick":"eogra7_","dateset":"2014-01-01 02:43:41","locked":null,"lastsync":null}
factoids_history = reprapDB.table('factoids_history')
seen = reprapDB.table('seen')
tell = reprapDB.table('tell')



Nick = Query()
x = None

#{"id":3030,"item":"JATMN","are":0,"value":"JAT.MN","nick":"JATMN","dateset":"2014-01-01 02:46:48","locked":null,"lastsync":null}
count = factoids_history.count(Nick.item == x)
namelist = factoids.search(Nick.nick == x)

# for name in namelist:
#     name["dateset"] = arrow.get(name["dateset"]).timestamp
# sorted_facts = sorted(namelist, key=lambda k: k["dateset"], reverse=True)


try:
    # print ('count: {}'.format(count))
    b = tell.all()
    print(len(b))
except:
    pass
#print(factoids.get(eid=2674))
Exemplo n.º 60
0
def open():
    global db, pipes
    db = TinyDB(DB)
    pipes = db.table('pipes', cache_size=0)