Giter VIP home page Giter VIP logo

apply_subs's Introduction

apply-subs

PyPI PyPI codecov pre-commit.ci status Imports: isort Code style: black

A command line application to apply a dictionnary (json) of substitutions to text file corpus. This program is a find-and-replace tool to perform a arbitrarily large set of substitutions in a reproducible fashion.

Disclaimer, this app is far less powerful than sed and in particular doesn't support regex replacement. What it offers a simpler interface to make simple sweeping changes across a whole project in a reproducible fashion.

Installation

The easiest installation method is

$ pip install apply-subs

In order to install apply-subs in isolation, use pipx instead.

Examples

apply-subs uses a json file as input. This file should specify substitutions as new: old, where old can either be a single str, or a list of strings.

minimal case

echo "bunnies and bongos and bananas" > mytext.txt
echo '{"bunnies": "rabbits", "SECRETS": ["bongos", "bananas"]}' > mysubs.json
apply-subs mytext.txt -s mysubs.json

will print the patched content

bunnies and SECRETS and SECRETS

diff mode

Use diff mode (-d/--diff) to print a diff instead of the end result

--- mytext.txt
+++ mytext.txt (patched)
@@ -1 +1 @@
-Lorem ipsum dolor sit amet, consectetur adipiscing elit
+Hello dolor sit amet, consectetur goodbye

Use -cp/--cdiff/--colored-diff for a colored output (when supported).

inplace substitutions

-i/--inplace

apply-subs --inplace mytext.txt -s mysubs.json

is equivalent to

apply-subs mytext.txt -s mysubs.json > mytext.txt

target several files in one go

The target positional argument can consist of a single file (as illustrated above), or many. This is useful for instance if you need to apply a set of subtitutions to all files in a project whose name match a regexp.

git ls-files | egrep "(.md|.py)$" | xargs apply-subs -s subsubs.json -i

apply_subs's People

Contributors

neutrinoceros avatar pre-commit-ci[bot] avatar

Watchers

 avatar  avatar

apply_subs's Issues

doc: adress sed

Disclaimer, this app is far less powerful than sed and in particular doesn't support regex replacement, but
It offers a simpler interface to make simple sweeping changes in a reproducible fashion.

Feature: reverse mode

Add a --reverse flag to swap between old and new in parsing. This will only work for key to key schéma and be invalid for key to list.
This should be dealt with after #13

feature: apply subs to multiple targets

This will require changing the order of positional arguments (version 0.3.0)

goal: make this valid

git ls-files | grep ".py" | xargs python -m apply_subs -s subsubs.json -i

Missing doc

The README should mention the possibility of running more than one target and give an example with git ls-files

Feature: multiple entries to one sub

The main function should be able to parse data formatted as

{
  "key1": [
    "form1",
    "FORM2",
    "FoRm3"
  ]
}

⚠️ this implies that the default behavior should be to parse {new: old}
which is the opposite of the current implementation.

This also requires some schema validation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.