From e11533e8c16e856f7fde174b30aed5960d039043 Mon Sep 17 00:00:00 2001 From: sd2017 Date: Sun, 2 Oct 2016 20:38:49 +0300 Subject: [PATCH] add CopyFileCommand --- command.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++--- test_command.py | 10 +++++- 2 files changed, 92 insertions(+), 6 deletions(-) diff --git a/command.py b/command.py index 27c25555..445cf9ee 100644 --- a/command.py +++ b/command.py @@ -1,15 +1,44 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - +import traceback +import sys import os +import shutil from os.path import lexists -class MoveFileCommand(object): + +class Command(object): + def execute(self): + pass + def undo(self): + pass + + +class CopyFileCommand(Command): + # TODO handle src parameter as path + def __init__(self, src ): + self.src = src + self.dest = "Copy_"+src + self.name = "Copy " + src + + def execute(self): + self.copy(self.src, self.dest) + + def undo(self): + print('remove {} '.format( self.dest)) + os.remove( self.dest) + + def copy(self , src,dest ): + print('copy {} to {}'.format(src, dest)) + shutil.copy(src, dest) + + +class MoveFileCommand(Command): def __init__(self, src, dest): self.src = src self.dest = dest - + self.name="Move "+src def execute(self): self.rename(self.src, self.dest) @@ -25,26 +54,75 @@ def main(): command_stack = [] # commands are just pushed into the command stack + command_stack.append(CopyFileCommand('foo1.txt')) + command_stack.append(CopyFileCommand('Copy_foo1.txt')) + command_stack.append(MoveFileCommand('foo.txt', 'bar.txt')) command_stack.append(MoveFileCommand('bar.txt', 'baz.txt')) + names=["foo.txt","baz.txt","foo1.txt"] + for name in names: + try: + os.unlink(name) + except: + pass + finally: + pass + # verify that none of the target files exist assert(not lexists("foo.txt")) assert(not lexists("bar.txt")) assert(not lexists("baz.txt")) try: - with open("foo.txt", "w"): # Creating the file - pass + with open("foo.txt", "w") as src1: # Creating the file + src1.close() + with open("foo1.txt", "w") as src2: # Creating the file + src2.close() # they can be executed later on for cmd in command_stack: + print "******Executing "+cmd.name cmd.execute() # and can also be undone at will for cmd in reversed(command_stack): + print "" \ + "------Undo " + cmd.name cmd.undo() + + + + except : + #e = sys.exc_info()[0] + #print("Error: %s" % e) + exc_type, exc_value, exc_traceback = sys.exc_info() + print "*** print_tb:" + traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) + print "*** print_exception:" + traceback.print_exception(exc_type, exc_value, exc_traceback, + limit=2, file=sys.stdout) + print "*** print_exc:" + traceback.print_exc() + print "*** format_exc, first and last line:" + formatted_lines = traceback.format_exc().splitlines() + print formatted_lines[0] + print formatted_lines[-1] + print "*** format_exception:" + print repr(traceback.format_exception(exc_type, exc_value, + exc_traceback)) + print "*** extract_tb:" + print repr(traceback.extract_tb(exc_traceback)) + print "*** format_tb:" + print repr(traceback.format_tb(exc_traceback)) + print "*** tb_lineno:", exc_traceback.tb_lineno + + finally: + print "finaly block " os.unlink("foo.txt") + os.unlink("foo1.txt") + + if __name__ == "__main__": main() diff --git a/test_command.py b/test_command.py index 584b273b..daadb4db 100644 --- a/test_command.py +++ b/test_command.py @@ -1,5 +1,6 @@ #!/usr/bin/env python from command import MoveFileCommand +from command import CopyFileCommand import os, shutil, subprocess, sys if sys.version_info < (2, 7): @@ -27,11 +28,14 @@ def setUpClass(self): """ os.mkdir('test_command') open('test_command/foo.txt', 'w').close() + open('test_command/foo1.txt', 'w').close() + self.__get_test_directory() self.command_stack = [] self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'foo.txt'), os.path.join(self.test_dir, 'bar.txt'))) self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'bar.txt'), os.path.join(self.test_dir, 'baz.txt'))) - + self.command_stack.append(CopyFileCommand(os.path.join(self.test_dir,'foo1.txt'))) + self.command_stack.append(CopyFileCommand(os.path.join(self.test_dir,'Copy_foo1.txt'))) def test_sequential_execution(self): self.command_stack[0].execute() output_after_first_execution = os.listdir(self.test_dir) @@ -40,6 +44,10 @@ def test_sequential_execution(self): output_after_second_execution = os.listdir(self.test_dir) self.assertEqual(output_after_second_execution[0], 'baz.txt') + self.command_stack[2].execute() + output_after_second_execution = os.listdir(self.test_dir) + self.assertEqual(output_after_second_execution[2], 'baz.txt foo1.txt Copy_foo1.txt') + def test_sequential_undo(self): self.command_stack = list(reversed(self.command_stack)) self.command_stack[0].undo()