Commit 5f5cc428 authored by jacquemier's avatar jacquemier
Browse files

upload/download/ls

parent 27b962d1
......@@ -33,12 +33,10 @@ def log(level, text):
Parameters
----------
level : LogLevel constant
text : string
text : Object
test to display.
"""
#print level(text)
print level + text + LogLevel.ENDC
print level + str(text) + LogLevel.ENDC
......@@ -79,6 +77,7 @@ def connect(server, username, password, port =80, protocol='https'):
except:
raise Exception
def auto(webdav):
'''
Automatically get the new file minus the current one
......@@ -99,6 +98,24 @@ def auto(webdav):
'''
return True
def list_dir_content(webdav, directory):
'''
list and display on screen dir contents
Parameters
----------
webdav: easywebdav.client.Client
webdav connected instance
directory : string
directory to list
'''
#log(LogLevel.INFO, webdav.ls(directory))
result=webdav.ls(directory)
print "total " + str(len(result))
for file in result:
print str(file.mtime) + "\t" + str(file.size) + "\t" + \
str(file.contenttype) +\
"\t" + str(file.name)
def execute(webdav, command, arguments=None):
'''
......@@ -125,24 +142,24 @@ def execute(webdav, command, arguments=None):
available_command = ('cd', 'ls', 'exists', 'mkdir', 'mkdirs', 'rmdir',
'delete', 'upload', 'download', 'auto')
#if command not in available_command:
find = False
for cmd in available_command:
if command.find(cmd) == 0:
find = True
if not find:
raise Exception("command {} not available".format(command))
if not arguments:
arguments = ''
elif command == 'auto':
if command == 'auto':
return auto(webdav)
elif command == 'cd':
return webdav.cd(arguments)
elif command == 'ls':
return webdav.ls(arguments)
list_dir_content(webdav, arguments)
elif command == 'exists':
return webdav.exists(arguments)
elif command == 'mkdir':
......@@ -155,6 +172,9 @@ def execute(webdav, command, arguments=None):
return webdav.delete(arguments)
elif command == 'upload' or command == 'download':
# convert string of arguments to list of arguments
if isinstance(arguments, str):
arguments = arguments.split()
if len(arguments) !=2:
raise Exception("Download wait 2 arguments remote_path, local_path")
if command == 'upload':
......@@ -166,23 +186,31 @@ def execute(webdav, command, arguments=None):
def parse_args():
"""
PArse command line arguments.
def main():
parser = argparse.ArgumentParser(formatter_class = argparse.RawDescriptionHelpFormatter,
epilog = textwrap.dedent('''\
Command arguments:
auto (automatically get and delete new files on webdav CompactRio server. Do not get and delete current acquisition file)
cd -a path
ls -a path=None
exists -a remote_path
mkdir -a path
mkdirs -a path
rmdir -a path
delete -a file_path
upload -a "local_path remote_path"
download -a "remote_path, local_path"
'''))
Parameters
----------
Returns
-------
ArgumentParser object
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''\
Command arguments:
auto (automatically get and delete new files on webdav CompactRio server. Do not get and delete current acquisition file)
cd -a path
ls -a path=None
exists -a remote_path
mkdir -a path
mkdirs -a path
rmdir -a path
delete -a file_path
upload -a "local_path remote_path"
download -a "remote_path, local_path"
'''))
parser.add_argument("server")
parser.add_argument("-v", "--verbose", help="increase output verbosity",
action="store_true")
......@@ -193,18 +221,22 @@ def main():
parser.add_argument("-t", "--port", help="port", required=False)
parser.add_argument("-r", "--protocol", help="protocol", required=False)
args = parser.parse_args()
# convert string of arguments to list of arguments
args.arguments = args.arguments.split()
return parser.parse_args()
def main():
args = parse_args()
if args.verbose:
print "verbosity turned on"
log(LogLevel.INFO, "Verbosity turned on")
# Connect to WebDav
try:
webdav = connect(args.server, args.username,args.username,
args.port, args.protocol)
log(LogLevel.INFO, 'Connect to Webdav server[{}], username[{}], '
'protocol[{}] success'
if args.verbose: log(LogLevel.INFO, 'Connect to Webdav server[{}], '
'username[{}], protocol[{}] success'
.format(args.server, args.username, args.protocol))
except:
......@@ -213,8 +245,9 @@ def main():
'}] failed'.format(args.server, args.username, args.protocol))
return
# Execute WebDav command
try:
result = execute(webdav, args.command, args.arguments)
execute(webdav, args.command, args.arguments)
log(LogLevel.INFO, "Command {} succeeded".format(args.command))
except Exception as e:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment