PlexKodiConnect/resources/lib/library_sync/process_metadata.py

107 lines
3.8 KiB
Python
Raw Normal View History

2017-04-03 01:02:41 +10:00
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
2017-04-03 01:02:41 +10:00
from logging import getLogger
2018-10-20 23:49:04 +11:00
import xbmc
import xbmcgui
2017-04-03 01:02:41 +10:00
2018-10-20 23:49:04 +11:00
from . import common
from .. import utils, backgroundthread
2017-04-03 01:02:41 +10:00
2018-10-20 23:49:04 +11:00
LOG = getLogger('PLEX.library_sync.process_metadata')
2017-04-03 01:02:41 +10:00
2018-10-21 21:03:21 +11:00
class InitNewSection(object):
"""
Throw this into the queue used for ProcessMetadata to tell it which
Plex library section we're looking at
context: itemtypes.Movie, itemtypes.Episode, etc.
"""
def __init__(self, context, total_number_of_items, section_name,
section_id):
self.context = context
self.total = total_number_of_items
self.name = section_name
self.id = section_id
2018-10-20 23:49:04 +11:00
class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin):
2017-04-03 01:02:41 +10:00
"""
Not yet implemented for more than 1 thread - if ever. Only to be called by
ONE thread!
Processes the XML metadata in the queue
Input:
queue: Queue.Queue() object that you'll need to fill up with
the downloaded XML eTree objects
item_class: as used to call functions in itemtypes.py e.g. 'Movies' =>
2017-04-03 01:02:41 +10:00
itemtypes.Movies()
"""
2018-10-21 21:03:21 +11:00
def __init__(self, queue, last_sync):
2017-04-03 01:02:41 +10:00
self.queue = queue
2018-10-21 21:03:21 +11:00
self.last_sync = last_sync
self.total = 0
2018-10-20 23:49:04 +11:00
self.current = 0
self.title = None
2018-10-21 21:03:21 +11:00
self.section_name = None
2018-10-20 23:49:04 +11:00
super(ProcessMetadata, self).__init__()
2017-04-03 01:02:41 +10:00
2018-10-20 23:49:04 +11:00
def update_dialog(self):
2017-04-03 01:02:41 +10:00
"""
"""
2018-10-20 23:49:04 +11:00
try:
progress = int(float(self.current) / float(self.total) * 100.0)
except ZeroDivisionError:
progress = 0
self.dialog.update(progress,
2018-10-21 21:03:21 +11:00
self.section_name,
2018-10-20 23:49:04 +11:00
'%s/%s: %s'
% (self.current, self.total, self.title))
2017-04-03 01:02:41 +10:00
def run(self):
"""
Do the work
"""
LOG.debug('Processing thread started')
2018-10-20 23:49:04 +11:00
self.dialog = xbmcgui.DialogProgressBG()
self.dialog.create(utils.lang(39714))
2018-10-21 21:03:21 +11:00
try:
# Init with the very first library section. This will block!
section = self.queue.get()
self.queue.task_done()
if section is None:
return
2018-10-20 23:49:04 +11:00
while self.isCanceled() is False:
2018-10-21 21:03:21 +11:00
if section is None:
2018-10-20 23:49:04 +11:00
break
2018-10-21 21:03:21 +11:00
self.total = section.total
self.section_name = section.name
with section.context(self.last_sync) as context:
while self.isCanceled() is False:
# grabs item from queue
try:
xml = self.queue.get(block=False)
except backgroundthread.Queue.Empty:
xbmc.sleep(20)
continue
self.queue.task_done()
if xml is InitNewSection or xml is None:
section = xml
break
try:
context.add_update(xml[0],
viewtag=section.name,
viewid=section.id,
children=xml.children)
except:
utils.ERROR(txt='process_metadata crashed',
notify=True)
self.current += 1
if self.current % 20 == 0:
self.title = utils.cast(unicode,
xml[0].get('title'))
self.update_dialog()
finally:
self.dialog.close()
LOG.debug('Processing thread terminated')