From 7a5a5eb005449522ce235df119632ff4d49900c4 Mon Sep 17 00:00:00 2001 From: Pavion Date: Mon, 19 May 2014 21:39:06 +0200 Subject: [PATCH] Syno unicode fixes --- bottle.py | 40 ++++++++++++++++++++++++------ sql.py | 6 +++-- synology/scripts/start-stop-status | 2 +- tvstreamrecord.py | 32 +++++++----------------- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/bottle.py b/bottle.py index 3f4f0de..5e32209 100644 --- a/bottle.py +++ b/bottle.py @@ -19,8 +19,9 @@ __version__ = '0.13-dev' __license__ = 'MIT' -# The gevent server adapter needs to patch some modules before they are imported -# This is why we parse the commandline parameters here but handle them later +# The gevent and eventlet server adapters need to patch some modules before +# they are imported. This is why we parse the commandline parameters here but +# handle them later if __name__ == '__main__': from optparse import OptionParser _cmd_parser = OptionParser(usage="usage: %prog [options] package.module:app") @@ -32,8 +33,11 @@ _opt("--debug", action="store_true", help="start server in debug mode.") _opt("--reload", action="store_true", help="auto-reload on file changes.") _cmd_options, _cmd_args = _cmd_parser.parse_args() - if _cmd_options.server and _cmd_options.server.startswith('gevent'): - import gevent.monkey; gevent.monkey.patch_all() + if _cmd_options.server: + if _cmd_options.server.startswith('gevent'): + import gevent.monkey; gevent.monkey.patch_all() + elif _cmd_options.server.startswith('eventlet'): + import eventlet; eventlet.monkey_patch() import base64, cgi, email.utils, functools, hmac, imp, itertools, mimetypes,\ os, re, subprocess, sys, tempfile, threading, time, warnings @@ -1229,6 +1233,7 @@ def POST(self): elif py3k: args['encoding'] = 'utf8' data = cgi.FieldStorage(**args) + self['_cgi.FieldStorage'] = data #http://bugs.python.org/issue18394#msg207958 data = data.list or [] for item in data: if item.filename: @@ -2819,15 +2824,32 @@ def load(self): class EventletServer(ServerAdapter): - """ Untested """ + """ Untested. Options: + + * `backlog` adjust the eventlet backlog parameter which is the maximum + number of queued connections. Should be at least 1; the maximum + value is system-dependent. + * `family`: (default is 2) socket family, optional. See socket + documentation for available families. + """ def run(self, handler): - from eventlet import wsgi, listen + from eventlet import wsgi, listen, patcher + if not patcher.is_monkey_patched(os): + msg = "Bottle requires eventlet.monkey_patch() (before import)" + raise RuntimeError(msg) + socket_args = {} + for arg in ('backlog', 'family'): + try: + socket_args[arg] = self.options.pop(arg) + except KeyError: + pass + address = (self.host, self.port) try: - wsgi.server(listen((self.host, self.port)), handler, + wsgi.server(listen(address, **socket_args), handler, log_output=(not self.quiet)) except TypeError: # Fallback, if we have old version of eventlet - wsgi.server(listen((self.host, self.port)), handler) + wsgi.server(listen(address), handler) class RocketServer(ServerAdapter): @@ -2974,6 +2996,8 @@ def run(app=None, server='wsgiref', host='127.0.0.1', port=8080, raise ValueError("Application is not callable: %r" % app) for plugin in plugins or []: + if isinstance(plugin, basestring): + plugin = load(plugin) app.install(plugin) if server in server_names: diff --git a/sql.py b/sql.py index f399f3d..9964c67 100644 --- a/sql.py +++ b/sql.py @@ -26,7 +26,8 @@ def sqlRun(sql, t=-1, many=0): conn = sqlite3.connect('settings.db') c = conn.cursor() c.execute('PRAGMA journal_mode = OFF;') - #conn.text_factory = str + conn.text_factory = sqlite3.OptimizedUnicode + #print (conn.text_factory) if t != -1: if many == 1: rows = c.executemany(sql, t) @@ -41,7 +42,8 @@ def sqlRun(sql, t=-1, many=0): conn.commit() conn.close() except Exception as ex: - print ("exception: %s" % sql) + print ("SQL Exception: %s" % sql) +# print (ex) pass return fa diff --git a/synology/scripts/start-stop-status b/synology/scripts/start-stop-status index 6af12a5..47e30cb 100644 --- a/synology/scripts/start-stop-status +++ b/synology/scripts/start-stop-status @@ -37,7 +37,7 @@ case $1 in cd ${SYNOPKG_PKGDEST} findpython echo `date +"%Y-%m-%d %H:%M:%S"` "OUT Using Python (${TVSTREAMRECORD_PYTHON})..." >> ${SYNOPKG_PKGDEST}/log.txt - nohup ${TVSTREAMRECORD_PYTHON} tvstreamrecord.py >/dev/null 2>&1 & + nohup /usr/bin/env LANG="en_US.utf8" ${TVSTREAMRECORD_PYTHON} tvstreamrecord.py >/dev/null 2>&1 & ln -s "${SYNOPKG_PKGDEST}/webman" /usr/syno/synoman/webman/3rdparty/tvstreamrecord exit 0 ;; diff --git a/tvstreamrecord.py b/tvstreamrecord.py index 37e4887..71e851e 100644 --- a/tvstreamrecord.py +++ b/tvstreamrecord.py @@ -200,10 +200,9 @@ def internationalize(templ): def fileexists(file): try: - file = file.encode('utf-8') + return os.path.isfile(file) except Exception as ex: - print (ex) - return os.path.isfile(file) + return os.path.isfile(file.encode('utf-8').decode(sys.getfilesystemencoding())) #------------------------------- Main menu ------------------------------- @@ -684,15 +683,15 @@ def epglist_getter(): if iSearch and iSearch!="": sWhere = "AND (guide_chan.g_name LIKE '%" + iSearch + "%' OR guide.g_title LIKE '%" + iSearch + "%' OR guide.g_desc LIKE '%" + iSearch + "%')" - query = "SELECT guide_chan.g_name, guide.g_title, guide.g_desc, guide.g_start, guide.g_stop, (records.renabled is not null and records.renabled = 1), guide.rowid FROM ((guide INNER JOIN guide_chan ON guide.g_id = guide_chan.g_id) INNER JOIN channels ON channels.cname=guide_chan.g_name) LEFT JOIN records ON records.cid=channels.cid AND datetime(guide.g_start, '-%s minutes')=records.rvon and datetime(guide.g_stop, '+%s minutes')=records.rbis WHERE datetime(guide.g_stop)>datetime('now', 'localtime') AND channels.cenabled<>0 %s %s %s" % (config.cfg_delta_for_epg, config.cfg_delta_for_epg, sWhere, sOrder, sLimit) - countquery = "SELECT COUNT(guide.g_start) FROM ((guide INNER JOIN guide_chan ON guide.g_id = guide_chan.g_id) INNER JOIN channels ON channels.cname=guide_chan.g_name) LEFT JOIN records ON records.cid=channels.cid AND datetime(guide.g_start, '-%s minutes')=records.rvon and datetime(guide.g_stop, '+%s minutes')=records.rbis WHERE datetime(guide.g_stop)>datetime('now', 'localtime') AND channels.cenabled<>0 %s" % (config.cfg_delta_for_epg, config.cfg_delta_for_epg, sWhere) + query = "SELECT guide_chan.g_name, guide.g_title, guide.g_desc, guide.g_start, guide.g_stop, (records.renabled is not null and records.renabled = 1), guide.rowid FROM guide INNER JOIN guide_chan ON guide.g_id = guide_chan.g_id INNER JOIN channels ON channels.cname=guide_chan.g_name LEFT JOIN records ON records.cid=channels.cid AND datetime(guide.g_start, '-%s minutes')=records.rvon and datetime(guide.g_stop, '+%s minutes')=records.rbis WHERE datetime(guide.g_stop)>datetime('now', 'localtime') AND channels.cenabled<>0 %s %s %s" % (config.cfg_delta_for_epg, config.cfg_delta_for_epg, sWhere, sOrder, sLimit) + countquery = "SELECT COUNT(guide.g_start) FROM guide INNER JOIN guide_chan ON guide.g_id = guide_chan.g_id INNER JOIN channels ON channels.cname=guide_chan.g_name LEFT JOIN records ON records.cid=channels.cid AND datetime(guide.g_start, '-%s minutes')=records.rvon and datetime(guide.g_stop, '+%s minutes')=records.rbis WHERE datetime(guide.g_stop)>datetime('now', 'localtime') AND channels.cenabled<>0 %s" % (config.cfg_delta_for_epg, config.cfg_delta_for_epg, sWhere) count = sqlRun(countquery) if count: totalrows = count[0][0] rows=sqlRun(query) else: # Client-side processing - rows=sqlRun("SELECT guide_chan.g_name, guide.g_title, guide.g_desc, guide.g_start, guide.g_stop, (records.renabled is not null and records.renabled = 1), guide.rowid FROM ((guide INNER JOIN guide_chan ON guide.g_id = guide_chan.g_id) INNER JOIN channels ON channels.cname=guide_chan.g_name) LEFT JOIN records ON records.cid=channels.cid AND datetime(guide.g_start, '-%s minutes')=records.rvon and datetime(guide.g_stop, '+%s minutes')=records.rbis WHERE datetime(guide.g_stop)>datetime('now', 'localtime') AND channels.cenabled<>0 ORDER BY g_start LIMIT %s;" % (config.cfg_delta_for_epg, config.cfg_delta_for_epg, config.cfg_epg_max_events)) + rows=sqlRun("SELECT guide_chan.g_name, guide.g_title, guide.g_desc, guide.g_start, guide.g_stop, (records.renabled is not null and records.renabled = 1), guide.rowid FROM guide INNER JOIN guide_chan ON guide.g_id = guide_chan.g_id INNER JOIN channels ON channels.cname=guide_chan.g_name LEFT JOIN records ON records.cid=channels.cid AND datetime(guide.g_start, '-%s minutes')=records.rvon and datetime(guide.g_stop, '+%s minutes')=records.rbis WHERE datetime(guide.g_stop)>datetime('now', 'localtime') AND channels.cenabled<>0 ORDER BY g_start LIMIT %s;" % (config.cfg_delta_for_epg, config.cfg_delta_for_epg, config.cfg_epg_max_events)) for row in rows: retlist.append([row[0], row[1], row[2], row[3], row[4], row[5], row[6]]) @@ -794,14 +793,6 @@ def __init__(self, row): self.von = datetime.strptime(row[2],"%Y-%m-%d %H:%M:%S") self.bis = datetime.strptime(row[3],"%Y-%m-%d %H:%M:%S") self.name = row[5] -# print ("<< c: %s; t: %s; l: %s" % (type(row[5]), type(self.name), len(self.name))) -# try: -# print ("a %s" % self.name) -# self.name = self.name.decode("UTF-8") -# except: -# print ("b") -# pass -# print (">> c: %s; t: %s; l: %s" % (type(row[5]), type(self.name), len(self.name))) self.url = row[1].strip() self.mask = row[6] self.myrow = row @@ -832,10 +823,6 @@ def doRecord(self): self.running = 1 dateholder = datetime.now().strftime("%Y%m%d%H%M%S") titleholder = "".join([x if x.isalnum() else "_" for x in self.name]) - #try: - # titleholder = titleholder.encode('ascii', 'replace') - #except: - # pass fn = config.cfg_recordpath + config.cfg_record_mask.replace("%date%", dateholder).replace("%title%", titleholder) + self.ext num = 1 while fileexists(fn) and num<127: @@ -865,13 +852,12 @@ def doRecord(self): else: block_sz = 8192 print ("Record: '%s' started" % (self.name)) - try: - fn = fn.encode('utf-8') - except Exception as ex: - print (ex) try: u = urllib32.urlopen(self.url) - f = open(fn, 'wb') + try: + f = open(fn, 'wb') + except: + f = open(fn.encode('utf-8').decode(sys.getfilesystemencoding()), 'wb') except urllib32.URLError: print ("Stream could not be parsed (URL=%s), aborting..." % (self.url)) pass