Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to be closed #11

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
a3d55c4
feat/intial-v initial code
hminaee-tc Jun 12, 2024
9603a50
feat/intial-v initial code
hminaee-tc Jun 12, 2024
b529922
feat/intial-v initial code
hminaee-tc Jun 12, 2024
3f25857
feat/intial-v initial code
hminaee-tc Jun 12, 2024
7a082ad
feat/intial-v initial code
hminaee-tc Jun 12, 2024
818c988
feat/intial-v initial code
hminaee-tc Jun 12, 2024
0313659
feat/intial-v initial code
hminaee-tc Jun 12, 2024
77a40c1
feat/intial-v initial code
hminaee-tc Jun 12, 2024
a600786
feat/intial-v initial code
hminaee-tc Jun 12, 2024
e529f7d
feat/intial-v initial code
hminaee-tc Jun 12, 2024
122b152
feat/intial-v initial code
hminaee-tc Jun 12, 2024
18ba185
feat/intial-v initial code
hminaee-tc Jun 12, 2024
d108c05
feat/intial-v initial code
hminaee-tc Jun 12, 2024
073a227
feat/intial-v initial code
hminaee-tc Jun 12, 2024
1a43b34
feat/intial-v initial code
hminaee-tc Jun 12, 2024
7f2f564
feat/intial-v initial code
hminaee-tc Jun 12, 2024
644caf2
feat/intial-v initial code
hminaee-tc Jun 12, 2024
7353f8d
feat/intial-v initial code
hminaee-tc Jun 12, 2024
6c9a6c6
feat/intial-v initial code
hminaee-tc Jun 12, 2024
da5eadf
feat/intial-v initial code
hminaee-tc Jun 12, 2024
6cc413a
feat/intial-v initial code
hminaee-tc Jun 12, 2024
c69171b
feat/intial-v initial code
hminaee-tc Jun 12, 2024
f590033
feat/intial-v unit test
hminaee-tc Jun 13, 2024
8469058
feat/intial-v unit test
hminaee-tc Jun 13, 2024
dd3ffd5
feat/intial-v unit test
hminaee-tc Jun 13, 2024
315b947
feat/initial-v-update-usability-and-readability-setup-cfg-metadata
andreouellet Jun 13, 2024
c57f89f
feat/intial-v fix publishing
hminaee-tc Jun 13, 2024
8427546
feat/intial-v fix tests
hminaee-tc Jun 13, 2024
128f5b2
feat/intial-v update the hook
hminaee-tc Jun 13, 2024
2edcfaa
feat/intial-v update the hook
hminaee-tc Jun 13, 2024
e305ae4
feat/intial-v update readme
hminaee-tc Jun 13, 2024
b35548f
feat/intial-v version update
hminaee-tc Jun 13, 2024
8559e60
feat/intial-v update names
hminaee-tc Jun 13, 2024
1ef5339
test toml file
hminaee-tc Jun 13, 2024
84b9376
add toml file
hminaee-tc Jun 13, 2024
4c8c282
fix exclusion
hminaee-tc Jun 13, 2024
074c57c
merge conflicct
hminaee-tc Jun 13, 2024
24ac255
feat/intial-v-update-publish-pypi-ghactions-only-run-on-merged-pr
andreouellet Jun 13, 2024
e06bf97
feat/intial-v rename folder
hminaee-tc Jun 13, 2024
d6fb84f
feat/intial-v clean up
hminaee-tc Jun 13, 2024
67e3d56
feat/intial-v clean up
hminaee-tc Jun 13, 2024
faa5dd9
feat/intial-v add commit hook
hminaee-tc Jun 14, 2024
1dcd360
feat/intial-v add commit hook
hminaee-tc Jun 14, 2024
d5b1927
feat/intial-v publishing the package
hminaee-tc Jun 14, 2024
ff3ed8d
feat/intial-v publishing the package
hminaee-tc Jun 14, 2024
496c37f
feat/intial-v publishing the package
hminaee-tc Jun 14, 2024
71b40b4
feat/intial-v version hook
hminaee-tc Jun 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Publish Python 🐍 distributions 📦 to PyPI

on:
pull_request:
branches:
- main

jobs:
build-n-publish:
name: Build and publish Python 🐍 distributions 📦 to PyPI
runs-on: ubuntu-latest
permissions:
id-token: write

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v5.1.0
with:
python-version: 3.11

- name: Install pip packages
run: pip install twine build setuptools

- name: Build the package
run: python -m build

- name: Publish release distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
skip-existing: true

# - name: Publish the package
# run: twine upload --repository-url https://upload.pypi.org/legacy/ -u __token__ -p ${{ secrets.PYPI_TOKEN }} ${{ github.workspace }}/dist/*
27 changes: 27 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Unit tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
unit-tests:
name: Build and publish Python 🐍 distributions 📦 to PyPI
runs-on: ubuntu-latest
permissions:
id-token: write

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v5.1.0
with:
python-version: 3.11

- name: Run tests
run: python3 -m unittest tests/test_main.py
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ __pycache__/
*.py[cod]
*$py.class

.DS_Store

# C extensions
*.so

Expand Down
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-merge-conflict
- id: check-added-large-files
args: [--maxkb=500]
- id: trailing-whitespace
- id: detect-private-key
- id: end-of-file-fixer
- id: fix-encoding-pragma
- id: file-contents-sorter
- id: check-case-conflict
- id: mixed-line-ending
args: [--fix=lf]

- repo: https://github.com/zricethezav/gitleaks
rev: v8.18.1
hooks:
- id: gitleaks

- repo: local
hooks:
- id: check-version
name: Check version
entry: python assets/check_version.py
language: system
files: pyproject.toml
5 changes: 5 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- id: find-and-replace-strings
name: find-and-replace-strings
description: Finds strings in files and replaces them with other strings.
entry: find-and-replace-strings
language: python
137 changes: 136 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,137 @@
# find-and-replace
Python package and pre-commit-hook for finding and replacing string(s) in file(s)

Python package and pre-commit-hook for finding and replacing string(s) in file(s).

## Installation

This is an easy to use package which is already available here https://pypi.org/project/find-and-replace-template-commit-check/:

![package to use](./assets/pypi-package.png "Title")

You can install the package via pip:

```bash
pip install find-and-replace-strings
```

## Usage

To use this package, you need to add it to your pre-commit configuration file (.pre-commit-config.yaml). Here's an example:

For config mod

```
repos:
- repo: https://github.com/opencepk/find-and-replace
rev: v0.0.1
hooks:
- id: find-and-replace-strings
name: find-and-replace-strings
description: Find and replace strings
entry: find-and-replace-strings
language: python
pass_filenames: true
exclude_types:
- binary
files: README.md
verbose: true

```

and for direct mode

```
repos:
- repo: https://github.com/opencepk/find-and-replace
rev: v0.0.1
hooks:
- id: find-and-replace-strings
name: find-and-replace-strings
description: Find and replace strings
entry: find-and-replace-strings
language: python
pass_filenames: true
exclude_types:
- binary
args: ["--find", "search_string", "--replacement", "replacement_string"]
files: README.md
verbose: true
```

Please note you also have a choice of
files: '.\*\.md$'
or
files: .

In this configuration, the find-and-replace hook is set to read search and replacement strings from a file (.project-properties.json by default which should be defined in the root of the project you want to use this package). You can also specify the search and replacement strings directly in the args field (which is not a suggested way).

## Run tests

```
python -m unittest tests.test_main

```

## How to run it using installed python package

```
pip install find-and-replace-strings
find-and-replace --config .find-and-replace.json README1.md README2.md
```

also if you prefer to use a direct mod

```
find-and-replace-strings --find "old_string" --replacement "new_string" README1.md README2.md
```

## If you need more help with the flags and usage of them

