Exemplo n.º 1
0
    def archive(self, src_filename):
        """
        Function run on the source pdf after parsing is complete.

        :param src_filename: ``string``
        """
        log.debug('%s archive not implemented' % self.__class__.__name__)
Exemplo n.º 2
0
 def write(self, output):
     """ """
     pdf_file_writer = PdfFileWriter()
     for page in self.pages:
         log.debug('Adding page to file for writing')
         pdf_file_writer.addPage(page.page)
     pdf_file_writer.write(output)
Exemplo n.º 3
0
    def process(self, file):
        """
        Function run on each parsed file.

        :param file: :class:`~efos.parser.File`
        """
        log.debug('%s process not implemented' % self.__class__.__name__)
Exemplo n.º 4
0
 def __init__(self, options=None):
     """ """
     log.debug('%s Setup' % self.__class__.__name__)
     if not options:
         log.critical('No options passed to %s' % self.__class__.__name__)
         raise ValueError('No Options')
     self.options = options
     self.setup()
Exemplo n.º 5
0
    def setup(self):
        log.debug('%s Setup' % self.__class__.__name__)
        self.dbx_key = self.options.dbx_key
        self.dbx_secret = self.options.dbx_secret
        self.dbx_token = self.get_token()

        try:
            self.dbx = dropbox.Dropbox(self.dbx_token)
            self.dbx_user = self.dbx.users_get_current_account()
            log.info(self.dbx_user.name.display_name)
        except AuthError as ex:
            log.warning('Dropbox Auth Error')
Exemplo n.º 6
0
 def _parse_data(self):
     """ """
     try:
         self._efos_sig = re.match(EFOS_SIG, self.raw_data).group()  # find the efos sig
         self.raw_data = self.raw_data.replace(self._efos_sig, "")
         self._is_efos = True
         log.debug('efos sig found: %s' % self.raw_data)
         self._parse_efos_data()
         self._parse_qs_data()
     except:
         pass
     log.debug('regex not found: %s' % self.raw_data)
Exemplo n.º 7
0
    def parse(self):
        """ """
        new_file = None
        for pdf_page in self.pdf_file.pages:
            page = Page(page=pdf_page)

            if page.is_efos:
                new_file = self.new_file(page.get_barcode())
                log.info("Creating page %s" % new_file.get_filename())
            elif new_file:
                new_file.add(page)
                log.debug("Adding page to %s" % new_file.get_filename())
Exemplo n.º 8
0
    def process(self, file):
        if self.options.disable_output:
            log.debug("FileHandler has been disabled.")
            return

        log.info("Saving %(filename)s" % {'filename': file.get_filename()})
        full_path = self.options.output_path % file.barcode.data
        full_path = os.path.normpath(os.path.join(full_path, file.get_filename()))
        try:
            f = open(full_path, 'wb')
            file.write(f)
            f.close()
        except IOError as ex:
            log.error("%(type)s: %(msg)s" % {'type': type(ex).__name__, 'msg': ex.strerror, 'args': ex.args})
Exemplo n.º 9
0
    def process(self, file):
        if self.options.url:
            log.info("Uploading %(filename)s to %(url)s" % {'filename': file.get_filename(), 'url': self.options.url})
            try:
                f = StringIO.StringIO()
                file.write(f)
                files = {'file': (os.path.basename(file.get_filename()), f.getvalue(), 'application/pdf', {})}
                log.debug(self.get_form_data(file))
                r = requests.post(self.options.url, data=self.get_form_data(file), files=files)

                if r.status_code == 200:
                    log.info("file uploaded!")
                else:
                    log.warning("Server responded with status code %s [%s]" % (r.status_code, r.text))

            except IOError as ex:
                log.error("%(type)s: %(msg)s" % {'type': type(ex).__name__, 'msg': ex.message, 'args': ex.args})
            except requests.exceptions.ConnectionError as ex:
                log.error("Could not contact server")
            except requests.exceptions.Timeout as ex:
                log.error("Request timed out")
            except Exception as ex:
                log.error("%(type)s: %(msg)s" % {'type': type(ex).__name__, 'msg': ex.message, 'args': ex.args})
Exemplo n.º 10
0
    def setup(self):
        log.debug('%s Setup' % self.__class__.__name__)

        if not os.path.isabs(self.options.output_path):
            self.options.output_path = os.path.join(self.options.watch, self.options.output_path)

        if not os.path.isabs(self.options.archive_path):
            self.options.archive_path = os.path.join(self.options.watch, self.options.archive_path)

        if not self.options.disable_output:
            if not os.path.isdir(self.options.output_path):
                log.debug("Creating output directory %s.", self.options.output_path)
                os.makedirs(self.options.output_path)
            log.info("Output directory: %s" % self.options.output_path)

        if self.options.archive:
            if not os.path.isdir(self.options.archive_path):
                log.debug("Creating archive directory %s.", self.options.archive_path)
                os.makedirs(self.options.archive_path)
            log.info("Archive directory: %s" % self.options.archive_path)
Exemplo n.º 11
0
 def __init__(self, filename=None, options=None):
     """ """
     if not options:
         log.critical('No options passed to Parser')
         raise ValueError('No Options')
     self.options = options
     self.filename = filename
     log.debug('saved: %s and %s' % (self.filename, self.options))
     self.files = []
     log.debug('init file: %s' % self.files)
     self.handlers = get_handlers(self.options)
     log.debug('init finished')
     try:
         if not os.path.exists(self.filename):
             print "File path is invalid."
         elif not os.path.isfile(self.filename):
             print "File does not exist."
         elif not os.access(self.filename, os.R_OK):
             print "File cannot be read."
         else:
             self.pdf_file = PdfFileReader(self.filename, strict=False)
     except IOError as ex:
         print "I/O error({0}): {1}".format(ex.errno, ex.strerror)
         raise ex