Skip to content

Commit

Permalink
Add ruff action and format (#110)
Browse files Browse the repository at this point in the history
* Ruff format

* Add ruff action; use uv
  • Loading branch information
ka-sarthak authored Aug 16, 2024
1 parent f19c0e3 commit 5550cd3
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 171 deletions.
41 changes: 32 additions & 9 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# This workflow will install Python dependencies, run tests, and lint with multiple version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python test
Expand All @@ -12,21 +12,44 @@ on:
permissions:
contents: read

env:
UV_SYSTEM_PYTHON: true

jobs:
test:

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
python_version: ["3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.9
uses: actions/setup-python@v3
- uses: actions/checkout@v4
- name: Set up Python ${{matrix.python_version}}
uses: actions/setup-python@v5
with:
python-version: "3.9"
python-version: ${{matrix.python_version}}
- name: Install uv
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e .[dev] --index-url https://gitlab.mpcdf.mpg.de/api/v4/projects/2187/packages/pypi/simple
uv pip install -e '.[dev]'
- name: Test with pytest
run: |
pytest
ruff-linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
with:
args: "check ."

ruff-formatting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
with:
args: "format . --check"
17 changes: 11 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies = [
[project.optional-dependencies]
dev = [
"pytest",
"ruff==0.1.8",
"ruff",
"structlog==22.3.0",
]

Expand All @@ -42,8 +42,18 @@ file = "LICENSE"
"Homepage" = "https://github.com/FAIRmat-NFDI/nomad-measurements"
"Bug Tracker" = "https://github.com/FAIRmat-NFDI/nomad-measurements/issues"

[tool.uv]
index-url = "https://gitlab.mpcdf.mpg.de/api/v4/projects/2187/packages/pypi/simple"

[tool.ruff]
include = ["src/*.py", "tests/*.py"]
exclude = ["dependencies"]

# Same as Black.
line-length = 88
indent-width = 4

[tool.ruff.lint]
select = [
"E", # pycodestyle
"W", # pycodestyle
Expand All @@ -65,11 +75,6 @@ ignore = [
"PLR5501", # else-if-used
]
fixable = ["ALL"]
exclude = ["dependencies"]

# Same as Black.
line-length = 88
indent-width = 4

[tool.ruff.format]
# use single quotes for strings.
Expand Down
51 changes: 26 additions & 25 deletions src/nomad_measurements/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import (
TYPE_CHECKING
)
from typing import TYPE_CHECKING
from nomad.metainfo.metainfo import (
Category,
)
Expand All @@ -44,17 +42,20 @@
BoundLogger,
)


class NOMADMeasurementsCategory(EntryDataCategory):
'''
"""
A category for all measurements defined in the `nomad-measurements` plugin.
'''
"""

m_def = Category(label='NOMAD Measurements', categories=[EntryDataCategory])


class ActivityReference(SectionReference):
'''
"""
A section used for referencing an Activity.
'''
"""

reference = Quantity(
type=Activity,
description='A reference to a NOMAD `Activity` entry.',
Expand All @@ -65,58 +66,57 @@ class ActivityReference(SectionReference):
)
lab_id = Quantity(
type=str,
description='''
description="""
The readable identifier for the activity.
''',
""",
a_eln=ELNAnnotation(
component=ELNComponentEnum.StringEditQuantity,
),
)

def normalize(self, archive, logger: 'BoundLogger') -> None:
'''
"""
The normalizer for the `EntityReference` class.
Will attempt to fill the `reference` from the `lab_id` or vice versa.
Args:
archive (EntryArchive): The archive containing the section that is being
normalized.
logger ('BoundLogger'): A structlog logger.
'''
"""
super(ActivityReference, self).normalize(archive, logger)
if self.reference is None and self.lab_id is not None:
from nomad.search import search, MetadataPagination
query = {
'results.eln.lab_ids': self.lab_id
}

query = {'results.eln.lab_ids': self.lab_id}
search_result = search(
owner='all',
query=query,
pagination=MetadataPagination(page_size=1),
user_id=archive.metadata.main_author.user_id)
user_id=archive.metadata.main_author.user_id,
)
if search_result.pagination.total > 0:
entry_id = search_result.data[0]["entry_id"]
upload_id = search_result.data[0]["upload_id"]
entry_id = search_result.data[0]['entry_id']
upload_id = search_result.data[0]['upload_id']
self.reference = f'../uploads/{upload_id}/archive/{entry_id}#data'
if search_result.pagination.total > 1:
logger.warn(
f'Found {search_result.pagination.total} entries with lab_id: '
f'"{self.lab_id}". Will use the first one found.'
)
else:
logger.warn(
f'Found no entries with lab_id: "{self.lab_id}".'
)
logger.warn(f'Found no entries with lab_id: "{self.lab_id}".')
elif self.lab_id is None and self.reference is not None:
self.lab_id = self.reference.lab_id
if self.name is None and self.lab_id is not None:
self.name = self.lab_id


class ProcessReference(ActivityReference):
'''
"""
A section used for referencing a Process.
'''
"""

reference = Quantity(
type=Process,
description='A reference to a NOMAD `Process` entry.',
Expand All @@ -128,10 +128,11 @@ class ProcessReference(ActivityReference):


class InSituMeasurement(Measurement):
'''
"""
A section used for a measurement performed in-situ during a process.
'''
"""

process = SubSection(
section_def=ProcessReference,
description='A reference to the process during which the measurement occurred.',
)
)
2 changes: 1 addition & 1 deletion src/nomad_measurements/transmission/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .schema import *
from .parser import *
from .parser import *
13 changes: 6 additions & 7 deletions src/nomad_measurements/transmission/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import (
TYPE_CHECKING
)
from typing import TYPE_CHECKING
from nomad.metainfo import (
Quantity,
)
Expand All @@ -43,12 +41,11 @@ class RawFileTransmissionData(EntryData):
type=ELNTransmission,
a_eln=ELNAnnotation(
component='ReferenceEditQuantity',
)
),
)


