def servePage(self, view, url, params): if url == "/edit": selectedLang = params.get('lang', None) if selectedLang and len(selectedLang) == 2: Config.setProperty(Config.KEY_LANGUAGE, selectedLang) # I18nManager will be triggered here because it listens to the Config fsf = params.get('friendsseefriends', None) friendsseefriends = fsf is not None and len(fsf) > 0 Config.setProperty(Config.KEY_ALLOW_FRIENDS_TO_SEE_FRIENDS, friendsseefriends) slw = params.get('showlogwindow', None) showlogwindow = slw is not None and len(slw) > 0 Config.setProperty(Config.KEY_SHOW_LOG_WINDOW, showlogwindow) # If Config has changed, may need to update profile to include/hide friends info DbClient.updateContactList(friendsseefriends) # When friends are notified next time, the profile's hash will be calculated and sent afw = params.get('allowfriendrequests', None) allowfriendrequests = afw is not None and len(afw) > 0 Config.setProperty(Config.KEY_ALLOW_FRIEND_REQUESTS, allowfriendrequests) # Save config to file in case it's changed Config.save() contents = self.buildPage({'pageTitle' : I18nManager.getText("settings.title"), 'pageBody' : "<p>Settings changed... should I go back to settings or back to home now?</p>", 'pageFooter' : "<p>Footer</p>"}) view.setHtml(contents) else: pageProps = {"friendsseefriends" : "checked" if Config.getProperty(Config.KEY_ALLOW_FRIENDS_TO_SEE_FRIENDS) else "", "allowfriendrequests" : "checked" if Config.getProperty(Config.KEY_ALLOW_FRIEND_REQUESTS) else "", "showlogwindow" : "checked" if Config.getProperty(Config.KEY_SHOW_LOG_WINDOW) else "", "language_en":"", "language_de":""} pageProps["language_" + Config.getProperty(Config.KEY_LANGUAGE)] = "selected" #print("body:", self.formtemplate.getHtml(pageProps)) contents = self.buildPage({'pageTitle' : I18nManager.getText("settings.title"), 'pageBody' : self.formtemplate.getHtml(pageProps), 'pageFooter' : "<p>Footer</p>"}) view.setHtml(contents)
def dealWithUnencryptedMessage(message): '''Decide what to do with the given unencrypted message''' if message.messageType == Message.TYPE_CONTACT_REQUEST: print("Received a contact request from", message.senderId) # Check config to see whether we accept these or not if Config.getProperty(Config.KEY_ALLOW_FRIEND_REQUESTS) \ and MessageShuffler._isProfileStatusOk(message.senderId, [None, 'requested', 'untrusted', 'trusted']): # Call DbClient to store new message in inbox rowToStore = {"messageType":"contactrequest", "fromId":message.senderId, "fromName":message.senderName, "messageBody":message.message, "publicKey":message.publicKey, "timestamp":message.timestamp, "messageRead":False, "messageReplied":False} DbClient.addMessageToInbox(rowToStore) elif message.messageType == Message.TYPE_CONTACT_RESPONSE: print("It's an unencrypted contact response, so it must be a refusal") sender = DbClient.getProfile(message.senderId, False) if MessageShuffler._isProfileStatusOk(message.senderId, ['requested']): senderName = sender.get("displayName") if sender else "" ContactMaker.handleReceiveDeny(message.senderId) # Call DbClient to store new message in inbox rowToStore = {"messageType":"contactresponse", "fromId":message.senderId, "fromName":senderName, "messageBody":"", "accepted":False, "messageRead":False, "messageReplied":False, "timestamp":message.timestamp, "recipients":MessageShuffler.getOwnTorId()} DbClient.addMessageToInbox(rowToStore) else: print("Hä? It's unencrypted but the message type is", message.messageType)
def main(): collectionName = "job_lang_top_corps" infoCollectionName = "jobinfo_lang_top_corps" dbClient = DbClient('localhost', 27017, "jobaly") collection = dbClient.getCollection(collectionName) infoCollection = dbClient.getCollection(infoCollectionName) getter = IndeedPageGetter(infoCollection) pageSize = 10 pageNo = 149 has_more = True pageNum = 10000 find_sort = None find_spec = None while has_more and pageNo <= pageNum: page = dbClient.getPage(collection, find_spec, find_sort, pageSize, pageNo) getter.processPage(page, pageNo) pageNo += 1 count = page.count(with_limit_and_skip=True) # print "count=",count if (count < pageSize): has_more = False
def __init__(self, window, size, options): """ Initialise and start the snackspace application """ self.inittime = int(time.time()) self.options = options self.input_handler = InputHandler() self.task_handler = TaskHandler(self) self.task_handler.add_function(self.rfid_task, 500, True) self.logger = logging.getLogger("snackspace") self.rfid = RFIDReader(self.options.rfid_port) self.is_fullscreen = True self.cursor_visible = False self.window_size = size self.screen_manager = ScreenManager(self, window, size) self.user = None self.products = [] self.reply_queue = Queue.Queue() self.dbaccess = DbClient(self.options.hostip, self.task_handler, self.db_state_callback) self.dbaccess.daemon = True self.dbaccess.start()
def getByCities(): cities = [ "Austin, TX", "San Jose, CA", "Portland, OR", " New York, NY", "Houston, TX", "Boston, MA", "Davis, CA", "Palo Alto, CA", " Irvine, CA", "Olathe, KS", "Columbia, MD", " Atlanta, GA", ] param = {"q": "software engineer", "fromage": "30"} collectionName = "job_se_10city" indeedClient = ApiClient(param) # client.getPage(0) dbClient = DbClient("localhost", 27017, "jobaly") collection = dbClient.getCollection(collectionName) for city in cities: print "-----prcoss city %s -------" % city indeedClient.processQuery(collection, "l", city)
def main(): cities = [ 'MoutainView, CA', 'Seattle, WA', 'San Diego, CA', 'San Francisco, CA', 'Austin, TX', 'San Jose, CA', 'Portland, OR', ' New York, NY', 'Houston, TX', 'Boston, MA', 'Davis, CA', 'Palo Alto, CA', ' Irvine, CA', 'Olathe, KS', 'Columbia, MD', ' Atlanta, GA' ] cities = [ 'Austin, TX', 'San Jose, CA', 'Portland, OR', ' New York, NY', 'Houston, TX', 'Boston, MA', 'Davis, CA', 'Palo Alto, CA', ' Irvine, CA', 'Olathe, KS', 'Columbia, MD', ' Atlanta, GA' ] _pageSize = 25 _fromage = 30 _location = 94040 _radius = 25 _query = "software engineer" collectionName = "job_se_10city" indeedClient = ApiClient(_query, _pageSize, _fromage, _location, _radius) # client.getPage(0) dbClient = DbClient('localhost', 27017, "jobaly") collection = dbClient.getCollection(collectionName) for city in cities: print "-----prcoss city %s -------" % city indeedClient.processCity(collection, city)
def _createSubpayload(self): '''Use the stored fields to pack the payload contents together''' if self.profileHash is None or self.profileHash == "": self.profileHash = DbClient.calculateHash(DbClient.getProfile()) return self.packBytesTogether([ self.encodeNumberToBytes(1 if self.online else 0, 1), self.encodeNumberToBytes(1 if self.ping else 0, 1), self.profileHash])
def handleInitiate(torId, displayName): '''We have requested contact with another id, so we can set up this new contact's name with a status of "requested"''' # TODO: If row already exists then get status (and name/displayname) and error with it # Add new row in db with id, name and "requested" if torId and torId != DbClient.getOwnTorId(): DbClient.updateContact(torId, {'displayName':displayName, 'name':displayName, 'status':'requested'})
def main(): df = pd.read_csv(RESULTS_CALENDER_PATH, skiprows=1, names=RESULTS_CALENDER_COLUMNS) dbClient = DbClient() for i, record in enumerate(yaml.safe_load(df.to_json(orient='records'))): record['result_date'] = datetime.datetime.strptime( record['result_date'], "%d %b %Y").strftime('%Y-%m-%d') dbClient.update_results_calender(record)
def keyFingerprintChecked(torId): '''The fingerprint of this contact's public key has been checked (over a separate channel)''' # Check that userid exists and that status is currently "untrusted" (trusted also doesn't hurt) profile = DbClient.getProfile(torId, False) if profile and profile.get("status", "nostatus") in ["untrusted", "trusted"]: # Update the user's status to trusted DbClient.updateContact(torId, {"status" : "trusted"}) # Trigger a StatusNotify to tell them we're online notify = StatusNotifyMessage(online=True, ping=True, profileHash=None) notify.recipients = [torId] DbClient.addMessageToOutbox(notify)
def broadcastOnlineStatus(self): '''Queue a status notification message for each of our trusted contacts''' print("Outgoing postman is broadcasting the status...") self._broadcasting = True profileList = DbClient.getContactList("trusted") if profileList: msg = StatusNotifyMessage(online=True, ping=True, profileHash=None) msg.recipients = [c['torid'] for c in profileList] DbClient.addMessageToOutbox(msg) self._broadcasting = False self.flushSignal.emit()
def checkAllContactsKeys(): for c in DbClient.getMessageableContacts(): torId = c.get("torid", None) if c else None if torId: keyId = c.get("keyid", None) if not keyId: print("No keyid found for torid", torId) elif not CryptoClient.getPublicKey(keyId): print("CryptoClient hasn't got a public key for torid", torId) if not keyId or not CryptoClient.getPublicKey(keyId): # We haven't got their key in our keyring! DbClient.updateContact(torId, {"status":"requested"})
def _createSubpayload(self): '''Use the stored fields to pack the payload contents together''' if self.infoType is None: self.infoType = InfoRequestMessage.INFO_PROFILE if self.profileString is None: self.profileString = dbutils.getOwnProfileAsString() self.profileHash = DbClient.calculateHash(DbClient.getProfile()) return self.packBytesTogether([ self.encodeNumberToBytes(self.infoType), self.encodeNumberToBytes(len(self.profileString), 4), self.profileString, self.encodeNumberToBytes(len(self.profileHash), 4), self.profileHash])
def finish(self): '''Finished the key gen''' # Store key, name in mongo under own profile selectedKey = self.privateKeys[self.keypairListWidget.currentRow()] ownid = TorClient.getOwnId() # See if a name was entered before, if so use that myname = self.keygenParamBoxes['name'].text() if not myname: # Extract the name from the string which comes back from the key as "'Some Name (no comment) <*****@*****.**>'" myname = self.extractName(selectedKey['uids']) profile = {"name" : myname, "keyid" : selectedKey['keyid'], "torid" : ownid, "status" : "self", "ownprofile" : True} DbClient.updateContact(ownid, profile) return True
def generateListPage(self, doEdit=False, userid=None, extraParams=None): self.requirePageResources(['avatar-none.jpg', 'status-self.png', 'status-requested.png', 'status-untrusted.png', 'status-trusted.png']) # List of contacts, and show details for the selected one (or self if userid=None) selectedprofile = DbClient.getProfile(userid) if selectedprofile is None: selectedprofile = DbClient.getProfile() userid = selectedprofile['torid'] ownPage = userid == DbClient.getOwnTorId() # Build list of contacts userboxes = [] for p in DbClient.getContactList(): box = Bean() box.dispName = p['displayName'] box.torid = p['torid'] box.tilestyle = "contacttile" + ("selected" if p['torid'] == userid else "") box.status = p['status'] box.isonline = Contacts.isOnline(box.torid) userboxes.append(box) # expand templates using current details lefttext = self.listtemplate.getHtml({'webcachedir' : Config.getWebCacheDir(), 'contacts' : userboxes}) pageProps = {"webcachedir" : Config.getWebCacheDir(), 'person':selectedprofile} # Add extra parameters if necessary if extraParams: pageProps.update(extraParams) # See which contacts we have in common with this person (sharedContactIds, possIdsForThem, possIdsForMe, nameMap) = ContactMaker.getSharedAndPossibleContacts(userid) sharedContacts = self._makeIdAndNameBeanList(sharedContactIds, nameMap) pageProps.update({"sharedcontacts" : sharedContacts}) possibleContacts = self._makeIdAndNameBeanList(possIdsForThem, nameMap) pageProps.update({"possiblecontactsforthem" : possibleContacts}) possibleContacts = self._makeIdAndNameBeanList(possIdsForMe, nameMap) pageProps.update({"possiblecontactsforme" : possibleContacts}) # Which template to use depends on whether we're just showing or also editing if doEdit: # Use two different details templates, one for self and one for others detailstemplate = self.editowndetailstemplate if ownPage else self.editdetailstemplate righttext = detailstemplate.getHtml(pageProps) else: detailstemplate = self.detailstemplate # just show righttext = detailstemplate.getHtml(pageProps) contents = self.buildTwoColumnPage({'pageTitle' : I18nManager.getText("contacts.title"), 'leftColumn' : lefttext, 'rightColumn' : righttext, 'pageFooter' : "<p>Footer</p>"}) return contents
def servePage(self, view, url, params): print("Special function:", url) if url == "/selectprofilepic": # Get home directory for file dialog homedir = os.path.expanduser("~/") fname = QtGui.QFileDialog.getOpenFileName(view, I18nManager.getText("gui.dialogtitle.openimage"), homedir, I18nManager.getText("gui.fileselection.filetypes.jpg")) if fname: view.page().mainFrame().evaluateJavaScript("updateProfilePic('" + fname + "');") elif url == "/friendstorm": if not DbClient.hasFriends(): view.page().mainFrame().evaluateJavaScript("window.alert('No friends :(');") return # Launch a storm self.bs = Brainstorm(I18nManager.getText("contacts.storm.title")) self.bs.show() storm = Storm() # Build up Nodes and Edges using our contact list and if possible our friends' contact lists myTorId = DbClient.getOwnTorId() friends = {} friendsOfFriends = {} for c in DbClient.getContactList(): #print("Contact:", c['torid'], "'", c['displayName'], "'") nodeid = storm.getUnusedNodeId() torid = c['torid'] friends[torid] = nodeid storm.addNode(Node(None, nodeid, c['displayName'])) friendsOfFriends[torid] = c.get('contactlist', "") for torid in friends: if torid != myTorId: storm.addEdge(friends[torid], friends[myTorId]) for torid in friendsOfFriends: if torid != myTorId: ffList = friendsOfFriends[torid] if ffList: for ff in ffList.split(","): if ff and len(ff) > 16: ffTorid = ff[:16] ffName = ff[16:] if ffTorid != myTorId: if not friends.get(ffTorid, None): # Friend's friend is not in the list yet - add it nodeid = storm.getUnusedNodeId() friends[ffTorid] = nodeid storm.addNode(Node(None, nodeid, ffName)) # Add edge from torid to ffTorid storm.addEdge(friends[torid], friends[ffTorid]) self.bs.setStorm(storm)
def generateFingerprintsPage(self, userid): '''Build the page for checking the fingerprints of the selected user''' # First, get the name of the user person = DbClient.getProfile(userid, False) dispName = person.get('displayName', '') fullName = person.get('name', '') if not dispName: dispName = fullName if dispName != fullName: fullName = dispName + " (" + fullName + ")" fc = self._makeFingerprintChecker(userid) # check it's ok to generate status = person.get('status', '') if not fc.valid \ or status not in ['untrusted', 'trusted']: print("Not generating fingerprints page because status is", status) return None # Get one set of words for us and three sets for them printsAlreadyChecked = (person.get('status', '') == "trusted") bodytext = self.fingerprintstemplate.getHtml( {"mywords":fc.getCodeWords(True, 0, "en"), "theirwords0":fc.getCodeWords(False, 0, "en"), "theirwords1":fc.getCodeWords(False, 1, "en"), "theirwords2":fc.getCodeWords(False, 2, "en"), "fullname":fullName, "shortname":dispName, "userid":userid, "alreadychecked":printsAlreadyChecked}) return self.buildPage({'pageTitle' : I18nManager.getText("contacts.title"), 'pageBody' : bodytext, 'pageFooter' : "<p>Footer</p>"})
def dealWithMessage(message): '''Examine the received message and decide what to do with it''' print("Hmm, the MessageShuffler has been given some kind of message") # We must be online if we've received a message Contacts.comeOnline(MessageShuffler.getOwnTorId()) if message.senderMustBeTrusted: sender = DbClient.getProfile(message.senderId, False) if not sender or sender['status'] != "trusted": return # throw message away if not message.isComplete(): print("A message of type", message.encryptionType, "was received but it's not complete - throwing away") return # throw message away # if it's not encrypted, it's for us -> save in inbox if message.encryptionType == Message.ENCTYPE_NONE: MessageShuffler.dealWithUnencryptedMessage(message) elif message.encryptionType == Message.ENCTYPE_SYMM: # if it's symmetric, forget it for now pass elif message.encryptionType == Message.ENCTYPE_ASYM: MessageShuffler.dealWithAsymmetricMessage(message) else: print("Hä? What kind of encryption type is that? ", message.encryptionType) # Log receipt of message (do we want to know about relays at all?) if message.encryptionType in [Message.ENCTYPE_NONE, Message.ENCTYPE_ASYM]: logMessage = "Message of type: %s received from %s" % (message.getMessageTypeKey(), message.senderId) MessageShuffler.getTannoy().shout(logMessage)
def getPublicKey(self, torid): '''Use the keyid stored in mongo, and get the corresponding public key from the Crypto module''' profile = DbClient.getProfile(torid) if profile is not None: keyid = profile.get('keyid', None) if keyid is not None: return CryptoClient.getPublicKey(keyid)
def main(): pageSize = 100 startPageNo = 13 endPageNo = 10000 dbClient = DbClient('localhost', 27017, "SimilarQuestion") collection = dbClient.getCollection("question_test") questionGetter = QuestionGetter(pageSize,"python") for pg in range(startPageNo, endPageNo): print "--get page at : %d -----" % pg items = questionGetter.getPage(pg) if items == "NO_ITEMS": break print "--page at : %d have %d questions--" % (pg, len(items)) questionGetter.savePage(collection,items) time.sleep(10)
class DbClientTest(unittest.TestCase): def setUp(self): mongoConnection = mongomock.Connection() self.dbClient = DbClient(mongoConnection) self.dbClient._storeInGridFs = Mock() self.dbClient._storeInGridFs.return_value = [1,2,3] def tearDown(self): print('In tearDown()') def test_insert(self): cik, filingDate, filerName, formType, contentDict = 'testcik',sentinel.FilingDate, sentinel.Filer, sentinel.FormType, {} self.dbClient.insertFiling(cik, filingDate, filerName, formType, contentDict) result = self.dbClient.findByCik(cik) self.assertTrue(bool(result))
def _createSubpayload(self): '''Use the stored fields to pack the payload contents together''' if self.senderKey is None: self.senderKey = self.getOwnPublicKey() # Get own torid and name if not self.senderId: self.senderId = DbClient.getOwnTorId() if not self.senderName: self.senderName = DbClient.getProfile(None).get('name', self.senderId) if not self.introMessage: self.introMessage = "" nameAsBytes = self.senderName.encode('utf-8') messageAsBytes = self.introMessage.encode('utf-8') print("Packing contact request with senderId", self.senderId) return self.packBytesTogether([ self.senderId, self.encodeNumberToBytes(len(nameAsBytes), 4), nameAsBytes, self.encodeNumberToBytes(len(messageAsBytes), 4), messageAsBytes, self.senderKey])
def testProfileResponse(self): m = message.InfoResponseMessage(message.InfoRequestMessage.INFO_PROFILE) output = m.createUnencryptedOutput() bac = message.Message.MessageFromReceivedData(output, False) self.assertIsNotNone(bac, "couldn't decode the data") self.assertEqual(message.InfoRequestMessage.INFO_PROFILE, bac.infoType) mydescription = DbClient.getProfile()["description"] bacProfile = bac.profile self.assertEqual(mydescription, bacProfile["description"])
def getByCities(): cities = [ 'Austin, TX', 'San Jose, CA', 'Portland, OR', ' New York, NY', 'Houston, TX', 'Boston, MA', 'Davis, CA', 'Palo Alto, CA', ' Irvine, CA', 'Olathe, KS', 'Columbia, MD', ' Atlanta, GA' ] param = {"q": "software engineer", "fromage": "30"} collectionName = "job_se_10city" indeedClient = ApiClient(param) # client.getPage(0) dbClient = DbClient('localhost', 27017, "jobaly") collection = dbClient.getCollection(collectionName) for city in cities: print "-----prcoss city %s -------" % city indeedClient.processQuery(collection, "l", city)
def getSharedAndPossibleContacts(torid): '''Check which contacts we share with the given torid and which ones we could recommend to each other''' nameMap = {} ourContactIds = set() trustedContactIds = set() theirContactIds = set() # Get our id so we can exclude it from the sets myTorId = DbClient.getOwnTorId() if torid == myTorId: return (None, None, None, None) # Find the contacts of the specified person selectedProfile = DbClient.getProfile(torid, False) selectedContacts = selectedProfile.get('contactlist', None) if selectedProfile else None if selectedContacts: for s in selectedContacts.split(","): if s and len(s) >= 16: foundid = s[0:16] if foundid != myTorId: foundName = s[16:] theirContactIds.add(foundid) nameMap[foundid] = foundName foundTheirContacts = len(theirContactIds) > 0 # Now get information about our contacts for c in DbClient.getMessageableContacts(): foundid = c['torid'] ourContactIds.add(foundid) if c['status'] == 'trusted' and foundid != torid: trustedContactIds.add(foundid) nameMap[foundid] = c.get('displayName', c.get('name', None)) # Should we check the contact information too? if not foundTheirContacts: foundContacts = c.get('contactlist', None) if foundContacts: for s in foundContacts.split(","): if s[0:16] == torid: theirContactIds.add(foundid) # Now we have three sets of torids: our contacts, our trusted contacts, and their contacts. sharedContactIds = ourContactIds.intersection(theirContactIds) # might be empty suggestionsForThem = trustedContactIds.difference(theirContactIds) possibleForMe = theirContactIds.difference(ourContactIds) # Some or all of these sets may be empty, but we still return the map so we can look up names return (sharedContactIds, suggestionsForThem, possibleForMe, nameMap)
def getByCorps(): print " --- get job by companies---" collectionName = "job_se_top_corps" param = {"q": "software engineer", "fromage": "30"} indeedClient = ApiClient(param) # client.getPage(0) dbClient = DbClient("localhost", 27017, "jobaly") collection = dbClient.getCollection(collectionName) corps = [] fileName = "topcorps.txt" with open(fileName, "r") as the_file: for line in the_file: word = line.strip() if not len(word) == 0: corps.append(word) for corp in corps: q = indeedClient.buildQuery("software engineer", {"company": corp}) print "-----prcoss corp %s -------" % corp indeedClient.processQuery(collection, "q", q)
def main(): collectionName = "jobinfo_se_top_corps" dbClient = DbClient('localhost', 27017, "jobaly") collection = dbClient.getCollection(collectionName) pageSize = 100 pageNo = 1 has_more = True pageNum = 10000 find_sort = None find_spec=None while has_more and pageNo <= pageNum : page = dbClient.getPage(collection, find_spec,find_sort, pageSize, pageNo) processPage(collection, page,pageNo) pageNo+=1 count = page.count(with_limit_and_skip = True) # print "count=",count if ( count < pageSize ) : has_more = False
def main(): collectionName = "jobinfo_se_top_corps" dbClient = DbClient('localhost', 27017, "jobaly") pageProcessor = CollectionPageProcessor(dbClient, collectionName) # pageProcessor.process(printPageCount, pageNo = 1, pageNum=10 ) pageProcessor.process(addField, pageNo=1, pageNum=100, pageSize=100, find_sort=[("_id", 1)])
def getByCorps(): print " --- get job by companies---" collectionName = "job_se_top_corps" param = {"q": "software engineer", "fromage": "30"} indeedClient = ApiClient(param) # client.getPage(0) dbClient = DbClient('localhost', 27017, "jobaly") collection = dbClient.getCollection(collectionName) corps = [] fileName = "topcorps.txt" with open(fileName, 'r') as the_file: for line in the_file: word = line.strip() if not len(word) == 0: corps.append(word) for corp in corps: q = indeedClient.buildQuery("software engineer", {"company": corp}) print "-----prcoss corp %s -------" % corp indeedClient.processQuery(collection, "q", q)
def testProfileHash(self): # Clear out and reset own profile myTorId = "ABC123DEF456GH78" myprofile = {"name" : "Constantin Taylor", "keyid" : "someKeyId", "displayName" : "Me", "status" : "self", "ownprofile" : True, "description":"Some fairly descriptive text", "birthday":None, "interests":"chocolate pudding with fudge"} DbClient.updateContact(myTorId, myprofile) firstHash = DbClient.calculateHash(DbClient.getProfile()) self.assertEqual(firstHash, "12ae5c8dc8e1c2186b4ed4918040bb16", "First hash not what I was expecting") # Now change interests and check that hash changes DbClient.updateContact(myTorId, {"interests":"roasted vegetables and hummus"}) secondHash = DbClient.calculateHash(DbClient.getProfile()) self.assertNotEqual(firstHash, secondHash, "Profile hash should have changed")
def main(): pageSize = 100 startPageNo = 1 endPageNo = 10000 dbClient = DbClient('localhost', 27017, "SimilarQuestion") collection = dbClient.getCollection("english_questions") questionGetter = QuestionGetter(pageSize,"") for pg in range(startPageNo, endPageNo): print "--- get page %d ---" %pg items = questionGetter.getPage(pg) # print items if ( items == "NO_MORE" ) : print "have no more questions, quit program !!" break print "--- page %d has %d questions ---" %(pg,len(items)) if ( items != "NO_ITEMS" ) : i = questionGetter.savePage(collection,items) print "--- page %d has save %d question " %(pg,i)
def getByLang(): print " --- get job by language and companies---" collectionName = "job_lang_top_corps" param = { "q" : "software engineer", "fromage" : "30" } lang_names = utils.loadArrayFromFile("pro_langs.txt") corps_names = utils.loadArrayFromFile("topcorps.txt") indeedClient= ApiClient( param ) # client.getPage(0) dbClient = DbClient('localhost', 27017, "jobaly") collection = dbClient.getCollection(collectionName) for corp in corps_names: for lang in lang_names: q = indeedClient.buildQuery(lang, {"company": corp }) print "-----prcoss corp %s with language %s -------" % (corp, lang) indeedClient.processQuery(collection, "q", q)
def construct(payload, isEncrypted=True): '''Factory constructor using a given payload and extracting the fields''' if not payload: return None signatureKey = None if isEncrypted: # Decrypt the payload with our key decrypted, signatureKey = CryptoClient.decryptAndCheckSignature(payload) else: decrypted = payload if decrypted: print("Asymmetric message, length of decrypted is", len(decrypted)) else: print("Asymmetric message has no decrypted") # Separate fields of message into common ones and the type-specific payload msgType, subpayload, tstmp = AsymmetricMessage._stripFields(decrypted) print("Recovered timestamp='", tstmp, "' (", len(tstmp), ")") # Find a suitable subclass to call using the messageType msg = None if msgType == Message.TYPE_CONTACT_RESPONSE: msg = ContactResponseMessage.constructFrom(subpayload) elif msgType == Message.TYPE_STATUS_NOTIFY: msg = StatusNotifyMessage.constructFrom(subpayload) elif msgType == Message.TYPE_ASYM_MESSAGE: msg = RegularMessage.constructFrom(subpayload) elif msgType == Message.TYPE_INFO_REQUEST: msg = InfoRequestMessage.constructFrom(subpayload) elif msgType == Message.TYPE_INFO_RESPONSE: msg = InfoResponseMessage.constructFrom(subpayload) elif msgType == Message.TYPE_FRIEND_REFERRAL: msg = ContactReferralMessage.constructFrom(subpayload) # Ask the message if it's ok to have no signature if isEncrypted and msg: if msg.acceptUnrecognisedSignature(): # Save the encrypted contents so we can verify it later msg.encryptedContents = payload elif not signatureKey: msg = None if msg: try: msgTimestamp = tstmp.decode('utf-8') except: msgTimestamp = msg.makeCurrentTimestamp() msg.timestamp = Message.convertTimestampFromString(msgTimestamp) msg.signatureKeyId = signatureKey if signatureKey: print("Asymm setting senderId because I've got a signatureKey: '%s'" % signatureKey) signatureId = DbClient.findUserIdFromKeyId(signatureKey) if signatureId: msg.senderId = signatureId return msg
def servePage(self, view, url, params): DbClient.exportAvatars(Config.getWebCacheDir()) if url == "/send": print("send message of type '%(messageType)s' to id '%(sendTo)s'" % params) elif url.startswith("/delete/"): DbClient.deleteMessageFromInbox(params.get("msgId", "")) # Make dictionary to convert ids to names contactNames = {c['torid']:c['displayName'] for c in DbClient.getContactList()} unknownSender = I18nManager.getText("messages.sender.unknown") unknownRecpt = I18nManager.getText("messages.recpt.unknown") # Get contact requests, responses and mails from inbox conreqs = [] conresps = [] mails = [] for m in DbClient.getInboxMessages(): m['msgId'] = str(m.get("_id", "")) if m['messageType'] == "contactrequest": conreqs.append(m) elif m['messageType'] == "contactrefer": senderId = m.get('fromId', None) m['senderName'] = contactNames.get(senderId, unknownSender) conreqs.append(m) elif m['messageType'] == "contactresponse": if not m.get('accepted', False): m['messageBody'] = I18nManager.getText("messages.contactrequest.refused") m['fromName'] = DbClient.getProfile(m['fromId'], True).get("displayName") elif not m.get('messageBody', False): m['messageBody'] = I18nManager.getText("messages.contactrequest.accepted") conresps.append(m) else: senderId = m.get('fromId', None) if not senderId and m.get('signatureKeyId', None): senderId = DbClient.findUserIdFromKeyId(m['signatureKeyId']) m['senderName'] = contactNames.get(senderId, unknownSender) m['sentTimeStr'] = self.makeLocalTimeString(m['timestamp']) # Split m['recipients'] by commas, and look up each id with contactNames recpts = m.get('recipients', '') if recpts: m['recipients'] = ", ".join([contactNames.get(i, unknownRecpt) for i in recpts.split(",")]) else: m['recipients'] = unknownRecpt mails.append(m) bodytext = self.messagestemplate.getHtml({"contactrequests":conreqs, "contactresponses":conresps, "mails":mails, "nummessages":len(conreqs)+len(conresps)+len(mails), "webcachedir" : Config.getWebCacheDir()}) contents = self.buildPage({'pageTitle' : I18nManager.getText("messages.title"), 'pageBody' : bodytext, 'pageFooter' : "<p>Footer</p>"}) view.setHtml(contents)
def flushOutboxInSeparateThread(self): '''This can take quite a while to do the flush''' if self._flushing: return print("Outgoing postman is flushing the outbox...") self._flushing = True # Look in the outbox for messages messagesFound = 0 messagesSent = 0 failedRecpts = set() for m in DbClient.getOutboxMessages(): messagesFound += 1 # Get recipient, timestamp, relays, message message = m['message'] sendTimestamp = m.get('timestamp', None) # not used yet # TODO: if the timestamp is too old, then either just delete the message (if it's not to be queued) or move to inbox # Some messages have a single recipient (and maybe relays), others only have a recipientList recipient = m.get('recipient', None) if recipient: sendSuccess = self.RC_MESSAGE_FAILED if recipient in failedRecpts else self.sendMessage(message, recipient) if sendSuccess == self.RC_MESSAGE_IGNORED: print("Dealt with message so I should delete it from the db:", m["_id"]) DbClient.deleteMessageFromOutbox(m["_id"]) elif sendSuccess == self.RC_MESSAGE_SENT: print("Sent message so I should delete it from the db:", m["_id"]) DbClient.deleteMessageFromOutbox(m["_id"]) messagesSent += 1 testMsg = "Message sent, type was %s and recipient was %s" % (m.get("msgType", "unknown"), recipient) # TODO: Pass these values with the signal as an object, not a string self.emit(QtCore.SIGNAL("messageSent"), testMsg) elif not m.get('queue', False): print("I failed to send a message but it shouldn't be queued, deleting it") DbClient.deleteMessageFromOutbox(m["_id"]) failedRecpts.add(recipient) else: print("I failed to send but I'll keep the message and try again later") failedRecpts.add(recipient) else: # There isn't a direct recipient, so let's hope there's a recipient list recipientList = m.get('recipientList', None) if recipientList: print("I've got a message to relay to: ", recipientList) failedRecipientsForThisMessage = set() # TODO: Try to send to each in the list # TODO: Does the parent even need to know when a send has worked? if messagesSent > 0: self.parent.postmanKnock() # only once if messagesFound > 0: print("For %d found messages, I managed to send %d copies" % (messagesFound, messagesSent)) # We tried to send a message to these recipients but failed - set them to be offline for r in failedRecpts: Contacts.goneOffline(r) self._flushing = False
def main(): collectionName = "job_lang_top_corps" dbClient = DbClient('localhost', 27017, "jobaly") collection = dbClient.getCollection(collectionName) title_dict = {} for job in collection.find(): # print job["_id"], job["jobtitle"] title = job["jobtitle"] if title_dict.has_key(title): title_dict[title] += 1 else : title_dict[title] = 1 stat_file_name = "jobtitle_stat.txt" with open( stat_file_name , "w") as text_file: i = 0 for (key, value) in sorted(title_dict.iteritems(), key=operator.itemgetter(1), reverse = True): # print key, ":", value text_file.write("%s : %s \n" % (key.encode('utf8'),value)) i+=1 print i, " lines had been writen into file:", stat_file_name
def main(): stopwords = getStopWords("stopwords.txt") tfgetter = TfGetter(stopwords) collectionName = "jobinfo_se_top_corps" dbClient = DbClient('localhost', 27017, "jobaly") pageProcessor = CollectionPageProcessor(dbClient, collectionName) pageProcessor.process(tfgetter.processPage, pageNo=1, pageNum=100, find_sort=[("_id", 1)]) printDict(tfgetter.term_num_docs, "term_idf.txt")
def main(): collectionName = "job_se_10city" infoCollectionName = "jobinfo_se_10city" collectionName = "job_lang_top_corps" infoCollectionName = "jobinfo_lang_top_corps" dbClient = DbClient('localhost', 27017, "jobaly") collection = dbClient.getCollection(collectionName) infoCollection = dbClient.getCollection(infoCollectionName) pageSize = 20 pageNo = 1 has_more = True pageNum = 10000 find_sort = None find_spec = None threadNum = 20 queue = Queue.Queue() for i in range(threadNum): t = JobGetter(queue, infoCollection) t.setDaemon(True) t.start() while has_more and pageNo <= pageNum: page = dbClient.getPage(collection, find_spec, find_sort, pageSize, pageNo) queue.put((page, pageNo)) pageNo += 1 count = page.count(with_limit_and_skip=True) # print "count=",count if (count < pageSize): has_more = False queue.join()
def main(backup_day): global NSE_DAILY_DATA_URL, NSE_DAILY_DATA_CSV_ZIP_FILENAME, NSE_DAILY_DATA_CSV_FILENAME, DOWNLOADS_PATH year, month, _day = backup_day.split('-')[0], MONTH_NAMES[backup_day.split( '-')[1]], backup_day.split('-')[2] NSE_DAILY_DATA_URL = NSE_DAILY_DATA_URL.format(day=_day, month=month, year=year) NSE_DAILY_DATA_CSV_ZIP_FILENAME = NSE_DAILY_DATA_CSV_ZIP_FILENAME.format( day=_day, month=month, year=year) NSE_DAILY_DATA_CSV_FILENAME = NSE_DAILY_DATA_CSV_FILENAME.format( day=_day, month=month, year=year) logging.info("Started backup for the day: {}".format(backup_day)) file_path = os.path.join(DOWNLOADS_PATH, NSE_DAILY_DATA_CSV_ZIP_FILENAME) if not os.path.exists(file_path): if not os.path.exists(os.path.dirname(file_path)): os.makedirs(os.path.dirname(file_path)) print NSE_DAILY_DATA_URL req = urllib2.Request(NSE_DAILY_DATA_URL, headers=NSE_DAILY_DATA_HEADERS) page = urllib2.urlopen(req) with open(file_path, 'wb') as writer: writer.write(page.read()) zf = zipfile.ZipFile(file_path) df = pd.read_csv(zf.open(NSE_DAILY_DATA_CSV_FILENAME), skiprows=1, names=NSE_DAILY_DATA_COLUMNS) del df['empty'] dbClient = DbClient() for i, record in enumerate(yaml.safe_load(df.to_json(orient='records'))): record['added_on'] = datetime.datetime.strptime( backup_day, "%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S') record['p_change'] = round( (float(record['close']) - float(record['prev_close'])), 2) record['change_percent'] = round( (float(record['close']) - float(record['prev_close'])) / float(record['prev_close']) * 100, 2) dbClient.update_nse_daily_data(record)
def main(): targetDb = "jobaly" targetClient = DbClient('localhost', 27017, targetDb) srcDb = "jobaly_daily" srcClient = DbClient('localhost', 27017, srcDb) targetCollName = "job1000" srcCollnames = "daily_job_info_2014-06-16" srcColl = srcClient.getCollection(srcCollnames) targetColl = targetClient.getCollection(targetCollName) size = 1000 copyCollection(srcColl, targetColl, size)
# get server credentials from config configur = ConfigParser() configur.read(configPath) servermode = 'cloud' # Either 'cloud' or 'local' hostname = configur.get(servermode, 'host') password = configur.get(servermode, 'password') port = configur.getint(servermode, 'port') #initialization screen = Screen() analyzer = Analyzer() scene = Scene() db = DbClient(screen, analyzer, scene.handleUpdate, host=hostname, port=port, password=password) def setup(): sid = db.getActive() scene.setup(sid, db, analyzer, screen) def main(): # for layer in layers: # layer.draw(screen.ctx) # star.draw() #rms, bark, silent = analyzer.get() #screen.update(rms, bark, silent)
from pymongo import MongoClient import exceptions from settings import AUTH_TOKEN import os import urllib app = Flask(__name__) app.config.update( MONGODB_HOST='mongo', MONGODB_PORT='27017', MONGODB_DB='processed_meetings', ) CORS(app) dbclient = DbClient(MongoClient("mongo", 27017).processed_meetings) @app.before_request def verify_token(): # TODO make this a little more secure if "/health" in request.url: return None url_token = request.args.get('token') if not url_token: return jsonify({"error": "Valid Token Required"}), 401 elif url_token != AUTH_TOKEN: return jsonify({"error": "Invalid Token Provided"}), 401 else: return None
class Snackspace: # pylint: disable=R0902 """ Implements the main snackspace class. Responsible for: DB and screen managers RFID and barcode scanners UI interaction via pygame pylint R0902 disabled: too many instance attributes """ def __init__(self, window, size, options): """ Initialise and start the snackspace application """ self.inittime = int(time.time()) self.options = options self.input_handler = InputHandler() self.task_handler = TaskHandler(self) self.task_handler.add_function(self.rfid_task, 500, True) self.logger = logging.getLogger("snackspace") self.rfid = RFIDReader(self.options.rfid_port) self.is_fullscreen = True self.cursor_visible = False self.window_size = size self.screen_manager = ScreenManager(self, window, size) self.user = None self.products = [] self.reply_queue = Queue.Queue() self.dbaccess = DbClient(self.options.hostip, self.task_handler, self.db_state_callback) self.dbaccess.daemon = True self.dbaccess.start() def start(self): """ The main snackspace event loop """ ticks = 0 while (1): for event in pygame.event.get(): if event.type == pygame.QUIT: self.quit() if event.type == pygame.MOUSEBUTTONUP: self.logger.debug("GUI Event") self.screen_manager.current.on_gui_event(event.pos) if event.type == pygame.KEYDOWN: self.keypress(event) try: packet = self.reply_queue.get(False) self.handle_new_packet(packet) except Queue.Empty: pass if (pygame.time.get_ticks() - ticks) > 0: ticks = pygame.time.get_ticks() self.task_handler.tick() def quit(self): #Request the DB client thread stop and wait for it to stop self.dbaccess.stop() while self.dbaccess.is_alive(): pass sys.exit() def set_fullscreen(self, fullscreen): screen = pygame.display.get_surface() tmp = screen.convert() caption = pygame.display.get_caption() flags = screen.get_flags() if fullscreen: flags = flags | pygame.FULLSCREEN else: flags = flags & ~pygame.FULLSCREEN bits = screen.get_bitsize() #pygame.display.quit() #pygame.display.init() screen = pygame.display.set_mode(self.window_size, flags, bits) screen.blit(tmp, (0, 0)) pygame.display.set_caption(*caption) self.is_fullscreen = fullscreen def handle_new_packet(self, packet): if packet.type == PacketTypes.ProductData: self.on_db_got_product_data(packet) elif packet.type == PacketTypes.UnknownProduct: self.on_db_got_unknown_product(packet) elif packet.type == PacketTypes.UserData: self.on_db_got_user_data(packet) elif packet.type == PacketTypes.RandomProduct: self.on_db_random_product_callback(packet) elif packet.type == PacketTypes.Result: if packet.data['action'] == PacketTypes.Transaction: self.charge_all_handler.on_db_send_transactions_callback( packet) elif packet.data['action'] == PacketTypes.AddCredit: self.charge_all_handler.on_db_add_credit_callback(packet) elif packet.data['action'] == PacketTypes.AddProduct: self.on_db_add_product_callback(packet) def keypress(self, event): """ Handle a keyboard press or barcode scan character """ # Push keypress to the current screen self.screen_manager.current.on_key_event(event.dict['unicode']) # Then do our own handling via the input handler result = self.input_handler.new_event(event) if result == InputHandler.FAKE_BAD_PRODUCT: # # Fake a bad product scan self.on_scan_event('BADBARCODE') elif result == InputHandler.FAKE_GOOD_PRODUCT: # # Fake a good product scan self.dbaccess.get_random_product(self.reply_queue) elif result == InputHandler.FAKE_RFID: # # Fake an RFID swipe self.rfid.set_fake_rfid() elif result == InputHandler.NEW_SCANNED_INPUT: # # Buffer is complete, process it scanned_input = self.input_handler.scanned_input self.input_handler.scanned_input = '' self.logger.debug("Got raw input '%s'" % scanned_input) self.on_scan_event(scanned_input) elif result == InputHandler.PRODUCT_ENTRY: # # Go to product entry screen self.screen_manager.req(Screens.PRODUCTENTRY) elif result == InputHandler.FULLSCREEN_TOGGLE: self.set_fullscreen(not self.is_fullscreen) self.screen_manager.req(self.screen_manager.currentscreen, True) elif result == InputHandler.CURSOR_TOGGLE: self.cursor_visible = not self.cursor_visible pygame.mouse.set_visible(self.cursor_visible) elif result == InputHandler.QUIT: self.quit() def rfid_task(self): """ To be run periodically to check for an RFID swipe """ rfid = self.rfid.poll() if rfid is not None: self.on_swipe_event(self.rfid.mangle_rfid(rfid)) def db_state_callback(self, old_state, new_state, first_update): """ Callback when database state changes """ if old_state != new_state or first_update: self.screen_manager.get( Screens.INTROSCREEN).set_db_state(new_state) if not new_state: self.screen_manager.get(Screens.MAINSCREEN).clear_all() self.forget_products() self.forget_user() def on_swipe_event(self, cardnumber): """ When an RFID swipe is made, gets user from the database """ if not self.dbaccess.found_server: return else: self.dbaccess.get_user_data(cardnumber, self.reply_queue) def on_scan_event(self, barcode): """ When a barcode scan is made, pulls product from the database """ if not self.dbaccess.found_server or len(barcode) == 0: return self.add_product_to_basket(barcode) def charge_all(self, callback): """ Charge the current user for the current set of products """ self.charge_all_handler = ChargeAllHandler(self.dbaccess, self.user, self.products, callback, self.reply_queue) def credit_user(self, amount): """ Adds credit to a user's account """ self.user.add_credit(amount) def forget_user(self): """ Clear the user """ self.user = None def total_price(self): """ Get the total price of the basket """ return sum([product.total_price for product in self.products]) def remove_product(self, product_to_remove): """ Remove a product from the basket """ # First reduce the count of this product. # If zero, the product itself can be removed from the list if product_to_remove.decrement() == 0: self.products = [ product for product in self.products if product != product_to_remove ] return product_to_remove.count def forget_products(self): """ Delete the product basket """ self.products = [] def new_product(self, barcode, description, priceinpence, callback): """ Add a new product to the database """ self.dbaccess.add_product(barcode, description, priceinpence, callback) def add_product_to_basket(self, barcode): """ Adds a new scanned product to the list ('basket') of scanned products """ product = next( (product for product in self.products if barcode == product.barcode), None) if product is not None: product.increment( ) # Product already exists once, so just increment its count self.screen_manager.current.on_scan(product) else: # Otherwise need to get product info from the database self.dbaccess.get_product(barcode, self.reply_queue) def on_db_random_product_callback(self, packet): """ Callback when random product data is returned from the database """ barcode = packet.data['barcode'] self.on_scan_event(barcode) def on_db_got_user_data(self, packet): """ Callback when user data returned """ member_id = packet.data['memberid'] username = packet.data['username'] balance = packet.data['balance'] credit_limit = packet.data['limit'] self.user = User(member_id, username, balance, credit_limit, self.options) self.logger.debug("Got user %s" % self.user.name) self.screen_manager.current.on_rfid() def on_db_got_unknown_user(self, packet): self.logger.debug("Bad RFID %s" % packet.data['rfid']) self.screen_manager.current.OnBadRFID() def on_db_got_product_data(self, packet): """ Called when product data returned """ barcode = packet.data['barcode'] description = packet.data['description'] priceinpence = packet.data['priceinpence'] product = Product(barcode, description, priceinpence) # pylint: disable=W0142 if product.valid: self.products.append(product) self.screen_manager.current.on_scan(product) def on_db_got_unknown_product(self, packet): """ Called when user data request failed """ barcode = packet.data['barcode'] self.screen_manager.current.on_bad_scan(barcode)
def __init__(self): self.dbclient = DbClient() pass
class FinancialService: def __init__(self): self.dbclient = DbClient() pass def update_financial_data(self, str_data, validator): parsed_fin_data = parse_financial_data(str_data, validator) if parsed_fin_data: self.dbclient.update_consolidated_profit_before_tax( parsed_fin_data['consolidated_profit_before_tax']) self.dbclient.update_consolidated_net_sales( parsed_fin_data['consolidated_net_sales']) self.dbclient.update_consolidated_balance_sheet( parsed_fin_data['consolidated_balance_sheet']) self.dbclient.update_standalone_net_profit( parsed_fin_data['standalone_net_profit']) self.dbclient.update_standalone_net_sales( parsed_fin_data['standalone_net_sales']) self.dbclient.update_standalone_profit_before_tax( parsed_fin_data['standalone_profit_before_tax']) self.dbclient.update_standalone_other_income( parsed_fin_data['standalone_other_income']) self.dbclient.update_standalone_balance_sheet( parsed_fin_data['standalone_balance_sheet']) self.dbclient.update_consolidated_other_income( parsed_fin_data['consolidated_other_income']) self.dbclient.update_consolidated_net_profit( parsed_fin_data['consolidated_net_profit'])
def __init__(self): self.task_handler = TaskHandler(self) self.dbaccess = DbClient(True, self.task_handler)
def main(): """Main program handler""" with DbClient() as db: load_staging_tables(db) insert_tables(db)
def main(): """Main program handler""" with DbClient() as db: execute_queries(db)
def main(): """Main program handler""" with DbClient() as db: drop_tables(db) create_tables(db)