Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor bugfixes #63

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ src/static/raw_docs
.env
.idea/
venv/
dist/
2,154 changes: 1,072 additions & 1,082 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ skip_gitignore = true
name = "academyruins-api"
version = "0.5.0"
description = ""
authors = ["Your Name <you@example.com>"]
authors = ["Václav Luňák < >"]
readme = "README.md"

[tool.poetry.scripts]
academyruins = "src.cli_scripts.run:app"

[tool.poetry.dependencies]
python = "^3.10"
fastapi = { extras = ["all"], version = "^0.103.2" }
logging = "^0.4.9.6"
APScheduler = "^3.9.1"
boto3 = "^1.24.84"
pydantic = "^2.4.2"
Expand Down
12 changes: 6 additions & 6 deletions src/admin/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Response
from sqlalchemy.orm import Session

from admin import service
from db import get_db
from extractor.cr.refresh_cr import refresh_cr
from extractor.ipg.refresh_ipg import refresh_ipg
from extractor.mtr.refresh_mtr import refresh_mtr
from schemas import ResponseModel
from src.admin import service
from src.db import get_db
from src.extractor.cr.refresh_cr import refresh_cr
from src.extractor.ipg.refresh_ipg import refresh_ipg
from src.extractor.mtr.refresh_mtr import refresh_mtr
from src.schemas import ResponseModel

router = APIRouter(include_in_schema=False)

Expand Down
10 changes: 5 additions & 5 deletions src/admin/service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from sqlalchemy import select
from sqlalchemy.orm import Session

from cr.models import Cr, PendingCr
from diffs.models import CrDiff, CrDiffItem, MtrDiff, PendingCrDiff, PendingMtrDiff
from link.models import PendingRedirect, Redirect
from mtr.models import Mtr, PendingMtr
from mtr.service import get_pending_mtr
from src.cr.models import Cr, PendingCr
from src.diffs.models import CrDiff, CrDiffItem, MtrDiff, PendingCrDiff, PendingMtrDiff
from src.link.models import PendingRedirect, Redirect
from src.mtr.models import Mtr, PendingMtr
from src.mtr.service import get_pending_mtr


def apply_pending_redirect(db: Session, resource: str) -> str | None:
Expand Down
20 changes: 10 additions & 10 deletions create_cr_and_diff.py → src/cli_scripts/create_cr_and_diff.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
import os

from difftool.diffmaker import CRDiffMaker
from extractor.cr import extract_cr
from extractor.formatter import CRFormatterFactory
from src.difftool.diffmaker import CRDiffMaker
from src.extractor.cr import extract_cr
from src.extractor.formatter import CRFormatterFactory


def diff(old_txt, new_txt, old_set_code=None, new_set_code=None, forced_matches=None):
Expand Down Expand Up @@ -67,13 +67,13 @@ def diffall():
diff_save(old, new)


cr_in_dir = "src/static/raw_docs/cr"
cr_out_dir = "./gen/cr"
diff_dir = "./gen/diff_unchecked"
maps_dir = "./gen/map"
gloss_dir = "./gen/gloss"
key_dir = "./gen/keywords"
toc_dir = "./gen/toc"
cr_in_dir = "../static/raw_docs/cr"
cr_out_dir = "../../gen/cr"
diff_dir = "../../gen/diff_unchecked"
maps_dir = "../../gen/map"
gloss_dir = "../../gen/gloss"
key_dir = "../../gen/keywords"
toc_dir = "../../gen/toc"

if __name__ == "__main__":
# diffall()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from dotenv import load_dotenv

from difftool.diffmaker import MtrDiffMaker
from extractor.mtr.extract_mtr import extract
from src.difftool.diffmaker import MtrDiffMaker
from src.extractor.mtr.extract_mtr import extract

load_dotenv()

Expand Down Expand Up @@ -47,8 +47,8 @@ def diff_save(old: str, new: str):
file.write(mtr_to_text(new_date, new_sections))


mtr_out_dir = Path(".") / "gen" / "mtr"
mtr_diff_dir = Path(".") / "gen" / "mtr-diff"
mtr_out_dir = Path("../..") / "gen" / "mtr"
mtr_diff_dir = Path("../..") / "gen" / "mtr-diff"

