Skip to content

Commit

Permalink
linter plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-Kamina committed Jun 12, 2024
1 parent d406bb7 commit 95371c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
21 changes: 13 additions & 8 deletions deluge/plugin_resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import logging
import sys


from contextlib import ExitStack
from os.path import isfile
from pathlib import Path
Expand All @@ -35,6 +33,7 @@ class PluginResourceManager:

class FileContextManager:
"""FileContextManager helps us keep track of open files to avoid creating multiple copies of any given file."""

def __init__(self):
self.files_open: dict[str, Path] = {}
self.stack = ExitStack()
Expand All @@ -44,8 +43,10 @@ def reset(self):
self.stack.pop_all().close()
self.files_open.clear()

resource_manager_lock = Lock() # Just in case, share a lock to prevent race conditions.
resource_managers: dict[str, FileContextManager] = {} # We keep a FileContextManager for each enabled plugin.
resource_manager_lock = Lock()
# Just in case, share a lock to prevent race conditions.
resource_managers: dict[str, FileContextManager] = {}
# We keep a FileContextManager for each enabled plugin.

@classmethod
def resource_filename(cls, module: str, path: str) -> str:
Expand All @@ -55,7 +56,9 @@ def resource_filename(cls, module: str, path: str) -> str:
package_name = packages_distributions()[module][0]
if module not in cls.resource_managers:
with cls.resource_manager_lock:
cls.resource_managers.update({module : PluginResourceManager.FileContextManager()})
cls.resource_managers.update(
{module : PluginResourceManager.FileContextManager()}
)
fs_path = Path()
if path not in cls.resource_managers[module].files_open:
fs_path = cls.resource_managers[module].stack.enter_context(file)
Expand All @@ -65,12 +68,12 @@ def resource_filename(cls, module: str, path: str) -> str:
fs_path = cls.resource_managers[module].stack.enter_context(file)
except ModuleNotFoundError as e:
raise ValueError(
f'Can\'t determine version for module {module} which maps to package: {package_name}'
f'Can\'t determine version for module {module} which maps to package: {package_name}'
) from e
except FileNotFoundError as e:
raise ValueError(f'File not found: {path}') from e
with cls.resource_manager_lock:
cls.resource_managers[module].files_open.update({path : fs_path})
cls.resource_managers[module].files_open.update({path: fs_path})
return str(fs_path)

@classmethod
Expand All @@ -81,7 +84,9 @@ def exists_for(cls, name: str) -> bool:
def prepare_for(cls, name: str):
cls.clear_for(name)
with cls.resource_manager_lock:
cls.resource_managers.update({name : PluginResourceManager.FileContextManager()})
cls.resource_managers.update(
{name : PluginResourceManager.FileContextManager()}
)

@classmethod
def clear_for(cls, name: str):
Expand Down
10 changes: 4 additions & 6 deletions deluge/pluginmanagerbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ def scan_for_plugins(self):
plugin_eggs = list(Path(base_dir).glob('*.egg'))
plugin_eggs.extend(list(Path(user_dir).glob('*.egg')))

plugin_dirs = [
str(f)
for f in plugin_wheels + plugin_eggs
if os.path.isfile(f)
]
plugin_dirs = [str(f) for f in plugin_wheels + plugin_eggs if os.path.isfile(f)]
plugin_dirs.extend([base_dir, user_dir] + base_subdir)
sys.path.extend(plugin_dirs)
plugin_eps = entry_points(group=self.entry_name)
Expand Down Expand Up @@ -171,7 +167,9 @@ def enable_plugin(self, plugin_name):
return defer.succeed(False)
except Exception as ex:
log.error(
'Unable to instantiate plugin %r from %r!', plugin_name, ep.loader.archive
'Unable to instantiate plugin %r from %r!',
plugin_name,
ep.loader.archive
)
log.exception(ex)
continue
Expand Down

0 comments on commit 95371c1

Please sign in to comment.