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.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() |