batch_input = [
# "2023-03-29",
Expand All @@ -66,7 +66,7 @@ def diff_save(old: str, new: str):


def diff_batch():
mtrdir = Path("mtr")
mtrdir = Path("../../mtr")
for i in range(1, len(batch_input)):
old = mtrdir / ("mtr-" + batch_input[i] + ".pdf")
new = mtrdir / ("mtr-" + batch_input[i - 1] + ".pdf")
Expand Down
35 changes: 35 additions & 0 deletions src/cli_scripts/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Annotated, Union

import typer
import uvicorn
from dotenv import load_dotenv

app = typer.Typer()


@app.command()
def run(
port: int = 8000,
host: str = "localhost",
reload: Annotated[
bool, typer.Option(help="Whether the server should automatically restart on file change.")
] = False,
):
"""
Start the API web server.
"""
uvicorn.run("src.main:app", port=port, host=host, reload=reload)


@app.callback()
def options(
envfile: Annotated[
Union[str, None], typer.Option(help="Optional location of a .env file that should be loaded")
] = None
):
if envfile:
load_dotenv(dotenv_path=envfile)


if __name__ == "__main__":
app()
2 changes: 1 addition & 1 deletion src/cr/keyword_def.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from cr import service as cr_service
from src.cr import service as cr_service

keyword_regex = r"702.(?:[2-9]|\d\d+)"
keyword_action_regex = r"701.(?:[2-9]|\d\d+)"
Expand Down
2 changes: 1 addition & 1 deletion src/cr/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sqlalchemy import Column, Date, Integer, String, Text
from sqlalchemy.dialects.postgresql import JSONB

from models import Base
from src.models import Base


class Cr(Base):
Expand Down
16 changes: 8 additions & 8 deletions src/cr/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
from sqlalchemy.orm import Session
from thefuzz import fuzz, process

from cr import schemas, service
from cr.keyword_def import get_best_rule
from db import get_db
from openapi.no422 import no422
from openapi.strings import crTag, filesTag
from resources import static_paths as paths
from resources.cache import GlossaryCache
from schemas import Error, FileFormat
from src.cr import schemas, service
from src.cr.keyword_def import get_best_rule
from src.db import get_db
from src.openapi.no422 import no422
from src.openapi.strings import crTag, filesTag
from src.resources import static_paths as paths
from src.resources.cache import GlossaryCache
from src.schemas import Error, FileFormat

router = APIRouter()
glossary = GlossaryCache()
Expand Down
6 changes: 3 additions & 3 deletions src/cr/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from pydantic import Field

from diffs.schemas import CrDiffMetadata
from schemas import Error, ResponseModel
from src.diffs.schemas import CrDiffMetadata
from src.schemas import Error, ResponseModel


class RuleError(Error):
Expand Down Expand Up @@ -72,6 +72,6 @@ class TraceDiffRule(ResponseModel):

class TraceItem(ResponseModel):
action: TraceItemAction
old: TraceDiffRule | None
old: TraceDiffRule | None = None
new: TraceDiffRule
diff: CrDiffMetadata
10 changes: 5 additions & 5 deletions src/cr/service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from sqlalchemy import select
from sqlalchemy.orm import Session

from cr import utils
from cr.models import Cr
from cr.schemas import TraceItem
from diffs.models import CrDiff, CrDiffItem
from diffs.schemas import CrDiffMetadata
from src.cr import utils
from src.cr.models import Cr
from src.cr.schemas import TraceItem
from src.diffs.models import CrDiff, CrDiffItem
from src.diffs.schemas import CrDiffMetadata


def get_latest_cr(db: Session) -> Cr:
Expand Down
6 changes: 3 additions & 3 deletions src/cr/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from cr.schemas import TraceDiffRule, TraceItem, TraceItemAction
from diffs.models import CrDiffItem, DiffItemKind
from diffs.schemas import CrDiffMetadata
from src.cr.schemas import TraceDiffRule, TraceItem, TraceItemAction
from src.diffs.models import CrDiffItem, DiffItemKind
from src.diffs.schemas import CrDiffMetadata


def format_trace_item(db_item: CrDiffItem) -> TraceItem:
Expand Down
2 changes: 1 addition & 1 deletion src/diffs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import relationship

from models import Base
from src.models import Base


class CrDiff(Base):
Expand Down
10 changes: 5 additions & 5 deletions src/diffs/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session

from db import get_db
from diffs import schemas, service
from diffs.models import PendingCrDiff
from difftool.diffsorter import CRDiffSorter
from openapi.strings import diffTag
from src.db import get_db
from src.diffs import schemas, service
from src.diffs.models import PendingCrDiff
from src.difftool.diffsorter import CRDiffSorter
from src.openapi.strings import diffTag

router = APIRouter(tags=[diffTag.name])

Expand Down
20 changes: 10 additions & 10 deletions src/diffs/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from pydantic import Field

from mtr.schemas import MtrChunk
from schemas import Error, ResponseModel
from src.mtr.schemas import MtrChunk
from src.schemas import Error, ResponseModel


class CrDiffError(Error):
Expand All @@ -16,8 +16,8 @@ class MtrDiffError(Error):


class CRDiffError(Error):
old: str
new: str
old: str | None = None
new: str | None = None


class CRDiffRule(ResponseModel):
Expand All @@ -26,8 +26,8 @@ class CRDiffRule(ResponseModel):


class CRDiffItem(ResponseModel):
old: CRDiffRule | None
new: CRDiffRule | None
old: CRDiffRule | None = None
new: CRDiffRule | None = None


class CRMoveItem(ResponseModel):
Expand All @@ -50,8 +50,8 @@ class CRDiffNavigation(ResponseModel):
class CRDiff(CrDiffMetadata):
creation_day: datetime.date = Field(..., alias="creationDay")
changes: list[CRDiffItem] = Field(...)
moves: list[CRMoveItem] = Field(description="List of moved rules")
nav: CRDiffNavigation | None = Field(description="Navigational information.")
moves: list[CRMoveItem] = Field([], description="List of moved rules")
nav: CRDiffNavigation | None = Field(None, description="Navigational information.")


class PendingCRDiff(ResponseModel):
Expand All @@ -64,8 +64,8 @@ class PendingCRDiffResponse(ResponseModel):


class MtrDiffItem(ResponseModel):
old: MtrChunk | None
new: MtrChunk | None
old: MtrChunk | None = None
new: MtrChunk | None = None


class MtrDiff(ResponseModel):
Expand Down
6 changes: 3 additions & 3 deletions src/diffs/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from sqlalchemy import select
from sqlalchemy.orm import Session, aliased

from cr.models import Cr
from diffs.models import CrDiff, CrDiffItem, MtrDiff, PendingCrDiff, PendingMtrDiff
from mtr.models import Mtr
from src.cr.models import Cr
from src.diffs.models import CrDiff, CrDiffItem, MtrDiff, PendingCrDiff, PendingMtrDiff
from src.mtr.models import Mtr


def get_cr_diff(db: Session, old_code: str | None, new_code: str | None) -> CrDiff | None:
Expand Down
6 changes: 3 additions & 3 deletions src/difftool/diffmaker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from dataclasses import dataclass

from .diffsorter import CRDiffSorter, DiffSorter, MtrDiffSorter
from .itemdiffer import CRItemDiffer, ItemDiffer, MtrItemDiffer
from .matcher import CRMatcher, Matcher, MtrMatcher
from src.difftool.diffsorter import CRDiffSorter, DiffSorter, MtrDiffSorter
from src.difftool.itemdiffer import CRItemDiffer, ItemDiffer, MtrItemDiffer
from src.difftool.matcher import CRMatcher, Matcher, MtrMatcher


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion src/difftool/matcher.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import difflib
from abc import ABC, abstractmethod

from .matchscoregraph import MatchScoreGraph
from src.difftool.matchscoregraph import MatchScoreGraph


class Matcher(ABC):
Expand Down
6 changes: 3 additions & 3 deletions src/extractor/cr/extract_cr.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
import re

from cr.keyword_def import ability_words_rule, keyword_action_regex, keyword_regex
from cr.schemas import ToCSection, ToCSubsection
from resources import static_paths as paths
from src.cr.keyword_def import ability_words_rule, keyword_action_regex, keyword_regex
from src.cr.schemas import ToCSection, ToCSubsection
from src.resources import static_paths as paths

# TODO rework into new class hierarchy

Expand Down
Loading