22`resolver` -- Resolve Identifiers to Image Paths
33================================================
44"""
5+ from contextlib import closing
6+ import glob
7+ import json
58from logging import getLogger
69from os .path import join , exists , dirname , split
710from os import remove
811from shutil import copy
912import tempfile
10- from contextlib import closing
11- import glob
12- import json
13- import os
1413from urllib .parse import unquote
14+ import warnings
1515
1616import requests
1717
1818from loris import constants
1919from loris .identifiers import CacheNamer , IdentRegexChecker
20- from loris .loris_exception import ResolverException
20+ from loris .loris_exception import ResolverException , ConfigError
2121from loris .utils import mkdir_p , safe_rename
2222from loris .img_info import ImageInfo
2323
2424
2525logger = getLogger (__name__ )
2626
2727
28- class _AbstractResolver ( object ) :
28+ class _AbstractResolver :
2929
3030 def __init__ (self , config ):
3131 self .config = config
3232 if config :
33+ # check for previous settings
34+ if "use_extra_info" in self .config and "use_auth_rules" in self .config :
35+ raise ConfigError ("You cannot set both use_extra_info and use_auth_rules. Please remove use_extra_info from your config." )
36+
37+ if "use_extra_info" in self .config :
38+ warnings .warn ("The use_extra_info field has been renamed to use_auth_rules and will be removed in a future version. Please update your config." , DeprecationWarning )
39+ self .config ["use_auth_rules" ] = self .config ["use_extra_info" ]
40+
3341 self .auth_rules_ext = self .config .get ('auth_rules_ext' , 'rules.json' )
34- self .use_extra_info = self .config .get ('use_extra_info ' , True )
42+ self .use_auth_rules = self .config .get ('use_auth_rules ' , False )
3543
3644 def is_resolvable (self , ident ):
3745 """
@@ -65,12 +73,10 @@ def resolve(self, app, ident, base_uri):
6573 cn = self .__class__ .__name__
6674 raise NotImplementedError ('resolve() not implemented for %s' % (cn ,))
6775
68- def get_extra_info (self , ident , source_fp ):
76+ def get_auth_rules (self , ident , source_fp ):
6977 """
70- Given the identifier and any resolved source file, find the associated
71- extra information to include in info.json, plus any additional authorizer
72- specific information. It might end up there after being copied by a
73- caching implementation, or live there permanently.
78+ Given the identifier and any resolved source file (ie. on the filesystem), grab the associated
79+ authentication rules from the filesystem (if they exist).
7480
7581 Args:
7682 ident (str):
@@ -80,12 +86,12 @@ def get_extra_info(self, ident, source_fp):
8086 Returns:
8187 dict: The dict of information to embed in info.json
8288 """
89+ if not self .use_auth_rules :
90+ return {}
8391 xjsfp = source_fp .rsplit ('.' , 1 )[0 ] + "." + self .auth_rules_ext
8492 if exists (xjsfp ):
85- fh = open (xjsfp )
86- xjs = json .load (fh )
87- fh .close ()
88- return xjs
93+ with open (xjsfp ) as fh :
94+ return json .load (fh )
8995 else :
9096 return {}
9197
@@ -130,14 +136,13 @@ def is_resolvable(self, ident):
130136 return not self .source_file_path (ident ) is None
131137
132138 def resolve (self , app , ident , base_uri ):
133-
134139 if not self .is_resolvable (ident ):
135140 self .raise_404_for_ident (ident )
136141
137142 source_fp = self .source_file_path (ident )
138143 format_ = self .format_from_ident (ident )
139- extra = self .get_extra_info (ident , source_fp )
140- return ImageInfo (app , source_fp , format_ , extra )
144+ auth_rules = self .get_auth_rules (ident , source_fp )
145+ return ImageInfo (app = app , src_img_fp = source_fp , src_format = format_ , auth_rules = auth_rules )
141146
142147
143148class ExtensionNormalizingFSResolver (SimpleFSResolver ):
@@ -271,7 +276,7 @@ def _web_request_url(self, ident):
271276 return (url , self .request_options ())
272277
273278 def cache_dir_path (self , ident ):
274- return os . path . join (
279+ return join (
275280 self .cache_root ,
276281 CacheNamer .cache_directory_name (ident = ident ),
277282 ident ,
@@ -347,19 +352,19 @@ def copy_to_cache(self, ident):
347352 # These files are < 2k in size, so fetch in one go.
348353 # Assumes that the rules will be next to the image
349354 # cache_dir is image specific, so this is easy
350-
351- bits = split (source_url )
352- fn = bits [1 ].rsplit ('.' , 1 )[0 ] + "." + self .auth_rules_ext
353- rules_url = bits [0 ] + '/' + fn
354- try :
355- resp = requests .get (rules_url )
356- if resp .status_code == 200 :
357- local_rules_fp = join (cache_dir , "loris_cache." + self .auth_rules_ext )
358- if not exists (local_rules_fp ):
359- with open (local_rules_fp , 'w' ) as fh :
360- fh .write (resp .text )
361- except requests .exceptions .RequestException :
362- pass
355+ if self . use_auth_rules :
356+ bits = split (source_url )
357+ fn = bits [1 ].rsplit ('.' , 1 )[0 ] + "." + self .auth_rules_ext
358+ rules_url = bits [0 ] + '/' + fn
359+ try :
360+ resp = requests .get (rules_url )
361+ if resp .status_code == 200 :
362+ local_rules_fp = join (cache_dir , "loris_cache." + self .auth_rules_ext )
363+ if not exists (local_rules_fp ):
364+ with open (local_rules_fp , 'w' ) as fh :
365+ fh .write (resp .text )
366+ except requests .exceptions .RequestException :
367+ pass
363368
364369 return local_fp
365370
@@ -368,11 +373,8 @@ def resolve(self, app, ident, base_uri):
368373 if not cached_file_path :
369374 cached_file_path = self .copy_to_cache (ident )
370375 format_ = self .get_format (cached_file_path , None )
371- if self .use_extra_info :
372- extra = self .get_extra_info (ident , cached_file_path )
373- else :
374- extra = {}
375- return ImageInfo (app , cached_file_path , format_ , extra )
376+ auth_rules = self .get_auth_rules (ident , cached_file_path )
377+ return ImageInfo (app = app , src_img_fp = cached_file_path , src_format = format_ , auth_rules = auth_rules )
376378
377379
378380class TemplateHTTPResolver (SimpleHTTPResolver ):
@@ -553,5 +555,5 @@ def resolve(self, app, ident, base_uri):
553555
554556 cache_fp = self .cache_file_path (ident )
555557 format_ = self .format_from_ident (ident )
556- extra = self .get_extra_info (ident , cache_fp )
557- return ImageInfo (app , cache_fp , format_ , extra )
558+ auth_rules = self .get_auth_rules (ident , cache_fp )
559+ return ImageInfo (app = app , src_img_fp = cache_fp , src_format = format_ , auth_rules = auth_rules )
0 commit comments