Skip to content

Commit f2285d4

Browse files
committed
🔧 feat: add config (for global sdk vars), Logger for factory log generation, update exception styling
Signed-off-by: saif <[email protected]>
1 parent 3757ddb commit f2285d4

File tree

4 files changed

+152
-3
lines changed

4 files changed

+152
-3
lines changed

src/mapillary/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
from . import exceptions # noqa: F401
1616
from . import geojson # noqa: F401
1717
from . import api # noqa: F401
18+
from . import logger # noqa: F401
19+
from . import config # noqa: F401

src/mapillary/models/config.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
mapillary.models.config
6+
~~~~~~~~~~~~~~~~~~~~~~~~
7+
8+
This module contains the Config class which sets up some global variables fo the duration of
9+
the session that the SDK is in use for.
10+
11+
For more information, please check out https://www.mapillary.com/developer/api-documentation/.
12+
13+
- Copyright: (c) 2021 Facebook
14+
- License: MIT License
15+
"""
16+
17+
from mapillary.models.logger import Logger
18+
19+
Logger.setup_logger(name="mapillary.models.config")
20+
21+
22+
class Config:
23+
"""
24+
Config setup for the SDK
25+
26+
Different parts of the SDK react differently depending on what is set
27+
28+
Usage::
29+
30+
>>> from mapillary.models.config import Config
31+
32+
:param use_strict: If set to True, the SDK will raise an exception if an invalid arguments
33+
are sent to the functions in config.api calls. If set to False, the SDK will just log a warning.
34+
:type use_strict: bool
35+
:default use_strict: True
36+
"""
37+
38+
# Strict mode will raise exceptions when,
39+
# 1. Invalid arguments are passed to a function, such as those
40+
# files in the config.api directory. This can mean invalid date
41+
# formats, invalid IDs, invalid data types, etc.
42+
43+
use_strict = True
44+
45+
def __init__(self, use_strict: bool = True) -> None:
46+
"""
47+
Initialize the Config class
48+
"""
49+
50+
self.use_strict = use_strict

src/mapillary/models/exceptions.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,19 @@ def enforce_literal(
356356
param=param, value=option_selected, options=options
357357
)
358358

359+
359360
class InvalidNumberOfArguments(MapillaryException):
360361
"""
361362
Raised when an inappropriate number of parameters are passed to a function
362363
"""
363364

364-
def __init__(self, number_of_params_passed: int, actual_allowed_params: int, param: str, *args: object) -> None:
365+
def __init__(
366+
self,
367+
number_of_params_passed: int,
368+
actual_allowed_params: int,
369+
param: str,
370+
*args: object,
371+
) -> None:
365372
super().__init__(*args)
366373

367374
self.number_of_params_passed = number_of_params_passed
@@ -371,8 +378,12 @@ def __init__(self, number_of_params_passed: int, actual_allowed_params: int, par
371378
def __str__(self):
372379
return (
373380
f'InvalidNumberOfArguments: The parameter, "{self.param}" was '
374-
f'passed "{self.number_of_params_passed}" items, when the max number of allowed items are "{self.actual_allowed_params}"'
381+
f'passed "{self.number_of_params_passed}" items, when the max number of'
382+
f'allowed items are "{self.actual_allowed_params}"'
375383
)
376384

377385
def __repr__(self):
378-
return f"InvalidNumberOfArguments(number_of_params_passed={self.number_of_params_passed}, actual_allowed_params={self.actual_allowed_params}, param={self.param})"
386+
return (
387+
f"InvalidNumberOfArguments(number_of_params_passed={self.number_of_params_passed},"
388+
f"actual_allowed_params={self.actual_allowed_params}, param={self.param})"
389+
)

src/mapillary/models/logger.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates. (https://www.facebook.com)
2+
# Copyright (c) 2022 Mapillary, Inc. (https://www.mapillary.com)
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
# -*- coding: utf-8 -*-
6+
7+
"""
8+
mapillary.utils.logger
9+
~~~~~~~~~~~~~~~~~~~~~~
10+
11+
This module implements the logger for mapillary, which is a wrapper of the logger package and
12+
the default configuration for each of the loggers per file.
13+
14+
"""
15+
16+
import logging
17+
import sys
18+
import os
19+
20+
21+
class Logger:
22+
23+
format_string: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
24+
level: int = logging.INFO
25+
26+
@staticmethod
27+
def setup_logger(name: str, level: int = logging.INFO) -> logging.Logger:
28+
"""
29+
Function setup as many loggers as you want. To be used at the top of the file.
30+
31+
Usage::
32+
33+
>>> Logger.setup_logger(name='mapillary.xxxx.yyyy', level=logging.INFO)
34+
logger.Logger
35+
36+
:param name: The name of the logger
37+
:type name: str
38+
39+
:param level: The level of the logger
40+
:type level: int
41+
42+
:return: The logger object
43+
:rtype: logging.Logger
44+
"""
45+
46+
# Basic logger setup
47+
logger: Logger = logging.getLogger(name)
48+
49+
# stdout logger setup
50+
stream_handler: logging.StreamHandler = logging.StreamHandler(sys.stdout)
51+
logger.addHandler(stream_handler)
52+
53+
try:
54+
logger.setLevel(level)
55+
except ValueError:
56+
logger.setLevel(logging.INFO)
57+
logger.warning("LOG_LEVEL: Invalid variable - defaulting to: INFO")
58+
59+
# File logger setup
60+
formatter: logging.Formatter = logging.Formatter(Logger.format_string)
61+
handler: logging.FileHandler = logging.FileHandler(
62+
Logger.get_os_log_path(name.split(".")[1] + ".log")
63+
)
64+
handler.setFormatter(formatter)
65+
logger.addHandler(handler)
66+
67+
return logger
68+
69+
@staticmethod
70+
def get_os_log_path(log_file: str) -> str:
71+
"""
72+
Get the path of the log file based on the OS
73+
74+
:param log_file: The name of the log file
75+
:type log_file: str
76+
77+
:return: The path where the logs will be stored
78+
:rtype: str
79+
"""
80+
81+
log_path: str = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
82+
83+
if not os.path.exists(log_path):
84+
os.makedirs(log_path)
85+
86+
return os.path.join(log_path, log_file)

0 commit comments

Comments
 (0)