Skip to content

Commit

Permalink
Merge pull request #15 from tilezen/linter
Browse files Browse the repository at this point in the history
Add linters
  • Loading branch information
peitili authored Dec 15, 2021
2 parents 0465a5e + 7f93b48 commit f9197a3
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 54 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: pre-commit

on:
pull_request:
push:
branches: [master]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: pre-commit/[email protected]
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-docstring-first
- id: check-yaml
- id: debug-statements
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-json
- id: end-of-file-fixer
- id: trailing-whitespace
- id: requirements-txt-fixer
- id: double-quote-string-fixer
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.6.0
hooks:
- id: reorder-python-imports
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.5.7
hooks:
- id: autopep8
- repo: https://github.com/asottile/yesqa
rev: v1.3.0
hooks:
- id: yesqa
- repo: https://gitlab.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
args: [--max-line-length=120]
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)

Scoville - A tool to attribute size information in MVT tiles.

Current scoville commands:
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
click
msgpack
Pillow
requests
requests_futures
squarify
msgpack
Pillow
19 changes: 11 additions & 8 deletions scoville/command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import click
import json

import click

from scoville.mvt import Tile


Expand Down Expand Up @@ -106,7 +108,7 @@ def _info(mvt_file, kind, d3_json):
if res.status_code == 200:
tile = Tile(res.content)
else:
click.echo("Failed to fetch tile, status was %r" %
click.echo('Failed to fetch tile, status was %r' %
(res.status_code,))
return

Expand Down Expand Up @@ -156,16 +158,16 @@ def compare(tiles_file, url1, url2, kind):
tile_urls1 = read_urls(tiles_file, url1)
tile_urls2 = read_urls(tiles_file, url2)
for tile, url1, url2 in zip(tiles, tile_urls1, tile_urls2):
print("===BEGIN===>%s" % tile)
print('===BEGIN===>%s' % tile)

print("url1: --->%s" % url1)
print('url1: --->%s' % url1)
_info(url1, kind, None)
print()

print("url2: --->%s" % url2)
print('url2: --->%s' % url2)
_info(url1, kind, None)

print("<===END===%s\n\n" % tile)
print('<===END===%s\n\n' % tile)


@cli.command()
Expand Down Expand Up @@ -346,9 +348,10 @@ def outliers(tiles_file, url, cache, nprocs, num_outliers_per_layer):
result = calculate_outliers(tiles, num_outliers_per_layer, cache, nprocs)

for name in sorted(result.keys()):
click.secho("Layer %r" % name, fg='green', bold=True)
click.secho('Layer %r' % name, fg='green', bold=True)
for size, features_size, properties_size, url in sorted(result[name]):
click.echo("t:%8d f:%8d p:%8d %s" % (size, features_size, properties_size, url))
click.echo('t:%8d f:%8d p:%8d %s' %
(size, features_size, properties_size, url))


def scoville_main():
Expand Down
23 changes: 13 additions & 10 deletions scoville/mvt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from scoville.pbf import Message, WireType
from enum import Enum, IntEnum
from enum import Enum
from enum import IntEnum

from scoville.pbf import Message
from scoville.pbf import WireType


class GeomType(Enum):
Expand Down Expand Up @@ -67,15 +70,15 @@ def _decode_value(data):
value = field.as_int32() != 0

else:
raise ValueError("Unexpected tag %d while decoding value"
raise ValueError('Unexpected tag %d while decoding value'
% (field.tag,))

# the MVT spec says that there should be one and only one field in the
# Value message, so check for that.
if value is None:
raise ValueError("Found no fields when decoding value")
raise ValueError('Found no fields when decoding value')
if count > 1:
raise ValueError("Found multiple fields when decoding value")
raise ValueError('Found multiple fields when decoding value')

return value

Expand Down Expand Up @@ -134,7 +137,7 @@ def __unpack(self):
self._geom_cmds_size += field.size

else:
raise ValueError("Unknown Feature tag %d" % field.tag)
raise ValueError('Unknown Feature tag %d' % field.tag)

self.unpacked = True

Expand Down Expand Up @@ -239,10 +242,10 @@ def __init__(self, field):
self.extent = field.as_uint32()

else:
raise ValueError("Unknown Layer tag %d" % field.tag)
raise ValueError('Unknown Layer tag %d' % field.tag)

if self.name is None:
raise ValueError("Layer missing name, but name is required")
raise ValueError('Layer missing name, but name is required')

def __iter__(self):
return iter(self.features)
Expand All @@ -261,7 +264,7 @@ def __next__(self):

if field.tag != Tile.Tags.LAYER:
raise ValueError(
"Expecting layer with tag %d, got tag %d instead."
'Expecting layer with tag %d, got tag %d instead.'
% (Tile.Tags.LAYER, field.tag))

return Layer(field)
Expand Down Expand Up @@ -289,7 +292,7 @@ class Tile(object):
class Tags(IntEnum):
LAYER = 3

def __init__(self, data, name=""):
def __init__(self, data, name=''):
self.data = data
self.name = name

Expand Down
8 changes: 4 additions & 4 deletions scoville/pbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def as_memoryview(self):

def as_packed(self, wire_type):
if wire_type is WireType.length_delimited:
raise ValueError("Length delimited wire types cannot be packed.")
raise ValueError('Length delimited wire types cannot be packed.')

return _LengthDelimited._iter(
self.buf, self.tag, _WIRE_TYPES[wire_type])
Expand Down Expand Up @@ -144,16 +144,16 @@ def varint(self):

def get_bytes(self, num_bytes):
if self.pos + num_bytes > self.end:
raise EOFError("Unexpected end of PBF data, attempting to read "
"%d bytes from position %d goes past end at %d"
raise EOFError('Unexpected end of PBF data, attempting to read '
'%d bytes from position %d goes past end at %d'
% (num_bytes, self.pos, self.end))
m = self.buf[self.pos:self.pos+num_bytes]
self.pos += num_bytes
return m

def get_byte(self):
if self.pos == self.end:
raise EOFError("Unexpected end of PBF data at byte %d" % self.pos)
raise EOFError('Unexpected end of PBF data at byte %d' % self.pos)

v = self.buf[self.pos]
self.pos += 1
Expand Down
18 changes: 12 additions & 6 deletions scoville/percentiles.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import requests
from collections import defaultdict

import requests

from scoville.mvt import Tile


Expand All @@ -12,7 +14,7 @@ def _fetch_http(url):

# TODO: retry? better error handling!
if res.status_code != requests.codes.ok:
print("Got tile response %d for %s" % (res.status_code, url))
print('Got tile response %d for %s' % (res.status_code, url))
return None

return res.content
Expand Down Expand Up @@ -137,7 +139,8 @@ def add(self, tile_url):
return
tile = Tile(data)
for layer in tile:
self._insert(layer.name, layer.size, layer.features_size, layer.properties_size, tile_url)
self._insert(layer.name, layer.size, layer.features_size,
layer.properties_size, tile_url)

def encode(self):
from msgpack import packb
Expand Down Expand Up @@ -191,7 +194,8 @@ def parallel(tile_urls, factory, nprocs):

workers = []
for i in range(0, nprocs):
w = Process(target=worker, args=(input_queue, output_queue, factory.create()))
w = Process(target=worker, args=(
input_queue, output_queue, factory.create()))
w.start()
workers.append(w)

Expand Down Expand Up @@ -248,7 +252,8 @@ def factory_fn():
return Aggregator(cache)

if nprocs > 1:
results = parallel(tile_urls, FactoryFunctionHolder(factory_fn), nprocs)
results = parallel(
tile_urls, FactoryFunctionHolder(factory_fn), nprocs)
else:
results = sequential(tile_urls, factory_fn)

Expand Down Expand Up @@ -283,7 +288,8 @@ def factory_fn():
return LargestN(num_outliers, cache)

if nprocs > 1:
results = parallel(tile_urls, FactoryFunctionHolder(factory_fn), nprocs)
results = parallel(
tile_urls, FactoryFunctionHolder(factory_fn), nprocs)
else:
results = sequential(tile_urls, factory_fn)

Expand Down
Loading

0 comments on commit f9197a3

Please sign in to comment.