Example #1
0
def add_packaged_resource(file,
                          user,
                          wgt_file=None,
                          template=None,
                          deploy_only=False):

    close_wgt = False
    if wgt_file is None:
        wgt_file = WgtFile(file)
        close_wgt = True

    if template is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)

    resource_info = template.get_resource_info()

    resource_id = (
        resource_info['vendor'],
        resource_info['name'],
        resource_info['version'],
    )
    file_name = '_'.join(resource_id) + '.wgt'

    check_packaged_resource(wgt_file, resource_info)

    local_dir = wgt_deployer.get_base_dir(*resource_id)
    local_wgt = os.path.join(local_dir, file_name)

    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    overrides = extract_resource_media_from_package(template, wgt_file,
                                                    local_dir)
    if close_wgt:
        wgt_file.close()

    f = open(local_wgt, "wb")
    file.seek(0)
    f.write(file.read())
    f.close()

    if not deploy_only:
        resource_info.update(overrides)

        resource = CatalogueResource.objects.create(
            short_name=resource_info['name'],
            vendor=resource_info['vendor'],
            version=resource_info['version'],
            type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']),
            creator=user,
            template_uri=file_name,
            creation_date=now(),
            popularity='0.0',
            json_description=json.dumps(resource_info))

        return resource
Example #2
0
def add_packaged_resource(file, user, wgt_file=None, template=None, deploy_only=False):

    close_wgt = False
    if wgt_file is None:
        wgt_file = WgtFile(file)
        close_wgt = True

    if template is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)

    resource_info = template.get_resource_info()

    resource_id = (
        resource_info['vendor'],
        resource_info['name'],
        resource_info['version'],
    )
    file_name = '_'.join(resource_id) + '.wgt'

    check_packaged_resource(wgt_file, resource_info)

    local_dir = wgt_deployer.get_base_dir(*resource_id)
    local_wgt = os.path.join(local_dir, file_name)

    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    overrides = extract_resource_media_from_package(template, wgt_file, local_dir)
    if close_wgt:
        wgt_file.close()

    f = open(local_wgt, "wb")
    file.seek(0)
    f.write(file.read())
    f.close()

    if not deploy_only:
        resource_info.update(overrides)

        resource = CatalogueResource.objects.create(
            short_name=resource_info['name'],
            vendor=resource_info['vendor'],
            version=resource_info['version'],
            type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']),
            creator=user,
            template_uri=file_name,
            creation_date=now(),
            popularity='0.0',
            json_description=json.dumps(resource_info)
        )

        return resource
Example #3
0
def add_packaged_resource(file, user, wgt_file=None, template=None, deploy_only=False):

    close_wgt = False
    if wgt_file is None:
        wgt_file = WgtFile(file)
        close_wgt = True

    if template is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)

    resource_info = template.get_resource_info()
    if resource_info['type'] == 'widget':
        code_url = resource_info['contents']['src']
        if not code_url.startswith(('http://', 'https://')):

            try:
                code = wgt_file.read(code_url)
            except KeyError:
                msg = _('Missing contents file: %(file_name)s.')
                raise InvalidContents(msg % {'file_name': code_url})

            try:
                unicode(code, resource_info['contents']['charset'])
            except UnicodeDecodeError:
                msg = _('%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file).')
                raise InvalidContents(msg % {'file_name': code_url, 'charset': resource_info['contents']['charset']})

    resource_id = (
        template.get_resource_vendor(),
        template.get_resource_name(),
        template.get_resource_version(),
    )
    file_name = '_'.join(resource_id) + '.wgt'
    local_dir = wgt_deployer.get_base_dir(*resource_id)
    local_wgt = os.path.join(local_dir, file_name)

    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    overrides = extract_resource_media_from_package(template, wgt_file, local_dir)
    if close_wgt:
        wgt_file.close()

    f = open(local_wgt, "wb")
    file.seek(0)
    f.write(file.read())
    f.close()

    if not deploy_only:
        resource_info.update(overrides)

        resource = CatalogueResource.objects.create(
            short_name=resource_info['name'],
            vendor=resource_info['vendor'],
            version=resource_info['version'],
            type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']),
            creator=user,
            template_uri=file_name,
            creation_date=now(),
            popularity='0.0',
            json_description=json.dumps(resource_info)
        )

        return resource
