Skip to main content

nginx config file formatter/beautifier with no additional dependencies.

Project description

nginx config file formatter/beautifier

nginx config file formatter/beautifier written in Python with no additional dependencies. It can be used as library or standalone script. It formats nginx configuration files in consistent way, described below:

  • All lines are indented in uniform manner, with 4 spaces per level. Number of spaces is customizable.
  • Neighbouring empty lines are collapsed to at most two empty lines.
  • Curly braces placement follows Java convention.
  • Whitespaces are collapsed, except in comments and quotation marks.

Installation

Python 3.4 or later is needed to run this program. The easiest way is to download package from PyPI:

pip3 install nginxfmt

Manual installation

The simplest form of installation would be copying nginxfmt.py to your scripts directory. It has no 3-rd party dependencies.

You can also clone the repository and symlink the executable:

cd
git clone https://github.com/slomkowski/nginx-config-formatter.git
ln -s ~/nginx-config-formatter/nginxfmt.py ~/bin/nginxfmt.py

Usage as standalone script

It can format one or several files. Result is by default saved to the original file, but can be redirected to stdout. It can also function in piping mode, with --pipe switch.

usage: nginxfmt.py [-h] [-v] [-] [-p | -b] [-i INDENT] [config_files ...]

Formats nginx configuration files in consistent way.

positional arguments:
  config_files          configuration files to format

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         show formatted file names
  -, --pipe             reads content from standard input, prints result to stdout
  -p, --print-result    prints result to stdout, original file is not changed
  -b, --backup-original
                        backup original config file as filename.conf~

formatting options:
  -i INDENT, --indent INDENT
                        specify number of spaces for indentation

Using as library

Main logic is within Formatter class, which can be used in 3rd-party code.

import nginxfmt

# initializing with standard FormatterOptions
f = nginxfmt.Formatter()

# format from string
formatted_text = f.format_string(unformatted_text)

# format file and save result to the same file
f.format_file(unformatted_file_path)

# format file and save result to the same file, original unformatted content is backed up
f.format_file(unformatted_file_path, backup_path)

Customizing formatting options:

import nginxfmt

fo = nginxfmt.FormatterOptions()
fo.indentation = 2  # 2 spaces instead of default 4

# initializing with standard FormatterOptions
f = nginxfmt.Formatter(fo)

Reporting bugs

Please create issue under https://github.com/slomkowski/nginx-config-formatter/issues. Be sure to add config snippets to reproduce the issue, preferably:

  • snippet do be formatted
  • actual result with invalid formatting
  • desired result

Credits

Copyright 2021 Michał Słomkowski. License: Apache 2.0. Previously published under https://github.com/1connect/nginx-config-formatter.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nginxfmt-1.2.2.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nginxfmt-1.2.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file nginxfmt-1.2.2.tar.gz.

File metadata

  • Download URL: nginxfmt-1.2.2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.3 Linux/5.11.15-arch1-2

File hashes

Hashes for nginxfmt-1.2.2.tar.gz
Algorithm Hash digest
SHA256 fbde172d2791d7e44f484615036c8cde2b8d3f9720009ba8f44628838a01804d
MD5 df3fef67566bd3830802c86df08acafe
BLAKE2b-256 25e7f9a5a4288a8a949b781ce155c2de6876ced1c90b0a06e160812138e1cd8c

See more details on using hashes here.

File details

Details for the file nginxfmt-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: nginxfmt-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.3 Linux/5.11.15-arch1-2

File hashes

Hashes for nginxfmt-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0f27c437e84b7142e879402b699102bf63ee4ced6d0fd7d5b96ca49a3d8a566b
MD5 7c5f5319003c6369e27fbc65319987d5
BLAKE2b-256 a80a6fc0727dc1e5fdfaf91af26af6740713ddc5ecea2f9cac9e21ac4064f0d5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page