Skip to content

Commit

Permalink
CI - Switch from Flake8 to Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed May 3, 2024
1 parent 413ab86 commit d625d92
Show file tree
Hide file tree
Showing 22 changed files with 289 additions and 239 deletions.
17 changes: 5 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ on:

jobs:

flake8:
linter:
runs-on: ubuntu-latest
steps:

Expand All @@ -31,18 +31,11 @@ jobs:
- name: Checkout
uses: actions/[email protected]

- uses: actions/[email protected]
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements/dev.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install Python requirements
run: pip install -r requirements/dev.txt
run: make install-tests

- name: Run flake8
run: flake8
- name: Run ruff
run: make lint

tests:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -86,7 +79,7 @@ jobs:
./run-tests.sh
release:
needs: [tests, flake8]
needs: [tests, linter]

runs-on: ubuntu-latest
if: github.repository_owner == '3liz' && contains(github.ref, 'refs/tags/')
Expand Down
8 changes: 4 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ qgis-server:
tags:
- infrav3

flake:
linter:
image: ${REGISTRY_URL}/factory-ci-runner:qgis-ltr
stage: test
before_script:
- pip3 install --user -r requirements/dev.txt
script:
- flake8
- source ~/.bashrc
- make install-tests lint FLAVOR=$QGIS_FLAVOR
tags:
- factory

Expand Down
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ LOCAL_HOME ?= $(shell pwd)

SRCDIR=$(shell realpath .)

PYTHON_PKG=lizmap_server
TESTDIR=test

tests:
@mkdir -p $$(pwd)/.local $(LOCAL_HOME)/.cache
@echo Do not forget to run docker pull $(QGIS_IMAGE) from time to time
Expand All @@ -30,3 +33,21 @@ tests:
-e QGIS_SERVER_LIZMAP_REVEAL_SETTINGS=TRUE \
-e PYTEST_ADDOPTS="$(TEST_OPTS) --assert=plain" \
$(QGIS_IMAGE) ./run-tests.sh

.PHONY: test

install-tests:
pip install -U --upgrade-strategy=eager -r requirements/dev.txt

export QGIS_SERVER_LIZMAP_REVEAL_SETTINGS=TRUE
test:
cd test && pytest -v --qgis-plugins=..

lint:
@ruff check $(PYTHON_PKG) $(TESTDIR)

lint-preview:
@ruff check --preview $(PYTHON_PKG) $(TESTDIR)

lint-fix:
@ruff check --fix --preview $(PYTHON_PKG) $(TESTDIR)
4 changes: 2 additions & 2 deletions lizmap_server/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def is_editing_context(handler: QgsRequestHandler) -> bool:
return False


def server_feature_id_expression(feature_id, data_provider: QgsVectorDataProvider) -> str:
def server_feature_id_expression(feature_id: str, data_provider: QgsVectorDataProvider) -> str:
""" Fetch the QGIS server feature ID expression according to the current QGIS version. """
if Qgis.QGIS_VERSION_INT >= 32400:
from qgis.server import QgsServerFeatureId
Expand All @@ -344,7 +344,7 @@ def server_feature_id_expression(feature_id, data_provider: QgsVectorDataProvide
return _server_feature_id_expression(feature_id, data_provider.pkAttributeIndexes(), data_provider.fields())


def _server_feature_id_expression(feature_id, pk_attributes: list, fields: QgsFields) -> str:
def _server_feature_id_expression(feature_id: str, pk_attributes: list, fields: QgsFields) -> str:
""" Port of QgsServerFeatureId::getExpressionFromServerFid for QGIS < 3.24
The value "@@" is hardcoded in the CPP file.
Expand Down
8 changes: 4 additions & 4 deletions lizmap_server/expression_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def evaluate(params: Dict[str, str], response: QgsServerResponse, project: QgsPr
'status': 'success',
'results': [],
'errors': [],
'features': 0
'features': 0,
}

# without features just evaluate expression with layer context
Expand Down Expand Up @@ -421,7 +421,7 @@ def replace_expression_text(
'status': 'success',
'results': [],
'errors': [],
'features': 0
'features': 0,
}

# without features just replace expression string with layer context
Expand Down Expand Up @@ -536,8 +536,8 @@ def replace_expression_text(
response.write(
',\n'.join([
'{"type": "FeatureCollection"',
'"features": [' + ',\n'.join(body['results']) + ']}'
])
'"features": [' + ',\n'.join(body['results']) + ']}',
]),
)
else:
write_json_response(body, response)
Expand Down
10 changes: 5 additions & 5 deletions lizmap_server/filter_by_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def is_valid(self) -> bool:

return True

def sql_query(self, uri: QgsDataSourceUri, sql) -> Tuple[Tuple]:
def sql_query(self, uri: QgsDataSourceUri, sql: str) -> Tuple[Tuple]:
""" For a given URI, execute an SQL query and return the result. """
if self.connection is None:
# noinspection PyArgumentList
Expand Down Expand Up @@ -205,7 +205,7 @@ def subset_sql(self, groups_or_user: tuple) -> Tuple[str, str]:

ewkt = "SRID={crs};{wkt}".format(
crs=self.polygon.crs().postgisSrid(),
wkt=polygon.asWkt(6 if self.polygon.crs().isGeographic() else 2)
wkt=polygon.asWkt(6 if self.polygon.crs().isGeographic() else 2),
)

use_st_intersect = False if self.spatial_relationship == 'contains' else True
Expand Down Expand Up @@ -266,7 +266,7 @@ def _polygon_for_groups_with_qgis_api(self, groups_or_user: tuple) -> QgsGeometr
)
)""".format(
polygon_field=self.group_field,
groups_or_user=','.join(groups_or_user)
groups_or_user=','.join(groups_or_user),
)

# Create request
Expand Down Expand Up @@ -492,13 +492,13 @@ def _format_qgis_expression_relationship(
:returns: The QGIS expression
"""
geom = "geom_from_wkt('{wkt}')".format(
wkt=polygons.asWkt(6 if filtering_crs.isGeographic() else 2)
wkt=polygons.asWkt(6 if filtering_crs.isGeographic() else 2),
)
if filtering_crs != filtered_crs:
geom = "transform({geom}, '{from_crs}', '{to_crs}')".format(
geom=geom,
from_crs=filtering_crs.authid(),
to_crs=filtered_crs.authid()
to_crs=filtered_crs.authid(),
)

if use_centroid:
Expand Down
12 changes: 7 additions & 5 deletions lizmap_server/get_feature_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
QgsFeature,
QgsFeatureRequest,
QgsProject,
QgsRelationManager,
)
from qgis.server import QgsServerFilter, QgsServerProjectUtils

Expand Down Expand Up @@ -78,7 +79,8 @@ def append_maptip(cls, string: str, layer_name: str, feature_id: Union[str, int]
return xml_string.strip()

@classmethod
def feature_list_to_replace(cls, cfg: dict, project: QgsProject, relation_manager, xml) -> List[Result]:
def feature_list_to_replace(
cls, cfg: dict, project: QgsProject, relation_manager: QgsRelationManager, xml: str) -> List[Result]:
""" Parse the XML and check for each layer according to the Lizmap CFG file. """
features = []
for layer_name, feature_id in GetFeatureInfoFilter.parse_xml(xml):
Expand Down Expand Up @@ -191,13 +193,13 @@ def responseComplete(self):
# The user has clicked in a random area on the map or no interesting LAYERS,
# no features are returned.
logger.info(
"No features found in the XML from QGIS Server for project {}".format(project_path)
"No features found in the XML from QGIS Server for project {}".format(project_path),
)
return

logger.info(
"Replacing the maptip from QGIS by the drag and drop expression for {} features on {}".format(
len(features), ','.join([result.layer.name() for result in features]))
len(features), ','.join([result.layer.name() for result in features])),
)

# Let's evaluate each expression popup
Expand Down Expand Up @@ -231,7 +233,7 @@ def responseComplete(self):
logger.warning(
"The feature {} for layer {} is not valid, skip replacing this XML "
"GetFeatureInfo, continue to the next feature".format(
result.feature_id, result.layer.id())
result.feature_id, result.layer.id()),
)
continue

Expand All @@ -243,7 +245,7 @@ def responseComplete(self):
logger.warning(
"The GetFeatureInfo result for feature {} in layer {} is not valid, skip replacing "
"this XML GetFeatureInfo, , continue to the next feature".format(
result.feature_id, result.layer.id())
result.feature_id, result.layer.id()),
)
continue

Expand Down
8 changes: 4 additions & 4 deletions lizmap_server/get_legend_graphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def responseComplete(self):
'title': layer_name,
'icon': self.warning_icon(),
'valid': False,
}]
}],
}
handler.clearBody()
handler.appendBody(json.dumps(json_data).encode('utf8'))
Expand Down Expand Up @@ -183,7 +183,7 @@ def responseComplete(self):

@classmethod
def _extract_categories(
cls, layer: QgsVectorLayer, show_feature_count: bool = False, project_path: str = ""
cls, layer: QgsVectorLayer, show_feature_count: bool = False, project_path: str = "",
) -> dict:
""" Extract categories from the layer legend. """
# TODO Annotations QGIS 3.22 [str, Category]
Expand All @@ -209,14 +209,14 @@ def _extract_categories(
if not result:
Logger.warning(
f"The expression in the project '{project_path}', layer '{layer.name()}' has not "
f"been generated correctly, setting the expression to an empty string"
f"been generated correctly, setting the expression to an empty string",
)
expression = ''

if item.label() in categories.keys():
Logger.warning(
f"The label key '{item.label()}' is not unique, expect the legend to be broken in the project "
f"'{project_path}', layer '{layer.name()}'."
f"'{project_path}', layer '{layer.name()}'.",
)

categories[item.label()] = Category(
Expand Down
6 changes: 3 additions & 3 deletions lizmap_server/lizmap_accesscontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ def layerPermissions(self, layer: QgsMapLayer) -> QgsAccessControlFilter.LayerPe

# Check lizmap edition config
layer_id = layer.id()
if 'editionLayers' in cfg and cfg['editionLayers']:
if cfg.get('editionLayers'):
if layer_id in cfg['editionLayers'] and cfg['editionLayers'][layer_id]:
edit_layer = cfg['editionLayers'][layer_id]

# Check if edition is possible
# By default not
can_edit = False
if 'acl' in edit_layer and edit_layer['acl']:
if edit_layer.get('acl'):
# acl is defined and not an empty string
# authorization defined for edition
group_edit = edit_layer['acl'].split(',')
Expand Down Expand Up @@ -383,7 +383,7 @@ def _filter_by_login(cfg_layer_login_filter: dict, groups: tuple, login: str) ->
# Build filter
layer_filter = '{} IN ({})'.format(
QgsExpression.quotedColumnRef(cfg_layer_login_filter['filterAttribute']),
', '.join(quoted_values)
', '.join(quoted_values),
)

return layer_filter
6 changes: 3 additions & 3 deletions lizmap_server/lizmap_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def polygon_filter(
body = {
'status': 'success',
'filter': ALL_FEATURES,
'polygons': ''
'polygons': '',
}

# Check first the headers to avoid unnecessary config file reading
Expand Down Expand Up @@ -166,7 +166,7 @@ def polygon_filter(
body = {
'status': 'success',
'filter': NO_FEATURES,
'polygons': ''
'polygons': '',
}
write_json_response(body, response)
return
Expand Down Expand Up @@ -199,7 +199,7 @@ def polygon_filter(
body = {
'status': 'success',
'filter': NO_FEATURES,
'polygons': ''
'polygons': '',
}
write_json_response(body, response)

Expand Down
4 changes: 2 additions & 2 deletions lizmap_server/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def log_exception(e: BaseException):
Logger.critical(
"Critical exception:\n{e}\n{traceback}".format(
e=e,
traceback=traceback.format_exc()
)
traceback=traceback.format_exc(),
),
)


Expand Down
10 changes: 5 additions & 5 deletions lizmap_server/server_info_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def plugins_installed(py_qgis_server: bool) -> list:
return server_active_plugins


def plugin_metadata_key(py_qgis_server: bool, name: str, key: str, ) -> str:
def plugin_metadata_key(py_qgis_server: bool, name: str, key: str) -> str:
""" Return the version for a given plugin. """
unknown = 'unknown'
# it seems configparser is transforming all keys as lowercase...
Expand All @@ -89,8 +89,8 @@ def plugin_metadata_key(py_qgis_server: bool, name: str, key: str, ) -> str:
('version', str),
('build_id', Union[int, None]),
('commit_id', Union[int, None]),
('is_stable', bool)
]
('is_stable', bool),
],
)


Expand Down Expand Up @@ -217,7 +217,7 @@ def handleRequest(self, context):
'gdal': gdal.VersionInfo('VERSION_NUM'),
'python': sys.version,
'qt': Qt.QT_VERSION_STR,
}
},
}
self.write(data, context)

Expand All @@ -244,6 +244,6 @@ def parameters(self, context):
"CHECK_CUSTOM_HEADERS",
False,
QgsServerQueryStringParameter.Type.String,
"If we check custom headers"
"If we check custom headers",
),
]
2 changes: 1 addition & 1 deletion lizmap_server/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def check_environment_variable() -> bool:
'https://docs.lizmap.com/current/en/install/pre_requirements.html#lizmap-server-plugin '
'An environment variable must be enabled to have Lizmap Web Client ≥ 3.5 working.',
"Lizmap",
Qgis.Critical
Qgis.Critical,
)
return False

Expand Down
Loading

0 comments on commit d625d92

Please sign in to comment.