File: drZip.py

package info (click to toggle)
drpython 1%3A3.11.4-1.1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 3,868 kB
  • ctags: 2,871
  • sloc: python: 16,653; makefile: 141; sh: 1
file content (298 lines) | stat: -rw-r--r-- 9,435 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#   Programmer: Daniel Pozmanter
#   E-mail:     drpython@bluebottle.com
#   Note:       You must reply to the verification e-mail to get through.
#
#   Copyright 2003-2010 Daniel Pozmanter
#
#   Distributed under the terms of the GPL (GNU Public License)
#
#   DrPython 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 2 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, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#   Requirements(Dependencies):  Install Python, and wxPython.
#
#   Tested On Windows, Linux, Mac OS X
#
#   Icons taken from "Klassic New Crisp Icons" by Asif Ali Rizwaan (therizwaan) from the KDE-LOOK site (some edited a bit).
#   A humble and excellent artist.
#   Oh, the python icon is taken from wxPython.
#   The basic design of the program is meant to roughly (ROUGHLY) mimick DrScheme.
#   The purpose is the same, to provide a simple IDE(integrated development environment) ideal for teaching.
#   The DrPython icon itself was based on the DrScheme icon, with a slightly edited wxpython icon inserted(note yellow tongue, googly eyes).
#
#   This program could not have been written without the wonderful work of the people behind
#   python and wxPython, in particular the Styled Text Control.  Thank you.  Hopefully this tool will be of use.

#drzip

#This is a module to make adding
#to / from zip files really easy.

#Targets are prefs directory,
#creating a directory structure from the drscript menu
#an all plugins.

import os, zipfile, string, tempfile
from drPrefsFile import ExtractPreferenceFromText

def AddDirectoryToZipFile(directory, dirname, zip):
    dlist = os.listdir(directory)

    for item in dlist:
        absitem = os.path.join(directory, item)
        zipitem = os.path.join(dirname, item)
        if os.path.isdir(absitem):
            AddDirectoryToZipFile(absitem, zipitem, zip)
        else:
            zip.write(absitem, zipitem)

def AddDrScriptsToZipFile(prefdir, zip):
    scriptfile = os.path.join(prefdir, "drscript.dat")

    if os.path.exists(scriptfile):
        newlines = []

        dirstrings = ["drscripts"]
        lastCount = 0
        #Read from the file
        f = open(scriptfile, 'rb')
        #Initialize
        line = f.readline()
        while line:
            c = line.count('\t')
            indentationstring = line[:c]
            line = line[c:].rstrip()
            while lastCount > c:
                dirstrings.pop()
                lastCount = lastCount - 1

            if line[0] == '>':
                dirstrings.append(line[1:])
                newlines.append(indentationstring + line)
                c = c + 1
            else:
                line_path = ExtractPreferenceFromText(line, "path")
                line_title = ExtractPreferenceFromText(line, "title")

                line_filename = os.path.basename(line_path)

                if os.path.exists(line_path):
                    zippath = string.join(dirstrings, '/')
                    zipname = os.path.join(zippath, line_filename)
                    zip.write(line_path, zipname)
                    newlines.append(indentationstring + "<path>" + zipname + "</path><title>" + line_title + "</title>")

            lastCount = c
            line = f.readline()
        f.close()


        #Add the edited Script File:
        newtext = string.join(newlines, '\n')

        tdrscript = tempfile.mktemp()

        f = open(tdrscript, 'wb')
        f.write(newtext)
        f.close()

        zip.write(tdrscript, "drscript.dat")

        #Remove the temporary file:
        os.remove(tdrscript)

        #Add Shortcuts
        zip.write(os.path.join(prefdir, "drscript.shortcuts.dat"), "drscript.shortcuts.dat")


def CreateDirectories(targetdir, zippedfilename):
    zippedfilename = zippedfilename.replace('\\', '/')
    d = zippedfilename.find('/')
    while d > -1:
        dir = zippedfilename[:d]
        targetdir = os.path.join(targetdir, dir)
        if not os.path.exists(targetdir):
            os.mkdir(targetdir)
        zippedfilename = zippedfilename[d+1:]
        d = zippedfilename.find('/')

def ExportDirectoryTo(targetdirectory, filename, ziproot = ""):
    zf = zipfile.ZipFile(filename, 'w')

    AddDirectoryToZipFile(targetdirectory, ziproot, zf)

    zf.close()

def ExportDrScriptsTo(prefdir, filename):
    zf = zipfile.ZipFile(filename, 'w')

    AddDrScriptsToZipFile(prefdir, zf)

    zf.close()

