Exemple #1
0
    def _test_path_and_fileobj(self, data, expected, **kwargs):
        # write the file to disk
        path = self.mktempfile(data)

        path_fp = safe_open(path, 'r', **kwargs)
        path_data = path_fp.read()
        path_fp.close()

        fp_fp = safe_open(open(path, 'rb'), 'r', **kwargs)
        fp_data = fp_fp.read()
        fp_fp.close()

        self.assertEqual(path_data, fp_data)
        self.assertEqual(path_data, expected)
Exemple #2
0
    def _test_path_and_fileobj(self, data, expected, **kwargs):
        # write the file to disk
        path = self.mktempfile(data)

        path_fp = safe_open(path, 'r', **kwargs)
        path_data = path_fp.read()
        path_fp.close()

        fp_fp = safe_open(open(path, 'rb'), 'r', **kwargs)
        fp_data = fp_fp.read()
        fp_fp.close()

        assert path_data == fp_data
        assert path_data == expected
Exemple #3
0
    def include_file(self, shell, path):
        fp = None
        ifile = IncludeFile(path)

        if self.stack:
            for i in self.stack:
                if i.abspath == ifile.abspath:
                    self.error(shell, "recursive include for file ",
                               ifile.abspath, '\n')
                    return -1

        self.stack.append(ifile)

        try:
            fp = safe_open(path, 'r')
        except (OSError, IOError) as e:
            self.error(shell, "error opening file ", path, ": ", str(e), '\n')
            return -1

        for line in fp:
            shell.execute(line.strip())
            ifile.line += 1

        self.stack.pop()
        fp.close()

        return 0
Exemple #4
0
    def include_file(self, shell, path):
        fp = None
        ifile = IncludeFile(path)

        if self.stack:
            for i in self.stack:
                if i.abspath == ifile.abspath:
                    self.error(shell, "recursive include for file ",
                               ifile.abspath, '\n')
                    return -1

        self.stack.append(ifile)

        try:
            fp = safe_open(path, 'r')
        except (OSError, IOError) as e:
            self.error(shell, "error opening file ", path, ": ", str(e), '\n')
            return -1

        for line in fp:
            shell.execute(line.strip())
            ifile.line += 1

        self.stack.pop()
        fp.close()

        return 0
Exemple #5
0
    def include_file(self, shell, path):
        fp = None
        ifile = IncludeFile(path)

        if self.stack:
            for i in self.stack:
                if i.abspath == ifile.abspath:
                    self.error(shell, "recursive include for file ",
                               ifile.abspath, '\n')
                    return -1

        self.stack.append(ifile)

        try:
            fp = safe_open(path, 'r')
        except (OSError, IOError) as e:
            self.error(shell, "error opening file {}: {}".format(path, str(e)))
            return -1

        try:
            # Execute the lines in the file
            shell.include(fp)
        except Exception as e:
            self.error(shell, "error executing file ", path, ": ", str(e))

        self.stack.pop()
        fp.close()

        return 0
Exemple #6
0
    def include_file(self, shell, path):
        fp = None
        ifile = IncludeFile(path)

        if self.stack:
            for i in self.stack:
                if i.abspath == ifile.abspath:
                    self.error(shell, "recursive include for file ",
                               ifile.abspath, '\n')
                    return -1

        self.stack.append(ifile)

        try:
            fp = safe_open(path, 'r')
        except (OSError, IOError) as e:
            self.error(shell, "error opening file {}: {}".format(path, str(e)))
            return -1

        try:
            # Execute the lines in the file
            shell.include(fp)
        except Exception as e:
            self.error(shell, "error executing file ", path, ": ", str(e))

        self.stack.pop()
        fp.close()

        return 0
