Created
November 24, 2018 10:38
-
-
Save ruffsl/19c0f9b3ab726c68d9ae6aa522b45821 to your computer and use it in GitHub Desktop.
ComArmor Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from IPython.display import display, Markdown\n", | |
"from xml.etree import ElementTree\n", | |
"import comarmor\n", | |
"from comarmor.xml.utils import beautify_xml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def display_xml_markdown(data):\n", | |
" if type(data) is str:\n", | |
" display(Markdown('``` xml\\n' + data + '\\n```'))\n", | |
" elif type(data) is ElementTree.Element:\n", | |
" display(Markdown('``` xml\\n' + ElementTree.tostring(data).decode('utf-8') + '\\n```'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"commarmor_profile_dir = ['/home/ruffsl/Desktop/keymint_ws/profile/comarmor.d']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"profile_storage = comarmor.parse_profiles(commarmor_profile_dir)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"profile = profile_storage.filter_profile('/foo/bar/talker')[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/markdown": [ | |
"``` xml\n", | |
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", | |
"<profiles>\n", | |
" <profile name=\"My Talker Profile\">\n", | |
" <attachments>\n", | |
" <attachment>*/talker</attachment>\n", | |
" </attachments>\n", | |
" <profile name=\"Node\">\n", | |
" <attachments>\n", | |
" <attachment>/*</attachment>\n", | |
" </attachments>\n", | |
" <profile name=\"Node/Time\">\n", | |
" <attachments>\n", | |
" <attachment>/*</attachment>\n", | |
" </attachments>\n", | |
" <ros_topic qualifier=\"ALLOW\">\n", | |
" <attachments>\n", | |
" <attachment>/clock</attachment>\n", | |
" </attachments>\n", | |
" <permissions>\n", | |
" <subscribe/>\n", | |
" </permissions>\n", | |
" </ros_topic>\n", | |
" </profile>\n", | |
" <profile name=\"Node/Parameters\">\n", | |
" <attachments>\n", | |
" <attachment>/*</attachment>\n", | |
" </attachments>\n", | |
" <ros_topic qualifier=\"ALLOW\">\n", | |
" <attachments>\n", | |
" <attachment>/foo/bar/parameter_events</attachment>\n", | |
" </attachments>\n", | |
" <permissions>\n", | |
" <publish/>\n", | |
" <subscribe/>\n", | |
" </permissions>\n", | |
" </ros_topic>\n", | |
" <ros_service qualifier=\"ALLOW\">\n", | |
" <attachments>\n", | |
" <attachment>/foo/bar/talker/describe_parameters</attachment>\n", | |
" <attachment>/foo/bar/talker/get_parameter_types</attachment>\n", | |
" <attachment>/foo/bar/talker/get_parameters</attachment>\n", | |
" <attachment>/foo/bar/talker/list_parameters</attachment>\n", | |
" <attachment>/foo/bar/talker/set_parameters</attachment>\n", | |
" <attachment>/foo/bar/talker/set_parameters_atomically</attachment>\n", | |
" </attachments>\n", | |
" <permissions>\n", | |
" <request/>\n", | |
" </permissions>\n", | |
" </ros_service>\n", | |
" </profile>\n", | |
" </profile>\n", | |
" <ros_topic qualifier=\"ALLOW\">\n", | |
" <attachments>\n", | |
" <attachment>/foo/bar/chatter</attachment>\n", | |
" </attachments>\n", | |
" <permissions>\n", | |
" <publish/>\n", | |
" </permissions>\n", | |
" </ros_topic>\n", | |
" </profile>\n", | |
"</profiles>\n", | |
"\n", | |
"```" | |
], | |
"text/plain": [ | |
"<IPython.core.display.Markdown object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"display_xml_markdown(beautify_xml(profile.tree.getroot()))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/markdown": [ | |
"``` xml\n", | |
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", | |
"<rules>\n", | |
" <ros_topic qualifier=\"ALLOW\">\n", | |
" <attachments>\n", | |
" <attachment>/clock</attachment>\n", | |
" </attachments>\n", | |
" <permissions>\n", | |
" <subscribe/>\n", | |
" </permissions>\n", | |
" </ros_topic>\n", | |
" <ros_topic qualifier=\"ALLOW\">\n", | |
" <attachments>\n", | |
" <attachment>/foo/bar/parameter_events</attachment>\n", | |
" </attachments>\n", | |
" <permissions>\n", | |
" <publish/>\n", | |
" <subscribe/>\n", | |
" </permissions>\n", | |
" </ros_topic>\n", | |
" <ros_topic qualifier=\"ALLOW\">\n", | |
" <attachments>\n", | |
" <attachment>/foo/bar/chatter</attachment>\n", | |
" </attachments>\n", | |
" <permissions>\n", | |
" <publish/>\n", | |
" </permissions>\n", | |
" </ros_topic>\n", | |
"</rules>\n", | |
"\n", | |
"```" | |
], | |
"text/plain": [ | |
"<IPython.core.display.Markdown object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"ros_topic_rules = profile.extract_rules('ros_topic')\n", | |
"display_xml_markdown(beautify_xml(ros_topic_rules))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment