Completed
Pull Request — master (#351)
by Koen
01:20
created

main()   D

Complexity

Conditions 11

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 61
rs 4.0909
cc 11

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
import optparse
3
import sys
4
import os
5
import time
6
import textwrap
7
8
from pyramid.paster import bootstrap
9
10
from atramhasis.errors import (
11
    SkosRegistryNotFoundException
12
)
13
14
from skosprovider_rdf import utils
15
16
def main():
17
    description = """\
18
    Dump all conceptschemes to files. Will serialise as Turtle and RDF/XML format.
19
    """
20
    usage = "usage: %prog config_uri"
21
    parser = optparse.OptionParser(
22
        usage=usage,
23
        description=textwrap.dedent(description)
24
    )
25
    parser.add_option(
26
        '-l', '--location', dest='dump_location', type='string',
27
        help='Specify where to dump the conceptschemes. Defaults to the location of your ini file.'
28
    )
29
30
    options, args = parser.parse_args(sys.argv[1:])
31
32
    if not len(args) >= 1:
33
        print('You must provide at least one argument.')
34
        return 2
35
36
    config_uri = args[0]
37
38
    dump_location = options.dump_location
39
    if dump_location is None:
40
        dump_location = os.path.abspath(os.path.dirname(config_uri))
41
    print(dump_location)
42
43
    env = bootstrap(config_uri)
44
    request = env['request']
45
46
    if hasattr(request, 'skos_registry') and request.skos_registry is not None:
47
        skos_registry = request.skos_registry
48
    else:
49
        raise SkosRegistryNotFoundException()   # pragma: no cover
50
51
    conceptschemes = [
52
        {'id': x.get_metadata()['id'],
53
            'conceptscheme': x.concept_scheme}
54
        for x in skos_registry.get_providers() if not any([not_shown in x.get_metadata()['subject']
55
                                                                for not_shown in ['external', 'hidden']])
56
    ]
57
58
    for p in skos_registry.get_providers():
59
        if any([not_shown in p.get_metadata()['subject'] for not_shown in ['external', 'hidden']]):
60
            continue;
61
        start_time = time.time()
62
        pid = p.get_metadata()['id']
63
        filename = os.path.join(dump_location, '%s-full' % pid)
64
        filename_ttl = '%s.ttl' % filename
65
        filename_rdf = '%s.rdf' % filename
66
        print('Generating dump for %s' % pid)
67
        graph = utils.rdf_dumper(p)
68
        print('Dumping %s to Turtle: %s' % (pid, filename_ttl))
69
        graph.serialize(destination=filename_ttl, format='turtle')
70
        print('Dumping %s to RDFxml: %s' % (pid, filename_rdf))
71
        graph.serialize(destination=filename_rdf, format='pretty-xml')
72
        print("--- %s seconds ---" % (time.time() - start_time))
73
74
    print('All files dumped to %s' % dump_location)
75
76
    env['closer']()
77