Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import datetime
- import os
- import pathlib
- import stat
- class Singleton:
- def __init__(self):
- self.commands_dir = {}
- def __call__(self, func):
- self.commands_dir[func.__name__] = func
- @classmethod
- def get_instance(cls):
- if not hasattr(cls, 'instance'):
- cls.instance = cls()
- return cls.instance
- @Singleton.get_instance()
- def ls(command_args: tuple):
- parser = argparse.ArgumentParser()
- parser.add_argument(
- "-l",
- dest="show_info",
- action="store_true",
- default=False
- )
- args = parser.parse_args(command_args)
- file_names = os.listdir()
- display_info = " ".join(file_names)
- # -l flag
- if args.show_info:
- files_info = []
- for file_name in file_names:
- file_stat = os.stat(file_name)
- file_mode = stat.filemode(file_stat.st_mode)
- file_hard_links_number = str(file_stat.st_nlink)
- file_size = str(file_stat.st_size)
- file_mod_date = str(
- datetime.datetime.fromtimestamp(file_stat.st_mtime)
- )
- files_info.append(
- " ".join(
- [
- file_mode,
- file_hard_links_number,
- file_size,
- file_mod_date,
- file_name
- ]
- )
- )
- display_info = "\n".join(files_info)
- return display_info
- @Singleton.get_instance()
- def cat():
- pass
- @Singleton.get_instance()
- def cd(command_args: tuple):
- parser = argparse.ArgumentParser()
- parser.add_argument(
- "string",
- metavar="dir_name",
- type=str,
- nargs="+"
- )
- args = parser.parse_args(command_args)
- if args.dir_name in ["..", "../"]:
- os.chdir(args.dir_name)
- @Singleton.get_instance()
- def rm():
- pass
- @Singleton.get_instance()
- def pwd():
- pass
- @Singleton.get_instance()
- def touch():
- pass
- def main():
- while True:
- user_input = input(f"{pathlib.Path().absolute()}: ").split()
- if user_input[0] == "exit":
- break
- command = user_input[0]
- command_args = tuple(user_input[1::])
- print(Singleton.get_instance().commands_dir[command](command_args))
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment