Skip to content

Commit

Permalink
FileHandler match the filename caseinsensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Kraft committed Mar 26, 2021
1 parent 564c305 commit 1dea506
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions odmf/webpage/filemanager/filehandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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<pre>\n' + f.read() + '\n</pre>\n'


class ImageFileHandler(BaseFileHandler):

def to_html(self, path: Path) -> str:
return f'<img src="{path.raw_url}"/>'
return f'''
<img class="handler-generated" src="{path.raw_url}" style="max-width: 100%"/>
'''


class PdfFileHandler(BaseFileHandler):
Expand All @@ -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:
Expand All @@ -108,4 +123,4 @@ def to_html(self, path: Path) -> str:
raise
except UnicodeDecodeError as e:
pass
return
return None

0 comments on commit 1dea506

Please sign in to comment.