Commit f58a4295 authored by Jgiannelos's avatar Jgiannelos
Browse files

cli: Default to reading input from stdin

parent e01b138b
Pipeline #1488 failed with stages
......@@ -5,13 +5,14 @@ Given an input file with a list of line seperated tiles in `{zoom}/{x}/{y}` form
## Usage
```
usage: cli.py [-h] tilelist_path maxzoom
usage: maps-deduped-tilelist [-h] minzoom maxzoom [tilelist_file]
Given a tile list as input, generate a distinct list of map tiles consisted of all the parent and children tiles recursively up to a zoom level.
positional arguments:
tilelist_path Path to the tilelist input file.
maxzoom The maxzoom of the generated tilelist.
minzoom The minimum zoom level of the generated tilelist.
maxzoom The maximum zoom level of the generated tilelist.
tilelist_file Path to the tilelist input file.
optional arguments:
-h, --help show this help message and exit
......
......@@ -25,8 +25,8 @@ class Tile:
class TileSet:
def __init__(self, tilelistPath, minzoom, maxzoom):
self.tilelistPath = tilelistPath
def __init__(self, tilelistFile, minzoom, maxzoom):
self.tilelistFile = tilelistFile
self.tileset = set()
self.maxzoom = maxzoom
self.minzoom = minzoom
......@@ -57,9 +57,8 @@ class TileSet:
def read(self):
"""Read tilelist as input"""
with open(self.tilelistPath, "r") as f:
for line in f.readlines():
tile = self.parse(line)
self.addParent(tile.parent)
self.addChildren(tile.children)
self.tileset.add(tile)
for line in self.tilelistFile.readlines():
tile = self.parse(line)
self.addParent(tile.parent)
self.addChildren(tile.children)
self.tileset.add(tile)
import argparse
import sys
from tileset import TileSet
......@@ -10,20 +11,24 @@ def main():
"recursively up to a zoom level."
)
parser.add_argument(
"tilelist_path", help="Path to the tilelist input file.", type=str
"minzoom",
default=0,
help="The minimum zoom level of the generated tilelist.",
type=int,
)
parser.add_argument(
"maxzoom", help="The maximum zoom level of the generated tilelist.", type=int
)
parser.add_argument(
"minzoom",
default=0,
help="The minimum zoom level of the generated tilelist.",
type=int,
"tilelist_file",
help="Path to the tilelist input file.",
type=argparse.FileType("r"),
default=sys.stdin,
nargs="?",
)
args = parser.parse_args()
ts = TileSet(args.tilelist_path, args.minzoom, args.maxzoom)
ts = TileSet(args.tilelist_file, args.minzoom, args.maxzoom)
ts.read()
for tile in ts.tileset:
......
from argparse import Namespace
from io import StringIO
from unittest import TestCase, mock
from tileset.cli import main
......@@ -10,11 +11,15 @@ class CliTest(TestCase):
return_value=Namespace(tilelist_path="/path/to/tilelist", minzoom=0, maxzoom=1),
)
def test_main(self, mock_parse_args):
test_input = ["0/0/0", "1/0/0"]
test_input_file = "\n".join(test_input)
mock_open = mock.mock_open(read_data=test_input_file)
test_input = "\n".join(["0/0/0", "1/0/0"])
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
with mock.patch("builtins.open", mock_open):
with mock.patch("tileset.cli.argparse.ArgumentParser.parse_args") as parse_args:
parse_args.return_value = Namespace(
tilelist_file=test_input_file, minzoom=0, maxzoom=1
)
with mock.patch("builtins.print") as mock_print:
main()
call_list = [
......
from io import StringIO
from unittest import TestCase, mock
from tileset import Tile, TileSet
......@@ -21,12 +22,13 @@ class TestTileSet(TestCase):
@mock.patch("tileset.TileSet.addParent")
def test_tileset_read(self, mock_addParent, mock_addChildren):
test_input = ["15/5/5", "15/12/12"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 15)
tileset.read()
tileset = TileSet(test_input_file, 0, 15)
tileset.read()
parent_list = [mock.call(Tile(14, 2, 2)), mock.call(Tile(14, 6, 6))]
mock_addParent.assert_has_calls(parent_list)
......@@ -62,90 +64,99 @@ class TestTileSet(TestCase):
def test_tileset_add_single_minzoom(self):
test_input = ["0/0/0"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 1)
tileset.read()
tileset = TileSet(test_input_file, 0, 1)
tileset.read()
self.assertEqual(len(tileset.tileset), 5)
def test_tileset_add_multiple_minzoom(self):
test_input = ["0/0/0", "0/1/1"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 1)
tileset.read()
tileset = TileSet(test_input_file, 0, 1)
tileset.read()
self.assertEqual(len(tileset.tileset), 10)
def test_tileset_add_multiple_overlapping_minzoom(self):
test_input = ["0/0/0", "1/0/0"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 1)
tileset.read()
tileset = TileSet(test_input_file, 0, 1)
tileset.read()
self.assertEqual(len(tileset.tileset), 5)
def test_tileset_add_single_z_between_maxzoom(self):
test_input = ["2/0/0"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
tileset = TileSet(test_input_file, 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 7)
def test_tileset_add_multiple_z_between_maxzoom(self):
test_input = ["2/0/0", "2/10/10"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
tileset = TileSet(test_input_file, 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 14)
def test_tileset_add_multiple_overlapping_z_between_maxzoom(self):
test_input = ["2/0/0", "3/0/0"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
tileset = TileSet(test_input_file, 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 7)
def test_tileset_add_single_maxzoom(self):
test_input = ["3/0/0"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
tileset = TileSet(test_input_file, 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 4)
def test_tileset_add_multiple_maxzoom(self):
test_input = ["3/0/0", "3/10/10"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
tileset = TileSet(test_input_file, 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 8)
def test_tileset_add_multiple_overlapping_maxzoom(self):
test_input = ["3/0/0", "3/1/1"]
test_input_file = "\n".join(test_input)
test_input = "\n".join(test_input)
test_input_file = StringIO()
test_input_file.write(test_input)
test_input_file.seek(0)
mock_open = mock.mock_open(read_data=test_input_file)
with mock.patch("builtins.open", mock_open):
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
tileset = TileSet(test_input_file, 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 5)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment