<commit_before>#
# Copyright (c) 2011 rPath, Inc.
#
# All Rights Reserved
#

import sys
import exceptions
import StringIO

import smartform.descriptor

from mint.django_rest.rbuilder.manager import basemanager
from mint.django_rest.rbuilder.platforms import models as platform_models
from mint.django_rest.rbuilder.projects import models as project_models

IMAGE_TYPE_DESCRIPTORS="mint.django_rest.rbuilder.platforms.image_type_descriptors"
exposed = basemanager.exposed

class SourceTypeManager(basemanager.BaseManager):
    @exposed
    def getSourceType(self, source_type):
        ContentSourceTypes = platform_models.ContentSourceTypes()
        cst = platform_models.ContentSourceType.objects.all().filter(content_source_type=source_type)
        ContentSourceTypes.content_source_type = cst
        return ContentSourceTypes
        
    @exposed
    def getSourceTypes(self):
        ContentSourceTypes = platform_models.ContentSourceTypes()
        ContentSourceTypes.content_source_type = platform_models.ContentSourceType.objects.all()
        return ContentSourceTypes
    
    @exposed
    def getSourceTypeById(self, content_source_type_id):
        ContentSourceType = platform_models.ContentSourceType.objects.get(pk=content_source_type_id)
        return ContentSourceType
    
    @exposed
    def createSourceType(self, content_source_type):
        content_source_type.save()
        return content_source_type
        
    @exposed
    def updateSourceType(self, content_source_type):
        content_source_type.save()
        return content_source_type


class PlatformManager(basemanager.BaseManager):
    @exposed
    def getPlatform(self, platform_id):
        return platform_models.Platform.objects.get(platform_id=platform_id)

    @exposed
    def getPlatforms(self):
        Platforms = platform_models.Platforms()
        Platforms.platform = platform_models.Platform.objects.all()
        return Platforms

    @exposed
    def createPlatform(self, platform):
        platform.save()
        return platform

    @exposed
    def updatePlatform(self, platform_id, platform):
        platform.save()
        return platform

    @exposed
    def getImageTypeDefinitionDescriptor(self, name):
        '''
        An image type definition descriptor contains the smartform
        XML needed to define an image type, for example,
        a vmwareImage type will need virtualized RAM and disk
        parameters.   (These are served from templates unless
        the type is deferred, in which case it is generated
        dynamically.  More types may be dynamic later.)
        '''
        if name == 'deferredImage':
            return self._getDeferredImageTypeDescriptor()

        modname = "%s.%s" % (IMAGE_TYPE_DESCRIPTORS, name)
        try:
             __import__(modname)
        except exceptions.ImportError:
             return None
        mod = sys.modules[modname]
        return mod.XML.strip()

    def _getDeferredImageTypeDescriptor(self):
        '''
        A deferred image is a base image + an appliance
        group to install on it. Here we only need to prompt
        for the base image as the appliance group is defined
        based on the project name.
        '''
        desc = smartform.descriptor.ConfigurationDescriptor()
        desc.setRootElement("createApplianceDescriptor")
        desc.setDisplayName("Deferred Image Configuration")
        desc.addDescription("Deferred Image Configuration")

        # TODO: this might be filtered more aggressively later
        # right now the image list could get very large

        deployable_images = project_models.Image.objects.filter(
            output_trove__isnull=False
        )

        smartform_values = [ desc.ValueWithDescription(
           x.output_trove, descriptions=x.name) for
               x in deployable_images ]

        desc.addDataField("baseImageTrove",
            required = True,
            multiple = False,
            type = desc.EnumeratedType(smartform_values)
        )
        sio = StringIO.StringIO()
        desc.serialize(sio)
        result = sio.getvalue()

        # FIXME -- setRootElement does not set the actual root, just the
        # metadata, this is temporary until I find the right way to use the lib.<commit_msg>add displayName field for deferred images (#571)<commit_after>#
# Copyright (c) 2011 rPath, Inc.
#
# All Rights Reserved
#

import sys
import exceptions
import StringIO

import smartform.descriptor

from mint.django_rest.rbuilder.manager import basemanager
from mint.django_rest.rbuilder.platforms import models as platform_models
from mint.django_rest.rbuilder.projects import models as project_models

IMAGE_TYPE_DESCRIPTORS="mint.django_rest.rbuilder.platforms.image_type_descriptors"
exposed = basemanager.exposed

class SourceTypeManager(basemanager.BaseManager):
    @exposed
    def getSourceType(self, source_type):
        ContentSourceTypes = platform_models.ContentSourceTypes()
        cst = platform_models.ContentSourceType.objects.all().filter(content_source_type=source_type)
        ContentSourceTypes.content_source_type = cst
        return ContentSourceTypes
        
    @exposed
    def getSourceTypes(self):
        ContentSourceTypes = platform_models.ContentSourceTypes()
        ContentSourceTypes.content_source_type = platform_models.ContentSourceType.objects.all()
        return ContentSourceTypes
    
    @exposed
    def getSourceTypeById(self, content_source_type_id):
        ContentSourceType = platform_models.ContentSourceType.objects.get(pk=content_source_type_id)
        return ContentSourceType
    
    @exposed
    def createSourceType(self, content_source_type):
        content_source_type.save()
        return content_source_type
        
    @exposed
    def updateSourceType(self, content_source_type):
        content_source_type.save()
        return content_source_type


class PlatformManager(basemanager.BaseManager):
    @exposed
    def getPlatform(self, platform_id):
        return platform_models.Platform.objects.get(platform_id=platform_id)

    @exposed
    def getPlatforms(self):
        Platforms = platform_models.Platforms()
        Platforms.platform = platform_models.Platform.objects.all()
        return Platforms

    @exposed
    def createPlatform(self, platform):
        platform.save()
        return platform

    @exposed
    def updatePlatform(self, platform_id, platform):
        platform.save()
        return platform

    @exposed
    def getImageTypeDefinitionDescriptor(self, name):
        '''
        An image type definition descriptor contains the smartform
        XML needed to define an image type, for example,
        a vmwareImage type will need virtualized RAM and disk
        parameters.   (These are served from templates unless
        the type is deferred, in which case it is generated
        dynamically.  More types may be dynamic later.)
        '''
        if name == 'deferredImage':
            return self._getDeferredImageTypeDescriptor()

        modname = "%s.%s" % (IMAGE_TYPE_DESCRIPTORS, name)
        try:
             __import__(modname)
        except exceptions.ImportError:
             return None
        mod = sys.modules[modname]
        return mod.XML.strip()

    def _getDeferredImageTypeDescriptor(self):
        '''
        A deferred image is a base image + an appliance
        group to install on it. Here we only need to prompt
        for the base image as the appliance group is defined
        based on the project name.
        '''
        desc = smartform.descriptor.ConfigurationDescriptor()
        desc.setRootElement("createApplianceDescriptor")
        desc.setDisplayName("Deferred Image Configuration")
        desc.addDescription("Deferred Image Configuration")

        # TODO: this might be filtered more aggressively later
        # right now the image list could get very large

        deployable_images = project_models.Image.objects.filter(
            output_trove__isnull=False
        )

        smartform_values = [ desc.ValueWithDescription(
           x.output_trove, descriptions=x.name) for
               x in deployable_images ]

        desc.addDataField('displayName',
            required = True,
            multiple = False,
            type = 'str',
            descriptions = ('Image Name', ),
        )

        desc.addDataField("baseImageTrove",
            required = True,
            multiple = False,
            type = desc.EnumeratedType(smartform_values)
        )

        sio = StringIO.StringIO()
        desc.serialize(sio)
        result = sio.getvalue()

        # FIXME -- setRootElement does not set the actual root, just the
        # metadata, this is temporary until I find the right way to use the lib.
{'fraction_non_alphanumeric': 0.06652996126680337, 'fraction_numerical': 0.0012531328320802004, 'mean_word_length': 2.803726169844021, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2359105', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2224, 'n_words': 607}