utils: Backward compatibility for 'imp.load_source'

This patch adds a function for providing the backward compatibility
for 'imp.load_source' in Python 2 and fixes bgp/application.py to
use this wrapper.

Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
IWASE Yusuke 2016-10-24 17:18:40 +09:00 committed by FUJITA Tomonori
parent 6792d6df2a
commit a7721fcce4
2 changed files with 22 additions and 3 deletions

View File

@ -15,14 +15,14 @@
"""
Defines bases classes to create a BGP application.
"""
import imp
import logging
import traceback
from oslo_config import cfg
from ryu.lib import hub
from ryu.utils import load_source
from ryu.base.app_manager import RyuApp
from ryu.services.protocols.bgp.api.base import call
from ryu.services.protocols.bgp.base import add_bgp_error_metadata
from ryu.services.protocols.bgp.base import BGPSException
@ -138,7 +138,7 @@ class RyuBGPSpeaker(RyuApp):
# Check if file can be read
try:
return imp.load_source('settings', config_file)
return load_source('settings', config_file)
except Exception as e:
raise ApplicationException(desc=str(e))

View File

@ -36,9 +36,28 @@ import os
import sys
import re
import six
LOG = logging.getLogger('ryu.utils')
def load_source(name, pathname):
"""
This function provides the backward compatibility for 'imp.load_source'
in Python 2.
:param name: Name used to create or access a module object.
:param pathname: Path pointing to the source file.
:return: Loaded and initialized module.
"""
if six.PY2:
import imp
return imp.load_source(name, pathname)
else:
loader = importlib.machinery.SourceFileLoader(name, pathname)
return loader.load_module(name)
def chop_py_suffix(p):
for suf in ['.py', '.pyc', '.pyo']:
if p.endswith(suf):