test_rule_parser.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # License: BSD
00004 #   https://raw.github.com/robotics-in-concert/rocon_tools/license/LICENSE
00005 #
00006 # This is more just an example and a check to ensure that the parts we
00007 # use stay working.
00008 
00009 ##############################################################################
00010 # Imports
00011 ##############################################################################
00012 
00013 # enable some python3 compatibility options:
00014 # (unicode_literals not compatible with python2 uuid module)
00015 from __future__ import absolute_import, print_function
00016 
00017 from nose.tools import assert_raises
00018 import rocon_ebnf.rule_parser as rule_parser
00019 
00020 ##############################################################################
00021 # Tests
00022 ##############################################################################
00023 
00024 def try_using_windoze_attribute(parser_result):
00025     '''
00026       Used to check if one of the parser's variables is set or not. If not, it raises
00027       an AttributeError
00028     '''
00029     print("%s" % parser_result.windoze)
00030 
00031 def print_linux_parsing(input_string, parser_result):
00032     print("Input: %s" % input_string)
00033     if parser_result is not None:
00034         try:
00035             print("  OS list: %s" % parser_result.operating_systems_list)
00036         except AttributeError:
00037             pass
00038         try:
00039             print("  Ubuntu : %s" % parser_result.ubuntu)
00040         except AttributeError:
00041             pass
00042         try:
00043             print("  Linux  : %s" % parser_result.linux)
00044         except AttributeError:
00045             pass
00046         try:
00047             print("  OS : %s" % parser_result.os)
00048         except AttributeError:
00049             pass
00050         #print("  Windows: %s" % result.windows) throws an AttributeError
00051     else:
00052         print("Error in parsing")
00053 
00054 def test_message_to_string():
00055     # about this rule:
00056     #   - accomodate a trailing slash (sep? at the end)
00057     operating_systems_rule = [
00058              'init operating_systems_list=[] ',
00059              'pattern           ::= os_zero operating_systems*',
00060              'os_zero           ::= os                          @operating_systems_list.append("$os")', 
00061              'operating_systems ::= "|" os                      @operating_systems_list.append("$os")', 
00062              'os                ::= "*" | windoze | linux | "osx" | "freebsd"',
00063                'windoze         ::= "winxp" | "windows7"',
00064                'linux           ::= "arch" | "debian" | "fedora" | "gentoo" | "opensuse" | ubuntu | "linux"',
00065                  'ubuntu        ::= "precise" | "quantal" | "raring" | "ubuntu"' 
00066               ]
00067     input_string = "precise|quantal"
00068     result = rule_parser.match(operating_systems_rule, input_string)
00069     assert result is not None
00070     assert "precise" in result.operating_systems_list
00071     assert "quantal" in result.operating_systems_list
00072     assert result.ubuntu == "quantal"
00073     assert result.linux == "quantal"
00074     assert result.os == "quantal"
00075     assert_raises(AttributeError, try_using_windoze_attribute, result)
00076     print_linux_parsing(input_string, result)
00077 
00078     input_string = "*"
00079     result = rule_parser.match(operating_systems_rule, input_string)
00080     assert result is not None
00081     assert result.os == "*"
00082     print_linux_parsing(input_string, result)
00083 
00084     input_string = "ubuntu"
00085     result = rule_parser.match(operating_systems_rule, input_string)
00086     assert result is not None
00087     assert result.os == "ubuntu"
00088     print_linux_parsing(input_string, result)
00089     
00090     rule = [ 'uri      ::= sep os* sep system* sep platform* sep name* sep?',
00091              'sep      ::= r"/"',
00092              'os       ::= "windows" | "linux" | "precise"',
00093              'system   ::= "opros" | ros',
00094                'ros      ::= "groovy" | "hydro" | "ros"', 
00095              'platform ::= r"." ^sep',
00096              'name     ::= r"." ^sep',
00097              ]
00098     rocon_uri = "/precise/ros/turtlebot/dude"
00099     print("Input: %s" % rocon_uri)
00100     result = rule_parser.match(rule, rocon_uri)
00101     if result is not None:
00102         print("  os      : %s" % result.os)
00103         print("  system  : %s" % result.system)
00104         print("  platform: %s" % result.platform)
00105         print("  name    : %s" % result.name)
00106     else:
00107         print("Error in parsing")
00108     
00109 #     rule = [ 'rosdistro ::= "groovy" | "hydro"' ]
00110 #     text = 'groovy'
00111 #     result = rule_parser.rp.match(rule, text)
00112 #     print("\nResult: %s" % result)
00113 #     print("rosdistro: %s" % result.rosdistro)
00114 
00115 #     assert_raises(rocon_uri.RoconURIInvalidException, rocon_uri.parse, 'http:/precise/hydro/turtlebot/dude#rocon_apps/chirp')
00116 #     assert_raises(rocon_uri.RoconURIInvalidException, rocon_uri.parse, 'rocon:/precise//turtlebot/dude#rocon_apps/chirp')
00117 #     assert(str(rocon_uri_object), rocon_uri_string)
00118         
00119 # #
00120 # # we define the rule as a list of (sub)rules
00121 # #
00122 # # r"\S"*   means regular expression specifying 
00123 # #          any character except blank 
00124 # rule=['sqs  ::=  parms  fileid ', 
00125 #       'parms::=  r"\S"* ',        
00126 #       'fileid::= r"\S"* ']        
00127 # #
00128 # # we concatenate arguments ... as words
00129 # parms=' '.join(sys.argv[1:])
00130 # #
00131 # # we make the parsing 
00132 # cmp=rule_parser.rp.match(rule,parms)
00133 # # 
00134 # #as re module, if the result object is None, 
00135 # # the parsing is unsuccessful
00136 # if cmp==None:
00137 #     print "Error in parsing:"   
00138 # else:
00139 #     #
00140 #     # now, to get values from parsing, 
00141 #     # we use rule names as parser arguments.
00142 #     # cmp.sqs    will contain input parameters
00143 #     # cmp.parms  will contain string to locate
00144 #     # cmp.fileid will contain fileid to search in
00145 #     try:
00146 #         id=open(cmp.fileid)
00147 #         for l in id.readlines():
00148 #             if l.find(cmp.parms)>-1: 
00149 #                 print l[:-1]
00150 #     except Exception,e:
00151 #         print e
00152 #     else:
00153 #         id.close()
00154 


rocon_ebnf
Author(s): LParis
autogenerated on Fri May 2 2014 10:35:48