Added class Font

pull/126/head
Sheng 2020-01-23 19:15:39 +08:00
parent dcbd4a575a
commit 8901eb0580
4 changed files with 28 additions and 14 deletions

View File

@ -10,7 +10,7 @@ import tornado.options as options
from tests.utils import make_tests_data_path from tests.utils import make_tests_data_path
from webssh.policy import load_host_keys from webssh.policy import load_host_keys
from webssh.settings import ( from webssh.settings import (
get_host_keys_settings, get_policy_setting, base_dir, get_font_setting, get_host_keys_settings, get_policy_setting, base_dir, get_font_filename,
get_ssl_context, get_trusted_downstream, get_origin_setting, print_version get_ssl_context, get_trusted_downstream, get_origin_setting, print_version
) )
from webssh.utils import UnicodeType from webssh.utils import UnicodeType
@ -170,11 +170,11 @@ class TestSettings(unittest.TestCase):
def test_get_font_setting(self): def test_get_font_setting(self):
font_dir = os.path.join(base_dir, 'tests', 'data', 'fonts') font_dir = os.path.join(base_dir, 'tests', 'data', 'fonts')
font = '' font = ''
self.assertEqual(get_font_setting(font, font_dir), 'fake-font') self.assertEqual(get_font_filename(font, font_dir), 'fake-font')
font = 'fake-font' font = 'fake-font'
self.assertEqual(get_font_setting(font, font_dir), 'fake-font') self.assertEqual(get_font_filename(font, font_dir), 'fake-font')
font = 'wrong-name' font = 'wrong-name'
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
get_font_setting(font, font_dir) get_font_filename(font, font_dir)

View File

@ -478,8 +478,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
pass pass
def get(self): def get(self):
self.render('index.html', debug=self.debug, font_filename=self.font, self.render('index.html', debug=self.debug, font=self.font)
font_family=self.font.split('.')[0])
@tornado.gen.coroutine @tornado.gen.coroutine
def post(self): def post(self):

View File

@ -49,9 +49,23 @@ define('version', type=bool, help='Show version information',
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
font_dirs = ['webssh', 'static', 'css', 'fonts']
max_body_size = 1 * 1024 * 1024 max_body_size = 1 * 1024 * 1024
class Font(object):
def __init__(self, filename, dirs):
self.family = self.get_family(filename)
self.url = self.get_url(filename, dirs)
def get_family(self, filename):
return filename.split('.')[0]
def get_url(self, filename, dirs):
return os.path.join(*(dirs + [filename]))
def get_app_settings(options): def get_app_settings(options):
settings = dict( settings = dict(
template_path=os.path.join(base_dir, 'webssh', 'templates'), template_path=os.path.join(base_dir, 'webssh', 'templates'),
@ -59,9 +73,10 @@ def get_app_settings(options):
websocket_ping_interval=options.wpintvl, websocket_ping_interval=options.wpintvl,
debug=options.debug, debug=options.debug,
xsrf_cookies=options.xsrf, xsrf_cookies=options.xsrf,
font=get_font_setting( font=Font(
options.font, get_font_filename(options.font,
os.path.join(base_dir, 'webssh', 'static', 'css', 'fonts') os.path.join(base_dir, *font_dirs)),
font_dirs[1:]
), ),
origin_policy=get_origin_setting(options) origin_policy=get_origin_setting(options)
) )
@ -157,7 +172,7 @@ def get_origin_setting(options):
return origins return origins
def get_font_setting(font, font_dir): def get_font_filename(font, font_dir):
filenames = {f for f in os.listdir(font_dir) if not f.startswith('.') filenames = {f for f in os.listdir(font_dir) if not f.startswith('.')
and os.path.isfile(os.path.join(font_dir, f))} and os.path.isfile(os.path.join(font_dir, f))}
if font: if font:

View File

@ -24,14 +24,14 @@
.btn-danger { .btn-danger {
margin-left: 5px; margin-left: 5px;
} }
{% if font_filename %} {% if font.url %}
@font-face { @font-face {
font-family: '{{font_family}}'; font-family: '{{ font.family }}';
src: url('static/css/fonts/{{font_filename}}'); src: url('{{ font.url }}');
} }
body { body {
font-family: '{{font_family}}'; font-family: '{{ font.family }}';
} }
{% end %} {% end %}
</style> </style>