Пример #1
0
 def __output_to_dir_func(self):
     """
     Requires:
         nothing
     Returns:
         nothing
     Logic:
         Create a file within the output directory.
         Read one file at a time. Output line to the newly-created file.
     """
     base_name = os.path.basename(self.__orig_file)
     base_name, ext  = os.path.splitext(base_name)
     output_file = os.path.join(self.__output_dir, '%s.xml' % base_name)
     # change if user wants to output to a specific file
     if self.__out_file:
         output_file = os.path.join(self.__output_dir, self.__out_file)
     user_response = 'o'
     if os.path.isfile(output_file) and not self.__no_ask:
         msg = 'Do you want to overwrite %s?\n' % output_file
         msg += ('Type "o" to overwrite.\n'
                 'Type any other key to print to standard output.\n')
         sys.stderr.write(msg)
         user_response = raw_input()
     if user_response == 'o':
         with open_for_read(self.__file) as read_obj:
             with open_for_write(self.output_file) as write_obj:
                 for line in read_obj:
                     write_obj.write(line)
     else:
         self.__output_to_standard_func()
Пример #2
0
 def __output_to_dir_func(self):
     """
     Requires:
         nothing
     Returns:
         nothing
     Logic:
         Create a file within the output directory.
         Read one file at a time. Output line to the newly-created file.
     """
     base_name = os.path.basename(self.__orig_file)
     base_name, ext = os.path.splitext(base_name)
     output_file = os.path.join(self.__output_dir, '%s.xml' % base_name)
     # change if user wants to output to a specific file
     if self.__out_file:
         output_file = os.path.join(self.__output_dir, self.__out_file)
     user_response = 'o'
     if os.path.isfile(output_file) and not self.__no_ask:
         msg = 'Do you want to overwrite %s?\n' % output_file
         msg += ('Type "o" to overwrite.\n'
                 'Type any other key to print to standard output.\n')
         sys.stderr.write(msg)
         user_response = raw_input()
     if user_response == 'o':
         with open(self.__file, 'r') as read_obj:
             with open(self.output_file, 'w') as write_obj:
                 for line in read_obj:
                     write_obj.write(line)
     else:
         self.__output_to_standard_func()
Пример #3
0
def cli(port=4444):
    prints('Connecting to remote debugger on port %d...' % port)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    for i in range(20):
        try:
            sock.connect(('127.0.0.1', port))
            break
        except socket.error:
            pass
        time.sleep(0.1)
    else:
        try:
            sock.connect(('127.0.0.1', port))
        except socket.error as err:
            prints('Failed to connect to remote debugger:', err, file=sys.stderr)
            raise SystemExit(1)
    prints('Connected to remote process')
    import readline
    histfile = os.path.join(cache_dir(), 'rpdb.history')
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)
    p = pdb.Pdb()
    readline.set_completer(p.complete)
    readline.parse_and_bind("tab: complete")
    stdin = getattr(sys.stdin, 'buffer', sys.stdin)
    stdout = getattr(sys.stdout, 'buffer', sys.stdout)

    try:
        while True:
            recvd = b''
            while not recvd.endswith(PROMPT) or select.select([sock], [], [], 0) == ([sock], [], []):
                buf = eintr_retry_call(sock.recv, 16 * 1024)
                if not buf:
                    return
                recvd += buf
            recvd = recvd[:-len(PROMPT)]
            if recvd.startswith(QUESTION):
                recvd = recvd[len(QUESTION):]
                stdout.write(recvd)
                raw = stdin.readline() or b'n'
            else:
                stdout.write(recvd)
                raw = b''
                try:
                    raw = raw_input(PROMPT.decode('utf-8'))
                except (EOFError, KeyboardInterrupt):
                    pass
                else:
                    if not isinstance(raw, bytes):
                        raw = raw.encode('utf-8')
                    raw += b'\n'
                if not raw:
                    raw = b'quit\n'
            eintr_retry_call(sock.send, raw)
    except KeyboardInterrupt:
        pass
Пример #4
0
 def get_input(prompt):
     prints(prompt, end=' ')
     ans = raw_input()
     if isinstance(ans, bytes):
         ans = ans.decode(enc)
     if iswindows:
         # https://bugs.python.org/issue11272
         ans = ans.rstrip('\r')
     return ans
Пример #5
0
    def __call__(self):
        if hasattr(self, 'readline'):
            history = os.path.join(cache_dir(), 'pyj-repl-history.txt')
            self.readline.parse_and_bind("tab: complete")
            try:
                self.readline.read_history_file(history)
            except EnvironmentError as e:
                if e.errno != errno.ENOENT:
                    raise
            atexit.register(partial(self.readline.write_history_file, history))

        def completer(text, num):
            if self.completions is None:
                self.to_repl.put(('complete', text))
                self.completions = list(filter(None, self.get_from_repl()))
                if not self.completions:
                    return None
            try:
                return self.completions[num]
            except (IndexError, TypeError, AttributeError, KeyError):
                self.completions = None

        if hasattr(self, 'readline'):
            self.readline.set_completer(completer)

        while True:
            lw = self.get_from_repl()
            if lw is None:
                raise SystemExit(1)
            q = self.prompt
            if hasattr(self, 'readline'):
                self.readline.set_pre_input_hook(lambda: (
                    self.readline.insert_text(lw), self.readline.redisplay()))
            else:
                q += lw
            try:
                line = raw_input(q)
                self.to_repl.put(('line', line))
            except EOFError:
                return
            except KeyboardInterrupt:
                self.to_repl.put(('SIGINT', None))
Пример #6
0
    def __call__(self):
        if hasattr(self, 'readline'):
            history = os.path.join(cache_dir(), 'pyj-repl-history.txt')
            self.readline.parse_and_bind("tab: complete")
            try:
                self.readline.read_history_file(history)
            except EnvironmentError as e:
                if e.errno != errno.ENOENT:
                    raise
            atexit.register(partial(self.readline.write_history_file, history))

        def completer(text, num):
            if self.completions is None:
                self.to_repl.put(('complete', text))
                self.completions = list(filter(None, self.get_from_repl()))
                if not self.completions:
                    return None
            try:
                return self.completions[num]
            except (IndexError, TypeError, AttributeError, KeyError):
                self.completions = None

        if hasattr(self, 'readline'):
            self.readline.set_completer(completer)

        while True:
            lw = self.get_from_repl()
            if lw is None:
                raise SystemExit(1)
            q = self.prompt
            if hasattr(self, 'readline'):
                self.readline.set_pre_input_hook(lambda:(self.readline.insert_text(lw), self.readline.redisplay()))
            else:
                q += lw
            try:
                line = raw_input(q)
                self.to_repl.put(('line', line))
            except EOFError:
                return
            except KeyboardInterrupt:
                self.to_repl.put(('SIGINT', None))
Пример #7
0
def debug_device_driver():
    from calibre.devices import debug
    debug(ioreg_to_tmp=True, buf=sys.stdout)
    if iswindows:  # no2to3
        raw_input('Press Enter to continue...')  # no2to3
Пример #8
0
def input_unicode(prompt):
    ans = raw_input(prompt)
    if isinstance(ans, bytes):
        ans = ans.decode(sys.stdin.encoding)
    return ans
Пример #9
0
 def get_input(prompt):
     prints(prompt, end=' ')
     ans = raw_input()
     if isinstance(ans, bytes):
         ans = ans.decode(enc)
     return ans
Пример #10
0
def input_unicode(prompt):
    ans = raw_input(prompt)
    if isinstance(ans, bytes):
        ans = ans.decode(sys.stdin.encoding)
    return ans
Пример #11
0
def debug_device_driver():
    from calibre.devices import debug
    debug(ioreg_to_tmp=True, buf=sys.stdout)
    if iswindows:
        raw_input('Press Enter to continue...')
Пример #12
0
def cli(port=4444):
    prints('Connecting to remote debugger on port %d...' % port)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    for i in range(20):
        try:
            sock.connect(('127.0.0.1', port))
            break
        except socket.error:
            pass
        time.sleep(0.1)
    else:
        try:
            sock.connect(('127.0.0.1', port))
        except socket.error as err:
            prints('Failed to connect to remote debugger:',
                   err,
                   file=sys.stderr)
            raise SystemExit(1)
    prints('Connected to remote process')
    import readline
    histfile = os.path.join(cache_dir(), 'rpdb.history')
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)
    p = pdb.Pdb()
    readline.set_completer(p.complete)
    readline.parse_and_bind("tab: complete")
    stdin = getattr(sys.stdin, 'buffer', sys.stdin)
    stdout = getattr(sys.stdout, 'buffer', sys.stdout)

    try:
        while True:
            recvd = b''
            while not recvd.endswith(PROMPT) or select.select(
                [sock], [], [], 0) == ([sock], [], []):
                buf = eintr_retry_call(sock.recv, 16 * 1024)
                if not buf:
                    return
                recvd += buf
            recvd = recvd[:-len(PROMPT)]
            if recvd.startswith(QUESTION):
                recvd = recvd[len(QUESTION):]
                stdout.write(recvd)
                raw = stdin.readline() or b'n'
            else:
                stdout.write(recvd)
                raw = b''
                try:
                    raw = raw_input(PROMPT.decode('utf-8'))
                except (EOFError, KeyboardInterrupt):
                    pass
                else:
                    if not isinstance(raw, bytes):
                        raw = raw.encode('utf-8')
                    raw += b'\n'
                if not raw:
                    raw = b'quit\n'
            eintr_retry_call(sock.send, raw)
    except KeyboardInterrupt:
        pass