Exemplo n.º 1
0
def all_files(app, router, src):
    '''Generator of all files within a directory
    '''
    if os.path.isdir(src):
        for dirpath, _, filenames in os.walk(src):
            if skipfile(os.path.basename(dirpath) or dirpath):
                continue
            rel_dir = get_rel_dir(dirpath, src)
            for filename in filenames:
                if skipfile(filename):
                    continue
                name, ext = split(filename)
                name = os.path.join(rel_dir, name)
                fpath = os.path.join(dirpath, filename)
                yield name, fpath, ext
    #
    elif os.path.isfile(src):
        dirpath, filename = os.path.split(src)
        assert not filename.startswith('.')
        name, ext = split(filename)
        fpath = os.path.join(dirpath, filename)
        yield name, dirpath, ext
    #
    else:
        raise BuildError("'%s' not found." % src)
Exemplo n.º 2
0
def get_context_files(app):
    '''Load static context
    '''
    ctx = {}
    location = content_location(app, 'context')
    if location and os.path.isdir(location):
        for dirpath, dirs, filenames in os.walk(location, topdown=False):
            if skipfile(os.path.basename(dirpath) or dirpath):
                continue
            for filename in filenames:
                if skipfile(filename):
                    continue
                file_bits = filename.split('.')
                bits = [file_bits[0]]

                prefix = get_rel_dir(dirpath, location)
                while prefix:
                    prefix, tail = os.path.split(prefix)
                    bits.append(tail)

                filename = os.path.join(dirpath, filename)
                suffix = get_reader(app, filename).suffix
                name = '_'.join(reversed(bits))
                if suffix:
                    name = '%s_%s' % (suffix, name)
                ctx[name] = filename
    return ctx
Exemplo n.º 3
0
def all_files(app, router, src):
    """Generator of all files within a directory
    """
    if os.path.isdir(src):
        for dirpath, _, filenames in os.walk(src):
            if skipfile(os.path.basename(dirpath) or dirpath):
                continue
            rel_dir = get_rel_dir(dirpath, src)
            for filename in filenames:
                if skipfile(filename):
                    continue
                name, ext = split(filename)
                name = os.path.join(rel_dir, name)
                fpath = os.path.join(dirpath, filename)
                yield name, fpath, ext
    #
    elif os.path.isfile(src):
        dirpath, filename = os.path.split(src)
        assert not filename.startswith(".")
        name, ext = split(filename)
        fpath = os.path.join(dirpath, filename)
        yield name, dirpath, ext
    #
    else:
        raise BuildError("'%s' not found." % src)
Exemplo n.º 4
0
def static_context(app, location, context):
    '''Load static context from ``location``
    '''
    ctx = {}
    if os.path.isdir(location):
        for dirpath, dirs, filenames in os.walk(location, topdown=False):
            if skipfile(os.path.basename(dirpath) or dirpath):
                continue
            for filename in filenames:
                if skipfile(filename):
                    continue
                file_bits = filename.split('.')
                bits = [file_bits[0]]

                prefix = get_rel_dir(dirpath, location)
                while prefix:
                    prefix, tail = os.path.split(prefix)
                    bits.append(tail)

                filename = os.path.join(dirpath, filename)
                reader = get_reader(app, filename)
                name = '_'.join(reversed(bits))
                content = reader.read(filename, name)
                if content.suffix:
                    name = '%s_%s' % (content.suffix, name)
                ctx[name] = content.render(context)
                context[name] = ctx[name]
    return ctx
Exemplo n.º 5
0
    def commands(self):
        """Load all commands from installed applications"""
        cmnds = OrderedDict()
        available = set()
        for e in reversed(self.config['EXTENSIONS']):
            try:
                modname = e + ('.core' if e == 'lux' else '') + '.commands'
                mod = import_module(modname)
                if hasattr(mod, '__path__'):
                    path = os.path.dirname(getfile(mod))
                    try:
                        commands = []

                        for f in os.listdir(path):
                            if skipfile(f) or not f.endswith('.py'):
                                continue
                            command = f[:-3]
                            if command not in available:
                                available.add(command)
                                commands.append(command)

                        if commands:
                            cmnds[e] = tuple(commands)
                    except OSError:
                        continue
            except ImportError:
                pass  # No management module
        return OrderedDict(((e, cmnds[e]) for e in reversed(cmnds)))
