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

Add ruff linting #105

Merged
merged 12 commits into from
Oct 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Linting

on:
pull_request:
push:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff pre-commit
- name: Run pre-commit on all files
env:
RUFF_OUTPUT_FORMAT: github
run: |
pre-commit run --show-diff-on-failure --color=always --all-files
2 changes: 1 addition & 1 deletion .github/workflows/pypi_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
run: |
python -m build --sdist --outdir dist/ .
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
uses: pypa/gh-action-pypi-publish@release/v1
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks.git
rev: v4.6.0
hooks:
- id: no-commit-to-branch
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-ast
- id: check-case-conflict
- id: check-json
- id: check-merge-conflict
- id: check-symlinks
- id: check-xml
- id: check-yaml
exclude: "^(python/src/badger/_version.py)$"

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ or `pip`
pip install badger-opt
```

Currently Badger only officially support Linux. Badger on MacOS and Windows could be potentially unstable.
Currently, Badger only officially supports Linux. Badger on MacOS and
Windows could be potentially unstable.

## Run an optimization

Expand Down Expand Up @@ -99,6 +100,40 @@ BibTex entry:
}
```

Developers
==========

Clone this repository:
```shell
git clone https://github.com/xopt-org/badger.git
```

Create a fresh Python environment, for example using conda:
```shell
conda env create -n badger-env python=3.12
```

Install Badger as editable:
```shell
conda activate badger-env
pip install -e ".[dev]"
```

You'll also need to install `ruff` and `pre-commit` to be able to
pass the linting and formatting checks:
```
pip install ruff pre-commit
```

Then install the pre-commit hooks:
```
pre-commit install
```

You can also do a run at any time to check your progress:
```
pre-commit run --all-files
```
## Issues or questions?

