def _write_config_file(self):
     ini_path = Path(self.conversion_settings.working_directory, 'data', self._config_file)
     try:
         with open(ini_path, 'w') as config_file:
             self.write(config_file)
             self.logger.info("Saving configuration file")
     except FileNotFoundError as e:
         if not Path(ini_path.parent).is_dir():
             message = f"Unable to save config.ini file '{ini_path.parent}' " \
                       f"is not a directory. {e.strerror}"
             self.logger.error(message)
             self.logger.error(helper_functions.log_traceback(e))
             if not config.yanom_globals.is_silent:
                 print(message)
         else:
             message = f"Unable to save config.ini file " \
                       f"- '{ini_path}' " \
                       f"- {e.strerror}"
             self.logger.error(message)
             self.logger.error(helper_functions.log_traceback(e))
             if not config.yanom_globals.is_silent:
                 print(message)
     except IOError as e:
         message = f"Unable to save config.ini file `{ini_path.parent}`.\n{e}"
         self.logger.error(message)
         self.logger.error(helper_functions.log_traceback(e))
         if not config.yanom_globals.is_silent:
             print(message)
    def create_attachment_folder(self):
        if self.full_path_to_notebook:  # if full path is still None then the folder was not created and we can skip
            self.logger.debug(f"Creating attachment folder")
            try:
                Path(self.full_path_to_notebook,
                     self.conversion_settings.attachment_folder_name).mkdir()
            except FileNotFoundError as e:
                msg = f'Unable to create attachment folder there is a problem with the path.\n{e}'
                if helper_functions.are_windows_long_paths_disabled():
                    msg = f"{msg}\n Windows long path names are not enabled check path length"
                self.logger.error(f'{msg}')
                self.logger.error(helper_functions.log_traceback(e))
                if not config.yanom_globals.is_silent:
                    print(f'{msg}')
            except OSError as e:
                msg = f'Unable to create attachment folder\n{e}'
                self.logger.error(f'{msg}')
                self.logger.error(helper_functions.log_traceback(e))
                if not config.yanom_globals.is_silent:
                    print(f'{msg}')

            return

        self.logger.warning(
            f"Attachment folder for '{self.title}' "
            f"was not created as the notebook folder has not been created")
    def create_notebook_folder(self, parents=True):
        self.logger.debug(f"Creating notebook folder for {self.title}")

        n = 0
        target_path = Path(self.conversion_settings.working_directory,
                           config.yanom_globals.data_dir,
                           self.nsx_file.conversion_settings.export_folder,
                           self.folder_name)

        while target_path.exists():
            n += 1
            target_path = Path(self.conversion_settings.working_directory,
                               config.yanom_globals.data_dir,
                               self.nsx_file.conversion_settings.export_folder,
                               f"{self.folder_name}-{n}")
        try:
            target_path.mkdir(parents=parents, exist_ok=False)
            self.folder_name = Path(target_path.name)
            self._full_path_to_notebook = target_path
        except FileNotFoundError as e:
            msg = f'Unable to create notebook folder there is a problem with the path.\n{e}'
            if helper_functions.are_windows_long_paths_disabled():
                msg = f"{msg}\n Windows long path names are not enabled check path length"
            self.logger.error(f'{msg}')
            self.logger.error(helper_functions.log_traceback(e))
            if not config.yanom_globals.is_silent:
                print(f'{msg}')
        except OSError as e:
            msg = f'Unable to create note book folder\n{e}'
            self.logger.error(f'{msg}')
            self.logger.error(helper_functions.log_traceback(e))
            if not config.yanom_globals.is_silent:
                print(f'{msg}')
Exemple #4
0
def _error_handling(e, target_filename, zip_filename, message=''):
    """Error handling for errors encountered reading form zip files"""

    traceback_text = helper_functions.log_traceback(e)
    msg = f'Error - {e}'

    if isinstance(e, FileNotFoundError):
        msg = f'Error - unable to read zip file "{zip_filename}"'
        logger.error(msg)
        logger.error(traceback_text)
        if not config.yanom_globals.is_silent:
            print(msg)
        sys.exit(1)  # TODO need a more graceful handling than this

    if isinstance(e, KeyError):
        msg = f'Warning - For the note "{message}" ' \
              f'- unable to find the file "{target_filename}" in the zip file "{zip_filename}"'

    if isinstance(e, ValueError):
        msg = "Warning Value Error accessing zip file contents, possibly treating file as directory or vice versa."

    logger.warning(msg)
    logger.warning(traceback_text)
    if not config.yanom_globals.is_silent:
        print(msg)
Exemple #5
0
def error_handling(e, write_type):
    if isinstance(e, FileNotFoundError):
        logger.error(f"Attempting to write {write_type} to invalid path - {e}")
        if helper_functions.are_windows_long_paths_disabled():
            logger.error(
                "Windows long file paths are not enabled check path length is not too long."
            )
        logger.error(helper_functions.log_traceback(e))
        return

    if isinstance(e, IsADirectoryError):
        logger.error(
            f"Attempting to write {write_type} to an existing directory name not a file - {e}"
        )
        logger.error(helper_functions.log_traceback(e))
        return

    if isinstance(e, IOError):
        logger.error(f"Attempting to write {write_type} failed - {e}")
        logger.error(helper_functions.log_traceback(e))
    def find_pandoc_version(self):
        try:
            self._pandoc_version = subprocess.run([self._pandoc_path, '-v'], capture_output=True, text=True, timeout=3)
            self._pandoc_version = self._pandoc_version.stdout[7:].split('\n', 1)[0].strip()
            if not config.yanom_globals.is_silent:
                print('Found pandoc ' + str(self._pandoc_version))
            self.logger.debug(f"Found pandoc version {str(self._pandoc_version)} at {self._pandoc_path}")

        except subprocess.CalledProcessError as e:
            self.logger.error(f"Exiting as unable to get pandoc version")
            self.logger.error(helper_functions.log_traceback(e))
            if not config.yanom_globals.is_silent:
                print("Unable to fetch pandoc version please check log files for additional information.")
                print("Exiting.")
            sys.exit(1)
    def convert_using_strings(self, input_data, note_title):
        try:
            out = subprocess.run(self.pandoc_options, input=input_data, capture_output=True,
                                 encoding='utf-8', text=True, timeout=20)
            if out.returncode > 0:
                self.logger.error(f"Pandoc Return code={out.returncode}, error={out.stderr}")
            return out.stdout

        except subprocess.CalledProcessError as e:
            self.logger.error(f'Unable to convert note "{note_title}".')
            self.logger.error(helper_functions.log_traceback(e))
            if not config.yanom_globals.is_silent:
                print(f"Error converting note {note_title} with pandoc please check log file and pandoc installation.")
                print("Attempting to continue...")

        return 'Error converting data'
Exemple #8
0
 def _report_create_export_folder_errors(self, msg, e):
     self.logger.error(f'{msg}')
     self.logger.error(helper_functions.log_traceback(e))
     if not config.yanom_globals.is_silent:
         print(f'{msg}')