Skip to content

Commit

Permalink
Merge pull request #172 from jacebrowning/handle-commented-lists
Browse files Browse the repository at this point in the history
Handle commented lists in matching files
  • Loading branch information
jacebrowning committed Apr 13, 2020
2 parents 7977594 + aa5ae3f commit 392704a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
17 changes: 14 additions & 3 deletions datafiles/formats.py
Expand Up @@ -7,6 +7,8 @@
from pathlib import Path
from typing import IO, Any, Dict, List

import log

from . import settings, types


Expand Down Expand Up @@ -84,7 +86,13 @@ def extensions(cls):
def deserialize(cls, file_object):
from ruamel import yaml

return yaml.round_trip_load(file_object, preserve_quotes=True) or {}
try:
data = yaml.round_trip_load(file_object, preserve_quotes=True)
except NotImplementedError as e:
log.error(str(e))
return {}
else:
return data or {}

@classmethod
def serialize(cls, data):
Expand All @@ -105,8 +113,11 @@ def serialize(cls, data):
text = f.getvalue().strip() + '\n'
else:
text = yaml.round_trip_dump(data) or ""
text = text.replace('- \n', '-\n')
return "" if text == "{}\n" else text

if text == "{}\n":
return ""

return text.replace('- \n', '-\n')


class PyYAML(Formatter):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "0.9b5"
version = "0.9b6"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down
40 changes: 38 additions & 2 deletions tests/test_orm_usage.py
@@ -1,11 +1,11 @@
"""Tests that represent usage as an ORM."""

from typing import Optional
from typing import List, Optional

import pytest

from datafiles import datafile
from datafiles.utils import logbreak
from datafiles.utils import logbreak, write


# This model is based on the example dataclass from:
Expand Down Expand Up @@ -97,3 +97,39 @@ class Sample:
logbreak("get key=42")
sample2 = Sample.objects.get(42)
expect(sample2.name) == sample.name


def test_comments_in_matched_files(expect):
@datafile("../tmp/templates/{self.key}/config.yml")
class LegacyTemplate:
key: str
name: str
link: str
default: List[str]
aliases: List[str]

write(
'tmp/templates/foo/config.yml',
"""
link: # placeholder
default:
- # placeholder
- # placeholder
aliases:
- # placeholder
""",
)
write(
'tmp/templates/bar/config.yml',
"""
link: http://example.com
default:
- abc
- def
aliases:
- qux
""",
)

items = list(LegacyTemplate.objects.all())
expect(len(items)) == 2

0 comments on commit 392704a

Please sign in to comment.