def import_channels(): print("Beginning Slack channel import...") try: db.create_tables([SlackChannel]) except Exception as e: # Most likely table already exists, but we'll print the error to confirm. print('%s' % e) for c in json_data('channels.json'): # Because we may create channels by name when importing messages # if we see unexpected channel info, we have to get by name here. # In a better world, we should probably throw out unexpected channels. # There *shouldn't* be any channels that aren't represented in channels.json try: SlackChannel.get(SlackChannel.name == c['name']) except SlackChannel.DoesNotExist: SlackChannel.create(channel_id=c['id'], name=c['name']) print('...continuing with private group channels...') for c in json_data('groups.json'): try: SlackChannel.get(SlackChannel.channel_id == c['id']) except SlackChannel.DoesNotExist: SlackChannel.create(channel_id=c['id'], name=c['name'], private_group=True) print('...and finally private messaging channels...') for c in json_data('dms.json'): try: SlackChannel.get(SlackChannel.channel_id == c['id']) except SlackChannel.DoesNotExist: SlackChannel.create(channel_id=c['id'], name=c['id'], private_messages=True) print("...finished")
def shops_updater(bot: Bot, job: Job) -> None: logger.debug("Entering: shops_updater") resp = requests.get(config.SHOP_API, verify=False) if resp.status_code == 200: logger.debug('Retrieved shop data. Dropping and recreating tables.') dbOffer.drop_table(with_all_data=True) dbShop.drop_table(with_all_data=True) db.create_tables(True) shops = resp.json() with orm.db_session: # TODO: Get f*****g marshmallow deserialisation working for shop in shops: logger.debug("creating shop %s", shop['name']) s = dbShop(link=shop['link'], name=shop['name'], ownerName=shop['ownerName'], ownerCastle=shop['ownerCastle'], kind=shop['kind'], mana=shop['mana']) for offer in shop['offers']: logger.debug("adding offer %s to shop %s", offer['item'], shop['name']) s.offers.create(**offer) else: logger.debug("Exiting: shops_updater") return
def create_db_tables(self): if Record.table_exists(): messagebox.showerror( "Table exists", "The Record table already exists. You blast that db. ") else: db.create_tables([Record])
def init_bot(): logger.info("Joker bot started.") updater = Updater(settings.TELEGRAM_API_TOKEN, use_context=True) dispatcher = updater.dispatcher db.create_tables([Status]) updater.job_queue.run_repeating(status.status_check, interval=60, first=0) dispatcher.add_handler(CommandHandler("quem", status.quem)) dispatcher.add_handler(CommandHandler("status", status.status)) updater.job_queue.run_repeating( schedule.generate_ics, interval=60 * 60, first=0 ) updater.job_queue.run_repeating( schedule.pin_today_event, interval=60 * 60 * 24, first=datetime.time(5, 0) ) dispatcher.add_handler(CommandHandler("quando", schedule.quando)) dispatcher.add_handler(CommandHandler("grana", money.grana)) dispatcher.add_handler(pizza.pizza_conversation_handler) dispatcher.add_handler(CommandHandler("quemsou", generic.quemsou)) dispatcher.add_handler(CommandHandler("batima", generic.batima)) dispatcher.add_handler(CommandHandler("boom", generic.boom)) # This dispatcher must be the last included dispatcher.add_handler(MessageHandler(Filters.text, generic.non_commands)) logger.info("Command handlers set. Start pooling.") updater.start_polling() updater.idle()
def setUp(self): """ Initialize client to send HTTP requests and run migrations on the test db """ self.test_client = app.test_client() db.connect() db.create_tables([Game, LetterGuessed])
def init_db(): db.create_tables([Obj, Bucket, Host], safe=True) h = Host.update_path(name="localhost", path=DATA_PATH) if h: return h = Host.create_host(name="localhost", path=DATA_PATH, ip_addr="localhost") msg = "Init host success !" if h else "Error: init host failed !" print(msg)
def db_connection(): memdb = CountingSqliteDatabase(":memory:") models = [User, Post, Follow, Like] db.initialize(memdb) db.connect() db.create_tables(models) yield memdb db.drop_tables(models) db.close()
def use_db(): db.connect() db.drop_tables(app_models) db.create_tables(app_models) yield db db.close()
def import_messages(): print('Beginning Slack Message import...') try: db.create_tables([SlackMessage]) except Exception as e: # Most likely table already exists, but we'll print the error to confirm. print('%s' % e) file_count = len(os.listdir(SLACK_FILES_DIR)) print("... processing %s message files" % file_count) for root, dirs, files in os.walk(SLACK_FILES_DIR): if root == SLACK_FILES_DIR: continue file_count -= 1 if file_count % 1000 == 0 and file_count != 0: print('... %s remaining...' % file_count) for fname in files: dir_name = root.split('data-import')[1] dir_name = ''.join(dir_name.split('/', 1)) dir_name = ''.join(dir_name.split('\\', 1)) channel_date, ftype = fname.rsplit('.') # Guard against .ds_store and other stray files. # We only want to read json files. if ftype == 'json': # Resolve channel name try: channel = SlackChannel.get(SlackChannel.name == dir_name) channel_name = channel.name except SlackChannel.DoesNotExist: print( "Unexpected channel data encountered! Could not find Slack channel `%s` in database." % dir_name) print("Creating new channel, but proceed with caution.") # Note that because the channel is unexpected we have no ID and have to fake it. channel = SlackChannel.create(channel_id=dir_name, name=dir_name) jsonfile = '%s/%s' % (dir_name, fname) for d in json_data(jsonfile): # in the case of bot posts, there will be no user. if 'user' in d: ts = float(d['ts']) try: SlackMessage.get( SlackMessage.channel_name == channel_name, SlackMessage.date == channel_date, SlackMessage.ts == ts) except SlackMessage.DoesNotExist: SlackMessage.create(channel_name=channel_name, user=d['user'], message=d['text'], date=channel_date, ts=ts) print("...finished")
def __init__(self, **options): super().__init__(**options) self.log = logger.get_logger('Alexis') db.connect() db.create_tables([Post, Ban, Redditor, Meme], True) try: with open('config.yml', 'r') as file: self.config = yaml.safe_load(file) except Exception as ex: self.log.exception(ex) raise
def copy_db(): # Name_2, Petition_2, User # db.drop_tables([Peticia], safe=True) # db.create_tables([Peticia], safe=True) # db.create_tables([Name], safe=True) # query = Name_2.select() # data = [] # for i in query: # data.append((i.username, i.gender)) # # with db.atomic(): # # by default SQLite limits the number of bound variables in a SQL query to 999 # for batch in chunked(data, 450): # Name.insert_many(batch, fields=[Name.username, Name.gender]).execute() # # # # db.create_tables([Petition], safe=True) # query = Petition_2.select() # for i in query: # Petition.create( # petition_id=i.petition_id, # status=i.status, # title=i.title, # article=i.article, # answer=i.answer # ) # # # db.create_tables([Vote], safe=True) for i in User.select(User.petition_id).distinct(): data = [] # p = Petition.get(petition_id=i.petition_id).petition_id p = i.petition_id query = User.select().where(User.petition == i.petition_id) for user in query: data.append((p, user.position_number, user.username, user.sign_date, user.gender)) with db.atomic(): # by default SQLite limits the number of bound variables in a SQL query to 999 for batch in chunked(data, 198): # User.insert_many(batch, fields=[User.petition, User.position_number, # User.username, User.sign_date, User.gender]).execute() Vote.insert_many(batch).execute()
def create_db(): """ Re-creates the entire database from the scratch. """ import inspect import os import models from models import DATABASE, db def is_model(obj): return (inspect.isclass(obj) and issubclass(obj, models.BaseModel) and obj is not models.BaseModel) models = [value for (name, value) in inspect.getmembers(models, is_model)] os.remove(DATABASE) db.connect() db.create_tables(models) db.close()
def import_users(): print("Beginning Slack user import....") try: db.create_tables([SlackUser]) except Exception as e: # Most likely table already exists, but we'll print the error to confirm. print('%s' % e) for d in json_data('users.json'): # There's some key error on real_name. # probably related to older schema when real name was only in profile. try: real_name = d['real_name'] except: real_name = d['profile']['real_name'] try: SlackUser.get(SlackUser.user_id == d['id']) except SlackUser.DoesNotExist: SlackUser.create(user_id=d['id'], name=d['name'], real_name=real_name) print("...finished")
def legacy_import(): """ This fulfills the legacy needs from slackToDB.py from the deprecated `slack-urls` repo and is only called if a `--legacy` argument is passed to the importer. For details see the legacy commands documentation: ../README.md#legacy-commands """ try: db.create_tables([Slack]) except Exception as e: # Most likely table already exists, but we'll print the error to confirm. print('%s' % e) for root, dirs, files in os.walk(SLACK_FILES_DIR): if root != SLACK_FILES_DIR: print( "You don't appear to be in the correct directory. Please double-check" ) break for fname in files: dir_name = root.split(SLACK_FILES_DIR + '/')[1] channel_date, ftype = fname.rsplit('.') # Guard against .ds_store and other stray files. # We only want to read json files. if ftype == 'json': jsonfile = '%s/%s' % (dir_name, fname) for d in json_data(jsonfile): # in the case of bot posts, there will be no user. try: Slack.get(Slack.channel == dir_name, Slack.channel_date == channel_date, Slack.data == json.dumps(d)) except Slack.DoesNotExist: Slack.create(channel=dir_name, channel_date=channel_date, data=json.dumps(d))
def main(): opts = get_options() if os.path.exists(opts.name): if opts.force_overwrite: os.remove(opts.name) else: sys.exit(f'File {opts.name} exists, use --force to overwrite') initialize_database(db, opts.name) db.create_tables( [PeeweeIndex4S, PeeweeIndex4U, PeeweeIndex5S, PeeweeIndex5U]) for statement in SQL_TABLE_STATEMENTS.values(): db.execute_sql(statement) for fn in os.listdir(opts.corpus_dir): path = os.path.join(opts.corpus_dir, fn) with open(path) as fp: text = fp.read() for cls in [ PeeweeIndex4S, PeeweeIndex4U, PeeweeIndex5S, PeeweeIndex5U ]: cls.insert({'text': text, 'source': fn}).execute() for tbl_name in ['di_4_s', 'di_4_u', 'di_5_s', 'di_5_u']: sql = f'insert into {tbl_name} (text, source) values (?, ?)' db.execute_sql(sql, (text, fn))
def main(): db.connect() db.create_tables([Languages]) for language in languages.languages: if language.part1 != '': try: wp_page = wptools.page(lang=language.part1).get_query() wp_text = wp_page.extext except pycurl.error: # No wikipedia articles for this language wp_text = None else: wp_text = None Languages.create( name=language.name, part1=language.part1, part2=language.part2b, part3=language.part3, text=wp_text, ) db.commit()
def is_bot(self, user_id): return self.slack_client.api_call('users.info', user=user_id)['user']['is_bot'] def get_win_streak(self, player_slack_id): win_streak = 0 matches = Match.select().where(Match.pending == False, (player_slack_id == Match.winner) | (player_slack_id == Match.loser)).order_by(Match.played.desc()) for match in matches: if (player_slack_id == match.winner_id): win_streak = win_streak + 1 else: break return win_streak def get_channel_id(slack_client, channel_name): channels = slack_client.api_call("channels.list") for channel in channels['channels']: if channel['name'] == channel_name: return channel['id'] print('Unable to find channel: ' + channel_name) quit() with open('config.json') as config_data: config = json.load(config_data) slack_client = SlackClient(config['slack_token']) db.connect() db.create_tables([Player, Match]) EloBot(slack_client, get_channel_id(slack_client, config['channel']), config)
for key, value in menu.items(): print(f"{key}) {value.__doc__}") print("\nType 'q' to quit.\n") choice = input('> ').lower().strip() if choice == 'q': break if choice in menu: menu[choice]() def main(): clear_screen() print("Welcome to Work Log Tracker") choice = None menu = OrderedDict([('a', log_work), ('b', view_work)]) while choice != 'q': for key, value in menu.items(): print(f"{key}) {value.__doc__}") print("\nType 'q' to quit.\n") choice = input('> ').lower().strip() if choice in menu: menu[choice]() print("\nSee you next time") if __name__ == '__main__': db.connect() db.create_tables([Task], safe=True) main()
def prepDB(): cleanDB() db.create_tables([Site, User])
return self.slack_client.api_call('users.info', user=user_id)['user']['is_bot'] def get_win_streak(self, player_slack_id): win_streak = 0 matches = Match.select().where(Match.pending == False, (player_slack_id == Match.winner) | (player_slack_id == Match.loser)).order_by(Match.played.desc()) for match in matches: if (player_slack_id == match.winner_id): win_streak = win_streak + 1 else: break return win_streak def get_channel_id(slack_client, channel_name): channels = slack_client.api_call("channels.list") for channel in channels['channels']: if channel['name'] == channel_name: return channel['id'] print('Unable to find channel: ' + channel_name) quit() with open('config.json') as config_data: config = json.load(config_data) slack_client = SlackClient(config['slack_token']) db.connect() db.create_tables([Player, Match], True) EloBot(slack_client, get_channel_id(slack_client, config['channel']), config)
from models import db, User, Role, UserRoles, Food, Image db.connect() db.create_tables([Food, User, Role, UserRoles, Image], safe=True)
def create_tables(): with db: db.create_tables([Paper])
import os #Initialize flask application app = Flask(__name__) app.config.from_object('config') app.config.from_object('local_config') #Routings from routings import mod_routings app.register_blueprint(mod_routings) #Initialize databse db.init(app.config['DBPATH']) if not os.path.exists(app.config['DBPATH']): #Create db if not exist try: db.create_tables([Books, Stories, Authors, Files, AuthorsStories]) try: #Add sample data from add_sample import add_sample add_sample() except: pass except OperationalError: pass if __name__ == '__main__': #Run flask server app.run(host='0.0.0.0', port=5000)
def _create_tables(): with db: db.create_tables(MODELS)
from models import db, Action, Record db.create_tables([Action]) db.create_tables([Record])
def create_tables(): db.connect() db.create_tables([Message, Room, User]) db.close()
from models import db, Url import argparse parser = argparse.ArgumentParser() parser.add_argument("--action", help="action to execute", required=True, choices=['create-database']) args = parser.parse_known_args()[0] if args.action == 'create-database': db.create_tables([Url])
def connect_database(): """Connect to SqliteDatabase""" db.connect() db.create_tables([File, Tag, FileTag], safe=True)
def create_tables(): db.create_tables(MODEL_LIST)
def create_tables(): db.connect() db.create_tables([Athlete, Team, Game, Play, Collaboration, Action, Observation]) db.close()
def create_tables(): '''テーブルを作成する ''' db.connect() db.create_tables([Location]) db.close()
from models import db from models.usuario import Usuario from models.estudante import Estudante from models.professor import Professor from models.disciplina import Disciplina from models.instituicao import Instituicao from models.atividade import Atividade from models.estudante_disciplina import EstudanteDisciplina try: db.create_tables([ Usuario, Estudante, Professor, Disciplina, Instituicao, Atividade, EstudanteDisciplina, ]) except: pass # controllers from controllers import professor_controller from controllers import estudante_controller from controllers import usuario_controller if __name__ == '__main__': app.run(debug=True)
from models import db, KeyValue, Member, Channel, Message, Verifier db.connect() db.create_tables([KeyValue, Member, Channel, Message, Verifier])
@app.route("/add_pokemon", methods=["GET", "POST"]) def add_pokemon(): name = request.form["name"] number = request.form["number"] types = request.form["types"] poke = Pokemon.create(name=name, number=number, types=types) if request.method == "POST": image = request.files["image"] print(image.filename) image.save( os.path.join(app.config["UPLOAD_FOLDER"], secure_filename(image.filename))) print("File Uploaded") print("Name: " + name + " Number: " + number + " Types: " + types + "Filename: " + image.filename) return redirect("/") if __name__ == "__main__": try: db.connect() db.create_tables([Pokemon]) # db.drop_tables([Pokemon]) except peewee.OperationalError as e: print("Error " + str(e)) app.run(debug=True, port=5000)
# Рекомендации: # Можно создать отдельный модуль для инициализирования базы данных. # Как далее использовать эту базу данных в движке: # Передавать DatabaseUpdater url-путь # https://peewee.readthedocs.io/en/latest/peewee/playhouse.html#db-url # Приконнектится по полученному url-пути к базе данных # Инициализировать её через DatabaseProxy() # https://peewee.readthedocs.io/en/latest/peewee/database.html#dynamically-defining-a-database import argparse from control_center import ControlPanel from models import db, Weather program = ControlPanel() db.create_tables([Weather]) parser = argparse.ArgumentParser(description="Weather") parser.add_argument("-a", "--add_to_database", required=False, action='store_true') parser.add_argument("-p", "--print_forecast", required=False, action='store_true') parser.add_argument("-c", "--create_postcards", required=False, action='store_true') args = parser.parse_args()
def create_tables(): db.connect() db.create_tables([Log], safe=True)
def create_tables(): db.create_tables([User, Game, UserGame, Action], safe=True)
def create_database(): db.connect() try: db.create_tables((User, Stat, Media.users.get_through_model(), Media)) except OperationalError: print 'database already exists'