Skip to content

Commit

Permalink
Enable TextFileReader filter and allow multiple text files
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanS94 committed Oct 28, 2024
1 parent ae8d4a1 commit 6730e47
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
48 changes: 32 additions & 16 deletions pycinema/filters/TextFileReader.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from pycinema import Filter

import csv
from os.path import exists
import os
import re
import logging as log

from pycinema import getTableExtent

class TextFileSource(Filter):

def __init__(self):
super().__init__(
inputs={
'path': ''
'table': [[]],
'file_column': 'FILE',
'cache': True
},
outputs={
'text': ''
Expand All @@ -19,22 +23,34 @@ def __init__(self):

def _update(self):

filePath = self.inputs.path.get()
table = self.inputs.table.get()
tableExtent = getTableExtent(table)
fileColumn = self.inputs.file_column.get()

if not filePath:
self.outputs.text.set("")
try:
fileColumnIdx = [i for i, item in enumerate(table[0]) if re.search(fileColumn, item, re.IGNORECASE)].pop()
except Exception as e:
log.error("table does not contain '" + fileColumn + "' column!")
return 0

if not exists(filePath):
log.error(" file not found: '" + filePath + "'")
self.outputs.text.set("")
return 0
text = ''
for i in range(1, len(table)):

try:
with open(filePath, 'r+') as textfile:
self.outputs.text.set(textfile.read())
except:
log.error(" Unable to open file: '" + filePath + "'")
self.outputs.text.set([[]])
return 0
row = table[i]
filePath = row[fileColumnIdx]

if not filePath:
text = text

if not os.path.exists(filePath):
log.error(" file not found: '" + filePath + "'")
text = text

try:
with open(filePath, 'r') as textfile:
text = text + '\n' + filePath + '\n' + textfile.read()
except:
log.error(" Unable to open file: '" + filePath + "'")
text = text
self.outputs.text.set(text)
return 1
1 change: 1 addition & 0 deletions pycinema/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .TableWriter import *
from .TableView import *
from .TextEditor import *
from .TextFileReader import *
from .ParametersView import *
from .ParallelCoordinates import *
from .InspectorView import *
Expand Down

0 comments on commit 6730e47

Please sign in to comment.