예제 #1
0
def url_escape(path):
    """Escape special characters in a URL path

    Turns '/foo bar/' into '/foo%20bar/'
    """
    parts = py3compat.unicode_to_str(path, encoding='utf8').split('/')
    return u'/'.join([quote(p) for p in parts])
예제 #2
0
파일: test_jsonutil.py 프로젝트: DT021/wau
def test_encode_images():
    # invalid data, but the header and footer are from real files
    pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82'
    jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9'
    pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>'

    fmt = {
        'image/png': pngdata,
        'image/jpeg': jpegdata,
        'application/pdf': pdfdata
    }
    encoded = encode_images(fmt)
    for key, value in iteritems(fmt):
        # encoded has unicode, want bytes
        decoded = decodestring(encoded[key].encode('ascii'))
        nt.assert_equal(decoded, value)
    encoded2 = encode_images(encoded)
    nt.assert_equal(encoded, encoded2)

    b64_str = {}
    for key, encoded in iteritems(encoded):
        b64_str[key] = unicode_to_str(encoded)
    encoded3 = encode_images(b64_str)
    nt.assert_equal(encoded3, b64_str)
    for key, value in iteritems(fmt):
        # encoded3 has str, want bytes
        decoded = decodestring(str_to_bytes(encoded3[key]))
        nt.assert_equal(decoded, value)
예제 #3
0
def test_encode_images():
    # invalid data, but the header and footer are from real files
    pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82'
    jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9'
    pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>'
    bindata = b'\xff\xff\xff\xff'
    
    fmt = {
        'image/png'  : pngdata,
        'image/jpeg' : jpegdata,
        'application/pdf' : pdfdata,
        'application/unrecognized': bindata,
    }
    encoded = json_clean(encode_images(fmt))
    for key, value in fmt.items():
        # encoded has unicode, want bytes
        decoded = a2b_base64(encoded[key])
        assert decoded == value
    encoded2 = json_clean(encode_images(encoded))
    assert encoded == encoded2
    
    # test that we don't double-encode base64 str
    b64_str = {}
    for key, encoded in encoded.items():
        b64_str[key] = unicode_to_str(encoded)
    encoded3 = json_clean(encode_images(b64_str))
    assert encoded3 == b64_str
    for key, value in fmt.items():
        decoded = a2b_base64(encoded3[key])
        assert decoded == value
예제 #4
0
def test_encode_images():
    # invalid data, but the header and footer are from real files
    pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82'
    jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9'
    pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>'
    
    fmt = {
        'image/png'  : pngdata,
        'image/jpeg' : jpegdata,
        'application/pdf' : pdfdata
    }
    encoded = encode_images(fmt)
    for key, value in iteritems(fmt):
        # encoded has unicode, want bytes
        decoded = decodestring(encoded[key].encode('ascii'))
        nt.assert_equal(decoded, value)
    encoded2 = encode_images(encoded)
    nt.assert_equal(encoded, encoded2)
    
    b64_str = {}
    for key, encoded in iteritems(encoded):
        b64_str[key] = unicode_to_str(encoded)
    encoded3 = encode_images(b64_str)
    nt.assert_equal(encoded3, b64_str)
    for key, value in iteritems(fmt):
        # encoded3 has str, want bytes
        decoded = decodestring(str_to_bytes(encoded3[key]))
        nt.assert_equal(decoded, value)
예제 #5
0
파일: utils.py 프로젝트: jhamrick/notebook
def url_escape(path):
    """Escape special characters in a URL path
    
    Turns '/foo bar/' into '/foo%20bar/'
    """
    parts = py3compat.unicode_to_str(path, encoding='utf8').split('/')
    return u'/'.join([quote(p) for p in parts])
예제 #6
0
def test_encode_images():
    # invalid data, but the header and footer are from real files
    pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82'
    jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9'
    pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>'
    bindata = b'\xff\xff\xff\xff'
    
    fmt = {
        'image/png'  : pngdata,
        'image/jpeg' : jpegdata,
        'application/pdf' : pdfdata,
        'application/unrecognized': bindata,
    }
    encoded = json_clean(encode_images(fmt))
    for key, value in fmt.items():
        # encoded has unicode, want bytes
        decoded = a2b_base64(encoded[key])
        assert decoded == value
    encoded2 = json_clean(encode_images(encoded))
    assert encoded == encoded2
    
    # test that we don't double-encode base64 str
    b64_str = {}
    for key, encoded in encoded.items():
        b64_str[key] = unicode_to_str(encoded)
    encoded3 = json_clean(encode_images(b64_str))
    assert encoded3 == b64_str
    for key, value in fmt.items():
        decoded = a2b_base64(encoded3[key])
        assert decoded == value
예제 #7
0
    def complete_request(self, text):
        line = str_to_unicode(readline.get_line_buffer())
        byte_cursor_pos = readline.get_endidx()

        # get_endidx is a byte offset
        # account for multi-byte characters to get correct cursor_pos
        bytes_before_cursor = cast_bytes(line)[:byte_cursor_pos]
        cursor_pos = len(cast_unicode(bytes_before_cursor))

        # send completion request to kernel
        # Give the kernel up to 5s to respond
        msg_id = self.client.complete(
            code=line,
            cursor_pos=cursor_pos,
        )

        msg = self.client.shell_channel.get_msg(timeout=self.timeout)
        if msg['parent_header']['msg_id'] == msg_id:
            content = msg['content']
            cursor_start = content['cursor_start']
            matches = [line[:cursor_start] + m for m in content['matches']]
            if content["cursor_end"] < cursor_pos:
                extra = line[content["cursor_end"]:cursor_pos]
                matches = [m + extra for m in matches]
            matches = [unicode_to_str(m) for m in matches]
            return matches
        return []
예제 #8
0
 def complete_request(self, text):
     line = str_to_unicode(readline.get_line_buffer())
     byte_cursor_pos = readline.get_endidx()
     
     # get_endidx is a byte offset
     # account for multi-byte characters to get correct cursor_pos
     bytes_before_cursor = cast_bytes(line)[:byte_cursor_pos]
     cursor_pos = len(cast_unicode(bytes_before_cursor))
     
     # send completion request to kernel
     # Give the kernel up to 5s to respond
     msg_id = self.client.complete(
         code=line,
         cursor_pos=cursor_pos,
     )
 
     msg = self.client.shell_channel.get_msg(timeout=self.timeout)
     if msg['parent_header']['msg_id'] == msg_id:
         content = msg['content']
         cursor_start = content['cursor_start']
         matches = [ line[:cursor_start] + m for m in content['matches'] ]
         if content["cursor_end"] < cursor_pos:
             extra = line[content["cursor_end"]: cursor_pos]
             matches = [m + extra for m in matches]
         matches = [ unicode_to_str(m) for m in matches ]
         return matches
     return []
예제 #9
0
def _construct_readline_matches(code, cursor_pos, content):
    cursor_start = content['cursor_start']
    matches = [code[:cursor_start] + m for m in content['matches']]
    if content["cursor_end"] < cursor_pos:
        extra = code[content["cursor_end"]:cursor_pos]
        matches = [m + extra for m in matches]
    matches = [unicode_to_str(m) for m in matches]
    return matches
예제 #10
0
def _construct_readline_matches(code, cursor_pos, content):
    cursor_start = content['cursor_start']
    matches = [ code[:cursor_start] + m for m in content['matches'] ]
    if content["cursor_end"] < cursor_pos:
        extra = code[content["cursor_end"]: cursor_pos]
        matches = [m + extra for m in matches]
    matches = [ unicode_to_str(m) for m in matches ]
    return matches
예제 #11
0
def url_unescape(path):
    """Unescape special characters in a URL path

    Turns '/foo%20bar/' into '/foo bar/'
    """
    return u'/'.join([
        py3compat.str_to_unicode(unquote(p), encoding='utf8')
        for p in py3compat.unicode_to_str(path, encoding='utf8').split('/')
    ])
예제 #12
0
파일: test_app.py 프로젝트: pudumula/nlp_p2
    def get_command(self):
        """Get the command to run."""
        terminalsAvailable = self.web_app.settings['terminals_available']
        # Compatibility with Notebook 4.2.
        token = getattr(self, 'token', '')
        config = dict(baseUrl=self.connection_url, token=token,
                      terminalsAvailable=str(terminalsAvailable),
                      foo='bar')

        cwd = self.karma_base_dir

        karma_inject_file = pjoin(cwd, 'build', 'injector.js')
        if not os.path.exists(pjoin(cwd, 'build')):
            os.makedirs(pjoin(cwd, 'build'))

        with open(karma_inject_file, 'w') as fid:
            fid.write("""
            require('es6-promise/dist/es6-promise.js');
            require('@phosphor/widgets/style/index.css');

            var node = document.createElement('script');
            node.id = 'jupyter-config-data';
            node.type = 'application/json';
            node.textContent = '%s';
            document.body.appendChild(node);
            """ % json.dumps(config))

        # validate the pattern
        parser = argparse.ArgumentParser()
        parser.add_argument('--pattern', action='store')
        args, argv = parser.parse_known_args()
        pattern = args.pattern or self.karma_pattern
        files = glob.glob(pjoin(cwd, pattern))
        if not files:
            msg = 'No files matching "%s" found in "%s"'
            raise ValueError(msg % (pattern, cwd))
        # Find and validate the coverage folder
        with open(pjoin(cwd, 'package.json')) as fid:
            data = json.load(fid)
        name = data['name'].replace('@jupyterlab/test-', '')
        folder = osp.realpath(pjoin(HERE, '..', '..', 'packages', name))
        if not osp.exists(folder):
            raise ValueError(
                'No source package directory found for "%s", use the pattern '
                '"@jupyterlab/test-<package_dir_name>"' % name
            )

        if PY2:
            karma_inject_file = karma_inject_file.encode('utf-8')
            folder = folder.encode('utf-8')
        env = os.environ.copy()
        env['KARMA_INJECT_FILE'] = karma_inject_file
        env.setdefault('KARMA_FILE_PATTERN', py3compat.unicode_to_str(pattern))
        env.setdefault('KARMA_COVER_FOLDER', folder)
        cwd = self.karma_base_dir
        cmd = ['karma', 'start'] + sys.argv[1:]
        return cmd, dict(env=env, cwd=cwd)
예제 #13
0
파일: utils.py 프로젝트: jhamrick/notebook
def url_unescape(path):
    """Unescape special characters in a URL path
    
    Turns '/foo%20bar/' into '/foo bar/'
    """
    return u'/'.join([
        py3compat.str_to_unicode(unquote(p), encoding='utf8')
        for p in py3compat.unicode_to_str(path, encoding='utf8').split('/')
    ])
예제 #14
0
    def _input_request(self, prompt, ident, parent, password=False):
        # Flush output before making the request.
        sys.stderr.flush()
        sys.stdout.flush()

        # flush the stdin socket, to purge stale replies
        while True:
            try:
                self.stdin_socket.recv_multipart(zmq.NOBLOCK)
            except zmq.ZMQError as e:
                if e.errno == zmq.EAGAIN:
                    break
                else:
                    raise

        # Send the input request.
        content = json_clean(dict(prompt=prompt, password=password))
        self.session.send(self.stdin_socket,
                          u'input_request',
                          content,
                          parent,
                          ident=ident)

        # Await a response.
        while True:
            try:
                # Use polling with select() so KeyboardInterrupts can get
                # through; doing a blocking recv() means stdin reads are
                # uninterruptible on Windows. We need a timeout because
                # zmq.select() is also uninterruptible, but at least this
                # way reads get noticed immediately and KeyboardInterrupts
                # get noticed fairly quickly by human response time standards.
                rlist, _, xlist = zmq.select([self.stdin_socket], [],
                                             [self.stdin_socket], 0.01)
                if rlist or xlist:
                    ident, reply = self.session.recv(self.stdin_socket)
                    if (ident, reply) != (None, None):
                        break
            except KeyboardInterrupt:
                # re-raise KeyboardInterrupt, to truncate traceback
                raise KeyboardInterrupt("Interrupted by user") from None
            except Exception as e:
                self.log.warning("Invalid Message:", exc_info=True)

        try:
            value = py3compat.unicode_to_str(reply['content']['value'])
        except:
            self.log.error("Bad input_reply: %s", parent)
            value = ''
        if value == '\x04':
            # EOF
            raise EOFError
        return value
예제 #15
0
    def _input_request(self, prompt, ident, parent, password=False):
        # Flush output before making the request.
        sys.stderr.flush()
        sys.stdout.flush()
        # flush the stdin socket, to purge stale replies
        while True:
            try:
                self.stdin_socket.recv_multipart(zmq.NOBLOCK)
            except zmq.ZMQError as e:
                if e.errno == zmq.EAGAIN:
                    break
                else:
                    raise

        # Send the input request.
        content = json_clean(dict(prompt=prompt, password=password))
        self.session.send(self.stdin_socket,
                          u'input_request',
                          content,
                          parent,
                          ident=ident)

        # Await a response.
        while True:
            try:
                ident, reply = self.session.recv(self.stdin_socket, 0)
            except Exception:
                self.log.warning("Invalid Message:", exc_info=True)
            except KeyboardInterrupt:
                # re-raise KeyboardInterrupt, to truncate traceback
                raise KeyboardInterrupt
            else:
                break
        try:
            value = py3compat.unicode_to_str(reply['content']['value'])
        except:
            self.log.error("Bad input_reply: %s", parent)
            value = ''
        if value == '\x04':
            # EOF
            raise EOFError
        return value
예제 #16
0
    def _input_request(self, prompt, ident, parent, password=False):
        # Flush output before making the request.
        sys.stderr.flush()
        sys.stdout.flush()
        # flush the stdin socket, to purge stale replies
        while True:
            try:
                self.stdin_socket.recv_multipart(zmq.NOBLOCK)
            except zmq.ZMQError as e:
                if e.errno == zmq.EAGAIN:
                    break
                else:
                    raise

        # Send the input request.
        content = json_clean(dict(prompt=prompt, password=password))
        self.session.send(self.stdin_socket, u'input_request', content, parent,
                          ident=ident)

        # Await a response.
        while True:
            try:
                ident, reply = self.session.recv(self.stdin_socket, 0)
            except Exception:
                self.log.warn("Invalid Message:", exc_info=True)
            except KeyboardInterrupt:
                # re-raise KeyboardInterrupt, to truncate traceback
                raise KeyboardInterrupt
            else:
                break
        try:
            value = py3compat.unicode_to_str(reply['content']['value'])
        except:
            self.log.error("Bad input_reply: %s", parent)
            value = ''
        if value == '\x04':
            # EOF
            raise EOFError
        return value