-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from sbalian/api-key
API key
- Loading branch information
Showing
14 changed files
with
215 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,4 @@ jobs: | |
- name: Test | ||
run: | | ||
source $HOME/.poetry/env | ||
poetry run pytest | ||
QRANDOM_API_KEY=key poetry run pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#!/usr/bin/env python | ||
|
||
import random | ||
|
||
import matplotlib.pyplot as plt | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "quantum-random" | ||
version = "1.1.1" | ||
version = "1.2.2" | ||
description = "Quantum random numbers" | ||
authors = ["Seto Balian <[email protected]>"] | ||
packages = [{ include = "qrandom" }] | ||
|
@@ -28,6 +28,7 @@ requests = "^2.25.1" | |
|
||
numpy = { version = "^1.17", optional = true, python = ">=3.7,<3.10" } | ||
randomgen = { version = "^1.21.2", optional = true, python = ">=3.7,<3.10" } | ||
xdg = "^5.1.1" | ||
|
||
[tool.poetry.dev-dependencies] | ||
isort = "^5.7.0" | ||
|
@@ -44,6 +45,9 @@ scipy = "<1.8" | |
[tool.poetry.extras] | ||
numpy = ["numpy", "randomgen"] | ||
|
||
[tool.poetry.scripts] | ||
qrandom-init = 'qrandom._cli:init' | ||
|
||
[tool.isort] | ||
line_length = 79 | ||
known_first_party = "qrandom" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import configparser | ||
import os | ||
import pathlib | ||
import sys | ||
|
||
import xdg | ||
|
||
DEFAULT_DIR = xdg.xdg_config_home() / "qrandom" | ||
|
||
|
||
def init(): | ||
print("This utility will help you set the API key for qrandom.") | ||
print("You can get a key from https://quantumnumbers.anu.edu.au/.") | ||
print("Where would you like to store the key?") | ||
print("[Type in a directory path and press enter or just press enter to ") | ||
print(f"use the default path ({DEFAULT_DIR})]:") | ||
user_input_dir = input().strip() | ||
if user_input_dir in ["", DEFAULT_DIR]: | ||
config_dir = DEFAULT_DIR | ||
os.makedirs(config_dir, exist_ok=True) | ||
config_path = config_dir / "qrandom.ini" | ||
else: | ||
config_dir = pathlib.Path(user_input_dir).expanduser().resolve() | ||
if config_dir.exists() and config_dir.is_file(): | ||
print(f"{config_dir} must be a directory.") | ||
sys.exit(1) | ||
os.makedirs(config_dir, exist_ok=True) | ||
config_path = config_dir / "qrandom.ini" | ||
if config_path.exists(): | ||
print(f"{config_path} exists. Would you like to overwrite it? [Y/n]:") | ||
if input().strip() != "Y": | ||
print("Aborted.") | ||
sys.exit(1) | ||
config = configparser.ConfigParser() | ||
config.add_section("default") | ||
print("Enter or paste your API key:") | ||
api_key = input().strip() | ||
config["default"]["key"] = api_key | ||
with open(config_path, "w") as f: | ||
config.write(f) | ||
print(f"Stored API key in {config_path}.") | ||
if config_dir != DEFAULT_DIR: | ||
print( | ||
"Since you did not write to the default path, do not forget to " | ||
f"set QRANDOM_CONFIG_DIR to {config_dir} when using qrandom." | ||
) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import configparser | ||
import os | ||
import pathlib | ||
|
||
import xdg | ||
|
||
INIT_MSG = "initialise qrandom.ini by running qrandom-init" | ||
|
||
|
||
class ApiKeyNotFoundInEnvError(Exception): | ||
pass | ||
|
||
|
||
class CustomConfigDirNotFoundError(Exception): | ||
pass | ||
|
||
|
||
def get_from_env() -> str: | ||
try: | ||
return os.environ["QRANDOM_API_KEY"] | ||
except KeyError: | ||
raise ApiKeyNotFoundInEnvError | ||
|
||
|
||
def get_custom_dir() -> pathlib.Path: | ||
try: | ||
config_dir = ( | ||
pathlib.Path(os.environ["QRANDOM_CONFIG_DIR"]) | ||
.expanduser() | ||
.resolve() | ||
) | ||
if not config_dir.exists(): | ||
raise IOError( | ||
f"{config_dir} does not exist. {INIT_MSG.capitalize()}." | ||
) | ||
if config_dir.is_file(): | ||
raise IOError( | ||
f"{config_dir} must be a directory. {INIT_MSG.capitalize()}." | ||
) | ||
return config_dir | ||
except KeyError: | ||
raise CustomConfigDirNotFoundError | ||
|
||
|
||
def get_from_file(config_dir: pathlib.Path) -> str: | ||
config_path = config_dir / "qrandom.ini" | ||
if not config_path.exists(): | ||
raise FileNotFoundError( | ||
f"{config_path} does not exist. {INIT_MSG.capitalize()}." | ||
) | ||
if config_path.is_dir(): | ||
raise IsADirectoryError( | ||
f"{config_path} cannot be a directory.{INIT_MSG.capitalize()}." | ||
) | ||
config = configparser.ConfigParser() | ||
config.read(config_path) | ||
return config["default"]["key"] | ||
|
||
|
||
def get_api_key() -> str: | ||
try: | ||
return get_from_env() | ||
except ApiKeyNotFoundInEnvError: | ||
try: | ||
config_dir = get_custom_dir() | ||
except CustomConfigDirNotFoundError: | ||
config_dir = xdg.xdg_config_home() / "qrandom" | ||
config_path = config_dir / "qrandom.ini" | ||
if not config_path.exists(): | ||
raise FileNotFoundError( | ||
f"{config_path} does not exist. {INIT_MSG.capitalize()}." | ||
) | ||
if config_path.is_dir(): | ||
raise IsADirectoryError( | ||
f"{config_path} cannot be a directory.{INIT_MSG.capitalize()}." | ||
) | ||
config = configparser.ConfigParser() | ||
config.read(config_path) | ||
return config["default"]["key"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.