Skip to content

Commit

Permalink
Merge pull request #75 from binary-butterfly/source-cli
Browse files Browse the repository at this point in the history
source cli
  • Loading branch information
the-infinity authored Dec 26, 2024
2 parents 568ec86 + a9021d2 commit 2559922
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Version 1.2.1

Released 2024-12-26

### Features

* [Provides an abstract command line interface for data sources](https://github.com/binary-butterfly/ocpdb/pull/75)


## Version 1.2.0

Released 2024-12-19
Expand Down
3 changes: 1 addition & 2 deletions config_dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ SERVER_AUTH_USERS:
- bnetza

AUTO_FETCH_SOURCES:
- bnetza
- sw-stuttgart
- sw_stuttgart
3 changes: 3 additions & 0 deletions config_dist_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ REMOTE_SERVERS:
url: http://localhost:5000
user: bnetza
password: test

AUTO_FETCH_SOURCES:
- sw_stuttgart
2 changes: 2 additions & 0 deletions webapp/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .giroe_cli import giroe_cli
from .match_cli import match_cli
from .ochp_cli import ochp_cli
from .source_cli import source_cli
from .stadtnavi_cli import stadtnavi_cli
from .sw_stuttgart_cli import sw_stuttgart_cli

Expand All @@ -35,3 +36,4 @@ def register_cli_to_app(app: Flask):
app.cli.add_command(ochp_cli)
app.cli.add_command(stadtnavi_cli)
app.cli.add_command(sw_stuttgart_cli)
app.cli.add_command(source_cli)
41 changes: 41 additions & 0 deletions webapp/cli/source_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Open ChargePoint DataBase OCPDB
Copyright (C) 2021 binary butterfly GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

import click
from flask.cli import AppGroup

from webapp.common.error_handling import catch_exception
from webapp.dependencies import dependencies
from webapp.services.import_services import ImportServices

source_cli = AppGroup('source', help='Generic interface for fetching all data sources')


@source_cli.command('fetch-all', help='Fetches static and realtime data from all auto-fetched sources.')
@catch_exception
def fetch_all_sources() -> None:
import_service: ImportServices = dependencies.get_import_services()
import_service.fetch_sources()


@source_cli.command('fetch', help='Fetches static and realtime data for specified source.')
@click.argument('source_uid')
@catch_exception
def fetch_source(source_uid: str) -> None:
import_service: ImportServices = dependencies.get_import_services()
import_service.fetch_source(source_uid)
25 changes: 25 additions & 0 deletions webapp/services/import_services/import_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,28 @@ def __init__(
self.stadtnavi_import_service.source_info.uid: self.stadtnavi_import_service,
self.sw_stuttgart_import_service.source_info.uid: self.sw_stuttgart_import_service,
}

def fetch_sources(self) -> None:
"""
Fetch all sources enabled via AUTO_FETCH_SOURCES by fetching static and realtime data
"""
for source_uid in self.importer_by_uid.keys():
if source_uid not in self.config_helper.get('AUTO_FETCH_SOURCES', []):
continue

self.fetch_source(source_uid)

def fetch_source(self, source_uid: str) -> None:
"""
Fetches static and realtime data for a specific source uid.
"""
if 'source_uid' not in self.importer_by_uid:
raise Exception('Unknown source UID')

importer_service = self.importer_by_uid[source_uid]

if hasattr(importer_service, 'fetch_static_data'):
importer_service.fetch_static_data()

if hasattr(importer_service, 'fetch_realtime_data'):
importer_service.fetch_realtime_data()

0 comments on commit 2559922

Please sign in to comment.