Exemple #1
0
def get_code(path):
    """Read in the code from the specified file or the pasteboard."""
    if path is None:
        return pasteboard.get()
    else:
        with open(path, 'r', encoding='UTF-8') as html_file:
            code = html_file.read()
        return code
Exemple #2
0
    def test_repair(self):
        """Confirm that the code to be repaired is retrieved from the pasteboard and put back on it."""
        code = '<html></html>'
        pasteboard.set(code)
        main.main('repair -p'.split())

        self.mock_document.assert_called_with(document.Document, code)
        self.assertTrue(self._document.repair.called,
                        'The document should be repaired.')
        self.assertEqual(
            code, pasteboard.get(),
            'The repaired document should be put back on the pasteboard.')
Exemple #3
0
def main(args):
    ap = argparse.ArgumentParser()
    ap.add_argument('-o',
                    '--output-file',
                    nargs='?',
                    help='save content as file')
    ap.add_argument('url',
                    nargs='?',
                    help='the url to read from (default to clipboard)')

    ns = ap.parse_args(args)
    url = ns.url or clipboard.get()
    output_file = ns.output_file or url.split('/')[-1]

    try:

        #print('Opening: %s\n' % url)
        u = urlopen(url)

        meta = u.info()
        try:
            file_size = int(meta["Content-Length"])
        except (IndexError, ValueError, TypeError):
            file_size = 0

        #print("Save as: {} ".format(output_file), end="")
        #print("({} bytes)".format(file_size if file_size else "???"))

        with open(output_file, 'wb') as f:
            file_size_dl = 0
            block_sz = 8192
            while True:
                buf = u.read(block_sz)
                if not buf:
                    break
                file_size_dl += len(buf)
                f.write(buf)
                if file_size:
                    status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl *
                                                   100. / file_size)
                else:
                    status = "%10d" % file_size_dl
                #print('\r' + status, end="")
            print("")

    except Exception as e:
        print(e)
        print('Unable to download file: %s' % url)
        return 1

    return 0
Exemple #4
0
def main(args):

    from progress.bar import ChargingBar as Bar

    ap = argparse.ArgumentParser()
    ap.add_argument('-o', '--output-file', nargs='?', help='save content as file')
    ap.add_argument('url', nargs='?', help='the url to read from (default to clipboard)')

    ns = ap.parse_args(args)
    url = ns.url or clipboard.get()
    output_file = ns.output_file or url.split('/')[-1]

    try:

        #print('Opening: %s\n' % url)
        u = urlopen(url)

        meta = u.info()
        try:
            file_size = int(meta["Content-Length"])
        except (IndexError, ValueError, TypeError):
            file_size = 0

        #print("Save as: {} ".format(output_file), end="")
        #print("({} bytes)".format(file_size if file_size else "???"))
        
        with open(output_file, 'wb') as f:

            file_size_dl = 0
            block_sz = 8192

            if file_size != 0 and file_size is not None:
                bar = Bar('Downloading', max=100)
            else:
                bar = None

            _n = 0

            while True:
                buf = u.read(block_sz)
                if not buf:
                    break
                file_size_dl += len(buf)
                f.write(buf)
                
                if bar is not None:
                    n = int(file_size_dl * 100. / file_size)

                    if n == _n:
                        continue

                    _n = n

                    now = monotonic()
                    dt = now - bar._ts
                    bar.update_avg(n, dt)
                    bar._ts = now
                    bar.index = n
                    bar.update()

            if bar is not None:
                bar.finish()

    except Exception as e:
        print(e)
        print('Unable to download file: %s' % url)
        return 1

    return 0