Skip to content

Dynamic load #291

Open
Open
@madhavajay

Description

@madhavajay

I want to be able to define the capnp definitions at run time.

It seems that .load won't accept anything but a real file due to this exception:

if not _os.path.isfile(file_name):
    raise IOError("File not found: " + file_name)

I can work around this by saving a file to /tmp and loading it which seems silly.
My question is other than only having the same module name once in the module tree is there any reason why this shouldn't be allowed? I am happy to open a pr which allows this work unless there is some reason why it should never be done.

I imagine that allowing a network user to define an arbitrary capnp definition and having that initialized miiiight be a concern? But I guess the actual serde code where the traversal_limit_in_words is set is the main concern?

import time
import capnp


def str_to_capnp(proto: str) -> type:
    temp_url = f"/tmp/{int(time.time())}.capnp"
    with open(temp_url, "w+") as f:
        f.write(proto)
    f.close()
    print(temp_url)
    return capnp.load(temp_url)


s = """
@0x8f9c60bd7a9842fc;

struct REPT {
  child @0 :Data;
  minVals @1 :Data;
  maxVals @2 :Data;
  entities @3 :Data;
}
"""

x = str_to_capnp(s)
print(x, type(x))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions