Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import getpass
- class GlobalPath:
- SELF_PATH = __file__[:__file__.rfind('\\')]
- visible_path = ''
- def ls(path: str = 'DEFAULT') -> str:
- if path != 'DEFAULT':
- files = os.listdir(path)
- else:
- files = os.listdir(GlobalPath.SELF_PATH)
- res = ''
- for offset, file in enumerate(files):
- res += file + '\t'
- if offset % 6 == 0 and offset != 0:
- res += '\n'
- return res
- def cat(*files) -> str:
- res = ''
- for file in files:
- with open(file=f'{GlobalPath.SELF_PATH}\\{file}', mode='r', encoding='utf-8') as f:
- for line in f:
- res += line
- return res
- def pwd() -> str:
- return GlobalPath.SELF_PATH
- def cd(path: str):
- if path == '..':
- GlobalPath.SELF_PATH = generate_new_path(GlobalPath.SELF_PATH)
- path = generate_new_path(GlobalPath.visible_path).replace('\\\\', '')
- GlobalPath.visible_path = path
- else:
- if os.path.exists(GlobalPath.SELF_PATH + f'\\{path}'):
- GlobalPath.SELF_PATH += f'\\{path}'
- GlobalPath.visible_path += f'\\{path}'
- def convert_path_to_list(visible_path: str) -> list:
- return visible_path.split('\\')
- def generate_new_path(path):
- tmp = convert_path_to_list(path)
- tmp.pop()
- path = ''
- for offset, file in enumerate(tmp):
- if offset != 0:
- path += f'\\{file}'
- else:
- path += f'{file}'
- return path
- def run():
- commands = {
- 'ls': ls,
- 'cat': cat,
- 'pwd': pwd,
- 'cd': cd,
- }
- while True:
- user_name = f'{getpass.getuser()}@ubuntu:{GlobalPath.visible_path if GlobalPath.visible_path else "~"}$ '
- enter_command = input(user_name).split(' ')
- command, args = enter_command[0], enter_command[1:]
- if not commands.get(command):
- continue
- try:
- res = commands[command](*args)
- except Exception as e:
- print(e)
- continue
- if res:
- print(res)
- if __name__ == '__main__':
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement