mirror of
https://github.com/arsenetar/dupeguru.git
synced 2025-03-10 05:34:36 +00:00
base py: pep8ified dupeguru.directories (about time!).
--HG-- extra : convert_revision : svn%3Ac306627e-7827-47d3-bdf0-9a457c9553a1/trunk%4072
This commit is contained in:
parent
3ada0ff89c
commit
a7a76f2b40
@ -80,7 +80,7 @@ class DupeGuru(RegistrableApplication):
|
||||
return False
|
||||
|
||||
def _do_load(self, j):
|
||||
self.directories.LoadFromFile(op.join(self.appdata, 'last_directories.xml'))
|
||||
self.directories.load_from_file(op.join(self.appdata, 'last_directories.xml'))
|
||||
j = j.start_subjob([1, 9])
|
||||
self.results.load_from_xml(op.join(self.appdata, 'last_results.xml'), self._get_file, j)
|
||||
files = flatten(g[:] for g in self.results.groups)
|
||||
@ -194,7 +194,7 @@ class DupeGuru(RegistrableApplication):
|
||||
changed_groups.add(g)
|
||||
|
||||
def save(self):
|
||||
self.directories.SaveToFile(op.join(self.appdata, 'last_directories.xml'))
|
||||
self.directories.save_to_file(op.join(self.appdata, 'last_directories.xml'))
|
||||
self.results.save_to_xml(op.join(self.appdata, 'last_results.xml'))
|
||||
|
||||
def save_ignore_list(self):
|
||||
|
@ -226,7 +226,7 @@ class DupeGuru(app.DupeGuru):
|
||||
|
||||
def SetDirectoryState(self,node_path,state):
|
||||
d = self.GetDirectory(node_path)
|
||||
self.directories.SetState(d.path,state)
|
||||
self.directories.set_state(d.path,state)
|
||||
|
||||
def sort_dupes(self,key,asc):
|
||||
self.results.sort_dupes(key,asc,self.display_delta_values)
|
||||
@ -279,7 +279,7 @@ class DupeGuru(app.DupeGuru):
|
||||
d = self.GetDirectory(node_path)
|
||||
return [
|
||||
d.name,
|
||||
self.directories.GetState(d.path)
|
||||
self.directories.get_state(d.path)
|
||||
]
|
||||
|
||||
def GetOutlineViewMarked(self, tag, node_path):
|
||||
|
@ -169,7 +169,7 @@ class DupeGuruPE(app_cocoa.DupeGuru):
|
||||
return app_cocoa.DupeGuru._do_delete_dupe(self, dupe)
|
||||
|
||||
def _do_load(self, j):
|
||||
self.directories.LoadFromFile(op.join(self.appdata, 'last_directories.xml'))
|
||||
self.directories.load_from_file(op.join(self.appdata, 'last_directories.xml'))
|
||||
for d in self.directories:
|
||||
if isinstance(d, IPhotoLibrary):
|
||||
d.update()
|
||||
@ -201,7 +201,7 @@ class DupeGuruPE(app_cocoa.DupeGuru):
|
||||
def start_scanning(self):
|
||||
for directory in self.directories:
|
||||
if isinstance(directory, IPhotoLibrary):
|
||||
self.directories.SetState(directory.refpath, directories.STATE_EXCLUDED)
|
||||
self.directories.set_state(directory.refpath, directories.STATE_EXCLUDED)
|
||||
return app_cocoa.DupeGuru.start_scanning(self)
|
||||
|
||||
def selected_dupe_path(self):
|
||||
|
@ -1,13 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Unit Name: dupeguru.directories
|
||||
Created By: Virgil Dupras
|
||||
Created On: 2006/02/27
|
||||
Last modified by:$Author: virgil $
|
||||
Last modified on:$Date: 2009-05-28 16:02:48 +0200 (Thu, 28 May 2009) $
|
||||
$Revision: 4388 $
|
||||
Copyright 2004-2006 Hardcoded Software (http://www.hardcoded.net)
|
||||
"""
|
||||
# Unit Name: dupeguru.directories
|
||||
# Created By: Virgil Dupras
|
||||
# Created On: 2006/02/27
|
||||
# $Id$
|
||||
# Copyright 2009 Hardcoded Software (http://www.hardcoded.net)
|
||||
|
||||
import xml.dom.minidom
|
||||
|
||||
from hsfs import phys
|
||||
@ -56,7 +52,7 @@ class Directories(object):
|
||||
|
||||
def _get_files(self, from_dir):
|
||||
from_path = from_dir.path
|
||||
state = self.GetState(from_path)
|
||||
state = self.get_state(from_path)
|
||||
if state == STATE_EXCLUDED:
|
||||
# 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
|
||||
@ -90,7 +86,7 @@ class Directories(object):
|
||||
self._dirs.append(d)
|
||||
return d
|
||||
except fs.InvalidPath:
|
||||
raise InvalidPathError
|
||||
raise InvalidPathError()
|
||||
|
||||
def get_files(self):
|
||||
"""Returns a list of all files that are not excluded.
|
||||
@ -105,22 +101,21 @@ class Directories(object):
|
||||
except fs.InvalidPath:
|
||||
pass
|
||||
|
||||
def GetState(self, path):
|
||||
def get_state(self, path):
|
||||
"""Returns the state of 'path' (One of the STATE_* const.)
|
||||
"""
|
||||
try:
|
||||
if path in self.states:
|
||||
return self.states[path]
|
||||
except KeyError:
|
||||
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.GetState(parent)
|
||||
else:
|
||||
return STATE_NORMAL
|
||||
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:
|
||||
return STATE_NORMAL
|
||||
|
||||
def LoadFromFile(self,infile):
|
||||
def load_from_file(self, infile):
|
||||
try:
|
||||
doc = xml.dom.minidom.parse(infile)
|
||||
except:
|
||||
@ -128,11 +123,11 @@ class Directories(object):
|
||||
root_dir_nodes = doc.getElementsByTagName('root_directory')
|
||||
for rdn in root_dir_nodes:
|
||||
if not rdn.getAttributeNode('path'):
|
||||
continue
|
||||
continue
|
||||
path = rdn.getAttributeNode('path').nodeValue
|
||||
try:
|
||||
self.add_path(Path(path))
|
||||
except (AlreadyThereError,InvalidPathError):
|
||||
except (AlreadyThereError, InvalidPathError):
|
||||
pass
|
||||
state_nodes = doc.getElementsByTagName('state')
|
||||
for sn in state_nodes:
|
||||
@ -140,32 +135,29 @@ class Directories(object):
|
||||
continue
|
||||
path = sn.getAttributeNode('path').nodeValue
|
||||
state = sn.getAttributeNode('value').nodeValue
|
||||
self.SetState(Path(path), int(state))
|
||||
self.set_state(Path(path), int(state))
|
||||
|
||||
def Remove(self,directory):
|
||||
self._dirs.remove(directory)
|
||||
|
||||
def SaveToFile(self,outfile):
|
||||
def save_to_file(self,outfile):
|
||||
with FileOrPath(outfile, 'wb') as fp:
|
||||
doc = xml.dom.minidom.Document()
|
||||
root = doc.appendChild(doc.createElement('directories'))
|
||||
for root_dir in self:
|
||||
root_dir_node = root.appendChild(doc.createElement('root_directory'))
|
||||
root_dir_node.setAttribute('path', unicode(root_dir.path).encode('utf-8'))
|
||||
for path,state in self.states.iteritems():
|
||||
for path, state in self.states.iteritems():
|
||||
state_node = root.appendChild(doc.createElement('state'))
|
||||
state_node.setAttribute('path', unicode(path).encode('utf-8'))
|
||||
state_node.setAttribute('value', str(state))
|
||||
doc.writexml(fp,'\t','\t','\n',encoding='utf-8')
|
||||
doc.writexml(fp, '\t', '\t', '\n', encoding='utf-8')
|
||||
|
||||
def SetState(self,path,state):
|
||||
if self.GetState(path) == state:
|
||||
def set_state(self, path, state):
|
||||
if self.get_state(path) == state:
|
||||
return
|
||||
# we don't want to needlessly fill self.states. if GetState returns the same thing
|
||||
# we don't want to needlessly fill self.states. if get_state returns the same thing
|
||||
# without an explicit entry, remove that entry
|
||||
if path in self.states:
|
||||
del self.states[path]
|
||||
if self.GetState(path) == state: # no need for an entry
|
||||
if self.get_state(path) == state: # no need for an entry
|
||||
return
|
||||
self.states[path] = state
|
||||
|
||||
|
@ -84,52 +84,52 @@ class TCDirectories(TestCase):
|
||||
d = Directories()
|
||||
p = testpath + 'utils'
|
||||
d.add_path(p)
|
||||
self.assertEqual(STATE_NORMAL,d.GetState(p))
|
||||
d.SetState(p,STATE_REFERENCE)
|
||||
self.assertEqual(STATE_REFERENCE,d.GetState(p))
|
||||
self.assertEqual(STATE_REFERENCE,d.GetState(p + 'dir1'))
|
||||
self.assertEqual(STATE_NORMAL,d.get_state(p))
|
||||
d.set_state(p,STATE_REFERENCE)
|
||||
self.assertEqual(STATE_REFERENCE,d.get_state(p))
|
||||
self.assertEqual(STATE_REFERENCE,d.get_state(p + 'dir1'))
|
||||
self.assertEqual(1,len(d.states))
|
||||
self.assertEqual(p,d.states.keys()[0])
|
||||
self.assertEqual(STATE_REFERENCE,d.states[p])
|
||||
|
||||
def test_GetState_with_path_not_there(self):
|
||||
def test_get_state_with_path_not_there(self):
|
||||
# When the path's not there, just return STATE_NORMAL
|
||||
d = Directories()
|
||||
d.add_path(testpath + 'utils')
|
||||
eq_(d.GetState(testpath), STATE_NORMAL)
|
||||
eq_(d.get_state(testpath), STATE_NORMAL)
|
||||
|
||||
def test_states_remain_when_larger_directory_eat_smaller_ones(self):
|
||||
d = Directories()
|
||||
p = testpath + 'utils'
|
||||
d.add_path(p)
|
||||
d.SetState(p,STATE_EXCLUDED)
|
||||
d.set_state(p,STATE_EXCLUDED)
|
||||
d.add_path(testpath)
|
||||
d.SetState(testpath,STATE_REFERENCE)
|
||||
self.assertEqual(STATE_EXCLUDED,d.GetState(p))
|
||||
self.assertEqual(STATE_EXCLUDED,d.GetState(p + 'dir1'))
|
||||
self.assertEqual(STATE_REFERENCE,d.GetState(testpath))
|
||||
d.set_state(testpath,STATE_REFERENCE)
|
||||
self.assertEqual(STATE_EXCLUDED,d.get_state(p))
|
||||
self.assertEqual(STATE_EXCLUDED,d.get_state(p + 'dir1'))
|
||||
self.assertEqual(STATE_REFERENCE,d.get_state(testpath))
|
||||
|
||||
def test_SetState_keep_state_dict_size_to_minimum(self):
|
||||
def test_set_state_keep_state_dict_size_to_minimum(self):
|
||||
d = Directories()
|
||||
p = Path(phys_test.create_fake_fs(self.tmpdir()))
|
||||
d.add_path(p)
|
||||
d.SetState(p,STATE_REFERENCE)
|
||||
d.SetState(p + 'dir1',STATE_REFERENCE)
|
||||
d.set_state(p,STATE_REFERENCE)
|
||||
d.set_state(p + 'dir1',STATE_REFERENCE)
|
||||
self.assertEqual(1,len(d.states))
|
||||
self.assertEqual(STATE_REFERENCE,d.GetState(p + 'dir1'))
|
||||
d.SetState(p + 'dir1',STATE_NORMAL)
|
||||
self.assertEqual(STATE_REFERENCE,d.get_state(p + 'dir1'))
|
||||
d.set_state(p + 'dir1',STATE_NORMAL)
|
||||
self.assertEqual(2,len(d.states))
|
||||
self.assertEqual(STATE_NORMAL,d.GetState(p + 'dir1'))
|
||||
d.SetState(p + 'dir1',STATE_REFERENCE)
|
||||
self.assertEqual(STATE_NORMAL,d.get_state(p + 'dir1'))
|
||||
d.set_state(p + 'dir1',STATE_REFERENCE)
|
||||
self.assertEqual(1,len(d.states))
|
||||
self.assertEqual(STATE_REFERENCE,d.GetState(p + 'dir1'))
|
||||
self.assertEqual(STATE_REFERENCE,d.get_state(p + 'dir1'))
|
||||
|
||||
def test_get_files(self):
|
||||
d = Directories()
|
||||
p = Path(phys_test.create_fake_fs(self.tmpdir()))
|
||||
d.add_path(p)
|
||||
d.SetState(p + 'dir1',STATE_REFERENCE)
|
||||
d.SetState(p + 'dir2',STATE_EXCLUDED)
|
||||
d.set_state(p + 'dir1',STATE_REFERENCE)
|
||||
d.set_state(p + 'dir2',STATE_EXCLUDED)
|
||||
files = d.get_files()
|
||||
self.assertEqual(5, len(list(files)))
|
||||
for f in files:
|
||||
@ -142,7 +142,7 @@ class TCDirectories(TestCase):
|
||||
d = Directories()
|
||||
p = testpath + 'utils'
|
||||
d.add_path(p)
|
||||
d.SetState(p,STATE_EXCLUDED)
|
||||
d.set_state(p,STATE_EXCLUDED)
|
||||
self.assertEqual([], list(d.get_files()))
|
||||
|
||||
def test_save_and_load(self):
|
||||
@ -152,14 +152,14 @@ class TCDirectories(TestCase):
|
||||
p2 = self.tmppath()
|
||||
d1.add_path(p1)
|
||||
d1.add_path(p2)
|
||||
d1.SetState(p1, STATE_REFERENCE)
|
||||
d1.SetState(p1 + 'dir1',STATE_EXCLUDED)
|
||||
d1.set_state(p1, STATE_REFERENCE)
|
||||
d1.set_state(p1 + 'dir1',STATE_EXCLUDED)
|
||||
tmpxml = op.join(self.tmpdir(), 'directories_testunit.xml')
|
||||
d1.SaveToFile(tmpxml)
|
||||
d2.LoadFromFile(tmpxml)
|
||||
d1.save_to_file(tmpxml)
|
||||
d2.load_from_file(tmpxml)
|
||||
self.assertEqual(2, len(d2))
|
||||
self.assertEqual(STATE_REFERENCE,d2.GetState(p1))
|
||||
self.assertEqual(STATE_EXCLUDED,d2.GetState(p1 + 'dir1'))
|
||||
self.assertEqual(STATE_REFERENCE,d2.get_state(p1))
|
||||
self.assertEqual(STATE_EXCLUDED,d2.get_state(p1 + 'dir1'))
|
||||
|
||||
def test_invalid_path(self):
|
||||
d = Directories()
|
||||
@ -167,10 +167,10 @@ class TCDirectories(TestCase):
|
||||
self.assertRaises(InvalidPathError, d.add_path, p)
|
||||
self.assertEqual(0, len(d))
|
||||
|
||||
def test_SetState_on_invalid_path(self):
|
||||
def test_set_state_on_invalid_path(self):
|
||||
d = Directories()
|
||||
try:
|
||||
d.SetState(Path('foobar',),STATE_NORMAL)
|
||||
d.set_state(Path('foobar',),STATE_NORMAL)
|
||||
except LookupError:
|
||||
self.fail()
|
||||
|
||||
@ -184,7 +184,7 @@ class TCDirectories(TestCase):
|
||||
d.add_path(testpath)
|
||||
self.assert_(isinstance(d[0], MySpecialDirclass))
|
||||
|
||||
def test_LoadFromFile_with_invalid_path(self):
|
||||
def test_load_from_file_with_invalid_path(self):
|
||||
#This test simulates a load from file resulting in a
|
||||
#InvalidPath raise. Other directories must be loaded.
|
||||
d1 = Directories()
|
||||
@ -192,12 +192,12 @@ class TCDirectories(TestCase):
|
||||
#Will raise InvalidPath upon loading
|
||||
d1.add_path(self.tmppath()).name = 'does_not_exist'
|
||||
tmpxml = op.join(self.tmpdir(), 'directories_testunit.xml')
|
||||
d1.SaveToFile(tmpxml)
|
||||
d1.save_to_file(tmpxml)
|
||||
d2 = Directories()
|
||||
d2.LoadFromFile(tmpxml)
|
||||
d2.load_from_file(tmpxml)
|
||||
self.assertEqual(1, len(d2))
|
||||
|
||||
def test_LoadFromFile_with_same_paths(self):
|
||||
def test_load_from_file_with_same_paths(self):
|
||||
#This test simulates a load from file resulting in a
|
||||
#AlreadyExists raise. Other directories must be loaded.
|
||||
d1 = Directories()
|
||||
@ -208,29 +208,21 @@ class TCDirectories(TestCase):
|
||||
#Will raise AlreadyExists upon loading
|
||||
d1.add_path(self.tmppath()).name = unicode(p1)
|
||||
tmpxml = op.join(self.tmpdir(), 'directories_testunit.xml')
|
||||
d1.SaveToFile(tmpxml)
|
||||
d1.save_to_file(tmpxml)
|
||||
d2 = Directories()
|
||||
d2.LoadFromFile(tmpxml)
|
||||
d2.load_from_file(tmpxml)
|
||||
self.assertEqual(2, len(d2))
|
||||
|
||||
def test_Remove(self):
|
||||
d = Directories()
|
||||
d1 = d.add_path(self.tmppath())
|
||||
d2 = d.add_path(self.tmppath())
|
||||
d.Remove(d1)
|
||||
self.assertEqual(1, len(d))
|
||||
self.assert_(d[0] is d2)
|
||||
|
||||
def test_unicode_save(self):
|
||||
d = Directories()
|
||||
p1 = self.tmppath() + u'hello\xe9'
|
||||
io.mkdir(p1)
|
||||
io.mkdir(p1 + u'foo\xe9')
|
||||
d.add_path(p1)
|
||||
d.SetState(d[0][0].path, STATE_EXCLUDED)
|
||||
d.set_state(d[0][0].path, STATE_EXCLUDED)
|
||||
tmpxml = op.join(self.tmpdir(), 'directories_testunit.xml')
|
||||
try:
|
||||
d.SaveToFile(tmpxml)
|
||||
d.save_to_file(tmpxml)
|
||||
except UnicodeDecodeError:
|
||||
self.fail()
|
||||
|
||||
@ -252,16 +244,16 @@ class TCDirectories(TestCase):
|
||||
io.rmtree(p)
|
||||
self.assertEqual([], list(d.get_files()))
|
||||
|
||||
def test_GetState_returns_excluded_by_default_for_hidden_directories(self):
|
||||
def test_get_state_returns_excluded_by_default_for_hidden_directories(self):
|
||||
d = Directories()
|
||||
p = Path(self.tmpdir())
|
||||
hidden_dir_path = p + '.foo'
|
||||
io.mkdir(p + '.foo')
|
||||
d.add_path(p)
|
||||
self.assertEqual(d.GetState(hidden_dir_path), STATE_EXCLUDED)
|
||||
self.assertEqual(d.get_state(hidden_dir_path), STATE_EXCLUDED)
|
||||
# But it can be overriden
|
||||
d.SetState(hidden_dir_path, STATE_NORMAL)
|
||||
self.assertEqual(d.GetState(hidden_dir_path), STATE_NORMAL)
|
||||
d.set_state(hidden_dir_path, STATE_NORMAL)
|
||||
self.assertEqual(d.get_state(hidden_dir_path), STATE_NORMAL)
|
||||
|
||||
def test_special_dirclasses(self):
|
||||
# if a path is in special_dirclasses, use this class instead
|
||||
@ -287,11 +279,11 @@ class TCDirectories(TestCase):
|
||||
io.mkdir(p1 + 'foobaz')
|
||||
io.open(p1 + 'foobaz/somefile', 'w').close()
|
||||
d.add_path(p1)
|
||||
eq_(d.GetState(p1 + 'foobaz'), STATE_NORMAL)
|
||||
eq_(d.GetState(p1 + 'foobar'), STATE_EXCLUDED)
|
||||
eq_(d.get_state(p1 + 'foobaz'), STATE_NORMAL)
|
||||
eq_(d.get_state(p1 + 'foobar'), STATE_EXCLUDED)
|
||||
eq_(len(list(d.get_files())), 1) # only the 'foobaz' file is there
|
||||
# However, the default state can be changed
|
||||
d.SetState(p1 + 'foobar', STATE_NORMAL)
|
||||
eq_(d.GetState(p1 + 'foobar'), STATE_NORMAL)
|
||||
d.set_state(p1 + 'foobar', STATE_NORMAL)
|
||||
eq_(d.get_state(p1 + 'foobar'), STATE_NORMAL)
|
||||
eq_(len(list(d.get_files())), 2)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user