• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Calysto / metakernel / 651

pending completion
651

Pull #117

travis-ci

web-flow
Reinstate some of the expect tests
Pull Request #117: Use pexpect and clean up handling of replwrap

44 of 44 new or added lines in 6 files covered. (100.0%)

2626 of 3461 relevant lines covered (75.87%)

0.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.21
/metakernel/magics/download_magic.py
1
# Copyright (c) Metakernel Development Team.
2
# Distributed under the terms of the Modified BSD License.
3

4

5
from metakernel import Magic, option
1✔
6
import urllib
1✔
7
try:
1✔
8
    import urlparse
1✔
9
except ImportError:
1✔
10
    from urllib import parse as urlparse
1✔
11
import os
1✔
12

13
try:
1✔
14
    urllib.URLopener
1✔
15
    def download(url, filename):
×
16
        opener = urllib.URLopener()
×
17
        opener.retrieve(url, filename)
×
18
except: # python3
1✔
19
    import urllib.request
1✔
20
    def download(url, filename):
1✔
21
        g = urllib.request.urlopen(url)
1✔
22
        with open(filename, 'wb') as f:
1✔
23
            f.write(g.read())        
1✔
24

25
class DownloadMagic(Magic):
1✔
26

27
    @option(
1✔
28
        '-f', '--filename', action='store', default=None,
29
        help='use the provided name as filename'
30
    )
31
    def line_download(self, url, filename=None):
1✔
32
        """
33
        %download URL [-f FILENAME] - download file from URL
34

35
        This line magic will download and save a file. By
36
        default it will use the same filename as the URL.
37
        You can give it another name using -f.
38

39
        Examples:
40
            %%download http://some/file/from/internet.txt -f myfile.txt
41
            %%download http://some/file/from/program.ss
42

43
        """
44
        if filename is None:
1✔
45
            parts = urlparse.urlsplit(url)
1✔
46
            #('http', 'example.com', '/somefile.zip', '', '')
47
            path = parts[2]
1✔
48
            filename = os.path.basename(path)
1✔
49
            if filename != '':
1✔
50
                basename, extname = os.path.splitext(filename)
1✔
51
                if extname == '':
1✔
52
                    filename += ".html"
×
53
                filename = filename.replace("~", "")
1✔
54
                filename = filename.replace("%20", "_")
1✔
55
            else:
56
                filename = "output.html"
×
57
        try:
1✔
58
            download(url, filename)
1✔
59
            self.kernel.Print("Downloaded '%s'." % filename)
1✔
60
        except Exception as e:
×
61
            self.kernel.Error(str(e))
×
62

63

64
def register_magics(kernel):
1✔
65
    kernel.register_magics(DownloadMagic)
1✔
66

67
def register_ipython_magics():
1✔
68
    from metakernel import IPythonKernel
×
69
    from IPython.core.magic import register_line_magic
×
70
    kernel = IPythonKernel()
×
71
    magic = DownloadMagic(kernel)
×
72
    # Make magics callable:
73
    kernel.line_magics["download"] = magic
×
74

75
    @register_line_magic
×
76
    def download(line):
77
        kernel.call_magic("%download " + line)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc