commit b80560527853310b0d774c07e5cc2b0ea13c96cc
parent 359a91b5e801aae840e78e79454fee045f68b57f
Author: Sheng <webmaster0115@gmail.com>
Date: Thu, 23 Jan 2020 16:50:14 +0800
Eased custom font configuration
Diffstat:
5 files changed, 46 insertions(+), 3 deletions(-)
diff --git a/tests/data/fonts/fake-font b/tests/data/fonts/fake-font
diff --git 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
@@ -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
@@ -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
@@ -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 %}
</style>
</head>
<body>