MCPcopy
hub / github.com/lektor/lektor / load_raw_data

Method load_raw_data

lektor/db.py:1338–1392  ·  view source on GitHub ↗

Internal helper that loads the raw record data. This performs very little data processing on the data.

(self, path, alt=PRIMARY_ALT, cls=None, fallback=True)

Source from the content-addressed store, hash-verified

1336 return os.path.join(self.env.root_path, "content", untrusted_to_os_path(path))
1337
1338 def load_raw_data(self, path, alt=PRIMARY_ALT, cls=None, fallback=True):
1339 """Internal helper that loads the raw record data. This performs
1340 very little data processing on the data.
1341 """
1342 path = cleanup_path(path)
1343 if cls is None:
1344 cls = dict
1345
1346 fn_base = self.to_fs_path(path)
1347
1348 rv = cls()
1349 rv_type = None
1350
1351 choiceiter = _iter_filename_choices(
1352 fn_base, [alt], self.config, fallback=fallback
1353 )
1354 for fs_path, source_alt, is_attachment in choiceiter:
1355 # If we already determined what our return value is but the
1356 # type mismatches what we try now, we have to abort. Eg:
1357 # a page can not become an attachment or the other way round.
1358 if rv_type is not None and rv_type != is_attachment:
1359 break
1360
1361 try:
1362 with open(fs_path, "rb") as f:
1363 if rv_type is None:
1364 rv_type = is_attachment
1365 for key, lines in metaformat.tokenize(f, encoding="utf-8"):
1366 if key not in rv:
1367 rv[key] = "".join(lines)
1368 except OSError as e:
1369 if e.errno not in (errno.ENOTDIR, errno.ENOENT, errno.EINVAL):
1370 raise
1371 if not is_attachment or not os.path.isfile(fs_path[:-3]):
1372 continue
1373 # Special case: we are loading an attachment but the meta
1374 # data file does not exist. In that case we still want to
1375 # record that we're loading an attachment.
1376 if is_attachment:
1377 rv_type = True
1378
1379 if "_source_alt" not in rv:
1380 rv["_source_alt"] = source_alt
1381
1382 if rv_type is None:
1383 return None
1384
1385 rv["_path"] = path
1386 rv["_id"] = posixpath.basename(path)
1387 rv["_gid"] = hashlib.md5(path.encode("utf-8")).hexdigest()
1388 rv["_alt"] = alt
1389 if rv_type:
1390 rv["_attachment_for"] = posixpath.dirname(path)
1391
1392 return rv
1393
1394 def iter_items(self, path, alt=PRIMARY_ALT):
1395 """Iterates over all items below a path and yields them as

Callers 2

getMethod · 0.80
make_editor_sessionFunction · 0.80

Calls 4

to_fs_pathMethod · 0.95
cleanup_pathFunction · 0.90
_iter_filename_choicesFunction · 0.85
md5Method · 0.80

Tested by

no test coverage detected