diff --git a/src/tww/localization.py b/src/tww/localization.py index 0e7e182..155d7c8 100644 --- a/src/tww/localization.py +++ b/src/tww/localization.py @@ -60,7 +60,7 @@ def get_default_locale(): return country_code, lang_code, encoding, default_locale -def resolve_locale(locale_s): +def resolve_locale(locale_s=None): country_code, lang_code, encoding, default_locale = get_default_locale() rv = dict( query=locale_s, diff --git a/src/tww/templates/dt.html b/src/tww/templates/dt.html new file mode 100644 index 0000000..880b446 --- /dev/null +++ b/src/tww/templates/dt.html @@ -0,0 +1,18 @@ + +

+ + + + + \ No newline at end of file diff --git a/src/tww/templates/dt_tr.html b/src/tww/templates/dt_tr.html new file mode 100644 index 0000000..627f4dc --- /dev/null +++ b/src/tww/templates/dt_tr.html @@ -0,0 +1,18 @@ + +

+ + + + + \ No newline at end of file diff --git a/src/tww/templates/home.html b/src/tww/templates/home.html index 301b723..6ddb8d7 100644 --- a/src/tww/templates/home.html +++ b/src/tww/templates/home.html @@ -29,35 +29,115 @@ +

Time When and Where

-

- -

- - - - +
- {% for to_tz in to_tzs %} - {% endfor %} +
+ + + + diff --git a/src/tww/templates/td.html b/src/tww/templates/td.html new file mode 100644 index 0000000..0b4dc84 --- /dev/null +++ b/src/tww/templates/td.html @@ -0,0 +1 @@ +

Timedelta: {{ timedelta.diff.duration_iso8601 }} \ No newline at end of file diff --git a/src/tww/templates/tz.html b/src/tww/templates/tz.html new file mode 100644 index 0000000..fa55585 --- /dev/null +++ b/src/tww/templates/tz.html @@ -0,0 +1 @@ +

Timezone: {{ tz.tz_offset }} \ No newline at end of file diff --git a/src/tww/tokenizer.py b/src/tww/tokenizer.py index f0d8c91..504c148 100644 --- a/src/tww/tokenizer.py +++ b/src/tww/tokenizer.py @@ -2,7 +2,6 @@ import argparse import json import locale import re -import sys from datetime import datetime from pygments import highlight, lexers, formatters @@ -13,7 +12,7 @@ from tww import ISO_FORMAT, time_to_emoji, time_ago, workday_diff, workhours_dif from tww import resolve_timezone, dateparser_parse_dt, get_utcnow, get_s_since_epoch, get_ms_since_epoch, \ dt_tz_translation, get_local_now, query_to_format_result -custom_locale = None +custom_locale = resolve_locale() r_generic = re.compile('(.*)', flags=re.IGNORECASE) r_time_in_epoch_s_now = re.compile('(?:time since epoch|seconds since epoch)', flags=re.IGNORECASE) diff --git a/src/tww/web.py b/src/tww/web.py index 7d4e841..76b29bc 100644 --- a/src/tww/web.py +++ b/src/tww/web.py @@ -1,6 +1,8 @@ import pytz from flask import Flask, render_template, jsonify, request +from tokenizer import resolve_query, QUERY_TYPE_DT, QUERY_TYPE_DT_TR, QUERY_TYPE_TZ, QUERY_TYPE_TD + app = Flask(__name__) import dateparser @@ -89,14 +91,37 @@ def home(): ctx = dict(all_tz=pytz.all_timezones) q = request.args.get('q') if q: - ctx["q_resp"] = jsonify(parse_query(q)) + ctx["q_resp"] = jsonify(resolve_query(q)) return render_template("home.html", all_tz=pytz.all_timezones) +def render_solution(solution): + query_type = solution["query_type"] + if query_type == QUERY_TYPE_DT: + return render_template("dt.html", **solution) + elif query_type == QUERY_TYPE_DT_TR: + return render_template("dt_tr.html", **solution) + elif query_type == QUERY_TYPE_TZ: + return render_template("tz.html", **solution) + elif query_type == QUERY_TYPE_TD: + return render_template("td.html", **solution) + + + +def render_solutions(results): + rv = [] + for solution in results['solutions']: + rv.append(render_solution(solution)) + return rv + + @app.route("/api/datetime") def api_datetime(): q = request.args.get('q') - return jsonify(parse_query(q)) + results = resolve_query(q) + rendered_solutions = render_solutions(results) + results['rendered_solutions'] = rendered_solutions + return jsonify(results) if __name__ == "__main__":