Skip to content

Enhancements to python-discuss from the pergamon project #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion discuss/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def autoreconnect(self, *args, **kwargs):
class Client(object):
"""Discuss client."""

def __init__(self, server, port = 2100, auth = True, timeout = None):
def __init__(self, server, port = 2100, auth = True, timeout = None, RPCClient=RPCClient):
self.rpc = RPCClient(server, port, auth, timeout)
if auth and self.who_am_i().startswith("???@"):
raise ProtocolError("Authentication to server failed")
Expand All @@ -65,6 +65,17 @@ def who_am_i(self):
reply = self.rpc.request(request)
return reply.read_string()

@autoreconnects
def create_mtg(self, location, long_mtg_name, public):
request = USPBlock(constants.CREATE_MTG)
request.put_string(location)
request.put_string(long_mtg_name)
request.put_boolean(public)
reply = self.rpc.request(request)
result = reply.read_long_integer()
if result != 0:
raise DiscussError(result)

def close(self):
"""Disconnect from the server."""

Expand Down Expand Up @@ -300,6 +311,15 @@ def set_access(self, principal, modes):
if result != 0:
raise DiscussError(result)

def ensure_access(self, principal, modes):
current = self.get_access(principal)
self.set_access(principal, current+modes)

def remove_access(self, principal, modes):
current = self.get_access(principal)
new_modes = ''.join(c for c in current if not c in modes)
self.set_access(principal, new_modes)

@autoreconnects
def undelete_transaction(self, trn_number):
"""Undelete the transaction by its number."""
Expand Down
21 changes: 21 additions & 0 deletions discuss/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
#

import errno
import fcntl
import socket
from struct import pack, unpack, calcsize
import subprocess
from functools import partial

from . import constants
Expand Down Expand Up @@ -314,3 +316,22 @@ def request(self, block):
raise ProtocolError("Transport-level error")
return reply

class RPCLocalClient(RPCClient):
# Args are for compatibility with the remote RPC; most aren't used
def __init__(self, server, port, auth, timeout):
# Used as the id field on meeting objects, so copy it in
self.server = server
# port 2100 is the default port -> use the binary
if port == 2100:
port = '/usr/sbin/disserve'
self.cmd = port

self.connect()
self.make_wrapper()

def connect(self):
pair = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM)
subprocess.Popen([self.cmd], stdin=pair[1], close_fds=True)
pair[1].close()
fcntl.fcntl(pair[0].fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel confused by the purpose of that line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ummm... Most specifically, if we fork and exec, we shouldn't pass the socket pair into the child. I'm guessing I got that from some "here's how to do sockets in Python". It seems like probably a reasonable general practice in a library, to avoid impacting any exec'd children. shrug I'm certainly not devoted to keeping it.

(I, um, clearly managed to miss/forget/something this comment.)

self.socket = pair[0]