<commit_before>#!/usr/bin/env python
# encoding: utf-8

from postcards.postcards import Postcards
import sys
import os
import random
import ntpath


class PostcardsFolder(Postcards):
    """
    Send postcards with images from a local folder
    """

    supported_ext = ['.jpg', '.jpeg', '.png']

    def get_img_and_text(self, payload, cli_args):
        if not payload.get('folder'):
            self.logger.error("no folder set in configuration")
            exit(1)

        folder = self._make_absolute_path(payload.get('folder'))

        candidates = []
        for file in os.listdir(folder):
            for ext in self.supported_ext:
                if file.endswith(ext):
                    candidates.append(file)

        if not candidates:
            self.logger.error("no images in folder " + folder)
            exit(1)

        img_name = random.choice(candidates)
        img_path = os.path.join(folder, img_name)
        move_info = 'moving to sent folder' if payload.get('move') else 'no move'

        self.logger.info('choosing image {} ({})'.format(img_path, move_info))
        file = open(img_path, 'rb')

        if payload.get('move'):
            self._move_to_sent(folder, img_path)

        return {
            'img': file,
            'text': ''
        }

    def _move_to_sent(self, picture_folder, image_path):
        sent_folder = os.path.join(picture_folder, 'sent')
        if not os.path.exists(sent_folder):
            os.makedirs(sent_folder)
            self.logger.debug('creating folder {}'.format(sent_folder))

        img_name = self._get_filename(image_path)
        sent_img_path = os.path.join(sent_folder, img_name)
        os.rename(image_path, sent_img_path)
        self.logger.debug('moving image from {} to {}'.format(image_path, sent_img_path))

    def _get_filename(self, path):
        head, tail = ntpath.split(path)
        return tail or ntpath.basename(head)

    def _make_absolute_path(self, path):
        if not os.path.isabs(path):
            abs_dir = os.path.dirname(os.path.realpath(__file__))
            return os.path.join(abs_dir, path)
        else:
            return path


def main():
    PostcardsFolder().main(sys.argv[1:])


if __name__ == '__main__':
    main()<commit_msg>fix issue with path in folder plugin<commit_after>#!/usr/bin/env python
# encoding: utf-8

from postcards.postcards import Postcards
import sys
import os
import random
import ntpath


class PostcardsFolder(Postcards):
    """
    Send postcards with images from a local folder
    """

    supported_ext = ['.jpg', '.jpeg', '.png']

    def get_img_and_text(self, payload, cli_args):
        if not payload.get('folder'):
            self.logger.error("no folder set in configuration")
            exit(1)

        folder = self._make_absolute_path(payload.get('folder'))

        candidates = []
        for file in os.listdir(folder):
            for ext in self.supported_ext:
                if file.endswith(ext):
                    candidates.append(file)

        if not candidates:
            self.logger.error("no images in folder " + folder)
            exit(1)

        img_name = random.choice(candidates)
        img_path = os.path.join(folder, img_name)
        move_info = 'moving to sent folder' if payload.get('move') else 'no move'

        self.logger.info('choosing image {} ({})'.format(img_path, move_info))
        file = open(img_path, 'rb')

        if payload.get('move'):
            self._move_to_sent(folder, img_path)

        return {
            'img': file,
            'text': ''
        }

    def _move_to_sent(self, picture_folder, image_path):
        sent_folder = os.path.join(picture_folder, 'sent')
        if not os.path.exists(sent_folder):
            os.makedirs(sent_folder)
            self.logger.debug('creating folder {}'.format(sent_folder))

        img_name = self._get_filename(image_path)
        sent_img_path = os.path.join(sent_folder, img_name)
        os.rename(image_path, sent_img_path)
        self.logger.debug('moving image from {} to {}'.format(image_path, sent_img_path))

    def _get_filename(self, path):
        head, tail = ntpath.split(path)
        return tail or ntpath.basename(head)

    def _make_absolute_path(self, path):
        if not os.path.isabs(path):
            return os.path.join(os.getcwd(), path)
        else:
            return path


def main():
    PostcardsFolder().main(sys.argv[1:])


if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.11767327835970581, 'fraction_numerical': 0.0017829284599955428, 'mean_word_length': 2.3871698113207547, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6530087', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1354, 'n_words': 311}