コード例 #1
0
    def __init__(self,
                 name,
                 usage=None,
                 rc=Struct(opts=None, args=None),
                 user_ns=None,
                 user_global_ns=None,
                 banner2='',
                 **kw):
        """Similar to the normal InteractiveShell, but with threading control"""

        InteractiveShell.__init__(self, name, usage, rc, user_ns,
                                  user_global_ns, banner2)

        # A queue to hold the code to be executed.
        self.code_queue = Queue.Queue()

        # Stuff to do at closing time
        self._kill = None
        on_kill = kw.get('on_kill', [])
        # Check that all things to kill are callable:
        for t in on_kill:
            if not callable(t):
                raise TypeError, 'on_kill must be a list of callables'
        self.on_kill = on_kill
        # thread identity of the "worker thread" (that may execute code directly)
        self.worker_ident = None
        self.reactor_started = False
        self.first_run = True
コード例 #2
0
    def highlight(self, parameter_s=''):
        """
        Display the contents of a source code file with syntax highlighting.

        Requires the pygments library.

        Usage:
            %highlight [options] <file name>

        Options:

            -g {'dark', 'light'}: Specify the 'dark' or 'light' color scheme.
                Defaults to 'dark'.

            -l <lexer name>: Manually specify the language of the code using
                any lexer name from http://pygments.org/docs/lexers/.
                By default the source language is guessed from the file name.

        """
        opts_def = Struct(l='', g='dark')
        opts, arg_str = self.parse_options(parameter_s, 'l:g:')
        opts.merge(opts_def)

        if opts.l:
            lexer = get_lexer_by_name(opts.l)
        else:
            lexer = get_lexer_for_filename(arg_str)

        formatter = TerminalFormatter(bg=opts.g)

        with open(arg_str) as f:
            code = f.read()

        print(highlight(code, lexer, formatter))
コード例 #3
0
ファイル: zmqshell.py プロジェクト: GunioRobot/ipython
    def magic_doctest_mode(self, parameter_s=''):
        """Toggle doctest mode on and off.

        This mode is intended to make IPython behave as much as possible like a
        plain Python shell, from the perspective of how its prompts, exceptions
        and output look.  This makes it easy to copy and paste parts of a
        session into doctests.  It does so by:

        - Changing the prompts to the classic ``>>>`` ones.
        - Changing the exception reporting mode to 'Plain'.
        - Disabling pretty-printing of output.

        Note that IPython also supports the pasting of code snippets that have
        leading '>>>' and '...' prompts in them.  This means that you can paste
        doctests from files or docstrings (even if they have leading
        whitespace), and the code will execute correctly.  You can then use
        '%history -t' to see the translated history; this will give you the
        input after removal of all the leading prompts and whitespace, which
        can be pasted back into an editor.

        With these features, you can switch into this mode easily whenever you
        need to do testing and changes to doctests, without having to leave
        your existing IPython session.
        """

        from IPython.utils.ipstruct import Struct

        # Shorthands
        shell = self.shell
        # dstore is a data store kept in the instance metadata bag to track any
        # changes we make, so we can undo them later.
        dstore = shell.meta.setdefault('doctest_mode', Struct())
        save_dstore = dstore.setdefault

        # save a few values we'll need to recover later
        mode = save_dstore('mode', False)
        save_dstore('rc_pprint', shell.pprint)
        save_dstore('xmode', shell.InteractiveTB.mode)

        if mode == False:
            # turn on
            shell.pprint = False
            shell.magic_xmode('Plain')
        else:
            # turn off
            shell.pprint = dstore.rc_pprint
            shell.magic_xmode(dstore.xmode)

        # Store new mode and inform on console
        dstore.mode = bool(1 - int(mode))
        mode_label = ['OFF', 'ON'][dstore.mode]
        print('Doctest mode is:', mode_label)

        # Send the payload back so that clients can modify their prompt display
        payload = dict(
            source=
            'IPython.zmq.zmqshell.ZMQInteractiveShell.magic_doctest_mode',
            mode=dstore.mode)
        self.payload_manager.write_payload(payload)
コード例 #4
0
ファイル: coloransi.py プロジェクト: dhomeier/ipython-py3k
class ColorScheme:
    """Generic color scheme class. Just a name and a Struct."""
    def __init__(self,__scheme_name_,colordict=None,**colormap):
        self.name = __scheme_name_
        if colordict is None:
            self.colors = Struct(**colormap)
        else:
            self.colors = Struct(colordict)

    def copy(self,name=None):
        """Return a full copy of the object, optionally renaming it."""
        if name is None:
            name = self.name
        return ColorScheme(name, self.colors.dict())
コード例 #5
0
class ColorScheme:
    """Generic color scheme class. Just a name and a Struct."""
    def __init__(self, __scheme_name_, colordict=None, **colormap):
        self.name = __scheme_name_
        if colordict is None:
            self.colors = Struct(**colormap)
        else:
            self.colors = Struct(colordict)

    def copy(self, name=None):
        """Return a full copy of the object, optionally renaming it."""
        if name is None:
            name = self.name
        return ColorScheme(name, self.colors.dict())
コード例 #6
0
    def _save_file(code, opts, namespace):
        # read arguments
        opts.merge(Struct(f=None))
        file_name = opts.f

        if not file_name:
            return "Usage: %%save_file -f file_name"

        file_name = file_name[0]

        with open(file_name, 'w') as fileName:
            fileName.write(code)

        print("Saved cell to {}".format(file_name))
        return
コード例 #7
0
    def dispatch_custom_completer(self, text):
        #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg
        line = self.full_lbuf
        if not line.strip():
            return None

        event = Struct()
        event.line = line
        event.symbol = text
        cmd = line.split(None, 1)[0]
        event.command = cmd
        #print "\ncustom:{%s]\n" % event # dbg

        # for foo etc, try also to find completer for %foo
        if not cmd.startswith(self.magic_escape):
            try_magic = self.custom_completers.s_matches(self.magic_escape +
                                                         cmd)
        else:
            try_magic = []

        for c in itertools.chain(
                self.custom_completers.s_matches(cmd), try_magic,
                self.custom_completers.flat_matches(self.lbuf)):
            #print "try",c # dbg
            try:
                res = c(event)
                # first, try case sensitive match
                withcase = [r for r in res if r.startswith(text)]
                if withcase:
                    return withcase
                # if none, then case insensitive ones are ok too
                return [r for r in res if r.lower().startswith(text.lower())]
            except TryNext:
                pass

        return None
コード例 #8
0
    def highlight_html(self, parameter_s=''):
        """
        Display the contents of a source code file with syntax highlighting.

        You must be in an environment that can display HTML output.
        Requires the pygments library.

        Usage:
            %highlight [options] <file name>

        Options:

            -n: Show line numbers.

            -s <style name>: An available Pygments style, default is 'default'.

            -l <lexer name>: Manually specify the language of the code using
                any lexer name from http://pygments.org/docs/lexers/.
                By default the source language is guessed from the file name.

        """
        opts_def = Struct(l='', s='default')
        opts, arg_str = self.parse_options(parameter_s, 'l:s:n')
        opts.merge(opts_def)

        if opts.l:
            lexer = get_lexer_by_name(opts.l)
        else:
            lexer = get_lexer_for_filename(arg_str)

        if 'n' in opts:
            linenos = 'table'
        else:
            linenos = False

        formatter = HtmlFormatter(style=opts.s,
                                  cssclass='pygments' + str(uuid.uuid4()),
                                  linenos=linenos)

        with open(arg_str) as f:
            code = f.read()

        html_code = highlight(code, lexer, formatter)
        css = formatter.get_style_defs()

        html = HTML_TEMPLATE.format(css, html_code)

        display(HTML(html))
コード例 #9
0
ファイル: QtIPy.py プロジェクト: SPRIME01/qtipy
    def run_notebook(self, nb, vars={}):
        if len(nb['worksheets']) == 0:
            nb['worksheets'] = [NotebookNode({'cells': [], 'metadata': {}})]

        start = nb['worksheets'][0]['cells']
        start.insert(
            0,
            Struct(
                **{
                    'cell_type': 'code',
                    'language': 'python',
                    'outputs': [],
                    'collapsed': False,
                    'prompt_number': -1,
                    'input': 'qtipy=%s' % vars,
                    'metadata': {},
                }))
        self.runner.nb = nb

        try:
            self.runner.run_notebook()
        except:
            self.latest_run['success'] = False
            raise
        else:
            self.latest_run['success'] = True
        finally:
            ext = dict(
                html='html',
                slides='slides',
                latex='latex',
                markdown='md',
                python='py',
                rst='rst',
            )

            output, resources = IPyexport(
                IPyexporter_map[self.config.get('output_format')],
                self.runner.nb)
            output_path = vars['output_path'] + 'notebook.%s' % ext[
                self.config.get('output_format')]
            logging.info("Exporting updated notebook to %s" % output_path)
            with open(output_path, "w") as f:
                f.write(output)
コード例 #10
0
def init_db(parameter_s=''):
    ip = get_ipapi()
    user_ns = get_user_ns()
    global _DB_SYMB
    old_db = user_ns.get(_DB_SYMB)

    db = __get_db(parameter_s)

    if old_db is not None and hasattr(old_db, "_db_cache"):
        old_junk = old_db._db_cache["junk"].keys()
        for e in old_junk:
            del user_ns[e]
    else:
        old_junk = ()

    if db is None:
        return

    os.environ["TANGO_HOST"] = "%s:%s" % (db.get_db_host(), db.get_db_port())

    # Initialize device and server information
    query = "SELECT name, alias, server, class FROM device order by name"

    r = db.command_inout("DbMySqlSelect", query)
    row_nb, column_nb = r[0][-2], r[0][-1]
    data = r[1]
    assert row_nb == len(data) / column_nb
    devices, aliases, servers, klasses = data[0::4], data[1::4], data[
        2::4], data[3::4]

    # CD = tango.utils.CaselessDict
    CD = dict
    dev_dict, serv_dict, klass_dict, alias_dict = CD(), CD(), CD(), CD()

    for device, alias, server, klass in zip(devices, aliases, servers,
                                            klasses):
        dev_lower = device.lower()

        # hide dserver devices
        if dev_lower.startswith("dserver/"): continue

        # hide alias that start with "_"
        if alias and alias[0] == "_": alias = ''

        # last None below is to be filled by DeviceProxy object on demand
        # last empty dict<str, int> where keys is attribute name and value is
        # the subscription id
        dev_dict[device] = [alias, server, klass, None, {}]
        serv_devs = serv_dict.get(server)
        if serv_devs is None:
            serv_dict[server] = serv_devs = []
        serv_devs.append(device)
        klass_devs = klass_dict.get(klass)
        if klass_devs is None:
            klass_dict[klass] = klass_devs = []
        klass_devs.append(device)
        if len(alias):
            alias_dict[alias] = device
            serv_devs.append(alias)
            klass_devs.append(alias)

    exposed_klasses = {}
    excluded_klasses = "DServer",
    for klass, devices in klass_dict.items():
        if klass in excluded_klasses:
            continue
        exists = klass in user_ns
        if not exists or klass in old_junk:
            c = DeviceClassCompleter(klass, devices)
            ip.set_hook('complete_command',
                        c,
                        re_key=".*" + klass + "[^\w\.]+")
            exposed_klasses[klass] = tango.DeviceProxy

    # expose classes no user namespace
    user_ns.update(exposed_klasses)

    # Initialize attribute information
    query = "SELECT name, alias FROM attribute_alias order by alias"

    r = db.command_inout("DbMySqlSelect", query)
    row_nb, column_nb = r[0][-2], r[0][-1]
    data = r[1]
    assert row_nb == len(data) / column_nb
    attributes, aliases = data[0::2], data[1::2]

    attr_alias_dict = {}
    for attribute, alias in zip(attributes, aliases):
        if len(alias):
            attr_alias_dict[alias] = attribute

    device_list = tango.utils.CaselessList(dev_dict.keys())
    alias_list = tango.utils.CaselessList(alias_dict.keys())
    attr_alias_list = tango.utils.CaselessList(attr_alias_dict.keys())

    # Build cache
    db_cache = Struct(devices=dev_dict,
                      aliases=alias_dict,
                      servers=serv_dict,
                      klasses=klass_dict,
                      junk=exposed_klasses,
                      attr_aliases=attr_alias_dict,
                      device_list=device_list,
                      alias_list=alias_list,
                      attr_alias_list=attr_alias_list)

    db._db_cache = db_cache

    # Add this DB to the list of known DBs (for possible use in magic commands)
    if db.get_db_port_num() == 10000:
        db_name = db.get_db_host()
    else:
        db_name = "%s:%s" % (db.get_db_host(), db.get_db_port())
    return db
コード例 #11
0
ファイル: basic.py プロジェクト: gear2000/jiffy-base-venv
    def doctest_mode(self, parameter_s=''):
        """Toggle doctest mode on and off.

        This mode is intended to make IPython behave as much as possible like a
        plain Python shell, from the perspective of how its prompts, exceptions
        and output look.  This makes it easy to copy and paste parts of a
        session into doctests.  It does so by:

        - Changing the prompts to the classic ``>>>`` ones.
        - Changing the exception reporting mode to 'Plain'.
        - Disabling pretty-printing of output.

        Note that IPython also supports the pasting of code snippets that have
        leading '>>>' and '...' prompts in them.  This means that you can paste
        doctests from files or docstrings (even if they have leading
        whitespace), and the code will execute correctly.  You can then use
        '%history -t' to see the translated history; this will give you the
        input after removal of all the leading prompts and whitespace, which
        can be pasted back into an editor.

        With these features, you can switch into this mode easily whenever you
        need to do testing and changes to doctests, without having to leave
        your existing IPython session.
        """

        # Shorthands
        shell = self.shell
        pm = shell.prompt_manager
        meta = shell.meta
        disp_formatter = self.shell.display_formatter
        ptformatter = disp_formatter.formatters['text/plain']
        # dstore is a data store kept in the instance metadata bag to track any
        # changes we make, so we can undo them later.
        dstore = meta.setdefault('doctest_mode',Struct())
        save_dstore = dstore.setdefault

        # save a few values we'll need to recover later
        mode = save_dstore('mode',False)
        save_dstore('rc_pprint',ptformatter.pprint)
        save_dstore('xmode',shell.InteractiveTB.mode)
        save_dstore('rc_separate_out',shell.separate_out)
        save_dstore('rc_separate_out2',shell.separate_out2)
        save_dstore('rc_prompts_pad_left',pm.justify)
        save_dstore('rc_separate_in',shell.separate_in)
        save_dstore('rc_active_types',disp_formatter.active_types)
        save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template))

        if mode == False:
            # turn on
            pm.in_template = '>>> '
            pm.in2_template = '... '
            pm.out_template = ''

            # Prompt separators like plain python
            shell.separate_in = ''
            shell.separate_out = ''
            shell.separate_out2 = ''

            pm.justify = False

            ptformatter.pprint = False
            disp_formatter.active_types = ['text/plain']

            shell.magic('xmode Plain')
        else:
            # turn off
            pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates

            shell.separate_in = dstore.rc_separate_in

            shell.separate_out = dstore.rc_separate_out
            shell.separate_out2 = dstore.rc_separate_out2

            pm.justify = dstore.rc_prompts_pad_left

            ptformatter.pprint = dstore.rc_pprint
            disp_formatter.active_types = dstore.rc_active_types

            shell.magic('xmode ' + dstore.xmode)

        # Store new mode and inform
        dstore.mode = bool(1-int(mode))
        mode_label = ['OFF','ON'][dstore.mode]
        print('Doctest mode is:', mode_label)
コード例 #12
0
    def lprun(self, parameter_s=''):
        """ Execute a statement under the line-by-line profiler from the
        line_profiler module.

        Usage:
          %lprun -f func1 -f func2 <statement>

        The given statement (which doesn't require quote marks) is run via the
        LineProfiler. Profiling is enabled for the functions specified by the -f
        options. The statistics will be shown side-by-side with the code through the
        pager once the statement has completed.

        Options:

        -f <function>: LineProfiler only profiles functions and methods it is told
        to profile.  This option tells the profiler about these functions. Multiple
        -f options may be used. The argument may be any expression that gives
        a Python function or method object. However, one must be careful to avoid
        spaces that may confuse the option parser.

        -m <module>: Get all the functions/methods in a module

        One or more -f or -m options are required to get any useful results.

        -D <filename>: dump the raw statistics out to a pickle file on disk. The
        usual extension for this is ".lprof". These statistics may be viewed later
        by running line_profiler.py as a script.

        -T <filename>: dump the text-formatted statistics with the code side-by-side
        out to a text file.

        -r: return the LineProfiler object after it has completed profiling.

        -s: strip out all entries from the print-out that have zeros.

        -u: specify time unit for the print-out in seconds.
        """

        # Escape quote markers.
        opts_def = Struct(D=[''], T=[''], f=[], m=[], u=None)
        parameter_s = parameter_s.replace('"', r'\"').replace("'", r"\'")
        opts, arg_str = self.parse_options(parameter_s,
                                           'rsf:m:D:T:u:',
                                           list_all=True)
        opts.merge(opts_def)

        global_ns = self.shell.user_global_ns
        local_ns = self.shell.user_ns

        # Get the requested functions.
        funcs = []
        for name in opts.f:
            try:
                funcs.append(eval(name, global_ns, local_ns))
            except Exception as e:
                raise UsageError('Could not find function %r.\n%s: %s' %
                                 (name, e.__class__.__name__, e))

        profile = LineProfiler(*funcs)

        # Get the modules, too
        for modname in opts.m:
            try:
                mod = __import__(modname, fromlist=[''])
                profile.add_module(mod)
            except Exception as e:
                raise UsageError('Could not find module %r.\n%s: %s' %
                                 (modname, e.__class__.__name__, e))

        if opts.u is not None:
            try:
                output_unit = float(opts.u[0])
            except Exception as e:
                raise TypeError("Timer unit setting must be a float.")
        else:
            output_unit = None

        # Add the profiler to the builtins for @profile.
        if PY3:
            import builtins
        else:
            import __builtin__ as builtins

        if 'profile' in builtins.__dict__:
            had_profile = True
            old_profile = builtins.__dict__['profile']
        else:
            had_profile = False
            old_profile = None
        builtins.__dict__['profile'] = profile

        try:
            try:
                profile.runctx(arg_str, global_ns, local_ns)
                message = ''
            except SystemExit:
                message = """*** SystemExit exception caught in code being profiled."""
            except KeyboardInterrupt:
                message = (
                    "*** KeyboardInterrupt exception caught in code being "
                    "profiled.")
        finally:
            if had_profile:
                builtins.__dict__['profile'] = old_profile

        # Trap text output.
        stdout_trap = StringIO()
        profile.print_stats(stdout_trap,
                            output_unit=output_unit,
                            stripzeros='s' in opts)
        output = stdout_trap.getvalue()
        output = output.rstrip()

        page(output)
        print(message, end="")

        dump_file = opts.D[0]
        if dump_file:
            profile.dump_stats(dump_file)
            print('\n*** Profile stats pickled to file %r. %s' %
                  (dump_file, message))

        text_file = opts.T[0]
        if text_file:
            pfile = open(text_file, 'w')
            pfile.write(output)
            pfile.close()
            print('\n*** Profile printout saved to text file %r. %s' %
                  (text_file, message))

        return_value = None
        if 'r' in opts:
            return_value = profile

        return return_value
