diff --git a/README.md b/README.md index 851bb15..47cedb9 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,9 @@ python3 main.py profile enable -i 4 # Enable (ignore state conflicts and/or potential security issues). python3 main.py profile enable -i 4 -f + +# Enable (delete any existing profile data on beforehand). +python3 main.py profile enable -i 4 -p ``` #### Disable Profile diff --git a/core/controllers/ProfileController.py b/core/controllers/ProfileController.py index 8c9a3d0..0c46b6d 100644 --- a/core/controllers/ProfileController.py +++ b/core/controllers/ProfileController.py @@ -34,7 +34,7 @@ class ProfileController: profile_observer.notify('created', profile) @staticmethod - def enable(profile: Union[SessionProfile, SystemProfile], force: bool = False, profile_observer: ProfileObserver = None, application_version_observer: ApplicationVersionObserver = None, connection_observer: ConnectionObserver = None): + def enable(profile: Union[SessionProfile, SystemProfile], force: bool = False, pristine: bool = False, profile_observer: ProfileObserver = None, application_version_observer: ApplicationVersionObserver = None, connection_observer: ConnectionObserver = None): from core.controllers.ConnectionController import ConnectionController @@ -45,6 +45,9 @@ class ProfileController: else: ProfileController.disable(profile) + if pristine: + profile.delete_data() + if profile.is_session_profile(): application_version = profile.application_version @@ -114,7 +117,7 @@ class ProfileController: def destroy(profile: Union[SessionProfile, SystemProfile], profile_observer: ProfileObserver = None): ProfileController.disable(profile) - Profile.delete(profile) + profile.delete() if profile_observer is not None: profile_observer.notify('destroyed', profile) diff --git a/core/models/BaseProfile.py b/core/models/BaseProfile.py index cd95375..a0bbc3d 100644 --- a/core/models/BaseProfile.py +++ b/core/models/BaseProfile.py @@ -48,6 +48,14 @@ class BaseProfile: def is_system_profile(self): return type(self).__name__ == 'SystemProfile' + def delete_data(self): + shutil.rmtree(BaseProfile.__get_data_path(self.id)) + + def delete(self): + + shutil.rmtree(BaseProfile.__get_config_path(self.id)) + shutil.rmtree(BaseProfile.__get_data_path(self.id)) + @staticmethod def find_by_id(id: int): @@ -134,12 +142,6 @@ class BaseProfile: text_io_wrapper = open(BaseProfile.__get_config_path(profile.id) + '/config.json', 'w') text_io_wrapper.write(config_file_contents) - @staticmethod - def delete(profile): - - shutil.rmtree(BaseProfile.__get_config_path(profile.id)) - shutil.rmtree(BaseProfile.__get_data_path(profile.id)) - @staticmethod def __get_config_path(id: int): return Constants.SP_PROFILE_CONFIG_HOME + '/' + str(id) diff --git a/main.py b/main.py index 4b7e800..c1f1db4 100644 --- a/main.py +++ b/main.py @@ -46,6 +46,9 @@ if __name__ == '__main__': profile_observer.subscribe('disabled', lambda event: pprint.pp((event.subject, 'Disabled')) if event.meta.get('explicitly') else None) profile_observer.subscribe('enabled', lambda event: pprint.pp((event.subject, 'Enabled'))) + pristine_parser = argparse.ArgumentParser(add_help=False) + pristine_parser.add_argument('--pristine', '-p', action='store_true') + safe_parser = argparse.ArgumentParser(add_help=False) safe_parser.add_argument('--force', '-f', action='store_true') @@ -83,7 +86,7 @@ if __name__ == '__main__': profile_subparsers.add_parser('destroy', parents=[profile_base_parser, safe_parser]) - profile_subparsers.add_parser('enable', parents=[profile_base_parser, safe_parser]) + profile_subparsers.add_parser('enable', parents=[profile_base_parser, pristine_parser, safe_parser]) profile_subparsers.add_parser('disable', parents=[profile_base_parser, safe_parser]) get_parser = main_subparsers.add_parser('get') @@ -160,7 +163,7 @@ if __name__ == '__main__': if profile is not None: try: - ProfileController.enable(profile, arguments.force, profile_observer=profile_observer, application_version_observer=application_version_observer, connection_observer=connection_observer) + ProfileController.enable(profile, force=arguments.force, pristine=arguments.pristine, profile_observer=profile_observer, application_version_observer=application_version_observer, connection_observer=connection_observer) except (InvalidSubscriptionError, MissingSubscriptionError) as exception: @@ -208,7 +211,7 @@ if __name__ == '__main__': else: raise RuntimeError('The subscription could not be activated. Please try again later.') - ProfileController.enable(profile, force=arguments.force, profile_observer=profile_observer, application_version_observer=application_version_observer, connection_observer=connection_observer) + ProfileController.enable(profile, force=arguments.force, pristine=arguments.pristine, profile_observer=profile_observer, application_version_observer=application_version_observer, connection_observer=connection_observer) elif manage_subscription_input == '2': @@ -220,7 +223,7 @@ if __name__ == '__main__': if subscription is not None: ProfileController.attach_subscription(profile, subscription) - ProfileController.enable(profile, arguments.force, profile_observer=profile_observer, application_version_observer=application_version_observer, connection_observer=connection_observer) + ProfileController.enable(profile, force=arguments.force, pristine=arguments.pristine, profile_observer=profile_observer, application_version_observer=application_version_observer, connection_observer=connection_observer) else: