Beispiel #1
0
    def store_document(
        self,
        index: str,
        body: dict,
        inject_metadata=True,
        *args,
        **kwargs,
    ):
        util.check_type(index, str)
        util.check_type(body, dict)
        if 'doc_type' in kwargs:
            raise ValueError('''
                doc_type attribute has been deprecated - see:
                https://www.elastic.co/guide/en/elasticsearch/reference/6.0/removal-of-types.html
                ''')

        if inject_metadata and _metadata_dict():
            md = _metadata_dict()
            body['cc_meta'] = md

        return self._api.index(
            index=index,
            doc_type='_doc',
            body=body,
            *args,
            **kwargs,
        )
Beispiel #2
0
    def __new__(
        cls,
        category_id: str,
        target_group_id: str,
        text: str,
        reference_type: ReferenceType,
        reference_id: str,
        user_login: str,
        source_repo: str,
        cn_current_repo: ComponentName,
    ):
        if reference_id:
            check_type(reference_id, str)

        reference = Reference(type=reference_type, identifier=reference_id)

        cn_source_repo = ComponentName(name=source_repo)
        is_current_repo = cn_current_repo == cn_source_repo
        from_same_github_instance = cn_current_repo.github_host() == cn_source_repo.github_host()
        self = super().__new__(
            cls,
            category_id,
            target_group_id,
            text,
            reference,
            user_login,
            is_current_repo,
            from_same_github_instance,
            cn_source_repo
        )
        return self
Beispiel #3
0
def greatest_references(references: typing.Iterable[DependencyBase]):
    '''
    yields the component references from the specified iterable of ComponentReference that
    have the greates version (grouped by component name).
    Id est: if the sequence contains exactly one version of each contained component name,
    the sequence is returned unchanged.
    '''
    not_none(references)
    references = list(references)
    for ref in references:
        check_type(ref, DependencyBase)

    names = [ref.name() for ref in references]

    for name in names:
        matching_refs = [r for r in references if r.name() == name]
        if len(matching_refs) == 1:
            # in case reference name was unique, do not bother sorting
            # (this also works around issues from non-semver versions)
            yield matching_refs[0]
            continue

        # there might be multiple component versions of the same name
        # --> use the greatest version in that case
        matching_refs = sorted(
            matching_refs,
            key=lambda r: semver.parse_version_info(r.version()),
        )
        # greates version comes last
        yield matching_refs[-1]
Beispiel #4
0
    def __init__(self, variables, graphs, children):
        """
        Creates a new symbolic process.
        :param variables: The variables defined on the root level of this symbolic process.
        :param graphs: The control flow graphs the parallel composition of which define the external behavior of this process.
        :param children: A dict mapping identifiers to child processes that this process controls internally.
        """
        super().__init__()

        self._members = {}
        for v in variables:
            check_type(v, Variable)
            if v.name in self._members.keys():
                raise ValueError("There must not be two member variables with the same name!")
            v.own(self)
            self._members[v.name] = v

        self._behavior = tuple(check_type(g, Graph) for g in graphs)
        self._alphabet_fromouter = frozenset(a for g in graphs for a in g.alphabet_from_outer)

        for k, c in children.items():
            check_type(c, SymbolicProcess)
            if k in self._members.keys():
                raise ValueError("The name '{}' was specified for at least one child process and at the same time"
                                 " for another child process or variable, which is illegal.".format(k))
            self._members[k] = c
Beispiel #5
0
    def store_bulk(
        self,
        body: str,
        inject_metadata=True,
        *args,
        **kwargs,
    ):
        util.check_type(body, str)

        if inject_metadata and _metadata_dict():

            def inject_meta(line):
                parsed = json.loads(line)
                if 'index' not in parsed:
                    parsed['cc_meta'] = md
                    return json.dumps(parsed)
                return line

            md = _metadata_dict()
            patched_body = '\n'.join(
                [inject_meta(line) for line in body.splitlines()])
            body = patched_body

        return self._api.bulk(
            body=body,
            *args,
            **kwargs,
        )
Beispiel #6
0
 def __getitem__(self, pos_able):
   if not isinstance(pos_able, Pos):
     assert hasattr(pos_able, 'pos'), pos_able
     pos = pos_able.pos
   else:
     check_type(pos_able, Pos)
     pos = pos_able
   return self._loc_matrix[pos.y][pos.x]
Beispiel #7
0
 def __init__(self, guard, statement, **kwargs):
     """
     Creates a new guarded statement.
     :param guard: The expression that needs to evaluate to True for this statement to be enabled.
     :param statement: The statement that is guarded.
     :param kwargs: See statement constructor.
     """
     super().__init__(check_type(guard, Expression),
                      check_type(statement, Statement), **kwargs)
Beispiel #8
0
    def __init__(self, *children, start=None, end=None):
        super().__init__()

        for c in children:
            check_type(c, Node)

        self._children = children
        self._start = check_type(start, TokenPosition)
        self._end = check_type(end, TokenPosition)
Beispiel #9
0
 def __init__(self, condition, body, **kwargs):
     """
     Creates a new while loop.
     :param guard: The loop condition.
     :param body: The loop body.
     :param kwargs: See statement constructor.
     """
     super().__init__(check_type(condition, Expression),
                      check_type(body, Statement), **kwargs)
Beispiel #10
0
 def __init__(self, callee, *args, **kwargs):
     """
     Creates procedure call.
     :param callee: The expression representing the procedure to be called.
     :param args: The expressions representing the arguments to the call.
     :param kwargs: See AssigneableExpression constructor.
     """
     super().__init__(check_type(callee, Expression),
                      *(check_type(a, Expression) for a in args), **kwargs)
Beispiel #11
0
 def __init__(self, value, identifier, **kwargs):
     """
     Creates an attribute lookup.
     :param value: The expression representing the value to retrieve the attribute form.
     :param identifier: The identifier of the attribute to retrieve.
     :param kwargs: See AssigneableExpression constructor.
     """
     super().__init__(check_type(value, Expression), **kwargs)
     self._identifier = check_type(identifier, Identifier)
Beispiel #12
0
 def __init__(self, op, arg, **kwargs):
     """
     Creates a new unary operation.
     :param op: The operator for this operation.
     :param left: The operand expression.
     :param kwargs: See Expression constructor.
     """
     super().__init__(check_type(arg, Expression), **kwargs)
     self._op = check_type(op, Enum)
Beispiel #13
0
def _parse_image_reference(image_reference):
    util.check_type(image_reference, str)

    if '@' in image_reference:
        name = docker_name.Digest(image_reference)
    else:
        name = docker_name.Tag(image_reference)

    return name
Beispiel #14
0
 def __init__(self, target, value, **kwargs):
     """
     Creates a new assignment.
     :param target: An assignable expression.
     :param value: The expression the value of which is to be assigned.
     :param kwargs: See Statement constructor.
     """
     super().__init__(check_type(target, AssignableExpression),
                      check_type(value, Expression), **kwargs)
Beispiel #15
0
 def __init__(self, action_identifier, statement, **kwargs):
     """
     Creates a action-decorated statement.
     :param action: The action label decorating this statement.
     :param statement: The statement that is decorated.
     :param kwargs: See statement constructor.
     """
     super().__init__(check_type(action_identifier, Identifier),
                      check_type(statement, Statement), **kwargs)
Beispiel #16
0
def _credentials(image_reference: str, privileges: Privileges = None):
    util.check_type(image_reference, str)
    registry_cfg = model.container_registry.find_config(
        image_reference, privileges)
    if not registry_cfg:
        return None
    credentials = registry_cfg.credentials()
    return docker_creds.Basic(username=credentials.username(),
                              password=credentials.passwd())
Beispiel #17
0
 def __init__(self, target, index, **kwargs):
     """
     Creates a new projection expression.
     :param components: The expression representing the sequence to project from.
     :param index: The expression representing the projection index.
     :param kwargs: See AssigneableExpression constructor.
     """
     super().__init__(check_type(target, Expression),
                      check_type(index, Expression), **kwargs)
def read_json_definition(json_file: Path, source_dir: Path) -> Data:
    check_type(json_file, Path)
    if not json_file.exists():
        raise FileExistsError(f'{str(json_file)}')
    with json_file.open('r') as f:
        data = json.load(f)
        song_files = [source_dir / Path(s) for s in data['songs']]
        book_name = data['book_name']
        return Data(song_files=song_files, book_name=book_name, project_title=data['project_title'],
                    s3=data['s3'])
Beispiel #19
0
    def target_matches(self, reference: DependencyBase):
        util.check_type(reference, DependencyBase)

        if reference.type_name() != self.reference_type_name:
            return False
        if reference.name() != self.ref_name:
            return False
        if reference.version() != self.to_ref.version():
            return False

        return True
Beispiel #20
0
 def __init__(self, m):
     """
     Creates a new valuation.
     :param m: A dict mapping Variable objects to Value objects.
     """
     super().__init__()
     self._m = {
         check_type(k, Variable): check_type(v, k.dtype)
         for k, v in m.items()
     }
     self._hash = None
Beispiel #21
0
 def add_pairs(self, user_id, pairs):
     pairs = json.loads(pairs)
     util.check_type(pairs, [{
         "url": unicode,
         "nl": unicode,
         "cmd": unicode
     }],
                     value_name="pairs")
     with DBConnection() as db:
         db.add_pairs(user_id=user_id, pairs=pairs)
         return True
Beispiel #22
0
def reference_type(name: str):
    check_type(name, str)
    if name == 'component':
        return ComponentReference
    if name == 'container_image':
        return ContainerImageReference
    if name == 'generic':
        return GenericDependencyReference
    if name == 'web':
        return WebDependencyReference
    raise ValueError('unknown dependency type name: ' + str(name))
Beispiel #23
0
 def __init__(self, name, argnames, body, **kwargs):
     """
     Creates a procedure definition.
     :param name: The name of the procedure to be defined.
     :param argnames: The names of the arguments of the procedure to be defined.
     :param body: The body of the procedure.
     :param kwargs: See statement constructor.
     """
     super().__init__(check_type(name, Identifier),
                      *(check_type(n, Identifier) for n in argnames),
                      check_type(body, Statement), **kwargs)
Beispiel #24
0
 def __init__(self, op, left, right, **kwargs):
     """
     Creates a new binary operation.
     :param op: The operator for this operation.
     :param left: The left operand expression.
     :param right: The right operand expression.
     :param kwargs: See Expression constructor.
     """
     super().__init__(check_type(left, Expression),
                      check_type(right, Expression), **kwargs)
     self._op = check_type(op, Enum)
Beispiel #25
0
    def add_edge(self, e):
        """
        Adds an edge originating from this location.
        :param e: An Edge object.
        """

        if self._graph is not None:
            raise OwnershipError("This location is already part of a graph and thus cannot be modified anymore!")

        check_type(e, Edge)
        e.own(self)
        self._edges.append(e)
Beispiel #26
0
    def filter_attrs(attrs: 'typing.Iterable[AttributeSpec]',
                     required: RequiredPolicy):
        if required:
            util.check_type(required, RequiredPolicy)
        else:
            # no filtering
            yield from attrs

        for attr in attrs:
            util.check_type(attr, AttributeSpec)
            if attr.required_policy() is required:
                yield attr
Beispiel #27
0
 def draw(self, display):
     # Place the background to fill the surface.
     if self.background is not None:
         if util.check_type(self.background, "Color"):
             self.frame_surface.fill(self.background)
         elif util.check_type(self.background, "Sprite"):
             self.background.scale(self.w, self.h)
             self.frame_surface.blit(self.background, (0, 0))
     # Draw each component in order.
     for component in self.components:
         component.draw(self.frame_surface)
     # Draw the frame to the screen.
     display.blit(self.frame_surface, (self.x, self.y))
Beispiel #28
0
def _retrieve_matching_credentials(image_reference: str,
                                   privileges: Privileges = None):
    util.check_type(image_reference, str)
    cfg_factory = util.ctx().cfg_factory()

    matching_cfgs = [
        cfg for cfg in cfg_factory._cfg_elements('container_registry')
        if cfg.image_ref_matches(image_reference, privileges=privileges)
    ]

    if not matching_cfgs:
        return None

    # return first match
    return matching_cfgs[0].credentials()
Beispiel #29
0
 def __init__(self, value, **kwargs):
     """
     Creates a new return statement.
     :param value: The expression computing the value that is to be returned.
     :param kwargs: See Statement constructor.
     """
     super().__init__(check_type(value, Expression), **kwargs)
Beispiel #30
0
    def __init__(self, l0):
        """
        Creates a new process graph.
        :param l0: The initial control flow location of this graph.
        """
        super().__init__()
        self._l0 = check_type(l0, Location)

        # Own all the locations in the given graph:
        ls = set()
        agenda = {l0}
        while len(agenda) > 0:
            l = agenda.pop(0)
            l.own(self)
            ls.add(l)
            for e in l.edges:
                ll = e.destination
                if ll.owner is None:
                    agenda.add(ll)

        self._locations = frozenset(ls)
        self._alphabet = None
        self._alphabet_fromouter = None
        self._alphabet_parallel = None
        self._variables = None
Beispiel #31
0
def warshall(matrix, not_connected_value=-1):
    """ The Implementation of the Warshall Algorithmus
    """
    check_type(matrix)
    check_shape(matrix)
    dim = matrix.shape[0]
    hull = matrix.copy()
    np.place(hull, hull == not_connected_value, float('inf'))
    for k in range(dim):
        for i in range(dim):
            if hull[i, k] == 1:
                for j in range(dim):
                    if hull[k, j] == 1:
                        hull[i, j] = 1
    np.place(hull, hull == float('inf'), not_connected_value)
    return hull
Beispiel #32
0
def floyd(matrix, not_connected_value=-1):
    """ Floyd Algorithm
    """
    check_type(matrix)
    check_shape(matrix)
    dim = matrix.shape[0]
    dist = matrix.copy()
    np.place(dist, dist == not_connected_value, float('inf'))
    for k in range(dim):
        for i in range(dim):
            for j in range(dim):
                if j == i:
                    continue
                if dist[i, j] > dist[i, k] + dist[k, j]:
                    dist[i, j] = dist[i, k] + dist[k, j]
    np.place(dist, dist == float('inf'), not_connected_value)
    return dist
Beispiel #33
0
 def add_pairs(self, user_id, pairs):
     pairs = json.loads(pairs)
     util.check_type(pairs, [{"url":unicode, "nl":unicode, "cmd":unicode}], value_name="pairs")
     with DBConnection() as db:
         db.add_pairs(user_id=user_id, pairs=pairs)
         return True