for range_str in ranges_str.split():<EOL><INDENT>rmatch = range_re.match(range_str)<EOL>if not rmatch:<EOL><INDENT>continue<EOL><DEDENT>start = int(rmatch.group("<STR_LIT:start>"))<EOL>end = rmatch.group("<STR_LIT:end>")<EOL>end = int(end) if end else start+<NUM_LIT:1>   <EOL>if rmatch.group("<STR_LIT>") == "<STR_LIT:->":       <EOL><INDENT>end += <NUM_LIT:1><EOL><DEDENT>startsess = rmatch.group("<STR_LIT>") or "<STR_LIT:0>"<EOL>endsess = rmatch.group("<STR_LIT>") or startsess<EOL>startsess = int(startsess.replace("<STR_LIT>","<STR_LIT:->"))<EOL>endsess = int(endsess.replace("<STR_LIT>","<STR_LIT:->"))<EOL>assert endsess >= startsess<EOL>if endsess == startsess:<EOL><INDENT>yield (startsess, start, end)<EOL>continue<EOL><DEDENT>yield (startsess, start, None)<EOL>for sess in range(startsess+<NUM_LIT:1>, endsess):<EOL><INDENT>yield (sess, <NUM_LIT:1>, None)<EOL><DEDENT>yield (endsess, <NUM_LIT:1>, end)<EOL><DEDENT>

Turn a string of history ranges into 3-tuples of (session, start, stop).

    Examples
    --------
    list(extract_input_ranges("~8/5-~7/4 2"))
    [(-8, 5, None), (-7, 1, 4), (0, 2, 3)]


if session == <NUM_LIT:0>:<EOL><INDENT>return str(line)<EOL><DEDENT>return "<STR_LIT>" % (session, line)<EOL>

Helper function to format line numbers properly.


def __init__(self, profile='<STR_LIT:default>', hist_file=u'<STR_LIT>', config=None, **traits):

<EOL>super(HistoryAccessor, self).__init__(config=config, **traits)<EOL>if hist_file:<EOL><INDENT>self.hist_file = hist_file<EOL><DEDENT>if self.hist_file == u'<STR_LIT>':<EOL><INDENT>self.hist_file = self._get_hist_file_name(profile)<EOL><DEDENT>if sqlite3 is None:<EOL><INDENT>warn("<STR_LIT>")<EOL>self.db = DummyDB()<EOL>return<EOL><DEDENT>try:<EOL><INDENT>self.init_db()<EOL><DEDENT>except sqlite3.DatabaseError:<EOL><INDENT>if os.path.isfile(self.hist_file):<EOL><INDENT>base,ext = os.path.splitext(self.hist_file)<EOL>newpath = base + '<STR_LIT>' + ext<EOL>os.rename(self.hist_file, newpath)<EOL>print("<STR_LIT>",<EOL>"<STR_LIT>" % newpath, "<STR_LIT>")<EOL>self.init_db()<EOL><DEDENT>else:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>

Create a new history accessor.

        Parameters
        ----------
        profile : str
          The name of the profile from which to open history.
        hist_file : str
          Path to an SQLite history database stored by IPython. If specified,
          hist_file overrides profile.
        config :
          Config object. hist_file can also be set through this.


return os.path.join(locate_profile(profile), '<STR_LIT>')<EOL>

Find the history file for the given profile name.

        This is overridden by the HistoryManager subclass, to use the shell's
        active profile.

        Parameters
        ----------
        profile : str
          The name of a profile which has a history file.


Connect to the database, and create tables if necessary.


pass<EOL>

Overridden by HistoryManager to dump the cache before certain
        database lookups.


def _run_sql(self, sql, params, raw=True, output=False):

toget = '<STR_LIT>' if raw else '<STR_LIT:source>'<EOL>sqlfrom = "<STR_LIT>"<EOL>if output:<EOL><INDENT>sqlfrom = "<STR_LIT>"<EOL>toget = "<STR_LIT>" % toget<EOL><DEDENT>cur = self.db.execute("<STR_LIT>" %(toget, sqlfrom) + sql, params)<EOL>if output:    <EOL><INDENT>return ((ses, lin, (inp, out)) for ses, lin, inp, out in cur)<EOL><DEDENT>return cur<EOL>

Prepares and runs an SQL query for the history database.

        Parameters
        ----------
        sql : str
          Any filtering expressions to go after SELECT ... FROM ...
        params : tuple
          Parameters passed to the SQL query (to replace "?")
        raw, output : bool
          See :meth:`get_range`

        Returns
        -------
        Tuples as :meth:`get_range`


if session <= <NUM_LIT:0>:<EOL><INDENT>session += self.session_number<EOL><DEDENT>query = "<STR_LIT>"<EOL>return self.db.execute(query, (session,)).fetchone()<EOL>

get info about a session

        Parameters
        ----------

        session : int
            Session number to retrieve. The current session is 0, and negative
            numbers count back from current session, so -1 is previous session.

        Returns
        -------

        (session_id [int], start [datetime], end [datetime], num_cmds [int],
        remark [unicode])

        Sessions that are running or did not exit cleanly will have `end=None`
        and `num_cmds=None`.


def search(self, pattern="<STR_LIT:*>", raw=True, search_raw=True,<EOL>output=False):

tosearch = "<STR_LIT>" if search_raw else "<STR_LIT:source>"<EOL>if output:<EOL><INDENT>tosearch = "<STR_LIT>" + tosearch<EOL><DEDENT>self.writeout_cache()<EOL>return self._run_sql("<STR_LIT>" % tosearch, (pattern,),<EOL>raw=raw, output=output)<EOL>

Search the database using unix glob-style matching (wildcards
        * and ?).

        Parameters
        ----------
        pattern : str
          The wildcarded pattern to match when searching
        search_raw : bool
          If True, search the raw input, otherwise, the parsed input
        raw, output : bool
          See :meth:`get_range`

        Returns
        -------
        Tuples as :meth:`get_range`


def get_range(self, session, start=<NUM_LIT:1>, stop=None, raw=True,output=False):

if stop:<EOL><INDENT>lineclause = "<STR_LIT>"<EOL>params = (session, start, stop)<EOL><DEDENT>else:<EOL><INDENT>lineclause = "<STR_LIT>"<EOL>params = (session, start)<EOL><DEDENT>return self._run_sql("<STR_LIT>""<STR_LIT>" % lineclause,<EOL>params, raw=raw, output=output)<EOL>

Retrieve input by session.

        Parameters
        ----------
        session : int
            Session number to retrieve.
        start : int
            First line to retrieve.
        stop : int
            End of line range (excluded from output itself). If None, retrieve
            to the end of the session.
        raw : bool
            If True, return untranslated input
        output : bool
            If True, attempt to include output. This will be 'real' Python
            objects for the current session, or text reprs from previous
            sessions if db_log_output was enabled at the time. Where no output
            is found, None is used.

        Returns
        -------
        An iterator over the desired lines. Each line is a 3-tuple, either
        (session, line, input) if output is False, or
        (session, line, (input, output)) if output is True.


def get_range_by_str(self, rangestr, raw=True, output=False):

for sess, s, e in extract_hist_ranges(rangestr):<EOL><INDENT>for line in self.get_range(sess, s, e, raw=raw, output=output):<EOL><INDENT>yield line<EOL><DEDENT><DEDENT>

Get lines of history from a string of ranges, as used by magic
        commands %hist, %save, %macro, etc.

        Parameters
        ----------
        rangestr : str
          A string specifying ranges, e.g. "5 ~2/1-4". See
          :func:`magic_history` for full details.
        raw, output : bool
          As :meth:`get_range`

        Returns
        -------
        Tuples as :meth:`get_range`


def __init__(self, shell=None, config=None, **traits):

<EOL>super(HistoryManager, self).__init__(shell=shell, config=config,<EOL>**traits)<EOL>self.save_flag = threading.Event()<EOL>self.db_input_cache_lock = threading.Lock()<EOL>self.db_output_cache_lock = threading.Lock()<EOL>if self.hist_file != '<STR_LIT>':<EOL><INDENT>self.save_thread = HistorySavingThread(self)<EOL>self.save_thread.start()<EOL><DEDENT>self.new_session()<EOL>

Create a new history manager associated with a shell instance.


profile_dir = self.shell.profile_dir.location<EOL>return os.path.join(profile_dir, '<STR_LIT>')<EOL>

Get default history file name based on the Shell's profile.

        The profile parameter is ignored, but must exist for compatibility with
        the parent class.


if conn is None:<EOL><INDENT>conn = self.db<EOL><DEDENT>with conn:<EOL><INDENT>cur = conn.execute("""<STR_LIT>""", (datetime.datetime.now(),))<EOL>self.session_number = cur.lastrowid<EOL><DEDENT>


self.writeout_cache()<EOL>with self.db:<EOL><INDENT>self.db.execute("""<STR_LIT>""", (datetime.datetime.now(),<EOL>len(self.input_hist_parsed)-<NUM_LIT:1>, self.session_number))<EOL><DEDENT>self.session_number = <NUM_LIT:0><EOL>

Close the database session, filling in the end time and line count.


with self.db:<EOL><INDENT>self.db.execute("<STR_LIT>",<EOL>(name, self.session_number))<EOL><DEDENT>

Give the current session a name in the history database.


self.output_hist.clear()<EOL>self.dir_hist[:] = [os.getcwdu()]<EOL>if new_session:<EOL><INDENT>if self.session_number:<EOL><INDENT>self.end_session()<EOL><DEDENT>self.input_hist_parsed[:] = ["<STR_LIT>"]<EOL>self.input_hist_raw[:] = ["<STR_LIT>"]<EOL>self.new_session()<EOL><DEDENT>

Clear the session history, releasing all object references, and
        optionally open a new session.


def _get_range_session(self, start=<NUM_LIT:1>, stop=None, raw=True, output=False):

input_hist = self.input_hist_raw if raw else self.input_hist_parsed<EOL>n = len(input_hist)<EOL>if start < <NUM_LIT:0>:<EOL><INDENT>start += n<EOL><DEDENT>if not stop or (stop > n):<EOL><INDENT>stop = n<EOL><DEDENT>elif stop < <NUM_LIT:0>:<EOL><INDENT>stop += n<EOL><DEDENT>for i in range(start, stop):<EOL><INDENT>if output:<EOL><INDENT>line = (input_hist[i], self.output_hist_reprs.get(i))<EOL><DEDENT>else:<EOL><INDENT>line = input_hist[i]<EOL><DEDENT>yield (<NUM_LIT:0>, i, line)<EOL><DEDENT>

Get input and output history from the current session. Called by
        get_range, and takes similar parameters.


def get_range(self, session=<NUM_LIT:0>, start=<NUM_LIT:1>, stop=None, raw=True,output=False):

if session <= <NUM_LIT:0>:<EOL><INDENT>session += self.session_number<EOL><DEDENT>if session==self.session_number:          <EOL><INDENT>return self._get_range_session(start, stop, raw, output)<EOL><DEDENT>return super(HistoryManager, self).get_range(session, start, stop, raw,<EOL>output)<EOL>

Retrieve input by session.

        Parameters
        ----------
        session : int
            Session number to retrieve. The current session is 0, and negative
            numbers count back from current session, so -1 is previous session.
        start : int
            First line to retrieve.
        stop : int
            End of line range (excluded from output itself). If None, retrieve
            to the end of the session.
        raw : bool
            If True, return untranslated input
        output : bool
            If True, attempt to include output. This will be 'real' Python
            objects for the current session, or text reprs from previous
            sessions if db_log_output was enabled at the time. Where no output
            is found, None is used.

        Returns
        -------
        An iterator over the desired lines. Each line is a 3-tuple, either
        (session, line, input) if output is False, or
        (session, line, (input, output)) if output is True.


def store_inputs(self, line_num, source, source_raw=None):

if source_raw is None:<EOL><INDENT>source_raw = source<EOL><DEDENT>source = source.rstrip('<STR_LIT:\n>')<EOL>source_raw = source_raw.rstrip('<STR_LIT:\n>')<EOL>if self._exit_re.match(source_raw.strip()):<EOL><INDENT>return<EOL><DEDENT>self.input_hist_parsed.append(source)<EOL>self.input_hist_raw.append(source_raw)<EOL>with self.db_input_cache_lock:<EOL><INDENT>self.db_input_cache.append((line_num, source, source_raw))<EOL>if len(self.db_input_cache) >= self.db_cache_size:<EOL><INDENT>self.save_flag.set()<EOL><DEDENT><DEDENT>self._iii = self._ii<EOL>self._ii = self._i<EOL>self._i = self._i00<EOL>self._i00 = source_raw<EOL>new_i = '<STR_LIT>' % line_num<EOL>to_main = {'<STR_LIT>': self._i,<EOL>'<STR_LIT>': self._ii,<EOL>'<STR_LIT>': self._iii,<EOL>new_i : self._i00 }<EOL>self.shell.push(to_main, interactive=False)<EOL>

Store source and raw input in history and create input cache
        variables _i*.

        Parameters
        ----------
        line_num : int
          The prompt number of this input.

        source : str
          Python input.

        source_raw : str, optional
          If given, this is the raw input without any IPython transformations
          applied to it.  If not given, ``source`` is used.


if (not self.db_log_output) or (line_num not in self.output_hist_reprs):<EOL><INDENT>return<EOL><DEDENT>output = self.output_hist_reprs[line_num]<EOL>with self.db_output_cache_lock:<EOL><INDENT>self.db_output_cache.append((line_num, output))<EOL><DEDENT>if self.db_cache_size <= <NUM_LIT:1>:<EOL><INDENT>self.save_flag.set()<EOL><DEDENT>

If database output logging is enabled, this saves all the
        outputs from the indicated prompt number to the database. It's
        called by run_cell after code has been executed.

        Parameters
        ----------
        line_num : int
          The line number from which to save outputs


if conn is None:<EOL><INDENT>conn = self.db<EOL><DEDENT>with self.db_input_cache_lock:<EOL><INDENT>try:<EOL><INDENT>self._writeout_input_cache(conn)<EOL><DEDENT>except sqlite3.IntegrityError:<EOL><INDENT>self.new_session(conn)<EOL>print("<STR_LIT>",<EOL>"<STR_LIT>",<EOL>self.session_number)<EOL>try:<EOL><INDENT>self._writeout_input_cache(conn)<EOL><DEDENT>except sqlite3.IntegrityError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>self.db_input_cache = []<EOL><DEDENT><DEDENT>with self.db_output_cache_lock:<EOL><INDENT>try:<EOL><INDENT>self._writeout_output_cache(conn)<EOL><DEDENT>except sqlite3.IntegrityError:<EOL><INDENT>print("<STR_LIT>",<EOL>"<STR_LIT>")<EOL><DEDENT>finally:<EOL><INDENT>self.db_output_cache = []<EOL><DEDENT><DEDENT>

Write any entries in the cache to the database.


self.stop_now = True<EOL>self.history_manager.save_flag.set()<EOL>self.join()<EOL>

This can be called from the main thread to safely stop this thread.

        Note that it does not attempt to write out remaining history before
        exiting. That should be done by calling the HistoryManager's
        end_session method.


def logstart(self, logfname=None, loghead=None, logmode=None,<EOL>log_output=False, timestamp=False, log_raw_input=False):

if self.logfile is not None:<EOL><INDENT>raise RuntimeError('<STR_LIT>' %<EOL>self.logfname)<EOL><DEDENT>if logfname is not None: self.logfname = logfname<EOL>if loghead is not None: self.loghead = loghead<EOL>if logmode is not None: self.logmode = logmode<EOL>self.timestamp = timestamp<EOL>self.log_output = log_output<EOL>self.log_raw_input = log_raw_input<EOL>isfile = os.path.isfile<EOL>logmode = self.logmode<EOL>if logmode == '<STR_LIT>':<EOL><INDENT>self.logfile = io.open(self.logfname, '<STR_LIT:a>', encoding='<STR_LIT:utf-8>')<EOL><DEDENT>elif logmode == '<STR_LIT>':<EOL><INDENT>if isfile(self.logfname):<EOL><INDENT>backup_logname = self.logfname+'<STR_LIT>'<EOL>if isfile(backup_logname):<EOL><INDENT>os.remove(backup_logname)<EOL><DEDENT>os.rename(self.logfname,backup_logname)<EOL><DEDENT>self.logfile = io.open(self.logfname, '<STR_LIT:w>', encoding='<STR_LIT:utf-8>')<EOL><DEDENT>elif logmode == '<STR_LIT>':<EOL><INDENT>self.logfname = os.path.join(self.home_dir,self.logfname)<EOL>self.logfile = io.open(self.logfname, '<STR_LIT:a>', encoding='<STR_LIT:utf-8>')<EOL><DEDENT>elif logmode == '<STR_LIT>':<EOL><INDENT>if isfile(self.logfname):<EOL><INDENT>os.remove(self.logfname)<EOL><DEDENT>self.logfile = io.open(self.logfname,'<STR_LIT:w>', encoding='<STR_LIT:utf-8>')<EOL><DEDENT>elif logmode == '<STR_LIT>':<EOL><INDENT>if isfile(self.logfname):<EOL><INDENT>if isfile(self.logfname+'<STR_LIT>'):<EOL><INDENT>old = glob.glob(self.logfname+'<STR_LIT>')<EOL>old.sort()<EOL>old.reverse()<EOL>for f in old:<EOL><INDENT>root, ext = os.path.splitext(f)<EOL>num = int(ext[<NUM_LIT:1>:-<NUM_LIT:1>])+<NUM_LIT:1><EOL>os.rename(f, root+'<STR_LIT:.>'+repr(num).zfill(<NUM_LIT:3>)+'<STR_LIT>')<EOL><DEDENT><DEDENT>os.rename(self.logfname, self.logfname+'<STR_LIT>')<EOL><DEDENT>self.logfile = io.open(self.logfname, '<STR_LIT:w>', encoding='<STR_LIT:utf-8>')<EOL><DEDENT>if logmode != '<STR_LIT>':<EOL><INDENT>self.logfile.write(self.loghead)<EOL><DEDENT>self.logfile.flush()<EOL>self.log_active = True<EOL>

Generate a new log-file with a default header.

        Raises RuntimeError if the log has already been started


if val not in [False,True,<NUM_LIT:0>,<NUM_LIT:1>]:<EOL><INDENT>raise ValueError('<STR_LIT>').with_traceback(val)<EOL><DEDENT>label = {<NUM_LIT:0>:'<STR_LIT>',<NUM_LIT:1>:'<STR_LIT>',False:'<STR_LIT>',True:'<STR_LIT>'}<EOL>if self.logfile is None:<EOL><INDENT>print("""<STR_LIT>""")<EOL><DEDENT>else:<EOL><INDENT>if self.log_active == val:<EOL><INDENT>print('<STR_LIT>',label[val])<EOL><DEDENT>else:<EOL><INDENT>print('<STR_LIT>',label[val])<EOL>self.log_active = not self.log_active<EOL>self.log_active_out = self.log_active<EOL><DEDENT><DEDENT>

Switch logging on/off. val should be ONLY a boolean.


if self.logfile is None:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>state = self.log_active and '<STR_LIT>' or '<STR_LIT>'<EOL>print('<STR_LIT>',self.logfname)<EOL>print('<STR_LIT>',self.logmode)<EOL>print('<STR_LIT>',self.log_output)<EOL>print('<STR_LIT>',self.log_raw_input)<EOL>print('<STR_LIT>',self.timestamp)<EOL>print('<STR_LIT>',state)<EOL><DEDENT>

Print a status message about the logger.


<EOL>if self.log_raw_input:<EOL><INDENT>self.log_write(line_ori)<EOL><DEDENT>else:<EOL><INDENT>self.log_write(line_mod)<EOL><DEDENT>

Write the sources to a log.

        Inputs:

        - line_mod: possibly modified input, such as the transformations made
        by input prefilters or input handlers of various kinds.  This should
        always be valid Python.

        - line_ori: unmodified input line from the user.  This is not
        necessarily valid Python.


def log_write(self, data, kind='<STR_LIT:input>'):

<EOL>if self.log_active and data:<EOL><INDENT>write = self.logfile.write<EOL>if kind=='<STR_LIT:input>':<EOL><INDENT>if self.timestamp:<EOL><INDENT>write(str_to_unicode(time.strftime('<STR_LIT>',<EOL>time.localtime())))<EOL><DEDENT>write(data)<EOL><DEDENT>elif kind=='<STR_LIT>' and self.log_output:<EOL><INDENT>odata = '<STR_LIT:\n>'.join(['<STR_LIT>' % s<EOL>for s in data.splitlines()])<EOL>write('<STR_LIT>' % odata)<EOL><DEDENT>self.logfile.flush()<EOL><DEDENT>


if self.logfile is not None:<EOL><INDENT>self.logfile.close()<EOL>self.logfile = None<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>self.log_active = False<EOL>

Fully stop logging and close log file.

        In order to start logging again, a new logstart() call needs to be
        made, possibly (though not necessarily) with a new filename, mode and
        other options.


<EOL>encoding = get_stream_enc(sys.stdin, '<STR_LIT:utf-8>')<EOL>line = py3compat.cast_unicode(line, encoding)<EOL>if pattern is None:<EOL><INDENT>pattern = line_split<EOL><DEDENT>match = pattern.match(line)<EOL>if not match:<EOL><INDENT>try:<EOL><INDENT>ifun, the_rest = line.split(None,<NUM_LIT:1>)<EOL><DEDENT>except ValueError:<EOL><INDENT>ifun, the_rest = line, u'<STR_LIT>'<EOL><DEDENT>pre = re.match('<STR_LIT>',line).groups()[<NUM_LIT:0>]<EOL>esc = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>pre, esc, ifun, the_rest = match.groups()<EOL><DEDENT>return pre, esc or '<STR_LIT>', ifun.strip(), the_rest.lstrip()<EOL>

Split user input into initial whitespace, escape character, function part
    and the rest.


return ip._ofind(self.ifun)<EOL>

Do a full, attribute-walking lookup of the ifun in the various
        namespaces for the given IPython InteractiveShell instance.

        Return a dict with keys: {found, obj, ospace, ismagic}

        Note: can cause state changes because of calling getattr, but should
        only be run if autocall is on and if the line hasn't matched any
        other, less dangerous handlers.

        Does cache the results of the call, so can be called multiple times
        without worrying about *further* damaging state.


<EOL>if '<STR_LIT:_>' in builtins.__dict__:<EOL><INDENT>try:<EOL><INDENT>del self.shell.user_ns['<STR_LIT:_>']<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Check if the user has set the '_' variable by hand.


<EOL>try:<EOL><INDENT>cell = self.shell.history_manager.input_hist_parsed[self.prompt_count]<EOL>if cell.rstrip().endswith('<STR_LIT:;>'):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>except IndexError:<EOL><INDENT>pass<EOL><DEDENT>return False<EOL>

Should we silence the display hook because of ';'?


pass<EOL>

Start the displayhook, initializing resources.


<EOL>io.stdout.write(self.shell.separate_out)<EOL>outprompt = self.shell.prompt_manager.render('<STR_LIT>')<EOL>if self.do_full_cache:<EOL><INDENT>io.stdout.write(outprompt)<EOL><DEDENT>

Write the output prompt.

        The default implementation simply writes the prompt to
        ``io.stdout``.


return self.shell.display_formatter.format(result)<EOL>

Compute format data of the object to be displayed.

        The format data is a generalization of the :func:`repr` of an object.
        In the default implementation the format data is a :class:`dict` of
        key value pair where the keys are valid MIME types and the values
        are JSON'able data structure containing the raw data for that MIME
        type. It is up to frontends to determine pick a MIME to to use and
        display that data in an appropriate manner.

        This method only computes the format data for the object and should
        NOT actually print or write that to a stream.

        Parameters
        ----------
        result : object
            The Python object passed to the display hook, whose format will be
            computed.

        Returns
        -------
        format_data : dict
            A :class:`dict` whose keys are valid MIME types and values are
            JSON'able raw data for that MIME type. It is recommended that
            all return values of this should always include the "text/plain"
            MIME type representation of the object.


<EOL>result_repr = format_dict['<STR_LIT>']<EOL>if '<STR_LIT:\n>' in result_repr:<EOL><INDENT>prompt_template = self.shell.prompt_manager.out_template<EOL>if prompt_template and not prompt_template.endswith('<STR_LIT:\n>'):<EOL><INDENT>result_repr = '<STR_LIT:\n>' + result_repr<EOL><DEDENT><DEDENT>print(result_repr, file=io.stdout)<EOL>

Write the format data dict to the frontend.

        This default version of this method simply writes the plain text
        representation of the object to ``io.stdout``. Subclasses should
        override this method to send the entire `format_dict` to the
        frontends.

        Parameters
        ----------
        format_dict : dict
            The format dict for the object passed to `sys.displayhook`.


<EOL>if result is not self.shell.user_ns['<STR_LIT>']:<EOL><INDENT>if len(self.shell.user_ns['<STR_LIT>']) >= self.cache_size and self.do_full_cache:<EOL><INDENT>warn('<STR_LIT>'+<EOL>repr(self.cache_size)+'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL>self.flush()<EOL><DEDENT>if '<STR_LIT:_>' not in builtins.__dict__:<EOL><INDENT>self.___ = self.__<EOL>self.__ = self._<EOL>self._ = result<EOL>self.shell.push({'<STR_LIT:_>':self._,<EOL>'<STR_LIT>':self.__,<EOL>'<STR_LIT>':self.___}, interactive=False)<EOL><DEDENT>to_main = {}<EOL>if self.do_full_cache:<EOL><INDENT>new_result = '<STR_LIT:_>'+repr(self.prompt_count)<EOL>to_main[new_result] = result<EOL>self.shell.push(to_main, interactive=False)<EOL>self.shell.user_ns['<STR_LIT>'][self.prompt_count] = result<EOL><DEDENT><DEDENT>

Update user_ns with various things like _, __, _1, etc.


io.stdout.write(self.shell.separate_out2)<EOL>io.stdout.flush()<EOL>


self.check_for_underscore()<EOL>if result is not None and not self.quiet():<EOL><INDENT>self.start_displayhook()<EOL>self.write_output_prompt()<EOL>format_dict = self.compute_format_data(result)<EOL>self.write_format_data(format_dict)<EOL>self.update_user_ns(result)<EOL>self.log_output(format_dict)<EOL>self.finish_displayhook()<EOL><DEDENT>

Printing with history cache management.

        This is invoked everytime the interpreter needs to print, and is
        activated by setting the variable sys.displayhook to it.


if sys.displayhook is not self.hook:<EOL><INDENT>self.old_hook = sys.displayhook<EOL>sys.displayhook = self.hook<EOL><DEDENT>


sys.displayhook = self.old_hook<EOL>


global _loaded<EOL>if not _loaded:<EOL><INDENT>ip.register_magics(CythonMagics)<EOL>_loaded = True<EOL><DEDENT>


@cell_magic<EOL><INDENT>def cython_inline(self, line, cell):<DEDENT>

locs = self.shell.user_global_ns<EOL>globs = self.shell.user_ns<EOL>return Cython.inline(cell, locals=locs, globals=globs)<EOL>

Compile and run a Cython code cell using Cython.inline.

        This magic simply passes the body of the cell to Cython.inline
        and returns the result. If the variables `a` and `b` are defined
        in the user's namespace, here is a simple example that returns
        their sum::

            %%cython_inline
            return a+b

        For most purposes, we recommend the usage of the `%%cython` magic.


@cell_magic<EOL><INDENT>def cython_pyximport(self, line, cell):<DEDENT>

module_name = line.strip()<EOL>if not module_name:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>fname = module_name + '<STR_LIT>'<EOL>with io.open(fname, '<STR_LIT:w>', encoding='<STR_LIT:utf-8>') as f:<EOL><INDENT>f.write(cell)<EOL><DEDENT>if '<STR_LIT>' not in sys.modules:<EOL><INDENT>import pyximport<EOL>pyximport.install(reload_support=True)<EOL><DEDENT>if module_name in self._reloads:<EOL><INDENT>module = self._reloads[module_name]<EOL>reload(module)<EOL><DEDENT>else:<EOL><INDENT>__import__(module_name)<EOL>module = sys.modules[module_name]<EOL>self._reloads[module_name] = module<EOL><DEDENT>self._import_all(module)<EOL>

Compile and import a Cython code cell using pyximport.

        The contents of the cell are written to a `.pyx` file in the current
        working directory, which is then imported using `pyximport`. This
        magic requires a module name to be passed::

            %%cython_pyximport modulename
            def f(x):
                return 2.0*x

        The compiled module is then imported and all of its symbols are injected into
        the user's namespace. For most purposes, we recommend the usage of the
        `%%cython` magic.


@magic_arguments()<EOL><INDENT>@argument(<EOL>'<STR_LIT:-c>', '<STR_LIT>', action='<STR_LIT>', default=[],<EOL>help="<STR_LIT>"<EOL>)<EOL>@argument(<EOL>'<STR_LIT>', '<STR_LIT>', action='<STR_LIT>', default=[],<EOL>help="<STR_LIT>"<EOL>)<EOL>@argument(<EOL>'<STR_LIT>', '<STR_LIT>', action='<STR_LIT>', default=[],<EOL>help="<STR_LIT>"<EOL>)<EOL>@argument(<EOL>'<STR_LIT>', '<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help="<STR_LIT>"<EOL>)<EOL>@cell_magic<EOL>def cython(self, line, cell):<DEDENT>

args = parse_argstring(self.cython, line)<EOL>code = cell if cell.endswith('<STR_LIT:\n>') else cell+'<STR_LIT:\n>'<EOL>lib_dir = os.path.join(self.shell.ipython_dir, '<STR_LIT>')<EOL>cython_include_dirs = ['<STR_LIT:.>']<EOL>force = args.force<EOL>quiet = True<EOL>ctx = Context(cython_include_dirs, default_options)<EOL>key = code, sys.version_info, sys.executable, Cython.__version__<EOL>module_name = "<STR_LIT>" + hashlib.md5(str(key).encode('<STR_LIT:utf-8>')).hexdigest()<EOL>so_ext = [ ext for ext,_,mod_type in imp.get_suffixes() if mod_type == imp.C_EXTENSION ][<NUM_LIT:0>]<EOL>module_path = os.path.join(lib_dir, module_name+so_ext)<EOL>if not os.path.exists(lib_dir):<EOL><INDENT>os.makedirs(lib_dir)<EOL><DEDENT>if force or not os.path.isfile(module_path):<EOL><INDENT>c_include_dirs = args.include<EOL>if '<STR_LIT>' in code:<EOL><INDENT>import numpy<EOL>c_include_dirs.append(numpy.get_include())<EOL><DEDENT>pyx_file = os.path.join(lib_dir, module_name + '<STR_LIT>')<EOL>pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding())<EOL>with io.open(pyx_file, '<STR_LIT:w>', encoding='<STR_LIT:utf-8>') as f:<EOL><INDENT>f.write(code)<EOL><DEDENT>extension = Extension(<EOL>name = module_name,<EOL>sources = [pyx_file],<EOL>include_dirs = c_include_dirs,<EOL>extra_compile_args = args.compile_args,<EOL>libraries = args.lib,<EOL>)<EOL>dist = Distribution()<EOL>config_files = dist.find_config_files()<EOL>try: <EOL><INDENT>config_files.remove('<STR_LIT>')<EOL><DEDENT>except ValueError:<EOL><INDENT>pass<EOL><DEDENT>dist.parse_config_files(config_files)<EOL>build_extension = build_ext(dist)<EOL>build_extension.finalize_options()<EOL>try:<EOL><INDENT>build_extension.extensions = cythonize([extension], ctx=ctx, quiet=quiet)<EOL><DEDENT>except CompileError:<EOL><INDENT>return<EOL><DEDENT>build_extension.build_temp = os.path.dirname(pyx_file)<EOL>build_extension.build_lib  = lib_dir<EOL>build_extension.run()<EOL>self._code_cache[key] = module_name<EOL><DEDENT>module = imp.load_dynamic(module_name, module_path)<EOL>self._import_all(module)<EOL>

Compile and import everything from a Cython code cell.

        The contents of the cell are written to a `.pyx` file in the
        directory `IPYTHONDIR/cython` using a filename with the hash of the code.
        This file is then cythonized and compiled. The resulting module
        is imported and all of its symbols are injected into the user's
        namespace. The usage is similar to that of `%%cython_pyximport` but
        you don't have to pass a module name::

        %%cython
        def f(x):
            return 2.0*x


for ext, mode, typ in imp.get_suffixes():<EOL><INDENT>if typ == imp.PY_COMPILED:<EOL><INDENT>return ext<EOL><DEDENT><DEDENT>

Official way to get the extension of compiled files (.pyc or .pyo)


for name in func_attrs:<EOL><INDENT>try:<EOL><INDENT>setattr(old, name, getattr(new, name))<EOL><DEDENT>except (AttributeError, TypeError):<EOL><INDENT>pass<EOL><DEDENT><DEDENT>


for key in old.__dict__.keys():<EOL><INDENT>old_obj = getattr(old, key)<EOL>try:<EOL><INDENT>new_obj = getattr(new, key)<EOL><DEDENT>except AttributeError:<EOL><INDENT>try:<EOL><INDENT>delattr(old, key)<EOL><DEDENT>except (AttributeError, TypeError):<EOL><INDENT>pass<EOL><DEDENT>continue<EOL><DEDENT>if update_generic(old_obj, new_obj): continue<EOL>try:<EOL><INDENT>setattr(old, key, getattr(new, key))<EOL><DEDENT>except (AttributeError, TypeError):<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Replace stuff in the __dict__ of a class, and upgrade
    method code objects


update_generic(old.fdel, new.fdel)<EOL>update_generic(old.fget, new.fget)<EOL>update_generic(old.fset, new.fset)<EOL>

Replace get/set/del functions of a property


def superreload(module, reload=reload, old_objects={}):

<EOL>for name, obj in module.__dict__.items():<EOL><INDENT>if not hasattr(obj, '<STR_LIT>') or obj.__module__ != module.__name__:<EOL><INDENT>continue<EOL><DEDENT>key = (module.__name__, name)<EOL>try:<EOL><INDENT>old_objects.setdefault(key, []).append(weakref.ref(obj))<EOL><DEDENT>except TypeError:<EOL><INDENT>if not PY3 and isinstance(obj, types.ClassType):<EOL><INDENT>old_objects.setdefault(key, []).append(StrongRef(obj))<EOL><DEDENT><DEDENT><DEDENT>try:<EOL><INDENT>old_dict = module.__dict__.copy()<EOL>old_name = module.__name__<EOL>module.__dict__.clear()<EOL>module.__dict__['<STR_LIT>'] = old_name<EOL>module.__dict__['<STR_LIT>'] = old_dict['<STR_LIT>']<EOL><DEDENT>except (TypeError, AttributeError, KeyError):<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>module = reload(module)<EOL><DEDENT>except:<EOL><INDENT>module.__dict__.update(old_dict)<EOL>raise<EOL><DEDENT>for name, new_obj in module.__dict__.items():<EOL><INDENT>key = (module.__name__, name)<EOL>if key not in old_objects: continue<EOL>new_refs = []<EOL>for old_ref in old_objects[key]:<EOL><INDENT>old_obj = old_ref()<EOL>if old_obj is None: continue<EOL>new_refs.append(old_ref)<EOL>update_generic(old_obj, new_obj)<EOL><DEDENT>if new_refs:<EOL><INDENT>old_objects[key] = new_refs<EOL><DEDENT>else:<EOL><INDENT>del old_objects[key]<EOL><DEDENT><DEDENT>return module<EOL>

Enhanced version of the builtin reload function.

    superreload remembers objects previously in the module, and

    - upgrades the class dictionary of every old class in the module
    - upgrades the code object of every old function and method
    - clears the module's namespace before reloading


global _loaded<EOL>if not _loaded:<EOL><INDENT>plugin = AutoreloadPlugin(shell=ip, config=ip.config)<EOL>ip.plugin_manager.register_plugin('<STR_LIT>', plugin)<EOL>_loaded = True<EOL><DEDENT>


try:<EOL><INDENT>del self.modules[module_name]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>self.skip_modules[module_name] = True<EOL>

Skip reloading the named module in the future


try:<EOL><INDENT>del self.skip_modules[module_name]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>self.modules[module_name] = True<EOL>

Reload the named module in the future (if it is imported)


self.mark_module_reloadable(module_name)<EOL>__import__(module_name)<EOL>top_name = module_name.split('<STR_LIT:.>')[<NUM_LIT:0>]<EOL>top_module = sys.modules[top_name]<EOL>return top_module, top_name<EOL>

Import a module, and mark it reloadable

        Returns
        -------
        top_module : module
            The imported module if it is top-level, or the top-level
        top_name : module
            Name of top_module


if not self.enabled and not check_all:<EOL><INDENT>return<EOL><DEDENT>if check_all or self.check_all:<EOL><INDENT>modules = sys.modules.keys()<EOL><DEDENT>else:<EOL><INDENT>modules = self.modules.keys()<EOL><DEDENT>for modname in modules:<EOL><INDENT>m = sys.modules.get(modname, None)<EOL>if modname in self.skip_modules:<EOL><INDENT>continue<EOL><DEDENT>if not hasattr(m, '<STR_LIT>'):<EOL><INDENT>continue<EOL><DEDENT>if m.__name__ == '<STR_LIT:__main__>':<EOL><INDENT>continue<EOL><DEDENT>filename = m.__file__<EOL>path, ext = os.path.splitext(filename)<EOL>if ext.lower() == '<STR_LIT>':<EOL><INDENT>ext = PY_COMPILED_EXT<EOL>pyc_filename = pyfile.cache_from_source(filename)<EOL>py_filename = filename<EOL><DEDENT>else:<EOL><INDENT>pyc_filename = filename<EOL>try:<EOL><INDENT>py_filename = pyfile.source_from_cache(filename)<EOL><DEDENT>except ValueError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>try:<EOL><INDENT>pymtime = os.stat(py_filename).st_mtime<EOL>if pymtime <= os.stat(pyc_filename).st_mtime:<EOL><INDENT>continue<EOL><DEDENT>if self.failed.get(py_filename, None) == pymtime:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>except OSError:<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>superreload(m, reload, self.old_objects)<EOL>if py_filename in self.failed:<EOL><INDENT>del self.failed[py_filename]<EOL><DEDENT><DEDENT>except:<EOL><INDENT>print >> sys.stderr, "<STR_LIT>" % (<EOL>modname, traceback.format_exc(<NUM_LIT:1>))<EOL>self.failed[py_filename] = pymtime<EOL><DEDENT><DEDENT>

Check whether some modules need to be reloaded.


if parameter_s == '<STR_LIT>':<EOL><INDENT>self._reloader.check(True)<EOL><DEDENT>elif parameter_s == '<STR_LIT:0>':<EOL><INDENT>self._reloader.enabled = False<EOL><DEDENT>elif parameter_s == '<STR_LIT:1>':<EOL><INDENT>self._reloader.check_all = False<EOL>self._reloader.enabled = True<EOL><DEDENT>elif parameter_s == '<STR_LIT:2>':<EOL><INDENT>self._reloader.check_all = True<EOL>self._reloader.enabled = True<EOL><DEDENT>

r"""%autoreload => Reload modules automatically

        %autoreload
        Reload all modules (except those excluded by %aimport) automatically
        now.

        %autoreload 0
        Disable automatic reloading.

        %autoreload 1
        Reload all modules imported with %aimport every time before executing
        the Python code typed.

        %autoreload 2
        Reload all modules (except those excluded by %aimport) every time
        before executing the Python code typed.

        Reloading Python modules in a reliable way is in general
        difficult, and unexpected things may occur. %autoreload tries to
        work around common pitfalls by replacing function code objects and
        parts of classes previously in the module with new versions. This
        makes the following things to work:

        - Functions and classes imported via 'from xxx import foo' are upgraded
          to new versions when 'xxx' is reloaded.

        - Methods and properties of classes are upgraded on reload, so that
          calling 'c.foo()' on an object 'c' created before the reload causes
          the new code for 'foo' to be executed.

        Some of the known remaining caveats are:

        - Replacing code objects does not always succeed: changing a @property
          in a class to an ordinary method or a method to a member variable
          can cause problems (but in old objects only).

        - Functions that are removed (eg. via monkey-patching) from a module
          before it is reloaded are not upgraded.

        - C extension modules cannot be reloaded, and so cannot be
          autoreloaded.


@line_magic<EOL><INDENT>def aimport(self, parameter_s='<STR_LIT>', stream=None):<DEDENT>

modname = parameter_s<EOL>if not modname:<EOL><INDENT>to_reload = self._reloader.modules.keys()<EOL>to_reload.sort()<EOL>to_skip = self._reloader.skip_modules.keys()<EOL>to_skip.sort()<EOL>if stream is None:<EOL><INDENT>stream = sys.stdout<EOL><DEDENT>if self._reloader.check_all:<EOL><INDENT>stream.write("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>stream.write("<STR_LIT>" % '<STR_LIT:U+0020>'.join(to_reload))<EOL><DEDENT>stream.write("<STR_LIT>" % '<STR_LIT:U+0020>'.join(to_skip))<EOL><DEDENT>elif modname.startswith('<STR_LIT:->'):<EOL><INDENT>modname = modname[<NUM_LIT:1>:]<EOL>self._reloader.mark_module_skipped(modname)<EOL><DEDENT>else:<EOL><INDENT>top_module, top_name = self._reloader.aimport_module(modname)<EOL>self.shell.push({top_name: top_module})<EOL><DEDENT>

%aimport => Import modules for automatic reloading.

        %aimport
        List modules to automatically import and not to import.

        %aimport foo
        Import module 'foo' and mark it to be autoreloaded for %autoreload 1

        %aimport -foo
        Mark module 'foo' to not be autoreloaded for %autoreload 1


is_data_frame = ro.r('<STR_LIT>')<EOL>colnames = ro.r('<STR_LIT>')<EOL>rownames = ro.r('<STR_LIT>') <EOL>names = ro.r('<STR_LIT>')<EOL>if dataframe:<EOL><INDENT>as_data_frame = ro.r('<STR_LIT>')<EOL>cols = colnames(Robj)<EOL>_names = names(Robj)<EOL>if cols != ri.NULL:<EOL><INDENT>Robj = as_data_frame(Robj)<EOL>names = tuple(np.array(cols))<EOL><DEDENT>elif _names != ri.NULL:<EOL><INDENT>names = tuple(np.array(_names))<EOL><DEDENT>else: <EOL><INDENT>return np.asarray(Robj)<EOL><DEDENT>Robj = np.rec.fromarrays(Robj, names = names)<EOL><DEDENT>return np.asarray(Robj)<EOL>

Convert an object in R's namespace to one suitable
for ipython's namespace.

For a data.frame, it tries to return a structured array.
It first checks for colnames, then names.
If all are NULL, it returns np.asarray(Robj), else
it tries to construct a recarray

Parameters
----------

Robj: an R object returned from rpy2


global _loaded<EOL>if not _loaded:<EOL><INDENT>ip.register_magics(RMagics)<EOL>_loaded = True<EOL><DEDENT>


def __init__(self, shell, Rconverter=Rconverter,<EOL>pyconverter=np.asarray,<EOL>cache_display_data=False):

super(RMagics, self).__init__(shell)<EOL>self.cache_display_data = cache_display_data<EOL>self.r = ro.R()<EOL>self.Rstdout_cache = []<EOL>self.pyconverter = pyconverter<EOL>self.Rconverter = Rconverter<EOL>

Parameters
----------

shell : IPython shell

pyconverter : callable
    To be called on values in ipython namespace before
    assigning to variables in rpy2.

cache_display_data : bool
    If True, the published results of the final call to R are
    cached in the variable 'display_cache'.


old_writeconsole = ri.get_writeconsole()<EOL>ri.set_writeconsole(self.write_console)<EOL>try:<EOL><INDENT>value = ri.baseenv['<STR_LIT>'](ri.parse(line))<EOL><DEDENT>except (ri.RRuntimeError, ValueError) as exception:<EOL><INDENT>warning_or_other_msg = self.flush() <EOL>raise RInterpreterError(line, str_to_unicode(str(exception)), warning_or_other_msg)<EOL><DEDENT>text_output = self.flush()<EOL>ri.set_writeconsole(old_writeconsole)<EOL>return text_output, value<EOL>

Parse and evaluate a line with rpy2.
Returns the output to R's stdout() connection
and the value of eval(parse(line)).


A hook to capture R's stdout in a cache.


value = '<STR_LIT>'.join([str_to_unicode(s, '<STR_LIT:utf-8>') for s in self.Rstdout_cache])<EOL>self.Rstdout_cache = []<EOL>return value<EOL>

Flush R's stdout cache to a string, returning the string.


def write_file(self, filename, content):

<EOL>time.sleep(<NUM_LIT>)<EOL>f = open(filename, '<STR_LIT:w>')<EOL>try:<EOL><INDENT>f.write(content)<EOL><DEDENT>finally:<EOL><INDENT>f.close()<EOL><DEDENT>

Write a file, and force a timestamp difference of at least one second

Notes
-----
Python's .pyc files record the timestamp of their compilation
with a time resolution of one second.

Therefore, we need to force a timestamp difference between .py
and .pyc, without having the .py file be timestamped in the
future, and without changing the timestamp of the .pyc file
(because that is stored in the file).  The only reliable way
to achieve this seems to be to sleep.


if cycle:<EOL><INDENT>return p.text('<STR_LIT>')<EOL><DEDENT>out = pretty(o, use_unicode=True)<EOL>if '<STR_LIT:\n>' in out:<EOL><INDENT>p.text(u'<STR_LIT:\n>')<EOL><DEDENT>p.text(out)<EOL>

A function to pretty print sympy Basic objects.


s = latex(o, mode='<STR_LIT>')<EOL>s = s.replace('<STR_LIT>','<STR_LIT>')<EOL>s = s.replace('<STR_LIT>', '<STR_LIT>')<EOL>png = latex_to_png(s)<EOL>return png<EOL>

A function to display sympy expression using inline style LaTeX in PNG.


s = latex(o, mode='<STR_LIT>')<EOL>s = s.strip('<STR_LIT:$>')<EOL>png = latex_to_png('<STR_LIT>' % s, backend='<STR_LIT>')<EOL>return png<EOL>

A function to display sympy expression using display style LaTeX in PNG.


import sympy<EOL>if isinstance(o, (list, tuple, set, frozenset)):<EOL><INDENT>return all(can_print_latex(i) for i in o)<EOL><DEDENT>elif isinstance(o, dict):<EOL><INDENT>return all((isinstance(i, basestring) or can_print_latex(i)) and can_print_latex(o[i]) for i in o)<EOL><DEDENT>elif isinstance(o,(sympy.Basic, sympy.matrices.Matrix, int, long, float)):<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

Return True if type o can be printed with LaTeX.

If o is a container type, this is True if and only if every element of o
can be printed with LaTeX.


if can_print_latex(o):<EOL><INDENT>s = latex(o, mode='<STR_LIT>')<EOL>s = s.replace('<STR_LIT>','<STR_LIT>')<EOL>s = s.strip('<STR_LIT:$>')<EOL>return '<STR_LIT>' % s<EOL><DEDENT>return None<EOL>

A function to generate the latex representation of sympy
    expressions.


import sympy<EOL>try:<EOL><INDENT>import sympy.interactive.ipythonprinting<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>ip.extension_manager.load_extension('<STR_LIT>')<EOL>return<EOL><DEDENT>global _loaded<EOL>if not _loaded:<EOL><INDENT>plaintext_formatter = ip.display_formatter.formatters['<STR_LIT>']<EOL>for cls in (object, str):<EOL><INDENT>plaintext_formatter.for_type(cls, print_basic_unicode)<EOL><DEDENT>printable_containers = [list, tuple]<EOL>if sympy.__version__ > '<STR_LIT>':<EOL><INDENT>printable_containers += [set, frozenset]<EOL><DEDENT>else:<EOL><INDENT>plaintext_formatter.for_type(cls, print_basic_unicode)<EOL><DEDENT>plaintext_formatter.for_type_by_name(<EOL>'<STR_LIT>', '<STR_LIT>', print_basic_unicode<EOL>)<EOL>plaintext_formatter.for_type_by_name(<EOL>'<STR_LIT>', '<STR_LIT>', print_basic_unicode<EOL>)<EOL>png_formatter = ip.display_formatter.formatters['<STR_LIT>']<EOL>png_formatter.for_type_by_name(<EOL>'<STR_LIT>', '<STR_LIT>', print_png<EOL>)<EOL>png_formatter.for_type_by_name(<EOL>'<STR_LIT>', '<STR_LIT>', print_display_png<EOL>)<EOL>for cls in [dict, int, long, float] + printable_containers:<EOL><INDENT>png_formatter.for_type(cls, print_png)<EOL><DEDENT>latex_formatter = ip.display_formatter.formatters['<STR_LIT>']<EOL>latex_formatter.for_type_by_name(<EOL>'<STR_LIT>', '<STR_LIT>', print_latex<EOL>)<EOL>latex_formatter.for_type_by_name(<EOL>'<STR_LIT>', '<STR_LIT>', print_latex<EOL>)<EOL>for cls in printable_containers:<EOL><INDENT>latex_formatter.for_type(cls, print_latex)<EOL><DEDENT>_loaded = True<EOL><DEDENT>


global _loaded<EOL>if not _loaded:<EOL><INDENT>ip.register_magics(OctaveMagics)<EOL>_loaded = True<EOL><DEDENT>


super(OctaveMagics, self).__init__(shell)<EOL>self._oct = oct2py.Oct2Py()<EOL>self._plot_format = '<STR_LIT>'<EOL>self._publish_display_data = publish_display_data<EOL>

Parameters
----------
shell : IPython shell


def _fix_gnuplot_svg_size(self, image, size=None):

(svg,) = minidom.parseString(image).getElementsByTagName('<STR_LIT>')<EOL>viewbox = svg.getAttribute('<STR_LIT>').split('<STR_LIT:U+0020>')<EOL>if size is not None:<EOL><INDENT>width, height = size<EOL><DEDENT>else:<EOL><INDENT>width, height = viewbox[<NUM_LIT:2>:]<EOL><DEDENT>svg.setAttribute('<STR_LIT:width>', '<STR_LIT>' % width)<EOL>svg.setAttribute('<STR_LIT>', '<STR_LIT>' % height)<EOL>return svg.toxml()<EOL>

GnuPlot SVGs do not have height/width attributes.  Set
these to be the same as the viewBox, so that the browser
scales the image correctly.

Parameters
----------
image : str
    SVG data.
size : tuple of int
    Image width, height.


global _loaded<EOL>if not _loaded:<EOL><INDENT>plugin = StoreMagic(shell=ip, config=ip.config)<EOL>ip.plugin_manager.register_plugin('<STR_LIT>', plugin)<EOL>_loaded = True<EOL><DEDENT>


config = kwargs.pop('<STR_LIT>', None)<EOL>if config is not None:<EOL><INDENT>self.config = config<EOL><DEDENT>super(Configurable, self).__init__(**kwargs)<EOL>self.created = datetime.datetime.now()<EOL>

Create a configurable given a config config.

        Parameters
        ----------
        config : Config
            If this is empty, default values are used. If config is a
            :class:`Config` instance, it will be used to configure the
            instance.

        Notes
        -----
        Subclasses of Configurable must call the :meth:`__init__` method of
        :class:`Configurable` *before* doing anything else and using
        :func:`super`::

            class MyConfigurable(Configurable):
                def __init__(self, config=None):
                    super(MyConfigurable, self).__init__(config)
                    # Then any other code you need to finish initialization.

        This ensures that instances will be configured properly.


def _config_changed(self, name, old, new):

<EOL>traits = self.traits(config=True)<EOL>section_names = [cls.__name__ for cls inreversed(self.__class__.__mro__) if<EOL>issubclass(cls, Configurable) and issubclass(self.__class__, cls)]<EOL>for sname in section_names:<EOL><INDENT>if new._has_section(sname):<EOL><INDENT>my_config = new[sname]<EOL>for k, v in traits.items():<EOL><INDENT>if k[<NUM_LIT:0>].upper()==k[<NUM_LIT:0>] and not k.startswith('<STR_LIT:_>'):<EOL><INDENT>raise ConfigurableError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' %(self.__class__.__name__, k))<EOL><DEDENT>try:<EOL><INDENT>config_value = my_config[k]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>setattr(self, k, deepcopy(config_value))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>

Update all the class traits having ``config=True`` as metadata.

        For any class trait with a ``config`` metadata attribute that is
        ``True``, we update the trait with the value of the corresponding
        config entry.


<EOL>newconfig = deepcopy(self.config)<EOL>newconfig._merge(config)<EOL>self.config = newconfig<EOL>

Fire the traits events when the config is updated.


assert inst is None or isinstance(inst, cls)<EOL>cls_traits = cls.class_traits(config=True)<EOL>final_help = []<EOL>final_help.append('<STR_LIT>' % cls.__name__)<EOL>final_help.append(len(final_help[<NUM_LIT:0>])*'<STR_LIT:->')<EOL>for k,v in sorted(cls.class_traits(config=True).items()):<EOL><INDENT>help = cls.class_get_trait_help(v, inst)<EOL>final_help.append(help)<EOL><DEDENT>return '<STR_LIT:\n>'.join(final_help)<EOL>

Get the help string for this class in ReST format.

        If `inst` is given, it's current trait values will be used in place of
        class defaults.


@classmethod<EOL><INDENT>def class_get_trait_help(cls, trait, inst=None):<DEDENT>

assert inst is None or isinstance(inst, cls)<EOL>lines = []<EOL>header = "<STR_LIT>" % (cls.__name__, trait.name, trait.__class__.__name__)<EOL>lines.append(header)<EOL>if inst is not None:<EOL><INDENT>lines.append(indent('<STR_LIT>' % getattr(inst, trait.name), <NUM_LIT:4>))<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>dvr = repr(trait.get_default_value())<EOL><DEDENT>except Exception:<EOL><INDENT>dvr = None <EOL><DEDENT>if dvr is not None:<EOL><INDENT>if len(dvr) > <NUM_LIT:64>:<EOL><INDENT>dvr = dvr[:<NUM_LIT>]+'<STR_LIT>'<EOL><DEDENT>lines.append(indent('<STR_LIT>' % dvr, <NUM_LIT:4>))<EOL><DEDENT><DEDENT>if '<STR_LIT>' in trait.__class__.__name__:<EOL><INDENT>lines.append(indent('<STR_LIT>' % (trait.values,)))<EOL><DEDENT>help = trait.get_metadata('<STR_LIT>')<EOL>if help is not None:<EOL><INDENT>help = '<STR_LIT:\n>'.join(wrap_paragraphs(help, <NUM_LIT>))<EOL>lines.append(indent(help, <NUM_LIT:4>))<EOL><DEDENT>return '<STR_LIT:\n>'.join(lines)<EOL>

Get the help string for a single trait.

        If `inst` is given, it's current trait values will be used in place of
        the class default.


Get the help string for a single trait and print it.


def c(s):<EOL><INDENT>"""<STR_LIT>"""<EOL>s = '<STR_LIT>'.join(wrap_paragraphs(s, <NUM_LIT>))<EOL>return '<STR_LIT>' + s.replace('<STR_LIT:\n>', '<STR_LIT>')<EOL><DEDENT>breaker = '<STR_LIT:#>' + '<STR_LIT:->'*<NUM_LIT><EOL>s = "<STR_LIT>"%cls.__name__<EOL>lines = [breaker, s, breaker, '<STR_LIT>']<EOL>desc = cls.class_traits().get('<STR_LIT:description>')<EOL>if desc:<EOL><INDENT>desc = desc.default_value<EOL><DEDENT>else:<EOL><INDENT>desc = getattr(cls, '<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>if desc:<EOL><INDENT>lines.append(c(desc))<EOL>lines.append('<STR_LIT>')<EOL><DEDENT>parents = []<EOL>for parent in cls.mro():<EOL><INDENT>if parent is not cls and issubclass(parent, Configurable) andparent.class_traits(config=True):<EOL><INDENT>parents.append(parent)<EOL><DEDENT><DEDENT>if parents:<EOL><INDENT>pstr = '<STR_LIT:U+002CU+0020>'.join([ p.__name__ for p in parents ])<EOL>lines.append(c('<STR_LIT>'%(cls.__name__, pstr)))<EOL>lines.append('<STR_LIT>')<EOL><DEDENT>for name,trait in cls.class_traits(config=True).items():<EOL><INDENT>help = trait.get_metadata('<STR_LIT>') or '<STR_LIT>'<EOL>lines.append(c(help))<EOL>lines.append('<STR_LIT>'%(cls.__name__, name, trait.get_default_value()))<EOL>lines.append('<STR_LIT>')<EOL><DEDENT>return '<STR_LIT:\n>'.join(lines)<EOL>


for subclass in cls.mro():<EOL><INDENT>if issubclass(cls, subclass) andissubclass(subclass, SingletonConfigurable) andsubclass != SingletonConfigurable:<EOL><INDENT>yield subclass<EOL><DEDENT><DEDENT>

Walk the cls.mro() for parent classes that are also singletons

        For use in instance()


if not cls.initialized():<EOL><INDENT>return<EOL><DEDENT>for subclass in cls._walk_mro():<EOL><INDENT>if isinstance(subclass._instance, cls):<EOL><INDENT>subclass._instance = None<EOL><DEDENT><DEDENT>

unset _instance for this class and singleton parents.


@classmethod<EOL><INDENT>def instance(cls, *args, **kwargs):<DEDENT>

<EOL>if cls._instance is None:<EOL><INDENT>inst = cls(*args, **kwargs)<EOL>for subclass in cls._walk_mro():<EOL><INDENT>subclass._instance = inst<EOL><DEDENT><DEDENT>if isinstance(cls._instance, cls):<EOL><INDENT>return cls._instance<EOL><DEDENT>else:<EOL><INDENT>raise MultipleInstanceError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % cls.__name__<EOL>)<EOL><DEDENT>

Returns a global instance of this class.

        This method create a new instance if none have previously been created
        and returns a previously created instance is one already exists.

        The arguments and keyword arguments passed to this method are passed
        on to the :meth:`__init__` method of the class upon instantiation.

        Examples
        --------

        Create a singleton class using instance, and retrieve it::

            >>> from IPython.config.configurable import SingletonConfigurable
            >>> class Foo(SingletonConfigurable): pass
            >>> foo = Foo.instance()
            >>> foo == Foo.instance()
            True

        Create a subclass that is retrived using the base class instance::

            >>> class Bar(SingletonConfigurable): pass
            >>> class Bam(Bar): pass
            >>> bam = Bam.instance()
            >>> bam == Bar.instance()
            True


return hasattr(cls, "<STR_LIT>") and cls._instance is not None<EOL>


@decorator<EOL>def catch_config_error(method, app, *args, **kwargs):

try:<EOL><INDENT>return method(app, *args, **kwargs)<EOL><DEDENT>except (TraitError, ArgumentError) as e:<EOL><INDENT>app.print_description()<EOL>app.print_help()<EOL>app.print_examples()<EOL>app.log.fatal("<STR_LIT>")<EOL>app.log.fatal(str(e))<EOL>app.log.debug("<STR_LIT>", app.config)<EOL>app.exit(<NUM_LIT:1>)<EOL><DEDENT>

Method decorator for catching invalid config (Trait/ArgumentErrors) during init.

    On a TraitError (generally caused by bad config), this will print the trait's
    message, and exit the app.

    For use on init methods, to prevent invoking excepthook on invalid input.


def boolean_flag(name, configurable, set_help='<STR_LIT>', unset_help='<STR_LIT>'):

<EOL>set_help = set_help or "<STR_LIT>"%configurable<EOL>unset_help = unset_help or "<STR_LIT>"%configurable<EOL>cls,trait = configurable.split('<STR_LIT:.>')<EOL>setter = {cls : {trait : True}}<EOL>unsetter = {cls : {trait : False}}<EOL>return {name : (setter, set_help), '<STR_LIT>'+name : (unsetter, unset_help)}<EOL>

Helper for building basic --trait, --no-trait flags.

    Parameters
    ----------

    name : str
        The name of the flag.
    configurable : str
        The 'Class.trait' string of the trait to be set/unset with the flag
    set_help : unicode
        help string for --name flag
    unset_help : unicode
        help string for --no-name flag

    Returns
    -------

    cfg : dict
        A dict with two keys: 'name', and 'no-name', for setting and unsetting
        the trait, respectively.


def _log_level_changed(self, name, old, new):

if isinstance(new, str):<EOL><INDENT>new = getattr(logging, new)<EOL>self.log_level = new<EOL><DEDENT>self.log.setLevel(new)<EOL>

Adjust the log level when log_level is set.


log = logging.getLogger(self.__class__.__name__)<EOL>log.setLevel(self.log_level)<EOL>if sys.executable.endswith('<STR_LIT>'):<EOL><INDENT>_log_handler = logging.StreamHandler(open(os.devnull, '<STR_LIT:w>'))<EOL><DEDENT>else:<EOL><INDENT>_log_handler = logging.StreamHandler()<EOL><DEDENT>_log_formatter = logging.Formatter(self.log_format)<EOL>_log_handler.setFormatter(_log_formatter)<EOL>log.addHandler(_log_handler)<EOL>return log<EOL>

Start logging for this application.

        The default is to log to stdout using a StreaHandler. The log level
        starts at loggin.WARN, but this can be adjusted by setting the
        ``log_level`` attribute.


def _flags_changed(self, name, old, new):

for key,value in new.items():<EOL><INDENT>assert len(value) == <NUM_LIT:2>, "<STR_LIT>"%(key,value)<EOL>assert isinstance(value[<NUM_LIT:0>], (dict, Config)), "<STR_LIT>"%(key,value)<EOL>assert isinstance(value[<NUM_LIT:1>], str), "<STR_LIT>"%(key,value)<EOL><DEDENT>


Do the basic steps to configure me.

        Override in subclasses.


if self.subapp is not None:<EOL><INDENT>return self.subapp.start()<EOL><DEDENT>

Start the app mainloop.

        Override in subclasses.


if not self.aliases:<EOL><INDENT>return<EOL><DEDENT>lines = []<EOL>classdict = {}<EOL>for cls in self.classes:<EOL><INDENT>for c in cls.mro()[:-<NUM_LIT:3>]:<EOL><INDENT>classdict[c.__name__] = c<EOL><DEDENT><DEDENT>for alias, longname in self.aliases.items():<EOL><INDENT>classname, traitname = longname.split('<STR_LIT:.>',<NUM_LIT:1>)<EOL>cls = classdict[classname]<EOL>trait = cls.class_traits(config=True)[traitname]<EOL>help = cls.class_get_trait_help(trait).splitlines()<EOL>help[<NUM_LIT:0>] = help[<NUM_LIT:0>].replace(longname, alias) + '<STR_LIT>'%longname<EOL>if len(alias) == <NUM_LIT:1>:<EOL><INDENT>help[<NUM_LIT:0>] = help[<NUM_LIT:0>].replace('<STR_LIT>'%alias, '<STR_LIT>'%alias)<EOL><DEDENT>lines.extend(help)<EOL><DEDENT>print(os.linesep.join(lines))<EOL>


if not self.flags:<EOL><INDENT>return<EOL><DEDENT>lines = []<EOL>for m, (cfg,help) in self.flags.items():<EOL><INDENT>prefix = '<STR_LIT>' if len(m) > <NUM_LIT:1> else '<STR_LIT:->'<EOL>lines.append(prefix+m)<EOL>lines.append(indent(dedent(help.strip())))<EOL><DEDENT>print(os.linesep.join(lines))<EOL>


if not self.subcommands:<EOL><INDENT>return<EOL><DEDENT>lines = ["<STR_LIT>"]<EOL>lines.append('<STR_LIT:->'*len(lines[<NUM_LIT:0>]))<EOL>lines.append('<STR_LIT>')<EOL>for p in wrap_paragraphs(self.subcommand_description):<EOL><INDENT>lines.append(p)<EOL>lines.append('<STR_LIT>')<EOL><DEDENT>for subc, (cls, help) in self.subcommands.items():<EOL><INDENT>lines.append(subc)<EOL>if help:<EOL><INDENT>lines.append(indent(dedent(help.strip())))<EOL><DEDENT><DEDENT>lines.append('<STR_LIT>')<EOL>print(os.linesep.join(lines))<EOL>


self.print_subcommands()<EOL>self.print_options()<EOL>if classes:<EOL><INDENT>if self.classes:<EOL><INDENT>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print()<EOL>for p in wrap_paragraphs(self.keyvalue_description):<EOL><INDENT>print(p)<EOL>print()<EOL><DEDENT><DEDENT>for cls in self.classes:<EOL><INDENT>cls.class_print_help()<EOL>print()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL>print()<EOL><DEDENT>

Print the help for each Configurable class in self.classes.

        If classes=False (the default), only flags and aliases are printed.


for p in wrap_paragraphs(self.description):<EOL><INDENT>print(p)<EOL>print()<EOL><DEDENT>


if self.examples:<EOL><INDENT>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>print()<EOL>print(indent(dedent(self.examples.strip())))<EOL>print()<EOL><DEDENT>

Print usage and examples.

        This usage string goes at the end of the command line help string
        and should contain examples of the application's usage.


print(self.version)<EOL>


<EOL>newconfig = deepcopy(self.config)<EOL>newconfig._merge(config)<EOL>self.config = newconfig<EOL>


@catch_config_error<EOL><INDENT>def initialize_subcommand(self, subc, argv=None):<DEDENT>

subapp,help = self.subcommands.get(subc)<EOL>if isinstance(subapp, str):<EOL><INDENT>subapp = import_item(subapp)<EOL><DEDENT>self.__class__.clear_instance()<EOL>self.subapp = subapp.instance()<EOL>self.subapp.initialize(argv)<EOL>


<EOL>mro_tree = defaultdict(list)<EOL>for cls in self.classes:<EOL><INDENT>clsname = cls.__name__<EOL>for parent in cls.mro()[<NUM_LIT:1>:-<NUM_LIT:3>]:<EOL><INDENT>mro_tree[parent.__name__].append(clsname)<EOL><DEDENT><DEDENT>aliases = {}<EOL>for alias, cls_trait in self.aliases.items():<EOL><INDENT>cls,trait = cls_trait.split('<STR_LIT:.>',<NUM_LIT:1>)<EOL>children = mro_tree[cls]<EOL>if len(children) == <NUM_LIT:1>:<EOL><INDENT>cls = children[<NUM_LIT:0>]<EOL><DEDENT>aliases[alias] = '<STR_LIT:.>'.join([cls,trait])<EOL><DEDENT>flags = {}<EOL>for key, (flagdict, help) in self.flags.items():<EOL><INDENT>newflag = {}<EOL>for cls, subdict in flagdict.items():<EOL><INDENT>children = mro_tree[cls]<EOL>if len(children) == <NUM_LIT:1>:<EOL><INDENT>cls = children[<NUM_LIT:0>]<EOL><DEDENT>newflag[cls] = subdict<EOL><DEDENT>flags[key] = (newflag, help)<EOL><DEDENT>return flags, aliases<EOL>

flatten flags and aliases, so cl-args override as expected.

        This prevents issues such as an alias pointing to InteractiveShell,
        but a config file setting the same trait in TerminalInteraciveShell
        getting inappropriate priority over the command-line arg.

        Only aliases with exactly one descendent in the class list
        will be promoted.


argv = sys.argv[<NUM_LIT:1>:] if argv is None else argv<EOL>if argv and argv[<NUM_LIT:0>] == '<STR_LIT>':<EOL><INDENT>argv = argv[<NUM_LIT:1>:] + ['<STR_LIT>']<EOL><DEDENT>if self.subcommands and len(argv) > <NUM_LIT:0>:<EOL><INDENT>subc, subargv = argv[<NUM_LIT:0>], argv[<NUM_LIT:1>:]<EOL>if re.match(r'<STR_LIT>', subc) and subc in self.subcommands:<EOL><INDENT>return self.initialize_subcommand(subc, subargv)<EOL><DEDENT><DEDENT>if '<STR_LIT>' in argv or '<STR_LIT>' in argv or '<STR_LIT>' in argv:<EOL><INDENT>self.print_description()<EOL>self.print_help('<STR_LIT>' in argv)<EOL>self.print_examples()<EOL>self.exit(<NUM_LIT:0>)<EOL><DEDENT>if '<STR_LIT>' in argv or '<STR_LIT>' in argv:<EOL><INDENT>self.print_version()<EOL>self.exit(<NUM_LIT:0>)<EOL><DEDENT>flags,aliases = self.flatten_flags()<EOL>loader = KVArgParseConfigLoader(argv=argv, aliases=aliases,<EOL>flags=flags)<EOL>config = loader.load_config()<EOL>self.update_config(config)<EOL>self.extra_args = loader.extra_args<EOL>


@catch_config_error<EOL><INDENT>def load_config_file(self, filename, path=None):<DEDENT>

loader = PyFileConfigLoader(filename, path=path)<EOL>try:<EOL><INDENT>config = loader.load_config()<EOL><DEDENT>except ConfigFileNotFound:<EOL><INDENT>raise<EOL><DEDENT>except Exception:<EOL><INDENT>filename = loader.full_filename or filename<EOL>self.log.error("<STR_LIT>",<EOL>filename, exc_info=True)<EOL><DEDENT>else:<EOL><INDENT>self.log.debug("<STR_LIT>", loader.full_filename)<EOL>self.update_config(config)<EOL><DEDENT>

Load a .py based config file by filename and path.


lines = ["<STR_LIT>"%self.name]<EOL>lines.append('<STR_LIT>')<EOL>lines.append('<STR_LIT>')<EOL>lines.append('<STR_LIT>')<EOL>for cls in self.classes:<EOL><INDENT>lines.append(cls.class_config_section())<EOL><DEDENT>return '<STR_LIT:\n>'.join(lines)<EOL>

generate default config file from Configurables


config = Config()<EOL>for cf in config_files:<EOL><INDENT>loader = PyFileConfigLoader(cf, path=path)<EOL>try:<EOL><INDENT>next_config = loader.load_config()<EOL><DEDENT>except ConfigFileNotFound:<EOL><INDENT>pass<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>config._merge(next_config)<EOL><DEDENT><DEDENT>return config<EOL>

Load multiple Python config files, merging each of them in turn.

    Parameters
    ==========
    config_files : list of str
        List of config files names to load and merge into the config.
    path : unicode
        The full path to the location of the config files.


self.clear()<EOL>

A base class for config loaders.

        Examples
        --------

        >>> cl = ConfigLoader()
        >>> config = cl.load_config()
        >>> config
        {}


self.clear()<EOL>return self.config<EOL>

Load a config from somewhere, return a :class:`Config` instance.

        Usually, this will cause self.config to be set and then returned.
        However, in most cases, :meth:`ConfigLoader.clear` should be called
        to erase any previous state.


def __init__(self, filename, path=None):

super(PyFileConfigLoader, self).__init__()<EOL>self.filename = filename<EOL>self.path = path<EOL>self.full_filename = '<STR_LIT>'<EOL>self.data = None<EOL>

Build a config loader for a filename and path.

        Parameters
        ----------
        filename : str
            The file name of the config file.
        path : str, list, tuple
            The path to search for the config file on, or a sequence of
            paths to try in order.


self.clear()<EOL>try:<EOL><INDENT>self._find_file()<EOL><DEDENT>except IOError as e:<EOL><INDENT>raise ConfigFileNotFound(str(e))<EOL><DEDENT>self._read_file_as_dict()<EOL>self._convert_to_config()<EOL>return self.config<EOL>

Load the config from a file and return it as a Struct.


self.full_filename = filefind(self.filename, self.path)<EOL>

Try to find the file by searching the paths.


<EOL>def load_subconfig(fname, profile=None):<EOL><INDENT>from IPython.core.profiledir import ProfileDir, ProfileDirError<EOL>if profile is not None:<EOL><INDENT>try:<EOL><INDENT>profile_dir = ProfileDir.find_profile_dir_by_name(<EOL>get_ipython_dir(),<EOL>profile,<EOL>)<EOL><DEDENT>except ProfileDirError:<EOL><INDENT>return<EOL><DEDENT>path = profile_dir.location<EOL><DEDENT>else:<EOL><INDENT>path = self.path<EOL><DEDENT>loader = PyFileConfigLoader(fname, path)<EOL>try:<EOL><INDENT>sub_config = loader.load_config()<EOL><DEDENT>except ConfigFileNotFound:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>self.config._merge(sub_config)<EOL><DEDENT><DEDENT>def get_config():<EOL><INDENT>return self.config<EOL><DEDENT>namespace = dict(load_subconfig=load_subconfig, get_config=get_config)<EOL>fs_encoding = sys.getfilesystemencoding() or '<STR_LIT:ascii>'<EOL>conf_filename = self.full_filename.encode(fs_encoding)<EOL>py3compat.execfile(conf_filename, namespace)<EOL>

Load the config file into self.config, with recursive loading.


rhs = os.path.expanduser(rhs)<EOL>try:<EOL><INDENT>value = eval(rhs)<EOL><DEDENT>except (NameError, SyntaxError):<EOL><INDENT>value = rhs<EOL><DEDENT>exec('<STR_LIT>' % lhs)<EOL>

execute self.config.<lhs> = <rhs>

        * expands ~ with expanduser
        * tries to assign with raw eval, otherwise assigns with just the string,
          allowing `--C.a=foobar` and `--C.a="foobar"` to be equivalent.  *Not*
          equivalent are `--C.a=4` and `--C.a='4'`.


if isinstance(cfg, (dict, Config)):<EOL><INDENT>for sec,c in cfg.items():<EOL><INDENT>self.config[sec].update(c)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>" % cfg)<EOL><DEDENT>

update self.config from a flag, which can be a dict or Config


def __init__(self, argv=None, aliases=None, flags=None):

self.clear()<EOL>if argv is None:<EOL><INDENT>argv = sys.argv[<NUM_LIT:1>:]<EOL><DEDENT>self.argv = argv<EOL>self.aliases = aliases or {}<EOL>self.flags = flags or {}<EOL>

Create a key value pair config loader.

        Parameters
        ----------
        argv : list
            A list that has the form of sys.argv[1:] which has unicode
            elements of the form u"key=value". If this is None (default),
            then sys.argv[1:] will be used.
        aliases : dict
            A dict of aliases for configurable traits.
            Keys are the short aliases, Values are the resolved trait.
            Of the form: `{'alias' : 'Configurable.trait'}`
        flags : dict
            A dict of flags, keyed by str name. Vaues can be Config objects,
            dicts, or "key=value" strings.  If Config or dict, when the flag
            is triggered, The flag is loaded as `self.config.update(m)`.

        Returns
        -------
        config : Config
            The resulting Config object.

        Examples
        --------

            >>> from IPython.config.loader import KeyValueConfigLoader
            >>> cl = KeyValueConfigLoader()
            >>> d = cl.load_config(["--A.name='brian'","--B.number=0"])
            >>> sorted(d.items())
            [('A', {'name': 'brian'}), ('B', {'number': 0})]


uargv = []<EOL>if enc is None:<EOL><INDENT>enc = DEFAULT_ENCODING<EOL><DEDENT>for arg in argv:<EOL><INDENT>if not isinstance(arg, str):<EOL><INDENT>arg = arg.decode(enc)<EOL><DEDENT>uargv.append(arg)<EOL><DEDENT>return uargv<EOL>

decode argv if bytes, using stin.encoding, falling back on default enc


def load_config(self, argv=None, aliases=None, flags=None):

from IPython.config.configurable import Configurable<EOL>self.clear()<EOL>if argv is None:<EOL><INDENT>argv = self.argv<EOL><DEDENT>if aliases is None:<EOL><INDENT>aliases = self.aliases<EOL><DEDENT>if flags is None:<EOL><INDENT>flags = self.flags<EOL><DEDENT>uargv = self._decode_argv(argv)<EOL>for idx,raw in enumerate(uargv):<EOL><INDENT>item = raw.lstrip('<STR_LIT:->')<EOL>if raw == '<STR_LIT>':<EOL><INDENT>self.extra_args.extend(uargv[idx+<NUM_LIT:1>:])<EOL>break<EOL><DEDENT>if kv_pattern.match(raw):<EOL><INDENT>lhs,rhs = item.split('<STR_LIT:=>',<NUM_LIT:1>)<EOL>if lhs in aliases:<EOL><INDENT>lhs = aliases[lhs]<EOL><DEDENT>if '<STR_LIT:.>' not in lhs:<EOL><INDENT>warn.warn("<STR_LIT>"%lhs)<EOL><DEDENT>try:<EOL><INDENT>self._exec_config_str(lhs, rhs)<EOL><DEDENT>except Exception:<EOL><INDENT>raise ArgumentError("<STR_LIT>" % raw)<EOL><DEDENT><DEDENT>elif flag_pattern.match(raw):<EOL><INDENT>if item in flags:<EOL><INDENT>cfg,help = flags[item]<EOL>self._load_flag(cfg)<EOL><DEDENT>else:<EOL><INDENT>raise ArgumentError("<STR_LIT>"%raw)<EOL><DEDENT><DEDENT>elif raw.startswith('<STR_LIT:->'):<EOL><INDENT>kv = '<STR_LIT>'+item<EOL>if kv_pattern.match(kv):<EOL><INDENT>raise ArgumentError("<STR_LIT>"%(raw, kv))<EOL><DEDENT>else:<EOL><INDENT>raise ArgumentError("<STR_LIT>"%raw)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self.extra_args.append(item)<EOL><DEDENT><DEDENT>return self.config<EOL>

Parse the configuration and generate the Config object.

        After loading, any arguments that are not key-value or
        flags will be stored in self.extra_args - a list of
        unparsed command-line arguments.  This is used for
        arguments such as input files or subcommands.

        Parameters
        ----------
        argv : list, optional
            A list that has the form of sys.argv[1:] which has unicode
            elements of the form u"key=value". If this is None (default),
            then self.argv will be used.
        aliases : dict
            A dict of aliases for configurable traits.
            Keys are the short aliases, Values are the resolved trait.
            Of the form: `{'alias' : 'Configurable.trait'}`
        flags : dict
            A dict of flags, keyed by str name. Values can be Config objects
            or dicts.  When the flag is triggered, The config is loaded as
            `self.config.update(cfg)`.


def __init__(self, argv=None, aliases=None, flags=None, *parser_args, **parser_kw):

super(CommandLineConfigLoader, self).__init__()<EOL>self.clear()<EOL>if argv is None:<EOL><INDENT>argv = sys.argv[<NUM_LIT:1>:]<EOL><DEDENT>self.argv = argv<EOL>self.aliases = aliases or {}<EOL>self.flags = flags or {}<EOL>self.parser_args = parser_args<EOL>self.version = parser_kw.pop("<STR_LIT:version>", None)<EOL>kwargs = dict(argument_default=argparse.SUPPRESS)<EOL>kwargs.update(parser_kw)<EOL>self.parser_kw = kwargs<EOL>

Create a config loader for use with argparse.

        Parameters
        ----------

        argv : optional, list
          If given, used to read command-line arguments from, otherwise
          sys.argv[1:] is used.

        parser_args : tuple
          A tuple of positional arguments that will be passed to the
          constructor of :class:`argparse.ArgumentParser`.

        parser_kw : dict
          A tuple of keyword arguments that will be passed to the
          constructor of :class:`argparse.ArgumentParser`.

        Returns
        -------
        config : Config
            The resulting Config object.


self.clear()<EOL>if argv is None:<EOL><INDENT>argv = self.argv<EOL><DEDENT>if aliases is None:<EOL><INDENT>aliases = self.aliases<EOL><DEDENT>if flags is None:<EOL><INDENT>flags = self.flags<EOL><DEDENT>self._create_parser(aliases, flags)<EOL>self._parse_args(argv)<EOL>self._convert_to_config()<EOL>return self.config<EOL>

Parse command line arguments and return as a Config object.

        Parameters
        ----------

        args : optional, list
          If given, a list with the structure of sys.argv[1:] to parse
          arguments from. If not given, the instance's self.argv attribute
          (given at construction time) is used.


<EOL>enc = DEFAULT_ENCODING<EOL>uargs = [py3compat.cast_unicode(a, enc) for a in args]<EOL>self.parsed_data, self.extra_args = self.parser.parse_known_args(uargs)<EOL>


for k, v in vars(self.parsed_data).items():<EOL><INDENT>exec("<STR_LIT>"%k, locals(), globals())<EOL><DEDENT>


<EOL>if '<STR_LIT>' in self.parsed_data:<EOL><INDENT>subcs = self.parsed_data._flags<EOL>del self.parsed_data._flags<EOL><DEDENT>else:<EOL><INDENT>subcs = []<EOL><DEDENT>for k, v in vars(self.parsed_data).items():<EOL><INDENT>if v is None:<EOL><INDENT>subcs.append(self.alias_flags[k])<EOL><DEDENT>else:<EOL><INDENT>self._exec_config_str(k, v)<EOL><DEDENT><DEDENT>for subc in subcs:<EOL><INDENT>self._load_flag(subc)<EOL><DEDENT>if self.extra_args:<EOL><INDENT>sub_parser = KeyValueConfigLoader()<EOL>sub_parser.load_config(self.extra_args)<EOL>self.config._merge(sub_parser.config)<EOL>self.extra_args = sub_parser.extra_args<EOL><DEDENT>

self.parsed_data->self.config, parse unrecognized extra args via KVLoader.


self.registered_types = set() <EOL>self.registered_senders = set() <EOL>self.observers = {}<EOL>


def post_notification(self, ntype, sender, *args, **kwargs):

if(ntype==None or sender==None):<EOL><INDENT>raise NotificationError(<EOL>"<STR_LIT>")<EOL><DEDENT>if((ntype not in self.registered_types and<EOL>None not in self.registered_types) or<EOL>(sender not in self.registered_senders and<EOL>None not in self.registered_senders)):<EOL><INDENT>return<EOL><DEDENT>for o in self._observers_for_notification(ntype, sender):<EOL><INDENT>o(ntype, sender, *args, **kwargs)<EOL><DEDENT>

Post notification to all registered observers.

        The registered callback will be called as::

            callback(ntype, sender, *args, **kwargs)

        Parameters
        ----------
        ntype : hashable
            The notification type.
        sender : hashable
            The object sending the notification.
        *args : tuple
            The positional arguments to be passed to the callback.
        **kwargs : dict
            The keyword argument to be passed to the callback.

        Notes
        -----
        * If no registered observers, performance is O(1).
        * Notificaiton order is undefined.
        * Notifications are posted synchronously.


def _observers_for_notification(self, ntype, sender):

keys = (<EOL>(ntype,sender),<EOL>(ntype, None),<EOL>(None, sender),<EOL>(None,None)<EOL>)<EOL>obs = set()<EOL>for k in keys:<EOL><INDENT>obs.update(self.observers.get(k, set()))<EOL><DEDENT>return obs<EOL>

Find all registered observers that should recieve notification


def add_observer(self, callback, ntype, sender):

assert(callback != None)<EOL>self.registered_types.add(ntype)<EOL>self.registered_senders.add(sender)<EOL>self.observers.setdefault((ntype,sender), set()).add(callback)<EOL>

Add an observer callback to this notification center.

        The given callback will be called upon posting of notifications of
        the given type/sender and will receive any additional arguments passed
        to post_notification.

        Parameters
        ----------
        callback : callable
            The callable that will be called by :meth:`post_notification`
            as ``callback(ntype, sender, *args, **kwargs)
        ntype : hashable
            The notification type. If None, all notifications from sender
            will be posted.
        sender : hashable
            The notification sender. If None, all notifications of ntype
            will be posted.


Removes all observers from this notification center


return path<EOL>


return os.path.isdir(path) and os.access(path, os.W_OK)<EOL>

Whether `path` is a directory, to which the user has write access.


return _get_long_path_name(path)<EOL>

Expand a path into its long form.

    On Windows this expands any ~ in the paths. On other platforms, it is
    a null operation.


if win32:<EOL><INDENT>if name.startswith(("<STR_LIT:'>", '<STR_LIT:">')) and name.endswith(("<STR_LIT:'>", '<STR_LIT:">')):<EOL><INDENT>name = name[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL><DEDENT><DEDENT>return name<EOL>

On Windows, remove leading and trailing quotes from filenames.


name = os.path.expanduser(name)<EOL>if force_win32 is None:<EOL><INDENT>win32 = (sys.platform == '<STR_LIT:win32>')<EOL><DEDENT>else:<EOL><INDENT>win32 = force_win32<EOL><DEDENT>name = unquote_filename(name, win32=win32)<EOL>if not os.path.isfile(name) and not name.endswith('<STR_LIT>'):<EOL><INDENT>name += '<STR_LIT>'<EOL><DEDENT>if os.path.isfile(name):<EOL><INDENT>return name<EOL><DEDENT>else:<EOL><INDENT>raise IOError('<STR_LIT>' % name)<EOL><DEDENT>

Return a valid python filename in the current directory.

    If the given name is not a file, it adds '.py' and searches again.
    Raises IOError with an informative message if the file isn't found.

    On Windows, apply Windows semantics to the filename. In particular, remove
    any quoting that has been applied to it. This option can be forced for
    testing purposes.


<EOL>filename = filename.strip('<STR_LIT:">').strip("<STR_LIT:'>")<EOL>if os.path.isabs(filename) and os.path.isfile(filename):<EOL><INDENT>return filename<EOL><DEDENT>if path_dirs is None:<EOL><INDENT>path_dirs = ("<STR_LIT>",)<EOL><DEDENT>elif isinstance(path_dirs, str):<EOL><INDENT>path_dirs = (path_dirs,)<EOL><DEDENT>for path in path_dirs:<EOL><INDENT>if path == '<STR_LIT:.>': path = os.getcwd()<EOL>testname = expand_path(os.path.join(path, filename))<EOL>if os.path.isfile(testname):<EOL><INDENT>return os.path.abspath(testname)<EOL><DEDENT><DEDENT>raise IOError("<STR_LIT>" %<EOL>(filename, path_dirs) )<EOL>

Find a file by looking through a sequence of paths.

    This iterates through a sequence of paths looking for a file and returns
    the full, absolute path of the first occurence of the file.  If no set of
    path dirs is given, the filename is tested as is, after running through
    :func:`expandvars` and :func:`expanduser`.  Thus a simple call::

        filefind('myfile.txt')

    will find the file in the current working dir, but::

        filefind('~/myfile.txt')

    Will find the file in the users home directory.  This function does not
    automatically try any paths, such as the cwd or the user's home directory.

    Parameters
    ----------
    filename : str
        The filename to look for.
    path_dirs : str, None or sequence of str
        The sequence of paths to look for the file in.  If None, the filename
        need to be absolute or be in the cwd.  If a string, the string is
        put into a sequence and the searched.  If a sequence, walk through
        each element and join with ``filename``, calling :func:`expandvars`
        and :func:`expanduser` before testing for existence.

    Returns
    -------
    Raises :exc:`IOError` or returns absolute path to file.


<EOL>if hasattr(sys, "<STR_LIT>"): <EOL><INDENT>if '<STR_LIT>' in IPython.__file__.lower():<EOL><INDENT>root, rest = IPython.__file__.lower().split('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>root=os.path.join(os.path.split(IPython.__file__)[<NUM_LIT:0>],"<STR_LIT>")<EOL><DEDENT>root=os.path.abspath(root).rstrip('<STR_LIT:\\>')<EOL>if _writable_dir(os.path.join(root, '<STR_LIT>')):<EOL><INDENT>os.environ["<STR_LIT>"] = root<EOL><DEDENT>return py3compat.cast_unicode(root, fs_encoding)<EOL><DEDENT>homedir = os.path.expanduser('<STR_LIT>')<EOL>homedir = os.path.realpath(homedir)<EOL>if not _writable_dir(homedir) and os.name == '<STR_LIT>':<EOL><INDENT>try:<EOL><INDENT>import winreg as wreg<EOL>key = wreg.OpenKey(<EOL>wreg.HKEY_CURRENT_USER,<EOL>"<STR_LIT>"<EOL>)<EOL>homedir = wreg.QueryValueEx(key,'<STR_LIT>')[<NUM_LIT:0>]<EOL>key.Close()<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if (not require_writable) or _writable_dir(homedir):<EOL><INDENT>return py3compat.cast_unicode(homedir, fs_encoding)<EOL><DEDENT>else:<EOL><INDENT>raise HomeDirError('<STR_LIT>'<EOL>'<STR_LIT>' % homedir)<EOL><DEDENT>

Return the 'home' directory, as a unicode string.

    * First, check for frozen env in case of py2exe
    * Otherwise, defer to os.path.expanduser('~')

    See stdlib docs for how this is determined.
    $HOME is first priority on *ALL* platforms.

    Parameters
    ----------

    require_writable : bool [default: False]
        if True:
            guarantees the return value is a writable directory, otherwise
            raises HomeDirError
        if False:
            The path is resolved, but it is not guaranteed to exist or be writable.


env = os.environ<EOL>if os.name == '<STR_LIT>' and sys.platform != '<STR_LIT>':<EOL><INDENT>xdg = env.get("<STR_LIT>", None) or os.path.join(get_home_dir(), '<STR_LIT>')<EOL>if xdg and _writable_dir(xdg):<EOL><INDENT>return py3compat.cast_unicode(xdg, fs_encoding)<EOL><DEDENT><DEDENT>return None<EOL>

Return the XDG_CONFIG_HOME, if it is defined and exists, else None.

    This is only for non-OS X posix (Linux,Unix,etc.) systems.


env = os.environ<EOL>pjoin = os.path.join<EOL>ipdir_def = '<STR_LIT>'<EOL>xdg_def = '<STR_LIT>'<EOL>home_dir = get_home_dir()<EOL>xdg_dir = get_xdg_dir()<EOL>if '<STR_LIT>' in env:<EOL><INDENT>warnings.warn('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>ipdir = env.get('<STR_LIT>', env.get('<STR_LIT>', None))<EOL>if ipdir is None:<EOL><INDENT>home_ipdir = pjoin(home_dir, ipdir_def)<EOL>if xdg_dir:<EOL><INDENT>xdg_ipdir = pjoin(xdg_dir, xdg_def)<EOL>if _writable_dir(xdg_ipdir) or not _writable_dir(home_ipdir):<EOL><INDENT>ipdir = xdg_ipdir<EOL><DEDENT><DEDENT>if ipdir is None:<EOL><INDENT>ipdir = home_ipdir<EOL><DEDENT><DEDENT>ipdir = os.path.normpath(os.path.expanduser(ipdir))<EOL>if os.path.exists(ipdir) and not _writable_dir(ipdir):<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>"%ipdir)<EOL>ipdir = tempfile.mkdtemp()<EOL><DEDENT>elif not os.path.exists(ipdir):<EOL><INDENT>parent = ipdir.rsplit(os.path.sep, <NUM_LIT:1>)[<NUM_LIT:0>]<EOL>if not _writable_dir(parent):<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>"%parent)<EOL>ipdir = tempfile.mkdtemp()<EOL><DEDENT><DEDENT>return py3compat.cast_unicode(ipdir, fs_encoding)<EOL>

Get the IPython directory for this platform and user.

    This uses the logic in `get_home_dir` to find the home directory
    and then adds .ipython to the end of the path.


ipdir = os.path.dirname(IPython.__file__)<EOL>return py3compat.cast_unicode(ipdir, fs_encoding)<EOL>

Get the base directory where IPython itself is installed.


if module_str == '<STR_LIT>':<EOL><INDENT>return os.path.join(get_ipython_package_dir(), '<STR_LIT>')<EOL><DEDENT>mod = import_item(module_str)<EOL>the_path = mod.__file__.replace('<STR_LIT>', '<STR_LIT>')<EOL>the_path = the_path.replace('<STR_LIT>', '<STR_LIT>')<EOL>return py3compat.cast_unicode(the_path, fs_encoding)<EOL>

Find the path to an IPython module in this version of IPython.

    This will always find the version of the module that is in this importable
    IPython package. This will always return the path to the ``.py``
    version of the module.


from IPython.core.profiledir import ProfileDir, ProfileDirError<EOL>try:<EOL><INDENT>pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)<EOL><DEDENT>except ProfileDirError:<EOL><INDENT>raise IOError("<STR_LIT>" % profile)<EOL><DEDENT>return pd.location<EOL>

Find the path to the folder associated with a given profile.

    I.e. find $IPYTHONDIR/profile_whatever.


<EOL>if os.name=='<STR_LIT>':<EOL><INDENT>s = s.replace('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>s = os.path.expandvars(os.path.expanduser(s))<EOL>if os.name=='<STR_LIT>':<EOL><INDENT>s = s.replace('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT>return s<EOL>

Expand $VARS and ~names in a string, like a shell

    :Examples:

       In [2]: os.environ['FOO']='test'

       In [3]: expand_path('variable FOO is $FOO')
       Out[3]: 'variable FOO is test'


try:<EOL><INDENT>target_time = os.path.getmtime(target)<EOL><DEDENT>except os.error:<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>for dep in deps:<EOL><INDENT>dep_time = os.path.getmtime(dep)<EOL>if dep_time > target_time:<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT><DEDENT>return <NUM_LIT:0><EOL>

Determine whether a target is out of date.

    target_outdated(target,deps) -> 1/0

    deps: list of filenames which MUST exist.
    target: single filename which may or may not exist.

    If target doesn't exist or is older than any file listed in deps, return
    true, otherwise return false.


if target_outdated(target,deps):<EOL><INDENT>system(cmd)<EOL><DEDENT>

Update a target with a given command given a list of dependencies.

    target_update(target,deps,cmd) -> runs cmd if target is outdated.

    This is just a wrapper around target_outdated() which calls the given
    command if target is outdated.


with open(path, "<STR_LIT>") as f:<EOL><INDENT>return md5(py3compat.str_to_bytes(f.read())).hexdigest()<EOL><DEDENT>

Make an MD5 hash of a file, ignoring any differences in line
    ending characters.


if ipython_dir is None:<EOL><INDENT>ipython_dir = get_ipython_dir()<EOL><DEDENT>old_configs = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>warned = False<EOL>for cfg in old_configs:<EOL><INDENT>f = os.path.join(ipython_dir, cfg)<EOL>if os.path.exists(f):<EOL><INDENT>if filehash(f) == old_config_md5.get(cfg, '<STR_LIT>'):<EOL><INDENT>os.unlink(f)<EOL><DEDENT>else:<EOL><INDENT>warnings.warn("<STR_LIT>"%f)<EOL>warned = True<EOL><DEDENT><DEDENT><DEDENT>if warned:<EOL><INDENT>warnings.warn("""<STR_LIT>""")<EOL><DEDENT>

Check for old config files, and present a warning if they exist.

    A link to the docs of the new config is included in the message.

    This should mitigate confusion with the transition to the new
    config system in 0.11.


<EOL>from IPython.core.profiledir import ProfileDir<EOL>try:<EOL><INDENT>pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)<EOL><DEDENT>except Exception:<EOL><INDENT>raise IOError("<STR_LIT>")<EOL><DEDENT>return filefind(filename, ['<STR_LIT:.>', pd.security_dir])<EOL>

Return the absolute path of a security file given by filename and profile

    This allows users and developers to find security files without
    knowledge of the IPython directory structure. The search path
    will be ['.', profile.security_dir]

    Parameters
    ----------

    filename : str
        The file to be found. If it is passed as an absolute path, it will
        simply be returned.
    profile : str [default: 'default']
        The name of the profile to search.  Leaving this unspecified
        The file to be found. If it is passed as an absolute path, fname will
        simply be returned.

    Returns
    -------
    Raises :exc:`IOError` if file not found or returns absolute path to file.


return OneTimeProperty(func)<EOL>

Decorator to create OneTimeProperty attributes.

    Parameters
    ----------
      func : method
        The method that will be called the first time to compute a value.
        Afterwards, the method's name will be a standard attribute holding the
        value of this computation.

    Examples
    --------
    >>> class MagicProp(object):
    ...     @auto_attr
    ...     def a(self):
    ...         return 99
    ...
    >>> x = MagicProp()
    >>> 'a' in x.__dict__
    False
    >>> x.a
    99
    >>> 'a' in x.__dict__
    True


instdict = self.__dict__<EOL>classdict = self.__class__.__dict__<EOL>for mname, mval in classdict.items():<EOL><INDENT>if mname in instdict and isinstance(mval, OneTimeProperty):<EOL><INDENT>delattr(self, mname)<EOL><DEDENT><DEDENT>

Reset all OneTimeProperty attributes that may have fired already.


self.getter = func<EOL>self.name = func.func_name<EOL>

Create a OneTimeProperty instance.

         Parameters
         ----------
           func : method

             The method that will be called the first time to compute a value.
             Afterwards, the method's name will be a standard attribute holding
             the value of this computation.


if obj is None:<EOL><INDENT>return self.getter<EOL><DEDENT>val = self.getter(obj)<EOL>setattr(obj, self.name, val)<EOL>return val<EOL>

This will be called on attribute access on the class or instance.


if level><NUM_LIT:0>:<EOL><INDENT>header = ['<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>','<STR_LIT>']<EOL>io.stderr.write('<STR_LIT>' % (header[level],msg))<EOL>if level == <NUM_LIT:4>:<EOL><INDENT>print >> io.stderr,'<STR_LIT>'<EOL>sys.exit(exit_val)<EOL><DEDENT><DEDENT>

Standard warning printer. Gives formatting consistency.

    Output is sent to io.stderr (sys.stderr by default).

    Options:

    -level(2): allows finer control:
      0 -> Do nothing, dummy function.
      1 -> Print message.
      2 -> Print 'WARNING:' + message. (Default level).
      3 -> Print 'ERROR:' + message.
      4 -> Print 'FATAL ERROR:' + message and trigger a sys.exit(exit_val).

    -exit_val (1): exit value returned by sys.exit() for a level 4
    warning. Ignored for all other levels.


warn(msg,exit_val=exit_val,level=<NUM_LIT:4>)<EOL>


path = sp.Popen(['<STR_LIT>', '<STR_LIT>', cmd],<EOL>stdout=sp.PIPE, stderr=sp.PIPE).communicate()[<NUM_LIT:0>]<EOL>return py3compat.bytes_to_str(path)<EOL>

Find the full path to a command using which.


def __init__(self, logfile=None, read_timeout=None, terminate_timeout=None):

self.read_timeout = (ProcessHandler.read_timeout if read_timeout is<EOL>None else read_timeout)<EOL>self.terminate_timeout = (ProcessHandler.terminate_timeout if<EOL>terminate_timeout is None else<EOL>terminate_timeout)<EOL>self.logfile = sys.stdout if logfile is None else logfile<EOL>


try:<EOL><INDENT>return pexpect.run(self.sh, args=['<STR_LIT:-c>', cmd]).replace('<STR_LIT:\r\n>', '<STR_LIT:\n>')<EOL><DEDENT>except KeyboardInterrupt:<EOL><INDENT>print('<STR_LIT>', file=sys.stderr, end='<STR_LIT>')<EOL><DEDENT>

Run a command and return its stdout/stderr as a string.

        Parameters
        ----------
        cmd : str
          A command to be executed in the system shell.

        Returns
        -------
        output : str
          A string containing the combination of stdout and stderr from the
        subprocess, in whatever order the subprocess originally wrote to its
        file descriptors (so the order of the information in this string is the
        correct order as would be seen if running the command in a terminal).


<EOL>enc = DEFAULT_ENCODING<EOL>patterns = [pexpect.TIMEOUT, pexpect.EOF]<EOL>EOF_index = patterns.index(pexpect.EOF)<EOL>out_size = <NUM_LIT:0><EOL>try:<EOL><INDENT>if hasattr(pexpect, '<STR_LIT>'):<EOL><INDENT>child = pexpect.spawnb(self.sh, args=['<STR_LIT:-c>', cmd]) <EOL><DEDENT>else:<EOL><INDENT>child = pexpect.spawn(self.sh, args=['<STR_LIT:-c>', cmd])  <EOL><DEDENT>flush = sys.stdout.flush<EOL>while True:<EOL><INDENT>res_idx = child.expect_list(patterns, self.read_timeout)<EOL>print(child.before[out_size:].decode(enc, '<STR_LIT:replace>'), end='<STR_LIT>')<EOL>flush()<EOL>if res_idx==EOF_index:<EOL><INDENT>break<EOL><DEDENT>out_size = len(child.before)<EOL><DEDENT><DEDENT>except KeyboardInterrupt:<EOL><INDENT>child.sendline(chr(<NUM_LIT:3>))<EOL>try:<EOL><INDENT>out_size = len(child.before)<EOL>child.expect_list(patterns, self.terminate_timeout)<EOL>print(child.before[out_size:].decode(enc, '<STR_LIT:replace>'), end='<STR_LIT>')<EOL>sys.stdout.flush()<EOL><DEDENT>except KeyboardInterrupt:<EOL><INDENT>pass<EOL><DEDENT>finally:<EOL><INDENT>child.terminate(force=True)<EOL><DEDENT><DEDENT>child.isalive()<EOL>return child.exitstatus<EOL>

Execute a command in a subshell.

        Parameters
        ----------
        cmd : str
          A command to be executed in the system shell.

        Returns
        -------
        int : child's exitstatus


package = '<STR_LIT:.>'.join(name.split('<STR_LIT:.>')[<NUM_LIT:0>:-<NUM_LIT:1>])<EOL>obj = name.split('<STR_LIT:.>')[-<NUM_LIT:1>]<EOL>if package:<EOL><INDENT>module = __import__(package,fromlist=[obj])<EOL>try:<EOL><INDENT>pak = module.__dict__[obj]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise ImportError('<STR_LIT>' % obj)<EOL><DEDENT>return pak<EOL><DEDENT>else:<EOL><INDENT>return __import__(obj)<EOL><DEDENT>

Import and return bar given the string foo.bar.


<EOL>os.makedirs(IP_TEST_DIR)<EOL>os.makedirs(os.path.join(XDG_TEST_DIR, '<STR_LIT>'))<EOL>

Setup testenvironment for the module:

            - Adds dummy home dir tree


Teardown testenvironment for the module:

            - Remove dummy home dir tree


global oldstuff, platformstuff<EOL>oldstuff = (env.copy(), os.name, sys.platform, path.get_home_dir, IPython.__file__, os.getcwd())<EOL>if os.name == '<STR_LIT>':<EOL><INDENT>platformstuff = (wreg.OpenKey, wreg.QueryValueEx,)<EOL><DEDENT>

Setup testenvironment for some functions that are tested
    in this module. In particular this functions stores attributes
    and other things that we need to stub in some test functions.
    This needs to be done on a function level and not module level because
    each testfunction needs a pristine environment.


(oldenv, os.name, sys.platform, path.get_home_dir, IPython.__file__, old_wd) = oldstuff<EOL>os.chdir(old_wd)<EOL>reload(path)<EOL>for key in env.keys():<EOL><INDENT>if key not in oldenv:<EOL><INDENT>del env[key]<EOL><DEDENT><DEDENT>env.update(oldenv)<EOL>if hasattr(sys, '<STR_LIT>'):<EOL><INDENT>del sys.frozen<EOL><DEDENT>if os.name == '<STR_LIT>':<EOL><INDENT>(wreg.OpenKey, wreg.QueryValueEx,) = platformstuff<EOL><DEDENT>

Restore things that were remebered by the setup_environment function


lines = ["<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>",<EOL>"<STR_LIT>"]<EOL>self.mktmp('<STR_LIT:\n>'.join(lines))<EOL>


<EOL>os.makedirs(join(TMP_TEST_DIR, "<STR_LIT>"))<EOL>os.makedirs(join(TMP_TEST_DIR, "<STR_LIT>"))<EOL>make_empty_file(join(TMP_TEST_DIR, "<STR_LIT>"))<EOL>make_empty_file(join(TMP_TEST_DIR, "<STR_LIT>"))<EOL>make_empty_file(join(TMP_TEST_DIR, "<STR_LIT>"))<EOL>make_empty_file(join(TMP_TEST_DIR, "<STR_LIT>"))<EOL>sys.path = [TMP_TEST_DIR]<EOL>


<EOL>shutil.rmtree(TMP_TEST_DIR)<EOL>sys.path = old_syspath<EOL>

Teardown testenvironment for the module:

            - Remove tempdir
            - restore sys.path


global ignore_termtitle<EOL>ignore_termtitle = not(val)<EOL>

Control whether set_term_title is active or not.

    set_term_title() allows writing to the console titlebar.  In embedded
    widgets this can cause problems, so this call can be used to toggle it on
    or off as needed.

    The default state of the module is for the function to be disabled.

    Parameters
    ----------
      val : bool
        If True, set_term_title() actually writes to the terminal (using the
        appropriate platform-specific module).  If False, it is a no-op.


pass<EOL>


sys.stdout.write('<STR_LIT>' % title)<EOL>

Change virtual terminal title in xterm-workalikes


if ignore_termtitle:<EOL><INDENT>return<EOL><DEDENT>_set_term_title(title)<EOL>

Set terminal title using the necessary platform-dependent calls.


raise TryNext<EOL>


raise TryNext<EOL>

Custom completer dispatching for python objects.

    Parameters
    ----------
    obj : object
        The object to complete.
    prev_completions : list
        List of attributes discovered so far.

    This should return the list of attributes in obj. If you only wish to
    add to the attributes already discovered normally, return
    own_attrs + prev_completions.


self.root = Path(root).expanduser().abspath()<EOL>if not self.root.isdir():<EOL><INDENT>self.root.makedirs()<EOL><DEDENT>self.cache = {}<EOL>

Return a db object that will manage the specied directory


fil = self.root / key<EOL>try:<EOL><INDENT>mtime = (fil.stat()[stat.ST_MTIME])<EOL><DEDENT>except OSError:<EOL><INDENT>raise KeyError(key)<EOL><DEDENT>if fil in self.cache and mtime == self.cache[fil][<NUM_LIT:1>]:<EOL><INDENT>return self.cache[fil][<NUM_LIT:0>]<EOL><DEDENT>try:<EOL><INDENT>obj = pickle.loads(fil.open("<STR_LIT:rb>").read())<EOL><DEDENT>except:<EOL><INDENT>raise KeyError(key)<EOL><DEDENT>self.cache[fil] = (obj,mtime)<EOL>return obj<EOL>


fil = self.root / key<EOL>parent = fil.parent<EOL>if parent and not parent.isdir():<EOL><INDENT>parent.makedirs()<EOL><DEDENT>pickled = pickle.dump(value,fil.open('<STR_LIT:wb>'), protocol=<NUM_LIT:2>)<EOL>try:<EOL><INDENT>self.cache[fil] = (value,fil.mtime)<EOL><DEDENT>except OSError as e:<EOL><INDENT>if e.errno != <NUM_LIT:2>:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>


hroot = self.root / hashroot<EOL>if not hroot.isdir():<EOL><INDENT>hroot.makedirs()<EOL><DEDENT>hfile = hroot / gethashfile(key)<EOL>d = self.get(hfile, {})<EOL>d.update( {key : value})<EOL>self[hfile] = d<EOL>


def hget(self, hashroot, key, default = _sentinel, fast_only = True):

hroot = self.root / hashroot<EOL>hfile = hroot / gethashfile(key)<EOL>d = self.get(hfile, _sentinel )<EOL>if d is _sentinel:<EOL><INDENT>if fast_only:<EOL><INDENT>if default is _sentinel:<EOL><INDENT>raise KeyError(key)<EOL><DEDENT>return default<EOL><DEDENT>d = self.hdict(hashroot)<EOL><DEDENT>return d.get(key, default)<EOL>


hfiles = self.keys(hashroot + "<STR_LIT>")<EOL>hfiles.sort()<EOL>last = len(hfiles) and hfiles[-<NUM_LIT:1>] or '<STR_LIT>'<EOL>if last.endswith('<STR_LIT>'):<EOL><INDENT>hfiles = [last] + hfiles[:-<NUM_LIT:1>]<EOL><DEDENT>all = {}<EOL>for f in hfiles:<EOL><INDENT>try:<EOL><INDENT>all.update(self[f])<EOL><DEDENT>except KeyError:<EOL><INDENT>print("<STR_LIT>",f,"<STR_LIT>")<EOL>del self[f]<EOL><DEDENT>self.uncache(f)<EOL><DEDENT>return all<EOL>

Get all data contained in hashed category 'hashroot' as dict


hfiles = self.keys(hashroot + "<STR_LIT>")<EOL>all = {}<EOL>for f in hfiles:<EOL><INDENT>all.update(self[f])<EOL>self.uncache(f)<EOL><DEDENT>self[hashroot + '<STR_LIT>'] = all<EOL>for f in hfiles:<EOL><INDENT>p = self.root / f<EOL>if p.basename() == '<STR_LIT>':<EOL><INDENT>continue<EOL><DEDENT>p.remove()<EOL><DEDENT>

Compress category 'hashroot', so hset is fast again

        hget will fail if fast_only is True for compressed items (that were
        hset before hcompress).


fil = self.root / key<EOL>self.cache.pop(fil,None)<EOL>try:<EOL><INDENT>fil.remove()<EOL><DEDENT>except OSError:<EOL><INDENT>pass<EOL><DEDENT>


return str(self.root.relpathto(p)).replace('<STR_LIT:\\>','<STR_LIT:/>')<EOL>


if globpat is None:<EOL><INDENT>files = self.root.walkfiles()<EOL><DEDENT>else:<EOL><INDENT>files = [Path(p) for p in glob.glob(self.root/globpat)]<EOL><DEDENT>return [self._normalized(p) for p in files if p.isfile()]<EOL>

All keys in DB, or all keys matching a glob


if not items:<EOL><INDENT>self.cache = {}<EOL><DEDENT>for it in items:<EOL><INDENT>self.cache.pop(it,None)<EOL><DEDENT>

Removes all, or specified items from cache

        Use this after reading a large amount of large objects
        to free up memory, when you won't be needing the objects
        for a while.