Skip to content

Commit

Permalink
Added black (#31)
Browse files Browse the repository at this point in the history
* update dev reqs

* added black

* added black to travis

* update docs

* added pre-commit config
  • Loading branch information
cehbrecht authored Oct 21, 2020
1 parent 6c1218f commit a1e525f
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 67 deletions.
58 changes: 58 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
default_language_version:
python: python3.8

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
hooks:
- id: trailing-whitespace
language_version: python3
- id: end-of-file-fixer
language_version: python3
- id: check-yaml
language_version: python3
- id: debug-statements
language_version: python3
- repo: https://github.com/ambv/black
rev: 20.8b1
hooks:
- id: black
language_version: python3
args: ["--target-version", "py38"]
- repo: https://github.com/pycqa/flake8
rev: 3.8.3
hooks:
- id: flake8
language_version: python3
args: ['--config=setup.cfg']
#- repo: https://github.com/pre-commit/mirrors-autopep8
# rev: v1.4.4
# hooks:
# - id: autopep8
# args: ['--global-config=setup.cfg','--in-place']
# - repo: https://github.com/timothycrosley/isort
# rev: 5.0.7
# hooks:
# - id: isort
# language_version: python3
# args: ['--profile', 'black']
#- repo: https://github.com/pycqa/pydocstyle
# rev: 5.0.2
# hooks:
# - id: pydocstyle
# args: ["--conventions=numpy"]
# - repo: https://github.com/asottile/pyupgrade
# rev: v2.4.1
# hooks:
# - id: pyupgrade
# language_version: python3
# - repo: meta
# hooks:
# - id: check-hooks-apply
# - id: check-useless-excludes
# - repo: https://github.com/kynan/nbstripout
# rev: 0.3.7
# hooks:
# - id: nbstripout
# language_version: python3
# files: ".ipynb"
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ matrix:
env:
- CONDA_FN="Miniconda3-latest-Linux-x86_64.sh"
- PEP8=true
- BLACK=true
- PYTHON_DESIRED=3.8

sudo: false
Expand All @@ -47,10 +48,11 @@ install:
- conda env update -f environment.yml
- source activate rooki
# Packages for testing
- pip install pytest nbval flake8
- pip install pytest nbval flake8 black
# Install package
- python setup.py install
script:
- make test
- make test-nb
- if [[ $PEP8 == true ]]; then flake8 rooki tests; fi
- if [[ $BLACK == true ]]; then black --check --target-version py38 rooki tests; fi
18 changes: 16 additions & 2 deletions docs/source/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ Install additional dependencies:
$ pip install -r requirements_dev.txt
When you're done making changes, check that your changes pass `flake8` and the tests:
When you're done making changes, check that your changes pass `black`, `flake8` and the tests:

.. code-block:: console
$ flake8 rooki
$ black rooki tests
$ flake8 rooki tests
$ pytest tests
Or use the Makefile:
Expand All @@ -34,6 +35,19 @@ Or use the Makefile:
$ make lint
$ make test
Add pre-commit hooks
--------------------

Before committing your changes, we ask that you install `pre-commit` in your environment.
`Pre-commit` runs git hooks that ensure that your code resembles that of the project
and catches and corrects any small errors or inconsistencies when you `git commit`:

.. code-block:: console
$ conda install -c conda-forge pre_commit
$ pre-commit install
Write Documentation
-------------------

Expand Down
5 changes: 3 additions & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pip>=18.1
pip>=20.0
bumpversion>=0.5.3
wheel>=0.32.1
watchdog>=0.9.0
Expand All @@ -8,7 +8,8 @@ coverage>=4.5.1
Sphinx>=1.8.1
nbsphinx>=0.7.0
twine>=1.12.1

pre-commit>=2.7.1
black>=20.0
pytest>=3.8.2
pytest-runner>=4.2
nbval>=0.9.6
2 changes: 1 addition & 1 deletion rooki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@


__all__ = [
'rooki',
"rooki",
]
6 changes: 3 additions & 3 deletions rooki/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

class Rooki(WPSClient):
def __init__(self, url=None, mode=None, verify=None):
self._url = url or config.get_config_value('service', 'url')
self._mode = mode or config.get_config_value('service', 'mode')
self._url = url or config.get_config_value("service", "url")
self._mode = mode or config.get_config_value("service", "mode")
if verify is None:
self._verify = config.get_config_value('service', 'ssl_verify')
self._verify = config.get_config_value("service", "ssl_verify")
else:
self._verify = verify
progress = self.mode == ASYNC
Expand Down
47 changes: 26 additions & 21 deletions rooki/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import logging

ROOKI_HOME = os.path.abspath(os.path.dirname(__file__))
DEFAULT_CFG = os.path.join(ROOKI_HOME, 'default.cfg')
DEFAULT_CFG = os.path.join(ROOKI_HOME, "default.cfg")

RAW_OPTIONS = [('logging', 'format'), ]
RAW_OPTIONS = [
("logging", "format"),
]

CONFIG = None
LOGGER = logging.getLogger("ROOKI")
Expand All @@ -23,7 +25,7 @@ def get_config_value(section, option):
if not CONFIG:
load_configuration()

value = ''
value = ""

if CONFIG.has_section(section):
if CONFIG.has_option(section, option):
Expand All @@ -47,32 +49,32 @@ def load_configuration(cfgfiles=None):
"""
global CONFIG

LOGGER.info('loading configuration')
LOGGER.info("loading configuration")
CONFIG = configparser.ConfigParser(os.environ)

LOGGER.debug('setting default values')
CONFIG.add_section('service')
CONFIG.set('service', 'url', 'http://localhost:5000/wps')
LOGGER.debug("setting default values")
CONFIG.add_section("service")
CONFIG.set("service", "url", "http://localhost:5000/wps")

config_files = _get_default_config_files_location()
if cfgfiles:
config_files.extend(cfgfiles)

if 'ROOKI_CFG' in os.environ:
config_files.append(os.environ['ROOKI_CFG'])
if "ROOKI_CFG" in os.environ:
config_files.append(os.environ["ROOKI_CFG"])

loaded_files = CONFIG.read(config_files)
if loaded_files:
LOGGER.info('Configuration file(s) {} loaded'.format(loaded_files))
LOGGER.info("Configuration file(s) {} loaded".format(loaded_files))
else:
LOGGER.info('No configuration files loaded. Using default values')
LOGGER.info("No configuration files loaded. Using default values")
# dirty hack to set rook url on binder
if 'ROOK_URL' in os.environ:
CONFIG.set('service', 'url', os.environ['ROOK_URL'])
if 'ROOK_MODE' in os.environ:
CONFIG.set('service', 'mode', os.environ['ROOK_MODE'])
if 'ROOK_SSL_VERIFY' in os.environ:
CONFIG.set('service', 'ssl_verify', os.environ['ROOK_SSL_VERIFY'])
if "ROOK_URL" in os.environ:
CONFIG.set("service", "url", os.environ["ROOK_URL"])
if "ROOK_MODE" in os.environ:
CONFIG.set("service", "mode", os.environ["ROOK_MODE"])
if "ROOK_SSL_VERIFY" in os.environ:
CONFIG.set("service", "ssl_verify", os.environ["ROOK_SSL_VERIFY"])


def _get_default_config_files_location():
Expand All @@ -83,8 +85,11 @@ def _get_default_config_files_location():
:returns: configuration files
:rtype: list of strings
"""
LOGGER.debug('trying to estimate the default location')
cfgfiles = [DEFAULT_CFG, "/etc/rooki.cfg", ]
if 'HOME' in os.environ:
cfgfiles.append(os.path.join(os.environ['HOME'], ".rooki.cfg"))
LOGGER.debug("trying to estimate the default location")
cfgfiles = [
DEFAULT_CFG,
"/etc/rooki.cfg",
]
if "HOME" in os.environ:
cfgfiles.append(os.path.join(os.environ["HOME"], ".rooki.cfg"))
return cfgfiles
52 changes: 25 additions & 27 deletions rooki/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@


class Operator:

def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

def _tree(self, tree=defaultdict(dict)):
# args = [arg._tree(tree) for arg in self.args]
[arg._tree(tree) for arg in self.args]
tree['steps'][self.method_key] = {
'run': self.METHOD,
'in': {
'collection': _unpack_if_single([arg.collection for arg in self.args]),
tree["steps"][self.method_key] = {
"run": self.METHOD,
"in": {
"collection": _unpack_if_single([arg.collection for arg in self.args]),
**self.kwargs,
}
},
}
tree['outputs']['output'] = self.collection
tree["outputs"]["output"] = self.collection
return tree

def _serialise(self, doc='workflow'):
def _serialise(self, doc="workflow"):
tree = self._tree()
tree['doc'] = doc
tree["doc"] = doc
return json.dumps(tree)

def orchestrate(self):
Expand All @@ -36,7 +35,7 @@ def orchestrate(self):
@property
def method_key(self):
methods = self._get_methods([])
return f'{self.METHOD}_{self.variable}_{methods.count(self.METHOD)}'
return f"{self.METHOD}_{self.variable}_{methods.count(self.METHOD)}"

def _get_methods(self, methods):
# method_names = [arg._get_methods(methods) for arg in self.args]
Expand All @@ -46,67 +45,66 @@ def _get_methods(self, methods):

@property
def collection(self):
return f'{self.method_key}/output'
return f"{self.method_key}/output"

@property
def variable(self):
return _unpack_if_single([arg.variable for arg in self.args])


class Input:

def __init__(self, variable, dataset):
self.variable = variable
self.dataset = dataset

def _serialise(self, doc='workflow'):
def _serialise(self, doc="workflow"):
tree = self._tree()
tree['doc'] = doc
tree["doc"] = doc
return json.dumps(tree)

def orchestrate(self):
return rooki.orchestrate(workflow=self._serialise())

def _tree(self, tree=defaultdict(dict)):
tree['inputs'][self.variable] = self.dataset
tree["inputs"][self.variable] = self.dataset
return tree

def _get_methods(self, methods):
return methods

@property
def collection(self):
return f'inputs/{self.variable}'
return f"inputs/{self.variable}"


class Average(Operator):
METHOD = 'average'
METHOD = "average"


class Subset(Operator):
METHOD = 'subset'
METHOD = "subset"


class Diff(Operator):
METHOD = 'diff'
METHOD = "diff"

def _tree(self, tree=defaultdict(dict)):
# args = [arg._tree(tree) for arg in self.args]
[arg._tree(tree) for arg in self.args]
tree['steps'][self.method_key] = {
'run': self.METHOD,
'in': {
'collection_a': _unpack_if_single([self.args[0].collection]),
'collection_b': _unpack_if_single([self.args[1].collection]),
tree["steps"][self.method_key] = {
"run": self.METHOD,
"in": {
"collection_a": _unpack_if_single([self.args[0].collection]),
"collection_b": _unpack_if_single([self.args[1].collection]),
**self.kwargs,
}
},
}
tree['outputs']['output'] = self.collection
tree["outputs"]["output"] = self.collection
return tree

@property
def variable(self):
return 'var'
return "var"


def _unpack_if_single(list):
Expand Down
Loading

0 comments on commit a1e525f

Please sign in to comment.