From 1dea5069171869841b6f941643c146331a6d2f25 Mon Sep 17 00:00:00 2001 From: Philipp Kraft Date: Fri, 26 Mar 2021 14:32:24 +0100 Subject: [PATCH] FileHandler match the filename caseinsensitive --- odmf/webpage/filemanager/filehandlers.py | 35 +++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/odmf/webpage/filemanager/filehandlers.py b/odmf/webpage/filemanager/filehandlers.py index d38d2508..e2309b7d 100644 --- a/odmf/webpage/filemanager/filehandlers.py +++ b/odmf/webpage/filemanager/filehandlers.py @@ -11,13 +11,13 @@ class BaseFileHandler: def __init__(self, pattern: str = ''): - self.pattern = re.compile(pattern) + self.pattern = re.compile(pattern, re.IGNORECASE) def matches(self, path: Path): """ Checks if a path matches the file pattern """ - return bool(self.pattern.match(path.absolute)) + return bool(self.pattern.search(path.absolute)) def to_html(self, path) -> str: """ @@ -75,10 +75,24 @@ def to_html(self, path: Path) -> str: return html +class CsvFileHandler(BaseFileHandler): + + def to_html(self, path: Path) -> str: + import pandas as pd + try: + df = pd.read_csv(path.absolute, sep=None) + return df.to_html(classes=['table']) + except: + with open(path.absolute, 'r') as f: + return '\n
\n' + f.read() + '\n
\n' + + class ImageFileHandler(BaseFileHandler): def to_html(self, path: Path) -> str: - return f'' + return f''' + + ''' class PdfFileHandler(BaseFileHandler): @@ -91,12 +105,13 @@ def to_html(self, path) -> str: class MultiHandler(BaseFileHandler): handlers = [ - MarkDownFileHandler(r'.*\.(md|wiki)'), - PlotFileHandler(r'.*\.plot'), - ExcelFileHandler(r'.*\.xls?'), - PdfFileHandler(r'.*\.pdf'), - ImageFileHandler(r'.*\.(jpg|jpeg|png|svg|gif)'), - TextFileHandler('.*'), + MarkDownFileHandler(r'\.(md|wiki)'), + PlotFileHandler(r'\.plot'), + ExcelFileHandler(r'\.xls?'), + CsvFileHandler(r'\.csv'), + PdfFileHandler(r'\.pdf'), + ImageFileHandler(r'\.(jpg|jpeg|png|svg|gif)'), + TextFileHandler(''), ] def to_html(self, path: Path) -> str: @@ -108,4 +123,4 @@ def to_html(self, path: Path) -> str: raise except UnicodeDecodeError as e: pass - return + return None