Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ mrclient.h
*.pyc
build
dist

# Cython autogenerated
*.egg-info
12 changes: 7 additions & 5 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
Installing PyMoira
==================

To install PyMoira, you will first need to install Pyrex_. It's always
To install PyMoira, you will first need to install Cython. It's always
a good idea to install Pyrex through your package manager, if
possible. Your system's Pyrex package may be named ``python-pyrex`` or
``pyrex-py25``. If your package manager doesn't have a package for
possible. Your system's Pyrex package may be named ``python3-Cython`` or
``cython3``. If your package manager doesn't have a package for
Pyrex, or if you wish to install Pyrex by hand anyway, you can do so
by running::

$ easy_install Pyrex
$ pip install Cython

Once you've done that, to install PyMoira globally, run::

$ python setup.py install
$ pip install .

If you want to build PyMoira without installing it globally, you may
want to run::
Expand All @@ -27,6 +27,8 @@ working directory is the root of the PyMoira source tree.
Alternatively, PyMoira has been packaged for Debian and Ubuntu. To
build the Debian package of the latest release, run::

_(NOTE: deb generation is untested)_
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test it?


$ git checkout debian
$ git buildpackage
$ sudo debi
Expand Down
6 changes: 3 additions & 3 deletions _moira.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MoiraException(Exception):
__connected = False

def _error(code):
raise MoiraException, (code, error_message(code))
raise MoiraException(code, error_message(code))

def connect(server=''):
"""
Expand Down Expand Up @@ -86,9 +86,9 @@ def auth(program, krb4=False):
discouraged
"""
if krb4:
status = mr_auth(program)
status = mr_auth(program.encode())
else:
status = mr_krb5_auth(program)
status = mr_krb5_auth(program.encode())
if status != MR_SUCCESS:
_error(status)

Expand Down
36 changes: 28 additions & 8 deletions moira.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
_et_cache = {}


def _to_bytes(s):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not completely sure how necessary this function is, but doing .encode() instead of _to_bytes everywhere I used _to_bytes results in messages like AttributeError: 'bytes' object has no attribute 'encode'. Did you mean: 'decode'?.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like _to_bytes is basically coping with sometimes being passed a bytes and sometimes a string. If you need to handle that, I suspect you need something like _to_bytes, but if you can be consistent about "this function always takes a string" or "this function always takes bytes", you can just use .encode as needed.

"""
If given a string, converts it to bytes, otherwise returns it as is
"""
if isinstance(s, str):
return s.encode()
return s


def _clear_caches():
"""Clear query caches.

Expand All @@ -34,7 +43,7 @@ def _clear_caches():


def connect(server=''):
_moira.connect(server)
_moira.connect(_to_bytes(server))
version(-1)
connect.__doc__ = _moira.connect.__doc__

Expand Down Expand Up @@ -74,8 +83,18 @@ def _list_query(handle, *args):
This bypasses the tuple -> dict conversion done in moira.query()
"""
results = []
_moira._query(handle, results.append, *args)
return results

# Python 3 wants bytes
args_converted = (_to_bytes(arg) for arg in args)

# Perform the query
_moira._query(_to_bytes(handle), results.append, *args_converted)

# We get bytes back, convert back to string
return [
tuple(val.decode() for val in result)
for result in results
]


def _parse_args(handle, args, kwargs):
Expand All @@ -99,7 +118,7 @@ def _parse_args(handle, args, kwargs):
return tuple(kwargs.get(i, '*')
for i in _arg_cache[handle])
else:
return args
return tuple(_to_bytes(arg) for arg in args)


def query(handle, *args, **kwargs):
Expand All @@ -116,7 +135,8 @@ def query(handle, *args, **kwargs):
the function.
"""
if handle.startswith('_'):
return _list_query(handle, *args)
args_converted = (_to_bytes(arg) for arg in args)
return _list_query(handle, *args_converted)
else:
fmt = kwargs.pop('fmt', dict)

Expand All @@ -126,7 +146,7 @@ def query(handle, *args, **kwargs):
results = []

for r in plain_results:
results.append(fmt(zip(_return_cache[handle], r)))
results.append(fmt(list(zip(_return_cache[handle], r))))

return results

Expand All @@ -147,9 +167,9 @@ def access(handle, *args, **kwargs):
args = _parse_args(handle, args, kwargs)

try:
_moira._access(handle, *args)
_moira._access(_to_bytes(handle), *args)
return True
except MoiraException, e:
except MoiraException as e:
if e.code != errors()['MR_PERM']:
raise
return False
Expand Down
10 changes: 5 additions & 5 deletions qy
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def main():
moira.auth(options.program)

if args[0].startswith('_'):
print '\n'.join(', '.join(x) for x in
moira._list_query(*args))
print('\n'.join(', '.join(x) for x in
moira._list_query(*args)))
else:
results = moira.query(fmt=tuple, *args)

Expand All @@ -63,11 +63,11 @@ def main():
r = filter_fields(r, options.fields)

if options.single:
print ', '.join(v for (k, v) in r)
print(', '.join(v for (k, v) in r))
else:
for k, v in r:
print '%-*s: %s' % (keylen, k, v)
print
print('%-*s: %s' % (keylen, k, v))
print()

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext
from Cython.Distutils import build_ext

setup(
name="PyMoira",
Expand Down