| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
Shared code for dependency files |
| 19 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 20 |
|
| 21 |
""" |
| 22 |
|
| 23 |
import logging as _logging |
| 24 |
import re as _re |
| 25 |
|
| 26 |
from .. import _parse |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
logger = _logging.getLogger("deps.suggest.file_model") |
| 31 |
|
| 32 |
|
| 33 |
class Model(object): |
| 34 |
""" |
| 35 |
File model base |
| 36 |
|
| 37 |
Attributes: |
| 38 |
parts (list): |
| 39 |
The file parts (strings vs literals) |
| 40 |
""" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
type = None |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
encoding = "latin-1" |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
require_version = None |
| 56 |
|
| 57 |
def __init__(self, parts): |
| 58 |
""" |
| 59 |
Initialization |
| 60 |
|
| 61 |
Parameters: |
| 62 |
parts (list): |
| 63 |
The file parts |
| 64 |
""" |
| 65 |
self.parts = parts |
| 66 |
|
| 67 |
@classmethod |
| 68 |
def by_name(cls, filename): |
| 69 |
""" |
| 70 |
Construct by filename |
| 71 |
|
| 72 |
Parameters: |
| 73 |
filename (str): |
| 74 |
The filename |
| 75 |
|
| 76 |
Returns: |
| 77 |
Model: New container instance |
| 78 |
""" |
| 79 |
with open(filename, "rb") as fp: |
| 80 |
return cls(cls._parse_stream(fp)) |
| 81 |
|
| 82 |
@classmethod |
| 83 |
def _parse_stream(cls, fp): |
| 84 |
""" |
| 85 |
Parse from binary file stream |
| 86 |
|
| 87 |
Parameters: |
| 88 |
fp (file): |
| 89 |
Binary file stream |
| 90 |
|
| 91 |
Returns: |
| 92 |
list: The parts |
| 93 |
""" |
| 94 |
raise NotImplementedError() |
| 95 |
|
| 96 |
|
| 97 |
class ReqFile(Model): |
| 98 |
"""Requirement file container""" |
| 99 |
|
| 100 |
type = "text" |
| 101 |
encoding = "latin-1" |
| 102 |
require_version = False |
| 103 |
|
| 104 |
@classmethod |
| 105 |
def _parse_stream(cls, fp): |
| 106 |
""" |
| 107 |
Parse req file |
| 108 |
""" |
| 109 |
parts = [] |
| 110 |
for line in fp: |
| 111 |
line = line.decode(cls.encoding) |
| 112 |
xline = line.strip() |
| 113 |
if not xline or xline.startswith("#"): |
| 114 |
parts.append(LiteralPart(line)) |
| 115 |
else: |
| 116 |
parts.append(StringPart.from_text(line)) |
| 117 |
|
| 118 |
return parts |
| 119 |
|
| 120 |
|
| 121 |
class SetupFile(Model): |
| 122 |
"""Setup file container""" |
| 123 |
|
| 124 |
type = "setup" |
| 125 |
encoding = "latin-1" |
| 126 |
require_version = True |
| 127 |
|
| 128 |
@classmethod |
| 129 |
def _parse_stream(cls, fp): |
| 130 |
""" |
| 131 |
Parse setup file |
| 132 |
""" |
| 133 |
parts = [] |
| 134 |
file_content = fp.read().decode(cls.encoding) |
| 135 |
|
| 136 |
last = 0 |
| 137 |
for match in _parse.find_py_str_or_comment(file_content): |
| 138 |
|
| 139 |
|
| 140 |
if match.group(2): |
| 141 |
continue |
| 142 |
|
| 143 |
if match.start() > last: |
| 144 |
parts.append(LiteralPart(file_content[last : match.start()])) |
| 145 |
last = match.end() |
| 146 |
raw = match.start() > 0 and file_content[match.start() - 1] == "r" |
| 147 |
parts.append(StringPart.from_python(raw, match.group(1))) |
| 148 |
|
| 149 |
if len(file_content) > last: |
| 150 |
parts.append(LiteralPart(file_content[last:])) |
| 151 |
|
| 152 |
return parts |
| 153 |
|
| 154 |
|
| 155 |
class TomlFile(Model): |
| 156 |
""" |
| 157 |
TOML file container |
| 158 |
""" |
| 159 |
|
| 160 |
type = "toml" |
| 161 |
encoding = "utf-8" |
| 162 |
require_version = True |
| 163 |
|
| 164 |
@classmethod |
| 165 |
def _parse_stream(cls, fp): |
| 166 |
""" |
| 167 |
Parse toml file |
| 168 |
""" |
| 169 |
parts = [] |
| 170 |
file_content = fp.read().decode(cls.encoding) |
| 171 |
|
| 172 |
last = 0 |
| 173 |
for match in _parse.find_toml_str_or_comment(file_content): |
| 174 |
|
| 175 |
|
| 176 |
if match.group(2): |
| 177 |
continue |
| 178 |
|
| 179 |
if match.start() > last: |
| 180 |
parts.append(LiteralPart(file_content[last : match.start()])) |
| 181 |
last = match.end() |
| 182 |
raw = file_content[match.start()] == "'" |
| 183 |
parts.append(StringPart.from_toml(raw, match.group(1))) |
| 184 |
|
| 185 |
if len(file_content) > last: |
| 186 |
parts.append(LiteralPart(file_content[last:])) |
| 187 |
|
| 188 |
return parts |
| 189 |
|
| 190 |
|
| 191 |
class StringPart(object): |
| 192 |
""" |
| 193 |
String part (of a dep file) |
| 194 |
|
| 195 |
Attributes: |
| 196 |
raw (bool): |
| 197 |
Is it a raw string? |
| 198 |
|
| 199 |
quote (str): |
| 200 |
The quote used |
| 201 |
|
| 202 |
content (str): |
| 203 |
The string content, escaped quotes stripped if not raw |
| 204 |
""" |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
is_literal = False |
| 210 |
|
| 211 |
def __init__(self, raw, quote, content): |
| 212 |
""" |
| 213 |
Initialization |
| 214 |
|
| 215 |
:Parameters: |
| 216 |
raw (bool): |
| 217 |
Is it a raw string? |
| 218 |
|
| 219 |
quote (str): |
| 220 |
The quote used |
| 221 |
|
| 222 |
content (str): |
| 223 |
The string content, escaped quotes stripped if not raw |
| 224 |
""" |
| 225 |
self.raw = raw |
| 226 |
self.quote = quote |
| 227 |
self.content = content |
| 228 |
|
| 229 |
def __eq__(self, other): |
| 230 |
""" |
| 231 |
Return equality |
| 232 |
|
| 233 |
Returns: |
| 234 |
bool: Is it equal to other? |
| 235 |
""" |
| 236 |
if not isinstance(other, self.__class__): |
| 237 |
return False |
| 238 |
return (self.raw, self.quote, self.content) == ( |
| 239 |
other.raw, |
| 240 |
other.quote, |
| 241 |
other.content, |
| 242 |
) |
| 243 |
|
| 244 |
def __ne__(self, other): |
| 245 |
""" |
| 246 |
Return non-equality |
| 247 |
|
| 248 |
Returns: |
| 249 |
bool: Is it not equal to other? |
| 250 |
""" |
| 251 |
return not self == other |
| 252 |
|
| 253 |
def __repr__(self): |
| 254 |
""" |
| 255 |
Create repr of this container |
| 256 |
|
| 257 |
Returns: |
| 258 |
str: Representation of the container |
| 259 |
""" |
| 260 |
return "%s(%r, %r, %r)" % ( |
| 261 |
self.__class__.__name__, |
| 262 |
self.raw, |
| 263 |
self.quote, |
| 264 |
self.content, |
| 265 |
) |
| 266 |
|
| 267 |
@classmethod |
| 268 |
def from_python(cls, raw, content): |
| 269 |
""" |
| 270 |
Construct string from python-encoded |
| 271 |
|
| 272 |
Parameters: |
| 273 |
raw (bool): |
| 274 |
Is it a raw string? |
| 275 |
|
| 276 |
content (str): |
| 277 |
The string content from python, including quotes |
| 278 |
|
| 279 |
Returns: |
| 280 |
String: New string container |
| 281 |
""" |
| 282 |
if content.startswith(('"""', "'''")): |
| 283 |
quote, content = content[:3], content[3:-3] |
| 284 |
else: |
| 285 |
quote, content = content[0], content[1:-1] |
| 286 |
if not raw: |
| 287 |
content = _re.sub( |
| 288 |
r"(?s)\\(.)", |
| 289 |
lambda m: m.group(m.group(1) in "'\""), |
| 290 |
content, |
| 291 |
) |
| 292 |
return cls(raw, quote, content) |
| 293 |
|
| 294 |
from_toml = from_python |
| 295 |
|
| 296 |
@classmethod |
| 297 |
def from_text(cls, content): |
| 298 |
""" |
| 299 |
Construct string from text |
| 300 |
|
| 301 |
Parameters: |
| 302 |
content (str): |
| 303 |
The string content from text file |
| 304 |
|
| 305 |
Returns: |
| 306 |
String: New string container |
| 307 |
""" |
| 308 |
return cls(True, "", content) |
| 309 |
|
| 310 |
def __str__(self): |
| 311 |
""" |
| 312 |
Rebuild the string part as close to the original as possible |
| 313 |
|
| 314 |
Returns: |
| 315 |
str: The string, quoted, quotes escaped if needed |
| 316 |
""" |
| 317 |
content = self.content |
| 318 |
if not self.raw: |
| 319 |
content = content.replace(self.quote, "\\%s" % (self.quote,)) |
| 320 |
return "%s%s%s" % (self.quote, content, self.quote) |
| 321 |
|
| 322 |
|
| 323 |
class LiteralPart(object): |
| 324 |
""" |
| 325 |
Literal part (of a dep file) |
| 326 |
|
| 327 |
Attributes: |
| 328 |
content (str): |
| 329 |
The part content |
| 330 |
""" |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
is_literal = True |
| 336 |
|
| 337 |
def __init__(self, content): |
| 338 |
""" |
| 339 |
Initialization |
| 340 |
|
| 341 |
Parameters: |
| 342 |
content (str): |
| 343 |
The part content |
| 344 |
""" |
| 345 |
self.content = content |
| 346 |
|
| 347 |
def __eq__(self, other): |
| 348 |
""" |
| 349 |
Return equality |
| 350 |
|
| 351 |
Returns: |
| 352 |
bool: Is it equal to other? |
| 353 |
""" |
| 354 |
if not isinstance(other, self.__class__): |
| 355 |
return False |
| 356 |
return self.content == other.content |
| 357 |
|
| 358 |
def __ne__(self, other): |
| 359 |
""" |
| 360 |
Return non-equality |
| 361 |
|
| 362 |
Returns: |
| 363 |
bool: Is it not equal to other? |
| 364 |
""" |
| 365 |
return not self == other |
| 366 |
|
| 367 |
def __repr__(self): |
| 368 |
""" |
| 369 |
Create repr of this container |
| 370 |
|
| 371 |
Returns: |
| 372 |
str: Representation of the container |
| 373 |
""" |
| 374 |
return "%s(%r)" % (self.__class__.__name__, self.content) |
| 375 |
|
| 376 |
def __str__(self): |
| 377 |
""" |
| 378 |
Rebuild the part as string |
| 379 |
|
| 380 |
Returns: |
| 381 |
str: The part as string |
| 382 |
""" |
| 383 |
return self.content |