Skip to content

Commit 749032f

Browse files
committed
Updates based on flake8 and yapf.
1 parent 3f589ab commit 749032f

23 files changed

+424
-313
lines changed

.travis.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ install:
1111
- pip install -r requirements-dev.txt
1212

1313
script:
14-
- flake8 pystac
15-
- flake8 tests
16-
- yapf -dpr pystac
17-
- yapf -dpr tests
14+
- .travis/style_checks
1815
- mypy -m pystac
1916
- python -m unittest discover tests/
2017

.travis/style_checks

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
if [[ -n "${PYSTAC_DEBUG}" ]]; then
6+
set -x
7+
fi
8+
9+
SOURCE="${BASH_SOURCE[0]}"
10+
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
11+
TRAVIS_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
12+
SRC_DIR="$( cd -P "$( dirname "TRAVIS_DIR" )" && pwd )"
13+
14+
function usage() {
15+
echo -n \
16+
"Usage: $(basename "$0")
17+
Test that code is compliant with yapf (code formatter) and flake8.
18+
"
19+
}
20+
21+
22+
if [ "${1:-}" = "--help" ]; then
23+
usage
24+
else
25+
echo $SRC_DIR
26+
27+
echo "Checking that library code is consistent with flake8..."
28+
flake8 "$SRC_DIR/pystac"
29+
30+
echo "Checking that test code is consistent with flake8..."
31+
flake8 "$SRC_DIR/tests"
32+
33+
# Exit code of 1 if yapf has a non-empty diff
34+
# (ie. scripts/format_code needs to be run)
35+
echo "Checking that code is consistent with yapf..."
36+
if !(yapf -dpr "$SRC_DIR/pystac" > /dev/null &&
37+
yapf -dpr "$SRC_DIR/tests" > /dev/null); then
38+
echo "Code has not been formatted by yapf."
39+
exit 1
40+
fi
41+
fi

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ Unit tests are in the `tests` folder. To run unit tests, use `unittest`:
5959

6060
### Code quality checks
6161

