Skip to content

Commit

Permalink
adding citelang badge
Browse files Browse the repository at this point in the history
and freaking out because boulder is on fire but as long I have internet I can
pretend nothing is going on and keep programming

Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Mar 27, 2022
1 parent 94839ad commit 217d9cb
Show file tree
Hide file tree
Showing 16 changed files with 271 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
pyflakes citelang/*.py
pyflakes citelang/client
pyflakes citelang/main/*.py
pyflakes citelang/main/badge/*.py
pyflakes citelang/utils/fileio.py
pyflakes citelang/utils/terminal.py
pyflakes citelang/main/graph/base.py
Expand Down Expand Up @@ -61,7 +62,7 @@ jobs:
run: |
export PATH="/usr/share/miniconda/bin:$PATH"
source activate testing
pip install -e .
pip install -e .[all]
pip install bs4
cd citelang/tests
/bin/bash test_client.sh
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
uses: actions/checkout@v2

- name: Install CiteLang
run: pip install -e .
run: pip install -e .[all]

- name: Test CiteLang Released
uses: vsoch/citelang/action/gen@main
Expand All @@ -34,3 +34,11 @@ jobs:

- name: View generated file
run: cat citelang.md

- name: Test citelang badge
uses: ./action/badge
env:
CITELANG_LIBRARIES_KEY: ${{ secrets.CITELANG_LIBRARIES_KEY }}
with:
package: citelang
manager: pypi
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ that when you publish your software, you should publish it to an appropriate pac
⭐️ [Documentation](https://vsoch.github.io/citelang) ⭐️


![docs/assets/img/pypi-citelang.png](docs/assets/img/pypi-citelang.png)


## TODO

- citelang should be able to parse requirements.txt or go.sum, etc.
- add graphic summary (need static version to go into markdown)
- colors should be meaningful
- finish GitHub actions (render, docs, remember to mention to use token, etc.)
- add gha to run here!

## Contributors
Expand Down
38 changes: 38 additions & 0 deletions action/badge/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: "citelang badge action"
description: "Generate a citelang badge (png) for your package"
inputs:
manager:
description: the name of the package manager
required: true
package:
description: the name of the package
required: true
outfile:
description: the name of the outputfile (png)
required: false
default: ""
args:
description: additional arguments / parameters for citelang.
required: false
default: ""

runs:
using: "composite"
steps:

- name: Install CiteLang
shell: bash
run: which citelang || pip install citelang

- name: Generate CiteLang
env:
manager: ${{ inputs.manager }}
package: ${{ inputs.package }}
outfile: ${{ inputs.outfile }}
args: ${{ inputs.args }}
run: |
if [[ "${outfile}" == "" ]]; then
outfile="${manager}-${package}.png"
fi
citelang badge ${manager} ${package} --outfile ${outfile} ${args}
shell: bash
18 changes: 10 additions & 8 deletions citelang/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ def get_parser():

# Cache control
cache = subparsers.add_parser("cache", description="cache control")
cache.add_argument(
"--force",
help="do not ask for confirmation",
default=False,
action="store_true",
)
cache.add_argument(
"--clear", help="clear the cache", default=False, action="store_true"
)
Expand All @@ -101,10 +95,18 @@ def get_parser():
"--template",
"-t",
help="Template to use for badge",
choices=["treemap", "sunburst"],
default="sunburst",
choices=["treemap", "sunburst", "static"],
default="static",
)

for command in [cache, badge]:
command.add_argument(
"--force",
help="do not ask for confirmation",
default=False,
action="store_true",
)

graph = subparsers.add_parser(
"graph", description="generate a graph for some package dependencies."
)
Expand Down
9 changes: 8 additions & 1 deletion citelang/client/badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__license__ = "MPL 2.0"

from citelang.main import Client
from citelang.logger import logger
import tempfile
import os

Expand All @@ -21,6 +22,12 @@ def main(args, parser, extra, subparser):
template=args.template,
)

if not args.outfile:
if not args.outfile and args.template == "static":
args.outfile = os.path.join("%s-%s.png" % (args.package[0], args.package[1]))

elif not args.outfile:
args.outfile = os.path.join(tempfile.mkdtemp(), "index.html")

if os.path.exists(args.outfile) and not args.force:
logger.exit(f"{args.outfile} exists, add --force to overwrite.")
result.save(args.outfile)
86 changes: 86 additions & 0 deletions citelang/main/badge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
__author__ = "Vanessa Sochat"
__copyright__ = "Copyright 2022, Vanessa Sochat"
__license__ = "MPL 2.0"

from citelang.logger import logger
import citelang.main.colors as colors


try:
import plotly.graph_objs as go
except ImportError:
logger.exit(
"Plotly and dependencies are needed to generate this badge. pip install citelang[badge]."
)


def generate(data, outfile=None):
"""
Generate a static badge
"""
labels, parents, values = unwrap_tree(data)
ids, parentids = get_ids(labels, parents)

# colors for subtrees
colorset = colors.get_color_range(N=data["levels"] - 1)

# add color for root (white)
colorset = ["#ffffff"] + [str(x) for x in colorset]

trace = go.Sunburst(
ids=ids,
labels=labels,
parents=parentids,
values=values,
marker={"colors": colorset, "line": {"width": 1.5}},
)

layout = go.Layout(
title={"text": "Package " + data["name"], "x": 0.5},
font={"size": 12},
showlegend=False,
autosize=False,
height=750,
xaxis={"visible": False},
yaxis={"visible": False},
hovermode="closest",
)

fig = go.Figure(data=[trace], layout=layout)
if outfile:
fig.write_image(outfile)
return fig


def get_ids(labels, parents):
"""
Given labels and parent node ids, generate labels for the nodes.
"""
if len(labels) != len(parents):
logger.exit(
"The list of labels should have the same length like the list of parents"
)
ids = [str(id) for id in range(len(labels))]

# associate to each label the corresponding id
dlabels = {label: idx for label, idx in zip(labels, ids)}

# empty is for the root node
parentids = [""] + [dlabels[label] for label in parents[1:]]
return ids, parentids


def unwrap_tree(tree_dict, labels=None, parents=None, values=None):
"""
Given a typical structured data with nodes and children, unwrap into flat lists.
"""
labels = labels or []
parents = parents or [""]
values = values or []
labels.append(tree_dict["name"])
values.append(tree_dict["weight"])
last_node = tree_dict["name"]
for child in tree_dict.get("children"):
parents.append(last_node)
unwrap_tree(child, labels, parents, values)
return labels, parents, values
4 changes: 3 additions & 1 deletion citelang/main/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ def graph(self, fmt=None, *args, **kwargs):
root = self._graph(*args, **kwargs)
return results.Graph(root).graph(fmt=fmt)

def badge(self, template="sunburst", *args, **kwargs):
def badge(self, template="static", *args, **kwargs):
"""
Generate a badge for a package
"""
root = self._graph(*args, **kwargs)
if template == "treemap":
return results.Treemap(root)
elif template == "sunburst":
return results.InteractiveBadge(root)
return results.Badge(root)

def _graph(
Expand Down
35 changes: 31 additions & 4 deletions citelang/main/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,11 @@ def parse_data(self):
"color": color_lookup[self.result.name],
}

def add(data, children, total):
def add(data, children, total, levels=1):
total += data.credit
if data.children:
child_size = 100 / len(data.children)
levels += 1
for child in data.children:
node = {
"name": child.name,
Expand All @@ -243,13 +244,15 @@ def add(data, children, total):
"children": [],
"color": color_lookup[child.name],
}
total = add(child, node["children"], total)
total, levels = add(child, node["children"], total, levels)
children.append(node)
return total
return total, levels

total = add(self.result, data["children"], 0)
# Get total and number of levels in the tree
total, levels = add(self.result, data["children"], 0)
self.data = data
self.data["total"] = total
self.data["levels"] = levels

def print_result(self):
"""
Expand Down Expand Up @@ -321,6 +324,30 @@ def save(self, outfile):


class Badge(Tree):
"""
This is an static badge that uses plotly.
"""

def print_result(self):
pass

def save(self, outfile):
"""
Save to output file
"""
import citelang.main.badge as badge

logger.info("Saving to %s..." % outfile)

# Set root weight to 0 for correct render
self.data["weight"] = 0

# Add an extra parent to the data (root) so the one child is requests
badge.generate(self.data, outfile)
logger.info("Result saved to %s" % outfile)


class InteractiveBadge(Tree):
"""
A badge uses tree data with d3 for an interactive visualization
"""
Expand Down
19 changes: 10 additions & 9 deletions citelang/tests/test_client.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,21 @@ runTest 0 $output citelang --settings-file $settings credit pypi requests --fmt
runTest 0 $output citelang --settings-file $settings credit pypi requests --fmt gexf
runTest 0 $output citelang --settings-file $settings credit pypi requests --fmt dot

echo
echo "#### Testing render "
runTest 0 $output citelang --settings-file $settings render ../../examples/paper.md
runTest 0 $output citelang --settings-file $settings render ../../examples/paper.md --outfile paper.md
cat paper.md
# echo
# echo "#### Testing render "
# runTest 0 $output citelang --settings-file $settings render ../../examples/paper.md
# runTest 0 $output citelang --settings-file $settings render ../../examples/paper.md --outfile paper.md
# cat paper.md

echo
echo "#### Testing gen "
runTest 0 $output citelang --settings-file $settings gen pypi requests
# echo
# echo "#### Testing gen "
# runTest 0 $output citelang --settings-file $settings gen pypi requests

echo
echo "#### Testing badge "
runTest 0 $output citelang --settings-file $settings badge pypi requests
runTest 0 $output citelang --settings-file $settings badge --template treemap pypi requests
# runTest 0 $output citelang --settings-file $settings badge --template sunburst pypi requests
# runTest 0 $output citelang --settings-file $settings badge --template treemap pypi requests

echo
echo "#### Testing cache "
Expand Down
7 changes: 6 additions & 1 deletion citelang/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
("colour", {"min_version": None}),
)

BADGE_REQUIRES = (
("plotly", {"min_version": None}),
("kaleido", {"min_version": None}),
)

TESTS_REQUIRES = (("pytest", {"min_version": "4.6.2"}),)

################################################################################
# Submodule Requirements (versions that include database)

INSTALL_REQUIRES_ALL = INSTALL_REQUIRES + TESTS_REQUIRES
INSTALL_REQUIRES_ALL = INSTALL_REQUIRES + TESTS_REQUIRES + BADGE_REQUIRES
Binary file added docs/assets/img/pypi-citelang.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/getting_started/img/badge-pypi-requests.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 217d9cb

Please sign in to comment.