Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LimaGroup
Lima-Launcher
Commits
bcb7c942
Commit
bcb7c942
authored
Feb 09, 2022
by
Alejandro Homs Puron
Committed by
Alejandro Homs Puron
Mar 22, 2022
Browse files
[SCRIPTS] Add json_2_tango_res
parent
a8fe953e
Changes
1
Hide whitespace changes
Inline
Side-by-side
scripts/json_2_tango_res
0 → 100755
View file @
bcb7c942
#!/usr/bin/env python
import
re
import
sys
import
argparse
import
json
from
contextlib
import
closing
def
load_json_config
(
f
):
json_str
=
f
.
read
()
data
=
json
.
loads
(
json_str
)
keys
=
[
'executable'
,
'server'
,
'class'
,
'device'
,
'config'
]
missing_keys
=
[
k
for
k
in
keys
if
k
not
in
data
]
if
missing_keys
:
raise
RuntimeError
(
f
'Invalid JSON config: missing keys:
{
missing_keys
}
'
)
json_lines
=
json_str
.
split
(
'
\n
'
)
if
json_lines
[:
1
]
!=
[
'{'
]
or
json_lines
[
-
2
:]
!=
[
'}'
,
''
]:
raise
RuntimeError
(
f
'Invalid JSON string:
{
json_str
}
'
)
config
=
None
indent
=
None
for
l
in
json_lines
[
1
:
-
2
]:
if
config
is
None
:
entry_re
=
re
.
compile
(
r
'(?P<indent>[ \t]+)"(?P<key>[^"]+)":[ \t]+'
r
'("(?P<val>[^"]+)",|(\{))$'
)
m
=
entry_re
.
match
(
l
)
if
not
m
:
raise
RuntimeError
(
f
'Invalid JSON string:
{
json_str
}
'
)
if
m
[
'key'
]
!=
'config'
:
if
m
[
'val'
]
!=
data
[
m
[
'key'
]]:
raise
RuntimeError
(
f
'JSON parse mismatch:
{
l
}
'
)
elif
not
m
.
group
(
5
):
raise
RuntimeError
(
f
'Unexpected JSON config line:
{
l
}
'
)
else
:
indent
=
m
[
'indent'
]
config
=
[
m
.
group
(
5
)]
continue
if
not
l
.
startswith
(
indent
):
raise
RuntimeError
(
f
'Unexpected JSON indent:
{
l
}
'
)
l
=
l
[
len
(
indent
):]
config
.
append
(
l
)
if
l
==
'}'
:
break
data
[
'config'
]
=
config
return
data
def
dump_tango_res
(
data
):
data
[
'klass'
]
=
data
[
'class'
]
out
=
[
'{executable}/{server}/DEVICE/{klass}: "{device}"'
.
format
(
**
data
)]
out
+=
[
''
]
device_line
=
'{device}->config: '
.
format
(
**
data
)
indent
=
' '
*
len
(
device_line
)
config
=
data
[
'config'
]
term
=
',
\\
'
out
+=
[
device_line
+
config
[
0
]
+
term
]
for
l
in
config
[
1
:
-
1
]:
l
=
l
.
replace
(
'"'
,
'
\\
"'
)
out
+=
[
f
'
{
indent
}
"
{
l
}
"
{
term
}
'
]
out
+=
[
indent
+
config
[
-
1
]]
return
'
\n
'
.
join
(
out
)
def
main
():
parser
=
argparse
.
ArgumentParser
(
description
=
(
'Tango DB res -> '
'JSON converter.'
))
parser
.
add_argument
(
'json_in'
,
nargs
=
'?'
,
default
=
'-'
,
help
=
'input JSON file name or "-" for stdin'
)
parser
.
add_argument
(
'res_out'
,
nargs
=
'?'
,
default
=
'-'
,
help
=
'output Tango DB res. file name or "-" for stdout'
)
args
=
parser
.
parse_args
()
out_file
=
sys
.
stdout
if
args
.
res_out
==
'-'
else
open
(
args
.
res_out
,
'wt'
)
with
closing
(
out_file
):
in_file
=
sys
.
stdin
if
args
.
json_in
==
'-'
else
open
(
args
.
json_in
,
'rt'
)
with
closing
(
in_file
):
data
=
load_json_config
(
in_file
)
tango_res
=
dump_tango_res
(
data
)
out_file
.
write
(
tango_res
+
'
\n
'
)
if
__name__
==
'__main__'
:
main
()
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