Best Python code snippet using tempest_python
workspace.py
Source:workspace.py  
...89        workspace_path = self.workspaces.pop(name)90        self._write_file()91        return workspace_path92    @lockutils.synchronized('workspaces', external=True)93    def remove_workspace_directory(self, workspace_path):94        self._validate_path(workspace_path)95        shutil.rmtree(workspace_path)96    @lockutils.synchronized('workspaces', external=True)97    def list_workspaces(self):98        self._populate()99        self._validate_workspaces()100        return self.workspaces101    def _workspace_name_exists(self, name):102        if name in self.workspaces:103            print("A workspace already exists with name: {0}.".format(104                name))105            sys.exit(1)106    def _invalid_name_check(self, name):107        if not name:108            print("None or empty name is specified."109                  " Please specify correct name for workspace.")110            sys.exit(1)111    def _validate_path(self, path):112        if not path:113            print("None or empty path is specified for workspace."114                  " Please specify correct workspace path.")115            sys.exit(1)116        if not os.path.exists(path):117            print("Path does not exist.")118            sys.exit(1)119    @lockutils.synchronized('workspaces', external=True)120    def register_new_workspace(self, name, path, init=False):121        """Adds the new workspace and writes out the new workspace config"""122        self._populate()123        path = os.path.abspath(os.path.expanduser(path)) if path else path124        # This only happens when register is called from outside of init125        if not init:126            self._validate_path(path)127        self._invalid_name_check(name)128        self._workspace_name_exists(name)129        self.workspaces[name] = path130        self._write_file()131    def _validate_workspaces(self):132        if self.workspaces is not None:133            self.workspaces = {n: p for n, p in self.workspaces.items()134                               if os.path.exists(p)}135            self._write_file()136    def _write_file(self):137        with open(self.path, 'w') as f:138            f.write(yaml.dump(self.workspaces))139    def _populate(self):140        if not os.path.isfile(self.path):141            return142        with open(self.path, 'r') as f:143            self.workspaces = yaml.safe_load(f) or {}144def add_global_arguments(parser):145    parser.add_argument(146        '--workspace-path', required=False, default=None,147        help="The path to the workspace file, the default is "148             "~/.tempest/workspace.yaml")149    return parser150class TempestWorkspaceRegister(command.Command):151    def get_description(self):152        return ('Registers a new tempest workspace via a given '153                '--name and --path')154    def get_parser(self, prog_name):155        parser = super(TempestWorkspaceRegister, self).get_parser(prog_name)156        add_global_arguments(parser)157        parser.add_argument('--name', required=True)158        parser.add_argument('--path', required=True)159        return parser160    def take_action(self, parsed_args):161        self.manager = WorkspaceManager(parsed_args.workspace_path)162        self.manager.register_new_workspace(parsed_args.name, parsed_args.path)163        sys.exit(0)164class TempestWorkspaceRename(command.Command):165    def get_description(self):166        return 'Renames a tempest workspace from --old-name to --new-name'167    def get_parser(self, prog_name):168        parser = super(TempestWorkspaceRename, self).get_parser(prog_name)169        add_global_arguments(parser)170        parser.add_argument('--old-name', required=True)171        parser.add_argument('--new-name', required=True)172        return parser173    def take_action(self, parsed_args):174        self.manager = WorkspaceManager(parsed_args.workspace_path)175        self.manager.rename_workspace(176            parsed_args.old_name, parsed_args.new_name)177        sys.exit(0)178class TempestWorkspaceMove(command.Command):179    def get_description(self):180        return 'Changes the path of a given tempest workspace --name to --path'181    def get_parser(self, prog_name):182        parser = super(TempestWorkspaceMove, self).get_parser(prog_name)183        add_global_arguments(parser)184        parser.add_argument('--name', required=True)185        parser.add_argument('--path', required=True)186        return parser187    def take_action(self, parsed_args):188        self.manager = WorkspaceManager(parsed_args.workspace_path)189        self.manager.move_workspace(parsed_args.name, parsed_args.path)190        sys.exit(0)191class TempestWorkspaceRemove(command.Command):192    def get_description(self):193        return 'Deletes the entry for a given tempest workspace --name'194    def get_parser(self, prog_name):195        parser = super(TempestWorkspaceRemove, self).get_parser(prog_name)196        add_global_arguments(parser)197        parser.add_argument('--name', required=True)198        parser.add_argument('--rmdir', action='store_true',199                            help='Deletes the given workspace directory')200        return parser201    def take_action(self, parsed_args):202        self.manager = WorkspaceManager(parsed_args.workspace_path)203        workspace_path = self.manager.remove_workspace_entry(parsed_args.name)204        if parsed_args.rmdir:205            self.manager.remove_workspace_directory(workspace_path)206        sys.exit(0)207class TempestWorkspaceList(lister.Lister):208    def get_description(self):209        return 'Outputs the name and path of all known tempest workspaces'210    def get_parser(self, prog_name):211        parser = super(TempestWorkspaceList, self).get_parser(prog_name)212        add_global_arguments(parser)213        return parser214    def take_action(self, parsed_args):215        self.manager = WorkspaceManager(parsed_args.workspace_path)216        return (("Name", "Path"),...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