コード例 #13
0
    def _run_with_profiler(self, code, opts, namespace):
        """
        Run `code` with profiler.  Used by ``%prun`` and ``%run -p``.

        Parameters
        ----------
        code : str
            Code to be executed.
        opts : Struct
            Options parsed by `self.parse_options`.
        namespace : dict
            A dictionary for Python namespace (e.g., `self.shell.user_ns`).

        """

        # Fill default values for unspecified options:
        opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))

        prof = profile.Profile()
        try:
            prof = prof.runctx(code, namespace, namespace)
            sys_exit = ''
        except SystemExit:
            sys_exit = """*** SystemExit exception caught in code being profiled."""

        stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)

        lims = opts.l
        if lims:
            lims = []  # rebuild lims with ints/floats/strings
            for lim in opts.l:
                try:
                    lims.append(int(lim))
                except ValueError:
                    try:
                        lims.append(float(lim))
                    except ValueError:
                        lims.append(lim)

        # Trap output.
        stdout_trap = StringIO()
        stats_stream = stats.stream
        try:
            stats.stream = stdout_trap
            stats.print_stats(*lims)
        finally:
            stats.stream = stats_stream

        output = stdout_trap.getvalue()
        output = output.rstrip()

        if 'q' not in opts:
            page.page(output)
        print sys_exit,

        dump_file = opts.D[0]
        text_file = opts.T[0]
        if dump_file:
            dump_file = unquote_filename(dump_file)
            prof.dump_stats(dump_file)
            print '\n*** Profile stats marshalled to file',\
                  repr(dump_file)+'.',sys_exit
        if text_file:
            text_file = unquote_filename(text_file)
            pfile = open(text_file, 'w')
            pfile.write(output)
            pfile.close()
            print '\n*** Profile printout saved to text file',\
                  repr(text_file)+'.',sys_exit

        if 'r' in opts:
            return stats
        else:
            return None
コード例 #14
0
ファイル: coloransi.py プロジェクト: dhomeier/ipython-py3k
 def __init__(self,__scheme_name_,colordict=None,**colormap):
     self.name = __scheme_name_
     if colordict is None:
         self.colors = Struct(**colormap)
     else:
         self.colors = Struct(colordict)
コード例 #15
0
class Magics(object):
    """Base class for implementing magic functions.

    Shell functions which can be reached as %function_name. All magic
    functions should accept a string, which they can parse for their own
    needs. This can make some functions easier to type, eg `%cd ../`
    vs. `%cd("../")`

    Classes providing magic functions need to subclass this class, and they
    MUST:

    - Use the method decorators `@line_magic` and `@cell_magic` to decorate
    individual methods as magic functions, AND

    - Use the class decorator `@magics_class` to ensure that the magic
    methods are properly registered at the instance level upon instance
    initialization.

    See :mod:`magic_functions` for examples of actual implementation classes.
    """
    # Dict holding all command-line options for each magic.
    options_table = None
    # Dict for the mapping of magic names to methods, set by class decorator
    magics = None
    # Flag to check that the class decorator was properly applied
    registered = False
    # Instance of IPython shell
    shell = None

    def __init__(self, shell):
        if not (self.__class__.registered):
            raise ValueError('Magics subclass without registration - '
                             'did you forget to apply @magics_class?')
        self.shell = shell
        self.options_table = {}
        # The method decorators are run when the instance doesn't exist yet, so
        # they can only record the names of the methods they are supposed to
        # grab.  Only now, that the instance exists, can we create the proper
        # mapping to bound methods.  So we read the info off the original names
        # table and replace each method name by the actual bound method.
        # But we mustn't clobber the *class* mapping, in case of multiple instances.
        class_magics = self.magics
        self.magics = {}
        for mtype in magic_kinds:
            tab = self.magics[mtype] = {}
            cls_tab = class_magics[mtype]
            for magic_name, meth_name in cls_tab.iteritems():
                if isinstance(meth_name, basestring):
                    # it's a method name, grab it
                    tab[magic_name] = getattr(self, meth_name)
                else:
                    # it's the real thing
                    tab[magic_name] = meth_name

    def arg_err(self, func):
        """Print docstring if incorrect arguments were passed"""
        print 'Error in arguments:'
        print oinspect.getdoc(func)

    def format_latex(self, strng):
        """Format a string for latex inclusion."""

        # Characters that need to be escaped for latex:
        escape_re = re.compile(r'(%|_|\$|#|&)', re.MULTILINE)
        # Magic command names as headers:
        cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC, re.MULTILINE)
        # Magic commands
        cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
                            re.MULTILINE)
        # Paragraph continue
        par_re = re.compile(r'\\$', re.MULTILINE)

        # The "\n" symbol
        newline_re = re.compile(r'\\n')

        # Now build the string for output:
        #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
        strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
                                strng)
        strng = cmd_re.sub(r'\\texttt{\g<cmd>}', strng)
        strng = par_re.sub(r'\\\\', strng)
        strng = escape_re.sub(r'\\\1', strng)
        strng = newline_re.sub(r'\\textbackslash{}n', strng)
        return strng

    def parse_options(self, arg_str, opt_str, *long_opts, **kw):
        """Parse options passed to an argument string.

        The interface is similar to that of getopt(), but it returns back a
        Struct with the options as keys and the stripped argument string still
        as a string.

        arg_str is quoted as a true sys.argv vector by using shlex.split.
        This allows us to easily expand variables, glob files, quote
        arguments, etc.

        Options:
          -mode: default 'string'. If given as 'list', the argument string is
          returned as a list (split on whitespace) instead of a string.

          -list_all: put all option values in lists. Normally only options
          appearing more than once are put in a list.

          -posix (True): whether to split the input line in POSIX mode or not,
          as per the conventions outlined in the shlex module from the
          standard library."""

        # inject default options at the beginning of the input line
        caller = sys._getframe(1).f_code.co_name
        arg_str = '%s %s' % (self.options_table.get(caller, ''), arg_str)

        mode = kw.get('mode', 'string')
        if mode not in ['string', 'list']:
            raise ValueError, 'incorrect mode given: %s' % mode
        # Get options
        list_all = kw.get('list_all', 0)
        posix = kw.get('posix', os.name == 'posix')
        strict = kw.get('strict', True)

        # Check if we have more than one argument to warrant extra processing:
        odict = {}  # Dictionary with options
        args = arg_str.split()
        if len(args) >= 1:
            # If the list of inputs only has 0 or 1 thing in it, there's no
            # need to look for options
            argv = arg_split(arg_str, posix, strict)
            # Do regular option processing
            try:
                opts, args = getopt(argv, opt_str, long_opts)
            except GetoptError, e:
                raise UsageError('%s ( allowed: "%s" %s)' %
                                 (e.msg, opt_str, " ".join(long_opts)))
            for o, a in opts:
                if o.startswith('--'):
                    o = o[2:]
                else:
                    o = o[1:]
                try:
                    odict[o].append(a)
                except AttributeError:
                    odict[o] = [odict[o], a]
                except KeyError:
                    if list_all:
                        odict[o] = [a]
                    else:
                        odict[o] = a

        # Prepare opts,args for return
        opts = Struct(odict)
        if mode == 'string':
            args = ' '.join(args)

        return opts, args
