Skip to content

Commit

Permalink
Fix initials
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jun 11, 2022
1 parent 68ed0d6 commit 562d91e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
22 changes: 17 additions & 5 deletions audiorename/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,23 @@ def fields_sorted(cls):
###############################################################################

@staticmethod
def _initials(value: str) -> str:
def _find_initials(value: str) -> str:
"""
:param str value: A string to extract the initials.
"""
return value[0:1].lower()
# To avoid ae -> a
value = Functions.tmpl_asciify(value)
# To avoid “!K7-Compilations” -> “!”
value = re.sub(r'^\W*', '', value)
initial = value[0:1].lower()

if re.match(r'\d', initial):
return '0'

if initial == '':
return '_'

return initial

@staticmethod
def _normalize_performer(
Expand Down Expand Up @@ -629,7 +641,7 @@ def ar_initial_album(self) -> typing.Optional[str]:
* ``Die Meistersinger von Nürnberg`` → ``d``
"""
if self.ar_combined_album:
return self._initials(self.ar_combined_album)
return self._find_initials(self.ar_combined_album)

@property
def ar_initial_artist(self):
Expand All @@ -642,7 +654,7 @@ def ar_initial_artist(self):
* ``Just Friends`` → ``j``
* ``Die Meistersinger von Nürnberg`` → ``d``
"""
return self._initials(self.ar_combined_artist_sort)
return self._find_initials(self.ar_combined_artist_sort)

@property
def ar_combined_artist(self) -> str:
Expand Down Expand Up @@ -712,7 +724,7 @@ def ar_initial_composer(self) -> str:
* :class:`audiorename.meta.Meta.ar_combined_composer`
"""
return self._initials(self.ar_combined_composer)
return self._find_initials(self.ar_combined_composer)

@property
def ar_combined_composer(self) -> str:
Expand Down
Binary file not shown.
4 changes: 2 additions & 2 deletions test/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
path_compilation = '/_compilations/t/the album_2001/4-02_full.mp3'


def get_testfile(*path_list):
def get_testfile(*path_list) -> str:
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files',
*path_list)

Expand All @@ -36,7 +36,7 @@ def get_meta(*path_list):
return audiorename.meta.Meta(get_testfile(*path_list), False)


def copy_to_tmp(*path_list):
def copy_to_tmp(*path_list) -> str:
orig = get_testfile(*path_list)

tmp = os.path.join(tempfile.mkdtemp(), os.path.basename(orig))
Expand Down
4 changes: 2 additions & 2 deletions test/test_audiofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class TestUnicodeUnittest(unittest.TestCase):

def setUp(self):
self.uni = helper.get_testfile('äöü', 'ÅåÆæØø.mp3')
self.renamed = os.path.join('/', '►', '$ar_combined_album',
self.renamed = os.path.join('/_', '►', '$ar_combined_album',
'$ar_combined_disctrack'
'_ÁáČčĎďÉéĚěÍíŇňÓóŘřŠšŤťÚúŮůÝýŽž.mp3')

Expand All @@ -300,7 +300,7 @@ def test_copy(self):

def tearDown(self):
try:
shutil.rmtree(helper.dir_cwd + '//')
shutil.rmtree(helper.dir_cwd + '/_/')
except OSError:
pass

Expand Down
17 changes: 17 additions & 0 deletions test/test_cli_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,23 @@ def test_wagner_04(self):
)


# --compilation
class TestCompilation(unittest.TestCase):

def assertDryRun(self, rel_path, test):
self.assertEqual(helper.dry_run([
helper.get_testfile('real-world', '_compilations', rel_path)
]), test)

def test_default(self):
self.assertDryRun(
os.path.join('k', 'K7-Compilation_2003',
'1-01_Supa-Sista-Modaji-Downlow-mix.mp3'),
'/_compilations/k/K7-Compilation_2003/'
'1-01_Supa-Sista-Modaji-Downlow-mix.mp3'
)


# --copy
class TestBasicCopy(unittest.TestCase):

Expand Down
4 changes: 2 additions & 2 deletions test/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,10 @@ def setUp(self):
self.meta = get_meta('files', 'album.mp3')

def test_lowercase(self):
self.assertEqual(self.meta._initials('beethoven'), 'b')
self.assertEqual(self.meta._find_initials('beethoven'), 'b')

def test_uppercase(self):
self.assertEqual(self.meta._initials('Beethoven'), 'b')
self.assertEqual(self.meta._find_initials('Beethoven'), 'b')


class TestStaticMethodNormalizePerformer(unittest.TestCase):
Expand Down

0 comments on commit 562d91e

Please sign in to comment.