Commit ce493dd2 authored by Louis's avatar Louis
Browse files

Web server takes arguments from a yaml file, or from `pkg=`, `cols=`, `user=` GET arguments.

parent 8bc43782
* pypimonitor 0.2.0 (unreleased)
* Takes arguments (which packages to monitor) from a yaml file, or from `pkg=`, `cols=`, `user=`.
* Now has two modes: command line and web server.
* Name changed from "pypistats" to "pypimonitor".
* Use chartsjs.org to draw line charts (was morrisjs before).
......
- [ ] `python -m pypimonitor` write an html file to stdout. It taks as argument a yaml file (see below)
- [x] `python -m pypimonitor` write an html file to stdout. It taks as argument a yaml file (see below)
- [x] `python -m pypimonitor.http --port PORT DIR1 DIR2` launch a web server
- [ ] - Connection to http://localhost:PORT/foo: render DIR1/foo.yaml as an html page (or DIR2/foo.yaml if not found)
- [ ] - Connection to http://localhost:PORT: same action as now, using pkg=, usr=, and column= arguments. The array is limited to some pypi-only cells
- [x] - Connection to http://localhost:PORT/foo: render DIR1/foo.yaml as an html page (or DIR2/foo.yaml if not found)
- [x] - Connection to http://localhost:PORT: same action as now, using pkg=, usr=, and column= arguments. The array is limited to some pypi-only cells
- [x] Change default port
- [x] Use chart.js (see TODO/chart.html)
- [x] Use it
......
from urllib.parse import urlparse, parse_qs
import http.server
import argparse
import pkg_resources
import os
import logging
import pypimonitor
......@@ -9,29 +11,41 @@ LOGGER = logging.getLogger(pypimonitor.__name__)
DEFAULT_PORT = 8080
DEFAULT_HOST = ""
class HttpHandler(http.server.BaseHTTPRequestHandler):
def render(self, packages, columns, users):
return bytes(pypimonitor.Renderer.from_args(
packages=packages,
columns=columns,
users=users,
).render(), "utf-8")
class HttpHandler(http.server.SimpleHTTPRequestHandler):
yaml_dir = []
def do_HEAD(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
def _find_yaml(self, path):
yaml = "{}.yaml".format(path)
for directory in self.yaml_dir:
if os.path.exists(os.path.join(directory, yaml)):
return os.path.join(directory, yaml)
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)
self.wfile.write(self.render(
packages=querydict.get('pkg'),
columns=querydict.get('col', ["homepage", "pypi_version", "pypi_mdownload", "pypi_wdownload", "pypi_ddownload"]),
users=querydict.get('user'),
))
path = urlparse(self.path).path.strip("/")
if path in ['', 'index.html']:
querydict = parse_qs(urlparse(self.path).query)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes(pypimonitor.Renderer.from_args(
packages=querydict.get('pkg'),
columns=querydict.get('col', ["homepage", "pypi_version", "pypi_mdownload", "pypi_wdownload", "pypi_ddownload"]),
users=querydict.get('user'),
).render(), "utf-8"))
elif self._find_yaml(path):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes(pypimonitor.Renderer.from_yaml(self._find_yaml(path)).render(), "utf-8"))
else:
super().do_GET()
def _type_address(text):
partition = text.partition(":")
......@@ -44,6 +58,9 @@ def _type_address(text):
return partition[0], DEFAULT_PORT
return partition[0], DEFAULT_PORT
def _type_abspath(text):
return os.path.abspath(text)
def commandline_parser():
"""Return a command line parser."""
......@@ -67,10 +84,20 @@ def commandline_parser():
default=[(DEFAULT_HOST, DEFAULT_PORT)],
)
parser.add_argument(
'dir',
help='Directory containing yaml files to serve.',
nargs='?',
type=_type_abspath,
default=".",
)
return parser
if __name__ == "__main__":
arguments = commandline_parser().parse_args()
HttpHandler.yaml_dir = [arguments.dir]
os.chdir(os.path.join(pkg_resources.resource_filename(pypimonitor.__name__, 'data'), 'static'))
server = http.server.HTTPServer(arguments.address[0], HttpHandler)
try:
server.serve_forever()
......
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