Coverage for nexusLIMS/extractors/plugins/basic_metadata.py: 100%
27 statements
« prev ^ index » next coverage.py v7.11.3, created at 2026-03-24 05:23 +0000
« prev ^ index » next coverage.py v7.11.3, created at 2026-03-24 05:23 +0000
1"""Basic metadata extractor plugin (fallback for unknown file types)."""
3import logging
4from datetime import datetime as dt
5from typing import Any, ClassVar
7from nexusLIMS.extractors.base import ExtractionContext
8from nexusLIMS.instruments import get_instr_from_filepath
9from nexusLIMS.utils.time import current_system_tz
11_logger = logging.getLogger(__name__)
14class BasicFileInfoExtractor:
15 """
16 Fallback extractor for files without a specific format handler.
18 This extractor provides basic metadata (creation time, file size, etc.)
19 for files that don't have a specialized extractor. It has the lowest
20 priority and will only be used if no other extractor supports the file.
21 """
23 name = "basic_file_info_extractor"
24 priority = 0 # Lowest priority - only used as fallback
25 supported_extensions: ClassVar = None # Wildcard extractor - supports all files
27 def supports(self, context: ExtractionContext) -> bool: # noqa: ARG002
28 """
29 Check if this extractor supports the given file.
31 This extractor always returns True since it's the fallback for all files.
33 Parameters
34 ----------
35 context
36 The extraction context containing file information
38 Returns
39 -------
40 bool
41 Always True (this is the fallback extractor)
42 """
43 return True
45 def extract(self, context: ExtractionContext) -> list[dict[str, Any]]:
46 """
47 Extract basic metadata from any file.
49 Provides minimal metadata such as modification time and instrument ID
50 for files that don't have a specialized extractor.
52 Parameters
53 ----------
54 context
55 The extraction context containing file information
57 Returns
58 -------
59 list[dict]
60 List containing a single metadata dict with 'nx_meta' key
61 """
62 _logger.debug(
63 "Extracting basic metadata from file (no specialized extractor): %s",
64 context.file_path,
65 )
67 mdict = {"nx_meta": {}}
68 mdict["nx_meta"]["DatasetType"] = "Unknown"
69 mdict["nx_meta"]["Data Type"] = "Unknown"
71 # get the modification time (as ISO format):
72 mtime = context.file_path.stat().st_mtime
73 # Use instrument timezone if available, otherwise fall back to system timezone
74 tz = context.instrument.timezone if context.instrument else current_system_tz()
75 mtime_iso = dt.fromtimestamp(mtime, tz=tz).isoformat()
76 mdict["nx_meta"]["Creation Time"] = mtime_iso
78 return [mdict]
81# Backward compatibility function for tests
82def get_basic_metadata(filename):
83 """
84 Get basic metadata from a file.
86 Returns basic metadata from a file that's not currently interpretable by NexusLIMS.
88 .. deprecated::
89 This function is deprecated. Use BasicFileInfoExtractor class instead.
91 Parameters
92 ----------
93 filename : pathlib.Path
94 path to a file saved in the harvested directory of the instrument
96 Returns
97 -------
98 mdict : dict
99 A description of the file in lieu of any metadata extracted from it.
100 """
101 context = ExtractionContext(
102 file_path=filename, instrument=get_instr_from_filepath(filename)
103 )
104 extractor = BasicFileInfoExtractor()
105 return extractor.extract(context)