# Path: chromabot/config.py
# class Config(object):
# 
#     def __init__(self, conffile=None):
#         # Try to locate our configuration directory
# 
#         proposals = [conffile, os.environ.get("CHROMABOT_CONFIG"),
#                     "../config/config.json", "./config/config.json",
#                     "/etc/chromabot/config.json"]
# 
#         if not self.check_exist(proposals):
#             logging.error("Could not locate config file!")
#             raise SystemExit
# 
#         self.refresh()
# 
#     def __getitem__(self, key):
#         return self.data[key]
# 
#     def __getattr__(self, key):
#         return self.data[key]
# 
#     def check_exist(self, proposed_paths):
#         for fullpath in proposed_paths:
#             if fullpath and os.path.exists(fullpath):
#                 self.conffile = fullpath
#                 return True
#         return False
# 
#     def praw(self):
#         """Return a praw.Reddit object configured according to this config"""
#         ua = self.data["bot"]["useragent"]
#         site = self.data["bot"].get('site')
# 
#         result = praw.Reddit(user_agent=ua, site_name=site)
#         return result
# 
#     def refresh(self):
#         with open(self.conffile) as data_file:
#             self.data = json.loads(data_file.read())
# 
#     # Useful properties follow
#     @property
#     def dbstring(self):
#         return self.data["db"]["connection"]
# 
#     @property
#     def game(self):
#         return self.data["game"]
# 
#     @property
#     def headquarters(self):
#         return self.data["bot"]["hq_sub"]
# 
#     @property
#     def password(self):
#         return self.data["bot"]["password"]
# 
#     @property
#     def username(self):
#         return self.data["bot"]["username"]


import sys
from collections import defaultdict
from chromabot.config import Config
from chromabot.db import *
sys.path.append("./chromabot")



def main():
    config = Config()
    dbconn = DB(config)
    sess = dbconn.session()

    source = sys.argv[1]

    with open(source) as srcfile:
        unconverted = json.load(srcfile)

    for region_dict in unconverted:
        name = region_dict['name'].lower()
        region = sess.query(Region).filter_by(name=name).first()
        print "%s" % name
        aliases = region_dict.get("aliases", [])

        for alias in aliases:
            alias = alias.lower()
            prev = sess.query(Alias).filter_by(name=alias).first()
            if prev:
                print "\t[old] %s" % prev.name
            else:
                new_alias = region.create_alias(alias)
                print "\t[NEW] %s" % new_alias.name

if __name__ == '__main__':

# Path: chromabot/config.py
# class Config(object):
# 
#     def __init__(self, conffile=None):
#         # Try to locate our configuration directory
# 
#         proposals = [conffile, os.environ.get("CHROMABOT_CONFIG"),
#                     "../config/config.json", "./config/config.json",
#                     "/etc/chromabot/config.json"]
# 
#         if not self.check_exist(proposals):
#             logging.error("Could not locate config file!")
#             raise SystemExit
# 
#         self.refresh()
# 
#     def __getitem__(self, key):
#         return self.data[key]
# 
#     def __getattr__(self, key):
#         return self.data[key]
# 
#     def check_exist(self, proposed_paths):
#         for fullpath in proposed_paths:
#             if fullpath and os.path.exists(fullpath):
#                 self.conffile = fullpath
#                 return True
#         return False
# 
#     def praw(self):
#         """Return a praw.Reddit object configured according to this config"""
#         ua = self.data["bot"]["useragent"]
#         site = self.data["bot"].get('site')
# 
#         result = praw.Reddit(user_agent=ua, site_name=site)
#         return result
# 
#     def refresh(self):
#         with open(self.conffile) as data_file:
#             self.data = json.loads(data_file.read())
# 
#     # Useful properties follow
#     @property
#     def dbstring(self):
#         return self.data["db"]["connection"]
# 
#     @property
#     def game(self):
#         return self.data["game"]
# 
#     @property
#     def headquarters(self):
#         return self.data["bot"]["hq_sub"]
# 
#     @property
#     def password(self):
#         return self.data["bot"]["password"]
# 
#     @property
#     def username(self):
#         return self.data["bot"]["username"]


# Path: bin/realias.py
import sys
from collections import defaultdict
from chromabot.config import Config
from chromabot.db import *

sys.path.append("./chromabot")



def main():
    config = Config()
    dbconn = DB(config)
    sess = dbconn.session()

    source = sys.argv[1]

    with open(source) as srcfile:
        unconverted = json.load(srcfile)

    for region_dict in unconverted:
        name = region_dict['name'].lower()
        region = sess.query(Region).filter_by(name=name).first()
        print "%s" % name
        aliases = region_dict.get("aliases", [])

        for alias in aliases:
            alias = alias.lower()
            prev = sess.query(Alias).filter_by(name=alias).first()
            if prev:
                print "\t[old] %s" % prev.name
            else:
                new_alias = region.create_alias(alias)
                print "\t[NEW] %s" % new_alias.name

if __name__ == '__main__':

    main()