# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
from rosidl_cmake import expand_template
from rosidl_cmake import extract_message_types
from rosidl_cmake import get_newest_modification_time
from rosidl_parser import parse_message_file
from rosidl_parser import parse_service_file
from rosidl_parser import validate_field_types
from pathlib import Path
def generate_micro_ros_agent_xml_support(args):
pkg_name = args['package_name']
known_msg_types = extract_message_types(
pkg_name, args['ros_interface_files'], args.get('ros_interface_dependencies', []))
functions = {
'get_header_filename_from_msg_name': convert_camel_case_to_lower_case_underscore,
}
for idl_file in args['ros_interface_files']:
extension = os.path.splitext(idl_file)[1]
if extension == '.msg':
spec = parse_message_file(pkg_name, idl_file)
validate_field_types(spec, known_msg_types)
subfolder = os.path.basename(os.path.dirname(idl_file))
data = {
'spec': spec,
'pkg': spec.base_type.pkg_name,
'msg': spec.msg_name,
'type': spec.base_type.type,
'subfolder': subfolder,
}
data.update(functions)
# Make destinatino dir
if not os.path.exists(args['output_dir']):
os.makedirs(args['output_dir'])
# Check if publixher exists
pub_file_path = "/root/install/publisher.xml"
pub_file = Path(pub_file_path)
if not pub_file.is_file():
publ = open(pub_file_path, 'a')
publ.write("\n")
else:
publ = open(pub_file_path)
lines = publ.readlines()
publ.close()
publ = open(pub_file_path,'w')
publ.writelines([item for item in lines[:-1]])
#publ = open(os.path.join(args['output_dir'], 'publisher.xml'), 'w+')
#publ = open('/root/install/publisher.xml', 'a')
publ.write(" \n")
publ.write(" \n")
publ.write(" NO_KEY\n")
publ.write(" %sPubSubTopic\n" % (spec.msg_name))
publ.write(" %s::%s::dds_::%s_\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
publ.write(" \n")
publ.write(" KEEP_LAST\n")
publ.write(" 5\n")
publ.write(" \n")
publ.write(" \n")
publ.write(" TRANSIENT_LOCAL\n")
publ.write(" \n")
publ.write(" \n")
publ.write(" \n")
publ.write("\n")
publ.close()
# Check if subcriber exists
subs_file_path = "/root/install/subscriber.xml"
subs_file = Path(subs_file_path)
if not subs_file.is_file():
subs = open(subs_file_path, 'a')
subs.write("\n")
else:
subs = open(subs_file_path)
lines = subs.readlines()
subs.close()
subs = open(subs_file_path,'w')
subs.writelines([item for item in lines[:-1]])
#subs = open(os.path.join(args['output_dir'], 'subscriber.xml'), 'w+')
#subs = open('/root/install/subscriber.xml', 'a')
#subs.write("\n")
subs.write(" \n")
subs.write(" \n")
subs.write(" NO_KEY\n")
subs.write(" %sPubSubTopic\n" % (spec.msg_name))
subs.write(" %s::%s::dds_::%s_\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
subs.write(" \n")
subs.write(" KEEP_LAST\n")
subs.write(" 5\n")
subs.write(" \n")
subs.write(" \n")
subs.write(" TRANSIENT_LOCAL\n")
subs.write(" \n")
subs.write(" \n")
subs.write(" \n")
subs.write("\n")
subs.close()
# Check if topic exists
topi_file_path = "/root/install/topic.xml"
topi_file = Path(topi_file_path)
if not topi_file.is_file():
topi = open(topi_file_path, 'a')
topi.write("\n")
else:
topi = open(topi_file_path)
lines = topi.readlines()
topi.close()
topi = open(topi_file_path,'w')
topi.writelines([item for item in lines[:-1]])
#topi = open(os.path.join(args['output_dir'], 'topic.xml'), 'w+')
#topi = open('/root/install/topic.xml', 'a')
#topi.write("\n")
topi.write(" \n")
topi.write(" %sPubSubTopic\n" % (spec.msg_name))
topi.write(" %s::%s::dds_::%s_\n" % (spec.base_type.pkg_name, subfolder, spec.msg_name))
topi.write(" \n")
topi.write("\n")
topi.close()
#elif extension == '.srv':
#data = {'spec': spec}
#data.update(functions)
#if not os.path.exists(args['output_dir']):
# os.makedirs(args['output_dir'])
#f = open(os.path.join(args['output_dir'], 'demofile.txt'), 'w+')
#f.write("%s\n" % spec)
#f.close()
return 0