Skip to content

Commit

Permalink
convert: Reduce amount of duplicate code for linking.
Browse files Browse the repository at this point in the history
Also slightly reworded documentation.
  • Loading branch information
Vexatos committed Aug 21, 2019
1 parent aeb7d88 commit 151c043
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 81 deletions.
110 changes: 43 additions & 67 deletions beetsplug/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,45 +307,35 @@ def convert_item(self, dest_dir, keep_new, path_formats, fmt,
util.displayable_path(original))
util.move(item.path, original)

linked = False

if should_transcode(item, fmt):
linked = False
try:
self.encode(command, original, converted, pretend)
except subprocess.CalledProcessError:
continue
else:
if hardlink:
if pretend:
self._log.info(u'ln {0} {1}',
util.displayable_path(original),
util.displayable_path(converted))
else:
# No transcoding necessary.
self._log.info(u'Hardlinking {0}',
util.displayable_path(item.path))
linked = link or hardlink
if pretend:
msg = 'ln' if hardlink else ('ln -s' if link else 'cp')

self._log.info(u'{2} {0} {1}',
util.displayable_path(original),
util.displayable_path(converted),
msg)
else:
# No transcoding necessary.
msg = 'Hardlinking' if hardlink \
else ('Linking' if link else 'Copying')

self._log.info(u'{1} {0}',
util.displayable_path(item.path),
msg)

if hardlink:
util.hardlink(original, converted)
linked = True
elif link:
if pretend:
self._log.info(u'ln -s {0} {1}',
util.displayable_path(original),
util.displayable_path(converted))
else:
# No transcoding necessary.
self._log.info(u'Linking {0}',
util.displayable_path(item.path))
elif link:
util.link(original, converted)
linked = True
else:
if pretend:
self._log.info(u'cp {0} {1}',
util.displayable_path(original),
util.displayable_path(converted))
else:
# No transcoding necessary.
self._log.info(u'Copying {0}',
util.displayable_path(item.path))
util.copy(original, converted)

if pretend:
Expand Down Expand Up @@ -434,35 +424,26 @@ def copy_album_art(self, album, dest_dir, path_formats, pretend=False,
if not pretend:
ArtResizer.shared.resize(maxwidth, album.artpath, dest)
else:
if hardlink:
if pretend:
self._log.info(u'ln {0} {1}',
util.displayable_path(album.artpath),
util.displayable_path(dest))
else:
self._log.info(u'Hardlinking cover art from {0} to {1}',
util.displayable_path(album.artpath),
util.displayable_path(dest))
if pretend:
msg = 'ln' if hardlink else ('ln -s' if link else 'cp')

self._log.info(u'{2} {0} {1}',
util.displayable_path(album.artpath),
util.displayable_path(dest),
msg)
else:
msg = 'Hardlinking' if hardlink \
else ('Linking' if link else 'Copying')

self._log.info(u'{2} cover art from {0} to {1}',
util.displayable_path(album.artpath),
util.displayable_path(dest),
msg)
if hardlink:
util.hardlink(album.artpath, dest)
elif link:
if pretend:
self._log.info(u'ln -s {0} {1}',
util.displayable_path(album.artpath),
util.displayable_path(dest))
else:
self._log.info(u'Linking cover art from {0} to {1}',
util.displayable_path(album.artpath),
util.displayable_path(dest))
elif link:
util.link(album.artpath, dest)
else:
if pretend:
self._log.info(u'cp {0} {1}',
util.displayable_path(album.artpath),
util.displayable_path(dest))
else:
self._log.info(u'Copying cover art from {0} to {1}',
util.displayable_path(album.artpath),
util.displayable_path(dest))
util.copy(album.artpath, dest)

def convert_func(self, lib, opts, args):
Expand All @@ -482,21 +463,16 @@ def convert_func(self, lib, opts, args):
else:
pretend = self.config['pretend'].get(bool)

if opts.link is not None:
if opts.hardlink is not None:
hardlink = opts.hardlink
link = False
elif opts.link is not None:
hardlink = False
link = opts.link

if opts.hardlink is not None:
hardlink = opts.hardlink
else:
hardlink = self.config['hardlink'].get(bool) and not link
else:
hardlink = self.config['hardlink'].get(bool)
link = self.config['link'].get(bool)

if opts.hardlink is not None:
hardlink = opts.hardlink
else:
hardlink = self.config['hardlink'].get(bool)

if opts.album:
albums = lib.albums(ui.decargs(args))
items = [i for a in albums for i in a.items()]
Expand Down
10 changes: 4 additions & 6 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ New features:
(the MBID), and ``work_disambig`` (the disambiguation string).
Thanks to :user:`dosoe`.
:bug:`2580` :bug:`3272`
* :doc:`/plugins/convert`: Added new ``-l`` (``--link``) flag and ``link`` option
which symlinks files that do not need to be converted instead of copying them.
:bug:`2324`
* :doc:`/plugins/convert`: Added new ``-H`` (``--hardlink``) flag and ``hardlink``
option which hardlinks files that do not need to be converted
instead of copying them.
* :doc:`/plugins/convert`: Added new ``-l`` (``--link``) flag and ``link``
option as well as the ``-H`` (``--hardlink``) flag and ``hardlink``
option which symlinks or hardlinks files that do not need to
be converted instead of copying them.
:bug:`2324`
* :doc:`/plugins/bpd`: BPD now supports most of the features of version 0.16
of the MPD protocol. This is enough to get it talking to more complicated
Expand Down
12 changes: 4 additions & 8 deletions docs/plugins/convert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ them.
By default, files that do not need to be transcoded will be copied to their
destination. Passing the ``-l`` (``--link``) flag creates symbolic links
instead, passing ``-H`` (``--hardlink``) creates hard links.
Refer to the ``link`` and ``hardlink`` options below
for potential issues with this.
Note that album art embedding is disabled for files that are linked.
Refer to the ``link`` and ``hardlink`` options below.


Configuration
Expand Down Expand Up @@ -106,12 +106,8 @@ file. The available options are:
enabled. For this reason, album-art embedding is disabled
for files that are linked.
Default: ``false``.
- **hardlink**: By default, files that do not need to be transcoded will be
copied to their destination. This option creates hard links instead. Note that
options such as ``embed`` that modify the output files after the transcoding
step will cause the original files to be modified as well if ``hardlink`` is
enabled. For this reason, album-art embedding is disabled
for files that are linked.
- **hardlink**: This options works similar to ``link``, but it creates
hard links instead of symlinks.
This option overrides ``link``. Only works when converting to a directory
on the same filesystem as the library.
Default: ``false``.
Expand Down

0 comments on commit 151c043

Please sign in to comment.