Beispiel #1
0
Datei: db.py Projekt: bmnz90/cozy
def init_db():
    global db
    global update
    global data_dir

    tmp_db = None
    
    if update:
        update_db()
    else:
        tmp_db = SqliteDatabase(os.path.join(data_dir, "cozy.db"))
        if PeeweeVersion[0] == '2':
            with tmp_db.connection_context():
                tmp_db.create_tables([Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache], True)
        else:
            with tmp_db.connection_context():
                tmp_db.create_tables([Track, Book, Settings, ArtworkCache, Storage, StorageBlackList, OfflineCache])

    # this is necessary to ensure that the tables have indeed been created
    if tmp_db:
        while not tmp_db.table_exists("settings"):
            time.sleep(0.01)

    try:
        db.connect()
    except Exception as e:
        log.error("Could not connect to database. ")
        log.error(e)


    if PeeweeVersion[0] == '3':
        db.bind([Book, Track, Settings, ArtworkCache, StorageBlackList, OfflineCache, Storage], bind_refs=False, bind_backrefs=False)

    if (Settings.select().count() == 0):
        Settings.create(path="", last_played_book=None)
Beispiel #2
0
def create_database():
    with open("/home/wojjak/PycharmProjects/untitled/data.db", "a") as f:
        f.write("")
    db = SqliteDatabase("data.db")
    db.connect()
    db.create_tables([Client])
    db.create_tables([User])
Beispiel #3
0
def setup_db_before_test():
    """Sets up database automatically before each test"""
    _db = SqliteDatabase(":memory:")
    with _db.bind_ctx(MODELS):
        _db.create_tables(MODELS)
        yield _db
        _db.drop_tables(MODELS)
Beispiel #4
0
    def create(cls, db_name, db_type, host=None, user=None, password=None,
               port=5432, models=_MODELS):
        """ Setup ORM."""

        if db_type == 'sqlite':
            logger.info(F'Opening connection to sqlite')
            db = SqliteDatabase(db_name,
                                pragmas={
                                    'journal_mode': 'wal',
                                    'cache_size': -1 * 64000,  # 64MB
                                    'foreign_keys': 1,
                                    'ignore_check_constraints': 0,
                                }
                                )
        elif db_type == 'postgres':

            db = PostgresqlDatabase(db_name, user=user, password=password,
                                    host=host, port=port, autoconnect=True)
        else:
            logger.error(F'Unknown database type {db_type}')
            raise ValueError

        db.bind(models)
        db.create_tables(models, safe=True)

        cls._db_handle = db
        cls._db_name = db_name
        cls._db_models = models
Beispiel #5
0
class SubstitutableDatabase(object):
    def __init__(self, filepath=":memory:", tables=[]):
        self._tables = tables
        self._create_database(filepath)

    def _create_database(self, filepath):
        self._db = SqliteDatabase(
            abspath(filepath) if filepath != ":memory:" else filepath,
            pragmas={"foreign_keys": 1},
        )
        for model in self._tables:
            model.bind(self._db, bind_refs=False, bind_backrefs=False)
        self._db.connect()
        self._db.create_tables(self._tables, safe=True)

    def _change_path(self, filepath):
        self.close()
        self._create_database(filepath)

    def _vacuum(self):
        print("Vacuuming database ")
        self.execute_sql("VACUUM;")

    def __getattr__(self, attr):
        return getattr(self._db, attr)
Beispiel #6
0
def create_db():
    import os
    if not ('DYNO' in os.environ):
        db = SqliteDatabase('../sender.sqlite')
        db.connect(True)

        db.drop_tables([AdminPage, TargetGroup, UserPage, SenderPage])
        db.create_tables([AdminPage, TargetGroup, UserPage, SenderPage])

        yuri = AdminPage(vkid=142872618)
        yuri.save()
    else:
        init_db()

        db_proxy.connect(True)
        print('CONNECTED')
        # TODO сделать так, чтобы дубликаты не добавлялись
        db_proxy.create_tables([AdminPage, TargetGroup, UserPage, SenderPage],
                               safe=True)

        print('before AdminPage')
        yuri = AdminPage(vkid=142872618)
        yuri.save()

        print('before db.close()')
        db_proxy.close()

        return 'DB is created!'
Beispiel #7
0
def create_db():
    import os
    if not ('HEROKU' in os.environ):
        db = SqliteDatabase('sender.sqlite')
        db.connect(True)

        db.drop_tables([Account, Subscription, Messenger, AccessToken])
        db.create_tables([Account, Subscription, Messenger, AccessToken])

        vk = Messenger.create(name='VK', cost=200)
        vk.save()

        telegram = Messenger.create(name='Telegram', cost=200)
        telegram.save()

    else:
        init_db()

        db_proxy.connect(True)
        print('CONNECTED')

        db_proxy.create_tables([AdminPage, TargetGroup, UserPage, SenderPage],
                               safe=True)

        print('before AdminPage')
        yuri = AdminPage(vkid=142872618)
        yuri.save()

        print('before db.close()')
        db_proxy.close()

        return 'DB is created!'
class NonDeterminismCatcher:

    __db = None
    __logging = None

    def __init__(self, logger) -> None:
        self.__db = SqliteDatabase('non_determinism_quic.db')
        self.__db.connect()
        self.__db.drop_tables(
            [LearningRunModel, NonDeterministicResponseModel])
        self.__db.create_tables(
            [LearningRunModel, NonDeterministicResponseModel])
        self.__logging = logger
        logger = logging.getLogger('peewee')
        logger.addHandler(logging.StreamHandler())
        logger.setLevel(logging.DEBUG)

    def add_run(self, q, res):
        # Check if there is already a record with this query
        try:
            if not res:
                res = "---"
            previous_run = LearningRunModel.get(LearningRunModel.run == q)
            self.__logging.info("Received query {}".format(q))
            if not previous_run.result == res:
                self.__logging.info("Not deterministic with {}".format(
                    previous_run.result))
                NonDeterministicResponseModel(run=q, result=res).save()
        except LearningRunModel.DoesNotExist:
            self.__logging.info("New run inserted.")
            LearningRunModel(run=q, result=res).save()
Beispiel #9
0
    def __init__(self, database: SqliteDatabase, view: MainView,
                 presenter: MainPresenter, builder: MainBuilder,
                 udev_interactor: UdevInteractor, *args: Any,
                 **kwargs: Any) -> None:
        LOG.debug("init Application")
        GLib.set_application_name(_(APP_NAME))
        super().__init__(*args,
                         application_id=APP_ID,
                         flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE,
                         **kwargs)

        database.connect()
        database.create_tables(
            [SpeedProfile, SpeedStep, CurrentSpeedProfile, Setting])

        if SpeedProfile.select().count() == 0:
            load_db_default_data()

        self.add_main_option_entries(self._get_main_option_entries())
        self._view = view
        self._presenter = presenter
        self._presenter.application_quit = self.quit
        self._window: Optional[Gtk.ApplicationWindow] = None
        self._builder: Gtk.Builder = builder
        self._udev_interactor = udev_interactor
        self._start_hidden: bool = False
Beispiel #10
0
 def _create_database(self):
     db = SqliteDatabase(self._filepath)
     for model in self._tables:
         model.bind(db, bind_refs=False, bind_backrefs=False)
     db.connect()
     db.create_tables(self._tables)
     return db
    def test_select_game_should_fail_if_game_had_been_registered(
            self,
            get_games_mock,
            mock_keyboard
    ):
        database = SqliteDatabase(':memory:')
        database_proxy.initialize(database)
        database.create_tables([User, Game])
        mock_user = User.create(id=111, steam_id=0, authorization_key='')
        Game.create(id=27000, owner=mock_user, name='Test Game')

        bot_mock = Mock()
        update_mock = Mock()
        update_mock.message.from_user.id = 111
        update_mock.message.text = 'Test Game'
        mock_keyboard.return_value = mock_keyboard

        with open(
            (os.path.dirname(__file__))+'/fixtures/game_request.json'
        ) as f:
            json_data = json.load(f)

        game_data = json_data['Games']
        get_games_mock.return_value = game_data

        self.assertEqual(
            ConversationHandler.END,
            cmd_new_game.select_game(bot_mock, update_mock)
        )
        update_mock.message.reply_text.assert_called_with(
            'Game already registered',
            reply_markup=mock_keyboard
        )
    def test_new_game_should_return_list_of_games(
            self,
            get_games_mock,
            mock_keyboard
    ):
        database = SqliteDatabase(':memory:')
        database_proxy.initialize(database)
        database.create_tables([User, Game])
        User.create(id=111, steam_id=0, authorization_key='')

        mock_keyboard.return_value = mock_keyboard
        bot_mock = Mock()
        update_mock = Mock()
        update_mock.message.from_user.id = 111

        with open(
            (os.path.dirname(__file__))+'/fixtures/game_request.json'
        ) as f:
            json_data = json.load(f)

        game_data = json_data['Games']
        get_games_mock.return_value = game_data

        self.assertEqual(
            cmd_new_game.SELECT,
            cmd_new_game.new_game(bot_mock, update_mock)
        )
        update_mock.message.reply_text.assert_called_with(
            'Chose the game',
            reply_markup=mock_keyboard
        )
        mock_keyboard.assert_called()
    def test_add_game_should_display_error_if_no_available_games(
            self,
            get_games_mock
    ):
        database = SqliteDatabase(':memory:')
        database_proxy.initialize(database)
        database.create_tables([User, Game])
        mock_user = User.create(id=111, steam_id=0, authorization_key='')
        Game.create(id=27000, owner=mock_user, name='Test Game')

        bot_mock = Mock()
        update_mock = Mock()
        update_mock.message.from_user.id = 111

        with open(
            (os.path.dirname(__file__))+'/fixtures/game_request.json'
        ) as f:
            json_data = json.load(f)

        game_data = json_data['Games']
        get_games_mock.return_value = game_data

        self.assertEqual(
            ConversationHandler.END,
            cmd_new_game.new_game(bot_mock, update_mock)
        )
        update_mock.message.reply_text.assert_called_with(
            'No games to be added'
        )
Beispiel #14
0
class PeeweeDBManager(object):
    def __init__(self, connection=None):
        self.connection = connection
        # proxy initialization
        self.database = SqliteDatabase(
            self.connection,
            # thread_safe=False,
            # check_same_thread=False,
            pragmas={
                'journal_mode': 'wal',
                'cache_size': -1 * 64000,  # 64MB
                'foreign_keys': 1,
                'ignore_check_constraints': 0,
                'synchronous': 0
            })
        db.initialize(self.database)
        # dynamic initialization
        # db.init(self.connection)
        # db = connect(self.connection)
        # self.database = db

    @property
    def session(self):
        return self.database.connection_context

    def setup(self):
        # Normally we would add whatever db setup code we needed here.
        # This will for fine for the ORM
        try:
            self.database.create_tables(MODELS, safe=True)
        except Exception as e:
            print('Could not initialize DB: {}'.format(e))
Beispiel #15
0
    def test_poll_game_should_return_false_if_it_is_not_in_any_chats(
            self,
            mock_gmr
    ):
        bot_mock = Mock()
        database = SqliteDatabase(':memory:')
        database_proxy.initialize(database)
        database.create_tables([User, Game, Subscription])

        with open((os.path.dirname(__file__))
                  + '/fixtures/game_request.json') as f:
            json_data = json.load(f)

        mock_gmr.return_value = json_data['Games'][0]

        user = User.create(id=111, steam_id=0, authorization_key='')
        game = Game.create(
            id=27000,
            name='Test Game',
            owner=user
        )

        self.assertEqual(False, job_games.poll_game(bot_mock, game))
        game.refresh()
        self.assertEqual(True, game.active)
