commit d55e3fce891b991cacead4996cceb6c38f51d9b2
parent c34a8530c58483d2d1e010aa88b7997f996ea9bb
Author: Isak Lindhé <isak.e.lindhe@gmail.com>
Date: Fri, 12 Apr 2019 10:14:56 +0200
global config and options for hiding meta bars
Diffstat:
2 files changed, 39 insertions(+), 16 deletions(-)
diff --git a/txtv/config.py b/txtv/config.py
@@ -6,8 +6,12 @@ CONFIG_DEFAULT_PATH = CONFIG_DIR / 'txtv.cfg'
CONFIG_DEFAULT_VALUES = {
'alias' : { },
'general' : {
- 'prompt': 'txtv> ',
- # 'show_top_row': 'no',
+ 'prompt': 'txtv> ',
+ },
+ 'show': {
+ 'svt_header': 'yes',
+ 'publicerad_header': 'yes',
+ 'navigation_footer': 'yes',
},
}
diff --git a/txtv/txtv.py b/txtv/txtv.py
@@ -9,6 +9,7 @@ from pathlib import Path
from txtv.util import err
from txtv.config import get_config, apply_aliases, configparser
+cfg = get_config()
class Page:
def __init__(self, num: int):
@@ -27,21 +28,43 @@ class Page:
def show(self, subpages=None) -> str:
"""Prints the page contained by the specified tag in color."""
- out = ""
+
+ def _has_class(node: bs4.element.Tag, cls: str):
+ return 'class' in node.attrs and cls in node.attrs['class']
+
+ parsed = ""
for page in subpages or self.subpages:
for node in page:
if isinstance(node, str):
- out += str(node)
+ # if node_idx != 0 or cfg.getboolean('show', 'svt_header'):
+ parsed += str(node)
continue
style = ''
- if 'DH' in node.attrs['class']:
+ if _has_class(node, 'DH'):
style = Fore.YELLOW + Style.BRIGHT
- elif 'Y' in node.attrs['class']:
+ elif _has_class(node, 'Y'):
style = Style.DIM
- elif 'bgB' in node.attrs['class']:
+ elif _has_class(node, 'bgB'):
style = Fore.BLUE
- out += str(style + node.get_text() + Style.RESET_ALL)
- return out
+ parsed += str(style + node.get_text() + Style.RESET_ALL)
+ # filter out stuff according to config
+ lines = parsed.splitlines()
+ filtered = ''
+ # pprint(lines)
+ for idx, line in enumerate(lines):
+ if idx == 0 and not cfg.getboolean('show', 'svt_header'):
+ pass
+ elif idx == 1 \
+ and 'PUBLICERAD' in line \
+ and not cfg.getboolean('show', 'publicerad_header'):
+ pass
+ elif idx == len(lines) - 1 \
+ and re.match(r'.* [0-9]{3} +.* [0-9]{3} +.* [0-9]{3}', line) \
+ and not cfg.getboolean('show', 'navigation_footer'):
+ pass
+ else:
+ filtered += line + '\n'
+ return filtered
def next_page(self):
return Page(self.next)
@@ -49,7 +72,6 @@ class Page:
def prev_page(self):
return Page(self.prev)
-
def validate_page_nbr(arg: str) -> int:
"""
Validates a page number, returns as int. Raises ValueError if bad.
@@ -72,7 +94,7 @@ def match_command(arg: str, interactive: bool=False) -> tuple:
return None, None
-def interactive(start_page: Page, cfg: configparser.ConfigParser):
+def interactive(start_page: Page):
print(start_page.show())
state = dict(page=start_page)
while True:
@@ -91,12 +113,10 @@ def interactive(start_page: Page, cfg: configparser.ConfigParser):
except (EOFError, KeyboardInterrupt):
exit(0)
-
#####################
# COMMAND FUNCTIONS #
#####################
-
def cmd_help(**kwargs) -> str:
out = 'commands:\n'
for cmd in commands:
@@ -190,17 +210,16 @@ commands = [
def run():
colorama.init()
- cfg = get_config()
if len(sys.argv) > 2:
err('one arg only plz')
if len(sys.argv) == 1:
- interactive(Page(100), cfg=cfg)
+ interactive(Page(100))
else:
raw_arg = sys.argv[1]
real_arg = apply_aliases(raw_arg, cfg)
cmd, _ = match_command(real_arg)
if cmd:
- print(cmd['func'](arg=real_arg, cfg=cfg), end='')
+ print(cmd['func'](arg=real_arg), end='')
sys.exit(0)
else:
err("That's not a command, kompis. 'txtv help' gives you a list of commands.")