-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
048b348
commit f945dc8
Showing
17 changed files
with
303 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import re | ||
from typing import Any | ||
|
||
from pydbml.tools import indent | ||
|
||
|
||
class StickyNote: | ||
dont_compare_fields = ('database',) | ||
|
||
def __init__(self, name: str, text: Any) -> None: | ||
self.name = name | ||
self.text = str(text) if text is not None else '' | ||
|
||
self.database = None | ||
|
||
def __str__(self): | ||
''' | ||
>>> print(StickyNote('mynote', 'Note text')) | ||
StickyNote('mynote', 'Note text') | ||
''' | ||
|
||
return self.__class__.__name__ + f'({repr(self.name)}, {repr(self.text)})' | ||
|
||
def __bool__(self): | ||
return bool(self.text) | ||
|
||
def __repr__(self): | ||
''' | ||
>>> StickyNote('mynote', 'Note text') | ||
<StickyNote 'mynote', 'Note text'> | ||
''' | ||
|
||
return f'<{self.__class__.__name__} {self.name!r}, {self.text!r}>' | ||
|
||
def _prepare_text_for_dbml(self): | ||
'''Escape single quotes''' | ||
pattern = re.compile(r"('''|')") | ||
return pattern.sub(r'\\\1', self.text) | ||
|
||
@property | ||
def dbml(self): | ||
text = self._prepare_text_for_dbml() | ||
if '\n' in text: | ||
note_text = f"'''\n{text}\n'''" | ||
else: | ||
note_text = f"'{text}'" | ||
|
||
note_text = indent(note_text) | ||
result = f'Note {self.name} {{\n{note_text}\n}}' | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import pyparsing as pp | ||
|
||
from .common import _, end, _c | ||
from .generic import string_literal, name | ||
from ..parser.blueprints import StickyNoteBlueprint | ||
|
||
sticky_note = _c + pp.CaselessLiteral('note') + _ + (name('name') + _ - '{' + _ - string_literal('text') + _ - '}') + end | ||
|
||
|
||
def parse_sticky_note(s, loc, tok): | ||
''' | ||
Note single_line_note { | ||
'This is a single line note' | ||
} | ||
''' | ||
init_dict = {'name': tok['name'], 'text': tok['text']} | ||
|
||
return StickyNoteBlueprint(**init_dict) | ||
|
||
|
||
sticky_note.set_parse_action(parse_sticky_note) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from unittest import TestCase | ||
|
||
from pydbml.classes.sticky_note import StickyNote | ||
from pydbml.parser.blueprints import StickyNoteBlueprint | ||
|
||
class TestNote(TestCase): | ||
def test_build(self) -> None: | ||
bp = StickyNoteBlueprint(name='mynote', text='Note text') | ||
result = bp.build() | ||
self.assertIsInstance(result, StickyNote) | ||
self.assertEqual(result.name, bp.name) | ||
self.assertEqual(result.text, bp.text) | ||
|
||
def test_preformat_not_needed(self): | ||
oneline = 'One line of note text' | ||
multiline = 'Multiline\nnote\n\ntext' | ||
long_line = 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Aspernatur quidem adipisci, impedit, ut illum dolorum consequatur odio voluptate numquam ea itaque excepturi, a libero placeat corrupti. Amet beatae suscipit necessitatibus. Ea expedita explicabo iste quae rem aliquam minus cumque eveniet enim delectus, alias aut impedit quaerat quia ex, aliquid sint amet iusto rerum! Sunt deserunt ea saepe corrupti officiis. Assumenda.' | ||
|
||
bp = StickyNoteBlueprint(name='mynote', text=oneline) | ||
self.assertEqual(bp.name, bp.name) | ||
self.assertEqual(bp._preformat_text(), oneline) | ||
bp = StickyNoteBlueprint(name='mynote', text=multiline) | ||
self.assertEqual(bp.name, bp.name) | ||
self.assertEqual(bp._preformat_text(), multiline) | ||
bp = StickyNoteBlueprint(name='mynote', text=long_line) | ||
self.assertEqual(bp.name, bp.name) | ||
self.assertEqual(bp._preformat_text(), long_line) | ||
|
||
def test_preformat_needed(self): | ||
uniform_indentation = ' line1\n line2\n line3' | ||
varied_indentation = ' line1\n line2\n\n line3' | ||
empty_lines = '\n\n\n\n\n\n\nline1\nline2\nline3\n\n\n\n\n\n\n' | ||
empty_indented_lines = '\n \n\n \n\n line1\n line2\n line3\n\n\n\n \n\n\n' | ||
|
||
exptected = 'line1\nline2\nline3' | ||
bp = StickyNoteBlueprint(name='mynote', text=uniform_indentation) | ||
self.assertEqual(bp._preformat_text(), exptected) | ||
self.assertEqual(bp.name, bp.name) | ||
|
||
exptected = 'line1\n line2\n\n line3' | ||
bp = StickyNoteBlueprint(name='mynote', text=varied_indentation) | ||
self.assertEqual(bp._preformat_text(), exptected) | ||
self.assertEqual(bp.name, bp.name) | ||
|
||
exptected = 'line1\nline2\nline3' | ||
bp = StickyNoteBlueprint(name='mynote', text=empty_lines) | ||
self.assertEqual(bp._preformat_text(), exptected) | ||
self.assertEqual(bp.name, bp.name) | ||
|
||
exptected = 'line1\nline2\nline3' | ||
bp = StickyNoteBlueprint(name='mynote', text=empty_indented_lines) | ||
self.assertEqual(bp._preformat_text(), exptected) | ||
self.assertEqual(bp.name, bp.name) |
Oops, something went wrong.