def __act_according_to_mode(self, profilepath, mode): all_profiles = os.listdir(Pathfinder.get_profile_directory()) if not mode and self.__check_if_profile_exists(profilepath): click.echo('Not installing profile: ' + os.path.splitext(profilepath)[0]) click.echo('to do so anyway, exectute again with option --force') pass else: click.echo('Installing profile: ' + os.path.splitext(profilepath)[0]) shutil.copy(profilepath, Pathfinder.get_profile_directory())
def setup_logging(func): if os.path.exists(os.path.join(Pathfinder.get_logger_directory_path())): logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename=os.path.join( Pathfinder.get_logger_directory_path(), 'debug.log')) return func
def edit_profile(self, profilename: str): if profilename != 'all': executable_editor = YMLConfigReader.get_profile_editor_name() profiledir = Pathfinder.get_profile_directory() pre_edit_profile = self.__safe_profile_before_edit(profilename) if os.path.exists(os.path.join(profiledir, profilename + '.yml')): process = subprocess.run([ executable_editor, os.path.join(profiledir, os.path.splitext(profilename)[0] + '.yml') ], check=True) if not process.check_returncode(): if self.__validate_profile_after_edit(profilename): click.echo(Fore.LIGHTMAGENTA_EX + 'Edit of profile ' + Fore.GREEN + profilename + Fore.LIGHTMAGENTA_EX + ' complete') else: click.echo( Fore.LIGHTRED_EX + 'Edit of profile ' + profilename + ' invalid. Your changes invalidated the profile.' ' Changeling will now reset the profile to its' ' previous state!') self.__revert_to_previous(profilename, pre_edit_profile) else: click.echo( Fore.LIGHTRED_EX + 'This profile does not exist. Please provide an existing profilename' ) else: click.echo(Fore.LIGHTRED_EX + 'You can\'t edit the "all" profile. It is restricted')
def list_profiles(self): profiles = [ os.path.splitext(profilename)[0] for profilename in os.listdir(Pathfinder.get_profile_directory()) if os.path.splitext(profilename)[0] != 'all' ] return print(*profiles, sep="\n")
def __create_profile_manager_folderstructure(self): try: os.mkdir( os.path.join( Pathfinder.get_wonderdraft_userfolder(), YMLConfigReader.get_changeling_manager_directory_name())) except OSError as exception: logging.getLogger('debug').exception( 'You can\'t create the profile manager folder twice')
def __create_deactivated_folder_generic(self, path_splitter, name): try: os.mkdir( os.path.join( Pathfinder.get_wonderdraft_userfolder(), YMLConfigReader.get_changeling_manager_directory_name(), path_splitter)) except OSError as exception: logging.getLogger('debug').exception( 'You can\'t create the inactive ' + name + ' folder twice')
def __safe_profile_before_edit(self, profilename) -> dict: with open( os.path.join(Pathfinder.get_profile_directory(), os.path.splitext(profilename)[0] + '.yml')) as profile: try: return yaml.safe_load(profile) except yaml.YAMLError as exception: logging.getLogger('debug')\ .exception('Could not load profile '+profilename+'before edit. Does it exist?')
def show_profile(self, profile): try: with open( os.path.join(Pathfinder.get_profile_directory(), profile + '.yml')) as profile: return profile.read() except FileNotFoundError as exception: click.echo('This profile is not installed into changeling') logging.getLogger( 'debug').exception('Could not show profile ' + profile + ' because it is not installed')
def __validate_profile_after_edit(self, profilename) -> bool: with open( os.path.join(Pathfinder.get_profile_directory(), os.path.splitext(profilename)[0] + '.yml')) as profile: try: loaded_profile = yaml.safe_load(profile) return YMLConfigValidator.validate_profile(loaded_profile) except yaml.YAMLError as exception: logging.getLogger('debug') \ .exception('Could not load profile ' + profilename + 'after edit. Did you delete it?')
def __sanity_check_inactive_themes_dir(self): return os.path.exists( os.path.join( Pathfinder.get_wonderdraft_userfolder(), YMLConfigReader.get_changeling_manager_directory_name(), YMLConfigReader.get_deactivated_themes_folder_name()))
def __sanity_check_main_dir(self): return os.path.exists( os.path.join( Pathfinder.get_wonderdraft_userfolder(), YMLConfigReader.get_changeling_manager_directory_name()))
def __check_if_profile_exists(self, profilepath): all_profiles = os.listdir(Pathfinder.get_profile_directory()) if os.path.basename(profilepath) in all_profiles: return True return False
def __revert_to_previous(self, profilename, data): with open( os.path.join(Pathfinder.get_profile_directory(), os.path.splitext(profilename)[0] + '.yml'), 'w') as to_revert: yaml.dump(data, to_revert, default_flow_style=False)