From 88908034bedd778f83030fd4e6269705e250c08e Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Sun, 14 Jul 2002 18:19:52 +0000 Subject: [PATCH] Lots of code tidying. git-svn-id: https://rox.svn.sourceforge.net/svnroot/rox/trunk/Archive@1685 66de3db3-b00d-0410-b41b-f4738ad19bea --- AppRun | 291 ++++++++++------------------------------------------------------- box.py | 101 +++++++++++++++++++++++ 2 files changed, 142 insertions(+), 250 deletions(-) rewrite AppRun (89%) create mode 100644 box.py diff --git a/AppRun b/AppRun dissimilarity index 89% index f9a4588..ffe8ced 100755 --- a/AppRun +++ b/AppRun @@ -1,250 +1,41 @@ -#!/usr/bin/env python - -import findrox -import sys, os, formats - -import rox -from rox import g, TRUE, FALSE, saving - -if len(sys.argv) != 2: - rox.info("Drag a file or directory onto Archive to archive it. " - "Drag an archive onto it to extract.") - sys.exit(0) - -from support import escape, Tmp, pipe_through_command - -# Show the savebox, so at least the user knows something is happening.. -class FileData(saving.Saveable): - "A file on the local filesystem." - def __init__(self, path, stream): - self.path = path - self.start = stream.read(300) - try: - stream.seek(0) - self.stream = stream - except: - print "(unseekable)" - # Input is not a regular, local, seekable file, so copy it - # to a local temp file. - import shutil - tmp = Tmp() - tmp.write(self.start) - shutil.copyfileobj(stream, tmp) - self.stream = tmp - - create_arc = () - - create_stream = ( - ('test', "wibble", 'application/x-gzip'), - ('gz', "gzip -c -", 'application/x-gzip'), - ('bz2', "bzip2 -c -", 'application/x-bzip') - ) - - extract_arc = ( - # Archives. Executed from new directory. - ('tgz', "gunzip -c - | tar xf -", 'inode/directory'), - ('tar.gz', "gunzip -c - | tar xf -", 'inode/directory'), - ('tar.bz', "bunzip2 -c - | tar xf -", 'inode/directory'), - ('tar.bz2', "bunzip2 -c - | tar xf -", 'inode/directory'), - ('zip', "unzip -", None), - ('rar', "rar x -", None), - ('tar', "tar xf -", None) - ) - - extract_stream = ( - # Compressed streams - ('gz', "gunzip -c -", None), - ('bz', "bunzip2 -ck -", None), - ('bz2', "bunzip2 -ck -", None) - ) - - def need_dir(self, command): - return command in [c for e,c,t in self.extract_arc] - - def save_to_file(self, file): - command = self.op - if self.need_dir(command): - os.mkdir(file) - os.chdir(file) - pipe_through_command(command, self.stream, None) - else: - saving.Saveable.save_to_file(self, file) - - def save_to_stream(self, stream): - command = self.op - if self.need_dir(command): - raise Exception('Sorry, archives can only be extracted into ' - 'a local directory.') - self.stream.seek(0) - pipe_through_command(command, self.stream, stream) - - def set_op(self, op): - self.op = op - -class DirData(saving.Saveable): - def __init__(self, source): - self.path = source - self.start = None - - create_arc = ( - ('tgz', "tar cf - '%s' | gzip", 'application/x-compressed-tar'), - ('tar.gz', "tar cf - '%s' | gzip", 'application/x-compressed-tar'), - ('tar.bz', "tar cf - '%s' | bzip2", 'application/x-bzip-compressed-tar'), - ('tar.bz2', "tar cf - '%s' | bzip2", 'application/x-bzip-compressed-tar'), - ('zip', "zip -r - '%s'", 'application/zip'), - ('rar', "rar a - '%s'", 'application/x-rar'), - ('tar', "tar cf - '%s'", 'application/x-tar') - ) - - create_stream = () - extract_arc = () - extract_stream = () - - def save_to_stream(self, stream): - command = self.op % escape(self.path) - pipe_through_command(command, None, stream) - - def set_op(self, op): - self.op = op - -class ArchiveBox(saving.SaveBox): - def build_main_area(self): - self.vbox.add(self.save_area) - - self.operation = g.OptionMenu() - self.vbox.pack_start(self.operation, FALSE, TRUE, 0) - self.operation.show() - self.operation.set_border_width(5) - self.updating = 0 - - self.ops_menu = g.Menu() - self.operation.set_menu(self.ops_menu) - - def add_ops(self): - doc = self.save_area.document - self.i_to_op = [] - for op, command, type in doc.create_arc: - item = g.MenuItem('Create .%s archive' % op) - item.show() - self.ops_menu.append(item) - self.i_to_op.append((op, command, type)) - for op, command, type in doc.create_stream: - item = g.MenuItem('Compress as .%s' % op) - item.show() - self.ops_menu.append(item) - self.i_to_op.append((op, command, type)) - - if doc.start: - guess = formats.guess_format(doc.start) - else: - guess = None - - if guess in ['gz', 'bz2', 'bz']: - if doc.path.endswith('.tar.' + guess): - guess = 'tar.' + guess - - init = 0 - for op, command, type in doc.extract_arc: - item = g.MenuItem('Extract from a .%s' % op) - item.show() - self.ops_menu.append(item) - if op == guess: - init = len(self.i_to_op) - self.i_to_op.append((op, command, type)) - for op, command, type in doc.extract_stream: - item = g.MenuItem('Uncompress .%s' % op) - item.show() - self.ops_menu.append(item) - if op == guess: - init = len(self.i_to_op) - self.i_to_op.append((op, command, type)) - - name = doc.path - if guess and name.endswith('.' + guess): - name = name[:-len(guess)-1] - self.save_area.entry.set_text(name) - - self.operation.connect('changed', self.op_changed) - self.save_area.entry.connect('changed', self.name_changed) - self.operation.set_history(init) - - def name_changed(self, entry): - if self.updating: - return - self.updating = 1 - - name = entry.get_text() - doc = self.save_area.document - i = 0 - for o, command, type in doc.create_arc + doc.create_stream: - if name.endswith('.' + o): - self.operation.set_history(i) - break - i += 1 - - self.updating = 0 - - def op_changed(self, operation): - i = operation.get_history() - doc = self.save_area.document - op = self.i_to_op[i] - doc.set_op(op[1]) - if op[2]: - print op[2] - self.set_type(op[2]) - else: - self.set_type('text/plain') - - if self.updating: - return - self.updating = 1 - - name = self.save_area.entry.get_text() - if not name: - name = doc.path - for o, command, type in doc.create_arc + doc.create_stream: - if name.endswith('.' + o): - name = name[:-len(o)-1] - break - if i < len(doc.create_arc): - name += '.' + doc.create_arc[i][0] - else: - i -= len(doc.create_arc) - if i < len(doc.create_stream): - name += '.' + doc.create_stream[i][0] - self.save_area.entry.set_text(name) - - self.updating = 0 - -# Check that our input is a regular local file. -# If not, fetch the data and put it in /tmp. - -path = sys.argv[1] - -source = None - -if path == '-': - source = sys.stdin -else: - path = rox.get_local_path(path) - if not path: - rox.croak('Sorry, I can only extract/archive local files.') - if not os.path.isdir(path): - try: - source = file(path) - except: - rox.report_exception() - sys.exit(1) - -if source: - data = FileData(path, source) -else: - data = DirData(path) - -savebox = ArchiveBox(data, '', 'text/plain') -savebox.show() -g.gdk.flush() - -savebox.add_ops() - -rox.mainloop() +#!/usr/bin/env python + +import findrox +import sys, os, formats + +import rox +from rox import g, TRUE, FALSE, saving + +if len(sys.argv) != 2: + rox.info("Drag a file or directory onto Archive to archive it. " + "Drag an archive onto it to extract.") + sys.exit(0) + +from support import escape, Tmp, pipe_through_command + +from box import ArchiveBox + +# Check that our input is a regular local file. +# If not, fetch the data and put it in /tmp. + +path = sys.argv[1] + +if path != '-': + path = rox.get_local_path(path) + if not path: + rox.croak('Sorry, I can only extract/archive local files.') + path = os.path.abspath(path) + +# Show the savebox, so at least the user knows something is happening.. + +savebox = ArchiveBox() +savebox.show() +g.gdk.flush() + +if os.path.isdir(path): + data = formats.DirData(path) +else: + data = formats.FileData(path) +savebox.set_data(data) + +rox.mainloop() diff --git a/box.py b/box.py new file mode 100644 index 0000000..c7320f9 --- /dev/null +++ b/box.py @@ -0,0 +1,101 @@ +import sys + +import rox +from rox import saving, g, TRUE, FALSE + +import formats + +class ArchiveBox(saving.SaveBox, saving.Saveable): + "A savebox with an operation selector." + def __init__(self): + saving.SaveBox.__init__(self, self, '', 'text/plain') + self.data = None + + def build_main_area(self): + self.vbox.add(self.save_area) + + self.operation = g.OptionMenu() + self.vbox.pack_start(self.operation, FALSE, TRUE, 0) + self.operation.show() + self.operation.set_border_width(5) + self.updating = 0 + + self.ops_menu = g.Menu() + self.operation.set_menu(self.ops_menu) + + def set_data(self, data): + assert not self.data + + self.data = data + self.ops = [op for op in formats.operations if op.can_handle(data)] + + name = data.default_name + self.save_area.entry.set_text(name) + start = name.rfind('/') + 1 + #self.save_area.entry.select_region(start, -1) + g.Editable.select_region(self.save_area.entry, start, -1) + + last = None + + # Add all supported ops to the menu. + for op in self.ops: + last = op + item = g.MenuItem(str(op)) + item.show() + self.ops_menu.append(item) + + # Select the default + try: + self.operation.set_history(self.ops.index(data.default)) + except ValueError: + print >>sys.stderr, "Warning: %s not in ops list!" % data.default + pass + + self.operation.connect('changed', self.op_changed) + self.save_area.entry.connect('changed', self.name_changed) + + def name_changed(self, entry): + if self.updating: + return + self.updating = 1 + + name = entry.get_text() + doc = self.save_area.document + i = 0 + for op in self.ops: + if name.endswith('.' + op.extension): + self.operation.set_history(i) + break + i += 1 + + self.updating = 0 + + def op_changed(self, operation): + op = self.ops[operation.get_history()] + self.set_type(op.type) + + if self.updating: + return + self.updating = 1 + + name = self.save_area.entry.get_text() + for op in self.ops: + if name.endswith('.' + op.extension): + name = name[:-len(op.extension)-1] + break + if op.add_extension: + name += '.' + op.extension + self.save_area.entry.set_text(name) + + self.updating = 0 + + def save_to_file(self, path): + op = self.ops[self.operation.get_history()] + if hasattr(op, 'save_to_file'): + op.save_to_file(self.data, path) + else: + saving.Saveable.save_to_file(self, path) + + def save_to_stream(self, stream): + op = self.ops[self.operation.get_history()] + op.save_to_stream(self.data, stream) -- 2.11.4.GIT