Commit 67249436 authored by jnanar's avatar jnanar

initial commit

parents
This diff is collapsed.
#!/usr/bin/env python
# Copyright (c) 2018 odoo_cep project
# This code is distributed under the GPLv3 License
#!/usr/bin/env python
# Copyright (c) 2018 odoo_cep project
# This code is distributed under the GPLv3 License
# The code is inspired from https://github.com/rbarrois/aionotify
import aionotify
import argparse
import asyncio
import logging
import signal
import os
from datetime import datetime
from . import xmpp
import logging
logger = logging.getLogger(__name__)
class Watcher:
def __init__(self):
self.loop = None
self.watcher = None
self.task = None
self.path = None
def prepare(self, path):
self.path = path
self.watcher = aionotify.Watcher()
self.watcher.watch(path, aionotify.Flags.MODIFY | aionotify.Flags.CREATE | aionotify.Flags.DELETE)
@asyncio.coroutine
def _run(self, max_events):
yield from self.watcher.setup(self.loop)
for _i in range(max_events):
event = yield from self.watcher.get_event()
msg = str(event.name) + ' - ' + str(aionotify.Flags.parse(event.flags))
logger.info(msg)
if 'Flags.CREATE' in msg:
filename = event.name
# A new file has been uploaded
datetime_str = str(datetime.now())
full_filename = os.path.join(self.path , filename)
remote_filename = yield from xmpp.xmp_process(self.path, filename, 'send_file')
remote_filename = 'plop'
full_remote_filename = os.path.join(self.path + remote_filename)
os.rename(full_filename, full_filename + '-' + datetime_str)
self.shutdown()
def run(self, loop, max_events):
self.loop = loop
self.task = loop.create_task(self._run(max_events))
def shutdown(self):
self.watcher.close()
if self.task is not None:
self.task.cancel()
self.loop.stop()
def setup_signal_handlers(loop, example):
for sig in [signal.SIGINT, signal.SIGTERM]:
loop.add_signal_handler(sig, example.shutdown)
def watch(path='', events=1000, debug=False):
wa = Watcher()
wa.prepare(path)
loop = asyncio.get_event_loop()
if debug:
loop.set_debug(True)
setup_signal_handlers(loop, wa)
wa.run(loop, events)
try:
loop.run_forever()
finally:
loop.close()
This diff is collapsed.
#!/usr/bin/env python
# Copyright (c) 2018 odoo_cep project
# This code is distributed under the GPLv3 License
import file_sender_inotify.watcher as watcher
import logging
#logging.basicConfig(level=logging.INFO, filename='watcher.log')
logging.basicConfig(level=logging.DEBUG,)
logger = logging.getLogger(__name__)
if __name__ == '__main__':
path = '/home/arnaud/files/programmation/python/django/cep/odoo_cep/static/withdraw/xmpp'
events=1000
debug=True
watcher.watch(path=path, events=events, debug=debug)
\ No newline at end of file
<entry xmlns="http://www.w3.org/2005/Atom">
<author>
<name>AUTHOR</name>
</author>
<generator>cep.compagnonsducep.be</generator>
<title>File sent: {file}</title>
<content>The following file has been sent: {file}</content>
</entry>
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