modulecore.py

Mon, 08 Dec 2014 15:37:03 -0500

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 08 Dec 2014 15:37:03 -0500
changeset 109
87ad0c1b3405
parent 102
2bad379cd416
child 113
08e9b1c1b324
permissions
-rw-r--r--

- commit latest changes

62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 import os
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 import re
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
3 from configfile import Config
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents: 62
diff changeset
5 Modules = {}
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 Commands = {}
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 class CommandError (Exception):
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 def __init__ (self, value):
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 self.value = value
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 def __str__ (self):
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 return self.value
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 # init_data()
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 # Initializes command module data
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 def init_data():
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 global Commands
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents: 62
diff changeset
21 global Modules
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 files = os.listdir ('.')
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 for fn in files:
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents: 62
diff changeset
25 if fn[0:4] != 'mod_' or fn[-3:] != '.py':
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 continue
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 fn = fn[0:-3]
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 globals = {}
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 module = __import__ (fn)
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents: 62
diff changeset
31 Modules[fn] = module
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 for cmd in module.ModuleData['commands']:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 if cmd['args'] == None:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 cmd['args'] = ''
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 cmd['module'] = module
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 cmd['regex'] = make_regex (cmd['args'])
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 cmd['argnames'] = []
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 Commands[cmd['name']] = cmd
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 for argname in cmd['args'].split (' '):
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 argname = argname[1:-1]
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 if argname[-3:] == '...':
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 argname = argname[0:-3]
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 if argname == '':
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 continue
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 cmd['argnames'].append (argname)
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 #done
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 #done
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents: 62
diff changeset
55 print "Loaded module %s" % fn
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 #done
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents: 62
diff changeset
58 print 'Loaded %d commands in %d modules' % (len (Commands), len (Modules))
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 #enddef
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 # command_error (message)
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 # Raises a command error
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 def command_error (message):
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 raise CommandError (message)
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 #
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
70 # is_available (cmd, ident, host)
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
71 #
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
72 # Is the given command available to the given user?
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
73 #
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
74 def is_available (cmd, ident, host):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
75 if cmd['level'] == 'admin' \
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
76 and not "%s@%s" % (ident, host) in Config.get_value ('admins', default=[]):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
77 return False
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
78
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
79 return True
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
80
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
81 #
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 # call_command (bot, message, cmdname, **kvargs)
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 # Calls a cobalt command
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 def call_command (bot, message, cmdname, **kvargs):
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 try:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 cmd = Commands[cmdname]
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 except KeyError:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 return False
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
91
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
92 if not is_available (cmd, kvargs['ident'], kvargs['host']):
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
93 command_error ("you may not use %s" % cmdname)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
94
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 match = re.compile (cmd['regex']).match (message)
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
96
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 if match == None:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 # didn't match
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
99 print "regex: %s" % cmd['regex']
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args']))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
101
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 i = 1
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 args = {}
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 for argname in cmd['argnames']:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 args[argname] = match.group (i)
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 i += 1
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
108
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, **kvargs)
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 return True
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 #
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
113 # get_available_commands
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
114 #
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
115 # Gets a list of commands available to the given user
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
116 #
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
117 def get_available_commands (ident, host):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
118 result=[]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
119
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
120 for cmdname,cmd in Commands.iteritems():
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
121 if not is_available (cmd, ident, host):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
122 continue
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
123
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
124 result.append (cmdname)
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
125
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
126 return result
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
127
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
128 #
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
129 # get_command_by_name
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
130 #
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
131 # Gets a command by name
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
132 #
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
133 def get_command_by_name (name):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
134 try:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
135 return Commands[name]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
136 except:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
137 return None
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
138
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
139 #
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
140 # make_regex
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 # Takes the argument list and returns a corresponding regular expression
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
143 #
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
144 def make_regex (arglist):
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 if arglist == None:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 return '^.+$'
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 gotoptional = False
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 gotvariadic = False
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 regex = ''
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 for arg in arglist.split (' '):
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 if gotvariadic:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154 raise CommandError ('variadic argument is not last')
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
155
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
156 if arg == '':
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
157 continue
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
158
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
159 gotliteral = False
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161 if arg[0] == '[' and arg[-1] == ']':
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
162 arg = arg[1:-1]
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163 gotoptional = True
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 elif arg[0] == '<' and arg[-1] == '>':
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 if gotoptional:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
166 raise CommandError ('mandatory argument after optional one')
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
167
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 arg = arg[1:-1]
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169 else:
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
170 gotliteral = True
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
171
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
172 if arg[-3:] == '...':
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173 gotvariadic = True
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 arg = arg[0:-3]
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 if gotoptional == False:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 regex += '\s+'
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 else:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 regex += '\s*'
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
181 if gotliteral:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
182 regex += arg
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
183 elif gotoptional:
62
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
184 if gotvariadic:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 regex += r'(.*)'
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 else:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 regex += r'([^ ]*)'
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 else:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 if gotvariadic:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 regex += r'(.+)'
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191 else:
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 regex += r'([^ ]+)'
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 #fi
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194 #done
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195
052a8a1e3d7d - revamped commands, added a much more modular system. not everything migrated yet
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 return '^[^ ]+%s$' % regex
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
197 #enddef

mercurial