Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
017c4fd7
Commit
017c4fd7
authored
Mar 26, 2019
by
di68kap
Browse files
- configuration: default values for serialization now constants rather than "magic" strings
parent
30e6dd80
Changes
3
Hide whitespace changes
Inline
Side-by-side
DHParser/configuration.py
View file @
017c4fd7
...
...
@@ -32,7 +32,12 @@ program and before any DHParser-function is invoked.
from
typing
import
Dict
,
Hashable
,
Any
__all__
=
(
'CONFIG_PRESET'
,)
__all__
=
(
'CONFIG_PRESET'
,
'XML_SERIALIZATION'
,
'SXPRESSION_SERIALIZATION'
,
'COMPACT_SERIALIZATION'
,
'JSON_SERIALIZATION'
,
'SERIALIZATIONS'
)
CONFIG_PRESET
=
dict
()
# type: Dict[Hashable, Any]
...
...
@@ -77,15 +82,25 @@ CONFIG_PRESET['max_parser_dropouts'] = 3
# Possible values are:
# 'XML' - output as XML
# 'S-expression' - output as S-expression, i.e. a list-like format
# 'compact' - compact tree output, i.e. children a represented
# on indented lines with no opening or closing tags,
# brackets etc.
# Default values: "compact" for concrete syntax trees and "XML" for
# abstract syntax trees and "S-expression" for any
# other kind of tree.
CONFIG_PRESET
[
'cst_serialization'
]
=
"compact"
CONFIG_PRESET
[
'ast_serialization'
]
=
"XML"
CONFIG_PRESET
[
'default_serialization'
]
=
"S-expression"
# 'compact' - compact tree output, i.e. children a represented on
# indented lines with no opening or closing tags, brackets
# etc.
# 'json' - output in JSON-format. This is probably the least
# readable representation, but useful for serialization, for
# example, to return syntax trees from remote procedure calls.
# Default values: "compact" for concrete syntax trees and "XML" for abstract
# syntax trees and "S-expression" for any other kind of tree.
XML_SERIALIZATION
=
"XML"
SXPRESSION_SERIALIZATION
=
"S-expression"
COMPACT_SERIALIZATION
=
"compact"
JSON_SERIALIZATION
=
"json"
SERIALIZATIONS
=
frozenset
({
XML_SERIALIZATION
,
SXPRESSION_SERIALIZATION
,
COMPACT_SERIALIZATION
,
JSON_SERIALIZATION
})
CONFIG_PRESET
[
'cst_serialization'
]
=
COMPACT_SERIALIZATION
CONFIG_PRESET
[
'ast_serialization'
]
=
XML_SERIALIZATION
CONFIG_PRESET
[
'default_serialization'
]
=
SXPRESSION_SERIALIZATION
# Defines the maximum line length for flattened S-expressions.
# Below this threshold S-expressions will be returned in flattened
...
...
DHParser/syntaxtree.py
View file @
017c4fd7
...
...
@@ -28,6 +28,8 @@ import copy
import
json
from
typing
import
Callable
,
cast
,
Iterator
,
List
,
AbstractSet
,
Set
,
Union
,
Tuple
,
Optional
,
Dict
from
DHParser.configuration
import
SERIALIZATIONS
,
XML_SERIALIZATION
,
SXPRESSION_SERIALIZATION
,
\
COMPACT_SERIALIZATION
,
JSON_SERIALIZATION
from
DHParser.error
import
Error
,
ErrorCode
,
linebreaks
,
line_col
from
DHParser.stringview
import
StringView
from
DHParser.toolkit
import
get_config_value
,
re
...
...
@@ -837,16 +839,17 @@ def serialize(node: Node, how: str = 'default') -> str:
elif
switch
==
'default'
:
switch
=
get_config_value
(
'default_serialization'
).
lower
()
if
switch
==
's-expression'
:
if
switch
==
SXPRESSION_SERIALIZATION
.
lower
()
:
return
node
.
as_sxpr
(
flatten_threshold
=
get_config_value
(
'flatten_sxpr_threshold'
))
elif
switch
==
'xml'
:
elif
switch
==
XML_SERIALIZATION
.
lower
()
:
return
node
.
as_xml
()
elif
switch
==
'json'
:
elif
switch
==
JSON_SERIALIZATION
.
lower
()
:
return
node
.
as_json
()
elif
switch
==
'compact'
:
elif
switch
==
COMPACT_SERIALIZATION
.
lower
()
:
return
node
.
as_sxpr
(
compact
=
True
)
else
:
raise
ValueError
(
'Unknown serialization %s, %s'
%
(
how
,
switch
))
raise
ValueError
(
'Unknown serialization %s. Allowed values are either: %s or : %s'
%
(
how
,
"'ast', 'cst', 'default'"
,
", "
.
join
(
list
(
SERIALIZATIONS
))))
class
FrozenNode
(
Node
):
...
...
DHParser/toolkit.py
View file @
017c4fd7
...
...
@@ -105,7 +105,7 @@ def set_config_value(key: Hashable, value: Any):
Changes a configuration value thread-safely. The configuration
value will be set only for the current thread. In order to
set configuration values for any new thread, add the key and value
to CONFIG_PRESET, before
the
thread is started.
to CONFIG_PRESET, before
any
thread
accessing config values
is started.
:param key: the key (an immutable, usually a string)
:param value: the value
"""
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment