# -*- coding: utf-8 -*-
import os
import egaia_root
import egaia_parsefn
[docs]def applyExclude(name, exclude):
"""Return True or False to indicate whether a given filename matches
a list of exclusion patterns."""
for pattern in exclude:
if str(pattern) in name:
return False
return True
[docs]def matchFilter(name, filter_type):
"""Return True or False to indicate whether a given filename matches
a filter."""
fileparts = egaia_parsefn.parseFilename(name)
if filter_type == 'tagged' and fileparts[1] is not None:
return True
elif filter_type == 'untagged' and fileparts[1] is None:
return True
# Originals: match "orig*"
elif 'orig' in filter_type and fileparts[1] is not None and not '.df-' in fileparts[0] and not '.pf-' in fileparts[0] and not '.metadata' in fileparts[0]:
return True
elif filter_type and fileparts[1] is not None and filter_type.startswith('df') or filter_type.startswith('pf') or filter_type.startswith('metadata'):
if filter_type in fileparts[0]:
return True
return False
[docs]def setFilepath():
try:
filepath = os.path.join(egaia_root.get_root(), 'data')
except:
exit("Could not find root")
return filepath
[docs]def decodeName(name):
# https://stackoverflow.com/a/23841227
if type(name) == str: # leave unicode ones alone
try:
name = name.decode('utf8')
except:
name = name.decode('windows-1252')
return name
[docs]def listDirs(filepath=None, filter_type=None, uuid=None, exclude=None):
"""List directories that can be tagged"""
if not filepath:
filepath = setFilepath()
dirs_list = list()
for path, dirs, files in os.walk(filepath):
for d in dirs:
if filter_type and matchFilter(d, filter_type) is False:
continue
if uuid and not uuid in d:
continue
if exclude and applyExclude(d, exclude) is False:
#print "excluding %s, pattern %s" % (name, exclude)
continue
if d.endswith('.dir') or d.endswith('.vclips'):
dirs_list.append(os.path.join(path,d))
# return an empty list if nothing found
return dirs_list
[docs]def listFiles(filepath=None, filter_type=None, uuid=None,
exclude=None, include_dirs=False):
"""List the files in a collection."""
if not filepath:
filepath = setFilepath()
files_list = list()
# list all the files
for path, dirs, files in os.walk(filepath):
if include_dirs is False:
# don't recurse into video stills or web page directories
# this function is used by egaia_tag!
dirs[:] = [d for d in dirs if not d.endswith('.dir') and not d.endswith('.vclips')]
elif filter_type:
# we want to filter "special" directories
dirs[:] = [d for d in dirs if matchFilter(os.path.basename(d), filter_type)]
for name in files:
name = decodeName(name)
# match uuid if specified
if uuid and not uuid in name:
continue
# check exclusion list
if exclude and applyExclude(name, exclude) is False:
continue
# apply filters, but NOT within "special" directories
if filter_type and not '.dir' in path and not '.vclips' in path:
if matchFilter(name, filter_type) is False:
continue
files_list.append(os.path.join(path, name))
# return an empty list if nothing found
return files_list
[docs]def listItems(filepath=None):
"""listItems(filepath=top)
List the UUIDs corresponding to the items in a collection."""
if not filepath:
filepath = setFilepath()
uuids = list()
# list all the files
for path, dirs, files in os.walk(filepath):
# don't recurse into video stills or web page directories
dirs[:] = [d for d in dirs if not d.endswith('.dir') and not d.endswith('.vclips')]
for name in files:
uuid = egaia_parsefn.getUuid(name)
if uuid and not uuid in uuids:
uuids.append(uuid)
return uuids
[docs]def countItems(filepath=None):
"""Provide a count of the items in a collection."""
if not filepath:
filepath = setFilepath()
uuids = listItems(filepath)
return len(uuids)
def _cli(args):
"""egaia list
List all the files in the current collection. Optionally filter the list
of file paths to include only tagged or untagged files. The output of this
utility is intended to be piped into other tools, notably "egaia tag".
Usage:
egaia list --help
egaia list --items
egaia list --count
egaia list --files [ --include-dirs ] [ --filter=FILTER ] [ --uuid=UUID ]
[ --exclude=EXCLUDE... ]
"""
if not egaia_root.get_root():
exit('Please run "egaia init" to create a new collection here.')
if args['--count']:
print countItems()
return
u_list = list()
if args['--files']:
u_list = listFiles(filter_type=args['--filter'], uuid=args['--uuid'],
exclude=args['--exclude'], include_dirs=args['--include-dirs'])
if args['--items']:
u_list = listItems()
for element in u_list:
print element