mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-06-28 06:41:43 +00:00
frozen dataclasses
This commit is contained in:
@ -149,7 +149,7 @@ class Users(Handler):
|
||||
Returns:
|
||||
User: built user descriptor
|
||||
"""
|
||||
user = User(args.username, args.password, args.role)
|
||||
if user.password is None:
|
||||
user.password = getpass.getpass()
|
||||
return user
|
||||
password = args.password
|
||||
if password is None:
|
||||
password = getpass.getpass()
|
||||
return User(username=args.username, password=password, access=args.role)
|
||||
|
@ -83,7 +83,7 @@ class Migrations(LazyLogging):
|
||||
module = import_module(f"{__name__}.{module_name}")
|
||||
steps: List[str] = getattr(module, "steps", [])
|
||||
self.logger.debug("found migration %s at index %s with steps count %s", module_name, index, len(steps))
|
||||
migrations.append(Migration(index, module_name, steps))
|
||||
migrations.append(Migration(index=index, name=module_name, steps=steps))
|
||||
|
||||
return migrations
|
||||
|
||||
@ -97,7 +97,7 @@ class Migrations(LazyLogging):
|
||||
migrations = self.migrations()
|
||||
current_version = self.user_version()
|
||||
expected_version = len(migrations)
|
||||
result = MigrationResult(current_version, expected_version)
|
||||
result = MigrationResult(old_version=current_version, new_version=expected_version)
|
||||
|
||||
if not result.is_outdated:
|
||||
self.logger.info("no migrations required")
|
||||
|
@ -58,7 +58,7 @@ class AuthOperations(Operations):
|
||||
|
||||
def run(connection: Connection) -> List[User]:
|
||||
return [
|
||||
User(cursor["username"], cursor["password"], UserAccess(cursor["access"]))
|
||||
User(username=cursor["username"], password=cursor["password"], access=UserAccess(cursor["access"]))
|
||||
for cursor in connection.execute(
|
||||
"""
|
||||
select * from users
|
||||
|
@ -154,7 +154,11 @@ class PackageOperations(Operations):
|
||||
Dict[str, Package]: map of the package base to its descriptor (without packages themselves)
|
||||
"""
|
||||
return {
|
||||
row["package_base"]: Package(row["package_base"], row["version"], RemoteSource.from_json(row), {})
|
||||
row["package_base"]: Package(
|
||||
base=row["package_base"],
|
||||
version=row["version"],
|
||||
remote=RemoteSource.from_json(row),
|
||||
packages={})
|
||||
for row in connection.execute("""select * from package_bases""")
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,7 @@ class Client:
|
||||
Returns:
|
||||
InternalStatus: current internal (web) service status
|
||||
"""
|
||||
return InternalStatus(BuildStatus())
|
||||
return InternalStatus(status=BuildStatus())
|
||||
|
||||
def remove(self, base: str) -> None:
|
||||
"""
|
||||
|
@ -189,7 +189,7 @@ class WebClient(Client, LazyLogging):
|
||||
self.logger.exception("could not get web service status: %s", exception_response_text(e))
|
||||
except Exception:
|
||||
self.logger.exception("could not get web service status")
|
||||
return InternalStatus(BuildStatus())
|
||||
return InternalStatus(status=BuildStatus())
|
||||
|
||||
def remove(self, base: str) -> None:
|
||||
"""
|
||||
|
@ -29,7 +29,7 @@ from typing import Any, Callable, Dict, List, Optional, Type
|
||||
from ahriman.core.util import filter_json, full_version
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class AURPackage:
|
||||
"""
|
||||
AUR package descriptor
|
||||
|
@ -47,7 +47,7 @@ class BuildStatusEnum(str, Enum):
|
||||
Success = "success"
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True)
|
||||
class BuildStatus:
|
||||
"""
|
||||
build status holder
|
||||
@ -64,7 +64,7 @@ class BuildStatus:
|
||||
"""
|
||||
convert status to enum type
|
||||
"""
|
||||
self.status = BuildStatusEnum(self.status)
|
||||
object.__setattr__(self, "status", BuildStatusEnum(self.status))
|
||||
|
||||
@classmethod
|
||||
def from_json(cls: Type[BuildStatus], dump: Dict[str, Any]) -> BuildStatus:
|
||||
|
@ -27,7 +27,7 @@ from ahriman.models.build_status import BuildStatus
|
||||
from ahriman.models.package import Package
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class Counters:
|
||||
"""
|
||||
package counters
|
||||
|
@ -26,7 +26,7 @@ from ahriman.models.build_status import BuildStatus
|
||||
from ahriman.models.counters import Counters
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class InternalStatus:
|
||||
"""
|
||||
internal server status
|
||||
|
@ -21,7 +21,7 @@ from dataclasses import dataclass
|
||||
from typing import List
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class Migration:
|
||||
"""
|
||||
migration implementation
|
||||
|
@ -22,7 +22,7 @@ from dataclasses import dataclass
|
||||
from ahriman.core.exceptions import MigrationError
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class MigrationResult:
|
||||
"""
|
||||
migration result implementation model
|
||||
|
@ -38,7 +38,7 @@ from ahriman.models.remote_source import RemoteSource
|
||||
from ahriman.models.repository_paths import RepositoryPaths
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(kw_only=True)
|
||||
class Package(LazyLogging):
|
||||
"""
|
||||
package properties representation
|
||||
@ -147,7 +147,7 @@ class Package(LazyLogging):
|
||||
"""
|
||||
package = pacman.handle.load_pkg(str(path))
|
||||
description = PackageDescription.from_package(package, path)
|
||||
return cls(package.base, package.version, remote, {package.name: description})
|
||||
return cls(base=package.base, version=package.version, remote=remote, packages={package.name: description})
|
||||
|
||||
@classmethod
|
||||
def from_aur(cls: Type[Package], name: str, pacman: Pacman) -> Package:
|
||||
@ -163,7 +163,11 @@ class Package(LazyLogging):
|
||||
"""
|
||||
package = AUR.info(name, pacman=pacman)
|
||||
remote = RemoteSource.from_source(PackageSource.AUR, package.package_base, package.repository)
|
||||
return cls(package.package_base, package.version, remote, {package.name: PackageDescription()})
|
||||
return cls(
|
||||
base=package.package_base,
|
||||
version=package.version,
|
||||
remote=remote,
|
||||
packages={package.name: PackageDescription()})
|
||||
|
||||
@classmethod
|
||||
def from_build(cls: Type[Package], path: Path) -> Package:
|
||||
@ -186,7 +190,7 @@ class Package(LazyLogging):
|
||||
packages = {key: PackageDescription() for key in srcinfo["packages"]}
|
||||
version = full_version(srcinfo.get("epoch"), srcinfo["pkgver"], srcinfo["pkgrel"])
|
||||
|
||||
return cls(srcinfo["pkgbase"], version, None, packages)
|
||||
return cls(base=srcinfo["pkgbase"], version=version, remote=None, packages=packages)
|
||||
|
||||
@classmethod
|
||||
def from_json(cls: Type[Package], dump: Dict[str, Any]) -> Package:
|
||||
@ -204,11 +208,7 @@ class Package(LazyLogging):
|
||||
for key, value in dump.get("packages", {}).items()
|
||||
}
|
||||
remote = dump.get("remote", {})
|
||||
return cls(
|
||||
base=dump["base"],
|
||||
version=dump["version"],
|
||||
remote=RemoteSource.from_json(remote),
|
||||
packages=packages)
|
||||
return cls(base=dump["base"], version=dump["version"], remote=RemoteSource.from_json(remote), packages=packages)
|
||||
|
||||
@classmethod
|
||||
def from_official(cls: Type[Package], name: str, pacman: Pacman, use_syncdb: bool = True) -> Package:
|
||||
@ -225,7 +225,11 @@ class Package(LazyLogging):
|
||||
"""
|
||||
package = OfficialSyncdb.info(name, pacman=pacman) if use_syncdb else Official.info(name, pacman=pacman)
|
||||
remote = RemoteSource.from_source(PackageSource.Repository, package.package_base, package.repository)
|
||||
return cls(package.package_base, package.version, remote, {package.name: PackageDescription()})
|
||||
return cls(
|
||||
base=package.package_base,
|
||||
version=package.version,
|
||||
remote=remote,
|
||||
packages={package.name: PackageDescription()})
|
||||
|
||||
@staticmethod
|
||||
def dependencies(path: Path) -> Set[str]:
|
||||
|
@ -27,7 +27,7 @@ from typing import Any, Dict, List, Optional, Type
|
||||
from ahriman.core.util import filter_json
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(kw_only=True)
|
||||
class PackageDescription:
|
||||
"""
|
||||
package specific properties
|
||||
|
@ -17,11 +17,11 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True)
|
||||
class Property:
|
||||
"""
|
||||
holder of object properties descriptor
|
||||
@ -34,4 +34,4 @@ class Property:
|
||||
|
||||
name: str
|
||||
value: Any
|
||||
is_required: bool = False
|
||||
is_required: bool = field(default=False, kw_only=True)
|
||||
|
@ -27,7 +27,7 @@ from ahriman.core.util import filter_json
|
||||
from ahriman.models.package_source import PackageSource
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class RemoteSource:
|
||||
"""
|
||||
remote package source properties
|
||||
@ -50,7 +50,7 @@ class RemoteSource:
|
||||
"""
|
||||
convert source to enum type
|
||||
"""
|
||||
self.source = PackageSource(self.source)
|
||||
object.__setattr__(self, "source", PackageSource(self.source))
|
||||
|
||||
@property
|
||||
def pkgbuild_dir(self) -> Path:
|
||||
|
@ -29,7 +29,7 @@ from typing import Set, Tuple, Type
|
||||
from ahriman.core.exceptions import InvalidPath
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True)
|
||||
class RepositoryPaths:
|
||||
"""
|
||||
repository paths holder. For the most operations with paths you want to use this object
|
||||
|
@ -19,7 +19,7 @@
|
||||
#
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, replace
|
||||
from typing import Optional, Type
|
||||
from passlib.pwd import genword as generate_password # type: ignore
|
||||
from passlib.handlers.sha2_crypt import sha512_crypt # type: ignore
|
||||
@ -27,7 +27,7 @@ from passlib.handlers.sha2_crypt import sha512_crypt # type: ignore
|
||||
from ahriman.models.user_access import UserAccess
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class User:
|
||||
"""
|
||||
authorized web user model
|
||||
@ -82,7 +82,7 @@ class User:
|
||||
"""
|
||||
if username is None or password is None:
|
||||
return None
|
||||
return cls(username, password, access)
|
||||
return cls(username=username, password=password, access=access)
|
||||
|
||||
@staticmethod
|
||||
def generate_password(length: int) -> str:
|
||||
@ -130,7 +130,7 @@ class User:
|
||||
# when we do not store any password here
|
||||
return self
|
||||
password_hash: str = self._HASHER.hash(self.password + salt)
|
||||
return User(self.username, password_hash, self.access)
|
||||
return replace(self, password=password_hash)
|
||||
|
||||
def verify_access(self, required: UserAccess) -> bool:
|
||||
"""
|
||||
|
@ -25,7 +25,7 @@ from dataclasses import dataclass
|
||||
from typing import Optional, Type
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True)
|
||||
class UserIdentity:
|
||||
"""
|
||||
user identity used inside web service
|
||||
|
Reference in New Issue
Block a user