1010# see exception subclasses at https://docs.pylonsproject.org/projects/pyramid/en/latest/api/httpexceptions.html
1111from pyramid .request import Request
1212from pyramid .httpexceptions import (
13- HTTPException ,
1413 HTTPOk ,
15- HTTPError ,
1614 HTTPServerError ,
17- HTTPNotFound ,
1815 HTTPBadRequest ,
19- HTTPInternalServerError ,
2016 HTTPForbidden ,
2117)
2218from beaker .cache import cache_managers
2521import json
2622import requests
2723import os
28- import re
29- import copy
3024import threading
3125
3226try :
@@ -51,8 +45,8 @@ def get_private_dir(request):
5145def atomic_write_json_if_not_found (obj , dest , request ):
5246 if os .path .exists (dest ):
5347 return False
54- dir = get_private_dir (request )
55- handle , tmpfn = tempfile .mkstemp (suffix = ".json" , dir = dir , text = True )
48+ pdir = get_private_dir (request )
49+ handle , tmpfn = tempfile .mkstemp (suffix = ".json" , dir = pdir , text = True )
5650 # mkstemp opens the file and returns a file descriptor,
5751 # but we are using write_as_json to open with the right encoding
5852 os .close (handle )
@@ -84,7 +78,7 @@ def compose_push_to_github_url(request, resource_id, doc_type):
8478_PHYLESYSTEM = None
8579
8680
87- def get_phylesystem (request ):
81+ def get_phylesystem (request , conf_obj = None ):
8882 global READ_ONLY_MODE
8983 global _PHYLESYSTEM
9084 # _LOG.debug('@@@ checking for _PHYLESYSTEM singleton...READ_ONLY_MODE? {}'.format(READ_ONLY_MODE))
@@ -94,6 +88,8 @@ def get_phylesystem(request):
9488 # _LOG.debug('@@@ NOT FOUND, creating now')
9589 from phylesystem_api .gitdata import GitData
9690
91+ if conf_obj is None :
92+ conf_obj = get_conf_object (request )
9793 (
9894 repo_parent ,
9995 repo_remote ,
@@ -103,7 +99,7 @@ def get_phylesystem(request):
10399 max_filesize ,
104100 max_num_trees ,
105101 READ_ONLY_MODE ,
106- ) = read_phylesystem_config (request )
102+ ) = read_phylesystem_config (request , conf_obj = conf_obj )
107103 peyotl_config , cfg_filename = read_peyotl_config ()
108104 if "phylesystem" not in peyotl_config .sections ():
109105 peyotl_config .add_section ("phylesystem" )
@@ -118,10 +114,9 @@ def get_phylesystem(request):
118114 },
119115 }
120116 mirror_info = {"push" : pmi }
121- conf = get_conf_object (request )
122117 a = {}
123118 try :
124- new_study_prefix = conf .get ("apis" , "new_study_prefix" )
119+ new_study_prefix = conf_obj .get ("apis" , "new_study_prefix" )
125120 a ["new_study_prefix" ] = new_study_prefix
126121 except :
127122 pass
@@ -135,9 +130,9 @@ def get_phylesystem(request):
135130 )
136131 # _LOG.debug('[[[[[[ repo_nexml2json = {}'.format(_PHYLESYSTEM.repo_nexml2json))
137132 if READ_ONLY_MODE :
138- _LOG .warn ("phylesytem-api running in READ_ONLY_MODE" )
133+ _LOG .warning ("phylesytem-api running in READ_ONLY_MODE" )
139134 else :
140- _LOG .warn ("phylesytem-api NOT running in READ_ONLY_MODE" )
135+ _LOG .warning ("phylesytem-api NOT running in READ_ONLY_MODE" )
141136 return _PHYLESYSTEM
142137
143138
@@ -167,9 +162,6 @@ def get_tree_collection_store(request):
167162 },
168163 }
169164 mirror_info = {"push" : pmi }
170- conf = get_conf_object (request )
171- import pprint
172-
173165 a = {}
174166 try :
175167 # any keyword args to pass along from config?
@@ -216,9 +208,6 @@ def get_taxonomic_amendment_store(request):
216208 },
217209 }
218210 mirror_info = {"push" : pmi }
219- conf = get_conf_object (request )
220- import pprint
221-
222211 a = {}
223212 try :
224213 # any keyword args to pass along from config?
@@ -255,21 +244,34 @@ def get_failed_push_filepath(request, doc_type=None):
255244 return os .path .join (get_private_dir (request ), failure_filename )
256245
257246
258- def get_conf_object (request ):
247+ def get_conf_object (request = None , localconfig_filename = None ):
259248 # There's apparently no easy way to retrieve the fully parsed
260249 # configuration from within the app. But we can access the variables
261250 # from the [app:main] setion, so we'll retrieve the full path to
262251 # our chosen INI file from there.
252+ if localconfig_filename is None :
253+ assert request is not None
254+ localconfig_filename = request .registry .settings ["config_file_path" ]
255+ _LOG .debug (
256+ 'get_conf_object(localconfig_filename="{}")' .format (localconfig_filename )
257+ )
258+ if not os .path .isfile (localconfig_filename ):
259+ raise RuntimeError (
260+ "localconfig_filename={} does not exist" .format (localconfig_filename )
261+ )
263262 conf = ConfigParser (allow_no_value = True )
264- localconfig_filename = request .registry .settings ["config_file_path" ]
265- if os .path .isfile (localconfig_filename ):
266- conf .readfp (open (localconfig_filename ))
263+ conf .read (localconfig_filename )
267264 return conf
268265
269266
270- def read_phylesystem_config (request ):
267+ def read_phylesystem_config (request , conf_obj = None ):
271268 """Load settings for managing the main Nexson docstore"""
272- conf = get_conf_object (request )
269+ if conf_obj is None :
270+ conf_obj = get_conf_object (request )
271+ return _read_phylesystem_from_conf_obj (conf_obj )
272+
273+
274+ def _read_phylesystem_from_conf_obj (conf ):
273275 repo_parent = conf .get ("apis" , "repo_parent" )
274276 repo_remote = conf .get ("apis" , "repo_remote" )
275277 try :
@@ -660,14 +662,12 @@ def clear_matching_cache_keys(key_pattern):
660662 assert len (namespaces ) == 1
661663 active_namespace = list (namespaces .values ())[0 ]
662664 # NB - again, code may change if we use multiple namespaces here
663- item_count_before = len (list (active_namespace .items ()))
664- """
665- print("=== %d RAM cache keys BEFORE clearing: ===" % item_count_before)
666- for k, v in active_namespace.items():
667- print('{k} ===> {v}'.format(k=k,v=v))
668- print("===")
669- """
670665 # _LOG.debug("> clearing cached items matching [%s]" % key_pattern)
666+ # item_count_before = len(list(active_namespace.items()))
667+ # print("=== %d RAM cache keys BEFORE clearing: ===" % item_count_before)
668+ # for k, v in active_namespace.items():
669+ # print('{k} ===> {v}'.format(k=k,v=v))
670+ # print("===")
671671
672672 matching_keys = []
673673 for k , v in active_namespace .items ():
@@ -676,14 +676,12 @@ def clear_matching_cache_keys(key_pattern):
676676 for matching_key in matching_keys :
677677 del active_namespace [matching_key ]
678678
679- """
680- item_count_after = len(list(active_namespace.items()))
681- print("=== %d RAM cache keys AFTER clearing: ===" % item_count_after)
682- for k, v in active_namespace.items():
683- print('{k} ===> {v}'.format(k,v))
684- print("===")
685- print(" %d items removed" % (item_count_before - item_count_after,))
686- """
679+ # item_count_after = len(list(active_namespace.items()))
680+ # print("=== %d RAM cache keys AFTER clearing: ===" % item_count_after)
681+ # for k, v in active_namespace.items():
682+ # print('{k} ===> {v}'.format(k,v))
683+ # print("===")
684+ # print(" %d items removed" % (item_count_before - item_count_after,))
687685
688686
689687def raise_on_CORS_preflight (request ):
@@ -875,7 +873,6 @@ def extract_json_from_http_call(request, data_field_name="data", request_params=
875873
876874 request_params can be the Pyramids request.params multidict or just a dict.
877875 """
878- json_obj = None
879876 try :
880877 # check for kwarg data_field_name, or load the full request body
881878 if data_field_name in request_params :
0 commit comments