atramhasis.skos   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 37.04 %

Importance

Changes 0
Metric Value
eloc 34
dl 20
loc 54
rs 10
c 0
b 0
f 0
wmc 4

2 Functions

Rating   Name   Duplication   Size   Complexity  
A register_providers_from_db() 0 11 2
A create_registry() 20 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import logging
2
3
import requests
4
from cachecontrol import CacheControl
5
from cachecontrol.heuristics import ExpiresAfter
6
from skosprovider.registry import Registry
7
from skosprovider_getty.providers import AATProvider
8
from skosprovider_getty.providers import TGNProvider
9
from sqlalchemy.orm import Session
10
11
from atramhasis import utils
12
from atramhasis.data.datamanagers import ProviderDataManager
13
14
log = logging.getLogger(__name__)
15
LICENSES = [
16
    'https://creativecommons.org/licenses/by/4.0/',
17
    'https://data.vlaanderen.be/doc/licentie/modellicentie-gratis-hergebruik/v1.0',
18
]
19
20
21
def register_providers_from_db(registry: Registry, session: Session) -> None:
22
    """
23
    Retrieve all providers stored in the database and add them to the registry.
24
25
    :param registry: The registry to which the providers will be added.
26
    :param session: A database session.
27
    """
28
    manager = ProviderDataManager(session)
29
    for db_provider in manager.get_all_providers():
30
        provider = utils.db_provider_to_skosprovider(db_provider)
31
        registry.register_provider(provider)
32
33
34 View Code Duplication
def create_registry(request):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
35
    try:
36
        registry = Registry(instance_scope='threaded_thread')
37
38
        getty_session = CacheControl(
39
            requests.Session(), heuristic=ExpiresAfter(weeks=1)
40
        )
41
42
        aat = AATProvider({'id': 'AAT', 'subject': ['external']}, session=getty_session)
43
44
        tgn = TGNProvider({'id': 'TGN', 'subject': ['external']}, session=getty_session)
45
46
        registry.register_provider(aat)
47
        registry.register_provider(tgn)
48
        register_providers_from_db(registry, request.db)
49
50
        return registry
51
    except AttributeError:
52
        log.exception('Attribute error during creation of Registry.')
53
        raise
54