Skip to content

Commit

Permalink
Lint and Format with ruff (#46)
Browse files Browse the repository at this point in the history
* Updated imports
* lint and formatting
* Added some ignores in pyproject.toml

Signed-off-by: apetrynet <[email protected]>

---------

Signed-off-by: apetrynet <[email protected]>
  • Loading branch information
apetrynet authored Aug 22, 2024
1 parent e9a820e commit 9424f9d
Show file tree
Hide file tree
Showing 26 changed files with 679 additions and 793 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ dependencies = [
"mkdocs-autorefs"
]

[tool.ruff.lint]
ignore = ["RET504", "RUF012", "FA100", "TID252", "FBT001", "FBT002"]

[tool.hatch.envs.docs.scripts]
build = "mkdocs build --clean --strict"
serve = "mkdocs serve --dev-addr localhost:8000"
Expand Down
76 changes: 38 additions & 38 deletions src/pyfdl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
from .canvas import Canvas
from .canvas_template import CanvasTemplate
from .common import (
Base,
Dimensions,
Point,
rounding_strategy,
RoundStrategy,
DEFAULT_ROUNDING_STRATEGY,
FDL_SCHEMA_MAJOR,
FDL_SCHEMA_MINOR,
FDL_SCHEMA_VERSION,
NO_ROUNDING,
Base,
Dimensions,
Point,
RoundStrategy,
TypedCollection,
rounding_strategy,
set_rounding_strategy,
TypedCollection
)
from .header import Header
from .framing_intent import FramingIntent
from .framing_decision import FramingDecision
from .canvas import Canvas
from .context import Context
from .canvas_template import CanvasTemplate
from .fdl import FDL
from .errors import FDLError, FDLValidationError
from .handlers import read_from_file, read_from_string, write_to_string, write_to_file
from .fdl import FDL
from .framing_decision import FramingDecision
from .framing_intent import FramingIntent
from .handlers import read_from_file, read_from_string, write_to_file, write_to_string
from .header import Header

__all__ = [
'Base',
'Canvas',
'CanvasTemplate',
'Context',
'DEFAULT_ROUNDING_STRATEGY',
'Dimensions',
'FDL',
'FDLError',
'FDLValidationError',
'FDL_SCHEMA_MAJOR',
'FDL_SCHEMA_MINOR',
'FDL_SCHEMA_VERSION',
'FramingDecision',
'FramingIntent',
'Header',
'NO_ROUNDING',
'Point',
'read_from_file',
'read_from_string',
'rounding_strategy',
'RoundStrategy',
'set_rounding_strategy',
'TypedCollection',
'write_to_file',
'write_to_string'
"Base",
"Canvas",
"CanvasTemplate",
"Context",
"DEFAULT_ROUNDING_STRATEGY",
"Dimensions",
"FDL",
"FDLError",
"FDLValidationError",
"FDL_SCHEMA_MAJOR",
"FDL_SCHEMA_MINOR",
"FDL_SCHEMA_VERSION",
"FramingDecision",
"FramingIntent",
"Header",
"NO_ROUNDING",
"Point",
"read_from_file",
"read_from_string",
"rounding_strategy",
"RoundStrategy",
"set_rounding_strategy",
"TypedCollection",
"write_to_file",
"write_to_string",
]

__version__ = "0.1.0.dev0"
148 changes: 61 additions & 87 deletions src/pyfdl/canvas.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,49 @@
from typing import Tuple, Union
from typing import Optional, TypeVar, Union

from pyfdl import (
Base,
Dimensions,
Point,
FramingDecision,
TypedCollection,
FramingIntent
)
from .common import Base, Dimensions, Point, TypedCollection
from .framing_decision import FramingDecision
from .framing_intent import FramingIntent

CanvasTemplate = TypeVar("CanvasTemplate")


class Canvas(Base):
attributes = [
'label',
'id',
'source_canvas_id',
'dimensions',
'effective_dimensions',
'effective_anchor_point',
'photosite_dimensions',
'physical_dimensions',
'anamorphic_squeeze',
'framing_decisions'
"label",
"id",
"source_canvas_id",
"dimensions",
"effective_dimensions",
"effective_anchor_point",
"photosite_dimensions",
"physical_dimensions",
"anamorphic_squeeze",
"framing_decisions",
]
kwarg_map = {'id': 'id_'}
kwarg_map = {"id": "id_"}
object_map = {
'dimensions': Dimensions,
'effective_dimensions': Dimensions,
'effective_anchor_point': Point,
'photosite_dimensions': Dimensions,
'physical_dimensions': Dimensions,
'framing_decisions': FramingDecision
"dimensions": Dimensions,
"effective_dimensions": Dimensions,
"effective_anchor_point": Point,
"photosite_dimensions": Dimensions,
"physical_dimensions": Dimensions,
"framing_decisions": FramingDecision,
}
required = ['id', 'source_canvas_id', 'dimensions', 'effective_dimensions.effective_anchor_point']
defaults = {'source_canvas_id': 'self.id', 'anamorphic_squeeze': 1}
required = ["id", "source_canvas_id", "dimensions", "effective_dimensions.effective_anchor_point"]
defaults = {"source_canvas_id": "self.id", "anamorphic_squeeze": 1}

def __init__(
self,
label: str = None,
id_: str = None,
source_canvas_id: str = None,
dimensions: Dimensions = None,
effective_dimensions: Dimensions = None,
effective_anchor_point: Point = None,
photosite_dimensions: Dimensions = None,
physical_dimensions: Dimensions = None,
anamorphic_squeeze: float = None,
framing_decisions: TypedCollection = None
self,
label: Optional[str] = None,
id_: Optional[str] = None,
source_canvas_id: Optional[str] = None,
dimensions: Optional[Dimensions] = None,
effective_dimensions: Optional[Dimensions] = None,
effective_anchor_point: Optional[Point] = None,
photosite_dimensions: Optional[Dimensions] = None,
physical_dimensions: Optional[Dimensions] = None,
anamorphic_squeeze: Optional[float] = None,
framing_decisions: Optional[TypedCollection] = None,
):
super().__init__()
self.label = label
Expand Down Expand Up @@ -119,8 +116,8 @@ def place_framing_intent(self, framing_intent: FramingIntent) -> str:

return framing_decision.id

def get_dimensions(self) -> Tuple[Dimensions, Point]:
""" Get the most relevant dimensions and anchor point for the canvas.
def get_dimensions(self) -> tuple[Dimensions, Point]:
"""Get the most relevant dimensions and anchor point for the canvas.
`effective_dimensions` and `effective_anchor_point` win over `dimensions`
Returns:
Expand All @@ -134,11 +131,11 @@ def get_dimensions(self) -> Tuple[Dimensions, Point]:

@classmethod
def from_canvas_template(
cls,
canvas_template: 'CanvasTemplate',
source_canvas: 'Canvas',
source_framing_decision: Union[FramingDecision, int] = 0
) -> 'Canvas':
cls,
canvas_template: CanvasTemplate,
source_canvas: "Canvas",
source_framing_decision: Union[FramingDecision, int] = 0,
) -> "Canvas":
"""
Create a new `Canvas` from the provided `source_canvas` and `framing_decision`
based on a [CanvasTemplate](canvas_template.md#pyfdl.CanvasTemplate)
Expand All @@ -152,81 +149,62 @@ def from_canvas_template(
canvas: based on the provided canvas template and sources
"""
if type(source_framing_decision) is int:
if isinstance(source_framing_decision, int):
source_framing_decision = source_canvas.framing_decisions[source_framing_decision]

