66
77import importlib .abc
88import importlib .machinery
9+ import importlib .metadata
910import os .path
1011import pathlib
1112import sqlite3
1213import sys
1314import types
1415import typing
1516
17+ from sqliteimport .accessor import Accessor
18+
1619
1720class SqliteFinder (importlib .abc .MetaPathFinder ):
1821 def __init__ (self , database : pathlib .Path | sqlite3 .Connection ) -> None :
@@ -23,6 +26,7 @@ def __init__(self, database: pathlib.Path | sqlite3.Connection) -> None:
2326 _ , _ , path = database .execute ("PRAGMA database_list;" ).fetchone ()
2427 self .database = pathlib .Path (path )
2528 self .connection = database
29+ self .accessor = Accessor (self .connection )
2630
2731 def find_spec (
2832 self ,
@@ -44,6 +48,15 @@ def find_spec(
4448 )
4549 return spec
4650
51+ def find_distributions (
52+ self ,
53+ context : importlib .metadata .DistributionFinder .Context | None = None ,
54+ ) -> typing .Generator [SqliteDistribution ]:
55+ if context is None :
56+ context = importlib .metadata .DistributionFinder .Context ()
57+ if context .name is not None :
58+ yield SqliteDistribution (context .name , self .connection )
59+
4760
4861class SqliteLoader (importlib .abc .Loader ):
4962 def __init__ (self , source : str ) -> None :
@@ -62,3 +75,16 @@ def load(database: pathlib.Path | str | sqlite3.Connection) -> None:
6275 raise FileNotFoundError (f"{ database } must exist." )
6376 database = pathlib .Path (database )
6477 sys .meta_path .append (SqliteFinder (database ))
78+
79+
80+ class SqliteDistribution (importlib .metadata .Distribution ):
81+ def __init__ (self , name : str , connection : sqlite3 .Connection ):
82+ self .__name = name
83+ self .__connection = connection
84+ self .__accessor = Accessor (connection )
85+
86+ def locate_file (self , path : typing .Any ) -> pathlib .Path :
87+ raise NotImplementedError ()
88+
89+ def read_text (self , filename : str ) -> str :
90+ return self .__accessor .get_file (f"{ self .__name } -%/{ filename } " )
0 commit comments