gnu-keywords/datareader.py

44 lines
1.1 KiB
Python
Raw Normal View History

2022-05-18 18:51:42 +02:00
#
# <one line to give the program's name and a brief idea of what it does.>
# SPDX-FileCopyrightText: 2022 Beat Jäckle <beat@git,jdmweb2.ch>
# SPDX-License-Identifier: GPL-3.0-or-later
#
from os import listdir
class DataReader:
def __init__(self, datadir,
indexname='index.txt',
skippFirstLine=True
):
if datadir[-1] != '/':
datadir += '/'
self.datadir = datadir
self.skippFirstLine = skippFirstLine
self.__readTree__()
self.indexname = indexname
def __readTree__(self):
self.articles = listdir(self.datadir)
def __iter__(self):
self.indexnext = 0
return self
def __next__(self):
try:
articlename = self.articles[self.indexnext]
path = self.datadir +\
articlename +\
'/'+self.indexname
self.indexnext += 1
except IndexError:
raise StopIteration
return None
with open(path, "r") as f:
if self.skippFirstLine:
f.readline()
return [articlename, f.read()]