Source code

Download source archive ↓

src/contract.py

1"""Shared bytes-stream contract for the reviewed Galaxy adapters."""
2
3import base64
4import math
5
6from xstream_operator import bounded_sample, descriptor, result, run_operator, sample_bytes
7
8MAX_INPUT = 4 * 1024 * 1024
9MAX_OUTPUT = 8 * 1024 * 1024
10MAX_ROWS = 25000
11
12
13def collect(chunks, limit=MAX_INPUT):
14    data = bytearray()
15    for chunk in chunks:
16        if len(chunk) + len(data) > limit:
17            raise ValueError(f"input exceeds {limit} bytes")
18        data.extend(chunk)
19    return bytes(data)
20
21
22def text(data):
23    decoded = data.decode("utf-8")
24    if "\0" in decoded:
25        raise ValueError("NUL characters are invalid in text")
26    return decoded
27
28
29def lines(data):
30    rows = text(data).replace("\r\n", "\n").replace("\r", "\n").split("\n")
31    if rows[-1] == "":
32        rows.pop()
33    if len(rows) > MAX_ROWS or any(len(row) > 65536 for row in rows):
34        raise ValueError("too many rows or oversized text row")
35    return rows
36
37
38def validate(params, schema):
39    if not isinstance(params, dict) or params.keys() - schema.keys():
40        raise ValueError("unknown operator parameter")
41    configured = {}
42    for key, spec in schema.items():
43        value = params.get(key, spec["default"])
44        kind = spec["type"]
45        if kind == "integer":
46            valid = type(value) is int
47        elif kind == "number":
48            valid = type(value) in (int, float) and abs(value) <= 1e100 and math.isfinite(value)
49        elif kind == "boolean":
50            valid = type(value) is bool
51        elif kind == "string":
52            valid = isinstance(value, str) and len(value) <= spec.get("maxLength", 64)
53        else:
54            raise ValueError("unsupported parameter declaration")
55        if not valid or kind in ("integer", "number") and not spec["minimum"] <= value <= spec["maximum"]:
56            raise ValueError(f"invalid parameter: {key}")
57        if "enum" in spec and value not in spec["enum"]:
58            raise ValueError(f"invalid parameter: {key}")
59        configured[key] = value
60    return configured
61
62
63class Adapter:
64    def __init__(self, operation, ports, format, schema, execute):
65        self.operation = operation
66        self.ports = set(ports)
67        self.format = format
68        self.schema = schema
69        self.execute = execute
70
71    def _check(self, inputs, params):
72        if set(inputs) != self.ports:
73            raise ValueError("incorrect input ports")
74        return validate(params, self.schema)
75
76    def _execute(self, inputs, params):
77        output = self.execute(self.operation, inputs, params)
78        if len(output) > MAX_OUTPUT:
79            raise ValueError("result exceeds 8 MiB")
80        return output
81
82    def process(self, context):
83        params = self._check(context.input_names, context.params)
84        inputs = {name: collect(context.input(name).bytes()) for name in context.input_names}
85        output = self._execute(inputs, params)
86        writer = context.bytes_output("result", self.format)
87        for offset in range(0, len(output), 65536):
88            writer.write(output[offset:offset + 65536])
89
90    def infer_outputs(self, inputs, params):
91        self._check(inputs, params)
92        if any(value["representations"] and "bytes" not in value["representations"] for value in inputs.values()):
93            raise ValueError("expected a bytes input")
94        return result({"result": descriptor(["bytes"], [self.format], certainty="known")})
95
96    def preview_outputs(self, inputs, params, limits):
97        params = self._check(inputs, params)
98        samples = {}
99        for name, value in inputs.items():
100            sample = value.get("sample")
101            if not sample or sample["representation"] != "bytes" or sample["truncated"]:
102                return {}
103            samples[name] = base64.b64decode(sample["data_base64"], validate=True)
104        if any(len(data) > 4096 for data in samples.values()):
105            return {}
106        try:
107            data = self._execute(samples, params)
108        except ValueError:
109            return {}
110        sample = bounded_sample(sample_bytes(data, format=self.format), limits)
111        return {"result": sample}
112
113    def main(self):
114        run_operator(self.process, self.infer_outputs, preview_outputs=self.preview_outputs)