Exemplo n.º 1
0
    def define(self, container_def: str) -> str:
        """Adds a container via a container definition file

        Args:
            container_def: Definition file to use for adding a container.

        Returns:
            Name of newly defined container
        """
        LOGGER.debug("Reading container definition %s'", container_def)
        file_d = fops.file_read_convert(container_def, 'YAML')

        LOGGER.debug("Validating container definition %s", container_def)
        c_opts = self.validate_container_options(file_d)

        if c_opts['name'] in self.__containers:
            err_s = "Container '{0!s}' already defined.".format(c_opts['name'])
            raise DockerError(err_s)

        if self.__groups and c_opts['group']:
            if c_opts['group'] not in self.__groups.list():
                err_s = _ERR_CONTAINER_UNDEF_GROUP.format(c_opts['name'])
                raise ConfigError(err_s)
            g_info = self.__groups.get(c_opts['group'])
            if c_opts['name'] not in g_info.members:
                g_info.members.append(c_opts['name'])
                self.__groups.save()

        self.__containers[c_opts['name']] = os.path.join(
            self.__config_path, "{0!s}.yaml".format(c_opts['name']))

        _save_container_file(c_opts['name'], self.__containers[c_opts['name']],
                             c_opts.to_dict())

        return c_opts.name
Exemplo n.º 2
0
    def get(self, container_name: str) -> DockerContainer:
        """Returns an already defined container

        Args:
            container_name: Name of the container to return info for.

        Returns:
            DockerContainer information class
        """
        if container_name not in self.__containers:
            err_s = "Container '{0!s}' not defined."
            raise DockerError(err_s.format(container_name))

        LOGGER.debug("Reading container info for %s", container_name)
        file_d = fops.file_read_convert(self.__containers[container_name],
                                        'YAML')

        LOGGER.debug("Validating container info for %s", container_name)
        container_info = self.validate_container_options(file_d)

        LOGGER.debug("Initializing image class for %s", container_name)
        container_image = DockerImage(
            self.__client,
            container_info.image,
            build_path=container_info.image_build_path,
            build_squash=container_info.image_build_squash,
            insecure_registry=container_info.image_insecure,
            username=container_info.image_username,
            password=container_info.image_password)

        return DockerContainer(self.__client,
                               container_info,
                               container_image,
                               file_p=self.__containers[container_name])
Exemplo n.º 3
0
    def test_file_read_convert_file_does_not_exist_get_default(self):
        want = dict()

        got = fops.file_read_convert("should_not_exist_hopefully_maybe",
                                     fops.JSON,
                                     default=True)

        self.assertDictEqual(got, want)
Exemplo n.º 4
0
    def test_file_read_convert_yaml(self):
        data = '''test: "test"'''
        want = {'test': 'test'}
        path = _get_file(data, ".yml")

        got = fops.file_read_convert(path, fops.YAML)
        os.remove(path)

        self.assertDictEqual(got, want)
Exemplo n.º 5
0
    def read(system_path: str, user_path: str) -> dict:
        """Reads specified yaml configuration files

        Args:
            system_path: Full path to system wide YAML config file
            user_path: Full path to users YAML config file

        Returns:
            A dictionary filled with system settings, overlapped with the user specific settings
        """
        ret = {}
        if system_path:
            ret = merge_dictionaries(
                ret, fops.file_read_convert(system_path, 'yaml', True))
        if user_path:
            ret = merge_dictionaries(
                ret, fops.file_read_convert(user_path, 'yaml', True))

        return ret
Exemplo n.º 6
0
    def test_file_read_convert_kv(self):
        data = """test=test
        """
        want = {'test': 'test'}
        path = _get_file(data, ".tmp")

        got = fops.file_read_convert(path, fops.KV)
        os.remove(path)

        self.assertDictEqual(got, want)
