示例#1
0
 def create_magic(self, f):
     f.__name__ = str(self.magic)
     f.__doc__ = self.docstring
     f = MAGIC_TYPES[self.magic_type](self.magic)(f)
     for arg in self.args:
         f = arg(f)
     f = magic_arguments.magic_arguments()(f)
     return f
示例#2
0
 def create_magic(self, func):
     """Create magic for command"""
     func.__name__ = str(self.magic)
     func.__doc__ = self.docstring
     func = MAGIC_TYPES[self.magic_type](self.magic)(func)
     for arg in self.args:
         func = arg(func)
     func = magic_arguments.magic_arguments()(func)
     return func
示例#3
0
 def create_magic(self, func):
     """Create magic for command"""
     func.__name__ = str(self.magic)
     func.__doc__ = self.docstring
     func = MAGIC_TYPES[self.magic_type](self.magic)(func)
     for arg in self.args:
         func = arg(func)
     func = magic_arguments.magic_arguments()(func)
     return func
示例#4
0
文件: magic_args.py 项目: klauer/ECLI
 def __init__(self, plugin_class):
     self.plugin_class = plugin_class
     self.magic_args = magic_arguments()
示例#5
0
class FortranMagics(Magics):

    allowed_fcompilers = sorted(fcompiler.fcompiler_class.keys())
    allowed_compilers = sorted(compiler_class.keys())

    my_magic_arguments = compose(
        magic_arguments.magic_arguments(),
        magic_arguments.argument("-v",
                                 "--verbosity",
                                 action="count",
                                 default=0,
                                 help="increase output verbosity"),
        magic_arguments.argument(
            '--fcompiler',
            choices=allowed_fcompilers,
            help="""Specify Fortran compiler type by vendor.
                 See %%f2py_help --fcompiler""",
        ),
        magic_arguments.argument(
            '--compiler',
            choices=allowed_compilers,
            help="""Specify C compiler type (as defined by distutils).
                    See %%f2py_help --compiler"""),
        magic_arguments.argument('--f90flags',
                                 help="Specify F90 compiler flags"),
        magic_arguments.argument('--f77flags',
                                 help="Specify F77 compiler flags"),
        magic_arguments.argument('--opt', help="Specify optimization flags"),
        magic_arguments.argument(
            '--arch', help="Specify architecture specific optimization flags"),
        magic_arguments.argument('--noopt',
                                 action="store_true",
                                 help="Compile without optimization"),
        magic_arguments.argument('--noarch',
                                 action="store_true",
                                 help="Compile without "
                                 "arch-dependent optimization"),
        magic_arguments.argument('--debug',
                                 action="store_true",
                                 help="Compile with debugging "
                                 "information"),
        magic_arguments.argument(
            '--link',
            action='append',
            default=[],
            help="""Link extension module with LINK resource, as defined
                    by numpy.distutils/system_info.py. E.g. to link
                    with optimized LAPACK libraries (vecLib on MacOSX,
                    ATLAS elsewhere), use --link lapack_opt.
                    See also %%f2py_help --resources switch."""),
        magic_arguments.argument(
            '--extra',
            action='append',
            default=[],
            help=
            """Use --extra to pass any other argument in the f2py call. For example
                    --extra '-L/path/to/lib/ -l<libname>'
                    --extra '-D<define> -U<name>'
                    --extra '-DPREPEND_FORTRAN -DUPPERCASE_FORTRAN'
                    etc. """))

    def __init__(self, shell):
        super(FortranMagics, self).__init__(shell=shell)
        self._reloads = {}
        self._code_cache = {}
        self._lib_dir = os.path.join(get_ipython_cache_dir(), 'fortran')
        if not os.path.exists(self._lib_dir):
            os.makedirs(self._lib_dir)

    def _import_all(self, module, verbosity=0):
        imported = []
        for k, v in module.__dict__.items():
            if not k.startswith('__'):
                self.shell.push({k: v})
                imported.append(k)
        if verbosity > 0 and imported:
            print("\nOk. The following fortran objects "
                  "are ready to use: %s" % ", ".join(imported))

    def _run_f2py(self, argv, show_captured=False, verbosity=0):
        """
        Here we directly call the numpy.f2py.f2py2e.run_compile() entry point,
        after some small amount of setup to get sys.argv and the current
        working directory set appropriately.
        """
        old_argv = sys.argv
        old_cwd = os.getcwdu() if sys.version_info[0] == 2 else os.getcwd()
        try:
            sys.argv = ['f2py'] + list(map(str, argv))
            if verbosity > 1:
                print("Running...\n   %s" % ' '.join(sys.argv))

            os.chdir(self._lib_dir)
            try:
                with capture_output() as captured:
                    #subprocess.call(sys.argv)
                    # Refactor subprocess call to work with jupyterhub
                    try:
                        p = Popen(sys.argv,
                                  stdout=PIPE,
                                  stderr=PIPE,
                                  stdin=PIPE)
                    except OSError as e:
                        if e.errno == errno.ENOENT:
                            print("Couldn't find program: %r" % sys.argv[0])
                            return
                        else:
                            raise
                    try:
                        out, err = p.communicate(input=None)
                    except:
                        pass
                    if err:
                        sys.stderr.write(err)
                        sys.stderr.flush()
                if show_captured or verbosity > 2:
                    if out:
                        sys.stdout.write(out)
                        sys.stdout.flush()
                    captured()
            except SystemExit as e:
                captured()
                raise UsageError(str(e))
        finally:
            sys.argv = old_argv
            os.chdir(old_cwd)

    @magic_arguments.magic_arguments()
    @magic_arguments.argument(
        '--resources',
        action="store_true",
        help="""List system resources found by system_info.py.

                See also
                %%f2py_help --link <resource> switch.
                """)
    @magic_arguments.argument('--link',
                              help="""Given a resource name, show what it foun.
                E.g. try '--link lapack.

                See also
                %%f2py_help --link <resource> switch.
                """)
    @magic_arguments.argument(
        '--fcompiler',
        action="store_true",
        help="List available Fortran compilers",
    )
    @magic_arguments.argument(
        '--compiler',
        action="store_true",
        help="List available C compilers",
    )
    @line_magic
    def f2py_help(self, line):
        args = magic_arguments.parse_argstring(self.f2py_help, line)
        if args.fcompiler:
            self._run_f2py(['-c', '--help-fcompiler'], True)
        elif args.compiler:
            self._run_f2py(['-c', '--help-compiler'], True)
        elif args.resources:
            self._run_f2py(['--help-link'], True)
        elif args.link:
            self._run_f2py(['--help-link', args.link], True)

    @my_magic_arguments
    @magic_arguments.argument('--defaults',
                              action="store_true",
                              help="Delete custom configuration "
                              "and back to default")
    @line_magic
    def fortran_config(self, line):
        """
        View and handle the custom configuration for %%fortran magic.

            %fortran_config

                Show the current custom configuration

            %fortran_config --defaults

                Delete the current configuration and back to defaults

            %fortran_config <other options>

                Save <other options> to use with %%fortran
        """

        args = magic_arguments.parse_argstring(self.fortran_config, line)
        if args.defaults:
            try:
                del self.shell.db['fortran']
                print(
                    "Deleted custom config. Back to default arguments for %%fortran"
                )
            except KeyError:
                print("No custom config found for %%fortran")
        elif not line:
            try:
                line = self.shell.db['fortran']
            except KeyError:
                print("No custom config found for %%fortran")
            print("Current defaults arguments for %%fortran:\n\t%s" % line)
        else:
            self.shell.db['fortran'] = line
            print("New default arguments for %%fortran:\n\t%s" % line)

    @my_magic_arguments
    @cell_magic
    def fortran(self, line, cell):
        """Compile and import everything from a Fortran code cell, using f2py.

        The content of the cell is written to a `.f90` file in the
        directory `IPYTHONDIR/fortran` using a filename with the hash of the
        code. This file is then compiled. The resulting module
        is imported and all of its symbols are injected into the user's
        namespace.


        Usage
        =====
        Prepend ``%%fortran`` to your fortran code in a cell::

        ``%%fortran

        ! put your code here.
        ``


        """

        try:
            # custom saved arguments
            saved_defaults = vars(
                magic_arguments.parse_argstring(self.fortran,
                                                self.shell.db['fortran']))
            self.fortran.parser.set_defaults(**saved_defaults)
        except KeyError:
            saved_defaults = {'verbosity': 0}

        # verbosity is a "count" argument were each ocurrence is
        # added implicit.
        # so, for instance, -vv in %fortran_config and -vvv in %%fortran means
        # a nonsense verbosity=5.
        # To override: if verbosity is given for the magic cell
        # we ignore the saved config.
        if '-v' in line:
            self.fortran.parser.set_defaults(verbosity=0)

        args = magic_arguments.parse_argstring(self.fortran, line)

        # boolean flags
        f2py_args = ['--%s' % k for k, v in vars(args).items() if v is True]

        try:
            base_str_class = basestring
        except NameError:
            # py3
            base_str_class = str

        kw = [
            '--%s=%s' % (k, v) for k, v in vars(args).items()
            if isinstance(v, base_str_class)
        ]

        f2py_args.extend(kw)

        # link resource
        if args.link:
            resources = ['--link-%s' % r for r in args.link]
            f2py_args.extend(resources)

        if args.extra:
            extras = ' '.join(map(unquote, args.extra))
            extras = extras.split()
            f2py_args.extend(extras)

        code = cell if cell.endswith('\n') else cell + '\n'
        key = code, line, sys.version_info, sys.executable, f2py2e.f2py_version

        module_name = "_fortran_magic_" + \
                      hashlib.md5(str(key).encode('utf-8')).hexdigest()

        module_path = os.path.join(self._lib_dir, module_name + self.so_ext)

        f90_file = os.path.join(self._lib_dir, module_name + '.f90')
        f90_file = py3compat.cast_bytes_py2(
            f90_file, encoding=sys.getfilesystemencoding())
        with io.open(f90_file, 'w', encoding='utf-8') as f:
            f.write(code)

        self._run_f2py(f2py_args + ['-m', module_name, '-c', f90_file],
                       verbosity=args.verbosity)

        self._code_cache[key] = module_name
        module = imp.load_dynamic(module_name, module_path)
        self._import_all(module, verbosity=args.verbosity)

    @property
    def so_ext(self):
        """The extension suffix for compiled modules."""
        try:
            return self._so_ext
        except AttributeError:

            dist = Distribution()
            config_files = dist.find_config_files()
            try:
                config_files.remove('setup.cfg')
            except ValueError:
                pass
            dist.parse_config_files(config_files)
            build_extension = build_ext(dist)
            build_extension.finalize_options()
            self._so_ext = build_extension.get_ext_filename('')
            return self._so_ext
示例#6
0
def rest_arguments(func):
    """Magic arguments shared by `rest` and `rest_root` commands.
    """
    args = (
        magic_arguments.magic_arguments(),
        magic_arguments.argument(
            '--verbose', '-v',
            action='store_true',
            help='Dump full HTTP session log.',
            default=None
        ),
        magic_arguments.argument(
            '--quiet', '-q',
            action='store_true',
            help='Do not print HTTP request and response.',
            default=None
        ),
        magic_arguments.argument(
            '--insecure', '-k',
            action='store_true',
            help='Disable SSL certificate verification.',
            default=None
        ),
        magic_arguments.argument(
            '--proxy',
            type=str,
            action='store',
            dest='proxy',
            help='Sets the proxy server to use for HTTP and HTTPS.',
            default=None
        ),
        magic_arguments.argument(
            '--max-redirects',
            type=int,
            action='store',
            dest='max_redirects',
            help=("Set the maximum number of redirects allowed, "
                  "{0} by default.".format(DEFAULT_REDIRECT_LIMIT)),
            default=None
        ),
        magic_arguments.argument(
            '--timeout',
            type=float,
            action='store',
            dest='timeout',
            help=("Set the maximum number of seconds to wait for a response, "
                  "{0} by default.".format(DEFAULT_TIMEOUT)),
            default=None
        ),
        magic_arguments.argument(
            '--extract', '-e',
            type=str,
            action='store',
            dest='parser_expression',
            metavar='expression',
            help='Extract parts of a response content with the given Xpath/JSONPath expression.',
            default=None
        ),
        magic_arguments.argument(
            '--parser',
            type=str,
            action='store',
            dest='parser',
            help='Set which parser to use to extract parts of a response content.',
            choices=ResponseParser.parsers,
            default=None
        ),
        func
    )
    return functools.reduce(lambda res, f: f(res), reversed(args))