nicobot/nicobot/helpers.py
nicobo 698335d3b9
+ helpers.py (filter_files)
- --language (only --language-file is really useful)
+ specific case to translate into a given langaue (fixes #3)
2020-05-13 23:53:12 +02:00

30 lines
781 B
Python

# -*- coding: utf-8 -*-
"""
Helper functions
"""
def filter_files( files, should_exist=False, fallback_to=None ):
"""
files: a list of filenames / open files to filter
should_exist: filters out non-existing files
fallback_to: if the result would be an empty list, add the entry with this index in the result ; ignored if None
Returns : a list with only the files that passed the filters
"""
found = []
for file in files:
if should_exist:
try:
with open(file,'r') as f:
pass
except:
continue
found = found + [file]
if len(found) == 0 and fallback_to is not None:
return files[fallback_to:fallback_to+1]
return found