Skip to content

Instantly share code, notes, and snippets.

@ngc696
Created March 23, 2014 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngc696/9723701 to your computer and use it in GitHub Desktop.
Save ngc696/9723701 to your computer and use it in GitHub Desktop.
import os
import shutil
__author__ = 'Evgeny Chernigovsky'
class Command:
def __init__(self, function, arg_count):
self.execute = function
self.arg_count = arg_count
def create_directory(path):
if os.path.exists(path):
print('error: directory already exists')
return
os.makedirs(path)
def remove(path):
if not os.path.exists(path):
print('error: path not exists')
return
if os.path.isdir(path):
shutil.rmtree(path)
elif os.path.isfile(path):
os.remove(path)
else:
print('error: path is not directory or file')
def move(source, destination):
if not os.path.isfile(source) and not os.path.isdir(source):
print('error: source is not directory or file')
return
if not os.path.isdir(destination):
print('error: destination is not existing directory')
return
if os.path.commonprefix([os.path.abspath(source), os.path.abspath(destination)]) == os.path.abspath(source):
print('error: source contains destination')
return
shutil.move(source, destination)
def copy(source, destination):
if not os.path.isfile(source) and not os.path.isdir(source):
print('error: source is not directory or file')
return
if not os.path.isdir(destination):
print('error: destination is not existing directory')
return
if os.path.commonprefix([os.path.abspath(source), os.path.abspath(destination)]) == os.path.abspath(source):
print('error: source contains destination')
return
shutil.copy(source, destination)
def program_exit():
exit(0)
command_dict = {'createdir': Command(create_directory, 1),
'remove': Command(remove, 1),
'move': Command(move, 2),
'copy': Command(copy, 2),
'exit': Command(program_exit, 0)}
while True:
print('$', end=' ')
name, separator, args = input().strip().partition(' ')
args = args.split()
if not name in command_dict.keys():
print('error: wrong command name')
continue
if len(args) != command_dict[name].arg_count:
print('error: wrong argument count')
continue
command_dict[name].execute(*args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment