From 9610a5a0dd69b6b97b65b2b3b339e926132488d6 Mon Sep 17 00:00:00 2001 From: shypike Date: Sat, 12 May 2012 15:54:44 +0200 Subject: [PATCH] Do HTML quoting for " and ' characters in skin texts. Improve skin text translation cache implementation. --- SABnzbd.py | 2 ++ sabnzbd/__init__.py | 3 ++- sabnzbd/api.py | 26 +++++++++++++++----------- sabnzbd/encoding.py | 16 ++++++++++++++++ sabnzbd/interface.py | 2 +- sabnzbd/osxmenu.py | 3 +-- sabnzbd/sabtray.py | 2 +- sabnzbd/wizard.py | 2 +- 8 files changed, 39 insertions(+), 17 deletions(-) diff --git a/SABnzbd.py b/SABnzbd.py index fac0d05..fc4f693 100755 --- a/SABnzbd.py +++ b/SABnzbd.py @@ -1483,6 +1483,8 @@ def main(): # Wait for server to become ready cherrypy.engine.wait(cherrypy.process.wspbus.states.STARTED) + sabnzbd.WEBUI_READY = True + if enable_https: browser_url = "https://%s:%s/sabnzbd" % (browserhost, cherryport) else: diff --git a/sabnzbd/__init__.py b/sabnzbd/__init__.py index 3e2f7b7..8156152 100644 --- a/sabnzbd/__init__.py +++ b/sabnzbd/__init__.py @@ -129,6 +129,7 @@ PAUSED_ALL = False OLD_QUEUE = False SCHED_RESTART = False # Set when restarted through scheduler WINTRAY = None # Thread for the Windows SysTray icon +WEBUI_READY = False __INITIALIZED__ = False __SHUTTING_DOWN__ = False @@ -259,7 +260,7 @@ def initialize(pause_downloader = False, clean_up = False, evalSched=False, repa ### Set language files lang.set_locale_info('SABnzbd', DIR_LANGUAGE) lang.set_language(cfg.language()) - sabnzbd.api.cache_skin_trans() + sabnzbd.api.clear_trans_cache() ### Check for old queue (when a new queue is not present) if not os.path.exists(os.path.join(cfg.cache_dir.get_path(), QUEUE_FILE_NAME)): diff --git a/sabnzbd/api.py b/sabnzbd/api.py index b1cbb83..b87ef95 100644 --- a/sabnzbd/api.py +++ b/sabnzbd/api.py @@ -53,7 +53,7 @@ from sabnzbd.utils.pathbrowser import folders_at_path from sabnzbd.misc import loadavg, to_units, diskfree, disktotal, get_ext, \ get_filename, int_conv, globber, time_format, remove_all, \ starts_with_path -from sabnzbd.encoding import xml_name, unicoder, special_fixer, platform_encode +from sabnzbd.encoding import xml_name, unicoder, special_fixer, platform_encode, html_escape from sabnzbd.postproc import PostProcessor from sabnzbd.articlecache import ArticleCache from sabnzbd.utils.servertests import test_nntp_server_dict @@ -1517,21 +1517,25 @@ _SKIN_CACHE = {} # Stores pre-translated acronyms def Ttemplate(txt): """ Translation function for Skin texts """ - return _SKIN_CACHE.get(txt, txt) + global _SKIN_CACHE + if txt in _SKIN_CACHE: + return _SKIN_CACHE[txt] + else: + tra = html_escape(Tx(SKIN_TEXT.get(txt, txt))) + _SKIN_CACHE[txt] = tra + return tra -def cache_skin_trans(): - """ Build cache for skin translations +def clear_trans_cache(): + """ Clean cache for skin translations """ global _SKIN_CACHE - for txt in SKIN_TEXT: - _SKIN_CACHE[txt] = Tx(SKIN_TEXT.get(txt, txt)) + # One dummy entry needed for check_trans() + dummy = _SKIN_CACHE + _SKIN_CACHE = {} + del dummy + logging.debug('Cleared translation cache') -def check_trans(): - """ Check whether language has been initialized - """ - global _SKIN_CACHE - return bool(_SKIN_CACHE) def build_header(prim, webdir=''): try: diff --git a/sabnzbd/encoding.py b/sabnzbd/encoding.py index cac1d18..d90d32c 100644 --- a/sabnzbd/encoding.py +++ b/sabnzbd/encoding.py @@ -323,4 +323,20 @@ def fixup_ff4(p): return ''.join(name) +_HTML_TABLE = { + #'&' : '&', # Not yet, texts need to be cleaned from HTML first + #'>' : '>', # Not yet, texts need to be cleaned from HTML first + #'<' : '<', # Not yet, texts need to be cleaned from HTML first + '"' : '"', + "'" : ''' + } + +def html_escape(txt): + """ Replace HTML metacharacters with &-constructs """ + # Replacement for inefficient xml.sax.saxutils.escape function + if [True for ch in _HTML_TABLE if ch in txt]: + return ''.join((_HTML_TABLE.get(ch, ch) for ch in txt)) + else: + return txt + auto_fsys() diff --git a/sabnzbd/interface.py b/sabnzbd/interface.py index 591397a..041fe90 100644 --- a/sabnzbd/interface.py +++ b/sabnzbd/interface.py @@ -1369,7 +1369,7 @@ class ConfigGeneral(object): if language and language != cfg.language(): cfg.language.set(language) set_language(language) - sabnzbd.api.cache_skin_trans() + sabnzbd.api.clear_trans_cache() cleanup_list = kwargs.get('cleanup_list') if cleanup_list and sabnzbd.WIN32: diff --git a/sabnzbd/osxmenu.py b/sabnzbd/osxmenu.py index 179e526..10eec0f 100644 --- a/sabnzbd/osxmenu.py +++ b/sabnzbd/osxmenu.py @@ -50,7 +50,6 @@ import sabnzbd.dirscanner as dirscanner from sabnzbd.bpsmeter import BPSMeter from sabnzbd.newzbin import Bookmarks from sabnzbd.database import get_history_handle -from sabnzbd.api import check_trans status_icons = {'idle':'../Resources/sab_idle.png','pause':'../Resources/sab_pause.png','clicked':'../Resources/sab_clicked.png'} start_time = NSDate.date() @@ -93,7 +92,7 @@ class SABnzbdDelegate(NSObject): #cherrypy.engine.wait(cherrypy.process.wspbus.states.STARTED) # Wait for translated texts to be loaded - while not check_trans() and not sabnzbd.SABSTOP: + while not sabnzbd.WEBUI_READY and not sabnzbd.SABSTOP: time.sleep(0.5) if (debug == 1) : NSLog("[osx] language file not loaded, waiting") diff --git a/sabnzbd/sabtray.py b/sabnzbd/sabtray.py index 6ee9b54..e7d83fd 100644 --- a/sabnzbd/sabtray.py +++ b/sabnzbd/sabtray.py @@ -45,7 +45,7 @@ class SABTrayThread(SysTrayIconThread): def __init__(self): # Wait for translated texts to be loaded - while not api.check_trans(): + while not sabnzbd.WEBUI_READY: sleep(0.2) logging.debug('language file not loaded, waiting') diff --git a/sabnzbd/wizard.py b/sabnzbd/wizard.py index 882834a..429a286 100644 --- a/sabnzbd/wizard.py +++ b/sabnzbd/wizard.py @@ -77,7 +77,7 @@ class Wizard(object): language = kwargs.get('lang') cfg.language.set(language) set_language(language) - sabnzbd.api.cache_skin_trans() + sabnzbd.api.clear_trans_cache() # Always setup Plush sabnzbd.interface.change_web_dir('Plush - Gold')