# Path: ringo/scripts/helpers.py
# def get_db(config_file):
#     result = run("sed -n 's|^sqlalchemy.url.*//.*@.*/\\([[:graph:]]*\\).*|\\1|p' %s" % config_file, hide="out")
#     return result.stdout.strip()
# 
# def get_package_name(config_file):
#     result = run("sed -n 's|^use = egg:\\([[:graph:]]*\\).*|\\1|p' %s | head -1" % config_file, hide="out")
#     return result.stdout.strip()
# 
# def get_fixtures(appname, path=None):
#     if path:
#         try:
#             result = run("ls %s/*.json" % path, hide="out").stdout.strip()
#         except Exception as e:
#             return []
#     else:
#         apppath = os.path.join(get_package_location(appname), appname)
#         result = run("ls %s/fixtures/*.json"
#                      % apppath, hide="out").stdout.strip()
#     fixtures = []
#     for fixture in sorted(result.split("\n")):
#         fixtures.append((fixture, "_".join(fixture.split("_")[1:]).split(".")[0]))
#     return fixtures


from invoke import run
from ringo.scripts.helpers import get_db, get_package_name, get_fixtures


def handle_fixture_load_command(args):
    appname = get_package_name(args.config)
    db = get_db(args.config)
    print "Loading data in %s" % db
    for fixture, modul in get_fixtures(args.app, args.path):
        print "Loading fixture %s" % fixture
        run("%s-admin db loaddata --loadbyid --config %s %s %s" % (appname, args.config, modul, fixture))


def handle_fixture_save_command(args):
    appname = get_package_name(args.config)

# Path: ringo/scripts/helpers.py
# def get_db(config_file):
#     result = run("sed -n 's|^sqlalchemy.url.*//.*@.*/\\([[:graph:]]*\\).*|\\1|p' %s" % config_file, hide="out")
#     return result.stdout.strip()
# 
# def get_package_name(config_file):
#     result = run("sed -n 's|^use = egg:\\([[:graph:]]*\\).*|\\1|p' %s | head -1" % config_file, hide="out")
#     return result.stdout.strip()
# 
# def get_fixtures(appname, path=None):
#     if path:
#         try:
#             result = run("ls %s/*.json" % path, hide="out").stdout.strip()
#         except Exception as e:
#             return []
#     else:
#         apppath = os.path.join(get_package_location(appname), appname)
#         result = run("ls %s/fixtures/*.json"
#                      % apppath, hide="out").stdout.strip()
#     fixtures = []
#     for fixture in sorted(result.split("\n")):
#         fixtures.append((fixture, "_".join(fixture.split("_")[1:]).split(".")[0]))
#     return fixtures


# Path: ringo/scripts/fixture.py
from invoke import run
from ringo.scripts.helpers import get_db, get_package_name, get_fixtures



def handle_fixture_load_command(args):
    appname = get_package_name(args.config)
    db = get_db(args.config)
    print "Loading data in %s" % db
    for fixture, modul in get_fixtures(args.app, args.path):
        print "Loading fixture %s" % fixture
        run("%s-admin db loaddata --loadbyid --config %s %s %s" % (appname, args.config, modul, fixture))


def handle_fixture_save_command(args):
    appname = get_package_name(args.config)

    db = get_db(args.config)