def __save_settings_data(self): if not os.path.exists(os.path.dirname(self.__settings_data_path)): plPath(os.path.dirname(self.__settings_data_path)).mkdir(parents=True, exist_ok=True) try: with (open(self.__settings_data_path, 'w')) as datafile: json.dump(self.__settings_data, datafile) except FileNotFoundError: print('Something went horribly wrong while trying to save your current user data.')
def _parse(self, base_path: typing.Union[str, plPath], metadata: typing.Optional[dict] = None) -> dict: """ Find a path by first replacing `{format_strings}` with variables from the passed metadata dict and then globbing over any `'*'` This class ensures a single path is returned, and raises an :class:`.AmbiguityError` otherwise. To return multiple paths, use :class:`.Globs` Parameters ---------- base_path : metadata : Returns ------- """ # replace format string try: format_str = self.format.format(**metadata) except KeyError as e: # reraise error with additional informative message about what else to use raise type( e )(str(e) + '\nField not found in metadata, did you add it with `add_metadata`?' ).with_traceback(sys.exc_info()[2]) # add to base_path full_path = plPath(base_path).absolute() / format_str # glob us some matching files if it's got an asterisk if '*' in str(full_path): paths = glob.glob(str(full_path)) if self.only_dirs: paths = [path for path in paths if plPath(path).is_dir()] if len(paths) > 1: raise AmbiguityError( f'Multiple paths matched glob string: {str(full_path)},\nif this was intentional, use Globs instead!' ) elif len(paths) < 0: raise FileNotFoundError( f'No file was found matching query string {str(full_path)}' ) path = paths[0] else: path = str(full_path) return {self.key: path}
def downloadAllFiles(self): """ Downloads all files inside the instance's files list. """ # Scan all files self.scanCourses() # Check the file paths paths = list(set([os.path.join(self.params['download_path'],f['path']) for f in self.files])) for p in paths: if not plPath(p).exists(): plPath(p).mkdir(parents=True, exist_ok=True) # Download all files for r in ThreadPool(self.params['num_download_threads']).imap_unordered(self.downloadFile, self.files): pass
def _parse_dir(self, base_path: typing.Union[str, plPath]) -> list: """ First part of :meth:`.Path._parse` , given a base directory and parser, return a list of dicts of matching keys found. """ # make absolute base_path = plPath(base_path).absolute() # globify format string to find all matching files format_glob = re.sub(r'\{.*?\}', '*', self.format) # find matching files relative to the base_path matching_files = base_path.glob(format_glob) # parse results results = [] for match in matching_files: # make relative to base_path to match format match = match.relative_to(base_path) parsed = self.parser.parse(str(match)) # parser returns None if no matches if parsed is not None: results.append(parsed.named) if len(results) == 0: raise ValueError( f'No matches were found between \n(relative) format:\n{self.format}\nglob string:{format_glob}\nin\n{base_path}' ) return results
def copy_catalog_html_resource(output_folder_path): try: css_output_file_name = plPath(environment_config_local['output_catalog_html_resource']['css_file_name']) css_data_file_path = plPath(environment_config_local['data_catalog_html_resource']['css_file_full_path']) css_output_file_path = output_folder_path.joinpath(css_output_file_name) copyfile(css_data_file_path, css_output_file_path) js_output_file_name = plPath(environment_config_local['output_catalog_html_resource']['js_file_name']) js_data_file_path = plPath(environment_config_local['data_catalog_html_resource']['js_file_full_path']) js_output_file_path = output_folder_path.joinpath(js_output_file_name) copyfile(js_data_file_path, js_output_file_path) except: error_message = message_config_local['err']['failed_to_copy_file'] crPrintCyan(error_message) raise CrEnvironmentError(error_message)
def get_catalog_chm_file_full_path(): global magic_value_config_local global message_config_local # Result placeholder catalog_file_full_path = None try: # Glob all potential catalog file # Call it a list, but see python generator for more information catalog_file_list = plPath(crDevInput.unpackedChmFolder) \ .glob(magic_value_config_local['chm']['catalog_file_search_pattern']) # Expect only one catalog file catalog_file_list_count = 0 # Explore the glob generator for catalog_file_glob_result in catalog_file_list: # Increase file count on every loop catalog_file_list_count = catalog_file_list_count + 1 # Prevent more than one catalog file if catalog_file_list_count > 1: crPrintCyan( message_config_local['err']['multiple_catalog_file']) raise CrNotImplementedError( message_config_local['err']['multiple_catalog_file']) # Everything is fine, copy it as plPath catalog_file_full_path = plPath(catalog_file_glob_result) # After explored the generator if catalog_file_list_count is 0: # Nothing found, raise error error_message = message_config_local['err'][ 'catalog_file_not_found'] crPrintCyan(error_message) raise CrFileNotFoundError(error_message) except: raise return catalog_file_full_path
def _load_environment_config(): global environment_config environment_config_path = plPath('environment.ini') if osPath.exists(environment_config_path) is False: # Message config not yet loaded error_message = "Config file missing: " + str(environment_config_path) crPrintCyan(error_message) raise CrFileNotFoundError(error_message) environment_config.read(environment_config_path)
def _load_message_config(): global message_config message_config_path = plPath( environment_config['message']['config_message_path']) if osPath.exists(message_config_path) is False: # Message config not yet loaded error_message = "Config file missing: " + str(message_config_path) crPrintCyan(error_message) raise CrFileNotFoundError(error_message) message_config.read(message_config_path, encoding='utf-8')
def _load_magic_value_config(): global magic_value_config global message_config magic_value_config_path = plPath( environment_config['dev']['magic_value_config_path']) if osPath.exists(magic_value_config_path) is False: error_message = message_config['err']['software_broken'] \ + str(magic_value_config_path) crPrintCyan(error_message) raise CrFileNotFoundError(error_message) magic_value_config.read(magic_value_config_path)
def myLocaleParser(): global configEnvironment # Check system language languageRoot = configEnvironment['language']['root'] systemLanguage = locale.getdefaultlocale()[0] languageCurrent = languageRoot + systemLanguage + '/' # language/en_US/ currentLanguageExists = osPath.exists(plPath(languageCurrent)) if currentLanguageExists is True: languageCurrent = configEnvironment['language']['fallback'] # # Apply language to path configPathMessage = configEnvironment['message']['configPathMessage'] configPathMessage = languageCurrent + \ configPathMessage # language/en_US/message.xml configEnvironment['message']['configPathMessage'] = configPathMessage
def _my_locale_parser(config_environment): # Get current system language language_root = config_environment['language']['root_path'] system_language = get_system_language() current_language = language_root + system_language + '/' # language/en_US/ # Confirm the language files are exists. # If not, use default fallback language current_language_exists = osPath.exists(plPath(current_language)) if current_language_exists is False: current_language = config_environment['language']['fallback_path'] # Apply language to path config_message_path = config_environment['message']['message_config_path'] config_message_path = current_language + config_message_path # language/en_US/message.xml # Put it back config_environment['message']['config_message_path'] = config_message_path return config_environment
def get_root_output_folder_full_path(): output_folder = plPath(crDevInput.outputFolder) crCreateFolder(output_folder) return output_folder
def get_catalog_html_resource_output_full_path(root_output_folder): output_catalog_html_root_path = plPath(environment_config_local['output_catalog_html_resource']['root_path']) output_catalog_html_root_full_path = root_output_folder.joinpath(output_catalog_html_root_path) return output_catalog_html_root_full_path
def pythonConfigParser(): global configEnvironment configEnvironmentPath = plPath(R'./environment.ini') configEnvironment.read(configEnvironmentPath)