Optimize imports for utils.py

This commit is contained in:
tomkat83 2017-01-29 13:52:46 +01:00
parent 289690813a
commit c17ebf0647

View file

@ -2,18 +2,18 @@
############################################################################### ###############################################################################
import logging import logging
import cProfile from cProfile import Profile
import json from json import loads, dumps
import pstats from pstats import Stats
import sqlite3 from sqlite3 import connect, OperationalError
from datetime import datetime, timedelta from datetime import datetime, timedelta
import StringIO from StringIO import StringIO
import time from time import localtime, strftime, strptime
import unicodedata from unicodedata import normalize
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
from functools import wraps from functools import wraps
from calendar import timegm from calendar import timegm
import os from os import path as os_path
import xbmc import xbmc
import xbmcaddon import xbmcaddon
@ -192,8 +192,8 @@ def DateToKodi(stamp):
""" """
try: try:
stamp = float(stamp) + float(window('kodiplextimeoffset')) stamp = float(stamp) + float(window('kodiplextimeoffset'))
date_time = time.localtime(stamp) date_time = localtime(stamp)
localdate = time.strftime('%Y-%m-%d %H:%M:%S', date_time) localdate = strftime('%Y-%m-%d %H:%M:%S', date_time)
except: except:
localdate = None localdate = None
return localdate return localdate
@ -207,7 +207,7 @@ def IfExists(path):
Returns True if path exists, else false Returns True if path exists, else false
""" """
dummyfile = tryEncode(os.path.join(path, 'dummyfile.txt')) dummyfile = tryEncode(os_path.join(path, 'dummyfile.txt'))
try: try:
etree.ElementTree(etree.Element('test')).write(dummyfile) etree.ElementTree(etree.Element('test')).write(dummyfile)
except: except:
@ -255,7 +255,7 @@ def kodiSQL(media_type="video"):
dbPath = DB_TEXTURE_PATH dbPath = DB_TEXTURE_PATH
else: else:
dbPath = DB_VIDEO_PATH dbPath = DB_VIDEO_PATH
return sqlite3.connect(dbPath, timeout=60.0) return connect(dbPath, timeout=60.0)
def create_actor_db_index(): def create_actor_db_index():
@ -269,7 +269,7 @@ def create_actor_db_index():
CREATE UNIQUE INDEX index_name CREATE UNIQUE INDEX index_name
ON actor (name); ON actor (name);
""") """)
except sqlite3.OperationalError: except OperationalError:
# Index already exists # Index already exists
pass pass
conn.commit() conn.commit()
@ -288,7 +288,7 @@ def getScreensaver():
'setting': "screensaver.mode" 'setting': "screensaver.mode"
} }
} }
return json.loads(xbmc.executeJSONRPC(json.dumps(query)))['result']['value'] return loads(xbmc.executeJSONRPC(dumps(query)))['result']['value']
def setScreensaver(value): def setScreensaver(value):
# Toggle the screensaver # Toggle the screensaver
@ -304,7 +304,7 @@ def setScreensaver(value):
} }
} }
log.debug("Toggling screensaver: %s %s" log.debug("Toggling screensaver: %s %s"
% (value, xbmc.executeJSONRPC(json.dumps(query)))) % (value, xbmc.executeJSONRPC(dumps(query))))
def reset(): def reset():
@ -382,12 +382,12 @@ def reset():
for dir in allDirs: for dir in allDirs:
allDirs, allFiles = xbmcvfs.listdir(path+dir) allDirs, allFiles = xbmcvfs.listdir(path+dir)
for file in allFiles: for file in allFiles:
if os.path.supports_unicode_filenames: if os_path.supports_unicode_filenames:
xbmcvfs.delete(os.path.join( xbmcvfs.delete(os_path.join(
path + tryDecode(dir), path + tryDecode(dir),
tryDecode(file))) tryDecode(file)))
else: else:
xbmcvfs.delete(os.path.join( xbmcvfs.delete(os_path.join(
tryEncode(path) + dir, tryEncode(path) + dir,
file)) file))
@ -426,14 +426,14 @@ def profiling(sortby="cumulative"):
def decorator(func): def decorator(func):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
pr = cProfile.Profile() pr = Profile()
pr.enable() pr.enable()
result = func(*args, **kwargs) result = func(*args, **kwargs)
pr.disable() pr.disable()
s = StringIO.StringIO() s = StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps = Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats() ps.print_stats()
log.info(s.getvalue()) log.info(s.getvalue())
@ -448,7 +448,7 @@ def convertdate(date):
except TypeError: except TypeError:
# TypeError: attribute of type 'NoneType' is not callable # TypeError: attribute of type 'NoneType' is not callable
# Known Kodi/python error # Known Kodi/python error
date = datetime(*(time.strptime(date, "%Y-%m-%dT%H:%M:%SZ")[0:6])) date = datetime(*(strptime(date, "%Y-%m-%dT%H:%M:%SZ")[0:6]))
return date return date
@ -468,7 +468,7 @@ def normalize_nodes(text):
# Remove dots from the last character as windows can not have directories # Remove dots from the last character as windows can not have directories
# with dots at the end # with dots at the end
text = text.rstrip('.') text = text.rstrip('.')
text = tryEncode(unicodedata.normalize('NFKD', unicode(text, 'utf-8'))) text = tryEncode(normalize('NFKD', unicode(text, 'utf-8')))
return text return text
@ -487,7 +487,7 @@ def normalize_string(text):
# Remove dots from the last character as windows can not have directories # Remove dots from the last character as windows can not have directories
# with dots at the end # with dots at the end
text = text.rstrip('.') text = text.rstrip('.')
text = tryEncode(unicodedata.normalize('NFKD', unicode(text, 'utf-8'))) text = tryEncode(normalize('NFKD', unicode(text, 'utf-8')))
return text return text
@ -1017,8 +1017,8 @@ def changePlayState(itemType, kodiId, playCount, lastplayed):
} }
query['method'] = method[itemType] query['method'] = method[itemType]
query['params'] = params[itemType] query['params'] = params[itemType]
result = xbmc.executeJSONRPC(json.dumps(query)) result = xbmc.executeJSONRPC(dumps(query))
result = json.loads(result) result = loads(result)
result = result.get('result') result = result.get('result')
log.debug("JSON result was: %s" % result) log.debug("JSON result was: %s" % result)
@ -1040,8 +1040,8 @@ class JSONRPC(object):
} }
if self.params is not None: if self.params is not None:
query['params'] = self.params query['params'] = self.params
return json.dumps(query) return dumps(query)
def execute(self, params=None): def execute(self, params=None):
self.params = params self.params = params
return json.loads(xbmc.executeJSONRPC(self._query())) return loads(xbmc.executeJSONRPC(self._query()))