Skip to content

Commit

Permalink
feat: Tiff file reader. (#77)
Browse files Browse the repository at this point in the history
Used Irregular tags which are parsed from the file
  • Loading branch information
StarmanMartin authored Jan 25, 2024
1 parent 48564f6 commit dd676b7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions converter_app/readers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .dwl import DWLReader
from .ebl import EblReader
from .cfx import CfxReader
from .tif import TifReader
from .jcamp_conv_reader import JcampReader
from .pdf import PdfReader
from .lithoz_pdf import PdfLithozReader
Expand Down Expand Up @@ -85,6 +86,7 @@ def match_reader(self, file):
registry.register(DWLReader)
registry.register(EblReader)
registry.register(CfxReader)
registry.register(TifReader)
registry.register(JcampReader)
registry.register(PdfReader)
registry.register(PdfLithozReader)
Expand Down
66 changes: 66 additions & 0 deletions converter_app/readers/tif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import logging
import re
from .base import Reader

logger = logging.getLogger(__name__)

UNIT_EXTENSION = "_unit"


class TifReader(Reader):
identifier = 'tif_reader'
priority = 96
_parsed_values = None


def check(self):
result = False
if self.file.suffix.lower() == '.tif' and self.file.mime_type == 'image/tiff':
self._parsed_values = self._read_img()
result = self._parsed_values is not None and len(self._parsed_values) > 0
logger.debug('result=%s', result)
return result
def _read_img(self):
txt = re.sub(r'\\x[0-9a-f]{2}', '', self.file.content.__str__())

txt = re.sub(r'^.+@@@@@@0\\r\\n', '', txt)
lines = re.split(r'\\r\\n', txt)
del lines[-1]
return [x.split('=') for x in lines]


def get_value(self, value):
if self.float_de_pattern.match(value):
# remove any digit group seperators and replace the comma with a period
return value.replace('.', '').replace(',', '.')
if self.float_us_pattern.match(value):
# just remove the digit group seperators
return value.replace(',', '')
else:
return None

def get_tables(self):
tables = []
table = self.append_table(tables)
for val in self._parsed_values:
if len(val) == 1:
num_val = self.get_value(val[0])
if num_val is not None:
table['rows'].append([len(table['rows']), len(table['rows']), float(num_val)])
else:
table['metadata'][val[0]] = '='.join(val[1:])
table['header'].append(f"{'='.join(val)}")

table['columns'].append({
'key': '1',
'name': 'Idx'
})
table['columns'].append({
'key': '2',
'name': 'Number'
})

table['metadata']['rows'] = str(len(table['rows']))
table['metadata']['columns'] = str(len(table['columns']))

return tables

0 comments on commit dd676b7

Please sign in to comment.