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

Split function to extract filename for .yml use is not reliable #62

Closed
jgherbert opened this Issue Dec 16, 2013 · 1 comment

Comments

Projects
None yet
2 participants
@jgherbert
Copy link
Contributor

jgherbert commented Dec 16, 2013

I found this problem playing with routes.py yesterday:

Traceback (most recent call last):
  File "showroute.py", line 4, in <module>
    from jnpr.junos.op.routes import RouteTable
  File "/usr/local/lib/python2.7/dist-packages/jnpr/junos/op/routes.py", line 3, in <module>
    globals().update(loadyaml(_yml_file))
  File "/usr/local/lib/python2.7/dist-packages/jnpr/junos/factory/loadyaml.py", line 33, in loadyaml
    return FactoryLoader().load( yaml.load( open(path, 'r' )))
IOError: [Errno 2] No such file or directory: '/usr/local/lib/python2.yml'

What's happening here is that the path to the file includes "python2.7", and the split('.') function is therefore splitting on the '.' in the middle of "2.7" rather than the dot before the "py" suffix.

Oddly, it didn't seem to affect ethport.py when I tested, which is coded identically as far as I could see, but I'm not arguing. The solution is to match on the last occurrence of the '.', OR to import the os module and use os.path to extract the filename without extension.

Here is the line causing the issue, in op/routes.py:

_yml_file = __file__.split('.')[0]+'.yml'

I propose two solutions to this issue for your consideration. The first is a text solution using rsplit:

_yml_file = __file__.rsplit('.',1)[0]+'.yml'

The second is to use the os.path.splitext function to extract the path (which may be safer across multiple platforms) instead:

from os.path import splitext
_yml_file = splitext(__file__)[0]+'.yml'

I welcome your thoughts; I can implement either "fix" for you and issue pull request if you'd like one done.

j.

@jeremyschulman

This comment has been minimized.

Copy link
Member

jeremyschulman commented Dec 20, 2013

fixed, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment