Passed
Push — master ( 2fcf26...e1f278 )
by
unknown
02:10
created

parseIndexname()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
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
annif.config.from_object('annif.config.Config')
14
15
16
projectIndexConf = {
0 ignored issues
show
Coding Style Naming introduced by
The name projectIndexConf 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...
17
        'mappings': {
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (remove 4 spaces).
Loading history...
18
            'project': {
19
                'properties': {
20
                    'name': {
21
                        'type': 'text'
22
                        },
23
                    'language': {
24
                        'type': 'text'
25
                        },
26
                    'analyzer': {
27
                        'type': 'text'
28
                        }
29
                    }
30
                }
31
            }
32
        }
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation.
Loading history...
33
34
35
def parseIndexname(projectid):
0 ignored issues
show
Coding Style Naming introduced by
The name parseIndexname 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...
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...
36
    return "{0}-{1}".format(annif.config['INDEX_NAME'], projectid)
37
38
39
@annif.cli.command('init')
40
def init():
41
    """
42
    Generate the Elasticsearch repository for projects.
43
44
    Usage: annif init
45
    """
46
    if index.exists(annif.config['INDEX_NAME']):
47
        index.delete(annif.config['INDEX_NAME'])
48
    return es.indices.create(index=annif.config['INDEX_NAME'],
49
                             body=projectIndexConf)
50
51
52
@annif.cli.command('list-projects')
53
def listprojects():
54
    """
55
    List available projects.
56
57
    Usage: annif list-projects
58
59
    REST equivalent: GET /projects/
60
    """
61
62
    doc = {'size': 1000, 'query': {'match_all': {}}}
63
    results = es.search(index=annif.config['INDEX_NAME'], doc_type='project',
64
                        body=doc)
65
    projects = [x['_source'] for x in results['hits']['hits']]
66
    for p in projects:
0 ignored issues
show
Coding Style Naming introduced by
The name p does not conform to the variable 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...
67
        print(p)
68
69
70
@annif.cli.command('show-project')
71
@click.argument('projectid')
72
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...
73
    """
74
    Show project information.
75
76
    Usage: annif show-project <projectId>
77
78
    REST equivalent:
79
80
    GET /projects/<projectId>
81
    """
82
    pass
83
84
85
@annif.cli.command('create-project')
86
@click.argument('projectid')
87
@click.option('--language')
88
@click.option('--analyzer')
89
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...
90
    """
91
    Create a new project.
92
93
    Usage: annif create-project <projectId> --language <lang> --analyzer
94
    <analyzer>
95
96
    REST equivalent:
97
98
    PUT /projects/<projectId>
99
    """
100
    proj_indexname = parseIndexname(projectid)
101
102
    # Create an index for the project
103
    index.create(index=proj_indexname)
104
105
    # Add the details of the new project to the 'master' index
106
    resp = es.create(index=annif.config['INDEX_NAME'], doc_type='project',
107
                     id=projectid,
108
                     body={'name': projectid, 'language': language,
109
                           'analyzer': analyzer})
110
    print(resp)
111
112
113
@annif.cli.command('drop-project')
114
@click.argument('projectid')
115
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...
116
    """
117
    Delete a project.
118
    USAGE: annif drop-project <projectid>
119
120
    REST equivalent:
121
122
    DELETE /projects/<projectid>
123
    """
124
    # Delete the index from the 'master' index
125
    result = es.delete(index=annif.config['INDEX_NAME'],
126
                       doc_type='project', id=projectid)
127
128
    print(result)
129
130
    # Then delete the project index
131
    result = index.delete(index=parseIndexname(projectid))
132
    print(result)
133
134
135
@annif.cli.command('list-subjects')
136
@click.argument('projectid')
137
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...
138
    """
139
    Show all subjects for a project.
140
141
    USAGE: annif list-subjects <projectid>
142
143
    REST equivalent:
144
145
    GET /projects/<projectid>/subjects
146
    """
147
    pass
148
149
150
@annif.cli.command('show-subject')
151
@click.argument('projectid')
152
@click.argument('subjectid')
153
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 subjectid seems to be unused.
Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
154
    """
155
    Show information about a subject.
156
157
    USAGE: annif show-subject <projectid> <subjectid>
158
159
    REST equivalent:
160
161
    GET /projects/<projectid>/subjects/<subjectid>
162
    """
163
    pass
164
165
166
@annif.cli.command('create-subject')
167
@click.argument('projectid')
168
@click.argument('subjectid')
169
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 subjectid seems to be unused.
Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
170
    """
171
    Create a new subject, or update an existing one.
172
173
    annif create-subject <projectid> <subjectid> <subject.txt
174
175
    REST equivalent:
176
177
    PUT /projects/<projectid>/subjects/<subjectid>
178
    """
179
    pass
180
181
182
@annif.cli.command('load')
183
@click.argument('projectid')
184
@click.argument('directory')
185
@click.option('--clear', default=False)
186
def load(projectid, directory, clear):
0 ignored issues
show
Unused Code introduced by
The argument directory seems to be unused.
Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
Unused Code introduced by
The argument clear seems to be unused.
Loading history...
187
    """
188
    Load all subjects from a directory.
189
190
    USAGE: annif load <projectid> <directory> [--clear=CLEAR]
191
    """
192
    pass
193
194
195
@annif.cli.command('drop-subject')
196
@click.argument('projectid')
197
@click.argument('subjectid')
198
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 subjectid seems to be unused.
Loading history...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
199
    """
200
    Delete a subject.
201
202
    USAGE: annif drop-subject <projectid> <subjectid>
203
204
    REST equivalent:
205
206
    DELETE /projects/<projectid>/subjects/<subjectid>
207
208
    """
209
    pass
210
211
212
@annif.cli.command('analyze')
213
@click.option('--maxhits', default=20)
214
@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...
215
def analyze(projectid, maxhits, threshold):
0 ignored issues
show
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...
Unused Code introduced by
The argument projectid seems to be unused.
Loading history...
216
    """"
217
    Delete a subject.
218
219
    USAGE: annif drop-subject <projectid> <subjectid>
220
221
    REST equivalent:
222
223
    DELETE /projects/<projectid>/subjects/<subjectid>
224
225
    """
226
    pass
227
228
229
@annif.route('/')
230
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...
231
    return 'Started application'
232
233
234
if __name__ == "__main__":
235
    annif.run(port=8000)
236