Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scicat metadata parsing #218

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions rfi_file_monitor/metadataparser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from abc import ABC, abstractmethod


class MetadataParser(ABC):
"""Each file will need it's own metadata parser, as every import library works in different ways.
This class aims to standardise the function names across each file loader."""

NAME = "Metadataparser"

def __init__(self):
self.metadata = []

@classmethod
@abstractmethod
def supports_file(cls, file: str):
"""This method allows you to add the file types that the method supports"""
raise NotImplementedError

@classmethod
@abstractmethod
def extract_metadata(cls, capture_metadata, file):
"""extracts metadata, for a given set of vars"""
raise NotImplementedError
40 changes: 40 additions & 0 deletions rfi_file_monitor/metadataparsers/csv_metadata_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from rfi_file_monitor.metadataparser import MetadataParser
from itertools import takewhile
import re
import logging

logger = logging.getLogger(__name__)


class CsvMetadataParser(MetadataParser):
NAME = "CSV Parser"

@classmethod
def supports_file(cls, file: str):
if file.endswith("csv") or file.endswith("tsv"):
return True
return False

@classmethod
def extract_metadata(cls, capture_metadata, file):
# check for metadata stored as comments

with open(file, "r") as fobj:
# takewhile returns an iterator over all the lines
# that start with the comment string
headiter = takewhile(lambda s: s.startswith("#"), fobj)
# you may want to process the headers differently,
# but here we just convert it to a list
metadata = list(headiter)

scicat_metadata = {}
for line in metadata:
line = line.strip("#")
line = line.strip("\n")
for i in capture_metadata:
if i in line:
splitline = re.split(r'[ ,|;"=]+', line)
splitline = [part for part in splitline if part != ""]
scicat_metadata[splitline[0]] = splitline[1]

return scicat_metadata
Loading