def ExportPreferencesTo(pluginsdirectory, prefdir, shortcutsdir, datdirectory, filename,
                        shortcuts = True, popupmenu = True, toolbar = True, plugins = True, drscripts = True):
    zf = zipfile.ZipFile(filename, 'w')

    #Add Plugins
    if plugins:
        AddDirectoryToZipFile(pluginsdirectory, "", zf)

    if drscripts:
        AddDrScriptsToZipFile(prefdir, zf)

    #Add Preferences
    zf.write(os.path.join(prefdir, "preferences.dat"), "preferences.dat")

    #Add Shortcuts
    if shortcuts:
        zf.write(os.path.join(shortcutsdir, "shortcuts.dat"), "shortcuts.dat")
        zf.write(os.path.join(shortcutsdir, "stcshortcuts.dat"), "stcshortcuts.dat")

    #Add Pop Up Menu
    if popupmenu:
        zf.write(os.path.join(datdirectory, "popupmenu.dat"), "popupmenu.dat")

    #Add ToolBar
    if toolbar:
        zf.write(os.path.join(datdirectory, "toolbar.dat"), "toolbar.dat")

    zf.close()



def ImportDrScriptsFrom(prefdir, filename):
    UnPackIf(prefdir, filename, "drscript")
    SetupImportedDrScripts(prefdir)

def ImportPluginsFrom(prefdir, filename):
    UnPackIf(prefdir, filename, "plugins")

def ImportPreferencesFrom(prefdir, filename):
    UnPack(prefdir, filename)
    SetupImportedDrScripts(prefdir)

def ImportJustPreferencesFrom(prefdir, filename):
    UnPackJustPreferences(prefdir, filename)
    SetupImportedDrScripts(prefdir)

def SetupImportedDrScripts(prefdir):
    scriptfile = os.path.join(prefdir, "drscript.dat")

    if os.path.exists(scriptfile):

        #Read from the file
        f = open(scriptfile, 'rb')
        lines = f.readlines()
        f.close()

        newlines = []

        for line in lines:
            c = line.count('\t')
            identationstring =  line[:c]

            if line[0] != '>':

                line_path = ExtractPreferenceFromText(line, "path")
                line_title = ExtractPreferenceFromText(line, "title")

                new_path = os.path.join(prefdir, line_path)

                if os.path.exists(new_path):
                    newlines.append(identationstring + "<path>" + new_path + "</path><title>" + line_title + "</title>\n")
            else:
                newlines.append(identationstring + line)

        f = open(scriptfile, 'wb')
        f.writelines(newlines)
        f.close()

def UnPack(targetdirectory, filename, label=""):
    zf = zipfile.ZipFile(filename, 'r')

    dir = targetdirectory + label

    if not os.path.exists(dir):
        os.mkdir(dir)

    zippedfiles = zf.namelist()

    for zippedfile in zippedfiles:
        l = len(zippedfile)
        if (zippedfile[l-1] == '/') or (zippedfile[l-1] == '\\'):
            CreateDirectories(dir, zippedfile)
        else:
            CreateDirectories(dir, zippedfile)
            data = zf.read(zippedfile)
            f = open(os.path.join(dir, zippedfile), 'wb')
            f.write(data)
            f.close()

    zf.close()

def UnPackIf(targetdirectory, filename, prefix, label=""):
    zf = zipfile.ZipFile(filename, 'r')

    dir = targetdirectory + label

    if not os.path.exists(dir):
        os.mkdir(dir)

    zippedfiles = zf.namelist()

    for zippedfile in zippedfiles:
        if zippedfile.find(prefix) == 0:
            l = len(zippedfile)
            if (zippedfile[l-1] == '/') or (zippedfile[l-1] == '\\'):
                CreateDirectories(dir, zippedfile)
            else:
                CreateDirectories(dir, zippedfile)
                data = zf.read(zippedfile)
                f = open(os.path.join(dir, zippedfile), 'wb')
                f.write(data)
                f.close()

    zf.close()

def UnPackJustPreferences(targetdirectory, filename, label=""):
    zf = zipfile.ZipFile(filename, 'r')

    dir = targetdirectory + label

    if not os.path.exists(dir):
        os.mkdir(dir)

    rawzippedfiles = zf.namelist()

    zippedfiles = []

    targets = ["preferences.dat", "popupmenu.dat", "shortcuts.dat", "stcshortcuts.dat", "toolbar.dat"]

    for rz in rawzippedfiles:
        if rz in targets:
            zippedfiles.append(rz)

    for zippedfile in zippedfiles:
        l = len(zippedfile)
        if (zippedfile[l-1] == '/') or (zippedfile[l-1] == '\\'):
            CreateDirectories(dir, zippedfile)
        else:
            CreateDirectories(dir, zippedfile)
            data = zf.read(zippedfile)
            f = open(os.path.join(dir, zippedfile), 'wb')
            f.write(data)
            f.close()

    zf.close()