Completed
Branch master (c130d7)
by Osma
03:05 queued 01:02
created

createSubject()   A

Complexity

Conditions 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 14
rs 9.4285
1
#!/usr/bin/env python3
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
3
import click
4
from flask import Flask
5
from elasticsearch import Elasticsearch
6
from elasticsearch.client import IndicesClient
7
8
es = Elasticsearch()
0 ignored issues
show
Coding Style Naming introduced by
The name es does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
9
index = IndicesClient(es)
0 ignored issues
show
Coding Style Naming introduced by
The name index does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
10
11
annif = Flask(__name__)
0 ignored issues
show
Coding Style Naming introduced by
The name annif does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
12
13
INDEX_NAME = 'annif'
14
15
16
@annif.cli.command('init')
17
def init():
18
    """
19
    Generate the Elasticsearch repository for projects.
20
21
    Usage: annif init
22
    """
23
    if index.exists(INDEX_NAME):
24
        index.delete(INDEX_NAME)
25
    return es.indices.create(index=INDEX_NAME, ignore=400)
0 ignored issues
show
Bug introduced by
The keyword ignore does not seem to exist for the method call.
Loading history...
26
27
28
@annif.cli.command('list-projects')
29
def listprojects():
30
    """
31
    List available projects.
32
33
    Usage: annif list-projects
34
35
    REST equivalent: GET /projects/
36
    """
37
    projs = es.search(index=INDEX_NAME)['hits']
38
    print(projs)
39
    pass
0 ignored issues
show
Unused Code introduced by
Unnecessary pass statement
Loading history...
40
41
42
@annif.cli.command('show-project')
43
@click.argument('projectid')
44
def showProject(projectid):
0 ignored issues
show
Coding Style Naming introduced by
The name showProject does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
45
    """
46
    Show project information.
47
48
    Usage: annif show-project <projectId>
49
50
    REST equivalent:
51
52
    GET /projects/<projectId>
53
    """
54
    pass
55
56
57
@annif.cli.command('create-project')
58
@click.argument('projectid')
59
@click.option('--language')
60
@click.option('--analyzer')
61
def createProject(projectid, language, analyzer):
0 ignored issues
show
Coding Style Naming introduced by
The name createProject does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
62
    """
63
    Create a new project.
64
65
    Usage: annif create-project <projectId> --language <lang> --analyzer
66
    <analyzer>
67
68
    REST equivalent:
69
70
    PUT /projects/<projectId>
71
    """
72
    proj_indexname = "{0}-{1}".format(INDEX_NAME, projectid)
73
    project = {'doc': {'projectid': projectid,
74
                       'language': language, 'analyzer': analyzer}}
75
    index.create(index=proj_indexname)
76
    resp = es.update(
77
            index=proj_indexname,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
78
            doc_type='project',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
79
            id=projectid,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
80
            body=project)
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
81
    print(resp)
82
83
84
@annif.cli.command('drop-project')
85
@click.argument('projectid')
86
def dropProject(projectid):
0 ignored issues
show
Coding Style Naming introduced by
The name dropProject does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
87
    """
88
    Delete a project.
89
    USAGE: annif drop-project <projectid>
90
91
    REST equivalent:
92
93
    DELETE /projects/<projectid>
94
    """
95
    pass
96
97
98
@annif.cli.command('list-subjects')
99
@click.argument('projectid')
100
def listSubjects(projectid):
0 ignored issues
show
Coding Style Naming introduced by
The name listSubjects does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
101
    """
102
    Show all subjects for a project.
103
104
    USAGE: annif list-subjects <projectid>
105
106
    REST equivalent:
107
108
    GET /projects/<projectid>/subjects
109
    """
110
    pass
111
112
113
@annif.cli.command('show-subject')
114
@click.argument('projectid')
115
@click.argument('subjectid')
116
def showSubject(projectid, subjectid):
0 ignored issues
show
Coding Style Naming introduced by
The name showSubject does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
Unused Code introduced by
The argument subjectid seems to be unused.
Loading history...
117
    """
118
    Show information about a subject.
119
120
    USAGE: annif show-subject <projectid> <subjectid>
121
122
    REST equivalent:
123
124
    GET /projects/<projectid>/subjects/<subjectid>
125
    """
126
    pass
127
128
129
@annif.cli.command('create-subject')
130
@click.argument('projectid')
131
@click.argument('subjectid')
132
def createSubject(projectid, subjectid):
0 ignored issues
show
Coding Style Naming introduced by
The name createSubject does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
Unused Code introduced by
The argument subjectid seems to be unused.
Loading history...
133
    """
134
    Create a new subject, or update an existing one.
135
136
    annif create-subject <projectid> <subjectid> <subject.txt
137
138
    REST equivalent:
139
140
    PUT /projects/<projectid>/subjects/<subjectid>
141
    """
142
    pass
143
144
145
@annif.cli.command('load')
146
@click.argument('projectid')
147
@click.argument('directory')
148
@click.option('--clear', default=False)
149
def load(projectid, directory, clear):
0 ignored issues
show
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
Unused Code introduced by
The argument directory seems to be unused.
Loading history...
Unused Code introduced by
The argument clear seems to be unused.
Loading history...
150
    """
151
    Load all subjects from a directory.
152
153
    USAGE: annif load <projectid> <directory> [--clear=CLEAR]
154
    """
155
    pass
156
157
158
@annif.cli.command('drop-subject')
159
@click.argument('projectid')
160
@click.argument('subjectid')
161
def dropSubject(projectid, subjectid):
0 ignored issues
show
Coding Style Naming introduced by
The name dropSubject does not conform to the function naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
Unused Code introduced by
The argument subjectid seems to be unused.
Loading history...
162
    """
163
    Delete a subject.
164
165
    USAGE: annif drop-subject <projectid> <subjectid>
166
167
    REST equivalent:
168
169
    DELETE /projects/<projectid>/subjects/<subjectid>
170
171
    """
172
    pass
173
174
175
@annif.cli.command('analyze')
176
@click.option('--maxhits', default=20)
177
@click.option('--threshold', default=0.9)  # TODO: Check this.
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
178
def analyze(projectid, maxhits, threshold):
0 ignored issues
show
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
Unused Code introduced by
The argument threshold seems to be unused.
Loading history...
Unused Code introduced by
The argument maxhits seems to be unused.
Loading history...
179
    """"
180
    Delete a subject.
181
182
    USAGE: annif drop-subject <projectid> <subjectid>
183
184
    REST equivalent:
185
186
    DELETE /projects/<projectid>/subjects/<subjectid>
187
188
    """
189
    pass
190
191
192
@annif.route('/')
193
def start():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
194
    return 'Started application'
195
196
197
if __name__ == "__main__":
198
    annif.run(port=8000)
199