Пример #1
0
 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
Пример #2
0
 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))
Пример #3
0
 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
Пример #4
0
 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')
Пример #5
0
 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))
Пример #6
0
 def dump(self, nb, file_or_path):
     with _get_file(file_or_path, 'w') as f:
         nbformat.write(nb, f, _NBFORMAT_VERSION)
Пример #7
0
 def load(self, file_or_path):
     with _get_file(file_or_path, 'r') as f:
         return nbformat.read(f, _NBFORMAT_VERSION)
Пример #8
0
 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
Пример #9
0
 def dump(self, nb, file_or_path):
     with _get_file(file_or_path, 'w') as f:
         nbformat.write(nb, f, _NBFORMAT_VERSION)
Пример #10
0
 def load(self, file_or_path):
     with _get_file(file_or_path, 'r') as f:
         nb = nbformat.read(f, _NBFORMAT_VERSION)
     return nb