2
2
Database helper functions for API
3
3
"""
4
4
5
+ import logging
5
6
import re
6
7
import shutil
7
8
import tempfile
15
16
from openfoodfacts_taxonomy_parser import unparser # Unparser for taxonomies
16
17
from openfoodfacts_taxonomy_parser import utils as parser_utils
17
18
18
- from . import settings
19
+ from . import settings , utils
19
20
from .controllers .node_controller import create_entry_node
20
21
from .controllers .project_controller import create_project , edit_project , get_project
21
22
from .exceptions import GithubBranchExistsError # Custom exceptions
33
34
)
34
35
from .models .node_models import EntryNodeCreate
35
36
from .models .project_models import ProjectCreate , ProjectEdit , ProjectStatus
36
- from .utils import file_cleanup
37
+
38
+ log = logging .getLogger (__name__ )
37
39
38
40
39
41
async def async_list (async_iterable ):
@@ -48,6 +50,10 @@ def __init__(self, branch_name, taxonomy_name):
48
50
self .branch_name = branch_name
49
51
self .project_name = "p_" + taxonomy_name + "_" + branch_name
50
52
53
+ @property
54
+ def taxonomy_path_in_repository (self ):
55
+ return utils .taxonomy_path_in_repository (self .taxonomy_name )
56
+
51
57
def get_label (self , id ):
52
58
"""
53
59
Helper function for getting the label for a given id
@@ -82,13 +88,13 @@ async def get_local_taxonomy_file(self, tmpdir: str, uploadfile: UploadFile):
82
88
83
89
async def get_github_taxonomy_file (self , tmpdir : str ):
84
90
async with TransactionCtx ():
85
- filename = f"{ self .taxonomy_name } .txt"
86
- filepath = f" { tmpdir } / { filename } "
87
- base_url = (
88
- "https://raw.githubusercontent.com/" + settings . repo_uri + "/ main/taxonomies/ "
91
+ filepath = f"{ tmpdir } / { self .taxonomy_name } .txt"
92
+ target_url = (
93
+ f"https://raw.githubusercontent.com/ { settings . repo_uri } "
94
+ f"/ main/{ self . taxonomy_path_in_repository } "
89
95
)
90
96
try :
91
- await run_in_threadpool (urllib .request .urlretrieve , base_url + filename , filepath )
97
+ await run_in_threadpool (urllib .request .urlretrieve , target_url , filepath )
92
98
github_object = GithubOperations (self .taxonomy_name , self .branch_name )
93
99
commit_sha = (await github_object .get_branch ("main" )).commit .sha
94
100
file_sha = await github_object .get_file_sha ()
@@ -131,6 +137,7 @@ async def get_and_parse_taxonomy(self, uploadfile: UploadFile | None = None):
131
137
# add an error node so we can display it with errors in the app
132
138
async with TransactionCtx ():
133
139
await edit_project (self .project_name , ProjectEdit (status = ProjectStatus .FAILED ))
140
+ log .exception (e )
134
141
raise e
135
142
136
143
async def import_taxonomy (
@@ -169,9 +176,10 @@ def dump_taxonomy(self, background_tasks: BackgroundTasks):
169
176
# Dump taxonomy with given file name and branch name
170
177
unparser_object (filename , self .branch_name , self .taxonomy_name )
171
178
# Program file removal in the background
172
- background_tasks .add_task (file_cleanup , filename )
179
+ background_tasks .add_task (utils . file_cleanup , filename )
173
180
return filename
174
181
except Exception as e :
182
+ log .exception (e )
175
183
raise TaxonomyUnparsingError () from e
176
184
177
185
async def file_export (self , background_tasks : BackgroundTasks ):
0 commit comments