def load(self, file_or_path): """Load a JSON file and return an AST instance.""" # logger.debug("Load JSON file `%s`.", path) with _get_file(file_or_path, 'r') as f: d = json.load(f) assert isinstance(d, list) ast = ast_from_pandoc(d) assert isinstance(ast, ASTNode) return ast
def dump(self, text, file_or_path, context=None): """Dump string to a Markdown file.""" with _get_file(file_or_path, 'w') as f: path = op.realpath(f.name) f.write(text) f.write('\n') # Save the resources. if (context or {}).get('resources', {}): _save_resources(context.get('resources', {}), _get_resources_path(path))
def load(self, file_or_path): """Load a JSON file and return an AST instance.""" # logger.debug("Load JSON file `%s`.", path) with _get_file(file_or_path, 'r') as f: # Get the path to the JSON file. # path = op.realpath(f.name) d = json.load(f) assert isinstance(d, dict) ast = ast_from_pandoc(d) assert isinstance(ast, ASTNode) return ast
def dump(self, ast, file_or_path): """Dump an AST instance to a JSON file.""" assert isinstance(ast, ASTNode) d = ast.to_pandoc() assert isinstance(d, list) # logger.debug("Save JSON file `%s`.", path) with _get_file(file_or_path, 'w') as f: json.dump(d, f, sort_keys=True, indent=2, separators=(',', ': ')) # avoid trailing whitespaces # Add a new line at the end. f.write('\n')
def dump(self, ast, file_or_path, context=None): """Dump an AST instance to a JSON file.""" assert isinstance(ast, ASTNode) d = ast.to_pandoc() assert isinstance(d, dict) # logger.debug("Save JSON file `%s`.", path) with _get_file(file_or_path, 'w') as f: path = op.realpath(f.name) json.dump(d, f, sort_keys=True, indent=2, separators=(',', ': ')) # avoid trailing whitespaces # Add a new line at the end. f.write('\n') # Save the resources. if (context or {}).get('resources', {}): _save_resources(context.get('resources', {}), _get_resources_path(path))
def dump(self, nb, file_or_path): with _get_file(file_or_path, 'w') as f: nbformat.write(nb, f, _NBFORMAT_VERSION)
def load(self, file_or_path): with _get_file(file_or_path, 'r') as f: return nbformat.read(f, _NBFORMAT_VERSION)
def load(self, file_or_path): """Load a Markdown file and return a string.""" with _get_file(file_or_path, 'r') as f: text = f.read() return text
def load(self, file_or_path): with _get_file(file_or_path, 'r') as f: nb = nbformat.read(f, _NBFORMAT_VERSION) return nb