# Path: static_aid/config.py
# ROOT = realpath(curdir)
# CONFIG_DEFAULTS_FILE_PATH = join(ROOT, 'local_settings.default')
#     ROOT = realpath(join(dirname(__file__), '..'))
#     CONFIG_DEFAULTS_FILE_PATH = join(ROOT, 'local_settings.default')
# CONFIG_FILE_PATH = join(ROOT, 'local_settings.cfg')
# SAMPLE_DATA_DIR = join(ROOT, 'data')
# SITE_SRC_DIR = join(ROOT, 'site')
# BUILD_DIR = join(ROOT, 'build')
# DATA_DIR = join(BUILD_DIR, 'data')
# STAGING_DIR = join(BUILD_DIR, 'staging')
# RAW_DATA_DIR = join(BUILD_DIR, 'raw')
# SITE_BUILD_DIR = join(BUILD_DIR, 'site')  # must match 'dest' settings in Gruntfile.js
# TEMP_DIR = join(BUILD_DIR, 'tmp')
# PID_FILE_PATH = join(TEMP_DIR, 'daemon.pid')
# OBJECT_CACHE_DIR = join(TEMP_DIR, 'object_cache')
# ROW_FETCH_LIMIT = 100
# def _configSection(section):
# def _stringToBoolean(string):
# def _stringToList(string):
# 
# Path: static_aid/DataExtractor.py
# class DataExtractor(object):
# 
#     def __init__(self, update=False):
#         if self.is_running():
#             logging.error('Process already running, exiting')
#             exit()
#         self.register_pid()
#         self.update = update
# 
#     def run(self):
#         logging.info('\n*** Export started ***')
# 
#         start_time = int(time())
#         utils.remove_file_or_dir(config.DATA_DIR)
#         self._run()
#         self.set_last_export_time(start_time)
# 
#         logging.info('*** Export completed ***')
#         utils.remove_file_or_dir(config.PID_FILE_PATH)
# 
#     def _run(self):
#         """Override this method in each extractor subclass."""
#         raise Exception('override this method for each DataExtractor subclass')
# 
#     def is_running(self):
#         """Determines whether or not the staticAid process is running."""
#         if isfile(config.PID_FILE_PATH):
#             pidfile = open(config.PID_FILE_PATH, "r")
#             for line in pidfile:
#                 pid = int(line.strip())
#                 if pid_exists(pid):
#                     return True
#         return False
# 
#     def register_pid(self):
#         """Registers the PID of the current process."""
#         if not exists(dirname(config.PID_FILE_PATH)):
#             makedirs(dirname(config.PID_FILE_PATH))
#         currentPid = str(getpid())
#         with open(config.PID_FILE_PATH, 'w') as pf:
#             pf.write(currentPid)
# 
#     def make_destinations(self):
#         """Creates destination directories for data files."""
#         for k in config.destinations:
#             path = join(config.DATA_DIR, config.destinations[k])
#             if not exists(path):
#                 makedirs(path)
# 
#     def get_last_export_time(self):
#         """Returns last export time in Unix epoch time, for example 1439563523."""
#         if self.update:
#             try:
#                 with open(config.lastExportFilepath, 'rb') as pickle_handle:
#                     return int(str(pickle.load(pickle_handle)))
#             except:
#                 pass
#         return 0
# 
#     def set_last_export_time(self, start_time):
#         """Store the current time in Unix epoch time, for example 1439563523."""
#         with open(config.lastExportFilepath, 'wb') as pickle_handle:
#             pickle.dump(start_time, pickle_handle)
#         logging.info('Last export time updated to {}'.format(start_time))
# 
#     def remove_data_file(self, identifier, destination):
#         """If a JSON file exists, deletes it."""
#         filename = join(destination, '%s.json' % str(identifier))
#         utils.remove_file_or_dir(filename)
# 
#     def save_data_file(self, identifier, data, destination_dir):
#         """Saves JSON data to a file location"""
#         filename = join(config.DATA_DIR, destination_dir, '{}.json'.format(identifier))
#         with open(filename, 'w') as fp:
#             dump(data, fp, indent=4, sort_keys=True)
#         logging.debug('ID %s exported to %s', identifier, filename)


import logging
from os.path import join
from asnake.aspace import ASpace
from static_aid import config
from static_aid.DataExtractor import DataExtractor
            "/repositories/{}/resources/{}/tree".format(config.archivesSpace['repository'], identifier)).json()
        self.save_data_file(identifier, tree, config.destinations['trees'])

    def log_fetch_start(self, fetch_type, last_export):
        if last_export > 0:
            logging.info('*** Getting a list of {} modified since %d ***'.format(fetch_type), last_export)
        else:
            logging.info('*** Getting a list of all {} ***'.format(fetch_type))

    def get_updated_resources(self, last_export):
        """Fetches and saves updated resource records and associated trees."""
        self.log_fetch_start("resources", last_export)
        for resource in self.repo.resources(with_params={'all_ids': True, 'modified_since': last_export}):
            resource_id = resource.uri.split("/")[-1]
            if resource.publish:
                self.save_data_file(resource_id, resource.json(), config.destinations['collections'])
                self.find_tree(resource_id)
            else:
                self.remove_data_file(resource_id, config.destinations['collections'])
                self.remove_data_file(resource_id, config.destinations['trees'])

    def get_updated_objects(self, last_export):
        """Fetches and saves updated archival objects and associated breadcrumbs."""
        self.log_fetch_start("objects", last_export)
        for archival_object in self.repo.archival_objects(with_params={'all_ids': True, 'modified_since': last_export}):
            archival_object_id = archival_object.uri.split("/")[-1]
            if archival_object.publish:
                self.save_data_file(archival_object_id, archival_object.json(), config.destinations['objects'])
                breadcrumbs = self.aspace.client.get(
                    "/repositories/{}/resources/{}/tree/node_from_root?node_ids[]={}&published_only=true".format(

# Path: static_aid/config.py
# ROOT = realpath(curdir)
# CONFIG_DEFAULTS_FILE_PATH = join(ROOT, 'local_settings.default')
#     ROOT = realpath(join(dirname(__file__), '..'))
#     CONFIG_DEFAULTS_FILE_PATH = join(ROOT, 'local_settings.default')
# CONFIG_FILE_PATH = join(ROOT, 'local_settings.cfg')
# SAMPLE_DATA_DIR = join(ROOT, 'data')
# SITE_SRC_DIR = join(ROOT, 'site')
# BUILD_DIR = join(ROOT, 'build')
# DATA_DIR = join(BUILD_DIR, 'data')
# STAGING_DIR = join(BUILD_DIR, 'staging')
# RAW_DATA_DIR = join(BUILD_DIR, 'raw')
# SITE_BUILD_DIR = join(BUILD_DIR, 'site')  # must match 'dest' settings in Gruntfile.js
# TEMP_DIR = join(BUILD_DIR, 'tmp')
# PID_FILE_PATH = join(TEMP_DIR, 'daemon.pid')
# OBJECT_CACHE_DIR = join(TEMP_DIR, 'object_cache')
# ROW_FETCH_LIMIT = 100
# def _configSection(section):
# def _stringToBoolean(string):
# def _stringToList(string):
# 
# Path: static_aid/DataExtractor.py
# class DataExtractor(object):
# 
#     def __init__(self, update=False):
#         if self.is_running():
#             logging.error('Process already running, exiting')
#             exit()
#         self.register_pid()
#         self.update = update
# 
#     def run(self):
#         logging.info('\n*** Export started ***')
# 
#         start_time = int(time())
#         utils.remove_file_or_dir(config.DATA_DIR)
#         self._run()
#         self.set_last_export_time(start_time)
# 
#         logging.info('*** Export completed ***')
#         utils.remove_file_or_dir(config.PID_FILE_PATH)
# 
#     def _run(self):
#         """Override this method in each extractor subclass."""
#         raise Exception('override this method for each DataExtractor subclass')
# 
#     def is_running(self):
#         """Determines whether or not the staticAid process is running."""
#         if isfile(config.PID_FILE_PATH):
#             pidfile = open(config.PID_FILE_PATH, "r")
#             for line in pidfile:
#                 pid = int(line.strip())
#                 if pid_exists(pid):
#                     return True
#         return False
# 
#     def register_pid(self):
#         """Registers the PID of the current process."""
#         if not exists(dirname(config.PID_FILE_PATH)):
#             makedirs(dirname(config.PID_FILE_PATH))
#         currentPid = str(getpid())
#         with open(config.PID_FILE_PATH, 'w') as pf:
#             pf.write(currentPid)
# 
#     def make_destinations(self):
#         """Creates destination directories for data files."""
#         for k in config.destinations:
#             path = join(config.DATA_DIR, config.destinations[k])
#             if not exists(path):
#                 makedirs(path)
# 
#     def get_last_export_time(self):
#         """Returns last export time in Unix epoch time, for example 1439563523."""
#         if self.update:
#             try:
#                 with open(config.lastExportFilepath, 'rb') as pickle_handle:
#                     return int(str(pickle.load(pickle_handle)))
#             except:
#                 pass
#         return 0
# 
#     def set_last_export_time(self, start_time):
#         """Store the current time in Unix epoch time, for example 1439563523."""
#         with open(config.lastExportFilepath, 'wb') as pickle_handle:
#             pickle.dump(start_time, pickle_handle)
#         logging.info('Last export time updated to {}'.format(start_time))
# 
#     def remove_data_file(self, identifier, destination):
#         """If a JSON file exists, deletes it."""
#         filename = join(destination, '%s.json' % str(identifier))
#         utils.remove_file_or_dir(filename)
# 
#     def save_data_file(self, identifier, data, destination_dir):
#         """Saves JSON data to a file location"""
#         filename = join(config.DATA_DIR, destination_dir, '{}.json'.format(identifier))
#         with open(filename, 'w') as fp:
#             dump(data, fp, indent=4, sort_keys=True)
#         logging.debug('ID %s exported to %s', identifier, filename)


# Path: static_aid/DataExtractor_ArchivesSpace.py
import logging
from os.path import join
from asnake.aspace import ASpace
from static_aid import config
from static_aid.DataExtractor import DataExtractor

            "/repositories/{}/resources/{}/tree".format(config.archivesSpace['repository'], identifier)).json()
        self.save_data_file(identifier, tree, config.destinations['trees'])

    def log_fetch_start(self, fetch_type, last_export):
        if last_export > 0:
            logging.info('*** Getting a list of {} modified since %d ***'.format(fetch_type), last_export)
        else:
            logging.info('*** Getting a list of all {} ***'.format(fetch_type))

    def get_updated_resources(self, last_export):
        """Fetches and saves updated resource records and associated trees."""
        self.log_fetch_start("resources", last_export)
        for resource in self.repo.resources(with_params={'all_ids': True, 'modified_since': last_export}):
            resource_id = resource.uri.split("/")[-1]
            if resource.publish:
                self.save_data_file(resource_id, resource.json(), config.destinations['collections'])
                self.find_tree(resource_id)
            else:
                self.remove_data_file(resource_id, config.destinations['collections'])
                self.remove_data_file(resource_id, config.destinations['trees'])

    def get_updated_objects(self, last_export):
        """Fetches and saves updated archival objects and associated breadcrumbs."""
        self.log_fetch_start("objects", last_export)
        for archival_object in self.repo.archival_objects(with_params={'all_ids': True, 'modified_since': last_export}):
            archival_object_id = archival_object.uri.split("/")[-1]
            if archival_object.publish:
                self.save_data_file(archival_object_id, archival_object.json(), config.destinations['objects'])
                breadcrumbs = self.aspace.client.get(
                    "/repositories/{}/resources/{}/tree/node_from_root?node_ids[]={}&published_only=true".format(

                        config.archivesSpace['repository'],