# Path: shimehari/core/manage/AbstractCommand.py
# class CreatableCommand(AbstractCommand):
# 
#     u"""-----------------------------
#         ::pkg:: Shimehari.core.manage.AbstractCommand.CreatableCommand
#         toWritable
#         ~~~~~~~~~~
# 
#         ファイルを書き込み可能状態にします。
#         [args]
#             :filename 対象ファイル名
#     ------------------------------"""
#     def toWritable(self, filename):
#         if sys.platform.startswith('java'):
#             return
# 
#         if not os.access(filename, os.W_OK):
#             st = os.stat(filename)
#             # Undefined name "start"
#             newPermission = stat.S_IMODE(st.st_mode)
#             os.chmod(filename, newPermission)
# 
# Path: shimehari/core/helpers.py
# def importFromString(targetName):
#     if isinstance(targetName, unicode):
#         targetName = str(targetName)
#     try:
#         if '.' in targetName:
#             pkgs, obj = targetName.rsplit('.', 1)
#         else:
#             return __import__('config')
#         try:
#             return getattr(__import__(pkgs, fromlist=[obj]), obj)
#         except (ImportError, AttributeError):
#             pkgName = pkgs + '.' + obj
#             __import__(pkgName)
#             return sys.modules[pkgName]
# 
#     except ImportError, error:
#         raise ImportError(error)
# 
# Path: shimehari/core/exceptions.py
# class CommandError(Exception):
#     def __init__(self, description):
#         self.description = description
# 
#     def __str__(self):
#         return 'CommandError: %s' % self.description


import os
import sys
import errno
import shutil
import shimehari
from optparse import make_option
from shimehari.core.manage import CreatableCommand
from shimehari.core.helpers import importFromString
from shimehari.core.exceptions import CommandError

u"""
===============================
    ::pkg:: Shimehari.core.manage.commands.create
    Command
    ~~~~~~~
    コマンドの実装

===============================
"""


class Command(CreatableCommand):
    name = 'create'
    summary = 'Create Shimehari Application'
    usage = "Usage: %prog APPLICATION_NAME [OPTIONS]"
    option_list = CreatableCommand.option_list + (
        make_option('--path', '-p', action='store', type='string', dest='path', help='target create path'),
        make_option('--template', '-t', action='store', type='string', dest='template', help='using project tempalte')
    )

    def __init__(self):
        super(Command, self).__init__()

    def handle(self, appDir='app', *args, **options):
        try:
            importFromString(appDir)
        except ImportError:
            pass
        else:

# Path: shimehari/core/manage/AbstractCommand.py
# class CreatableCommand(AbstractCommand):
# 
#     u"""-----------------------------
#         ::pkg:: Shimehari.core.manage.AbstractCommand.CreatableCommand
#         toWritable
#         ~~~~~~~~~~
# 
#         ファイルを書き込み可能状態にします。
#         [args]
#             :filename 対象ファイル名
#     ------------------------------"""
#     def toWritable(self, filename):
#         if sys.platform.startswith('java'):
#             return
# 
#         if not os.access(filename, os.W_OK):
#             st = os.stat(filename)
#             # Undefined name "start"
#             newPermission = stat.S_IMODE(st.st_mode)
#             os.chmod(filename, newPermission)
# 
# Path: shimehari/core/helpers.py
# def importFromString(targetName):
#     if isinstance(targetName, unicode):
#         targetName = str(targetName)
#     try:
#         if '.' in targetName:
#             pkgs, obj = targetName.rsplit('.', 1)
#         else:
#             return __import__('config')
#         try:
#             return getattr(__import__(pkgs, fromlist=[obj]), obj)
#         except (ImportError, AttributeError):
#             pkgName = pkgs + '.' + obj
#             __import__(pkgName)
#             return sys.modules[pkgName]
# 
#     except ImportError, error:
#         raise ImportError(error)
# 
# Path: shimehari/core/exceptions.py
# class CommandError(Exception):
#     def __init__(self, description):
#         self.description = description
# 
#     def __str__(self):
#         return 'CommandError: %s' % self.description


# Path: shimehari/core/manage/commands/create.py
import os
import sys
import errno
import shutil
import shimehari
from optparse import make_option
from shimehari.core.manage import CreatableCommand
from shimehari.core.helpers import importFromString
from shimehari.core.exceptions import CommandError


u"""
===============================
    ::pkg:: Shimehari.core.manage.commands.create
    Command
    ~~~~~~~
    コマンドの実装

===============================
"""


class Command(CreatableCommand):
    name = 'create'
    summary = 'Create Shimehari Application'
    usage = "Usage: %prog APPLICATION_NAME [OPTIONS]"
    option_list = CreatableCommand.option_list + (
        make_option('--path', '-p', action='store', type='string', dest='path', help='target create path'),
        make_option('--template', '-t', action='store', type='string', dest='template', help='using project tempalte')
    )

    def __init__(self):
        super(Command, self).__init__()

    def handle(self, appDir='app', *args, **options):
        try:
            importFromString(appDir)
        except ImportError:
            pass
        else:

            raise CommandError('%s mou aru' % appDir)