Skip to content

Commit 89aa5c7

Browse files
committed
Merge branch 'master' into lazy-album-fetch-in-formatter
2 parents 0754940 + 9ddf028 commit 89aa5c7

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

beets/dbcore/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import contextlib
2626

2727
import beets
28-
from beets.util.functemplate import Template
28+
from beets.util import functemplate
2929
from beets.util import py3_path
3030
from beets.dbcore import types
3131
from .query import MatchQuery, NullSort, TrueQuery
@@ -597,7 +597,7 @@ def evaluate_template(self, template, for_path=False):
597597
"""
598598
# Perform substitution.
599599
if isinstance(template, six.string_types):
600-
template = Template(template)
600+
template = functemplate.template(template)
601601
return template.substitute(self.formatted(for_path),
602602
self._template_funcs())
603603

beets/library.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from beets import util
3232
from beets.util import bytestring_path, syspath, normpath, samefile, \
3333
MoveOperation, lazy_property
34-
from beets.util.functemplate import Template
34+
from beets.util.functemplate import template, Template
3535
from beets import dbcore
3636
from beets.dbcore import types
3737
import beets
@@ -867,7 +867,7 @@ def destination(self, fragment=False, basedir=None, platform=None,
867867
if isinstance(path_format, Template):
868868
subpath_tmpl = path_format
869869
else:
870-
subpath_tmpl = Template(path_format)
870+
subpath_tmpl = template(path_format)
871871

872872
# Evaluate the selected template.
873873
subpath = self.evaluate_template(subpath_tmpl, True)
@@ -947,7 +947,7 @@ class Album(LibModel):
947947
'releasegroupdisambig': types.STRING,
948948
'rg_album_gain': types.NULL_FLOAT,
949949
'rg_album_peak': types.NULL_FLOAT,
950-
'r128_album_gain': types.PaddedInt(6),
950+
'r128_album_gain': types.NullPaddedInt(6),
951951
'original_year': types.PaddedInt(4),
952952
'original_month': types.PaddedInt(2),
953953
'original_day': types.PaddedInt(2),
@@ -1146,7 +1146,7 @@ def art_destination(self, image, item_dir=None):
11461146
image = bytestring_path(image)
11471147
item_dir = item_dir or self.item_dir()
11481148

1149-
filename_tmpl = Template(
1149+
filename_tmpl = template(
11501150
beets.config['art_filename'].as_str())
11511151
subpath = self.evaluate_template(filename_tmpl, True)
11521152
if beets.config['asciify_paths']:

beets/ui/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from beets import library
3737
from beets import plugins
3838
from beets import util
39-
from beets.util.functemplate import Template
39+
from beets.util.functemplate import template
4040
from beets import config
4141
from beets.util import confit, as_string
4242
from beets.autotag import mb
@@ -616,7 +616,7 @@ def get_path_formats(subview=None):
616616
subview = subview or config['paths']
617617
for query, view in subview.items():
618618
query = PF_KEY_QUERIES.get(query, query) # Expand common queries.
619-
path_formats.append((query, Template(view.as_str())))
619+
path_formats.append((query, template(view.as_str())))
620620
return path_formats
621621

622622

beets/util/functemplate.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import types
3636
import sys
3737
import six
38+
import functools
3839

3940
SYMBOL_DELIM = u'$'
4041
FUNC_DELIM = u'%'
@@ -553,8 +554,23 @@ def _parse(template):
553554
return Expression(parts)
554555

555556

556-
# External interface.
557+
def cached(func):
558+
"""Like the `functools.lru_cache` decorator, but works (as a no-op)
559+
on Python < 3.2.
560+
"""
561+
if hasattr(functools, 'lru_cache'):
562+
return functools.lru_cache(maxsize=128)(func)
563+
else:
564+
# Do nothing when lru_cache is not available.
565+
return func
566+
557567

568+
@cached
569+
def template(fmt):
570+
return Template(fmt)
571+
572+
573+
# External interface.
558574
class Template(object):
559575
"""A string template, including text, Symbols, and Calls.
560576
"""

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ Some improvements have been focused on improving beets' performance:
117117
to be displayed.
118118
Thanks to :user:`pprkut`.
119119
:bug:`3089`
120+
* Another query optimization works by compiling templates once and reusing
121+
them instead of recompiling them to print out each matching object.
122+
Thanks to :user:`SimonPersson`.
123+
:bug:`3258`
120124
* :doc:`/plugins/absubmit`, :doc:`/plugins/badfiles`: Analysis now works in
121125
parallel (on Python 3 only).
122126
Thanks to :user:`bemeurer`.

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = py27-test, py37-test, py38-test, py27-flake8, docs
7+
envlist = py27-test, py37-test, py27-flake8, docs
88

99
# The exhaustive list of environments is:
1010
# envlist = py{27,34,35}-{test,cov}, py{27,34,35}-flake8, docs

0 commit comments

Comments
 (0)