Пример #1
0
def test_observer_checked_already_terminated_completed():
    m, n = 0, 0

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

    def on_error(x):
        assert(False)

    def on_completed():
        nonlocal n
        n += 1

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

    o.on_next(1)
    o.on_next(2)
    o.on_completed()

    assert_raises(CompletedException, o.on_completed)
    
    try:  
        on.on_error(Exception('error'))
    except Exception:
        pass

    assert(2 == m)
    assert(1 == n)
Пример #2
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])
Пример #3
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)
Пример #4
0
def test_observer_checked_reentrant_error():
    msg = "Re-entrancy detected"
    n = [0]
    
    def on_next(x):
        assert(False)
        
    def on_error(ex):
        n[0] += 1

        try:
            o.on_next(9)
        except Exception as 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

    def on_completed():
        assert(False)

    o = Observer(on_next, on_error, on_completed).checked()
    o.on_error(Exception('error'))
    assert(1 == n[0])
Пример #5
0
def test_create_on_next_on_completed_has_error():
    e_ = None
    ex = 'ex'
    next = [False]
    completed = [False]


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

    def on_completed():
        completed[0] = True

    res = Observer(on_next, None, on_completed)
    
    res.on_next(42)
    assert(next[0])
    assert(not completed[0])
    try:
        res.on_error(ex)
        assert(False)
    except Exception as e:
        e_ = e.args[0]
    
    assert(ex == e_)
    assert(not completed[0])
Пример #6
0
def test_create_on_next_on_error_on_completed2():
    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_error(ex)
    
    assert(not completed[0])
    assert(error[0])
Пример #7
0
def test_observer_checked_reentrant_error():
    msg = "Re-entrancy detected"
    n = 0
    
    def on_next(x):
        assert(False)
        
    def on_error(ex):
        nonlocal n
        n += 1

        try:
            o.on_next(9)
        except Exception as 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

    def on_completed():
        assert(False)

    o = Observer(on_next, on_error, on_completed).checked()
    o.on_error(Exception('error'))
    assert(1 == n)
Пример #8
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)
Пример #9
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])
Пример #10
0
def start():
    longitude = 43.151325
    latitude = -77.620632
    a = Observer()

    source = Observable.interval(10000)
    source.subscribe(lambda n: a.onNext(get_weather((longitude, latitude))))
Пример #11
0
def test_to_observer_notification_on_completed():
    i = 0
    def next(n):
        assert(i == 0)
        assert(n.kind == 'C')
        assert(not n.has_value)

    Observer.from_notifier(next).on_completed()
Пример #12
0
def test_to_observer_notification_on_next():
    i = 0
    def next(n):
        assert(i == 0)
        assert(n.kind == 'N')
        assert(n.value == 42)
        assert(not hasattr(n, "exception"))
        assert(n.has_value)
    
    Observer.from_notifier(next).on_next(42)
Пример #13
0
def test_to_observer_notification_on_error():
    ex = 'ex'
    i = 0
    def next(n):
        assert(i == 0)
        assert(n.kind == 'E')
        assert(n.exception == ex)
        assert(not n.has_value)
    
    Observer.from_notifier(next).on_error(ex)
Пример #14
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])
Пример #15
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()
Пример #16
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()
Пример #17
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()
Пример #18
0
def test_subject_create():
    _x = [None]
    _ex = [None]
    done = False

    def on_next(x):
        _x[0] = x

    def on_error(ex):
        _ex[0] = ex
    
    def on_completed():
        done = True

    v = Observer(on_next, on_error, on_completed)
    
    o = Observable.return_value(42)

    s = Subject.create(v, o)

    def on_next2(x):
        _x[0] = x
    s.subscribe(on_next2)

    assert(42 == _x[0])
    s.on_next(21)

    e = 'ex'
    s.on_error(e)

    assert(e == _ex[0])

    s.on_completed()
    assert(not done)
Пример #19
0
    def subscribe(observer):
        active_plans = []
        external_subscriptions = {}

        def on_error(err):
            for v in external_subscriptions.values():
                v.on_error(err)
            observer.on_error(err)

        out_observer = Observer(observer.on_next, on_error,
                                observer.on_completed)

        def deactivate(active_plan):
            active_plans.remove(active_plan)
            if not len(active_plans):
                observer.on_completed()

        try:
            for plan in plans:
                active_plans.append(
                    plan.activate(external_subscriptions, out_observer,
                                  deactivate))
        except Exception as ex:
            Observable.throw(ex).subscribe(observer)

        group = CompositeDisposable()
        for join_observer in external_subscriptions.values():
            join_observer.subscribe()
            group.add(join_observer)

        return group
Пример #20
0
def test_create_on_next_has_error():
    ex = 'ex'
    next = [False]
    _e = None

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

    res = Observer(on_next)
    
    res.on_next(42)
    assert(next[0])
    
    try:
        res.on_error(ex)
        assert(False)
    except Exception as e:
        e_ = e.args[0]

    assert(ex == e_)
Пример #21
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)
Пример #22
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()
Пример #23
0
def test_create_on_next_on_completed_has_error():
    e_ = None
    ex = 'ex'
    next = False
    completed = False


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

    def on_completed():
        nonlocal completed
        completed = True

    res = Observer(on_next, None, on_completed)
    
    res.on_next(42)
    assert(next)
    assert(not completed)
    try:
        res.on_error(ex)
        assert(False)
    except Exception as e:
        e_ = e.args[0]
    
    assert(ex == e_)
    assert(not completed)
Пример #24
0
 def produce_data(self, data, observer: Observer):
     if data[0:4] == "TEMP":
         observer.on_next(TemperatureData(data))
     elif data[0:4] == "HUMI":
         observer.on_next(HumidityData(data))
     elif data[0:3] == "SM0":
         observer.on_next(SoilMoistureData(data))
Пример #25
0
def test_create_on_next_on_error_on_completed2():
    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_error(ex)
    
    assert(not completed)
    assert(error)
Пример #26
0
def test_create_on_next_on_error_on_completed2():
    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_error(ex)

    assert (not completed[0])
    assert (error[0])
Пример #27
0
def test_create_on_next_on_completed_has_error():
    e_ = None
    ex = 'ex'
    next = [False]
    completed = [False]

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

    def on_completed():
        completed[0] = True

    res = Observer(on_next, None, on_completed)

    res.on_next(42)
    assert (next[0])
    assert (not completed[0])
    try:
        res.on_error(ex)
        assert (False)
    except Exception as e:
        e_ = e.args[0]

    assert (ex == e_)
    assert (not completed[0])
Пример #28
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])
Пример #29
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)
Пример #30
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)
Пример #31
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)
Пример #32
0
    def observer_from_notifier(cls, handler):
        """Creates an observer from a notification callback.

        Keyword arguments:
        handler -- Action that handles a notification.

        Returns the observer object that invokes the specified handler using a
        notification corresponding to each message it receives."""
        def _on_next(value):
            return handler(OnNext(value))

        def _on_error(ex):
            return handler(OnError(ex))

        def _on_completed():
            return handler(OnCompleted())

        return Observer(_on_next, _on_error, _on_completed)
Пример #33
0
    def subscribe(self, on_next, on_error=None, on_completed=None):
        log.debug("HotObservable:subscribe()")

        if isinstance(on_next, AbstractObserver):
            observer = on_next
        else:
            observer = Observer(on_next, on_error, on_completed)

        observable = self
        self.observers.append(observer)
        self.subscriptions.append(Subscription(self.scheduler.clock))
        index = len(self.subscriptions) - 1

        def dispose_action():
            log.debug("HotObservable:subscribe:dispose_action(%s)" %
                      self.scheduler.clock)
            observable.observers.remove(observer)
            start = observable.subscriptions[index].subscribe
            end = observable.scheduler.clock
            observable.subscriptions[index] = Subscription(start, end)

        return Disposable(dispose_action)
Пример #34
0
    def subscribe(self, on_next, on_error=None, on_completed=None):
        print("ColdObservable:subscribe()")

        if isinstance(on_next, AbstractObserver):
            observer = on_next
        else:
            observer = Observer(on_next, on_error, on_completed)

        self.subscriptions.append(Subscription(self.scheduler.clock))
        index = len(self.subscriptions) - 1
        disposable = CompositeDisposable()

        def get_action(notification):
            def action(scheduler, state):
                notification.accept(observer)
                return Disposable.empty()

            return action

        for message in self.messages:
            notification = message.value
            print("Notification: ", notification)

            # Don't make closures within a loop
            action = get_action(notification)
            disposable.add(
                self.scheduler.schedule_relative(message.time, action))

        def dispose():
            print("ColdObservable:dispose()")
            start = self.subscriptions[index].subscribe
            end = self.scheduler.clock
            self.subscriptions[index] = Subscription(start, end)
            disposable.dispose()

        return Disposable(dispose)
Пример #35
0
def test_create_on_next_has_error():
    ex = 'ex'
    next = [False]
    _e = None

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

    res = Observer(on_next)

    res.on_next(42)
    assert (next[0])

    try:
        res.on_error(ex)
        assert (False)
    except Exception as e:
        e_ = e.args[0]

    assert (ex == e_)
    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)
Пример #37
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])
Пример #38
0
def test_create_on_next_on_error():
    ex = 'ex'
    next = True
    error = False

    def on_next(x):
        nonlocal next
        assert(42 == x)
        next = True
    
    def on_error(e):
        nonlocal error
        assert(ex == e)
        error = True

    res = Observer(on_next, on_error)
    
    res.on_next(42)

    assert(next)
    assert(not error)

    res.on_error(ex)
    assert(error)
Пример #39
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()
Пример #40
0
 def __init__(self, name):
     Observer.__init__(self)
     self._name = name