Skip to content

Commit

Permalink
adding debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
femalves committed Nov 24, 2023
1 parent a2960e7 commit 292af66
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
24 changes: 19 additions & 5 deletions scan_explorer_service/manifest_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from scan_explorer_service.models import Article, Page, Collection
from typing import Union
from itertools import chain
from scan_explorer_service.views.view_utils import logger

class ManifestFactoryExtended(ManifestFactory):
""" Extended manifest factory.
Expand All @@ -12,30 +13,37 @@ class ManifestFactoryExtended(ManifestFactory):
"""

def create_manifest(self, item: Union[Article, Collection]):
logger.debug(f"Creating manifest for item: {item}")
manifest = self.manifest(
ident=f'{item.id}/manifest.json', label=item.id)
manifest.description = item.id
manifest.add_sequence(self.create_sequence(item))
for range in self.create_range(item):
manifest.add_range(range)

logger.debug(f"Manifest created: {manifest}")
return manifest

def create_sequence(self, item: Union[Article, Collection]):
logger.debug(f"Creating sequence for item: {item}")
sequence: Sequence = self.sequence()
logger.debug(f"Sequence is: {sequence}. Adding canvases to sequence.")
for page in item.pages:
logger.debug(f"Adding canvas to sequence: {page}.")
sequence.add_canvas(self.get_or_create_canvas(page))


logger.debug(f"Final sequence created: {sequence}")
return sequence

def create_range(self, item: Union[Article, Collection]):
logger.debug(f"Creating range for item: {item}")
if isinstance(item, Collection):
return list(chain(*[self.create_range(article) for article in item.articles]))

range: Range = self.range(ident=item.bibcode, label=item.bibcode)
for page in item.pages:
logger.debug(f"Adding canvas to range: {page}.")
range.add_canvas(self.get_or_create_canvas(page))

logger.debug(f"Range created: {[range]}")
return [range]

def get_canvas_dict(self) -> Dict[str, Canvas]:
Expand All @@ -44,6 +52,7 @@ def get_canvas_dict(self) -> Dict[str, Canvas]:
return self.canvas_dict

def get_or_create_canvas(self, page: Page):
logger.debug(f"Getting or creating canvas for page: {page}")
canvas_dict = self.get_canvas_dict()
if(page.id in canvas_dict.keys()):
return canvas_dict[page.id]
Expand All @@ -61,24 +70,29 @@ def get_or_create_canvas(self, page: Page):
annotation.on = canvas.id
canvas.add_annotation(annotation)
canvas_dict[page.id] = canvas

logger.debug(f"Canvas created: {canvas}")
return canvas

def create_image_annotation(self, page: Page):
logger.debug(f"Creating image annotation for page: {page}")
annotation: Annotation = self.annotation(ident=str(page.id))
image: Image = annotation.image(
ident=page.image_path, label=f'p. {page.label}', iiif=True)

# Override default image quality and format set by prezi
image.id = image.id.replace(f'/default.jpg', f'/{page.image_color_quality}.tif')

logger.debug(f"Image id: {image.id}")
image.format = page.format
image.height = page.height
image.width = page.width

logger.debug(f"Image annotation created: {annotation}")
return annotation

def add_search_service(self, manifest: Manifest, search_url: str):
logger.debug(f"Adding search services for manifest {manifest} and search url {search_url}")
context = 'http://iiif.io/api/search/1/context.json'
profile = 'http://iiif.io/api/search/1/search'

manifest.add_service(ident=search_url, context=context, profile=profile)
logger.debug(f"Adding search services for manifest {manifest} and search url {search_url}")
4 changes: 4 additions & 0 deletions scan_explorer_service/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@

from flask import current_app, url_for
from scan_explorer_service.views.view_utils import logger


def url_for_proxy(endpoint: str, **values):
values['_external'] = False

server, prefix = proxy_url()
logger.debug(f"Server is {server} and prefix is {prefix}.")
path = url_for(endpoint, **values).lstrip('/')

logger.debug(f"Url is {server}/{prefix}/{path}.")
return f'{server}/{prefix}/{path}'

def proxy_url():
Expand Down
9 changes: 9 additions & 0 deletions scan_explorer_service/views/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from scan_explorer_service.open_search import EsFields, text_search_highlight
from scan_explorer_service.utils.utils import proxy_url, url_for_proxy
from typing import Union
from scan_explorer_service.views.view_utils import logger


bp_manifest = Blueprint('manifest', __name__, url_prefix='/manifest')
Expand All @@ -26,18 +27,26 @@ def before_request():
@bp_manifest.route('/<string:id>/manifest.json', methods=['GET'])
def get_manifest(id: str):
""" Creates an IIIF manifest from an article or Collection"""

logger.debug(f"Fetching manifest for item with id : {id}")
with current_app.session_scope() as session:
logger.debug(f"Fetching item (Article/Collection).")
item: Union[Article, Collection] = (
session.query(Article).filter(Article.id == id).one_or_none()
or session.query(Collection).filter(Collection.id == id).one_or_none())

if item:
logger.debug(f"Item successfully fetched. Creating manifest for item: {item}")
manifest = manifest_factory.create_manifest(item)
logger.debug(f"Getting url for proxy for endpoint manifest.search and id {id}")
search_url = url_for_proxy('manifest.search', id=id)
logger.debug(f"Getting url for proxy for endpoint manifest.search and id {id}")
manifest_factory.add_search_service(manifest, search_url)
logger.debug(f"Manifest generated successfully for ID: {id}")

return manifest.toJSON(top=True)
else:
logger.debug(f"Item not found for article with id: {id}")
return jsonify(exception='Article not found'), 404


Expand Down
10 changes: 10 additions & 0 deletions scan_explorer_service/views/view_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import enum
import os
from adsmutils import setup_logging, load_config

# ============================= INITIALIZATION ==================================== #

proj_home = os.path.realpath(os.path.dirname(__file__))
config = load_config(proj_home=proj_home)
logger = setup_logging('view_utils.py', proj_home=proj_home,
level=config.get('LOGGING_LEVEL', 'DEBUG'),
attach_stdout=config.get('LOG_STDOUT', False))

class ApiErrors(enum.Enum):
SearchError = 1

0 comments on commit 292af66

Please sign in to comment.