From f99dcc1e2c1c1db3de3225da6546d187b74dc6a7 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Mon, 23 Apr 2018 23:09:40 +0200 Subject: [PATCH] First pass with Prospector Also some beautifying, line wrapping, etc. --- gcp | 68 ++++++++++++++++++++++++++++++----------------------- setup.py | 2 -- test_gcp.py | 6 ++--- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/gcp b/gcp index cf4df75..ece9d8a 100755 --- a/gcp +++ b/gcp @@ -21,30 +21,32 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ -### logging ### import logging from logging import debug, info, error, warning -logging.basicConfig(level=logging.INFO, - format='%(message)s') -### import gettext -gettext.install('gcp', "i18n") import sys -import os, os.path +import os +import os.path from argparse import ArgumentParser, RawDescriptionHelpFormatter import pickle + +logging.basicConfig(level=logging.INFO, format='%(message)s') +gettext.install('gcp', "i18n") + try: from gi.repository import GObject #DBus - import dbus, dbus.glib + import dbus + import dbus.glib import dbus.service import dbus.mainloop.glib except ImportError as e: error(_("Error during import")) - error(_("Please check dependecies:"),e) + error(_("Please check dependecies:"), e) exit(1) + try: from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed pbar_available=True @@ -229,14 +231,15 @@ class GCP(): def __getMountPoints(self): """Parse /proc/mounts to get currently mounted devices""" - #TODO: reparse when a new device is added/a device is removed - #(check freedesktop mounting signals) + # TODO: reparse when a new device is added/a device is removed + # (check freedesktop mounting signals) ret = {} try: with open("/proc/mounts",'r') as mounts: for line in mounts.readlines(): - fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = line.split(' ') - ret[fs_file] = fs_vfstype + fs_spec, fs_file, fs_vfstype, \ + fs_mntops, fs_freq, fs_passno = line.split(' ') + ret[fs_file] = fs_vfstype except: error (_("Can't read mounts table")) return ret @@ -245,12 +248,15 @@ class GCP(): """Add a file to the copy list @param path: absolute path of file @param options: options as return by optparse""" - debug (_("Adding to copy list: %(path)s ==> %(dest_path)s (%(fs_type)s)") % {"path":path, "dest_path":dest_path, "fs_type":self.getFsType(dest_path)} ) + debug(_("Adding to copy list: %(path)s ==> %(dest_path)s (%(fs_type)s)") + % {"path":path, "dest_path":dest_path, + "fs_type":self.getFsType(dest_path)}) try: self.bytes_total+=os.path.getsize(path) self.copy_list.insert(0,(path, dest_path, options)) except OSError as e: - error(_("Can't copy %(path)s: %(exception)s") % {'path':path, 'exception':e.strerror}) + error(_("Can't copy %(path)s: %(exception)s") + % {'path':path, 'exception':e.strerror}) def __appendDirToList(self, dirpath, dest_path, options): @@ -449,19 +455,17 @@ class GCP(): def __get_string_size(self, size): """Return a nice string representation of a size""" - - if size>=2**50: - return _("%.2f PiB") % (float(size)/2**50) - elif size>=2**40: - return _("%.2f TiB") % (float(size)/2**40) - elif size>=2**30: - return _("%.2f GiB") % (float(size)/2**30) - elif size>=2**20: - return _("%.2f MiB") % (float(size)/2**20) - elif size>=2**10: - return _("%.2f KiB") % (float(size)/2**10) - else: - return _("%i B") % size + if size >= 2**50: + return _("%.2f PiB") % (float(size) / 2**50) + if size >= 2**40: + return _("%.2f TiB") % (float(size) / 2**40) + if size >= 2**30: + return _("%.2f GiB") % (float(size) / 2**30) + if size >= 2**20: + return _("%.2f MiB") % (float(size) / 2**20) + if size >= 2**10: + return _("%.2f KiB") % (float(size) / 2**10) + return _("%i B") % size def _pbar_update(self): """Update progress bar position, create the bar if it doesn't exist""" @@ -469,12 +473,16 @@ class GCP(): try: if self.pbar.maxval != self.bytes_total: self.pbar.maxval = self.bytes_total - self.pbar.widgets[0] = _("Copying %s") % self.__get_string_size(self.bytes_total) + pbar_msg = _("Copying %s") % self.__get_string_size(self.bytes_total) + self.pbar.widgets[0] = pbar_msg except AttributeError: if not self.bytes_total: - #No progress bar if the files have a null size + # No progress bar if the files have a null size return - self.pbar = ProgressBar(self.bytes_total,[_("Copying %s") % self.__get_string_size(self.bytes_total)," ",Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()]) + pbar_msg = _("Copying %s") % self.__get_string_size(self.bytes_total) + self.pbar = ProgressBar(self.bytes_total, + [pbar_msg, " ", Percentage(), " ", Bar(), + " ", FileTransferSpeed(), " ", ETA()]) self.pbar.start() self.pbar.update(self.bytes_copied) diff --git a/setup.py b/setup.py index fd23ecf..59fb489 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 from setuptools import setup -import sys -from os import path name = 'gcp' diff --git a/test_gcp.py b/test_gcp.py index f64b859..0109653 100755 --- a/test_gcp.py +++ b/test_gcp.py @@ -24,7 +24,6 @@ import unittest from os import getcwd, chdir, system, mkdir, makedirs, listdir from os.path import join, isdir from shutil import rmtree -from random import randrange from hashlib import sha1 # gcp command. This assumes gcp is in the PATH. Alternatively, we could use @@ -78,7 +77,7 @@ def dirCheck(dir_path): # @param size: size of the file to create in bytes # """ # def seq(size): -# return ''.join(chr(randrange(256)) for i in range(size)) +# return ''.join(chr(random.randrange(256)) for i in range(size)) # fd = open(path, 'w') # for byte in range(size//buf_size): # fd.write(seq(buf_size)) @@ -92,7 +91,7 @@ def makeRandomFile(path, size=S10K, buf_size=4096): """ source = open('/dev/urandom', 'rb') dest = open(path, 'wb') - for byte in range(size // buf_size): + for _ in range(size // buf_size): dest.write(source.read(buf_size)) dest.write(source.read(size % buf_size)) dest.close() @@ -240,5 +239,6 @@ class TestCopyCases(unittest.TestCase): check_after = dirCheck('.') self.assertEqual(check_before, check_after) + if __name__ == '__main__': unittest.main()