From 279721eb9df87672213ba8ae1856698efc5d3d5f Mon Sep 17 00:00:00 2001 From: Jordan Date: Wed, 26 Jun 2019 21:52:09 -0700 Subject: general cleanup, rm write() --- tor-metrics/generate.py | 36 ++++++++++++++++------------- tor-metrics/templates/base.html | 8 +++---- tor-metrics/templates/effective_family.html | 6 ++--- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/tor-metrics/generate.py b/tor-metrics/generate.py index b6b2b3c..3235bd0 100755 --- a/tor-metrics/generate.py +++ b/tor-metrics/generate.py @@ -27,8 +27,8 @@ def unsorted(relays, filename, is_index): template = env.get_template(filename) template_render = template.render(relays=relays, is_index=is_index) output = os.path.join(config.CONFIG['output_root'], filename) - with open(output, 'w', encoding='utf8') as out: - out.write(template_render) + with open(output, 'w', encoding='utf8') as html: + html.write(template_render) def effective_family(relays): template = env.get_template('effective_family.html') @@ -48,8 +48,15 @@ def effective_family(relays): if fingerprint in p_relay['effective_family']: members.append(p_relay) bandwidth += p_relay['observed_bandwidth'] - write(output_path, members, bandwidth, template, fingerprint, - category='family') + dir_path = os.path.join(output_path, fingerprint) + os.makedirs(dir_path) + f_bandwidth = round(bandwidth / 1000000, 2) # convert to MB/s + rendered = template.render(relays=members, bandwidth=f_bandwidth, + is_index=False, path_prefix='../../', deactivate='family', + family=fingerprint) + with open(os.path.join(dir_path, 'index.html'), 'w', + encoding='utf8') as html: + html.write(rendered) def pages_by_key(relays, key): template = env.get_template(key + '.html') @@ -68,18 +75,15 @@ def pages_by_key(relays, key): if p_relay.get(key) and p_relay[key] == relay[key]: found_relays.append(p_relay) bandwidth += p_relay['observed_bandwidth'] - write(output_path, found_relays, bandwidth, template, relay[key]) - -def write(parent_dir, relays, bandwidth, template, key, category=None): - dir_path = os.path.join(parent_dir, key) - os.makedirs(dir_path) - f_bandwidth = round(bandwidth / 1000000, 2) # convert to MB/s - rendered = template.render(relays=relays, bandwidth=f_bandwidth, - focused=key, is_index=False, category=category, path_prefix='../../', - special_countries=countries.the_prefixed) - with open(os.path.join(dir_path, 'index.html'), 'w', encoding='utf8') as html: - html.write(rendered) - + dir_path = os.path.join(output_path, relay[key]) + os.makedirs(dir_path) + f_bandwidth = round(bandwidth / 1000000, 2) # convert to MB/s + rendered = template.render(relays=found_relays, + bandwidth=f_bandwidth, is_index=False, path_prefix='../../', + deactivate=key, special_countries=countries.the_prefixed) + with open(os.path.join(dir_path, 'index.html'), 'w', + encoding='utf8') as html: + html.write(rendered) relays = Relays() generate_html(relays) diff --git a/tor-metrics/templates/base.html b/tor-metrics/templates/base.html index 4f75ab1..95ef872 100644 --- a/tor-metrics/templates/base.html +++ b/tor-metrics/templates/base.html @@ -36,14 +36,14 @@ {% else %} {% set obs_bandwidth = '%s %s'|format((relay['observed_bandwidth'] / 1000)|round(2, 'common'), 'KB/s') %}{% endif %} - {% if category != 'family' and relay['effective_family']|length > 1 %} + {% if deactivate != 'family' and relay['effective_family']|length > 1 %} {{ relay['nickname'] }} ({{ relay['effective_family']|length }}) {% else %} {{ relay['nickname'] }}{% endif %} {{ obs_bandwidth }} {{ relay['or_addresses'][0].split(':', 1)[0] }} - {% if relay['as'] %}{% if focused != 'as' %} + {% if relay['as'] %}{% if deactivate != 'as' %} {{ relay['as'] }} {% else %} {{ relay['as'] }}{% endif %} @@ -55,14 +55,14 @@ {% else %} Unknown{% endif %} - {% if relay['country'] %}{% if focused != 'country' %} + {% if relay['country'] %}{% if deactivate != 'country' %} {{ relay['country_name'] }} {% else %} {{ relay['country_name'] }}{% endif %} {% else %} X{% endif %} - {% if focused != 'platform' %} + {% if deactivate != 'platform' %} {{ relay['platform'] }} {% else %} {{ relay['platform'] }}{% endif %} diff --git a/tor-metrics/templates/effective_family.html b/tor-metrics/templates/effective_family.html index 1acec41..fec8609 100644 --- a/tor-metrics/templates/effective_family.html +++ b/tor-metrics/templates/effective_family.html @@ -1,4 +1,4 @@ {% extends "base.html" %} -{% block title %}Tor Relays :: Family {{ focused }}{% endblock %} -{% block header %}Home :: Family {{ focused }}{% endblock %} -{% block description %}Relays with effective family member {{ focused }} are responsible for ~{{ bandwidth }} MB/s of traffic.{% endblock %} +{% block title %}Tor Relays :: Family {{ family }}{% endblock %} +{% block header %}Home :: Family {{ family }}{% endblock %} +{% block description %}Relays with effective family member {{ family }} are responsible for ~{{ bandwidth }} MB/s of traffic.{% endblock %} -- cgit v1.2.3-54-g00ecf