Beispiel #16
0
 def CreateAllTables(self):
     database = SqliteDatabase('database.db', pragmas={'foreign_keys': 1})
     database.connect()
     database.create_tables([
         Player, LeaderboardRecord, Level, Terrain, Background, Monster,
         TerrainInfo, BackgroundInfo, MonsterInfo
     ])
Beispiel #17
0
def initialize_database(path: Optional[str] = None):
    path = path if path is not None else join(expanduser("~"), '.hyperion.db')
    database = SqliteDatabase(path)
    database_proxy.initialize(database)
    database.connect()
    database.create_tables([Neighbourhood, Bike, Location, Link, Postcode],
                           safe=True)
Beispiel #18
0
def main():
    db = SqliteDatabase('daily.sqlite')

    class Daily_2021(Model):
        month = IntegerField()
        day = IntegerField()

        text = CharField(max_length=1000)
        verset = CharField()

        class Meta:
            database = db

    db.connect()

    db.create_tables([Daily_2021])

    # result = get_text()
    for month in range(1, 13):
        for day in range(1, get_month_days(month) + 1):
            result = get_text(month, day)
            print(f"{month} {day} {result[0]} {result[1]}")
            Daily_2021.create(month=month,
                              day=day,
                              text=result[0],
                              verset=result[1])
        print()
class TestArtRoutes(TestCase):
    def setUp(self):
        database_config.db_path = 'test_db.sqlite'
        self.database = SqliteDatabase(database_config.db_path,
                                       pragmas={'foreign_keys': 1})

        self.database.drop_tables([Art, Artist])
        self.database.create_tables([Art, Artist])

        self.test_app = app.test_client()

    def test_add_artist(self):
        response = self.test_app.post('/artists',
                                      data={
                                          'name': 'cheese',
                                          'email_address':
                                          '*****@*****.**'
                                      },
                                      follow_redirects=True)

        # response is a stream of bytes, decode into a string
        response_text = response.data.decode('utf-8')

        # Is data on page? Could also assert data is in specific HTML elements with a little more work
        # A HTML parser like Beautiful Soup could help https://www.crummy.com/software/BeautifulSoup/bs4/doc/
        self.assertIn('cheese', response_text)
        self.assertIn('*****@*****.**', response_text)

        # Flash message shown?
        self.assertIn('The new artist has been added!', response_text)

        # new artist in DB?
        new_artist = Artist.get_or_none(
            Artist.name == 'cheese'
            and Artist.email_address == '*****@*****.**')
        self.assertIsNotNone(new_artist)

    def test_no_duplicate_artist(self):
        self.ats1 = Artist.create(name='ats1',
                                  email_address='*****@*****.**').save()
        response = self.test_app.post('/artists',
                                      data={
                                          'name': 'ats1',
                                          'email_address': '*****@*****.**'
                                      })

        response_text = response.data.decode('utf-8')

        # Flash message?
        self.assertIn('Artist ats1 has already been added.', response_text)

        # Still only one artist in DB?
        artist_count = Artist.select().count()
        self.assertEqual(1, artist_count)

        # artist with duplicate name not added
        new_artist = Artist.get_or_none(
            Artist.name == 'ats1' and Artist.email_address == '*****@*****.**')
        self.assertIsNone(new_artist)
def db_connection():
    models = [Metric]
    db = SqliteDatabase(':memory:')
    with db:
        db.bind(models)
        db.create_tables(models)
        yield db
        db.drop_tables(models)
Beispiel #21
0
def create_db_tables():
    logger.info("Checking database...")
    if not DB_PATH.exists():
        logger.info("Database does not exist...")
        db = SqliteDatabase(str(DB_PATH))
        with db:
            db.create_tables([User, Question])
            logger.info("Tables created!")
Beispiel #22
0
def init_db(filename: str = ":memory:") -> SqliteDatabase:
    """
    Bind an SQLite database to the Peewee ORM models.
    """
    db = SqliteDatabase(filename)
    db.bind(BaseModel.model_registry)
    db.create_tables(BaseModel.model_registry)
    return db
Beispiel #23
0
def make_database(address = ":memory:"):
    database = SqliteDatabase(address)
    proxy.initialize(database)
    database.create_tables([
        Author, Book, BookAuthors,
        Card, CardIndex,
        File, User])
    return database
Beispiel #24
0
def db_connection():
    models = [ClubUser, ClubMessage]
    db = SqliteDatabase(':memory:')
    with db:
        db.bind(models)
        db.create_tables(models)
        yield db
        db.drop_tables(models)
def db_connection():
    models = [Event, EventSpeaking, MessageAuthor]
    db = SqliteDatabase(':memory:')
    with db:
        db.bind(models)
        db.create_tables(models)
        yield db
        db.drop_tables(models)
def create_database(db):
    if not os.path.isfile(db):
        database = SqliteDatabase(db)
        database.connect()
        database.execute_sql('PRAGMA foreign_keys = ON;')

        database.create_tables([Customer])
        database.close()
Beispiel #27
0
class ValveDriverTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        fakesleep.monkey_patch()
        SetTestMode()

    @classmethod
    def tearDownClass(cls):
        fakesleep.monkey_restore()

    def setUp(self):
        self.test_db = SqliteDatabase(':memory:')
        self.test_db.bind(MODELS)
        self.test_db.connect()
        self.test_db.create_tables(MODELS)

    def tearDown(self):
        self.test_db.drop_tables(MODELS)
        self.test_db.close()

    def test_valve_driver(self):
        valve_output_1 = Output.create(number=2)
        valve_1 = Valve.create(number=1,
                               name='valve 1',
                               delay=30,
                               output=valve_output_1)

        SetUpTestInjections(output_controller=mock.Mock(OutputController))
        driver_1 = ValveDriver(valve_1)

        self.assertEqual(valve_1.id, driver_1.id)
        self.assertEqual(0, driver_1.percentage)
        self.assertEqual(0, driver_1._desired_percentage)
        self.assertFalse(driver_1.is_open)
        self.assertFalse(driver_1.in_transition)

        driver_1.set(50)
        self.assertEqual(50, driver_1._desired_percentage)
        driver_1.close()
        self.assertEqual(0, driver_1._desired_percentage)
        driver_1.open()
        self.assertEqual(100, driver_1._desired_percentage)
        self.assertTrue(driver_1.will_open)
        driver_1.steer_output()
        driver_1._output_controller.set_output_status.assert_called_once()
        self.assertFalse(driver_1.will_open)
        self.assertEqual(100, driver_1.percentage)
        self.assertFalse(driver_1.is_open)
        self.assertTrue(driver_1.in_transition)

        time.sleep(20)
        self.assertFalse(driver_1.is_open)
        self.assertTrue(driver_1.in_transition)

        time.sleep(15)
        self.assertTrue(driver_1.is_open)
        self.assertFalse(driver_1.in_transition)
 def test_db_closure(*args, **kwargs):
     test_db = SqliteDatabase(":memory:")
     with test_db.bind_ctx(dbs):
         test_db.create_tables(dbs)
         try:
             func(*args, **kwargs)
         finally:
             test_db.drop_tables(dbs)
             test_db.close()
Beispiel #29
0
def create_db():
    db = SqliteDatabase(':memory:')

    Promoter.bind(db)
    Event.bind(db)
    Ticket.bind(db)
    Order.bind(db)

    db.create_tables([Promoter, Event, Ticket, Order])
Beispiel #30
0
def app():
    app = create_app({"TESTING": True})

    database = SqliteDatabase(get_db_path())
    database.create_tables([Poll, Choice, VoteCast])

    yield app

    database.drop_tables([Poll, Choice, VoteCast])
Beispiel #31
0
class TestManage(unittest.TestCase):
    def setUp(self):
        self.db = SqliteDatabase('peewee.db')
        self.db.connect()

    def tearDown(self):
        self.db.close()

    def testCreate(self):
        self.db.create_tables(ALL_MODELS, safe=True)
def init_models(app):
    if app.config['TESTING']:
        database = SqliteDatabase(':memory:')
    else:
        database = SqliteDatabase('local.db')

    database_proxy.initialize(database)
    database.connect()
    database.create_tables([
        TodoItem,
    ], safe=True)
Beispiel #33
0
    name = Field(String, 10)
    age = Field(Int, 2)
    salary = Field(Float, 14, precision=4)

    class Meta:
        selector_string = u'01'
        database = db


class Address(PeeweeRecord):
    typ = Field(Int, 2)
    address = Field(String, 10, truncate=True)
    phone = Field(String, 20)

    class Meta:
        selector_string = u'02'
        database = db

# read sample file
engine = FixedEngine([Header, Address], selector_slice=(0, 2))

# connect to db
db.connect()

# create tables
db.create_tables([Header, Address])

# write fields to db using save
for r in engine.load('../samples/sample_utf8.txt'):
    r.save()
Beispiel #34
0
        payment = Payment.get_by_id(payment_id)

        return {
            "payment": payment.to_dict(),
        }

    def put(self, payment_id):
        payment = Payment.get_by_id(payment_id)

        payment.name = request.form["name"]
        payment.price = request.form["price"]

        payment.save()


class MetricsResource(Resource):

    def get(self):
        return {
            "balance": get_balance(),
            "days_left": get_days_left(),
        }

api.add_resource(PaymentsResource, "/payments")
api.add_resource(PaymentResource, "/payment/<int:payment_id>")
api.add_resource(MetricsResource, "/metrics")


db.connect()
db.create_tables([Payment], safe=True)
except :
  pass


db = SqliteDatabase(DB_NAME, threadlocals=True)

class OUI(Model) :
  class Meta :
    database = db

  oui = CharField(unique=True, max_length=OUI_LEN)
  short_name = TextField()
  long_name = TextField()

db.connect()
db.create_tables([OUI,])

with open(OUI_SOURCE, 'r') as f :
  for line in f.readlines() :
    if line.startswith('#') :
      continue
    parts = line.split()
    if len(parts) <= 1 :
      continue
    if len(parts[0]) != OUI_LEN :
      continue
    oui = parts[0]
    short_name = parts[1]

    parts = line.split('#')
    if len(parts) == 2 :
Beispiel #36
0
                               help_text='Imagem original')
    normalized_image = CharField(unique=True,
                                 verbose_name='Imagem normalizada',
                                 help_text='Imagem normalizada')
    thumbnail_image = CharField(unique=True,
                                verbose_name='Imagem reduzida',
                                help_text='Imagem reduzida')

    def delete_all(self, site_img_path):
        if self.original_image:
            original_file = os.path.join(site_img_path, self.original_image)
            if os.path.exists(original_file):
                os.remove(original_file)
        if self.normalized_image:
            normalized_file = os.path.join(site_img_path, self.normalized_image)
            if os.path.exists(normalized_file):
                os.remove(normalized_file)
        if self.thumbnail_image:
            thumbnail_file = os.path.join(site_img_path, self.thumbnail_image)
            if os.path.exists(thumbnail_file):
                os.remove(thumbnail_file)
        super(Picture, self).delete_instance()

    def save(self, force_insert=False, only=None):
        super(Picture, self).save(force_insert, only)


# Connect to db and create tables
db.connect()
db.create_tables([User, Site, Portfolio, Picture], safe=True)
Beispiel #37
0
pw = SqliteDatabase('peewee-simple.db')


class Book(Model):
    title = CharField(null=True, unique=True)
    year_published = IntegerField()

    class Meta:
        database = pw


class BookResource(ModelResource):
    class Meta:
        name = 'book'
        model = Book

    class Schema:
        year_published = fields.Integer(minimum=1400)


api = Api(app, default_manager=PeeweeManager)
api.add_resource(BookResource)

