/file_manip

Primary LanguagePythonMIT LicenseMIT

File manipulation

Explanation:

The idea is to transform a function that transform one text into another text and call it transforming the desired files with that function.

This is not a package made thinking about being used in a project, rather it's a package made to make a programmer's life easier.

INSTRUCTIONS:

Decorator that transforms an [str] = function( [str]:text, *args, **kwargs ) into an function that apllies itself into a folder.

The default values for the new function can be set at the decorator.

- at - regex to search target files

- path - folder with files to change (start folder on recursive mode)

- recursive - makes the function call itself on all folders inside the start folder

- ignore - ignore changes on file if function returns any error and continues to the next file

- test - creates a new file with the changes for test (NOT recommended with: recursive = True)

- prefix/posfix - strings added before and after test files

- log - shows on console steps

Default values on decorator are set to avoid writting over important files with broken functions.

!! ATENTION !!

MAKE SURE TO ALWAYS TEST YOUR FUNCTIONS BEFORE APPLYING ANY TRUE CHANGES.

Simple functions:

Imagine you want to comment all prints in your project:

from manip import manip

@manip()
def comment_print(text):
    return text.replace('print(', '# print(')

Now we can call the function on a folder like:

comment_print(path='./target')
comment_print(path='./target', test=False) # to save changes on original files

If you wanna just call a single line function on the command line you can import manipulate:

from manip import manipulate

manipulate(lambda text: text.replace('print(', '# print('), path='./target', test=False)

up to change whole files with json data, maybe:

from manip import manip
import json

@manip(at='.json$')
def join_props(text, prop1='key', prop2='value', new_prop='new'):
    obj = json.loads(text)
    obj[new_prop] = (prop1, prop2)
    
    del(obj[prop1])
    del(obj[prop2])

    return json.dumps(obj)

More info:

GitHub