morss: code spacing

One of those commits that make me feel useful
master
pictuga 2020-03-21 23:41:46 +01:00
parent 37b4e144a9
commit 7c3091d64c
1 changed files with 25 additions and 0 deletions

View File

@ -67,6 +67,7 @@ def log(txt, force=False):
if DEBUG or force: if DEBUG or force:
if 'REQUEST_URI' in os.environ: if 'REQUEST_URI' in os.environ:
open('morss.log', 'a').write("%s\n" % repr(txt)) open('morss.log', 'a').write("%s\n" % repr(txt))
else: else:
print(repr(txt)) print(repr(txt))
@ -74,6 +75,7 @@ def log(txt, force=False):
def len_html(txt): def len_html(txt):
if len(txt): if len(txt):
return len(lxml.html.fromstring(txt).text_content()) return len(lxml.html.fromstring(txt).text_content())
else: else:
return 0 return 0
@ -81,6 +83,7 @@ def len_html(txt):
def count_words(txt): def count_words(txt):
if len(txt): if len(txt):
return len(lxml.html.fromstring(txt).text_content().split()) return len(lxml.html.fromstring(txt).text_content().split())
return 0 return 0
@ -89,12 +92,14 @@ class Options:
if len(args): if len(args):
self.options = args self.options = args
self.options.update(options or {}) self.options.update(options or {})
else: else:
self.options = options or {} self.options = options or {}
def __getattr__(self, key): def __getattr__(self, key):
if key in self.options: if key in self.options:
return self.options[key] return self.options[key]
else: else:
return False return False
@ -108,17 +113,23 @@ class Options:
def parseOptions(options): def parseOptions(options):
""" Turns ['md=True'] into {'md':True} """ """ Turns ['md=True'] into {'md':True} """
out = {} out = {}
for option in options: for option in options:
split = option.split('=', 1) split = option.split('=', 1)
if len(split) > 1: if len(split) > 1:
if split[0].lower() == 'true': if split[0].lower() == 'true':
out[split[0]] = True out[split[0]] = True
elif split[0].lower() == 'false': elif split[0].lower() == 'false':
out[split[0]] = False out[split[0]] = False
else: else:
out[split[0]] = split[1] out[split[0]] = split[1]
else: else:
out[split[0]] = True out[split[0]] = True
return out return out
@ -209,6 +220,7 @@ def ItemFill(item, options, feedurl='/', fast=False):
if len(match): if len(match):
link = match[0] link = match[0]
log(link) log(link)
else: else:
link = None link = None
@ -218,6 +230,7 @@ def ItemFill(item, options, feedurl='/', fast=False):
if len(match) and urlparse(match[0]).netloc != 'www.facebook.com': if len(match) and urlparse(match[0]).netloc != 'www.facebook.com':
link = match[0] link = match[0]
log(link) log(link)
else: else:
link = None link = None
@ -300,6 +313,7 @@ def UrlFix(url):
return url return url
def FeedFetch(url, options): def FeedFetch(url, options):
# allow for code execution for feedify # allow for code execution for feedify
pre = feedify.pre_worker(url) pre = feedify.pre_worker(url)
@ -379,6 +393,7 @@ def FeedGather(rss, url, options):
value = queue.get() value = queue.get()
try: try:
worker(*value) worker(*value)
except Exception as e: except Exception as e:
log('Thread Error: %s' % e.message) log('Thread Error: %s' % e.message)
queue.task_done() queue.task_done()
@ -418,6 +433,7 @@ def FeedGather(rss, url, options):
for i, item in enumerate(list(rss.items)): for i, item in enumerate(list(rss.items)):
if threads == 1: if threads == 1:
worker(*[i, item]) worker(*[i, item])
else: else:
queue.put([i, item]) queue.put([i, item])
@ -560,6 +576,7 @@ def cgi_app(environ, start_response):
else: else:
return [out] return [out]
def middleware(func): def middleware(func):
" Decorator to turn a function into a wsgi middleware " " Decorator to turn a function into a wsgi middleware "
# This is called when parsing the code # This is called when parsing the code
@ -576,6 +593,7 @@ def middleware(func):
return app_builder return app_builder
@middleware @middleware
def cgi_file_handler(environ, start_response, app): def cgi_file_handler(environ, start_response, app):
" Simple HTTP server to serve static files (.html, .css, etc.) " " Simple HTTP server to serve static files (.html, .css, etc.) "
@ -619,6 +637,7 @@ def cgi_file_handler(environ, start_response, app):
else: else:
return app(environ, start_response) return app(environ, start_response)
@middleware @middleware
def cgi_error_handler(environ, start_response, app): def cgi_error_handler(environ, start_response, app):
try: try:
@ -633,11 +652,13 @@ def cgi_error_handler(environ, start_response, app):
log('ERROR: %s' % repr(e), force=True) log('ERROR: %s' % repr(e), force=True)
return [cgitb.html(sys.exc_info())] return [cgitb.html(sys.exc_info())]
@middleware @middleware
def cgi_encode(environ, start_response, app): def cgi_encode(environ, start_response, app):
out = app(environ, start_response) out = app(environ, start_response)
return [x if isinstance(x, bytes) else x.encode('utf-8') for x in out] return [x if isinstance(x, bytes) else x.encode('utf-8') for x in out]
def cli_app(): def cli_app():
options = Options(filterOptions(parseOptions(sys.argv[1:-1]))) options = Options(filterOptions(parseOptions(sys.argv[1:-1])))
url = sys.argv[-1] url = sys.argv[-1]
@ -662,6 +683,7 @@ def isInt(string):
try: try:
int(string) int(string)
return True return True
except ValueError: except ValueError:
return False return False
@ -683,6 +705,7 @@ def main():
argPort = int(sys.argv[1]) argPort = int(sys.argv[1])
if argPort > 0: if argPort > 0:
port = argPort port = argPort
else: else:
raise MorssException('Port must be positive integer') raise MorssException('Port must be positive integer')
@ -702,8 +725,10 @@ def main():
# as a CLI app # as a CLI app
try: try:
cli_app() cli_app()
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
raise raise
except Exception as e: except Exception as e:
print('ERROR: %s' % e.message) print('ERROR: %s' % e.message)