Last active
August 29, 2015 14:02
-
-
Save njwilson23/2a8f2572c36afa7dffa2 to your computer and use it in GitHub Desktop.
py3-world-files-test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def world_files(fname): | |
""" | |
Determine potential world filename combinations, without checking | |
their existence. | |
For example, a '*.tif' file may have one of the following | |
popular conventions for world file extensions '*.tifw', | |
'*.tfw', '*.TIFW' or '*.TFW'. | |
Given the possible world file extensions, the upper case basename | |
combinations are also generated. For example, the file 'map.tif' | |
will generate the following world file variations, 'map.tifw', | |
'map.tfw', 'map.TIFW', 'map.TFW', 'MAP.tifw', 'MAP.tfw', 'MAP.TIFW' | |
and 'MAP.TFW'. | |
Args: | |
* fname: | |
Name of the file for which to get all the possible world | |
filename combinations. | |
Returns: | |
A list of possible world filename combinations. | |
Examples: | |
>>> from cartopy.io.img_nest import Img | |
>>> Img.world_files('img.png')[:6] | |
['img.pngw', 'img.pgw', 'img.PNGW', 'img.PGW', 'IMG.pngw', 'IMG.pgw'] | |
>>> Img.world_files('/path/to/img.TIF')[:2] | |
['/path/to/img.tifw', '/path/to/img.tfw'] | |
>>> Img.world_files('/path/to/img/with_no_extension')[0] | |
'/path/to/img/with_no_extension.w' | |
""" | |
froot, fext = os.path.splitext(fname) | |
# If there was no extension to the filename. | |
if froot == fname: | |
result = ['{}.{}'.format(fname, 'w'), | |
'{}.{}'.format(fname, 'W')] | |
else: | |
fext = fext[1::].lower() | |
if len(fext) < 3: | |
result = ['{}.{}'.format(fname, 'w'), | |
'{}.{}'.format(fname, 'W')] | |
else: | |
fext_types = [fext + 'w', fext[0] + fext[-1] + 'w'] | |
fext_types.extend([ext.upper() for ext in fext_types]) | |
result = ['{}.{}'.format(froot, ext) for ext in fext_types] | |
def _convert_basename(name): | |
dirname, basename = os.path.dirname(name), os.path.basename(name) | |
base, ext = os.path.splitext(basename) | |
if base == base.upper(): | |
result = base.lower() + ext | |
else: | |
result = base.upper() + ext | |
if dirname: | |
result = os.path.join(dirname, result) | |
return result | |
# result.extend(map(_convert_basename, result)) # original | |
# result += [_convert_basename(r) for r in result] # PR #448 | |
# result.extend(_convert_basename(r) for r in result) # generator expression | |
result.extend([_convert_basename(r) for r in result]) # list comprehension | |
return result | |
world_files("img.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment