#!/usr/bin/env python3
"""Patch: add xbmc.Monitor, fix xbmc.Player, add xbmcvfs.File to _XBMC block"""
import re

path = '/home/matthiasberner/Schreibtisch/kodi/xstreamDownloader/mini_app.py'
with open(path) as f:
    c = f.read()

old = """        _XBMC.Player = type('Player', (), {'play': lambda s, *a, **k: None, 'isPlayingVideo': lambda s: False})()
        sys.modules['xbmc'] = _XBMC"""

new = """        def _Player(*a, **kw):
            inst = types.SimpleNamespace()
            inst.play = lambda *a, **k: None
            inst.isPlayingVideo = lambda: False
            inst.isPlaying = lambda: False
            inst.getPlayingFile = lambda: ''
            inst.stop = lambda: None
            return inst
        def _Monitor(*a, **kw):
            inst = types.SimpleNamespace()
            inst.abortRequested = lambda: False
            inst.waitForAbort = lambda t=None: False
            return inst
        _XBMC.Player = _Player
        _XBMC.Monitor = _Monitor
        sys.modules['xbmc'] = _XBMC"""

if old in c and 'Monitor' not in c:
    c = c.replace(old, new)
    open(path, 'w').write(c)
    print("Patched OK")
else:
    print("Already patched or pattern not found")