コード例 #16
0
 def fake_open(arg):
     open_called_with.append(arg)
     return Struct(show=lambda: show_called_with.append(None))
コード例 #17
0
 def __init__(self, __scheme_name_, colordict=None, **colormap):
     self.name = __scheme_name_
     if colordict is None:
         self.colors = Struct(**colormap)
     else:
         self.colors = Struct(colordict)
コード例 #18
0
def load_config(config):
    import itango
    import IPython.utils.coloransi

    d = {
        "version": str(common.get_pytango_version()),
        "pyver": str(common.get_python_version()),
        "ipyver": str(common.get_ipython_version()),
        "pytangover": str(common.get_pytango_version())
    }
    d.update(IPython.utils.coloransi.TermColors.__dict__)

    so = Struct(
        tango_banner=
        """%(Blue)shint: Try typing: mydev = Device("%(LightBlue)s<tab>%(Normal)s\n"""
    )

    so = config.get("tango_options", so)

    # ------------------------------------
    # Application
    # ------------------------------------
    app = config.Application
    app.log_level = 30

    # ------------------------------------
    # InteractiveShell
    # ------------------------------------
    i_shell = config.InteractiveShell
    i_shell.colors = 'Linux'

    # ------------------------------------
    # PromptManager (ipython >= 0.12)
    # ------------------------------------
    prompt = config.PromptManager
    prompt.in_template = 'ITango [\\#]: '
    prompt.out_template = 'Result [\\#]: '

    # ------------------------------------
    # InteractiveShellApp
    # ------------------------------------
    i_shell_app = config.InteractiveShellApp
    extensions = getattr(i_shell_app, 'extensions', [])
    extensions.append('itango')
    i_shell_app.extensions = extensions

    # ------------------------------------
    # TerminalIPythonApp: options for the IPython terminal (and not Qt Console)
    # ------------------------------------
    term_app = config.TerminalIPythonApp
    term_app.display_banner = True
    #term_app.nosep = False
    #term_app.classic = True

    # ------------------------------------
    # IPKernelApp: options for the  Qt Console
    # ------------------------------------
    #kernel_app = config.IPKernelApp
    ipython_widget = config.IPythonWidget
    ipython_widget.in_prompt = 'ITango [<span class="in-prompt-number">%i</span>]: '
    ipython_widget.out_prompt = 'Result [<span class="out-prompt-number">%i</span>]: '

    #zmq_i_shell = config.ZMQInteractiveShell

    # ------------------------------------
    # TerminalInteractiveShell
    # ------------------------------------
    term_i_shell = config.TerminalInteractiveShell
    banner = """\
%(Purple)sITango %(version)s%(Normal)s -- An interactive %(Purple)sTango%(Normal)s client.

Running on top of Python %(pyver)s, IPython %(ipyver)s and PyTango %(pytangover)s

help      -> ITango's help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.
"""

    banner = banner % d
    banner = banner.format(**d)
    tango_banner = so.tango_banner % d
    tango_banner = tango_banner.format(**d)
    all_banner = "\n".join((banner, tango_banner))

    term_i_shell.banner1 = banner
    term_i_shell.banner2 = tango_banner

    # ------------------------------------
    # FrontendWidget
    # ------------------------------------
    frontend_widget = config.ITangoConsole
    frontend_widget.banner = all_banner
コード例 #19
0
ファイル: execution.py プロジェクト: waldman/ipython
    def prun(self,
             parameter_s='',
             cell=None,
             user_mode=True,
             opts=None,
             arg_lst=None,
             prog_ns=None):
        """Run a statement through the python code profiler.

        Usage, in line mode:
          %prun [options] statement

        Usage, in cell mode:
          %%prun [options] [statement]
          code...
          code...

        In cell mode, the additional code lines are appended to the (possibly
        empty) statement in the first line.  Cell mode allows you to easily
        profile multiline blocks without having to put them in a separate
        function.
        
        The given statement (which doesn't require quote marks) is run via the
        python profiler in a manner similar to the profile.run() function.
        Namespaces are internally managed to work correctly; profile.run
        cannot be used in IPython because it makes certain assumptions about
        namespaces which do not hold under IPython.

        Options:

        -l <limit>: you can place restrictions on what or how much of the
        profile gets printed. The limit value can be:

          * A string: only information for function names containing this string
          is printed.

          * An integer: only these many lines are printed.

          * A float (between 0 and 1): this fraction of the report is printed
          (for example, use a limit of 0.4 to see the topmost 40% only).

        You can combine several limits with repeated use of the option. For
        example, '-l __init__ -l 5' will print only the topmost 5 lines of
        information about class constructors.

        -r: return the pstats.Stats object generated by the profiling. This
        object has all the information about the profile in it, and you can
        later use it for further analysis or in other functions.

       -s <key>: sort profile by given key. You can provide more than one key
        by using the option several times: '-s key1 -s key2 -s key3...'. The
        default sorting key is 'time'.

        The following is copied verbatim from the profile documentation
        referenced below:

        When more than one key is provided, additional keys are used as
        secondary criteria when the there is equality in all keys selected
        before them.

        Abbreviations can be used for any key names, as long as the
        abbreviation is unambiguous.  The following are the keys currently
        defined:

                Valid Arg       Meaning
                  "calls"      call count
                  "cumulative" cumulative time
                  "file"       file name
                  "module"     file name
                  "pcalls"     primitive call count
                  "line"       line number
                  "name"       function name
                  "nfl"        name/file/line
                  "stdname"    standard name
                  "time"       internal time

        Note that all sorts on statistics are in descending order (placing
        most time consuming items first), where as name, file, and line number
        searches are in ascending order (i.e., alphabetical). The subtle
        distinction between "nfl" and "stdname" is that the standard name is a
        sort of the name as printed, which means that the embedded line
        numbers get compared in an odd way.  For example, lines 3, 20, and 40
        would (if the file names were the same) appear in the string order
        "20" "3" and "40".  In contrast, "nfl" does a numeric compare of the
        line numbers.  In fact, sort_stats("nfl") is the same as
        sort_stats("name", "file", "line").

        -T <filename>: save profile results as shown on screen to a text
        file. The profile is still shown on screen.

        -D <filename>: save (via dump_stats) profile statistics to given
        filename. This data is in a format understood by the pstats module, and
        is generated by a call to the dump_stats() method of profile
        objects. The profile is still shown on screen.

        -q: suppress output to the pager.  Best used with -T and/or -D above.

        If you want to run complete programs under the profiler's control, use
        '%run -p [prof_opts] filename.py [args to program]' where prof_opts
        contains profiler specific options as described here.

        You can read the complete documentation for the profile module with::

          In [1]: import profile; profile.help()
        """

        opts_def = Struct(D=[''], l=[], s=['time'], T=[''])

        if user_mode:  # regular user call
            opts, arg_str = self.parse_options(parameter_s,
                                               'D:l:rs:T:q',
                                               list_all=True,
                                               posix=False)
            namespace = self.shell.user_ns
            if cell is not None:
                arg_str += '\n' + cell
        else:  # called to run a program by %run -p
            try:
                filename = get_py_filename(arg_lst[0])
            except IOError as e:
                try:
                    msg = str(e)
                except UnicodeError:
                    msg = e.message
                error(msg)
                return

            arg_str = 'execfile(filename,prog_ns)'
            namespace = {
                'execfile': self.shell.safe_execfile,
                'prog_ns': prog_ns,
                'filename': filename
            }

        opts.merge(opts_def)

        prof = profile.Profile()
        try:
            prof = prof.runctx(arg_str, namespace, namespace)
            sys_exit = ''
        except SystemExit:
            sys_exit = """*** SystemExit exception caught in code being profiled."""

        stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)

        lims = opts.l
        if lims:
            lims = []  # rebuild lims with ints/floats/strings
            for lim in opts.l:
                try:
                    lims.append(int(lim))
                except ValueError:
                    try:
                        lims.append(float(lim))
                    except ValueError:
                        lims.append(lim)

        # Trap output.
        stdout_trap = StringIO()
        stats_stream = stats.stream
        try:
            stats.stream = stdout_trap
            stats.print_stats(*lims)
        finally:
            stats.stream = stats_stream

        output = stdout_trap.getvalue()
        output = output.rstrip()

        if 'q' not in opts:
            page.page(output)
        print sys_exit,

        dump_file = opts.D[0]
        text_file = opts.T[0]
        if dump_file:
            dump_file = unquote_filename(dump_file)
            prof.dump_stats(dump_file)
            print '\n*** Profile stats marshalled to file',\
                  repr(dump_file)+'.',sys_exit
        if text_file:
            text_file = unquote_filename(text_file)
            pfile = open(text_file, 'w')
            pfile.write(output)
            pfile.close()
            print '\n*** Profile printout saved to text file',\
                  repr(text_file)+'.',sys_exit

        if 'r' in opts:
            return stats
        else:
            return None
コード例 #20
0
    def parse_options(self, arg_str, opt_str, *long_opts, **kw):
        """Parse options passed to an argument string.

        The interface is similar to that of :func:`getopt.getopt`, but it
        returns a :class:`~IPython.utils.struct.Struct` with the options as keys
        and the stripped argument string still as a string.

        arg_str is quoted as a true sys.argv vector by using shlex.split.
        This allows us to easily expand variables, glob files, quote
        arguments, etc.

        Parameters
        ----------

        arg_str : str
          The arguments to parse.

        opt_str : str
          The options specification.

        mode : str, default 'string'
          If given as 'list', the argument string is returned as a list (split
          on whitespace) instead of a string.

        list_all : bool, default False
          Put all option values in lists. Normally only options
          appearing more than once are put in a list.

        posix : bool, default True
          Whether to split the input line in POSIX mode or not, as per the
          conventions outlined in the :mod:`shlex` module from the standard
          library.
        """

        # inject default options at the beginning of the input line
        caller = sys._getframe(1).f_code.co_name
        arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)

        mode = kw.get('mode','string')
        if mode not in ['string','list']:
            raise ValueError('incorrect mode given: %s' % mode)
        # Get options
        list_all = kw.get('list_all',0)
        posix = kw.get('posix', os.name == 'posix')
        strict = kw.get('strict', True)

        # Check if we have more than one argument to warrant extra processing:
        odict = {}  # Dictionary with options
        args = arg_str.split()
        if len(args) >= 1:
            # If the list of inputs only has 0 or 1 thing in it, there's no
            # need to look for options
            argv = arg_split(arg_str, posix, strict)
            # Do regular option processing
            try:
                opts,args = getopt(argv, opt_str, long_opts)
            except GetoptError as e:
                raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
                                        " ".join(long_opts)))
            for o,a in opts:
                if o.startswith('--'):
                    o = o[2:]
                else:
                    o = o[1:]
                try:
                    odict[o].append(a)
                except AttributeError:
                    odict[o] = [odict[o],a]
                except KeyError:
                    if list_all:
                        odict[o] = [a]
                    else:
                        odict[o] = a

        # Prepare opts,args for return
        opts = Struct(odict)
        if mode == 'string':
            args = ' '.join(args)

        return opts,args
コード例 #21
0
    def doctest_mode(self, parameter_s=""):
        """Toggle doctest mode on and off.

        This mode is intended to make IPython behave as much as possible like a
        plain Python shell, from the perspective of how its prompts, exceptions
        and output look.  This makes it easy to copy and paste parts of a
        session into doctests.  It does so by:

        - Changing the prompts to the classic ``>>>`` ones.
        - Changing the exception reporting mode to 'Plain'.
        - Disabling pretty-printing of output.

        Note that IPython also supports the pasting of code snippets that have
        leading '>>>' and '...' prompts in them.  This means that you can paste
        doctests from files or docstrings (even if they have leading
        whitespace), and the code will execute correctly.  You can then use
        '%history -t' to see the translated history; this will give you the
        input after removal of all the leading prompts and whitespace, which
        can be pasted back into an editor.

        With these features, you can switch into this mode easily whenever you
        need to do testing and changes to doctests, without having to leave
        your existing IPython session.
        """

        # Shorthands
        shell = self.shell
        meta = shell.meta
        disp_formatter = self.shell.display_formatter
        ptformatter = disp_formatter.formatters["text/plain"]
        # dstore is a data store kept in the instance metadata bag to track any
        # changes we make, so we can undo them later.
        dstore = meta.setdefault("doctest_mode", Struct())
        save_dstore = dstore.setdefault

        # save a few values we'll need to recover later
        mode = save_dstore("mode", False)
        save_dstore("rc_pprint", ptformatter.pprint)
        save_dstore("xmode", shell.InteractiveTB.mode)
        save_dstore("rc_separate_out", shell.separate_out)
        save_dstore("rc_separate_out2", shell.separate_out2)
        save_dstore("rc_separate_in", shell.separate_in)
        save_dstore("rc_active_types", disp_formatter.active_types)

        if not mode:
            # turn on

            # Prompt separators like plain python
            shell.separate_in = ""
            shell.separate_out = ""
            shell.separate_out2 = ""

            ptformatter.pprint = False
            disp_formatter.active_types = ["text/plain"]

            shell.magic("xmode Plain")
        else:
            # turn off
            shell.separate_in = dstore.rc_separate_in

            shell.separate_out = dstore.rc_separate_out
            shell.separate_out2 = dstore.rc_separate_out2

            ptformatter.pprint = dstore.rc_pprint
            disp_formatter.active_types = dstore.rc_active_types

            shell.magic("xmode " + dstore.xmode)

        # mode here is the state before we switch; switch_doctest_mode takes
        # the mode we're switching to.
        shell.switch_doctest_mode(not mode)

        # Store new mode and inform
        dstore.mode = bool(not mode)
        mode_label = ["OFF", "ON"][dstore.mode]
        print("Doctest mode is:", mode_label)