Hafixie93

Pembelajaran

Dec 10th, 2020
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.45 KB | None | 0 0
  1. import argparse
  2. import datetime
  3. import os
  4. import pathlib
  5. import stat
  6.  
  7.  
  8. class Singleton:
  9.     def __init__(self):
  10.         self.commands_dir = {}
  11.  
  12.     def __call__(self, func):
  13.         self.commands_dir[func.__name__] = func
  14.  
  15.     @classmethod
  16.     def get_instance(cls):
  17.         if not hasattr(cls, 'instance'):
  18.             cls.instance = cls()
  19.         return cls.instance
  20.  
  21.  
  22. @Singleton.get_instance()
  23. def ls(command_args: tuple):
  24.     parser = argparse.ArgumentParser()
  25.  
  26.     parser.add_argument(
  27.         "-l",
  28.         dest="show_info",
  29.         action="store_true",
  30.         default=False
  31.     )
  32.  
  33.     args = parser.parse_args(command_args)
  34.  
  35.     file_names = os.listdir()
  36.     display_info = " ".join(file_names)
  37.  
  38.     # -l flag
  39.     if args.show_info:
  40.         files_info = []
  41.  
  42.         for file_name in file_names:
  43.             file_stat = os.stat(file_name)
  44.  
  45.             file_mode = stat.filemode(file_stat.st_mode)
  46.             file_hard_links_number = str(file_stat.st_nlink)
  47.             file_size = str(file_stat.st_size)
  48.             file_mod_date = str(
  49.                 datetime.datetime.fromtimestamp(file_stat.st_mtime)
  50.             )
  51.  
  52.             files_info.append(
  53.                 " ".join(
  54.                     [
  55.                         file_mode,
  56.                         file_hard_links_number,
  57.                         file_size,
  58.                         file_mod_date,
  59.                         file_name
  60.                     ]
  61.                 )
  62.             )
  63.  
  64.         display_info = "\n".join(files_info)
  65.  
  66.     return display_info
  67.  
  68.  
  69. @Singleton.get_instance()
  70. def cat():
  71.     pass
  72.  
  73.  
  74. @Singleton.get_instance()
  75. def cd(command_args: tuple):
  76.     parser = argparse.ArgumentParser()
  77.  
  78.     parser.add_argument(
  79.         "string",
  80.         metavar="dir_name",
  81.         type=str,
  82.         nargs="+"
  83.     )
  84.  
  85.     args = parser.parse_args(command_args)
  86.  
  87.     if args.dir_name in ["..", "../"]:
  88.         os.chdir(args.dir_name)
  89.  
  90.  
  91. @Singleton.get_instance()
  92. def rm():
  93.     pass
  94.  
  95.  
  96. @Singleton.get_instance()
  97. def pwd():
  98.     pass
  99.  
  100.  
  101. @Singleton.get_instance()
  102. def touch():
  103.     pass
  104.  
  105.  
  106. def main():
  107.     while True:
  108.         user_input = input(f"{pathlib.Path().absolute()}: ").split()
  109.  
  110.         if user_input[0] == "exit":
  111.             break
  112.  
  113.         command = user_input[0]
  114.         command_args = tuple(user_input[1::])
  115.  
  116.         print(Singleton.get_instance().commands_dir[command](command_args))
  117.  
  118.  
  119. if __name__ == '__main__':
  120.     main()
  121.  
Add Comment
Please, Sign In to add comment