Skip to content

Commit

Permalink
Cattrs structuring leads to fallback to default
Browse files Browse the repository at this point in the history
  • Loading branch information
ekiefl committed Jan 8, 2025
1 parent 67e626d commit 1e23f82
Showing 1 changed file with 55 additions and 32 deletions.
87 changes: 55 additions & 32 deletions pooltool/physics/resolve/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

from __future__ import annotations

import shutil
import traceback
from pathlib import Path
from typing import Optional

import attrs
from cattrs.errors import ClassValidationError

import pooltool.user_config
from pooltool.events.datatypes import AgentType, Event, EventType
Expand Down Expand Up @@ -120,40 +123,60 @@ def load(cls, path: Pathish) -> Resolver:
@classmethod
def default(cls) -> Resolver:
"""Load ~/.config/pooltool/physics/resolver.yaml if exists, create otherwise"""
if RESOLVER_PATH.exists():
resolver = cls.load(RESOLVER_PATH)

if resolver.version == VERSION:
return resolver
else:
run.info_single(
f"{RESOLVER_PATH} is has version {resolver.version}, which is not up to "
f"date with the most current version: {VERSION}. It will be replaced with the "
f"default."
)

resolver = cls(
ball_ball=FrictionalMathavan(
friction=AlciatoreBallBallFriction(
a=0.009951,
b=0.108,
c=1.088,
def _default_config():
return cls(

Check warning on line 128 in pooltool/physics/resolve/resolver.py

View check run for this annotation

Codecov / codecov/patch

pooltool/physics/resolve/resolver.py#L128

Added line #L128 was not covered by tests
ball_ball=FrictionalMathavan(
friction=AlciatoreBallBallFriction(
a=0.009951,
b=0.108,
c=1.088,
),
num_iterations=1000,
),
ball_linear_cushion=Han2005Linear(),
ball_circular_cushion=Han2005Circular(),
ball_pocket=CanonicalBallPocket(),
stick_ball=InstantaneousPoint(
english_throttle=1.0,
squirt_throttle=1.0,
),
num_iterations=1000,
),
ball_linear_cushion=Han2005Linear(),
ball_circular_cushion=Han2005Circular(),
ball_pocket=CanonicalBallPocket(),
stick_ball=InstantaneousPoint(
english_throttle=1.0,
squirt_throttle=1.0,
),
transition=CanonicalTransition(),
version=VERSION,
)

resolver.save(RESOLVER_PATH)
return resolver
transition=CanonicalTransition(),
version=VERSION,
)

if not RESOLVER_PATH.exists():
resolver = _default_config()
resolver.save(RESOLVER_PATH)
return resolver

Check warning on line 151 in pooltool/physics/resolve/resolver.py

View check run for this annotation

Codecov / codecov/patch

pooltool/physics/resolve/resolver.py#L149-L151

Added lines #L149 - L151 were not covered by tests

try:
resolver = cls.load(RESOLVER_PATH)
except ClassValidationError:
full_traceback = traceback.format_exc()
dump_path = RESOLVER_PATH.parent / f".{RESOLVER_PATH.name}"
run.info_single(

Check warning on line 158 in pooltool/physics/resolve/resolver.py

View check run for this annotation

Codecov / codecov/patch

pooltool/physics/resolve/resolver.py#L155-L158

Added lines #L155 - L158 were not covered by tests
f"{RESOLVER_PATH} is malformed and can't be loaded. It is being "
f"replaced with a default working version. Your version has been moved to "
f"{dump_path} if you want to diagnose it. Here is the error:\n{full_traceback}"
)
shutil.move(RESOLVER_PATH, dump_path)
resolver = _default_config()
resolver.save(RESOLVER_PATH)

Check warning on line 165 in pooltool/physics/resolve/resolver.py

View check run for this annotation

Codecov / codecov/patch

pooltool/physics/resolve/resolver.py#L163-L165

Added lines #L163 - L165 were not covered by tests

if resolver.version == VERSION:
return resolver
else:
dump_path = RESOLVER_PATH.parent / f".{RESOLVER_PATH.name}"
run.info_single(

Check warning on line 171 in pooltool/physics/resolve/resolver.py

View check run for this annotation

Codecov / codecov/patch

pooltool/physics/resolve/resolver.py#L170-L171

Added lines #L170 - L171 were not covered by tests
f"{RESOLVER_PATH} is has version {resolver.version}, which is not up to "
f"date with the most current version: {VERSION}. It will be replaced with the "
f"default. Your version has been moved to {dump_path}."
)
shutil.move(RESOLVER_PATH, dump_path)
resolver = _default_config()
resolver.save(RESOLVER_PATH)
return resolver

Check warning on line 179 in pooltool/physics/resolve/resolver.py

View check run for this annotation

Codecov / codecov/patch

pooltool/physics/resolve/resolver.py#L176-L179

Added lines #L176 - L179 were not covered by tests


def _snapshot_initial(shot: System, event: Event) -> None:
Expand Down

0 comments on commit 1e23f82

Please sign in to comment.