Пример #1
0
def test_AppHighLevel_plan_work():
    "AppHighLevel: test plan_work"
    from datetime import datetime
    from mob_map_dl.common import MapMeta
    from mob_map_dl.main import AppHighLevel

    print "Start"
    #Create source and destination lists
    src = [
        MapMeta("map1", "f/map1", 1, datetime(2000, 1, 1), "foo", "bar"),
        MapMeta("map2", "f/map2", 1, datetime(2000, 1, 1), "foo", "bar"),
        MapMeta("map3", "f/map3", 1, datetime(2000, 1, 1), "foo", "bar")
    ]
    dst = [
        MapMeta("map1", "f/map1", 1, datetime(2000, 1, 1), "foo", "bar"),
        MapMeta("map2", "f/map2", 1, datetime(1999, 1, 1), "foo", "bar")
    ]

    app = AppHighLevel()

    work = app.plan_work(src, dst, "only_missing")
    pprint(work)
    assert len(work) == 1
    assert work[0].disp_name == "map3"

    work = app.plan_work(src, dst, "replace_newer")
    pprint(work)
    assert len(work) == 2
    assert work[0].disp_name == "map2"
    assert work[1].disp_name == "map3"

    work = app.plan_work(src, dst, "replace_all")
    pprint(work)
    assert len(work) == 3
Пример #2
0
    def get_file_list_base(self, filter_pattern):
        """
        Return a list of locally stored maps. Maps are searched in 
        ``self.download_dir``.
        
        Argument
        --------
        
        filter_pattern: str
            pattern with wildcards to filter the filenames in 
            ``self.download_dir``. For example: "*.map".
            
        Returns
        -------
        
        list[MapMeta]
        """
        dir_names = os.listdir(self.download_dir)
        map_names = fnmatch.filter(dir_names, filter_pattern)
        map_names.sort()

        map_metas = []
        for name in map_names:
            archive_name = path.join(self.download_dir, name)
            disp_name = self.make_disp_name(name)
            _, size_total, date_time = self.get_map_extractor(archive_name)
            map_meta = MapMeta(disp_name=disp_name,
                               full_name=archive_name,
                               size=size_total,
                               time=date_time,
                               description="",
                               map_type=None)
            map_metas.append(map_meta)

        return map_metas
Пример #3
0
def test_AppHighLevel_filter_possible_work():
    from mob_map_dl.common import MapMeta
    from mob_map_dl.main import AppHighLevel

    print "Start"
    #Create application and device directories to initialize the application
    app_directory, mobile_device = create_writable_test_dirs("m02")
    #Create source and destination lists
    work = [
        MapMeta("osmand/map1", None, None, None, None, None),
        MapMeta("foo/map2", None, None, None, None, None),
        MapMeta("bar/map3", None, None, None, None, None)
    ]

    app = AppHighLevel()
    app.create_low_level_components(app_directory, mobile_device)

    good_work = app.filter_possible_work(work, app.local_managers)
    pprint(good_work)

    assert len(good_work) == 1
    assert good_work[0].disp_name == "osmand/map1"
Пример #4
0
    def get_file_list(self):
        """
        Get list of maps for Osmand that are available for download.
        
        Return
        -------
        
        list[MapMeta]
        
        Note
        ------
        
        The function parses the regular, human readable, HTML documents, that
        lists the existing maps for Openandromaps. 
        """
        #Return cached file list if it exists and is recent
        cached_file_list = self.get_cached_file_list()
        if cached_file_list:
            return cached_file_list

        map_metas = []
        for url in self.list_urls:
            #Download HTML document with list of maps from server of Osmand project
            req = urllib2.Request(url, None, self.headers)
            downloader = urllib2.urlopen(req)
            list_html = downloader.read()

            #Parse HTML list of maps
            root = lxml.html.document_fromstring(list_html)
            table = root.find(".//tbody")
            #        print lxml.html.tostring(table, pretty_print=True)
            for row in table:
                link = row[3][0]
                download_url = link.get("href")
                map_meta = MapMeta(
                    disp_name=self.make_disp_name(download_url),
                    full_name=download_url,
                    size=float(row[2].text) * 1024**2,  #[Byte]
                    time=dateutil.parser.parse(row[1].text),
                    description="",
                    map_type="openandromaps")
                map_metas.append(map_meta)

        self.set_cached_file_list(map_metas)
        return map_metas
Пример #5
0
def test_AppHighLevel_install_file():
    "AppHighLevel: test install_file()"
    from mob_map_dl.main import AppHighLevel
    from mob_map_dl.common import MapMeta

    print "Start"
    app_directory, mobile_device = create_writable_test_dirs("m04")
    file_meta = MapMeta(disp_name="osmand/Monaco_europe_2.obf",
                        full_name=path.join(app_directory,
                                            "osmand/Monaco_europe_2.obf.zip"),
                        size=None,
                        time=None,
                        description=None,
                        map_type=None)
    #Remove file that will be created though install algorithm
    os.remove(path.join(mobile_device, "osmand/Monaco_europe_2.obf"))

    app = AppHighLevel()
    app.create_low_level_components(app_directory, mobile_device)

    app.install_file(file_meta)

    assert path.isfile(path.join(mobile_device, "osmand/Monaco_europe_2.obf"))
Пример #6
0
def test_AppHighLevel_download_file():
    "AppHighLevel: test download_file()"
    from mob_map_dl.main import AppHighLevel
    from mob_map_dl.common import MapMeta

    print "Start"
    app_directory, mobile_device = create_writable_test_dirs("m03")
    file_meta = MapMeta(
        disp_name="osmand/Cape-verde_africa_2.obf",
        full_name=
        "http://download.osmand.net/download.php?standard=yes&file=Cape-verde_africa_2.obf.zip",
        size=None,
        time=None,
        description=None,
        map_type=None)

    app = AppHighLevel()
    app.create_low_level_components(app_directory, mobile_device)

    app.download_file(file_meta)

    assert path.isfile(
        path.join(app_directory, "osmand/Cape-verde_africa_2.obf.zip"))
Пример #7
0
def test_AppHighLevel_delete_file_local():
    "AppHighLevel: test install_file()"
    from mob_map_dl.main import AppHighLevel
    from mob_map_dl.common import MapMeta

    print "Start"
    app_directory, mobile_device = create_writable_test_dirs("m06")
    file_meta = MapMeta(disp_name="osmand/Monaco_europe_2.obf",
                        full_name=None,
                        size=None,
                        time=None,
                        description=None,
                        map_type=None)
    delete_path = path.join(app_directory, "osmand/Monaco_europe_2.obf.zip")
    #Test that directory that we are going to delete really exists
    assert path.exists(delete_path)

    app = AppHighLevel()
    app.create_low_level_components(app_directory, mobile_device)

    app.delete_file_local(file_meta)

    #test that file has really been deleted
    assert not path.exists(delete_path)
Пример #8
0
    def get_file_list(self):
        """
        Get list of maps for Osmand that are available for download.
        
        Return
        -------
        
        list[MapMeta]
        
        Note
        ------
        
        The function parses the regular, human readable, HTML document, that
        lists the existing maps for Osmand. It has the following structure:
        
        <html>
            <head> ... </head>
            <body>
                <h1> ... </h1>
                <table>
                    <tr> ... Table headers ... </tr>
                    <tr> ... Nonsense (".gitignore") ... </tr>
                    <tr>
                        <td>
                            <A HREF="/download.php?standard=yes&file=Afghanistan_asia_2.obf.zip">Afghanistan_asia_2.obf.zip</A>
                        </td>
                        <td>03.08.2014</td>
                        <td>8.2</td>
                        <td>
                            Map, Roads, POI, Transport, Address data for 
                            Afghanistan asia
                        </td>
                    </tr>
                    <tr> ... The next map record ... </tr>
                </table>
            </body>
        </html>
        """
        #Return cached file list if it exists and is recent
        cached_file_list = self.get_cached_file_list()
        if cached_file_list:
            return cached_file_list

        #Download HTML document with list of maps from server of Osmand project
        req = urllib2.Request(self.list_url, None, self.headers)
        u = urllib2.urlopen(req)
        list_html = u.read()
        #        print list_html

        #Parse HTML list of maps
        root = lxml.html.document_fromstring(list_html)
        table = root.find(".//table")
        map_metas = []
        for row in table[2:]:
            link = row[0][0]
            download_url = urlparse.urljoin(self.list_url, link.get("href"))
            map_meta = MapMeta(
                disp_name=self.make_disp_name(link.text),
                full_name=download_url,
                size=float(row[2].text) * 1024**2,  #[Byte]
                time=dateutil.parser.parse(row[1].text, dayfirst=True),
                description=row[3].text,
                map_type="osmand")
            map_metas.append(map_meta)

        self.set_cached_file_list(map_metas)
        return map_metas