62-
PySTAC uses [flake8](), [yapf](), and [mypy]() for code formatting and type checking.
62+
PySTAC uses [flake8](http://flake8.pycqa.org/en/latest/), [yapf](https://github.com/google/yapf), and [mypy](http://mypy-lang.org/) for code formatting and type checking.
6363

64-
To run the style checks:
64+
To run the flake8 style checks:
6565

6666
```
6767
> flake8 pystac
@@ -75,6 +75,8 @@ To format code:
7575
> yapf -ipr tests
7676
```
7777

78+
You could also run the `.travis/style_checks` script to check flake8 and yapf.
79+
7880
To run the mypy static checker:
7981

8082
```

pystac/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66

77
from pystac.version import (__version__, STAC_VERSION)
88

9+
910
class STACError(Exception):
1011
pass
1112

13+
1214
from pystac.io import STAC_IO
1315
from pystac.stac_object import STACObject
1416
from pystac.link import (Link, LinkType)
1517
from pystac.catalog import (Catalog, CatalogType)
16-
from pystac.collection import (Collection, Extent, SpatialExtent, TemporalExtent, Provider)
18+
from pystac.collection import (Collection, Extent, SpatialExtent,
19+
TemporalExtent, Provider)
1720
from pystac.item import (Item, Asset)
1821
from pystac.eo import *
1922
from pystac.label import *
2023

24+
2125
def stac_object_from_dict(d):
2226
"""Determines how to deserialize a dictionary into a STAC object.
2327
@@ -33,4 +37,5 @@ def stac_object_from_dict(d):
3337
else:
3438
return Catalog.from_dict(d)
3539

40+
3641
STAC_IO.stac_object_from_dict = stac_object_from_dict

pystac/catalog.py

+31-26
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class Catalog(STACObject):
4242
4343
See: https://github.com/radiantearth/stac-spec/blob/v0.8.0/catalog/catalog-spec.md
4444
"""
45-
4645
"""Default file name that will be given to this STAC item in a cononical format."""
4746
DEFAULT_FILE_NAME = "catalog.json"
4847

@@ -63,8 +62,8 @@ def __repr__(self):
6362

6463
def set_root(self, root, link_type=LinkType.ABSOLUTE):
6564
STACObject.set_root(self, root, link_type)
66-
root._resolved_objects = ResolvedObjectCache.merge(root._resolved_objects,
67-
self._resolved_objects)
65+
root._resolved_objects = ResolvedObjectCache.merge(
66+
root._resolved_objects, self._resolved_objects)
6867

6968
def add_child(self, child, title=None):
7069
child.set_root(self.get_root())
@@ -180,16 +179,16 @@ def make_all_asset_hrefs_absolute(self):
180179
for item in items:
181180
item.make_asset_hrefs_absolute()
182181

183-
def normalize_and_save(self,
184-
root_href,
185-
catalog_type):
182+
def normalize_and_save(self, root_href, catalog_type):
186183
self.normalize_hrefs(root_href)
187184
self.save(catalog_type)
188185

189186
def normalize_hrefs(self, root_href):
190187
# Normalizing requires an absolute path
191188
if not is_absolute_href(root_href):
192-
root_href = make_absolute_href(root_href, os.getcwd(), start_is_dir=True)
189+
root_href = make_absolute_href(root_href,
190+
os.getcwd(),
191+
start_is_dir=True)
193192

194193
# Fully resolve the STAC to avoid linking issues.
195194
# This particularly can happen with unresolved links that have
@@ -230,23 +229,27 @@ def save(self, catalog_type):
230229
else:
231230
self.make_all_links_relative()
232231

233-
include_self_link = catalog_type in [CatalogType.ABSOLUTE_PUBLISHED,
234-
CatalogType.RELATIVE_PUBLISHED]
232+
include_self_link = catalog_type in [
233+
CatalogType.ABSOLUTE_PUBLISHED, CatalogType.RELATIVE_PUBLISHED
234+
]
235235

236236
if catalog_type == CatalogType.RELATIVE_PUBLISHED:
237237
child_catalog_type = CatalogType.SELF_CONTAINED
238238
else:
239239
child_catalog_type = catalog_type
240240

241-
items_include_self_link = catalog_type in [CatalogType.ABSOLUTE_PUBLISHED]
241+
items_include_self_link = catalog_type in [
242+
CatalogType.ABSOLUTE_PUBLISHED
243+
]
242244

243245
for child_link in self.get_child_links():
244246
if child_link.is_resolved():
245247
child_link.target.save(catalog_type=child_catalog_type)
246248

247249
for item_link in self.get_item_links():
248250
if item_link.is_resolved():
249-
item_link.target.save_object(include_self_link=items_include_self_link)
251+
item_link.target.save_object(
252+
include_self_link=items_include_self_link)
250253

251254
self.save_object(include_self_link=include_self_link)
252255

@@ -270,8 +273,9 @@ def map_items(self, item_mapper):
270273
"""Creates a copy of a catalog, with each item passed through the item_mapper function.
271274
272275
Args:
273-
item_mapper: A function that takes in an item, and returns either an item or list of items.
274-
The item that is passed into the item_mapper is a copy, so the method can mutate it safetly.
276+
item_mapper: A function that takes in an item, and returns either
277+
an item or list of items. The item that is passed into the item_mapper
278+
is a copy, so the method can mutate it safetly.
275279
"""
276280

277281
new_cat = self.full_copy()
@@ -290,9 +294,9 @@ def process_catalog(catalog):
290294
item_links.append(item_link)
291295
else:
292296
for i in mapped:
293-
l = item_link.clone()
294-
l.target = i
295-
item_links.append(l)
297+
new_link = item_link.clone()
298+
new_link.target = i
299+
item_links.append(new_link)
296300
catalog.clear_items()
297301
catalog.add_links(item_links)
298302

@@ -304,9 +308,10 @@ def map_assets(self, asset_mapper):
304308
through the asset_mapper function.
305309
306310
Args:
307-
asset_mapper: A function that takes in an key and an Asset, and returns
308-
either an Asset, a (key, Asset), or a dictionary of Assets with unique keys.
309-
The Asset that is passed into the item_mapper is a copy, so the method can mutate it safetly.
311+
asset_mapper: A function that takes in an key and an Asset, and returns
312+
either an Asset, a (key, Asset), or a dictionary of Assets with unique keys.
313+
The Asset that is passed into the item_mapper is a copy, so the method can
314+
mutate it safetly.
310315
"""
311316
def apply_asset_mapper(tup):
312317
k, v = tup
@@ -324,8 +329,10 @@ def apply_asset_mapper(tup):
324329
return assets
325330

326331
def item_mapper(item):
327-
new_assets = [x for result in map(apply_asset_mapper, item.assets.items())
328-
for x in result]
332+
new_assets = [
333+
x for result in map(apply_asset_mapper, item.assets.items())
334+
for x in result
335+
]
329336
item.assets = dict(new_assets)
330337
return item
331338

@@ -337,9 +344,9 @@ def describe(self, indent=0, include_hrefs=False):
337344
s += ' {}'.format(self.get_self_href())
338345
print(s)
339346
for child in self.get_children():
340-
child.describe(indent=indent+4, include_hrefs=include_hrefs)
347+
child.describe(indent=indent + 4, include_hrefs=include_hrefs)
341348
for item in self.get_items():
342-
s = '{}* {}'.format(' ' * (indent+2), item)
349+
s = '{}* {}'.format(' ' * (indent + 2), item)
343350
if include_hrefs:
344351
s += ' {}'.format(item.get_self_href())
345352
print(s)
@@ -350,9 +357,7 @@ def from_dict(d):
350357
description = d['description']
351358
title = d.get('title')
352359

353-
cat = Catalog(id=id,
354-
description=description,
355-
title=title)
360+
cat = Catalog(id=id, description=description, title=title)
356361

357362
for l in d['links']:
358363
if not l['rel'] == 'root':

pystac/collection.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import json
22
from datetime import datetime
3-
from datetime import timezone
43
import dateutil.parser
54
from copy import (copy, deepcopy)
6-
import json
75

8-
from pystac import STACError, STAC_IO
6+
from pystac import STACError
97
from pystac.catalog import Catalog
108
from pystac.link import (Link, LinkType)
119
from pystac.io import STAC_IO
1210
from pystac.utils import (make_absolute_href, is_absolute_href)
1311

12+
1413
class Collection(Catalog):
1514
DEFAULT_FILE_NAME = "collection.json"
1615

@@ -70,8 +69,8 @@ def clone(self):
7069
extent=self.extent.clone(),
7170
title=self.title,
7271
license=self.license,
73-
stac_extensions = self.stac_extensions,
74-
keywords = self.keywords,
72+
stac_extensions=self.stac_extensions,
73+
keywords=self.keywords,
7574
version=self.version,
7675
providers=self.providers,
7776
properties=self.properties,
@@ -118,8 +117,8 @@ def from_dict(d):
118117
# If a root link was included, we want to inheret
119118
# whether it was relative or not.
120119
if not is_absolute_href(l['href']):
121-
collection.get_single_link('root').link_type = LinkType.RELATIVE
122-
120+
collection.get_single_link(
121+
'root').link_type = LinkType.RELATIVE
123122

124123
return collection
125124

@@ -147,8 +146,7 @@ def to_dict(self):
147146
return deepcopy(d)
148147

149148
def clone(self):
150-
return Extent(spatial=copy(self.spatial),
151-
temporal=copy(self.temporal))
149+
return Extent(spatial=copy(self.spatial), temporal=copy(self.temporal))
152150

153151
@staticmethod
154152
def from_dict(d):
@@ -161,7 +159,7 @@ def __init__(self, bboxes):
161159
self.bboxes = bboxes
162160

163161
def to_dict(self):
164-
d = { 'bbox' : self.bboxes }
162+
d = {'bbox': self.bboxes}
165163
return deepcopy(d)
166164

167165
def clone(self):
@@ -176,7 +174,8 @@ def from_coordinates(coordinates):
176174
def process_coords(l, xmin=None, ymin=None, xmax=None, ymax=None):
177175
for coord in l:
178176
if type(coord[0]) is list:
179-
xmin, ymin, xmax, ymax = process_coords(coord, xmin, ymin, xmax, ymax)
177+
xmin, ymin, xmax, ymax = process_coords(
178+
coord, xmin, ymin, xmax, ymax)
180179
else:
181180
x, y = coord
182181
if xmin is None or x < xmin:
@@ -210,10 +209,12 @@ def to_dict(self):
210209
end = None
211210

212211
if i[0]:
213-
start = '{}Z'.format(i[0].replace(microsecond=0, tzinfo=None).isoformat())
212+
start = '{}Z'.format(i[0].replace(microsecond=0,
213+
tzinfo=None).isoformat())
214214

215215
if i[1]:
216-
end = '{}Z'.format(i[1].replace(microsecond=0, tzinfo=None).isoformat())
216+
end = '{}Z'.format(i[1].replace(microsecond=0,
217+
tzinfo=None).isoformat())
217218

218219
encoded_intervals.append([start, end])
219220

@@ -241,7 +242,8 @@ def from_dict(d):
241242

242243
@staticmethod
243244
def from_now():
244-
return TemporalExtent(intervals=[[datetime.utcnow().replace(microsecond=0), None]])
245+
return TemporalExtent(
246+
intervals=[[datetime.utcnow().replace(microsecond=0), None]])
245247

246248

247249
class Provider:
@@ -252,7 +254,7 @@ def __init__(self, name, description=None, roles=None, url=None):
252254
self.url = url
253255

254256
def to_dict(self):
255-
d = { 'name': self.name }
257+
d = {'name': self.name}
256258
if self.description is not None:
257259
d['description'] = self.description
258260
if self.roles is not None:

0 commit comments

Comments
 (0)