[MERGE] file_open: give precedence to directory before zip.

bzr revid: florent.xicluna@gmail.com-20120208222801-p2gfhmsnptujdfru
This commit is contained in:
Florent Xicluna
2012-02-08 23:28:01 +01:00
3 changed files with 2248 additions and 3066 deletions
File diff suppressed because it is too large Load Diff
+14 -11
View File
@@ -280,25 +280,28 @@ def get_module_as_zip(modulename, b64enc=True, src=True):
def get_module_resource(module, *args):
"""Return the full path of a resource of the given module.
@param module: the module
@param args: the resource path components
:param module: module name
:param list(str) args: resource path components within module
@return: absolute path to the resource
:rtype: str
:return: absolute path to the resource
TODO name it get_resource_path
TODO make it available inside on osv object (self.get_resource_path)
"""
a = get_module_path(module)
if not a: return False
resource_path = opj(a, *args)
if zipfile.is_zipfile( a +'.zip') :
zip = zipfile.ZipFile( a + ".zip")
mod_path = get_module_path(module)
if not mod_path: return False
resource_path = opj(mod_path, *args)
if os.path.isdir(mod_path):
# the module is a directory - ignore zip behavior
if os.path.exists(resource_path):
return resource_path
elif zipfile.is_zipfile(mod_path + '.zip'):
zip = zipfile.ZipFile( mod_path + ".zip")
files = ['/'.join(f.split('/')[1:]) for f in zip.namelist()]
resource_path = '/'.join(args)
if resource_path in files:
return opj(a, resource_path)
elif os.path.exists(resource_path):
return resource_path
return opj(mod_path, resource_path)
return False
def get_module_icon(module):
+13 -11
View File
@@ -181,11 +181,20 @@ def file_open(name, mode="r", subdir='addons', pathinfo=False):
def _fileopen(path, mode, basedir, pathinfo):
head = os.path.normpath(path)
name = os.path.normpath(os.path.join(basedir, path))
name2 = False
# Give higher priority to module directories, which is
# a more common case than zipped modules.
if os.path.isfile(name):
fo = open(name, mode)
if pathinfo:
return (fo, name)
return fo
# Support for loading modules in zipped form.
# This will not work for zipped modules that are sitting
# outside of known addons paths.
head = os.path.normpath(path)
zipname = False
# Check for a zipfile in the path, but stop at the 'basedir' level
while os.sep in head:
head, tail = os.path.split(head)
if not tail:
@@ -208,14 +217,7 @@ def _fileopen(path, mode, basedir, pathinfo):
return (fo, name)
return fo
except Exception:
name2 = os.path.normpath(os.path.join(zpath, zipname))
# Look for a normal file
for fname in (name2, name):
if fname and os.path.isfile(fname):
fo = open(fname, mode)
if pathinfo:
return (fo, fname)
return fo
pass
# Not found
if name.endswith('.rml'):
raise IOError('Report %r doesn\'t exist or deleted' % name)