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

Delta Between Two Patch Sets: modules/adblockplus/files/mimeo.py

Issue 29504594: #2687 - Include mimeo python module (Closed)
Left Patch Set: Created Aug. 3, 2017, 5:50 p.m.
Right Patch Set: For comments 47 and 48 Created Aug. 22, 2017, 8:33 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « hiera/roles/web/adblockbrowser.yaml ('k') | modules/adblockplus/manifests/web/mimeo.pp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python3 1 #!/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.
2 2
3 import argparse 3 import argparse
4 import logging 4 import re
5 import sys
6 import threading
7 import traceback
5 8
6 from http.server import BaseHTTPRequestHandler, HTTPServer 9 from http.server import BaseHTTPRequestHandler, HTTPServer
7 from string import Template 10 from string import Template
8 11
9 punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" 12 # The token used for the headers passed by nginx is: http_
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.
10 proxy_token = 'http_' 13 REGEX = r'\$http_\w+\b'
14 DEFAULT_LOG = '$remote_addr - - [$time_local] "$request" $status $bytes_sent'
15 _lock = threading.Lock()
16
11 17
12 class Handler(BaseHTTPRequestHandler): 18 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.
13 def set_values(self, values, log_format): 19 def get_header_values(self):
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.
14 for var in log_format.split(): 20 values = {}
15 if proxy_token in var: 21 headers = re.findall(REGEX, self.format)
16 name = var.rstrip(punctuation).lstrip(punctuation) 22 for name in headers:
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.
17 new_var = name[len(proxy_token):].title() 23 new_var = name[6:].replace('_', '-')
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.
18 values[name] = self.headers.get(new_var, '-') 24 values[name[1:]] = self.headers.get(new_var, '-')
19 return values 25 return values
20 26
21 def log_mensaje(self, form, args): 27 def send_simple_response(self, status, response=None):
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.
22 message = Template(form) 28 self.send_response(status)
23 logging.info(message.safe_substitute(args)) 29 self.end_headers()
30 if response is None:
31 response = bytes(self.responses[status][0], 'UTF-8')
32 self.wfile.write(response)
33
34 def write_info(self, args):
35 message = Template(self.format).safe_substitute(args) + '\n'
36 with _lock:
37 self.output.write(message)
38 self.output.flush()
24 39
25 def do_POST(self): 40 def do_POST(self):
26 status = 200 41 status = 200
27 content = bytes(self.response, "UTF-8") 42 content = bytes(self.response, 'UTF-8')
28 values = { 43 values = {
29 "remote_addr": self.address_string(), 44 'remote_addr': self.address_string(),
30 "time_local": self.log_date_time_string(), 45 'time_local': self.log_date_time_string(),
31 "request": self.requestline, 46 'request': self.requestline,
32 "status": status, 47 'status': status,
33 "bytes_sent": len(content), 48 'bytes_sent': len(content),
34 "Content-Type": "text/plain",
35 "Content-Length": len(content),
36 } 49 }
37 values = self.set_values(values, self.log_format) 50 values.update(self.get_header_values())
38 self.log_mensaje(self.log_format, values) 51 try:
39 self.send_response(status) 52 self.write_info(values)
40 self.send_header("Content-Type",values["Content-Type"]) 53 self.send_simple_response(status, content)
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.
41 self.send_header("Content-Length", values["Content-Length"]) 54 except:
42 self.end_headers() 55 traceback.print_exc(file=sys.stderr)
43 self.wfile.write(content) 56 self.send_simple_response(500)
44 self.log_request(200)
45 57
46 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.
47 server_address = ('', port)
48 httpd = server_class(server_address, handler_class)
49 httpd.serve_forever()
50 58
51 if __name__ == '__main__': 59 if __name__ == '__main__':
52 parser = argparse.ArgumentParser() 60 parser = argparse.ArgumentParser()
53 parser.add_argument('port', action='store', 61 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.
54 default=8000, type=int, 62 default=8000, type=int,
55 nargs='?', 63 nargs='?',
56 help='Specify alternate port [default: 8000]') 64 help='Port to use [default: 8000]')
57 parser.add_argument('--response', action='store', 65 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.
66 type=str, nargs='?', default='OK',
67 help='The response send to the client')
68 parser.add_argument('--format', action='store',
58 type=str, nargs='?', 69 type=str, nargs='?',
59 help='The response send to the client') 70 default=DEFAULT_LOG,
60 parser.add_argument('--log_format', action='store', 71 help='Format of the log ouput')
61 type=str, nargs='?', 72 parser.add_argument('output', action='store',
62 help='Specify the format of the log ouput') 73 type=str, nargs='?', default='-',
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.
63 parser.add_argument('--log_file', action='store',
64 type=str, nargs='?',
65 help='The file where the logs will be written') 74 help='The file where the logs will be written')
66 args = parser.parse_args() 75 args = parser.parse_args()
67 handler_class = Handler 76 if args.output and args.output != '-':
68 logging.basicConfig(filename=args.log_file, level=logging.INFO) 77 fh = open(args.output, 'a')
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.
69 setattr(handler_class, 'log_format', args.log_format) 78 else:
70 setattr(handler_class, 'response', args.response) 79 fh = open(sys.stdout.fileno(), 'w', closefd=False)
71 run(handler_class=handler_class, port=args.port, log_format=args.log_format) 80 try:
72 81 Handler.output = fh
82 Handler.format = args.format
83 Handler.response = args.response
84 server_address = ('', args.port)
85 httpd = HTTPServer(server_address, Handler)
86 httpd.serve_forever()
87 finally:
88 fh.close()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld