import os
import logging
import shutil
import re
from grab.error import GrabError

logger = logging.getLogger('grab.script.start_project')


def setup_arg_parser(parser):
    parser.add_argument('project_name')
    parser.add_argument('--template')


def process_content(content, context):
    changed = False
    for key, value in context.items():
        re_macros = re.compile(r'\{\{\s*%s\s*\}\}' % re.escape(key))
        if re_macros.search(content):
            changed = True
            content = re_macros.sub(value, content)
    return changed, content


def process_file_path(path, context):
    for key, value in context.items():
        path = path.replace(key, value)
    return path


def underscore_to_camelcase(val):
    items = val.lower().split('_')
    return ''.join(x.title() for x in items)


def main(project_name, template, **kwargs):
    cur_dir = os.getcwd()
    project_dir = os.path.join(cur_dir, project_name)

    if template is None:
        grab_root = os.path.dirname(
            os.path.dirname(os.path.realpath(__file__)))
        template_path = os.path.join(grab_root, 'util/default_project')
    else:
        template_path = template

    if os.path.exists(project_dir):
        raise GrabError('Directory %s already exists' % project_dir)
    else:
        logger.debug('Copying %s to %s' % (template_path, project_dir))
        shutil.copytree(template_path, project_dir)

        project_name_camelcase = underscore_to_camelcase(project_name)
        context = {
            'PROJECT_NAME': project_name,
            'PROJECT_NAME_CAMELCASE': project_name_camelcase,
        }
        for base, dir_names, file_names in os.walk(project_dir):
            for file_name in file_names:
                if file_name.endswith('.py'):
                    file_path = os.path.join(base, file_name)
                    changed, content = process_content(open(file_path).read(),
                                                       context)
                    new_file_path = process_file_path(file_path, context)
                    if changed:
                        with open(new_file_path, 'w') as out:
                            out.write(content)
                        os.unlink(file_path)

@@ -287,28 +287,8 @@
 t):%0A
-    changed = False%0A
     
@@ -433,35 +433,8 @@
 t):%0A
-            changed = True%0A
     
@@ -491,25 +491,16 @@
   return
- changed,
  content
@@ -1710,16 +1710,72 @@
 '.py'):%0A
+                    print('Processing: %25s' %25 file_name)%0A
         
@@ -1851,17 +1851,8 @@
     
- changed,
  con
@@ -1897,16 +1897,25 @@
 .read(),
+ context)
 %0A       
@@ -1931,52 +1931,164 @@
     
-                                   
+new_file_path = process_file_path(file_path, context)%0A                    with open(new_file_path, 'w') as out:%0A                        out.write(
 conte
-x
+n
 t)%0A
+%0A
     
@@ -2095,36 +2095,35 @@
                 
-new_
+if 
 file_path = proc
@@ -2112,33 +2112,30 @@
 f file_path 
+!
 = 
-process
+new
 _file_path(f
@@ -2132,36 +2132,17 @@
 ile_path
-(file_path, context)
+:
 %0A       
@@ -2158,19 +2158,32 @@
     
-if changed:
+    os.unlink(file_path)
 %0A   
@@ -2207,92 +2207,65 @@
     
-with open(new_file_path, 'w') as out:%0A                            out.write(content)
+print('%25s: OK' %25 new_file_path)%0A                    else:
 %0A   
@@ -2281,33 +2281,40 @@
             
-os.unlink(
+print('%25s: OK' %25 
 file_path)%0A

Fix bug in start_project script