Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
avara1986 committed Nov 30, 2019
1 parent 652d97d commit 9634741
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 12 deletions.
4 changes: 4 additions & 0 deletions pyms/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pyms.config.conf import get_conf
from pyms.config.confile import ConfFile

__all__ = ['get_conf', 'ConfFile']
4 changes: 2 additions & 2 deletions pyms/config/confile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def __init__(self, *args, **kwargs):
kwargs=kwargs,
))

config = {k: v for k, v in self.normalize_config(config)}
[setattr(self, k, v) for k, v in config.items()]
config = dict(self.normalize_config(config))
_ = [setattr(self, k, v) for k, v in config.items()]
super(ConfFile, self).__init__(config)

def normalize_config(self, config):
Expand Down
13 changes: 9 additions & 4 deletions pyms/flask/app/create_app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import logging
import os
from typing import Text

import connexion
from flask import Flask
from flask_opentracing import FlaskTracing

from pyms.config.conf import get_conf
from pyms.config import get_conf
from pyms.constants import LOGGER_NAME, SERVICE_ENVIRONMENT
from pyms.flask.healthcheck import healthcheck_blueprint
from pyms.flask.services.driver import ServicesManager
from pyms.logger import CustomJsonFormatter
from pyms.utils.utils import check_package_exists
from pyms.utils import check_package_exists

logger = logging.getLogger(LOGGER_NAME)

Expand Down Expand Up @@ -56,7 +57,7 @@ def init_libs(self):
return self.application

def init_tracer(self):
if getattr(self, "tracer", False) and self.tracer:
if self._exists_service("tracer"):
client = self.tracer.get_client()
self.application.tracer = FlaskTracing(client, True, self.application)

Expand All @@ -78,7 +79,7 @@ def init_logger(self):
self.application.logger.setLevel(logging.INFO)

def init_app(self) -> Flask:
if getattr(self, "swagger", False) and self.swagger:
if self._exists_service("swagger"):
check_package_exists("connexion")
app = connexion.App(__name__, specification_dir=os.path.join(self.path, self.swagger.path))
app.add_api(
Expand Down Expand Up @@ -123,6 +124,10 @@ def create_app(self):

return self.application

def _exists_service(self, service_name: Text) -> bool:
service = getattr(self, service_name, False)
return service and service is not None

def add_error_handlers(self):
"""Subclasses will override this method in order to add specific error handlers. This should be done with
calls to add_error_handler method.
Expand Down
2 changes: 1 addition & 1 deletion pyms/flask/services/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pyms.constants import LOGGER_NAME
from pyms.flask.services.driver import DriverService
from pyms.utils.utils import check_package_exists, import_package, import_from
from pyms.utils import check_package_exists, import_package, import_from

logger = logging.getLogger(LOGGER_NAME)

Expand Down
3 changes: 3 additions & 0 deletions pyms/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from pyms.utils.utils import import_from, import_package, check_package_exists

__all__ = ['import_from', 'import_package', 'check_package_exists']
4 changes: 1 addition & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import unittest
from unittest import mock

from pyms.config.conf import get_conf
from pyms.config.confile import ConfFile
from pyms.config import get_conf, ConfFile
from pyms.constants import CONFIGMAP_FILE_ENVIRONMENT, LOGGER_NAME
from pyms.exceptions import AttrDoesNotExistException, ConfigDoesNotFoundException, ServiceDoesNotExistException

Expand Down Expand Up @@ -112,7 +111,6 @@ def test_empty_conf_three_levels(self):
self.assertEqual(config.my_ms.level_two.level_three, {})



class GetConfig(unittest.TestCase):
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

Expand Down
2 changes: 1 addition & 1 deletion tests/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_healthcheck(self):

class MicroserviceTest(unittest.TestCase):
"""
Tests for healthcheack endpoints
Tests for Singleton
"""

def setUp(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from pyms.exceptions import PackageNotExists
from pyms.utils.utils import check_package_exists, import_package
from pyms.utils import check_package_exists, import_package


class ConfUtils(unittest.TestCase):
Expand Down

0 comments on commit 9634741

Please sign in to comment.