def add(options, args): if len(options.objects) == 1: if options.objects[0] == 'Workspace': # We are handling Workspace descriptions new_workspaces = b.create_workspace(options.user_id, [unicode(arg) for arg in args], [u'' for arg in args]) else: created = b.put(options.user_id, options.workspace_id, options.objects[0], [unicode(arg) for arg in args]) if len(created) == 0: raise Exception('Write forbidden.') if isinstance(created, dict): created = [created] if options.top and 'display_position' in created[0]: for obj in created: b.move(options.user_id, options.workspace_id, obj, direction='float', all_the_way=True) print 'Created:', format(created, options) b.commit() else: raise Exception('One and only one object in add')
def main(): options, args = parser.parse_args() b.initialize(backend_settings) cli_settings = dict(ini.items('cli')) username = get_username(options, cli_settings) if 'remote' in backend_settings and backend_settings['remote']: if hasattr(options, 'initial_token') and \ options.initial_token is not None: try: b.trade_initial_token(username, options.initial_token) except b.RemoteException as e: if e.code == 403: print 'Your initial token was not accepted. Please ' \ 'check with your pydidit administrator.' sys.exit(1) else: raise e print 'Successfully authenticated!' return else: if not b.check_access_token(username): print 'Please use the --trade-initial-token option to authenticate.' sys.exit(1) if options.objects is None or len(options.objects) == 0: if options.operations is None or 'search' not in options.operations: options.objects = ['Todo'] # Check for add user request if hasattr(options, 'add_user') and options.add_user is not None: b.create_user(unicode(options.add_user)) b.commit() return if username is None: print 'No username defined' return users = b.get_users(unicode(username)) if len(users) == 0: print 'User {0} not found.'.format(username) return if len(users) > 1: print 'Multiple users with username {0} found - this is unsupported.'\ .format(username) return options.user_id = users[0]['user_id'] workspace_name = cli_settings['workspace'] \ if 'workspace' in cli_settings \ else None if hasattr(options, 'workspace_name') and options.workspace_name is not None: workspace_name = options.workspace_name # If we just want to operate on workspaces, ignore the configured # workspace if len(options.objects) != 1 or options.objects[0] != 'Workspace': workspaces = b.get_workspaces(options.user_id, unicode(workspace_name)) if len(workspaces) == 0: print 'Workspace {0} not found.'.format(workspace_name) return if len(workspaces) > 1: print 'Multiple workspaces with name {0} found - this is ' \ 'unsupported.'.format(workspace_name) return options.workspace_id = workspaces[0]['id'] # Next, check for add workspace permission or revoke workspace permission # request handled_workspace_permission = False for workspace_permission in ( 'add_workspace_permission', 'revoke_workspace_permission' ): if getattr(options, workspace_permission, None) is not None: globals()[workspace_permission](options) handled_workspace_permission = True # Don't keep going if we did workspace permission work if handled_workspace_permission: return if options.operations is None: read(options, args) elif len(options.operations) > 1: raise Exception('Only one operation at a time supported.') else: if options.operations[0] == 'read': read(options, args) elif options.operations[0] == 'add': add(options, args) elif options.operations[0] == 'update': update(options, args) elif options.operations[0] == 'delete': delete(options, args) elif options.operations[0] == 'complete': complete(options, args) elif options.operations[0] == 'float': b.move(options.user_id, options.workspace_id, int(args[0]), direction='float', model_name=options.objects[0], all_the_way=options.top) b.commit() elif options.operations[0] == 'sink': b.move(options.user_id, options.workspace_id, int(args[0]), direction='sink', model_name=options.objects[0], all_the_way=options.bottom) b.commit() elif options.operations[0] == 'move': b.move(options.user_id, options.workspace_id, int(args[0]), int(args[1]), model_name=options.objects[0]) b.commit() elif options.operations[0] == 'link': lnk(options, args) elif options.operations[0] == 'unlink': lnk(options, args) elif options.operations[0] == 'search': search(options, args)
def main(): options, args = parser.parse_args() if options.objects is None or len(options.objects) == 0: if options.operations is None or 'search' not in options.operations: options.objects = ['Todo'] config = StringIO() ini.write(config) config.seek(0) b.initialize(external_config_fp=config) cli_settings = dict(ini.items('cli')) # First, check for add user request if hasattr(options, 'add_user') and options.add_user is not None: b.create_user(unicode(options.add_user)) b.commit() return username = \ cli_settings['username'] if 'username' in cli_settings else None if hasattr(options, 'username') and options.username is not None: username = options.username if username is None: print 'No username defined' return users = b.get_users(unicode(username)) if len(users) == 0: print 'User {0} not found.'.format(username) return if len(users) > 1: print 'Multiple users with username {0} found - this is unsupported.'\ .format(username) return options.user_id = users[0]['user_id'] workspace_name = cli_settings['workspace'] \ if 'workspace' in cli_settings \ else None if hasattr(options, 'workspace_name') and options.workspace_name is not None: workspace_name = options.workspace_name workspaces = b.get_workspaces(options.user_id, unicode(workspace_name)) if len(workspaces) == 0: print 'Workspace {0} not found.'.format(workspace_name) return if len(workspaces) > 1: print 'Multiple workspaces with name {0} found - this is ' \ 'unsupported.'.format(workspace_name) return options.workspace_id = workspaces[0]['workspace_id'] # Next, check for add workspace permission or revoke workspace permission # request handled_workspace_permission = False for workspace_permission in ( 'add_workspace_permission', 'revoke_workspace_permission' ): if getattr(options, workspace_permission, None) is not None: globals()[workspace_permission](options) handled_workspace_permission = True # Don't keep going if we did workspace permission work if handled_workspace_permission: return if options.operations is None: read(options, args) elif len(options.operations) > 1: raise Exception('Only one operation at a time supported.') else: if options.operations[0] == 'read': read(options, args) elif options.operations[0] == 'add': add(options, args) elif options.operations[0] == 'update': update(options, args) elif options.operations[0] == 'delete': delete(options, args) elif options.operations[0] == 'complete': complete(options, args) elif options.operations[0] == 'float': b.move(options.user_id, options.workspace_id, int(args[0]), direction='float', model_name=options.objects[0], all_the_way=options.top) b.commit() elif options.operations[0] == 'sink': b.move(options.user_id, options.workspace_id, int(args[0]), direction='sink', model_name=options.objects[0], all_the_way=options.bottom) b.commit() elif options.operations[0] == 'move': b.move(options.user_id, options.workspace_id, int(args[0]), int(args[1]), model_name=options.objects[0]) b.commit() elif options.operations[0] == 'link': lnk(options, args) elif options.operations[0] == 'unlink': lnk(options, args) elif options.operations[0] == 'search': search(options, args)