def insert_cell(target_nb, source, cell_id): source_nb = get_notebook(source) for cell in source_nb['notebook']: if cell['id'] == cell_id: new_cell = prepare_cell(cell, locate_highest(target_nb, cell['type'])+1) target_nb['notebook'].insert(0, new_cell) break return new_cell['id']
def inspect_file(fn, string): content = get_notebook(fn) for element in content: for cell_name in element['content']: cell = element['content'][cell_name] if 'props' in cell and 'searchable' in cell['props'].split(';'): if cell['content'].find(string) > -1: return fn return False
def parse_bibliography(self, fn): data = get_notebook(fn, new_note=self.resource.new_bibliography) header_str = '<tr><th>#</th>' header_str += ''.join(['<th>%s</th>'%(elem.title()) for elem in self.resource.bibliography_nothon_header]) header_str += '</tr>' note = {'table_header' : header_str} note['full_bibliography'] = simplejson.dumps(data) note['extra_data'] = simplejson.dumps({'separator': os.sep, 'folder': notebook_folder(fn)}) return note
def remove_cell_handler(message, resource): target_nb = get_notebook(message['target']) addresses = message['addresses'] for address in addresses: cell = address.split('#')[1] remove_cell(target_nb, cell) with open(message['target'], 'w') as fout: fout.write(print_notebook(target_nb, resource.notebook_item_order)) return simplejson.dumps({'success' : 'success'})
def new_notebook(self, fn, aux=False): " Creates an empty notebook on disc " if os.path.exists(fn): return aux create_notebook_folder(fn) _save_notebook(fn, self.resource.new_notebook) if aux: if aux.get('type') in ('calendar'): nb = get_notebook(fn) nb['_metadata']['raw_date'] = aux.get('raw_date', '') _save_notebook(fn, nb) new_notebook(fn, self.resource)
def paste_cell_handler(message, resource): target_nb = get_notebook(message['target']) addresses = message['addresses'] addresses.reverse() labels = message['labels'] labels.reverse() new_cells = [] for address in addresses: source = address.split('#')[0] cell = address.split('#')[1] new_cells.append(insert_cell(target_nb, source, cell)) write_notebook(message['target'], target_nb, resource.notebook_item_order) return simplejson.dumps({'target' : message['target'], 'id' : message['id'], 'links' : zip(labels, new_cells)})
def insert_new_entry(fn, entry, resource): try: data = get_notebook(fn) bibliography = data.get('bibliography') except EnvironmentError: return 'Could not write file %s'%(fn) if bibliography: keys = bibliography.keys() keys.sort(key=int) new_key = int(keys[-1]) + 1 if entry: bibliography['%s'%new_key] = entry data['date'] = datetime.datetime.now().strftime(resource.time_format) data['bibliography'] = bibliography return write_notebook(fn, data, resource.bibliography_item_order)
def parse_note(self, fn): print 'Reading file %s %s'%(fn, datetime.datetime.now().strftime("%H:%M:%S.%f")) data = get_notebook(fn) notebook = data.get('notebook') _metadata = data.get('_metadata') directory = _metadata.get('directory') for element in notebook: elem_type = element.get('type') if elem_type in ('plot', 'head', 'code', 'text', 'paragraph', 'section'): exec('obj = %s(self.resource)'%(elem_type.title())) element = obj.render(element, directory, self.render) else: pass note = {'full_notebook': simplejson.dumps({'_metadata': data['_metadata'], 'notebook': notebook})} print 'Read file %s %s'%(fn, datetime.datetime.now().strftime("%H:%M:%S.%f")) return note