Skip to content

Commit

Permalink
Add linting (#3)
Browse files Browse the repository at this point in the history
* Add pre-commit config

* Format files using ruff

* Add lint to CI

* Update ruff settings

* Changed line-length to 79 characters
  • Loading branch information
imenelydiaker authored Jun 23, 2024
1 parent 4e9f028 commit cd7a7a6
Show file tree
Hide file tree
Showing 24 changed files with 701 additions and 191 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ jobs:
key: venv-${{ hashFiles('poetry.lock') }}
- name: Install the project dependencies
run: poetry install --with dev
- name: Run lint
run: |
make lint
- name: Run tests
run: |
make tests
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
#.idea/
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-added-large-files
args: ['--maxkb=600']
- id: check-yaml
- id: check-json
- id: check-toml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-docstring-first
- repo: https://github.com/python-poetry/poetry
rev: 1.8.0
hooks:
- id: poetry-check
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.2
hooks:
- id: ruff
args: [ --fix ]
exclude: (^plume_python/samples/|^tests/)
- id: ruff-format
exclude: (^plume_python/samples/|^tests/)
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fixed a bug where extracting samples by time range would throw an exception if the record contained timeless samples.
- Fixed a bug where extracting samples by time range would throw an exception if the record contained timeless samples.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,4 @@ into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.
<https://www.gnu.org/licenses/why-not-lgpl.html>.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ install:
tests:
@echo "--- 🧪 Running tests ---"
poetry run pytest

.PHONY: lint
lint:
@echo "--- 🧹 Linting code ---"
poetry run pre-commit run --all-files
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ For more advanced usage, the package can be imported in a Python script:
import plume_python as plm
from plume_python.utils.dataframe import samples_to_dataframe, record_to_dataframes
from plume_python.samples.unity import transform_pb2
from plume_python.export import xdf_exporter
from plume_python.export import xdf_exporter
from plume_python.utils.game_object import find_names_by_guid, find_first_identifier_by_name

# Load a record file
Expand Down Expand Up @@ -136,4 +136,4 @@ Sophie VILLENAVE - [email protected]
```

[Button Docs]: https://img.shields.io/badge/Explore%20the%20docs-%E2%86%92-brightgreen
[Explore the docs]: https://liris-xr.github.io/PLUME/
[Explore the docs]: https://liris-xr.github.io/PLUME/
7 changes: 0 additions & 7 deletions plume_python/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
from . import file_reader
from . import parser
from . import record
from . import utils
from . import samples
from . import export
from . import cli
95 changes: 63 additions & 32 deletions plume_python/cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import os.path

import click
import click # type: ignore

from plume_python import parser
from plume_python.export.xdf_exporter import export_xdf_from_record
from plume_python.samples import sample_types_from_names
from plume_python.utils.dataframe import record_to_dataframes, samples_to_dataframe, world_transforms_to_dataframe
from plume_python.utils.game_object import find_names_by_guid, find_identifiers_by_name, \
find_identifier_by_game_object_id
from plume_python.utils.dataframe import (
record_to_dataframes,
samples_to_dataframe,
world_transforms_to_dataframe,
)
from plume_python.utils.game_object import (
find_names_by_guid,
find_identifiers_by_name,
find_identifier_by_game_object_id,
)
from plume_python.utils.transform import compute_transform_time_series


Expand All @@ -17,33 +24,40 @@ def cli():


@click.command()
@click.argument('record_path', type=click.Path(exists=True, readable=True))
@click.option('--xdf_output_path', type=click.Path(writable=True))
@click.argument("record_path", type=click.Path(exists=True, readable=True))
@click.option("--xdf_output_path", type=click.Path(writable=True))
def export_xdf(record_path: str, xdf_output_path: str | None):
"""Export a XDF file including LSL samples and markers."""
if not record_path.endswith('.plm'):
if not record_path.endswith(".plm"):
click.echo(err=True, message="Input file must be a .plm file")
return

if xdf_output_path is None:
xdf_output_path = record_path.replace('.plm', '.xdf')
xdf_output_path = record_path.replace(".plm", ".xdf")

if os.path.exists(xdf_output_path):
if not click.confirm(f"File '{xdf_output_path}' already exists, do you want to overwrite it?"):
if not click.confirm(
f"File '{xdf_output_path}' already exists, do you want to overwrite it?"
):
return

with open(xdf_output_path, "wb") as xdf_output_file:
record = parser.parse_record_from_file(record_path)
export_xdf_from_record(xdf_output_file, record)
click.echo('Exported xdf from record: ' + record_path + ' to ' + xdf_output_path)
click.echo(
"Exported xdf from record: "
+ record_path
+ " to "
+ xdf_output_path
)


@click.command()
@click.argument('record_path', type=click.Path(exists=True, readable=True))
@click.argument('guid', type=click.STRING)
@click.argument("record_path", type=click.Path(exists=True, readable=True))
@click.argument("guid", type=click.STRING)
def find_name(record_path: str, guid: str):
"""Find the name(s) of a GameObject with the given GUID in the record."""
if not record_path.endswith('.plm'):
if not record_path.endswith(".plm"):
click.echo(err=True, message="Input file must be a .plm file")
return

Expand All @@ -58,11 +72,11 @@ def find_name(record_path: str, guid: str):


@click.command()
@click.argument('record_path', type=click.Path(exists=True, readable=True))
@click.argument('name', type=click.STRING)
@click.argument("record_path", type=click.Path(exists=True, readable=True))
@click.argument("name", type=click.STRING)
def find_guid(record_path: str, name: str):
"""Find the GUID(s) of a GameObject by the given name."""
if not record_path.endswith('.plm'):
if not record_path.endswith(".plm"):
click.echo(err=True, message="Input file must be a .plm file")
return

Expand All @@ -77,11 +91,11 @@ def find_guid(record_path: str, name: str):


@click.command()
@click.argument('record_path', type=click.Path(exists=True, readable=True))
@click.argument('guid', type=click.STRING)
@click.argument("record_path", type=click.Path(exists=True, readable=True))
@click.argument("guid", type=click.STRING)
def export_world_transforms(record_path: str, guid: str):
"""Export world transforms of a GameObject with the given GUID to a CSV file."""
if not record_path.endswith('.plm'):
if not record_path.endswith(".plm"):
click.echo(err=True, message="Input file must be a .plm file")
return

Expand All @@ -92,40 +106,57 @@ def export_world_transforms(record_path: str, guid: str):
click.echo(err=True, message=f"No identifier found for GUID {guid}")
return

time_series = compute_transform_time_series(record, identifier.transform_id)
time_series = compute_transform_time_series(
record, identifier.transform_id
)
df = world_transforms_to_dataframe(time_series)
file_path = record_path.replace('.plm', f'_{guid}_world_transform.csv')
file_path = record_path.replace(".plm", f"_{guid}_world_transform.csv")
df.to_csv(file_path)


@click.command()
@click.argument('record_path', type=click.Path(exists=True, readable=True))
@click.argument('output_dir', type=click.Path(exists=True, writable=True))
@click.option('--filter', default="all", show_default=True, type=click.STRING,
help="Comma separated list of sample types to export (eg. 'TransformUpdate,GameObjectUpdate')")
@click.argument("record_path", type=click.Path(exists=True, readable=True))
@click.argument("output_dir", type=click.Path(exists=True, writable=True))
@click.option(
"--filter",
default="all",
show_default=True,
type=click.STRING,
help="Comma separated list of sample types to export (eg. 'TransformUpdate,GameObjectUpdate')",
)
def export_csv(record_path: str, output_dir: str | None, filter: str):
"""Export samples from the record to CSV files."""
if not record_path.endswith('.plm'):
if not record_path.endswith(".plm"):
click.echo(err=True, message="Input file must be a .plm file")
return

record = parser.parse_record_from_file(record_path)

filters = [d.strip() for d in filter.split(',')]
filters = [d.strip() for d in filter.split(",")]

if filters == ['all'] or filters == ['*']:
if filters == ["all"] or filters == ["*"]:
dataframes = record_to_dataframes(record)
for sample_type, df in dataframes.items():
file_path = os.path.join(output_dir, sample_type.__name__ + '.csv')
file_path = os.path.join(output_dir, sample_type.__name__ + ".csv")
df.to_csv(file_path)
click.echo('Exported CSV for sample type: ' + sample_type.__name__ + ' to ' + file_path)
click.echo(
"Exported CSV for sample type: "
+ sample_type.__name__
+ " to "
+ file_path
)
else:
sample_types = sample_types_from_names(filters)
for sample_type in sample_types:
df = samples_to_dataframe(record.get_samples_by_type(sample_type))
file_path = os.path.join(output_dir, sample_type.__name__ + '.csv')
file_path = os.path.join(output_dir, sample_type.__name__ + ".csv")
df.to_csv(file_path)
click.echo('Exported CSV for sample type: ' + sample_type.__name__ + ' to ' + file_path)
click.echo(
"Exported CSV for sample type: "
+ sample_type.__name__
+ " to "
+ file_path
)


cli.add_command(export_csv)
Expand Down
Loading

0 comments on commit cd7a7a6

Please sign in to comment.