aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/store.py
blob: 4978b659af88f6ae1d1fd3a7b0df3af3c2be5c44 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# SPDX-License-Identifier: GPL-3.0-or-later

# Haketilo proxy on-disk data storage.
#
# This file is part of Hydrilla&Haketilo.
#
# Copyright (C) 2022 Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use this code
# in a proprietary program, I am not going to enforce this in court.

"""This module facilitates storing and modifying Haketilo proxy data on-disk."""

# Enable using with Python 3.7.
from __future__ import annotations

import threading
import dataclasses as dc
import typing as t

from pathlib import Path
from enum import Enum

from immutables import Map

from .. url_patterns import parse_pattern
from .. import versions
from . import state


@dc.dataclass(frozen=True, eq=False)
class StoredItemRef(state.ItemRef):
    item_id: int

    def __eq__(self, other: object) -> bool:
        return isinstance(other, StoredItemRef) and \
            self.item_id == other.item_id

    def __hash__(self) -> int:
        return hash(self.item_id)

    def _id(self) -> str:
        return str(self.item_id)


@dc.dataclass(frozen=True, eq=False)
class StoredPayloadRef(state.PayloadRef):
    payload_id: int

    def __eq__(self, other: object) -> bool:
        return isinstance(other, StoredPayloadRef) and \
            self.payload_id == other.payload_id

    def __hash__(self) -> int:
        return hash(self.payload_id)

    def _id(self) -> str:
        return str(self.payload_id)


# class ItemStoredData:
#     """...."""
#     def __init__(
#             self,
#             item_id:       int
#             ty#pe:          ItemType
#             repository_id: int
#             version:       str
#             identifier:    str
#             orphan:        bool
#             installed:     bool
#             enabled:       EnabledStatus
#     ) -> None:
#         """...."""
#         self.item_id       = item_id
#         self.type          = ItemType(type)
#         self.repository_id = repository_id
#         self.version       = parse
#             identifier:    str
#             orphan:        bool
#             installed:     bool
#             enabled:       EnabledStatus


@dc.dataclass
class HaketiloStore:
    """...."""
    store_dir: Path

    lock: threading.RLock = dc.field(default_factory=threading.RLock)

    # def load_all_resources(self) -> t.Sequence[item_infos.ResourceInfo]:
    #     """...."""
    #     # TODO: implement
    #     with self.lock:
    #         return []

    def load_installed_mappings_data(self) \
        -> t.Mapping[state.MappingRef, state.EnabledStatus]:
        """...."""
        # TODO: implement
        with self.lock:
            dummy_item_ref = StoredItemRef(
                item_id    = 47,
                identifier = 'somemapping',
                version    = versions.parse_normalize_version('1.2.3'),
                repository = 'somerepo',
                orphan     = False
            )

            return Map({
                state.MappingRef(dummy_item_ref): state.EnabledStatus.ENABLED
            })

    def load_payloads_data(self) \
        -> t.Mapping[state.MappingRef, t.Iterable[state.PayloadRef]]:
        """...."""
        # TODO: implement
        with self.lock:
            dummy_item_ref = StoredItemRef(
                item_id    = 47,
                identifier = 'somemapping',
                version    = versions.parse_normalize_version('1.2.3'),
                repository = 'somerepo',
                orphan     = False
            )

            dummy_mapping_ref = state.MappingRef(dummy_item_ref)

            payload_refs = []
            for parsed_pattern in parse_pattern('http*://example.com/a/***'):
                dummy_payload_ref = StoredPayloadRef(
                    payload_id  = 22,
                    mapping_ref = dummy_mapping_ref,
                    pattern     = parsed_pattern
                )

                payload_refs.append(dummy_payload_ref)

            return Map({dummy_mapping_ref: payload_refs})

    def load_file_data(
            self,
            payload_ref:         state.PayloadRef,
            resource_identifier: str,
            file_path:           t.Sequence[str]
    ) -> t.Optional[state.FileData]:
        # TODO: implement
        with self.lock:
            return None

    def load_global_settings(self) -> state.HaketiloGlobalSettings:
        """...."""
        # TODO: implement
        with self.lock:
            return state.HaketiloGlobalSettings(
                state.MappingApplicationMode.WHEN_ENABLED,
                False
            )

    def write_global_settings(self, settings: state.HaketiloGlobalSettings) \
        -> None:
        """...."""
        # TODO: implement
        with self.lock:
            pass