Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: modules/adblockplus/files/mimeo.py

Issue 29504594: #2687 - Include mimeo python module (Closed)
Patch Set: Created Aug. 3, 2017, 5:50 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: modules/adblockplus/files/mimeo.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/modules/adblockplus/files/mimeo.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
Vasily Kuznetsov 2017/08/04 18:01:46 Yay! Python 3 FTW!
f.lopez 2017/08/07 03:10:14 Done.
+
+import argparse
+import logging
+
+from http.server import BaseHTTPRequestHandler, HTTPServer
+from string import Template
+
+punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
Vasily Kuznetsov 2017/08/04 18:01:46 These things look like constants, they should be n
f.lopez 2017/08/04 18:58:51 Acknowledged.
f.lopez 2017/08/07 03:10:15 Done.
f.lopez 2017/08/07 03:10:15 Done.
+proxy_token = 'http_'
+
+class Handler(BaseHTTPRequestHandler):
Vasily Kuznetsov 2017/08/04 18:01:46 PEP8 recommends to surround top level classes and
f.lopez 2017/08/04 18:58:51 Acknowledged.
f.lopez 2017/08/07 03:10:15 Done.
f.lopez 2017/08/07 03:10:15 Done.
+ def set_values(self, values, log_format):
Vasily Kuznetsov 2017/08/04 18:01:46 So here you take a dict of variables as a paramete
f.lopez 2017/08/04 18:58:51 Ok this makes way more sense, thanks
f.lopez 2017/08/07 03:10:15 Done.
+ for var in log_format.split():
+ if proxy_token in var:
+ name = var.rstrip(punctuation).lstrip(punctuation)
Vasily Kuznetsov 2017/08/04 18:01:45 This seems to be equivalent to `var.strip(punctuat
f.lopez 2017/08/04 18:58:50 Acknowledged.
f.lopez 2017/08/07 03:10:15 Done.
f.lopez 2017/08/07 03:10:15 Done.
+ new_var = name[len(proxy_token):].title()
Vasily Kuznetsov 2017/08/04 18:01:46 You seem to be assuming that `proxy_token` is at t
f.lopez 2017/08/04 18:58:51 you are right.
f.lopez 2017/08/07 03:10:15 Done.
+ values[name] = self.headers.get(new_var, '-')
+ return values
+
+ def log_mensaje(self, form, args):
Vasily Kuznetsov 2017/08/04 18:01:47 I'd suggest to name this in English. Not everyone
f.lopez 2017/08/04 18:58:51 Ok I did change it but apparently I forgot to push
f.lopez 2017/08/07 03:10:15 Done.
+ message = Template(form)
+ logging.info(message.safe_substitute(args))
+
+ def do_POST(self):
+ status = 200
+ content = bytes(self.response, "UTF-8")
+ values = {
+ "remote_addr": self.address_string(),
+ "time_local": self.log_date_time_string(),
+ "request": self.requestline,
+ "status": status,
+ "bytes_sent": len(content),
+ "Content-Type": "text/plain",
+ "Content-Length": len(content),
+ }
+ values = self.set_values(values, self.log_format)
+ self.log_mensaje(self.log_format, values)
+ self.send_response(status)
+ self.send_header("Content-Type",values["Content-Type"])
Vasily Kuznetsov 2017/08/04 18:01:46 Our style guide recommends using single quotes in
f.lopez 2017/08/04 18:58:50 Gonna use what you suggested before for the code s
f.lopez 2017/08/07 03:10:14 Done.
+ self.send_header("Content-Length", values["Content-Length"])
+ self.end_headers()
+ self.wfile.write(content)
+ self.log_request(200)
+
+def run(server_class=HTTPServer, handler_class=Handler, port=8000, log_format=''):
Vasily Kuznetsov 2017/08/04 18:01:46 Do you really need this function? There's a lot of
f.lopez 2017/08/04 18:58:51 Acknowledged.
f.lopez 2017/08/07 03:10:15 Done.
+ server_address = ('', port)
+ httpd = server_class(server_address, handler_class)
+ httpd.serve_forever()
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser()
+ parser.add_argument('port', action='store',
Vasily Kuznetsov 2017/08/04 18:01:46 Why is "port" a positional argument while all othe
f.lopez 2017/08/04 18:58:51 Acknowledged.
f.lopez 2017/08/07 03:10:14 Done.
+ default=8000, type=int,
+ nargs='?',
+ help='Specify alternate port [default: 8000]')
+ parser.add_argument('--response', action='store',
Vasily Kuznetsov 2017/08/04 18:01:45 This is an optional parameter with no default. If
f.lopez 2017/08/04 18:58:51 Acknowledged.
f.lopez 2017/08/07 03:10:15 Done.
+ type=str, nargs='?',
+ help='The response send to the client')
+ parser.add_argument('--log_format', action='store',
+ type=str, nargs='?',
+ help='Specify the format of the log ouput')
Vasily Kuznetsov 2017/08/04 18:01:47 I don't think you need the word "Specify" here. Th
f.lopez 2017/08/04 18:58:51 Acknowledged.
f.lopez 2017/08/07 03:10:14 Done.
+ parser.add_argument('--log_file', action='store',
+ type=str, nargs='?',
+ help='The file where the logs will be written')
+ args = parser.parse_args()
+ handler_class = Handler
+ logging.basicConfig(filename=args.log_file, level=logging.INFO)
Vasily Kuznetsov 2017/08/04 18:01:46 What do you think about moving this line 2 lines d
f.lopez 2017/08/04 18:58:51 I'm not using the function anymore but I'll still
f.lopez 2017/08/07 03:10:14 Done.
+ setattr(handler_class, 'log_format', args.log_format)
+ setattr(handler_class, 'response', args.response)
+ run(handler_class=handler_class, port=args.port, log_format=args.log_format)
+

Powered by Google App Engine
This is Rietveld