def __init__(self): self.serializer = ZUsagePacketSerializer() self.logger = None self.running = False self.version = ZVersion() self.profileGuid = getApplicationModel().getUserProfile().getGuid() self.usageDir = None
class ZUsageStatisticsService(IZService, IZDataStoreListener, IZMediaStorageServiceListener, IZAccountStoreListener, IZRunnable): def __init__(self): self.serializer = ZUsagePacketSerializer() self.logger = None self.running = False self.version = ZVersion() self.profileGuid = getApplicationModel().getUserProfile().getGuid() self.usageDir = None # end __init__() def start(self, applicationModel): self.queue = Queue(0) userProfile = applicationModel.getUserProfile() self.usageDir = userProfile.getDirectory(u"usage") #$NON-NLS-1$ engine = applicationModel.getEngine() self.logger = engine.getService(IZBlogAppServiceIDs.LOGGER_SERVICE_ID) self.logger.debug(u"Anonymous Usage Statistics Service started.") #$NON-NLS-1$ accountStore = engine.getService(IZBlogAppServiceIDs.ACCOUNT_STORE_SERVICE_ID) dataStore = engine.getService(IZBlogAppServiceIDs.DATA_STORE_SERVICE_ID) mediaStoreService = engine.getService(IZBlogAppServiceIDs.MEDIA_STORAGE_SERVICE_ID) accountStore.addListener(self) dataStore.addListener(self) mediaStoreService.addListener(self) self.done = False self.running = True thread = ZThread(self, u"ZUsageStatisticsService", True) #$NON-NLS-1$ thread.start() # end start() def stop(self): self.logger = None packet = self._createPacket(IZUsagePacketTypes.EXIT_SERVICE) self.queue.put_nowait(packet) while self.running: pass # end stop() def run(self): done = False while not done: packet = self.queue.get() if packet.getType() == IZUsagePacketTypes.EXIT_SERVICE: done = True else: self._savePacket(packet) self.running = False # end run() def _savePacket(self, packet): packetDom = self.serializer.serialize(packet) fname = os.path.join(self.usageDir, packet.getId() + u".xml") #$NON-NLS-1$ packetDom.save(fname, True) # end _savePacket() # # def onDocumentAdded(self, document): # self.logger.debug(u"Document added.") # # end onDocumentAdded() # # def onDocumentChanged(self, document, metaDataOnly): # self.logger.debug(u"Document changed.") # # end onDocumentChange() # # def onDocumentDeleted(self, document): # self.logger.debug(u"Document deleted.") # # end onDocumentDeleted() def onMediaStorageAdded(self, mediaStore): packet = self._createPacket(IZUsagePacketTypes.NEW_MEDIA_STORE) packet.addAttribute(u"mediastorage.site-id", mediaStore.getMediaSiteId()) #$NON-NLS-1$ packet.addAttribute(u"mediastorage.id-hash", hash(mediaStore.getId())) #$NON-NLS-1$ self.queue.put_nowait(packet) # end onMediaStorageAdded() def onMediaStorageRemoved(self, mediaStore): packet = self._createPacket(IZUsagePacketTypes.DELETE_MEDIA_STORE) packet.addAttribute(u"mediastorage.site-id", mediaStore.getMediaSiteId()) #$NON-NLS-1$ packet.addAttribute(u"mediastorage.id-hash", hash(mediaStore.getId())) #$NON-NLS-1$ self.queue.put_nowait(packet) # end onMediaStorageRemoved() #$NON-NLS-1$ def onAccountAdded(self, account): packet = self._createPacket(IZUsagePacketTypes.NEW_ACCOUNT) if isinstance(account, IZBlogAccount): apiInfo = account.getAPIInfo() packet.addAttribute(u"account.type", apiInfo.getType()) #$NON-NLS-1$ packet.addAttribute(u"account.id-hash", unicode(hash(account.getId()))) #$NON-NLS-1$ self.queue.put_nowait(packet) # end onAccountAdded() def onAccountChanged(self, account): #@UnusedVariable pass # end onAccountChange() def onAccountDeleted(self, account): packet = self._createPacket(IZUsagePacketTypes.NEW_ACCOUNT) if isinstance(account, IZBlogAccount): apiInfo = account.getAPIInfo() packet.addAttribute(u"account.type", apiInfo.getType()) #$NON-NLS-1$ packet.addAttribute(u"account.id-hash", unicode(hash(account.getId()))) #$NON-NLS-1$ self.queue.put_nowait(packet) # end onAccountDeleted() def _createPacket(self, type): packet = ZUsagePacket(type) packet.addAttribute(u"global.app-version", self.version.getFullVersionString()) #$NON-NLS-1$ packet.addAttribute(u"global.profile-guid", self.profileGuid) #$NON-NLS-1$ return packet
def __init__(self): ZApplicationModel.__init__(self) self.version = ZVersion() self.viewRegistry = ZViewRegistry()