Skip to content

Commit

Permalink
Do not return objects modified in place
Browse files Browse the repository at this point in the history
In Python, a mutable object such as a `dict` is passed by reference to a
function. Do not return a `dict` when a method modifies it.
  • Loading branch information
DocMarty84 committed Dec 31, 2019
1 parent 476e46b commit b05578d
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions models/oomusic_folder_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ def _build_cache_folder(self, folder_id, user_id, cache):
:param int folder_id: ID of the folder to scan
:param int user_id: ID of the user to whom belongs the folder
:return dict: content of the cache
"""
params = (user_id, folder_id)
query = """
Expand Down Expand Up @@ -264,8 +263,7 @@ def _manage_dir(self, rootdir, cache):
:param str rootdir: folder to check
:param dict cache: reading cache
:return dict, bool: tuple with content of the cache updated, and boolean which indicates if
the folder can be skipped
:return bool: indicates if the folder scanning can be skipped
"""
skip = False
folder = cache["folder"].get(rootdir)
Expand All @@ -288,7 +286,6 @@ def _create_folder(self, rootdir, cache):
:param str rootdir: path of the folder to create
:param dict cache: reading cache
:return dict: cache updated
"""
parent_dir = os.sep.join(rootdir.split(os.sep)[:-1])
parent_dir = cache["folder"].get(parent_dir)
Expand Down Expand Up @@ -333,7 +330,6 @@ def _create_related(self, vals, cache, rootdir):
:param dict vals: data of the track
:param dict cache: reading cache
:param str rootdir: path of the folder being scanned
:return dict: cache updated
"""
MusicAlbum = self.env["oomusic.album"]
MusicArtist = self.env["oomusic.artist"]
Expand Down Expand Up @@ -367,8 +363,6 @@ def _create_related(self, vals, cache, rootdir):
{"name": vals["genre_id"], "user_id": cache["user_id"]}
).id

return cache

def _replace_related_by_id(self, vals, cache, rootdir):
"""
Replace the data of the track with the ID of the related object. In other words, convert the
Expand All @@ -377,7 +371,6 @@ def _replace_related_by_id(self, vals, cache, rootdir):
:param dict vals: data of the track
:param dict cache: reading cache
:param str rootdir: path of the folder being scanned
:return dict: vals updated
"""
if vals.get("album_id"):
vals["album_id"] = cache["album"][(vals["album_id"], cache["folder"][rootdir][0])]
Expand All @@ -390,16 +383,13 @@ def _replace_related_by_id(self, vals, cache, rootdir):
if vals.get("genre_id"):
vals["genre_id"] = cache["genre"][vals["genre_id"]]

return vals

def _update_cache_write(self, vals, cache_write):
"""
Update the write cache with additional information. For example adds the year, artist and
genre to an album.
:param dict vals: data of the track
:param dict cache_write: writing cache
:return dict: cache updated
"""
if vals.get("album_id") and vals["album_id"] not in cache_write["album"]:
cache_write["album"][vals["album_id"]] = {
Expand All @@ -408,8 +398,6 @@ def _update_cache_write(self, vals, cache_write):
"genre_id": vals.get("genre_id"),
}

return cache_write

def _write_cache_write(self, cache_write):
"""
Performs the write oopration of the write_cache
Expand Down Expand Up @@ -523,10 +511,10 @@ def _scan_folder(self, folder_id):
)

# Create new album, artist or genre, and update the cache
cache = self._create_related(vals, cache, rootdir)
self._create_related(vals, cache, rootdir)

# Replace album, artist or genre by ID
vals = self._replace_related_by_id(vals, cache, rootdir)
self._replace_related_by_id(vals, cache, rootdir)

# Add missing fields
if tag.__name__ == "taglib":
Expand Down

0 comments on commit b05578d

Please sign in to comment.