Test Failed
Push — master ( 8192fe...83ef1f )
by Steffen
02:25
created

FileHandler.unicode_translate()   A

Complexity

Conditions 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
nop 3
1
#!/usr/bin/python
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
# -*- coding: utf-8 -*-
3
import html
4
import os
5
import re
6
from shutil import move
7
from typing import Generator
0 ignored issues
show
introduced by
Unable to import 'typing'
Loading history...
8
9
from saucenao.files.filter import Filter
0 ignored issues
show
Unused Code introduced by
Unused Filter imported from saucenao.files.filter
Loading history...
10
11
12
class FileHandler:
0 ignored issues
show
Coding Style introduced by
This class 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...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
13
    @staticmethod
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
14
    def get_files(directory, file_filter=None) -> Generator[str, None, None]:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
15
        """Get all files from given directory
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
16
17
        :type directory: str
18
        :type file_filter: Filter
19
        :return:
20
        """
21
        if file_filter:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
22
            for f in file_filter.apply(directory=directory):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
Coding Style Naming introduced by
The name f 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...
23
                yield f
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
24
        else:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
25
            for f in os.listdir(directory):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
Coding Style Naming introduced by
The name f 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...
26
                if os.path.isfile(os.path.join(directory, f)):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
27
                    yield f
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
28
29
    @staticmethod
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
30
    def unicode_translate(text, chars="", replacement="") -> str:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
31
        """Replacement for the string.maketrans function
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
32
33
        :type text: str
34
        :type chars: str
35
        :type replacement: str
36
        :return:
37
        """
38
        for char in chars:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
39
            text = text.replace(char, replacement[chars.index(char)])
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
40
        return text
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
41
42
    @staticmethod
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
43
    def move_to_category(filename, category, base_directory=os.getcwd()):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
44
        """Move file to the sub_category folder
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
45
46
        :type base_directory: str
47
        :type filename: str
48
        :type category: str
49
        :return:
50
        """
51
        folder = re.sub(r'["/?*:<>|]', r'', html.unescape(category))
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
52
        folder = os.path.join(base_directory, folder)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
53
        folder = os.path.abspath(FileHandler.unicode_translate(folder, "\n\t\r", "   "))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (88/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
54
        if not os.path.exists(folder):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
55
            os.makedirs(folder)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
56
        move(os.path.join(base_directory, filename), os.path.join(folder, filename))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (84/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
57