| Conditions | 9 |
| Total Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """ Package to convert PDF to PNG |
||
| 38 | def execute(self): |
||
| 39 | """ Execute the command |
||
| 40 | """ |
||
| 41 | self.logger.debug("::: PDF conversion (%s) :::" % str(self.unzipped)) |
||
| 42 | |||
| 43 | if not isdir(self.unzipped): |
||
| 44 | self.logger.error("%s is not a directory" % self.unzipped) |
||
| 45 | return 2 |
||
| 46 | |||
| 47 | self.logger.debug("Browsing %s for pdf files..." % self.unzipped) |
||
| 48 | pdf_list = [join(self.unzipped, f) for f in listdir(self.unzipped) |
||
| 49 | if isfile(join(self.unzipped, f)) and f.endswith(".pdf")] |
||
| 50 | |||
| 51 | if len(pdf_list) != 1: |
||
| 52 | self.logger.error("Incorrect number of PDF file in " + self.unzipped |
||
| 53 | + " (" + str(len(pdf_list)) + " found, 1 expected)") |
||
| 54 | self.finalize() |
||
| 55 | return 1 |
||
| 56 | |||
| 57 | filename = str(pdf_list[0]) |
||
| 58 | with open(filename, "rb") as pdf: |
||
| 59 | pdf_filereader = PyPDF2.PdfFileReader(pdf) |
||
| 60 | pdf_page_nb = pdf_filereader.getNumPages() |
||
| 61 | |||
| 62 | pdf_dirname = dirname(filename) |
||
| 63 | imagesdir = "png" |
||
| 64 | |||
| 65 | self.logger.debug(str(pdf_page_nb) + " page(s) detected") |
||
| 66 | for p in xrange(pdf_page_nb): |
||
| 67 | |||
| 68 | try: # Reading the PDF |
||
| 69 | img = PythonMagick.Image() |
||
| 70 | img.density(str(self.density)) |
||
| 71 | img.depth(self.depth) |
||
| 72 | img.quality(self.quality) |
||
| 73 | |||
| 74 | pdf_page_file = filename + '[' + str(p) + ']' |
||
| 75 | self.logger.debug("Reading " + pdf_page_file + "...") |
||
| 76 | img.read(pdf_page_file) |
||
| 77 | |||
| 78 | png_dirname = join(pdf_dirname, imagesdir) |
||
| 79 | png_filename = splitext(basename(filename))[0] + '-' + str(p) + '.png' |
||
| 80 | png_page_file = join(png_dirname, png_filename) |
||
| 81 | self.logger.debug("Writing " + png_page_file + "...") |
||
| 82 | img.write(png_page_file) |
||
| 83 | except Exception, e: |
||
| 84 | self.logger.fatal("An exception has been caugth: "+str(e.message)) |
||
| 85 | self.finalize() |
||
| 86 | return 1 |
||
| 87 | |||
| 88 | self.finalize() |
||
| 89 | return 0 |
||
| 90 | |||
| 96 |