xref: /trunk/test/aoo-test/src/aootest_worker/ops/roundtrip.py (revision 2cb6d6f119bbe2f78742f1c5c8538cb0d4e72ede)
1################################################################
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20################################################################
21
22"""Reload checks: evaluate expressions over a loaded document and report the values."""
23import os
24
25import uno
26
27from aootest_worker.ops import core
28from aootest_worker.ops.core import pv
29
30
31def plain(v):
32    """A JSON-able copy of a UNO value."""
33    if isinstance(v, (bool, int, float, str)) or v is None:
34        return v
35    if isinstance(v, (tuple, list)):
36        return [plain(x) for x in v]
37    if isinstance(v, uno.Enum):
38        return v.value
39    if hasattr(v, "typeName"):
40        return {k: plain(getattr(v, k)) for k in dir(v) if not k.startswith("_") and k not in ("typeName", "value")}
41    return repr(v)
42
43
44def roundtrip(path, checks):
45    """Loads `path` and evaluates each {"name", "expr"} of `checks` with `doc` and `uno` in scope. The expressions come from
46    the suite's own data files. Returns {name: value}; an expression that raises gives the string "ERROR <repr>"."""
47    doc = core._desktop().loadComponentFromURL(uno.systemPathToFileUrl(os.path.abspath(path)), "_blank", 0,
48                                               (pv("Hidden", True), pv("ReadOnly", True), pv("MacroExecutionMode", 0), pv("UpdateDocMode", 0)))
49    if doc is None:
50        raise core.Fail("uno", "load returned None for %s" % path)
51    got = {}
52    try:
53        for c in checks:
54            try:
55                got[c["name"]] = plain(eval(c["expr"], {"doc": doc, "uno": uno}))
56            except Exception as e:
57                got[c["name"]] = "ERROR " + repr(e)
58    finally:
59        core._close(doc)
60    return got
61
62
63OPS = {"roundtrip": core.guarded(roundtrip)}
64