Skip to content

Commit

Permalink
Enable string normalization in Black (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
soininen authored Apr 2, 2024
2 parents 2a045b5 + 697fc85 commit 45fc13f
Show file tree
Hide file tree
Showing 20 changed files with 273 additions and 274 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ ignore_errors = true

[tool.black]
line-length = 120
skip-string-normalization = true
exclude = '\.git'
86 changes: 43 additions & 43 deletions spine_engine/execution_managers/conda_kernel_spec_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _validate_kernelspec_path(self, proposal):
return new_value

name_format = Unicode(
'{language} [conda env:{environment}]',
"{language} [conda env:{environment}]",
config=True,
help="""String name format; available field names within the string:
'{0}' = Language
Expand Down Expand Up @@ -106,15 +106,15 @@ def clean_kernel_name(kname):
a bit of effort to preserve readability.
"""
try:
kname.encode('ascii')
kname.encode("ascii")
except UnicodeEncodeError:
# Replace accented characters with unaccented equivalents
import unicodedata

nfkd_form = unicodedata.normalize('NFKD', kname)
kname = u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
nfkd_form = unicodedata.normalize("NFKD", kname)
kname = "".join([c for c in nfkd_form if not unicodedata.combining(c)])
# Replace anything else, including spaces, with underscores
kname = re.sub(r'[^a-zA-Z0-9._\-]', '_', kname)
kname = re.sub(r"[^a-zA-Z0-9._\-]", "_", kname)
return kname

@property
Expand All @@ -131,15 +131,15 @@ def _conda_info(self):
# This is to make sure that subprocess can find 'conda' even if
# it is a Windows batch file---which is the case in non-root
# conda environments.
shell = self._conda_executable == 'conda' and sys.platform.startswith('win')
shell = self._conda_executable == "conda" and sys.platform.startswith("win")
try:
# conda info --json uses the standard JSON escaping
# mechanism for non-ASCII characters. So it is always
# valid to decode here as 'ascii', since the JSON loads()
# method will recover any original Unicode for us.
p = subprocess.check_output([self._conda_executable, "info", "--json"], shell=shell).decode("ascii")
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
result = ansi_escape.sub('', p) # Remove ANSI Escape Sequences, such as ESC[0m
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
result = ansi_escape.sub("", p) # Remove ANSI Escape Sequences, such as ESC[0m
conda_info = json.loads(result)
except Exception as err:
conda_info = None
Expand All @@ -155,24 +155,24 @@ def _all_envs(self):
environment names as keys, and full paths as values.
"""
conda_info = self._conda_info
envs = conda_info['envs']
base_prefix = conda_info['conda_prefix']
envs_prefix = join(base_prefix, 'envs')
build_prefix = join(base_prefix, 'conda-bld', '')
envs = conda_info["envs"]
base_prefix = conda_info["conda_prefix"]
envs_prefix = join(base_prefix, "envs")
build_prefix = join(base_prefix, "conda-bld", "")
# Older versions of conda do not seem to include the base prefix
# in the environment list, but we do want to scan that
if base_prefix not in envs:
envs.insert(0, base_prefix)
envs_dirs = conda_info['envs_dirs']
envs_dirs = conda_info["envs_dirs"]
if not envs_dirs:
envs_dirs = [join(base_prefix, 'envs')]
envs_dirs = [join(base_prefix, "envs")]
all_envs = {}
for env_path in envs:
if self.env_filter is not None:
if self._env_filter_regex.search(env_path):
continue
if env_path == base_prefix:
env_name = 'root'
env_name = "root"
elif env_path.startswith(build_prefix):
# Skip the conda-bld directory entirely
continue
Expand All @@ -183,13 +183,13 @@ def _all_envs(self):
# directory named 'envs' is a collection of environments
# as created by, say, conda or anaconda-project. The name
# of the parent directory, then, provides useful context.
if basename(env_base) == 'envs' and (env_base != envs_prefix or env_name in all_envs):
env_name = u'{}-{}'.format(basename(dirname(env_base)), env_name)
if basename(env_base) == "envs" and (env_base != envs_prefix or env_name in all_envs):
env_name = "{}-{}".format(basename(dirname(env_base)), env_name)
# Further disambiguate, if necessary, with a counter.
if env_name in all_envs:
base_name = env_name
for count in range(len(all_envs)):
env_name = u'{}-{}'.format(base_name, count + 2)
env_name = "{}-{}".format(base_name, count + 2)
if env_name not in all_envs:
break
all_envs[env_name] = env_path
Expand All @@ -209,16 +209,16 @@ def _all_specs(self):
all_specs = {}
# We need to be able to find conda-run in the base conda environment
# even if this package is not running there
conda_prefix = self._conda_info['conda_prefix']
conda_prefix = self._conda_info["conda_prefix"]
all_envs = self._all_envs()
for env_name, env_path in all_envs.items():
kspec_base = join(env_path, 'share', 'jupyter', 'kernels')
kspec_glob = glob.glob(join(kspec_base, '*', 'kernel.json'))
kspec_base = join(env_path, "share", "jupyter", "kernels")
kspec_glob = glob.glob(join(kspec_base, "*", "kernel.json"))
for spec_path in kspec_glob:
try:
with open(spec_path, 'rb') as fp:
with open(spec_path, "rb") as fp:
data = fp.read()
spec = json.loads(data.decode('utf-8'))
spec = json.loads(data.decode("utf-8"))
except Exception as err:
self.log.error("[nb_conda_kernels] error loading %s:\n%s", spec_path, err)
continue
Expand All @@ -231,35 +231,35 @@ def _all_specs(self):
# the naming convention is as close as possible to the previous
# versions of this package; particularly so that the tests
# pass without change.
if kernel_name in ('python2', 'python3'):
kernel_name = 'py'
elif kernel_name == 'ir':
kernel_name = 'r'
kernel_prefix = '' if env_name == 'root' else 'env-'
kernel_name = u'conda-{}{}-{}'.format(kernel_prefix, env_name, kernel_name)
if kernel_name in ("python2", "python3"):
kernel_name = "py"
elif kernel_name == "ir":
kernel_name = "r"
kernel_prefix = "" if env_name == "root" else "env-"
kernel_name = "conda-{}{}-{}".format(kernel_prefix, env_name, kernel_name)
# Replace invalid characters with dashes
kernel_name = self.clean_kernel_name(kernel_name)

display_prefix = spec['display_name']
if display_prefix.startswith('Python'):
display_prefix = 'Python'
display_prefix = spec["display_name"]
if display_prefix.startswith("Python"):
display_prefix = "Python"
display_name = self.name_format.format(
display_prefix,
env_name,
conda_kernel=kernel_name,
display_name=spec['display_name'],
display_name=spec["display_name"],
environment=env_name,
kernel=raw_kernel_name,
language=display_prefix,
)
if env_path == sys.prefix:
display_name += ' *'
spec['display_name'] = display_name
display_name += " *"
spec["display_name"] = display_name
if env_path != sys.prefix:
spec['argv'] = RUNNER_COMMAND + [conda_prefix, env_path] + spec['argv']
metadata = spec.get('metadata', {})
metadata.update({'conda_env_name': env_name, 'conda_env_path': env_path})
spec['metadata'] = metadata
spec["argv"] = RUNNER_COMMAND + [conda_prefix, env_path] + spec["argv"]
metadata = spec.get("metadata", {})
metadata.update({"conda_env_name": env_name, "conda_env_path": env_path})
spec["metadata"] = metadata

if self.kernelspec_path is not None:
# Install the kernel spec
Expand All @@ -271,16 +271,16 @@ def _all_specs(self):
kernel_spec = join(destination, "kernel.json")
tmp_spec = spec.copy()
if env_path == sys.prefix: # Add the conda runner to the installed kernel spec
tmp_spec['argv'] = RUNNER_COMMAND + [conda_prefix, env_path] + spec['argv']
tmp_spec["argv"] = RUNNER_COMMAND + [conda_prefix, env_path] + spec["argv"]
with open(kernel_spec, "w") as f:
json.dump(tmp_spec, f)
except OSError as error:
self.log.warning(
u"[nb_conda_kernels] Fail to install kernel '{}'.".format(kernel_dir), exc_info=error
"[nb_conda_kernels] Fail to install kernel '{}'.".format(kernel_dir), exc_info=error
)

# resource_dir is not part of the spec file, so it is added at the latest time
spec['resource_dir'] = abspath(kernel_dir)
spec["resource_dir"] = abspath(kernel_dir)

all_specs[kernel_name] = spec

Expand Down Expand Up @@ -363,7 +363,7 @@ def get_all_specs(self):
for name, resource_dir in self.find_kernel_specs().items():
try:
spec = self.get_kernel_spec(name)
res[name] = {'resource_dir': resource_dir, 'spec': spec.to_dict()}
res[name] = {"resource_dir": resource_dir, "spec": spec.to_dict()}
except NoSuchKernel:
self.log.warning("Error loading kernelspec %r", name, exc_info=True)
return res
Expand Down
42 changes: 21 additions & 21 deletions spine_engine/execution_managers/conda_kernel_spec_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,43 @@ def exec_in_env(conda_prefix, env_path, *command):
# Run the standard conda activation script, and print the
# resulting environment variables to stdout for reading.
is_current_env = env_path == sys.prefix
if sys.platform.startswith('win'):
if sys.platform.startswith("win"):
if is_current_env:
subprocess.Popen(list(command)).wait()
else:
activate = os.path.join(conda_prefix, 'Scripts', 'activate.bat')
activate = os.path.join(conda_prefix, "Scripts", "activate.bat")
ecomm = [
os.environ['COMSPEC'],
'/S',
'/U',
'/C',
'@echo',
'off',
'&&',
'chcp',
'65001',
'&&',
'call',
os.environ["COMSPEC"],
"/S",
"/U",
"/C",
"@echo",
"off",
"&&",
"chcp",
"65001",
"&&",
"call",
activate,
env_path,
'&&',
'@echo',
'CONDA_PREFIX=%CONDA_PREFIX%',
'&&',
"&&",
"@echo",
"CONDA_PREFIX=%CONDA_PREFIX%",
"&&",
] + list(command)
subprocess.Popen(ecomm).wait()
else:
quoted_command = [quote(c) for c in command]
if is_current_env:
os.execvp(quoted_command[0], quoted_command)
else:
activate = os.path.join(conda_prefix, 'bin', 'activate')
activate = os.path.join(conda_prefix, "bin", "activate")
ecomm = ". '{}' '{}' && echo CONDA_PREFIX=$CONDA_PREFIX && exec {}".format(
activate, env_path, ' '.join(quoted_command)
activate, env_path, " ".join(quoted_command)
)
ecomm = ['sh' if 'bsd' in sys.platform else 'bash', '-c', ecomm]
ecomm = ["sh" if "bsd" in sys.platform else "bash", "-c", ecomm]
os.execvp(ecomm[0], ecomm)


if __name__ == '__main__':
if __name__ == "__main__":
exec_in_env(*(sys.argv[1:]))
4 changes: 2 additions & 2 deletions spine_engine/execution_managers/kernel_execution_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ def __init__(
self._msg_head = dict(kernel_name=kernel_name)
self._commands = commands
self._cmd_failed = False
self.std_out = kwargs["stdout"] = open(os.devnull, 'w')
self.std_err = kwargs["stderr"] = open(os.devnull, 'w')
self.std_out = kwargs["stdout"] = open(os.devnull, "w")
self.std_err = kwargs["stderr"] = open(os.devnull, "w")
# Don't show console when frozen
kwargs["creationflags"] = subprocess.CREATE_NO_WINDOW if sys.platform == "win32" else 0
self._kernel_manager = _kernel_manager_factory.new_kernel_manager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def _start_persistent(self):
def _log_stdout(self):
"""Puts stdout from the process into the queue (it will be consumed by issue_command())."""
try:
for line in iter(self._persistent.stdout.readline, b''):
for line in iter(self._persistent.stdout.readline, b""):
data = line.decode("UTF8", "replace").rstrip()
self._msg_queue.put(dict(type="stdout", data=data))
except ValueError:
Expand All @@ -156,7 +156,7 @@ def _log_stdout(self):
def _log_stderr(self):
"""Puts stderr from the process into the queue (it will be consumed by issue_command())."""
try:
for line in iter(self._persistent.stderr.readline, b''):
for line in iter(self._persistent.stderr.readline, b""):
data = line.decode("UTF8", "replace").rstrip()
self._msg_queue.put(dict(type="stderr", data=data))
except ValueError:
Expand Down Expand Up @@ -337,8 +337,8 @@ def _communicate(self, request, *args, receive=True):
"""
if not self.is_persistent_alive():
raise PersistentIsDead()
req_args_sep = '\u001f' # Unit separator
args_sep = '\u0091' # Private Use 1
req_args_sep = "\u001f" # Unit separator
args_sep = "\u0091" # Private Use 1
args = args_sep.join(args)
msg = f"{request}{req_args_sep}{args}"
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
Expand Down
4 changes: 2 additions & 2 deletions spine_engine/execution_managers/process_execution_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def stop_execution(self):
self._process.terminate()

def _log_stdout(self, stdout):
for line in iter(stdout.readline, b''):
for line in iter(stdout.readline, b""):
line = line.decode("UTF8", "replace").strip()
self._logger.msg_proc.emit(line)
self._logger.msg_standard_execution.emit({"type": "stdout", "data": line})
stdout.close()

def _log_stderr(self, stderr):
for line in iter(stderr.readline, b''):
for line in iter(stderr.readline, b""):
line = line.decode("UTF8", "replace").strip()
self._logger.msg_proc_error.emit(line)
self._logger.msg_standard_execution.emit({"type": "stderr", "data": line})
Expand Down
4 changes: 2 additions & 2 deletions spine_engine/execution_managers/spine_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class SpineDBServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
class _RequestHandler(socketserver.BaseRequestHandler):
def handle(self):
data = self.request.recv(1024).decode("UTF8")
req_args_sep = '\u001f' # Unit separator
args_sep = '\u0091' # Private Use 1
req_args_sep = "\u001f" # Unit separator
args_sep = "\u0091" # Private Use 1
request, args = data.split(req_args_sep)
args = args.split(args_sep)
handler = {
Expand Down
8 changes: 4 additions & 4 deletions spine_engine/server/engine_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def __init__(self, protocol, port, sec_model, sec_folder):
if not sec_folder:
raise ValueError("Path to security folder missing")
base_dir = sec_folder
self.keys_dir = os.path.join(base_dir, 'certificates')
self.public_keys_dir = os.path.join(base_dir, 'public_keys')
self.secret_keys_dir = os.path.join(base_dir, 'private_keys')
self.keys_dir = os.path.join(base_dir, "certificates")
self.public_keys_dir = os.path.join(base_dir, "public_keys")
self.secret_keys_dir = os.path.join(base_dir, "private_keys")
if not os.path.exists(self.keys_dir):
raise ValueError(f"Security folder: {self.keys_dir} does not exist")
if not os.path.exists(self.public_keys_dir):
Expand Down Expand Up @@ -338,7 +338,7 @@ def enable_stonehouse_security(self, frontend):
allowed_str = "\n".join(allowed)
print(f"StoneHouse security activated. Allowed endpoints ({len(allowed)}):\n{allowed_str}")
# Tell the authenticator how to handle CURVE requests
auth.configure_curve(domain='*', location=zmq.auth.CURVE_ALLOW_ANY)
auth.configure_curve(domain="*", location=zmq.auth.CURVE_ALLOW_ANY)
server_secret_file = os.path.join(self.secret_keys_dir, "server.key_secret")
server_public, server_secret = zmq.auth.load_certificate(server_secret_file)
frontend.curve_secretkey = server_secret
Expand Down
4 changes: 2 additions & 2 deletions spine_engine/server/persistent_execution_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def run(self):
for msg in pm.issue_command(cmd, add_history=True, catch_exception=False):
json_msg = json.dumps(msg)
self.push_socket.send(json_msg.encode("utf-8")) # This blocks until somebody is pulling (receiving)
self.push_socket.send(b'END')
self.push_socket.send(b"END")
retval_tuple = cmd_type, "ok"
elif cmd_type == "get_completions":
retval = pm.get_completions(cmd)
Expand All @@ -65,7 +65,7 @@ def run(self):
for msg in pm.restart_persistent():
json_msg = json.dumps(msg)
self.push_socket.send(json_msg.encode("utf-8"))
self.push_socket.send(b'END')
self.push_socket.send(b"END")
retval_tuple = cmd_type, "ok"
elif cmd_type == "interrupt_persistent":
pm.interrupt_persistent()
Expand Down
Loading

0 comments on commit 45fc13f

Please sign in to comment.