koi-net 1.0.0b11__py3-none-any.whl → 1.0.0b12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of koi-net might be problematic. Click here for more details.

koi_net/core.py CHANGED
@@ -1,126 +1,126 @@
1
- import logging
2
- import httpx
3
- from rid_lib.ext import Cache, Bundle
4
- from .network import NetworkInterface
5
- from .processor import ProcessorInterface
6
- from .processor import default_handlers
7
- from .processor.handler import KnowledgeHandler
8
- from .identity import NodeIdentity
9
- from .protocol.node import NodeProfile
10
- from .protocol.event import Event, EventType
11
-
12
- logger = logging.getLogger(__name__)
13
-
14
-
15
- class NodeInterface:
16
- cache: Cache
17
- identity: NodeIdentity
18
- network: NetworkInterface
19
- processor: ProcessorInterface
20
- first_contact: str
21
- use_kobj_processor_thread: bool
22
-
23
- def __init__(
24
- self,
25
- name: str,
26
- profile: NodeProfile,
27
- identity_file_path: str = "identity.json",
28
- event_queues_file_path: str = "event_queues.json",
29
- cache_directory_path: str = "rid_cache",
30
- use_kobj_processor_thread: bool = False,
31
- first_contact: str | None = None,
32
- handlers: list[KnowledgeHandler] | None = None,
33
- cache: Cache | None = None,
34
- network: NetworkInterface | None = None,
35
- processor: ProcessorInterface | None = None
36
- ):
37
- self.cache = cache or Cache(cache_directory_path)
38
- self.identity = NodeIdentity(
39
- name=name,
40
- profile=profile,
41
- cache=self.cache,
42
- file_path=identity_file_path
43
- )
44
- self.first_contact = first_contact
45
- self.network = network or NetworkInterface(
46
- file_path=event_queues_file_path,
47
- first_contact=self.first_contact,
48
- cache=self.cache,
49
- identity=self.identity
50
- )
51
-
52
- # pull all handlers defined in default_handlers module
53
- if handlers is None:
54
- handlers = [
55
- obj for obj in vars(default_handlers).values()
56
- if isinstance(obj, KnowledgeHandler)
57
- ]
58
-
59
- self.use_kobj_processor_thread = use_kobj_processor_thread
60
- self.processor = processor or ProcessorInterface(
61
- cache=self.cache,
62
- network=self.network,
63
- identity=self.identity,
64
- use_kobj_processor_thread=self.use_kobj_processor_thread,
65
- default_handlers=handlers
66
- )
67
-
68
- def start(self) -> None:
69
- """Starts a node, call this method first.
70
-
71
- Starts the processor thread (if enabled). Loads event queues into memory. Generates network graph from nodes and edges in cache. Processes any state changes of node bundle. Initiates handshake with first contact (if provided) if node doesn't have any neighbors.
72
- """
73
- if self.use_kobj_processor_thread:
74
- logger.info("Starting processor worker thread")
75
- self.processor.worker_thread.start()
76
-
77
- self.network._load_event_queues()
78
- self.network.graph.generate()
79
-
80
- self.processor.handle(
81
- bundle=Bundle.generate(
82
- rid=self.identity.rid,
83
- contents=self.identity.profile.model_dump()
84
- )
85
- )
86
-
87
- logger.debug("Waiting for kobj queue to empty")
88
- if self.use_kobj_processor_thread:
89
- self.processor.kobj_queue.join()
90
- else:
91
- self.processor.flush_kobj_queue()
92
- logger.debug("Done")
93
-
94
- if not self.network.graph.get_neighbors() and self.first_contact:
95
- logger.debug(f"I don't have any neighbors, reaching out to first contact {self.first_contact}")
96
-
97
- events = [
98
- Event.from_rid(EventType.FORGET, self.identity.rid),
99
- Event.from_bundle(EventType.NEW, self.identity.bundle)
100
- ]
101
-
102
- try:
103
- self.network.request_handler.broadcast_events(
104
- url=self.first_contact,
105
- events=events
106
- )
107
-
108
- except httpx.ConnectError:
109
- logger.warning("Failed to reach first contact")
110
- return
111
-
112
-
113
- def stop(self):
114
- """Stops a node, call this method last.
115
-
116
- Finishes processing knowledge object queue. Saves event queues to storage.
117
- """
118
- logger.info("Stopping node...")
119
-
120
- if self.use_kobj_processor_thread:
121
- logger.info(f"Waiting for kobj queue to empty ({self.processor.kobj_queue.unfinished_tasks} tasks remaining)")
122
- self.processor.kobj_queue.join()
123
- else:
124
- self.processor.flush_kobj_queue()
125
-
1
+ import logging
2
+ import httpx
3
+ from rid_lib.ext import Cache, Bundle
4
+ from .network import NetworkInterface
5
+ from .processor import ProcessorInterface
6
+ from .processor import default_handlers
7
+ from .processor.handler import KnowledgeHandler
8
+ from .identity import NodeIdentity
9
+ from .protocol.node import NodeProfile
10
+ from .protocol.event import Event, EventType
11
+
12
+ logger = logging.getLogger(__name__)
13
+
14
+
15
+ class NodeInterface:
16
+ cache: Cache
17
+ identity: NodeIdentity
18
+ network: NetworkInterface
19
+ processor: ProcessorInterface
20
+ first_contact: str
21
+ use_kobj_processor_thread: bool
22
+
23
+ def __init__(
24
+ self,
25
+ name: str,
26
+ profile: NodeProfile,
27
+ identity_file_path: str = "identity.json",
28
+ event_queues_file_path: str = "event_queues.json",
29
+ cache_directory_path: str = "rid_cache",
30
+ use_kobj_processor_thread: bool = False,
31
+ first_contact: str | None = None,
32
+ handlers: list[KnowledgeHandler] | None = None,
33
+ cache: Cache | None = None,
34
+ network: NetworkInterface | None = None,
35
+ processor: ProcessorInterface | None = None
36
+ ):
37
+ self.cache = cache or Cache(cache_directory_path)
38
+ self.identity = NodeIdentity(
39
+ name=name,
40
+ profile=profile,
41
+ cache=self.cache,
42
+ file_path=identity_file_path
43
+ )
44
+ self.first_contact = first_contact
45
+ self.network = network or NetworkInterface(
46
+ file_path=event_queues_file_path,
47
+ first_contact=self.first_contact,
48
+ cache=self.cache,
49
+ identity=self.identity
50
+ )
51
+
52
+ # pull all handlers defined in default_handlers module
53
+ if handlers is None:
54
+ handlers = [
55
+ obj for obj in vars(default_handlers).values()
56
+ if isinstance(obj, KnowledgeHandler)
57
+ ]
58
+
59
+ self.use_kobj_processor_thread = use_kobj_processor_thread
60
+ self.processor = processor or ProcessorInterface(
61
+ cache=self.cache,
62
+ network=self.network,
63
+ identity=self.identity,
64
+ use_kobj_processor_thread=self.use_kobj_processor_thread,
65
+ default_handlers=handlers
66
+ )
67
+
68
+ def start(self) -> None:
69
+ """Starts a node, call this method first.
70
+
71
+ Starts the processor thread (if enabled). Loads event queues into memory. Generates network graph from nodes and edges in cache. Processes any state changes of node bundle. Initiates handshake with first contact (if provided) if node doesn't have any neighbors.
72
+ """
73
+ if self.use_kobj_processor_thread:
74
+ logger.info("Starting processor worker thread")
75
+ self.processor.worker_thread.start()
76
+
77
+ self.network._load_event_queues()
78
+ self.network.graph.generate()
79
+
80
+ self.processor.handle(
81
+ bundle=Bundle.generate(
82
+ rid=self.identity.rid,
83
+ contents=self.identity.profile.model_dump()
84
+ )
85
+ )
86
+
87
+ logger.debug("Waiting for kobj queue to empty")
88
+ if self.use_kobj_processor_thread:
89
+ self.processor.kobj_queue.join()
90
+ else:
91
+ self.processor.flush_kobj_queue()
92
+ logger.debug("Done")
93
+
94
+ if not self.network.graph.get_neighbors() and self.first_contact:
95
+ logger.debug(f"I don't have any neighbors, reaching out to first contact {self.first_contact}")
96
+
97
+ events = [
98
+ Event.from_rid(EventType.FORGET, self.identity.rid),
99
+ Event.from_bundle(EventType.NEW, self.identity.bundle)
100
+ ]
101
+
102
+ try:
103
+ self.network.request_handler.broadcast_events(
104
+ url=self.first_contact,
105
+ events=events
106
+ )
107
+
108
+ except httpx.ConnectError:
109
+ logger.warning("Failed to reach first contact")
110
+ return
111
+
112
+
113
+ def stop(self):
114
+ """Stops a node, call this method last.
115
+
116
+ Finishes processing knowledge object queue. Saves event queues to storage.
117
+ """
118
+ logger.info("Stopping node...")
119
+
120
+ if self.use_kobj_processor_thread:
121
+ logger.info(f"Waiting for kobj queue to empty ({self.processor.kobj_queue.unfinished_tasks} tasks remaining)")
122
+ self.processor.kobj_queue.join()
123
+ else:
124
+ self.processor.flush_kobj_queue()
125
+
126
126
  self.network._save_event_queues()
koi_net/identity.py CHANGED
@@ -1,70 +1,70 @@
1
- import logging
2
- from pydantic import BaseModel
3
- from rid_lib.ext.bundle import Bundle
4
- from rid_lib.ext.cache import Cache
5
- from rid_lib.types.koi_net_node import KoiNetNode
6
- from .protocol.node import NodeProfile
7
-
8
- logger = logging.getLogger(__name__)
9
-
10
-
11
- class NodeIdentityModel(BaseModel):
12
- rid: KoiNetNode
13
- profile: NodeProfile
14
-
15
- class NodeIdentity:
16
- """Represents a node's identity (RID, profile, bundle)."""
17
-
18
- _identity: NodeIdentityModel
19
- file_path: str
20
- cache: Cache
21
-
22
- def __init__(
23
- self,
24
- name: str,
25
- profile: NodeProfile,
26
- cache: Cache,
27
- file_path: str = "identity.json"
28
- ):
29
- """Initializes node identity from a name and profile.
30
-
31
- Attempts to read identity from storage. If it doesn't already exist, a new RID is generated from the provided name, and that RID and profile are written to storage. Changes to the name or profile will update the stored identity.
32
-
33
- WARNING: If the name is changed, the RID will be overwritten which will have consequences for the rest of the network.
34
- """
35
- self.cache = cache
36
- self.file_path = file_path
37
-
38
- self._identity = None
39
- try:
40
- with open(file_path, "r") as f:
41
- self._identity = NodeIdentityModel.model_validate_json(f.read())
42
-
43
- except FileNotFoundError:
44
- pass
45
-
46
- if self._identity:
47
- if self._identity.rid.name != name:
48
- logger.warning("Node name changed which will change this node's RID, if you really want to do this manually delete the identity JSON file")
49
- if self._identity.profile != profile:
50
- self._identity.profile = profile
51
- else:
52
- self._identity = NodeIdentityModel(
53
- rid=KoiNetNode.generate(name),
54
- profile=profile,
55
- )
56
-
57
- with open(file_path, "w") as f:
58
- f.write(self._identity.model_dump_json(indent=2))
59
-
60
- @property
61
- def rid(self) -> KoiNetNode:
62
- return self._identity.rid
63
-
64
- @property
65
- def profile(self) -> NodeProfile:
66
- return self._identity.profile
67
-
68
- @property
69
- def bundle(self) -> Bundle:
1
+ import logging
2
+ from pydantic import BaseModel
3
+ from rid_lib.ext.bundle import Bundle
4
+ from rid_lib.ext.cache import Cache
5
+ from rid_lib.types.koi_net_node import KoiNetNode
6
+ from .protocol.node import NodeProfile
7
+
8
+ logger = logging.getLogger(__name__)
9
+
10
+
11
+ class NodeIdentityModel(BaseModel):
12
+ rid: KoiNetNode
13
+ profile: NodeProfile
14
+
15
+ class NodeIdentity:
16
+ """Represents a node's identity (RID, profile, bundle)."""
17
+
18
+ _identity: NodeIdentityModel
19
+ file_path: str
20
+ cache: Cache
21
+
22
+ def __init__(
23
+ self,
24
+ name: str,
25
+ profile: NodeProfile,
26
+ cache: Cache,
27
+ file_path: str = "identity.json"
28
+ ):
29
+ """Initializes node identity from a name and profile.
30
+
31
+ Attempts to read identity from storage. If it doesn't already exist, a new RID is generated from the provided name, and that RID and profile are written to storage. Changes to the name or profile will update the stored identity.
32
+
33
+ WARNING: If the name is changed, the RID will be overwritten which will have consequences for the rest of the network.
34
+ """
35
+ self.cache = cache
36
+ self.file_path = file_path
37
+
38
+ self._identity = None
39
+ try:
40
+ with open(file_path, "r") as f:
41
+ self._identity = NodeIdentityModel.model_validate_json(f.read())
42
+
43
+ except FileNotFoundError:
44
+ pass
45
+
46
+ if self._identity:
47
+ if self._identity.rid.name != name:
48
+ logger.warning("Node name changed which will change this node's RID, if you really want to do this manually delete the identity JSON file")
49
+ if self._identity.profile != profile:
50
+ self._identity.profile = profile
51
+ else:
52
+ self._identity = NodeIdentityModel(
53
+ rid=KoiNetNode.generate(name),
54
+ profile=profile,
55
+ )
56
+
57
+ with open(file_path, "w") as f:
58
+ f.write(self._identity.model_dump_json(indent=2))
59
+
60
+ @property
61
+ def rid(self) -> KoiNetNode:
62
+ return self._identity.rid
63
+
64
+ @property
65
+ def profile(self) -> NodeProfile:
66
+ return self._identity.profile
67
+
68
+ @property
69
+ def bundle(self) -> Bundle:
70
70
  return self.cache.read(self.rid)
