Skip to content

Commit

Permalink
#23: Terminal colored output.
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermechapiewski committed May 1, 2009
1 parent beb20d0 commit 2400c17
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
21 changes: 16 additions & 5 deletions src/simple_db_migrate/cli.py
Expand Up @@ -3,6 +3,17 @@

class CLI(object):

color = {
"PINK": "\033[95m",
"BLUE": "\033[94m",
"CYAN": "\033[96m",
"GREEN": "\033[92m",
"YELLOW": "\033[93m",
"RED": "\033[91m",
"GRAY": "\033[90m",
"END": "\033[0m",
}

def __init__(self):
self.__config_parser()

Expand Down Expand Up @@ -49,12 +60,12 @@ def parse(self):
return self.__parser.parse_args()

def error_and_exit(self, msg):
print "[ERROR] %s\n" % (msg)
self.msg("[ERROR] %s\n" % msg, "RED")
sys.exit(1)

def info_and_exit(self, msg):
print "%s\n" % (msg)
self.msg("%s\n" % msg, "BLUE")
sys.exit(0)
def msg(self, msg):
print msg

def msg(self, msg, color="CYAN"):
print "%s%s%s" % (self.color[color], msg, self.color["END"])
4 changes: 2 additions & 2 deletions src/simple_db_migrate/core.py
Expand Up @@ -115,8 +115,8 @@ def get_migration_version(self, sql_file):

def check_if_version_exists(self, version):
files = self.get_all_migration_files()
for f in files:
if f.startswith(version):
for file_name in files:
if file_name[0:14] == version:
return True
return False

Expand Down
20 changes: 10 additions & 10 deletions src/simple_db_migrate/main.py
Expand Up @@ -19,12 +19,12 @@ def __init__(self, config=None, mysql=None, db_migrate=None):
self.__db_migrate = Migrations(config)

def execute(self):
self.__cli.msg("\nStarting DB migration...")
self.__cli.msg("\nStarting DB migration...", "PINK")
if self.__config.get("new_migration"):
self._create_migration()
else:
self._migrate()
self.__cli.msg("\nDone.\n")
self.__cli.msg("\nDone.\n", "PINK")

def _create_migration(self):
new_file = self.__db_migrate.create_migration(self.__config.get("new_migration"))
Expand All @@ -34,8 +34,8 @@ def _migrate(self):
destination_version = self._get_destination_version()
current_version = self.__mysql.get_current_schema_version()

self.__cli.msg("- Current version is: %s" % current_version)
self.__cli.msg("- Destination version is: %s" % destination_version)
self.__cli.msg("- Current version is: %s" % current_version, "GREEN")
self.__cli.msg("- Destination version is: %s" % destination_version, "GREEN")

# if current and destination versions are the same,
# will consider a migration up to execute remaining files
Expand Down Expand Up @@ -79,16 +79,16 @@ def _execute_migrations(self, current_version, destination_version, is_migration
versions_to_be_executed = self._get_migration_files_to_be_executed(current_version, destination_version)

if versions_to_be_executed is None or len(versions_to_be_executed) == 0:
self.__cli.msg("\nNothing to do.\n")
self.__cli.msg("\nNothing to do.\n", "PINK")
sys.exit(0)

up_down_label = "up" if is_migration_up else "down"
if self.__config.get("show_sql_only"):
self.__cli.msg("\nWARNING: database migrations are not being executed ('--showsqlonly' activated)")
self.__cli.msg("WARNING: database migrations are not being executed ('--showsqlonly' activated)", "YELLOW")
else:
self.__cli.msg("\nStarting migration %s!" % up_down_label)

self.__cli.msg("*** versions: %s\n" % versions_to_be_executed)
self.__cli.msg("*** versions: %s\n" % versions_to_be_executed, "GRAY")

sql_statements_executed = ""
for migration_version in versions_to_be_executed:
Expand All @@ -103,7 +103,7 @@ def _execute_migrations(self, current_version, destination_version, is_migration
sql_statements_executed += sql

if self.__config.get("show_sql") or self.__config.get("show_sql_only"):
self.__cli.msg("__________ SQL statements executed __________")
self.__cli.msg(sql_statements_executed)
self.__cli.msg("_____________________________________________")
self.__cli.msg("__________ SQL statements executed __________", "YELLOW")
self.__cli.msg(sql_statements_executed, "YELLOW")
self.__cli.msg("_____________________________________________", "YELLOW")

0 comments on commit 2400c17

Please sign in to comment.