~ubuntu-branches/ubuntu/jaunty/calibre/jaunty-backports

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/rtf2xml/copy.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-01-20 17:14:02 UTC
  • Revision ID: james.westby@ubuntu.com-20090120171402-8y3znf6nokwqe80k
Tags: upstream-0.4.125+dfsg
ImportĀ upstreamĀ versionĀ 0.4.125+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#########################################################################
 
2
#                                                                       #
 
3
#                                                                       #
 
4
#   copyright 2002 Paul Henry Tremblay                                  #
 
5
#                                                                       #
 
6
#   This program is distributed in the hope that it will be useful,     #
 
7
#   but WITHOUT ANY WARRANTY; without even the implied warranty of      #
 
8
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    #
 
9
#   General Public License for more details.                            #
 
10
#                                                                       #
 
11
#   You should have received a copy of the GNU General Public License   #
 
12
#   along with this program; if not, write to the Free Software         #
 
13
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA            #
 
14
#   02111-1307 USA                                                      #
 
15
#                                                                       #
 
16
#                                                                       #
 
17
#########################################################################
 
18
import sys, os
 
19
class Copy:
 
20
    """Copy each changed file to a directory for debugging purposes"""
 
21
    __dir = ""
 
22
    def __init__(self, bug_handler, file = None, deb_dir = None, ):
 
23
        self.__file = file
 
24
        self.__bug_handler = bug_handler
 
25
    def set_dir(self, deb_dir):
 
26
        """Set the temporary directory to write files to"""
 
27
        if deb_dir is None:
 
28
            message = "No directory has been provided to write to in the copy.py"
 
29
            raise self.__bug_handler, message
 
30
        check = os.path.isdir(deb_dir)
 
31
        if not check:
 
32
            message = "%(deb_dir)s is not a directory" % vars()
 
33
            raise self.__bug_handler , message
 
34
        Copy.__dir = deb_dir
 
35
    def remove_files(self ):
 
36
        """Remove files from directory"""
 
37
        self.__remove_the_files(Copy.__dir)
 
38
        """
 
39
        list_of_files = os.listdir(Copy.__dir)
 
40
        list_of_files = os.listdir(the_dir)
 
41
        for file in list_of_files:
 
42
            rem_file = os.path.join(Copy.__dir,file)
 
43
            if os.path.isdir(rem_file):
 
44
                self.remove_files(rem_file)
 
45
            else:
 
46
                os.remove(rem_file)
 
47
        """
 
48
    def __remove_the_files(self, the_dir):
 
49
        """Remove files from directory"""
 
50
        list_of_files = os.listdir(the_dir)
 
51
        for file in list_of_files:
 
52
            rem_file = os.path.join(Copy.__dir,file)
 
53
            if os.path.isdir(rem_file):
 
54
                self.__remove_the_files(rem_file)
 
55
            else:
 
56
                try:
 
57
                    os.remove(rem_file)
 
58
                except OSError:
 
59
                    pass
 
60
    def copy_file(self, file, new_file):
 
61
        """
 
62
        Copy the file to a new name
 
63
        If the platform is linux, use the faster linux command
 
64
        of cp. Otherwise, use a safe python method.
 
65
        """
 
66
        write_file = os.path.join(Copy.__dir,new_file)
 
67
        platform = sys.platform
 
68
        if platform[:5] == 'linux':
 
69
            command = 'cp %(file)s %(write_file)s' % vars()
 
70
            os.system(command)
 
71
        else:
 
72
            read_obj = open(file,'r')
 
73
            write_obj = open(write_file, 'w')
 
74
            line = "dummy"
 
75
            while line:
 
76
                    line = read_obj.read(1000)
 
77
                    write_obj.write(line )
 
78
            read_obj.close()
 
79
            write_obj.close()
 
80
    def rename(self, source, dest):
 
81
        read_obj = open(source, 'r')
 
82
        write_obj = open(dest, 'w')
 
83
        line = 1
 
84
        while line:
 
85
            line = read_obj.readline()
 
86
            write_obj.write(line)
 
87
        read_obj.close()
 
88
        write_obj.close()