Skip to content

Commit

Permalink
Improve Dlis reading methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroKpaxo committed Sep 19, 2024
1 parent b110fc2 commit cd10144
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion test/dlis/test_dlis_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_search_files():
dlis_processor = DlisReader()
dlis_files = dlis_processor.search_files(folder_path)
assert len(dlis_files) == 2
assert dlis_files[0].name == '1PIR1AL_conv_ccl_canhoneio.dlis'
assert '1PIR1AL_conv_ccl_canhoneio.dlis' and '1PIR1AL_conv_ccl_canhoneio-error.dlis' in [file.name for file in dlis_files]

# Testing the search_files method with a wrong path
dlis_files = dlis_processor.search_files(folder_path / 'wrong_path')
Expand Down
2 changes: 1 addition & 1 deletion wellbelog/belodlis/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def open_dlis_file(file_path: str) -> dlis.PhysicalFile:
return dlis.load(file_path)

except Exception as e:
raise e
return e


def unpack_physical_dlis(ph_file: dlis.PhysicalFile) -> list[dlis.LogicalFile]:
Expand Down
46 changes: 25 additions & 21 deletions wellbelog/belodlis/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def load_raw(self, path_to_file: str, unpack=False) -> PhysicalFileModel:
PhysicalFileModel: The raw data.
"""
file = open_dlis_file(pathlib.Path(path_to_file).absolute())
if isinstance(file, Exception):
self.logger.error(f'Error while opening the DLIS file: {file}')
raise file

Check warning on line 35 in wellbelog/belodlis/reader.py

View check run for this annotation

Codecov / codecov/patch

wellbelog/belodlis/reader.py#L34-L35

Added lines #L34 - L35 were not covered by tests
if unpack:
return unpack_physical_dlis(file)
return file
Expand Down Expand Up @@ -63,23 +66,22 @@ def process_physical_file(self, path: str, folder_name: str = None) -> PhysicalF
self.logger.info(f'Processing the file: {file_name}')
# XXX Create a PhysicalFileModel object
physical = PhysicalFileModel(file_name=file_name, logical_files=[], folder_name=folder_name)
# Open the DLIS file or raise an exception
file = open_dlis_file(pathlib.Path(path).absolute())
if isinstance(file, Exception):
self.logger.error(f'Error while opening the DLIS file: {file}')
physical.error = True
physical.error_message = file.__str__()
return physical

# NOTE This is to be used on a batch processing
try:
# Open the DLIS file or raise an exception
file = open_dlis_file(pathlib.Path(path).absolute())
logical_files = unpack_physical_dlis(file)

# Process each logical file
for file in logical_files:

logical_files = unpack_physical_dlis(file)
# Process each logical file
for file in logical_files:
logical_file = LogicalFileModel(file_name=file_name, frames=[])
try:
logical_file.logical_id = file.fileheader.id
logical_file.summary = get_logical_file_summary(file)
frames: list[Frame] = file.find('FRAME')
logical_file = LogicalFileModel(
file_name=file_name,
logical_id=file.fileheader.id,
summary=get_logical_file_summary(file),
frames=[]
)

# If there are no frames, set the error flag and the error message
if not frames:
Expand Down Expand Up @@ -108,6 +110,7 @@ def process_physical_file(self, path: str, folder_name: str = None) -> PhysicalF
# Check if the data is an exception
# If it is, set the error flag and the error message
if data is Exception:
self.logger.error(f'Error while processing the frame: {data}')

Check warning on line 113 in wellbelog/belodlis/reader.py

View check run for this annotation

Codecov / codecov/patch

wellbelog/belodlis/reader.py#L113

Added line #L113 was not covered by tests
frame_model.error = True
frame_model.error_message = data.__str__()
logical_file.error = True
Expand All @@ -129,10 +132,11 @@ def process_physical_file(self, path: str, folder_name: str = None) -> PhysicalF
# Append the logical file to the physical file
physical.logical_files.append(logical_file)

return physical
return physical

except Exception as e:
self.logger.error(f'Error while processing the physical file: {e}')
physical.error = True
physical.error_message = str(e)
return physical
except Exception as e:
self.logger.error(f'Error while processing the logical file: {e}')
logical_file.error = True
logical_file.error_message = e.__str__()
physical.error_files.append(logical_file)
continue

Check warning on line 142 in wellbelog/belodlis/reader.py

View check run for this annotation

Codecov / codecov/patch

wellbelog/belodlis/reader.py#L137-L142

Added lines #L137 - L142 were not covered by tests
4 changes: 2 additions & 2 deletions wellbelog/schemas/dlis.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class LogicalFileModel(TimeStampedModelSchema):
"""

file_name: str = Field(..., description="The name of the file.")
logical_id: Any = Field(..., description="The id of the logical file.")
summary: LogicalFileSummary = Field(..., description="The summary of the file.")
logical_id: Optional[Any] = Field(None, description="The id of the logical file.")
summary: Optional[LogicalFileSummary] = Field(None, description="The summary of the file.")
frames: list[FrameModel] = Field(None, description="The frames of the file.")
error: bool = Field(False, description="If the file has any error during opening.")
error_message: Optional[str] = Field(None, description="The error exception if any.")
Expand Down

0 comments on commit cd10144

Please sign in to comment.