aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sec_certs/sample/certificate.py
blob: 6fbf8af4d6871195db9b38ede487268b6bedceaa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import annotations

import copy
import logging
from abc import ABC, abstractmethod
from collections import ChainMap
from dataclasses import dataclass, field
from typing import Any, Generic, TypeVar

import sec_certs.utils.extract
from sec_certs.cert_rules import PANDAS_KEYWORDS_CATEGORIES
from sec_certs.serialization.json import ComplexSerializableType

logger = logging.getLogger(__name__)

T = TypeVar("T", bound="Certificate")
H = TypeVar("H", bound="Heuristics")
P = TypeVar("P", bound="PdfData")


@dataclass
class References(ComplexSerializableType):
    directly_referenced_by: set[str] | None = field(default=None)
    indirectly_referenced_by: set[str] | None = field(default=None)
    directly_referencing: set[str] | None = field(default=None)
    indirectly_referencing: set[str] | None = field(default=None)

    def __bool__(self):
        return any(getattr(self, x) for x in vars(self))


class Heuristics:
    pass


class PdfData:
    def get_keywords_df_data(self, var: str) -> dict[str, float]:
        data_dct = getattr(self, var)
        return dict(
            ChainMap(
                *[
                    sec_certs.utils.extract.get_sums_for_rules_subset(data_dct, cat)
                    for cat in PANDAS_KEYWORDS_CATEGORIES
                ]
            )
        )


class Certificate(Generic[T, H, P], ABC, ComplexSerializableType):
    manufacturer: str | None
    name: str | None
    pdf_data: P
    heuristics: H

    def __init__(self, *args, **kwargs):
        pass

    def __repr__(self) -> str:
        return str(self.to_dict())

    def __str__(self) -> str:
        return "Not implemented"

    @property
    @abstractmethod
    def dgst(self) -> str:
        raise NotImplementedError("Not meant to be implemented")

    @property
    @abstractmethod
    def label_studio_title(self):
        raise NotImplementedError("Not meant to be implemented")

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Certificate):
            return False
        return self.dgst == other.dgst

    def to_dict(self) -> dict[str, Any]:
        return {
            **{"dgst": self.dgst},
            **{key: val for key, val in copy.deepcopy(self.__dict__).items() if key in self.serialized_attributes},
        }

    @classmethod
    def from_dict(cls: type[T], dct: dict) -> T:
        dct.pop("dgst")
        return cls(**dct)