Skip to content

Commit 4be7730

Browse files
authored
Merge pull request #701 from razzeee/add-typing-coverage-873585794030377872
2 parents 1706758 + b5ad474 commit 4be7730

File tree

14 files changed

+223
-173
lines changed

14 files changed

+223
-173
lines changed

resources/lib/deviceAuthDialog.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121

2222
class DeviceAuthDialog(xbmcgui.WindowXMLDialog):
23+
code: str
24+
url: str
2325

24-
def __init__(self, xmlFile, resourcePath, code, url):
26+
def __init__(self, xmlFile: str, resourcePath: str, code: str, url: str) -> None:
2527
self.code = code
2628
self.url = url
2729

28-
def onInit(self):
30+
def onInit(self) -> None:
2931
instuction = self.getControl(INSTRUCTION_LABEL)
3032
authcode = self.getControl(AUTHCODE_LABEL)
3133
warning = self.getControl(WARNING_LABEL)
@@ -34,17 +36,17 @@ def onInit(self):
3436
authcode.setLabel(self.code)
3537
warning.setLabel(getString(32162))
3638

37-
def onAction(self, action):
39+
def onAction(self, action: xbmcgui.Action) -> None:
3840
if action == ACTION_PREVIOUS_MENU or action == ACTION_BACK:
3941
self.close()
4042

41-
def onControl(self, control):
43+
def onControl(self, control: xbmcgui.Control) -> None:
4244
pass
4345

44-
def onFocus(self, control):
46+
def onFocus(self, control: xbmcgui.Control) -> None:
4547
pass
4648

47-
def onClick(self, control):
49+
def onClick(self, control: xbmcgui.Control) -> None:
4850
logger.debug('onClick: %s' % (control))
4951

5052
if control == LATER_BUTTON:

resources/lib/globals.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
22
#
3+
from typing import Any
34

4-
traktapi = None
5+
traktapi: Any = None

resources/lib/kodilogging.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
class KodiLogHandler(logging.StreamHandler):
2727

28-
def __init__(self):
28+
def __init__(self) -> None:
2929
logging.StreamHandler.__init__(self)
3030
addon_id = xbmcaddon.Addon().getAddonInfo('id')
3131
prefix = "[%s] " % addon_id
3232
formatter = logging.Formatter(prefix + '%(name)s: %(message)s')
3333
self.setFormatter(formatter)
3434

35-
def emit(self, record):
35+
def emit(self, record: logging.LogRecord) -> None:
3636
levels = {
3737
logging.CRITICAL: xbmc.LOGFATAL,
3838
logging.ERROR: xbmc.LOGERROR,
@@ -44,10 +44,10 @@ def emit(self, record):
4444
if getSettingAsBool('debug'):
4545
xbmc.log(self.format(record), levels[record.levelno])
4646

47-
def flush(self):
47+
def flush(self) -> None:
4848
pass
4949

50-
def config():
50+
def config() -> None:
5151
logger = logging.getLogger()
5252
logger.addHandler(KodiLogHandler())
5353
logger.setLevel(logging.DEBUG)

resources/lib/obfuscation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# -*- coding: utf-8 -*-
2+
from typing import List, Union
23

3-
def deobfuscate(data):
4+
5+
def deobfuscate(data: Union[List[int], str]) -> str:
46
if not data or not isinstance(data, list):
57
return ""
68
return "".join(chr(b ^ 0x42) for b in data)
79

8-
def obfuscate(data):
10+
def obfuscate(data: str) -> List[int]:
911
if not data:
1012
return []
1113
return [ord(c) ^ 0x42 for c in data]

resources/lib/rating.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import xbmcgui
66
from resources.lib import utilities
77
from resources.lib import kodiUtilities
8+
from typing import Dict, List, Optional, Any, Union
89
from resources.lib import globals
910
import logging
1011

@@ -13,7 +14,7 @@
1314
__addon__ = xbmcaddon.Addon("script.trakt")
1415

1516

16-
def ratingCheck(media_type, items_to_rate, watched_time, total_time):
17+
def ratingCheck(media_type: str, items_to_rate: List[Dict], watched_time: float, total_time: float) -> None:
1718
"""Check if a video should be rated and if so launches the rating dialog"""
1819
logger.debug("Rating Check called for '%s'" % media_type)
1920
if not kodiUtilities.getSettingAsBool("rate_%s" % media_type):
@@ -30,7 +31,7 @@ def ratingCheck(media_type, items_to_rate, watched_time, total_time):
3031
media_type, watched, kodiUtilities.getSettingAsFloat("rate_min_view_time")))
3132

3233

33-
def rateMedia(media_type, itemsToRate, unrate=False, rating=None):
34+
def rateMedia(media_type: str, itemsToRate: List[Dict], unrate: bool = False, rating: Optional[Union[int, str]] = None) -> None:
3435
"""Launches the rating dialog"""
3536
for summary_info in itemsToRate:
3637
if not utilities.isValidMediaType(media_type):
@@ -117,7 +118,7 @@ def rateMedia(media_type, itemsToRate, unrate=False, rating=None):
117118
rating = None
118119

119120

120-
def __rateOnTrakt(rating, media_type, media, unrate=False):
121+
def __rateOnTrakt(rating: Union[int, str], media_type: str, media: Dict, unrate: bool = False) -> None:
121122
logger.debug("Sending rating (%s) to Trakt.tv" % rating)
122123

123124
params = media
@@ -166,6 +167,12 @@ def __rateOnTrakt(rating, media_type, media, unrate=False):
166167

167168

168169
class RatingDialog(xbmcgui.WindowXMLDialog):
170+
media_type: str
171+
media: Dict
172+
rating: Optional[Union[int, str]]
173+
rerate: bool
174+
default_rating: int
175+
169176
buttons = {
170177
11030: 1,
171178
11031: 2,
@@ -192,17 +199,17 @@ class RatingDialog(xbmcgui.WindowXMLDialog):
192199
11039: 32027
193200
}
194201

195-
def __init__(self, xmlFile, resourcePath, media_type, media, rerate):
202+
def __init__(self, xmlFile: str, resourcePath: str, media_type: str, media: Dict, rerate: bool) -> None:
196203
self.media_type = media_type
197204
self.media = media
198205
self.rating = None
199206
self.rerate = rerate
200207
self.default_rating = kodiUtilities.getSettingAsInt('rating_default')
201208

202-
def __new__(cls, xmlFile, resourcePath, media_type, media, rerate):
209+
def __new__(cls, xmlFile: str, resourcePath: str, media_type: str, media: Dict, rerate: bool) -> Any:
203210
return super(RatingDialog, cls).__new__(cls, xmlFile, resourcePath)
204211

205-
def onInit(self):
212+
def onInit(self) -> None:
206213
s = utilities.getFormattedItemName(self.media_type, self.media)
207214
self.getControl(10012).setLabel(s)
208215

@@ -211,12 +218,12 @@ def onInit(self):
211218
rateID = 11029 + int(self.media['user']['ratings']['rating'])
212219
self.setFocus(self.getControl(rateID))
213220

214-
def onClick(self, controlID):
221+
def onClick(self, controlID: int) -> None:
215222
if controlID in self.buttons:
216223
self.rating = self.buttons[controlID]
217224
self.close()
218225

219-
def onFocus(self, controlID):
226+
def onFocus(self, controlID: int) -> None:
220227
if controlID in self.focus_labels:
221228
s = kodiUtilities.getString(self.focus_labels[controlID])
222229

resources/lib/script.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
from resources.lib import utilities
66
from resources.lib import kodiUtilities
77
from resources.lib import sqlitequeue
8+
from typing import Dict
89
from resources.lib.traktContextMenu import traktContextMenu
910

1011
logger = logging.getLogger(__name__)
1112

1213

13-
def __getArguments():
14+
def __getArguments() -> Dict:
1415
data = None
1516
default_actions = {0: "sync"}
1617
if len(sys.argv) == 1:
@@ -26,7 +27,7 @@ def __getArguments():
2627
return data
2728

2829

29-
def run():
30+
def run() -> None:
3031
args = __getArguments()
3132
data = {}
3233

resources/lib/scrobbler.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import xbmc
55
import time
66
import logging
7+
from typing import Dict, List, Optional, Any
78
from resources.lib import utilities
89
from resources.lib import kodiUtilities
910
import math
@@ -13,34 +14,34 @@
1314

1415

