Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Jekyll-style YAML front matter offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.
This is a small package to load and parse files (or just text) with YAML (or JSON, TOML or other) front matter.
pip install python-frontmatter
>>> import frontmatter
Load a post from a filename:
>>> post = frontmatter.load('tests/yaml/hello-world.txt')
Or a file (or file-like object):
>>> with open('tests/yaml/hello-world.txt') as f:
... post = frontmatter.load(f)
Or load from text:
>>> with open('tests/yaml/hello-world.txt') as f:
... post = frontmatter.loads(f.read())
If the file has a Byte-Order Mark (BOM), strip it off first. An easy way to do this is by using the utf-8-sig
encoding:
>>> with open('tests/yaml/hello-world.txt', encoding="utf-8-sig") as f:
... post = frontmatter.load(f)
Access content:
>>> print(post.content)
Well, hello there, world.
# this works, too
>>> print(post)
Well, hello there, world.
Use metadata (metadata gets proxied as post keys):
>>> print(post['title'])
Hello, world!
Metadata is a dictionary, with some handy proxies:
>>> sorted(post.keys())
['layout', 'title']
>>> from pprint import pprint
>>> post['excerpt'] = 'tl;dr'
>>> pprint(post.metadata)
{'excerpt': 'tl;dr', 'layout': 'post', 'title': 'Hello, world!'}
If you don't need the whole post object, just parse:
>>> with open('tests/yaml/hello-world.txt') as f:
... metadata, content = frontmatter.parse(f.read())
>>> print(metadata['title'])
Hello, world!
Write back to plain text, too:
>>> print(frontmatter.dumps(post)) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.
Or write to a file (or file-like object):
>>> from io import BytesIO
>>> f = BytesIO()
>>> frontmatter.dump(post, f)
>>> print(f.getvalue().decode('utf-8')) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.
For more examples, see files in the tests/
directory. Each sample file has a corresponding .result.json
file showing the expected parsed output. See also the examples/
directory, which covers more ways to customize input and output.
FAQs
Parse and manage posts with YAML (or other) frontmatter
We found that python-frontmatter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.