def process(self): """ """ if len(self.files) == 0: log.warning("No efos files found") for file in self.files: if file.page_count == 0: log.warning("%s has no pages. Will not process file." % file.get_filename()) else: for handler in self.handlers: handler.process(file)
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')
def get_filename(self): """ """ if self._filename: return self._filename self._filename = self.barcode.raw try: self._filename = self._filename_format % self.barcode.data except KeyError as ex: log.warning("Barcode does not contain %s which is required by file-format" % ex) except Exception as ex: log.error("%(type)s: %(msg)s" % {'type': type(ex).__name__, 'msg': ex.message, 'args': ex.args}) return self._filename
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})
def generate_token(self): if not (self.dbx_key and self.dbx_secret): log.warning('Must have a key and secret to generate a token.') return flow = dropbox.client.DropboxOAuth2FlowNoRedirect(self.dbx_key, self.dbx_secret) authorize_url = flow.start() # Have the user sign in and authorize this token authorize_url = flow.start() print('1. Go to: %s' % authorize_url) print('2. Click "Allow" (you might have to log in first)') print('3. Copy the authorization code.') code = raw_input("Enter the authorization code here: ").strip() # This will fail if the user enters an invalid authorization code try: access_token, user_id = flow.finish(code) print('Your token is: %s' % access_token) print('It is stored in dbx.dat') print('You may also put it in the config options.') return access_token except Exception as ex: print('Looks like that was an incorrect code.')