def store_document(export_format: str, tracker: Tracker, dbx: dropbox.Dropbox, document_id: str): """ Download a document to the local file system. :param export_format: The format to store the document in. Possible values are "html", "markdown" or "all". For "html" also referenced images are downloaded. :param tracker: Instance of the tracker to create new files with. :param dbx: Dropbox instance with logged in account with sufficient permissions to download documents. :param document_id: The document id if the paper document to download. """ try: folders = [ folder.name.replace('/', '+') for folder in dbx.paper_docs_get_folder_info(document_id).folders or [] ] except dropbox.exceptions.ApiError: folders = [] try: if export_format == 'html' or export_format == 'all': document_meta, document_body = dbx.paper_docs_download( document_id, dropbox.paper.ExportFormat('html')) document_reference = "%s-%s" % (document_id, document_meta.revision) content = replace_images(tracker, folders, document_reference, document_body.content) file_name = "%s [%s].html" % (document_meta.title.replace( '/', '+'), document_reference) with tracker.file_handler(folders, file_name) as fd: if fd: fd.write(content) if export_format == 'markdown' or export_format == 'all': document_meta, document_body = dbx.paper_docs_download( document_id, dropbox.paper.ExportFormat('markdown')) document_reference = "%s-%s" % (document_id, document_meta.revision) content = document_body.content file_name = "%s [%s].md" % (document_meta.title.replace( '/', '+'), document_reference) with tracker.file_handler(folders, file_name) as fd: if fd: fd.write(content) except dropbox.exceptions.ApiError as e: logging.exception('dropbox api error for document %s: %s', document_id, e)