Please check out current Badger issues [here](https://github.com/xopt-org/Badger/issues) before [opening a new one](https://github.com/xopt-org/Badger/issues/new/). Alternatively, you are welcome to [shoot us an email](mailto:[email protected]), or join our [Slack channel](https://join.slack.com/share/enQtNjI3NjY0NDk0MzUxMC0zNThjYTdmNmZlOTA3MzM2NjFjZTVmNDY5MDk4NmQ2NmQxMjE1NjBjN2VhNzYxNjBjYjgxYzMwY2ExZTIxYzkw).
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ where = ["src"]
include = [ "badger", ]
namespaces = false

[tool.ruff]
# select = []
# ignore = []
[tool.ruff.lint]
extend-select = ["TID252"] # Defaults + check imports
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"]
2 changes: 1 addition & 1 deletion src/badger/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
try:
from ._version import __version__
from badger._version import __version__
except ImportError:
__version__ = "0.0.0"
161 changes: 93 additions & 68 deletions src/badger/__main__.py
Original file line number Diff line number Diff line change
@@ -1,108 +1,133 @@
import argparse

from .log import config_log
config_log() # has to happen here to make sure the config taking effect
from .actions import show_info
from .actions.doctor import self_check
from .actions.routine import show_routine
from .actions.generator import show_generator
from .actions.env import show_env
from .actions.install import plugin_install
from .actions.uninstall import plugin_remove
from .actions.intf import show_intf
from .actions.run import run_routine
from .actions.config import config_settings
from badger.actions import show_info
from badger.actions.doctor import self_check
from badger.actions.routine import show_routine
from badger.actions.generator import show_generator
from badger.actions.env import show_env
from badger.actions.install import plugin_install
from badger.actions.uninstall import plugin_remove
from badger.actions.intf import show_intf
from badger.actions.run import run_routine
from badger.actions.config import config_settings
from badger.log import config_log

config_log()


def main():
# Create the top-level parser
parser = argparse.ArgumentParser(description='Badger the optimizer')
parser.add_argument('-g', '--gui', action='store_true',
help='launch the GUI')
parser.add_argument('-ga', '--gui-acr', action='store_true',
help='launch the GUI for ACR')
parser.add_argument('-l', '--log', choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'],
default='WARNING', const='WARNING', nargs='?',
help='change the log level')
parser = argparse.ArgumentParser(description="Badger the optimizer")
parser.add_argument("-g", "--gui", action="store_true", help="launch the GUI")
parser.add_argument(
"-ga", "--gui-acr", action="store_true", help="launch the GUI for ACR"
)
parser.add_argument(
"-l",
"--log",
choices=["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"],
default="WARNING",
const="WARNING",
nargs="?",
help="change the log level",
)
parser.set_defaults(func=show_info)
subparsers = parser.add_subparsers(help='Badger commands help')
subparsers = parser.add_subparsers(help="Badger commands help")

# Parser for the 'doctor' command
parser_doctor = subparsers.add_parser(
'doctor', help='Badger status self-check')
parser_doctor.add_argument('-r', '--reset', action='store_true',
help='reset Badger to factory settings')
parser_doctor = subparsers.add_parser("doctor", help="Badger status self-check")
parser_doctor.add_argument(
"-r", "--reset", action="store_true", help="reset Badger to factory settings"
)
parser_doctor.set_defaults(func=self_check)

# Parser for the 'routine' command
parser_routine = subparsers.add_parser('routine', help='Badger routines')
parser_routine.add_argument('routine_name',
nargs='?', type=str, default=None)
parser_routine.add_argument('-r', '--run', action='store_true',
help='run the routine')
parser_routine.add_argument('-y', '--yes', action='store_true',
help='run the routine without confirmation')
parser_routine.add_argument('-v', '--verbose', type=int, choices=[0, 1, 2],
default=2, const=2, nargs='?',
help='verbose level of optimization progress')
parser_routine = subparsers.add_parser("routine", help="Badger routines")
parser_routine.add_argument("routine_name", nargs="?", type=str, default=None)
parser_routine.add_argument(
"-r", "--run", action="store_true", help="run the routine"
)
parser_routine.add_argument(
"-y", "--yes", action="store_true", help="run the routine without confirmation"
)
parser_routine.add_argument(
"-v",
"--verbose",
type=int,
choices=[0, 1, 2],
default=2,
const=2,
nargs="?",
help="verbose level of optimization progress",
)
parser_routine.set_defaults(func=show_routine)

# Parser for the 'generator' command
parser_generator = subparsers.add_parser('generator', help='Badger generators')
parser_generator.add_argument('generator_name', nargs='?', type=str, default=None)
parser_generator = subparsers.add_parser("generator", help="Badger generators")
parser_generator.add_argument("generator_name", nargs="?", type=str, default=None)
parser_generator.set_defaults(func=show_generator)

# Parser for the 'intf' command
parser_intf = subparsers.add_parser('intf', help='Badger interfaces')
parser_intf.add_argument('intf_name', nargs='?', type=str, default=None)
parser_intf = subparsers.add_parser("intf", help="Badger interfaces")
parser_intf.add_argument("intf_name", nargs="?", type=str, default=None)
parser_intf.set_defaults(func=show_intf)

# Parser for the 'env' command
parser_env = subparsers.add_parser('env', help='Badger environments')
parser_env.add_argument('env_name', nargs='?', type=str, default=None)
parser_env = subparsers.add_parser("env", help="Badger environments")
parser_env.add_argument("env_name", nargs="?", type=str, default=None)
parser_env.set_defaults(func=show_env)

# Parser for the 'install' command
parser_inst = subparsers.add_parser('install', help='Badger install plugin')
parser_inst.add_argument('plugin_type', nargs='?', type=str, default=None)
parser_inst.add_argument('plugin_specific', nargs='?', type=str, default=None)
parser_inst = subparsers.add_parser("install", help="Badger install plugin")
parser_inst.add_argument("plugin_type", nargs="?", type=str, default=None)
parser_inst.add_argument("plugin_specific", nargs="?", type=str, default=None)
parser_inst.set_defaults(func=plugin_install)

# Parser for the 'remove' command
parser_remove = subparsers.add_parser('remove', help='Badger remove plugin')
parser_remove.add_argument('plugin_type', nargs='?', type=str, default=None)
parser_remove.add_argument('plugin_specific', nargs='?', type=str, default=None)
parser_remove = subparsers.add_parser("remove", help="Badger remove plugin")
parser_remove.add_argument("plugin_type", nargs="?", type=str, default=None)
parser_remove.add_argument("plugin_specific", nargs="?", type=str, default=None)
parser_remove.set_defaults(func=plugin_remove)

# Parser for the 'run' command
parser_run = subparsers.add_parser('run', help='run routines')
parser_run.add_argument('-a', '--generator', required=True,
help='generator to use')
parser_run.add_argument('-ap', '--generator_params',
help='parameters for the generator')
parser_run.add_argument('-e', '--env', required=True,
help='environment to use')
parser_run.add_argument('-ep', '--env_params',
help='parameters for the environment')
parser_run.add_argument('-c', '--config', required=True,
help='config for the routine')
parser_run.add_argument('-s', '--save', nargs='?', const='',
help='the routine name to be saved')
parser_run.add_argument('-y', '--yes', action='store_true',
help='run the routine without confirmation')
parser_run.add_argument('-v', '--verbose', type=int, choices=[0, 1, 2],
default=2, const=2, nargs='?',
help='verbose level of optimization progress')
parser_run = subparsers.add_parser("run", help="run routines")
parser_run.add_argument("-a", "--generator", required=True, help="generator to use")
parser_run.add_argument(
"-ap", "--generator_params", help="parameters for the generator"
)
parser_run.add_argument("-e", "--env", required=True, help="environment to use")
parser_run.add_argument(
"-ep", "--env_params", help="parameters for the environment"
)
parser_run.add_argument(
"-c", "--config", required=True, help="config for the routine"
)
parser_run.add_argument(
"-s", "--save", nargs="?", const="", help="the routine name to be saved"
)
parser_run.add_argument(
"-y", "--yes", action="store_true", help="run the routine without confirmation"
)
parser_run.add_argument(
"-v",
"--verbose",
type=int,
choices=[0, 1, 2],
default=2,
const=2,
nargs="?",
help="verbose level of optimization progress",
)
parser_run.set_defaults(func=run_routine)

# Parser for the 'config' command
parser_config = subparsers.add_parser('config', help='Badger configurations')
parser_config.add_argument('key', nargs='?', type=str, default=None)
parser_config = subparsers.add_parser("config", help="Badger configurations")
parser_config.add_argument("key", nargs="?", type=str, default=None)
parser_config.set_defaults(func=config_settings)

args = parser.parse_args()
args.func(args)


if __name__ == '__main__':
if __name__ == "__main__":
main()
24 changes: 12 additions & 12 deletions src/badger/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from importlib import metadata
from .doctor import check_n_config_paths
from ..utils import yprint
from ..log import set_log_level
from badger.actions.doctor import check_n_config_paths
from badger.utils import yprint
from badger.log import set_log_level


def show_info(args):
Expand All @@ -10,15 +10,15 @@ def show_info(args):

if args.gui:
if check_n_config_paths():
from ..gui.default import launch_gui
from badger.gui.default import launch_gui

launch_gui()

return

if args.gui_acr:
if check_n_config_paths():
from ..gui.default import launch_gui
from badger.gui.default import launch_gui

launch_gui()

Expand All @@ -27,7 +27,7 @@ def show_info(args):
if not check_n_config_paths():
return

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

config_singleton = init_settings()
BADGER_PLUGIN_ROOT = config_singleton.read_value("BADGER_PLUGIN_ROOT")
Expand All @@ -36,12 +36,12 @@ def show_info(args):
BADGER_ARCHIVE_ROOT = config_singleton.read_value("BADGER_ARCHIVE_ROOT")

info = {
'name': 'Badger the optimizer',
'version': metadata.version('badger-opt'),
'plugin root': BADGER_PLUGIN_ROOT,
'database root': BADGER_DB_ROOT,
'logbook root': BADGER_LOGBOOK_ROOT,
'archive root': BADGER_ARCHIVE_ROOT,
"name": "Badger the optimizer",
"version": metadata.version("badger-opt"),
"plugin root": BADGER_PLUGIN_ROOT,
"database root": BADGER_DB_ROOT,
"logbook root": BADGER_LOGBOOK_ROOT,
"archive root": BADGER_ARCHIVE_ROOT,
# 'plugin installation url': read_value('BADGER_PLUGINS_URL')
}

Expand Down
Loading