示例#1
0
文件: planner.py 项目: UKCloud/maloja
def read_objects(text):
    log = logging.getLogger("maloja.planner")
    contents = yaml_loads(text)
    if contents is None:
        log.warning("No objects found.")
        return
    elif isinstance(contents, collections.abc.Mapping):
        contents = [contents]

    for n, data in enumerate(contents):
        try:
            typ = types[data.get("type", None)]
        except KeyError:
            log.warning("Type unrecognised at item {}".format(n + 1))
            continue

        attribs = {k: data.get(k, None) for k, v in typ._defaults}
        try:
            obj = typ(**attribs)
        except TypeError as e:
            log.warning("Type mismatch at item {}".format(n + 1))
            log.warning(e)
            obj = None

        yield obj
示例#2
0
 def test_object_cache(self):
     self.maxDiff = 1200
     for obj, path in self.fixture:
         with self.subTest(path=path):
             fP = cache(path, obj)
             with open(fP, 'r') as data:
                 text = data.read()
                 rv = type(obj)(**yaml_loads(text))
                 self.assertEqual(vars(obj), vars(rv))
示例#3
0
文件: path.py 项目: UKCloud/maloja
def find_ypath(path: Path, query, **kwargs):
    """
    Find objects within the Maloja cache tree whose attributes match certain
    values.

    :param path: search location in cache.
    :param query: an archetype of the object to look for.
    :param kwargs: specifies attribute values to filter by.
     If no keyword arguments are supplied, then attributes
     on the query object serve as criteria to be matched.

    :return: An iterator over matching (path, object) tuples.
    """
    log = logging.getLogger("maloja.path.find_ypath")
    wildcards = [i if i is not None else '*' for i in path[:-1]]
    locations = {
        Project: wildcards[:2] + ["project.yaml"],
        Org: wildcards[:3] + ["org.yaml"],
        Catalog: wildcards[:5] + ["catalog.yaml"],
        Gateway: wildcards[:4] + ["edge.yaml"],
        Vdc: wildcards[:4] + ["vdc.yaml"],
        Network: wildcards[:6] + ["net.yaml"],
        VApp: wildcards[:6] + ["vapp.yaml"],
        Template: wildcards[:6] + ["template.yaml"],
        Vm: wildcards[:7] + ["vm.yaml"],
    }
    typ = type(query)
    criteria = set(kwargs.items()) or set(query.elements)
    pattern = os.path.join(*locations[typ])
    for fP in glob.glob(pattern):
        obj = None
        try:
            locks[fP].acquire()
            with open(fP, 'r') as data:
                obj = typ(**yaml_loads(data.read()))
        finally:
            locks[fP].release()

        if obj is not None and criteria.issubset(set(obj.elements)):
            tail = fP[len(path.root):].split(os.sep)
            pack = 8 - len(locations[typ])
            hit = [path.root] + tail[1:-1] + [None] * pack + tail[-1:]
            yield (Path(*hit), obj)