Exemplo n.º 1
0
 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())
Exemplo n.º 2
0
    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')
Exemplo n.º 3
0
    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")
Exemplo n.º 4
0
 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?')
Exemplo n.º 5
0
 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')
Exemplo n.º 6
0
 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?')
Exemplo n.º 7
0
 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
Exemplo n.º 8
0
 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)