GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( cc9150...e8485a )
by thatsIch
01:17
created

YamlContentReader.__get_yaml_content_by_git_path()   A

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
1
"""This module is about reading YAML files for Rainmeter related definition files."""
2
3
4
import os.path
5
import zipfile
6
7
import sublime
8
9
from .. import logger
0 ignored issues
show
Bug introduced by
The name logger does not seem to exist in module completion.
Loading history...
10
11
12
class YamlContentReader(object):  # pylint: disable=R0903; this is an abstract class
13
    """
14
    Use this to read the content of yamls inside of sublime text packages.
15
16
    supports multiple ways to access them:
17
    * .sublime-package
18
    * .zip
19
    * folder
20
    """
21
22
    @classmethod
23
    def __get_zip_content(cls, path_to_zip, resource):
0 ignored issues
show
Coding Style introduced by
This method 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...
24
        if not os.path.exists(path_to_zip):
25
            return None
26
27
        ret_value = None
28
29
        with zipfile.ZipFile(path_to_zip) as zip_file:
30
            namelist = zip_file.namelist()
31
            if resource in namelist:
32
                ret_value = zip_file.read(resource)
33
                return ret_value.decode("utf-8")
34
35
        logger.error("no zip content with resource '" + resource + "' found in .")
36
37
        return ret_value
38
39
    def __get_yaml_content_in_package(self, package, dir_of_yaml, yaml_file):
0 ignored issues
show
Coding Style introduced by
This method 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...
40
        # try searching in Installed Packages e.g. if packaged in .sublime-package
41
        packages_path = sublime.installed_packages_path()
42
        sublime_package = package + ".sublime-package"
43
        rm_package_path = os.path.join(packages_path, sublime_package)
44
        if os.path.exists(rm_package_path):
45
            logger.info("found packaged resource in '" + rm_package_path + "'")
46
            resource = dir_of_yaml + yaml_file
47
48
            return self.__get_zip_content(rm_package_path, resource)
49
50
        return None
51
52
    def __get_yaml_content_by_sublime_api(self, dir_of_yaml, yaml_file):
0 ignored issues
show
Coding Style Naming introduced by
The name __get_yaml_content_by_sublime_api does not conform to the method naming conventions ([a-z_][a-z0-9_]{2,30}$).

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 method 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...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
53
        # try over sublimes find resources first
54
        # should handle loose and packaged version
55
        for resource in sublime.find_resources(yaml_file):
56
            if dir_of_yaml in resource:
57
                logger.info(
58
                    "found sublime resource '" + dir_of_yaml + yaml_file + "' in '" + resource + "'"
59
                )
60
                return sublime.load_resource(resource)
61
62
    def __get_yaml_content_by_git_path(self, dir_of_yaml, yaml_file):
0 ignored issues
show
Coding Style introduced by
This method 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...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
63
        # try over absolute paths determined from root e.g. by cloning with git
64
        rm_root_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
65
        # the dir_of_yaml comes in as foo/bar but internally python uses foo\\bar
66
        norm_dir_of_yaml = os.path.normpath(dir_of_yaml)
67
        # e.g. Rainmeter + completion/skin + metadata_section.yaml
68
        yaml_path = os.path.join(rm_root_path, norm_dir_of_yaml, yaml_file)
69
70
        if os.path.exists(yaml_path):
71
            logger.info("found absolute resource in '" + yaml_path + "'")
72
            with open(yaml_path, 'r') as yaml_content_stream:
73
                return yaml_content_stream.read()
74
75
    def __fail(self, dir_of_yaml, yaml_file):
0 ignored issues
show
Coding Style introduced by
This method 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...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
76
        logger.error(
77
            "found not yaml neither via sublime resources, nor absolute pathing, " +
78
            "nor .sublime-package for '" + dir_of_yaml + yaml_file + "'."
79
        )
80
        return None
81
82
    def _get_yaml_content(self, dir_of_yaml, yaml_file):
83
        """
84
        Get yaml content of a yaml file.
85
86
        It is located either in:
87
        * Installed Packages/Rainmeter.sublime-package
88
        * Packages/Rainmeter
89
        Parameters
90
        ----------
91
        """
92
        return self.__get_yaml_content_by_sublime_api(dir_of_yaml, yaml_file) or \
93
            self.__get_yaml_content_by_git_path(dir_of_yaml, yaml_file) or \
94
            self.__get_yaml_content_in_package("Rainmeter", dir_of_yaml, yaml_file) or \
95
            self.__fail(dir_of_yaml, yaml_file)
96