def _build_group_tree(self, timestamp):
        """Build a tree of all the groups in the organization.

        Args:
            timestamp: String of snapshot timestamp, formatted as
                YYYYMMDDTHHMMSSZ.

        Returns:
            The root node that holds the tree structure of all the groups
                in the organization.
        """
        root = MemberNode(MY_CUSTOMER, MY_CUSTOMER)

        all_groups = self.dao.get_all_groups('groups', timestamp)
        for group in all_groups:
            group_node = MemberNode(group.get('group_id'),
                                    group.get('group_email'), 'group',
                                    'ACTIVE', root)
            group_node = self.get_recursive_members(group_node, timestamp)

        LOGGER.debug(
            anytree.RenderTree(
                root, style=anytree.AsciiStyle()).by_attr('member_email'))

        return root
    def _render_ascii(self, starting_node, attr):
        """Render an ascii representation of the tree structure.

        Args:
            starting_node: The starting node to render the ascii.

        Returns:rm 
            attr: String of the attribute to render.
        """
        return anytree.RenderTree(starting_node,
                                  style=anytree.AsciiStyle()).by_attr(attr)
    def dumpDocumentTree(
        self,
        document: ValueTreeNode,
        tree_traversal_options: Optional[TreeTraversalOptions] = None,
        generator_options: Optional[GeneratorOptions] = None,
    ) -> str:
        """Dump the document tree as a string"""

        generator_options = generator_options or GeneratorOptions()

        anytree_tree = DocumentExplorer.generateAnytreeTree(
            document=document,
            tree_traversal_options=tree_traversal_options,
            generator_options=generator_options,
        )

        self.sink.reset()

        # Non-ascii styles cause problems with encoding on Windows system
        if os.name == "nt":
            style = anytree.AsciiStyle()
        else:
            style = anytree.ContStyle()

        for pre, _, node in anytree.RenderTree(anytree_tree, style=style):

            if hasattr(node, "value_tree_value"):
                node_representation = prettyPrintValue(
                    node.value_tree_value,
                    display_values=self.display_values,
                    name_alias=node.name,
                )
            elif hasattr(node, "value_tree_node"):
                node_representation = prettyPrintNode(
                    node.value_tree_node,
                    display_values=self.display_values,
                    name_alias=node.name,
                )
            else:
                node_representation = node.name
            self.sink.output(f"{pre}{node_representation}")

        return self.sink.flush()
Example #4
0
    def _render_ascii(self, starting_node, attr):
        """Render an ascii representation of the tree structure.

        Args:
            starting_node: The starting node to render the ascii.

        Returns:
            attr: String of the attribute to render.
        """
        rows = []
        for pre, fill, node in anytree.RenderTree(starting_node,
                                                  style=anytree.AsciiStyle()):
            value = getattr(node, attr, "")
            if isinstance(value, (list, tuple)):
                lines = value
            else:
                lines = str(value).split("\n")
            rows.append(u"%s%s" % (pre, json.dumps(lines[0], sort_keys=True)))
            for line in lines[1:]:
                rows.append(u"%s%s" % (fill, json.dumps(line, sort_keys=True)))

        return '\n'.join(rows)
Example #5
0
    def _build_group_tree(self):
        """Build a tree of all the groups in the organization.

        Returns:
            node: The tree structure of all the groups in the organization.
        """
        root = MemberNode(MY_CUSTOMER, MY_CUSTOMER)
        model_manager = self.service_config.model_manager
        scoped_session, data_access = model_manager.get(self.model_name)
        with scoped_session as session:
            all_groups = data_access.iter_groups(session)

            for group in all_groups:
                group_node = MemberNode(group.name, group.member_name,
                                        group.type, 'ACTIVE', root)
                self._get_recursive_members(group_node)

        LOGGER.debug(
            anytree.RenderTree(
                root, style=anytree.AsciiStyle()).by_attr('member_email'))

        return root
Example #6
0
def test_asciistyle():
    style = anytree.AsciiStyle()
    eq_(style.vertical, u'|   ')
    eq_(style.cont, '|-- ')
    eq_(style.end, u'+-- ')
    def _find_runnable_pipelines(self, root):
        """Find the enabled pipelines to run.

        Args:
            root (PipelineNode): Represents the top-level starting point
                of the pipeline dependency tree. The entire pipeline
                dependency tree are tuple of children PipelineNodes
                of this root.

                Example:
                root.resource_name = 'organizations'
                root.enabled = True
                root.parent = None
                root.children = (pipeline_node1, pipeline_node2, ...)

        Returns:
            list: List of the pipelines that will be run. The
                order in the list represents the order they need to be run.
                i.e. going top-down in the dependency tree.
        """
        # If child pipeline is true, then all parents will become true.
        # Even if the parent(s) is(are) false.
        # Manually traverse the parents since anytree walker api doesn't
        # make sense.
        for node in anytree.iterators.PostOrderIter(root):
            if node.enabled:
                while node.parent is not None:
                    node.parent.enabled = node.enabled
                    node = node.parent

        LOGGER.debug(
            'Dependency tree of the pipelines: %s',
            anytree.RenderTree(
                root, style=anytree.AsciiStyle()).by_attr('resource_name'))
        LOGGER.debug(
            'Which pipelines are enabled: %s',
            anytree.RenderTree(root,
                               style=anytree.AsciiStyle()).by_attr('enabled'))

        # Now, we have the true state of whether a pipeline should be run.
        # Get a list of pipeline instances that will actually be run.
        # The order matters: must go top-down in the tree, by PreOrder.
        # http://anytree.readthedocs.io/en/latest/apidoc/anytree.iterators.html
        runnable_pipelines = []
        for node in anytree.iterators.PreOrderIter(root):
            if node.enabled:
                module_path = 'google.cloud.security.inventory.pipelines.{}'
                module_name = module_path.format(
                    pipeline_requirements_map.REQUIREMENTS_MAP.get(
                        node.resource_name).get('module_name'))
                try:
                    module = importlib.import_module(module_name)
                except (ImportError, TypeError, ValueError) as e:
                    LOGGER.error('Unable to import %s\n%s', module_name, e)
                    continue

                # Convert module naming to class naming.
                # Module naming is "this_is_foo"
                # Class naming is "ThisIsFoo"
                class_name = (pipeline_requirements_map.REQUIREMENTS_MAP.get(
                    node.resource_name).get('module_name').title().replace(
                        '_', ''))
                try:
                    pipeline_class = getattr(module, class_name)
                except AttributeError:
                    LOGGER.error('Unable to instantiate %s\n%s', class_name,
                                 sys.exc_info()[0])
                    continue

                api_name = (pipeline_requirements_map.REQUIREMENTS_MAP.get(
                    node.resource_name).get('api_name'))
                try:
                    api = self._get_api(api_name)
                except api_errors.ApiInitializationError:
                    continue

                dao = self.dao_map.get(
                    pipeline_requirements_map.REQUIREMENTS_MAP.get(
                        node.resource_name).get('dao_name'))
                if dao is None:
                    LOGGER.error('Unable to find dao for %s',
                                 node.resource_name)
                    continue

                pipeline = pipeline_class(self.cycle_timestamp,
                                          self.global_configs, api, dao)
                runnable_pipelines.append(pipeline)

        return runnable_pipelines
Example #8
0
            todo_cards.append((leftedge_card_name, new_node1))

        while (todo_cards):
            todo_card = todo_cards[0]
            todo_cards = todo_cards[1:]
            after_cardnames = self.get_after_cards(todo_card[0])
            for after_cardname in after_cardnames:
                new_node2 = anytree.Node(
                    self.get_card(after_cardname).get('display_name'),
                    todo_card[1])
                if after_cardname in rightedge_cards_dict.keys():
                    anytree.Node('[# 出口 #]', new_node2)
                else:
                    pass
                todo_cards.append((after_cardname, new_node2))

        return ktree_inport


# 载入知识图 json 数据
with open('khan_kst.json', 'r', encoding='utf8') as kst_json:
    kst_json_dict = json.load(kst_json)

cards_list = kst_json_dict.get('knowledge_graph_data')

cardspace1 = CardSpace('数学知识图', cards_list)

ktree_root = cardspace1.build_knowledge_tree()

print(anytree.RenderTree(ktree_root, style=anytree.AsciiStyle()).by_attr())
Example #9
0
def write_tree(root, filepath):
    
    with open(filepath, 'w') as f:
        renderTree = anytree.RenderTree(root, style=anytree.AsciiStyle())
        f.write(renderTree.by_attr())