<s> from __future__ import absolute_import <EOL> import codecs <EOL> import jinja2 <EOL> import logging <EOL> import os <EOL> import sys <EOL> from distutils . dir_util import copy_tree <EOL> from jinja2 import Environment , FileSystemLoader , ChoiceLoader , PrefixLoader <EOL> from jinja2 . exceptions import TemplateNotFound <EOL> from . pkgmeta import __url__ as sigal_link <EOL> from . utils import url_from_path <EOL> THEMES_PATH = os . path . normpath ( os . path . join ( <EOL> os . path . abspath ( os . path . dirname ( __file__ ) ) , '<STR_LIT>' ) ) <EOL> class Writer ( object ) : <EOL> """<STR_LIT>""" <EOL> template_file = '<STR_LIT>' <EOL> def __init__ ( self , settings , index_title = '<STR_LIT>' ) : <EOL> self . settings = settings <EOL> self . output_dir = settings [ '<STR_LIT>' ] <EOL> self . theme = settings [ '<STR_LIT>' ] <EOL> self . index_title = index_title <EOL> self . logger = logging . getLogger ( __name__ ) <EOL> if not os . path . exists ( self . theme ) or not os . path . exists ( os . path . join ( self . theme , '<STR_LIT>' ) ) : <EOL> self . theme = os . path . join ( THEMES_PATH , self . theme ) <EOL> if not os . path . exists ( self . theme ) : <EOL> raise Exception ( "<STR_LIT>" % self . theme ) <EOL> self . logger . info ( "<STR_LIT>" , self . theme ) <EOL> theme_relpath = os . path . join ( self . theme , '<STR_LIT>' ) <EOL> default_loader = FileSystemLoader ( os . path . join ( THEMES_PATH , '<STR_LIT:default>' , <EOL> '<STR_LIT>' ) ) <EOL> env_options = { '<STR_LIT>' : True , '<STR_LIT>' : True } <EOL> try : <EOL> if tuple ( int ( x ) for x in jinja2 . __version__ . split ( '<STR_LIT:.>' ) ) >= ( <NUM_LIT:2> , <NUM_LIT:7> ) : <EOL> env_options [ '<STR_LIT>' ] = True <EOL> except ValueError : <EOL> pass <EOL> env = Environment ( <EOL> loader = ChoiceLoader ( [ <EOL> FileSystemLoader ( theme_relpath ) , <EOL> default_loader , <EOL> PrefixLoader ( { '<STR_LIT>' : default_loader } ) <EOL> ] ) , <EOL> ** env_options <EOL> ) <EOL> try : <EOL> self . template = env . get_template ( self . template_file ) <EOL> except TemplateNotFound : <EOL> self . logger . error ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self . theme_path = os . path . join ( self . output_dir , '<STR_LIT>' ) <EOL> copy_tree ( os . path . join ( self . theme , '<STR_LIT>' ) , self . theme_path ) <EOL> def generate_context ( self , album ) : <EOL> """<STR_LIT>""" <EOL> self . logger . info ( "<STR_LIT>" , album ) <EOL> return { <EOL> '<STR_LIT>' : album , <EOL> '<STR_LIT>' : self . index_title , <EOL> '<STR_LIT>' : self . settings , <EOL> '<STR_LIT>' : sigal_link , <EOL> '<STR_LIT>' : { '<STR_LIT:name>' : os . path . basename ( self . theme ) , <EOL> '<STR_LIT:url>' : url_from_path ( os . path . relpath ( self . theme_path , <EOL> album . dst_path ) ) } , <EOL> } <EOL> def write ( self , album ) : <EOL> """<STR_LIT>""" <EOL> page = self . template . render ( ** self . generate_context ( album ) ) <EOL> output_file = os . path . join ( album . dst_path , album . output_file ) <EOL> with codecs . open ( output_file , '<STR_LIT:w>' , '<STR_LIT:utf-8>' ) as f : <EOL> f . write ( page ) </s>
