Exemplo n.º 1
0
 def setUpClass(cls):
     """TODO
     """
     super().setUpClass()
     # We will take charge of setting logging for lyrics_scraper and cie
     lyrics_scraper._SETUP_LOGGING = False
     # Load logging config
     logging_cfg = load_cfg("log")
     # Disable add_datetime
     logging_cfg['add_datetime'] = False
     # Change console handler's level to DEBUG
     logging_cfg['handlers']['console']['level'] = "DEBUG"
     # Change console handler's formatter to 'console'
     logging_cfg['handlers']['console']['formatter'] = "console"
     # Update file handler's filename our test logger's filename
     logging_cfg['handlers']['file']['filename'] = cls.log_filepath
     # Add file handlers to all loggers
     for log_name, log_attrs in logging_cfg['loggers'].items():
         log_attrs['handlers'].append('file')
     # IMPORTANT: remove the root logger because if we don't our test logger
     # will be setup as the root logger in the logging config file
     del logging_cfg['root']
     # Force custom modules to use a different logger with a shorter name
     azlyrics_scraper.logger = logging.getLogger("scraper")
     lyrics_scraper.logger = logging.getLogger("scraper")
     # Setup logging for all custom modules based on updated logging config
     # dict
     setup_logging_from_cfg(logging_cfg)
Exemplo n.º 2
0
 def restore(self):
     """TODO
     """
     shutil.move(self._tmp_cfg_filepath, self._cfg_filepath)
     cfg_dict = load_cfg(self._cfg_type)
     assert_msg = "The {} config file is not the same as the original " \
                  "one".format(self._cfg_type)
     assert self._orig_cfg_dict == cfg_dict, assert_msg
Exemplo n.º 3
0
 def __init__(self, cfg_type):
     self._cfg_type = cfg_type
     self._orig_cfg_dict = load_cfg(self._cfg_type)
     self._cfg_filepath = get_data_filepath(self._cfg_type)
     self._tmp_cfg_filepath = self._cfg_filepath + ".tmp"
     self._new_opt_key = "new_option"
     self._new_opt_val = 22
     self._new_option = {self._new_opt_key: self._new_opt_val}
Exemplo n.º 4
0
def reset_config(cfg_type):
    """Reset a configuration file to its default values.

    The user chooses what type of config file (`cfg_type`) to reset: 'log' for
    the logging file and 'main' for the main config file.

    Parameters
    ----------
    cfg_type : str, {'log', 'main'}
        The type of configuration file we want to reset. 'log' refers to the
        logging config file, and 'main' to the main config file used to setup a
        lyrics scraper.

    Returns
    -------
    retcode: int
        If there is an I/O related error, the return code is 1. Otherwise, it
        is 0 if the config was reset successfully.

    """
    try:
        # TODO: explain
        user_cfg = load_cfg(cfg_type)
        default_cfg = load_cfg('default_' + cfg_type)
        if user_cfg == default_cfg:
            logger.warning("<color>The {} configuration file is already reset"
                           "</color>".format(cfg_type))
            retcode = 2
        else:
            # Get the paths to the default and user-defined config files
            default_cfg_filepath = get_data_filepath('default_' + cfg_type)
            user_cfg_filepath = get_data_filepath(cfg_type)
            # Backup config file (for undo purposes)
            shutil.copyfile(user_cfg_filepath,
                            get_backup_cfg_filepath(cfg_type))
            # Reset config file to factory default values
            shutil.copyfile(default_cfg_filepath, user_cfg_filepath)
            logger.info("<color>The {} configuration file is reset"
                        "</color>".format(cfg_type))
            retcode = 0
    except OSError as e:
        raise e
    else:
        return retcode
Exemplo n.º 5
0
    def _test_reset_config(self, args, expected_retcode=0, extra_msg=None):
        """TODO

        Parameters
        ----------
        args
        expected_retcode
        extra_msg
        undo_reset

        """
        # TODO: explain
        results = self.parse_args(args, extra_msg)
        cfg_type = results.cfg_type
        # Call main() with the given arguments
        retcode = self.call_scraping_main()
        if expected_retcode == 0:  # Success: config file reset
            assert_msg = "The {} config file couldn't be reset. Return code " \
                         "is {}".format(cfg_type, retcode)
            self.assertTrue(retcode == expected_retcode, assert_msg)
            # Test that the config file was really reset by checking its content
            default_cfg_dict = load_cfg('default_' + cfg_type)
            user_cfg_dict = load_cfg(cfg_type)
            assert_msg = "The {} config file was not reset as " \
                         "expected".format(cfg_type)
            self.assertDictEqual(default_cfg_dict, user_cfg_dict, assert_msg)
            self.logger.info("The {} config file was reset <color>as "
                             "expected</color>".format(cfg_type))
        elif expected_retcode == 1:  # ERROR
            self.assert_retcode_when_fail(cfg_type, retcode, expected_retcode,
                                          "reset")
        elif expected_retcode == 2:  # Config file ALREADY reset
            self.assert_retcode_when_fail(cfg_type, retcode, expected_retcode,
                                          "reset")
        else:
            self.fail("Very odd! Return code ({}) not as expected "
                      "({})".format(retcode, expected_retcode))
Exemplo n.º 6
0
 def modify(self):
     shutil.copy(get_data_filepath(self._cfg_type),
                 get_data_filepath(self._cfg_type) + ".tmp")
     cfg_dict = load_cfg(self._cfg_type)
     cfg_dict.update(self._new_option)
     dump_cfg(get_data_filepath(self._cfg_type), cfg_dict)
Exemplo n.º 7
0
 def check_cfg(self):
     cfg_dict = load_cfg(self._cfg_type)
     assert_msg = "The {} config file was not reverted " \
                  "correctly".format(self._cfg_type)
     assert cfg_dict.get(self._new_opt_key) == self._new_opt_val, \
         assert_msg