Advertisement
banaandrew

Untitled

Oct 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 KB | None | 0 0
  1.  
  2. from pathlib import Path, PurePosixPath
  3. from shutil import copyfile
  4. import re
  5. import os
  6.  
  7. def __main__():
  8.     # will ask user for input again after invalid response or completion
  9.    
  10.     while(True):
  11.         # input_parser() returns a list
  12.         consider_list = input_parser_1()
  13.         print_list(consider_list)
  14.         interest_list = input_parser_1(consider_list)
  15.         print_list(interest_list)
  16.         take_action(input_parser_2(), interest_list)
  17.  
  18. def print_list(x) -> None:
  19.     #x.sort()
  20.     for elem in x:
  21.         print(elem)
  22.  
  23. def input_parser_1(consider_list=[]) -> list:
  24.     "receives input and stores the one-letter action and the phrase individually"
  25.    
  26.     while True:
  27.         response = input()
  28.         action = response[0]
  29.    
  30.         # Stores general phrase (could be path, text, etc) in p
  31.         p = response[2:]
  32.        
  33.         # If the inputted action ('D') is in the list of accepted actions ('DR'), proceed to consider_files()
  34.         if action in 'DR':
  35.             return consider_files(p, action)
  36.            
  37.         elif action in 'ANET<>':
  38.             return interest_files(p, action, consider_list)
  39.        
  40.         elif action in 'FDT':
  41.             print('hi')
  42.             return action
  43.            
  44.         else:
  45.             print('ERROR')
  46.            
  47. def input_parser_2() -> str:
  48.     while True:
  49.         response = input()
  50.         action = response
  51.        
  52.         if action in 'FDT':
  53.             return action
  54.        
  55.         else:
  56.             print('ERROR')
  57.  
  58. # separate function to consider files
  59. def consider_files(p, action, consider_list=[]) -> list:
  60.     "returns a list of considered files"
  61.        
  62.     if action == 'D':
  63.         for path in list(Path(p).iterdir()):
  64.             if path.is_file():
  65.                 consider_list.append(path)
  66.    
  67.     if action == 'R':
  68.         for path in list(Path(p).iterdir()):
  69.             if path.is_dir():
  70.                 consider_files(path, action, consider_list)
  71.             else:
  72.                 consider_list.append(path)
  73.    
  74.     return consider_list
  75.  
  76. def interest_files(p, action, consider_list, interest_list=[]) -> list:
  77.     "returns a list of interesting files"
  78.    
  79.     if action == 'A':
  80.         interest_list = consider_list
  81.    
  82.     if action == 'N':
  83.         for path in consider_list:
  84.             if p in str(path):
  85.                 interest_list.append(path)
  86.                
  87.     if action == 'E':
  88.         for path in consider_list:
  89.             extension = PurePosixPath(path).suffix
  90.             if p in extension:
  91.                 interest_list.append(path)
  92.                
  93.     if action == 'T':
  94.         for path in consider_list:
  95.             with open(path, 'r') as file:
  96.                 for line in file:
  97.                     if p in line:
  98.                         interest_list.append(path)
  99.                         break
  100.                    
  101.     if action == '<':
  102.         for path in consider_list:
  103.             if os.path.getsize(path) < int(p):
  104.                 interest_list.append(path)
  105.                
  106.     if action == '>':
  107.         for path in consider_list:
  108.             if os.path.getsize(path) > int(p):
  109.                 interest_list.append(path)
  110.    
  111.     return interest_list
  112.  
  113. def take_action(action, interest_list) -> None:
  114.     if action == 'F':
  115.         for path in interest_list:
  116.             with open(path, 'r') as file:
  117.                 print(file.readline().split(' ')[0])
  118.        
  119.     if action == 'D':
  120.         print("hi")
  121.         for path in interest_list:
  122.             print(path)
  123.             dst = str(path) + '.dup'
  124.             copy(path, dst)
  125.        
  126.     if action == 'T':
  127.         for path in interest_list:
  128.             os.utime(path)
  129.  
  130. if __name__ == '__main__':
  131.     __main__()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement