示例#1
0
def test_observer_checked_already_terminated_error():
    m, n = [0], [0]

    def on_next(x):
        m[0] += 1

    def on_error(x):
        n[0] += 1

    def on_completed():
        assert (False)

    o = Observer(on_next, on_error, on_completed).checked()

    o.on_next(1)
    o.on_next(2)
    o.on_error(Exception('error'))

    try:
        o.on_completed()
    except Exception:
        pass

    try:
        o.on_error(Exception('error'))
    except Exception:
        pass

    assert (2 == m[0])
    assert (1 == n[0])
示例#2
0
def test_create_on_next_on_completed():
    next = False
    completed = False

    def on_next(x):
        nonlocal next
        assert(42 == x)
        next = True
        return next

    def on_completed():
        nonlocal completed
        completed = True
        return completed

    res = Observer(on_next, None, on_completed)
    
    res.on_next(42)

    assert(next)
    assert(not completed)

    res.on_completed()

    assert(completed)
示例#3
0
def test_observer_checked_reentrant_completed():
    msg = "Re-entrancy detected"
    n = 0

    def on_next(x):
        assert(False)

    def on_error(ex):
        assert(False)

    def on_completed():
        nonlocal n
        n += 1
        try:
            o.on_next(9)
        except Exception as e:
            print (str(e))
            assert str(e) == msg

        try: 
            o.on_error(Exception('error'))
        except Exception as e:
            assert str(e) == msg

        try:
            o.on_completed()
        except Exception as e:
            assert str(e) == msg


    o = Observer(on_next, on_error, on_completed).checked()

    o.on_completed()
    assert(1 == n)
示例#4
0
def test_create_on_next_on_error_on_completed1():
    ex = 'ex'
    next = True
    error = False
    completed = False
    
    def on_next(x):
        nonlocal next
        assert(42 == x)
        next = True
    
    def on_error(e):
        nonlocal error
        assert(ex == e)
        error = True

    def on_completed():
        nonlocal completed
        completed = True

    res = Observer(on_next, on_error, on_completed)

    res.on_next(42)

    assert(next)
    assert(not error)
    assert(not completed)

    res.on_completed()

    assert(completed)
    assert(not error)
示例#5
0
def test_observer_checked_already_terminated_error():
    m, n = 0, 0

    def on_next(x):
        nonlocal m
        m += 1

    def on_error(x):
        nonlocal n
        n += 1

    def on_completed():
        assert(False)

    o = Observer(on_next, on_error, on_completed).checked()

    o.on_next(1)
    o.on_next(2)
    o.on_error(Exception('error'))

    try:
        o.on_completed()
    except Exception:
        pass

    try: 
        o.on_error(Exception('error'))
    except Exception:
        pass 

    assert(2 == m)
    assert(1 == n)
示例#6
0
def test_observer_checked_already_terminated_error():
    m, n = [0], [0]

    def on_next(x):
        m[0] += 1

    def on_error(x):
        n[0] += 1

    def on_completed():
        assert(False)

    o = Observer(on_next, on_error, on_completed).checked()

    o.on_next(1)
    o.on_next(2)
    o.on_error(Exception('error'))

    try:
        o.on_completed()
    except Exception:
        pass

    try: 
        o.on_error(Exception('error'))
    except Exception:
        pass 

    assert(2 == m[0])
    assert(1 == n[0])
示例#7
0
def test_observer_checked_reentrant_completed():
    msg = "Re-entrancy detected"
    n = [0]

    def on_next(x):
        assert (False)

    def on_error(ex):
        assert (False)

    def on_completed():
        n[0] += 1
        try:
            o.on_next(9)
        except Exception as e:
            print(str(e))
            assert str(e) == msg

        try:
            o.on_error(Exception('error'))
        except Exception as e:
            assert str(e) == msg

        try:
            o.on_completed()
        except Exception as e:
            assert str(e) == msg

    o = Observer(on_next, on_error, on_completed).checked()

    o.on_completed()
    assert (1 == n[0])
示例#8
0
def test_create_on_next_on_error_on_completed1():
    ex = 'ex'
    next = [True]
    error = [False]
    completed = [False]
    
    def on_next(x):
        assert(42 == x)
        next[0] = True
    
    def on_error(e):
        assert(ex == e)
        error[0] = True

    def on_completed():
        completed[0] = True

    res = Observer(on_next, on_error, on_completed)

    res.on_next(42)

    assert(next[0])
    assert(not error[0])
    assert(not completed[0])

    res.on_completed()

    assert(completed[0])
    assert(not error[0])
示例#9
0
def test_create_on_next_on_error_on_completed1():
    ex = 'ex'
    next = [True]
    error = [False]
    completed = [False]

    def on_next(x):
        assert (42 == x)
        next[0] = True

    def on_error(e):
        assert (ex == e)
        error[0] = True

    def on_completed():
        completed[0] = True

    res = Observer(on_next, on_error, on_completed)

    res.on_next(42)

    assert (next[0])
    assert (not error[0])
    assert (not completed[0])

    res.on_completed()

    assert (completed[0])
    assert (not error[0])
示例#10
0
    def __call__(self, observer: rx.Observer):
        """
        It emits an event to the observers with ParsedMediaItem and movie match candidate to fix match for that item.
        :param observer - an Observer to fix match.
        """
        try:
            observer.on_next((self._media, self._candidate))
            observer.on_completed()

        except BaseException as e:
            observer.on_error(e)
