extracted get_moon() to lib/view/moon.py

v2
Igor Chubin 5 years ago
parent 6b2577b745
commit 4ea5c74b03

@ -0,0 +1,51 @@
import sys
import os
import dateutil.parser
from gevent.subprocess import Popen, PIPE
sys.path.insert(0, "..")
import constants
import parse_query
import globals
def get_moon(location, html=False, lang=None, query=None):
if query is None:
query = {}
date = None
if '@' in location:
date = location[location.index('@')+1:]
location = location[:location.index('@')]
cmd = [globals.PYPHOON]
if date:
try:
dateutil.parser.parse(date)
except Exception as e:
print("ERROR: %s" % e)
else:
cmd += [date]
env = os.environ.copy()
if lang:
env['LANG'] = lang
p = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env)
stdout = p.communicate()[0]
stdout = stdout.decode("utf-8")
if query.get('no-terminal', False):
stdout = globals.remove_ansi(stdout)
if html:
p = Popen(
["bash", globals.ANSI2HTML, "--palette=solarized", "--bg=dark"],
stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate(stdout.encode("utf-8"))
stdout = stdout.decode("utf-8")
stderr = stderr.decode("utf-8")
if p.returncode != 0:
globals.error(stdout + stderr)
return stdout

@ -12,10 +12,9 @@ import sys
import os
import re
import time
import dateutil.parser
from translations import get_message, FULL_TRANSLATION, PARTIAL_TRANSLATION, SUPPORTED_LANGS
from globals import WEGO, PYPHOON, CACHEDIR, ANSI2HTML, \
from globals import WEGO, CACHEDIR, \
NOT_FOUND_LOCATION, DEFAULT_LOCATION, TEST_FILE, \
log, error
@ -215,42 +214,3 @@ def get_wetter(location, ip, html=False, lang=None, query=None, location_name=No
filename += '.html'
return open(filename).read()
def get_moon(location, html=False, lang=None, query=None):
if query is None:
query = {}
date = None
if '@' in location:
date = location[location.index('@')+1:]
location = location[:location.index('@')]
cmd = [PYPHOON]
if date:
try:
dateutil.parser.parse(date)
except Exception as e:
print("ERROR: %s" % e)
else:
cmd += [date]
env = os.environ.copy()
if lang:
env['LANG'] = lang
p = Popen(cmd, stdout=PIPE, stderr=PIPE, env=env)
stdout = p.communicate()[0]
stdout = stdout.decode("utf-8")
if query.get('no-terminal', False):
stdout = remove_ansi(stdout)
if html:
p = Popen(["bash", ANSI2HTML, "--palette=solarized", "--bg=dark"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate(stdout)
stdout = stdout.decode("utf-8")
stderr = stderr.decode("utf-8")
if p.returncode != 0:
error(stdout + stderr)
return stdout

@ -22,14 +22,15 @@ from globals import get_help_file, log, \
MY_EXTERNAL_IP, QUERY_LIMITS
from location import is_location_blocked, location_processing
from limits import Limits
from wttr import get_wetter, get_moon
from wttr import get_wetter
from view.moon import get_moon
from view.line import wttr_line
import cache
if not os.path.exists(os.path.dirname(LOG_FILE)):
os.makedirs(os.path.dirname(LOG_FILE))
logging.basicConfig(filename=LOG_FILE, level=logging.DEBUG, format='%(asctime)s %(message)s')
logging.basicConfig(filename=LOG_FILE, level=logging.INFO, format='%(asctime)s %(message)s')
LIMITS = Limits(whitelist=[MY_EXTERNAL_IP], limits=QUERY_LIMITS)

Loading…
Cancel
Save