Enum-ified Scan Type constants, looks nicer.

This commit is contained in:
Virgil Dupras 2010-08-14 19:52:23 +02:00
parent 5b2d506462
commit 58da335b17
8 changed files with 62 additions and 64 deletions

View File

@ -8,8 +8,7 @@ from hscommon.cocoa import signature
from core.app_cocoa_inter import PyDupeGuruBase, PyDetailsPanel
from core_me.app_cocoa import DupeGuruME
from core.scanner import (SCAN_TYPE_FILENAME, SCAN_TYPE_FIELDS, SCAN_TYPE_FIELDS_NO_ORDER,
SCAN_TYPE_TAG, SCAN_TYPE_CONTENT, SCAN_TYPE_CONTENT_AUDIO)
from core.scanner import ScanType
# Fix py2app imports which chokes on relative imports and other stuff
from core_me import app_cocoa, data, fs, scanner
@ -41,12 +40,12 @@ class PyDupeGuru(PyDupeGuruBase):
def setScanType_(self, scan_type):
try:
self.py.scanner.scan_type = [
SCAN_TYPE_FILENAME,
SCAN_TYPE_FIELDS,
SCAN_TYPE_FIELDS_NO_ORDER,
SCAN_TYPE_TAG,
SCAN_TYPE_CONTENT,
SCAN_TYPE_CONTENT_AUDIO
ScanType.Filename,
ScanType.Fields,
ScanType.FieldsNoOrder,
ScanType.Tag,
ScanType.Contents,
ScanType.ContentsAudio,
][scan_type]
except IndexError:
pass

View File

@ -6,7 +6,7 @@
from hscommon.cocoa import signature
from core import scanner
from core.scanner import ScanType
from core.app_cocoa_inter import PyDupeGuruBase, PyDetailsPanel
from core_se.app_cocoa import DupeGuru
@ -30,8 +30,8 @@ class PyDupeGuru(PyDupeGuruBase):
def setScanType_(self,scan_type):
try:
self.py.scanner.scan_type = [
scanner.SCAN_TYPE_FILENAME,
scanner.SCAN_TYPE_CONTENT
ScanType.Filename,
ScanType.Contents,
][scan_type]
except IndexError:
pass

View File

@ -17,13 +17,14 @@ from hsutil.str import get_file_ext, rem_file_ext
from . import engine
from .ignore import IgnoreList
(SCAN_TYPE_FILENAME,
SCAN_TYPE_FIELDS,
SCAN_TYPE_FIELDS_NO_ORDER,
SCAN_TYPE_TAG,
UNUSED, # Must not be removed. Constants here are what scan_type in the prefs are.
SCAN_TYPE_CONTENT,
SCAN_TYPE_CONTENT_AUDIO) = range(7)
class ScanType:
Filename = 0
Fields = 1
FieldsNoOrder = 2
Tag = 3
# number 4 is obsolete
Contents = 5
ContentsAudio = 6
SCANNABLE_TAGS = ['track', 'artist', 'album', 'title', 'genre', 'year']
@ -47,22 +48,22 @@ class Scanner(object):
for f in j.iter_with_progress(files, 'Read size of %d/%d files'):
f.size # pre-read, makes a smoother progress if read here (especially for bundles)
files = [f for f in files if f.size >= self.size_threshold]
if self.scan_type in (SCAN_TYPE_CONTENT, SCAN_TYPE_CONTENT_AUDIO):
sizeattr = 'size' if self.scan_type == SCAN_TYPE_CONTENT else 'audiosize'
return engine.getmatches_by_contents(files, sizeattr, partial=self.scan_type==SCAN_TYPE_CONTENT_AUDIO, j=j)
if self.scan_type in (ScanType.Contents, ScanType.ContentsAudio):
sizeattr = 'size' if self.scan_type == ScanType.Contents else 'audiosize'
return engine.getmatches_by_contents(files, sizeattr, partial=self.scan_type==ScanType.ContentsAudio, j=j)
else:
j = j.start_subjob([2, 8])
kw = {}
kw['match_similar_words'] = self.match_similar_words
kw['weight_words'] = self.word_weighting
kw['min_match_percentage'] = self.min_match_percentage
if self.scan_type == SCAN_TYPE_FIELDS_NO_ORDER:
self.scan_type = SCAN_TYPE_FIELDS
if self.scan_type == ScanType.FieldsNoOrder:
self.scan_type = ScanType.Fields
kw['no_field_order'] = True
func = {
SCAN_TYPE_FILENAME: lambda f: engine.getwords(rem_file_ext(f.name)),
SCAN_TYPE_FIELDS: lambda f: engine.getfields(rem_file_ext(f.name)),
SCAN_TYPE_TAG: lambda f: [engine.getwords(str(getattr(f, attrname))) for attrname in SCANNABLE_TAGS if attrname in self.scanned_tags],
ScanType.Filename: lambda f: engine.getwords(rem_file_ext(f.name)),
ScanType.Fields: lambda f: engine.getfields(rem_file_ext(f.name)),
ScanType.Tag: lambda f: [engine.getwords(str(getattr(f, attrname))) for attrname in SCANNABLE_TAGS if attrname in self.scanned_tags],
}[self.scan_type]
for f in j.iter_with_progress(files, 'Read metadata of %d/%d files'):
f.words = func(f)
@ -116,7 +117,7 @@ class Scanner(object):
match_similar_words = False
min_match_percentage = 80
mix_file_kind = True
scan_type = SCAN_TYPE_FILENAME
scan_type = ScanType.Filename
scanned_tags = set(['artist', 'title'])
size_threshold = 0
word_weighting = False

View File

@ -46,7 +46,7 @@ class ScannerTestFakeFiles(TestCase):
def test_default_settings(self):
s = Scanner()
eq_(s.min_match_percentage, 80)
eq_(s.scan_type, SCAN_TYPE_FILENAME)
eq_(s.scan_type, ScanType.Filename)
eq_(s.mix_file_kind, True)
eq_(s.word_weighting, False)
eq_(s.match_similar_words, False)
@ -98,7 +98,7 @@ class ScannerTestFakeFiles(TestCase):
def test_content_scan(self):
s = Scanner()
s.scan_type = SCAN_TYPE_CONTENT
s.scan_type = ScanType.Contents
f = [no('foo'), no('bar'), no('bleh')]
f[0].md5 = f[0].md5partial = 'foobar'
f[1].md5 = f[1].md5partial = 'foobar'
@ -115,13 +115,13 @@ class ScannerTestFakeFiles(TestCase):
raise AssertionError()
s = Scanner()
s.scan_type = SCAN_TYPE_CONTENT
s.scan_type = ScanType.Contents
f = [MyFile('foo', 1), MyFile('bar', 2)]
eq_(len(s.GetDupeGroups(f)), 0)
def test_min_match_perc_doesnt_matter_for_content_scan(self):
s = Scanner()
s.scan_type = SCAN_TYPE_CONTENT
s.scan_type = ScanType.Contents
f = [no('foo'), no('bar'), no('bleh')]
f[0].md5 = f[0].md5partial = 'foobar'
f[1].md5 = f[1].md5partial = 'foobar'
@ -137,7 +137,7 @@ class ScannerTestFakeFiles(TestCase):
def test_content_scan_doesnt_put_md5_in_words_at_the_end(self):
s = Scanner()
s.scan_type = SCAN_TYPE_CONTENT
s.scan_type = ScanType.Contents
f = [no('foo'),no('bar')]
f[0].md5 = f[0].md5partial = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
f[1].md5 = f[1].md5partial = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
@ -191,21 +191,21 @@ class ScannerTestFakeFiles(TestCase):
def test_fields(self):
s = Scanner()
s.scan_type = SCAN_TYPE_FIELDS
s.scan_type = ScanType.Fields
f = [no('The White Stripes - Little Ghost'), no('The White Stripes - Little Acorn')]
r = s.GetDupeGroups(f)
eq_(len(r), 0)
def test_fields_no_order(self):
s = Scanner()
s.scan_type = SCAN_TYPE_FIELDS_NO_ORDER
s.scan_type = ScanType.FieldsNoOrder
f = [no('The White Stripes - Little Ghost'), no('Little Ghost - The White Stripes')]
r = s.GetDupeGroups(f)
eq_(len(r), 1)
def test_tag_scan(self):
s = Scanner()
s.scan_type = SCAN_TYPE_TAG
s.scan_type = ScanType.Tag
o1 = no('foo')
o2 = no('bar')
o1.artist = 'The White Stripes'
@ -217,7 +217,7 @@ class ScannerTestFakeFiles(TestCase):
def test_tag_with_album_scan(self):
s = Scanner()
s.scan_type = SCAN_TYPE_TAG
s.scan_type = ScanType.Tag
s.scanned_tags = set(['artist', 'album', 'title'])
o1 = no('foo')
o2 = no('bar')
@ -236,7 +236,7 @@ class ScannerTestFakeFiles(TestCase):
def test_that_dash_in_tags_dont_create_new_fields(self):
s = Scanner()
s.scan_type = SCAN_TYPE_TAG
s.scan_type = ScanType.Tag
s.scanned_tags = set(['artist', 'album', 'title'])
s.min_match_percentage = 50
o1 = no('foo')
@ -252,7 +252,7 @@ class ScannerTestFakeFiles(TestCase):
def test_tag_scan_with_different_scanned(self):
s = Scanner()
s.scan_type = SCAN_TYPE_TAG
s.scan_type = ScanType.Tag
s.scanned_tags = set(['track', 'year'])
o1 = no('foo')
o2 = no('bar')
@ -269,7 +269,7 @@ class ScannerTestFakeFiles(TestCase):
def test_tag_scan_only_scans_existing_tags(self):
s = Scanner()
s.scan_type = SCAN_TYPE_TAG
s.scan_type = ScanType.Tag
s.scanned_tags = set(['artist', 'foo'])
o1 = no('foo')
o2 = no('bar')
@ -282,7 +282,7 @@ class ScannerTestFakeFiles(TestCase):
def test_tag_scan_converts_to_str(self):
s = Scanner()
s.scan_type = SCAN_TYPE_TAG
s.scan_type = ScanType.Tag
s.scanned_tags = set(['track'])
o1 = no('foo')
o2 = no('bar')
@ -296,7 +296,7 @@ class ScannerTestFakeFiles(TestCase):
def test_tag_scan_non_ascii(self):
s = Scanner()
s.scan_type = SCAN_TYPE_TAG
s.scan_type = ScanType.Tag
s.scanned_tags = set(['title'])
o1 = no('foo')
o2 = no('bar')
@ -310,7 +310,7 @@ class ScannerTestFakeFiles(TestCase):
def test_audio_content_scan(self):
s = Scanner()
s.scan_type = SCAN_TYPE_CONTENT_AUDIO
s.scan_type = ScanType.ContentsAudio
f = [no('foo'), no('bar'), no('bleh')]
f[0].md5 = 'foo'
f[1].md5 = 'bar'
@ -332,7 +332,7 @@ class ScannerTestFakeFiles(TestCase):
raise AssertionError()
s = Scanner()
s.scan_type = SCAN_TYPE_CONTENT_AUDIO
s.scan_type = ScanType.ContentsAudio
f = [MyFile('foo'), MyFile('bar')]
f[0].audiosize = 1
f[1].audiosize = 2
@ -465,7 +465,7 @@ class ScannerTest(TestCase):
# In this test, we have to delete one of the files between the get_matches() part and the
# get_groups() part.
s = Scanner()
s.scan_type = SCAN_TYPE_CONTENT
s.scan_type = ScanType.Contents
p = self.tmppath()
io.open(p + 'file1', 'w').write('foo')
io.open(p + 'file2', 'w').write('foo')

