Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrock committed Dec 30, 2024
1 parent 4ee0f19 commit f6bdaab
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/bubble/cli/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def get_bash_prompt():

town = Site(base_url, bind, repo)
with town.install_context():
with repo.new_graph():
with repo.using_new_buffer():
town.vat.create_identity_graph()

add(
Expand Down
27 changes: 11 additions & 16 deletions src/bubble/cli/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@

import trio
import structlog
import rdflib.collection

from typer import Option
from rdflib import PROV, BNode, Literal
from rdflib import PROV, Literal

from swash.prfx import NT
from swash.util import new
from swash.util import make_list, new
from bubble.cli.app import RepoPath, app
from bubble.repo.git import Git
from bubble.repo.repo import Repository, context
from bubble.repo.repo import Repository
from bubble.stat.stat import gather_system_info


Expand Down Expand Up @@ -40,34 +39,30 @@ async def _bubble_shell(repo_path: str, base_url: str) -> None:
system_info = await gather_system_info()
user = system_info["user_info"]

with repo.new_graph():
with repo.new_agent(
with repo.using_new_buffer():
# XXX: should reuse an agent entity
with repo.using_new_agent(
NT.Account,
{
NT.owner: user.pw_gecos,
},
) as agent:
arguments = BNode()
rdflib.collection.Collection(
context.buffer.get(),
arguments,
[Literal(arg) for arg in sys.argv[1:]],
)
# Start a new shell session activity
with repo.new_activity(
with repo.using_new_activity(
NT.InteractiveShellSession,
{
PROV.wasStartedBy: new(
NT.Command,
{
NT.programPath: Literal(sys.argv[0]),
NT.arguments: arguments,
NT.arguments: make_list(
[Literal(arg) for arg in sys.argv[1:]]
),
},
)
},
) as activity:
# Create a derived graph for the shell session
with repo.new_derived_graph(
with repo.using_derived_buffer(
source_graph=repo.metadata_id
) as derived_id:
# Get the directory path for this graph
Expand Down
5 changes: 2 additions & 3 deletions src/bubble/http/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ def urlquote(id: str):

@contextmanager
def base_shell(title: str):
"""Base shell layout with status bar showing town info like public key."""
with base_html(title):
with tag("div", classes="min-h-screen flex flex-col"):
with tag.div(classes="min-h-screen flex flex-col"):
render_status_bar()
with tag("main", id="main", classes="flex-1 p-4 flex"):
with tag.main(id="main", classes="flex-1 p-4 flex"):
yield


Expand Down
34 changes: 16 additions & 18 deletions src/bubble/repo/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ async def get_file(
file_uri = URIRef(f"file://{await file_path.absolute()}")

# Record file metadata in the graph itself
with self.using_graph(identifier):
with self.using_buffer(identifier):
# Link the file distribution to the graph
add(
identifier,
Expand Down Expand Up @@ -576,7 +576,7 @@ def using_metadata(self) -> Generator[Graph, None, None]:
yield self.metadata

@contextmanager
def using_graph(
def using_buffer(
self, identifier: URIRef
) -> Generator[Graph, None, None]:
"""Bind the specified graph as the current graph.
Expand All @@ -590,20 +590,20 @@ def using_graph(
yield graph

@contextmanager
def new_graph(self) -> Generator[URIRef, None, None]:
"""Create a new graph with a fresh URI and set it as the current graph.
def using_new_buffer(self) -> Generator[URIRef, None, None]:
"""Create a new graph with a fresh URI and set it as the current buffer.
Like (with-temp-buffer) in Emacs, this creates a new space and makes
it our current location. The URI is our address in the semantic web,
automatically chosen to be unique in our namespace - a fresh page
ready for new ideas.
"""
graph_id = fresh_uri(self.namespace)
with self.using_graph(graph_id):
with self.using_buffer(graph_id):
yield graph_id

@contextmanager
def new_activity(
def using_new_activity(
self, activity_type: URIRef, props: dict[P, Any] = {}
) -> Generator[URIRef, None, None]:
"""Create a new activity and set it as the current activity.
Expand Down Expand Up @@ -651,7 +651,7 @@ def new_activity(
)

@contextmanager
def new_agent(
def using_new_agent(
self,
agent_type: URIRef,
props: Optional[dict[P, Any]] = None,
Expand Down Expand Up @@ -761,9 +761,9 @@ def get_base_url(self) -> str:
return self.base_url

@contextmanager
def new_derived_graph(
def using_derived_buffer(
self,
source_graph: Optional[URIRef] = None,
origin: Optional[URIRef] = None,
activity: Optional[URIRef] = None,
) -> Generator[URIRef, None, None]:
"""Create a new graph derived from an existing graph.
Expand All @@ -774,15 +774,13 @@ def new_derived_graph(
major-mode.
Args:
source_graph: The graph this is derived from. Defaults to current_graph.
activity: Optional activity that caused this derivation. Defaults to current_activity.
origin: The graph this is derived from.
Defaults to the current buffer.
activity: Optional activity that caused this derivation.
Defaults to the current activity.
"""
graph_id = fresh_uri(self.namespace)
source = (
source_graph
if source_graph is not None
else context.buffer.get()
)
source = origin if origin is not None else context.buffer.get()
if source is None:
raise ValueError(
"No source graph specified and no current graph set"
Expand Down Expand Up @@ -827,7 +825,7 @@ async def from_env() -> AsyncIterator[Repository]:
"""Load repository and context from environment variables.
Requires the following environment variables:
- BUBBLE: Path to the repository
- BUBBLE_REPO: Path to the repository
- BUBBLE_BASE: Base URL for the repository
- BUBBLE_GRAPH: Current graph URI
- BUBBLE_ACTIVITY: Current activity URI
Expand All @@ -839,7 +837,7 @@ async def from_env() -> AsyncIterator[Repository]:
Raises:
ValueError: If required environment variables are missing
"""
bubble_path = os.environ.get("BUBBLE")
bubble_path = os.environ.get("BUBBLE_REPO")
bubble_base = os.environ.get("BUBBLE_BASE")
bubble_graph = os.environ.get("BUBBLE_GRAPH")
bubble_activity = os.environ.get("BUBBLE_ACTIVITY")
Expand Down
12 changes: 6 additions & 6 deletions src/bubble/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def test_graph_repo_basics():
repo = await Repository.create(git, base_url_template=EX)

# Create initial graph and add data
with repo.new_graph() as graph_id:
with repo.using_new_buffer() as graph_id:
subject = new(EX.Type, {EX.label: Literal("Test")})
assert graph_id in repo.list_graphs()
await repo.save_all()
Expand Down Expand Up @@ -51,12 +51,12 @@ async def test_graph_repo_new_derived_graph():
repo = await Repository.create(git, base_url_template=EX)

# Create source graph
with repo.new_graph() as source_graph_id:
with repo.using_new_buffer() as source_graph_id:
new(EX.Type, {EX.label: Literal("Test")})

# Test explicit derivation
activity = EX.activity1
with repo.new_derived_graph(
with repo.using_derived_buffer(
source_graph_id, activity=activity
) as derived_graph_id:
x = new(EX.Type, {EX.label: Literal("Derived")})
Expand All @@ -78,7 +78,7 @@ async def test_graph_repo_new_derived_graph():
with context.bind_graph(source_graph_id, repo):
current_act = EX.currentActivity
with context.activity.bind(current_act):
with repo.new_derived_graph() as derived_graph_id2:
with repo.using_derived_buffer() as derived_graph_id2:
new(EX.Type, {EX.label: Literal("Derived2")})

# Verify provenance with current context
Expand All @@ -99,7 +99,7 @@ async def test_graph_repo_new_graph():
repo = await Repository.create(git, base_url_template=EX)

# Create new graph and add data
with repo.new_graph() as graph_id:
with repo.using_new_buffer() as graph_id:
new(EX.Type, {EX.label: Literal("Test")})

# Verify graph contents
Expand All @@ -120,7 +120,7 @@ async def test_graph_repo_add_with_current_graph():
repo = await Repository.create(git, base_url_template=EX)

# Test adding with current graph
with repo.new_graph() as graph_id:
with repo.using_new_buffer() as graph_id:
new(EX.Type, {EX.label: Literal("Test")})

# Verify immediate state
Expand Down

0 comments on commit f6bdaab

Please sign in to comment.