def processCapability(self, attrs): name = None value = None if (attrs.has_key('name')): name = attrs.get('name') if (attrs.has_key('value')): value = attrs.get('value') if (not name or not value): raise TemplateParseException( _("ERROR: missing attribute at Capability element")) if (not self._gadget): raise TemplateParseException( _("ERROR: capabilities must be placed AFTER Resource definition!" )) if (self.save): capability = Capability(name=name, value=value, resource=self._gadget) capability.save() if (capability.name.lower() == 'contratable'): self._contratable = True
def processWire(self, attrs, wire): _friendCode = '' _wiring = '' if (attrs.has_key('friendcode') == True): _friendCode = attrs.get('friendcode') if (attrs.has_key('type') == False or attrs.has_key('name') == False): raise TemplateParseException( _("ERROR: missing attribute at Event or Slot element")) if (wire == 'Slot'): _wiring = 'in' if (wire == 'Event'): _wiring = 'out' if (_friendCode != '' and wire != ''): wiring = GadgetWiring(friendcode=_friendCode, wiring=_wiring, idResource_id=get_object_or_404( GadgetResource, short_name=self._name, vendor=self._vendor, version=self._version).id) wiring.save() else: raise TemplateParseException( _("ERROR: missing attribute at Event or Slot element"))
def processProperty(self, attrs): _name = '' _type = '' _description = '' if (attrs.has_key('name')): _name = attrs.get('name') if (attrs.has_key('type')): _type = attrs.get('type') if (attrs.has_key('description')): _description = attrs.get('description') if (_name != '' and _type != ''): vDef = VariableDef(name=_name, description=_description, type=self.typeText2typeCode(_type), aspect='PROP', friend_code=None, gadget=self._gadget) #vDef.save() relationship_eltos = {} relationship_eltos['vdef'] = vDef relationship_eltos['context'] = None relationship_eltos['option'] = [] self._relationships.append(relationship_eltos) else: raise TemplateParseException( _(u"ERROR: missing attribute at Property element"))
def processMsg(self, attrs): try: self.current_text = attrs['name'] except KeyError: raise TemplateParseException( _("ERROR: missing the language attribute at Translation element" ))
def processOption(self, attrs): _value = "" _name = "" if (attrs.has_key('name')): # backward compatibility _name = attrs.get('name') elif (attrs.has_key('label')): _name = attrs.get('label') if (attrs.has_key('value')): _value = attrs.get('value') if (_value != "") and (_name != "") and (self._lastPreference['vdef'].type == self.typeText2typeCode("list")): option = UserPrefOption(value=_value, name=_name, variableDef=self._lastPreference['vdef']) index = self.addIndex(_name) if index: self.addTranslation(index, option) self._lastPreference['option'].append({ "option": option, "index": index }) else: raise TemplateParseException( _("ERROR: missing attribute at Option element"))
def processTranslations(self, attrs): if (attrs.has_key('default')): self.default_lang = attrs.get('default') else: raise TemplateParseException( _("ERROR: missing the 'default' attribute at Translations element" ))
def processTranslations(self, attrs): try: self.default_lang = attrs['default'] except KeyError: raise TemplateParseException( _("ERROR: missing the 'default' attribute at Translations element" ))
def processExternalContext(self, attrs): _name = attrs.get('name', '') _type = attrs.get('type', '') _concept = attrs.get('concept', '') _description = attrs.get('description', '') if (_name != '' and _type != '' and _concept != ''): vDef = VariableDef(name=_name, description=_description, type=self.typeText2typeCode(_type), aspect='ECTX', friend_code=None, gadget=self._gadget) #vDef.save() context = ContextOption(concept=_concept, varDef=vDef) #context.save() relationship_eltos = {} relationship_eltos['vdef'] = vDef relationship_eltos['context'] = context relationship_eltos['option'] = [] relationship_eltos['trans'] = [] self._relationships.append(relationship_eltos) else: raise TemplateParseException( _("ERROR: missing attribute at External Context element"))
def processMsg(self, attrs): if (attrs.has_key('name')): self.current_text = attrs.get('name') else: raise TemplateParseException( _("ERROR: missing the language attribute at Translation element" ))
def processProperty(self, attrs): _name = attrs.get('name', '') _type = attrs.get('type', '') _description = attrs.get('description', '') _default_value = attrs.get('default', None) if (_name != '' and _type != ''): #check if it's shared shared_concept = get_shared_var_def(attrs) vDef = VariableDef(name=_name, description=_description, type=self.typeText2typeCode(_type), aspect='PROP', friend_code=None, default_value=_default_value, gadget=self._gadget, shared_var_def=shared_concept) #vDef.save() relationship_eltos = {} relationship_eltos['vdef'] = vDef relationship_eltos['context'] = None relationship_eltos['option'] = [] relationship_eltos['trans'] = [] self._relationships.append(relationship_eltos) else: raise TemplateParseException( _(u"ERROR: missing attribute at Property element"))
def processTranslation(self, attrs): try: self.current_lang = attrs['lang'] self.lang_list.append(self.current_lang) except KeyError: raise TemplateParseException( _("ERROR: missing the language attribute at Translation element" ))
def processTranslation(self, attrs): if (attrs.has_key('lang')): self.current_lang = attrs.get('lang') self.lang_list.append(self.current_lang) else: raise TemplateParseException( _("ERROR: missing the language attribute at Translation element" ))
def processCapability(self, attrs): name = attrs.get('name', None) value = attrs.get('value', None) if (not name or not value): raise TemplateParseException( _("ERROR: missing attribute at Capability element")) if (not self._gadget): raise TemplateParseException( _("ERROR: capabilities must be placed AFTER Resource definition!" )) self._capabilities.append( Capability(name=name.lower(), value=value.lower(), gadget=self._gadget))
def endElement(self, name): if (name == 'Name'): self._name = self._accumulator[0] return if (name == 'Vendor'): self._vendor = self._accumulator[0] return if (name == 'Version'): self._version = self._accumulator[0] return if (name == 'Author'): self._author = self._accumulator[0] return if (name == 'Description'): self._description = self._accumulator[0] return if (name == 'Mail'): self._mail = self._accumulator[0] return if (name == 'ImageURI'): if (self._accumulator == []): self._imageURI = 'no_url' else: self._imageURI = self._accumulator[0] return if (name == 'WikiURI'): self._wikiURI = self._accumulator[0] return if (self._name != '' and self._vendor != '' and self._version != '' and self._author != '' and self._description != '' and self._mail != '' and self._imageURI != '' and self._wikiURI != '' and not self._gadget_added): gadget = GadgetResource() gadget.short_name = self._name gadget.vendor = self._vendor gadget.added_by_user = self._user gadget.version = self._version gadget.author = self._author gadget.description = self._description gadget.mail = self._mail gadget.image_uri = self._imageURI gadget.wiki_page_uri = self._wikiURI gadget.template_uri = self._uri gadget.creation_date = datetime.today() gadget.popularity = 0.0 gadget.save() self._gadget_added = True elif (self._gadget_added): return else: raise TemplateParseException( _("ERROR: missing Resource description field at Resource element! Check schema!" ))
def processPreference(self, attrs): _name = '' _type = '' _description = '' _label = '' _default_value = '' if (attrs.has_key('name')): _name = attrs.get('name') if (attrs.has_key('type')): _type = attrs.get('type') if (attrs.has_key('description')): _description = attrs.get('description') if (attrs.has_key('label')): _label = attrs.get('label') if (attrs.has_key('default')): _default_value = attrs.get('default') if (_name != '' and _type != '' and _description != '' and _label != ''): vDef = VariableDef(name=_name, description=_description, type=self.typeText2typeCode(_type), aspect='PREF', friend_code=None, label=_label, default_value=_default_value, gadget=self._gadget) #vDef.save() relationship_eltos = {} relationship_eltos['vdef'] = vDef relationship_eltos['context'] = None relationship_eltos['option'] = [] relationship_eltos['trans'] = [] index = self.addIndex(_description) if index: self.addTranslation(index, vDef) relationship_eltos['trans'].append(index) index = self.addIndex(_label) if index: self.addTranslation(index, vDef) relationship_eltos['trans'].append(index) self._relationships.append(relationship_eltos) self._lastPreference = relationship_eltos else: raise TemplateParseException( _("ERROR: missing attribute at UserPreference element"))
def processRendering(self, attrs): _width = attrs.get('width', '') _height = attrs.get('height', '') if (_width != "" and _height != ""): self._gadgetWidth = _width self._gadgetHeight = _height else: raise TemplateParseException( _("ERROR: missing attribute at Rendering element"))
def endElement(self, name): if (name == 'Name'): self._name = self._accumulator[0] return if (name == 'Vendor'): self._vendor = self._accumulator[0] return if (name == 'Version'): self._version = self._accumulator[0] return if (name == 'Author'): self._author = self._accumulator[0] return if (name == 'Description'): self._description = self._accumulator[0] return if (name == 'Mail'): self._mail = self._accumulator[0] return if (name == 'ImageURI'): self._imageURI = self._accumulator[0] return if (name == 'WikiURI'): self._wikiURI = self._accumulator[0] return if (self._name != '' and self._vendor != '' and self._version != '' and self._author != '' and self._description != '' and self._mail != '' and self._imageURI != '' and self._wikiURI != '' and self._flag == ''): gadget = GadgetResource() gadget.short_name = self._name gadget.vendor = self._vendor gadget.added_by_user_id = get_object_or_404(User, username=self._user).id gadget.version = self._version gadget.author = self._author gadget.description = self._description gadget.mail = self._mail gadget.image_uri = self._imageURI gadget.wiki_page_uri = self._wikiURI gadget.template_uri = self._uri gadget.creation_date = datetime.today() gadget.save() self._flag = 'add' elif (self._flag == 'add'): return else: raise TemplateParseException( _("ERROR: Missing Resource describing info at Resource element! See schema!" ))
def parse_gadget_code(main_uri, code_uri, gadget_uri, content_type, from_wgt, cacheable=True, user=None): code = "" url = urlparse.urlparse(code_uri) if url.scheme == 'file': raise TemplateParseException(_('Invalid URL scheme: file')) if from_wgt: local_path = get_wgt_local_path(code_uri) if not os.path.isfile(local_path): raise TemplateParseException( _("'%(file)s' is not a file") % {'file': local_path}) f = open(local_path, 'r') code = f.read() f.close() else: if url.scheme == '': fetch_uri = urlparse.urljoin(main_uri, code_uri) else: fetch_uri = code_uri try: code = http_utils.download_http_content(fetch_uri, user=user) except HTTPError, e: msg = _("Error opening URL: code %(errorCode)s(%(errorMsg)s)") % { 'errorCode': e.code, 'errorMsg': e.msg, } raise TemplateParseException(msg) except URLError, e: msg = _("Error opening URL: %(errorMsg)s") % {'errorMsg': e.reason} raise TemplateParseException(msg)
def processMashupResource(self, attrs): if (attrs.has_key('vendor') == True and attrs.has_key('name') == True and attrs.has_key('version') == True): resource_id = get_object_or_404(GadgetResource, short_name=attrs.get('name'), vendor=attrs.get('vendor'), version=attrs.get('version')).id self._includedResources.append(resource_id) else: raise TemplateParseException( _("ERROR: missing attribute at Resource"))
def typeText2typeCode(self, typeText): if typeText == 'text': return 'S' elif typeText == 'number': return 'N' elif typeText == 'date': return 'D' elif typeText == 'boolean': return 'B' elif typeText == 'list': return 'L' elif typeText == 'password': return 'P' else: raise TemplateParseException( _(u"ERROR: unkown TEXT TYPE ") + typeText)
def endDocument(self): emptyRequiredFields = [] if self._gadgetName == "": emptyRequiredFields.append("name") if self._gadgetVendor == "": emptyRequiredFields.append("vendor") if self._gadgetVersion == "": emptyRequiredFields.append("version") if len(emptyRequiredFields) > 0: print emptyRequiredFields raise TemplateParseException( _("Missing required field(s): %(fields)s") % {fields: unicode(emptyRequiredFields)})
def processSlot(self, attrs): _name = attrs.get('name', '') _type = attrs.get('type', '') _description = attrs.get('description', '') _label = attrs.get('label', '') _friendCode = attrs.get('friendcode', '') _action_label = attrs.get('actionlabel', '') if (_name != '' and _type != '' and _friendCode != ''): vDef = VariableDef(name=_name, description=_description, type=self.typeText2typeCode(_type), aspect=self._SLOT, friend_code=_friendCode, label=_label, action_label=_action_label, gadget=self._gadget) #vDef.save() relationship_eltos = {} relationship_eltos['vdef'] = vDef relationship_eltos['context'] = None relationship_eltos['option'] = [] relationship_eltos['trans'] = [] index = self.addIndex(_description) if index: self.addTranslation(index, vDef) relationship_eltos['trans'].append(index) index = self.addIndex(_label) if index: self.addTranslation(index, vDef) relationship_eltos['trans'].append(index) index = self.addIndex(_action_label) if index: self.addTranslation(index, vDef) relationship_eltos['trans'].append(index) self._relationships.append(relationship_eltos) else: raise TemplateParseException( _("ERROR: missing attribute at Slot element"))
def parseUserEvents(self, codeURI, gadgetURI): xhtml = "" # TODO Fixme!! This works for now, but we have to check if a part of a url is empty address = codeURI.partition('://') query = address[2].partition('/') codeURI = address[0] + "://" + query[0] + "/" + urlquote(query[2]) try: xhtml = urlopen(codeURI).read() except Exception: raise TemplateParseException(_("XHTML code is not accessible")) self.xHTML = XHTML (uri=gadgetURI + "/xhtml", code=xhtml, url=codeURI) self.xHTML.save() #self.feed(xhtml) return
def parse(self, codeURI, gadgetURI, content_type): xhtml = "" # TODO Fixme!! This works for now, but we have to check if a part of a url is empty address = codeURI.split('://') query = address[1].split('/',1) codeURI = address[0] + "://" + query[0] + "/" + urlquote(query[1]) try: xhtml = download_http_content(codeURI) except Exception: raise TemplateParseException(_("XHTML code is not accessible")) uri = gadgetURI + "/xhtml" self.xHTML = XHTML (uri=uri, code=xhtml, url=codeURI, content_type=content_type) self.xHTML.save() return
def processXHTML(self, attrs): _href = "" if (attrs.has_key('href')): _href = attrs.get('href') if (_href != ""): try: # Gadget Code Parsing if environ.get("RUN_MAIN_DEV") == "true" and hasattr( settings, 'GADGETS_ROOT'): if path.isfile(path.join(settings.GADGETS_ROOT, _href)): _href = "file://%s" % path.join( settings.GADGETS_ROOT, _href) gadgetParser = GadgetCodeParser() gadgetParser.parse(_href, self._gadgetURI) self._xhtml = gadgetParser.getXHTML() except Exception, e: raise TemplateParseException( _("ERROR: XHTML could not be read") + " - " + unicode(e))
def processOption(self, attrs): _value = "" _name = "" if (attrs.has_key('name')): _name = attrs.get('name') if (attrs.has_key('value')): _value = attrs.get('value') if (_value != "") and (_name != "") and (self._lastPreference['vdef'].type == self.typeText2typeCode("list")): option = UserPrefOption(value=_value, name=_name, variableDef=self._lastPreference['vdef']) self._lastPreference['option'].append(option) else: raise TemplateParseException( _("ERROR: missing attribute at Option element"))
def processSlot(self, attrs): _name = '' _type = '' _description = '' _label = '' _friendCode = '' if (attrs.has_key('name')): _name = attrs.get('name') if (attrs.has_key('type')): _type = attrs.get('type') if (attrs.has_key('description')): _description = attrs.get('description') if (attrs.has_key('label')): _label = attrs.get('label') if (attrs.has_key('friendcode')): _friendCode = attrs.get('friendcode') if (_name != '' and _type != '' and _friendCode != ''): vDef = VariableDef(name=_name, description=_description, type=self.typeText2typeCode(_type), aspect=self._SLOT, friend_code=_friendCode, label=_label, gadget=self._gadget) #vDef.save() relationship_eltos = {} relationship_eltos['vdef'] = vDef relationship_eltos['context'] = None relationship_eltos['option'] = [] self._relationships.append(relationship_eltos) else: raise TemplateParseException( _("ERROR: missing attribute at Slot element"))
def processXHTML(self, attrs): _href = attrs.get('href', '').encode('utf8') _cacheable = attrs.get('cacheable', 'true').encode('utf8').lower() == 'true' _content_type = attrs.get('content-type', None) if _href != "": try: self._xhtml = parse_gadget_code(self.uri, _href, self._gadgetURI, _content_type, self.fromWGT, cacheable=_cacheable, user=self.user) except TemplateParseException: raise except Exception, e: raise TemplateParseException( _("ERROR: XHTML could not be read: %(errorMsg)s") % {'errorMsg': e.message})
def endDocument(self): emptyRequiredFields = [] if self._gadgetName == "": emptyRequiredFields.append("name") if self._gadgetVendor == "": emptyRequiredFields.append("vendor") if self._gadgetVersion == "": emptyRequiredFields.append("version") if self._gadgetAuthor == "": emptyRequiredFields.append("author") if self._gadgetMail == "": emptyRequiredFields.append("mail") if self._gadgetDesc == "": emptyRequiredFields.append("description") if self._gadgetWiki == "": emptyRequiredFields.append("wiki") if self._gadgetImage == "": emptyRequiredFields.append("image") if self._xhtml == "": emptyRequiredFields.append("xhtml") if len(emptyRequiredFields) > 0: print emptyRequiredFields raise TemplateParseException( _("Missing required field(s): %(fields)s") % {fields: unicode(emptyRequiredFields)}) # Check the default translation if len(self.lang_list) > 0 and not self.default_lang in self.lang_list: raise TemplateParseException( _("ERROR: There isn't a Translation element with the default language (" + self.default_lang + ") translations")) # Save the new gadget self._gadget.uri = self._gadgetURI self._gadget.vendor = self._gadgetVendor self._gadget.name = self._gadgetName self._gadget.display_name = self._gadgetDisplayName self._gadget.version = self._gadgetVersion self._gadget.xhtml = self._xhtml self._gadget.author = self._gadgetAuthor self._gadget.mail = self._gadgetMail self._gadget.wikiURI = self._gadgetWiki self._gadget.imageURI = self._gadgetImage self._gadget.iPhoneImageURI = self._gadgetIPhoneImage self._gadget.width = self._gadgetWidth self._gadget.height = self._gadgetHeight self._gadget.description = self._gadgetDesc self._gadget.menuColor = self._gadgetMenuColor self._gadget.save() # All relationship must be saved now, when all its data are known for rel in self._relationships: rel['vdef'].gadget = self._gadget rel['vdef'].save() #save translations for ind in rel['trans']: if self.translations.has_key(ind): for trans in self.translations[ind]["trans"]: self.createTranslation(trans, rel['vdef'].id) times = 1 if self.translations[ind].has_key("times"): times = self.translations[ind]["times"] if times == 1: del self.translations[ind] else: self.translations[ind]["times"] = times - 1 if rel['context']: rel['context'].varDef = rel['vdef'] rel['context'].save() for opt in rel['option']: opt["option"].variableDef = rel['vdef'] opt["option"].save() if opt["index"] and self.translations.has_key(opt["index"]): for trans in self.translations[opt["index"]]["trans"]: self.createTranslation(trans, opt["option"].id) times = 1 if self.translations[opt["index"]].has_key("times"): times = self.translations[opt["index"]]["times"] if times == 1: del self.translations[opt["index"]] else: self.translations[opt["index"]]["times"] = times - 1 # All capabilities for cap in self._capabilities: cap.gadget = self._gadget cap.save() # Gadget translations for index in self.translations: for trans in self.translations[index]["trans"]: self.createTranslation(trans, self._gadget.id)
def endElement(self, name): #add index to the translation list index = self.addIndex(self._accumulator) if index: self.addTranslation(index, self._gadget) if (name == 'Catalog.ResourceDescription'): self._gadgetURI = "/gadgets/" + self._gadgetVendor + "/" + self._gadgetName + "/" + self._gadgetVersion return if (name == 'Name'): if index: raise TemplateParseException( _("ERROR: The element Name cannot be translated")) self._gadgetName = self._accumulator return if (name == 'Version'): if index: raise TemplateParseException( _("ERROR: The element Version cannot be translated")) self._gadgetVersion = self._accumulator return if (name == 'Vendor'): if index: raise TemplateParseException( _("ERROR: The element Vendor cannot be translated")) self._gadgetVendor = self._accumulator return if (name == 'DisplayName'): self._gadgetDisplayName = self._accumulator return if (name == 'Author'): self._gadgetAuthor = self._accumulator return if (name == 'ImageURI'): self._gadgetImage = self._accumulator return if (name == 'iPhoneImageURI'): self._gadgetIPhoneImage = self._accumulator return if (name == 'WikiURI'): self._gadgetWiki = self._accumulator return if (name == 'Mail'): self._gadgetMail = self._accumulator if (name == 'Description'): self._gadgetDesc = self._accumulator return if (name == 'MenuColor'): self._gadgetMenuColor = self._accumulator return if (name == "Translation"): if self.current_lang == self.default_lang: for ind in self.translatable_list: self.translated_list.remove(ind) if len(self.translated_list) > 0: raise TemplateParseException( _("ERROR: the following translation indexes need a default value: " + str(self.translated_list))) if (name == "msg"): if not self.current_text in self.translatable_list: #message not used in the platform return if self.current_lang == self.default_lang: self.translated_list.append(self.current_text) # create the Translation (Gadget text) or add the language and value to the existing one (Variable or Option text) try: #existing Translation (VarDef or Option text) trans_list = self.translations[self.current_text]["trans"] if (type(trans_list) == type([])): #use the first Translation to get the table table_ = trans_list[0]["table"] else: #get the datum and create the list table_ = trans_list self.translations[self.current_text]["trans"] = [] trans = { "text_id": self.current_text, "table": table_, "language": self.current_lang, "value": self._accumulator, "default": (self.current_lang == self.default_lang) } except: #the text isn't in the translations dictionary return self.translations[self.current_text]["trans"].append(trans)