forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp_sensor.py
64 lines (51 loc) · 2 KB
/
ftp_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ftplib
import logging
from airflow.contrib.hooks.ftp_hook import FTPHook, FTPSHook
from airflow.operators.sensors import BaseSensorOperator
from airflow.utils.decorators import apply_defaults
class FTPSensor(BaseSensorOperator):
"""
Waits for a file or directory to be present on FTP.
:param path: Remote file or directory path
:type path: str
:param ftp_conn_id: The connection to run the sensor against
:type ftp_conn_id: str
"""
template_fields = ('path',)
@apply_defaults
def __init__(self, path, ftp_conn_id='ftp_default', *args, **kwargs):
super(FTPSensor, self).__init__(*args, **kwargs)
self.path = path
self.ftp_conn_id = ftp_conn_id
def _create_hook(self):
"""Return connection hook."""
return FTPHook(ftp_conn_id=self.ftp_conn_id)
def poke(self, context):
with self._create_hook() as hook:
logging.info('Poking for %s', self.path)
try:
hook.get_mod_time(self.path)
except ftplib.error_perm as e:
error = str(e).split(None, 1)
if error[1] != "Can't check for file existence":
raise e
return False
return True
class FTPSSensor(FTPSensor):
"""Waits for a file or directory to be present on FTP over SSL."""
def _create_hook(self):
"""Return connection hook."""
return FTPSHook(ftp_conn_id=self.ftp_conn_id)