Exemplo n.º 6
0
    def build(self, name, target):
        render = self.app.template_engine()
        base_name = 'project_name'
        context = {base_name: name,
                   'secret_key': generate_secret(50)}
        prefix_length = len(template_dir) + 1

        for root, dirs, files in os.walk(template_dir):
            path_rest = root[prefix_length:]
            relative_dir = path_rest.replace(base_name, name)

            if relative_dir:
                target_dir = path.join(target, relative_dir)
                if not path.exists(target_dir):
                    os.mkdir(target_dir)

            for dirname in dirs[:]:
                if skipfile(dirname):
                    dirs.remove(dirname)

            for filename in files:
                if (skipfile(filename) or
                        filename.endswith(('.pyo', '.pyc', '.py.class'))):
                    continue

                old_path = path.join(root, filename)

                with open(old_path, 'r') as template_file:
                    content = template_file.read()

                if not filename.endswith('.html'):
                    content = render(content, context)

                new_path = path.join(target, relative_dir,
                                     filename.replace(base_name, name))

                with open(new_path, 'w') as new_file:
                    new_file.write(content)
Exemplo n.º 7
0
    def build(self, name, target):
        render = self.app.template_engine()
        base_name = 'project_name'
        context = {base_name: name, 'secret_key': generate_secret(50)}
        prefix_length = len(template_dir) + 1

        for root, dirs, files in os.walk(template_dir):
            path_rest = root[prefix_length:]
            relative_dir = path_rest.replace(base_name, name)

            if relative_dir:
                target_dir = path.join(target, relative_dir)
                if not path.exists(target_dir):
                    os.mkdir(target_dir)

            for dirname in dirs[:]:
                if skipfile(dirname):
                    dirs.remove(dirname)

            for filename in files:
                if (skipfile(filename) or filename.endswith(
                    ('.pyo', '.pyc', '.py.class'))):
                    continue

                old_path = path.join(root, filename)

                with open(old_path, 'r') as template_file:
                    content = template_file.read()

                if not filename.endswith('.html'):
                    content = render(content, context)

                new_path = path.join(target, relative_dir,
                                     filename.replace(base_name, name))

                with open(new_path, 'w') as new_file:
                    new_file.write(content)
Exemplo n.º 8
0
    def _all(self, group):
        """Contents in this model group
        """
        model = self.model
        content = self.app.config['CONTENT_GROUPS'].get(group)
        if content:
            base_html_path = content.get('path')
            default_meta = content.get('meta', {})
        else:
            base_html_path = None
            default_meta = {}
        directory = os.path.join(model.directory, group)
        if not os.path.isdir(directory):
            if content:
                return []
            else:
                raise Http404
        ext = '.%s' % model.ext
        reader = get_reader(self.app, ext)
        data = []
        for dirpath, dirnames, filenames in os.walk(directory):
            for filename in filenames:
                if skipfile(filename):
                    continue

                if dirpath != directory:
                    path = os.path.relpath(dirpath, directory)
                    filename = os.path.join(path, filename)

                if filename.endswith(ext):
                    slug = filename[:-len(ext)]
                    src = os.path.join(directory, filename)
                    bits = slug.split('/')
                    if len(bits) > 1 and bits[-1] == 'index':
                        slug = '/'.join(bits[:-1])
                    meta = default_meta.copy()
                    #
                    html_path = self._html_path(base_html_path, slug)
                    meta.update({'group': group,
                                 'slug': slug})
                    if html_path is not None:
                        meta['path'] = html_path
                    data.append(reader.read(src, meta).tojson())
        return data