Skip to content

Commit

Permalink
Implement HSD-wrappers to manipulate nested content
Browse files Browse the repository at this point in the history
  • Loading branch information
aradi committed May 25, 2023
1 parent 0d97116 commit 405015b
Show file tree
Hide file tree
Showing 6 changed files with 644 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*~
.idea
.env
.vscode
*.pyc
dist
build
Expand Down
6 changes: 3 additions & 3 deletions src/hsd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"""
Toolbox for reading, writing and manipulating HSD-data.
"""
from hsd.common import HSD_ATTRIB_LINE, HSD_ATTRIB_EQUAL, HSD_ATTRIB_SUFFIX,\
HSD_ATTRIB_NAME, HsdError
from hsd.dict import HsdDictBuilder, HsdDictWalker
from hsd.common import HSD_ATTRIB_LINE, HSD_ATTRIB_EQUAL, HSD_ATTRIB_NAME, HsdError
from hsd.dict import ATTRIB_KEY_SUFFIX, HSD_ATTRIB_KEY_SUFFIX, HsdDictBuilder, HsdDictWalker
from hsd.eventhandler import HsdEventHandler, HsdEventPrinter
from hsd.formatter import HsdFormatter
from hsd.io import load, load_string, dump, dump_string
from hsd.parser import HsdParser
from hsd.wrappers import HsdDict, HsdList, HsdValue

__version__ = '0.1'
6 changes: 0 additions & 6 deletions src/hsd/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ def unquote(txt):
# Name for default attribute (when attribute name is not specified)
DEFAULT_ATTRIBUTE = "unit"

# Suffix to mark attribute
ATTRIB_SUFFIX = ".attrib"

# Suffix to mark hsd processing attributes
HSD_ATTRIB_SUFFIX = ".hsdattrib"

# HSD attribute containing the original tag name
HSD_ATTRIB_NAME = "name"

Expand Down
29 changes: 18 additions & 11 deletions src/hsd/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
"""
import re
from typing import List, Tuple, Union
from hsd.common import HSD_ATTRIB_NAME, np, ATTRIB_SUFFIX, HSD_ATTRIB_SUFFIX, HsdError,\
QUOTING_CHARS, SPECIAL_CHARS
from hsd.common import HSD_ATTRIB_NAME, np, HsdError, QUOTING_CHARS, SPECIAL_CHARS
from hsd.eventhandler import HsdEventHandler, HsdEventPrinter


# Dictionary key suffix to mark attribute
ATTRIB_KEY_SUFFIX = ".attrib"

# Dictionary keysuffix to mark hsd processing attributes
HSD_ATTRIB_KEY_SUFFIX = ".hsdattrib"


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

_DataType = Union[_ItemType, List[_ItemType]]
Expand Down Expand Up @@ -130,26 +137,26 @@ def close_tag(self, tagname):
parentblock[key] = [{None: prevcont}, self._curblock]

if attrib and prevcont is None:
parentblock[key + ATTRIB_SUFFIX] = attrib
parentblock[key + ATTRIB_KEY_SUFFIX] = attrib
elif prevcont is not None:
prevattrib = parentblock.get(key + ATTRIB_SUFFIX)
prevattrib = parentblock.get(key + ATTRIB_KEY_SUFFIX)
if isinstance(prevattrib, list):
prevattrib.append(attrib)
else:
parentblock[key + ATTRIB_SUFFIX] = [prevattrib, attrib]
parentblock[key + ATTRIB_KEY_SUFFIX] = [prevattrib, attrib]

if self._include_hsd_attribs:
if self._lower_tag_names:
hsdattrib = {} if hsdattrib is None else hsdattrib
hsdattrib[HSD_ATTRIB_NAME] = tagname
if prevcont is None:
parentblock[key + HSD_ATTRIB_SUFFIX] = hsdattrib
parentblock[key + HSD_ATTRIB_KEY_SUFFIX] = hsdattrib
else:
prevhsdattrib = parentblock.get(key + HSD_ATTRIB_SUFFIX)
prevhsdattrib = parentblock.get(key + HSD_ATTRIB_KEY_SUFFIX)
if isinstance(prevhsdattrib, list):
prevhsdattrib.append(hsdattrib)
else:
parentblock[key + HSD_ATTRIB_SUFFIX] = [prevhsdattrib, hsdattrib]
parentblock[key + HSD_ATTRIB_KEY_SUFFIX] = [prevhsdattrib, hsdattrib]
self._curblock = parentblock
self._data = None

Expand Down Expand Up @@ -219,11 +226,11 @@ def walk(self, dictobj):

for key, value in dictobj.items():

if key.endswith(ATTRIB_SUFFIX) or key.endswith(HSD_ATTRIB_SUFFIX):
if key.endswith(ATTRIB_KEY_SUFFIX) or key.endswith(HSD_ATTRIB_KEY_SUFFIX):
continue

hsdattrib = dictobj.get(key + HSD_ATTRIB_SUFFIX)
attrib = dictobj.get(key + ATTRIB_SUFFIX)
hsdattrib = dictobj.get(key + HSD_ATTRIB_KEY_SUFFIX)
attrib = dictobj.get(key + ATTRIB_KEY_SUFFIX)

if isinstance(value, dict):

Expand Down
Loading

0 comments on commit 405015b

Please sign in to comment.