Skip to content

Commit

Permalink
NO-REF: Refactoring statics (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylevillegas93 authored Nov 18, 2024
1 parent edda0ce commit 04a07a6
Show file tree
Hide file tree
Showing 39 changed files with 103 additions and 93 deletions.
17 changes: 17 additions & 0 deletions constants/get_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from glob import glob
import json
import re


def get_constants():
constants = {}

for path in glob('./constants/*.json'):
file_name = re.search(r'\/([a-z0-9]+)\.json', path).group(1)

with open(path, 'r') as constants_file:
file_constants = json.load(constants_file)

constants[file_name] = file_constants

return constants
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions mappings/base_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@


class BaseMapping(RecordMapping):
def __init__(self, source, statics):
def __init__(self, source, constants):
self.mapping = {}
self.source = source
self.record = None
self.staticValues = statics
self.constants = constants

self.formatter = CustomFormatter()

Expand Down
4 changes: 2 additions & 2 deletions mappings/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
logger = create_log(__name__)

class CSVMapping(BaseMapping):
def __init__(self, source, statics):
super().__init__(source, statics)
def __init__(self, source, constants):
super().__init__(source, constants)

def applyMapping(self):
newRecord = self.initEmptyRecord()
Expand Down
4 changes: 2 additions & 2 deletions mappings/doab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class DOABMapping(XMLMapping):
DOI_REGEX = r'doabooks.org\/handle\/([0-9]+\.[0-9]+\.[0-9]+\/[0-9]+)'
def __init__(self, source, namespace, statics):
super(DOABMapping, self).__init__(source, namespace, statics)
def __init__(self, source, namespace, constants):
super(DOABMapping, self).__init__(source, namespace, constants)
self.mapping = self.createMapping()

def createMapping(self):
Expand Down
6 changes: 3 additions & 3 deletions mappings/gutenberg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from mappings.xml import XMLMapping

class GutenbergMapping(XMLMapping):
def __init__(self, source, namespace, statics):
super(GutenbergMapping, self).__init__(source, namespace, statics)
def __init__(self, source, namespace, constants):
super(GutenbergMapping, self).__init__(source, namespace, constants)
self.mapping = self.createMapping()

def createMapping(self):
Expand Down Expand Up @@ -69,7 +69,7 @@ def applyFormatting(self):
# Parse contributors to set proper roles
for i, contributor in enumerate(self.record.contributors):
contribComponents = contributor.split('|')
lcRelation = self.staticValues['lc']['relators'].get(contribComponents[-1], 'Contributor')
lcRelation = self.constants['lc']['relators'].get(contribComponents[-1], 'Contributor')
self.record.contributors[i] = '{}|{}|{}|{}'.format(
*contribComponents[:-1], lcRelation
)
Expand Down
12 changes: 6 additions & 6 deletions mappings/hathitrust.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


class HathiMapping(CSVMapping):
def __init__(self, source, statics):
super(HathiMapping, self).__init__(source, statics)
def __init__(self, source, constants):
super(HathiMapping, self).__init__(source, constants)
self.mapping = self.createMapping()

def createMapping(self):
Expand Down Expand Up @@ -67,15 +67,15 @@ def applyFormatting(self):
self.record.contributors = self.record.contributors or []
for i, contributor in enumerate(self.record.contributors):
contribElements = contributor.lower().split('|')
fullContribName = self.staticValues['hathitrust']['sourceCodes'].get(contribElements[0], contribElements[0])
fullContribName = self.constants['hathitrust']['sourceCodes'].get(contribElements[0], contribElements[0])
self.record.contributors[i] = contributor.replace(contribElements[0], fullContribName)

# Parse rights codes
rightsElements = self.record.rights.split('|') if self.record.rights else [''] * 5
rightsMetadata = self.staticValues['hathitrust']['rightsValues'].get(rightsElements[1], {'license': 'und', 'statement': 'und'})
rightsMetadata = self.constants['hathitrust']['rightsValues'].get(rightsElements[1], {'license': 'und', 'statement': 'und'})
self.record.rights = 'hathitrust|{}|{}|{}|{}'.format(
rightsMetadata['license'],
self.staticValues['hathitrust']['rightsReasons'].get(rightsElements[2], rightsElements[2]),
self.constants['hathitrust']['rightsReasons'].get(rightsElements[2], rightsElements[2]),
rightsMetadata['statement'],
rightsElements[4]
)
Expand All @@ -102,4 +102,4 @@ def applyFormatting(self):

# Parse spatial (pub place) codes
self.record.spatial = self.record.spatial or ''
self.record.spatial = self.staticValues['marc']['countryCodes'].get(self.record.spatial.strip(), 'xx')
self.record.spatial = self.constants['marc']['countryCodes'].get(self.record.spatial.strip(), 'xx')
4 changes: 2 additions & 2 deletions mappings/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


class JSONMapping(BaseMapping):
def __init__(self, source, statics):
super().__init__(source, statics)
def __init__(self, source, constants):
super().__init__(source, constants)

def applyMapping(self):
self.record = self.initEmptyRecord()
Expand Down
4 changes: 2 additions & 2 deletions mappings/marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@


class MARCMapping(BaseMapping):
def __init__(self, source, statics):
super().__init__(source, statics)
def __init__(self, source, constants):
super().__init__(source, constants)

def applyMapping(self):
newRecord = self.initEmptyRecord()
Expand Down
6 changes: 3 additions & 3 deletions mappings/nypl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from .sql import SQLMapping

class NYPLMapping(SQLMapping):
def __init__(self, source, bibItems, statics, locationCodes):
super().__init__(source, statics)
def __init__(self, source, bibItems, constants, locationCodes):
super().__init__(source, constants)
self.mapping = self.createMapping()
self.locationCodes = locationCodes
self.bibItems = bibItems
Expand Down Expand Up @@ -119,7 +119,7 @@ def applyFormatting(self):
# Parse contributors to set proper roles
for i, contributor in enumerate(self.record.contributors):
contribComponents = contributor.split('|')
lcRelation = self.staticValues['lc']['relators'].get(contribComponents[-1], 'Contributor')
lcRelation = self.constants['lc']['relators'].get(contribComponents[-1], 'Contributor')
self.record.contributors[i] = '{}|{}|{}|{}'.format(
*contribComponents[:-1], lcRelation
)
Expand Down
4 changes: 2 additions & 2 deletions mappings/oclcCatalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class CatalogMapping(XMLMapping):
'hathitrust': r'catalog.hathitrust.org\/api\/volumes\/([a-z]{3,6}\/[a-zA-Z0-9]+)\.html'
}

