<commit_before># -*- coding: utf-8 -*-

##########
# PYCP
#########

##########################################################################
# Copyright 2009 <NAME>                                     #
#                                                                        #
#  This program is free software: you can redistribute it and/or modify  #
#  it under the terms of the GNU General Public License as published by  #
#  the Free Software Foundation, either version 3 of the License, or     #
#  (at your option) any later version.                                   #
#                                                                        #
#  This program is distributed in the hope that it will be useful,       #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU General Public License for more details.                          #
#                                                                        #
#  You should have received a copy of the GNU General Public License     #
#  along with this program.  If not, see <http://www.gnu.org/licenses/>. #
##########################################################################


"""
Having a progress bar while transferring files

Main idea : forking the file transfer in background, while reading size of
sources and destinations.

Module is called pycp because only copy was supported at the beginning
"""

__author__ = "<NAME>"
__author_email__  = "yannicklm1337 AT gmail DOT com"
__version__ = "4.1"

import sys
import time
import shutil

from threading import Thread
from threading import Event
from optparse import OptionParser

from os import path
from os import mkdir
from os import listdir

try:
    from progressbar import ProgressBar
    from progressbar import Percentage
    from progressbar import FileTransferSpeed
    from progressbar import ETA
    from progressbar import Bar
except ImportError:
    print "Error: Unable to find progressbar module"
    sys.exit(1)


class FileTransferManager:
    """
    Class that manages file transfer process

    It is assumed that source and destination are both files.
    (No directory here)

    """
    def __init__(self, source, destination, action, opts):
        self.file_transfer = None
        self.source        = source
        self.destination   = destination
        self.pbar          = None
        self.action        = action
        self.opts          = opts


    def transfer_file(self):
        """
        Create a FileTransfer instance to start transfer
        in a thread, and start monitoring transfer

        """
        self.file_transfer = FileTransfer(self.source,
                self.destination,
                self.action)
        self.file_transfer.start()
        print self.source + " -> " + self.destination
        self.monitor_file_transfer()


    def monitor_file_transfer(self):
        """
        Executed during file transfer to update the progressbar

        """
        source_size = float(path.getsize(self.source))

        if source_size == 0: # Using pycp to copy an empty file. Why not?
            return

        # Use of wonderful constructor from progessbar.
        self.pbar = ProgressBar(
          widgets = [
            Percentage()                          ,
            " "                                   ,
            Bar(marker='#', left='[', right=']' ) ,
            " - "                                 ,
            FileTransferSpeed()                   ,
            " | "                                 ,
            ETA() ]                               ,
         maxval = source_size )

        while (not self.file_transfer.is_finished()):
            try:
                dest_size = float(path.getsize(self.destination))
            except OSError:  # Maybe file has not been created yet
                dest_size = 0
            time.sleep(1)
            self.pbar.update(dest_size)

        self.pbar.finish()

def main(action="copy"):
    """
    Main: manages options and values

    The parameter action is the kind of file transfer we wish to do.
    (move or copy, for the moment)

    """
    prog_name = "pycp"
    if action == "move":
        prog_name = "pymv"

    usage   =                                                      \
      "usage: " + prog_name + " [options] SOURCE DESTINATION\n"    \
    + "       " + prog_name + " [options] SOURCE ... DIRECTORY\n"  \
    + action + " SOURCE to DESTINATION, or multiple SOURCE(s) to DIRECTORY"

    version = prog_name + " version " + __version__ + "\n" \
              + "Distributed under GPL license"

    parser = OptionParser(usage, version=version)

    parser.add_option("-i", "--interactive",
            action  = "store_true",
            dest    = "interactive",
            help    = "ask before overwriting existing files")

    parser.add_option("-s", "--safe",
            action  = "store_true",<commit_msg>Slightly modify usage for help2man to work<commit_after># -*- coding: utf-8 -*-

##########
# PYCP
#########

##########################################################################
# Copyright 2009 <NAME>                                     #
#                                                                        #
#  This program is free software: you can redistribute it and/or modify  #
#  it under the terms of the GNU General Public License as published by  #
#  the Free Software Foundation, either version 3 of the License, or     #
#  (at your option) any later version.                                   #
#                                                                        #
#  This program is distributed in the hope that it will be useful,       #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#  GNU General Public License for more details.                          #
#                                                                        #
#  You should have received a copy of the GNU General Public License     #
#  along with this program.  If not, see <http://www.gnu.org/licenses/>. #
##########################################################################


"""
Having a progress bar while transferring files

Main idea : forking the file transfer in background, while reading size of
sources and destinations.

Module is called pycp because only copy was supported at the beginning
"""

__author__ = "<NAME>"
__author_email__  = "yannicklm1337 AT gmail DOT com"
__version__ = "4.1"

import sys
import time
import shutil

from threading import Thread
from threading import Event
from optparse import OptionParser

from os import path
from os import mkdir
from os import listdir

try:
    from progressbar import ProgressBar
    from progressbar import Percentage
    from progressbar import FileTransferSpeed
    from progressbar import ETA
    from progressbar import Bar
except ImportError:
    print "Error: Unable to find progressbar module"
    sys.exit(1)


class FileTransferManager:
    """
    Class that manages file transfer process

    It is assumed that source and destination are both files.
    (No directory here)

    """
    def __init__(self, source, destination, action, opts):
        self.file_transfer = None
        self.source        = source
        self.destination   = destination
        self.pbar          = None
        self.action        = action
        self.opts          = opts


    def transfer_file(self):
        """
        Create a FileTransfer instance to start transfer
        in a thread, and start monitoring transfer

        """
        self.file_transfer = FileTransfer(self.source,
                self.destination,
                self.action)
        self.file_transfer.start()
        print self.source + " -> " + self.destination
        self.monitor_file_transfer()


    def monitor_file_transfer(self):
        """
        Executed during file transfer to update the progressbar

        """
        source_size = float(path.getsize(self.source))

        if source_size == 0: # Using pycp to copy an empty file. Why not?
            return

        # Use of wonderful constructor from progessbar.
        self.pbar = ProgressBar(
          widgets = [
            Percentage()                          ,
            " "                                   ,
            Bar(marker='#', left='[', right=']' ) ,
            " - "                                 ,
            FileTransferSpeed()                   ,
            " | "                                 ,
            ETA() ]                               ,
         maxval = source_size )

        while (not self.file_transfer.is_finished()):
            try:
                dest_size = float(path.getsize(self.destination))
            except OSError:  # Maybe file has not been created yet
                dest_size = 0
            time.sleep(1)
            self.pbar.update(dest_size)

        self.pbar.finish()

def main(action="copy"):
    """
    Main: manages options and values

    The parameter action is the kind of file transfer we wish to do.
    (move or copy, for the moment)

    """
    prog_name = "pycp"
    if action == "move":
        prog_name = "pymv"

    usage   =                                                      \
      "usage: " + prog_name + " [options] SOURCE... DESTINATION\n"    \
    + action + " SOURCE(s) to DESTINATION\n"

    version = prog_name + " version " + __version__ + "\n" \
              + "Distributed under GPL license"

    parser = OptionParser(usage, version=version)

    parser.add_option("-i", "--interactive",
            action  = "store_true",
            dest    = "interactive",
            help    = "ask before overwriting existing files")

    parser.add_option("-s", "--safe",
            action  = "store_true",
{'fraction_non_alphanumeric': 0.10763819095477387, 'fraction_numerical': 0.0033165829145728643, 'mean_word_length': 1.6649705409748259, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1227759', 'n_tokens_mistral': 2468, 'n_tokens_neox': 2347, 'n_words': 830}