Skip to content

Commit

Permalink
Merge branch '101-new-hplc-reader' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
StarmanMartin committed May 29, 2024
2 parents 06c1cb8 + feec8ad commit 9c415cd
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: [ "3.10", "3.11" ]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -16,7 +16,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install wheel setuptools pip pybind11 --upgrade
pip install -r ./requirements/dev.txt
pip install pylint
- name: Analysing the code with pylint
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install wheel setuptools pip pybind11 --upgrade
pip install -r ./requirements/dev.txt
pip install pytest
- name: Build tests
Expand Down
75 changes: 75 additions & 0 deletions converter_app/readers/hplc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import logging
import os
import shutil
import tempfile
import tarfile
import hplc as ph
from converter_app.readers.helper.base import Reader
from converter_app.readers.helper.reader import Readers

logger = logging.getLogger(__name__)


class HplcReader(Reader):
"""
Reads tarballed hplc files with extension .tar.gz
"""
identifier = 'hplc_reader'
priority = 5

def __init__(self, file):
super().__init__(file)
self.df = None
self.temp_dir = None

def check(self):
"""
:return: True if it fits
"""
result = self.file.name.endswith(".tar.gz") or self.file.name.endswith(".tar")
if result:
with tempfile.TemporaryDirectory() as temp_dir:
self.temp_dir = temp_dir
with tempfile.NamedTemporaryFile(delete=True) as temp_pdf:
try:
# Save the contents of FileStorage to the temporary file
self.file.fp.save(temp_pdf.name)
if self.file.name.endswith("tar.gz"):
mode = "r:gz"
elif self.file.name.endswith("tar"):
mode = "r:"
else:
return False
with tarfile.open(temp_pdf.name, mode) as tar:
tar.extractall(self.temp_dir)
tar.close()

for p in os.listdir(self.temp_dir):
file_path = os.path.join(self.temp_dir, p)
self.df = ph.read_chromatograms(file_path)
break
except ValueError:
return False
if not result and self.temp_dir is not None and os.path.exists(self.temp_dir) and os.path.isdir(self.temp_dir):
shutil.rmtree(self.temp_dir)
return result

def prepare_tables(self):
tables = []
table = self.append_table(tables)

keys = list(self.df.keys())
values = [self.df[x] for x in keys]
for i in range(len(values[0])):
table['rows'].append([])
for x in values:
table['rows'][-1].append(float(x[i]))

table['columns'] = [{
'key': str(idx),
'name': f'{value}'
} for idx, value in enumerate(keys)]
return tables


Readers.instance().register(HplcReader)
3 changes: 2 additions & 1 deletion requirements/common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Werkzeug~=2.2.2
jcamp~=1.2.2
PyMuPDF==1.23.7
pylint==3.0.3
str2bool~=1.1
str2bool~=1.1
parser-binary @ git+https://github.com/ComPlat/BinaryParser@main

0 comments on commit 9c415cd

Please sign in to comment.