Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Python 3 #1

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
33 changes: 26 additions & 7 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'?.

"""
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 @@ -126,7 +145,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 +166,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