You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features.md
+74-11Lines changed: 74 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,15 @@
1
-
Basically, the `codecs` library, relying on the built-in `_codecs` library, maintains a registry of search functions that maps an input `encoding` variable to the right de/encode function. `codext` hooks the native `codecs` to insert its own registry between the function calls and the native one.
1
+
Basically, the `codecs` library provides a series of functions from the built-in `_codecs` library which maintains a registry of search functions (a simple list) that maps ancodings to the right de/encode functions by returning a `CodecInfo` object once first matched.
2
2
3
-
!!! note "`codecs` import and the `open` built-in function"
3
+
`codext` hooks `codecs`'s functions to insert its own proxy registry between the function calls and the native registry so that new encodings can be added or replace existing ones while using `code[cs|xt].open`. Indeed, as the proxy registry is called first, the first possible match occurs in a custom codec, while if not existing, the native registry is used.
4
+
5
+
!!! note "The `open` built-in function"
6
+
7
+
Two behaviors are to be considered when using `codext`:
4
8
5
-
When `codext` is imported, the new encodings are added to its registry but also to the native one. Moreover, hooked functions are bound to the `codext` module but also overwrites the original ones in `codecs`. Consequently:
9
+
1. Encodings added from `codext` are only added to the proxy codecs registry of `codext` and are NOT available using `open(...)` (but well using `code[cs|xt].open(...)`.
10
+
2. Encodings added from `codecs` are added to the proxy registry AND ALSO to the native registry and are therefore available using `open(...)`.
6
11
7
-
1. Once `codext` has been imported, `codecs` can be imported elsewhere in the program and will have the `add` and hooked functions attached, with the new encodings available.
8
-
2. While `codecs.open` will handle the new encodings according to `codext`'s registry first, the native `open` function will rely on the native registry only and therefore handle the encoding search functions of `codext`_after_ these of the native registry has it will rely on non-hooked functions.
12
+
This difference allows to keep encodings added from `codext` removable while these added from `codecs` are not. This is the consequence from the fact that there is no unregister function in the native `_codecs` library.
9
13
10
14
-----
11
15
@@ -18,16 +22,20 @@ New codecs can be added easily using the new function `add`.
This adds a new codec to the codecs module setting its encode and/or decode
23
27
functions, eventually dynamically naming the encoding with a pattern and
24
28
withfile handling (if text isTrue).
25
29
26
-
:param ename: encoding name
27
-
:param encode: encoding function orNone
28
-
:param decode: decoding function orNone
29
-
:param pattern: pattern for dynamically naming the encoding
30
-
:param text: specify whether the codec is a text encoding
30
+
:param ename: encoding name
31
+
:param encode: encoding function orNone
32
+
:param decode: decoding function orNone
33
+
:param pattern: pattern for dynamically naming the encoding
34
+
:param text: specify whether the codec is a text encoding
35
+
:param add_to_codecs: also add the search function to the native registry
36
+
NB: this will make the codec available in the
37
+
built-inopen(...) but will make it impossible
38
+
to remove the codec later
31
39
32
40
```
33
41
@@ -78,6 +86,61 @@ In this second example, we can see that:
78
86
79
87
-----
80
88
89
+
## Remove a custom encoding
90
+
91
+
New codecs can be removed easily using the new function `remove`, which will only remove every codec matching the given encoding name in the proxy codecs registry and NOT in the native one.
92
+
93
+
```python
94
+
>>> codext.encode("test", "bin")
95
+
'01110100011001010111001101110100'
96
+
>>> codext.remove("bin")
97
+
>>> codext.encode("test", "bin")
98
+
99
+
Traceback (most recent call last):
100
+
File "<pyshell#39>", line 1, in<module>
101
+
codext.encode("test", "bin")
102
+
File "codext/__common__.py", line 245, in __encode
103
+
return __lookup(encoding).encode(obj, errors)[0]
104
+
File "codext/__common__.py", line 259, in __lookup
105
+
codecs.lookup = __lookup
106
+
LookupError: unknown encoding: bin
107
+
```
108
+
109
+
While trying to remove a codec that is in the native registry won't raise a `LookupError`.
110
+
111
+
```python
112
+
>>> codext.remove("utf-8")
113
+
>>> codext.encode("test", "utf-8")
114
+
b'test'
115
+
```
116
+
117
+
-----
118
+
119
+
## Remove or restore `codext` encodings
120
+
121
+
It can be useful while playing with encodings e.g. from Idle to be able to remove or restore `codext`'s encodings. This can be achieved using respectively the new `clear` and `reset` functions.
122
+
123
+
```python
124
+
>>> codext.clear()
125
+
>>> codext.encode("test", "bin")
126
+
Traceback (most recent call last):
127
+
File "<pyshell#4>", line 1, in<module>
128
+
codext.encode("test", "bin")
129
+
File "/mnt/data/Projects/maint/python-codext/codext/__common__.py", line 245, in __encode
130
+
return __lookup(encoding).encode(obj, errors)[0]
131
+
File "/mnt/data/Projects/maint/python-codext/codext/__common__.py", line 258, in __lookup
132
+
return orig_lookup(encoding)
133
+
LookupError: unknown encoding: bin
134
+
```
135
+
136
+
```python
137
+
>>> codext.reset()
138
+
>>> codext.encode("test", "bin")
139
+
'01110100011001010111001101110100'
140
+
```
141
+
142
+
-----
143
+
81
144
## Hooked `codecs` functions
82
145
83
146
In order to select the right de/encoding function and avoid any conflict, the native `codecs` library registers search functions (using the `register(search_function)` function), called in order of registration while searching for a codec.
0 commit comments