Skip to content

Commit 4186bad

Browse files
committed
push preset rewrite from local
1 parent 1c4ee35 commit 4186bad

38 files changed

+567
-1192
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 39 deletions
This file was deleted.

conftest.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "rtfm",
55
"Description": "a rtfm lookup plugin",
66
"Author": "cibere",
7-
"Version": "1.2.1a",
7+
"Version": "1.3.0a",
88
"Language": "python_v2",
99
"Website": "https://github.com/cibere/Flow.Launcher.Plugin.rtfm",
1010
"IcoPath": "assets/app.png",

plugin/libraries/__init__.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,17 @@
22

33
from typing import TYPE_CHECKING
44

5-
from yarl import URL
6-
7-
from . import doctypes, presets
5+
from . import doctypes
86

97
if TYPE_CHECKING:
108
from collections.abc import Iterable
119

1210
from .library import Library, PartialLibrary
13-
from .preset import PresetLibrary
1411

1512
doc_types: Iterable[type[Library]] = list(doctypes.fetch())
16-
preset_docs: Iterable[type[PresetLibrary]] = list(presets.fetch())
1713

1814

1915
def library_from_partial(lib: PartialLibrary) -> Library:
20-
if lib.type == "Preset":
21-
url = URL(lib.loc.rstrip("/"))
22-
for preset in preset_docs:
23-
if preset.validate_url(url):
24-
return preset.from_partial(lib)
25-
26-
raise ValueError(f"Unknown preset for {url!r}")
27-
2816
for doctype in doc_types:
2917
if lib.type == doctype.typename:
3018
return doctype.from_partial(lib)

plugin/libraries/algolia.py

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,103 @@
1-
from __future__ import annotations
1+
# from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, ClassVar
3+
# from typing import TYPE_CHECKING, ClassVar
44

5-
import msgspec
6-
from aiohttp import ClientSession
5+
# import msgspec
6+
# from aiohttp import ClientSession
7+
# from yarl import URL
8+
# from .library import Library
79

8-
from .preset import PresetLibrary
10+
# if TYPE_CHECKING:
11+
# from aiohttp import ClientSession
912

10-
if TYPE_CHECKING:
11-
from aiohttp import ClientSession
1213

14+
# class SearchHitHierarchy(msgspec.Struct):
15+
# lvl0: str | None
16+
# lvl1: str | None
17+
# lvl2: str | None
18+
# lvl3: str | None
19+
# lvl4: str | None
20+
# lvl5: str | None
21+
# lvl6: str | None
1322

14-
class SearchHitHierarchy(msgspec.Struct):
15-
lvl0: str | None
16-
lvl1: str | None
17-
lvl2: str | None
18-
lvl3: str | None
19-
lvl4: str | None
20-
lvl5: str | None
21-
lvl6: str | None
23+
# def to_text(self) -> str:
24+
# return " - ".join(
25+
# [
26+
# lvl
27+
# for lvl in reversed(
28+
# (
29+
# self.lvl0,
30+
# self.lvl1,
31+
# self.lvl2,
32+
# self.lvl3,
33+
# self.lvl4,
34+
# self.lvl5,
35+
# self.lvl6,
36+
# )
37+
# )
38+
# if lvl is not None
39+
# ]
40+
# )
2241

23-
def to_text(self) -> str:
24-
return " - ".join(
25-
[
26-
lvl
27-
for lvl in reversed(
28-
(
29-
self.lvl0,
30-
self.lvl1,
31-
self.lvl2,
32-
self.lvl3,
33-
self.lvl4,
34-
self.lvl5,
35-
self.lvl6,
36-
)
37-
)
38-
if lvl is not None
39-
]
40-
)
4142

43+
# class SearchHit(msgspec.Struct):
44+
# url: str
45+
# hierarchy: SearchHitHierarchy
4246

43-
class SearchHit(msgspec.Struct):
44-
url: str
45-
hierarchy: SearchHitHierarchy
4647

48+
# class SearchResult(msgspec.Struct):
49+
# hits: list[SearchHit]
4750

48-
class SearchResult(msgspec.Struct):
49-
hits: list[SearchHit]
5051

52+
# class SearchResponse(msgspec.Struct):
53+
# results: list[SearchResult]
5154

52-
class SearchResponse(msgspec.Struct):
53-
results: list[SearchResult]
5455

56+
# response_decoder = msgspec.json.Decoder(type=SearchResponse)
57+
# Jsonable = dict[str, "Jsonable"] | list["Jsonable"] | int | str
5558

56-
response_decoder = msgspec.json.Decoder(type=SearchResponse)
57-
Jsonable = dict[str, "Jsonable"] | list["Jsonable"] | int | str
5859

60+
# class AlgoliaConfig(msgspec.Struct):
61+
# url: str
62+
# index_name: str
63+
# kwargs: dict[str, Jsonable] = {}
5964

60-
class AlgoliaConfig(msgspec.Struct):
61-
url: str
62-
index_name: str
63-
kwargs: dict[str, Jsonable] = {}
6465

66+
# class AlgoliaBase(Library):
67+
# is_api: ClassVar[bool] = True
68+
# algolia_config: ClassVar[AlgoliaConfig]
6569

