Skip to content

Commit

Permalink
Merge pull request #54
Browse files Browse the repository at this point in the history
v3.2.0
  • Loading branch information
MatteoCampinoti94 authored Oct 7, 2024
2 parents d791ef9 + a92b843 commit 700bfdd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion acacore/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.1.1"
__version__ = "3.2.0"
37 changes: 37 additions & 0 deletions acacore/database/upgrade.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import reduce
from json import dumps
from json import loads
from sqlite3 import Connection
Expand Down Expand Up @@ -179,6 +180,40 @@ def convert_action_data(data: dict) -> dict | None:
return set_db_version(conn, Version("3.0.7"))


def upgrade_3_1to3_2(conn: Connection) -> Version:
def convert_action_data(data: dict) -> dict:
if not data.get("convert"):
pass
elif data["convert"]["tool"] == "copy":
if data["convert"].get("outputs"):
del data["convert"]["outputs"]
elif o := reduce(
lambda a, c: a or (c if c in data["convert"]["outputs"] else None),
["ods", "odt", "odp", "svg", "html", "xml", "svg", "msg"],
None,
):
data["convert"]["output"] = o
del data["convert"]["outputs"]
else:
data["convert"]["output"] = data["convert"]["outputs"][0]
del data["convert"]["outputs"]

return data

conn.executemany(
"update Files set action_data = ? where uuid = ?",
(
(dumps(action_data), uuid)
for uuid, action_data_raw in conn.execute("select uuid, action_data from Files where action_data != '{}'")
if (action_data := convert_action_data(loads(action_data_raw)))
),
)

conn.commit()

return set_db_version(conn, Version("3.2.0"))


def get_upgrade_function(current_version: Version, latest_version: Version) -> Callable[[Connection], Version]:
if current_version < Version("2.0.0"):
return upgrade_1to2
Expand All @@ -192,6 +227,8 @@ def get_upgrade_function(current_version: Version, latest_version: Version) -> C
return upgrade_3_0_2to3_0_6
elif current_version < Version("3.0.7"):
return upgrade_3_0_6to3_0_7
elif current_version < Version("3.2.0"):
return upgrade_3_1to3_2
elif current_version < latest_version:
return lambda c: set_db_version(c, Version(__version__))
else:
Expand Down
16 changes: 4 additions & 12 deletions acacore/models/reference_files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from typing import Any
from typing import get_args as get_type_args
from typing import Literal
from typing import Self
Expand Down Expand Up @@ -113,23 +112,16 @@ class ConvertAction(NoDefaultsModel):
Class representing an action to convert a file to a different format.
:ivar tool: The converter to use for the conversion.
:ivar outputs: The list of file types to convert to.
:ivar output: The output target for the converter.
"""

tool: str
outputs: list[str] = Field(default_factory=list)

# noinspection PyNestedDecorators
@field_validator("outputs", mode="before")
@classmethod
def _validate_outputs(cls, value: Any) -> list[str]: # noqa: ANN401
"""Allow a single string to be used as value."""
return [value] if isinstance(value, str) else value
output: str | None = None

@model_validator(mode="after")
def _validate_model(self) -> Self:
if not self.tool == "copy" and not self.outputs:
raise ValueError("Missing outputs.")
if not self.tool == "copy" and not self.output:
raise ValueError("Missing output.")
return self


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "acacore"
version = "3.1.1"
version = "3.2.0"
description = ""
authors = ["Matteo Campinoti <[email protected]>"]
license = "GPL-3.0"
Expand Down
3 changes: 2 additions & 1 deletion tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from acacore.models.file import File
from acacore.models.history import HistoryEntry
from acacore.models.reference_files import Action
from acacore.models.reference_files import ConvertAction


@pytest.fixture()
Expand Down Expand Up @@ -50,7 +51,7 @@ def test_file(test_files: Path, test_files_data: dict[str, dict]) -> File:
action: Action = Action(
name=filedata["matches"]["format"],
action="convert",
convert={"tool": "convertool", "outputs": ["odt", "pdf"]},
convert=ConvertAction(tool="convertool", output="odt"),
)
file: File = File.from_file(file_path)

Expand Down

0 comments on commit 700bfdd

Please sign in to comment.