Exemple #7
0
    def run(self, shell, args):
        try:
            ns = self.parser.parse_args(args)
        except CommandShortCircuit as e:
            return e.code

        rc = 0
        if ns.subcmd == 'list':
            start = 0
            if ns.count and ns.count > 0:
                start = len(shell.ctx.history) - ns.count
                if start < 0:
                    start = 0

            i = start + 1
            for event in shell.ctx.history[start:]:
                print(i, '    ', event, sep='')
                i += 1
        elif ns.subcmd == 'clear':
            shell.ctx.history.clear()
        elif ns.subcmd == 'delete':
            try:
                del shell.ctx.history[ns.index - 1]
            except:
                self.error(shell, "invalid event index\n")
                rc = -1
        elif ns.subcmd == 'save':
            try:
                with open(ns.path, 'w') as fp:
                    for event in shell.ctx.history:
                        fp.write(event)
                        fp.write('\n')
            except IOError as e:
                self.error(shell, "error saving history to file: ",
                           os.strerror(e.errno), '\n')
                rc = -1
        elif ns.subcmd == 'load':
            try:
                lines = []
                with safe_open(ns.path, 'r') as fp:
                    for event in fp:
                        lines.append(str(event))

                shell.ctx.history.clear()
                for line in lines:
                    shell.ctx.history.append(line.strip())
            except IOError as e:
                self.error(shell, "error saving history to file: ",
                           os.strerror(e.errno), '\n')
                rc = -1
            except UnicodeEncodeError:
                self.error(
                    shell, "error: file contains invalid unicode characters\n"
                )

        return rc
Exemple #8
0
    def run(self, shell, args):
        try:
            ns = self.parser.parse_args(args)
        except CommandShortCircuit as e:
            return e.code

        rc = 0
        if ns.subcmd == 'list':
            start = 0
            if ns.count and ns.count > 0:
                start = len(shell.ctx.history) - ns.count
                if start < 0:
                    start = 0

            i = start + 1
            for event in shell.ctx.history[start:]:
                print(i, '    ', event, sep='')
                i += 1
        elif ns.subcmd == 'clear':
            shell.ctx.history.clear()
        elif ns.subcmd == 'delete':
            try:
                del shell.ctx.history[ns.index - 1]
            except:
                self.error(shell, "invalid event index\n")
                rc = -1
        elif ns.subcmd == 'save':
            try:
                with open(ns.path, 'w') as fp:
                    for event in shell.ctx.history:
                        fp.write(event)
                        fp.write('\n')
            except IOError as e:
                self.error(shell, "error saving history to file: ",
                           os.strerror(e.errno), '\n')
                rc = -1
        elif ns.subcmd == 'load':
            try:
                lines = []
                with safe_open(ns.path, 'r') as fp:
                    for event in fp:
                        lines.append(str(event))

                shell.ctx.history.clear()
                for line in lines:
                    shell.ctx.history.append(line.strip())
            except IOError as e:
                self.error(shell, "error saving history to file: ",
                           os.strerror(e.errno), '\n')
                rc = -1
            except UnicodeEncodeError:
                self.error(
                    shell, "error: file contains invalid unicode characters\n")

        return rc
Exemple #9
0
 def load_tips(self, path):
     fp = safe_open(path, "r")
     tip = []
     for line in fp.readlines():
         line = line.rstrip()
         if line:
             tip.append(line)
         elif tip:
             self.tips.append(" ".join(tip))
             tip = []
     if tip:
         self.tips.append(" ".join(tip))
     fp.close()
Exemple #10
0
 def load_tips(self, path):
     fp = safe_open(path, 'r')
     tip = []
     for line in fp.readlines():
         line = line.rstrip()
         if line:
             tip.append(line)
         elif tip:
             self.tips.append(' '.join(tip))
             tip = []
     if tip:
         self.tips.append(' '.join(tip))
     fp.close()
Exemple #11
0
    def include_file(self, shell, path, ctx):
        fp = None
        ifile = IncludeFile(path)

        top = False
        templ = ''
        if self.stack:
            #templ = shell.error.prefix
            for i in self.stack:
                if i.abspath == ifile.abspath:
                    self.error(shell, "recursive include for file ",
                               ifile.abspath, '\n')
                    return -1
        else:
            #templ = shell.error.prefix + "error in file {file} on line {line}: "
            top = True

        self.stack.append(ifile)

        try:
            fp = safe_open(path, 'r')
        except (OSError, IOError) as e:
            self.error(shell, "error opening file ", path, ": ", str(e), '\n')
            return -1

        #orig_prefix = shell.error.prefix
        next = ctx.fork()
        for line in fp:
            #shell.error.prefix = templ.format(file=ifile.name, line=ifile.line)
            shell.execute(line.strip(), next)
            ifile.line += 1

        #if top:
        #    shell.error.prefix = orig_prefix

        self.stack.pop()
        fp.close()

        return 0
Exemple #12
0
    def include_file(self, shell, path, ctx):
        fp = None
        ifile = IncludeFile(path)

        top = False
        templ = ''
        if self.stack:
            #templ = shell.error.prefix
            for i in self.stack:
                if i.abspath == ifile.abspath:
                    self.error(shell, "recursive include for file ", ifile.abspath, '\n')
                    return -1
        else:
            #templ = shell.error.prefix + "error in file {file} on line {line}: "
            top = True

        self.stack.append(ifile)

        try:
            fp = safe_open(path, 'r')
        except (OSError, IOError) as e:
            self.error(shell, "error opening file ", path, ": ", str(e), '\n')
            return -1

        #orig_prefix = shell.error.prefix
        next = ctx.fork()
        for line in fp:
            #shell.error.prefix = templ.format(file=ifile.name, line=ifile.line)
            shell.execute(line.strip(), next)
            ifile.line += 1

        #if top:
        #    shell.error.prefix = orig_prefix

        self.stack.pop()
        fp.close()

        return 0
Exemple #13
0
 def test_rb_fp(self):
     path = self.mktempfile(b"hello")
     fp = open(path, 'rb')
     self.assertEqual(fp, safe_open(fp, 'rb'))
Exemple #14
0
 def test_rb_path(self):
     path = self.mktempfile(b"hello")
     fp = safe_open(path, 'rb')
     assert fp.mode == 'rb'
Exemple #15
0
    def run(self, shell, args, ctx):
        try:
            ns = self.parser.parse_args(args) #(shell, args)
        except CommandShortCircuit as e:
            return e.code

        rc = 0
        if ns.subcmd == 'list':
            start = 0
            if ns.count:
                start = len(shell.ctx.history) - ns.count
                if start < 0:
                    start = 0

            i = start + 1
            for event in shell.ctx.history[start:]:
                print(i, '    ', event, sep='')
                i += 1
        elif ns.subcmd == 'exec':
            event = None
            if ns.prefix.isdigit() or (ns.prefix[0] == '-' and ns.prefix[1:].isdigit()):
                try:
                    index = int(ns.prefix) - 1
                    event = shell.ctx.history[index]
                except ValueError:
                    self.error(shell, "invalid event index\n")
                    rc = -1
                except IndexError as e:
                    self.error(shell, "invalid event index\n")
                    rc = -1
            else:
                event = shell.ctx.history.search_prefix(ns.prefix)
                if event is None:
                    self.error(shell, "event not found")
                    rc = -1

            if event:
                print("found event: ", event, sep='')
                shell.execute(event,None)
                
        elif ns.subcmd == 'clear':
            shell.ctx.history.clear()
        elif ns.subcmd == 'delete':
            try:
                del shell.ctx.history[ns.index - 1]
            except:
                self.error(shell, "invalid event index\n")
                rc = -1
        elif ns.subcmd == 'save':
            try:
                with open(ns.path, 'w') as fp:
                    for event in shell.ctx.history:
                        fp.write(event)
                        fp.write('\n')
            except IOError as e:
                self.error(shell, "error saving history to file: ",
                           os.strerror(e.errno), '\n')
                rc = -1
        elif ns.subcmd == 'load':
            try:
                lines = []
                with safe_open(ns.path, 'r') as fp:
                    for event in fp:
                        lines.append(str(event))

                shell.ctx.history.clear()
                for line in lines:
                    shell.ctx.history.append(line.strip())
            except IOError as e:
                self.error(shell, "error saving history to file: ",
                           os.strerror(e.errno), '\n')
                rc = -1
            except UnicodeEncodeError:
                self.error(shell, "error: file contains invalide unicode characters\n")

        return rc
Exemple #16
0
 def test_rb_fp(self):
     path = self.mktempfile(b"hello")
     fp = open(path, 'rb')
     assert fp == safe_open(fp, 'rb')
Exemple #17
0
    def run(self, shell, args, ctx):
        ns = self.parser.parse_args(shell, args)
        if self.parser.rc is not None:
            return self.parser.rc

        rc = 0
        if ns.subcmd == 'list':
            start = 0
            if ns.count:
                start = len(shell.ctx.history) - ns.count
                if start < 0:
                    start = 0

            i = start + 1
            for event in shell.ctx.history[start:]:
                print(i, '    ', event, sep='')
                i += 1
        elif ns.subcmd == 'exec':
            event = None
            if ns.prefix.isdigit() or (ns.prefix[0] == '-'
                                       and ns.prefix[1:].isdigit()):
                try:
                    index = int(ns.prefix) - 1
                    event = shell.ctx.history[index]
                except ValueError:
                    self.error(shell, "invalid event index\n")
                    rc = -1
                except IndexError as e:
                    self.error(shell, "invalid event index\n")
                    rc = -1
            else:
                event = shell.ctx.history.search_prefix(ns.prefix)
                if event is None:
                    self.error(shell, "event not found")
                    rc = -1

            if event:
                print("found event: ", event, sep='')
        elif ns.subcmd == 'clear':
            shell.ctx.history.clear()
        elif ns.subcmd == 'delete':
            try:
                del shell.ctx.history[ns.index - 1]
            except:
                self.error(shell, "invalid event index\n")
                rc = -1
        elif ns.subcmd == 'save':
            try:
                with open(ns.path, 'w') as fp:
                    for event in shell.ctx.history:
                        fp.write(event)
                        fp.write('\n')
            except IOError as e:
                self.error(shell, "error saving history to file: ",
                           os.strerror(e.errno), '\n')
                rc = -1
        elif ns.subcmd == 'load':
            try:
                lines = []
                with safe_open(ns.path, 'r') as fp:
                    for event in fp:
                        lines.append(str(event))

                shell.ctx.history.clear()
                for line in lines:
                    shell.ctx.history.append(line.strip())
            except IOError as e:
                self.error(shell, "error saving history to file: ",
                           os.strerror(e.errno), '\n')
                rc = -1
            except UnicodeEncodeError:
                self.error(
                    shell,
                    "error: file contains invalide unicode characters\n")

        return rc
Exemple #18
0
 def test_empty_file_path(self):
     path = self.mktempfile(b'')
     assert safe_open(path, 'r').mode == 'r'
Exemple #19
0
 def load_motd(self, path):
     fp = safe_open(path, "r")
     self.motd = fp.read().rstrip()
     fp.close()
Exemple #20
0
 def test_empty_file_fp(self):
     path = self.mktempfile(b'')
     fp = open(path, 'rb')
     self.assertEqual(fp, safe_open(fp, 'r'))
Exemple #21
0
 def test_invalid_ascii(self):
     path = self.mktempfile(b'hello\xC3\x93')
     fp = safe_open(path, 'r', ascii_is_utf8=False, chunk_size=5)
     with pytest.raises(UnicodeDecodeError):
         fp.read()
Exemple #22
0
 def test_rb_path(self):
     path = self.mktempfile(b"hello")
     fp = safe_open(path, 'rb')
     self.assertEqual(fp.mode, 'rb')
Exemple #23
0
 def test_invalid_ascii(self):
     path = self.mktempfile(b'hello\xC3\x93')
     fp = safe_open(path, 'r', ascii_is_utf8=False, chunk_size=5)
     self.assertRaises(UnicodeDecodeError, fp.read)
Exemple #24
0
 def test_empty_file_fp(self):
     path = self.mktempfile(b'')
     fp = open(path, 'rb')
     assert fp == safe_open(fp, 'r')
Exemple #25
0
 def load_motd(self, path):
     fp = safe_open(path, 'r')
     self.motd = fp.read().rstrip()
     fp.close()
Exemple #26
0
 def test_empty_file_path(self):
     path = self.mktempfile(b'')
     self.assertEqual(safe_open(path, 'r').mode, 'r')