Example #1
0
    def parse_invocation(self, invocation, controller_tag):
        """
        Given an invocation string, determine which part is the path, the program,
        and the args.
        """
        if invocation.endswith('/'):
            invocation = invocation[:-1]
        if not invocation.startswith('/'):
            invocation = '/' + invocation
        if invocation == '':
            invocation = '/'

        all_programs = self.get_urls(controllers=[controller_tag])

        matching_paths = set()
        for program_path in sorted(all_programs):
            if invocation.startswith(program_path):
                matching_paths.add(program_path)

        longest = ""
        for path in matching_paths:
            longest = path if len(path) > len(longest) else longest

        matching_path = longest

        program = self.get_program(matching_path, controller=controller_tag)

        if not matching_path:
            raise ProgramNotFound("Can't find %s" % invocation)

        program_name = matching_path.split('/')[-1]
        path = "/".join(matching_path.split('/')[:-1]) + '/'
        args_fragment = invocation[len(matching_path):]

        superformat = None
        if args_fragment.startswith('.'):
            # args_fragment will be something like ".html/arg1/arg2" or just ".html"
            superformat = args_fragment.split('/')[0][1:]
            args = args_fragment.split('/')[1:]
            args_fragment = '/'.join(args)
        else:
            args = args_fragment.split("/")[1:] if args_fragment else []
            args_fragment = args_fragment[1:] if (
                args_fragment and args_fragment[0] == '/') else args_fragment

        return {
            'program': program,
            'program_name': program_name,
            'superformat': superformat,
            'superformat_mime': super_accept_to_mimetype(superformat),
            'args': args,
            'raw_args': args_fragment,
            'path': path,
            'invocation': invocation,
        }
Example #2
0
    def parse_invocation(self, invocation, controller_tag):
        """
        Given an invocation string, determine which part is the path, the program,
        and the args.
        """
        if invocation.endswith('/'):
            invocation = invocation[:-1]
        if not invocation.startswith('/'):
            invocation = '/' + invocation
        if invocation == '':
            invocation = '/'

        all_programs = self.get_urls(controllers=[controller_tag])

        matching_paths = set()
        for program_path in sorted(all_programs):
            if invocation.startswith(program_path):
                matching_paths.add(program_path)

        longest = ""
        for path in matching_paths:
            longest = path if len(path) > len(longest) else longest

        matching_path = longest

        program = self.get_program(matching_path, controller=controller_tag)

        if not matching_path:
            raise ProgramNotFound("Can't find %s" % invocation)

        program_name = matching_path.split('/')[-1]
        path = "/".join(matching_path.split('/')[:-1]) + '/'
        args_fragment = invocation[len(matching_path):]

        superformat = None
        if args_fragment.startswith('.'):
            # args_fragment will be something like ".html/arg1/arg2" or just ".html"
            superformat = args_fragment.split('/')[0][1:]
            args = args_fragment.split('/')[1:]
            args_fragment = '/'.join(args)
        else:
            args = args_fragment.split("/")[1:] if args_fragment else []
            args_fragment = args_fragment[1:] if (args_fragment and args_fragment[0] =='/') else args_fragment

        return {
            'program': program,
            'program_name': program_name,
            'superformat': superformat,
            'superformat_mime': super_accept_to_mimetype(superformat),
            'args': args,
            'raw_args': args_fragment,
            'path': path,
            'invocation': invocation,
        }
Example #3
0
    def __init__(self, persist=None, **kwargs):
        self.persist = persist
        self.render_map = {}
        class_defined_renderers = [x for x in dir(self) if not x.startswith('__')]
        self._register_renderers(class_defined_renderers)

        for format, function in kwargs.items():
            ## key word arguments can be passed into the constructor to
            ## override render methods from within the manifest.
            mime = super_accept_to_mimetype(format)
            setattr(function, 'mimetypes', [mime])
            setattr(self, format, function)

        # kwarg renderers kill any render methods that are defined by the class
        self._register_renderers(kwargs.keys())
Example #4
0
    def __init__(self, persist=None, **kwargs):
        self.persist = persist
        self.render_map = {} # renderers by mimetype
        self.reject_map = {} # renderers by name (no corresponding mimetype)
        class_defined_renderers = [x for x in dir(self) if not x.startswith('__')]
        self._register_renderers(class_defined_renderers)

        for format, function in kwargs.items():
            ## key word arguments can be passed into the constructor to
            ## override render methods from within the manifest.
            ## set 'mimetypes' as if it were using the @renders decorator
            mime = super_accept_to_mimetype(format)
            setattr(function, 'mimetypes', [mime or format])
            setattr(self, format, function)

        # kwarg renderers kill any render methods that are defined by the class
        self._register_renderers(kwargs.keys())
Example #5
0
 def _parse(self, raw_program_name, args, controller_tag):
     """
     Recursive function to transversing nested manifests.
     raw_program_name == program with superformat intact
     """
     program_name, superformat = self.extract_superformat(raw_program_name)
     try:
         program = self.get_program(program_name, controller_tag)
     except KeyError:
         # program name is not in keys, drop down to root...
         if '' in self.manifest:
             result = self.get_program('', controller_tag)
             if type(result) == ProgramManifest:
                 return result._parse(raw_program_name, args, controller_tag)
             else:
                 return {
                     'program': result,
                     'name': '',
                     'superformat': superformat,
                     'superformat_mime': None,
                     'args': [program_name] + args,
                 }
         else:
             raise ProgramNotFound("Program '%s' Does Not Exist" % program_name)
     else:
         if type(program) == ProgramManifest:
             if program_name == '':
                 return program._parse('', args, controller_tag)
             if not args:
                 raise ProgramNotFound('No root program for namespace, and no program match')
             return program._parse(args[0], args[1:], controller_tag)
         else:
             return {
                 'program': program,
                 'name': program_name,
                 'superformat': superformat,
                 'superformat_mime': super_accept_to_mimetype(superformat),
                 'args': args,
             }