class TransmissionParser(MatchingParser):

def __init__(self):
super().__init__(
code_name='XRD Parser',
Expand All @@ -59,5 +56,7 @@ def parse(self, mainfile: str, archive: 'EntryArchive', logger) -> None:
entry = ELNTransmission.m_from_dict(ELNTransmission.m_def.a_template)
entry.data_file = data_file
file_name = f'{data_file[:-6]}.archive.json'
archive.data = RawFileTransmissionData(measurement=create_archive(entry,archive,file_name))
archive.metadata.entry_name = data_file[:-4] + ' data file'
archive.data = RawFileTransmissionData(
measurement=create_archive(entry, archive, file_name)
)
archive.metadata.entry_name = data_file[:-4] + ' data file'
18 changes: 9 additions & 9 deletions src/nomad_measurements/transmission/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import re
import os
import json
from typing import (
TYPE_CHECKING
)
from typing import TYPE_CHECKING
from nomad.datamodel.metainfo.basesections import (
Measurement,
ReadableIdentifiers,
Expand Down Expand Up @@ -57,25 +55,25 @@


class Operator(ArchiveSection):
name=Quantity(
name = Quantity(
type=str,
a_eln=ELNAnnotation(
component=ELNComponentEnum.StringEditQuantity,
),
)
affiliation=Quantity(
affiliation = Quantity(
type=str,
a_eln=ELNAnnotation(
component=ELNComponentEnum.StringEditQuantity,
),
)
address=Quantity(
address = Quantity(
type=str,
a_eln=ELNAnnotation(
component=ELNComponentEnum.StringEditQuantity,
),
)
email=Quantity(
email = Quantity(
type=str,
a_eln=ELNAnnotation(
component=ELNComponentEnum.StringEditQuantity,
Expand Down Expand Up @@ -107,7 +105,9 @@ def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
if self.operator.name:
eln_dict['/ENTRY[entry]/operator/name'] = self.name
if self.operator.affiliation:
eln_dict['/ENTRY[entry]/operator/affiliation'] = self.operator.affiliation
eln_dict['/ENTRY[entry]/operator/affiliation'] = (
self.operator.affiliation
)
if self.operator.address:
eln_dict['/ENTRY[entry]/operator/address'] = self.operator.address
if self.operator.email:
Expand All @@ -128,7 +128,7 @@ def normalize(self, archive: 'EntryArchive', logger: 'BoundLogger') -> None:
os.path.join(raw_path, self.data_file),
os.path.join(raw_path, eln_filename),
],
'output': os.path.join(raw_path, output)
'output': os.path.join(raw_path, output),
}
try:
convert(**converter_params)
Expand Down
Loading

0 comments on commit 5550cd3

Please sign in to comment.