if not isfile('peewee-simple.db'):
    pw.connect()
    pw.create_tables([Book])

if __name__ == '__main__':
    app.run()
Beispiel #38
0
class AgentBook(object):

    def __init__(self, db):
        """ Initialize the manager for deferred messages and serialized
            information.

            db - absolute path to database file (sqlite)
        """
        self.db = SqliteDatabase(db)
        agent_book_proxy.initialize(self.db)
        self.db.create_tables([AgentInfo, AgentMessage], True)

    def delete_info(self, agent):
        """ Delete stored information for a given agent.

            agent - agent name
        """
        scoutlog.info('deleting stored information of agent %s' % agent)

        try:
            info = AgentInfo.get(AgentInfo.agent == agent)

        except AgentInfo.DoesNotExist:
            scoutlog.error('no information to delete for agent %s' % agent)
            return False

        info.delete_instance()
        return True

    def delete_messages(self, agent):
        """ Delete stored messages for a given agent.

            agent - agent name
        """
        scoutlog.info('deleting stored messages of agent %s' % agent)

        query = AgentMessage.delete().where(AgentMessage.agent == agent)
        query.execute()
        return True

    def get_info(self, agent):
        """ Return the stored information (if any).

            agent - agent name
        """
        scoutlog.info('obtaining stored information of agent %s' % agent)

        try:
            return AgentInfo.get(AgentInfo.agent == agent).info

        except AgentInfo.DoesNotExist:
            scoutlog.error('no information stored for agent %s' % agent)
            return None

    def get_messages(self, agent):
        """ Return all messages for a given agent.

            agent - agent name
        """
        scoutlog.info('obtaining stored stored messages for agent %s' % agent)

        try:
            return [a.message for a in AgentMessage.select().where(
                AgentMessage.agent == agent)]

        except:
            # Not found?
            scoutlog.warning('no messages for agent %s' % agent)
            return []

    def store_info(self, parser):
        """ Store serialized information for a given agent.

            parser - zoe MessageParser instance
        """
        dst = parser.get('agent')
        scoutlog.info('storing information of agent %s' % dst)

        new_map = parser._map.copy()

        # Prepare for direct dispatch
        new_map['dst'] = dst
        del new_map['agent']

        # Original tags removed! (should only have scout ones)
        new_map['tag'] = ['settle!', ]

        # Store message
        raw_msg = zoe.MessageBuilder(new_map).msg()

        try:
            AgentInfo.create(agent=dst, info=raw_msg)

        except IntegrityError as e:
            scoutlog.exception('failed to store information of agent %s' % dst)

    def store_message(self, parser):
        """ Store deferred messages. Convert special tags to their original
            counterparts.

            parser  - zoe MessageParser instance
        """
        dst = parser.get('_outpost_dst')
        src = parser.get('_outpost_src')
        tag = parser.get('_outpost_tag')

        scoutlog.info('storing deferred message for agent %s' % dst)

        new_map = parser._map.copy()

        # Remove unnecessary info
        del new_map['_outpost_dst']
        new_map['dst'] = dst

        if src:
            new_map['src'] = src
            del new_map['_outpost_src']

        if tag:
            new_map['tag'] = tag
            del new_map['_outpost_tag']
        else:
            del new_map['tag']

        # Store message
        raw_msg = zoe.MessageBuilder(new_map, parser._map).msg()
        AgentMessage.create(agent=dst, message=raw_msg)
Beispiel #39
0
class TestBase(unittest.TestCase):
    def populate_database(self):
        self.db = SqliteDatabase('peewee.db')
        self.db.connect()

        self.db.create_tables(model.ALL_MODELS, safe=True)

        for m in model.ALL_MODELS:
            m.delete().execute()

        self.db.close()

        # Config
        release = model.Config.create(app_api_key=APP_API_KEY, messaging_api_key=MESSAGING_API_KEY)

        admin_user = model.User.create_user(name='Administrator', description='Administrator', email='*****@*****.**', username=TEST_USER, password=TEST_PASSWORD)

        # Groups
        admin = model.Group.create(name=TEST_USER, owner=admin_user)
        user = model.Group.create(name='user', owner=admin_user)
        guest = model.Group.create(name='guest', owner=admin_user)

        admin.add_user(admin_user)

        # Users
        model.Device.create(user=admin, name='d2', resource='work', type='computer', dev_id='a')

        chloe = model.User.create_user(name='Chloe', username='******', password=TEST_PASSWORD)
        d = chloe.create_device(name='d2', resource='work', type='computer', dev_id='a')
        chloe.create_device(name='d0', resource='home', type='phone', dev_id='b')
        chloe.create_device(name='d3', resource='home', type='laptop', dev_id='c')
        chloe.create_device(name='d1', resource='work', type='phone', dev_id='d')
        model.UserToGroup.create(user=chloe, group=guest)

        sunshine = model.User.create_user(name='Sunshine', username='******', password=TEST_PASSWORD)
        sunshine.create_device(name='d5', resource='work', type='phone', dev_id='e')
        model.UserToGroup.create(user=sunshine, group=user)
        model.UserToGroup.create(user=sunshine, group=guest)
        p = model.Publication.create(user=sunshine, topic='Life and Times of Sunshine', description='', publish_group=guest, subscribe_group=guest)
        model.Message.create(user=sunshine, to_publication=p, subject='First post!')
        model.Message.create(user=sunshine, to_publication=p, subject='Eating breakfast')
        model.Message.create(user=sunshine, to_publication=p, subject='Time for a nap')

        guinness = model.User.create_user(name='Guinness', username='******', password=TEST_PASSWORD)
        guinness.create_device(name='d7', resource='work', type='phone', dev_id='g')
        model.UserToGroup.create(user=guinness, group=guest)

        felix = model.User.create_user(name='Felix', username='******', password=TEST_PASSWORD)
        felix.create_device(name='d6', resource='work', type='phone', dev_id='f')
        model.UserToGroup.create(user=felix, group=guest)
        model.Subscription.create(user=felix, publication=p)
        model.Message.create(user=felix, to_publication=p, subject='boring...')
        model.Message.create(user=felix, to_user=sunshine, subject='hi sunshine')
        model.Message.create(user=felix, to_device=d, subject='hi chloe')
        model.Message.create(user=felix, to_user=chloe, subject='hi chloe again')

        ducky = model.User.create_user(name='Ducky', username='******', password=TEST_PASSWORD)
        ducky.create_device(name='d8', resource='work', type='phone', dev_id='h')
        model.UserToGroup.create(user=ducky, group=admin)
        model.UserToGroup.create(user=ducky, group=user)
        model.UserToGroup.create(user=ducky, group=guest)

    def setUp(self):
        self.app = app.test_client()
        self.populate_database()

    def request(self, method, url, auth=None, json_data=None, **kwargs):
        headers = kwargs.get('headers', {})

        if auth:
            # Add the auth header if credentials are specified.
            headers['Authorization'] = 'Basic %s' % b64encode(bytes(auth[0] + ':' + auth[1], 'utf-8')).decode('ascii')

        if json:
            headers['Content-Type'] = 'application/json'
            kwargs['data'] = json.dumps(obj=json_data)
            #print(kwargs['data'])

        kwargs['headers'] = headers
        #print(kwargs['headers'])

        return self.app.open(url, method=method, **kwargs)
