Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace black/isort/flake8 with ruff #79

Merged
merged 9 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .commitlintrc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export default {
extends: [
'@commitlint/config-conventional'
],
ignores: [
(message) => message.includes('Signed-off-by: dependabot[bot]')
],
rules: {
'header-max-length': [
2,
'always',
72
],
'body-max-line-length': [
2,
'always',
72
],
'body-leading-blank': [
2,
'always'
],
'type-enum': [
2,
'always',
[
'build',
'chore',
'ci',
'deps',
'docs',
'feat',
'fix',
'perf',
'refactor',
'revert',
'style',
'test'
]
]
}
};
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,20 @@ jobs:
- name: Upload Coverage to Codecov
if: ${{ matrix.python_version == env.DEFAULT_PYTHON }}
uses: codecov/codecov-action@v4

commitlint:
name: Commitlint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check commitlint
uses: wagoid/commitlint-github-action@0d749a1a91d4770e983a7b8f83d4a3f0e7e0874e # v5.4.4

ruff:
name: Coding style - ruff
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
- run: ruff check --fix
- run: ruff format
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ __pycache__
django_munigeo.egg-info/
/build
/dist
.idea
.idea
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
default_language_version:
python: python3
default_install_hook_types: [pre-commit, commit-msg]
default_stages: [pre-commit, manual]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-added-large-files
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4 # Sync with requirements.txt
hooks:
- id: ruff
name: ruff lint
- id: ruff-format
name: ruff format
args: [ --check ]
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v9.13.0
hooks:
- id: commitlint
stages: [commit-msg, manual]
additional_dependencies: ["@commitlint/config-conventional"]
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Pinned the `django-parler` version to `>=2` and add a migration required to upgrade it.

### Fixed
- Add a `tzinfo` to `Street` and `Address.modified_at` migrations to fix the warning
- Add a `tzinfo` to `Street` and `Address.modified_at` migrations to fix the warning
saying that a timezone-naive date was passed to a `DateTimeField`.
- helsinki importer: Reverted the change introduced in v0.3.6 which broke Helsinki division import
- helsinki importer: Fixed empty field value handling
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ then
```
python manage.py geo_import helsinki --divisions
```

## Code format

This project uses [Ruff](https://docs.astral.sh/ruff/) for code formatting and quality checking.

Basic `ruff` commands:

* lint: `ruff check`
* apply safe lint fixes: `ruff check --fix`
* check formatting: `ruff format --check`
* format: `ruff format`

[`pre-commit`](https://pre-commit.com/) can be used to install and
run all the formatting tools as git hooks automatically before a
commit.
6 changes: 2 additions & 4 deletions munigeo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ def translated_fields_to_representation(self, obj, ret):
translated_fields = {}

for lang_key, trans_dict in ret.pop("translations", {}).items():

for field_name, translation in trans_dict.items():
if not field_name in translated_fields:
if field_name not in translated_fields:
translated_fields[field_name] = {lang_key: translation}
else:
translated_fields[field_name].update({lang_key: translation})
Expand Down Expand Up @@ -166,7 +165,6 @@ def geom_to_json(geom, target_srs):


class GeoModelSerializer(serializers.ModelSerializer):

def __init__(self, *args, **kwargs):
super(GeoModelSerializer, self).__init__(*args, **kwargs)
model = self.Meta.model
Expand Down Expand Up @@ -238,7 +236,7 @@ class AdministrativeDivisionSerializer(
):
def to_representation(self, obj):
ret = super(AdministrativeDivisionSerializer, self).to_representation(obj)
if not "request" in self.context:
if "request" not in self.context:
return ret
qparams = self.context["request"].query_params
if qparams.get("geometry", "").lower() in ("true", "1"):
Expand Down
15 changes: 3 additions & 12 deletions munigeo/importer/athens.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
# -*- coding: utf-8 -*-
import csv
import io
import json
import os

# import unicodecsv
import requests
import requests_cache
from django import db
from django.conf import settings
from django.contrib.gis.gdal import CoordTransform, DataSource, SpatialReference
from django.contrib.gis.geos import GEOSGeometry, MultiPolygon, Point
from django.contrib.gis.geos import Point

from munigeo import ocd
from munigeo.importer.base import Importer, register_importer
from munigeo.importer.sync import ModelSyncher
from munigeo.models import *
from munigeo.models import PROJECTION_SRID, Municipality

CITADEL_LIST = [
{
Expand Down Expand Up @@ -85,7 +76,7 @@ def __init__(self, *args, **kwargs):
self.muni_data_path = os.path.join(self.data_path, "gr", "athens")

def import_municipalities(self):
muni, c = Municipality.objects.get_or_create(id=30001, name="Athens")
Municipality.objects.get_or_create(id=30001, name="Athens")
self.logger.info("Athens municipality added.")

def import_pois_from_citadel(self):
Expand Down
6 changes: 2 additions & 4 deletions munigeo/importer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

import requests
from django.conf import settings
from django.contrib.gis.gdal import CoordTransform, DataSource, SpatialReference
from django.contrib.gis.geos import GEOSGeometry, MultiPolygon, Point
from django.contrib.gis.geos import Point
from django.utils.text import slugify

from munigeo.importer.sync import ModelSyncher
from munigeo.models import *
from munigeo.models import POI, PROJECTION_SRID, POICategory


def convert_from_wgs84(coords):
Expand Down
4 changes: 2 additions & 2 deletions munigeo/importer/finland.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
from munigeo.importer.base import Importer, register_importer
from munigeo.importer.sync import ModelSyncher
from munigeo.models import (
PROJECTION_SRID,
AdministrativeDivision,
AdministrativeDivisionGeometry,
AdministrativeDivisionType,
Municipality,
PROJECTION_SRID,
)

from .helsinki import FIN_GRID, TM35_SRID
Expand Down Expand Up @@ -176,7 +176,7 @@ def import_municipalities(self):
self._process_muni(syncher, feat)
if executor:
for f in futures:
res = f.result()
_ = f.result()
executor.shutdown()

AdministrativeDivision.objects.rebuild()
42 changes: 23 additions & 19 deletions munigeo/importer/helsinki.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@
from munigeo import ocd
from munigeo.importer.base import Importer, register_importer
from munigeo.importer.sync import ModelSyncher
from munigeo.models import *
from munigeo.models import (
POI,
PROJECTION_SRID,
Address,
AdministrativeDivision,
AdministrativeDivisionGeometry,
AdministrativeDivisionType,
Municipality,
Plan,
POICategory,
Street,
)

MUNI_URL = "https://tilastokeskus.fi/meta/luokitukset/kunta/001-2013/tekstitiedosto.txt"

Expand Down Expand Up @@ -216,7 +227,7 @@ def make_div_id(obj):
return obj.origin_id

self.logger.info(div["name"])
if not "origin_id" in div["fields"]:
if "origin_id" not in div["fields"]:
raise Exception(
"Field 'origin_id' not defined in config section '%s'" % div["name"]
)
Expand Down Expand Up @@ -329,16 +340,14 @@ def import_plans(self):
self._import_plans("Lv_rajaus.TAB", True)
self._import_plans("Kaava_vir_rajaus.TAB", False)
self.logger.info("Saving")
for key, obj in self.plan_map.items():
for obj in self.plan_map.values():
if obj.found:
obj.save()
else:
self.logger.info("Plan %s deleted" % obj.name)

@db.transaction.atomic
def import_addresses(self):
none_to_str = lambda s: s or ""

wfs_url = (
"https://kartta.hel.fi/ws/geoserver/avoindata/wfs?"
"SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&"
Expand Down Expand Up @@ -384,16 +393,15 @@ def make_addr_id(num, num_end, letter):
street.addrs[make_addr_id(a.number, a.number_end, a.letter)] = a

bulk_addr_list = []
bulk_street_list = []
count = 0

self.logger.info("starting data synchronization")
for feat in lyr:
count += 1
if count % 1000 == 0:
self.logger.debug("{} processed".format(count))
street_name = none_to_str(feat.get("katunimi")).strip()
street_name_sv = none_to_str(feat.get("gatan")).strip()
street_name = (feat.get("katunimi") or "").strip()
street_name_sv = (feat.get("gatan") or "").strip()

num = feat.get("osoitenumero")

Expand All @@ -413,10 +421,8 @@ def make_addr_id(num, num_end, letter):
)
continue

num2 = none_to_str(feat.get("osoitenumero2"))
if num2 == 0:
num2 = ""
letter = none_to_str(feat.get("osoitekirjain")).strip()
num2 = feat.get("osoitenumero2") or ""
letter = (feat.get("osoitekirjain") or "").strip()

coord_n = int(feat.get("n"))
coord_e = int(feat.get("e"))
Expand Down Expand Up @@ -475,8 +481,6 @@ def make_addr_id(num, num_end, letter):
# addr.save()
addr._found = True

# self.logger.info("%s: %s %d%s N%d E%d (%f,%f)" % (muni_name, street, num, letter, coord_n, coord_e, pnt.y, pnt.x))

if len(bulk_addr_list) >= 10000:
self.logger.info("Saving %d new addresses" % len(bulk_addr_list))

Expand Down Expand Up @@ -505,7 +509,7 @@ def make_addr_id(num, num_end, letter):
self.logger.info("synchronization complete")

def import_pois(self):
URL_BASE = "https://www.hel.fi/palvelukarttaws/rest/v2/unit/?service=%d"
url_base = "https://www.hel.fi/palvelukarttaws/rest/v2/unit/?service=%d"

muni_dict = {}
for muni in Municipality.objects.all():
Expand All @@ -518,7 +522,7 @@ def import_pois(self):
)

self.logger.info("Importing %s" % cat_type)
ret = requests.get(URL_BASE % srv_id)
ret = requests.get(url_base % srv_id)
for srv_info in ret.json():
srv_id = str(srv_info["id"])
try:
Expand All @@ -527,12 +531,12 @@ def import_pois(self):
poi = POI(origin_id=srv_id)
poi.name = srv_info["name_fi"]
poi.category = cat
if not "address_city_fi" in srv_info:
if "address_city_fi" not in srv_info:
self.logger.info("No city!")
self.logger.info(srv_info)
continue
city_name = srv_info["address_city_fi"]
if not city_name in muni_dict:
if city_name not in muni_dict:
city_name = city_name.encode("utf8")
post_code = srv_info.get("address_zip", "")
if post_code.startswith("00"):
Expand Down Expand Up @@ -564,7 +568,7 @@ def import_pois(self):
poi.municipality = muni_dict[city_name]
poi.street_address = srv_info.get("street_address_fi", None)
poi.zip_code = srv_info.get("address_zip", None)
if not "northing_etrs_gk25" in srv_info:
if "northing_etrs_gk25" not in srv_info:
self.logger.info("No location!")
self.logger.info(srv_info)
continue
Expand Down
Loading
Loading