def _sage_getdoc_unformatted(obj): r""" Return the unformatted docstring associated to ``obj`` as a string. Feed the results from this into the sage.misc.sagedoc.format for printing to the screen. INPUT: ``obj``, a function, module, etc.: something with a docstring. If ``obj`` is a Cython object with an embedded position in its docstring, the embedded position is stripped. EXAMPLES:: sage: from sagenb.misc.sageinspect import _sage_getdoc_unformatted sage: _sage_getdoc_unformatted(identity_matrix)[5:44] 'Return the `n \\times n` identity matrix' TESTS: Test that we suppress useless built-in output (Ticket #3342) sage: from sagenb.misc.sageinspect import _sage_getdoc_unformatted sage: _sage_getdoc_unformatted(isinstance.__class__) '' AUTHORS: - William Stein - extensions by Nick Alexander """ if obj is None: return '' r = None try: r = obj._sage_doc_() except AttributeError: r = obj.__doc__ #Check to see if there is an __init__ method, and if there #is, use its docstring. if r is None and hasattr(obj, '__init__'): r = obj.__init__.__doc__ if r is None: return '' # Check if the __doc__ attribute was actually a string, and # not a 'getset_descriptor' or similar. import types if not isinstance(r, types.StringTypes): return '' from sagenb.misc.misc import encoded_str return encoded_str(r)
def _sage_getdoc_unformatted(obj): r""" Return the unformatted docstring associated to ``obj`` as a string. Feed the results from this into the sage.misc.sagedoc.format for printing to the screen. INPUT: ``obj``, a function, module, etc.: something with a docstring. If ``obj`` is a Cython object with an embedded position in its docstring, the embedded position is stripped. EXAMPLES:: sage: from sagenb.misc.sageinspect import _sage_getdoc_unformatted sage: _sage_getdoc_unformatted(identity_matrix)[5:44] 'Return the `n \\times n` identity matrix' AUTHORS: - William Stein - extensions by Nick Alexander """ if obj is None: return '' r = None try: r = obj._sage_doc_() except AttributeError: r = obj.__doc__ #Check to see if there is an __init__ method, and if there #is, use its docstring. if r is None and hasattr(obj, '__init__'): r = obj.__init__.__doc__ if r is None: return '' from sagenb.misc.misc import encoded_str return encoded_str(r)
def export_worksheet(self, username, id_number, filename, title): """ Export the worksheet with given username and id_number to the given filename (e.g., 'worksheet.sws'). INPUT: - ``title`` - title to use for the exported worksheet (if None, just use current title) """ T = tarfile.open(filename, 'w:bz2') worksheet = self.load_worksheet(username, id_number) basic = copy.deepcopy(self._worksheet_to_basic(worksheet)) if title: # change the title basic['name'] = title basic['name'] = encoded_str(basic['name']) # Remove metainformation that perhaps shouldn't be distributed for k in ['owner', 'ratings', 'worksheet_that_was_published', 'viewers', 'tags', 'published_id_number', 'collaborators', 'auto_publish']: if basic.has_key(k): del basic[k] self._save(basic, self._worksheet_conf_filename(username, id_number) + '2') tmp = self._abspath(self._worksheet_conf_filename(username, id_number) + '2') T.add(tmp, os.path.join('sage_worksheet','worksheet_conf.pickle')) os.unlink(tmp) worksheet_html = self._abspath(self._worksheet_html_filename(username, id_number)) T.add(worksheet_html, os.path.join('sage_worksheet','worksheet.html')) # The following is purely for backwards compatibility with old # notebook servers prior to sage-4.1.2. fd, worksheet_txt = tempfile.mkstemp() old_heading = "%s\nsystem:%s\n"%(basic['name'], basic['system']) with open(worksheet_txt,'w') as f: with open(worksheet_html) as g: f.write(old_heading + g.read()) T.add(worksheet_txt, os.path.join('sage_worksheet','worksheet.txt')) os.unlink(worksheet_txt) # important, so we don't leave an open file handle! os.fdopen(fd,'w').close() # end backwards compat block. # Add the contents of the DATA directory path = self._abspath(self._worksheet_pathname(username, id_number)) data = os.path.join(path, 'data') if os.path.exists(data): for X in os.listdir(data): T.add(os.path.join(data, X), os.path.join('sage_worksheet','data',X)) # Add the contents of each of the cell directories. cells = os.path.join(path, 'cells') if os.path.exists(cells): for X in os.listdir(cells): T.add(os.path.join(cells, X), os.path.join('sage_worksheet','cells',X)) # NOTE: We do not export the snapshot/undo data. People # frequently *complain* about Sage exporting a record of their # mistakes anyways. T.close()
def export_worksheet(self, username, id_number, filename, title): """ Export the worksheet with given username and id_number to the given filename (e.g., 'worksheet.sws'). INPUT: - ``title`` - title to use for the exported worksheet (if None, just use current title) """ T = tarfile.open(filename, 'w:bz2') worksheet = self.load_worksheet(username, id_number) basic = copy.deepcopy(self._worksheet_to_basic(worksheet)) if title: # change the title basic['name'] = title basic['name'] = encoded_str(basic['name']) # Remove metainformation that perhaps shouldn't be distributed for k in [ 'owner', 'ratings', 'worksheet_that_was_published', 'viewers', 'tags', 'published_id_number', 'collaborators', 'auto_publish' ]: if k in basic: del basic[k] self._save(basic, self._worksheet_conf_filename(username, id_number) + '2') tmp = self._abspath( self._worksheet_conf_filename(username, id_number) + '2') T.add(tmp, os.path.join('sage_worksheet', 'worksheet_conf.pickle')) os.unlink(tmp) worksheet_html = self._abspath( self._worksheet_html_filename(username, id_number)) T.add(worksheet_html, os.path.join('sage_worksheet', 'worksheet.html')) # The following is purely for backwards compatibility with old # notebook servers prior to sage-4.1.2. fd, worksheet_txt = tempfile.mkstemp() old_heading = "%s\nsystem:%s\n" % (basic['name'], basic['system']) with open(worksheet_txt, 'w') as f: with open(worksheet_html) as g: f.write(old_heading + g.read()) T.add(worksheet_txt, os.path.join('sage_worksheet', 'worksheet.txt')) os.unlink(worksheet_txt) # important, so we don't leave an open file handle! os.close(fd) # end backwards compat block. # Add the contents of the DATA directory path = self._abspath(self._worksheet_pathname(username, id_number)) data = os.path.join(path, 'data') if os.path.exists(data): for X in os.listdir(data): T.add(os.path.join(data, X), os.path.join('sage_worksheet', 'data', X)) # Add the contents of each of the cell directories. cells = os.path.join(path, 'cells') if os.path.exists(cells): for X in os.listdir(cells): T.add(os.path.join(cells, X), os.path.join('sage_worksheet', 'cells', X)) # NOTE: We do not export the snapshot/undo data. People # frequently *complain* about Sage exporting a record of their # mistakes anyways. T.close()