def update(options, args): if len(options.objects) == 1: if len(args) == 2: to_update = b.get( options.user_id, options.workspace_id, options.objects[0], filter_by={'id': int(args[0])} )[0] update = None try: update = json.loads(args[1]) # FIXME: is there something in simplejson that lets me just check whether a string is valid JSON? except json.JSONDecodeError as e: value = args[1] if isinstance(value, str): value = unicode(value) b.set_attributes(options.user_id, options.workspace_id, to_update, { to_update['primary_descriptor']: value }) else: for prop, value in (json.loads(args[1])).iteritems(): if isinstance(value, str): value = unicode(value) b.set_attributes(options.user_id, options.workspace_id, to_update, {prop: value}) print 'Updated:', format(to_update, options) b.commit() else: raise Exception('Two and only two arguments in update') else: raise Exception('One and only one object in update')
def complete(options, args): if len(options.objects) == 1: if len(args) == 1: to_complete = b.get( options.user_id, options.workspace_id, options.objects[0], filter_by={'id': int(args[0])} )[0] result = b.set_completed(options.user_id, options.workspace_id, to_complete) if result is not None: print 'Completed:', format(to_complete, options) b.commit() else: raise Exception('One and only one argument in complete') else: raise Exception('One and only one object in complete')
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 change_workspace_permission(options, target_user_info, permissions, backend_function): if isinstance(target_user_info, basestring): target_users = b.get_users(unicode(target_user_info)) if len(target_users) == 0: print 'Target user {0} not found.'.format(target_user_info) return if len(target_users) > 1: print 'Multiple target users with username {0} found - ' \ 'this is unsupported.'.format(target_user_info) return target_user_info = target_users[0]['user_id'] print backend_function( options.user_id, options.workspace_id, target_user_info, permissions ) b.commit()
def delete(options, args): if len(options.objects) == 1: if len(args) == 1: to_delete = b.get( options.user_id, options.workspace_id, options.objects[0], filter_by={'id': int(args[0])} ) if len(to_delete) > 0: deleted = b.delete_from_db(options.user_id, options.workspace_id, to_delete[0]) print 'Deleted:', format(deleted, options) b.commit() else: raise Exception('Todo with id {0} not found.'.format( args[0] )) else: raise Exception('One and only one argument in delete') else: raise Exception('One and only one object in delete')
def lnk(options, args): if len(options.objects) == 2: if len(args) == 2: obj = b.get(options.user_id, options.workspace_id, options.objects[0], filter_by={'id': int(args[0])})[0] related_obj = b.get( options.user_id, options.workspace_id, options.objects[1], filter_by={'id': int(args[1])} )[0] if options.unlink: b.unlink(options.user_id, options.workspace_id, obj, related_obj, options.relationship) else: b.link(options.user_id, options.workspace_id, obj, related_obj, options.relationship) b.commit() else: raise Exception('Two and only two arguments in link') else: raise Exception('Two and only two objects in link')
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)