Exemplo n.º 1
0
    def output(self, formatted):
        if self.outfile != sys.stdout:
            with open(self.outfile, "a+") as outfile:
                outfile.write(
                    formatted.encode(guess_encoding(outfile), 'replace') +
                    os.linesep)

        else:
            for line in formatted.split('\n'):
                if self.termrows and (self.print_lines + 1) >= self.termrows:
                    self.outfile.write(PROMPT)
                    self.outfile.flush()
                    readch()
                    self.outfile.write('\b \b' * len(PROMPT))
                    self.print_lines = 0

                plen = len('%s'.replace(self.BOLD, '').replace(self.NC, '') %
                           line)
                if isinstance(line, unicode):
                    line = line.encode(guess_encoding(self.outfile), 'replace')

                print(line)

                if self.termcols:
                    self.print_lines += int(plen / self.termcols) + 1
                else:
                    self.print_lines += 1
Exemplo n.º 2
0
    def run(cls, args=None):
        """
        This static method can be called to run the application.

        It creates the application object, handles options, setups logging, calls
        the main() method, and catches common exceptions.

        You can't do anything after this call, as it *always* finishes with
        a call to sys.exit().

        For example:

        >>> from weboob.application.myapplication import MyApplication
        >>> MyApplication.run()
        """

        cls.setup_logging(logging.INFO, [cls.create_default_logger()])

        if sys.version_info.major == 2:
            encoding = sys.stdout.encoding
            if encoding is None:
                encoding = guess_encoding(sys.stdout)
                cls.stdout = sys.stdout = codecs.getwriter(encoding)(
                    sys.stdout)
                # can't do the same with stdin, codecs.getreader buffers too much to be usable in a REPL

        if args is None:
            args = [(cls.stdin.encoding and isinstance(arg, bytes)
                     and arg.decode(cls.stdin.encoding) or to_unicode(arg))
                    for arg in sys.argv]

        try:
            app = cls()
        except BackendsConfig.WrongPermissions as e:
            print(e, file=cls.stderr)
            sys.exit(1)

        try:
            try:
                args = app.parse_args(args)
                sys.exit(app.main(args))
            except KeyboardInterrupt:
                print('Program killed by SIGINT', file=cls.stderr)
                sys.exit(0)
            except EOFError:
                sys.exit(0)
            except ConfigError as e:
                print('Configuration error: %s' % e, file=cls.stderr)
                sys.exit(1)
            except CallErrors as e:
                try:
                    app.bcall_errors_handler(e)
                except KeyboardInterrupt:
                    pass
                sys.exit(1)
            except ResultsConditionError as e:
                print('%s' % e, file=cls.stderr)
                sys.exit(1)
        finally:
            app.deinit()
Exemplo n.º 3
0
    def run(cls, args=None):
        """
        This static method can be called to run the application.

        It creates the application object, handles options, setups logging, calls
        the main() method, and catches common exceptions.

        You can't do anything after this call, as it *always* finishes with
        a call to sys.exit().

        For example:

        >>> from weboob.application.myapplication import MyApplication
        >>> MyApplication.run()
        """

        cls.setup_logging(logging.INFO, [cls.create_default_logger()])

        if sys.version_info.major == 2:
            encoding = sys.stdout.encoding
            if encoding is None:
                encoding = guess_encoding(sys.stdout)
                cls.stdout = sys.stdout = codecs.getwriter(encoding)(sys.stdout)
                # can't do the same with stdin, codecs.getreader buffers too much to be usable in a REPL

        if args is None:
            args = [(cls.stdin.encoding and isinstance(arg, bytes) and arg.decode(cls.stdin.encoding) or to_unicode(arg)) for arg in sys.argv]

        try:
            app = cls()
        except BackendsConfig.WrongPermissions as e:
            print(e, file=cls.stderr)
            sys.exit(1)

        try:
            try:
                args = app.parse_args(args)
                sys.exit(app.main(args))
            except KeyboardInterrupt:
                print('Program killed by SIGINT', file=cls.stderr)
                sys.exit(0)
            except EOFError:
                sys.exit(0)
            except ConfigError as e:
                print('Configuration error: %s' % e, file=cls.stderr)
                sys.exit(1)
            except CallErrors as e:
                try:
                    ret = app.bcall_errors_handler(e)
                except KeyboardInterrupt:
                    pass
                else:
                    sys.exit(ret)
                sys.exit(1)
            except ResultsConditionError as e:
                print('%s' % e, file=cls.stderr)
                sys.exit(1)
        finally:
            app.deinit()
Exemplo n.º 4
0
    def output(self, formatted):
        if self.outfile != sys.stdout:
            with open(self.outfile, "a+") as outfile:
                outfile.write(formatted.encode(guess_encoding(outfile), 'replace') + os.linesep)

        else:
            for line in formatted.split('\n'):
                if self.termrows and (self.print_lines + 1) >= self.termrows:
                    self.outfile.write(PROMPT)
                    self.outfile.flush()
                    readch()
                    self.outfile.write('\b \b' * len(PROMPT))
                    self.print_lines = 0

                plen = len('%s'.replace(self.BOLD, '').replace(self.NC, '') % line)
                if isinstance(line, unicode):
                    line = line.encode(guess_encoding(self.outfile), 'replace')

                print(line)

                if self.termcols:
                    self.print_lines += int(plen/self.termcols) + 1
                else:
                    self.print_lines += 1
Exemplo n.º 5
0
    def output(self, formatted):
        if self.outfile != sys.stdout:
            encoding = guess_encoding(sys.stdout)
            with open(self.outfile, "a+", encoding=encoding, errors='replace') as outfile:
                outfile.write(formatted + os.linesep)

        else:
            for line in formatted.split('\n'):
                if self.termrows and (self.print_lines + 1) >= self.termrows:
                    self.outfile.write(PROMPT)
                    self.outfile.flush()
                    readch()
                    self.outfile.write('\b \b' * len(PROMPT))
                    self.print_lines = 0

                plen = len(line.replace(self.BOLD, '').replace(self.NC, ''))

                print(line)

                if self.termcols:
                    self.print_lines += int(plen/self.termcols) + 1
                else:
                    self.print_lines += 1
Exemplo n.º 6
0
    def output(self, formatted):
        if self.outfile != sys.stdout:
            encoding = guess_encoding(sys.stdout)
            with open(self.outfile, "a+", encoding=encoding,
                      errors='replace') as outfile:
                outfile.write(formatted + os.linesep)

        else:
            for line in formatted.split('\n'):
                if self.termrows and (self.print_lines + 1) >= self.termrows:
                    self.outfile.write(PROMPT)
                    self.outfile.flush()
                    readch()
                    self.outfile.write('\b \b' * len(PROMPT))
                    self.print_lines = 0

                plen = len(line.replace(self.BOLD, '').replace(self.NC, ''))

                print(line)

                if self.termcols:
                    self.print_lines += int(plen / self.termcols) + 1
                else:
                    self.print_lines += 1
Exemplo n.º 7
0
 def flush(self):
     s = self.get_formatted_table()
     if s is not None:
         self.output(s.encode(guess_encoding(self.outfile), 'replace'))
Exemplo n.º 8
0
 def guess_encoding(self, stdio=None):
     return guess_encoding(stdio or self.stdout)
Exemplo n.º 9
0
 def guess_encoding(self, stdio=None):
     return guess_encoding(stdio or self.stdout)
Exemplo n.º 10
0
 def flush(self):
     s = self.get_formatted_table()
     if s is not None:
         self.output(s.encode(guess_encoding(self.outfile), 'replace'))