Skip to content

Commit

Permalink
resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
pluflou committed Oct 29, 2024
2 parents ce8b344 + 6514fec commit 3ddf860
Show file tree
Hide file tree
Showing 37 changed files with 1,189 additions and 478 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ __pycache__
/examples/*.yaml
*.pickle
*.db
config.yaml
.vscode
src/badger/_version.py
.coverage*
Empty file added __init__.py
Empty file.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespaces = false
lint.ignore = ["E722"] # Until bare except blocks get fixed

[tool.pytest.ini_options]
addopts = "--cov=src/badger/"
addopts = "--cov=badger/"
log_cli_level = "info"
log_level = "debug"
testpaths = ["src/badger/tests"]
6 changes: 1 addition & 5 deletions src/badger/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import argparse

from .settings import init_settings
from .actions import show_info
from .actions.doctor import self_check
from .actions.routine import show_routine
Expand All @@ -13,13 +12,10 @@
from .actions.config import config_settings
from .log import config_log

config_log() # Has to happen here to make sure the config taking effect
config_log()


def main():
# Initialize the Badger settings
init_settings()

# Create the top-level parser
parser = argparse.ArgumentParser(description="Badger the optimizer")
parser.add_argument("-g", "--gui", action="store_true", help="launch the GUI")
Expand Down
11 changes: 6 additions & 5 deletions src/badger/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ def show_info(args):
if not check_n_config_paths():
return

from ..settings import read_value
from ..settings import init_settings

BADGER_PLUGIN_ROOT = read_value("BADGER_PLUGIN_ROOT")
BADGER_DB_ROOT = read_value("BADGER_DB_ROOT")
BADGER_LOGBOOK_ROOT = read_value("BADGER_LOGBOOK_ROOT")
BADGER_ARCHIVE_ROOT = read_value("BADGER_ARCHIVE_ROOT")
config_singleton = init_settings()
BADGER_PLUGIN_ROOT = config_singleton.read_value("BADGER_PLUGIN_ROOT")
BADGER_DB_ROOT = config_singleton.read_value("BADGER_DB_ROOT")
BADGER_LOGBOOK_ROOT = config_singleton.read_value("BADGER_LOGBOOK_ROOT")
BADGER_ARCHIVE_ROOT = config_singleton.read_value("BADGER_ARCHIVE_ROOT")

info = {
"name": "Badger the optimizer",
Expand Down
42 changes: 23 additions & 19 deletions src/badger/actions/config.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
from ..settings import init_settings
import os
import logging

from badger.settings import (
list_settings,
read_value,
write_value,
BADGER_PATH_DICT,
BADGER_CORE_DICT,
)
from badger.utils import yprint, convert_str_to_value

logger = logging.getLogger(__name__)


def config_settings(args):
config_singleton = init_settings()
key = args.key

if key is None:
yprint(list_settings())
yprint(config_singleton.list_settings())
return

try:
Expand All @@ -37,8 +32,15 @@ def config_settings(args):


def _config_path_var(var_name):
display_name = BADGER_PATH_DICT[var_name]["display name"]
desc = BADGER_PATH_DICT[var_name]["description"]
config_singleton = init_settings()

is_path = config_singleton.read_is_path(var_name)

if not is_path:
raise KeyError

display_name = config_singleton.read_display_name(var_name)
desc = config_singleton.read_description(var_name)

print(f"=== Configure {display_name} ===")
print(f"*** {desc} ***\n")
Expand All @@ -50,7 +52,7 @@ def _config_path_var(var_name):
break
if res == "R":
_res = input(
f"The current value {read_value(var_name)} will be reset, proceed (y/[n])? "
f"The current value {config_singleton.read_value(var_name)} will be reset, proceed (y/[n])? "
)
if _res == "y":
break
Expand Down Expand Up @@ -83,17 +85,19 @@ def _config_path_var(var_name):
print(f"Invalid choice: {_res}")

if res == "R":
write_value(var_name, None)
config_singleton.write_value(var_name, None)
print(f"You reset the Badger {display_name} folder setting")
elif res != "S":
write_value(var_name, res)
config_singleton.write_value(var_name, res)
print(f"You set the Badger {display_name} folder to {res}")


def _config_core_var(var_name):
display_name = BADGER_CORE_DICT[var_name]["display name"]
desc = BADGER_CORE_DICT[var_name]["description"]
default = BADGER_CORE_DICT[var_name]["default value"]
config_singleton = init_settings()

display_name = config_singleton.get_section("core")[var_name]["display name"]
desc = config_singleton.get_section("core")[var_name]["description"]
default = config_singleton.get_section("core")[var_name]["default value"]

print(f"=== Configure {display_name} ===")
print(f"*** {desc} ***\n")
Expand All @@ -105,7 +109,7 @@ def _config_core_var(var_name):
break
if res == "R":
_res = input(
f"The current value {read_value(var_name)} will be reset to {default}, proceed (y/[n])? "
f"The current value {config_singleton.read_value(var_name)} will be reset to {default}, proceed (y/[n])? "
)
if _res == "y":
break
Expand All @@ -118,8 +122,8 @@ def _config_core_var(var_name):
break

if res == "R":
write_value(var_name, default)
config_singleton.write_value(var_name, default)
print(f"You reset the {display_name} setting")
elif res != "S":
write_value(var_name, convert_str_to_value(res))
config_singleton.write_value(var_name, convert_str_to_value(res))
print(f"You set {display_name} to {res}")
17 changes: 8 additions & 9 deletions src/badger/actions/doctor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from ..settings import read_value, BADGER_PATH_DICT, reset_settings, mock_settings
from ..settings import init_settings, mock_settings
from .config import _config_path_var


def self_check(args):
config = init_settings()
# Reset Badger
if args.reset:
while True:
Expand All @@ -20,7 +21,7 @@ def self_check(args):
else:
print(f"Invalid choice: {_res}")

reset_settings()
config.reset_settings()
print("Badger has been reset to the factory settings.")
return

Expand All @@ -30,22 +31,20 @@ def self_check(args):


def check_n_config_paths():
good = True
config = init_settings()

good = True
issue_list = []

for pname in BADGER_PATH_DICT.keys():
if not read_value(pname):
for pname in config._config.dict(by_alias=True):
if config.read_value(pname) is None:
good = False
# dname = BADGER_PATH_DICT[pname]['display name']
# print(f'\n{dname} needs to be configured!')

issue_list.append(pname)

if not good:
# Initial setup
init = True
while False:
while True:
_res = input(
"If this is your first time launching Badger, you should initialize it now.\n"
"Proceed ([y]/n)? "
Expand Down
9 changes: 5 additions & 4 deletions src/badger/actions/install.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging
import requests
import tarfile
import os
import shutil
import os
from os.path import exists

import requests
import yaml
from tqdm.auto import tqdm

from badger.settings import read_value
from ..settings import init_settings

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -49,7 +49,8 @@ def plugin_install(args):
)
return

plugins_url = read_value("BADGER_PLUGINS_URL")
config = init_settings()
plugins_url = config.read_value("BADGER_PLUGINS_URL")

if args.plugin_specific is None:
if args.plugin_type == "local":
Expand Down
8 changes: 4 additions & 4 deletions src/badger/actions/routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ def show_routine(args):
return

# List routines
if args.routine_name is None:
routines = list_routine()[0]
if args.routine_id is None:
routines = list_routine()[1]
if routines:
yprint(routines)
else:
print("No routine has been saved yet")
return

try:
routine, _ = load_routine(args.routine_name)
routine, _ = load_routine(args.routine_id)
if routine is None:
print(f"Routine {args.routine_name} not found")
print(f"Routine {args.routine_id} not found")
return
except Exception as e:
print(e)
Expand Down
20 changes: 11 additions & 9 deletions src/badger/actions/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import sys
import time
import signal

from pandas import DataFrame
from badger.utils import curr_ts
from badger.core import run_routine as run
from badger.routine import Routine
from badger.settings import read_value
from badger.errors import BadgerRunTerminatedError

from ..utils import curr_ts
from ..core import run_routine as run
from ..routine import Routine
from ..settings import init_settings
from ..errors import BadgerRunTerminated

logger = logging.getLogger(__name__)

Expand All @@ -34,7 +36,7 @@ def handler(*args):
print("") # start a new line
if flush_prompt: # erase the last prompt
sys.stdout.write("\033[F")
raise BadgerRunTerminatedError
raise BadgerRunTerminated
storage["paused"] = True

signal.signal(signal.SIGINT, handler)
Expand Down Expand Up @@ -64,9 +66,9 @@ def after_evaluate(data: DataFrame):
# stas: list
ts = curr_ts()
ts_float = ts.timestamp()

config = init_settings()
# Try dump the run data and interface log to the disk
dump_period = float(read_value("BADGER_DATA_DUMP_PERIOD"))
dump_period = float(config.read_value("BADGER_DATA_DUMP_PERIOD"))
ts_last_dump = storage["ts_last_dump"]
if (ts_last_dump is None) or (ts_float - ts_last_dump > dump_period):
storage["ts_last_dump"] = ts_float
Expand Down Expand Up @@ -95,7 +97,7 @@ def states_ready(states):
evaluate_callback=after_evaluate,
states_callback=states_ready,
)
except BadgerRunTerminatedError as e:
except BadgerRunTerminated as e:
logger.info(e)
except Exception as e:
logger.error(e)
Expand Down
5 changes: 3 additions & 2 deletions src/badger/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

from .db import save_run, remove_run_by_filename
from .utils import ts_float_to_str
from .settings import read_value
from .settings import init_settings
from .routine import Routine
from .errors import BadgerConfigError

logger = logging.getLogger(__name__)


# Check badger optimization run archive root
BADGER_ARCHIVE_ROOT = read_value("BADGER_ARCHIVE_ROOT")
config_singleton = init_settings()
BADGER_ARCHIVE_ROOT = config_singleton.read_value("BADGER_ARCHIVE_ROOT")
if BADGER_ARCHIVE_ROOT is None:
raise BadgerConfigError("Please set the BADGER_ARCHIVE_ROOT env var!")
elif not os.path.exists(BADGER_ARCHIVE_ROOT):
Expand Down
6 changes: 3 additions & 3 deletions src/badger/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pandas import concat, DataFrame

from badger.errors import BadgerRunTerminatedError
from badger.errors import BadgerRunTerminated
from badger.logger import _get_default_logger
from badger.logger.event import Events
from badger.routine import Routine
Expand All @@ -14,7 +14,7 @@ def check_run_status(active_callback):
while True:
status = active_callback()
if status == 2:
raise BadgerRunTerminatedError
raise BadgerRunTerminated
elif status == 1:
time.sleep(0)
continue
Expand Down Expand Up @@ -143,7 +143,7 @@ def run_routine(
while True:
status = active_callback()
if status == 2:
raise BadgerRunTerminatedError
raise BadgerRunTerminated
elif status == 1:
time.sleep(0)
continue
Expand Down
Loading

0 comments on commit 3ddf860

Please sign in to comment.