예제 #1
0
def _templates_from_args(log, args):
    strategy = None
    template_path = args.template_path or []
    templates = []

    if args.wrap:
        # Load strategy and build extension
        from F2x.distutils.strategy import get_strategy
        strategy = get_strategy(args.wrap)

        if not args.template:
            for name in strategy.templates:
                module = get_template(name)
                if module is None:
                    continue

                for filename in module.templates or []:
                    filepath = os.path.join(module.package_dir, filename)
                    suffix, _ = os.path.splitext(os.path.basename(filename))
                    templates.append((module, filename, filepath, suffix))

    if args.template:
        # If templates are supplied, process them
        for filename in args.template:
            try:
                if filename[0] == u'@':
                    filename = filename[1:]
                    filepath = os.path.join(template_package_dir, filename)
                    template_path.append(os.path.dirname(filepath))

                    modpath, *_, basename = os.path.split(filename)
                    suffix, _ = os.path.splitext(basename)
                    module = get_template(modpath)

                else:
                    filepath = filename
                    basename, ext = os.path.splitext(filename)
                    suffix = os.path.basename(basename)

                    # Template without extension is most probably a built-in so we can (try to) load it
                    if not ext:
                        module = get_template(basename)
                    else:
                        module = None

                templates.append((module, filename, filepath, suffix))

            except ImportError:
                log.warn(f'could not load template "{filename}" as {modpath}.')
                continue

    return templates, template_path, strategy
예제 #2
0
    def load_templates(self, template_names):
        """
        Load a list with template names into a list with templates and extra information:

        * loaded template
        * template file name (as passed to loader)
        * full path to loaded template
        * package directory of containing package
        """
        templates = []

        for template_name in template_names:
            if not isinstance(template_name, str):
                templates.append(template_name)
                continue

            template = get_template(template_name)

            if template is None:
                log.warn(f'unknwon template "{template_name}"')
                continue

            for template_file, template_path in zip(template.templates,
                                                    template.template_files):
                package_dir = None if template_file.startswith(
                    '@') else template.package_dir
                templates.append(
                    (template, template_file, template_path, package_dir))

        return templates
예제 #3
0
    def finalize_options(self):
        self.set_undefined_options('build', ('build_base', 'build_base'),
                                   ('build_lib', 'build_lib'),
                                   ('force', 'force'))

        if self.package is None:
            self.package = self.distribution.ext_package

        self.extensions = self.distribution.ext_modules
        self.libraries = self.distribution.libraries or []
        self.py_modules = self.distribution.py_modules or []
        self.py_modules_dict = {}
        self.data_files = self.distribution.data_files or []

        if self.build_src is None:
            self.build_src = os.path.join(
                self.build_base, f"src.{get_platform()}-{sys.version[:3]}")

        if self.templates is not None:
            self.templates = [
                get_template(name) for name in shlex.split(self.templates)
            ]

        if self.strategy is not None:
            self.strategy = get_strategy(self.strategy)

        if self.f2x_options is None:
            self.f2x_options = []
        else:
            self.f2x_options = shlex.split(self.f2x_options)

        if self.inplace is None:
            build_ext = self.get_finalized_command('build_ext')
            self.inplace = build_ext.inplace
예제 #4
0
    def get_items(self, names):
        # type: (List[str]) -> List[Tuple[str, str, str]]
        """
        Collect templates by name. The fully-qualified package name is
        assumed.

        :param names: Names of templates to load.
        :return: A list of all loaded templates.
        """

        items = []

        for fullname in names:
            pkg, name = fullname.rsplit('.', 1)
            mod = None

            if pkg == template.__name__:
                # Try to load built-in template
                mod = template.get_template(name)

            if mod is None:
                try:
                    # Try to import template package
                    mod = importlib.import_module(fullname, pkg)
                    template.register_template(mod)

                except ImportError:
                    log.warning(f'Could not load template "{fullname}".')

            items.append((name, mod, fullname))

        return items
예제 #5
0
    def _resolve_templates(self, template_files):
        template_sources = []

        for template_name, template_file, template_path in template_files:
            if template_name[0] == u'@':
                template_path.append(os.path.dirname(template_file))
                template_dirname, template_basename = os.path.split(
                    template_file)
                _, template_modname = os.path.split(template_dirname)
                template_mod = get_template(template_modname)
                template_suffix, template_ext = os.path.splitext(
                    template_basename)
            else:
                template_mod = get_template(template_name)
                template_suffix, template_ext = os.path.splitext(
                    os.path.basename(template_file))

            template_sources.append(
                (template_mod, template_name, template_file, template_path,
                 template_suffix))

        return template_sources