Skip to content

Commit

Permalink
Merge pull request #4797 from jenshnielsen/use_imporlib_resources
Browse files Browse the repository at this point in the history
Stop using __file__ outside tests
  • Loading branch information
jenshnielsen committed Sep 24, 2023
2 parents 073dda9 + 82f57cf commit ee569af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
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
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"


__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 @@ def main() -> None:

# 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]

static_dir = files(parent_module).joinpath("dist")
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(
("", SERVER_PORT), http.server.SimpleHTTPRequestHandler
) as httpd:
log.debug("serving directory %s", static_dir)
webbrowser.open(f"http://localhost:{SERVER_PORT}")
httpd.serve_forever()
except KeyboardInterrupt:
log.info("Shutting Down HTTP Server")

Expand Down

0 comments on commit ee569af

Please sign in to comment.