<s> """<STR_LIT>""" <EOL> import os <EOL> from io import open <EOL> import shutil <EOL> import errno <EOL> import re <EOL> import yaml <EOL> from drupan . entity import Entity <EOL> class Reader ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , site , config ) : <EOL> """<STR_LIT>""" <EOL> self . site = site <EOL> self . config = config <EOL> self . content = config . get_option ( "<STR_LIT>" , "<STR_LIT:content>" , optional = True ) <EOL> self . extension = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . template = config . get_option ( "<STR_LIT>" , "<STR_LIT>" , optional = True ) <EOL> if self . content is None : <EOL> self . content = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> if self . template is None : <EOL> self . template = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" ) <EOL> if not self . extension . startswith ( "<STR_LIT:.>" ) : <EOL> self . extension = "<STR_LIT>" . format ( self . extension ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . read_content ( ) <EOL> self . read_template ( ) <EOL> self . read_images ( ) <EOL> def read_content ( self ) : <EOL> """<STR_LIT>""" <EOL> for current in os . listdir ( self . content ) : <EOL> if not os . path . splitext ( current ) [ <NUM_LIT:1> ] == self . extension : <EOL> continue <EOL> fqp = os . path . join ( self . content , current ) <EOL> with open ( fqp , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as infile : <EOL> self . parse_file ( infile . read ( ) ) <EOL> def parse_file ( self , raw ) : <EOL> """<STR_LIT>""" <EOL> ( header , separator , content ) = raw . partition ( "<STR_LIT>" ) <EOL> meta = yaml . load ( header ) <EOL> entity = Entity ( self . config ) <EOL> entity . meta = meta <EOL> entity . raw = content . strip ( ) <EOL> self . site . entities . append ( entity ) <EOL> def read_template ( self ) : <EOL> """<STR_LIT>""" <EOL> template_extensions = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> for file_names , dir_name in dir_walker ( self . template ) : <EOL> for name in file_names : <EOL> if name . startswith ( "<STR_LIT:.>" ) : <EOL> continue <EOL> store = self . site . templates <EOL> mode = "<STR_LIT:r>" <EOL> if not name . endswith ( template_extensions ) : <EOL> store = self . site . assets <EOL> mode = "<STR_LIT:rb>" <EOL> stripped = re . sub ( self . template , "<STR_LIT>" , dir_name ) <EOL> if stripped . startswith ( os . path . sep ) : <EOL> stripped = stripped [ <NUM_LIT:1> : ] <EOL> fqp = os . path . join ( dir_name , name ) <EOL> key = os . path . join ( stripped , name ) <EOL> with open ( fqp , mode ) as infile : <EOL> store [ key ] = infile . read ( ) <EOL> def read_images ( self ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . join ( self . content , "<STR_LIT>" ) <EOL> for file_names , dir_name in dir_walker ( path ) : <EOL> for name in file_names : <EOL> if name . startswith ( "<STR_LIT:.>" ) : <EOL> continue <EOL> with open ( os . path . join ( dir_name , name ) , "<STR_LIT:rb>" ) as infile : <EOL> self . site . images [ name ] = infile . read ( ) <EOL> class Writer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , site , config ) : <EOL> """<STR_LIT>""" <EOL> self . site = site <EOL> self . config = config <EOL> self . base_path = config . get_option ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . clean_dir ( ) <EOL> self . write_entities ( ) <EOL> self . write_assets ( ) <EOL> self . write_templates ( ) <EOL> def write_entities ( self ) : <EOL> """<STR_LIT>""" <EOL> for entity in self . site . entities : <EOL> if entity . path is None : <EOL> continue <EOL> path = os . path . join ( self . base_path , entity . path ) <EOL> create_path ( path ) <EOL> path = os . path . join ( path , "<STR_LIT>" ) <EOL> write ( entity . rendered , path ) <EOL> self . write_images ( entity ) <EOL> def write_assets ( self ) : <EOL> """<STR_LIT>""" <EOL> for name , content in self . site . assets . items ( ) : <EOL> path = os . path . join ( self . base_path , os . path . dirname ( name ) ) <EOL> create_path ( path ) <EOL> path = os . path . join ( self . base_path , name ) <EOL> write ( content , path , binary = True ) <EOL> def write_templates ( self ) : <EOL> """<STR_LIT>""" <EOL> for name , content in self . site . templates . items ( ) : <EOL> if name . startswith ( "<STR_LIT:_>" ) : <EOL> continue <EOL> path = os . path . join ( self . base_path , os . path . dirname ( name ) ) <EOL> create_path ( path ) <EOL> path = os . path . join ( self . base_path , name ) <EOL> write ( content , path ) <EOL> def write_images ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . join ( self . base_path , entity . path ) <EOL> for name in entity . images : <EOL> image = self . site . images [ name ] <EOL> output = os . path . join ( path , name ) <EOL> write ( image , output , binary = True ) <EOL> def clean_dir ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> shutil . rmtree ( self . base_path ) <EOL> except : <EOL> pass <EOL> def dir_walker ( path ) : <EOL> """<STR_LIT>""" <EOL> for dir_name , dir_names , file_names in os . walk ( path ) : <EOL> skip = False <EOL> for name in dir_name . split ( os . path . sep ) : <EOL> if name . startswith ( "<STR_LIT:.>" ) : <EOL> skip = True <EOL> if skip is True : <EOL> continue <EOL> yield ( file_names , dir_name ) <EOL> def create_path ( path ) : <EOL> """<STR_LIT>""" <EOL> if path == "<STR_LIT>" : <EOL> return <EOL> if os . path . exists ( path ) : <EOL> return <EOL> try : <EOL> os . makedirs ( path ) <EOL> except OSError : <EOL> if OSError . errno == errno . EEXIST : <EOL> pass <EOL> else : <EOL> raise <EOL> def write ( content , path , binary = False ) : <EOL> """<STR_LIT>""" <EOL> if binary : <EOL> with open ( path , "<STR_LIT:wb>" ) as output : <EOL> output . write ( content ) <EOL> else : <EOL> with open ( path , "<STR_LIT:w>" , encoding = "<STR_LIT:utf-8>" ) as output : <EOL> output . write ( content ) </s>
