Skip to content

Commit

Permalink
Merge pull request #37
Browse files Browse the repository at this point in the history
v2.0.2
  • Loading branch information
MatteoCampinoti94 authored Aug 6, 2024
2 parents 944d06e + 3ace8f6 commit 1246ae0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion acacore/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.1"
__version__ = "2.0.2"
11 changes: 9 additions & 2 deletions acacore/database/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,21 @@ def _schema_to_column(name: str, schema: dict, defs: dict[str, dict] | None = No
to_entry, from_entry = schema["enum"][0].__class__ if schema["enum"] else str, str
elif schema_type in ("object", "array"):
sql_type = "text"
to_entry, from_entry = lambda o: dumps(dump_object(o), default=str), lambda o: loads(o)
to_entry, from_entry = (
lambda o: None if o is None else dumps(dump_object(o), default=str),
lambda o: None if o is None else loads(o),
)
elif type_name in _sql_schema_type_converters:
to_entry, from_entry = _sql_schema_type_converters[type_name]
else:
raise TypeError(f"Cannot recognize type from schema {schema!r}")
elif schema_any_of:
if not schema_any_of[0] or len(schema_any_of) > 2:
sql_type, to_entry, from_entry = "text", lambda o: dumps(dump_object(o), default=str), lambda x: loads(x)
sql_type, to_entry, from_entry = (
"text",
lambda x: None if x is None else dumps(dump_object(x), default=str),
lambda x: None if x is None else loads(x),
)
else:
return _schema_to_column(name, {**schema_any_of[0], **schema}, defs)
else:
Expand Down
3 changes: 2 additions & 1 deletion acacore/database/files_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ def __init__(
"_IdentificationWarnings",
self.files,
self.files.model,
f'"{self.files.name}".warning is not null or "{self.files.name}".puid is NULL',
f'("{self.files.name}".warning is not null or "{self.files.name}".puid is null)'
f' and "{self.files.name}".size != 0',
)
self.checksum_count = self.create_view(
"_ChecksumCount",
Expand Down
14 changes: 14 additions & 0 deletions acacore/database/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def set_db_version(db: FileDB, version: Version) -> Version:
def get_upgrade_function(current_version: Version, latest_version: Version) -> Callable[[FileDB], Version]:
if current_version < Version("2.0.0"):
return upgrade_1to2
elif current_version < Version("2.0.2"):
return upgrade_2to2_0_2
elif current_version < latest_version:
return upgrade_last
else:
Expand All @@ -40,11 +42,23 @@ def upgrade_1to2(db: FileDB) -> Version:
db.execute("alter table Files add column lock boolean default false")
db.execute("update Files set lock = false where lock is null")
db.execute("update Files set action = 'template' where action = 'replace'")
db.execute("update Files set action_data = '{}' where action_data is null")

db.execute("drop view if exists _IdentificationWarnings")
db.identification_warnings.create()

for file in db.files.select():
db.files.update(file)

return set_db_version(db, Version("2.0.0"))


def upgrade_2to2_0_2(db: FileDB) -> Version:
db.execute("drop view if exists _IdentificationWarnings")
db.identification_warnings.create()
return set_db_version(db, Version("2.0.2"))


def upgrade_last(db: FileDB) -> Version:
db.init()
return set_db_version(db, Version(__version__))
Expand Down
7 changes: 7 additions & 0 deletions acacore/models/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ def get_action(

action: Action | None = reduce(lambda acc, cur: acc or actions.get(cur), identifiers, None)

if action and action.alternatives and (new_puid := action.alternatives.get(self.suffix.lower(), None)):
puid: str = self.puid
self.puid = new_puid
if new_action := self.get_action(actions, file_classes, set_match=set_match):
return new_action
self.puid = puid

if set_match:
self.action, self.action_data = (
action.action if action else None,
Expand Down
15 changes: 14 additions & 1 deletion acacore/models/reference_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pydantic import AliasChoices
from pydantic import BaseModel
from pydantic import Field
from pydantic import field_validator

from .base import NoDefaultsModel

Expand Down Expand Up @@ -208,8 +209,20 @@ class Action(ActionData):

name: str
description: str | None = None
alternatives: dict[str, str] = Field(default_factory=dict)
action: TActionType
ignore_warnings: list[str] = Field(default_factory=list, alias="ignore-warnings")
ignore_warnings: list[str] = Field(
default_factory=list,
validation_alias=AliasChoices("ignore_warnings", "ignore-warnings"),
)

# noinspection PyNestedDecorators
@field_validator("alternatives", mode="before")
@classmethod
def _validate_alternatives(cls, value: dict[str, str]) -> dict[str, str]:
if not isinstance(value, dict):
raise ValueError("Is not a dictionary.")
return {k.lower(): v for k, v in value.items()}

@property
def action_data(self) -> ActionData:
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "acacore"
version = "2.0.1"
version = "2.0.2"
description = ""
authors = ["Matteo Campinoti <[email protected]>"]
license = "GPL-3.0"
Expand Down Expand Up @@ -101,6 +101,7 @@ ignore = [
"PT012", # ptest.raises should contain a simple statement
"RET505", # unnecessary {branch} after return statement
"S101", # use of assert,
"SIM118", # Use `key in dict` instead of `key in dict.keys()`
"TRY003", # avoid using long messages outside exception class
"UP007", # not using | in type anotations
"INP001", # implicit namespace without __init__ (throws errors in tests)
Expand Down

0 comments on commit 1246ae0

Please sign in to comment.