Skip to content

Commit 33f9542

Browse files
committed
Extend import functionality
1 parent 0d97116 commit 33f9542

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed

src/hsd/dict.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from hsd.common import HSD_ATTRIB_NAME, np, ATTRIB_SUFFIX, HSD_ATTRIB_SUFFIX, HsdError,\
1313
QUOTING_CHARS, SPECIAL_CHARS
1414
from hsd.eventhandler import HsdEventHandler, HsdEventPrinter
15+
from hsd.interrupts import IncludeHsd, IncludeText
1516

1617
_ItemType = Union[float, complex, int, bool, str]
1718

@@ -158,7 +159,10 @@ def add_text(self, text):
158159
if self._curblock or self._data is not None:
159160
msg = "Data appeared in an invalid context"
160161
raise HsdError(msg)
161-
self._data = self._text_to_data(text)
162+
if isinstance(text, IncludeText) or isinstance(text, IncludeHsd):
163+
self._data = text
164+
else:
165+
self._data = self._text_to_data(text)
162166

163167

164168
def _text_to_data(self, txt: str) -> _DataType:
@@ -243,8 +247,14 @@ def walk(self, dictobj):
243247
self._eventhandler.close_tag(key)
244248

245249
else:
250+
nextline = False
246251
self._eventhandler.open_tag(key, attrib, hsdattrib)
247-
self._eventhandler.add_text(_to_text(value))
252+
if isinstance(value, IncludeHsd) or isinstance(value, IncludeText):
253+
text = str(value)
254+
nextline = True
255+
else:
256+
text = _to_text(value)
257+
self._eventhandler.add_text(text, nextline)
248258
self._eventhandler.close_tag(key)
249259

250260

src/hsd/formatter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ def close_tag(self, tagname: str):
9292
self._level -= 1
9393

9494

95-
def add_text(self, text: str):
95+
def add_text(self, text: str, nextline: bool = False):
9696

9797
equal = self._followed_by_equal[-1]
98-
multiline = "\n" in text
98+
multiline = "\n" in text or nextline
9999
if equal is None and not multiline:
100100
if len(self._followed_by_equal) > 1:
101101
equal = not self._followed_by_equal[-2]
@@ -108,7 +108,8 @@ def add_text(self, text: str):
108108
self._indent_level += 1
109109
indentstr = self._indent_level * _INDENT_STR
110110
self._fobj.write(f" {{\n{indentstr}")
111-
text = text.replace("\n", "\n" + indentstr)
111+
if not nextline:
112+
text = text.replace("\n", "\n" + indentstr)
112113

113114
self._fobj.write(text)
114115
self._fobj.write("\n")

src/hsd/interrupts.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#--------------------------------------------------------------------------------------------------#
2+
# hsd-python: package for manipulating HSD-formatted data in Python #
3+
# Copyright (C) 2011 - 2022 DFTB+ developers group #
4+
# Licensed under the BSD 2-clause license. #
5+
#--------------------------------------------------------------------------------------------------#
6+
#
7+
"""
8+
Contains hsd interrupts
9+
"""
10+
11+
from hsd.common import unquote
12+
13+
class IncludeText:
14+
"""class for dealing with text interrupts/inclueds"""
15+
16+
def __init__(self, file):
17+
self.file = unquote(file.strip())
18+
self.operator = "<<<"
19+
20+
def __str__(self):
21+
return self.operator + ' "' + self.file + '"'
22+
23+
class IncludeHsd:
24+
"""class for dealing with hsd interrupts/inclueds"""
25+
26+
def __init__(self, file):
27+
self.file = unquote(file.strip())
28+
self.operator = "<<+"
29+
30+
def __str__(self):
31+
return self.operator + ' "' + self.file + '"'

src/hsd/io.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919

2020
def load(hsdfile: Union[TextIO, str], lower_tag_names: bool = False,
21-
include_hsd_attribs: bool = False, flatten_data: bool = False) -> dict:
21+
include_hsd_attribs: bool = False, flatten_data: bool = False,
22+
include_file: bool = True) -> dict:
2223
"""Loads a file with HSD-formatted data into a Python dictionary
2324
2425
Args:
@@ -36,6 +37,7 @@ def load(hsdfile: Union[TextIO, str], lower_tag_names: bool = False,
3637
flatten_data: Whether multiline data in the HSD input should be
3738
flattened into a single list. Othewise a list of lists is created,
3839
with one list for every line (default).
40+
include_file: Whether files via "<<<"/"<<+" should be included or not
3941
4042
Returns:
4143
Dictionary representing the HSD data.
@@ -45,7 +47,7 @@ def load(hsdfile: Union[TextIO, str], lower_tag_names: bool = False,
4547
"""
4648
dictbuilder = HsdDictBuilder(lower_tag_names=lower_tag_names, flatten_data=flatten_data,
4749
include_hsd_attribs=include_hsd_attribs)
48-
parser = HsdParser(eventhandler=dictbuilder)
50+
parser = HsdParser(eventhandler=dictbuilder, include_file=include_file)
4951
if isinstance(hsdfile, str):
5052
with open(hsdfile, "r") as hsddescr:
5153
parser.parse(hsddescr)
@@ -56,8 +58,8 @@ def load(hsdfile: Union[TextIO, str], lower_tag_names: bool = False,
5658

5759
def load_string(
5860
hsdstr: str, lower_tag_names: bool = False,
59-
include_hsd_attribs: bool = False, flatten_data: bool = False
60-
) -> dict:
61+
include_hsd_attribs: bool = False, flatten_data: bool = False,
62+
include_file: bool = True) -> dict:
6163
"""Loads a string with HSD-formatted data into a Python dictionary.
6264
6365
Args:
@@ -75,6 +77,7 @@ def load_string(
7577
flatten_data: Whether multiline data in the HSD input should be
7678
flattened into a single list. Othewise a list of lists is created,
7779
with one list for every line (default).
80+
include_file: Whether files via "<<<"/"<<+" should be included or not
7881
7982
Returns:
8083
Dictionary representing the HSD data.
@@ -130,7 +133,8 @@ def load_string(
130133
131134
"""
132135
fobj = io.StringIO(hsdstr)
133-
return load(fobj, lower_tag_names, include_hsd_attribs, flatten_data)
136+
return load(fobj, lower_tag_names, include_hsd_attribs, flatten_data,
137+
include_file)
134138

135139

136140
def dump(data: dict, hsdfile: Union[TextIO, str],

src/hsd/parser.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from typing import Optional, TextIO, Union
1111
from hsd import common
1212
from hsd.eventhandler import HsdEventHandler, HsdEventPrinter
13-
13+
from hsd.interrupts import IncludeHsd, IncludeText
1414

1515
SYNTAX_ERROR = 1
1616
UNCLOSED_TAG_ERROR = 2
@@ -50,11 +50,13 @@ class HsdParser:
5050
{'Temperature': 100, 'Temperature.attrib': 'Kelvin'}}}}}
5151
"""
5252

53-
def __init__(self, eventhandler: Optional[HsdEventHandler] = None):
53+
def __init__(self, eventhandler: Optional[HsdEventHandler] = None,
54+
include_file: bool = True):
5455
"""Initializes the parser.
5556
5657
Args:
5758
eventhandler: Instance of the HsdEventHandler class or its children.
59+
include_file: Whether files via "<<<"/"<<+" should be included or not
5860
"""
5961
if eventhandler is None:
6062
self._eventhandler = HsdEventPrinter()
@@ -75,6 +77,7 @@ def __init__(self, eventhandler: Optional[HsdEventHandler] = None):
7577
self._has_child = True # Whether current node has a child already
7678
self._has_text = False # whether current node contains text already
7779
self._oldbefore = "" # buffer for tagname
80+
self._include_file = include_file # Whether files via "<<<"/"<<+" should be included or not
7881

7982

8083
def parse(self, fobj: Union[TextIO, str]):
@@ -216,10 +219,17 @@ def _parse(self, line):
216219
if txtinc:
217220
self._text("".join(self._buffer) + before)
218221
self._buffer = []
219-
self._eventhandler.add_text(self._include_txt(after[2:]))
222+
if self._include_file:
223+
text = self._include_txt(after[2:])
224+
else:
225+
text = IncludeText(after[2:])
226+
self._eventhandler.add_text(text)
220227
break
221228
if hsdinc:
222-
self._include_hsd(after[2:])
229+
if self._include_file:
230+
self._include_hsd(after[2:])
231+
else:
232+
self._eventhandler.add_text(IncludeHsd(after[2:]))
223233
break
224234
self._buffer.append(before + sign)
225235

0 commit comments

Comments
 (0)