<s> import os <EOL> import sys <EOL> import re <EOL> import json <EOL> from cement . utils import fs <EOL> from boss . core import exc as boss_exc <EOL> if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> : <EOL> from urllib2 import urlopen , HTTPError <EOL> from ConfigParser import RawConfigParser <EOL> input = raw_input <EOL> else : <EOL> from urllib . request import urlopen , HTTPError <EOL> from configparser import RawConfigParser <EOL> ALLOWED_STR_METHODS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:title>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class TemplateManager ( object ) : <EOL> def __init__ ( self , app , path ) : <EOL> self . app = app <EOL> self . basedir = fs . abspath ( path ) <EOL> self . config = self . _get_config ( ) <EOL> self . _word_map = dict ( ) <EOL> self . _vars = dict ( ) <EOL> def _get_config ( self ) : <EOL> config = None <EOL> possibles = [ <EOL> fs . abspath ( os . path . join ( self . basedir , '<STR_LIT>' ) ) , <EOL> fs . abspath ( os . path . join ( self . basedir , '<STR_LIT>' ) ) , <EOL> ] <EOL> for path in possibles : <EOL> if os . path . exists ( path ) : <EOL> if os . path . basename ( path ) == '<STR_LIT>' : <EOL> config = self . _get_json_config ( path ) <EOL> break <EOL> elif os . path . basename ( path ) == '<STR_LIT>' : <EOL> config = self . _get_yaml_config ( path ) <EOL> break <EOL> if not config : <EOL> raise boss_exc . BossTemplateError ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' not in config . keys ( ) : <EOL> config [ '<STR_LIT>' ] = ( '<STR_LIT:@>' , '<STR_LIT:@>' ) <EOL> config [ '<STR_LIT>' ] = config [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> config [ '<STR_LIT>' ] = config [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> else : <EOL> config [ '<STR_LIT>' ] = config [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> config [ '<STR_LIT>' ] = config [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> return config <EOL> def _get_json_config ( self , path ) : <EOL> full_path = fs . abspath ( path ) <EOL> if not os . path . exists ( full_path ) : <EOL> raise boss_exc . BossTemplateError ( "<STR_LIT>" ) <EOL> self . app . log . debug ( '<STR_LIT>' % full_path ) <EOL> import json <EOL> return json . load ( open ( full_path , '<STR_LIT:r>' ) ) <EOL> def _get_yaml_config ( self , path ) : <EOL> full_path = fs . abspath ( path ) <EOL> if not os . path . exists ( full_path ) : <EOL> raise boss_exc . BossTemplateError ( "<STR_LIT>" ) <EOL> self . app . log . debug ( '<STR_LIT>' % full_path ) <EOL> try : <EOL> import yaml <EOL> except ImportError as e : <EOL> raise boss_exc . BossRuntimeError ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> return yaml . load ( open ( full_path , '<STR_LIT:r>' ) ) <EOL> def _populate_vars ( self ) : <EOL> if '<STR_LIT>' in self . config . keys ( ) : <EOL> for var , question in self . config [ '<STR_LIT>' ] . items ( ) : <EOL> if self . app . pargs . defaults is True : <EOL> try : <EOL> res = self . app . config . get ( '<STR_LIT>' , var ) <EOL> except ConfigParser . NoOptionError as e : <EOL> res = "<STR_LIT>" <EOL> else : <EOL> if var . lower ( ) in self . app . config . keys ( '<STR_LIT>' ) : <EOL> default = self . app . config . get ( '<STR_LIT>' , var . lower ( ) ) <EOL> res = input ( "<STR_LIT>" % ( question , default ) ) <EOL> if len ( res ) == <NUM_LIT:0> : <EOL> res = default <EOL> else : <EOL> res = input ( "<STR_LIT>" % question ) <EOL> self . _vars [ var ] = res . strip ( ) <EOL> def _sub ( self , txt ) : <EOL> for line in str ( txt ) . split ( '<STR_LIT:\n>' ) : <EOL> for item in line . split ( '<STR_LIT:U+0020>' ) : <EOL> pattern = "<STR_LIT>" % ( <EOL> self . config [ '<STR_LIT>' ] , <EOL> self . config [ '<STR_LIT>' ] , <EOL> ) <EOL> if not re . match ( pattern , item ) : <EOL> continue <EOL> for key , value in sorted ( self . _vars . items ( ) ) : <EOL> if key in self . _word_map : <EOL> continue <EOL> pattern = "<STR_LIT>" % ( <EOL> self . config [ '<STR_LIT>' ] , <EOL> key , <EOL> self . config [ '<STR_LIT>' ] , <EOL> ) <EOL> m = re . match ( pattern , item ) <EOL> if m : <EOL> map_key = "<STR_LIT>" % ( <EOL> self . config [ '<STR_LIT>' ] , <EOL> m . group ( <NUM_LIT:2> ) , <EOL> self . config [ '<STR_LIT>' ] , <EOL> ) <EOL> self . _word_map [ map_key ] = str ( value ) <EOL> else : <EOL> pattern = "<STR_LIT>" % ( <EOL> self . config [ '<STR_LIT>' ] , <EOL> key , <EOL> self . config [ '<STR_LIT>' ] , <EOL> ) <EOL> m = re . match ( pattern , item ) <EOL> if m : <EOL> if len ( m . group ( <NUM_LIT:3> ) ) > <NUM_LIT:0> : <EOL> if m . group ( <NUM_LIT:3> ) in ALLOWED_STR_METHODS : <EOL> fixed = str ( getattr ( value , m . group ( <NUM_LIT:3> ) ) ( ) ) <EOL> else : <EOL> self . app . log . debug ( "<STR_LIT>" % m . group ( <NUM_LIT:3> ) ) <EOL> fixed = str ( value ) <EOL> else : <EOL> fixed = str ( value ) <EOL> new_key = "<STR_LIT>" % ( <EOL> self . config [ '<STR_LIT>' ] , <EOL> m . group ( <NUM_LIT:2> ) , <EOL> m . group ( <NUM_LIT:3> ) , <EOL> self . config [ '<STR_LIT>' ] , <EOL> ) <EOL> self . _word_map [ new_key ] = fixed <EOL> new_txt = str ( txt ) <EOL> for pattern , replacement in sorted ( self . _word_map . items ( ) ) : <EOL> new_txt = re . sub ( pattern , replacement , new_txt ) <EOL> return new_txt <EOL> def _sub_or_pass ( self , path , data ) : <EOL> if '<STR_LIT>' in self . config . keys ( ) : <EOL> for pattern in self . config [ '<STR_LIT>' ] : <EOL> if re . match ( pattern , path ) : <EOL> self . app . log . debug ( <EOL> "<STR_LIT>" % path <EOL> ) <EOL> return data <EOL> return self . _sub ( data ) <EOL> def _inject ( self , dest_path ) : <EOL> new_data = '<STR_LIT>' <EOL> write_it = False <EOL> f = open ( dest_path , '<STR_LIT:r>' ) <EOL> line_num = <NUM_LIT:0> <EOL> for line in f . readlines ( ) : <EOL> line_num = line_num + <NUM_LIT:1> <EOL> for inj , inj_data in self . config [ '<STR_LIT>' ] . items ( ) : <EOL> pattern = '<STR_LIT>' % ( <EOL> self . config [ '<STR_LIT>' ] , <EOL> inj , <EOL> self . config [ '<STR_LIT>' ] , <EOL> ) <EOL> m = re . match ( pattern , line ) <EOL> if m : <EOL> print ( "<STR_LIT>" % ( inj , dest_path , line_num ) ) <EOL> line = line + "<STR_LIT>" % self . _sub ( inj_data ) <EOL> write_it = True <EOL> break <EOL> new_data = new_data + line <EOL> f . close ( ) <EOL> if write_it : <EOL> fs . backup ( dest_path , suffix = '<STR_LIT>' ) <EOL> self . _write_file ( dest_path , new_data , overwrite = True ) <EOL> def _inject_or_pass ( self , path ) : <EOL> if '<STR_LIT>' in self . config . keys ( ) : <EOL> for pattern in self . config [ '<STR_LIT>' ] : <EOL> if re . match ( pattern , path ) : <EOL> self . app . log . debug ( <EOL> "<STR_LIT>" % path <EOL> ) <EOL> return False <EOL> return self . _inject ( path ) <EOL> return True <EOL> def _copy_path ( self , tmpl_path , dest_path ) : <EOL> f = open ( fs . abspath ( tmpl_path ) , '<STR_LIT:r>' ) <EOL> data = f . read ( ) <EOL> f . close ( ) <EOL> dest_path = self . _sub ( fs . abspath ( dest_path ) ) <EOL> dest_data = self . _sub_or_pass ( tmpl_path , data ) <EOL> self . _write_file ( dest_path , dest_data ) <EOL> def _write_file ( self , dest_path , data , overwrite = False ) : <EOL> if os . path . exists ( dest_path ) and overwrite == False : <EOL> self . app . log . warn ( '<STR_LIT>' % dest_path ) <EOL> return False <EOL> self . app . log . info ( '<STR_LIT>' % dest_path ) <EOL> if not os . path . exists ( os . path . dirname ( dest_path ) ) : <EOL> os . makedirs ( os . path . dirname ( dest_path ) ) <EOL> f = open ( dest_path , '<STR_LIT:w>' ) <EOL> f . write ( data ) <EOL> f . close ( ) <EOL> return True <EOL> def _walk_path ( self , path ) : <EOL> for items in os . walk ( fs . abspath ( path ) ) : <EOL> for _file in items [ <NUM_LIT:2> ] : <EOL> if _file == '<STR_LIT>' : <EOL> continue <EOL> elif re . match ( '<STR_LIT>' , _file ) : <EOL> continue <EOL> else : <EOL> yield fs . abspath ( os . path . join ( items [ <NUM_LIT:0> ] , _file ) ) <EOL> def copy ( self , dest_basedir ) : <EOL> self . _populate_vars ( ) <EOL> dest_basedir = fs . abspath ( dest_basedir ) <EOL> for tmpl_path in self . _walk_path ( self . basedir ) : <EOL> dest_path = fs . abspath ( re . sub ( self . basedir , dest_basedir , tmpl_path ) ) <EOL> self . _copy_path ( tmpl_path , dest_path ) <EOL> if '<STR_LIT>' in self . config . keys ( ) : <EOL> for _file , remote_uri in self . config [ '<STR_LIT>' ] . items ( ) : <EOL> dest_path = self . _sub ( os . path . join ( dest_basedir , _file ) ) <EOL> remote_uri = self . _sub ( remote_uri ) <EOL> try : <EOL> data = self . _sub ( urlopen ( remote_uri ) . read ( ) ) <EOL> except HTTPError as e : <EOL> data = '<STR_LIT>' <EOL> self . _write_file ( dest_path , data ) <EOL> if '<STR_LIT>' in self . config . keys ( ) and len ( self . config [ '<STR_LIT>' ] ) > <NUM_LIT:0> : <EOL> for dest_path in self . _walk_path ( dest_basedir ) : <EOL> self . _inject_or_pass ( dest_path ) </s>
