| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Code for selecting the potential dialects of a file. |
| 5 |
|
| 6 |
Author: Gertjan van den Burg |
| 7 |
|
| 8 |
""" |
| 9 |
|
| 10 |
import codecs |
| 11 |
import itertools |
| 12 |
import unicodedata |
| 13 |
|
| 14 |
from typing import Dict |
| 15 |
from typing import Iterable |
| 16 |
from typing import List |
| 17 |
from typing import Optional |
| 18 |
from typing import Set |
| 19 |
|
| 20 |
from ._regexes import PATTERN_URL |
| 21 |
from .dialect import SimpleDialect |
| 22 |
from .escape import is_potential_escapechar |
| 23 |
from .utils import pairwise |
| 24 |
|
| 25 |
|
| 26 |
def get_dialects( |
| 27 |
data: str, |
| 28 |
encoding: str = "UTF-8", |
| 29 |
delimiters: Optional[List[str]] = None, |
| 30 |
test_masked_by_quotes: bool = False, |
| 31 |
) -> List[SimpleDialect]: |
| 32 |
"""Return the possible dialects for the given data. |
| 33 |
|
| 34 |
We consider as escape characters those characters for which |
| 35 |
is_potential_escapechar() is True and that occur at least once before a |
| 36 |
quote character or delimiter in the dialect. |
| 37 |
|
| 38 |
One may wonder if self-escaping is an issue here (i.e. "\\\\", two times |
| 39 |
backslash). It is not. In a file where a single backslash is desired and |
| 40 |
escaping with a backslash is used, then it only makes sense to do this in a |
| 41 |
file where the backslash is already used as an escape character (in which |
| 42 |
case we include it). If it is never used as escape for the delimiter or |
| 43 |
quotechar, then it is not necessary to self-escape. This is an assumption, |
| 44 |
but it holds in general and it reduces noise. |
| 45 |
|
| 46 |
Parameters |
| 47 |
---------- |
| 48 |
data: str |
| 49 |
The data for the file |
| 50 |
|
| 51 |
encoding: str |
| 52 |
The encoding of the file |
| 53 |
|
| 54 |
delimiters: iterable |
| 55 |
Set of delimiters to consider. See :func:`get_delimiters` for more |
| 56 |
info. |
| 57 |
|
| 58 |
test_masked_by_quotes : bool |
| 59 |
Remove dialects where the delimiter is always masked by the quote |
| 60 |
character. Enabling this typically removes a number of potential |
| 61 |
dialects from the list, which can remove false positives. It however |
| 62 |
not a very fast operation, so it is disabled by default. |
| 63 |
|
| 64 |
Returns |
| 65 |
------- |
| 66 |
dialects: List[SimpleDialect] |
| 67 |
List of SimpleDialect objects that are considered potential dialects. |
| 68 |
|
| 69 |
""" |
| 70 |
|
| 71 |
no_url = filter_urls(data) |
| 72 |
delims = get_delimiters(no_url, encoding, delimiters=delimiters) |
| 73 |
quotechars = get_quotechars(no_url) |
| 74 |
escapechars = {} |
| 75 |
|
| 76 |
for delim, quotechar in itertools.product(delims, quotechars): |
| 77 |
escapechars[(delim, quotechar)] = set([""]) |
| 78 |
|
| 79 |
is_escapechar_cache: Dict[str, bool] = {} |
| 80 |
|
| 81 |
|
| 82 |
for u, v in pairwise(data): |
| 83 |
if u not in is_escapechar_cache: |
| 84 |
is_escapechar_cache[u] = is_potential_escapechar(u, encoding) |
| 85 |
if not is_escapechar_cache[u]: |
| 86 |
continue |
| 87 |
|
| 88 |
for delim, quotechar in itertools.product(delims, quotechars): |
| 89 |
if v == delim or v == quotechar: |
| 90 |
escapechars[(delim, quotechar)].add(u) |
| 91 |
|
| 92 |
|
| 93 |
dialects = [] |
| 94 |
for delim in delims: |
| 95 |
for quotechar in quotechars: |
| 96 |
for escapechar in escapechars[(delim, quotechar)]: |
| 97 |
if test_masked_by_quotes and masked_by_quotechar( |
| 98 |
data, quotechar, escapechar, delim |
| 99 |
): |
| 100 |
continue |
| 101 |
d = SimpleDialect(delim, quotechar, escapechar) |
| 102 |
dialects.append(d) |
| 103 |
|
| 104 |
return dialects |
| 105 |
|
| 106 |
|
| 107 |
def unicode_category(x: str, encoding: str) -> str: |
| 108 |
"""Return the Unicode category of a character |
| 109 |
|
| 110 |
Parameters |
| 111 |
---------- |
| 112 |
x : str |
| 113 |
character |
| 114 |
|
| 115 |
encoding: str |
| 116 |
Encoding of the character |
| 117 |
|
| 118 |
Returns |
| 119 |
------- |
| 120 |
category: str |
| 121 |
The Unicode category of the character. |
| 122 |
|
| 123 |
""" |
| 124 |
as_unicode = codecs.decode(bytes(x, encoding), encoding=encoding) |
| 125 |
return unicodedata.category(as_unicode) |
| 126 |
|
| 127 |
|
| 128 |
def filter_urls(data: str) -> str: |
| 129 |
"""Filter URLs from the data""" |
| 130 |
return PATTERN_URL.sub("U", data) |
| 131 |
|
| 132 |
|
| 133 |
def get_delimiters( |
| 134 |
data: str, |
| 135 |
encoding: str, |
| 136 |
delimiters: Optional[List[str]] = None, |
| 137 |
block_cat: Optional[List[str]] = None, |
| 138 |
block_char: Optional[List[str]] = None, |
| 139 |
) -> Set[str]: |
| 140 |
"""Get potential delimiters |
| 141 |
|
| 142 |
The set of potential delimiters is constructed as follows. For each unique |
| 143 |
character of the file, we check if its Unicode character category is in the |
| 144 |
set ``block_cat`` of prohibited categories. If it is, we don't allow it to |
| 145 |
be a delimiter, with the exception of Tab (which is in the Control |
| 146 |
category). We furthermore block characters in :attr:`block_char` from |
| 147 |
being delimiters. |
| 148 |
|
| 149 |
Parameters |
| 150 |
---------- |
| 151 |
data: str |
| 152 |
The data of the file |
| 153 |
|
| 154 |
encoding: str |
| 155 |
The encoding of the file |
| 156 |
|
| 157 |
delimiters: iterable |
| 158 |
Allowed delimiters. If provided, it overrides the block_cat/block_char |
| 159 |
mechanism and only the provided characters will be considered |
| 160 |
delimiters (if they occur in the file). If None, all characters can be |
| 161 |
considered delimiters subject to the :attr:`block_cat` and |
| 162 |
:attr:`block_char` parameters. |
| 163 |
|
| 164 |
block_cat: list |
| 165 |
List of Unicode categories (2-letter abbreviations) for characters that |
| 166 |
should not be considered as delimiters. If None, the following default |
| 167 |
set is used:: |
| 168 |
|
| 169 |
["Lu", "Ll", "Lt", "Lm", "Lo", "Nd", "Nl", "No", "Ps", "Pe", "Co"] |
| 170 |
|
| 171 |
block_char: list |
| 172 |
Explicit list of characters that should not be considered delimiters. |
| 173 |
If None, the following default set is used:: |
| 174 |
|
| 175 |
[".", "/", '"', "'", "\\n", "\\r"] |
| 176 |
|
| 177 |
|
| 178 |
Returns |
| 179 |
------- |
| 180 |
delims: set |
| 181 |
Set of potential delimiters. The empty string is added by default. |
| 182 |
|
| 183 |
""" |
| 184 |
if block_cat is None: |
| 185 |
block_cat = [ |
| 186 |
"Lu", |
| 187 |
"Ll", |
| 188 |
"Lt", |
| 189 |
"Lm", |
| 190 |
"Lo", |
| 191 |
"Nd", |
| 192 |
"Nl", |
| 193 |
"No", |
| 194 |
"Ps", |
| 195 |
"Pe", |
| 196 |
"Co", |
| 197 |
] |
| 198 |
if block_char is None: |
| 199 |
block_char = [".", "/", '"', "'", "\n", "\r"] |
| 200 |
|
| 201 |
D = set() |
| 202 |
for x in set(data): |
| 203 |
c = unicode_category(x, encoding=encoding) |
| 204 |
if delimiters is None: |
| 205 |
if x == "\t" or ((x not in block_char) and (c not in block_cat)): |
| 206 |
D.add(x) |
| 207 |
else: |
| 208 |
if x in delimiters: |
| 209 |
D.add(x) |
| 210 |
D.add("") |
| 211 |
return D |
| 212 |
|
| 213 |
|
| 214 |
def get_quotechars( |
| 215 |
data: str, quote_chars: Optional[Iterable[str]] = None |
| 216 |
) -> Set[str]: |
| 217 |
"""Get potential quote characters |
| 218 |
|
| 219 |
Quote characters are those that occur in the ``quote_chars`` set and are |
| 220 |
found at least once in the file. |
| 221 |
|
| 222 |
Parameters |
| 223 |
---------- |
| 224 |
|
| 225 |
data: str |
| 226 |
The data of the file as a string |
| 227 |
|
| 228 |
quote_chars: iterable |
| 229 |
Characters that should be considered quote characters. If it is None, |
| 230 |
the following default set is used:: |
| 231 |
|
| 232 |
["'", '"', "~", "`"] |
| 233 |
|
| 234 |
|
| 235 |
Returns |
| 236 |
------- |
| 237 |
quotes: set |
| 238 |
Set of potential quote characters. The empty string is added by |
| 239 |
default. |
| 240 |
|
| 241 |
""" |
| 242 |
if quote_chars is None: |
| 243 |
quote_chars = ["'", '"', "~", "`"] |
| 244 |
Q = set(quote_chars) & set(data) |
| 245 |
Q.add("") |
| 246 |
return Q |
| 247 |
|
| 248 |
|
| 249 |
def masked_by_quotechar( |
| 250 |
data: str, quotechar: str, escapechar: str, test_char: str |
| 251 |
) -> bool: |
| 252 |
"""Test if a character is always masked by quote characters |
| 253 |
|
| 254 |
This function tests if a given character is always within quoted segments |
| 255 |
(defined by the quote character). Double quoting and escaping is supported. |
| 256 |
|
| 257 |
Parameters |
| 258 |
---------- |
| 259 |
data: str |
| 260 |
The data of the file as a string |
| 261 |
|
| 262 |
quotechar: str |
| 263 |
The quote character |
| 264 |
|
| 265 |
escapechar: str |
| 266 |
The escape character |
| 267 |
|
| 268 |
test_char: str |
| 269 |
The character to test |
| 270 |
|
| 271 |
Returns |
| 272 |
------- |
| 273 |
masked: bool |
| 274 |
Returns True if the test character is never outside quoted segements, |
| 275 |
False otherwise. |
| 276 |
|
| 277 |
""" |
| 278 |
if test_char == "": |
| 279 |
return False |
| 280 |
escape_next = False |
| 281 |
in_quotes = False |
| 282 |
i = 0 |
| 283 |
while i < len(data): |
| 284 |
s = data[i] |
| 285 |
if s == quotechar: |
| 286 |
if escape_next: |
| 287 |
i += 1 |
| 288 |
continue |
| 289 |
if not in_quotes: |
| 290 |
in_quotes = True |
| 291 |
else: |
| 292 |
if i + 1 < len(data) and data[i + 1] == quotechar: |
| 293 |
i += 1 |
| 294 |
else: |
| 295 |
in_quotes = False |
| 296 |
elif s == test_char and not in_quotes: |
| 297 |
return False |
| 298 |
elif s == escapechar: |
| 299 |
escape_next = True |
| 300 |
i += 1 |
| 301 |
return True |