1516
class Scrobbler:
16-
traktapi = None
17-
isPlaying = False
18-
isPaused = False
19-
stopScrobbler = False
20-
isPVR = False
21-
isMultiPartEpisode = False
22-
lastMPCheck = 0
23-
curMPEpisode = 0
24-
videoDuration = 1
25-
watchedTime = 0
26-
pausedAt = 0
27-
curVideo = None
28-
curVideoInfo = None
29-
playlistIndex = 0
30-
traktShowSummary = None
31-
videosToRate = []
32-
33-
def __init__(self, api):
17+
traktapi: Any = None
18+
isPlaying: bool = False
19+
isPaused: bool = False
20+
stopScrobbler: bool = False
21+
isPVR: bool = False
22+
isMultiPartEpisode: bool = False
23+
lastMPCheck: float = 0
24+
curMPEpisode: int = 0
25+
videoDuration: float = 1
26+
watchedTime: float = 0
27+
pausedAt: float = 0
28+
curVideo: Optional[Dict] = None
29+
curVideoInfo: Optional[Dict] = None
30+
playlistIndex: int = 0
31+
traktShowSummary: Optional[Dict] = None
32+
videosToRate: List[Dict] = []
33+
34+
def __init__(self, api: Any) -> None:
3435
self.traktapi = api
3536

36-
def _currentEpisode(self, watchedPercent, episodeCount):
37+
def _currentEpisode(self, watchedPercent: float, episodeCount: int) -> int:
3738
split = 100 / episodeCount
3839
for i in range(episodeCount - 1, 0, -1):
3940
if watchedPercent >= (i * split):
4041
return i
4142
return 0
4243

43-
def transitionCheck(self, isSeek=False):
44+
def transitionCheck(self, isSeek: bool = False) -> None:
4445
if not xbmc.Player().isPlayingVideo():
4546
return
4647

@@ -198,7 +199,7 @@ def transitionCheck(self, isSeek=False):
198199
elif isSeek:
199200
self.__scrobble("start")
200201

201-
def playbackStarted(self, data):
202+
def playbackStarted(self, data: Dict) -> None:
202203
logger.debug("playbackStarted(data: %s)" % data)
203204
if not data:
204205
return
@@ -406,7 +407,7 @@ def playbackStarted(self, data):
406407

407408
self.__preFetchUserRatings(result)
408409

409-
def __preFetchUserRatings(self, result):
410+
def __preFetchUserRatings(self, result: Dict) -> None:
410411
if result:
411412
if utilities.isMovie(
412413
self.curVideo["type"]
@@ -439,7 +440,7 @@ def __preFetchUserRatings(self, result):
439440
}
440441
logger.debug("Pre-Fetch result: %s; Info: %s" % (result, self.curVideoInfo))
441442

442-
def playbackResumed(self):
443+
def playbackResumed(self) -> None:
443444
if not self.isPlaying or self.isPVR:
444445
return
445446

@@ -451,7 +452,7 @@ def playbackResumed(self):
451452
self.isPaused = False
452453
self.__scrobble("start")
453454

454-
def playbackPaused(self):
455+
def playbackPaused(self) -> None:
455456
if not self.isPlaying or self.isPVR:
456457
return
457458

@@ -461,14 +462,14 @@ def playbackPaused(self):
461462
self.pausedAt = time.time()
462463
self.__scrobble("pause")
463464

464-
def playbackSeek(self):
465+
def playbackSeek(self) -> None:
465466
if not self.isPlaying:
466467
return
467468

468469
logger.debug("playbackSeek()")
469470
self.transitionCheck(isSeek=True)
470471

471-
def playbackEnded(self):
472+
def playbackEnded(self) -> None:
472473
if not self.isPVR:
473474
self.videosToRate.append(self.curVideoInfo)
474475
if not self.isPlaying:
@@ -497,15 +498,15 @@ def playbackEnded(self):
497498
self.curVideo = None
498499
self.playlistIndex = 0
499500

500-
def __calculateWatchedPercent(self):
501+
def __calculateWatchedPercent(self) -> float:
501502
# we need to floor this, so this calculation yields the same result as the playback progress calculation
502503
floored = math.floor(self.videoDuration)
503504
if floored != 0:
504505
return (self.watchedTime / floored) * 100
505506
else:
506507
return 0
507508

508-
def __scrobble(self, status):
509+
def __scrobble(self, status: str) -> Optional[Dict]:
509510
if not self.curVideoInfo:
510511
return
511512

@@ -609,7 +610,7 @@ def __scrobble(self, status):
609610
% (self.traktShowSummary, self.curVideoInfo, watchedPercent, status)
610611
)
611612

612-
def __scrobbleNotification(self, info):
613+
def __scrobbleNotification(self, info: Dict) -> None:
613614
if not self.curVideoInfo:
614615
return
615616

0 commit comments

Comments
 (0)