def __init__(self, source, namespace, statics):
super(CatalogMapping, self).__init__(source, namespace, statics)
def __init__(self, source, namespace, constants):
super(CatalogMapping, self).__init__(source, namespace, constants)
self.mapping = self.createMapping()

def createMapping(self):
Expand Down
4 changes: 2 additions & 2 deletions mappings/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


class SQLMapping(BaseMapping):
def __init__(self, source, statics):
super().__init__(source, statics)
def __init__(self, source, constants):
super().__init__(source, constants)

def applyMapping(self):
newRecord = self.initEmptyRecord()
Expand Down
4 changes: 2 additions & 2 deletions mappings/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@


class XMLMapping(BaseMapping):
def __init__(self, source, namespace, statics):
super().__init__(source, statics)
def __init__(self, source, namespace, constants):
super().__init__(source, constants)
self.namespace = namespace

def applyMapping(self):
Expand Down
6 changes: 4 additions & 2 deletions processes/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from sqlalchemy.exc import DataError
from typing import Optional

from constants.get_constants import get_constants
from .core import CoreProcess
from managers import SFRRecordManager, KMeansManager, SFRElasticRecordManager, ElasticsearchManager, RedisManager
from model import Record, Work
from logger import create_log


logger = create_log(__name__)


Expand All @@ -35,6 +35,8 @@ def __init__(self, *args):
self.elastic_search_manager.createElasticSearchIngestPipeline()
self.elastic_search_manager.createElasticSearchIndex()

self.constants = get_constants()

