def getSelectedFiles(self, init_folder, multiple, filters): """ init_folder: folder default open multiple: True for multiple selected filters: Example ( ('XML', '*.xml'), ('TXT', '*.txt'), ) """ if init_folder: init_folder = comun.path_to_url(init_folder) else: init_folder = comun.path_to_url(self.getPath()) folder = self._create_instance('com.sun.star.ui.dialogs.FilePicker') folder.setDisplayDirectory(init_folder) folder.setMultiSelectionMode(multiple) if filters: folder.setCurrentFilter(filters[0][0]) for f in filters: folder.appendFilter(f[0], f[1]) if folder.execute(): files = folder.getSelectedFiles() if multiple: return tuple([comun.path_to_os(f) for f in files]) else: return comun.path_to_os(files[0]) else: return ""
def exportPDF(self, doc, path_save, options): """ Export to PDF http://wiki.services.openoffice.org/wiki/API/Tutorials/PDF_export """ close = False if isinstance(doc, str): close = True doc = self.openDoc(doc, (('Hidden', True), )) if path_save: if comun.isdir(path_save): path_save = comun.replace_name_ext(path_save, doc.getURL(), EXT_PDF) else: path_save = comun.replace_ext(doc.getURL(), EXT_PDF) path_save = comun.path_to_url(path_save) try: filter_name = '{}_pdf_Export'.format(self.getTypeDoc(doc)) if options: filter_data = comun.set_properties(options) filter_options = ( ('FilterName', filter_name), ('FilterData', filter_data), ) else: filter_options = (('FilterName', filter_name), ) media_descriptor = comun.set_properties(filter_options) doc.storeToURL(path_save, media_descriptor) except: log.error('PDF', exc_info=True) if close: doc.dispose() if comun.exists(path_save): return comun.path_to_os(path_save) return ''
def exportPDF(self, doc, path_save, options): """ Export to PDF http://wiki.services.openoffice.org/wiki/API/Tutorials/PDF_export """ close = False if isinstance(doc, str): close = True doc = self.openDoc(doc, (('Hidden', True),)) if path_save: if comun.isdir(path_save): path_save = comun.replace_name_ext(path_save, doc.getURL(), EXT_PDF) else: path_save = comun.replace_ext(doc.getURL(), EXT_PDF) path_save = comun.path_to_url(path_save) try: filter_name = '{}_pdf_Export'.format(self.getTypeDoc(doc)) if options: filter_data = comun.set_properties(options) filter_options = ( ('FilterName', filter_name), ('FilterData', filter_data), ) else: filter_options = (('FilterName', filter_name),) media_descriptor = comun.set_properties(filter_options) doc.storeToURL(path_save, media_descriptor) except: log.error('PDF', exc_info=True) if close: doc.dispose() if comun.exists(path_save): return comun.path_to_os(path_save) return ''
def getPath(self, name='Work'): """ Return de path name in config http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1util_1_1XPathSettings.html """ if not name: name = 'Work' path = self._create_instance('com.sun.star.util.PathSettings') return comun.path_to_os(getattr(path, name))
def fileSave(self, path, mode='w', data=None): if not mode: mode = 'w' if not data: return path = comun.path_to_os(path) with open(path, mode) as f: f.write(data) return
def send_mail(server, message): sender = server.User receivers = message.To.split(',') + message.Cc.split(',') + message.Bcc.split(',') email = MIMEMultipart() email['From'] = sender email['To'] = message.To email['Cc'] = message.Cc email['Subject'] = message.Subject body = message.Body.replace('\\n', '<br />').replace('\n', '<br />') email.attach(MIMEText(body, 'html')) for f in message.Files: path = comun.path_to_os(f) if not comun.exists(path): continue part = MIMEBase('application', 'octet-stream') part.set_payload(open(path, 'rb').read()) encoders.encode_base64(part) part.add_header( 'Content-Disposition', "attachment; filename={}".format(comun.basename(path))) email.attach(part) try: smtp = smtplib.SMTP(server.Name, server.Port, timeout=TIMEOUT) if server.Ssl: smtp.ehlo() smtp.starttls() smtp.ehlo() log.info('Connect server...') smtp.login(server.User, server.Password) log.info('Send mail...') smtp.sendmail(sender, receivers, email.as_string()) log.info('Log out server...') smtp.quit() if server.PathSave and message.Save: save_message(server.PathSave, email) log.info('Close...') return True except SMTPAuthenticationError as e: err = str(e) if '534' in err and 'gmail' in server.Server: msg = 'Need activate cant access other app in your account GMail' log.debug(msg) return False elif '535' in err: msg = 'User or password are invalid' log.debug(msg) return False except SMTPException as e: log.debug(e) return False except Exception as e: log.debug(e) return False
def send_mail(server, message): sender = server.User receivers = message.To.split(',') + message.Cc.split( ',') + message.Bcc.split(',') email = MIMEMultipart() email['From'] = sender email['To'] = message.To email['Cc'] = message.Cc email['Subject'] = message.Subject body = message.Body.replace('\\n', '<br />').replace('\n', '<br />') email.attach(MIMEText(body, 'html')) for f in message.Files: path = comun.path_to_os(f) if not comun.exists(path): continue part = MIMEBase('application', 'octet-stream') part.set_payload(open(path, 'rb').read()) encoders.encode_base64(part) part.add_header('Content-Disposition', "attachment; filename={}".format(comun.basename(path))) email.attach(part) try: smtp = smtplib.SMTP(server.Name, server.Port, timeout=TIMEOUT) if server.Ssl: smtp.ehlo() smtp.starttls() smtp.ehlo() log.info('Connect server...') smtp.login(server.User, server.Password) log.info('Send mail...') smtp.sendmail(sender, receivers, email.as_string()) log.info('Log out server...') smtp.quit() if server.PathSave and message.Save: save_message(server.PathSave, email) log.info('Close...') return True except SMTPAuthenticationError as e: err = str(e) if '534' in err and 'gmail' in server.Server: msg = 'Need activate cant access other app in your account GMail' log.debug(msg) return False elif '535' in err: msg = 'User or password are invalid' log.debug(msg) return False except SMTPException as e: log.debug(e) return False except Exception as e: log.debug(e) return False
def fileOpen(self, path, mode='r', array=False): data = '' if not mode: mode = 'r' path = comun.path_to_os(path) with open(path, mode) as f: if array: data = tuple(f.read().splitlines()) else: data = f.read() return data
def getFolder(self, init_folder=''): if init_folder: init_folder = comun.path_to_url(init_folder) else: init_folder = comun.path_to_url(self.getPath()) folder = self._create_instance('com.sun.star.ui.dialogs.FolderPicker') folder.setDisplayDirectory(init_folder) if folder.execute(): return comun.path_to_os(folder.getDirectory()) else: return ''
def importCSV(self, path, options): """ See https://docs.python.org/3.3/library/csv.html#csv.reader """ path = comun.path_to_os(path) if options: config = comun.to_dict(options) try: with open(path) as f: if options: data = tuple(csv.reader(f, **config)) else: data = tuple(csv.reader(f)) array = tuple(tuple(r) for r in data) return array except: log.debug('CSV', exc_info=True) return ()
def exportCSV(self, path, data, options): """ See https://docs.python.org/3.3/library/csv.html#csv.writer """ path = comun.path_to_os(path) if options: config = comun.to_dict(options) try: with open(path, 'w') as f: if options: if not 'lineterminator' in config: config['lineterminator'] = '\n' writer = csv.writer(f, **config) else: writer = csv.writer(f, lineterminator='\n') writer.writerows(data) return True except: log.debug('CSV', exc_info=True) return False
def getPathDB(self, name): return comun.path_to_os(self._dbc.getDatabaseLocation(name))
def log(self, pathLog, data): path_log = comun.path_to_os(pathLog) with open(path_log, 'a') as out: out.write('{} - {} - '.format(str(datetime.now())[:19], NAME_EXT)) pprint(data, stream=out) return