# Path: rophako/settings.py
# class ConfigHandler(object):
#     def load_settings(self):
#     def print_settings(self):
#     def load_plugins(self):
#     def __getattr__(self, section):
# 
# Path: rophako/utils.py
# def handle_exception(error):
#     """Send an e-mail to the site admin when an exception occurs."""
#     if current_app.config.get("DEBUG"):
#         print(traceback.format_exc())
#         raise
# 
#     import rophako.jsondb as JsonDB
# 
#     # Don't spam too many e-mails in a short time frame.
#     cache = JsonDB.get_cache("exception_catcher")
#     if cache:
#         last_exception = int(cache)
#         if int(time.time()) - last_exception < 120:
#             # Only one e-mail per 2 minutes, minimum
#             logger.error("RAPID EXCEPTIONS, DROPPING")
#             return
#     JsonDB.set_cache("exception_catcher", int(time.time()))
# 
#     username = "anonymous"
#     try:
#         if hasattr(g, "info") and "session" in g.info and "username" in g.info["session"]:
#             username = g.info["session"]["username"]
#     except:
#         pass
# 
#     # Get the timestamp.
#     timestamp = time.ctime(time.time())
# 
#     # Exception's traceback.
#     error = str(error.__class__.__name__) + ": " + str(error)
#     stacktrace = error + "\n\n" \
#         + "==== Start Traceback ====\n" \
#         + traceback.format_exc() \
#         + "==== End Traceback ====\n\n" \
#         + "Request Information\n" \
#         + "-------------------\n" \
#         + "Address: " + remote_addr() + "\n" \
#         + "User Agent: " + request.user_agent.string + "\n" \
#         + "Referrer: " + request.referrer
# 
#     # Construct the subject and message
#     subject = "Internal Server Error on {} - {} - {}".format(
#         Config.site.site_name,
#         username,
#         timestamp,
#     )
#     message = "{} has experienced an exception on the route: {}".format(
#         username,
#         request.path,
#     )
#     message += "\n\n" + stacktrace
# 
#     # Send the e-mail.
#     send_email(
#         to=Config.site.notify_address,
#         subject=subject,
#         message=message,
#     )
# 
# Path: rophako/log.py
# class LogHandler(logging.Handler):
#     def emit(self, record):


import codecs
import os
import os.path
import re
import redis
import json
import time
from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN
from rophako.settings import Config
from rophako.utils import handle_exception
from rophako.log import logger
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, absolute_import

"""JSON flat file database system."""



redis_client = None
cache_lifetime = 60*60 # 1 hour


def get(document, cache=True):
    """Get a specific document from the DB."""

# Path: rophako/settings.py
# class ConfigHandler(object):
#     def load_settings(self):
#     def print_settings(self):
#     def load_plugins(self):
#     def __getattr__(self, section):
# 
# Path: rophako/utils.py
# def handle_exception(error):
#     """Send an e-mail to the site admin when an exception occurs."""
#     if current_app.config.get("DEBUG"):
#         print(traceback.format_exc())
#         raise
# 
#     import rophako.jsondb as JsonDB
# 
#     # Don't spam too many e-mails in a short time frame.
#     cache = JsonDB.get_cache("exception_catcher")
#     if cache:
#         last_exception = int(cache)
#         if int(time.time()) - last_exception < 120:
#             # Only one e-mail per 2 minutes, minimum
#             logger.error("RAPID EXCEPTIONS, DROPPING")
#             return
#     JsonDB.set_cache("exception_catcher", int(time.time()))
# 
#     username = "anonymous"
#     try:
#         if hasattr(g, "info") and "session" in g.info and "username" in g.info["session"]:
#             username = g.info["session"]["username"]
#     except:
#         pass
# 
#     # Get the timestamp.
#     timestamp = time.ctime(time.time())
# 
#     # Exception's traceback.
#     error = str(error.__class__.__name__) + ": " + str(error)
#     stacktrace = error + "\n\n" \
#         + "==== Start Traceback ====\n" \
#         + traceback.format_exc() \
#         + "==== End Traceback ====\n\n" \
#         + "Request Information\n" \
#         + "-------------------\n" \
#         + "Address: " + remote_addr() + "\n" \
#         + "User Agent: " + request.user_agent.string + "\n" \
#         + "Referrer: " + request.referrer
# 
#     # Construct the subject and message
#     subject = "Internal Server Error on {} - {} - {}".format(
#         Config.site.site_name,
#         username,
#         timestamp,
#     )
#     message = "{} has experienced an exception on the route: {}".format(
#         username,
#         request.path,
#     )
#     message += "\n\n" + stacktrace
# 
#     # Send the e-mail.
#     send_email(
#         to=Config.site.notify_address,
#         subject=subject,
#         message=message,
#     )
# 
# Path: rophako/log.py
# class LogHandler(logging.Handler):
#     def emit(self, record):


# Path: rophako/jsondb.py
import codecs
import os
import os.path
import re
import redis
import json
import time
from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN
from rophako.settings import Config
from rophako.utils import handle_exception
from rophako.log import logger

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, absolute_import

"""JSON flat file database system."""



redis_client = None
cache_lifetime = 60*60 # 1 hour


def get(document, cache=True):
    """Get a specific document from the DB."""

    logger.debug("JsonDB: GET {}".format(document))