[V3] Update code standards (black code format pass) (#1650)

* ran black: code formatter against `redbot/` with `-l 99`

* badge
This commit is contained in:
Michael H
2018-05-14 15:33:24 -04:00
committed by Will
parent e7476edd68
commit b88b5a2601
90 changed files with 3629 additions and 3223 deletions

View File

@@ -3,7 +3,7 @@ import asyncio
import discord
from discord.ext import commands
__all__ = ["install_agreement", ]
__all__ = ["install_agreement"]
REPO_INSTALL_MSG = (
"You're about to add a 3rd party repository. The creator of Red"
@@ -17,29 +17,28 @@ REPO_INSTALL_MSG = (
def install_agreement():
async def pred(ctx: commands.Context):
downloader = ctx.command.instance
if downloader is None:
return True
elif downloader.already_agreed:
return True
elif ctx.invoked_subcommand is None or \
isinstance(ctx.invoked_subcommand, commands.Group):
elif ctx.invoked_subcommand is None or isinstance(ctx.invoked_subcommand, commands.Group):
return True
def does_agree(msg: discord.Message):
return ctx.author == msg.author and \
ctx.channel == msg.channel and \
msg.content == "I agree"
return ctx.author == msg.author and ctx.channel == msg.channel and msg.content == "I agree"
await ctx.send(REPO_INSTALL_MSG)
try:
await ctx.bot.wait_for('message', check=does_agree, timeout=30)
await ctx.bot.wait_for("message", check=does_agree, timeout=30)
except asyncio.TimeoutError:
await ctx.send("Your response has timed out, please try again.")
return False
downloader.already_agreed = True
return True
return commands.check(pred)

View File

@@ -5,6 +5,7 @@ from .installable import Installable
class InstalledCog(commands.Converter):
async def convert(self, ctx: commands.Context, arg: str) -> Installable:
downloader = ctx.bot.get_cog("Downloader")
if downloader is None:
@@ -12,8 +13,6 @@ class InstalledCog(commands.Converter):
cog = discord.utils.get(await downloader.installed_cogs(), name=arg)
if cog is None:
raise commands.BadArgument(
"That cog is not installed"
)
raise commands.BadArgument("That cog is not installed")
return cog

View File

@@ -22,20 +22,18 @@ from .installable import Installable
from .log import log
from .repo_manager import RepoManager, Repo
_ = Translator('Downloader', __file__)
_ = Translator("Downloader", __file__)
@cog_i18n(_)
class Downloader:
def __init__(self, bot: Red):
self.bot = bot
self.conf = Config.get_conf(self, identifier=998240343,
force_registration=True)
self.conf = Config.get_conf(self, identifier=998240343, force_registration=True)
self.conf.register_global(
installed=[]
)
self.conf.register_global(installed=[])
self.already_agreed = False
@@ -46,7 +44,7 @@ class Downloader:
self.LIB_PATH.mkdir(parents=True, exist_ok=True)
self.SHAREDLIB_PATH.mkdir(parents=True, exist_ok=True)
if not self.SHAREDLIB_INIT.exists():
with self.SHAREDLIB_INIT.open(mode='w', encoding='utf-8') as _:
with self.SHAREDLIB_INIT.open(mode="w", encoding="utf-8") as _:
pass
if str(self.LIB_PATH) not in syspath:
@@ -170,7 +168,7 @@ class Downloader:
for repo, reqs in has_reqs:
for req in reqs:
# noinspection PyTypeChecker
ret = ret and await repo.install_raw_requirements([req, ], self.LIB_PATH)
ret = ret and await repo.install_raw_requirements([req], self.LIB_PATH)
return ret
@staticmethod
@@ -200,8 +198,12 @@ class Downloader:
if success:
await ctx.send(_("Libraries installed."))
else:
await ctx.send(_("Some libraries failed to install. Please check"
" your logs for a complete list."))
await ctx.send(
_(
"Some libraries failed to install. Please check"
" your logs for a complete list."
)
)
@commands.group()
@checks.is_owner()
@@ -214,7 +216,7 @@ class Downloader:
@repo.command(name="add")
@install_agreement()
async def _repo_add(self, ctx, name: str, repo_url: str, branch: str=None):
async def _repo_add(self, ctx, name: str, repo_url: str, branch: str = None):
"""
Add a new repo to Downloader.
@@ -223,11 +225,7 @@ class Downloader:
"""
try:
# noinspection PyTypeChecker
repo = await self._repo_manager.add_repo(
name=name,
url=repo_url,
branch=branch
)
repo = await self._repo_manager.add_repo(name=name, url=repo_url, branch=branch)
except ExistingGitRepo:
await ctx.send(_("That git repo has already been added under another name."))
except CloningError:
@@ -275,20 +273,28 @@ class Downloader:
"""
cog = discord.utils.get(repo_name.available_cogs, name=cog_name) # type: Installable
if cog is None:
await ctx.send(_("Error, there is no cog by the name of"
" `{}` in the `{}` repo.").format(cog_name, repo_name.name))
await ctx.send(
_("Error, there is no cog by the name of" " `{}` in the `{}` repo.").format(
cog_name, repo_name.name
)
)
return
elif cog.min_python_version > sys.version_info:
await ctx.send(_(
"This cog requires at least python version {}, aborting install.".format(
'.'.join([str(n) for n in cog.min_python_version])
await ctx.send(
_(
"This cog requires at least python version {}, aborting install.".format(
".".join([str(n) for n in cog.min_python_version])
)
)
))
)
return
if not await repo_name.install_requirements(cog, self.LIB_PATH):
await ctx.send(_("Failed to install the required libraries for"
" `{}`: `{}`").format(cog.name, cog.requirements))
await ctx.send(
_("Failed to install the required libraries for" " `{}`: `{}`").format(
cog.name, cog.requirements
)
)
return
await repo_name.install_cog(cog, await self.cog_install_path())
@@ -317,12 +323,16 @@ class Downloader:
await self._remove_from_installed(cog_name)
await ctx.send(_("`{}` was successfully removed.").format(real_name))
else:
await ctx.send(_("That cog was installed but can no longer"
" be located. You may need to remove it's"
" files manually if it is still usable."))
await ctx.send(
_(
"That cog was installed but can no longer"
" be located. You may need to remove it's"
" files manually if it is still usable."
)
)
@cog.command(name="update")
async def _cog_update(self, ctx, cog_name: InstalledCog=None):
async def _cog_update(self, ctx, cog_name: InstalledCog = None):
"""
Updates all cogs or one of your choosing.
"""
@@ -358,7 +368,8 @@ class Downloader:
"""
cogs = repo_name.available_cogs
cogs = _("Available Cogs:\n") + "\n".join(
["+ {}: {}".format(c.name, c.short or "") for c in cogs])
["+ {}: {}".format(c.name, c.short or "") for c in cogs]
)
await ctx.send(box(cogs, lang="diff"))
@@ -369,9 +380,9 @@ class Downloader:
"""
cog = discord.utils.get(repo_name.available_cogs, name=cog_name)
if cog is None:
await ctx.send(_("There is no cog `{}` in the repo `{}`").format(
cog_name, repo_name.name
))
await ctx.send(
_("There is no cog `{}` in the repo `{}`").format(cog_name, repo_name.name)
)
return
msg = _("Information on {}:\n{}").format(cog.name, cog.description or "")
@@ -397,8 +408,9 @@ class Downloader:
return True, installable
return False, None
def format_findcog_info(self, command_name: str,
cog_installable: Union[Installable, object]=None) -> str:
def format_findcog_info(
self, command_name: str, cog_installable: Union[Installable, object] = None
) -> str:
"""Format a cog's info for output to discord.
Parameters
@@ -444,7 +456,7 @@ class Downloader:
The name of the cog according to Downloader..
"""
splitted = instance.__module__.split('.')
splitted = instance.__module__.split(".")
return splitted[-2]
@commands.command()

View File

@@ -1,6 +1,16 @@
__all__ = ["DownloaderException", "GitException", "InvalidRepoName", "ExistingGitRepo",
"MissingGitRepo", "CloningError", "CurrentHashError", "HardResetError",
"UpdateError", "GitDiffError", "PipError"]
__all__ = [
"DownloaderException",
"GitException",
"InvalidRepoName",
"ExistingGitRepo",
"MissingGitRepo",
"CloningError",
"CurrentHashError",
"HardResetError",
"UpdateError",
"GitDiffError",
"PipError",
]
class DownloaderException(Exception):

View File

@@ -56,6 +56,7 @@ class Installable(RepoJSONMixin):
:class:`InstallationType`.
"""
def __init__(self, location: Path):
"""Base installable initializer.
@@ -114,13 +115,9 @@ class Installable(RepoJSONMixin):
# noinspection PyBroadException
try:
copy_func(
src=str(self._location),
dst=str(target_dir / self._location.stem)
)
copy_func(src=str(self._location), dst=str(target_dir / self._location.stem))
except:
log.exception("Error occurred when copying path:"
" {}".format(self._location))
log.exception("Error occurred when copying path:" " {}".format(self._location))
return False
return True
@@ -130,7 +127,7 @@ class Installable(RepoJSONMixin):
if self._info_file.exists():
self._process_info_file()
def _process_info_file(self, info_file_path: Path=None) -> MutableMapping[str, Any]:
def _process_info_file(self, info_file_path: Path = None) -> MutableMapping[str, Any]:
"""
Processes an information file. Loads dependencies among other
information into this object.
@@ -144,13 +141,14 @@ class Installable(RepoJSONMixin):
raise ValueError("No valid information file path was found.")
info = {}
with info_file_path.open(encoding='utf-8') as f:
with info_file_path.open(encoding="utf-8") as f:
try:
info = json.load(f)
except json.JSONDecodeError:
info = {}
log.exception("Invalid JSON information file at path:"
" {}".format(info_file_path))
log.exception(
"Invalid JSON information file at path:" " {}".format(info_file_path)
)
else:
self._info = info
@@ -167,7 +165,7 @@ class Installable(RepoJSONMixin):
self.bot_version = bot_version
try:
min_python_version = tuple(info.get('min_python_version', [3, 5, 1]))
min_python_version = tuple(info.get("min_python_version", [3, 5, 1]))
except ValueError:
min_python_version = self.min_python_version
self.min_python_version = min_python_version
@@ -200,15 +198,12 @@ class Installable(RepoJSONMixin):
return info
def to_json(self):
return {
"repo_name": self.repo_name,
"cog_name": self.name
}
return {"repo_name": self.repo_name, "cog_name": self.name}
@classmethod
def from_json(cls, data: dict, repo_mgr: "RepoManager"):
repo_name = data['repo_name']
cog_name = data['cog_name']
repo_name = data["repo_name"]
cog_name = data["cog_name"]
repo = repo_mgr.get_repo(repo_name)
if repo is not None:

View File

@@ -24,7 +24,7 @@ class RepoJSONMixin:
return
try:
with self._info_file.open(encoding='utf-8') as f:
with self._info_file.open(encoding="utf-8") as f:
info = json.load(f)
except json.JSONDecodeError:
return
@@ -34,4 +34,4 @@ class RepoJSONMixin:
self.author = info.get("author")
self.install_msg = info.get("install_msg")
self.short = info.get("short")
self.description = info.get("description")
self.description = info.get("description")

View File

@@ -1,15 +1,11 @@
import subprocess
TO_TRANSLATE = [
'../downloader.py'
]
TO_TRANSLATE = ["../downloader.py"]
def regen_messages():
subprocess.run(
['pygettext', '-n'] + TO_TRANSLATE
)
subprocess.run(["pygettext", "-n"] + TO_TRANSLATE)
if __name__ == "__main__":
regen_messages()
regen_messages()

View File

@@ -1,3 +1,3 @@
import logging
log = logging.getLogger("red.downloader")
log = logging.getLogger("red.downloader")

View File

@@ -27,16 +27,23 @@ class Repo(RepoJSONMixin):
GIT_LATEST_COMMIT = "git -C {path} rev-parse {branch}"
GIT_HARD_RESET = "git -C {path} reset --hard origin/{branch} -q"
GIT_PULL = "git -C {path} pull -q --ff-only"
GIT_DIFF_FILE_STATUS = ("git -C {path} diff --no-commit-id --name-status"
" {old_hash} {new_hash}")
GIT_LOG = ("git -C {path} log --relative-date --reverse {old_hash}.."
" {relative_file_path}")
GIT_DIFF_FILE_STATUS = (
"git -C {path} diff --no-commit-id --name-status" " {old_hash} {new_hash}"
)
GIT_LOG = ("git -C {path} log --relative-date --reverse {old_hash}.." " {relative_file_path}")
GIT_DISCOVER_REMOTE_URL = "git -C {path} config --get remote.origin.url"
PIP_INSTALL = "{python} -m pip install -U -t {target_dir} {reqs}"
def __init__(self, name: str, url: str, branch: str, folder_path: Path,
available_modules: Tuple[Installable]=(), loop: asyncio.AbstractEventLoop=None):
def __init__(
self,
name: str,
url: str,
branch: str,
folder_path: Path,
available_modules: Tuple[Installable] = (),
loop: asyncio.AbstractEventLoop = None,
):
self.url = url
self.branch = branch
@@ -71,11 +78,12 @@ class Repo(RepoJSONMixin):
return poss_repo
def _existing_git_repo(self) -> (bool, Path):
git_path = self.folder_path / '.git'
git_path = self.folder_path / ".git"
return git_path.exists(), git_path
async def _get_file_update_statuses(
self, old_hash: str, new_hash: str) -> MutableMapping[str, str]:
self, old_hash: str, new_hash: str
) -> MutableMapping[str, str]:
"""
Gets the file update status letters for each changed file between
the two hashes.
@@ -85,29 +93,25 @@ class Repo(RepoJSONMixin):
"""
p = await self._run(
self.GIT_DIFF_FILE_STATUS.format(
path=self.folder_path,
old_hash=old_hash,
new_hash=new_hash
path=self.folder_path, old_hash=old_hash, new_hash=new_hash
)
)
if p.returncode != 0:
raise GitDiffError("Git diff failed for repo at path:"
" {}".format(self.folder_path))
raise GitDiffError("Git diff failed for repo at path:" " {}".format(self.folder_path))
stdout = p.stdout.strip().decode().split('\n')
stdout = p.stdout.strip().decode().split("\n")
ret = {}
for filename in stdout:
# TODO: filter these filenames by ones in self.available_modules
status, _, filepath = filename.partition('\t')
status, _, filepath = filename.partition("\t")
ret[filepath] = status
return ret
async def _get_commit_notes(self, old_commit_hash: str,
relative_file_path: str) -> str:
async def _get_commit_notes(self, old_commit_hash: str, relative_file_path: str) -> str:
"""
Gets the commit notes from git log.
:param old_commit_hash: Point in time to start getting messages
@@ -119,13 +123,15 @@ class Repo(RepoJSONMixin):
self.GIT_LOG.format(
path=self.folder_path,
old_hash=old_commit_hash,
relative_file_path=relative_file_path
relative_file_path=relative_file_path,
)
)
if p.returncode != 0:
raise GitException("An exception occurred while executing git log on"
" this repo: {}".format(self.folder_path))
raise GitException(
"An exception occurred while executing git log on"
" this repo: {}".format(self.folder_path)
)
return p.stdout.decode().strip()
@@ -146,10 +152,8 @@ class Repo(RepoJSONMixin):
Installable(location=name)
)
"""
for file_finder, name, is_pkg in pkgutil.walk_packages(path=[str(self.folder_path), ]):
curr_modules.append(
Installable(location=self.folder_path / name)
)
for file_finder, name, is_pkg in pkgutil.walk_packages(path=[str(self.folder_path)]):
curr_modules.append(Installable(location=self.folder_path / name))
self.available_modules = curr_modules
# noinspection PyTypeChecker
@@ -157,12 +161,11 @@ class Repo(RepoJSONMixin):
async def _run(self, *args, **kwargs):
env = os.environ.copy()
env['GIT_TERMINAL_PROMPT'] = '0'
kwargs['env'] = env
env["GIT_TERMINAL_PROMPT"] = "0"
kwargs["env"] = env
async with self._repo_lock:
return await self._loop.run_in_executor(
self._executor,
functools.partial(sp_run, *args, stdout=PIPE, **kwargs)
self._executor, functools.partial(sp_run, *args, stdout=PIPE, **kwargs)
)
async def clone(self) -> Tuple[str]:
@@ -176,24 +179,17 @@ class Repo(RepoJSONMixin):
"""
exists, path = self._existing_git_repo()
if exists:
raise ExistingGitRepo(
"A git repo already exists at path: {}".format(path)
)
raise ExistingGitRepo("A git repo already exists at path: {}".format(path))
if self.branch is not None:
p = await self._run(
self.GIT_CLONE.format(
branch=self.branch,
url=self.url,
folder=self.folder_path
branch=self.branch, url=self.url, folder=self.folder_path
).split()
)
else:
p = await self._run(
self.GIT_CLONE_NO_BRANCH.format(
url=self.url,
folder=self.folder_path
).split()
self.GIT_CLONE_NO_BRANCH.format(url=self.url, folder=self.folder_path).split()
)
if p.returncode != 0:
@@ -217,23 +213,18 @@ class Repo(RepoJSONMixin):
"""
exists, _ = self._existing_git_repo()
if not exists:
raise MissingGitRepo(
"A git repo does not exist at path: {}".format(self.folder_path)
)
raise MissingGitRepo("A git repo does not exist at path: {}".format(self.folder_path))
p = await self._run(
self.GIT_CURRENT_BRANCH.format(
path=self.folder_path
).split()
)
p = await self._run(self.GIT_CURRENT_BRANCH.format(path=self.folder_path).split())
if p.returncode != 0:
raise GitException("Could not determine current branch"
" at path: {}".format(self.folder_path))
raise GitException(
"Could not determine current branch" " at path: {}".format(self.folder_path)
)
return p.stdout.decode().strip()
async def current_commit(self, branch: str=None) -> str:
async def current_commit(self, branch: str = None) -> str:
"""Determine the current commit hash of the repo.
Parameters
@@ -252,15 +243,10 @@ class Repo(RepoJSONMixin):
exists, _ = self._existing_git_repo()
if not exists:
raise MissingGitRepo(
"A git repo does not exist at path: {}".format(self.folder_path)
)
raise MissingGitRepo("A git repo does not exist at path: {}".format(self.folder_path))
p = await self._run(
self.GIT_LATEST_COMMIT.format(
path=self.folder_path,
branch=branch
).split()
self.GIT_LATEST_COMMIT.format(path=self.folder_path, branch=branch).split()
)
if p.returncode != 0:
@@ -268,7 +254,7 @@ class Repo(RepoJSONMixin):
return p.stdout.decode().strip()
async def current_url(self, folder: Path=None) -> str:
async def current_url(self, folder: Path = None) -> str:
"""
Discovers the FETCH URL for a Git repo.
@@ -290,18 +276,14 @@ class Repo(RepoJSONMixin):
if folder is None:
folder = self.folder_path
p = await self._run(
Repo.GIT_DISCOVER_REMOTE_URL.format(
path=folder
).split()
)
p = await self._run(Repo.GIT_DISCOVER_REMOTE_URL.format(path=folder).split())
if p.returncode != 0:
raise RuntimeError("Unable to discover a repo URL.")
return p.stdout.decode().strip()
async def hard_reset(self, branch: str=None) -> None:
async def hard_reset(self, branch: str = None) -> None:
"""Perform a hard reset on the current repo.
Parameters
@@ -315,21 +297,18 @@ class Repo(RepoJSONMixin):
exists, _ = self._existing_git_repo()
if not exists:
raise MissingGitRepo(
"A git repo does not exist at path: {}".format(self.folder_path)
)
raise MissingGitRepo("A git repo does not exist at path: {}".format(self.folder_path))
p = await self._run(
self.GIT_HARD_RESET.format(
path=self.folder_path,
branch=branch
).split()
self.GIT_HARD_RESET.format(path=self.folder_path, branch=branch).split()
)
if p.returncode != 0:
raise HardResetError("Some error occurred when trying to"
" execute a hard reset on the repo at"
" the following path: {}".format(self.folder_path))
raise HardResetError(
"Some error occurred when trying to"
" execute a hard reset on the repo at"
" the following path: {}".format(self.folder_path)
)
async def update(self) -> (str, str):
"""Update the current branch of this repo.
@@ -345,15 +324,13 @@ class Repo(RepoJSONMixin):
await self.hard_reset(branch=curr_branch)
p = await self._run(
self.GIT_PULL.format(
path=self.folder_path
).split()
)
p = await self._run(self.GIT_PULL.format(path=self.folder_path).split())
if p.returncode != 0:
raise UpdateError("Git pull returned a non zero exit code"
" for the repo located at path: {}".format(self.folder_path))
raise UpdateError(
"Git pull returned a non zero exit code"
" for the repo located at path: {}".format(self.folder_path)
)
new_commit = await self.current_commit(branch=curr_branch)
@@ -389,7 +366,9 @@ class Repo(RepoJSONMixin):
return await cog.copy_to(target_dir=target_dir)
async def install_libraries(self, target_dir: Path, libraries: Tuple[Installable]=()) -> bool:
async def install_libraries(
self, target_dir: Path, libraries: Tuple[Installable] = ()
) -> bool:
"""Install shared libraries to the target directory.
If :code:`libraries` is not specified, all shared libraries in the repo
@@ -469,16 +448,16 @@ class Repo(RepoJSONMixin):
p = await self._run(
self.PIP_INSTALL.format(
python=executable,
target_dir=target_dir,
reqs=" ".join(requirements)
python=executable, target_dir=target_dir, reqs=" ".join(requirements)
).split()
)
if p.returncode != 0:
log.error("Something went wrong when installing"
" the following requirements:"
" {}".format(", ".join(requirements)))
log.error(
"Something went wrong when installing"
" the following requirements:"
" {}".format(", ".join(requirements))
)
return False
return True
@@ -490,8 +469,7 @@ class Repo(RepoJSONMixin):
"""
# noinspection PyTypeChecker
return tuple(
[m for m in self.available_modules
if m.type == InstallableType.COG and not m.hidden]
[m for m in self.available_modules if m.type == InstallableType.COG and not m.hidden]
)
@property
@@ -501,8 +479,7 @@ class Repo(RepoJSONMixin):
"""
# noinspection PyTypeChecker
return tuple(
[m for m in self.available_modules
if m.type == InstallableType.SHARED_LIBRARY]
[m for m in self.available_modules if m.type == InstallableType.SHARED_LIBRARY]
)
@classmethod
@@ -515,6 +492,7 @@ class Repo(RepoJSONMixin):
class RepoManager:
def __init__(self, downloader_config: Config):
self.downloader_config = downloader_config
@@ -526,7 +504,7 @@ class RepoManager:
@property
def repos_folder(self) -> Path:
data_folder = data_manager.cog_data_path(self)
return data_folder / 'repos'
return data_folder / "repos"
def does_repo_exist(self, name: str) -> bool:
return name in self._repos
@@ -537,7 +515,7 @@ class RepoManager:
raise InvalidRepoName("Not a valid Python variable name.")
return name.lower()
async def add_repo(self, url: str, name: str, branch: str="master") -> Repo:
async def add_repo(self, url: str, name: str, branch: str = "master") -> Repo:
"""Add and clone a git repository.
Parameters
@@ -557,13 +535,11 @@ class RepoManager:
"""
if self.does_repo_exist(name):
raise InvalidRepoName(
"That repo name you provided already exists."
" Please choose another."
"That repo name you provided already exists." " Please choose another."
)
# noinspection PyTypeChecker
r = Repo(url=url, name=name, branch=branch,
folder_path=self.repos_folder / name)
r = Repo(url=url, name=name, branch=branch, folder_path=self.repos_folder / name)
await r.clone()
self._repos[name] = r