Skip to content

Commit

Permalink
Use ruff as Python code formatter and linter
Browse files Browse the repository at this point in the history
Use `ruff` to replace:

* `autoflake`
* `black`
* `flake8`
* `isort`

Also reorganize the `pre-commit` hooks in a more logical order.
  • Loading branch information
fabcor-maxiv committed Feb 17, 2025
1 parent 073007f commit 754bfab
Show file tree
Hide file tree
Showing 142 changed files with 7,370 additions and 610 deletions.
19 changes: 0 additions & 19 deletions .flake8

This file was deleted.

4 changes: 0 additions & 4 deletions .isort.cfg

This file was deleted.

76 changes: 33 additions & 43 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
---

repos:

- repo: https://github.com/adrienverge/yamllint
rev: v1.35.1
hooks:
- id: yamllint
args:
- --strict
%YAML 1.2

- repo: https://github.com/python-poetry/poetry
rev: 2.0.1
hooks:
- id: poetry-check
---

- repo: https://github.com/PyCQA/autoflake
rev: v2.3.1
hooks:
- id: autoflake
name: Autoflake
args:
- --expand-star-imports
- --ignore-init-module-imports
- --in-place
- --remove-duplicate-keys
- --ignore-pass-after-docstring

- repo: https://github.com/pycqa/isort
rev: 6.0.0
hooks:
- id: isort
repos:

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 25.1.0
hooks:
- id: black
name: Black

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
Expand All @@ -46,17 +15,14 @@ repos:
exclude: |
(?x)^(
mxcubecore/configuration/.*|
mxcubecore/HardwareObjects/(MotorsNPosition|PlateManipulator|TangoMotor)\.py|
mxcubecore/HardwareObjects/(PlateManipulatorMaintenance|TangoLimaMpegVideo)\.py|
mxcubecore/HardwareObjects/DESY/P11(EDNACharacterisation|BackLight|Shutter|Collect|DetectorCover)\.py|
mxcubecore/HardwareObjects/LNLS/LNLS(PilatusDet|Energy|Diffractometer)\.py|
mxcubecore/HardwareObjects/(PlateManipulatorMaintenance|TangoMotor)\.py|
mxcubecore/HardwareObjects/DESY/P11(BackLight|Collect|DetectorCover|Shutter)\.py|
mxcubecore/HardwareObjects/LNLS/LNLS(Diffractometer|Energy|PilatusDet)\.py|
mxcubecore/HardwareObjects/LNLS/EPICS(Actuator|Motor|NState)\.py|
mxcubecore/HardwareObjects/LNLS/set_transmission_mnc.py|
mxcubecore/HardwareObjects/EMBL/EMBL(Energy|DoorInterlock|OnlineProcessing)\.py|
mxcubecore/HardwareObjects/mockup/(ISPyBClient|Motor|PlateManipulator)Mockup\.py|
mxcubecore/HardwareObjects/abstract/(AbstractXRFSpectrum|sample_changer/Crims)\.py|
mxcubecore/HardwareObjects/EMBL/EMBL(Energy|OnlineProcessing)\.py|
mxcubecore/HardwareObjects/mockup/MotorMockup\.py|
mxcubecore/HardwareObjects/SOLEIL/PX2/PX2Resolution\.py|
mxcubecore/HardwareObjects/ESRF/ID29XRFSpectrum\.py
)$
- id: end-of-file-fixer
# Exclude files from end-of-file-fixer checks until we get around fixing them.
Expand All @@ -75,6 +41,30 @@ repos:
- id: check-symlinks
- id: check-xml
- id: check-yaml
exclude: ^.drone\.yml
- id: mixed-line-ending
args: ["--fix=lf"]


- repo: https://github.com/adrienverge/yamllint
rev: v1.35.1
hooks:
- id: yamllint
args:
- --strict


- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.6
hooks:
- id: ruff
# TODO: args: [--fix]
- id: ruff-format


- repo: https://github.com/python-poetry/poetry
rev: 2.0.1
hooks:
- id: poetry-check


...
14 changes: 7 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,15 @@ def function_with_types_in_docstring(param1, param2):
```
The Python code is to be formatted and checked for lint with
[*ruff*](https://astral.sh/ruff).
This repository uses [*pre-commit*](https://pre-commit.com/) hooks
to check the code quality automatically.
These checks are done for each pull request on GitHub.
It is also recommended to do these checks locally by enabling the git pre-commit hooks.
For example with a one-time command like this: `pre-commit install`.
You can use [autopep8](https://pypi.org/project/autopep8/) and [black](https://pypi.org/project/autopep8/) to format your code:
```bash
autopep8 -a -r -j 0 -i --max-line-length 88 ./
black --safe ./
```
### Continuous integration (CI)
GitHub Action are used for continues integration
Expand Down
4 changes: 0 additions & 4 deletions deprecated/HardwareObjects/Camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,6 @@ def getBpmState(self):
def setROI(self, startx, endx, starty, endy):
"""taco"""
if self.is_ready():

self.getBpmValues()
if self.res["live"]:
self.set_live(False)
Expand All @@ -937,7 +936,6 @@ def setROI(self, startx, endx, starty, endy):
def setExposure(self, exposure):
"""taco"""
if self.is_ready():

self.getBpmValues()
if self.res["live"]:
self.set_live(False)
Expand All @@ -952,7 +950,6 @@ def setExposure(self, exposure):
def setGain(self, gain):
"""taco"""
if self.is_ready():

self.getBpmValues()
if self.res["live"]:
self.set_live(False)
Expand All @@ -967,7 +964,6 @@ def setGain(self, gain):
def setThreshold(self, threshold):
"""taco"""
if self.is_ready():

self.getBpmValues()
if self.res["live"]:
self.set_live(False)
Expand Down
1 change: 0 additions & 1 deletion deprecated/HardwareObjects/NamedState.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def get_state(self):
return "unknown"

def setState(self, statename):

self.emit("hardwareStateChanged", ("STANDBY",))
logging.getLogger().exception(
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>..............changing state for %s to ws: %s."
Expand Down
7 changes: 0 additions & 7 deletions mxcubecore/Command/Sardana.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@


def processSardanaEvents():

while not SardanaObject._eventsQueue.empty():

try:
ev = SardanaObject._eventsQueue.get_nowait()
except queue.Empty:
Expand Down Expand Up @@ -181,7 +179,6 @@ def result_callback(self, received_event):
self.emit("macroResultUpdated", received_event.attr_value.value)

def __call__(self, *args, **kwargs):

self._reply_arrived_event.clear()
self.result = None

Expand Down Expand Up @@ -348,7 +345,6 @@ def __init__(self, name, command, taurusname=None, username=None, **kwargs):
self.device = None

def init_device(self):

try:
self.device = Device(self.taurusname)
except DevFailed as traceback:
Expand All @@ -365,7 +361,6 @@ def init_device(self):
raise ConnectionError

def __call__(self, *args, **kwargs):

self.emit("commandBeginWaitReply", (str(self.name()),))

if self.device is None:
Expand Down Expand Up @@ -422,7 +417,6 @@ def __init__(self):
self.init_device()

def init_device(self):

try:
self.attribute = Attribute(self.model)
#
Expand Down Expand Up @@ -496,7 +490,6 @@ def get_info(self):
return self.info

def update(self, event):

data = event.event[2]

try:
Expand Down
1 change: 0 additions & 1 deletion mxcubecore/Command/Tango.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ def get_info(self):
return self.device.get_attribute_config(self.attribute_name)

def update(self, value=Poller.NotInitializedValue):

# start with checking if we have a numpy array, as comparing
# numpy.ndarray to Poller.NotInitializedValue raises a ValueError exception
if isinstance(value, numpy.ndarray):
Expand Down
1 change: 0 additions & 1 deletion mxcubecore/Command/Tine.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ def tineEventCallback(self, id, cc, data_list):
"""

def update(self, value=None):

# if self.tineName.split("/")[2] == 'ics':
# print '>>>>>>>>>>>>>>>>>>', self.attributeName, value, self.value, self.oldvalue

Expand Down
1 change: 1 addition & 0 deletions mxcubecore/Command/exporter/StandardClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# along with MXCuBE. If not, see <http://www.gnu.org/licenses/>.

"""ProtocolError and StandardClient implementation"""

import socket
import sys

Expand Down
2 changes: 0 additions & 2 deletions mxcubecore/CommandContainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ def add_channel(
)

elif channel_type.lower() == "sardana":

if "taurusname" not in attributes_dict:
try:
attributes_dict["taurusname"] = self.taurusname
Expand Down Expand Up @@ -762,7 +761,6 @@ def add_command(
)

elif cmd_type.lower() == "sardana":

doorname = None
taurusname = None
cmd_type = None
Expand Down
1 change: 0 additions & 1 deletion mxcubecore/HardwareObjects/ALBA/ALBAAutoProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def init(self):

# input files for standard collection auto processing
def create_input_files(self, xds_dir, mosflm_dir, dc_pars):

fileinfo = dc_pars["fileinfo"]
osc_seq = dc_pars["oscillation_sequence"][0]

Expand Down
2 changes: 0 additions & 2 deletions mxcubecore/HardwareObjects/ALBA/ALBABackLight.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(self, *args):
self.default_minimum_level = 7.0

def init(self):

self.backlightin_channel = self.get_channel_object("backlightin")
self.level_channel = self.get_channel_object("light_level")

Expand Down Expand Up @@ -65,7 +64,6 @@ def state_changed(self, value):
self.emit("stateChanged", "off")

def _current_state(self):

state = None

if self.actuator_status:
Expand Down
1 change: 0 additions & 1 deletion mxcubecore/HardwareObjects/ALBA/ALBACalibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def __init__(self, name):
super().__init__(name)

def init(self):

self.calibx = self.get_channel_object("calibx")
self.caliby = self.get_channel_object("caliby")

Expand Down
4 changes: 0 additions & 4 deletions mxcubecore/HardwareObjects/ALBA/ALBAClusterJob.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def run(self, *args):
pass

def wait_done(self, wait=True):

if not self.job:
return

Expand All @@ -47,7 +46,6 @@ class ALBAAutoprocJob(ALBAClusterJob):
sls_script = os.path.join(root, "edna-mx/autoproc/edna-mx.autoproc.sl")

def run(self, *args):

jobname = os.path.basename(os.path.dirname(edna_directory))
self.job = XalocJob(
"edna-autoproc", jobname, self.sls_script, input_file, edna_directory
Expand All @@ -67,11 +65,9 @@ def run(self, *args):


class ALBAStrategyJob(ALBAClusterJob):

sls_script = os.path.join(root, "edna-mx/strategy/edna-mx.strategy.sl")

def run(self, *args):

logging.getLogger("HWR").debug("Starting StrategyJob - ")

input_file, results_file, edna_directory = args
Expand Down
3 changes: 0 additions & 3 deletions mxcubecore/HardwareObjects/ALBA/ALBADataAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def init(self):
EDNACharacterisation.init(self)

def prepare_input(self, edna_input):

# used for strategy calculation (characterization) using data analysis cluster
# ALBA specific

Expand Down Expand Up @@ -119,7 +118,6 @@ def fix_path(self, path):
return outpath

def wait_done(self):

logging.getLogger("HWR").debug("Polling for Job state")
time.sleep(0.5)
logging.getLogger("HWR").debug("Polling for Job state 2")
Expand All @@ -145,7 +143,6 @@ def wait_done(self):
return state

def get_result(self):

jobstatus = self.job.status

# outname = self.input_file.replace("Input", "Output")
Expand Down
1 change: 0 additions & 1 deletion mxcubecore/HardwareObjects/ALBA/ALBAEnergy.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def get_wavelength_limits(self):


def test_hwo(hwo):

print("Wavelength is: ", hwo.get_wavelength())
print("Energy limits are: ", hwo.get_limits())
print("Wavelength limits are: ", hwo.get_wavelength_limits())
Expand Down
1 change: 0 additions & 1 deletion mxcubecore/HardwareObjects/ALBA/ALBAFrontLight.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(self, *args):
self.off_threshold = None

def init(self):

self.level_channel = self.get_channel_object("light_level")
self.state_channel = self.get_channel_object("state")
threshold = self.get_property("off_threshold")
Expand Down
1 change: 0 additions & 1 deletion mxcubecore/HardwareObjects/ALBA/ALBAMiniDiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def __init__(self, *args):
self.super_hwobj = None

def init(self):

self.calibration = self.get_object_by_role("calibration")

self.centring_hwobj = self.get_object_by_role("centring")
Expand Down
Loading

0 comments on commit 754bfab

Please sign in to comment.