66-
class AlgoliaBase(PresetLibrary):
67-
is_preset: ClassVar[bool] = True
68-
is_api: ClassVar[bool] = True
69-
algolia_config: ClassVar[AlgoliaConfig]
70+
# def __init__(self, name: str, *, use_cache: bool) -> None:
71+
# super().__init__(name, URL(self.base_url), use_cache=False)
7072

71-
def __init__(self, name: str, *, use_cache: bool) -> None:
72-
super().__init__(name, use_cache=False)
73+
# def __init_subclass__(
74+
# cls,
75+
# config: AlgoliaConfig,
76+
# base_url: str | None = None,
77+
# favicon_url: str | None = None,
78+
# ) -> None:
79+
# cls.algolia_config = config
80+
# return super().__init_subclass__(base_url, favicon_url)
7381

74-
def __init_subclass__(
75-
cls,
76-
config: AlgoliaConfig,
77-
base_url: str | None = None,
78-
favicon_url: str | None = None,
79-
) -> None:
80-
cls.algolia_config = config
81-
return super().__init_subclass__(base_url, favicon_url)
82+
# async def make_request(self, session: ClientSession, query: str) -> None:
83+
# payload = {
84+
# "requests": [
85+
# {
86+
# "query": query,
87+
# "indexName": self.algolia_config.index_name,
88+
# }
89+
# | self.algolia_config.kwargs
90+
# ]
91+
# }
92+
# async with session.post(self.algolia_config.url, json=payload) as res:
93+
# raw = await res.content.read()
8294

83-
async def make_request(self, session: ClientSession, query: str) -> None:
84-
payload = {
85-
"requests": [
86-
{
87-
"query": query,
88-
"indexName": self.algolia_config.index_name,
89-
}
90-
| self.algolia_config.kwargs
91-
]
92-
}
93-
async with session.post(self.algolia_config.url, json=payload) as res:
94-
raw = await res.content.read()
95+
# resp = response_decoder.decode(raw)
9596

96-
resp = response_decoder.decode(raw)
97+
# cache = {}
9798

98-
cache = {}
99+
# for result in resp.results:
100+
# for hit in result.hits:
101+
# cache[hit.hierarchy.to_text()] = hit.url
99102

100-
for result in resp.results:
101-
for hit in result.hits:
102-
cache[hit.hierarchy.to_text()] = hit.url
103-
104-
self.cache = cache
103+
# self.cache = cache
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Callable
4+
5+
if TYPE_CHECKING:
6+
from collections.abc import Awaitable
7+
8+
from aiohttp import ClientSession
9+
10+
from ....entry import Entry
11+
from ..structs import ApiIndex
12+
13+
from .algolia import Algolia
14+
from .github import Github
15+
from .mathworks import Mathworks
16+
17+
api_handlers: dict[
18+
str, Callable[[str, ApiIndex, ClientSession], Awaitable[dict[str, str | Entry]]]
19+
] = {"Algolia": Algolia, "Github": Github, "Mathworks": Mathworks}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Any
4+
5+
from msgspec import Struct, json
6+
7+
if TYPE_CHECKING:
8+
from collections.abc import Generator
9+
10+
from aiohttp import ClientSession
11+
12+
from ....entry import Entry
13+
from ..structs import ApiIndex
14+
15+
16+
class SearchHitHierarchy(Struct):
17+
lvl0: str | None
18+
lvl1: str | None
19+
lvl2: str | None
20+
lvl3: str | None
21+
lvl4: str | None
22+
lvl5: str | None
23+
lvl6: str | None
24+
25+
def to_text(self) -> str:
26+
return " - ".join(
27+
[
28+
lvl
29+
for lvl in reversed(
30+
(
31+
self.lvl0,
32+
self.lvl1,
33+
self.lvl2,
34+
self.lvl3,
35+
self.lvl4,
36+
self.lvl5,
37+
self.lvl6,
38+
)
39+
)
40+
if lvl is not None
41+
]
42+
)
43+
44+
45+
class SearchHit(Struct):
46+
url: str
47+
hierarchy: SearchHitHierarchy
48+
49+
50+
class SearchResult(Struct):
51+
hits: list[SearchHit]
52+
53+
54+
class SearchResponse(Struct):
55+
results: list[SearchResult]
56+
57+
58+
response_decoder = json.Decoder(type=SearchResponse)
59+
60+
61+
class Algolia:
62+
def __init__(self, query: str, info: ApiIndex, session: ClientSession) -> None:
63+
self.session = session
64+
self.info = info
65+
self.query = query
66+
67+
def __await__(self) -> Generator[Any, Any, dict[str, str | Entry]]:
68+
return self.__call__().__await__()
69+
70+
async def __call__(self) -> dict[str, str | Entry]:
71+
payload = {
72+
"requests": [
73+
{
74+
"query": self.query,
75+
}
76+
| self.info.options
77+
]
78+
}
79+
async with self.session.post(
80+
self.info.url, json=payload, headers=self.info.headers
81+
) as res:
82+
raw = await res.content.read()
83+
84+
resp = response_decoder.decode(raw)
85+
86+
cache = {}
87+
88+
for result in resp.results:
89+
for hit in result.hits:
90+
cache[hit.hierarchy.to_text()] = hit.url
91+
92+
return cache

0 commit comments

Comments
 (0)