本文整理汇总了Python中wirecloud.commons.utils.wgt.WgtFile类的典型用法代码示例。如果您正苦于以下问题:Python WgtFile类的具体用法?Python WgtFile怎么用?Python WgtFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WgtFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_resource_catalogue_cache
def update_resource_catalogue_cache(orm=None):
from wirecloud.catalogue.utils import wgt_deployer
if orm is not None:
resources = orm.CatalogueResource.objects.all()
else:
from wirecloud.catalogue.models import CatalogueResource
resources = CatalogueResource.objects.all()
for resource in resources:
try:
if resource.fromWGT:
base_dir = wgt_deployer.get_base_dir(resource.vendor, resource.short_name, resource.version)
wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
template = wgt_file.get_template()
wgt_file.close()
else:
template = download_http_content(resource.template_uri)
template_parser = TemplateParser(template)
resource.json_description = json.dumps(template_parser.get_resource_info())
resource.save()
except TemplateParseException as e:
from django.conf import settings
if getattr(settings, 'WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION', False) is not True:
raise e
print(' Removing %s' % (resource.vendor + '/' + resource.short_name + '/' + resource.version))
resource.delete()
示例2: wrapper
def wrapper(self, *args, **kwargs):
if shared:
base = self.shared_test_data_dir
else:
base = self.test_data_dir
final_creator = User.objects.get(username=creator) if creator is not None else None
final_users = tuple(User.objects.get(username=user) for user in users)
final_groups = tuple(Group.objects.get(name=group) for group in groups)
for resource in resources:
wgt_file = open(os.path.join(base, resource), 'rb')
wgt = WgtFile(wgt_file)
fix_dev_version(wgt, final_creator)
if deploy_only:
catalogue.add_packaged_resource(wgt.get_underlying_file(), final_creator, wgt_file=wgt, deploy_only=True)
wgt_file.close()
continue
resource = install_resource(wgt, final_creator)
if public:
resource.public = True
resource.save()
resource.users.add(*final_users)
resource.groups.add(*final_groups)
wgt_file.close()
return test_func(self, *args, **kwargs)
示例3: wrapper
def wrapper(self, *args, **kwargs):
owner_user = User.objects.get(username=owner)
if shared:
base = self.shared_test_data_dir
else:
base = self.test_data_dir
with open(os.path.join(base, file_name), 'rb') as f:
wgt = WgtFile(f)
template = TemplateParser(wgt.get_template())
resource_info = template.get_resource_processed_info(process_urls=False)
if resource_info["type"] != 'mashup':
raise Exception
for embedded_resource in resource_info['embedded']:
if embedded_resource['src'].startswith('https://'):
resource_file = download_http_content(embedded_resource['src'])
else:
resource_file = BytesIO(wgt.read(embedded_resource['src']))
extra_resource_contents = WgtFile(resource_file)
install_resource_to_user(owner_user, file_contents=extra_resource_contents)
buildWorkspaceFromTemplate(template, owner_user)
return test_func(self, *args, **kwargs)
示例4: install_resource
def install_resource(file_contents, templateURL, executor_user, packaged):
if packaged:
if isinstance(file_contents, basestring):
file_contents = StringIO(file_contents)
wgt_file = WgtFile(file_contents)
elif isinstance(file_contents, WgtFile):
wgt_file = file_contents
file_contents = wgt_file.get_underlying_file()
else:
raise Exception
template_contents = wgt_file.get_template()
else:
template_contents = file_contents
template = TemplateParser(template_contents)
resources = CatalogueResource.objects.filter(vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version())[:1]
# Create/recover catalogue resource
if len(resources) == 1:
resource = resources[0]
else:
if packaged:
resource = add_widget_from_wgt(file_contents, executor_user, wgt_file=wgt_file)
else:
resource = add_resource_from_template(templateURL, template_contents, executor_user)
return resource
示例5: add_widget_from_wgt
def add_widget_from_wgt(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_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:
return add_resource_from_template(file_name, template, user, fromWGT=True, overrides=overrides)
示例6: process
def process(self, request, to_ws_id):
data = parse_json_request(request)
mashup_id = data.get('mashup', '')
workspace_id = data.get('workspace', '')
if mashup_id == '' and workspace_id == '':
return build_error_response(request, 422, _('Missing workspace or mashup parameter'))
elif mashup_id != '' and workspace_id != '':
return build_error_response(request, 422, _('Workspace and mashup parameters cannot be used at the same time'))
to_ws = get_object_or_404(Workspace, id=to_ws_id)
if not request.user.is_superuser and to_ws.creator != request.user:
return build_error_response(request, 403, _('You are not allowed to update this workspace'))
if mashup_id != '':
values = mashup_id.split('/', 3)
if len(values) != 3:
return build_error_response(request, 422, _('invalid mashup id'))
(mashup_vendor, mashup_name, mashup_version) = values
try:
resource = CatalogueResource.objects.get(vendor=mashup_vendor, short_name=mashup_name, version=mashup_version)
if not resource.is_available_for(request.user) or resource.resource_type() != 'mashup':
raise CatalogueResource.DoesNotExist
except CatalogueResource.DoesNotExist:
return build_error_response(request, 422, _('Mashup not found: %(mashup_id)s') % {'mashup_id': mashup_id})
base_dir = catalogue.wgt_deployer.get_base_dir(mashup_vendor, mashup_name, mashup_version)
wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
template = TemplateParser(wgt_file.get_template())
else:
from_ws = get_object_or_404(Workspace, id=workspace_id)
if not request.user.is_superuser and from_ws.creator != request.user:
return build_error_response(request, 403, _('You are not allowed to read from workspace %s') % workspace_id)
options = {
'vendor': 'api',
'name': 'merge_op',
'version': '1.0',
'title': '',
'description': 'Temporal mashup for merging operation',
'email': '[email protected]',
}
template = TemplateParser(build_json_template_from_workspace(options, from_ws, from_ws.creator))
try:
check_mashup_dependencies(template, request.user)
except MissingDependencies as e:
details = {
'missingDependencies': e.missing_dependencies,
}
return build_error_response(request, 422, e, details=details)
fillWorkspaceUsingTemplate(to_ws, template)
return HttpResponse(status=204)
示例7: _parse_ac_request
def _parse_ac_request(request):
fileURL = None
file_contents = None
content_type = get_content_type(request)[0]
try:
data = json.loads(request.body)
except Exception as e:
msg = _("malformed json data: %s") % unicode(e)
return build_error_response(request, 400, msg)
if 'url' not in data:
return build_error_response(request, 400, _('Missing widget URL'))
fileURL = data.get('url')
id_4CaaSt = data.get('4CaaStID')
if id_4CaaSt is None:
return build_error_response(request, 400, _('Missing 4CaaStID'))
if not isinstance(id_4CaaSt, string_types) or id_4CaaSt.strip() == '':
return build_error_response(request, 400, _('Invalid 4CaaStID'))
try:
downloaded_file = download_http_content(fileURL)
except:
return build_error_response(request, 409, _('Mashable application component could not be downloaded'))
downloaded_file = StringIO(downloaded_file)
file_contents = WgtFile(downloaded_file)
# Create a custom version of the resource
template = TemplateParser(file_contents.get_template())
template_info = template.get_resource_info()
template_info['name'] += '@' + id_4CaaSt
for pref_name, pref_value in six.iteritems(data.get('preferences', {})):
for widget_pref_index, widget_pref in enumerate(template_info['preferences']):
if widget_pref['name'] == pref_name:
template_info['preferences'][widget_pref_index]['readonly'] = True
template_info['preferences'][widget_pref_index]['value'] = pref_value
break
# Write a new Wgt file
new_file = StringIO()
zin = zipfile.ZipFile(downloaded_file, 'r')
zout = zipfile.ZipFile(new_file, 'w')
zout.writestr('config.xml', write_rdf_description(template_info))
for item in zin.infolist():
if item.filename == 'config.xml':
continue
zout.writestr(item, zin.read(item.filename))
zin.close()
zout.close()
file_contents = WgtFile(new_file)
return id_4CaaSt, file_contents, fileURL
示例8: _handle
def _handle(self, *args, **options):
if len(args) < 1:
raise CommandError(_('Wrong number of arguments'))
self.verbosity = int(options.get('verbosity', 1))
users = []
groups = []
redeploy = options['redeploy']
public = options['public']
users_string = options['users'].strip()
groups_string = options['groups'].strip()
if redeploy is False and public is False and users_string == '' and groups_string == '':
raise CommandError(_('You must use at least one of the following flags: --redeploy, --users, --groups or --public '))
if not options['redeploy']:
if users_string != '':
for username in users_string.split(','):
users.append(User.objects.get(username=username))
if groups_string != '':
for groupname in groups_string.split(','):
groups.append(Group.objects.get(name=groupname))
for file_name in args:
try:
f = open(file_name, 'rb')
wgt_file = WgtFile(f)
except:
self.log(_('Failed to read from %(file_name)s') % {'file_name': file_name}, level=1)
continue
try:
template_contents = wgt_file.get_template()
template = TemplateParser(template_contents)
if options['redeploy']:
add_packaged_resource(f, None, wgt_file=wgt_file, template=template, deploy_only=True)
else:
for user in users:
install_resource_to_user(user, file_contents=wgt_file)
for group in groups:
install_resource_to_group(group, file_contents=wgt_file)
if public:
install_resource_to_all_users(file_contents=wgt_file)
wgt_file.close()
f.close()
self.log(_('Successfully imported \"%(name)s\" from \"%(file_name)s\"') % {'name': template.get_resource_processed_info()['title'], 'file_name': file_name}, level=1)
except:
self.log(_('Failed to import the mashable application component from %(file_name)s') % {'file_name': file_name}, level=1)
示例9: add_packaged_resource
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
示例10: create_workspace
def create_workspace(owner, f=None, mashup=None, new_name=None, new_title=None, preferences={}, searchable=True, public=False):
from wirecloud.platform.workspace.mashupTemplateParser import buildWorkspaceFromTemplate
if mashup is not None and f is not None:
raise Exception
if f is not None:
wgt = f if isinstance(f, WgtFile) else WgtFile(f)
template = TemplateParser(wgt.get_template())
resource_info = template.get_resource_processed_info(process_urls=False)
if resource_info["type"] != 'mashup':
raise Exception
for embedded_resource in resource_info['embedded']:
if embedded_resource['src'].startswith('https://'):
resource_file = download_http_content(embedded_resource['src'])
else:
resource_file = BytesIO(wgt.read(embedded_resource['src']))
extra_resource_contents = WgtFile(resource_file)
install_resource_to_user(owner, file_contents=extra_resource_contents)
else:
values = mashup.split('/', 3)
if len(values) != 3:
raise TypeError(_('invalid mashup id'))
(mashup_vendor, mashup_name, mashup_version) = values
try:
resource = CatalogueResource.objects.get(vendor=mashup_vendor, short_name=mashup_name, version=mashup_version)
if not resource.is_available_for(owner) or resource.resource_type() != 'mashup':
raise CatalogueResource.DoesNotExist
except CatalogueResource.DoesNotExist:
raise Exception(_('Mashup not found: %(mashup)s') % {'mashup': mashup})
base_dir = catalogue.wgt_deployer.get_base_dir(mashup_vendor, mashup_name, mashup_version)
wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
template = TemplateParser(wgt_file.get_template())
workspace, _foo = buildWorkspaceFromTemplate(template, owner, new_name=new_name, new_title=new_title, searchable=searchable, public=public)
if len(preferences) > 0:
update_workspace_preferences(workspace, preferences, invalidate_cache=False)
return workspace
示例11: wrapper
def wrapper(self, *args, **kwargs):
owner_user = User.objects.get(username=owner)
if shared:
base = self.shared_test_data_dir
else:
base = self.test_data_dir
wgt_file = open(os.path.join(base, file_name), "rb")
wgt = WgtFile(wgt_file)
template = wgt.get_template()
buildWorkspaceFromTemplate(template, owner_user)
wgt_file.close()
return test_func(self, *args, **kwargs)
示例12: add_widget_from_wgt
def add_widget_from_wgt(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)
if template.get_resource_type() == 'widget':
resource_info = template.get_resource_info()
code_url = resource_info['code_url']
if not code_url.startswith(('http://', 'https://')):
code = wgt_file.read(code_url)
try:
unicode(code, resource_info['code_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['code_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:
return add_resource_from_template(file_name, template, user, fromWGT=True, overrides=overrides)
示例13: update_resource_catalogue_cache
def update_resource_catalogue_cache(orm=None):
if orm is not None:
resources = orm.CatalogueResource.objects.all()
else:
resources = CatalogueResource.objects.all()
resources_to_remove = []
for resource in resources:
try:
if getattr(resource, "fromWGT", True):
base_dir = wgt_deployer.get_base_dir(resource.vendor, resource.short_name, resource.version)
wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
template = wgt_file.get_template()
wgt_file.close()
else:
# fromWGT attribute support was removed from Wirecloud in version 0.7.0
template = download_http_content(resource.template_uri)
template_parser = TemplateParser(template)
resource.json_description = json.dumps(template_parser.get_resource_info())
resource.save()
except (IOError, TemplateParseException) as e:
if isinstance(e, IOError) and e.errno != errno.ENOENT:
raise e
resources_to_remove.append(resource)
if (
len(resources_to_remove) > 0
and getattr(settings, "WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION", False) is False
):
raise Exception(
"There are some mashable application components that are not supported anymore (use WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION for removing automatically them in the migration process"
)
for resource in resources_to_remove:
print(" Removing %s" % (resource.vendor + "/" + resource.short_name + "/" + resource.version))
resource.delete()
示例14: handle
def handle(self, *args, **options):
if len(args) < 1:
raise CommandError(_('Wrong number of arguments'))
user = None
if not options['deploy_only']:
user = User.objects.get(pk=1)
for file_name in args:
try:
f = open(file_name, 'rb')
wgt_file = WgtFile(f)
except:
print _('Failed to read from %(file_name)s') % {'file_name': file_name}
continue
try:
template_contents = wgt_file.get_template()
template = TemplateParser(template_contents)
try:
add_widget_from_wgt(f, user, wgt_file=wgt_file, template=template, deploy_only=options['deploy_only'])
except IntegrityError:
if not options['reinstall']:
raise
else:
old_resource = CatalogueResource.objects.get(vendor=template.get_resource_vendor(),
short_name=template.get_resource_name(),
version=template.get_resource_version()
)
delete_resource(old_resource, user)
add_widget_from_wgt(f, user, wgt_file=wgt_file, template=template)
wgt_file.close()
f.close()
print _('Successfully imported %(name)s widget') % {'name': template.get_resource_name()}
except IntegrityError:
print _('Version %(version)s of the %(name)s widget (from %(vendor)s) already exists') % {
'name': template.get_resource_name(),
'version': template.get_resource_version(),
'vendor': template.get_resource_vendor(),
}
except:
print _('Failed to import widget from %(file_name)s') % {'file_name': file_name}
示例15: handle
def handle(self, *args, **options):
if len(args) < 1:
raise CommandError(_('Wrong number of arguments'))
if not options['deploy_only']:
user = User.objects.get(pk=1)
else:
user = FakeUser('admin')
for file_name in args:
try:
wgt_file = WgtFile(file_name)
except:
print _('Failed to parse %(file_name)s') % {'file_name': file_name}
continue
try:
template_contents = wgt_file.get_template()
template = TemplateParser(template_contents)
try:
create_widget_from_wgt(wgt_file, user, deploy_only=options['deploy_only'])
except IntegrityError:
if not options['reinstall']:
raise
else:
deleteWidget(user, template.get_resource_name(),
template.get_resource_vendor(),
template.get_resource_version()
)
create_widget_from_wgt(wgt_file, user)
wgt_file.close()
print _('Successfully imported %(name)s widget') % {'name': template.get_resource_name()}
except IntegrityError:
print _('Version %(version)s of the %(name)s widget (from %(vendor)s) already exists') % {
'name': template.get_resource_name(),
'version': template.get_resource_version(),
'vendor': template.get_resource_vendor(),
}
except:
print _('Failed to import widget from %(file_name)s') % {'file_name': file_name}