10
10
import re
11
11
12
12
import _moira
13
- from _moira import ( auth , host , motd , noop , proxy , MoiraException )
13
+ from _moira import auth , host , motd , noop , proxy , MoiraException
14
14
15
15
16
- help_re = re .compile ('([a-z0-9_, ]*) \(([a-z0-9_, ]*)\)(?: => ([a-z0-9_, ]*))?' ,
17
- re .I )
18
- et_re = re .compile (r'^\s*#\s*define\s+([A-Za-z0-9_]+)\s+.*?([0-9]+)' )
16
+ help_re = re .compile ("([a-z0-9_, ]*) \(([a-z0-9_, ]*)\)(?: => ([a-z0-9_, ]*))?" , re .I )
17
+ et_re = re .compile (r"^\s*#\s*define\s+([A-Za-z0-9_]+)\s+.*?([0-9]+)" )
19
18
20
19
21
20
_arg_cache = {}
22
21
_return_cache = {}
23
22
_et_cache = {}
24
23
25
24
25
+ def _to_bytes (s ):
26
+ """
27
+ If given a string, converts it to bytes, otherwise returns it as is
28
+ """
29
+ if isinstance (s , str ):
30
+ return s .encode ()
31
+ else :
32
+ return s
33
+
34
+
26
35
def _clear_caches ():
27
36
"""Clear query caches.
28
37
@@ -33,9 +42,11 @@ def _clear_caches():
33
42
_return_cache .clear ()
34
43
35
44
36
- def connect (server = '' ):
37
- _moira .connect (server )
45
+ def connect (server = "" ):
46
+ _moira .connect (_to_bytes ( server ) )
38
47
version (- 1 )
48
+
49
+
39
50
connect .__doc__ = _moira .connect .__doc__
40
51
41
52
@@ -54,13 +65,13 @@ def _load_help(handle):
54
65
and return values into and out of dictionaries and into and out of
55
66
tuples.
56
67
"""
57
- help_string = ', ' .join (query (' _help' , handle )[0 ]).strip ()
68
+ help_string = ", " .join (query (" _help" , handle )[0 ]).strip ()
58
69
59
- handle_str , arg_str , return_str = help_re .match (help_string ).groups ('' )
70
+ handle_str , arg_str , return_str = help_re .match (help_string ).groups ("" )
60
71
61
- handles = handle_str .split (', ' )
62
- args = arg_str .split (', ' )
63
- returns = return_str .split (', ' )
72
+ handles = handle_str .split (", " )
73
+ args = arg_str .split (", " )
74
+ returns = return_str .split (", " )
64
75
65
76
for h in handles :
66
77
_arg_cache [h ] = args
@@ -70,12 +81,21 @@ def _load_help(handle):
70
81
def _list_query (handle , * args ):
71
82
"""
72
83
Execute a Moira query and return the result as a list of tuples.
73
-
84
+
74
85
This bypasses the tuple -> dict conversion done in moira.query()
75
86
"""
76
87
results = []
77
- _moira ._query (handle , results .append , * args )
78
- return results
88
+
89
+ # Python 3 wants bytes
90
+ args_converted = (_to_bytes (arg ) for arg in args )
91
+
92
+ _moira ._query (_to_bytes (handle ), results .append , * args_converted )
93
+
94
+ # We get bytes back, convert back to string
95
+ return [
96
+ tuple (val .decode () for val in result )
97
+ for result in results
98
+ ]
79
99
80
100
81
101
def _parse_args (handle , args , kwargs ):
@@ -91,34 +111,34 @@ def _parse_args(handle, args, kwargs):
91
111
arguments that can be passed to the low-level Moira query
92
112
function.
93
113
"""
94
- if (handle not in _return_cache or
95
- not _return_cache [handle ]):
114
+ if handle not in _return_cache or not _return_cache [handle ]:
96
115
_load_help (handle )
97
116
98
117
if kwargs :
99
- return tuple (kwargs .get (i , '*' )
100
- for i in _arg_cache [handle ])
118
+ return tuple (_to_bytes (kwargs .get (i , "*" )) for i in _arg_cache [handle ])
101
119
else :
102
- return args
120
+ return tuple ( _to_bytes ( arg ) for arg in args )
103
121
104
122
105
123
def query (handle , * args , ** kwargs ):
106
124
"""
107
125
Execute a Moira query and return the result as a list of dicts.
108
-
126
+
109
127
Arguments can be specified either as positional or keyword
110
128
arguments. If specified by keyword, they are cross-referenced with
111
129
the argument name given by the query "_help handle".
112
-
130
+
113
131
All of the real work of Moira is done in queries. There are over
114
132
100 queries, each of which requires different arguments. The
115
133
arguments to the queries should be passed as separate arguments to
116
134
the function.
117
135
"""
118
- if handle .startswith ('_' ):
119
- return _list_query (handle , * args )
136
+ if handle .startswith ("_" ):
137
+ args_converted = (_to_bytes (arg ) for arg in args )
138
+
139
+ return _list_query (handle , * args_converted )
120
140
else :
121
- fmt = kwargs .pop (' fmt' , dict )
141
+ fmt = kwargs .pop (" fmt" , dict )
122
142
123
143
args = _parse_args (handle , args , kwargs )
124
144
@@ -147,10 +167,10 @@ def access(handle, *args, **kwargs):
147
167
args = _parse_args (handle , args , kwargs )
148
168
149
169
try :
150
- _moira ._access (handle , * args )
170
+ _moira ._access (_to_bytes ( handle ) , * args )
151
171
return True
152
172
except MoiraException as e :
153
- if e .code != errors ()[' MR_PERM' ]:
173
+ if e .code != errors ()[" MR_PERM" ]:
154
174
raise
155
175
return False
156
176
@@ -160,6 +180,8 @@ def version(ver):
160
180
# return values
161
181
_clear_caches ()
162
182
return _moira .version (ver )
183
+
184
+
163
185
version .__doc__ = _moira .version .__doc__
164
186
165
187
@@ -174,9 +196,8 @@ def errors():
174
196
bug that it isn't.
175
197
"""
176
198
if not _et_cache :
177
- for prefix in ('/usr/include' ,
178
- '/sw/include' ):
179
- header = os .path .join (prefix , 'moira/mr_et.h' )
199
+ for prefix in ("/usr/include" , "/sw/include" ):
200
+ header = os .path .join (prefix , "moira/mr_et.h" )
180
201
if os .path .exists (header ):
181
202
for line in open (header ):
182
203
m = et_re .search (line )
@@ -187,6 +208,18 @@ def errors():
187
208
return _et_cache
188
209
189
210
190
- __all__ = ['connect' , 'disconnect' , 'auth' , 'host' , 'motd' , 'noop' , 'query' ,
191
- 'proxy' , 'version' , 'access' , 'errors' , '_list_query' ,
192
- 'MoiraException' ]
211
+ __all__ = [
212
+ "connect" ,
213
+ "disconnect" ,
214
+ "auth" ,
215
+ "host" ,
216
+ "motd" ,
217
+ "noop" ,
218
+ "query" ,
219
+ "proxy" ,
220
+ "version" ,
221
+ "access" ,
222
+ "errors" ,
223
+ "_list_query" ,
224
+ "MoiraException" ,
225
+ ]
0 commit comments