Skip to content

Instantly share code, notes, and snippets.

@cmutel
Created May 3, 2023 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmutel/b3ad103d6b89bfde9ff1693f8b8e0c09 to your computer and use it in GitHub Desktop.
Save cmutel/b3ad103d6b89bfde9ff1693f8b8e0c09 to your computer and use it in GitHub Desktop.

The aim of bw_projects is relatively simple - we want to store data in a SQLite file and in a few subdirectories, and we want to be able to do switch this SQLite file and associated subdirectories whenever we want.

To do this we use peewee as an ORM to SQLite, and its ability to bind ORM tables to a database object after initial import:

    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

    @property
    def db(self):
        return self._database

    def change_path(self, filepath):
        self.db.close()
        self._filepath = filepath
        self._database = self._create_database()

This isn't necessarily pretty, but it works.

Objective: This behaviour should be explicitly tested (it is implicitly tested as we use it in many tests)

The SQLite database is very simple, it has one table with three columns:

    data = PickleField()
    name = TextField(index=True, unique=True)
    full_hash = BooleanField(default=True)

The use of pickle is unnecessary. The full_hash column can be dropped and all code relating to full_hash can be deleted.

Objective: Shift to a datetime-aware JSON column instead of using Pickle

The current path to the subdirectories is implicit, but this can cause problems.

Objective: The projects table should have a changed handling of paths. There should be a base_projects_directory or similar (which should be the same as it is right now; this is platform dependent), and another project_directory which is relative to base_projects_directory. A Project object returned by querying the projects.db database with peewee should have a property that gives the absolute path (as a pathlib.Path) to the base_projects_directory / project_directory.

We also need logs_dir and output_dir, these should also be columns with defaults coming from platformdirs (see below).

Loading the library should not create any directories or do filesystem operations - there needs to be an initiation step which would depend on the consuming libraries.

Normally there is only one database storing information on projects. It should be called projects.db. Its filepath is determined by platformdirs.

Objective: Replace use of appdirs with platformdirs. The projects.db file should be stored in a directory appropriate to the bw_projects library (i.e. this needs to be updated).

Tests are OK, but could be improved

Objective: A test decorator uses a new projects.db in a temporary directory. A second decorator does the same but also calls the initialization function to create subdirectories and set base_projects_directory

Downstream libraries need to know when a project changes.

Objective: There should be a few signal objects that libraries can attach to with specified interfaces, e.g. project_activated(name, path). I think activated, deactivated, created and deleted should be enough.

Objective: Project objects and the database ORM should support the following:

  • Create and delete projects
  • contains using project name
  • Iterate over project objects
  • len is number of projects
  • repr like in existing code
  • Delete should be soft-delete, i.e. add True to a deleted column, so it can be restored. A kwarg can force a hard delete.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment