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 ...@@ -5,13 +5,14 @@ Given an input file with a list of line seperated tiles in `{zoom}/{x}/{y}` form
## Usage ## 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. 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: positional arguments:
tilelist_path Path to the tilelist input file. minzoom The minimum zoom level of the generated tilelist.
maxzoom The maxzoom of the generated tilelist. maxzoom The maximum zoom level of the generated tilelist.
tilelist_file Path to the tilelist input file.
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
......
...@@ -25,8 +25,8 @@ class Tile: ...@@ -25,8 +25,8 @@ class Tile:
class TileSet: class TileSet:
def __init__(self, tilelistPath, minzoom, maxzoom): def __init__(self, tilelistFile, minzoom, maxzoom):
self.tilelistPath = tilelistPath self.tilelistFile = tilelistFile
self.tileset = set() self.tileset = set()
self.maxzoom = maxzoom self.maxzoom = maxzoom
self.minzoom = minzoom self.minzoom = minzoom
...@@ -57,9 +57,8 @@ class TileSet: ...@@ -57,9 +57,8 @@ class TileSet:
def read(self): def read(self):
"""Read tilelist as input""" """Read tilelist as input"""
with open(self.tilelistPath, "r") as f: for line in self.tilelistFile.readlines():
for line in f.readlines(): tile = self.parse(line)
tile = self.parse(line) self.addParent(tile.parent)
self.addParent(tile.parent) self.addChildren(tile.children)
self.addChildren(tile.children) self.tileset.add(tile)
self.tileset.add(tile)
import argparse import argparse
import sys
from tileset import TileSet from tileset import TileSet
...@@ -10,20 +11,24 @@ def main(): ...@@ -10,20 +11,24 @@ def main():
"recursively up to a zoom level." "recursively up to a zoom level."
) )
parser.add_argument( 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( parser.add_argument(
"maxzoom", help="The maximum zoom level of the generated tilelist.", type=int "maxzoom", help="The maximum zoom level of the generated tilelist.", type=int
) )
parser.add_argument( parser.add_argument(
"minzoom", "tilelist_file",
default=0, help="Path to the tilelist input file.",
help="The minimum zoom level of the generated tilelist.", type=argparse.FileType("r"),
type=int, default=sys.stdin,
nargs="?",
) )
args = parser.parse_args() args = parser.parse_args()
ts = TileSet(args.tilelist_path, args.minzoom, args.maxzoom) ts = TileSet(args.tilelist_file, args.minzoom, args.maxzoom)
ts.read() ts.read()
for tile in ts.tileset: for tile in ts.tileset:
......
from argparse import Namespace from argparse import Namespace
from io import StringIO
from unittest import TestCase, mock from unittest import TestCase, mock
from tileset.cli import main from tileset.cli import main
...@@ -10,11 +11,15 @@ class CliTest(TestCase): ...@@ -10,11 +11,15 @@ class CliTest(TestCase):
return_value=Namespace(tilelist_path="/path/to/tilelist", minzoom=0, maxzoom=1), return_value=Namespace(tilelist_path="/path/to/tilelist", minzoom=0, maxzoom=1),
) )
def test_main(self, mock_parse_args): def test_main(self, mock_parse_args):
test_input = ["0/0/0", "1/0/0"] test_input = "\n".join(["0/0/0", "1/0/0"])
test_input_file = "\n".join(test_input) test_input_file = StringIO()
mock_open = mock.mock_open(read_data=test_input_file) 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: with mock.patch("builtins.print") as mock_print:
main() main()
call_list = [ call_list = [
......
from io import StringIO
from unittest import TestCase, mock from unittest import TestCase, mock
from tileset import Tile, TileSet from tileset import Tile, TileSet
...@@ -21,12 +22,13 @@ class TestTileSet(TestCase): ...@@ -21,12 +22,13 @@ class TestTileSet(TestCase):
@mock.patch("tileset.TileSet.addParent") @mock.patch("tileset.TileSet.addParent")
def test_tileset_read(self, mock_addParent, mock_addChildren): def test_tileset_read(self, mock_addParent, mock_addChildren):
test_input = ["15/5/5", "15/12/12"] 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) tileset = TileSet(test_input_file, 0, 15)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 15)
tileset.read()
parent_list = [mock.call(Tile(14, 2, 2)), mock.call(Tile(14, 6, 6))] parent_list = [mock.call(Tile(14, 2, 2)), mock.call(Tile(14, 6, 6))]
mock_addParent.assert_has_calls(parent_list) mock_addParent.assert_has_calls(parent_list)
...@@ -62,90 +64,99 @@ class TestTileSet(TestCase): ...@@ -62,90 +64,99 @@ class TestTileSet(TestCase):
def test_tileset_add_single_minzoom(self): def test_tileset_add_single_minzoom(self):
test_input = ["0/0/0"] 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) tileset = TileSet(test_input_file, 0, 1)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 1)
tileset.read()
self.assertEqual(len(tileset.tileset), 5) self.assertEqual(len(tileset.tileset), 5)
def test_tileset_add_multiple_minzoom(self): def test_tileset_add_multiple_minzoom(self):
test_input = ["0/0/0", "0/1/1"] 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) tileset = TileSet(test_input_file, 0, 1)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 1)
tileset.read()
self.assertEqual(len(tileset.tileset), 10) self.assertEqual(len(tileset.tileset), 10)
def test_tileset_add_multiple_overlapping_minzoom(self): def test_tileset_add_multiple_overlapping_minzoom(self):
test_input = ["0/0/0", "1/0/0"] 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) tileset = TileSet(test_input_file, 0, 1)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 1)
tileset.read()
self.assertEqual(len(tileset.tileset), 5) self.assertEqual(len(tileset.tileset), 5)
def test_tileset_add_single_z_between_maxzoom(self): def test_tileset_add_single_z_between_maxzoom(self):
test_input = ["2/0/0"] 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) tileset = TileSet(test_input_file, 0, 3)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 7) self.assertEqual(len(tileset.tileset), 7)
def test_tileset_add_multiple_z_between_maxzoom(self): def test_tileset_add_multiple_z_between_maxzoom(self):
test_input = ["2/0/0", "2/10/10"] 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) tileset = TileSet(test_input_file, 0, 3)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 14) self.assertEqual(len(tileset.tileset), 14)
def test_tileset_add_multiple_overlapping_z_between_maxzoom(self): def test_tileset_add_multiple_overlapping_z_between_maxzoom(self):
test_input = ["2/0/0", "3/0/0"] 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) tileset = TileSet(test_input_file, 0, 3)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 7) self.assertEqual(len(tileset.tileset), 7)
def test_tileset_add_single_maxzoom(self): def test_tileset_add_single_maxzoom(self):
test_input = ["3/0/0"] 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) tileset = TileSet(test_input_file, 0, 3)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 4) self.assertEqual(len(tileset.tileset), 4)
def test_tileset_add_multiple_maxzoom(self): def test_tileset_add_multiple_maxzoom(self):
test_input = ["3/0/0", "3/10/10"] 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) tileset = TileSet(test_input_file, 0, 3)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 8) self.assertEqual(len(tileset.tileset), 8)
def test_tileset_add_multiple_overlapping_maxzoom(self): def test_tileset_add_multiple_overlapping_maxzoom(self):
test_input = ["3/0/0", "3/1/1"] 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) tileset = TileSet(test_input_file, 0, 3)
with mock.patch("builtins.open", mock_open): tileset.read()
tileset = TileSet("/path/to/tilelist", 0, 3)
tileset.read()
self.assertEqual(len(tileset.tileset), 5) 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