-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve image extension handling, add methods to modify / get defaults.
- Loading branch information
Showing
8 changed files
with
103 additions
and
23 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .parser_factory import create_parser | ||
from .img_extensions import * |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from copy import deepcopy | ||
|
||
__all__ = ['get_img_extensions', 'is_img_extension', 'set_img_extensions', 'add_img_extensions', 'del_img_extensions'] | ||
|
||
|
||
IMG_EXTENSIONS = ('.png', '.jpg', '.jpeg') # singleton, kept public for bwd compat use | ||
_IMG_EXTENSIONS_SET = set(IMG_EXTENSIONS) # set version, private, kept in sync | ||
|
||
|
||
def _set_extensions(extensions): | ||
global IMG_EXTENSIONS | ||
global _IMG_EXTENSIONS_SET | ||
dedupe = set() # NOTE de-duping tuple while keeping original order | ||
IMG_EXTENSIONS = tuple(x for x in extensions if x not in dedupe and not dedupe.add(x)) | ||
_IMG_EXTENSIONS_SET = set(extensions) | ||
|
||
|
||
def _valid_extension(x: str): | ||
return x and isinstance(x, str) and len(x) >= 2 and x.startswith('.') | ||
|
||
|
||
def is_img_extension(ext): | ||
return ext in _IMG_EXTENSIONS_SET | ||
|
||
|
||
def get_img_extensions(as_set=False): | ||
return deepcopy(_IMG_EXTENSIONS_SET if as_set else IMG_EXTENSIONS) | ||
|
||
|
||
def set_img_extensions(extensions): | ||
assert len(extensions) | ||
for x in extensions: | ||
assert _valid_extension(x) | ||
_set_extensions(extensions) | ||
|
||
|
||
def add_img_extensions(ext): | ||
if not isinstance(ext, (list, tuple, set)): | ||
ext = (ext,) | ||
for x in ext: | ||
assert _valid_extension(x) | ||
extensions = IMG_EXTENSIONS + tuple(ext) | ||
_set_extensions(extensions) | ||
|
||
|
||
def del_img_extensions(ext): | ||
if not isinstance(ext, (list, tuple, set)): | ||
ext = (ext,) | ||
extensions = tuple(x for x in IMG_EXTENSIONS if x not in ext) | ||
_set_extensions(extensions) |
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
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
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
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