View File

@ -6,8 +6,7 @@
# which should be included with this package. The terms are also available at
# http://www.hardcoded.net/licenses/hs_license
from core.scanner import (SCAN_TYPE_FILENAME, SCAN_TYPE_FIELDS, SCAN_TYPE_FIELDS_NO_ORDER,
SCAN_TYPE_TAG, SCAN_TYPE_CONTENT, SCAN_TYPE_CONTENT_AUDIO)
from core.scanner import ScanType
from base.preferences import Preferences as PreferencesBase
@ -47,7 +46,7 @@ class Preferences(PreferencesBase):
def _reset_specific(self):
self.filter_hardness = 80
self.scan_type = SCAN_TYPE_TAG
self.scan_type = ScanType.Tag
self.word_weighting = True
self.match_similar = False
self.scan_tag_track = False

View File

@ -9,19 +9,18 @@
from PyQt4.QtCore import SIGNAL, Qt
from PyQt4.QtGui import QDialog, QDialogButtonBox
from core.scanner import (SCAN_TYPE_FILENAME, SCAN_TYPE_FIELDS, SCAN_TYPE_FIELDS_NO_ORDER,
SCAN_TYPE_TAG, SCAN_TYPE_CONTENT, SCAN_TYPE_CONTENT_AUDIO)
from core.scanner import ScanType
from preferences_dialog_ui import Ui_PreferencesDialog
import preferences
SCAN_TYPE_ORDER = [
SCAN_TYPE_FILENAME,
SCAN_TYPE_FIELDS,
SCAN_TYPE_FIELDS_NO_ORDER,
SCAN_TYPE_TAG,
SCAN_TYPE_CONTENT,
SCAN_TYPE_CONTENT_AUDIO,
ScanType.Filename,
ScanType.Fields,
ScanType.FieldsNoOrder,
ScanType.Tag,
ScanType.Contents,
ScanType.ContentsAudio,
]
class PreferencesDialog(QDialog, Ui_PreferencesDialog):
@ -89,9 +88,9 @@ class PreferencesDialog(QDialog, Ui_PreferencesDialog):
def scanTypeChanged(self, index):
scan_type = SCAN_TYPE_ORDER[self.scanTypeComboBox.currentIndex()]
word_based = scan_type in [SCAN_TYPE_FILENAME, SCAN_TYPE_FIELDS, SCAN_TYPE_FIELDS_NO_ORDER,
SCAN_TYPE_TAG]
tag_based = scan_type == SCAN_TYPE_TAG
word_based = scan_type in (ScanType.Filename, ScanType.Fields, ScanType.FieldsNoOrder,
ScanType.Tag)
tag_based = scan_type == ScanType.Tag
self.filterHardnessSlider.setEnabled(word_based)
self.matchSimilarBox.setEnabled(word_based)
self.wordWeightingBox.setEnabled(word_based)

View File

@ -6,7 +6,7 @@
# which should be included with this package. The terms are also available at
# http://www.hardcoded.net/licenses/hs_license
from core.scanner import SCAN_TYPE_FILENAME, SCAN_TYPE_CONTENT
from core.scanner import ScanType
from base.preferences import Preferences as PreferencesBase
@ -32,7 +32,7 @@ class Preferences(PreferencesBase):
def _reset_specific(self):
self.filter_hardness = 80
self.scan_type = SCAN_TYPE_CONTENT
self.scan_type = ScanType.Contents
self.word_weighting = True
self.match_similar = False
self.ignore_small_files = True

View File

@ -11,14 +11,14 @@ from PyQt4.QtGui import QDialog, QDialogButtonBox
from hsutil.misc import tryint
from core.scanner import SCAN_TYPE_FILENAME, SCAN_TYPE_CONTENT
from core.scanner import ScanType
from preferences_dialog_ui import Ui_PreferencesDialog
import preferences
SCAN_TYPE_ORDER = [
SCAN_TYPE_FILENAME,
SCAN_TYPE_CONTENT,
ScanType.Filename,
ScanType.Contents,
]
class PreferencesDialog(QDialog, Ui_PreferencesDialog):
@ -78,7 +78,7 @@ class PreferencesDialog(QDialog, Ui_PreferencesDialog):
def scanTypeChanged(self, index):
scan_type = SCAN_TYPE_ORDER[self.scanTypeComboBox.currentIndex()]
word_based = scan_type == SCAN_TYPE_FILENAME
word_based = scan_type == ScanType.Filename
self.filterHardnessSlider.setEnabled(word_based)
self.matchSimilarBox.setEnabled(word_based)
self.wordWeightingBox.setEnabled(word_based)