diff --git a/lib/view/wttr.py b/lib/view/wttr.py index 0c3694f..7d69663 100644 --- a/lib/view/wttr.py +++ b/lib/view/wttr.py @@ -13,15 +13,22 @@ from gevent.subprocess import Popen, PIPE sys.path.insert(0, "..") from translations import get_message, SUPPORTED_LANGS -from globals import WEGO, TRANSLATION_TABLE, NOT_FOUND_LOCATION, \ - DEFAULT_LOCATION, ANSI2HTML, error, remove_ansi +from globals import ( + WEGO, + TRANSLATION_TABLE, + NOT_FOUND_LOCATION, + DEFAULT_LOCATION, + ANSI2HTML, + error, + remove_ansi, +) def get_wetter(parsed_query): - location = parsed_query['location'] - html = parsed_query['html_output'] - lang = parsed_query['lang'] + location = parsed_query["location"] + html = parsed_query["html_output"] + lang = parsed_query["lang"] location_not_found = False if location == NOT_FOUND_LOCATION: @@ -33,10 +40,14 @@ def get_wetter(parsed_query): stdout, stderr, returncode = _wego_wrapper(location, parsed_query) first_line, stdout = _wego_postprocessing(location, parsed_query, stdout) - if location_not_found or \ - (returncode != 0 \ - and ('Unable to find any matching weather' - ' location to the parsed_query submitted') in stderr): + if location_not_found or ( + returncode != 0 + and ( + "Unable to find any matching weather" + " location to the parsed_query submitted" + ) + in stderr + ): stdout, stderr, returncode = _wego_wrapper(DEFAULT_LOCATION, parsed_query) location_not_found = True @@ -49,14 +60,25 @@ def get_wetter(parsed_query): >>> >>> 404 %s: %s >>> -""" % (get_message("UNKNOWN_LOCATION", lang).upper(), parsed_query['override_location_name']) - - not_found_header = "\n".join("\033[48;5;91m" + x + " \033[0m" - for x in not_found_header.splitlines()[1:]) - - not_found_footer = get_message('NOT_FOUND_MESSAGE', lang) - not_found_footer = "\n".join("\033[48;5;91m " + x + " \033[0m" - for x in not_found_footer.splitlines() if x) + "\n" +""" % ( + get_message("UNKNOWN_LOCATION", lang).upper(), + parsed_query["override_location_name"], + ) + + not_found_header = "\n".join( + "\033[48;5;91m" + x + " \033[0m" + for x in not_found_header.splitlines()[1:] + ) + + not_found_footer = get_message("NOT_FOUND_MESSAGE", lang) + not_found_footer = ( + "\n".join( + "\033[48;5;91m " + x + " \033[0m" + for x in not_found_footer.splitlines() + if x + ) + + "\n" + ) first_line, stdout = _wego_postprocessing(location, parsed_query, stdout) stdout = not_found_header + "\n----\n" + stdout + not_found_footer @@ -65,33 +87,34 @@ def get_wetter(parsed_query): return _htmlize(stdout, first_line, parsed_query) return stdout + def _wego_wrapper(location, parsed_query): - lang = parsed_query['lang'] + lang = parsed_query["lang"] if location == DEFAULT_LOCATION: location_name = DEFAULT_LOCATION.capitalize() else: - location_name = parsed_query['override_location_name'] + location_name = parsed_query["override_location_name"] - cmd = [WEGO, '--city=%s' % location] + cmd = [WEGO, "--city=%s" % location] - if parsed_query.get('inverted_colors'): - cmd += ['-inverse'] + if parsed_query.get("inverted_colors"): + cmd += ["-inverse"] - if parsed_query.get('use_ms_for_wind'): - cmd += ['-wind_in_ms'] + if parsed_query.get("use_ms_for_wind"): + cmd += ["-wind_in_ms"] - if parsed_query.get('narrow'): - cmd += ['-narrow'] + if parsed_query.get("narrow"): + cmd += ["-narrow"] if lang and lang in SUPPORTED_LANGS: - cmd += ['-lang=%s'%lang] + cmd += ["-lang=%s" % lang] - if parsed_query.get('use_imperial', False): - cmd += ['-imperial'] + if parsed_query.get("use_imperial", False): + cmd += ["-imperial"] if location_name: - cmd += ['-location_name', location_name] + cmd += ["-location_name", location_name] proc = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() @@ -100,61 +123,63 @@ def _wego_wrapper(location, parsed_query): return stdout, stderr, proc.returncode + def _wego_postprocessing(location, parsed_query, stdout): - full_address = parsed_query['full_address'] - lang = parsed_query['lang'] + full_address = parsed_query["full_address"] + lang = parsed_query["lang"] - if 'days' in parsed_query: - if parsed_query['days'] == '0': + if "days" in parsed_query: + if parsed_query["days"] == "0": stdout = "\n".join(stdout.splitlines()[:7]) + "\n" - if parsed_query['days'] == '1': + if parsed_query["days"] == "1": stdout = "\n".join(stdout.splitlines()[:17]) + "\n" - if parsed_query['days'] == '2': + if parsed_query["days"] == "2": stdout = "\n".join(stdout.splitlines()[:27]) + "\n" - first = stdout.splitlines()[0] rest = stdout.splitlines()[1:] - if parsed_query.get('no-caption', False): - if ':' in first: + if parsed_query.get("no-caption", False): + if ":" in first: first = first.split(":", 1)[1] stdout = "\n".join([first.strip()] + rest) + "\n" - if parsed_query.get('no-terminal', False): + if parsed_query.get("no-terminal", False): stdout = remove_ansi(stdout) - if parsed_query.get('no-city', False): + if parsed_query.get("no-city", False): stdout = "\n".join(stdout.splitlines()[2:]) + "\n" - if parsed_query.get('dumb', False): + if parsed_query.get("dumb", False): stdout = stdout.translate(TRANSLATION_TABLE) - if full_address \ - and parsed_query.get('format', 'txt') != 'png' \ - and (not parsed_query.get('no-city') - and not parsed_query.get('no-caption') - and not parsed_query.get('days') == '0'): - line = "%s: %s [%s]\n" % ( - get_message('LOCATION', lang), - full_address, - location) + if ( + full_address + and parsed_query.get("format", "txt") != "png" + and ( + not parsed_query.get("no-city") + and not parsed_query.get("no-caption") + and not parsed_query.get("days") == "0" + ) + ): + line = "%s: %s [%s]\n" % (get_message("LOCATION", lang), full_address, location) stdout += line - if parsed_query.get('padding', False): + if parsed_query.get("padding", False): lines = [x.rstrip() for x in stdout.splitlines()] max_l = max(len(remove_ansi(x)) for x in lines) - last_line = " "*max_l + " .\n" - stdout = " \n" + "\n".join(" %s " %x for x in lines) + "\n" + last_line + last_line = " " * max_l + " .\n" + stdout = " \n" + "\n".join(" %s " % x for x in lines) + "\n" + last_line return first, stdout + def _htmlize(ansi_output, title, parsed_query): """Return HTML representation of `ansi_output`. Use `title` as the title of the page. Format page according to query parameters from `parsed_query`.""" cmd = ["bash", ANSI2HTML, "--palette=solarized"] - if not parsed_query.get('inverted_colors'): + if not parsed_query.get("inverted_colors"): cmd += ["--bg=dark"] proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE) @@ -164,20 +189,22 @@ def _htmlize(ansi_output, title, parsed_query): if proc.returncode != 0: error(stdout + stderr) - if parsed_query.get('inverted_colors'): + if parsed_query.get("inverted_colors"): stdout = stdout.replace( - '
', '') + '', '' + ) title = "