Hi guys
Firstly I would like to thank all those people who answer these questions on Bytes. Really you guys are fantastic.
Now I have seen guys post code on this site and some gurus come along and reduce it to a few lines of readable, frankly sexy code. I would love to see how optimised you guys can get this, just so I can learn from you.
General overview:
I create a folder with different files extracted from an incoming mail readying to be processed by different open office programs. Ie .xls for calc .doc for writer.
Later in the process I will make use of unoconv to handle this but:
I have created a whitelist of files I accept, though should there be an attachment with a file extension not supported the script then copies the downloaded email to an ignore folder for manual processing. But I will still process the remaining attachments, that is why i dont throw and exception.
I would love to optimise _locate_not_sup ported and _file_extension _check and get it to lovely readable pythonic
Thank you so much!
Firstly I would like to thank all those people who answer these questions on Bytes. Really you guys are fantastic.
Now I have seen guys post code on this site and some gurus come along and reduce it to a few lines of readable, frankly sexy code. I would love to see how optimised you guys can get this, just so I can learn from you.
General overview:
I create a folder with different files extracted from an incoming mail readying to be processed by different open office programs. Ie .xls for calc .doc for writer.
Later in the process I will make use of unoconv to handle this but:
I have created a whitelist of files I accept, though should there be an attachment with a file extension not supported the script then copies the downloaded email to an ignore folder for manual processing. But I will still process the remaining attachments, that is why i dont throw and exception.
I would love to optimise _locate_not_sup ported and _file_extension _check and get it to lovely readable pythonic
Code:
def _file_extension_check(self):
'''check for ext validity, if none still return true just'''
self.logger.info("checking file extension validity")
whitelist = "*.pdf","*.rtf","*.doc","*.xls","*.tif","*.tiff"
root = root=self.currentEmailAttachmentsPath
pdfgen = self._locate(whitelist[0], root)
rtfgen = self._locate(whitelist[1], root)
docgen = self._locate(whitelist[2], root)
xlsgen = self._locate(whitelist[3], root)
tifgen = self._locate(whitelist[4], root)
tiffgen = self._locate(whitelist[5], root)
self._locate_not_supported(whitelist, root)
return 1
def _locate_not_supported(self, patterns, root=None):
for path, dirs, files in os.walk(os.path.abspath(root)):
for file in files:
name, ext = os.path.splitext(file)
if ext != patterns[0][1:] and \
ext != patterns[1][1:] and \
ext != patterns[2][1:] and \
ext != patterns[3][1:] and \
ext != patterns[4][1:] and \
ext != patterns[5][1:]:
self._email_to_ignore_folder()
else:
return
def _locate(self,pattern, root=None):
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
def _email_to_ignore_folder(self):
self.logger.info("email sent to ignore folder")
print self._emailpath + self._ignorefolder
shutil.move(self.currentEmailPath, self._emailpath + self._ignorefolder)
Comment