2009-06-18 18:37:53 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2006/02/27
|
2013-04-28 14:35:51 +00:00
|
|
|
# Copyright 2013 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
#
|
2010-09-30 10:17:41 +00:00
|
|
|
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2009-08-05 08:59:46 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
# http://www.hardcoded.net/licenses/bsd_license
|
2009-06-18 18:37:53 +00:00
|
|
|
|
2010-08-15 12:42:55 +00:00
|
|
|
from xml.etree import ElementTree as ET
|
2011-01-26 11:50:44 +00:00
|
|
|
import logging
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-07-11 18:18:55 +00:00
|
|
|
from jobprogress import job
|
2011-01-11 10:59:53 +00:00
|
|
|
from hscommon.path import Path
|
2011-01-11 12:36:05 +00:00
|
|
|
from hscommon.util import FileOrPath
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-10-23 12:56:52 +00:00
|
|
|
from . import fs
|
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
__all__ = [
|
|
|
|
'Directories',
|
|
|
|
'DirectoryState',
|
|
|
|
'AlreadyThereError',
|
|
|
|
'InvalidPathError',
|
|
|
|
]
|
|
|
|
|
2011-04-12 11:22:29 +00:00
|
|
|
class DirectoryState:
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Enum describing how a folder should be considered.
|
|
|
|
|
|
|
|
* DirectoryState.Normal: Scan all files normally
|
|
|
|
* DirectoryState.Reference: Scan files, but make sure never to delete any of them
|
|
|
|
* DirectoryState.Excluded: Don't scan this folder
|
|
|
|
"""
|
2011-04-12 11:22:29 +00:00
|
|
|
Normal = 0
|
|
|
|
Reference = 1
|
|
|
|
Excluded = 2
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
class AlreadyThereError(Exception):
|
|
|
|
"""The path being added is already in the directory list"""
|
|
|
|
|
|
|
|
class InvalidPathError(Exception):
|
|
|
|
"""The path being added is invalid"""
|
|
|
|
|
2011-01-26 11:50:44 +00:00
|
|
|
class Directories:
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Holds user folder selection.
|
|
|
|
|
|
|
|
Manages the selection that the user make through the folder selection dialog. It also manages
|
|
|
|
folder states, and how recursion applies to them.
|
|
|
|
|
|
|
|
Then, when the user starts the scan, :meth:`get_files` is called to retrieve all files (wrapped
|
|
|
|
in :mod:`core.fs`) that have to be scanned according to the chosen folders/states.
|
|
|
|
"""
|
2009-06-01 09:55:11 +00:00
|
|
|
#---Override
|
2009-10-23 12:56:52 +00:00
|
|
|
def __init__(self, fileclasses=[fs.File]):
|
2009-06-01 09:55:11 +00:00
|
|
|
self._dirs = []
|
|
|
|
self.states = {}
|
2009-10-23 12:56:52 +00:00
|
|
|
self.fileclasses = fileclasses
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-10-23 12:56:52 +00:00
|
|
|
def __contains__(self, path):
|
|
|
|
for p in self._dirs:
|
|
|
|
if path in p:
|
2009-06-01 09:55:11 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __delitem__(self,key):
|
|
|
|
self._dirs.__delitem__(key)
|
|
|
|
|
|
|
|
def __getitem__(self,key):
|
|
|
|
return self._dirs.__getitem__(key)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._dirs)
|
|
|
|
|
|
|
|
#---Private
|
2009-06-09 15:35:17 +00:00
|
|
|
def _default_state_for_path(self, path):
|
|
|
|
# Override this in subclasses to specify the state of some special folders.
|
|
|
|
if path[-1].startswith('.'): # hidden
|
2011-04-12 11:22:29 +00:00
|
|
|
return DirectoryState.Excluded
|
2009-06-09 15:35:17 +00:00
|
|
|
|
2011-07-11 18:18:55 +00:00
|
|
|
def _get_files(self, from_path, j):
|
|
|
|
j.check_if_cancelled()
|
2009-06-18 18:37:53 +00:00
|
|
|
state = self.get_state(from_path)
|
2011-04-12 11:22:29 +00:00
|
|
|
if state == DirectoryState.Excluded:
|
2009-06-09 15:35:17 +00:00
|
|
|
# Recursively get files from folders with lots of subfolder is expensive. However, there
|
|
|
|
# might be a subfolder in this path that is not excluded. What we want to do is to skim
|
|
|
|
# through self.states and see if we must continue, or we can stop right here to save time
|
|
|
|
if not any(p[:len(from_path)] == from_path for p in self.states):
|
|
|
|
return
|
2009-10-23 12:56:52 +00:00
|
|
|
try:
|
2009-10-23 13:46:18 +00:00
|
|
|
filepaths = set()
|
2011-04-12 11:22:29 +00:00
|
|
|
if state != DirectoryState.Excluded:
|
2011-01-26 11:50:44 +00:00
|
|
|
found_files = fs.get_files(from_path, fileclasses=self.fileclasses)
|
2011-04-12 11:22:29 +00:00
|
|
|
logging.debug("Collected %d files in folder %s", len(found_files), str(from_path))
|
2011-01-26 11:50:44 +00:00
|
|
|
for file in found_files:
|
2011-04-12 11:22:29 +00:00
|
|
|
file.is_ref = state == DirectoryState.Reference
|
2009-10-23 13:46:18 +00:00
|
|
|
filepaths.add(file.path)
|
|
|
|
yield file
|
2012-08-09 14:53:24 +00:00
|
|
|
subpaths = [from_path + name for name in from_path.listdir()]
|
2009-10-23 13:46:18 +00:00
|
|
|
# it's possible that a folder (bundle) gets into the file list. in that case, we don't want to recurse into it
|
2012-08-09 14:53:24 +00:00
|
|
|
subfolders = [p for p in subpaths if not p.islink() and p.isdir() and p not in filepaths]
|
2009-10-23 13:46:18 +00:00
|
|
|
for subfolder in subfolders:
|
2011-07-11 18:18:55 +00:00
|
|
|
for file in self._get_files(subfolder, j):
|
2009-10-23 12:56:52 +00:00
|
|
|
yield file
|
|
|
|
except (EnvironmentError, fs.InvalidPath):
|
|
|
|
pass
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-07-11 18:18:55 +00:00
|
|
|
def _get_folders(self, from_folder, j):
|
|
|
|
j.check_if_cancelled()
|
2011-04-12 11:22:29 +00:00
|
|
|
try:
|
|
|
|
for subfolder in from_folder.subfolders:
|
2011-07-11 18:18:55 +00:00
|
|
|
for folder in self._get_folders(subfolder, j):
|
2011-04-12 11:22:29 +00:00
|
|
|
yield folder
|
2011-04-14 13:37:12 +00:00
|
|
|
state = self.get_state(from_folder.path)
|
2011-04-12 11:22:29 +00:00
|
|
|
if state != DirectoryState.Excluded:
|
|
|
|
from_folder.is_ref = state == DirectoryState.Reference
|
2011-04-14 13:37:12 +00:00
|
|
|
logging.debug("Yielding Folder %r state: %d", from_folder, state)
|
2011-04-12 11:22:29 +00:00
|
|
|
yield from_folder
|
|
|
|
except (EnvironmentError, fs.InvalidPath):
|
|
|
|
pass
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
#---Public
|
|
|
|
def add_path(self, path):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Adds ``path`` to self, if not already there.
|
|
|
|
|
|
|
|
Raises :exc:`AlreadyThereError` if ``path`` is already in self. If path is a directory
|
|
|
|
containing some of the directories already present in self, ``path`` will be added, but all
|
|
|
|
directories under it will be removed. Can also raise :exc:`InvalidPathError` if ``path``
|
|
|
|
does not exist.
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
:param Path path: path to add
|
2009-06-01 09:55:11 +00:00
|
|
|
"""
|
|
|
|
if path in self:
|
2009-10-23 12:56:52 +00:00
|
|
|
raise AlreadyThereError()
|
2012-08-09 14:53:24 +00:00
|
|
|
if not path.exists():
|
2009-06-18 18:37:53 +00:00
|
|
|
raise InvalidPathError()
|
2009-10-23 12:56:52 +00:00
|
|
|
self._dirs = [p for p in self._dirs if p not in path]
|
|
|
|
self._dirs.append(path)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_subfolders(path):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Returns a sorted list of paths corresponding to subfolders in ``path``.
|
|
|
|
|
|
|
|
:param Path path: get subfolders from there
|
|
|
|
:rtype: list of Path
|
|
|
|
"""
|
2009-10-23 12:56:52 +00:00
|
|
|
try:
|
2012-08-09 14:53:24 +00:00
|
|
|
names = [name for name in path.listdir() if (path + name).isdir()]
|
2009-10-23 12:56:52 +00:00
|
|
|
names.sort(key=lambda x:x.lower())
|
|
|
|
return [path + name for name in names]
|
|
|
|
except EnvironmentError:
|
|
|
|
return []
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-07-11 18:18:55 +00:00
|
|
|
def get_files(self, j=job.nulljob):
|
2009-06-01 09:55:11 +00:00
|
|
|
"""Returns a list of all files that are not excluded.
|
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
Returned files also have their ``is_ref`` attr set if applicable.
|
2009-06-01 09:55:11 +00:00
|
|
|
"""
|
2009-10-23 12:56:52 +00:00
|
|
|
for path in self._dirs:
|
2011-07-11 18:18:55 +00:00
|
|
|
for file in self._get_files(path, j):
|
2009-10-23 12:56:52 +00:00
|
|
|
yield file
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-07-11 18:18:55 +00:00
|
|
|
def get_folders(self, j=job.nulljob):
|
2011-04-12 11:22:29 +00:00
|
|
|
"""Returns a list of all folders that are not excluded.
|
|
|
|
|
2013-08-18 22:36:09 +00:00
|
|
|
Returned folders also have their ``is_ref`` attr set if applicable.
|
2011-04-12 11:22:29 +00:00
|
|
|
"""
|
|
|
|
for path in self._dirs:
|
|
|
|
from_folder = fs.Folder(path)
|
2011-07-11 18:18:55 +00:00
|
|
|
for folder in self._get_folders(from_folder, j):
|
2011-04-12 11:22:29 +00:00
|
|
|
yield folder
|
|
|
|
|
2009-06-18 18:37:53 +00:00
|
|
|
def get_state(self, path):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Returns the state of ``path``.
|
|
|
|
|
|
|
|
:rtype: :class:`DirectoryState`
|
2009-06-01 09:55:11 +00:00
|
|
|
"""
|
2009-06-18 18:37:53 +00:00
|
|
|
if path in self.states:
|
2009-06-01 09:55:11 +00:00
|
|
|
return self.states[path]
|
2009-06-18 18:37:53 +00:00
|
|
|
default_state = self._default_state_for_path(path)
|
|
|
|
if default_state is not None:
|
|
|
|
return default_state
|
|
|
|
parent = path[:-1]
|
|
|
|
if parent in self:
|
|
|
|
return self.get_state(parent)
|
|
|
|
else:
|
2011-04-12 11:22:29 +00:00
|
|
|
return DirectoryState.Normal
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-08-15 13:07:44 +00:00
|
|
|
def has_any_file(self):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Returns whether selected folders contain any file.
|
|
|
|
|
|
|
|
Because it stops at the first file it finds, it's much faster than get_files().
|
|
|
|
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2010-08-15 13:07:44 +00:00
|
|
|
try:
|
|
|
|
next(self.get_files())
|
|
|
|
return True
|
|
|
|
except StopIteration:
|
|
|
|
return False
|
|
|
|
|
2009-06-18 18:37:53 +00:00
|
|
|
def load_from_file(self, infile):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Load folder selection from ``infile``.
|
|
|
|
|
|
|
|
:param file infile: path or file pointer to XML generated through :meth:`save_to_file`
|
|
|
|
"""
|
2009-06-01 09:55:11 +00:00
|
|
|
try:
|
2010-08-15 12:42:55 +00:00
|
|
|
root = ET.parse(infile).getroot()
|
|
|
|
except Exception:
|
2009-06-01 09:55:11 +00:00
|
|
|
return
|
2010-08-15 12:42:55 +00:00
|
|
|
for rdn in root.getiterator('root_directory'):
|
2010-03-01 11:21:43 +00:00
|
|
|
attrib = rdn.attrib
|
|
|
|
if 'path' not in attrib:
|
2009-06-18 18:37:53 +00:00
|
|
|
continue
|
2010-03-01 11:21:43 +00:00
|
|
|
path = attrib['path']
|
2009-06-01 09:55:11 +00:00
|
|
|
try:
|
|
|
|
self.add_path(Path(path))
|
2009-06-18 18:37:53 +00:00
|
|
|
except (AlreadyThereError, InvalidPathError):
|
2009-06-01 09:55:11 +00:00
|
|
|
pass
|
2010-08-15 12:42:55 +00:00
|
|
|
for sn in root.getiterator('state'):
|
2010-03-01 11:21:43 +00:00
|
|
|
attrib = sn.attrib
|
|
|
|
if not ('path' in attrib and 'value' in attrib):
|
2009-06-01 09:55:11 +00:00
|
|
|
continue
|
2010-03-01 11:21:43 +00:00
|
|
|
path = attrib['path']
|
|
|
|
state = attrib['value']
|
2009-06-18 18:37:53 +00:00
|
|
|
self.set_state(Path(path), int(state))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2010-03-01 11:21:43 +00:00
|
|
|
def save_to_file(self, outfile):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Save folder selection as XML to ``outfile``.
|
|
|
|
|
|
|
|
:param file outfile: path or file pointer to XML file to save to.
|
|
|
|
"""
|
2009-06-01 09:55:11 +00:00
|
|
|
with FileOrPath(outfile, 'wb') as fp:
|
2010-08-15 12:42:55 +00:00
|
|
|
root = ET.Element('directories')
|
2009-10-23 12:56:52 +00:00
|
|
|
for root_path in self:
|
2010-08-15 12:42:55 +00:00
|
|
|
root_path_node = ET.SubElement(root, 'root_directory')
|
2010-08-11 14:39:06 +00:00
|
|
|
root_path_node.set('path', str(root_path))
|
|
|
|
for path, state in self.states.items():
|
2010-08-15 12:42:55 +00:00
|
|
|
state_node = ET.SubElement(root, 'state')
|
2010-08-11 14:39:06 +00:00
|
|
|
state_node.set('path', str(path))
|
|
|
|
state_node.set('value', str(state))
|
2010-08-15 12:42:55 +00:00
|
|
|
tree = ET.ElementTree(root)
|
2010-03-01 11:21:43 +00:00
|
|
|
tree.write(fp, encoding='utf-8')
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2009-06-18 18:37:53 +00:00
|
|
|
def set_state(self, path, state):
|
2013-08-18 22:36:09 +00:00
|
|
|
"""Set the state of folder at ``path``.
|
|
|
|
|
|
|
|
:param Path path: path of the target folder
|
|
|
|
:param state: state to set folder to
|
|
|
|
:type state: :class:`DirectoryState`
|
|
|
|
"""
|
2009-06-18 18:37:53 +00:00
|
|
|
if self.get_state(path) == state:
|
2009-06-18 18:13:45 +00:00
|
|
|
return
|
2009-06-18 18:37:53 +00:00
|
|
|
# we don't want to needlessly fill self.states. if get_state returns the same thing
|
2009-06-18 18:13:45 +00:00
|
|
|
# without an explicit entry, remove that entry
|
|
|
|
if path in self.states:
|
|
|
|
del self.states[path]
|
2009-06-18 18:37:53 +00:00
|
|
|
if self.get_state(path) == state: # no need for an entry
|
2009-06-01 09:55:11 +00:00
|
|
|
return
|
2009-06-18 18:13:45 +00:00
|
|
|
self.states[path] = state
|
2009-06-01 09:55:11 +00:00
|
|
|
|