Esempio n. 1
0
def _launch_cli(client):
    """
    NutStore Command Line Interface (0.4.3)

    NutStore WebDAV Settings: https://github.com/Kxrr/nutstore-cli/blob/master/docs/tutorial.md

    Project Page: https://github.com/Kxrr/nutstore-cli
    """
    output.debug('Client setup done')
    output.info('Hello.'.format(client.username))
    output.info('Type "help" to see supported commands.')
    context = Context(client=client)
    history = InMemoryHistory()
    while True:
        try:
            text = prompt(
                message=u'[{}] > '.format(to_unicode(
                    context.path)),  # message param needs to be unicode
                completer=completer,
                history=history,
                auto_suggest=AutoSuggestFromHistory(),
            )
        except EOFError:
            break
        else:
            execute(text, context)
            if context.should_exit:
                break
    output.info('Goodbye.')
Esempio n. 2
0
 def download(self, remote_path, local_path=None):
     """Download a remote file to your machine."""
     local_path = local_path or tempfile.mktemp(
         suffix=splitext(remote_path)[-1])
     output.debug('[DOWNLOAD] {0} => {1}'.format(remote_path, local_path))
     self._client.download(self._to_real_path(remote_path), local_path)
     return local_path
Esempio n. 3
0
 def upload(self, local_path, remote_dir=None):
     """Upload a local file to the remote(with the same filename)"""
     name = basename(local_path)
     directory = remote_dir or self.cwd
     remote_path = join(directory, name)
     output.debug('[UPLOAD] {0} => {1}'.format(local_path, remote_path))
     self._client.upload(local_path, self._to_real_path(remote_path))
     return remote_path
Esempio n. 4
0
 def __init__(self, prefix):
     self.config = {}
     for k in CONFIG_KEYS:
         env_key = '{prefix}{key}'.format(prefix=prefix, key=k).upper()
         v = getenv(env_key, self.NOT_SET)
         if v is not self.NOT_SET:
             debug('Set "{}" to "{}" from environment variable {}'.format(
                 k, v, env_key))
             self.config[k] = v
Esempio n. 5
0
    def ls(self):
        """
        :rtype: list[easywebdav.client.File]
        """
        def file_in_dir(filename, directory):
            return (directory in filename) and (filename != directory)

        real_path = self.np.real
        output.debug('List "{}"'.format(real_path))
        return filter(lambda f: file_in_dir(f.name, real_path),
                      self._client.ls(real_path))
Esempio n. 6
0
 def visit_ls(self, node, children):
     pretty_files = [PrettyFile(ef) for ef in self.context.client.ls()]
     grep_keywords = children[2].children[4].children[0].text if children[2].children else None
     if grep_keywords:
         output.debug('Issue a grep "{}"'.format(grep_keywords))
         pretty_files = ifilter(lambda pfile: re.search(grep_keywords, pfile._name, flags=re.IGNORECASE),
                                pretty_files)
     pretty_files = ifilter(lambda pfile: bool(pfile._name), pretty_files)  # ignore who has a empty filename
     pretty_files = sorted(pretty_files, key=lambda pfile: pfile.modify_time)
     output.echo(tabulate.tabulate(
         [pfile.pack() for pfile in pretty_files],
         headers=['Filename', 'Size', 'Modify Time']
     ))
Esempio n. 7
0
def _main(ctx, username, key, working_dir):
    client = NutStoreClient(username=username,
                            password=key,
                            working_dir=to_str(working_dir),
                            check_conn=False)
    output.debug('Try to initial a client by given args')
    try:
        client.check_conn()
    except Exception as e:
        import traceback
        output.error('Login failed, detail: {0}\n'.format(
            save_text(traceback.format_exc())))
        import sys
        sys.exit(-1)
    else:
        ctx.obj['client'] = client
Esempio n. 8
0
def main():
    output.debug('Current version: {}'.format(__version__))
    import sys
    output.debug('Args: {}'.format(sys.argv))
    if '--help' not in sys.argv and not set(
            sys.argv) & {'interact', 'upload', 'download'}:
        output.debug('Set "interact" as sub command')
        sys.argv.insert(1, 'interact')
    _main(obj={})
Esempio n. 9
0
 def load(self, filename):
     config = {}
     if not exists(filename):
         debug('Config file {} not exist'.format(filename))
         return config
     debug('Loading config from {}'.format(filename))
     with open(filename) as f:
         for line in f.xreadlines():
             m = self.PARSE_RE.search(line)
             if m and (m.group(1).strip() in CONFIG_KEYS):
                 k = m.group(1).strip()
                 v = m.group(2).strip()
                 debug('Set "{}" to "{}" in {}'.format(k, v, filename))
                 config[k] = v
         return config
Esempio n. 10
0
 def rm(self, remote_path):
     """Remove a file on the remote."""
     output.debug('[DELETE] {0}'.format(remote_path))
     self._client.delete(self._to_real_path(remote_path))
     return remote_path
Esempio n. 11
0
 def cd(self, directory):
     self.np.cd(directory)
     output.debug('Change directory to "{}"'.format(self.np.real))