Skip to content

Commit 1706758

Browse files
authored
Merge pull request #700 from razzeee/fix-bugs-and-improve-typing-4782863379123261006
2 parents dc20873 + 9a1bfb4 commit 1706758

4 files changed

Lines changed: 88 additions & 85 deletions

File tree

resources/lib/kodiUtilities.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88
import re
99
import logging
10+
from typing import Tuple, List, Dict, Union, Optional
1011
from resources.lib import utilities
1112

1213

@@ -19,46 +20,46 @@
1920

2021

2122
def notification(
22-
header: str, message: str, time=5000, icon=__addon__.getAddonInfo("icon")
23-
):
23+
header: str, message: str, time: int = 5000, icon: str = __addon__.getAddonInfo("icon")
24+
) -> None:
2425
xbmcgui.Dialog().notification(header, message, icon, time)
2526

2627

27-
def showSettings():
28+
def showSettings() -> None:
2829
__addon__.openSettings()
2930

3031

31-
def getSetting(setting):
32+
def getSetting(setting: str) -> str:
3233
return __addon__.getSetting(setting).strip()
3334

3435

35-
def setSetting(setting, value):
36+
def setSetting(setting: str, value: Union[str, int, float, bool]) -> None:
3637
__addon__.setSetting(setting, str(value))
3738

3839

39-
def getSettingAsBool(setting):
40+
def getSettingAsBool(setting: str) -> bool:
4041
return getSetting(setting).lower() == "true"
4142

4243

43-
def getSettingAsFloat(setting):
44+
def getSettingAsFloat(setting: str) -> float:
4445
try:
4546
return float(getSetting(setting))
4647
except ValueError:
4748
return 0
4849

4950

50-
def getSettingAsInt(setting):
51+
def getSettingAsInt(setting: str) -> int:
5152
try:
5253
return int(getSettingAsFloat(setting))
5354
except ValueError:
5455
return 0
5556

5657

57-
def getString(string_id):
58+
def getString(string_id: int) -> str:
5859
return __addon__.getLocalizedString(string_id)
5960

6061

61-
def kodiJsonRequest(params):
62+
def kodiJsonRequest(params: Dict) -> Optional[Union[Dict, List]]:
6263
data = json.dumps(params)
6364
request = xbmc.executeJSONRPC(data)
6465

@@ -131,7 +132,7 @@ def checkExclusion(fullpath: str) -> bool:
131132
return found
132133

133134

134-
def kodiRpcToTraktMediaObject(type, data, mode="collected"):
135+
def kodiRpcToTraktMediaObject(type: str, data: Dict, mode: str = "collected") -> Optional[Dict]:
135136
if type == "show":
136137
if "uniqueid" in data:
137138
data["ids"] = data.pop("uniqueid")
@@ -230,7 +231,7 @@ def kodiRpcToTraktMediaObject(type, data, mode="collected"):
230231
return
231232

232233

233-
def kodiRpcToTraktMediaObjects(data, mode="collected"):
234+
def kodiRpcToTraktMediaObjects(data: Dict, mode: str = "collected") -> Optional[List]:
234235
if "tvshows" in data:
235236
shows = data["tvshows"]
236237

@@ -270,7 +271,7 @@ def kodiRpcToTraktMediaObjects(data, mode="collected"):
270271
return
271272

272273

273-
def getShowDetailsFromKodi(showID, fields):
274+
def getShowDetailsFromKodi(showID: int, fields: List) -> Optional[Dict]:
274275
result = kodiJsonRequest(
275276
{
276277
"jsonrpc": "2.0",
@@ -292,7 +293,7 @@ def getShowDetailsFromKodi(showID, fields):
292293
return None
293294

294295

295-
def getSeasonDetailsFromKodi(seasonID, fields):
296+
def getSeasonDetailsFromKodi(seasonID: int, fields: List) -> Optional[Dict]:
296297
result = kodiJsonRequest(
297298
{
298299
"jsonrpc": "2.0",
@@ -317,7 +318,7 @@ def getSeasonDetailsFromKodi(seasonID, fields):
317318
# get a single episode from kodi given the id
318319

319320

320-
def getEpisodeDetailsFromKodi(libraryId, fields):
321+
def getEpisodeDetailsFromKodi(libraryId: int, fields: List) -> Optional[Dict]:
321322
result = kodiJsonRequest(
322323
{
323324
"jsonrpc": "2.0",
@@ -358,7 +359,7 @@ def getEpisodeDetailsFromKodi(libraryId, fields):
358359
# get a single movie from kodi given the id
359360

360361

361-
def getMovieDetailsFromKodi(libraryId, fields):
362+
def getMovieDetailsFromKodi(libraryId: int, fields: List) -> Optional[Dict]:
362363
result = kodiJsonRequest(
363364
{
364365
"jsonrpc": "2.0",
@@ -380,7 +381,7 @@ def getMovieDetailsFromKodi(libraryId, fields):
380381
return None
381382

382383

383-
def checkAndConfigureProxy():
384+
def checkAndConfigureProxy() -> Optional[str]:
384385
proxyActive = kodiJsonRequest(
385386
{
386387
"jsonrpc": "2.0",
@@ -480,7 +481,7 @@ def checkAndConfigureProxy():
480481
return None
481482

482483

483-
def getMediaType():
484+
def getMediaType() -> Optional[str]:
484485
listType = xbmc.getInfoLabel("ListItem.DBTYPE")
485486

486487
xbmc.log("list item type: %s" % listType, xbmc.LOGINFO)
@@ -497,7 +498,7 @@ def getMediaType():
497498
return None
498499

499500

500-
def getInfoLabelDetails(result):
501+
def getInfoLabelDetails(result: Dict) -> Tuple[str, Dict]:
501502
type = result["item"]["type"]
502503
data = {"action": "started"}
503504
# check type of item

resources/lib/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def __init__(self, *args, **kwargs):
433433

434434
def onNotification(self, sender, method, data):
435435
# method looks like Other.NEXTUPWATCHEDSIGNAL
436-
if method.split(".")[1].upper() != "NEXTUPWATCHEDSIGNAL":
436+
if "." not in method or method.split(".")[1].upper() != "NEXTUPWATCHEDSIGNAL":
437437
return
438438

439439
logger.debug("Callback received - Upnext skipped to the next episode")

resources/lib/sqlitequeue.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import xbmcvfs
1515
import xbmcaddon
1616
import logging
17+
from typing import Any, Iterator, Optional
1718

1819
logger = logging.getLogger(__name__)
1920

@@ -59,27 +60,27 @@ def __len__(self) -> int:
5960
executed = conn.execute(self._count).fetchone()[0]
6061
return executed
6162

62-
def __iter__(self):
63+
def __iter__(self) -> Iterator[Any]:
6364
with self._get_conn() as conn:
6465
for _, obj_buffer in conn.execute(self._iterate):
65-
yield loads(str(obj_buffer))
66+
yield loads(obj_buffer)
6667

67-
def _get_conn(self):
68+
def _get_conn(self) -> sqlite3.Connection:
6869
id = get_ident()
6970
if id not in self._connection_cache:
7071
self._connection_cache[id] = sqlite3.Connection(self.path, timeout=60)
7172
return self._connection_cache[id]
7273

73-
def purge(self):
74+
def purge(self) -> None:
7475
with self._get_conn() as conn:
7576
conn.execute(self._purge)
7677

77-
def append(self, obj):
78+
def append(self, obj: Any) -> None:
7879
obj_buffer = dumps(obj)
7980
with self._get_conn() as conn:
8081
conn.execute(self._append, (obj_buffer,))
8182

82-
def get(self, sleep_wait=True):
83+
def get(self, sleep_wait: bool = True) -> Optional[Any]:
8384
keep_pooling = True
8485
wait = 0.1
8586
max_wait = 2
@@ -89,10 +90,11 @@ def get(self, sleep_wait=True):
8990
while keep_pooling:
9091
conn.execute(self._write_lock)
9192
cursor = conn.execute(self._get)
92-
try:
93-
id, obj_buffer = cursor.fetchone()
93+
row = cursor.fetchone()
94+
if row:
95+
id, obj_buffer = row
9496
keep_pooling = False
95-
except StopIteration:
97+
else:
9698
conn.commit() # unlock the database
9799
if not sleep_wait:
98100
keep_pooling = False
@@ -102,13 +104,13 @@ def get(self, sleep_wait=True):
102104
wait = min(max_wait, tries / 10 + wait)
103105
if id:
104106
conn.execute(self._del, (id,))
105-
return loads(str(obj_buffer))
107+
return loads(obj_buffer)
106108
return None
107109

108-
def peek(self):
110+
def peek(self) -> Optional[Any]:
109111
with self._get_conn() as conn:
110112
cursor = conn.execute(self._peek)
111-
try:
112-
return loads(str(cursor[0]))
113-
except StopIteration:
114-
return None
113+
row = cursor.fetchone()
114+
if row:
115+
return loads(row[0])
116+
return None

0 commit comments

Comments
 (0)