def create_directory_watcher_thread(): client = CloudStorageClient(constants.get_env(constants.env_gcloud_bucket)) dw = DirectoryWatcher(constants.get_env(constants.env_video_dir)) dw_thread = threading.Thread(target=dw.for_each_file_do, args=(client.upload, )) dw_thread.setDaemon(True) return dw_thread
def __init__(self, default_recipient=None, default_sender=None): """ :param str default_recipient: The recipient to be used if no other recipient is provided. """ self.default_recipient = default_recipient if default_recipient is not None \ else constants.get_env(constants.env_email_recipient) self.default_sender = default_sender if default_sender is not None \ else constants.get_env(constants.env_email_sender) logger.info( 'Initiated EmailClient with default sender [%s] and default recipient [%s]', self.default_sender, self.default_recipient)
def send(self, subject, body, sender=None, recipient=None, attachment_path=None): """ :param str subject: The subject of the email to be sent. :param str body: The body of the email to be sent. :param str sender: The sender of the email to be sent. :param str recipient: The recipient of the email to be sent :param str attachment_path: Path to an image to attach """ sg = sendgrid.SendGridAPIClient( apikey=constants.get_env(constants.env_sendgrid_api_key)) from_email = Email(self.default_sender if sender is None else sender) to_email = Email( self.default_recipient if recipient is None else recipient) content = Content(constants.email_content_type, body) email = Mail(from_email, subject, to_email, content) if attachment_path is not None: attachment = Attachment() with open(attachment_path, 'rb') as image_file: data = image_file.read() image_file.close() data_b64 = base64.b64encode(data) attachment.content = data_b64 parts = attachment_path.split('/') attachment.filename = parts[len(parts) - 1].rstrip( constants.file_temp_extension) attachment.disposition = constants.email_attachment_disposition email.add_attachment(attachment) sg.client.mail.send.post(request_body=email.get())
def run_command(command, extra_env=None, cwd=None): print('$', ' '.join(command)) env = get_env() if isinstance(extra_env, dict): for k, v in extra_env.items(): env[k] = v use_shell = True if sys.platform == 'win32' else False proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=use_shell, cwd=cwd, env=env) while True: output = proc.stdout.readline() if len(output) == 0 and proc.poll() is not None: break print(output.decode(sys.stdout.encoding)) return proc.returncode == 0
def create_rpi_camera_thread(): rpi_camera = RpiCamera(constants.get_env(constants.env_video_dir)) rpi_camera_thread = threading.Thread(target=rpi_camera.run) rpi_camera_thread.setDaemon(True) return rpi_camera_thread