Skip to content

Commit bf17383

Browse files
committed
tools: Add compile-lvs command
Fix: #91
1 parent 64938de commit bf17383

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

src/ndn/bin/tools/__init__.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,42 @@
1616
# limitations under the License.
1717
# -----------------------------------------------------------------------------
1818
import argparse
19-
from . import cmd_fetch_data, cmd_serve_data, cmd_fetch_rdrcontent, cmd_serve_rdrcontent
2019

20+
from . import (
21+
cmd_compile_lvs,
22+
cmd_fetch_data,
23+
cmd_fetch_rdrcontent,
24+
cmd_serve_data,
25+
cmd_serve_rdrcontent,
26+
)
2127

22-
CMD_LIST = '''
28+
29+
CMD_LIST = """
2330
Available commands:
2431
Serve-Data (poke,sd)
2532
Fetch-Data (peek,fd)
2633
Serve-RdrContent (putchunks,src)
2734
Fetch-RdrContent (catchunks,frc)
35+
Compile-Lvs (compile-lvs)
2836
2937
Try '%(prog)s COMMAND -h' for more information on each command
30-
'''
38+
"""
3139

3240

3341
def main():
34-
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, epilog=CMD_LIST)
35-
subparsers = parser.add_subparsers(metavar='COMMAND', help='sub-command to execute')
42+
parser = argparse.ArgumentParser(
43+
formatter_class=argparse.RawDescriptionHelpFormatter, epilog=CMD_LIST
44+
)
45+
subparsers = parser.add_subparsers(metavar="COMMAND", help="sub-command to execute")
3646

3747
cmd_fetch_data.add_parser(subparsers)
3848
cmd_serve_data.add_parser(subparsers)
3949
cmd_fetch_rdrcontent.add_parser(subparsers)
4050
cmd_serve_rdrcontent.add_parser(subparsers)
51+
cmd_compile_lvs.add_parser(subparsers)
4152

4253
args = parser.parse_args()
43-
if 'executor' not in args:
54+
if "executor" not in args:
4455
parser.print_help()
4556
exit(-1)
4657

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (C) 2025-2025 The python-ndn authors
3+
#
4+
# This file is part of python-ndn.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
# -----------------------------------------------------------------------------
18+
import argparse
19+
import os
20+
import sys
21+
22+
import lark
23+
from ndn.app_support.light_versec import compile_lvs, SemanticError
24+
from ndn.app_support.light_versec.binary import *
25+
26+
27+
def add_parser(subparsers):
28+
parser = subparsers.add_parser("Compile-Lvs", aliases=["compile-lvs"])
29+
parser.add_argument(
30+
"-o",
31+
"--output",
32+
metavar="FILE",
33+
default="-",
34+
help="Write compiled result into a file",
35+
)
36+
parser.add_argument(
37+
"input_file",
38+
metavar="FILE",
39+
nargs="?",
40+
default="-",
41+
help="file containing the content of the LVS text, '-' for stdin (default)",
42+
)
43+
parser.set_defaults(executor=execute)
44+
45+
46+
def execute(args: argparse.Namespace):
47+
try:
48+
if args.input_file == "-":
49+
text = sys.stdin.read()
50+
else:
51+
with open(os.path.expandvars(args.input_file), "r") as f:
52+
text = f.read()
53+
except (ValueError, OSError, IndexError):
54+
print("Unable to read the input file")
55+
return -1
56+
57+
try:
58+
lvs_model = compile_lvs(text)
59+
except (SemanticError, lark.UnexpectedInput) as e:
60+
print("Unable to compile the LVS text input:", e)
61+
return -2
62+
63+
encoded_output = bytes(lvs_model.encode())
64+
65+
if args.output:
66+
if args.output == "-":
67+
sys.stdout.buffer.write(encoded_output)
68+
else:
69+
with open(os.path.expandvars(args.output), "wb") as f:
70+
f.write(encoded_output)

0 commit comments

Comments
 (0)