From b80560527853310b0d774c07e5cc2b0ea13c96cc Mon Sep 17 00:00:00 2001 From: Sheng Date: Thu, 23 Jan 2020 16:50:14 +0800 Subject: [PATCH] Eased custom font configuration --- tests/data/fonts/fake-font | 0 tests/test_settings.py | 16 ++++++++++++++-- webssh/handler.py | 4 +++- webssh/settings.py | 19 +++++++++++++++++++ webssh/templates/index.html | 10 ++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/data/fonts/fake-font diff --git a/tests/data/fonts/fake-font b/tests/data/fonts/fake-font new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_settings.py b/tests/test_settings.py index bcc8f0d..d62d220 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -10,8 +10,8 @@ import tornado.options as options from tests.utils import make_tests_data_path from webssh.policy import load_host_keys from webssh.settings import ( - get_host_keys_settings, get_policy_setting, base_dir, print_version, - get_ssl_context, get_trusted_downstream, get_origin_setting + get_host_keys_settings, get_policy_setting, base_dir, get_font_setting, + get_ssl_context, get_trusted_downstream, get_origin_setting, print_version ) from webssh.utils import UnicodeType from webssh._version import __version__ @@ -166,3 +166,15 @@ class TestSettings(unittest.TestCase): options.origin = 'www.example.com:80, www.example.org:443' result = {'http://www.example.com', 'https://www.example.org'} self.assertEqual(get_origin_setting(options), result) + + def test_get_font_setting(self): + font_dir = os.path.join(base_dir, 'tests', 'data', 'fonts') + font = '' + self.assertEqual(get_font_setting(font, font_dir), 'fake-font') + + font = 'fake-font' + self.assertEqual(get_font_setting(font, font_dir), 'fake-font') + + font = 'wrong-name' + with self.assertRaises(ValueError): + get_font_setting(font, font_dir) diff --git a/webssh/handler.py b/webssh/handler.py index 3a1a027..bb82e57 100644 --- a/webssh/handler.py +++ b/webssh/handler.py @@ -323,6 +323,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): self.host_keys_settings = host_keys_settings self.ssh_client = self.get_ssh_client() self.debug = self.settings.get('debug', False) + self.font = self.settings.get('font', '') self.result = dict(id=None, status=None, encoding=None) def write_error(self, status_code, **kwargs): @@ -477,7 +478,8 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): pass def get(self): - self.render('index.html', debug=self.debug) + self.render('index.html', debug=self.debug, font_filename=self.font, + font_family=self.font.split('.')[0]) @tornado.gen.coroutine def post(self): diff --git a/webssh/settings.py b/webssh/settings.py index f9da4c7..53d19dd 100644 --- a/webssh/settings.py +++ b/webssh/settings.py @@ -43,6 +43,7 @@ separated by comma; define('wpintvl', type=int, default=0, help='Websocket ping interval') define('maxconn', type=int, default=20, help='Maximum live connections (ssh sessions) per client') +define('font', default='', help='custom font filename') define('version', type=bool, help='Show version information', callback=print_version) @@ -58,6 +59,10 @@ def get_app_settings(options): websocket_ping_interval=options.wpintvl, debug=options.debug, xsrf_cookies=options.xsrf, + font=get_font_setting( + options.font, + os.path.join(base_dir, 'webssh', 'static', 'css', 'fonts') + ), origin_policy=get_origin_setting(options) ) return settings @@ -150,3 +155,17 @@ def get_origin_setting(options): raise ValueError('Empty origin list') return origins + + +def get_font_setting(font, font_dir): + filenames = {f for f in os.listdir(font_dir) if + os.path.isfile(os.path.join(font_dir, f))} + if font: + if font not in filenames: + raise ValueError( + 'Font file {!r} not found'.format(os.path.join(font_dir, font)) + ) + elif filenames: + font = filenames.pop() + + return font diff --git a/webssh/templates/index.html b/webssh/templates/index.html index 10436b7..79106f7 100644 --- a/webssh/templates/index.html +++ b/webssh/templates/index.html @@ -24,6 +24,16 @@ .btn-danger { margin-left: 5px; } + {% if font_filename %} + @font-face { + font-family: '{{font_family}}'; + src: url('static/css/fonts/{{font_filename}}'); + } + + body { + font-family: '{{font_family}}'; + } + {% end %}