Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using __file__ outside tests #4797

Merged
merged 7 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions qcodes/_version.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
def _get_version() -> str:
from importlib.resources import files

Check notice on line 2 in qcodes/_version.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

qcodes/_version.py#L2

Import outside toplevel (importlib.resources.files)
from pathlib import Path

import versioningit

import qcodes
# https://github.com/python/mypy/issues/4182
root_module = __loader__.name.split(".")[0] # type: ignore[name-defined]

qcodes_path = Path(qcodes.__file__).parent
return versioningit.get_version(project_dir=qcodes_path.parent)
module_path = files(root_module)
if isinstance(module_path, Path):
return versioningit.get_version(project_dir=Path(module_path).parent)
else:
return "0.0"

Check warning on line 14 in qcodes/_version.py

View check run for this annotation

Codecov / codecov/patch

qcodes/_version.py#L14

Added line #L14 was not covered by tests


__version__ = _get_version()
8 changes: 6 additions & 2 deletions qcodes/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import os
from collections.abc import Mapping
from importlib.resources import files
from os.path import expanduser
from pathlib import Path
from typing import Any
Expand All @@ -25,6 +26,9 @@
"required": []
}

# https://github.com/python/mypy/issues/4182
_PARENT_MODULE = ".".join(__loader__.name.split(".")[:-1]) # type: ignore[name-defined]


class Config:
"""
Expand All @@ -40,14 +44,14 @@ class Config:
schema_file_name = "qcodesrc_schema.json"
"""Name of schema file"""
# get abs path of packge config file
default_file_name = str(Path(__file__).parent / config_file_name)
default_file_name = str(files(_PARENT_MODULE) / config_file_name)
"""Filename of default config"""
current_config_path = default_file_name
"""Path of the last loaded config file"""
_loaded_config_files = [default_file_name]

# get abs path of schema file
schema_default_file_name = str(Path(__file__).parent / schema_file_name)
schema_default_file_name = str(files(_PARENT_MODULE) / schema_file_name)
"""Filename of default schema"""

# home dir, os independent
Expand Down
22 changes: 14 additions & 8 deletions qcodes/monitor/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from collections import defaultdict
from collections.abc import Awaitable, Sequence
from contextlib import suppress
from importlib.resources import as_file, files
from threading import Event, Thread
from typing import Any, Callable

Expand Down Expand Up @@ -266,15 +267,20 @@

# If this file is run, create a simple webserver that serves a simple
# website that can be used to view monitored parameters.
static_dir = os.path.join(os.path.dirname(__file__), "dist")
os.chdir(static_dir)
# # https://github.com/python/mypy/issues/4182
parent_module = ".".join(__loader__.name.split(".")[:-1]) # type: ignore[name-defined]

Check notice on line 271 in qcodes/monitor/monitor.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

qcodes/monitor/monitor.py#L271

Line too long (91/88)

Check warning on line 271 in qcodes/monitor/monitor.py

View check run for this annotation

Codecov / codecov/patch

qcodes/monitor/monitor.py#L271

Added line #L271 was not covered by tests

static_dir = files(parent_module).joinpath("dist")

Check warning on line 273 in qcodes/monitor/monitor.py

View check run for this annotation

Codecov / codecov/patch

qcodes/monitor/monitor.py#L273

Added line #L273 was not covered by tests
try:
log.info("Starting HTTP Server at http://localhost:%i", SERVER_PORT)
with socketserver.TCPServer(("", SERVER_PORT),
http.server.SimpleHTTPRequestHandler) as httpd:
log.debug("serving directory %s", static_dir)
webbrowser.open(f"http://localhost:{SERVER_PORT}")
httpd.serve_forever()
with as_file(static_dir) as extracted_dir:
os.chdir(extracted_dir)
log.info("Starting HTTP Server at http://localhost:%i", SERVER_PORT)
with socketserver.TCPServer(

Check warning on line 278 in qcodes/monitor/monitor.py

View check run for this annotation

Codecov / codecov/patch

qcodes/monitor/monitor.py#L275-L278

Added lines #L275 - L278 were not covered by tests
("", SERVER_PORT), http.server.SimpleHTTPRequestHandler
) as httpd:
log.debug("serving directory %s", static_dir)
webbrowser.open(f"http://localhost:{SERVER_PORT}")
httpd.serve_forever()

Check warning on line 283 in qcodes/monitor/monitor.py

View check run for this annotation

Codecov / codecov/patch

qcodes/monitor/monitor.py#L281-L283

Added lines #L281 - L283 were not covered by tests
except KeyboardInterrupt:
log.info("Shutting Down HTTP Server")

Expand Down