def from_directory(cls, directory, consider_only=None): """Load database and configuration from a directory on disk. The database is kept in `botfriend.sqlite` and bots are found in subdirectories. Default configuration settings can be kept in {directory}/default.yaml """ directory = directory or cls.default_directory() log = logging.getLogger("Loading configuration from %s" % directory) database_path = os.path.join(directory, 'botfriend.sqlite') _db = production_session(database_path) botmodels = [] seen_names = set() package_init = os.path.join(directory, '__init__.py') if not os.path.exists(package_init): log.warn( "%s does not exist; creating it so I can treat %s as a package.", package_init, directory ) open(package_init, 'w').close() if not directory in sys.path: logging.debug("Adding %s to sys.path" % directory) sys.path.append(directory) default_path = os.path.join(directory, "default.yaml") if os.path.exists(default_path): defaults = yaml.load(open(default_path)) else: defaults = {} for f in os.listdir(directory): bot_directory = os.path.join(directory, f) if os.path.isdir(bot_directory): # It's a directory; does it contain a bot? can_load = True for expect in ('bot.yaml'),: path = os.path.join(bot_directory, expect) if not os.path.exists(path): can_load = False if not f.startswith('.'): logging.warn( "Not loading %s: missing %s", f, expect ) break if can_load: botmodel = BotModel.from_directory(_db, bot_directory, defaults) if consider_only and ( botmodel.name not in consider_only and f not in consider_only ): continue if botmodel.name in seen_names: raise Exception( "Two different bots are configured with the same name. (%s)" % botmodel.name ) seen_names.add(botmodel.name) botmodels.append(botmodel) return Configuration(_db, botmodels, directory)
def __init__(self, _db=None, testing=False, emailer_class=Emailer): self.log = logging.getLogger("Library registry web app") if _db is None and not testing: _db = production_session() self._db = _db self.testing = testing self.setup_controllers(emailer_class)
"""Delete outdated ConfigurationSettings for the metadata wrangler.""" import os import sys import logging from nose.tools import set_trace bin_dir = os.path.split(__file__)[0] package_dir = os.path.join(bin_dir, "..") sys.path.append(os.path.abspath(package_dir)) from model import ( production_session, ExternalIntegration as EI, ) _db = production_session() try: integration = EI.lookup(_db, EI.METADATA_WRANGLER, EI.METADATA_GOAL) if integration: for setting in integration.settings: if setting.key == 'username': # A username (or client_id) is no longer required. _db.delete(setting) if setting.key == 'password': # The password (previously client_secret) must be reset to # register for a shared_secret. setting.value = None _db.commit() _db.close() except Exception as e:
import sys from nose.tools import set_trace d = os.path.split(__file__)[0] site.addsitedir(os.path.join(d, "..")) from model import ( DataSource, LicensePool, SessionManager, Work, Identifier, ) from model import production_session if __name__ == '__main__': session = production_session() data_source_name = sys.argv[1] identifier = sys.argv[2] data_source = DataSource.lookup(session, data_source_name) wid, ignore = Identifier.for_foreign_id( session, data_source.primary_identifier_type, identifier, False) pool = session.query(LicensePool).filter( LicensePool.data_source == data_source).filter( LicensePool.identifier == wid).one() primary_edition = pool.edition() old_work = primary_edition.work if old_work: old_work.license_pools.remove(pool) primary_edition.work = None pool.calculate_work()
"""Gather up LicensePool objects into Work objects.""" import os import site import sys from nose.tools import set_trace d = os.path.split(__file__)[0] site.addsitedir(os.path.join(d, "..")) from model import DataSource, LicensePool, SessionManager, Work, Identifier from model import production_session if __name__ == "__main__": session = production_session() data_source_name = sys.argv[1] identifier = sys.argv[2] data_source = DataSource.lookup(session, data_source_name) wid, ignore = Identifier.for_foreign_id(session, data_source.primary_identifier_type, identifier, False) pool = ( session.query(LicensePool) .filter(LicensePool.data_source == data_source) .filter(LicensePool.identifier == wid) .one() ) primary_edition = pool.edition() old_work = primary_edition.work if old_work: old_work.license_pools.remove(pool) primary_edition.work = None
def _db(self): if not hasattr(self, "_session"): self._session = production_session() return self._session
def __init__(self): Configuration.load() self.db = production_session()
import datetime from collections import defaultdict from model import ( CirculationEvent, DataSource, CoverageRecord, production_session, Identifier, Measurement, LicensePool, ) import json import gzip database = production_session() data_dir = sys.argv[1] OVERDRIVE = DataSource.lookup(database, DataSource.OVERDRIVE) TIME_FORMAT = "%Y-%m-%dT%H:%M:%S+00:00" def process_item(_db, item): overdrive_id = item['id'] event_name = item['event'] old_value = item.get('old_value', 0) new_value = item.get('new_value', 0) if event_name in ('check_out', 'check_in'): x = new_value new_value = old_value
import datetime from collections import defaultdict from model import ( CirculationEvent, DataSource, CoverageRecord, production_session, Identifier, Measurement, LicensePool, ) import json import gzip database = production_session() data_dir = sys.argv[1] OVERDRIVE = DataSource.lookup(database, DataSource.OVERDRIVE) TIME_FORMAT = "%Y-%m-%dT%H:%M:%S+00:00" def process_item(_db, item): overdrive_id = item['id'] event_name = item['event'] old_value = item.get('old_value', 0) new_value = item.get('new_value', 0) if event_name in ('check_out', 'check_in'): x = new_value new_value = old_value old_value = x
def f(services): print "Starting coverage provider" LinkedDataCoverageProvider(production_session(), services).run()