Begin to use config

This commit is contained in:
Joscha 2019-06-20 19:52:21 +00:00
parent 08b9af9b3d
commit ae63d3e704
2 changed files with 61 additions and 14 deletions

View file

@ -1,10 +1,15 @@
import asyncio
import logging
from pathlib import Path
from typing import Any, Optional
import urwid
import yaml
from ..attributed_text_widget import ATWidget
from ..markup import Attributes, AT
from ..markup import AT, Attributes
from .edit_widgets import EditWidget
from .euph_config import EuphConfig, EuphLoader
from .room_widget import RoomWidget
__all__ = ["SingleRoomApplication"]
@ -13,13 +18,13 @@ class ChooseRoomWidget(urwid.WidgetWrap):
def __init__(self,
room_style: Optional[str] = None,
error_attrs: Attributes = {},
error_room_attrs: Attributes = {},
error_style: Optional[str] = None,
error_room_style: Optional[str] = None,
) -> None:
self._room_style = room_style
self._error_attrs = error_attrs
self._error_room_attrs = error_room_attrs
self._error_style = error_style
self._error_room_style = error_room_style
self.error = None
self.edit = EditWidget("Choose a room:", caption="&", style=room_style)
@ -49,9 +54,9 @@ class ChooseRoomWidget(urwid.WidgetWrap):
self._w = self.filler
def could_not_connect(self, roomname: str) -> None:
text = AT("Could not connect to ", attributes=self._error_attrs)
text += AT("&" + roomname, attributes=self._error_room_attrs)
text += AT(".\n", attributes=self._error_attrs)
text = AT("Could not connect to ", style=self._error_style)
text += AT("&" + roomname, style=self._error_room_style)
text += AT(".\n", style=self._error_style)
self.set_error(ATWidget(text, align=urwid.CENTER))
@ -74,8 +79,14 @@ class SingleRoomApplication(urwid.WidgetWrap):
"home", "end",
}
def __init__(self) -> None:
self.choose_room = ChooseRoomWidget()
def __init__(self, config: EuphConfig) -> None:
self.c = config
self.choose_room = ChooseRoomWidget(
room_style=self.c.room_style,
error_style=self.c.error_style,
error_room_style=self.c.error_room_style,
)
super().__init__(self.choose_room)
def selectable(self) -> bool:
@ -96,7 +107,7 @@ class SingleRoomApplication(urwid.WidgetWrap):
roomname = self.choose_room.edit.text
if roomname:
room = RoomWidget(roomname)
room = RoomWidget(roomname, self.c)
urwid.connect_signal(room, "close", self.switch_to_choose)
self._w = room
room.connect()
@ -117,3 +128,32 @@ class SingleRoomApplication(urwid.WidgetWrap):
return super().keypress(size, key)
return key
def run_single_room_application(config_file: Path) -> None:
logging.disable()
config_yaml: Any
try:
with open(config_file) as f:
config_yaml = yaml.load(f.read(), Loader=yaml.SafeLoader)
except FileNotFoundError:
config_yaml = {}
loader = EuphLoader()
defaults = loader.defaults()
config = EuphConfig(defaults)
loader.load_to(config, config_yaml)
loop = asyncio.get_event_loop()
main_loop = urwid.MainLoop(
SingleRoomApplication(config),
event_loop=urwid.AsyncioEventLoop(loop=loop),
palette=config.palette,
)
main_loop.run()
if __name__ == "__main__":
config_path = Path("~/.cheuph.conf").expanduser()
run_single_room_application(config_path)