2017-12-30 18:19:40 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
""" WebDav Functions """
|
|
|
|
|
|
|
|
import os
|
2018-01-02 14:20:44 +00:00
|
|
|
import json
|
2017-12-30 18:19:40 +00:00
|
|
|
from datetime import datetime
|
|
|
|
import logging
|
|
|
|
import asyncio
|
|
|
|
import aioeasywebdav
|
|
|
|
|
|
|
|
class WebDav:
|
|
|
|
""" WebDav Client """
|
|
|
|
|
|
|
|
def __init__(self, url, username, password, webdav_basedir, tmp_dir):
|
|
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
self.logger.info('Connecting to WebDAV server: ' + url)
|
|
|
|
|
|
|
|
self.loop = asyncio.get_event_loop()
|
|
|
|
self.webdav_basedir = webdav_basedir
|
|
|
|
self.tmp_dir = tmp_dir
|
|
|
|
try:
|
|
|
|
self.webdav = aioeasywebdav.connect(
|
|
|
|
url,
|
|
|
|
username=username,
|
|
|
|
password=password,
|
|
|
|
)
|
|
|
|
except:
|
|
|
|
self.logger.error('WebDav connection failed - exiting')
|
|
|
|
|
|
|
|
self.logger.info('WebDav connection successfull')
|
|
|
|
|
|
|
|
def upload(self, file_name, f_id):
|
|
|
|
""" uploads a file to webdav - checks for existence before doing so """
|
|
|
|
|
|
|
|
# upload with webdav
|
|
|
|
remote_upload_dir = self.webdav_basedir + "/" + str(datetime.now().year) + "/" + f_id
|
|
|
|
self.logger.info('Uploading file to WebDAV:' + remote_upload_dir)
|
|
|
|
|
|
|
|
# create directory if not yet there
|
|
|
|
if not self.loop.run_until_complete(self.webdav.exists(remote_upload_dir)):
|
|
|
|
self.logger.info('Creating directory ' + remote_upload_dir)
|
|
|
|
self.loop.run_until_complete(self.webdav.mkdir(remote_upload_dir))
|
|
|
|
|
|
|
|
remote_file_path = remote_upload_dir + "/" + file_name
|
|
|
|
if self.loop.run_until_complete(self.webdav.exists(remote_file_path)):
|
|
|
|
self.logger.info('File ' + file_name + ' already exists on webdav')
|
|
|
|
else:
|
|
|
|
self.loop.run_until_complete(
|
|
|
|
self.webdav.upload(
|
|
|
|
os.path.join(self.tmp_dir, file_name),
|
|
|
|
remote_file_path,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.logger.info('File ' + file_name + ' uploaded')
|
|
|
|
|
|
|
|
def einsatz_exists(self, f_id):
|
|
|
|
""" check if an einsatz is already created """
|
|
|
|
|
|
|
|
remote_upload_dir = self.webdav_basedir + "/" + str(datetime.now().year) + "/" + f_id
|
|
|
|
if self.loop.run_until_complete(self.webdav.exists(remote_upload_dir)):
|
|
|
|
self.logger.info('Einsatz exists ' + f_id)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2018-01-02 14:20:44 +00:00
|
|
|
def store_lodur_data(self, f_id, lodur_data):
|
|
|
|
""" stores lodur data on webdav """
|
2017-12-30 18:19:40 +00:00
|
|
|
|
2018-01-02 14:20:44 +00:00
|
|
|
file_name = f_id + '_lodur.json'
|
2017-12-30 18:19:40 +00:00
|
|
|
file_path = os.path.join(self.tmp_dir, file_name)
|
|
|
|
|
2018-01-02 14:20:44 +00:00
|
|
|
file = open(file_path, 'w')
|
|
|
|
file.write(json.dumps(lodur_data))
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
self.logger.info('Stored Lodur data locally in: ' + file_path)
|
|
|
|
self.upload(file_name, f_id)
|
|
|
|
|
|
|
|
def get_lodur_data(self, f_id):
|
|
|
|
""" gets lodur data if it exists """
|
2017-12-30 18:19:40 +00:00
|
|
|
|
2018-01-02 14:20:44 +00:00
|
|
|
file_name = f_id + '_lodur.json'
|
2017-12-30 18:19:40 +00:00
|
|
|
file_path = os.path.join(self.tmp_dir, file_name)
|
|
|
|
|
|
|
|
# first check if we already have it locally - then check on webdav
|
|
|
|
if os.path.isfile(file_path):
|
|
|
|
with open(file_path, 'r') as content:
|
2018-01-02 14:20:44 +00:00
|
|
|
lodur_data = json.loads(content.read())
|
|
|
|
self.logger.info('[%s] Found Lodur data locally', f_id)
|
|
|
|
return lodur_data
|
2017-12-30 18:19:40 +00:00
|
|
|
else:
|
|
|
|
remote_upload_dir = self.webdav_basedir + "/" + str(datetime.now().year) + "/" + f_id
|
|
|
|
remote_file_path = remote_upload_dir + '/' + file_name
|
|
|
|
if self.loop.run_until_complete(self.webdav.exists(remote_file_path)):
|
|
|
|
self.loop.run_until_complete(self.webdav.download(remote_file_path, file_path))
|
|
|
|
with open(file_path, 'r') as content:
|
2018-01-02 14:20:44 +00:00
|
|
|
lodur_data = json.loads(content.read())
|
|
|
|
self.logger.info('[%s] Found Lodur data on WebDAV', f_id)
|
|
|
|
return lodur_data
|
2017-12-30 18:19:40 +00:00
|
|
|
else:
|
2018-01-02 14:20:44 +00:00
|
|
|
self.logger.info('[%s] No existing Lodur data found', f_id)
|
2017-12-30 18:19:40 +00:00
|
|
|
return False
|