protonabu 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- protonabu/__init__.py +54 -0
- protonabu/adapter/__init__.py +18 -0
- protonabu/adapter/adapterFactory.py +99 -0
- protonabu/adapter/addendum.nabu +36 -0
- protonabu/adapter/contrib/__init__.py +1 -0
- protonabu/adapter/snippetAdapter.py +99 -0
- protonabu/adapter/std/JsonSnippetAdapter/__init__.py +1 -0
- protonabu/adapter/std/JsonSnippetAdapter/jsonSnippetAdapter.py +235 -0
- protonabu/adapter/std/MarkdownSnippetAdapter/__init__.py +1 -0
- protonabu/adapter/std/MarkdownSnippetAdapter/markdownSnippetAdapter.py +94 -0
- protonabu/adapter/std/XmlSnippetAdapter/__init__.py +1 -0
- protonabu/adapter/std/XmlSnippetAdapter/xmlSnippetAdapter.py +278 -0
- protonabu/adapter/std/__init__.py +1 -0
- protonabu/adapter/testSnippetAdapters.py +257 -0
- protonabu/addendum.nabu +67 -0
- protonabu/config.py +198 -0
- protonabu/demoApp/__init__.py +0 -0
- protonabu/demoApp/__main__.py +62 -0
- protonabu/demoApp/doc/schema/publishSchema.py +35 -0
- protonabu/demoApp/example/demo_site.facts +101 -0
- protonabu/demoApp/htmlBuilder.py +482 -0
- protonabu/demoApp/pdfExporter.py +239 -0
- protonabu/demoApp/schema.facts +63 -0
- protonabu/demoApp/siteGenerator.py +424 -0
- protonabu/demoApp/siteModel.py +443 -0
- protonabu/demoApp/siteParser.py +387 -0
- protonabu/demoApp/styles.css +441 -0
- protonabu/demoApp/testSiteGenerator.py +121 -0
- protonabu/factParser.py +766 -0
- protonabu/factSnippet.py +479 -0
- protonabu/primitiveTokens.py +294 -0
- protonabu/protonabuParser.py +866 -0
- protonabu/protonabuSchema.py +655 -0
- protonabu/rawFact.py +717 -0
- protonabu/rawFactTestData.py +56 -0
- protonabu/reportSchema.py +326 -0
- protonabu/testLoops.py +195 -0
- protonabu/testProtonabuParser.py +819 -0
- protonabu/testRawFact.py +94 -0
- protonabu/testStringUtil.py +215 -0
- protonabu/testing/__init__.py +6 -0
- protonabu/testing/addendum.nabu +10 -0
- protonabu/testing/testing.py +305 -0
- protonabu/tokenizedFact.py +407 -0
- protonabu/util/__init__.py +20 -0
- protonabu/util/addendum.nabu +14 -0
- protonabu/util/error.py +190 -0
- protonabu/util/loops.py +185 -0
- protonabu/util/progress.py +94 -0
- protonabu/util/stringUtil.py +922 -0
- protonabu-1.0.0.dist-info/METADATA +75 -0
- protonabu-1.0.0.dist-info/RECORD +55 -0
- protonabu-1.0.0.dist-info/WHEEL +5 -0
- protonabu-1.0.0.dist-info/licenses/LICENSE +21 -0
- protonabu-1.0.0.dist-info/top_level.txt +1 -0
protonabu/__init__.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
""" Protonabu DSL Infrastructure Version """
|
|
2
|
+
__version__ = '1.0.0'
|
|
3
|
+
|
|
4
|
+
from .rawFact import RawFact
|
|
5
|
+
from .tokenizedFact import TokenizedFact
|
|
6
|
+
from .factSnippet import (
|
|
7
|
+
ProtonabuSnippet,
|
|
8
|
+
ProtonabuSnippetFromFile,
|
|
9
|
+
ProtonabuSnippetFromOpenFile,
|
|
10
|
+
ProtonabuSnippetInMemory
|
|
11
|
+
)
|
|
12
|
+
from .protonabuSchema import (
|
|
13
|
+
ProtonabuSchema,
|
|
14
|
+
Label,
|
|
15
|
+
ProtonabuParsingStack
|
|
16
|
+
)
|
|
17
|
+
from .protonabuParser import (
|
|
18
|
+
SnippetParser,
|
|
19
|
+
ProtonabuParseDispatcher,
|
|
20
|
+
ProtonabuParser,
|
|
21
|
+
ImportingProtonabuParser
|
|
22
|
+
)
|
|
23
|
+
from .util.error import (
|
|
24
|
+
Error,
|
|
25
|
+
InternalError,
|
|
26
|
+
ParsingError,
|
|
27
|
+
FactFileError
|
|
28
|
+
)
|
|
29
|
+
from .util.progress import (
|
|
30
|
+
IProgressIndicator,
|
|
31
|
+
DummyProgressIndicator
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
'RawFact',
|
|
36
|
+
'TokenizedFact',
|
|
37
|
+
'ProtonabuSnippet',
|
|
38
|
+
'ProtonabuSnippetFromFile',
|
|
39
|
+
'ProtonabuSnippetFromOpenFile',
|
|
40
|
+
'ProtonabuSnippetInMemory',
|
|
41
|
+
'ProtonabuSchema',
|
|
42
|
+
'Label',
|
|
43
|
+
'ProtonabuParsingStack',
|
|
44
|
+
'SnippetParser',
|
|
45
|
+
'ProtonabuParseDispatcher',
|
|
46
|
+
'ProtonabuParser',
|
|
47
|
+
'ImportingProtonabuParser',
|
|
48
|
+
'Error',
|
|
49
|
+
'InternalError',
|
|
50
|
+
'ParsingError',
|
|
51
|
+
'FactFileError',
|
|
52
|
+
'IProgressIndicator',
|
|
53
|
+
'DummyProgressIndicator',
|
|
54
|
+
]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
""" Package: Protonabu Snippet Adapter
|
|
2
|
+
- Author: Avner Ben
|
|
3
|
+
- Created: 17-May-2026
|
|
4
|
+
- Generator: Antigravity (Claude Opus 4.6)
|
|
5
|
+
- Generated: 17-May-2026
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .adapterFactory import snippetAdapterFactory
|
|
9
|
+
|
|
10
|
+
# to register standard Snippet Adapters
|
|
11
|
+
from .std.MarkdownSnippetAdapter.markdownSnippetAdapter import MarkdownSnippetAdapter
|
|
12
|
+
from .std.JsonSnippetAdapter.jsonSnippetAdapter import JsonSnippetAdapter
|
|
13
|
+
from .std.XmlSnippetAdapter.xmlSnippetAdapter import XmlSnippetAdapter
|
|
14
|
+
|
|
15
|
+
snippetAdapterFactory.register(MarkdownSnippetAdapter())
|
|
16
|
+
snippetAdapterFactory.register(JsonSnippetAdapter())
|
|
17
|
+
snippetAdapterFactory.register(XmlSnippetAdapter())
|
|
18
|
+
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
""" Module: Protonabu Snippet Adapter Factory
|
|
2
|
+
- Author: Avner Ben
|
|
3
|
+
- Created: 17-May-2026
|
|
4
|
+
- Generator: Antigravity (Claude Opus 4.6)
|
|
5
|
+
- Generated: 17-May-2026
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Optional, TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
from ..util.error import InternalError
|
|
13
|
+
# [Additional]
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from ..factSnippet import ProtonabuSnippet
|
|
16
|
+
# [Additional]
|
|
17
|
+
from .snippetAdapter import FactSnippetAdapter
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class SnippetAdapterFactory:
|
|
21
|
+
""" Snippet Adapter Factory
|
|
22
|
+
- Purpose: to provide Snippet Adapter by file extension
|
|
23
|
+
- Stereotype: Singleton
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self):
|
|
27
|
+
""" to INITIALIZE Snippet Adapter Factory
|
|
28
|
+
- Exported
|
|
29
|
+
"""
|
|
30
|
+
# Contains [0-N by Reference]: Snippet Adapter [Role "adapters]
|
|
31
|
+
# - Key: file extension
|
|
32
|
+
self.adapters: dict[str, FactSnippetAdapter] = {}
|
|
33
|
+
|
|
34
|
+
def register(self, adapter: FactSnippetAdapter):
|
|
35
|
+
""" to register Snippet Adapter
|
|
36
|
+
- Exported
|
|
37
|
+
- definitive [Part]
|
|
38
|
+
"""
|
|
39
|
+
# [Scan "file extensions"]: to register adapter
|
|
40
|
+
for ext in adapter.fileExtensions:
|
|
41
|
+
normalizedExt = ext if ext.startswith('.') else f'.{ext}'
|
|
42
|
+
# [Esc "Error"]: duplicate registration
|
|
43
|
+
if normalizedExt in self.adapters:
|
|
44
|
+
raise InternalError('Snippet adapter already registered for extension', [
|
|
45
|
+
('Extension', normalizedExt),
|
|
46
|
+
('Existing', type(self.adapters[normalizedExt]).__name__),
|
|
47
|
+
('New', type(adapter).__name__)
|
|
48
|
+
])
|
|
49
|
+
self.adapters[normalizedExt] = adapter
|
|
50
|
+
|
|
51
|
+
def getAdapter(self, fileName: str) -> Optional[FactSnippetAdapter]:
|
|
52
|
+
""" to get Snippet Adapter by file name
|
|
53
|
+
- Exported
|
|
54
|
+
- Output [Opt None]: Snippet Adapter
|
|
55
|
+
"""
|
|
56
|
+
ext = Path(fileName).suffix.lower()
|
|
57
|
+
return self.adapters.get(ext)
|
|
58
|
+
|
|
59
|
+
def hasAdapter(self, fileName: str) -> bool:
|
|
60
|
+
""" to tell if Snippet Adapter is registered for file name
|
|
61
|
+
- Exported
|
|
62
|
+
"""
|
|
63
|
+
ext = Path(fileName).suffix.lower()
|
|
64
|
+
return ext in self.adapters
|
|
65
|
+
|
|
66
|
+
def adaptInput(self,
|
|
67
|
+
snippet: ProtonabuSnippet
|
|
68
|
+
) -> ProtonabuSnippet:
|
|
69
|
+
""" to adapt snippet input using registered adapter
|
|
70
|
+
- Exported
|
|
71
|
+
- Input: raw snippet
|
|
72
|
+
- Output: native snippet
|
|
73
|
+
- definitive [Product]
|
|
74
|
+
"""
|
|
75
|
+
# [Msg]: to get Snippet Adapter by file name
|
|
76
|
+
adapter = self.getAdapter(snippet.fileName)
|
|
77
|
+
# [Esc Done]: no adapter registered
|
|
78
|
+
if not adapter: return snippet
|
|
79
|
+
# [Msg]: to adapt snippet input from foreign format
|
|
80
|
+
return adapter.adaptInput(snippet)
|
|
81
|
+
|
|
82
|
+
def adaptOutput(self,
|
|
83
|
+
snippet: ProtonabuSnippet
|
|
84
|
+
) -> ProtonabuSnippet:
|
|
85
|
+
""" to adapt snippet output using registered adapter
|
|
86
|
+
- Exported
|
|
87
|
+
- Input: nativesnippet
|
|
88
|
+
- Output: adapted snippet
|
|
89
|
+
"""
|
|
90
|
+
# [Msg]: to get Snippet Adapter by file name
|
|
91
|
+
adapter = self.getAdapter(snippet.fileName)
|
|
92
|
+
# [Esc Done]: no adapter registered
|
|
93
|
+
if not adapter: return snippet
|
|
94
|
+
# [Msg]: to adapt snippet output to foreign format
|
|
95
|
+
return adapter.adaptOutput(snippet)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# to INITIALIZE Snippet Adapter Factory
|
|
99
|
+
snippetAdapterFactory = SnippetAdapterFactory()
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
PRODUCT: Snippet Adapter Package
|
|
2
|
+
ID
|
|
3
|
+
PROPERTY [AUTHOR]: Avner Ben
|
|
4
|
+
BULLET [CREATED]: 17-May-2026
|
|
5
|
+
BULLET [IMPROVED]: 24-Aug-2026
|
|
6
|
+
PROPERTY [GENERATOR]: Antigravity (Gemini 3.7 Flash)
|
|
7
|
+
BULLET [IMPROVED]: 24-Aug-2026
|
|
8
|
+
|
|
9
|
+
USE CASE: to adapt snippet input using registered adapter
|
|
10
|
+
TITLE: Adapting Snippet Input from Foreign Format
|
|
11
|
+
REQUIRES: to get Snippet Adapter by file name
|
|
12
|
+
REQUIRES: to adapt snippet input from foreign format
|
|
13
|
+
|
|
14
|
+
USE CASE: to adapt snippet output using registered adapter
|
|
15
|
+
TITLE: Adapting Snippet Output to Foreign Format
|
|
16
|
+
REQUIRES: to get Snippet Adapter by file name
|
|
17
|
+
REQUIRES: to adapt snippet output to foreign format
|
|
18
|
+
|
|
19
|
+
USE CASE: to register Snippet Adapter
|
|
20
|
+
TITLE: Registering Snippet Adapter in Factory
|
|
21
|
+
REQUIRES: to register Snippet Adapter
|
|
22
|
+
|
|
23
|
+
USE CASE: to perform Snippet Adapter Test Cases
|
|
24
|
+
TITLE: Performing Snippet Adapter Test Cases
|
|
25
|
+
SCENARIO: Full Snippet Adapter test
|
|
26
|
+
IN SCENARIO [Full Snippet Adapter test]: to INITIALIZE Snippet Adapter Test Case [FOR to INITIALIZE test case]
|
|
27
|
+
REQUIRES: to perform Unit Test
|
|
28
|
+
SCANNING: "test cases"
|
|
29
|
+
REQUIRES: to INITIALIZE Snippet Adapter Test Case
|
|
30
|
+
REQUIRES: to perform Snippet Adapter test case
|
|
31
|
+
REQUIRES: to test Snippet Adapter factory registration
|
|
32
|
+
REQUIRES: to test Markdown input line adaptation
|
|
33
|
+
REQUIRES: to test Markdown output line adaptation
|
|
34
|
+
REQUIRES: to test full Markdown snippet iteration
|
|
35
|
+
REQUIRES: to test JSON round-trip
|
|
36
|
+
REQUIRES: to test XML round-trip
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# contrib adapter extensions
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
""" Module: Protonabu Snippet Adapter
|
|
2
|
+
- Author: Avner Ben
|
|
3
|
+
- Created: 17-May-2026
|
|
4
|
+
- Generator: Antigravity (Claude Opus 4.6)
|
|
5
|
+
- Generated: 17-May-2026
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from abc import ABC, abstractmethod
|
|
9
|
+
from typing import Iterator
|
|
10
|
+
|
|
11
|
+
# [Additional]
|
|
12
|
+
from ..factSnippet import ProtonabuSnippet
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class SnippetAdapterSource(ProtonabuSnippet.ISource, ABC):
|
|
16
|
+
""" Line Adapter Fact Stream
|
|
17
|
+
- Purpose: to adapt Fact Line Stream (such as markdown) for foreign protonabu native format
|
|
18
|
+
- Description: Wraps a concrete ISource, intercepting line-level
|
|
19
|
+
I/O to perform format-specific transformations (e.g., stripping
|
|
20
|
+
or prepending Markdown bullet decoration). Subclasses implement
|
|
21
|
+
the raw-line protocol by overriding toNative and toForeign.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, wrapped: ProtonabuSnippet.ISource):
|
|
25
|
+
""" to INITIALIZE Line Adapter Fact Stream
|
|
26
|
+
- Exported
|
|
27
|
+
"""
|
|
28
|
+
# Contains [1:1 By Reference]: Fact Stream
|
|
29
|
+
self.wrapped: ProtonabuSnippet.ISource = wrapped
|
|
30
|
+
|
|
31
|
+
@abstractmethod
|
|
32
|
+
def toNative(self, line: str) -> str:
|
|
33
|
+
""" to import fact-line thru <SUBSTITUTE Line> Adapter Fact Stream
|
|
34
|
+
- Exported
|
|
35
|
+
- definitive [Part]
|
|
36
|
+
- Input: Foreign format line
|
|
37
|
+
- Output: Native format line
|
|
38
|
+
"""
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
@abstractmethod
|
|
42
|
+
def toForeign(self, line: str) -> str:
|
|
43
|
+
""" to export fact-line thru <SUBSTITUTE Line> Adapter Fact Stream
|
|
44
|
+
- Exported
|
|
45
|
+
- Input: Native format line
|
|
46
|
+
- Output: Foreign format line
|
|
47
|
+
"""
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
def add(self, fact: str):
|
|
51
|
+
""" to append line to <Line Adapter> Fact Stream
|
|
52
|
+
- Exported
|
|
53
|
+
"""
|
|
54
|
+
# [Msg]: to export fact-line thru <SUBSTITUTE Line> Adapter Fact Stream
|
|
55
|
+
# [Msg]: to append line to Fact Stream
|
|
56
|
+
self.wrapped.add(self.toForeign(fact))
|
|
57
|
+
|
|
58
|
+
def __iter__(self) -> Iterator[str]:
|
|
59
|
+
""" to TRAVERSE <Line Adapter> Fact Stream
|
|
60
|
+
- Exported
|
|
61
|
+
- definitive
|
|
62
|
+
"""
|
|
63
|
+
# [Scan]: to TRAVERSE Fact Stream
|
|
64
|
+
for line in self.wrapped:
|
|
65
|
+
# [Msg]: to import fact-line thru <SUBSTITUTE Line> Adapter Fact Stream
|
|
66
|
+
yield self.toNative(line)
|
|
67
|
+
|
|
68
|
+
def close(self):
|
|
69
|
+
""" to close <Line Adapter> Fact Stream
|
|
70
|
+
- Exported
|
|
71
|
+
"""
|
|
72
|
+
# [Msg]: to close Fact Stream
|
|
73
|
+
self.wrapped.close()
|
|
74
|
+
|
|
75
|
+
class FactSnippetAdapter(ABC):
|
|
76
|
+
""" Fact Snippet Adapter
|
|
77
|
+
- Purpose: to adapt Protonabu Fact Snippet to and from foreign format
|
|
78
|
+
- Stereotype: Abstract
|
|
79
|
+
"""
|
|
80
|
+
fileExtensions: list[str] = []
|
|
81
|
+
|
|
82
|
+
@abstractmethod
|
|
83
|
+
def adaptInput(self, snippet: ProtonabuSnippet) -> ProtonabuSnippet:
|
|
84
|
+
""" to import facts thru <SUBSTITUTE Fact> Snippet Adapter
|
|
85
|
+
- Exported
|
|
86
|
+
- Input: foreign format snippet
|
|
87
|
+
- Output: native format snippet
|
|
88
|
+
- definitive [Part]
|
|
89
|
+
"""
|
|
90
|
+
pass
|
|
91
|
+
|
|
92
|
+
@abstractmethod
|
|
93
|
+
def adaptOutput(self, snippet: ProtonabuSnippet) -> ProtonabuSnippet:
|
|
94
|
+
""" to export facts thru <SUBSTITUTE Fact> Snippet Adapter
|
|
95
|
+
- Exported
|
|
96
|
+
- Input: native format snippet
|
|
97
|
+
- Output: foreign format snippet
|
|
98
|
+
"""
|
|
99
|
+
pass
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# JsonSnippetAdapter package
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
""" Module: JSON Snippet Adapter
|
|
2
|
+
- Author: Avner Ben
|
|
3
|
+
- Created: 17-May-2026
|
|
4
|
+
- Generator: Antigravity (Claude Opus 4.6)
|
|
5
|
+
- Generated: 17-May-2026
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
from typing import Iterator
|
|
10
|
+
|
|
11
|
+
from ....util.error import Error
|
|
12
|
+
# [Additional]
|
|
13
|
+
from ....rawFact import RawFact, EditDirective
|
|
14
|
+
# [Additional]
|
|
15
|
+
from ....factSnippet import ProtonabuSnippet
|
|
16
|
+
# [Additional]
|
|
17
|
+
from ...snippetAdapter import FactSnippetAdapter
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class JsonAdapterSource(ProtonabuSnippet.ISource):
|
|
21
|
+
""" JSON Fact Stream
|
|
22
|
+
- Purpose: to adapt Fact Stream for JSON format
|
|
23
|
+
- Description: On input, parses JSON and yields reconstructed
|
|
24
|
+
Nabu fact lines. On output, collects Nabu fact lines and
|
|
25
|
+
serializes them as JSON on close.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, wrapped: ProtonabuSnippet.ISource, isInput: bool = True):
|
|
29
|
+
""" to INITIALIZE JSON Fact Stream
|
|
30
|
+
- Input: wrapped
|
|
31
|
+
- Input [Opt "True"]: is Input
|
|
32
|
+
"""
|
|
33
|
+
# Contains [1:1 By Reference]: Fact Stream
|
|
34
|
+
self.wrapped: ProtonabuSnippet.ISource = wrapped
|
|
35
|
+
self.isInput: bool = isInput
|
|
36
|
+
self.outputLines: list[str] = []
|
|
37
|
+
|
|
38
|
+
# -- JSON ↔ RawFact conversion utilities --
|
|
39
|
+
|
|
40
|
+
@staticmethod
|
|
41
|
+
def _factToDict(fact: RawFact) -> dict:
|
|
42
|
+
""" to convert Raw Fact to JSON-serializable dictionary
|
|
43
|
+
- Input: Raw Fact
|
|
44
|
+
- Output: JSON-serializable dictionary
|
|
45
|
+
- definitive [Part]
|
|
46
|
+
"""
|
|
47
|
+
# to prepare JSON fact label
|
|
48
|
+
result = {'label': fact.label}
|
|
49
|
+
# [Opt]: to add label modifiers to JSON fact
|
|
50
|
+
if fact.labelModifiers:
|
|
51
|
+
result['labelModifiers'] = fact.labelModifiers
|
|
52
|
+
# [Opt]: to add argument to JSON fact
|
|
53
|
+
if fact.arg:
|
|
54
|
+
result['argument'] = fact.arg
|
|
55
|
+
# [Opt]: to add argument modifiers to JSON fact
|
|
56
|
+
if fact.argModifiers:
|
|
57
|
+
result['argModifiers'] = fact.argModifiers
|
|
58
|
+
# [Opt]: to add comment to JSON fact
|
|
59
|
+
if fact.comment:
|
|
60
|
+
result['comment'] = fact.comment
|
|
61
|
+
# [Opt]: to add edit directive to JSON fact
|
|
62
|
+
if fact.editDirective:
|
|
63
|
+
result['editDirective'] = fact.editDirective.toSymbol()
|
|
64
|
+
return result
|
|
65
|
+
|
|
66
|
+
@staticmethod
|
|
67
|
+
def _dictToFact(d: dict, rank: int) -> RawFact:
|
|
68
|
+
""" to convert JSON fact dictionary to Raw Fact
|
|
69
|
+
- Input: JSON fact dictionary
|
|
70
|
+
- Output: Raw Fact
|
|
71
|
+
- Input: rank
|
|
72
|
+
"""
|
|
73
|
+
# [Msg]: to initialize Raw Fact from JSON fact dictionary
|
|
74
|
+
fact = RawFact(
|
|
75
|
+
label=d.get('label', ''),
|
|
76
|
+
labelModifiers=d.get('labelModifiers'),
|
|
77
|
+
arg=d.get('argument'),
|
|
78
|
+
argModifiers=d.get('argModifiers')
|
|
79
|
+
)
|
|
80
|
+
# [Opt]: to convert persistence symbol to enum value
|
|
81
|
+
if 'editDirective' in d:
|
|
82
|
+
fact.editDirective = EditDirective.fromSymbol(d['editDirective'])
|
|
83
|
+
# [Opt]: to convert JSON comment to string
|
|
84
|
+
if 'comment' in d:
|
|
85
|
+
fact.comment = d['comment']
|
|
86
|
+
fact.rank = rank
|
|
87
|
+
return fact
|
|
88
|
+
|
|
89
|
+
@staticmethod
|
|
90
|
+
def _flattenJson(node: dict, rank: int = 1) -> list[RawFact]:
|
|
91
|
+
""" to flatten JSON tree to list of Raw Facts
|
|
92
|
+
- Input: JSON fact dictionary
|
|
93
|
+
- Output: List of Raw Facts
|
|
94
|
+
- Input [Opt "1"]: rank
|
|
95
|
+
"""
|
|
96
|
+
facts = []
|
|
97
|
+
# [Msg]: to convert JSON fact dictionary to Raw Fact
|
|
98
|
+
fact = JsonAdapterSource._dictToFact(node, rank)
|
|
99
|
+
facts.append(fact)
|
|
100
|
+
# [Msg; Scan]: to flatten JSON tree to list of Raw Facts
|
|
101
|
+
for child in node.get('children', []):
|
|
102
|
+
facts.extend(JsonAdapterSource._flattenJson(child, rank + 1))
|
|
103
|
+
return facts
|
|
104
|
+
|
|
105
|
+
# -- ISource implementation --
|
|
106
|
+
|
|
107
|
+
def add(self, fact: str):
|
|
108
|
+
""" to append line to <JSON> Fact Stream
|
|
109
|
+
- Input: fact
|
|
110
|
+
"""
|
|
111
|
+
self.outputLines.append(fact)
|
|
112
|
+
|
|
113
|
+
def __iter__(self) -> Iterator[str]:
|
|
114
|
+
""" to TRAVERSE <JSON> Fact Stream
|
|
115
|
+
- Output: result
|
|
116
|
+
"""
|
|
117
|
+
# to collect all JSON text from wrapped source
|
|
118
|
+
text = '\n'.join(line.rstrip('\n').rstrip('\r') for line in self.wrapped)
|
|
119
|
+
# [Guard]: to parse JSON from string
|
|
120
|
+
try:
|
|
121
|
+
data = json.loads(text)
|
|
122
|
+
# [Esc Error]: "Invalid JSON in snippet"
|
|
123
|
+
except json.JSONDecodeError as e:
|
|
124
|
+
raise Error('Invalid JSON in snippet', [('Error', str(e))])
|
|
125
|
+
# [Opt]: to normalize to list of root facts
|
|
126
|
+
if isinstance(data, dict):
|
|
127
|
+
data = [data]
|
|
128
|
+
# [Scan]: to flatten JSON tree to list of Raw Facts
|
|
129
|
+
for root in data:
|
|
130
|
+
# [Scan; Scan]: to handle fact (json Snippet Adapter at 0129)
|
|
131
|
+
for fact in self._flattenJson(root):
|
|
132
|
+
for line in fact.toCompositeFact():
|
|
133
|
+
yield line
|
|
134
|
+
|
|
135
|
+
def close(self):
|
|
136
|
+
""" to close <JSON> Fact Stream
|
|
137
|
+
"""
|
|
138
|
+
# [Opt]: to reconstruct fact hierarchy from collected output lines
|
|
139
|
+
if self.outputLines:
|
|
140
|
+
facts = []
|
|
141
|
+
# [Scan "output lines"]: to prepare JSON fact lines for printout
|
|
142
|
+
for line in self.outputLines:
|
|
143
|
+
# [Esc Once]: blank line
|
|
144
|
+
if not line.strip(): continue
|
|
145
|
+
# [Msg]: to INITIALIZE Raw Fact
|
|
146
|
+
fact = RawFact()
|
|
147
|
+
# [Msg]: to parse one Labeled Fact raw elements from Fact stream
|
|
148
|
+
fact.fromCompositeFact(line)
|
|
149
|
+
# [Opt]: to pick labeled fact for JSON output
|
|
150
|
+
if fact.label:
|
|
151
|
+
facts.append(fact)
|
|
152
|
+
# [Msg]: to build JSON tree from flat list of ranked Raw Facts
|
|
153
|
+
root = self._buildTree(facts)
|
|
154
|
+
# [Msg]: to serialize JSON tree to string
|
|
155
|
+
jsonText = json.dumps(root, indent=2, ensure_ascii=False)
|
|
156
|
+
# [Scan "jsonText"]: to write JSON line to wrapped source
|
|
157
|
+
for line in jsonText.split('\n'):
|
|
158
|
+
self.wrapped.add(line)
|
|
159
|
+
# [Msg]: to close Fact Stream
|
|
160
|
+
self.wrapped.close()
|
|
161
|
+
|
|
162
|
+
@staticmethod
|
|
163
|
+
def _buildTree(facts: list[RawFact]) -> list[dict]:
|
|
164
|
+
""" to build JSON tree from flat list of ranked Raw Facts
|
|
165
|
+
- Input: list of ranked Raw Facts
|
|
166
|
+
- Output: JSON tree
|
|
167
|
+
"""
|
|
168
|
+
# [Esc Done]: empty facts list
|
|
169
|
+
if not facts: return []
|
|
170
|
+
result = []
|
|
171
|
+
stack: list[tuple[int, dict]] = []
|
|
172
|
+
# [Scan "facts"]: to build JSON tree from flat list of ranked Raw Facts
|
|
173
|
+
for fact in facts:
|
|
174
|
+
# [Msg]: to convert Raw Fact to JSON-serializable dictionary
|
|
175
|
+
node = JsonAdapterSource._factToDict(fact)
|
|
176
|
+
# [Rpt]: to pop expired frame from the JSON stack
|
|
177
|
+
while stack and stack[-1][0] >= fact.rank:
|
|
178
|
+
# [Msg]: to pop list
|
|
179
|
+
stack.pop()
|
|
180
|
+
# [Alt]: to insert JSON fact root node
|
|
181
|
+
if not stack:
|
|
182
|
+
result.append(node)
|
|
183
|
+
# [Alt]: to insert as child of parent frame
|
|
184
|
+
else:
|
|
185
|
+
parent = stack[-1][1]
|
|
186
|
+
# [Opt]: to create children list in JSON parent fact
|
|
187
|
+
if 'children' not in parent:
|
|
188
|
+
parent['children'] = []
|
|
189
|
+
parent['children'].append(node)
|
|
190
|
+
# [Msg]: to push fact frame to the JSON stack
|
|
191
|
+
stack.append((fact.rank, node))
|
|
192
|
+
return result
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
class JsonSnippetAdapter(FactSnippetAdapter):
|
|
196
|
+
""" JSON Snippet Adapter
|
|
197
|
+
- Purpose: to adapt Protonabu Fact Snippet for JSON format
|
|
198
|
+
- Description: Converts between standard Nabu fact syntax and
|
|
199
|
+
JSON tree format by wrapping the snippet source
|
|
200
|
+
in a JsonAdapterSource.
|
|
201
|
+
"""
|
|
202
|
+
fileExtensions = ['.json']
|
|
203
|
+
|
|
204
|
+
def adaptInput(self, snippet: ProtonabuSnippet) -> ProtonabuSnippet:
|
|
205
|
+
""" to import facts thru <JSON> Snippet Adapter
|
|
206
|
+
- Exported: Json
|
|
207
|
+
- Input: JSON snippet
|
|
208
|
+
- Output: Native format snippet
|
|
209
|
+
- definitive [Part]
|
|
210
|
+
"""
|
|
211
|
+
# [Msg]: to INITIALIZE Protonabu Snippet
|
|
212
|
+
# - Using: to INITIALIZE Snippet Adapter Source
|
|
213
|
+
# - Using: snippet source
|
|
214
|
+
adapted = ProtonabuSnippet(
|
|
215
|
+
JsonAdapterSource(snippet.source, isInput=True),
|
|
216
|
+
snippet.fileName,
|
|
217
|
+
snippet.isImporting
|
|
218
|
+
)
|
|
219
|
+
return adapted
|
|
220
|
+
|
|
221
|
+
def adaptOutput(self, snippet: ProtonabuSnippet) -> ProtonabuSnippet:
|
|
222
|
+
""" to export facts thru <JSON> Snippet Adapter
|
|
223
|
+
- Exported: Json
|
|
224
|
+
- Input: Native format snippet
|
|
225
|
+
- Output: JSON format snippet
|
|
226
|
+
"""
|
|
227
|
+
# [Msg]: to INITIALIZE Protonabu Snippet
|
|
228
|
+
# - Using: to INITIALIZE Snippet Adapter Source
|
|
229
|
+
# - Using: snippet source
|
|
230
|
+
adapted = ProtonabuSnippet(
|
|
231
|
+
JsonAdapterSource(snippet.source, isInput=False),
|
|
232
|
+
snippet.fileName,
|
|
233
|
+
snippet.isImporting
|
|
234
|
+
)
|
|
235
|
+
return adapted
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# MarkdownSnippetAdapter package
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
""" Module: Markdown Snippet Adapter
|
|
2
|
+
- Author: Avner Ben
|
|
3
|
+
- Created: 17-May-2026
|
|
4
|
+
- Generator: Antigravity (Claude Opus 4.6)
|
|
5
|
+
- Generated: 17-May-2026
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
|
|
10
|
+
# [Additional]
|
|
11
|
+
from ....factSnippet import ProtonabuSnippet
|
|
12
|
+
# [Additional]
|
|
13
|
+
from ...snippetAdapter import FactSnippetAdapter, SnippetAdapterSource
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Pattern to match Markdown bullet list item prefix: "- " or "* "
|
|
17
|
+
_BULLET_PREFIX = re.compile(r'^(\s*)[*\-]\s')
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class MarkdownAdapterSource(SnippetAdapterSource):
|
|
21
|
+
""" Markdown Adapter Fact Stream
|
|
22
|
+
- Purpose: to adapt Fact Stream for Markdown bullet list format
|
|
23
|
+
- Description: On input, strips Markdown bullet prefixes (- or *)
|
|
24
|
+
preserving indentation. On output, prepends Markdown bullet
|
|
25
|
+
prefix (- ) to each line.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def toNative(self, line: str) -> str:
|
|
29
|
+
""" to import fact-line thru <Markdown> Adapter Fact Stream
|
|
30
|
+
- Input: Markdown line
|
|
31
|
+
- Output: Native fact-line
|
|
32
|
+
- definitive [Part]
|
|
33
|
+
"""
|
|
34
|
+
line = line.rstrip('\n').rstrip('\r')
|
|
35
|
+
match = _BULLET_PREFIX.match(line)
|
|
36
|
+
# [Opt]: to strip Markdown bullet prefix
|
|
37
|
+
if match:
|
|
38
|
+
line = match.group(1) + line[match.end():]
|
|
39
|
+
return line
|
|
40
|
+
|
|
41
|
+
def toForeign(self, line: str) -> str:
|
|
42
|
+
""" to export fact-line thru <Markdown> Adapter Fact Stream
|
|
43
|
+
- Input: Native fact-line
|
|
44
|
+
- Output: Markdown fact-line
|
|
45
|
+
"""
|
|
46
|
+
# [Esc]: blank line
|
|
47
|
+
if not line.strip():
|
|
48
|
+
return line
|
|
49
|
+
stripped = line.lstrip()
|
|
50
|
+
indent = line[:len(line) - len(stripped)]
|
|
51
|
+
return f'{indent}- {stripped}'
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class MarkdownSnippetAdapter(FactSnippetAdapter):
|
|
55
|
+
""" Markdown Snippet Adapter
|
|
56
|
+
- Purpose: to adapt Protonabu Fact Snippet for Markdown bullet list format
|
|
57
|
+
- Description: Converts between standard Nabu fact syntax and
|
|
58
|
+
Markdown bullet list format by wrapping the snippet source
|
|
59
|
+
in a MarkdownAdapterSource.
|
|
60
|
+
"""
|
|
61
|
+
fileExtensions = ['.md']
|
|
62
|
+
|
|
63
|
+
def adaptInput(self, snippet: ProtonabuSnippet) -> ProtonabuSnippet:
|
|
64
|
+
""" to import facts thru <Markdown> Snippet Adapter
|
|
65
|
+
- Exported: Markdown
|
|
66
|
+
- Input: Markdown snippet
|
|
67
|
+
- Output: Native format snippet
|
|
68
|
+
- definitive [Part]
|
|
69
|
+
"""
|
|
70
|
+
# [Msg]: to INITIALIZE Protonabu Snippet
|
|
71
|
+
# - Using: to INITIALIZE Snippet Adapter Source
|
|
72
|
+
# - Using: snippet source
|
|
73
|
+
adapted = ProtonabuSnippet(
|
|
74
|
+
MarkdownAdapterSource(snippet.source),
|
|
75
|
+
snippet.fileName,
|
|
76
|
+
snippet.isImporting
|
|
77
|
+
)
|
|
78
|
+
return adapted
|
|
79
|
+
|
|
80
|
+
def adaptOutput(self, snippet: ProtonabuSnippet) -> ProtonabuSnippet:
|
|
81
|
+
""" to export facts thru <Markdown> Snippet Adapter
|
|
82
|
+
- Exported: Markdown
|
|
83
|
+
- Input: Native format snippet
|
|
84
|
+
- Output: Markdown format snippet
|
|
85
|
+
"""
|
|
86
|
+
# [Msg]: to INITIALIZE Protonabu Snippet
|
|
87
|
+
# - Using: to INITIALIZE Snippet Adapter Source
|
|
88
|
+
# - Using: snippet source
|
|
89
|
+
adapted = ProtonabuSnippet(
|
|
90
|
+
MarkdownAdapterSource(snippet.source),
|
|
91
|
+
snippet.fileName,
|
|
92
|
+
snippet.isImporting
|
|
93
|
+
)
|
|
94
|
+
return adapted
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# XmlSnippetAdapter package
|