```
find-and-replace-strings -h
usage: find-and-replace-strings [-h] [--search SEARCH] [--replacement REPLACEMENT] [--read-from-file READ_FROM_FILE]
[--config REPLACEMENTS_FILE]
[files ...]

This script performs search and replace operations on one or more files. It supports two modes of operation: Direct Mode and
File Mode. In Direct Mode, you specify the search and replacement strings directly on the command line. In File Mode, the script
reads the search and replacement strings from a JSON file.

positional arguments:
files Files to perform search and replace

options:
-h, --help show this help message and exit
--search SEARCH Text to search for
--replacement REPLACEMENT
Text to replace with
--read-from-file READ_FROM_FILE
Read search and replacement strings from file
--config REPLACEMENTS_FILE
Path to the replacements file

```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the terms of the MIT license.

## Building and Publishing

To build and publish it to pypi run

```
bash assets/publish.sh
```

## Reference Info

- https://www.gnu.org/prep/standards/html_node/Option-Table.html#Option-Table
- https://setuptools.pypa.io/en/latest/userguide/declarative_config.html
- https://packaging.python.org/guides/distributing-packages-using-setuptools/
- https://autopilot-docs.readthedocs.io/en/latest/license_list.html
- https://pypi.org/classifiers/
20 changes: 20 additions & 0 deletions assets/check_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import toml
import sys
import requests

def main():
# Load the pyproject.toml file
data = toml.load(open("pyproject.toml"))

# Get the current version
current_version = data["project"]["version"]

# Check if the version is already published
response = requests.get(f"https://pypi.org/pypi/find-and-replace-strings/{current_version}/json")

if response.status_code == 200:
print("This version is already published. Please bump the version in pyproject.toml.")
sys.exit(1)

if __name__ == "__main__":
main()
18 changes: 18 additions & 0 deletions assets/publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
rm -r dist
# Check if twine and setuptools are installed
if ! python3 -c "import twine" &> /dev/null; then
echo "twine not found, installing..."
pip install twine
fi

if ! python3 -c "import setuptools" &> /dev/null; then
echo "setuptools not found, installing..."
pip install setuptools
fi

# Build the package
python3 setup.py sdist

# Upload the package to PyPI
twine upload --repository-url https://upload.pypi.org/legacy/ -u __token__ -p $PYPI_TOKEN dist/*
Binary file added assets/pypi-package.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
59 changes: 59 additions & 0 deletions find_and_replace_strings/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
import os
import argparse
import fileinput
import json
import sys


def replace_in_file(filename, search, replacement):
with fileinput.FileInput(filename, inplace=True) as file:
for line in file:
print(line.replace(rf"{search}", rf"{replacement}"), end='')


def main():
parser = argparse.ArgumentParser(
description="""Perform find and replace operations on one or more target files.
By default, the script reads the search and replacement entries (strings) from a JSON file.
You can also specify the search and replacement strings directly as command line args by setting the
--find "search_string" and --replacement "replacement_string" argument options."""
)
parser.add_argument(
'--config', default='.find-and-replace.json',
help='PATH to JSON config file containing find and replacement entries'
)
parser.add_argument(
'--find', dest='direct_mode', action='store_true', help='String to find in files'
)
parser.add_argument(
'--replacement', dest='direct_mode', action='store_true', help='String to replace with in files'
)
parser.add_argument(
'files', nargs='*', help='File(s) on which to perform search and replace'
)
args = parser.parse_args()

if args.direct_mode:
# Arguments --find and --replacement have been specified - running in direct mode
for filename in args.files:
replace_in_file(filename, args.find, args.replacement)
else:
# Arguments --find and --replacement have not been specified - running in default config file mode
try:
with open(os.path.join(os.getcwd(), args.config), 'r') as f:
replacements = json.load(f)
except FileNotFoundError:
print(f"Error: {args.config} file not found.")
sys.exit(1)
except json.JSONDecodeError:
print(f"Error: {args.config} is not a valid JSON file.")
sys.exit(1)

for filename in args.files:
for replacement in replacements:
replace_in_file(filename, replacement['search'], replacement['replacement'])


if __name__ == "__main__":
main()
Loading
Loading