Skip to content
Snippets Groups Projects

Optimize handling of problematic files

Merged jvoisin requested to merge optimize_handling_of_problematic_files into master
Files
4
+ 9
19
import json
import os
import re
import shutil
import subprocess
import tempfile
from typing import Dict, Union, Set, Callable, Any
from typing import Dict, Union, Set
from . import abstract
@@ -13,6 +10,8 @@ from . import abstract
assert Set
class ExiftoolParser(abstract.AbstractParser):
""" Exiftool is often the easiest way to get all the metadata
from a import file, hence why several parsers are re-using its `get_meta`
@@ -20,23 +19,14 @@ class ExiftoolParser(abstract.AbstractParser):
"""
meta_whitelist = set() # type: Set[str]
def _handle_problematic_filename(self, callback: Callable[[str], Any]) -> bytes:
""" This method takes a filename with a potentially problematic name,
and safely applies a `callback` to it.
"""
if re.search('^[a-z0-9/]', self.filename) is not None:
return callback(self.filename)
tmpdirname = tempfile.mkdtemp()
fname = os.path.join(tmpdirname, "temp_file")
shutil.copy(self.filename, fname)
out = callback(fname)
shutil.rmtree(tmpdirname)
return out
def __init__(self, filename: str):
super().__init__(filename)
if re.search('^[a-z0-9./]', filename) is None:
raise ValueError("mat2 doesn't support this filetype with a \
leading non-alphanumeric character")
def get_meta(self) -> Dict[str, Union[str, dict]]:
fun = lambda f: subprocess.check_output([_get_exiftool_path(), '-json', f])
out = self._handle_problematic_filename(fun)
out = subprocess.check_output([_get_exiftool_path(), '-json', self.filename])
meta = json.loads(out.decode('utf-8'))[0]
for key in self.meta_whitelist:
meta.pop(key, None)
Loading