canvas = Canvas(
label=canvas_template.label,
id_=Base.generate_uuid().replace('-', ''),
id_=Base.generate_uuid().replace("-", ""),
source_canvas_id=source_canvas.id,
anamorphic_squeeze=canvas_template.target_anamorphic_squeeze
anamorphic_squeeze=canvas_template.target_anamorphic_squeeze,
)

framing_decision = FramingDecision(
label=source_framing_decision.label,
id_=f'{canvas.id}-{source_framing_decision.framing_intent_id}',
framing_intent_id=source_framing_decision.framing_intent_id
id_=f"{canvas.id}-{source_framing_decision.framing_intent_id}",
framing_intent_id=source_framing_decision.framing_intent_id,
)
canvas.framing_decisions.add(framing_decision)

source_map = {
'framing_decision': source_framing_decision,
'canvas': source_canvas
}
source_map = {"framing_decision": source_framing_decision, "canvas": source_canvas}

dest_map = {
'framing_decision': framing_decision,
'canvas': canvas
}
dest_map = {"framing_decision": framing_decision, "canvas": canvas}

# Figure out what dimensions to use
fit_source = canvas_template.fit_source
source_type, source_attribute = fit_source.split('.')
source_type, source_attribute = fit_source.split(".")
preserve = canvas_template.preserve_from_source_canvas or fit_source
if preserve in [None, 'none']:
if preserve in [None, "none"]:
preserve = fit_source

# Get the scale factor
source_dimensions = getattr(source_map[source_type], source_attribute)
scale_factor = canvas_template.get_scale_factor(
source_dimensions,
source_canvas.anamorphic_squeeze
)
scale_factor = canvas_template.get_scale_factor(source_dimensions, source_canvas.anamorphic_squeeze)

# Dummy dimensions to test against if we received a proper value
dummy_dimensions = Dimensions(width=0, height=0)

# Copy and scale dimensions from source to target
for transfer_key in canvas_template.get_transfer_keys():
if transfer_key == fit_source:
target_size = canvas_template.fit_source_to_target(
source_dimensions,
source_canvas.anamorphic_squeeze
)
target_size = canvas_template.fit_source_to_target(source_dimensions, source_canvas.anamorphic_squeeze)
setattr(dest_map[source_type], source_attribute, target_size)
continue

source_type, dimension_source_attribute = transfer_key.split('.')
dimensions = getattr(
source_map[source_type],
dimension_source_attribute,
dummy_dimensions
).copy()
source_type, dimension_source_attribute = transfer_key.split(".")
dimensions = getattr(source_map[source_type], dimension_source_attribute, dummy_dimensions).copy()

if dimensions == dummy_dimensions:
# Source canvas/framing decision is missing this dimension. Let's move on
continue

dimensions.width = canvas_template.get_desqueezed_width(
dimensions.width,
source_canvas.anamorphic_squeeze
)
dimensions.width = canvas_template.get_desqueezed_width(dimensions.width, source_canvas.anamorphic_squeeze)
dimensions.scale_by(scale_factor)
setattr(dest_map[source_type], dimension_source_attribute, dimensions)

# Make sure the canvas has dimensions
if canvas.dimensions is None:
preserve_source_type, preserve_source_attribute = preserve.split('.')
preserve_source_type, preserve_source_attribute = preserve.split(".")
canvas.dimensions = getattr(dest_map[preserve_source_type], preserve_source_attribute).copy()

# Round values according to rules defined in the template
Expand All @@ -242,14 +220,10 @@ def from_canvas_template(
# Make sure all anchor points are correct according to new sizes
canvas.adjust_effective_anchor_point()
framing_decision.adjust_protection_anchor_point(
canvas,
canvas_template.alignment_method_horizontal,
canvas_template.alignment_method_vertical
canvas, canvas_template.alignment_method_horizontal, canvas_template.alignment_method_vertical
)
framing_decision.adjust_anchor_point(
canvas,
canvas_template.alignment_method_horizontal,
canvas_template.alignment_method_vertical
canvas, canvas_template.alignment_method_horizontal, canvas_template.alignment_method_vertical
)

return canvas
Expand All @@ -264,7 +238,7 @@ def adjust_effective_anchor_point(self) -> None:

self.effective_anchor_point = Point(
x=(self.dimensions.width - self.effective_dimensions.width) / 2,
y=(self.dimensions.height - self.effective_dimensions.height) / 2
y=(self.dimensions.height - self.effective_dimensions.height) / 2,
)

def __repr__(self):
Expand Down
Loading

0 comments on commit 9424f9d

Please sign in to comment.