Example #1
0
def render_tree(repository: "Repository") -> None:
    """Draws the repository's commit graph as a Git-like tree."""

    import asciitree
    from splitgraph.core.output import truncate_line

    # Get all commits in ascending time order
    all_images = {i.image_hash: i for i in repository.images()}

    if not all_images:
        return

    latest = repository.images["latest"]

    tag_dict = defaultdict(list)
    for img, img_tag in repository.get_all_hashes_tags():
        tag_dict[img].append(img_tag)
    tag_dict[latest.image_hash].append("latest")

    class ImageTraversal(asciitree.DictTraversal):
        def get_text(self, node):
            image = all_images[node[0]]
            result = format_image_hash(image.image_hash)
            result += format_tags(tag_dict[image.image_hash])
            result += format_time(image.created)
            if image.comment:
                result += " " + truncate_line(image.comment)
            return result

    tree: OrderedDict[str, OrderedDict] = OrderedDict(
        (image, OrderedDict()) for image in all_images
    )
    tree_elements = tree.copy()

    # Join children to parents to prepare a tree structure for asciitree
    for image in all_images.values():
        if image.parent_id is None or image.parent_id not in tree_elements:
            # If we only pulled a single image, it's possible that we won't have
            # the metadata for the image's parent.
            continue

        tree_elements[image.parent_id][image.image_hash] = tree_elements[image.image_hash]
        del tree[image.image_hash]

    # tree = _pull_up_children(tree)

    renderer = asciitree.LeftAligned(
        draw=asciitree.BoxStyle(
            gfx=asciitree.drawing.BOX_ASCII if SG_CMD_ASCII else asciitree.drawing.BOX_LIGHT,
            label_space=1,
            horiz_len=0,
        ),
        traverse=ImageTraversal(),
    )
    for root, root_tree in tree.items():
        click.echo(renderer({root: root_tree}))
Example #2
0
    def handle(self, *args, **options):

        if not options["project_id"]:
            projects = Project.objects.all()
        else:
            projects = []
            for project_id in options["project_id"]:
                matched_projects = Project.objects.filter(
                    Q(guid__icontains=project_id)
                    | Q(name__icontains=project_id))
                if not matched_projects:
                    logger.warn("No matching project found for keyword: %s" %
                                project_id)
                projects.extend(matched_projects)

        projects_tree = OrderedDict()
        for project_i, project in enumerate(projects):
            project_label = "P%s project: %s. GRCh%s" % (
                project_i + 1, project, project.genome_version)
            project_tree = projects_tree[project_label] = OrderedDict()
            for family_i, family in enumerate(
                    Family.objects.filter(project=project)):
                family_label = "F%s family: %s" % (
                    family_i + 1,
                    family,
                )
                family_tree = project_tree[family_label] = OrderedDict()
                for individual_i, individual in enumerate(
                        Individual.objects.filter(family=family)):
                    individual_label = "I%s individual: %s" % (
                        individual_i + 1,
                        individual,
                    )
                    individual_tree = family_tree[
                        individual_label] = OrderedDict()
                    for sample_i, sample in enumerate(
                            Sample.objects.filter(individual=individual)):
                        sample_label = "S%s sample: %s" % (
                            sample_i + 1,
                            "{sample_type}, elasticsearch_index: {elasticsearch_index} {dataset_file_path}"
                            .format(**sample.json()),
                        )
                        individual_tree[sample_label] = OrderedDict()

        #pprint(projects_tree)
        print(
            apply(
                asciitree.LeftAligned(draw=asciitree.BoxStyle(
                    gfx=asciitree.drawing.BOX_HEAVY, horiz_len=1)),
                [{
                    'Projects:': projects_tree
                }]))
Example #3
0
def make_skilltree(session, char, fmt=lambda sk: '{0.id}'.format(sk)):
    tr = asciitree.LeftAligned(
        draw = asciitree.BoxStyle(
            gfx = asciitree.drawing.BOX_HEAVY,
            horiz_len=0,
            indent=0,
            label_space=1
        )
    )
    space_char = '\u2001' # '\u2000' for BOX_LIGHT

    root_skills = session.query(Skill).filter(Skill.char == char, Skill.parent == None).order_by(Skill.created)
    d = OrderedDict()
    for skill in root_skills:
        d[fmt(skill)] = get_tree(session, skill, fmt)
    msg = tr(d)

    return '\n'.join(a.replace(' ', space_char) + b for a,b in (tree_re.match(l).groups() for l in msg.split('\n')))
Example #4
0
File: data.py Project: emtpb/dsch
The data nodes are also responsible for storing the data. Since dsch is built
to support multiple storage backends, there are specific data node classes
implementing the respective functionality. The classes in this module provide
common functionality and are intended to be used as base classes.
Different backends are implemented in the :mod:`dsch.backends` package.
"""
import datetime
import importlib

import asciitree

from . import exceptions

draw_tree = asciitree.LeftAligned(
    draw=asciitree.BoxStyle(gfx=asciitree.drawing.BOX_LIGHT, horiz_len=1)
)


class ItemNode:
    """Generic data item node.

    :class:`ItemNode` is the base class for data nodes, providing common
    functionality and the common interface. Subclasses may add functionality
    depending on the node type and backend (e.g. compression settings).

    Note that this is only the base class for item nodes, i.e. nodes that
    directly hold data. Collection nodes, i.e. :class:`Compilation` and
    :class:`List` are *not* based on this class.

    Attributes:
Example #5
0
def _tree_as_string(d):
    if len(d) != 1:
        d = dict(ROOT=d)
    box_tr = asciitree.LeftAligned(
        draw=asciitree.BoxStyle(gfx=BOX_DOUBLE, horiz_len=1))(d)
    return box_tr
Example #6
0
    def handle(self, *args, **options):

        if not options["project_id"]:
            projects = Project.objects.all()
        else:
            projects = []
            for project_id in options["project_id"]:
                matched_projects = Project.objects.filter(
                    Q(guid__icontains=project_id)
                    | Q(name__icontains=project_id))
                if not matched_projects:
                    logger.warn("No matching project found for keyword: %s" %
                                project_id)
                projects.extend(matched_projects)

        try:
            import pip
            pip.main(["install", "asciitree"])
            import asciitree
            from asciitree.util import *
            from asciitree.drawing import *
        except ImportError as e:
            logger.error(e)
            return

        projects_tree = OrderedDict()
        for project_i, project in enumerate(projects):
            project_label = "P%s project: %s" % (
                project_i + 1,
                project,
            )
            project_tree = projects_tree[project_label] = OrderedDict()
            for family_i, family in enumerate(
                    Family.objects.filter(project=project)):
                family_label = "F%s family: %s" % (
                    family_i + 1,
                    family,
                )
                family_tree = project_tree[family_label] = OrderedDict()
                for individual_i, individual in enumerate(
                        Individual.objects.filter(family=family)):
                    individual_label = "I%s individual: %s" % (
                        individual_i + 1,
                        individual,
                    )
                    individual_tree = family_tree[
                        individual_label] = OrderedDict()
                    for sample_i, sample in enumerate(
                            Sample.objects.filter(individual=individual)):
                        sample_label = "sample: %s" % (sample, )
                        datasets = Dataset.objects.filter(samples=sample)
                        sample_label += "   - dataset(s): " + str([
                            "%s: %s" % (d, d.source_file_path)
                            for d in datasets
                        ])
                        individual_tree[sample_label] = OrderedDict()

        pprint(projects_tree)
        print(
            apply(
                asciitree.LeftAligned(draw=asciitree.BoxStyle(
                    gfx=asciitree.drawing.BOX_HEAVY, horiz_len=1)),
                [{
                    'Projects:': projects_tree
                }]))
    def run(self):
        """ Runs the CLI login flow.

        Returns:
            None
        """
        fold_parents, org_parents = self._get_projects()

        def recurse_and_attach(parent, tree):
            """ Given a tree (a dict) and a parent node, traverses depth-first
              in the org hierarchy, attaching children to parents in tree.

            Args:
                parent: parent node as str
                tree: mapping to use to construct the org hierarchy

            Returns:
                None
            """
            result = self._crm_v2.folders().list(parent=parent).execute()
            if 'folders' in result:
                for folder in result['folders']:
                    f_str = '{} {} ({})'.format(_FOLDER, folder["displayName"],
                                                folder["name"].split("/")[1])
                    tree[f_str] = {}

                    _id = folder["name"].split('/')[1]
                    if _id in fold_parents:
                        for project in fold_parents[_id]:
                            proj_name = '{} {}'.format(_PROJ,
                                                       project["projectId"])
                            tree[f_str][proj_name] = {}

                    recurse_and_attach(folder['name'], tree[f_str])

        t = {}

        # If an org flag is set, finds the org by name or ID, walks the org
        # hierarchy starting at the organization, uses the tree and attaches
        # folders and projects recursively
        if self.organization:
            resp = self._crm_v1.organizations().search(body={}).execute()
            orgs = resp.get('organizations')

            if not orgs:
                raise SystemExit('No organizations found')

            org_id = [
                org['name'].split('/')[1] for org in orgs
                if self.organization in org['displayName']
                or self.organization in org['name']
            ][0]

            if self.use_org_id:
                filter_func = functools.partial(resource_filter,
                                                self.organization,
                                                field='name')
            else:
                filter_func = functools.partial(resource_filter,
                                                self.organization)

            try:
                org = next(filter(filter_func, orgs))
            except StopIteration:
                raise SystemExit('Could not find organization {}'
                                 ''.format(self.organization))

            org_name = '{} {} ({})'.format(_ORG, org["displayName"],
                                           org["name"].split('/')[1])
            t[org_name] = {}

            recurse_and_attach(org['name'], t[org_name])

            for project in org_parents.get(org_id, []):
                proj_name = '{} {}'.format(_PROJ, project["projectId"])
                t[org_name][proj_name] = {}

        # If the folder flag is set, walks the organization hierarchy starting
        # at a folder node and attaches folders and projects.
        if self.folder:
            folder = self._crm_v2.folders().get(
                name='folders/{}'.format(self.folder)).execute()
            fold_name = '{} {} ({})'.format(_FOLDER, folder["displayName"],
                                            self.folder)
            t[fold_name] = {}

            recurse_and_attach(folder['name'], t[fold_name])

            for project in fold_parents.get(self.folder, []):
                proj_name = '{} {}'.format(_PROJ, project["projectId"])
                t[fold_name][proj_name] = {}

        tr = asciitree.LeftAligned(draw=asciitree.BoxStyle())
        print(tr(t))