Pandaaaa906

nooi draft

Feb 10th, 2021
615
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import shelve
  2. from os import getenv
  3.  
  4. app_id=getenv('app_id')
  5. app_token=getenv('app_token')
  6.  
  7. class API:
  8.     def __init__(self, app_id, app_token, cache_file='db'):
  9.         self.app_token = app_token
  10.         self.app_id = app_id
  11.         self.cache = shelve.open(cache_file, writeback=True)
  12.         self._auth()
  13.  
  14.     def _auth(self):
  15.         # do ur auth action
  16.         self.cache['token'] = 'some_token'
  17.         pass
  18.  
  19.     @property
  20.     def logged_in(self):
  21.         return bool(self.cache.get('token'))
  22.  
  23.     def funcs(self):
  24.         pass
  25.    
  26.  
  27. class Action:
  28.     def __init__(self, func, usage:str):
  29.         self.usage = usage
  30.         self.run = func
  31.        
  32.  
  33.  
  34. class Nooi:
  35.     def __init__(self, app_id, app_token):
  36.         self.api = API(app_id, app_token)
  37.         self.actions = dict()
  38.  
  39.     def run(self):
  40.         if self.api.logged_in:
  41.             self.login()
  42.         while True:
  43.             for k, func in self.actions.items():
  44.                 print(f'[{k}]: {func.usage}')
  45.             choice = input('Choose your action:')
  46.             if choice not in self.actions:
  47.                 print('Not valid action')
  48.                 continue
  49.             action = self.actions[choice]
  50.             try:
  51.                 action.run(self)
  52.             except BaseException as e:
  53.                 print(e)
  54.                 continue
  55.  
  56.     def register(self, action:str, usage:str):
  57.         def wrapper(func):
  58.             self.actions[action] = Action(func, usage)
  59.             def inner_wrapper(*args, **kwargs):
  60.                 return func(*args, **kwargs)
  61.             return inner_wrapper
  62.         return wrapper
  63.  
  64.     def login(self):
  65.         pass
  66.  
  67.  
  68.  
  69. nooi = Nooi(app_id=app_id, app_token=app_token)
  70.  
  71.  
  72. @nooi.register('cd', usage='Enter directory')
  73. def enter_file(instance):
  74.     print('u r in enter directory action')
  75.     fp = instance.fp
  76.     choice = input('Input file index')
  77.  
  78. @nooi.register('p', usage='Exit directory')
  79. def exit_file(instance):
  80.     print('u r in exit directory action')
  81.     fp = instance.fp
  82.     choice = input('Input file index')
  83.  
  84. @nooi.register('d', usage='Download file')
  85. def exit_file(instance):
  86.     print('u r in Download file action')
  87.     fp = instance.fp
  88.     choice = input('Input file index')
  89.  
  90.  
  91. if __name__ == '__main__':
  92.     nooi.run()
RAW Paste Data