"""Path helper to the model.
Part of the tagit module.
A copy of the license is provided with the project.
Author: Matthias Baumgartner, 2016
"""
# imports
import os.path
# inner-module imports
from ..basics import fst
[docs]class DBpath(object):
"""Path operations on database entries.
"""
# !! use os.path vars: curdir = '.', extsep = '.', pardir = '..', pathsep = ':', sep = '/'
def __init__(self, settings, database):
self.settings = settings
self.conn = database
[docs] def is_dir(self, path):
"""Check if *path* is a directory.
.. TODO::
Implement
"""
# TODO: ????
# *path* found in the database but not uniquely (sure criteria)
# ends with a path sep
# exists in the file system
#
raise NotImplementedError()
[docs] def is_file(self, path):
"""Check if *path* is a file.
.. TODO::
Implement
"""
raise NotImplementedError()
if self.exists(path):
return True
# TODO: What if it's not in the database, but *path* might still be a file??
return False
[docs] def walk(self, top):
"""Recursively walk through directory structure from a *top* directory.
Example:
>>> for root, files, dirs in walk(top):
>>> print root, files, dirs
>>> # root is the base directory
>>> # files is a list of file names in root
>>> # dirs is a list of directory names in root
"""
search_dirs = [top]
while len(search_dirs) > 0:
root = search_dirs.pop(0)
dirs, files = self.getdir(root)
yield (root, files, dirs)
search_dirs.extend(map(lambda s: self.join(root, s) , dirs))
[docs] def is_temp(self, path):
"""Return True if *path* is within a temporary location.
"""
temps = self.settings.trace('session', 'temporary', [])
for temp in temps:
if self.is_prefix(temp, path):
return True
return False
[docs] def join(self, *parts):
"""Join parts of a path.
"""
return os.path.join(*parts)
[docs] def exists(self, path):
"""Return whether *path* exists in the database.
*path* should be a file. For directories, the result is always False because
directories are not mapped in the database.
"""
result = self.conn.execute("SELECT id FROM image WHERE path = ?", (path, )).fetchone()
return result is not None
[docs] def is_prefix(self, prefix, path):
"""Return True if *path* is a subdirectory of *prefix* or a file within *prefix*.
"""
return path.startswith(prefix)
def _normpath(self, path):
"""Path normalization."""
# Remove trailing seperator
if path[-1] == os.path.sep:
path = path[:-1]
return path
[docs] def listfiles(self, path):
"""List files of directory *path*.
*path* must be a directory.
"""
path = self._normpath(path) + '{0}[^{0}]+$'.format(os.path.sep)
files = map(fst, self.conn.execute("SELECT path FROM image WHERE path REGEXP ?", (path, )).fetchall())
return files
[docs] def getdir(self, path):
"""List contents of directory *path*.
*path* must be a directory.
Files and directories are listed, without '.' and '..'
"""
path = self._normpath(path) + os.path.sep
content = self.conn.execute("SELECT path FROM image WHERE path LIKE ?", (path + '%', )).fetchall()
if content is None: return []
content = map(lambda (s,): s[len(path):], content) # Strip *path*
dirs = filter(lambda s: s.find(os.path.sep) >= 0, content)
dirs = list(set(map(lambda s: s[:s.find(os.path.sep)], dirs))) # Strip anything after the seperator
files = filter(lambda s: s.find(os.path.sep) == -1, content)
return (dirs, files)
[docs] def listdir(self, path):
"""List contents of directory *path*.
*path* must be a directory.
Files and directories are listed, without '.' and '..'
"""
path = self._normpath(path) + os.path.sep
content = self.conn.execute("SELECT path FROM image WHERE path LIKE ?", (path + '%', )).fetchall()
if content is None: return []
content = map(lambda (s,): s[len(path):], content) # Strip *path*
dirs = filter(lambda s: s.find(os.path.sep) >= 0, content)
dirs = list(set(map(lambda s: s[:s.find(os.path.sep)], dirs))) # Strip anything after the seperator
files = filter(lambda s: s.find(os.path.sep) == -1, content)
return files + dirs
## EOF ##