def runProcess(self):
try:
if self.process == 'daily':
Expand Down Expand Up @@ -219,7 +221,7 @@ def get_matched_records(self, identifiers: list[str], already_matched_record_ids
return matched_records

def create_work_from_editions(self, editions, records):
record_manager = SFRRecordManager(self.session, self.statics['iso639'])
record_manager = SFRRecordManager(self.session, self.constants['iso639'])

work_data = record_manager.buildWork(records, editions)

Expand Down
3 changes: 1 addition & 2 deletions processes/core.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from managers import DBManager, S3Manager
from model import Record
from static.manager import StaticManager

from logger import create_log


logger = create_log(__name__)


class CoreProcess(DBManager, StaticManager, S3Manager):
class CoreProcess(DBManager, S3Manager):
def __init__(self, process, customFile, ingestPeriod, singleRecord, batchSize=500):
super(CoreProcess, self).__init__()
self.process = process
Expand Down
5 changes: 4 additions & 1 deletion processes/ingest/doab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import requests

from constants.get_constants import get_constants
from ..core import CoreProcess
from logger import create_log
from mappings.doab import DOABMapping
Expand Down Expand Up @@ -44,6 +45,8 @@ def __init__(self, *args):
self.rabbitmq_manager.createRabbitConnection()
self.rabbitmq_manager.createOrConnectQueue(self.fileQueue, self.fileRoute)

self.constants = get_constants()

def runProcess(self):
if self.process == 'daily':
self.importOAIRecords()
Expand All @@ -61,7 +64,7 @@ def runProcess(self):

def parseDOABRecord(self, oaiRec):
try:
doabRec = DOABMapping(oaiRec, self.OAI_NAMESPACES, self.statics)
doabRec = DOABMapping(oaiRec, self.OAI_NAMESPACES, self.constants)
doabRec.applyMapping()
except MappingError as e:
raise DOABError(e.message)
Expand Down
5 changes: 4 additions & 1 deletion processes/ingest/gutenberg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import re

from constants.get_constants import get_constants
from ..core import CoreProcess
from managers import GutenbergManager, RabbitMQManager
from mappings.gutenberg import GutenbergMapping
Expand Down Expand Up @@ -41,6 +42,8 @@ def __init__(self, *args):

self.s3Bucket = os.environ['FILE_BUCKET']

self.constants = get_constants()

def runProcess(self):
if self.process == 'daily':
self.importRDFRecords()
Expand Down Expand Up @@ -89,7 +92,7 @@ def processGutenbergBatch(self, dataFiles):
gutenbergRec = GutenbergMapping(
gutenbergRDF,
self.GUTENBERG_NAMESPACES,
self.statics
self.constants
)

gutenbergRec.applyMapping()
Expand Down
5 changes: 4 additions & 1 deletion processes/ingest/hathi_trust.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests
from requests.exceptions import ReadTimeout, HTTPError

from constants.get_constants import get_constants
from ..core import CoreProcess
from mappings.hathitrust import HathiMapping
from logger import create_log
Expand All @@ -23,6 +24,8 @@ def __init__(self, *args):
self.generateEngine()
self.createSession()

self.constants = get_constants()

def runProcess(self):
if self.process == 'daily':
date_time = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(hours=24)
Expand All @@ -47,7 +50,7 @@ def importFromSpecificFile(self, file_path):
self.readHathiFile(hathiReader)

def parseHathiDataRow(self, data_row):
hathiRec = HathiMapping(data_row, self.statics)
hathiRec = HathiMapping(data_row, self.constants)
hathiRec.applyMapping()
self.addDCDWToUpdateList(hathiRec)

Expand Down
4 changes: 3 additions & 1 deletion processes/local_development/seed_local_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import requests

from constants.get_constants import get_constants
from ..core import CoreProcess
from logger import create_log
from managers import RedisManager
Expand All @@ -18,6 +19,7 @@ def __init__(self, *args):
super(SeedLocalDataProcess, self).__init__(*args[:4])

self.redis_manager = RedisManager()
self.constants = get_constants()

def runProcess(self):
try:
Expand Down Expand Up @@ -86,7 +88,7 @@ def import_from_hathi_trust_data_file(self):
book_right = book[2]

if book_right not in in_copyright_statuses:
hathi_record = HathiMapping(book, self.statics)
hathi_record = HathiMapping(book, self.constants)
hathi_record.applyMapping()

self.addDCDWToUpdateList(hathi_record)
Expand Down
6 changes: 3 additions & 3 deletions services/sources/nypl_bib_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import requests
from typing import Optional

from constants.get_constants import get_constants
from logger import create_log
from managers.db import DBManager
from static.manager import StaticManager
from managers.nyplApi import NyplApiManager
from mappings.nypl import NYPLMapping
from .source_service import SourceService
Expand All @@ -32,7 +32,7 @@ def __init__(self):
self.location_codes = self.load_location_codes()
self.cce_api = os.environ['BARDO_CCE_API']

self.static_manager = StaticManager()
self.constants = get_constants()

def get_records(
self,
Expand Down Expand Up @@ -79,7 +79,7 @@ def parse_nypl_bib(self, bib) -> Optional[NYPLMapping]:
if self.is_pd_research_bib(dict(bib)):
bib_items = self.fetch_bib_items(dict(bib))

nypl_record = NYPLMapping(bib, bib_items, self.static_manager.statics, self.location_codes)
nypl_record = NYPLMapping(bib, bib_items, self.constants, self.location_codes)
nypl_record.applyMapping()

return nypl_record
Expand Down
29 changes: 0 additions & 29 deletions static/manager.py

This file was deleted.

Empty file.
Loading

0 comments on commit 04a07a6

Please sign in to comment.