示例#11
0
 def _subscribe_core(self, observer: Observer):
     self.observer = observer
     # flush buffer
     for item in self.buffer:
         observer.on_next(item)
     self.buffer = None
     if self.error:
         observer.on_error(self.error)
     elif self.completed:
         observer.on_completed()
     return AnonymousDisposable.create(self.dispose)
示例#12
0
    def __call__(self, observer: rx.Observer):
        """
        It emits an event to the observers with UnmatchedItem and movie match candidate to match with that item.
        :param observer - an Observer to handle actual match.
        """
        try:

            observer.on_next((self._media, self._candidate))
            observer.on_completed()

        except BaseException as e:
            observer.on_error(e)
示例#13
0
    def run(self, observer: rx.Observer):
        # Is it a generator?
        try:
            if (inspect.isgeneratorfunction(self.call)):
                for item in self.call(*self.args, **self.kwargs):
                    observer.on_next(item)

            else:
                observer.on_next(self.call(*self.args, **self.kwargs))

        except Exception as e:
            observer.on_error(e)

        observer.on_completed()
示例#14
0
def test_create_on_next():
    next = [False]
    def on_next(x):
        assert(42 == x)
        next[0] = True

    res = Observer(on_next)
    
    res.on_next(42)
    assert(next[0])
    return res.on_completed()
示例#15
0
def test_create_on_next():
    next = False
    def on_next(x):
        nonlocal next
        assert(42 == x)
        next = True

    res = Observer(on_next)
    
    res.on_next(42)
    assert(next)
    return res.on_completed()
示例#16
0
def test_create_on_next():
    next = [False]

    def on_next(x):
        assert (42 == x)
        next[0] = True

    res = Observer(on_next)

    res.on_next(42)
    assert (next[0])
    return res.on_completed()
示例#17
0
def test_create_on_next_on_completed():
    next = [False]
    completed = [False]

    def on_next(x):
        assert(42 == x)
        next[0] = True
        return next[0]

    def on_completed():
        completed[0] = True
        return completed[0]

    res = Observer(on_next, None, on_completed)
    
    res.on_next(42)

    assert(next[0])
    assert(not completed[0])

    res.on_completed()

    assert(completed[0])
    def subscribe(self,
                  on_next=None,
                  on_error=None,
                  on_completed=None,
                  observer: Observer = None):
        p = self.publisher
        if self.is_disposed is True:
            if observer is not None:
                observer.on_completed()
            return AnonymousDisposable(noop)

        if p is not None:
            subscription = p.subscribe(on_next=on_next,
                                       on_completed=on_completed,
                                       on_error=on_error,
                                       observer=observer)
            if observer is not None:
                observer.on_next(self.value)
            return subscription
        else:
            if observer is not None:
                observer.on_completed()
            return AnonymousDisposable(noop)
示例#19
0
def test_create_on_next_on_completed():
    next = [False]
    completed = [False]

    def on_next(x):
        assert (42 == x)
        next[0] = True
        return next[0]

    def on_completed():
        completed[0] = True
        return completed[0]

    res = Observer(on_next, None, on_completed)

    res.on_next(42)

    assert (next[0])
    assert (not completed[0])

    res.on_completed()

    assert (completed[0])
示例#20
0
    def __call__(self, observer: rx.Observer):
        def is_supported_filetype(path, file):
            # noinspection PyBroadException
            try:
                f = os.path.join(path, file)
                self.logger.debug(
                    f"Checking if {f} is supported by this scanner...")
                return filetype.video(f) is not None
            except:
                return False

        self.logger.info(
            f"Starting scan job: {self._job_context.id()} for a folder: {self._media_scan_path}"
        )

        for current_dir_name, subdirectories, files in self._media_source:
            for f in files:

                media = self.media_parser.parse(f)

                # noinspection PyBroadException
                try:
                    if not self.skip_filetype_checks and \
                            not is_supported_filetype(current_dir_name, f):
                        self.logger.info(
                            f'File {f} is not supported by this scanner. Skipping.'
                        )
                    else:
                        self.logger.debug(f"Processing {f}...")
                        media = ParsedMediaItem(
                            filename=f,
                            path=current_dir_name,
                            audio=media.get("audio"),
                            codec=media.get("codec"),
                            container=media.get("container"),
                            episode=media.get("episode"),
                            episodeName=media.get("episodeName"),
                            garbage=media.get("garbage"),
                            group=media.get("group"),
                            hardcoded=media.get("hardcoded"),
                            language=media.get("language"),
                            proper=media.get("proper"),
                            quality=media.get("quality"),
                            region=media.get("region"),
                            repack=media.get("repack"),
                            resolution=media.get("resolution"),
                            season=media.get("season"),
                            title=media.get("title"),
                            website=media.get("website"),
                            widescreen=media.get("widescreen"),
                            year=media.get("year"))

                        observer.on_next(media)

                except BaseException as e:
                    self.logger("FileSystemMediaScanner caught exception",
                                traceback.format_exc())
                    observer.on_error(e)

        self.logger.info(
            f"Completed file scan job: {self._job_context.id()} for a folder {self._media_scan_path}"
        )
        observer.on_completed()