fmenabe/python-unix

Error importing from unix.linux import Linux

Closed this issue · 6 comments

Seems to be related to this,

 ----> 7 from unix.linux._memory import Memory as _Memory
 ----> 6 _PARAM_RE = re.compile('(([a-zA-Z0-9_]*)(\((\w*)\))?):\s+(\d*)(.*)?')
 re.py:
      188 def compile(pattern, flags=0):
      189     "Compile a regular expression pattern, returning a pattern object."
 --> 190     return _compile(pattern, flags)

      240         p = sre_compile.compile(pattern, flags)
      241     except error, v:
 --> 242         raise error, v # invalid expression
      243     if len(_cache) >= _MAXCACHE:
      244         _cache.clear()

 error: nothing to repeat

Wondering if this is a bug related to the regex? I however didn't run into any issues when switching from python 2.7.5 to 3.4.3.

This is an odd bug I can reproduce on Ubuntu (12.04; both python 2.7 and 3.2) and Centos (7; python 2.7) but I don't have on Archlinux (python 2.7 and 3.5) ...

It seems the last element of the regex((.*)?) is the cause of the error (I don't really know why!):

>>> import re
>>> re.compile('(([a-zA-Z0-9_]*)(\((\w*)\))?):\s+(\d*)(.*)?')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/re.py", line 190, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.7/re.py", line 242, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat

>>> re.compile('(([a-zA-Z0-9_]*)(\((\w*)\))?):\s+(\d*)(\s+(\w*))?')
<_sre.SRE_Pattern object at 0x25bb9d0>

I change the regex to this (I name the elements for clarity):

re.compile(r'^(?P<param>[a-zA-Z0-9_]*)(\((?P<anon>\w*)\))?:\s+(?P<value>\d*)(\s+(?P<unit>\w*))?$')

and it seems to work.

I push the modification on the master branch, can you confirm it works for you?

Hi again,

Thanks for taking a peak at this. I can confirm I was able to import without issues on CentOS 7 with 2.7.5.

I tested with a virtualenv & pip install git+https://github.com/fmenabe/python-unix.git

I am however running into another bug associated with the kvm package now.

In [5]: localhost = kvm.Hypervisor(Linux(Local()))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-476501ba6af7> in <module>()
----> 1 localhost = kvm.Hypervisor(Linux(Local()))

/root/Workspace/Projects/testing/env/lib/python2.7/site-packages/kvm/__init__.py in Hypervisor(host)
    362         property_obj = type('_%s' % property_name.capitalize(),
    363                             (object,),
--> 364                             dict(__init__=__init))
    365
    366         for method_name, method_conf in property_methods.items():

TypeError: type() argument 1 must be string, not unicode

I tried a quick fix with from __future__ import unicode_literals but no go. I'll poke at the code some more, let me know if I should open issue under kvm module.

This part of the code dynamically add properties (which are objects) to the Hypervisor object based on the mapping in the JSON file. type first parameter should be of type str but json module in python2 apparently returns unicode strings. I correct the error by casting to str (type('_%s' % str(property_name).capitalize(), ...) and it seems to work for both python2 and python3.

I push the update to the master branch so you can test it.

And thanks for reporting these bugs!

I'll give it a try today when I have free moment. You're very welcome! I look forward to utilizing your libs with flask to a create a dashboard for my kvm lab.

I'm trying to import the unix.linux and got the similar issue:

Traceback (most recent call last):
File "", line 1, in
File "/home/eyeefan/virtual-env/virtualenv-15.0.3/KVM_ENV/lib/python2.7/site-packages/unix/linux/init.py", line 7, in
from unix.linux.memory import Memory as Memory
File "/home/eyeefan/virtual-env/virtualenv-15.0.3/KVM_ENV/lib/python2.7/site-packages/unix/linux/memory.py", line 6, in
PARAM_RE = re.compile('(([a-zA-Z0-9]
)(((\w
)))?):\s+(\d
)(._)?')
File "/home/eyeefan/virtual-env/virtualenv-15.0.3/KVM_ENV/lib/python2.7/re.py", line 190, in compile
return _compile(pattern, flags)
File "/home/eyeefan/virtual-env/virtualenv-15.0.3/KVM_ENV/lib/python2.7/re.py", line 245, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat

the package is downloaded from package index searching.

the importing issue is solved by installing from +https://github.com/fmenabe/python-unix.git