Commit 0b7766cb authored by Louis's avatar Louis
Browse files

Badges are displayed

parent 91e04fa0
- badges: à partir de GET, avec defaut
- if no pkg nor user: random 10 packages
- Set chart colors
- Add array of badges
......@@ -2,14 +2,13 @@
from pkgtools import pypi
import collections
import urllib
import datetime
import itertools
import logging
import jinja2
import pkg_resources
import pprint
import slumber
import sys
import urllib
try:
import xmlrpclib
......@@ -17,12 +16,16 @@ except ImportError:
import xmlrpc.client as xmlrpclib
_client = xmlrpclib.ServerProxy("http://pypi.python.org/pypi")
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.INFO)
class PackageList:
def __init__(self, user=None, pkg=None):
self.packages = {name: Package(name) for name in sorted(self._get_packages(user, pkg))}
self.packages = {}
for name in sorted(self._get_packages(user, pkg)):
try:
self.packages[name] = Package(name)
except ValueError:
sys.stderr.write("Ignoring package '{}' with no releases.\n".format(name))
continue
@staticmethod
def _get_packages(users, packages):
......@@ -32,20 +35,12 @@ class PackageList:
try:
yield package
except urllib.error.HTTPError as error:
LOGGER.warning("Ignoring user '{}': {}.".format(
sys.stderr.write("Ignoring user '{}': {}.".format(
user,
str(error),
))
if packages is not None:
yield from packages
#for pkg in packages:
# try:
# yield pypi.real_name(pkg)
# except urllib.error.HTTPError as error:
# LOGGER.warning("Ignoring package '{}': {}.".format(
# pkg,
# str(error),
# ))
def __iter__(self):
yield from self.packages
......@@ -93,6 +88,10 @@ def DateTime2datetime(date):
class Package:
docs_api = {
'readthedocs': slumber.API(base_url="http://readthedocs.org/api/v1/"),
}
def __init__(self, name):
sys.stderr.write("Retrieving information about pypi package '{}'...\n".format(name))
self.name = name
......@@ -105,6 +104,8 @@ class Package:
self.releases[date] = {}
self.releases[date]['downloads'] = sum((url['downloads'] for url in release_urls))
self.releases[date]['version'] = release
if not self.releases:
raise ValueError("Package '{}' does not exist or haven't any release.".format(name))
downloads = 0
for release in sorted(self.releases):
......@@ -121,6 +122,21 @@ class Package:
last = sorted(self.releases.keys())[-1]
return self.releases[last]['downloads'] + self.releases[last]['previous']
def get_documentation(self):
# Readthedocs
if self.docs_api['readthedocs'].project.get(slug=self.name.lower())['objects']:
return (
'readthedocs',
None,
)
# Pypi docs
if self.data['docs_url'] is not None:
return ("pypi", self.name)
# No documentation
return (None, {})
################################################################################
# OLD
################################################################################
......
......@@ -34,7 +34,7 @@ def _get_environment():
class HttpHandler(http.server.BaseHTTPRequestHandler):
def render(self, source, **kwargs):
template = _get_environment().get_template( source )
template = _get_environment().get_template(source)
return bytes(template.render(kwargs), "utf-8")
def do_HEAD(self):
......@@ -54,6 +54,7 @@ class HttpHandler(http.server.BaseHTTPRequestHandler):
user=querydict.get('user'),
pkg=querydict.get('pkg'),
),
badges = ["homepage", "pypi_version", "pypi_mdownload", "pypi_wdownload", "pypi_ddownload", "docs"],
))
if __name__ == "__main__":
......
{%- macro render(badge, package) -%}
{%- from "badges/%s.html" % badge import render -%}
{{- render(package) -}}
{%- endmacro -%}
{%- macro render(package) -%}
{%- if package is none -%}
Docs
{%- else -%}
{% set type, args = package.get_documentation() %}
{% if type is none %}
<!-- No documentation found for '{{ package.data.name }}'. -->
{% elif type == "readthedocs" %}
<a href="//{{ package.name }}.readthedocs.org">
<img src="//readthedocs.org/projects/{{ package.name.lower() }}/badge" alt="Documentation">
</a>
{% else %}
<a href="{{ package.data.docs_url }}">
<img src="//img.shields.io/badge/docs-latest-brightgreen.svg" alt="Documentation">
</a>
{% endif %}
{%- endif -%}
{%- endmacro -%}
{%- macro render(package) -%}
{%- if package is none -%}
Home
{%- else -%}
<a href="{{ package.data.home_page }}">{{ package.data.name }}</a>
{%- endif -%}
{%- endmacro -%}
{%- macro render(package, shortcut, alt, title) -%}
{%- if package is none -%}
{{ title }}
{%- else -%}
<a href="{{ package.data.package_url }}">
<img src="https://img.shields.io/pypi/{{ shortcut }}/{{ package.name }}.svg" alt="{{ alt }}">
</a>
{%- endif -%}
{%- endmacro -%}
{%- macro render(package) -%}
{% from "badges/pypi.html" import render as render_pypi %}
{{ render_pypi(package, "dd", "Daily download", "Daily") }}
{%- endmacro -%}
{%- macro render(package) -%}
{% from "badges/pypi.html" import render as render_pypi %}
{{ render_pypi(package, "dm", "Monthly download", "Monthly") }}
{%- endmacro -%}
{%- macro render(package) -%}
{% from "badges/pypi.html" import render as render_pypi %}
{{ render_pypi(package, "v", "Pypi version", "Version") }}
{%- endmacro -%}
{%- macro render(package) -%}
{% from "badges/pypi.html" import render as render_pypi %}
{{ render_pypi(package, "dw", "Weekly download", "Weekly") }}
{%- endmacro -%}
......@@ -13,7 +13,25 @@
<body>
{{ linecharts.plot(packages, none) }}
TODO badges
{% from "badge.html" import render as render_badge %}
<table>
<thead><tr>
{% for badge in badges -%}
<td>{{ render_badge(badge, none) }}</td>
{%- endfor %}
</tr></thead>
<tbody>
{% for package in packages|sort -%}
<tr>
{% for badge in badges -%}
<td>
{{ render_badge(badge, packages[package]) }}
</td>
{%- endfor %}
</tr>
{%- endfor %}
</tbody>
</table>
{% for package in packages|sort %}
{{ linecharts.plot(packages, package) }}
......
#!/bin/bash
(sleep 3 && wget --quiet -O index.html "http://localhost:8081/?user=spalax&pkg=patacrep") &
#(sleep 3 && wget --quiet -O index.html "http://localhost:8081/?user=spalax&pkg=patacrep") &
#(sleep 3 && wget --quiet -O index.html "http://localhost:8081/?pkg=sphinxcontrib-proof") &
python -m pipstats
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment