Exemplo n.º 1
0
    def paste(self, parameter_s=''):
        """Paste & execute a pre-formatted code block from clipboard.

        The text is pulled directly from the clipboard without user
        intervention and printed back on the screen before execution (unless
        the -q flag is given to force quiet mode).

        The block is dedented prior to execution to enable execution of method
        definitions. '>' and '+' characters at the beginning of a line are
        ignored, to allow pasting directly from e-mails, diff files and
        doctests (the '...' continuation prompt is also stripped).  The
        executed block is also assigned to variable named 'pasted_block' for
        later editing with '%edit pasted_block'.

        You can also pass a variable name as an argument, e.g. '%paste foo'.
        This assigns the pasted block to variable 'foo' as string, without
        executing it (preceding >>> and + is still stripped).

        Options:

          -r: re-executes the block previously entered by cpaste.

          -q: quiet mode: do not echo the pasted text back to the terminal.

        IPython statements (magics, shell escapes) are not supported (yet).

        See also
        --------
        cpaste: manually paste code into terminal until you mark its end.
        """
        opts, name = self.parse_options(parameter_s, 'rq', mode='string')
        if 'r' in opts:
            self.rerun_pasted()
            return
        try:
            block = self.shell.hooks.clipboard_get()
        except TryNext as clipboard_exc:
            message = getattr(clipboard_exc, 'args')
            if message:
                error(message[0])
            else:
                error('Could not get text from the clipboard.')
            return
        except ClipboardEmpty:
            raise UsageError("The clipboard appears to be empty")

        # By default, echo back to terminal unless quiet mode is requested
        if 'q' not in opts:
            write = self.shell.write
            write(self.shell.pycolorize(block))
            if not block.endswith('\n'):
                write('\n')
            write("## -- End pasted text --\n")

        self.store_or_execute(block, name)
Exemplo n.º 2
0
 def load_ext(self, module_str):
     """Load an IPython extension by its module name."""
     if not module_str:
         raise UsageError('Missing module name.')
     res = self.shell.extension_manager.load_extension(module_str)
     
     if res == 'already loaded':
         print("The %s extension is already loaded. To reload it, use:" % module_str)
         print("  %reload_ext", module_str)
     elif res == 'no load function':
         print("The %s module is not an IPython extension." % module_str)
Exemplo n.º 3
0
    def run_examples(self, arg):
        """ Run doctest-format examples in an object's docstring.

    """
        args = parse_argstring(self.run_examples, arg)
        obj = self.get_variable(args.object)
        if not hasattr(obj, '__doc__'):
            raise UsageError("%s does not have a docstring" % args.object)
        d = DoctestDemo(StringIO(obj.__doc__))
        while not d.finished:
            d()
Exemplo n.º 4
0
 def lineage_json(self, line):
     opts, args = self.parse_options(line, '', 'path')
     use_path = 'path' in opts
     self.validate_inputs()
     if use_path:
         if args not in self._declared_outputs.keys():
             raise UsageError(
                 'Could not find path {} in declared outputs'.format(args))
         return self._lineage_template_provider(
             self._declared_outputs[args], self._declared_inputs)
     elif not args:
         raise UsageError('Missing dataset name.')
     else:
         ds = self.shell.user_ns[args]
         output_schema = {
             "schema": ds.schema.json(),
             "timestamp": self.current_milli_time()
         }
         return self._lineage_template_provider(output_schema,
                                                self._declared_inputs)
Exemplo n.º 5
0
    def list_container(self, line):
        args = magic_arguments.parse_argstring(self.list_container, line)
        if not args.i:
            raise UsageError('-i option is mandatory for listing')
        if not args.o:
            raise UsageError('-o option is mandatory for listing')
        if not args.o[0].startswith(tuple(string.ascii_letters)):
            raise UsageError('The output variable name must be a valid prefix '
                             'of a python variable, that is, start with a '
                             'letter')

        # Get the objects
        conn = get_swift_connection()
        _, objects = conn.get_container(args.i, full_listing=True)

        # Populate the returned list
        obj_names = []
        for obj_dict in objects:
            obj_names.append(obj_dict['name'])
        self.shell.user_ns[args.o] = obj_names
Exemplo n.º 6
0
    def copy(self, line):
        args = magic_arguments.parse_argstring(self.copy, line)
        if not args.o:
            raise UsageError('-o option is mandatory for the invocation')
        if not args.o[0].startswith(tuple(string.ascii_letters)):
            raise UsageError('The output variable name must be a valid prefix '
                             'of a python variable, that is, start with a '
                             'letter')
        if not args.storlet:
            raise UsageError('--storlet option is mandatory '
                             'for the invocation')
        if not args.input:
            raise UsageError('--input option is mandatory for the invocation')

        if not args.output:
            raise UsageError('--output option is mandatory for the invocation')

        src_container, src_obj = self._parse_input_path(args.input)
        dst_container, dst_obj = self._parse_input_path(args.output)
        destination = '/%s/%s' % (dst_container, dst_obj)

        headers = {'X-Run-Storlet': '%s' % args.storlet}
        # pick -i option and translate the params to
        # X-Storlet-Parameter-x headers
        storlet_headers = self._generate_params_headers(
            self.shell.user_ns[args.i] if args.i else {})
        headers.update(storlet_headers)

        # invoke storlet app on copy
        conn = get_swift_connection()

        response_dict = dict()
        conn.copy_object(
            src_container, src_obj,
            destination=destination,
            headers=headers,
            response_dict=response_dict)

        res = Response(int(response_dict['status']),
                       response_dict['headers'])
        self.shell.user_ns[args.o] = res
Exemplo n.º 7
0
 def _load(argv):
     """Load an external Tcl script, modifies argv."""
     content = None
     if not argv:
         raise UsageError('%tcl FILENAME [ARGS...]')
     try:
         with open(argv[0], "r") as file:
             content = file.read()
     except EnvironmentError as e:
         sys.stderr.write(str(e))
     del argv[0]
     return content
Exemplo n.º 8
0
 def ec(self, line, cell='', address=target_memory.shell_code):
     """Evaluate a 32-bit C++ expression on the target"""
     d = self.shell.user_ns['d']
     try:
         return evalc(d,
                      line + cell,
                      defines=all_defines(),
                      includes=all_includes(),
                      address=address,
                      verbose=True)
     except CodeError, e:
         raise UsageError(str(e))
Exemplo n.º 9
0
    def less(self, arg_s):
        """Show a file through the pager.

        Files ending in .py are syntax-highlighted."""
        if not arg_s:
            raise UsageError('Missing filename.')

        if arg_s.endswith('.py'):
            cont = self.shell.pycolorize(openpy.read_py_file(arg_s, skip_encoding_cookie=False))
        else:
            cont = open(arg_s).read()
        page.page(cont)
Exemplo n.º 10
0
 def get_variable(self, variable):
     """ Get a variable from the shell's namespace.
     """
     ipshell = self.shell
     if variable not in ipshell.user_ns:
         try:
             obj = eval(variable, ipshell.user_global_ns, ipshell.user_ns)
         except Exception:
             raise UsageError('variable %r not in namespace' % variable)
     else:
         obj = ipshell.user_ns[variable]
     return obj
Exemplo n.º 11
0
 def inactive(self, parameter_s='', cell=None):
     """Does *not* exeutes a cell.
     
     Usage:
       %%inactive
       code...
             
     This magic can be used to mark a cell (temporary) as inactive.
     """
     if cell is None:
         raise UsageError('empty cell, nothing to ignore :-)')
     print("Cell inactive: not executed!")
Exemplo n.º 12
0
    def profile(self, parameter_s=''):
        """Print your currently active IPython profile.

        See Also
        --------
        prun : run code using the Python profiler
               (:meth:`~IPython.core.magics.execution.ExecutionMagics.prun`)
        """
        raise UsageError(
            "The `%profile` magic has been deprecated since IPython 2.0. "
            "and removed in IPython 6.0. Please use the value of `get_ipython().profile` instead "
            "to see current profile in use. Perhaps you meant to use `%prun` to profile code?"
        )
Exemplo n.º 13
0
    def _enable_autopx(self):
        """Enable %autopx mode by saving the original run_cell and installing
        pxrun_cell.
        """
        if self.active_view is None:
            raise UsageError(NO_ACTIVE_VIEW)

        # override run_cell
        self._original_run_cell = self.shell.run_cell
        self.shell.run_cell = self.pxrun_cell

        self._autopx = True
        print "%autopx enabled"
Exemplo n.º 14
0
def load_ipython_extension(ipython):
    """Load our IPython magic."""
    from IPython.core.error import UsageError
    import os

    if os.environ.get("__FIL_STATUS") != "api":
        raise UsageError(
            "In order to use Fil, you need to run your notebook with the Fil kernel.\n\n"
            "You can change the kernel via the 'Change Kernel' option at the bottom of "
            "the Kernel menu in Jupyter.")
    from ._ipython import FilMagics

    ipython.register_magics(FilMagics)
Exemplo n.º 15
0
    def _parse_input_path(self, path_str):
        """
        Parse formatted to path to swift container and object names

        :param path_str: path string starts with "path:" prefix
        :return (container, obj): Both container and obj are formatted
            as string
        :raise UsageError: if the path_str is not formatted as expected
        """
        if not path_str.startswith('path:'):
            raise UsageError(
                'swift object path must have the format: '
                '"path:/<container>/<object>"')
        try:
            src_container_obj = path_str[len('path:'):]
            src_container, src_obj = src_container_obj.strip(
                '/').split('/', 1)
            return src_container, src_obj
        except ValueError:
            raise UsageError(
                'swift object path must have the format: '
                '"path:/<container>/<object>"')
Exemplo n.º 16
0
    def pwd(self, parameter_s=''):
        """Return the current working directory path.

        Examples
        --------
        ::

          In [9]: pwd
          Out[9]: '/home/tsuser/sprint/ipython'
        """
        try:
            return os.getcwd()
        except FileNotFoundError:
            raise UsageError("CWD no longer exists - please use %cd to change directory.")
 def wrapper(self, ns):
     data_doc_client = DatasetDocClient(AuthClient.get_access_token,
                                        os.environ['DOC_TEMPLATE_URL'])
     version = _current_milli_time()
     if not hasattr(self._df, 'lineage'):
         return write_method(self, ns)
     lineage = json.dumps(extract_lineage(self._df, ns, version), indent=2)
     schema = self._df.schema.json()
     validation = data_doc_client.get_lineage_validation(schema, lineage)
     status = validation['status']
     if status == 'ok':
         return write_method(self, ns)
     message = validation['message']
     raise UsageError("{}".format(message))
Exemplo n.º 18
0
def test_parse_argstring_or_throw():
    parse_argstring = MagicMock(side_effect=UsageError('OOGABOOGABOOGA'))
    try:
        parse_argstring_or_throw(MagicMock(), MagicMock(), parse_argstring=parse_argstring)
        assert False
    except BadUserDataException as e:
        assert_equals(str(e), str(parse_argstring.side_effect))

    parse_argstring = MagicMock(side_effect=ValueError('AN UNKNOWN ERROR HAPPENED'))
    try:
        parse_argstring_or_throw(MagicMock(), MagicMock(), parse_argstring=parse_argstring)
        assert False
    except ValueError as e:
        assert_is(e, parse_argstring.side_effect)
Exemplo n.º 19
0
def wdb_f(self, arg):
    """ Debug a script (like %run -d) in IPython process, Using WinPdb

    Usage:

    %wdb test.py
        run test.py, with a winpdb breakpoint at start of the file

    %wdb pass
        Change the password (e.g. if you have forgotten the old one)

    Note that after the script has been run, you need to do "Go" (f5)
    in WinPdb to resume normal IPython operation.
    """

    global rpdb_started
    if not arg.strip():
        print __doc__
        return

    if arg.strip() == 'pass':
        passwd = raw_input('Enter new winpdb session password: '******'winpdb_pass'] = passwd
        print "Winpdb password changed"
        if rpdb_started:
            print "You need to restart IPython to use the new password"
        return

    path = os.path.abspath(arg)
    if not os.path.isfile(path):
        raise UsageError("%%wdb: file %s does not exist" % path)
    if not rpdb_started:
        passwd = ip.db.get('winpdb_pass', None)
        if passwd is None:
            import textwrap
            print textwrap.dedent("""\
            Winpdb sessions need a password that you use for attaching the external
            winpdb session. IPython will remember this. You can change the password later
            by '%wpdb pass'
            """)
            passwd = raw_input('Enter new winpdb session password: '******'winpdb_pass'] = passwd

        print "Starting rpdb2 in IPython process"
        rpdb2.start_embedded_debugger(passwd, timeout=0)
        rpdb_started = True

    rpdb2.set_temp_breakpoint(path)
    print 'It is time to attach with WinPdb (launch WinPdb if needed, File -> Attach)'
    ip.magic('%run ' + arg)
Exemplo n.º 20
0
    def bowtie(self, line=''):
        """Build and serve a Bowtie app."""
        opts, appvar = self.parse_options(line, 'w:h:b:p:')
        width = opts.get('w', 1500)
        height = opts.get('h', 1000)
        border = opts.get('b', 2)
        port = opts.get('p', 9991)
        host = '0.0.0.0'

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((host, port))
        if result == 0:
            raise Exception(f'Port {port} is unavailable on host {host}, aborting.')

        global_ns = self.shell.user_global_ns
        local_ns = self.shell.user_ns
        try:
            # pylint: disable=eval-used
            app = eval(appvar, global_ns, local_ns)
        except NameError:
            raise UsageError(f'Could not find App {appvar}')

        if not isinstance(app, App):
            raise UsageError(f'App is of type {type(app)} needs to be type <bowtie.App>')

        app._build(notebook=get_notebook_name())  # pylint: disable=protected-access
        self.process = Process(target=app._serve)  # pylint: disable=protected-access
        self.process.start()
        time.sleep(5)

        clear_output()
        display(
            HTML(
                f'<iframe src=http://localhost:9991 width={width} height={height} '
                f'frameBorder={border}></iframe>'
            )
        )
Exemplo n.º 21
0
 def env(self, parameter_s=''):
     """List environment variables."""
     if parameter_s.strip():
         split = '=' if '=' in parameter_s else ' '
         bits = parameter_s.split(split)
         if len(bits) == 1:
             key = parameter_s.strip()
             if key in os.environ:
                 return os.environ[key]
             else:
                 err = "Environment does not have key: {0}".format(key)
                 raise UsageError(err)
         if len(bits) > 1:
             return self.set_env(parameter_s)
     return dict(os.environ)
Exemplo n.º 22
0
    def profile(self, parameter_s=''):
        """DEPRECATED since IPython 2.0.

        Raise `UsageError`. To profile code use the :magic:`prun` magic.
        

        See Also
        --------
        prun : run code using the Python profiler (:magic:`prun`)
        """
        raise UsageError(
            "The `%profile` magic has been deprecated since IPython 2.0. "
            "and removed in IPython 6.0. Please use the value of `get_ipython().profile` instead "
            "to see current profile in use. Perhaps you meant to use `%prun` to profile code?"
        )
Exemplo n.º 23
0
 def unload_ext(self, module_str):
     """Unload an IPython extension by its module name.
     
     Not all extensions can be unloaded, only those which define an
     ``unload_ipython_extension`` function.
     """
     if not module_str:
         raise UsageError('Missing module name.')
     
     res = self.shell.extension_manager.unload_extension(module_str)
     
     if res == 'no unload function':
         print("The %s extension doesn't define how to unload it." % module_str)
     elif res == "not loaded":
         print("The %s extension is not loaded." % module_str)
Exemplo n.º 24
0
    def colors(self, parameter_s=""):
        """Switch color scheme for prompts, info system and exception handlers.

        Currently implemented schemes: NoColor, Linux, LightBG.

        Color scheme names are not case-sensitive.

        Examples
        --------
        To get a plain black and white terminal::

          %colors nocolor
        """

        def color_switch_err(name):
            warn(
                "Error changing %s color schemes.\n%s" % (name, sys.exc_info()[1]),
                stacklevel=2,
            )

        new_scheme = parameter_s.strip()
        if not new_scheme:
            raise UsageError("%colors: you must specify a color scheme. See '%colors?'")
        # local shortcut
        shell = self.shell

        # Set shell colour scheme
        try:
            shell.colors = new_scheme
            shell.refresh_style()
        except:
            color_switch_err("shell")

        # Set exception colors
        try:
            shell.InteractiveTB.set_colors(scheme=new_scheme)
            shell.SyntaxTB.set_colors(scheme=new_scheme)
        except:
            color_switch_err("exception")

        # Set info (for 'object?') colors
        if shell.color_info:
            try:
                shell.inspector.set_active_scheme(new_scheme)
            except:
                color_switch_err("object inspector")
        else:
            shell.inspector.set_active_scheme("NoColor")
Exemplo n.º 25
0
    def show_image(self, line):
        args = magic_arguments.parse_argstring(self.show_image, line)
        if not args.input:
            raise UsageError('--input option is mandatory')

        src_container, src_obj = self._parse_input_path(args.input)

        # invoke storlet app on get
        conn = get_swift_connection()
        response_dict = dict()
        resp_headers, resp_content_iter = conn.get_object(
            src_container, src_obj, response_dict=response_dict)
        img_str = ''
        for buf in resp_content_iter:
            img_str += buf
        show_image(img_str)
Exemplo n.º 26
0
Arquivo: kernel.py Projeto: mr-c/SOS
def py_from_R_repr(expr):
    '''
    Convert expression returned from R to python
    '''
    try:
        if 'read_dataframe' in expr:
            # imported to be used by eval
            from feather import read_dataframe
            # suppress flakes warning
            read_dataframe
        # the result is something like
        # [1] "{'a': 1}"
        return eval(eval(expr.split(' ', 1)[-1]))
    except Exception as e:
        raise UsageError('Failed to convert {} to Python object: {}'.format(
            expr, e))
 def create_widget(self, binding, key):
     binding_key = binding[key]
     if isinstance(binding_key, str):
         return self.create_text_input(binding, key)
     elif isinstance(binding_key, bool):
         return self.create_checkbox_input(binding, key)
     elif isinstance(binding_key,
                     dict) and binding_key.__contains__('enums'):
         return self.create_enum_selector(binding, key)
     elif isinstance(binding_key,
                     dict) and binding_key.__contains__('candidates'):
         return self.create_candidate_selector(binding, key)
     else:
         raise UsageError(
             "Unable to create a widget for '{}' with value '{}'\n{}".
             format(key, binding_key, json.dumps(binding, indent=2)))
Exemplo n.º 28
0
def switchdb(self, parameter_s=''):
    """Switches the active tango Database.

    Usage: switchdb <host>[(:| )<port>]

    <port> is optional. If not given it defaults to 10000.

    Examples:
    In [1]: switchdb homer:10005
    In [2]: switchdb homer 10005
    In [3]: switchdb homer"""

    if parameter_s == '':
        raise UsageError("%switchdb: Must specify a tango database name. "
                         "See '%switchdb?'")
    return init_db(parameter_s)
Exemplo n.º 29
0
def irm(ip, arg):
    """ irm path[s]...
    
    Remove file[s] or dir[s] path. Dirs are deleted recursively.
    """
    try:
        paths = mglob.expand(arg.split(None, 1)[1])
    except IndexError:
        raise UsageError("%irm paths...")
    import distutils.dir_util
    for p in paths:
        print "rm", p
        if os.path.isdir(p):
            distutils.dir_util.remove_tree(p, verbose=1)
        else:
            os.remove(p)
 def wrapper(self, ns):
     data_doc_client = DatasetDocClient(AuthClient.get_access_token,
                                        os.environ['DOC_TEMPLATE_URL'])
     doc = extract_doc(self._df)
     if doc is None:
         return write_method(self, ns)
     template_doc = json.dumps(doc, indent=2)
     schema = self._df.schema.json()
     validation = data_doc_client.get_doc_validation(schema, template_doc)
     status = validation['status']
     message = validation['message']
     if status == 'ok':
         if message != '':
             print(message)
         return write_method(self, ns)
     raise UsageError("{}".format(message))