class Workspace(Subcommand, object): ws = None subparser = None def setup(self, name, config, subparser): self.ws = Ws(config) self.subparser = subparser super(Workspace, self).setup(name, config, subparser) def parse(self): parser = self.subparser.add_parser( "workspace", help="Workspace managment", description="Manage repository's workspace") subparser = parser.add_subparsers( dest="workspace_subcommand") add_parser = subparser.add_parser( "add", help="Add workspace") rm_parser = subparser.add_parser( "remove", help="Remove existing workspace") subparser.add_parser( "list", help="Show registered workspace") add_parser.add_argument("name", type=str, help="Workspace name") add_parser.add_argument("path", type=str, help="Workspace path") rm_parser.add_argument("name", type=str, help="Workspace name") def execute(self, args): logger = logging.getLogger(__name__) if args.workspace_subcommand is None: self.parser.print_help() return None color = Color() if (args.workspace_subcommand == "add"): ws_name = slashes2dash(args.name) self.ws.add(ws_name, args.path) logger.info(color.colored( "Workspace `%s` successfuly added" % ws_name, "green")) elif (args.workspace_subcommand == "remove"): ws_name = slashes2dash(args.name) self.ws.remove(ws_name) logger.info(color.colored( "Workspace `%s` successfuly removed" % ws_name, "green")) elif (args.workspace_subcommand == "list"): table = PrettyTable(["Name", "Path"]) table.align["Name"] = "l" table.align["Path"] = "l" for key, ws in sorted(self.ws.list().items()): table.add_row([key, ws["path"]]) logger.info(table) def load_workspaces_subcommands(self, subcmd): for key, value in self.ws.list().items(): ws_subcmds = WorkspaceSubcommands(key, self.subparser, self.config) subcmd.commands[key] = ws_subcmds
def test_add(self): """Test add workspace.""" self.config.update({ "workspaces": {"foo": {"path": "/foo", "repositories": {}}}}) ws = Workspace(self.config) ws.add("bar", os.path.realpath(__file__))