[PATCH 2/5] Do not fail when missing directories are present in addon paths

Vratislav Podzimek vpodzime at redhat.com
Wed Jan 2 09:47:42 UTC 2013


On Wed, 2012-12-19 at 16:09 +0100, Martin Sivak wrote:
> ---
>  pyanaconda/ui/common.py | 71 ++++++++++++++++++++++++++-----------------------
>  1 file changed, 38 insertions(+), 33 deletions(-)
> 
> diff --git a/pyanaconda/ui/common.py b/pyanaconda/ui/common.py
> index 9a2b304..69d0390 100644
> --- a/pyanaconda/ui/common.py
> +++ b/pyanaconda/ui/common.py
> @@ -438,38 +438,43 @@ def collect(module_pattern, path, pred):
>      """
>  
>      retval = []
> -    for module_file in os.listdir(path):
> -        if (not module_file.endswith(".py")) and \
> -           (not module_file.endswith(".so")):
> -            continue
> -        
> -        if module_file == "__init__.py":
> -            continue
> +    try:
> +        for module_file in os.listdir(path):
> +            if (not module_file.endswith(".py")) and \
> +                (not module_file.endswith(".so")):
> +                continue
>  
> -        try:
> -            mod_name = module_file[:module_file.rindex(".")]
> -        except ValueError:
> -            mod_name = module_file
> +            if module_file == "__init__.py":
> +                continue
>  
> -        mod_info = None
> -        module = None
> -        
> -        try:    
> -            imp.acquire_lock()
> -            mod_info = imp.find_module(mod_name, [path])
> -            module = sys.modules.get(module_pattern % mod_name)
> -            if not module:
> -                module = imp.load_module(module_pattern % mod_name, *mod_info)
> -            imp.release_lock()
> -        except ImportError:
> -            continue
> -        finally:
> -            if mod_info and mod_info[0]:
> -                mod_info[0].close()
> -
> -        p = lambda obj: inspect.isclass(obj) and pred(obj)
> -
> -        for (name, val) in inspect.getmembers(module, p):
> -            retval.append(val)
> -
> -    return retval
> +            try:
> +                mod_name = module_file[:module_file.rindex(".")]
> +            except ValueError:
> +                mod_name = module_file
> +
> +            mod_info = None
> +            module = None
> +
> +            try:
> +                imp.acquire_lock()
> +                mod_info = imp.find_module(mod_name, [path])
> +                module = sys.modules.get(module_pattern % mod_name)
> +                if not module:
> +                    module = imp.load_module(module_pattern % mod_name, *mod_info)
> +                imp.release_lock()
> +            except ImportError:
> +                continue
> +            finally:
> +                if mod_info and mod_info[0]:
> +                    mod_info[0].close()
> +
> +            p = lambda obj: inspect.isclass(obj) and pred(obj)
> +
> +            for (name, val) in inspect.getmembers(module, p):
> +                retval.append(val)
> +
> +        return retval
> +
> +    # when the directory "path" does not exist
> +    except OSError:
> +        return []
Wouldn't it be better to have something like:

try:
    items = os.listdir(path)
except OSError:
    return []

for module_file in items:
    ...

instead of the long nested try-except block?

-- 
Vratislav Podzimek

Anaconda Rider | Red Hat, Inc. | Brno - Czech Republic



More information about the anaconda-patches mailing list