Skip to content

Support package resources (importlib.resources.abc.Traversable) for interface_dir #86

@tcyrus

Description

@tcyrus

The easiest way to do this is to convert str inputs for interface_dir to pathlib.Path. importlib.resources.abc.Traversable is a subset of pathlib.Path.

self.interface_dir = interface_dir

        if isinstance(interface_dir, str):
            interface_dir = Path(interface_dir)
        self.interface_dir = interface_dir

This would narrow the type of self.interface_dir to pathlib.Path | importlib.resources.abc.Traversable, which both implement joinpath and read_text.

python/varlink/server.py

Lines 260 to 267 in 70735a7

def _add_interface(self, filename, handler):
if not os.path.isabs(filename):
filename = os.path.join(self.interface_dir, filename + ".varlink")
with open(filename) as f:
interface = Interface(f.read())
self.interfaces[interface.name] = interface
self.interfaces_handlers[interface.name] = handler

    def _add_interface(self, filename, handler):
        filepath = filename
        if not isinstance(filepath, importlib.resources.abc.Traversable):
            filepath = Path(filename)
        if isinstance(filename, str) and not filepath.is_file():
            filepath = self.interface_dir.joinpath(filename + ".varlink")
        interface = Interface(filepath.read_text())
        self.interfaces[interface.name] = interface
        self.interfaces_handlers[interface.name] = handler

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions