35 |
extensions under path. Nonrecursive. |
extensions under path. Nonrecursive. |
36 |
""" |
""" |
37 |
files = [f for f in os.listdir(path) if not f.startswith('.') |
files = [f for f in os.listdir(path) if not f.startswith('.') |
38 |
and not f == "CVS" and '#' not in f and '~' not in f] |
and not f == 'CVS' and '#' not in f and '~' not in f] |
39 |
|
|
40 |
|
files = [f for f in files if extensions.count(f.split('.')[-1]) > 0 \ |
41 |
|
or os.path.isdir(_slashify(path)+f)] |
42 |
if not dirs: |
if not dirs: |
43 |
files = [f for f in os.listdir(path) if os.path.isfile(_slashify(path)+f)] |
files = [f for f in files if os.path.isfile(_slashify(path)+f)] |
|
files = [f for f in files if extensions.count(f.split('.')[-1]) > 0] |
|
44 |
return files |
return files |
45 |
|
|
46 |
def _slashify(path): |
def _slashify(path): |
48 |
Add a trailing slash if not already there. |
Add a trailing slash if not already there. |
49 |
""" |
""" |
50 |
if len(path) > 0: |
if len(path) > 0: |
51 |
return (path+"/").replace("//","/") |
return (path+'/').replace('//','/') |
52 |
return path |
return path |
53 |
|
|
54 |
def relative_path(source, target): |
def relative_path(source, target): |
55 |
""" |
""" |
56 |
Returns a relative path from the source to the target. |
Returns a relative path from the source to the target. |
57 |
""" |
""" |
58 |
if source.startswith("../") or target.startswith("../"): |
if source == None or len(source) == 0: return target |
59 |
|
if source.startswith('../') or target.startswith('../'): |
60 |
source = os.path.normpath(os.path.abspath(source)) |
source = os.path.normpath(os.path.abspath(source)) |
61 |
target = os.path.normpath(os.path.abspath(target)) |
target = os.path.normpath(os.path.abspath(target)) |
62 |
|
|
63 |
depth = source.count("/") |
depth = source.count('/') |
64 |
source_parts = source.split("/") |
source_parts = source.split('/') |
65 |
target_parts = target.split("/") |
target_parts = target.split('/') |
66 |
|
|
67 |
parts = len(source_parts) < len(target_parts) \ |
parts = len(source_parts) < len(target_parts) \ |
68 |
and len(source_parts) or len(target_parts) |
and len(source_parts) or len(target_parts) |
72 |
parts = i |
parts = i |
73 |
break |
break |
74 |
|
|
75 |
relative = "".join(["../" for i in range(depth-parts)]) \ |
relative = ''.join(['../' for i in range(depth-parts)]) \ |
76 |
+"".join([target_parts[i+parts]+"/" \ |
+''.join([target_parts[i+parts]+'/' \ |
77 |
for i in range(len(target_parts)-parts)]) |
for i in range(len(target_parts)-parts)]) |
78 |
|
|
79 |
return (relative[0:len(relative)-1]) |
return (relative[0:len(relative)-1]) |