Beispiel #40
0
from datetime import datetime
from peewee import SqliteDatabase, CharField, DateTimeField, Model

db = SqliteDatabase("clipboard.db")


class Paste(Model):
    text = CharField()
    date = DateTimeField()

    class Meta:
        database = db


db.create_tables([Paste])


def save_new_paste(text):
    paste = Paste(text=text, date=datetime.now())
    paste.save()


def get_lastest_paste():
    for paste in Paste.select().order_by(Paste.date.desc()):
        return paste.text


def get_pastes():
    return Paste.select().order_by(Paste.date.desc())

Beispiel #41
0
import sqlite3
from peewee import Model, SqliteDatabase, DateTimeField, ForeignKeyField, FloatField, CharField
from datetime import datetime


DB = SqliteDatabase('finance-manager.db')
DB.connect()


class User(Model):
    username = CharField(primary_key=True)


    class Meta:
        database = DB


class Amount(Model):
    user = ForeignKeyField(User, backref='amount')
    amount = FloatField()
    date_time = DateTimeField()


    class Meta:
        database = DB

DB.create_tables([User, Amount])
DB.close()
Beispiel #42
0
            ask_volume_1=self.ask_volume_1,
            gateway_name=self.gateway_name,
        )

        if self.bid_price_2:
            tick.bid_price_2 = self.bid_price_2
            tick.bid_price_3 = self.bid_price_3
            tick.bid_price_4 = self.bid_price_4
            tick.bid_price_5 = self.bid_price_5

            tick.ask_price_2 = self.ask_price_2
            tick.ask_price_3 = self.ask_price_3
            tick.ask_price_4 = self.ask_price_4
            tick.ask_price_5 = self.ask_price_5

            tick.bid_volume_2 = self.bid_volume_2
            tick.bid_volume_3 = self.bid_volume_3
            tick.bid_volume_4 = self.bid_volume_4
            tick.bid_volume_5 = self.bid_volume_5

            tick.ask_volume_2 = self.ask_volume_2
            tick.ask_volume_3 = self.ask_volume_3
            tick.ask_volume_4 = self.ask_volume_4
            tick.ask_volume_5 = self.ask_volume_5

        return tick


DB.connect()
DB.create_tables([DbBarData, DbTickData])
Beispiel #43
0
#!/usr/bin/env python

import config
from peewee import SqliteDatabase, Model, FloatField, DateTimeField

database = SqliteDatabase(config.DB_FILE)

class BaseModel(Model):
    class Meta:
        database = database

class DataLog(BaseModel):
    temp = FloatField()
    hum = FloatField()
    timestamp = DateTimeField(index=True)

    class Meta:
        order_by = ('timestamp',)

if __name__ == "__main__":
     database.connect()
     database.create_tables([DataLog])
Beispiel #44
0
database = SqliteDatabase('kongonline.sqlite', check_same_thread=False)
database.connect()


class Stats(Model):
    time_t = IntegerField(null=True)
    online = IntegerField(null=True)
    games = IntegerField(null=True)

    class Meta:
        database = database
        db_table = 'stats'


def write_info(time, online, games):
    Stats.create(time_t=time, online=online, games=games)


def get_last(num=10):
    ret = []
    for item in Stats.select().order_by(Stats.time_t.desc()).limit(num):
        time = dt.fromtimestamp(item.time_t).strftime('%y-%m-%d|%H:%M:%S')
        ret.append((time, item.online, item.games))
    return ret


def count_entries():
    return Stats.select().count()

database.create_tables((Stats,), safe=True)
Beispiel #45
0
def setup_db(name):
    db = SqliteDatabase(name)
    db.create_tables([Location, Statistics])
