Skip to content

check_service

ServiceChecker

Source code in src/cookiecutter_python/backend/hosting_services/check_service.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@attr.s(auto_attribs=True, slots=True, frozen=True)
class ServiceChecker:
    # parser of User Config (YAML) file
    name_extractor: Callable[[str], str]  # (config_file: str) -> str

    # Resolves URLs and makes Future HTTP Requests
    web_service_checker: WebHostingServiceChecker

    # Hard switch to enable/disable feature
    activate_flag: bool

    # Path to User Config (YAML) file, to parse at runtime, on __call__ invoke
    config_file_path: str

    def __call__(self):
        """Check the remote server for existing resource, if feture is enabled.

        Returns:
            Optional[CheckResult]: result of the check operation
        """
        if self.activate_flag:
            # TODO Improvement: enable feature regardless of default_config
            try:
                name = self.name_extractor(self.config_file_path)
                result = self.web_service_checker(name)
                return result
            except ContextVariableDoesNotExist as error:
                # we assume that client deliberately had the activate flag on
                # only because they know that the way the Generator has been
                # parametrized (ie from CLI),
                # accounting for User Config or Default Config precedance, is such
                # that on Generator call the User Config will have precendence.
                # But this is design to be call in pre_main, so rendering has
                # not happened yet, so we can't rely on the User Config.

                # We could manullay render the cookiecutter.json file, with jinja2

                # or we can signal, that this can be called after rendering
                # in post_main, but we are going to loose fancy Futures HTTP!
                # and we are going to have to do the HTTP request in the main thread
                # and we are going to have to do it in a blocking way, but still at the very end

                # Atm, leaning more towards an INFO than a WARNING
                logger.info(
                    "Skipping check of remote server, because of missing context variable"
                )
                logger.info(error)
                # atm service checker can only rely on user config yaml file
                # the info can be derived from static cookiecutter.json file
                # or after rendering the template
        return None

    @property
    def service_name(self):
        return str(self.web_service_checker)

    def __str__(self):
        return str(self.web_service_checker)

    @staticmethod
    def create(hosting_service_info, activate_flag: bool, config_file_path):
        return ServiceChecker(
            NameExtractor.create(hosting_service_info),
            WebHostingServiceChecker.create(hosting_service_info.service),
            activate_flag,
            config_file_path,
        )

__call__()

Check the remote server for existing resource, if feture is enabled.

Returns:

Type Description

Optional[CheckResult]: result of the check operation

Source code in src/cookiecutter_python/backend/hosting_services/check_service.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def __call__(self):
    """Check the remote server for existing resource, if feture is enabled.

    Returns:
        Optional[CheckResult]: result of the check operation
    """
    if self.activate_flag:
        # TODO Improvement: enable feature regardless of default_config
        try:
            name = self.name_extractor(self.config_file_path)
            result = self.web_service_checker(name)
            return result
        except ContextVariableDoesNotExist as error:
            # we assume that client deliberately had the activate flag on
            # only because they know that the way the Generator has been
            # parametrized (ie from CLI),
            # accounting for User Config or Default Config precedance, is such
            # that on Generator call the User Config will have precendence.
            # But this is design to be call in pre_main, so rendering has
            # not happened yet, so we can't rely on the User Config.

            # We could manullay render the cookiecutter.json file, with jinja2

            # or we can signal, that this can be called after rendering
            # in post_main, but we are going to loose fancy Futures HTTP!
            # and we are going to have to do the HTTP request in the main thread
            # and we are going to have to do it in a blocking way, but still at the very end

            # Atm, leaning more towards an INFO than a WARNING
            logger.info(
                "Skipping check of remote server, because of missing context variable"
            )
            logger.info(error)
            # atm service checker can only rely on user config yaml file
            # the info can be derived from static cookiecutter.json file
            # or after rendering the template
    return None