Example #4
0
def add_resource_from_template(template_uri, template, user, fromWGT=False, overrides=None):

    if isinstance(template, TemplateParser):
        parser = template
    else:
        parser = TemplateParser(template, base=template_uri)

    resource_info = parser.get_resource_info()
    if overrides is not None:
        resource_info.update(overrides)

    resource = CatalogueResource.objects.create(
        short_name=resource_info['name'],
        vendor=resource_info['vendor'],
        version=resource_info['version'],
        fromWGT=fromWGT,
        type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']),
        creator=user,
        author=resource_info['author'],
        display_name=resource_info['display_name'],
        description=resource_info['description'],
        mail=resource_info['email'],
        image_uri=resource_info['image_uri'],
        iphone_image_uri=resource_info.get('iphone_image_uri', ''),
        wiki_page_uri=resource_info['doc_uri'],
        template_uri=template_uri,
        creation_date=now(),
        popularity='0.0',
        json_description=json.dumps(resource_info)
    )

    for input_endpoint in resource_info['wiring']['inputs']:
        WidgetWiring.objects.create(
            idResource=resource,
            wiring='in',
            friendcode=input_endpoint['friendcode']
        )

    for output_endpoint in resource_info['wiring']['outputs']:
        WidgetWiring.objects.create(
            idResource=resource,
            wiring='out',
            friendcode=output_endpoint['friendcode']
        )

    resource_table = resource._get_table_id()
    for lang in resource_info['translations']:
        translation = resource_info['translations'][lang]
        for index in translation:
            value = translation[index]
            usages = resource_info['translation_index_usage'][index]
            for use in usages:
                if use['type'] != 'resource':
                    continue

                Translation.objects.create(
                    text_id=index,
                    element_id=resource.id,
                    table=resource_table,
                    language=lang,
                    value=value,
                    default=resource_info['default_lang'] == lang
                )

                # Create only a translation entry for this index
                break

    return resource
Example #5
0
def add_packaged_resource(file, user, wgt_file=None, template=None, deploy_only=False):

    close_wgt = False
    if wgt_file is None:
        wgt_file = WgtFile(file)
        close_wgt = True

    if template is None:
        template_contents = wgt_file.get_template()
        template = TemplateParser(template_contents)

    resource_info = template.get_resource_info()
    if resource_info["type"] == "widget":
        code_url = resource_info["contents"]["src"]
        if not code_url.startswith(("http://", "https://")):

            try:
                code = wgt_file.read(code_url)
            except KeyError:
                msg = _("Missing contents file: %(file_name)s.")
                raise InvalidContents(msg % {"file_name": code_url})

            try:
                code.decode(resource_info["contents"]["charset"])
            except UnicodeDecodeError:
                msg = _(
                    "%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file)."
                )
                raise InvalidContents(msg % {"file_name": code_url, "charset": resource_info["contents"]["charset"]})

    check_invalid_doc_content(wgt_file, resource_info, "longdescription")
    check_invalid_doc_content(wgt_file, resource_info, "doc")
    check_invalid_doc_content(wgt_file, resource_info, "changelog")

    resource_id = (template.get_resource_vendor(), template.get_resource_name(), template.get_resource_version())
    file_name = "_".join(resource_id) + ".wgt"
    local_dir = wgt_deployer.get_base_dir(*resource_id)
    local_wgt = os.path.join(local_dir, file_name)

    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    overrides = extract_resource_media_from_package(template, wgt_file, local_dir)
    if close_wgt:
        wgt_file.close()

    f = open(local_wgt, "wb")
    file.seek(0)
    f.write(file.read())
    f.close()

    if not deploy_only:
        resource_info.update(overrides)

        resource = CatalogueResource.objects.create(
            short_name=resource_info["name"],
            vendor=resource_info["vendor"],
            version=resource_info["version"],
            type=CatalogueResource.RESOURCE_TYPES.index(resource_info["type"]),
            creator=user,
            template_uri=file_name,
            creation_date=now(),
            popularity="0.0",
            json_description=json.dumps(resource_info),
        )

        return resource