예제 #1
0
 def format_lls(self, target):
     table = []
     with os.scandir(target) as dir_entries:
         for entry in dir_entries:
             stats = entry.stat()
             table.append(
                 (stats.st_uid, *self.decode_size(stats.st_size),
                  self.decode_time(datetime.fromtimestamp(stats.st_mtime)),
                  entry.name))
     align = ['l', 'r', 'l', 'r', 'l']
     pad = [2, 0, 2, 2, 2]
     return pr.table(table, align=align, pad=pad)
예제 #2
0
 def format_ls(self, data):
     output = []
     for entry in data:
         output.append(
             ((self.decode_user(entry.sharing_info.modified_by) if hasattr(
                 entry.sharing_info, 'modified_by') else 'folder'),
              *(self.decode_size(entry.size) if hasattr(entry, 'size') else
                ('4096', '')),
              (self.decode_time(entry.server_modified) if hasattr(
                  entry, 'server_modified') else '-'), entry.name))
     align = ['l', 'r', 'l', 'r', 'l']
     pad = [2, 0, 2, 2, 2]
     return pr.table(output, align=align, pad=pad)
예제 #3
0
 def exec_command(self, cmd):
     args = shlex.split(cmd)
     cmd = args.pop(0)
     if cmd == 'exit':
         pr.print('goodbye')
         return False
     elif cmd == 'help':
         pr.print(
             pr.table((
                 ('cd', 'changes dropbox directory to specified directory'),
                 ('dir', 'displays working directories'),
                 ('exit', 'exits the dropbox API shell'),
                 ('help', 'lists all valid commands and their usage'),
                 ('get', 'downloads file from dropbox to local filesystem'),
                 ('lcd', 'changes local directory to specified directory'),
                 ('lls',
                  'lists all the files and folders in working local directory'
                  ),
                 ('ls',
                  'lists all the files and folders in working dropbox directory'
                  ), ('put',
                      'uploads file from local filesystem to dropbox'))))
     elif cmd == 'ls':
         if len(args) < 2:
             try:
                 target = (self.drop_dir if len(args) == 0 else
                           self.decode_dir(self.drop_dir, args[0]))
                 pr.print(target)
                 files = self.dbx.files_list_folder(
                     target if target != '/' else '').entries
                 pr.print(self.format_ls(files))
             except Exception:
                 pr.print('invalid target directory')
         else:
             pr.print(
                 f'command "ls" expected zero or one arguments but got {len(args)}'
             )
     elif cmd == 'lls':
         if len(args) < 2:
             target = (self.local_dir if len(args) == 0 else
                       self.decode_dir(self.local_dir, args[0]))
             if os.path.isdir(target):
                 pr.print(target)
                 pr.print(self.format_lls(target))
             else:
                 pr.print('invalid target directory')
         else:
             pr.print(
                 f'command "lls" expected zero or one arguments but got {len(args)}'
             )
     elif cmd == 'cd':
         if len(args) == 1:
             try:
                 target = self.decode_dir(self.drop_dir, args[0])
                 if target != '/':
                     self.dbx.files_get_metadata(target)
                 self.drop_dir = target
             except Exception:
                 pr.print('invalid target directory')
         else:
             pr.print(
                 f'command "cd" expected exactly one argument but got {len(args)}'
             )
     elif cmd == 'lcd':
         if len(args) == 1:
             target = self.decode_dir(self.local_dir, args[0])
             if os.path.isdir(target):
                 self.local_dir = target
             else:
                 pr.print('invalid target directory')
         else:
             pr.print(
                 f'command "lcd" expected exactly one argument but got {len(args)}'
             )
     elif cmd == 'dir':
         pr.print(f'drop:  {self.drop_dir}')
         pr.print(f'local: {self.local_dir}')
     elif cmd == 'put':
         if len(args) == 2:
             if os.path.isfile(args[0]):
                 try:
                     local = self.decode_dir(self.local_dir, args[0])
                     drop = self.decode_dir(self.drop_dir, args[1])
                     pr.print('uploading file to dropbox')
                     self.upload(local, drop)
                 except Exception as err:
                     pr.print('invalid dropbox file path')
                     raise (err)
             else:
                 pr.print('invalid local file path')
         else:
             pr.print(
                 f'command "put" expected exactly two arguments but got {len(args)}'
             )
     elif cmd == 'get':
         if len(args) == 2:
             if os.path.isdir('/'.join(args[0].split('/')[:-1])):
                 try:
                     local = self.decode_dir(self.local_dir, args[0])
                     drop = self.decode_dir(self.drop_dir, args[1])
                     pr.print('downloading file from dropbox')
                     self.download(local, drop)
                 except Exception:
                     pr.print('invalid dropbox file path')
             else:
                 pr.print('invalid local directory')
         else:
             pr.print(
                 f'command "get" expected exactly two arguments but got {len(args)}'
             )
     else:
         pr.print('invalid command; type "help" for list of valid commands')
     return True