def download_all(cls, registry: Registry, args): canvas = registry.get_service(args.service, "canvas") local = registry.get_service('local') resources = canvas.api.get(cls.endpoint, retrieve_all=True) rows = [] for resource in tqdm(natsorted(resources, key=cls.sort_resource)): try: path = local.find_existing(registry, resource[cls.title_attribute]) rows.append(("Yes", "Yes", resource[cls.title_attribute], os.path.relpath(path))) except WaltzAmbiguousResource as war: paths = "\n".join( os.path.relpath(path) for path in war.args[0]) rows.append( ("Yes", "Multiple", resource[cls.title_attribute], paths)) except FileNotFoundError: rows.append(("Yes", "No", resource[cls.title_attribute], "")) full_resource = canvas.api.get(cls.endpoint + str(resource[cls.id])) registry.store_resource(canvas.name, cls.name, resource[cls.title_attribute], "", json.dumps(full_resource)) print(tabulate(rows, ('Remote', 'Local', 'Title', 'Path'))) print("Downloaded", len(resources), cls.name_plural)
def download(cls, registry: Registry, args): blockpy = registry.get_service(args.service, "blockpy") courses = blockpy.api.get('list/courses/')['courses'] potentials = [ course for course in courses if course['url'] == args.title ] if len(potentials) > 1: raise WaltzAmbiguousResource( "Too many courses with URL '{}'".format(args.title)) elif not potentials: raise WaltzResourceNotFound( "No course with URL '{}' found.".format(args.title)) bundle = blockpy.api.get('export/', json={'course_id': potentials[0]['id']}) # Assignments for assignment in bundle['assignments']: registry.store_resource(blockpy.name, 'problem', assignment['url'], "", json.dumps(assignment)) # Memberships groups_assignments = {} for membership in bundle['memberships']: if membership['assignment_group_url'] not in groups_assignments: groups_assignments[membership['assignment_group_url']] = [] groups_assignments[membership['assignment_group_url']].append( membership['assignment_url']) # Groups for group in bundle['groups']: group['problems'] = groups_assignments.get(group['url'], []) registry.store_resource(blockpy.name, 'blockpy_group', group['url'], "", json.dumps(group))
def encode(cls, registry: Registry, args): local = registry.get_service(args.local_service, 'local') source_path = local.find_existing(registry, args.title, folder_file=cls.folder_file) decoded_markdown = local.read(source_path) data = cls.encode_json(registry, decoded_markdown, args) registry.store_resource(args.service, cls.name, args.title, "", data)
def download(cls, registry: Registry, args): blockpy = registry.get_service(args.service, "blockpy") bundle = blockpy.api.get('export/', json={'assignment_url': args.title}) potentials = bundle['assignments'] # Assignments if len(potentials) > 1: raise WaltzAmbiguousResource( f"Too many problems with URL '{args.title}'") elif not potentials: raise WaltzResourceNotFound( f"No problem with URL '{args.title}' found.") assignment = potentials[0] registry.store_resource(blockpy.name, 'problem', assignment['url'], "", json.dumps(assignment))
def download(cls, registry: Registry, args): if args.all: cls.download_all(registry, args) return canvas = registry.get_service(args.service, "canvas") resource_json = cls.find(canvas, args.title) if resource_json is not None: try: registry.find_resource(canvas.name, cls.name, args.title, "") print("Downloaded new version of {}: ".format(cls.name), args.title) except WaltzException: print("Downloaded new {}:".format(cls.name), args.title) resource_json = json.dumps(resource_json) registry.store_resource(canvas.name, cls.name, args.title, "", resource_json) return resource_json cls.find_similar(registry, canvas, args)