pysorter icon indicating copy to clipboard operation
pysorter copied to clipboard

Add useful rule functions to the filetypes.py file

Open chriscz opened this issue 9 years ago • 2 comments

This issue will be split up if anyone is interested in working on it. Rule functions look as follows:

def move_rule(match, path):
    # do some really advanced things here
    # and return the destination path

The match object is a math object as returned by re.finditer.

Each rule function should have a complementary creator (higher order) function that makes new instances of it. For example, let's say I want a rule function that searches for keywords in a matched file. Since the keywords may be different for different files, it may look like this

from pysorter import rules

def make_keyword_rule(keywords, destination)
    # keywords is a set of string keywords
    def keyword_rule(match, path):
        # only handles files
        if path.endswith('/'):
            return rules.Unhandled

        with open(path, 'r') as f:
            words = f.read().split()
            for word in words:
                if word in keywords:
                    return destination

        return rules.Unhandled

    return keyword_rule

And so when it is used in a filetypes.py file, it may look like this:

...
('\.txt$', make_keyword_rule({'cat', 'kitten', 'claw', 'whiskers'}, 'txt_documents/cats/'))
...

Some more ideas (8 October 2016):

  • Scan the content of a pdf document and move to a directory if it contains at least a certain number of the keywords specified.
  • Try to detect the title of a pdf document and move it to a file with the appropriate name (might be tricky)

Some ideas for useful rule function are :

  • detect directory structures for common IDE's: pycharm, eclipse, intelij and netbeans
  • detect and '.git' directories
  • ~detect files based on their mimetypes~

chriscz avatar Oct 05 '16 17:10 chriscz

I am interested in working on this! Give me a heads up and the breakdown?

xssChauhan avatar Oct 08 '16 04:10 xssChauhan

Hey, Do you have any specific questions regarding the instructions in the first post?

Basically, You can add several make_* functions to the rules.py file. A good start would be doing .git folder detection.

chriscz avatar Oct 08 '16 17:10 chriscz