def extract_from_nimbus_file_embed( div_tag, processing_options: NimbusProcessingOptions): """Extra file link from nimbus html including it's original file name""" if div_tag.name != 'div': return a_tag = div_tag.find('a') if not a_tag: return link_href = a_tag.get('href', '') try: # use try except as 99.9% of time will be there.. probably.. anyway using ask for forgiveness approach filename_part = a_tag.find('span', class_='file-name-main').text except AttributeError: # if the file name parts are not in html use the parts of the href filename # it is the name of the file in the zip file filename_part = f"{Path(link_href).stem}." try: extension_part = a_tag.find('span', class_='file-name-ext').text except AttributeError: extension_part = str(Path(link_href).suffix).lstrip('.') dirty_filename = f"{filename_part}{extension_part}" # if filename parts were mot present, and so dirty filename is '' the clean file name will be a random string clean_filename = helper_functions.generate_clean_filename( dirty_filename, processing_options.filename_options) caption_text = get_caption_text(div_tag, processing_options) return FileEmbedNimbusHTML(processing_options, caption_text, link_href, clean_filename)
def get_clean_file_name(self): target_suffix = file_mover.get_file_suffix_for( self.processing_options.export_format) dirty_name = f'{self.contents}{target_suffix}' clean_filename = helper_functions.generate_clean_filename( dirty_name, self.processing_options.filename_options) return clean_filename
def test_generate_clean_filename(value, filename_options, expected, tmp_path): result = helper_functions.generate_clean_filename(value, filename_options) assert result == expected Path(tmp_path, result).touch() assert Path(tmp_path, result).exists()
def create_file_name(self): self._file_name = Path(helper_functions.generate_clean_filename(self._name, self._conversion_settings.filename_options)) if not Path(self._name).suffix: self.logger.warning(f'Original attachment name "{self._name}" has no file extension') self.add_suffix_if_possible() if not self._name == str(self._file_name): self.logger.info( f'Original attachment name was "{self._name}" the cleaned name used is "{self._file_name}"')
def _create_file_name(self, used_filenames): dirty_filename = self._title if self.conversion_settings.creation_time_in_exported_file_name: formatted_ctime = self.format_ctime_for_file_name() dirty_filename = f"{formatted_ctime}-{self._title}" dirty_filename = self._append_file_extension(dirty_filename) cleaned_filename = Path( helper_functions.generate_clean_filename( dirty_filename, self.conversion_settings.filename_options)) new_filename = cleaned_filename n = 0 while new_filename in used_filenames: n += 1 new_filename = Path( f'{Path(cleaned_filename).stem}-{n}{Path(cleaned_filename).suffix}' ) self._file_name = new_filename self.logger.info( f'For the note "{self._title}" the file name used is "{self._file_name}"' )
def initialise_new_note(zip_file, conversion_settings, processing_options: NimbusProcessingOptions): new_note = NimbusNote(processing_options, contents=[], conversion_settings=conversion_settings) new_note.title = zip_file.stem.replace('_', ' ') new_note.note_paths.path_to_note_source = Path(zip_file.parent) new_note.note_paths.path_to_source_folder = conversion_settings.source_absolute_root dirty_workspace_folder_name = Path(zip_file.parent).relative_to( conversion_settings.source_absolute_root).parts[0] new_note.note_paths.path_to_source_workspace = Path( conversion_settings.source_absolute_root, dirty_workspace_folder_name) new_note.note_paths.path_to_target_folder = conversion_settings.export_folder_absolute new_note.note_paths.note_target_suffix = get_file_suffix_for( conversion_settings.export_format) new_note.note_paths.note_source_file_name = zip_file.name clean_file_name = helper_functions.generate_clean_filename( new_note.title, processing_options.filename_options) new_note.note_paths.note_target_file_name = Path( f'{clean_file_name}' f'{new_note.note_paths.note_target_suffix}') new_note.note_paths.set_note_target_path(processing_options) new_note.note_paths.set_path_to_attachment_folder( conversion_settings.attachment_folder_name, processing_options) return new_note
def test_generate_clean_filename_force_windows_long_name(string_to_test, filename_options, expected): result = helper_functions.generate_clean_filename(string_to_test, filename_options) assert result == expected
def create_file_name(self): self._file_name = Path(helper_functions.generate_clean_filename(self._attachment_id, self._conversion_settings.filename_options))