Commit cf9d6b84 authored by Louis's avatar Louis
Browse files

[WIP] New version, with caching information, templates, and a web server

parent bb1e42ba
#!/usr/bin/env python3
from pkgtools import pypi
import collections
import urllib
import datetime
import itertools
import logging
import jinja2
import pkg_resources
import pprint
import sys
......@@ -10,17 +16,63 @@ try:
except ImportError:
import xmlrpc.client as xmlrpclib
USER = "spalax"
_client = xmlrpclib.ServerProxy("http://pypi.python.org/pypi")
LOGGER = logging.getLogger(__name__)
class PackageList:
def __init__(self, user=None, pkg=None):
if user is None:
self.user = []
else:
self.user = user
if pkg is None:
self.pkg = []
else:
self.pkg = pkg
self.packages = [Package(name) for name in self._get_packages()]
def _get_packages(self):
for user in self.user:
for _, package in _client.user_packages(user):
try:
yield package
except urllib.error.HTTPError as error:
LOGGER.warning("Ignoring user '{}': {}.".format(
user,
str(error),
))
for pkg in self.pkg:
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
class Package:
def __init__(self, name):
self.name = name
print("TODO", name)
################################################################################
# OLD
################################################################################
client = xmlrpclib.ServerProxy("http://pypi.python.org/pypi")
USER = "spalax"
def process_package(package):
data = {}
sys.stderr.write("Processing {}...\n".format(package))
for release in client.package_releases(package, True):
for release in _client.package_releases(package, True):
sys.stderr.write("Processing version {}...".format(release))
data[release] = {'downloads': 0}
for urls in client.release_urls(package, release):
for urls in _client.release_urls(package, release):
data[release]['downloads'] += urls['downloads']
data[release]['upload_time'] = urls['upload_time']
sys.stderr.write(" Done\n")
......@@ -154,16 +206,17 @@ def get_compiled(packages):
HTML_GRAPHDIV.format(counter=count),
)
packages = [package for role, package in client.user_packages(USER)] + ["patacrep"]
if __name__ == "__main__":
packages = [package for role, package in _client.user_packages(USER)] + ["patacrep"]
graphs = []
graphs.append(get_compiled(packages))
for package in packages:
graphs.append(get_compiled([package]))
print(HTML_BEGIN)
for _, div in graphs:
print(div)
for script, _ in graphs:
print(script)
print(HTML_END)
graphs = []
graphs.append(get_compiled(packages))
for package in packages:
graphs.append(get_compiled([package]))
print(HTML_BEGIN)
for _, div in graphs:
print(div)
for script, _ in graphs:
print(script)
print(HTML_END)
import http.server
from urllib.parse import urlparse, parse_qs
import os
import pkg_resources
import jinja2
from pipstats import PackageList
TEMPLATEDIR = os.path.join(
pkg_resources.resource_filename(__name__, 'data'),
'templates',
)
_template_env = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATEDIR))
class HttpHandler(http.server.BaseHTTPRequestHandler):
def render(self, source, **kwargs):
template = _template_env.get_template( source )
return bytes(template.render(kwargs), "utf-8")
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
querydict = parse_qs(urlparse(self.path).query)
print(querydict)
self.wfile.write(self.render(
'index.html',
s=self,
packages=PackageList(
user=querydict.get('user'),
pkg=querydict.get('pkg'),
),
))
if __name__ == "__main__":
http.server.HTTPServer(('', 8081), HttpHandler).serve_forever()
{% import 'linecharts.html' as linecharts %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pypi statistics</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
</head>
<body>
{{ linecharts.plot(packages) }}
TODO badges
{% for package in packages %}
{{ linecharts.plot([package]) }}
{% endfor %}
</body>
</html>
{% macro plot(packages) -%}
{% set id = counter() %}
<div id="linechart{{ id }}" style="height: 250px;"></div>
<script>
new Morris.Line({
element: "linechart{{ id }}",
data: [
{{ chart.yvalues(packages) }}
],
xkey: 'date',
ykeys: [{{ chart.ykeys(packages) }}],
labels: [{{ chart.ylabels(packages) }}]
});
</script>
{%- endmacro %}
Markdown is supported
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