Yeuk Hon Wong avatar Yeuk Hon Wong committed eda3d1c Draft

A new URL-like regex parser for parsing our commands.

Comments (0)

Files changed (3)

  ssl_key: 'keys/host.key'
  ssl_cert: 'keys/host.cert'
 
-url-rule:
- signup: ['@chat', 'signup', '<str>', '<str>'] # @chat signup <username> <keyid>
- send: ['@<str>', '<str>', '<str>'] # @<username> <message>
- list-all: ['@chat', 'list-all'] # @chat list-all
- help: ['@chat', 'help'] # @chat help
+url-conf:
+ - help: ['@chat', '--help'] # @chat --help
+ - list_all: ['@chat', '--list-all'] # @chat --list-all
+ - signup: ['@chat', '--signup', '(?P<username>\w+)', '(?P<keyid>\w+)'] # @chat --signup <username> <keyid>
+ - send: ['@(?P<to>\w+)', '(?P<message>\w+[\s\w+]*)'] # @<username> <message>

tests/tests_parsers.py

 import unittest
-from playground.tools.parsers import validate_url, validate_token
+#from playground.tools.parsers import validate_url, validate_tokrn
+from playground.tools.parsers import make_regex, match_url_conf
 
+class TestMakeRegEx(unittest.TestCase):
+    def test_make_regex_for_one_word(self):
+        pattern = ['\w+']
+        resp = make_regex(pattern)
+        self.assertEqual(resp, pattern[0])
+    def test_make_regex_for_zero_or_more_words(self):
+        pattern = ['[\w+\s]*']
+        resp = make_regex(pattern)
+        self.assertEqual(resp, pattern[0])
+    def test_make_multi_matching(self):
+        pattern = ['@chat', '--signup', '\w+', '\w+']
+        resp = make_regex(pattern)
+        self.assertEqual(resp, '@chat\s--signup\s\w+\s\w+')
+    def test_make_named_groups(self):
+        pattern = ['(?P<at>@chat)', '(?P<cmd>\w+)',\
+            '(?P<username>\w+)', '(?P<keyid>\w+)']
+        resp = make_regex(pattern)
+        expected = '(?P<at>@chat)\s(?P<cmd>\w+)\s(?P<username>\w+)\s(?P<keyid>\w+)'
+        self.assertEqual(resp, expected)
+
+class TestURLConfMatcher(unittest.TestCase):
+    def setUp(self):
+        self.conf = [{'signup': ['@chat', '--signup', '\w+', '\w+']},\
+                    {'send': ['@(?P<to>\w+)', '\w+[\s\w+]*']}]
+    def test_signup_matches(self):
+        url = '@chat --signup name keyid'
+        resp = match_url_conf(url, self.conf)
+        self.assertEqual(resp, {'key': 0, 'name': 'signup', 'params': [{}]})
+    def test_send_matches_its_named_group(self):
+        url = '@john hello this is a message'
+        resp = match_url_conf(url, self.conf)
+        self.assertEqual(resp, {'key': 1, 'name': 'send', 'params': [{'to':'john'}]})
+    def test_signup_fail_when_last_item_missing(self):
+        url = '@chat --signup name'
+        self.assertRaises(Exception, match_url_conf, url, self.conf)
+    
+        
+    
+"""
 class TestValidateUrl(unittest.TestCase):
     def setUp(self):
         self.rule1 = ['python', '<str>']
         data = 'python'*2
         resp = validate_token(self.rule1, data)
         self.assertEqual(resp, False)
-
+"""
-rules = ['<str>', '<int>']
+import re
 
-#TODO: Should perform safe parsing...
-def validate_token(rkey, tvalue):
-    if not rkey == tvalue and not rkey in rules:
-        return False
-    return True
-                
-def validate_url(rule, target):
-    targets = target.split(' ')
-    if not len(rule) == len(targets):
-        return False
-    ball = dict(zip(rule, targets))
-    for rule, target in ball.items():
-        if not validate_token(rule, target):
-            return False
-    return True
+def make_regex(patterns):
+    if len(patterns) == 1:
+        return patterns[0]
+    else:
+        return '\s'.join(p for p in patterns)
+
+def match_url_conf(url, conf):
+    for key, value in enumerate(conf):
+        for k, v in value.items():
+            name = k # the name of func
+            patterns = make_regex(v)
+            pat = re.compile(patterns)
+            match = pat.match(url)
+            if match:
+                params = [m.groupdict() for m in pat.finditer(url)]
+                return {'key': key,\
+                    'name': name, \
+                    'params': params} 
+
+    raise Exception('Request %s does not match any defined url-conf rule.' % url)
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.