def Upload(args): """ > waltz upload "Programming 37: For Loops" If service/category not in registry database, Then we can go ask all the services if they already know about this thing. """ registry = Registry.load(args.waltz_directory) resource_category = registry.guess_resource_category(args) resource_category.upload(registry, args)
def Init(args): if Registry.exists(args.directory): logging.warning("Existing Waltz registry in this directory.") if args.overwrite: Registry.delete(args.directory) else: return Registry.load(args.directory) registry = Registry.init(args.directory) registry.configure_service(Local(args.directory, {'path': args.directory})) registry.save_to_file() return registry
def Encode(args): """ > waltz encode "Programming 37: For Loops" > waltz encode canvas assignment "Programming 37: For Loops" If we found out the resource category, we can include that in the Registry Database. That might also allow us to infer the Service. """ registry = Registry.load(args.waltz_directory) resource_category = registry.guess_resource_category(args) resource_category.encode(registry, args)
def List(args): registry = Registry.load(args.waltz_directory) if not args.service: print("The following services are available:") for service_type, services in registry.services.items(): if services: print("\t", service_type+":") for service in services: print("\t\t", service.name) else: print("\t", service_type + ":", "(none configured)") else: service = registry.get_service(args.service) service.list(registry, args) return registry
def Push(args): registry = Registry.load(args.waltz_directory) resource_category = None if len(args.resource) == 1: local = registry.get_service('local', args.local_service) existing_file = local.find_existing(registry, args.resource[0], False, None) _, waltz, _ = extract_front_matter(local.read(existing_file)) if 'resource' in waltz: # TODO: validate resource category resource_category = registry.get_resource_category(waltz['resource']) args.category = waltz['resource'] args.title = args.resource[0] args.service = registry.get_service(resource_category.default_service).name if resource_category is None: resource_category = registry.guess_resource_category(args) resource_category.encode(registry, args) resource_category.upload(registry, args)
def Download(args): """ > waltz download --filename for_loops.md > waltz download "Programming 37: For Loops" > waltz download assignment "Final Exam" > waltz download canvas "Final Exam" > waltz download canvas assignment "Final Exam" > waltz download canvas --id 234347437743 > waltz download <Name> > waltz download <Service> <Name> > waltz download <Resource> <Name> > waltz download <Service> <Resource> <Name> > waltz download --parameter <Value> > waltz download --all """ registry = Registry.load(args.waltz_directory) resource_category = registry.guess_resource_category(args) resource_category.download(registry, args) return registry
def parse_command_line(args): parser = argparse.ArgumentParser( prog='waltz', description='Sync resources between services for a course') parser.add_argument( '--waltz_directory', type=str, default="./", help= "Path to the main waltz directory with the Waltz registry and DB file." ) subparsers = parser.add_subparsers(help='Available commands') # Init Waltz parser_init = subparsers.add_parser('init', help='Initialize a new Waltz here') parser_init.add_argument( '--directory', "-d", type=str, default="./", help= "The local directory to use for this waltz; defaults to current directory." ) parser_init.add_argument( '--overwrite', "-o", action="store_true", default=False, help="If used, then overwrites the existing waltz registry.") parser_init.set_defaults(func=actions.Init) # Reset Database parser_reset = subparsers.add_parser( 'reset', help='Reset the Waltz database entirely.') parser_reset.set_defaults(func=actions.Reset) # Configure service parser_configure = subparsers.add_parser( 'configure', help='Configure a new instance of the service.') parser_configure_services = parser_configure.add_subparsers( dest='type', help="The type of the service you are configuring.") for name, service_type in defaults.get_service_types().items(): service_type.add_parser_configure(parser_configure_services) parser_configure.set_defaults(func=actions.Configure) # List Services or Resources parser_list = subparsers.add_parser( 'list', help='List available services or resources') parser_list_services = parser_list.add_subparsers( dest='service', help="The service to search within.") for name, service_type in defaults.get_service_types().items(): service_type.add_parser_list(parser_list_services) registry = Registry.load('./', False) if registry is not None: for name, services in registry.services.items(): for service_type in services: service_type.add_parser_list(parser_list_services, service_type.name) parser_list.set_defaults(func=actions.List) # Show [Course|Service] # Search parser_search = subparsers.add_parser('search', help='Search for a resource.') parser_search.add_argument('category', type=str, help="The category of resource to search") parser_search.add_argument('what', type=str, help="The resource to download") parser_search.add_argument( "--service", type=str, help="The specific service to use in case of ambiguity.") parser_search.set_defaults(func=actions.Search) def add_id_and_url(subparser): subparser.add_argument( "--id", help= "A resource-specific ID to disambiguate this resource definitively." ) subparser.add_argument( "--url", help= "A resource-specific URL to disambiguate this resource definitively." ) subparser.add_argument("--all", action='store_true', help="Get all the resources of this type.") # Download parser_download = subparsers.add_parser( 'download', help='Download the raw version of a resource.') parser_download.add_argument( 'resource', nargs='+', type=str, help="The resource to download. Could be a " "resource title, filename, or some combination of those and the service and category." ) add_id_and_url(parser_download) parser_download.set_defaults(func=actions.Download) # Upload parser_upload = subparsers.add_parser( 'upload', help='Upload the raw version of a resource.') parser_upload.add_argument( 'resource', nargs='+', type=str, help="The resource to download. Could be a " "resource title, filename, or some combination of those and the service and category." ) add_id_and_url(parser_upload) parser_upload.set_defaults(func=actions.Upload) # Decode parser_decode = subparsers.add_parser( 'decode', help='Convert a raw resource into a locally editable one.') parser_decode.add_argument( 'resource', nargs='+', type=str, help="The resource to decode. Could be a " "filename, resource title, or some combination of those and the service and category." ) parser_decode.add_argument( "--local_service", type=str, help="The specific local service to use as an override.") parser_decode.add_argument( "--destination", "-d", type=str, help="The destination directory for this resource.") parser_decode.add_argument( "--combine", "-c", action='store_true', default=False, help="Whether to combine all subresources into a single file.") parser_decode.add_argument( "--hide_answers", action='store_true', default=False, help="Whether to hide answers to any questions.") parser_decode.add_argument( "--banks", nargs="*", type=str, help= "The question bank folders to check. First one will be the location for new questions." ) add_id_and_url(parser_decode) # TODO: Allow override of specific local, but otherwise assume default `local`? parser_decode.set_defaults(func=actions.Decode) # Encode parser_encode = subparsers.add_parser( 'encode', help='Convert a locally editable resource into a raw one.') parser_encode.add_argument( 'resource', nargs='+', type=str, help="The resource to encode. Could be a " "filename, resource title, or some combination of those and the service and category." ) parser_encode.add_argument( "--local_service", type=str, help="The specific local service to use as an override.") parser_encode.add_argument("--banks", nargs="*", type=str, help="The question bank folders to check.") add_id_and_url(parser_encode) parser_encode.set_defaults(func=actions.Encode) # Diff parser_diff = subparsers.add_parser( 'diff', help='Compare the remote version of a resource and the local one.') parser_diff.add_argument( 'resource', nargs='+', type=str, help="The resource to diff. Could be a " "filename, resource title, or some combination of those and the service and category." ) parser_diff.add_argument( "--local_service", type=str, help="The specific local service to use as an override.") parser_diff.add_argument( "--console", action="store_true", help="Do not generate HTML file; just print to console.") parser_diff.add_argument( "--prevent_open", action="store_true", help= "Prevent the generated HTML file from being automatically opened in your browser." ) parser_diff.add_argument("--banks", nargs="*", type=str, help="The question bank folders to check.") parser_diff.add_argument( "--combine", "-c", action='store_true', default=False, help="Whether to combine all subresources into a single file.") parser_diff.add_argument("--hide_answers", action='store_true', default=False, help="Whether to hide answers to any questions.") add_id_and_url(parser_diff) parser_diff.set_defaults(func=actions.Diff) # Push parser_push = subparsers.add_parser( 'push', help='Convert a locally editable resource into a raw one and upload it.' ) parser_push.add_argument( 'resource', nargs='+', type=str, help="The resource to encode and upload. Could be a " "filename, resource title, or some combination of those and the service and category." ) parser_push.add_argument( "--local_service", type=str, help="The specific local service to use as an override.") parser_push.add_argument("--banks", nargs="*", type=str, help="The question bank folders to check.") add_id_and_url(parser_push) parser_push.set_defaults(func=actions.Push) # Pull parser_pull = subparsers.add_parser( 'pull', help='Download a raw resource and convert it to a locally editable one.' ) parser_pull.add_argument( 'resource', nargs='+', type=str, help="The resource to download and decode. Could be a " "filename, resource title, or some combination of those and the service and category." ) parser_pull.add_argument( "--local_service", type=str, help="The specific local service to use as an override.") parser_pull.add_argument("--banks", nargs="*", type=str, help="The question bank folders to check.") parser_pull.add_argument( "--combine", "-c", action='store_true', default=False, help="Whether to combine all subresources into a single file.") parser_pull.add_argument("--hide_answers", action='store_true', default=False, help="Whether to hide answers to any questions.") parser_pull.add_argument( "--destination", "-d", type=str, help="The destination directory for this resource.") add_id_and_url(parser_pull) parser_pull.set_defaults(func=actions.Pull) # Extract # Build # Undo # ... Conclude! parsed = parser.parse_args(args) return parsed.func(parsed)
def handle_registry(args, registry): if registry is None: return Registry.load(args.registry_path) return registry
def Configure(args): registry = Registry.load(args.waltz_directory) new_service = Service.from_type(args.type).configure(args) registry.configure_service(new_service) registry.save_to_file() return registry
def Reset(args): registry = Registry.load(args.waltz_directory) registry.reset_database() registry.save_to_file() return registry
def Diff(args): registry = Registry.load(args.waltz_directory) resource_category = registry.guess_resource_category(args) resource_category.diff(registry, args)
def Pull(args): registry = Registry.load(args.waltz_directory) resource_category = registry.guess_resource_category(args) resource_category.download(registry, args) resource_category.decode(registry, args)