Skip to content

Commit

Permalink
Merge pull request #26 from samudradev/on_start
Browse files Browse the repository at this point in the history
🚀 BUMP: v0.9.1
  • Loading branch information
Thaza-Kun authored Mar 29, 2023
2 parents be2f1c2 + 6584211 commit 60ea916
Show file tree
Hide file tree
Showing 31 changed files with 558 additions and 1,506 deletions.
1,379 changes: 501 additions & 878 deletions poetry.lock

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
[tool.poetry]
name = "samudra"
version = "0.9.0"
version = "0.9.1"
description = ""
authors = ["Thaza_Kun <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.8"
pytomlpp = "^1.0.11"
fastapi = "^0.79.0"
uvicorn = "^0.18.2"
pydantic = "^1.9.1"
pandas = "^1.4.3"
openpyxl = "^3.0.10"
python-dotenv = "^0.20.0"
psycopg2 = { version = "^2.9.3", optional = true }
cryptography = { version = "^37.0.4", optional = true }
Expand All @@ -23,8 +18,6 @@ bcrypt = "^3.2.2"
python-multipart = "^0.0.5"
peewee = "^3.15.1"
requests = "^2.28.1"
typer = "^0.6.1"
rich = "^12.6.0"

[tool.poetry.extras]
mysql = ["cryptography", "PyMySQL"]
Expand Down
6 changes: 3 additions & 3 deletions samudra/conf/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from samudra.conf import get_database
from samudra.conf.database.core import get_active_database
from samudra.conf.database.options import DatabaseEngine
from samudra.models.base import database_proxy

try:
settings = toml.load("conf.toml")
Expand All @@ -19,5 +18,6 @@ def access_database(local: bool = True, name: str = None) -> peewee.Database:
raise NotImplementedError("Only local database is implemented")


def bind_proxy_with_active_database() -> None:
database_proxy.initialize(get_active_database())
def bind_proxy_with_active_database(proxy: peewee.Database) -> peewee.Database:
proxy.initialize(get_active_database())
return proxy
2 changes: 0 additions & 2 deletions samudra/core/crud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
- [golongan_kata][samudra.core.crud.golongan_kata]
"""

from .cakupan import *
from .golongan_kata import *
from .kata_asing import *
from .konsep import *
from .lemma import *
33 changes: 0 additions & 33 deletions samudra/core/crud/cakupan.py

This file was deleted.

4 changes: 3 additions & 1 deletion samudra/core/crud/golongan_kata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@


def create_golongan_kata(data: schemas.CreateGolonganKata) -> models.GolonganKata:
return models.GolonganKata.create(id=data.id, nama=data.nama, keterangan=data.keterangan)
return models.GolonganKata.create(
id=data.id, nama=data.nama, keterangan=data.keterangan
)
33 changes: 0 additions & 33 deletions samudra/core/crud/kata_asing.py

This file was deleted.

46 changes: 4 additions & 42 deletions samudra/core/crud/lemma.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import List, Optional, Any
from typing import List

from peewee import prefetch

from samudra import models
from samudra.schemas.input.query_filter import QueryFilter
from samudra.interfaces import LemmaBuilder


def create_lemma(lemma: str, force: bool = False) -> models.Lemma:
Expand All @@ -13,45 +12,8 @@ def create_lemma(lemma: str, force: bool = False) -> models.Lemma:
return value


def get_lemma_minimum_info(
query: Optional[QueryFilter] = None, where: Any = None
) -> List[models.Lemma]:
query: QueryFilter = QueryFilter(**query.dict())
stmt = models.Lemma.select().where(where)

to_return = prefetch(
stmt,
models.Konsep,
models.CakupanXKonsep,
models.Cakupan.select().where(
(
models.Cakupan.nama.in_(query.cakupan)
if query.cakupan is not None
else None
)
),
models.KataAsingXKonsep,
models.KataAsing.select().where(
(
models.KataAsing.nama.in_(query.kata_asing)
if query.kata_asing is not None
else None
)
),
)
return to_return


def get_lemma(query: QueryFilter) -> List[models.Lemma]:
return get_lemma_minimum_info(query=query)


def get_lemma_by_name(nama: str, query: QueryFilter) -> List[models.Lemma]:
return get_lemma_minimum_info(where=models.Lemma.nama == nama, query=query)


def get_lemma_by_id(lemma_id: int, query: QueryFilter) -> List[models.Lemma]:
return get_lemma_minimum_info(where=(models.Lemma.id == lemma_id), query=query)
def get_lemma(nama: str) -> List[models.Lemma]:
return LemmaBuilder(nama=nama).get_cakupan().get_kata_asing().query()


def delete_lemma(lemma: models.Lemma) -> int:
Expand Down
5 changes: 0 additions & 5 deletions samudra/core/stats/__init__.py

This file was deleted.

34 changes: 34 additions & 0 deletions samudra/interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import List
from peewee import JOIN

import peewee as pw
from samudra import models


class LemmaBuilder:
_query_stmt = models.Lemma.select()

def __init__(self, nama: str) -> None:
self._query_stmt = self._query_stmt.where(models.Lemma.nama == nama)
self.get_konsep()

def get_konsep(self) -> "LemmaBuilder":
self._query_stmt = self._query_stmt.join_from(
models.Lemma, models.Konsep, JOIN.LEFT_OUTER
)
return self

def get_cakupan(self) -> "LemmaBuilder":
self._query_stmt = self._query_stmt.join_from(
models.Konsep, models.CakupanXKonsep, JOIN.LEFT_OUTER
).join(models.Cakupan, JOIN.LEFT_OUTER)
return self

def get_kata_asing(self) -> "LemmaBuilder":
self._query_stmt = self._query_stmt.join_from(
models.Konsep, models.KataAsingXKonsep, JOIN.LEFT_OUTER
).join(models.KataAsing, JOIN.LEFT_OUTER)
return self

def query(self) -> List[models.Lemma]:
return pw.prefetch(self._query_stmt)
12 changes: 10 additions & 2 deletions samudra/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from samudra.conf.setup import bind_proxy_to_database

from samudra.models.base import database_proxy
from samudra.models import create_tables

def start_app() -> None:
bind_proxy_to_active_databse()

def on_start() -> None:
database = bind_proxy_to_database(proxy=database_proxy)
create_tables(database=database)


def on_shutdown() -> None:
...


if __name__ == "__main__":
Expand Down
31 changes: 2 additions & 29 deletions samudra/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,10 @@
# One <== Many
# Many <==> Many
# ```
from typing import List, Type
from typing import List

import peewee

from .auth.pengguna import Pengguna, Keizinan
from .base import BaseDataTable
from .core.cakupan import Cakupan, CakupanXKonsep
from .core.kata_asing import KataAsing, KataAsingXKonsep
from .core.konsep import Konsep, GolonganKata

# Ordered by table hierarchy
from .core.lemma import Lemma
from .experimental.petikan import Petikan, PetikanXKonsep, SumberPetikan


def create_tables(
database: peewee.Database,
Expand All @@ -40,29 +30,12 @@ def create_tables(
Args:
database (peewee.Database): The database engine to bind the models
auth (bool, optional): Whether to include auth tables or not. Defaults to True
foreign_lang (bool, optional): Whether to include foreign lang tables or not. Defaults to True
experimental (bool, optional): Whether to include experimental tables or not. Defaults to False
Returns:
List of tables created
"""
tables = bind_to_database(database, auth, experimental)
tables = database.get_tables()
database.create_tables(tables)
# TODO logging
return database.get_tables()


# Binding to database at runtime is time-consuming
# TODO ? REMOVE this in favor of db proxy init
def bind_to_database(
database: peewee.Database, auth: bool = True, experimental: bool = False
):
tables: List[Type[peewee.Model]] = []
tables.extend([Lemma, Konsep, GolonganKata, *Cakupan.with_dependencies()])
tables.extend([*KataAsing.with_dependencies()])
if auth:
tables.extend([Pengguna, Keizinan])
if experimental:
tables.extend([Petikan, PetikanXKonsep, SumberPetikan])
database.bind(tables)
return tables
18 changes: 0 additions & 18 deletions samudra/schemas/__init__.py

This file was deleted.

Empty file removed samudra/schemas/input/__init__.py
Empty file.
7 changes: 0 additions & 7 deletions samudra/schemas/input/accepted_fields.py

This file was deleted.

Loading

0 comments on commit 60ea916

Please sign in to comment.