<commit_before>#!/usr/bin/env python2
# -*- coding:utf-8 -*-

"""render theme for sigal"""

import os
from distutils.dir_util import copy_tree
from jinja2 import Environment, PackageLoader
import sigal.image

DEFAULT_THEME = "default"
INDEX_PAGE = "index.html"
IGNORED_DIR = ['css', 'js', 'img']

class Theme():
    """ Generate html """

    def __init__(self, params, path, theme=DEFAULT_THEME, tpl=INDEX_PAGE):
        self.path = path
        self.bigimg = params.getint('sigal', 'big_img')
        self.bigimg_dir = params.get('sigal', 'bigimg_dir')
        self.thumb_dir = params.get('sigal', 'thumb_dir')
        self.thumb_prefix = params.get('sigal', 'thumb_prefix')
        self.fileExtList = params.get('sigal', 'fileExtList')

        if params.has_option('sigal', 'theme'):
            theme = params.get('sigal', 'theme')

        env = Environment(loader=PackageLoader('sigal',
                                               os.path.join('..', 'themes', theme)))
        self.theme_dir = os.path.join('themes', theme)
        self.template = env.get_template(tpl)

        self.meta = {}
        self.meta['title'] = params.get('album', 'title') \
                             if params.has_option('album', 'title') else ''
        self.meta['author'] = params.get('album', 'author') \
                              if params.has_option('album', 'author') else ''
        self.meta['description'] = params.get('album', 'description') \
                              if params.has_option('album', 'description') else ''


    def filelist(self):
        "get the list of directories with files of particular extensions"
        ignored = [self.thumb_dir, self.bigimg_dir] + IGNORED_DIR

        for dirpath, dirnames, filenames in os.walk(self.path):
            # filelist = [os.path.normcase(f) for f in os.listdir(dir)]
            if os.path.split(dirpath)[1] not in ignored:
                imglist = [f for f in filenames \
                           if os.path.splitext(f)[1] in self.fileExtList]
                dirlist = [d for d in dirnames if d not in ignored]
                yield dirpath, dirlist, imglist

    def find_representative(self, path):
        """ find the representative image for a given album/path
        at the moment, this is the first image found.
        """

        files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) \
                 and os.path.splitext(f)[1] in self.fileExtList]
        return files[0]

    def render(self):

        copy_tree(os.path.abspath(self.theme_dir),
                  os.path.abspath(self.path))<commit_msg>try to find a landscape image for the representative<commit_after>#!/usr/bin/env python2
# -*- coding:utf-8 -*-

"""render theme for sigal"""

import os
import Image
from distutils.dir_util import copy_tree
from jinja2 import Environment, PackageLoader
import sigal.image

DEFAULT_THEME = "default"
INDEX_PAGE = "index.html"
IGNORED_DIR = ['css', 'js', 'img']

class Theme():
    """ Generate html pages for each directory of images """

    def __init__(self, params, path, theme=DEFAULT_THEME, tpl=INDEX_PAGE):
        self.path = path
        self.bigimg = params.getint('sigal', 'big_img')
        self.bigimg_dir = params.get('sigal', 'bigimg_dir')
        self.thumb_dir = params.get('sigal', 'thumb_dir')
        self.thumb_prefix = params.get('sigal', 'thumb_prefix')
        self.fileExtList = params.get('sigal', 'fileExtList')

        if params.has_option('sigal', 'theme'):
            theme = params.get('sigal', 'theme')

        env = Environment(loader=PackageLoader('sigal',
                                               os.path.join('..', 'themes', theme)))
        self.theme_dir = os.path.join('themes', theme)
        self.template = env.get_template(tpl)

        self.meta = {}
        self.meta['title'] = params.get('album', 'title') \
                             if params.has_option('album', 'title') else ''
        self.meta['author'] = params.get('album', 'author') \
                              if params.has_option('album', 'author') else ''
        self.meta['description'] = params.get('album', 'description') \
                              if params.has_option('album', 'description') else ''


    def filelist(self):
        "get the list of directories with files of particular extensions"
        ignored = [self.thumb_dir, self.bigimg_dir] + IGNORED_DIR

        for dirpath, dirnames, filenames in os.walk(self.path):
            # filelist = [os.path.normcase(f) for f in os.listdir(dir)]
            if os.path.split(dirpath)[1] not in ignored:
                imglist = [f for f in filenames \
                           if os.path.splitext(f)[1] in self.fileExtList]
                dirlist = [d for d in dirnames if d not in ignored]
                yield dirpath, dirlist, imglist

    def find_representative(self, path):
        """ find the representative image for a given album/path
        at the moment, this is the first image found.
        """

        files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) \
                 and os.path.splitext(f)[1] in self.fileExtList]

        for f in files:
            # find and return the first landscape image
            im = Image.open(os.path.join(path, f))
            if im.size[0] > im.size[1]:
                return f

        # else simply return the 1st image
        return files[0]

    def render(self):

        copy_tree(os.path.abspath(self.theme_dir),
                  os.path.abspath(self.path))
{'fraction_non_alphanumeric': 0.13048166786484544, 'fraction_numerical': 0.003055355859094177, 'mean_word_length': 2.211194460473168, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '454178', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1648, 'n_words': 420}