#NEWLINE# def generate_action( args ) : #NEWLINE# #INDENT# 'Generate action.' #NEWLINE# controller = args . get ( '<controller>' ) #NEWLINE# action = args . get ( '<action>' ) #NEWLINE# with_template = args . get ( '-t' ) #NEWLINE# current_path = os . getcwd ( ) #NEWLINE# logger . info ( 'Start generating action.' ) #NEWLINE# controller_file_path = os . path . join ( current_path , 'application/controllers' , ( controller + '.py' ) ) #NEWLINE# if ( not os . path . exists ( controller_file_path ) ) : #NEWLINE# #INDENT# logger . warning ( ( "The controller %s does't exist." % controller ) ) #NEWLINE# return #NEWLINE# #UNINDENT# if with_template : #NEWLINE# #INDENT# action_source_path = os . path . join ( dirname ( abspath ( __file__ ) ) , 'templates/action.py' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# action_source_path = os . path . join ( dirname ( abspath ( __file__ ) ) , 'templates/action_without_template.py' ) #NEWLINE# #UNINDENT# with open ( action_source_path , 'r' ) as action_source_file : #NEWLINE# #INDENT# with open ( controller_file_path , 'a' ) as controller_file : #NEWLINE# #INDENT# for action_line in action_source_file : #NEWLINE# #INDENT# new_line = action_line . replace ( '#{controller}' , controller ) . replace ( '#{action}' , action ) #NEWLINE# controller_file . write ( new_line ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# logger . info ( ( 'Updated: %s' % _relative_path ( controller_file_path ) ) ) #NEWLINE# if with_template : #NEWLINE# #INDENT# assets_dir_path = os . path . join ( current_path , ( 'application/pages/%s/%s' % ( controller , action ) ) ) #NEWLINE# _mkdir_p ( assets_dir_path ) #NEWLINE# action_html_template_path = os . path . join ( dirname ( abspath ( __file__ ) ) , 'templates/action.html' ) #NEWLINE# action_html_path = os . path . join ( assets_dir_path , ( '%s.html' % action ) ) #NEWLINE# with open ( action_html_template_path , 'r' ) as action_html_template_file : #NEWLINE# #INDENT# with open ( action_html_path , 'w' ) as action_html_file : #NEWLINE# #INDENT# for line in action_html_template_file : #NEWLINE# #INDENT# new_line = line . replace ( '#{action}' , action ) . replace ( '#{action|title}' , action . title ( ) ) . replace ( '#{controller}' , controller ) #NEWLINE# action_html_file . write ( new_line ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# logger . info ( ( 'New: %s' % _relative_path ( action_html_path ) ) ) #NEWLINE# action_js_template_path = os . path . join ( dirname ( abspath ( __file__ ) ) , 'templates/action.js' ) #NEWLINE# action_js_path = os . path . join ( assets_dir_path , ( '%s.js' % action ) ) #NEWLINE# shutil . copy ( action_js_template_path , action_js_path ) #NEWLINE# logger . info ( ( 'New: %s' % _relative_path ( action_js_path ) ) ) #NEWLINE# action_less_template_path = os . path . join ( dirname ( abspath ( __file__ ) ) , 'templates/action.less' ) #NEWLINE# action_less_path = os . path . join ( assets_dir_path , ( '%s.less' % action ) ) #NEWLINE# shutil . copy ( action_less_template_path , action_less_path ) #NEWLINE# logger . info ( ( 'New: %s' % _relative_path ( action_less_path ) ) ) #NEWLINE# #UNINDENT# logger . info ( 'Finish generating action.' )