Skip to content

Commit 69e6009

Browse files
committed
Improved list_encodings function
1 parent a6a6090 commit 69e6009

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exclude_lines =
1010
if.*?__name__.*?==.*?.__main__.:
1111
def main\(\)\:
1212
def __stdin_pipe\(\)\:
13+
for line in __stdin_pipe\(\)\:
1314
def __literal_eval\(o\)\:
1415
def __print_tabular\(lst, space\=4\)\:
1516
except ImportError:

codext/__common__.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import random
77
import re
88
import sys
9-
from encodings.aliases import aliases
9+
from encodings.aliases import aliases as ALIASES
1010
from functools import reduce, wraps
1111
from importlib import import_module
1212
from inspect import currentframe
@@ -40,10 +40,7 @@
4040
"DARWIN", "LANG", "LINUX", "MASKS", "PY3", "UNIX", "WINDOWS"]
4141
CODECS_REGISTRY = None
4242
CODECS_CATEGORIES = ["native", "custom"]
43-
try:
44-
LANG = getlocale()[0][:2].lower()
45-
except TypeError:
46-
LANG = None
43+
LANG = getlocale()[0][:2].lower() if getlocale() else None
4744
MASKS = {
4845
'a': printable,
4946
'b': "".join(chr(i) for i in range(256)),
@@ -601,7 +598,7 @@ def examples(encoding, number=10):
601598
except LookupError:
602599
pass
603600
i += 1
604-
for alias, codec in aliases.items():
601+
for alias, codec in ALIASES.items():
605602
if name == codec:
606603
if codec not in e:
607604
e.append(codec)
@@ -634,19 +631,25 @@ def list_encodings(*categories):
634631
# first, determine the list of valid categories
635632
valid_categories = list_categories()
636633
# then, if "non-native" is in the input list, extend the list with the whole categories but "native"
637-
categories = list(categories)
634+
categories, exclude = list(categories), []
638635
for c in categories[:]:
639636
if c == "non-native":
640637
for c in valid_categories:
641638
if c == "native" or c in categories:
642639
continue
643640
categories.append(c)
644641
categories.remove("non-native")
645-
break
642+
if c.startswith("~"):
643+
exclude.append(c[1:])
644+
categories.remove(c)
645+
try:
646+
categories.remove(c[1:])
647+
except ValueError:
648+
pass
646649
# now, filter codecs according to the input list of categories
647650
enc = []
648-
if len(categories) == 0 or "native" in categories:
649-
for a in set(aliases.values()):
651+
if (len(categories) == 0 or "native" in categories) and "native" not in exclude:
652+
for a in set(ALIASES.values()):
650653
try:
651654
__orig_lookup(a)
652655
except LookupError:
@@ -660,7 +663,7 @@ def list_encodings(*categories):
660663
else:
661664
ci = search_function(generate_string_from_regex(p))
662665
c = "other" if ci is None else ci.parameters['category']
663-
if len(categories) == 0 or c in categories:
666+
if (len(categories) == 0 or c in categories) and c not in exclude:
664667
enc.append(name)
665668
for category in categories:
666669
if category not in valid_categories:
@@ -873,7 +876,7 @@ def lookup(encoding, macro=True):
873876
try:
874877
# finally, get a CodecInfo with the original lookup function and refine it with a dictionary of parameters
875878
ci = __orig_lookup(encoding)
876-
ci.parameters = {'category': "native", 'module': "codecs", 'name': aliases.get(ci.name, ci.name)}
879+
ci.parameters = {'category': "native", 'module': "codecs", 'name': ALIASES.get(ci.name, ci.name)}
877880
return ci
878881
except LookupError:
879882
if not macro:
@@ -932,7 +935,7 @@ def search(encoding_regex):
932935
if c >= 3:
933936
matches.append(n)
934937
break
935-
for s, n in aliases.items():
938+
for s, n in ALIASES.items():
936939
if re.search(encoding_regex, s) or re.search(encoding_regex, n):
937940
matches.append(n)
938941
break

tests/test_common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def test_list_codecs(self):
9090
self.assertTrue(len(codext.list("non-native")) > 0)
9191
self.assertTrue(len(codext.list("native", "non-native", "crypto", "base")) > 0)
9292
self.assertTrue(len(codext.list("native", "language", "crypto")) > 0)
93+
self.assertTrue(len(codext.list("~crypto")) > 0)
94+
self.assertEqual(set(codext.list("~native")), set(codext.list("non-native")))
9395
self.assertEqual(set(codext.list()), set(codext.list("native") + codext.list("non-native")))
9496
self.assertRaises(ValueError, codext.list, "BAD_CATEGORY")
9597
self.assertTrue(codext.is_native("base64_codec"))

0 commit comments

Comments
 (0)