Exemplo n.º 7
0
    def test_file_read_convert_xml(self):
        data = """<?xml version="1.0"?>
            <test>test</test>
        """
        want = {'test': 'test'}
        path = _get_file(data, ".xml")

        got = fops.file_read_convert(path, fops.XML)
        os.remove(path)

        self.assertDictEqual(got, want)
Exemplo n.º 8
0
    def test_file_read_convert_json(self):
        data = """{
            "test": "test"
        }
        """
        want = {'test': 'test'}
        path = _get_file(data, ".json")

        got = fops.file_read_convert(path, fops.JSON)
        os.remove(path)

        self.assertDictEqual(got, want)
Exemplo n.º 9
0
def load_projects_folder_read() -> dict:
    """Reads YAML files in this directory and loads them as projects

    Returns:
        a dictionary to be merged into the main settings object.
    """
    projects = dict()
    files = glob.glob("*.yaml")

    for file in files:
        projects[os.path.splitext(file)[0]] = fops.file_read_convert(
            file, fops.YAML, True)

    return {'backup': {'projects': projects}}
Exemplo n.º 10
0
def run() -> None:
    """Run functionality."""
    # pylint: disable=global-statement
    global _DEBUG

    args = do_args()

    _DEBUG = args.debug_log
    setup_app_logging(args.debug_log)

    if args.version_out:
        do_version()

    if args.setup:
        do_setup()

    files, licenses = load_files_and_licenses()

    if args.list_file_types:
        do_print_file_types(files)

    if args.list_licenses:
        do_print_license_types(licenses)

    config = merge_dictionaries(DEFAULTS, fops.file_read_convert(CONFIG_FILE_PATH, fops.YAML, True))
    config = verify_config(config, args)

    if not files.get(config.get('file_type')):
        LOGGER.error("unsupported file type: %s", config.get('file_type'))
        raise SystemExit(-1)

    if not licenses.get(config.get('license')):
        LOGGER.error("unsupported license type: %s", config.get('license)'))
        raise SystemExit(-1)

    license_obj = licenses.get(config.get('license'))
    new_file = files.get(config.get('file_type')).new(config, license_obj)
    new_file.write()
Exemplo n.º 11
0
def copy_info(orig_info: str, new_info: str, no_info: str, new_base: str,
              pages: list) -> list:
    """Copies the ComicInfo.xml file over, fixing pages entries.

    Args:
        orig_info: Full path to the original ComicInfo.xml.
        new_info: Full path to the new ComicInfo.xml.
        no_info: Full path to NoComicInfo file if no ComicInfo.xml is found.
        new_base: Full path to the directory containing comic book files.
        pages: List of pages for the new comic book.
    """
    if not os.path.isfile(orig_info):
        Path(no_info).touch()
        return pages

    comic_info_data = fops.file_read_convert(orig_info, fops.XML, default=True)
    if comic_info_data.get('ComicInfo'):
        comic_info_pages_page = []
        page_count = 0
        for page in sorted(pages):
            path = os.path.join(new_base, page)
            image = Image.open(fr'{path}')
            page_info = OrderedDict()
            page_info['@Image'] = str(page_count)
            page_info['@ImageWidth'] = str(image.width)
            page_info['@ImageHeight'] = str(image.height)
            image.close()
            comic_info_pages_page.append(page_info)
            page_count += 1

        comic_info_pages_page[0]['@Type'] = 'FrontCover'
        comic_info_data['ComicInfo']['Pages'] = OrderedDict()
        comic_info_data['ComicInfo']['Pages']['Page'] = comic_info_pages_page

    fops.file_write_convert(new_info, fops.XML, comic_info_data)

    return pages + [COMIC_INFO_XML]
Exemplo n.º 12
0
 def __read(self) -> None:
     LOGGER.debug('Building list of currently defined groups.')
     groups_yaml = fops.file_read_convert(self.__config, 'YAML', True)
     for key, value in groups_yaml.items():
         self.add(key, value, False)