Commit bb1e42ba authored by Louis's avatar Louis
Browse files

Suppression des fins de ligne Windows

parent 07d1acce
#!/usr/bin/env python3
import collections
import datetime
import pprint
import sys
try:
import xmlrpclib
except ImportError:
import xmlrpc.client as xmlrpclib
USER = "spalax"
client = xmlrpclib.ServerProxy("http://pypi.python.org/pypi")
def process_package(package):
data = {}
sys.stderr.write("Processing {}...\n".format(package))
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):
data[release]['downloads'] += urls['downloads']
data[release]['upload_time'] = urls['upload_time']
sys.stderr.write(" Done\n")
return data
def get_release_date(data):
try:
return data[1]['upload_time']
except KeyError:
# Will be ignored later
return datetime.datetime.fromtimestamp(0)
def shift(data):
date = [{'downloads': 0}]
for release, _ in sorted(data.items(), key=get_release_date):
try:
datetuple = data[release]['upload_time'].timetuple()
except KeyError:
continue
date[-1].update({
'date': datetime.date(datetuple.tm_year, datetuple.tm_mon, datetuple.tm_mday),
'release': release,
})
date.append({
'downloads': data[release]['downloads'] + date[-1]['downloads'],
})
date[-1].update({
'date': datetime.date.today(),
'release': "",
})
return date
def bydate(package, data):
dateddata = collections.defaultdict(list)
for release in data:
dateddata[release['date']].append({
'downloads': release['downloads'],
'release': release['release'],
'package': package,
})
return dateddata
def compile_dates(package_data):
data = collections.defaultdict(list)
for package in package_data:
for date, release in package.items():
data[date].extend(release)
return data
HTML_BEGIN = """
<!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>
"""
HTML_GRAPHDIV = """
<div id="pystats{counter}" style="height: 250px;"></div>
"""
HTML_SCRIPT = """
<script>
new Morris.Line({{
element: 'pystats{counter}',
data: [
{yvalues}
],
xkey: 'date',
ykeys: [{ykeys}],
labels: [{ykeys}]
}});
</script>
"""
HTML_END = """
</body>
</html>
"""
def make_id(string):
new = ""
for char in string:
if char.isidentifier():
new += char
return new
def print_y(data):
for date, releaselist in data.items():
values = ", ".join((
"{}: {}".format(make_id(release['package']), release['downloads'])
for release in releaselist
))
yield "{{ date: '{}', {}}}".format(str(date), values)
def print_ykeys(data):
packages = set()
for releaselist in data.values():
for release in releaselist:
packages.add(make_id(release['package']))
for package in packages:
yield "'{}'".format(package)
class Counter:
def __init__(self):
self.total = 0
def count(self):
self.total += 1
return self.total
counter = Counter()
def get_compiled(packages):
compiled = compile_dates((
bydate(package, shift(process_package(package)))
for package in packages
))
count = counter.count()
return (
HTML_SCRIPT.format(
ykeys=", ".join(print_ykeys(compiled)),
yvalues=",\n".join(print_y(compiled)),
counter= count,
),
HTML_GRAPHDIV.format(counter=count),
)
packages = [package for role, package in client.user_packages(USER)]
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)
#!/usr/bin/env python3
import collections
import datetime
import pprint
import sys
try:
import xmlrpclib
except ImportError:
import xmlrpc.client as xmlrpclib
USER = "spalax"
client = xmlrpclib.ServerProxy("http://pypi.python.org/pypi")
def process_package(package):
data = {}
sys.stderr.write("Processing {}...\n".format(package))
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):
data[release]['downloads'] += urls['downloads']
data[release]['upload_time'] = urls['upload_time']
sys.stderr.write(" Done\n")
return data
def get_release_date(data):
try:
return data[1]['upload_time']
except KeyError:
# Will be ignored later
return datetime.datetime.fromtimestamp(0)
def shift(data):
date = [{'downloads': 0}]
for release, _ in sorted(data.items(), key=get_release_date):
try:
datetuple = data[release]['upload_time'].timetuple()
except KeyError:
continue
date[-1].update({
'date': datetime.date(datetuple.tm_year, datetuple.tm_mon, datetuple.tm_mday),
'release': release,
})
date.append({
'downloads': data[release]['downloads'] + date[-1]['downloads'],
})
date[-1].update({
'date': datetime.date.today(),
'release': "",
})
return date
def bydate(package, data):
dateddata = collections.defaultdict(list)
for release in data:
dateddata[release['date']].append({
'downloads': release['downloads'],
'release': release['release'],
'package': package,
})
return dateddata
def compile_dates(package_data):
data = collections.defaultdict(list)
for package in package_data:
for date, release in package.items():
data[date].extend(release)
return data
HTML_BEGIN = """
<!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>
"""
HTML_GRAPHDIV = """
<div id="pystats{counter}" style="height: 250px;"></div>
"""
HTML_SCRIPT = """
<script>
new Morris.Line({{
element: 'pystats{counter}',
data: [
{yvalues}
],
xkey: 'date',
ykeys: [{ykeys}],
labels: [{ykeys}]
}});
</script>
"""
HTML_END = """
</body>
</html>
"""
def make_id(string):
new = ""
for char in string:
if char.isidentifier():
new += char
return new
def print_y(data):
for date, releaselist in data.items():
values = ", ".join((
"{}: {}".format(make_id(release['package']), release['downloads'])
for release in releaselist
))
yield "{{ date: '{}', {}}}".format(str(date), values)
def print_ykeys(data):
packages = set()
for releaselist in data.values():
for release in releaselist:
packages.add(make_id(release['package']))
for package in packages:
yield "'{}'".format(package)
class Counter:
def __init__(self):
self.total = 0
def count(self):
self.total += 1
return self.total
counter = Counter()
def get_compiled(packages):
compiled = compile_dates((
bydate(package, shift(process_package(package)))
for package in packages
))
count = counter.count()
return (
HTML_SCRIPT.format(
ykeys=", ".join(print_ykeys(compiled)),
yvalues=",\n".join(print_y(compiled)),
counter= count,
),
HTML_GRAPHDIV.format(counter=count),
)
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)
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