From 23d884b6e8ea1a59857c5e3f245abb7d24e2fbeb Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sun, 25 May 2014 16:25:47 -0700 Subject: [PATCH] Move MusicalKey type out of dbcore module This is library-specific. --- beets/dbcore/types.py | 30 ------------------------------ beets/library.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/beets/dbcore/types.py b/beets/dbcore/types.py index fea70a5a3..b4386d25b 100644 --- a/beets/dbcore/types.py +++ b/beets/dbcore/types.py @@ -14,8 +14,6 @@ """Representation of type information for DBCore model fields. """ -import re - from . import query from beets.util import str2bool @@ -174,31 +172,3 @@ class Boolean(Type): def parse(self, string): return str2bool(string) - - -class MusicalKey(String): - """String representing the musical key of a song. - - The standard format is C, Cm, C#, C#m, etc. - """ - - ENHARMONIC = { - r'db': 'c#', - r'eb': 'd#', - r'gb': 'f#', - r'ab': 'g#', - r'bb': 'a#', - } - - def parse(self, key): - key = key.lower() - for flat, sharp in self.ENHARMONIC.items(): - key = re.sub(flat, sharp, key) - key = re.sub(r'[\W\s]+minor', 'm', key) - return key.capitalize() - - def normalize(self, key): - if key is None: - return None - else: - return self.parse(key) diff --git a/beets/library.py b/beets/library.py index 840afa880..7d2272211 100644 --- a/beets/library.py +++ b/beets/library.py @@ -20,6 +20,7 @@ import logging import shlex import unicodedata import time +import re from unidecode import unidecode from beets.mediafile import MediaFile, MutagenError from beets import plugins @@ -107,6 +108,33 @@ class PathType(types.Type): return value +class MusicalKey(types.String): + """String representing the musical key of a song. + + The standard format is C, Cm, C#, C#m, etc. + """ + ENHARMONIC = { + r'db': 'c#', + r'eb': 'd#', + r'gb': 'f#', + r'ab': 'g#', + r'bb': 'a#', + } + + def parse(self, key): + key = key.lower() + for flat, sharp in self.ENHARMONIC.items(): + key = re.sub(flat, sharp, key) + key = re.sub(r'[\W\s]+minor', 'm', key) + return key.capitalize() + + def normalize(self, key): + if key is None: + return None + else: + return self.parse(key) + + # Special path format key. PF_KEY_DEFAULT = 'default' @@ -244,7 +272,7 @@ class Item(LibModel): 'original_year': types.PaddedInt(4), 'original_month': types.PaddedInt(2), 'original_day': types.PaddedInt(2), - 'initial_key': types.MusicalKey(), + 'initial_key': MusicalKey(), 'length': types.Float(), 'bitrate': types.ScaledInt(1000, u'kbps'),