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.')
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
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
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
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))
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'] ))
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
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={})
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
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
def cd(self, directory): self.np.cd(directory) output.debug('Change directory to "{}"'.format(self.np.real))