Пример #1
0
def test(manager, watcher):
    p = Process(["echo", "Hello World!"]).register(manager)
    assert watcher.wait("registered")

    p.start()
    assert watcher.wait("started", p.channel)

    assert watcher.wait("terminated", p.channel)

    s = p.stdout.getvalue()
    assert s == b"Hello World!\n"
Пример #2
0
def test(manager, watcher):
    p = Process(["echo", "Hello World!"]).register(manager)
    assert watcher.wait("registered")

    p.start()
    assert watcher.wait("started", p.channel)

    assert watcher.wait("stopped", p.channel)

    s = p.stdout.getvalue()
    assert s == b"Hello World!\n"
Пример #3
0
def test_two_procs(manager, watcher):
    p1 = Process(["echo", "1"]).register(manager)
    p2 = Process("echo 2 ; sleep 1", shell = True).register(manager)
    
    p1.start()
    p2.start()
    
    assert watcher.wait("terminated", p1.channel)
    assert p1._terminated
    assert not p2._terminated
    assert not p2._stdout_closed
    assert not p2._stderr_closed
    
    watcher.clear()     # Get rid of first terminated()
    
    s1 = p1.stdout.getvalue()
    assert s1 == b"1\n"
    
    assert watcher.wait("terminated", p2.channel)
    assert p2._terminated
    assert p2._stdout_closed
    assert p2._stderr_closed
    
    s2 = p2.stdout.getvalue()
    assert s2 == b"2\n"
Пример #4
0
    def _process_download(self):
        if not self.downloading and self.queue:
            if not self.process:
                self.downloading = self.queue.popleft()
                flags = ' '.join(self.options)
                # Still not sure why I have to give a stock Manager() rather than self.
                self.process = Process(
                    self.yt_dl + ' ' + flags + ' ' + self.downloading,
                    cwd=self.save_dir).register(self.manager)

                self.process.start()
                self.start_time = time.clock()
                if not self.timer:
                    self.timer = Timer(1, poll_process(),
                                       persist=True).register(self)
Пример #5
0
class Ping(Component):

    # This adds the already instantiated stdout instnace
    stdout = stdout

    def init(self, host):
        self.p = Process(["/bin/ping", host]).register(self)
        self.p.start()

    def read(self, data):
        """read Event Handler

        This is fired by the File Component when there is data to be read
        from the underlying file that was opened.
        """

        self.fire(write(data), stdout)
Пример #6
0
def test2(manager, watcher, tmpdir):
    foo = tmpdir.ensure("foo.txt")

    p = Process(
        ["cat - > {0:s}".format(str(foo))], shell=True).register(manager)
    assert watcher.wait("registered")

    p.start()
    assert watcher.wait("started", p.channel)

    p.fire(write("Hello World!"), p._stdin)
    assert watcher.wait("write", p._stdin)

    p.stop()

    assert watcher.wait("eof", p._stdout.channel)

    with foo.open("r") as f:
        assert f.read() == "Hello World!"
Пример #7
0
def test2(manager, watcher, tmpdir):
    foo = tmpdir.ensure("foo.txt")

    p = Process(["cat - > {0:s}".format(str(foo))], shell=True).register(manager)
    assert watcher.wait("registered")

    p.start()
    assert watcher.wait("started", p.channel)

    p.fire(write("Hello World!"), p._stdin)
    assert watcher.wait("write", p._stdin)

    p.stop()

    assert watcher.wait("eof", p._stdout.channel)

    with foo.open("r") as f:
        assert f.read() == "Hello World!"
Пример #8
0
 def init(self, host):
     self.p = Process(["/bin/ping", host]).register(self)
     self.p.start()
Пример #9
0
class YoutubeDownloader(Component):
    channel = 'youtube'
    EXPECTED_MAX_SIZE = 15

    def init(self, quality=3, rate_limit=None):
        self.manager = Manager()
        script_dir = get_script_dir()
        save_dir = os.path.join(script_dir, 'songs')
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
        print('Script Dir: {}'.format(script_dir))
        print('Saving to: {}'.format(save_dir))
        self.save_dir = save_dir

        self.options = [
            '--extract-audio', '--prefer-ffmpeg', '--audio-format mp3',
            '--ffmpeg {}'.format(script_dir),
            '--audio-quality {}'.format(quality)
        ]
        if rate_limit:
            try:
                rate_limit = float(rate_limit)
            except ValueError as e:
                raise ValueError('rate_limit should be a float.')
            self.options.append(
                '--limit-rate {}'.format(str(rate_limit) + 'M'))
        else:
            rate_limit = 1

        # Make timeout to cancel download relative to rate limiting
        self.timeout = 2 * self.EXPECTED_MAX_SIZE / rate_limit
        self.queue = deque()
        self.downloading = None
        self.process = None
        self.yt_dl = 'youtube-dl'
        if sys.platform.startswith('win'):
            self.yt_dl += '.exe'
        self.timer = None
        self.start_time = None

    @handler('youtube_download')
    def download(self, url):
        self.queue.append(url)
        self.fire(process_download(), 'youtube')

    @handler('process_download')
    def _process_download(self):
        if not self.downloading and self.queue:
            if not self.process:
                self.downloading = self.queue.popleft()
                flags = ' '.join(self.options)
                # Still not sure why I have to give a stock Manager() rather than self.
                self.process = Process(
                    self.yt_dl + ' ' + flags + ' ' + self.downloading,
                    cwd=self.save_dir).register(self.manager)

                self.process.start()
                self.start_time = time.clock()
                if not self.timer:
                    self.timer = Timer(1, poll_process(),
                                       persist=True).register(self)

    def _shutdown_process(self):
        self.process.kill()
        self.process = None
        self.timer.stop()
        self.timer.unregister()
        self.timer = None

    @handler('poll_process')
    def _poll_process(self):
        status = self.process.status
        if (time.clock() - self.start_time) > self.timeout:
            self._shutdown_process()
            self.fire(
                youtube_error('Timeout while downloading {}'.format(
                    self.downloading)), '*')
            self.downloading = None
            self.fire(process_download())
            return
        if status is not None:
            self._shutdown_process()
            if status == 0:
                self.fire(youtube_download_complete(self.downloading), '*')
            else:
                self.fire(youtube_error(self.downloading), '*')
            self.downloading = None
            self.fire(process_download())