Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use Unix-style line endings in generator output #132

Merged
merged 3 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os.path
import re
from datetime import datetime
from os import makedirs, path

from typing import Any, Dict, Iterable, List, OrderedDict, Union

Expand Down Expand Up @@ -173,3 +174,17 @@ def _convert(text: str) -> Union[int, str]:
return int(text) if text.isdigit() else text

return [_convert(x) for x in re.split(r'(\d+)', key) if x]


def serialize_common(serializable: Any, output_directory: str, uuid: str, long_type: str, short_type: str) -> None:
"""
Centralized serialize() implementation shared between Component, Symbol, Device, Package
"""
dir_path = path.join(output_directory, uuid)
if not (path.exists(dir_path) and path.isdir(dir_path)):
makedirs(dir_path)
with open(path.join(dir_path, f'.librepcb-{short_type}'), 'w', newline='\n') as f:
f.write('1\n')
with open(path.join(dir_path, f'{long_type}.lp'), 'w', newline='\n') as f:
f.write(str(serializable))
f.write('\n')
17 changes: 7 additions & 10 deletions entities/component.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from os import makedirs, path

from typing import Iterable, List

from common import serialize_common

from .common import (
Author, BoolValue, Category, Created, Deprecated, Description, EnumValue, GeneratedBy, Keywords, Name, Position,
Rotation, StringValue, UUIDValue, Version
Expand Down Expand Up @@ -212,11 +212,8 @@ def add_variant(self, variant: Variant) -> None:
self.variants.append(variant)

def serialize(self, output_directory: str) -> None:
dir_path = path.join(output_directory, self.uuid)
if not (path.exists(dir_path) and path.isdir(dir_path)):
makedirs(dir_path)
with open(path.join(dir_path, '.librepcb-cmp'), 'w') as f:
f.write('1\n')
with open(path.join(dir_path, 'component.lp'), 'w') as f:
f.write(str(self))
f.write('\n')
serialize_common(serializable=self,
output_directory=output_directory,
uuid=self.uuid,
long_type='component',
short_type='cmp')
17 changes: 6 additions & 11 deletions entities/device.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from os import makedirs, path

from typing import Iterable, List, Optional

from common import escape_string
from common import escape_string, serialize_common
from entities.attribute import Attribute

from .common import (
Expand Down Expand Up @@ -103,11 +101,8 @@ def __str__(self) -> str:
return ret

def serialize(self, output_directory: str) -> None:
dir_path = path.join(output_directory, self.uuid)
if not (path.exists(dir_path) and path.isdir(dir_path)):
makedirs(dir_path)
with open(path.join(dir_path, '.librepcb-dev'), 'w') as f:
f.write('1\n')
with open(path.join(dir_path, 'device.lp'), 'w') as f:
f.write(str(self))
f.write('\n')
serialize_common(serializable=self,
output_directory=output_directory,
uuid=self.uuid,
long_type='device',
short_type='dev')
17 changes: 6 additions & 11 deletions entities/package.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from os import makedirs, path

from typing import Iterable, List

from common import format_float
from common import format_float, serialize_common

from .common import (
Align, Author, BoolValue, Category, Circle, Created, Deprecated, Description, EnumValue, FloatValue, GeneratedBy,
Expand Down Expand Up @@ -345,11 +343,8 @@ def __str__(self) -> str:
return ret

def serialize(self, output_directory: str) -> None:
dir_path = path.join(output_directory, self.uuid)
if not (path.exists(dir_path) and path.isdir(dir_path)):
makedirs(dir_path)
with open(path.join(dir_path, '.librepcb-pkg'), 'w') as f:
f.write('1\n')
with open(path.join(dir_path, 'package.lp'), 'w') as f:
f.write(str(self))
f.write('\n')
serialize_common(serializable=self,
output_directory=output_directory,
uuid=self.uuid,
long_type='package',
short_type='pkg')
17 changes: 6 additions & 11 deletions entities/symbol.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from os import makedirs, path

from typing import Iterable, List

from common import format_float
from common import format_float, serialize_common

from .common import (
Author, Category, Circle, Created, Deprecated, Description, FloatValue, GeneratedBy, Keywords, Length, Name,
Expand Down Expand Up @@ -117,11 +115,8 @@ def __str__(self) -> str:
return ret

def serialize(self, output_directory: str) -> None:
dir_path = path.join(output_directory, self.uuid)
if not (path.exists(dir_path) and path.isdir(dir_path)):
makedirs(dir_path)
with open(path.join(dir_path, '.librepcb-sym'), 'w') as f:
f.write('1\n')
with open(path.join(dir_path, 'symbol.lp'), 'w') as f:
f.write(str(self))
f.write('\n')
serialize_common(serializable=self,
output_directory=output_directory,
uuid=self.uuid,
long_type='symbol',
short_type='sym')