Beispiel #46
0
class ZoneBook(object):

    def __init__(self, db):
        """ Initialize the manager for agent locations and outpost resource
            storage.

            db - absolute path to database file (sqlite)
        """
        self.db = SqliteDatabase(db)
        zone_book_proxy.initialize(self.db)
        self.db.create_tables([OutpostZone, AgentZone], True)

    def get_agents(self):
        """ Return a list of agents.

            Note that this returns the object rather than just the name in
            case other information is wanted.
        """
        return list(AgentZone.select())

    def get_agent_location(self, name):
        """ Get the location for a given agent.

            Returns None if it does not exist.

            name - name of the agent
        """
        scoutlog.info('obtaining location of agent %s' % name)

        try:
            return AgentZone.get(AgentZone.name == name).location.name

        except Exception as e:
            scoutlog.warning('could not get location of agent %s' % name)
            return None

    def get_agents_in(self, outpost_id):
        """ Get the agents found in a given location.

            outpost_id - outpost id to search for
        """
        scoutlog.info('obtaining agents located in %s' % outpost_id)

        try:
            outpost = OutpostZone.get(OutpostZone.name == outpost_id)

        except OutpostZone.DoesNotExist as e:
            scoutlog.warning('outpost %s not found in zone book' % outpost_id)
            return []

        return outpost.agents

    def get_agent_names_in(self, outpost_id):
        """ Get the agent names found in a given location.

            outpost_id - outpost id to search for
        """
        scoutlog.info('obtaining agent names located in %s' % outpost_id)

        try:
            outpost = OutpostZone.get(OutpostZone.name == outpost_id)

        except OutpostZone.DoesNotExist as e:
            scoutlog.warning('outpost %s not found in zone book' % outpost_id)
            return []

        return [agent.name for agent in outpost.agents]

    def get_outposts(self):
        """ Return a list of outposts.

            Note that this returns the object rather than just the name in
            case other information is wanted.
        """
        return list(OutpostZone.select())


    def is_outpost_running(self, name):
        """ Check if an outpost is known to be running or not.

            Returns boolean value.

            name - name of the outpost
        """
        scoutlog.info('checking if outpost %s is currently running' % name)

        try:
            return OutpostZone.get(OutpostZone.name == name).is_running

        except OutpostZone.DoesNotExist as e:
            scoutlog.warning('outpost %s not found in zone book' % name)
            return None

    def move_agent(self, name, location):
        """ Move an agent to the given location.

            name     - name of the agent
            location - new location of the agent
        """
        scoutlog.info('moving agent %s to %s' % (name, location))

        try:
            outpost = OutpostZone.get(OutpostZone.name == location)

            query = AgentZone.update(location=outpost).where(
                    AgentZone.name == name)
            query.execute()
            return True

        except Exception as e:
            scoutlog.exception('could not update location of agent %s' % name)
            return False

    def refresh_agents(self, agent_list):
        """ Refresh the agents table with those in the given list.

            Agents that do not exist will be created, while agents that no
            longer exist will be removed.

            agent_list - list of agent names
        """
        scoutlog.info('refreshing agent list')

        current_list = [agent.name for agent in AgentZone.select()]
        diff_list = list(set(current_list) - set(agent_list))

        # Add new agents
        central, created = OutpostZone.get_or_create(name='central')

        for agent in agent_list:
            new, created = AgentZone.get_or_create(
                name=agent,
                defaults={'location': central})

        # Remove non-existing agents
        if diff_list:
            query = AgentZone.delete().where(AgentZone.name << diff_list)
            query.execute()

    def refresh_outposts(self, outpost_list):
        """ Refresh the outposts table with those in the given list.

            Outposts that do not exist will be created. Outposts that no longer
            exist are kept for historic purposes and to prevent issues with
            keys.

            outpost_list - ConfigParser instance of the outpost list
        """
        scoutlog.info('refreshing outpost list')

        # Add new outposts
        for outpost in filter(
            (lambda o: o.startswith('outpost ')), outpost_list.sections()):

            name = outpost.replace('outpost ', '', 1)
            outpost, created = OutpostZone.get_or_create(name=name)

    def set_outpost_running(self, name, value):
        """ Change the `is_running` flag of an outpost to indicate whether it
            has been started or not.

            name  - name of the outpost to change
            value - boolean indicating whether it is running or not
        """
        scoutlog.info('setting running status of outpost %s to: %r' % (
            name, value))

        try:
            query = OutpostZone.update(is_running=value).where(
                    OutpostZone.name == name)
            query.execute()
            return True

        except Exception as e:
            scoutlog.exception('could not set running status of outpost %s' %
                    name)
            return False

    def store_agent_resources(self, name, **info):
        """ Update the resources for a given agent.

            If no record for an outpost exists, it will be created.

            name - name of the agent
            info - parameters containing the information
        """
        scoutlog.info('updating resources of agent %s' % name)

        # Create agent record if necessary
        agent, created = AgentZone.get_or_create(
            name=name,
            defaults=info)

        if created:
            # Was updated when created
            return True

        # Update information
        try:
            query = AgentZone.update(**info).where(
                    AgentZone.name == name)
            query.execute()
            return True

        except Exception as e:
            scoutlog.exception('error while updating resources of %s' % name)
            return False
Beispiel #47
0
    def open(self, *args, **kwargs):
        cmd = [s.replace('\ ', ' ') for s in
               re.split(r'(?<!\\) ', self.command)] + list(args)
        print(str(cmd))
        ukwargs = self.DEFAULT_POPEN_ARGS.copy()
        if self.shell:
            ukwargs.update(self.SHELL_ALL_DEVNULL)
        ukwargs.update(kwargs)
        return Popen(cmd, **ukwargs)

    class Meta:
        database = os_db


# safely create FileCommand table, its const records are referenced in FileType
os_db.create_tables([FileCommand], safe=True)


class FileType(BaseModel):
    type = CharField()
    mime = CharField(null=True, default=None)
    command = ForeignKeyField(FileCommand, null=True, default=None)

    DIR = Record(id=1, type="Directory")
    PDF = Record(id=2, type="PDF", command=FileCommand.OPEN)
    TXT = Record(id=3, type="TXT", command=FileCommand.OPEN)
    XML = Record(id=4, type="XML", command=FileCommand.OPEN)
    ASP = Record(id=5, type="AS Project", command=FileCommand.ANDROID_STUDIO)

    OTH = Record(id=10, type="unknown file")
Beispiel #48
0
from peewee import BooleanField, IntegerField, Model, SqliteDatabase, TextField

db = SqliteDatabase('gen/data.db')


class User(Model):
    user_id = IntegerField(unique=True)
    chat_id = IntegerField()
    first_name = TextField()
    last_name = TextField(null=True)
    is_member = BooleanField()  # Current status of user (member/left)

    class Meta:
        database = db


db.create_tables([User], safe=True)
Beispiel #49
0
                operator = 'TELE2'
            # network status
            network_status = 'unknown'
            if network_info['State'] == 'HomeNetwork':
                network_status = 'online'
            modem_str += 'IMEI: {}, SIM IMSI: {}, Status: {}, Device: {}, Operator: {}, Network: {}, LAC: {}, CID: {}'.format(
                state_machine.GetIMEI(),
                state_machine.GetSIMIMSI(),
                network_status,
                state_machine.GetConfig()['Device'],
                operator,
                network_info['NetworkCode'],
                network_info['LAC'],
                network_info['CID']
                    )
            modem_str += '\n'
    except gammu.ERR_TIMEOUT:
        return 'Modem timeout problem, please reload page'
    return modem_str


def main():
    cli()


if __name__ == '__main__':
    db.connect()
    db.create_tables([Message, Number], safe=True)
    main()
    db.close()
Beispiel #50
0
class TestModel(unittest.TestCase):
    def setUp(self):
        self.db = SqliteDatabase('peewee.db')
        self.db.connect()

        self.db.create_tables([Device, Group, User, UserToGroup, Publication], safe=True)

        Device.delete().execute()
        Group.delete().execute()
        User.delete().execute()
        UserToGroup.delete().execute()
        Publication.delete().execute()

        self.user0 = User.create_user(name='user0name', username='******', password='******')
        self.user0.create_device(name='device0name', resource='device0resource', type='device0type', dev_id='device0id', reg_id='device0regid')

        self.user1 = User.create_user(name='user1name', username='******', password='******')
        self.user1.create_device(name='device1name', resource='device1resource', type='device1type', dev_id='device1id')

        self.group0 = Group.create(name='group0name', description='group0description', owner=self.user0)
        self.group0.add_user(user=self.user0)
        self.group0.add_user(user=self.user1)

        self.group1 = Group.create(name='group1name', description='group1description', owner=self.user0)
        self.group1.add_user(user=self.user0)

        self.group2 = Group.create(name='group2name', description='group2description', owner=self.user1)
        self.group2.add_user(user=self.user1)

        self.pub0 = Publication.create(user=self.user0, topic='pub0topic', description='pub0description', publish_group=self.group1, subscribe_group=self.group0)

    def tearDown(self):
        self.db.close()
        self.db = None

    def test_group_get(self):
        os = Group.select()
        self.assertEqual(len(os), 3)

        o = Group.select().where(Group.name == 'group0name').get()
        self.assertEqual(o, self.group0)

        o = Group.select().where(Group.id == self.group0.id).get()
        self.assertEqual(o, self.group0)
        self.assertTrue(o.owner_id)

    def test_group_memberships(self):
        self.assertTrue(self.group0.is_member(self.user0))
        self.assertTrue(self.group0.is_member(self.user1))

        self.assertTrue(self.group1.is_member(self.user0))
        self.assertFalse(self.group1.is_member(self.user1))

        self.assertFalse(self.group2.is_member(self.user0))
        self.assertTrue(self.group2.is_member(self.user1))

    def test_get_reg_ids_by_user_id(self):
        user_id = self.user0.id

        devices = Device.select(Device, User).join(User).where(User.id == user_id)
        self.assertEqual(len(devices), 1)

        reg_ids = [d.reg_id for d in devices]
        self.assertEqual(reg_ids, ['device0regid'])
Beispiel #51
0
class Operation(BaseModel):
    timestamp = DateTimeField(primary_key=True)
    user = ForeignKeyField(User, related_name="operations", null=True)
    chamber = ForeignKeyField(Chamber, related_name='chamber', null=True)
    action = CharField(null=True)
    locker = CharField(default='Dev Locker')

    def __repr__(self):
        return '<TRANSACTION> {}'.format(self.timestamp)

    @staticmethod
    @db.atomic()
    def add_operation(chamber=None, action=None, user=None):
        now = datetime.utcnow()
        op = Operation.create(timestamp=now, chamber=chamber,
                              action=action, user=user)
        op.save()
        return op

# this creates indexes, not sure why
tables = [User, Operation, Chamber]
db.create_tables(tables, True)


# ERASE TABLES
def drop_tables():
    try:
        db.drop_tables(tables)
    except:
        pass
Beispiel #52
0
        database = db


def get_user(user_id):
    try:
        user = User.get(User.id == user_id)
    except User.DoesNotExist:
        user = User.get(User.id == 0)
    return user

if __name__ == "__main__":
    if User.table_exists():
        print("Database Already Exists")
        sys.exit()

    db.connect()
    db.create_tables([User])

    User.insert_many([
        {"id": 0, "name": "Invlid User", "langpair": "!!|!!"},
        {"id": 120755813, "name": "Boris", "langpair": "ru|en"},
        {"id": 93649293, "name": "Elizaveta", "langpair": "ru|en"},
        {"id": 77815902, "name": "James", "langpair": "en|ru"},
        {"id": 68382468, "name": "Jill", "langpair": "en|ru"},
        {"id": 82080280, "name": "Kyle", "langpair": "en|ru", "admin": True},
        {"id": 117778855, "name": "Mickey", "langpair": "en|ru"},
        {"id": 97384423, "name": "Nataly", "langpair": "__|__"},
        {"id": 96351689, "name": "Transbotor", "langpair": "!!|!!"},
    ]).execute()
    db.close()
Beispiel #53
0
from peewee import SqliteDatabase, Model, \
    CharField, IntegerField, DateField, BlobField

db = SqliteDatabase(':memory:', threadlocals=True)

class BaseModel(Model):
    class Meta:
        database = db

class Vehicle(BaseModel):
    description = CharField(null=False)
    cylinder = IntegerField(null=False)
    brand = CharField(null=False)
    year = DateField(formats="%Y")
    owner = CharField(null=False)
    photo = BlobField(null=True)

db.connect()
db.create_tables([Vehicle])