koi_net/network/graph.py CHANGED
@@ -1,127 +1,127 @@
1
- import logging
2
- from typing import Literal
3
- import networkx as nx
4
- from rid_lib import RIDType
5
- from rid_lib.ext import Cache
6
- from rid_lib.types import KoiNetEdge, KoiNetNode
7
- from ..identity import NodeIdentity
8
- from ..protocol.edge import EdgeProfile, EdgeStatus
9
- from ..protocol.node import NodeProfile
10
-
11
- logger = logging.getLogger(__name__)
12
-
13
-
14
- class NetworkGraph:
15
- """Graph functions for this node's view of its network."""
16
-
17
- cache: Cache
18
- identity: NodeIdentity
19
- dg: nx.DiGraph
20
-
21
- def __init__(self, cache: Cache, identity: NodeIdentity):
22
- self.cache = cache
23
- self.dg = nx.DiGraph()
24
- self.identity = identity
25
-
26
- def generate(self):
27
- """Generates directed graph from cached KOI nodes and edges."""
28
- logger.debug("Generating network graph")
29
- self.dg.clear()
30
- for rid in self.cache.list_rids():
31
- if type(rid) == KoiNetNode:
32
- self.dg.add_node(rid)
33
- logger.debug(f"Added node {rid}")
34
-
35
- elif type(rid) == KoiNetEdge:
36
- edge_profile = self.get_edge_profile(rid)
37
- if not edge_profile:
38
- logger.warning(f"Failed to load {rid!r}")
39
- continue
40
- self.dg.add_edge(edge_profile.source, edge_profile.target, rid=rid)
41
- logger.debug(f"Added edge {rid} ({edge_profile.source} -> {edge_profile.target})")
42
- logger.debug("Done")
43
-
44
- def get_node_profile(self, rid: KoiNetNode) -> NodeProfile | None:
45
- """Returns node profile given its RID."""
46
- bundle = self.cache.read(rid)
47
- if bundle:
48
- return bundle.validate_contents(NodeProfile)
49
-
50
- def get_edge_profile(
51
- self,
52
- rid: KoiNetEdge | None = None,
53
- source: KoiNetNode | None = None,
54
- target: KoiNetNode | None = None,
55
- ) -> EdgeProfile | None:
56
- """Returns edge profile given its RID, or source and target node RIDs."""
57
- if source and target:
58
- if (source, target) not in self.dg.edges: return
59
- edge_data = self.dg.get_edge_data(source, target)
60
- if not edge_data: return
61
- rid = edge_data.get("rid")
62
- if not rid: return
63
- elif not rid:
64
- raise ValueError("Either 'rid' or 'source' and 'target' must be provided")
65
-
66
- bundle = self.cache.read(rid)
67
- if bundle:
68
- return bundle.validate_contents(EdgeProfile)
69
-
70
- def get_edges(
71
- self,
72
- direction: Literal["in", "out"] | None = None,
73
- ) -> list[KoiNetEdge]:
74
- """Returns edges this node belongs to.
75
-
76
- All edges returned by default, specify `direction` to restrict to incoming or outgoing edges only."""
77
-
78
- edges = []
79
- if direction != "in" and self.dg.out_edges:
80
- out_edges = self.dg.out_edges(self.identity.rid)
81
- edges.extend([e for e in out_edges])
82
-
83
- if direction != "out" and self.dg.in_edges:
84
- in_edges = self.dg.in_edges(self.identity.rid)
85
- edges.extend([e for e in in_edges])
86
-
87
- edge_rids = []
88
- for edge in edges:
89
- edge_data = self.dg.get_edge_data(*edge)
90
- if not edge_data: continue
91
- edge_rid = edge_data.get("rid")
92
- if not edge_rid: continue
93
- edge_rids.append(edge_rid)
94
-
95
- return edge_rids
96
-
97
- def get_neighbors(
98
- self,
99
- direction: Literal["in", "out"] | None = None,
100
- status: EdgeStatus | None = None,
101
- allowed_type: RIDType | None = None
102
- ) -> list[KoiNetNode]:
103
- """Returns neighboring nodes this node shares an edge with.
104
-
105
- All neighboring nodes returned by default, specify `direction` to restrict to neighbors connected by incoming or outgoing edges only."""
106
-
107
- neighbors = []
108
- for edge_rid in self.get_edges(direction):
109
- edge_profile = self.get_edge_profile(edge_rid)
110
-
111
- if not edge_profile:
112
- logger.warning(f"Failed to find edge {edge_rid!r} in cache")
113
- continue
114
-
115
- if status and edge_profile.status != status:
116
- continue
117
-
118
- if allowed_type and allowed_type not in edge_profile.rid_types:
119
- continue
120
-
121
- if edge_profile.target == self.identity.rid:
122
- neighbors.append(edge_profile.source)
123
- elif edge_profile.source == self.identity.rid:
124
- neighbors.append(edge_profile.target)
125
-
126
- return list(neighbors)
127
-
1
+ import logging
2
+ from typing import Literal
3
+ import networkx as nx
4
+ from rid_lib import RIDType
5
+ from rid_lib.ext import Cache
6
+ from rid_lib.types import KoiNetEdge, KoiNetNode
7
+ from ..identity import NodeIdentity
8
+ from ..protocol.edge import EdgeProfile, EdgeStatus
9
+ from ..protocol.node import NodeProfile
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+
14
+ class NetworkGraph:
15
+ """Graph functions for this node's view of its network."""
16
+
17
+ cache: Cache
18
+ identity: NodeIdentity
19
+ dg: nx.DiGraph
20
+
21
+ def __init__(self, cache: Cache, identity: NodeIdentity):
22
+ self.cache = cache
23
+ self.dg = nx.DiGraph()
24
+ self.identity = identity
25
+
26
+ def generate(self):
27
+ """Generates directed graph from cached KOI nodes and edges."""
28
+ logger.debug("Generating network graph")
29
+ self.dg.clear()
30
+ for rid in self.cache.list_rids():
31
+ if type(rid) == KoiNetNode:
32
+ self.dg.add_node(rid)
33
+ logger.debug(f"Added node {rid}")
34
+
35
+ elif type(rid) == KoiNetEdge:
36
+ edge_profile = self.get_edge_profile(rid)
37
+ if not edge_profile:
38
+ logger.warning(f"Failed to load {rid!r}")
39
+ continue
40
+ self.dg.add_edge(edge_profile.source, edge_profile.target, rid=rid)
41
+ logger.debug(f"Added edge {rid} ({edge_profile.source} -> {edge_profile.target})")
42
+ logger.debug("Done")
43
+
44
+ def get_node_profile(self, rid: KoiNetNode) -> NodeProfile | None:
45
+ """Returns node profile given its RID."""
46
+ bundle = self.cache.read(rid)
47
+ if bundle:
48
+ return bundle.validate_contents(NodeProfile)
49
+
50
+ def get_edge_profile(
51
+ self,
52
+ rid: KoiNetEdge | None = None,
53
+ source: KoiNetNode | None = None,
54
+ target: KoiNetNode | None = None,
55
+ ) -> EdgeProfile | None:
56
+ """Returns edge profile given its RID, or source and target node RIDs."""
57
+ if source and target:
58
+ if (source, target) not in self.dg.edges: return
59
+ edge_data = self.dg.get_edge_data(source, target)
60
+ if not edge_data: return
61
+ rid = edge_data.get("rid")
62
+ if not rid: return
63
+ elif not rid:
64
+ raise ValueError("Either 'rid' or 'source' and 'target' must be provided")
65
+
66
+ bundle = self.cache.read(rid)
67
+ if bundle:
68
+ return bundle.validate_contents(EdgeProfile)
69
+
70
+ def get_edges(
71
+ self,
72
+ direction: Literal["in", "out"] | None = None,
73
+ ) -> list[KoiNetEdge]:
74
+ """Returns edges this node belongs to.
75
+
76
+ All edges returned by default, specify `direction` to restrict to incoming or outgoing edges only."""
77
+
78
+ edges = []
79
+ if direction != "in" and self.dg.out_edges:
80
+ out_edges = self.dg.out_edges(self.identity.rid)
81
+ edges.extend([e for e in out_edges])
82
+
83
+ if direction != "out" and self.dg.in_edges:
84
+ in_edges = self.dg.in_edges(self.identity.rid)
85
+ edges.extend([e for e in in_edges])
86
+
87
+ edge_rids = []
88
+ for edge in edges:
89
+ edge_data = self.dg.get_edge_data(*edge)
90
+ if not edge_data: continue
91
+ edge_rid = edge_data.get("rid")
92
+ if not edge_rid: continue
93
+ edge_rids.append(edge_rid)
94
+
95
+ return edge_rids
96
+
97
+ def get_neighbors(
98
+ self,
99
+ direction: Literal["in", "out"] | None = None,
100
+ status: EdgeStatus | None = None,
101
+ allowed_type: RIDType | None = None
102
+ ) -> list[KoiNetNode]:
103
+ """Returns neighboring nodes this node shares an edge with.
104
+
105
+ All neighboring nodes returned by default, specify `direction` to restrict to neighbors connected by incoming or outgoing edges only."""
106
+
107
+ neighbors = []
108
+ for edge_rid in self.get_edges(direction):
109
+ edge_profile = self.get_edge_profile(edge_rid)
110
+
111
+ if not edge_profile:
112
+ logger.warning(f"Failed to find edge {edge_rid!r} in cache")
113
+ continue
114
+
115
+ if status and edge_profile.status != status:
116
+ continue
117
+
118
+ if allowed_type and allowed_type not in edge_profile.rid_types:
119
+ continue
120
+
121
+ if edge_profile.target == self.identity.rid:
122
+ neighbors.append(edge_profile.source)
123
+ elif edge_profile.source == self.identity.rid:
124
+ neighbors.append(edge_profile.target)
125
+
126
+ return list(neighbors)
127
+