file_name
large_stringlengths
4
140
prefix
large_stringlengths
0
39k
suffix
large_stringlengths
0
36.1k
middle
large_stringlengths
0
29.4k
fim_type
large_stringclasses
4 values
bayes.py
# coding=utf-8 from numpy import * """ 1 函数loadDataSet()创建了一些实验样本。 该函数返回的第一个变量是进行词条切分后的文档集合, 这些文档来自斑点犬爱好者留言 板。 这些留言文本被切分成一系列的词条集合, 标点符号从文本中去掉, loadDataSet( )函数返回的第二个 变量是一个类别标签的集合。 这里有两类, 侮辱性和非侮辱性。 这些文本的类别由人工标注, 这些标注信息用于训练程序以便自动检测侮辱性留言 """ def loadDataSet(): postingList=[['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'], ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'], ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'], ['stop', 'posting', 'stupid', 'worthless', 'garbage'], ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'], ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']] classVec = [0,1,0,1,0,1] #1 is abusive, 0 not return postingList,classVec """ 2 函数createVocabList()会创建一个包含在所有文档中出现的不重复词的列表, 为此使用了Python的set数据类型。 将词条列表输给 set构造函数, set就会返回一个不重复词表。 首先, 创建一个空集合❶, 然后将每篇文档返回的新词集合添加到该集合中❷。 操作符|用于 求两个集合的并集, 这也是一个按位或(OR) 操作符在数学符号表示上, 按位或操作与集合求并操作使用相同记号 """ def createVocabList(dataSet): vocabSet = set([]) #create empty set for document in dataSet: vocabSet = vocabSet | set(document) #union of the two sets return list(vocabSet) """ 3 获得词汇表后, 便可以使用函数setOfWords2Vec(), 该函数的输入参数为词汇表及某个文档, 输出的是文档向量, 向量的每一元素为1或0, 分别表示词汇表中的单词在输入文档中是否出现。 函数首先创建一个和词汇表等长的向量, 并将其元素都设置为0❸。 接着, 遍历文档中的所有单词, 如果出现了词汇表中的单词, 则将输出的文档向量中的对应值 设为1。 一切都顺利的话, 就不需要检查某个词是否还在vocabList中, 后边可能会用到这一操作 """ def setOfWords2Vec(vocabList, inputSet): returnVec = [0]*len(vocabList) for word in inputSet: if word in vocabList: returnVec[vocabList.index(word)] = 1 return returnVec """ 先看看前三个函数的执行效果 """ def test1(): listPosts, listClass = loadDataSet() mVocabList = createVocabList(listPosts) print mVocabList setOfWords2Vec(mVocabList, listPosts[0]) # test1() # ---------------------------训练算法: 从词向量计算概率-------------------------- """ 函数中的输入参数为文档矩阵trainMatrix, 以及由每篇文档类别标签所构成的向量trainCategory。 首先, 计算文档属于侮辱性文档 (class=1) 的概率, 即P(1)。 因为这是一个二类分类问题, 所以可以通过1-P(1)得到P(0)。 对于多于两类的分类问题, 则需要对代码稍加 修改。计算p(wi|c1) 和p(wi|c0), 需要初始化程序中的分子变量和分母变量❶。 由于w中元素如此众多, 因此可以使用NumPy数组快速计算这些 值。 上述程序中的分母变量是一个元素个数等于词汇表大小的NumPy数组。 在for循环中, 要遍历训练集trainMatrix中的所有文档。 一旦某 个词语(侮辱性或正常词语) 在某一文档中出现, 则该词对应的个数(p1Num或者p0Num) 就加1, 而且在所有的文档中, 该文档的总词数也 相应加1❷。 对于两个类别都要进行同样的计算处理。最后, 对每个元素除以该类别中的总词数❸。 利用NumPy可以很好实现, 用一个数组除以浮点数即可, 若使用常规的Python列表则难以完成这种任务, 读者可以自己尝试一下。 最后, 函数会返回两个向量和一个概率。 """ def trainNB0(trainMatrix,trainCategory): numTrainDocs = len(trainMatrix) numWords = len(trainMatrix[0]) pAbusive = sum(trainCategory)/float(numTrainDocs) # 1. (以下两行) 初始化概率 """ 利用贝叶斯分类器对文档进行分类时, 要计算多个概率的乘积以获得文档属于某个类别的概率, 即计算p(w0|1)p(w1|1)p(w2|1)。 如果其中一 个概率值为0, 那么最后的乘积也为0。 为降低这种影响, 可以将所有词的出现数初始化为1, 并将分母初始化为2 """ p0Num = ones(numWords); p1Num = ones(numWords) # change to ones() p0Denom = 2.0; p1Denom = 2.0 # change to 2.0 for i in range(numTrainDocs): if trainCategory[i] == 1: # 2. (以下两行) 向量相加 p1Num += trainMatrix[i] p1Denom += sum(trainMatrix[i]) else: p0Num += trainMatrix[i] p0Denom += sum(trainMatrix[i]) # 3. 对每个元素做除法 """ 另一个遇到的问题是下溢出, 这是由于太多很小的数相乘造成的。 当计算乘积p(w0|ci)p(w1|ci)p(w2|ci)...p(wN|ci)时, 由于大部分因子都 非常小, 所以程序会下溢出或者得到不正确的答案。 (读者可以用Python尝试相乘许多很小的数, 最后四舍五入后会得到0。 ) 一种解决 办法是对乘积取自然对数。 在代数中有ln(a*b) = ln(a)+ln(b), 于是通过求对数可以避免下溢出或者浮点数舍入导致的错误。 同时, 采用 自然对数进行处理不会有任何损失。 图4-4给出函数f(x)与ln(f(x))的曲线。 检查这两条曲线, 就会发现它们在相同区域内同时增加或者减 少, 并且在相同点上取到极值。 它们的取值虽然不同, 但不影响最终结果。 通过修改return前的两行代码, 将上述做法用到分类器中: """ p1Vect = log(p1Num/p1Denom) # change to log() p0Vect = log(p0Num/p0Denom) # change to log() return p0Vect,p1Vect,pAbusive def test2(): listPosts, listClass = loadDataSet() # 构建了一个包含所有词的列表mVocabList mVocabList = createVocabList(listPosts) setOfWords2Vec(mVocabList, listPosts[0]) trainMat = [] for postinDoc in listPosts: temp = setOfWords2Vec(mVocabList, postinDoc) trainMat.append(temp) # 文档属于侮辱类的概率pAb p0v, p1v, pAb = trainNB0(trainMat, listClass) print pAb """ 接下来看一看在给定文档类别条件下词汇表中单词的出现概率, 看看是否正确。 词汇表中的第一个词是cute, 其在类别0中出现1次, 而在类别1中 从未出现。 对应的条件概率分别为0.041 666 67与0.0。 该计算是正确的。 我们找找所有概率中的最大值, 该值出现在P(1)数组第26个下标位 置, 大小为0.157 894 74。 在myVocabList的第26个下标位置上可以查到该单词是stupid。 这意味着stupid是最能表征类别1(侮辱性文档类)的单词。 """ """ 代码有4个输入: 要分类的向量vec2Classify以及使用函数trainNB0()计算得到的三个概率。 使用NumPy的数组来计算两个 向量相乘的结果❶。 这里的相乘是指对应元素相乘, 即先将两个向量中的第1个元素相乘, 然后将第2个元素相乘, 以此类推。 接下来将词汇表 中所有词的对应值相加, 然后将该值加到类别的对数概率上。 最后, 比较类别的概率返回大概率对应的类别标签。 这一切不是很难, 对吧? """ def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1): # 1. 元素相乘 分类计算的核心 p1 = sum(vec2Classify * p1Vec) + log(pClass1) # element-wise mult p0 = sum(vec2Classify * p0Vec) + log(1.0 - pClass1) if p1 > p0: return 1 else: return 0 """ 对文本做一些修改, 看看分类器会输出什么结果。 这个例子非常简单,但是它展示了朴素贝叶斯分类器的工作原理。 接下来,我们会对代码做些修改, 使分类器工作得更好。 函数setOfWords2Vec()稍加修改, 修改后的函数称为bagOfWords2Vec() -----------------------------------准备数据: 文档词袋模型--------------------------------------- """ def bagOfWords2VecMN(vocabList, inputSet): returnVec = [0]*len(vocabList) for word in inputSet: if word in vocabList: # todo 这个词的操作 returnVec[vocabList.index(word)] += 1 return returnVec """ 函数是一个便利函数(convenience function) , 该函数封装所有操作, 以节省输入 """ def testingNB(): listOPosts,listClasses = loadDataSet() myVocabList = createVocabList(listOPosts) trainMat=[] for postinDoc in listOPosts: trainMat.append(setOfWords2Vec(myVocabList, postinDoc)) p0V,p1V,pAb = trainNB0(array(trainMat),array(listClasses)) testEntry = ['love', 'my', 'dalmation'] thisDoc = array(setOfWords2Vec(myVocabList, testEntry)) print (testEntry,'classified as: ',classifyNB(thisDoc,p0V,p1V,pAb)) testEntry = ['stupid', 'garbage'] thisDoc = array(setOfWords2Vec(myVocabList, testEntry)) print (testEntry,'classified as: ',classifyNB(thisDoc,p0V,p1V,pAb)) # testingNB() # -----------------------------------使用朴素贝叶斯过滤垃圾邮件---------------------------- """ 准备数据: 切分文本 可以看到, 切分的结果不错, 但是标点符号也被当成了词的一部分。 可以使用正则表示式来切分句子, 其中分隔符是除单词、 数字外的任意字符串 """ def textParse(bigString): #input is big string, #output is word list import re listOfTokens = re.split(r'\W*', bigString) return [tok.lower() for tok in listOfTokens if len(tok) > 2] """" 函数spamTest()对贝叶斯垃圾邮件分类器进行自动化处理。 导入文件夹spam与ham下的文本文件, 并将它们解析为词列表❶。 接下来 构建一个测试集与一个训练集, 两个集合中的邮件都是随机选出的。 本例中共有50封电子邮件, 并不是很多, 其中的10封电子邮件被随机选择 为测试集。 分类器所需要的概率计算只利用训练集中的文档来完成。Python变量trainingSet是一个整数列表, 其中的值从0到49。 接下 来, 随机选择其中10个文件❷。 选择出的数字所对应的文档被添加到测试集, 同时也将其从训练集中剔除。 这种随机选择数据的一部分作为训 练集, 而剩余部分作为测试集的过程称为留存交叉验证(hold-out crossvalidation) 。 假定现在只完成了一次迭代, 那么为了更精确地估计分类 器的错误率, 就应该进行多次迭代后求出平均错误率。接下来的for循环遍历训练集的所有文档, 对每封邮件基于词汇表并使 用setOfWords2Vec()函数来构建词向量。 这些词在traindNB0()函数中用于计算分类所需的概率。 然后遍历测试集, 对其中每封电子邮件进 行分类❸。 如果邮件分类错误, 则错误数加1, 最后给出总的错误百分比 """ def spamTest(): docList=[]; classList = []; fullText =[] for i in range(1,26): wordList = textParse(open('email/spam/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(1) wordList = textParse(open('email/ham/%d.txt' % i).read()) docList.append(wordList) fullText.extend(wordList) classList.append(0) vocabList = createVocabList(docList)# create vocabulary trainingSet = range(50); testSet=[] # create test set # (以下四行) 随机构建训练集 for i in range(10): randIndex = int(random.uniform(0,len(trainingSet))) testSet.append(trainingSet[randIndex]) # todo del这个操作步骤 del(trainingSet[randIndex]) trainMat=[]; trainClasses = [] # (以下四行) 对测试集分类 for docIndex in trainingSet:#train the classifier (get probs) trainNB0 trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) # todo 入参出参的计算方法 p0V,p1V,pSpam = trainNB0(array(trainMat),array(trainClasses)) errorCount = 0 for docIndex in testSet: #classify the remaining items wordVector = bagOfWords2VecMN(vocabList, docList[docIndex]) if classifyNB(array(wordVector),p0V,p1V,pSpam) != classList[docIndex]: errorCount += 1 print ("classification error",docList[docIndex]) print ('the error rate is: ',float(errorCount)/len(testSet)) #return vocabList,fullText # ------------------------自动化处理---------------------------------------- spamTest() # ----------------------------- 4.7. 示例: 使用朴素贝叶斯分类器从个人广告中获取区域倾向--------------------------- """" RSS源分类器及高频词去除函数 函数calcMostFreq() ❶。 该函数遍历词汇表中的每个词并统计它在文本中出现的次数, 然后根据出现次数从高到低对词典进行排序, 最后返回排序最高的30个单词。 你很快就会明白这个函数的重要性 以下四行) 计算出现频率 """ def calcMostFreq(vocabList,fullText): import operator freqDict = {} for token in vocabList: freqDict[token]=fullText.count(token) sortedFreq = sorted(freqDict.iteritems(), key=operator.itemgetter(1), reverse=True) return sortedFreq[:30] """" 函数localWords()使用两个RSS源作为参数。 RSS源要在函数外 导入, 这样做的原因是RSS源会随时间而改变。 如果想通过改变代码来 比较程序执行的差异, 就应该使用相同的输入。 重新加载RSS源就会得 到新的数据, 但很难确定是代码原因还是输入原因导致输出结果的改 变。 函数localWords()与程序清单4-5中的spamTest()函数几乎相 同, 区别在于这里访问的是RSS源❷而不是文件。 然后调用函 数calcMostFreq()来获得排序最高的30个单词并随后将它们移除❸。 函数的剩余部分与spamTest()基本类似, 不同的是最后一行要返回下 面要用到的值。 """ def localWords(feed1,feed0): import feedparser docList=[]; classList = []; fullText =[] minLen = min(len(feed1['entries']),len(feed0['entries'])) for i in range(minLen): # 2 每次访问一条RSS源 wordList = textParse(feed1['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(1) #NY is class 1 wordList = textParse(feed0['entries'][i]['summary']) docList.append(wordList) fullText.extend(wordList) classList.append(0) # (以下四行) 去掉出现次数最高的那些词 vocabList = createVocabList(docList)#create vocabulary top30Words = calcMostFreq(vocabList,fullText) #remove top 30 words for pairW in top30Words: if pairW[0] in vocabList: vocabList.remove(pairW[0]) trainingSet = range(2*minLen); testSet=[] #create test set for i in range(20): randIndex = int(random.uniform(0,len(trainingSet))) testSet.append(trainingSet[randIndex]) del(trainingSet[randIndex]) trainMat=[]; trainClasses = [] for docIndex in trainingSet:#train the classifier (get probs) trainNB0 trainMat.append(bagOfWords2VecMN(vocabList, docList[docIndex])) trainClasses.append(classList[docIndex]) p0V,p1V,pSpam = trainNB0(array(trainMat),array(trainClasses)) errorCount = 0 for docIndex in testSet: #classify the remaining items wordVector = bagOfWords2VecMN(vocabList, docList[docIndex]) if classifyNB(array(wordVector),p0V,p1V,pSpam) != classList[docIndex]: errorCount += 1 print ('the error rate is: ',float(errorCount)/len(testSet)) return vocabList,p0V,p1V def getTopWords(ny,sf): import operator vocabList,p0V,p1V=localWords(ny,sf) topNY=[]; topSF=[] for i in range(len(p0V)): if p0V[i] > -6.0 : topSF.append((vocabList[i],p0V[i])) if p1V[i] > -6.0 : topNY.append((vocabList[i],p1V[i])) sortedSF = sorted(topSF, key=lambda pair: pair[1], reverse=True) print ("SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**SF**") for item in sortedSF: print (item[0]) sortedNY = sorted(topNY, key=lambda pair: pair[1], reverse=True) print ("NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**NY**") for item in sortedNY: print (item[0])
identifier_body
main.js
if (document.location.hostname == "localhost" || document.location.hostname == "svn.inkling.com"){ s9.initialParams = { "image1": "img/slideline_placeholder_images_1.jpg", "caption1": "This is the first caption. This is optional.", "label1": "Label 1. This is optional.", "image2": "img/slideline_placeholder_images_2.jpg", "caption2": "This is the second caption. This is optional.", "label2": "Label 2. This is optional.", "image3": "img/slideline_placeholder_images_3.jpg", "caption3": "This is the third caption. This is optional.", "label3": "Label 3. This is optional.", "image4": "img/slideline_placeholder_images_4.jpg", "caption4": "This is the fourth caption. This is optional.", "label4": "Label 4. This is optional.", "image5": "img/slideline_placeholder_images_5.jpg", "caption5": "This is the fifth caption. This is optional.", "label5": "Label 5. This is optional.", "image6": "img/slideline_placeholder_images_6.jpg", "caption6": "This is the sixth caption. This is optional.", "label6": "Label 6. This is optional.", } }; $(document).ready(function(){ // variables for values var val; var wholeVal; var decVal; var last = 0; var lastStop = 0; // varables for each element var prevImage; var currentImage; var nextImage; var prevLabel; var currentLabel; var nextLabel; var prevCaption; var currentCaption; var nextCaption; // variables for catching and storing values during slide var oldVal; var currentVal; var newVal; var dataSource = s9.initialParams['dataSource']; function clearGhosts(start, end) { $('.image').each(function() { if (this.getAttribute('id') == 'image' + start || this.getAttribute('id') == 'image' + end) { return; } $(this).css({opacity: 0}); }); } //This function initializes the jQuery slider and shows the first slide function buildSlider(){ $('#slide_images #image1').css("opacity", 1); $('#slide_labels #label1').css("opacity", 1); $('#slide_captions #caption1').css("opacity", 1); currentImage = document.getElementById('image1'); currentLabel = document.getElementById('label1'); currentCaption = document.getElementById('caption1'); $("#slider").slider({ animate: true, value: 1, min: 1, max: $('#slide_images .image').size(), step: .01, slide: function (event, ui) { $('.ui-slider-handle').removeClass('pulsing'); sliderPos = (ui.value); //ex: 1.25 wholeSliderPos = Math.floor(sliderPos); //ex: 1 decVal = sliderPos - wholeSliderPos; // ex: 1.25 - 1 (=.25) //console.log('sliding: ' + decVal); var rangeStart = Math.floor(sliderPos); var rangeEnd = Math.ceil(sliderPos); if (lastStop > 0 && lastStop != rangeStart && lastStop != rangeEnd){ var old = $('#image' + lastStop); old.css('opacity', 0); } prevImage = document.getElementById('image' + (wholeSliderPos - 1)); currentImage = document.getElementById('image' + wholeSliderPos); nextImage = document.getElementById('image' + (wholeSliderPos + 1)); prevLabel = document.getElementById('label' + (wholeSliderPos - 1)); currentLabel = document.getElementById('label' + wholeSliderPos); nextLabel = document.getElementById('label' + (wholeSliderPos + 1)); prevCaption = document.getElementById('caption' + (wholeSliderPos - 1)); currentCaption = document.getElementById('caption' + wholeSliderPos); nextCaption = document.getElementById('caption' + (wholeSliderPos + 1)); if (ui.value > last) { $(currentImage).css("opacity", 1 - decVal); $(nextImage).css("opacity", decVal); } if (ui.value < last) { $(currentImage).css("opacity", 1 - decVal); $(nextImage).css("opacity", decVal); } if (Math.floor(last) != wholeSliderPos) { clearGhosts(rangeStart, rangeEnd); } last = ui.value; }, stop: function( event, ui ) { $('.ui-slider-handle').removeClass('pulsing'); var wholeVal = Math.round(ui.value); $( "#slider" ).slider( "value", wholeVal ); // console.log('stop: ' + wholeVal); prevImage = document.getElementById('image' + (wholeVal - 1)); currentImage = document.getElementById('image' + wholeVal); nextImage = document.getElementById('image' + (wholeVal + 1)); prevLabel = document.getElementById('label' + (wholeVal - 1)); currentLabel = document.getElementById('label' + wholeVal); nextLabel = document.getElementById('label' + (wholeVal + 1)); prevCaption = document.getElementById('caption' + (wholeVal - 1)); currentCaption = document.getElementById('caption' + wholeVal); nextCaption = document.getElementById('caption' + (wholeVal + 1)); $('.image').css("opacity", 0); $('.label').css("opacity", 0); $('.caption').css("opacity", 0); $(currentImage).css("opacity", 1); $(currentLabel).css("opacity", 1); $(currentCaption).css("opacity", 1); last = wholeVal; lastStop = wholeVal; } }); } //This function draws the tick marks/pips for the slider. It must be called after the slider's max is set. function setSliderTicks(){ var $slider = $('#slider'); var max = $slider.slider("option", "max"); if (max > 1) { var spacing = 100 / (max -1); } else { var spacing = 50; } $slider.find('.ui-slider-tick-mark').remove(); for (var i = 0; i < max ; i++) { $('<span class="ui-slider-tick-mark"></span>').css('left', (spacing * i) + '%').appendTo($slider); } } // Extract the text from the template .html() is the jquery helper method for that var image_template = $('#image-temp').html(); var label_template = $('#label-temp').html(); var caption_template = $('#caption-temp').html(); // Compile that into an handlebars template var imageTemplate = Handlebars.compile(image_template); var labelTemplate = Handlebars.compile(label_template); var captionTemplate = Handlebars.compile(caption_template); // Retrieve the placeHolder where the Posts will be displayed var imageHolder = $("#slide_images"); var labelHolder = $("#slide_labels"); var captionHolder = $("#slide_captions"); var imgArr = [] var slideNum = 0; for(var key in s9.initialParams) { if (key.slice(0, 5) == 'image'){ slideNum++; imgArr.push(s9.initialParams[key]); } } var slideInfo = { 'slideData': [] }; for(var i=0; i<slideNum; i++){ var temp = i+1; // console.log("loading slide " + temp); if (!s9.initialParams['image'+(i+1)]) { continue; } slideInfo['slideData'].push({ image : s9.initialParams['image'+(i+1)], caption : s9.initialParams['caption'+(i+1)], label : s9.initialParams['label'+(i+1)], index : temp }); }; if (slideInfo.slideData.length == 0){ //Show default - placeholder images. slideInfo.slideData = [{ image: 'img/placeholder-image1.svg', caption: 'Caption 1', label: 'Label 1', index: 1, }, { image: 'img/placeholder-image2.svg', caption: 'Caption 2', label: 'Label 2', index: 2, }, { image: 'img/placeholder-image3.svg', caption: 'Caption 3', label: 'Label 3', index: 3, }]; } //Add each of the slide images, labels, and captions to the DOM $.each(slideInfo.slideData,function(index,element){ // Generate the HTML for each post var imagehtml = imageTemplate(element); var labelhtml = labelTemplate(element); var captionhtml = captionTemplate(element); // Render the posts into the page imageHolder.append(imagehtml); if (element.label){ labelHolder.append(labelhtml); } if (element.caption) { captionHolder.append(captionhtml); } }); //Size the image div to the tallest slide image function imageSizing() { $('#slide_images').css({height: 0}); var numImages = $('.image img').length; var numDone = 0; $('.image img').each(function() { $(this).load(function () { var w = $('#slide_images').outerWidth(); var ratio = w / this.naturalWidth; var curHeight = $('#slide_images').height() || 0; var displayH = this.naturalHeight * ratio; if (displayH > curHeight){ $('#slide_images').css({height: displayH}); } numDone++; if (numDone == numImages) { setSize(); numDone = 0; } $(this).off('load'); }); if (this.complete)
}); } if ($('.label').length == 0){ $('#slide_labels').hide(); $('#slide_captions').css({'border-top': 'none'}); } else { labelSizing(); } if ($('.caption').length == 0){ $('#slide_captions').hide(); } else { captionSizing(); } // build the slideline slider buildSlider(); //add ticks to slideline slider setSliderTicks(); //set heights of image container imageSizing(); //This function sets the size of the caption & label areas to the tallest ones function captionSizing() { $('#slide_captions').css({'height': 0}); var captionHeights = []; $('.caption').each(function() { var captionHeight = $(this).children('p').outerHeight(true); captionHeights.push(captionHeight); }); var tallestCaption = Math.max.apply(Math,captionHeights); captionHeights = []; $('#slide_captions').css({'height': tallestCaption + 20}); }; function labelSizing() { $('#slide_labels').css({'height': 0}); var labelHeights = []; $('.label').each(function() { var labelHeight = $(this).outerHeight(true); labelHeights.push(labelHeight); }); var tallestLabel = Math.max.apply(Math,labelHeights); labelHeights = []; $('#slide_labels').css({'height': tallestLabel + 20}); } $(document).on('mouseout', function(e) { e = e ? e : window.event; var from = e.relatedTarget || e.toElement; if (!from || from.nodeName == 'HTML'){ // this detects that the mouse has left the frame - // somehow need to trigger the slider stop event or use a hack } }); $(window).resize(function() { captionSizing(); labelSizing(); imageSizing(); }); window.addEventListener('orientationchange', function() { captionSizing(); labelSizing(); imageSizing(); }); $(window).load(function() { $('.ui-slider-handle').addClass('pulsing'); //FastClick.attach(document.body); }); function setSize(){ //console.log('set size called'); s9.view.size({ height: $(document.body).outerHeight(true) }); } });
{ $(this).load(); }
conditional_block
main.js
if (document.location.hostname == "localhost" || document.location.hostname == "svn.inkling.com"){ s9.initialParams = { "image1": "img/slideline_placeholder_images_1.jpg", "caption1": "This is the first caption. This is optional.", "label1": "Label 1. This is optional.", "image2": "img/slideline_placeholder_images_2.jpg", "caption2": "This is the second caption. This is optional.", "label2": "Label 2. This is optional.", "image3": "img/slideline_placeholder_images_3.jpg", "caption3": "This is the third caption. This is optional.", "label3": "Label 3. This is optional.", "image4": "img/slideline_placeholder_images_4.jpg", "caption4": "This is the fourth caption. This is optional.", "label4": "Label 4. This is optional.", "image5": "img/slideline_placeholder_images_5.jpg", "caption5": "This is the fifth caption. This is optional.", "label5": "Label 5. This is optional.", "image6": "img/slideline_placeholder_images_6.jpg", "caption6": "This is the sixth caption. This is optional.", "label6": "Label 6. This is optional.", } }; $(document).ready(function(){ // variables for values var val; var wholeVal; var decVal; var last = 0; var lastStop = 0; // varables for each element var prevImage; var currentImage; var nextImage; var prevLabel; var currentLabel; var nextLabel; var prevCaption; var currentCaption; var nextCaption; // variables for catching and storing values during slide var oldVal; var currentVal; var newVal; var dataSource = s9.initialParams['dataSource']; function clearGhosts(start, end) { $('.image').each(function() { if (this.getAttribute('id') == 'image' + start || this.getAttribute('id') == 'image' + end) { return; } $(this).css({opacity: 0}); }); } //This function initializes the jQuery slider and shows the first slide function buildSlider(){ $('#slide_images #image1').css("opacity", 1); $('#slide_labels #label1').css("opacity", 1); $('#slide_captions #caption1').css("opacity", 1); currentImage = document.getElementById('image1'); currentLabel = document.getElementById('label1'); currentCaption = document.getElementById('caption1'); $("#slider").slider({ animate: true, value: 1, min: 1, max: $('#slide_images .image').size(), step: .01, slide: function (event, ui) { $('.ui-slider-handle').removeClass('pulsing'); sliderPos = (ui.value); //ex: 1.25 wholeSliderPos = Math.floor(sliderPos); //ex: 1 decVal = sliderPos - wholeSliderPos; // ex: 1.25 - 1 (=.25) //console.log('sliding: ' + decVal); var rangeStart = Math.floor(sliderPos); var rangeEnd = Math.ceil(sliderPos); if (lastStop > 0 && lastStop != rangeStart && lastStop != rangeEnd){ var old = $('#image' + lastStop); old.css('opacity', 0); } prevImage = document.getElementById('image' + (wholeSliderPos - 1)); currentImage = document.getElementById('image' + wholeSliderPos); nextImage = document.getElementById('image' + (wholeSliderPos + 1)); prevLabel = document.getElementById('label' + (wholeSliderPos - 1)); currentLabel = document.getElementById('label' + wholeSliderPos); nextLabel = document.getElementById('label' + (wholeSliderPos + 1)); prevCaption = document.getElementById('caption' + (wholeSliderPos - 1)); currentCaption = document.getElementById('caption' + wholeSliderPos); nextCaption = document.getElementById('caption' + (wholeSliderPos + 1)); if (ui.value > last) { $(currentImage).css("opacity", 1 - decVal); $(nextImage).css("opacity", decVal); } if (ui.value < last) { $(currentImage).css("opacity", 1 - decVal); $(nextImage).css("opacity", decVal); } if (Math.floor(last) != wholeSliderPos) { clearGhosts(rangeStart, rangeEnd); } last = ui.value; }, stop: function( event, ui ) { $('.ui-slider-handle').removeClass('pulsing'); var wholeVal = Math.round(ui.value); $( "#slider" ).slider( "value", wholeVal ); // console.log('stop: ' + wholeVal); prevImage = document.getElementById('image' + (wholeVal - 1)); currentImage = document.getElementById('image' + wholeVal); nextImage = document.getElementById('image' + (wholeVal + 1)); prevLabel = document.getElementById('label' + (wholeVal - 1)); currentLabel = document.getElementById('label' + wholeVal); nextLabel = document.getElementById('label' + (wholeVal + 1)); prevCaption = document.getElementById('caption' + (wholeVal - 1)); currentCaption = document.getElementById('caption' + wholeVal); nextCaption = document.getElementById('caption' + (wholeVal + 1)); $('.image').css("opacity", 0); $('.label').css("opacity", 0); $('.caption').css("opacity", 0); $(currentImage).css("opacity", 1); $(currentLabel).css("opacity", 1); $(currentCaption).css("opacity", 1); last = wholeVal; lastStop = wholeVal; } }); } //This function draws the tick marks/pips for the slider. It must be called after the slider's max is set. function setSliderTicks(){ var $slider = $('#slider'); var max = $slider.slider("option", "max"); if (max > 1) { var spacing = 100 / (max -1); } else { var spacing = 50; } $slider.find('.ui-slider-tick-mark').remove(); for (var i = 0; i < max ; i++) { $('<span class="ui-slider-tick-mark"></span>').css('left', (spacing * i) + '%').appendTo($slider); } } // Extract the text from the template .html() is the jquery helper method for that var image_template = $('#image-temp').html(); var label_template = $('#label-temp').html(); var caption_template = $('#caption-temp').html(); // Compile that into an handlebars template var imageTemplate = Handlebars.compile(image_template); var labelTemplate = Handlebars.compile(label_template); var captionTemplate = Handlebars.compile(caption_template); // Retrieve the placeHolder where the Posts will be displayed var imageHolder = $("#slide_images"); var labelHolder = $("#slide_labels"); var captionHolder = $("#slide_captions"); var imgArr = [] var slideNum = 0; for(var key in s9.initialParams) { if (key.slice(0, 5) == 'image'){ slideNum++; imgArr.push(s9.initialParams[key]); } } var slideInfo = { 'slideData': [] }; for(var i=0; i<slideNum; i++){ var temp = i+1; // console.log("loading slide " + temp); if (!s9.initialParams['image'+(i+1)]) { continue; } slideInfo['slideData'].push({ image : s9.initialParams['image'+(i+1)], caption : s9.initialParams['caption'+(i+1)], label : s9.initialParams['label'+(i+1)], index : temp }); }; if (slideInfo.slideData.length == 0){ //Show default - placeholder images. slideInfo.slideData = [{ image: 'img/placeholder-image1.svg', caption: 'Caption 1', label: 'Label 1', index: 1, }, { image: 'img/placeholder-image2.svg', caption: 'Caption 2', label: 'Label 2', index: 2, }, { image: 'img/placeholder-image3.svg', caption: 'Caption 3', label: 'Label 3', index: 3, }]; } //Add each of the slide images, labels, and captions to the DOM $.each(slideInfo.slideData,function(index,element){ // Generate the HTML for each post var imagehtml = imageTemplate(element); var labelhtml = labelTemplate(element); var captionhtml = captionTemplate(element); // Render the posts into the page imageHolder.append(imagehtml); if (element.label){ labelHolder.append(labelhtml); } if (element.caption) { captionHolder.append(captionhtml); } }); //Size the image div to the tallest slide image function imageSizing() { $('#slide_images').css({height: 0}); var numImages = $('.image img').length; var numDone = 0; $('.image img').each(function() { $(this).load(function () { var w = $('#slide_images').outerWidth(); var ratio = w / this.naturalWidth; var curHeight = $('#slide_images').height() || 0; var displayH = this.naturalHeight * ratio; if (displayH > curHeight){ $('#slide_images').css({height: displayH}); } numDone++; if (numDone == numImages) { setSize(); numDone = 0; } $(this).off('load'); }); if (this.complete) { $(this).load(); } }); } if ($('.label').length == 0){ $('#slide_labels').hide(); $('#slide_captions').css({'border-top': 'none'}); } else { labelSizing(); } if ($('.caption').length == 0){ $('#slide_captions').hide(); } else { captionSizing(); } // build the slideline slider buildSlider(); //add ticks to slideline slider setSliderTicks(); //set heights of image container imageSizing(); //This function sets the size of the caption & label areas to the tallest ones function
() { $('#slide_captions').css({'height': 0}); var captionHeights = []; $('.caption').each(function() { var captionHeight = $(this).children('p').outerHeight(true); captionHeights.push(captionHeight); }); var tallestCaption = Math.max.apply(Math,captionHeights); captionHeights = []; $('#slide_captions').css({'height': tallestCaption + 20}); }; function labelSizing() { $('#slide_labels').css({'height': 0}); var labelHeights = []; $('.label').each(function() { var labelHeight = $(this).outerHeight(true); labelHeights.push(labelHeight); }); var tallestLabel = Math.max.apply(Math,labelHeights); labelHeights = []; $('#slide_labels').css({'height': tallestLabel + 20}); } $(document).on('mouseout', function(e) { e = e ? e : window.event; var from = e.relatedTarget || e.toElement; if (!from || from.nodeName == 'HTML'){ // this detects that the mouse has left the frame - // somehow need to trigger the slider stop event or use a hack } }); $(window).resize(function() { captionSizing(); labelSizing(); imageSizing(); }); window.addEventListener('orientationchange', function() { captionSizing(); labelSizing(); imageSizing(); }); $(window).load(function() { $('.ui-slider-handle').addClass('pulsing'); //FastClick.attach(document.body); }); function setSize(){ //console.log('set size called'); s9.view.size({ height: $(document.body).outerHeight(true) }); } });
captionSizing
identifier_name
main.js
if (document.location.hostname == "localhost" || document.location.hostname == "svn.inkling.com"){ s9.initialParams = { "image1": "img/slideline_placeholder_images_1.jpg", "caption1": "This is the first caption. This is optional.", "label1": "Label 1. This is optional.", "image2": "img/slideline_placeholder_images_2.jpg", "caption2": "This is the second caption. This is optional.", "label2": "Label 2. This is optional.", "image3": "img/slideline_placeholder_images_3.jpg", "caption3": "This is the third caption. This is optional.", "label3": "Label 3. This is optional.", "image4": "img/slideline_placeholder_images_4.jpg", "caption4": "This is the fourth caption. This is optional.", "label4": "Label 4. This is optional.", "image5": "img/slideline_placeholder_images_5.jpg", "caption5": "This is the fifth caption. This is optional.", "label5": "Label 5. This is optional.", "image6": "img/slideline_placeholder_images_6.jpg", "caption6": "This is the sixth caption. This is optional.", "label6": "Label 6. This is optional.", } }; $(document).ready(function(){ // variables for values var val; var wholeVal; var decVal; var last = 0; var lastStop = 0; // varables for each element var prevImage; var currentImage; var nextImage; var prevLabel; var currentLabel; var nextLabel;
// variables for catching and storing values during slide var oldVal; var currentVal; var newVal; var dataSource = s9.initialParams['dataSource']; function clearGhosts(start, end) { $('.image').each(function() { if (this.getAttribute('id') == 'image' + start || this.getAttribute('id') == 'image' + end) { return; } $(this).css({opacity: 0}); }); } //This function initializes the jQuery slider and shows the first slide function buildSlider(){ $('#slide_images #image1').css("opacity", 1); $('#slide_labels #label1').css("opacity", 1); $('#slide_captions #caption1').css("opacity", 1); currentImage = document.getElementById('image1'); currentLabel = document.getElementById('label1'); currentCaption = document.getElementById('caption1'); $("#slider").slider({ animate: true, value: 1, min: 1, max: $('#slide_images .image').size(), step: .01, slide: function (event, ui) { $('.ui-slider-handle').removeClass('pulsing'); sliderPos = (ui.value); //ex: 1.25 wholeSliderPos = Math.floor(sliderPos); //ex: 1 decVal = sliderPos - wholeSliderPos; // ex: 1.25 - 1 (=.25) //console.log('sliding: ' + decVal); var rangeStart = Math.floor(sliderPos); var rangeEnd = Math.ceil(sliderPos); if (lastStop > 0 && lastStop != rangeStart && lastStop != rangeEnd){ var old = $('#image' + lastStop); old.css('opacity', 0); } prevImage = document.getElementById('image' + (wholeSliderPos - 1)); currentImage = document.getElementById('image' + wholeSliderPos); nextImage = document.getElementById('image' + (wholeSliderPos + 1)); prevLabel = document.getElementById('label' + (wholeSliderPos - 1)); currentLabel = document.getElementById('label' + wholeSliderPos); nextLabel = document.getElementById('label' + (wholeSliderPos + 1)); prevCaption = document.getElementById('caption' + (wholeSliderPos - 1)); currentCaption = document.getElementById('caption' + wholeSliderPos); nextCaption = document.getElementById('caption' + (wholeSliderPos + 1)); if (ui.value > last) { $(currentImage).css("opacity", 1 - decVal); $(nextImage).css("opacity", decVal); } if (ui.value < last) { $(currentImage).css("opacity", 1 - decVal); $(nextImage).css("opacity", decVal); } if (Math.floor(last) != wholeSliderPos) { clearGhosts(rangeStart, rangeEnd); } last = ui.value; }, stop: function( event, ui ) { $('.ui-slider-handle').removeClass('pulsing'); var wholeVal = Math.round(ui.value); $( "#slider" ).slider( "value", wholeVal ); // console.log('stop: ' + wholeVal); prevImage = document.getElementById('image' + (wholeVal - 1)); currentImage = document.getElementById('image' + wholeVal); nextImage = document.getElementById('image' + (wholeVal + 1)); prevLabel = document.getElementById('label' + (wholeVal - 1)); currentLabel = document.getElementById('label' + wholeVal); nextLabel = document.getElementById('label' + (wholeVal + 1)); prevCaption = document.getElementById('caption' + (wholeVal - 1)); currentCaption = document.getElementById('caption' + wholeVal); nextCaption = document.getElementById('caption' + (wholeVal + 1)); $('.image').css("opacity", 0); $('.label').css("opacity", 0); $('.caption').css("opacity", 0); $(currentImage).css("opacity", 1); $(currentLabel).css("opacity", 1); $(currentCaption).css("opacity", 1); last = wholeVal; lastStop = wholeVal; } }); } //This function draws the tick marks/pips for the slider. It must be called after the slider's max is set. function setSliderTicks(){ var $slider = $('#slider'); var max = $slider.slider("option", "max"); if (max > 1) { var spacing = 100 / (max -1); } else { var spacing = 50; } $slider.find('.ui-slider-tick-mark').remove(); for (var i = 0; i < max ; i++) { $('<span class="ui-slider-tick-mark"></span>').css('left', (spacing * i) + '%').appendTo($slider); } } // Extract the text from the template .html() is the jquery helper method for that var image_template = $('#image-temp').html(); var label_template = $('#label-temp').html(); var caption_template = $('#caption-temp').html(); // Compile that into an handlebars template var imageTemplate = Handlebars.compile(image_template); var labelTemplate = Handlebars.compile(label_template); var captionTemplate = Handlebars.compile(caption_template); // Retrieve the placeHolder where the Posts will be displayed var imageHolder = $("#slide_images"); var labelHolder = $("#slide_labels"); var captionHolder = $("#slide_captions"); var imgArr = [] var slideNum = 0; for(var key in s9.initialParams) { if (key.slice(0, 5) == 'image'){ slideNum++; imgArr.push(s9.initialParams[key]); } } var slideInfo = { 'slideData': [] }; for(var i=0; i<slideNum; i++){ var temp = i+1; // console.log("loading slide " + temp); if (!s9.initialParams['image'+(i+1)]) { continue; } slideInfo['slideData'].push({ image : s9.initialParams['image'+(i+1)], caption : s9.initialParams['caption'+(i+1)], label : s9.initialParams['label'+(i+1)], index : temp }); }; if (slideInfo.slideData.length == 0){ //Show default - placeholder images. slideInfo.slideData = [{ image: 'img/placeholder-image1.svg', caption: 'Caption 1', label: 'Label 1', index: 1, }, { image: 'img/placeholder-image2.svg', caption: 'Caption 2', label: 'Label 2', index: 2, }, { image: 'img/placeholder-image3.svg', caption: 'Caption 3', label: 'Label 3', index: 3, }]; } //Add each of the slide images, labels, and captions to the DOM $.each(slideInfo.slideData,function(index,element){ // Generate the HTML for each post var imagehtml = imageTemplate(element); var labelhtml = labelTemplate(element); var captionhtml = captionTemplate(element); // Render the posts into the page imageHolder.append(imagehtml); if (element.label){ labelHolder.append(labelhtml); } if (element.caption) { captionHolder.append(captionhtml); } }); //Size the image div to the tallest slide image function imageSizing() { $('#slide_images').css({height: 0}); var numImages = $('.image img').length; var numDone = 0; $('.image img').each(function() { $(this).load(function () { var w = $('#slide_images').outerWidth(); var ratio = w / this.naturalWidth; var curHeight = $('#slide_images').height() || 0; var displayH = this.naturalHeight * ratio; if (displayH > curHeight){ $('#slide_images').css({height: displayH}); } numDone++; if (numDone == numImages) { setSize(); numDone = 0; } $(this).off('load'); }); if (this.complete) { $(this).load(); } }); } if ($('.label').length == 0){ $('#slide_labels').hide(); $('#slide_captions').css({'border-top': 'none'}); } else { labelSizing(); } if ($('.caption').length == 0){ $('#slide_captions').hide(); } else { captionSizing(); } // build the slideline slider buildSlider(); //add ticks to slideline slider setSliderTicks(); //set heights of image container imageSizing(); //This function sets the size of the caption & label areas to the tallest ones function captionSizing() { $('#slide_captions').css({'height': 0}); var captionHeights = []; $('.caption').each(function() { var captionHeight = $(this).children('p').outerHeight(true); captionHeights.push(captionHeight); }); var tallestCaption = Math.max.apply(Math,captionHeights); captionHeights = []; $('#slide_captions').css({'height': tallestCaption + 20}); }; function labelSizing() { $('#slide_labels').css({'height': 0}); var labelHeights = []; $('.label').each(function() { var labelHeight = $(this).outerHeight(true); labelHeights.push(labelHeight); }); var tallestLabel = Math.max.apply(Math,labelHeights); labelHeights = []; $('#slide_labels').css({'height': tallestLabel + 20}); } $(document).on('mouseout', function(e) { e = e ? e : window.event; var from = e.relatedTarget || e.toElement; if (!from || from.nodeName == 'HTML'){ // this detects that the mouse has left the frame - // somehow need to trigger the slider stop event or use a hack } }); $(window).resize(function() { captionSizing(); labelSizing(); imageSizing(); }); window.addEventListener('orientationchange', function() { captionSizing(); labelSizing(); imageSizing(); }); $(window).load(function() { $('.ui-slider-handle').addClass('pulsing'); //FastClick.attach(document.body); }); function setSize(){ //console.log('set size called'); s9.view.size({ height: $(document.body).outerHeight(true) }); } });
var prevCaption; var currentCaption; var nextCaption;
random_line_split
main.js
if (document.location.hostname == "localhost" || document.location.hostname == "svn.inkling.com"){ s9.initialParams = { "image1": "img/slideline_placeholder_images_1.jpg", "caption1": "This is the first caption. This is optional.", "label1": "Label 1. This is optional.", "image2": "img/slideline_placeholder_images_2.jpg", "caption2": "This is the second caption. This is optional.", "label2": "Label 2. This is optional.", "image3": "img/slideline_placeholder_images_3.jpg", "caption3": "This is the third caption. This is optional.", "label3": "Label 3. This is optional.", "image4": "img/slideline_placeholder_images_4.jpg", "caption4": "This is the fourth caption. This is optional.", "label4": "Label 4. This is optional.", "image5": "img/slideline_placeholder_images_5.jpg", "caption5": "This is the fifth caption. This is optional.", "label5": "Label 5. This is optional.", "image6": "img/slideline_placeholder_images_6.jpg", "caption6": "This is the sixth caption. This is optional.", "label6": "Label 6. This is optional.", } }; $(document).ready(function(){ // variables for values var val; var wholeVal; var decVal; var last = 0; var lastStop = 0; // varables for each element var prevImage; var currentImage; var nextImage; var prevLabel; var currentLabel; var nextLabel; var prevCaption; var currentCaption; var nextCaption; // variables for catching and storing values during slide var oldVal; var currentVal; var newVal; var dataSource = s9.initialParams['dataSource']; function clearGhosts(start, end)
//This function initializes the jQuery slider and shows the first slide function buildSlider(){ $('#slide_images #image1').css("opacity", 1); $('#slide_labels #label1').css("opacity", 1); $('#slide_captions #caption1').css("opacity", 1); currentImage = document.getElementById('image1'); currentLabel = document.getElementById('label1'); currentCaption = document.getElementById('caption1'); $("#slider").slider({ animate: true, value: 1, min: 1, max: $('#slide_images .image').size(), step: .01, slide: function (event, ui) { $('.ui-slider-handle').removeClass('pulsing'); sliderPos = (ui.value); //ex: 1.25 wholeSliderPos = Math.floor(sliderPos); //ex: 1 decVal = sliderPos - wholeSliderPos; // ex: 1.25 - 1 (=.25) //console.log('sliding: ' + decVal); var rangeStart = Math.floor(sliderPos); var rangeEnd = Math.ceil(sliderPos); if (lastStop > 0 && lastStop != rangeStart && lastStop != rangeEnd){ var old = $('#image' + lastStop); old.css('opacity', 0); } prevImage = document.getElementById('image' + (wholeSliderPos - 1)); currentImage = document.getElementById('image' + wholeSliderPos); nextImage = document.getElementById('image' + (wholeSliderPos + 1)); prevLabel = document.getElementById('label' + (wholeSliderPos - 1)); currentLabel = document.getElementById('label' + wholeSliderPos); nextLabel = document.getElementById('label' + (wholeSliderPos + 1)); prevCaption = document.getElementById('caption' + (wholeSliderPos - 1)); currentCaption = document.getElementById('caption' + wholeSliderPos); nextCaption = document.getElementById('caption' + (wholeSliderPos + 1)); if (ui.value > last) { $(currentImage).css("opacity", 1 - decVal); $(nextImage).css("opacity", decVal); } if (ui.value < last) { $(currentImage).css("opacity", 1 - decVal); $(nextImage).css("opacity", decVal); } if (Math.floor(last) != wholeSliderPos) { clearGhosts(rangeStart, rangeEnd); } last = ui.value; }, stop: function( event, ui ) { $('.ui-slider-handle').removeClass('pulsing'); var wholeVal = Math.round(ui.value); $( "#slider" ).slider( "value", wholeVal ); // console.log('stop: ' + wholeVal); prevImage = document.getElementById('image' + (wholeVal - 1)); currentImage = document.getElementById('image' + wholeVal); nextImage = document.getElementById('image' + (wholeVal + 1)); prevLabel = document.getElementById('label' + (wholeVal - 1)); currentLabel = document.getElementById('label' + wholeVal); nextLabel = document.getElementById('label' + (wholeVal + 1)); prevCaption = document.getElementById('caption' + (wholeVal - 1)); currentCaption = document.getElementById('caption' + wholeVal); nextCaption = document.getElementById('caption' + (wholeVal + 1)); $('.image').css("opacity", 0); $('.label').css("opacity", 0); $('.caption').css("opacity", 0); $(currentImage).css("opacity", 1); $(currentLabel).css("opacity", 1); $(currentCaption).css("opacity", 1); last = wholeVal; lastStop = wholeVal; } }); } //This function draws the tick marks/pips for the slider. It must be called after the slider's max is set. function setSliderTicks(){ var $slider = $('#slider'); var max = $slider.slider("option", "max"); if (max > 1) { var spacing = 100 / (max -1); } else { var spacing = 50; } $slider.find('.ui-slider-tick-mark').remove(); for (var i = 0; i < max ; i++) { $('<span class="ui-slider-tick-mark"></span>').css('left', (spacing * i) + '%').appendTo($slider); } } // Extract the text from the template .html() is the jquery helper method for that var image_template = $('#image-temp').html(); var label_template = $('#label-temp').html(); var caption_template = $('#caption-temp').html(); // Compile that into an handlebars template var imageTemplate = Handlebars.compile(image_template); var labelTemplate = Handlebars.compile(label_template); var captionTemplate = Handlebars.compile(caption_template); // Retrieve the placeHolder where the Posts will be displayed var imageHolder = $("#slide_images"); var labelHolder = $("#slide_labels"); var captionHolder = $("#slide_captions"); var imgArr = [] var slideNum = 0; for(var key in s9.initialParams) { if (key.slice(0, 5) == 'image'){ slideNum++; imgArr.push(s9.initialParams[key]); } } var slideInfo = { 'slideData': [] }; for(var i=0; i<slideNum; i++){ var temp = i+1; // console.log("loading slide " + temp); if (!s9.initialParams['image'+(i+1)]) { continue; } slideInfo['slideData'].push({ image : s9.initialParams['image'+(i+1)], caption : s9.initialParams['caption'+(i+1)], label : s9.initialParams['label'+(i+1)], index : temp }); }; if (slideInfo.slideData.length == 0){ //Show default - placeholder images. slideInfo.slideData = [{ image: 'img/placeholder-image1.svg', caption: 'Caption 1', label: 'Label 1', index: 1, }, { image: 'img/placeholder-image2.svg', caption: 'Caption 2', label: 'Label 2', index: 2, }, { image: 'img/placeholder-image3.svg', caption: 'Caption 3', label: 'Label 3', index: 3, }]; } //Add each of the slide images, labels, and captions to the DOM $.each(slideInfo.slideData,function(index,element){ // Generate the HTML for each post var imagehtml = imageTemplate(element); var labelhtml = labelTemplate(element); var captionhtml = captionTemplate(element); // Render the posts into the page imageHolder.append(imagehtml); if (element.label){ labelHolder.append(labelhtml); } if (element.caption) { captionHolder.append(captionhtml); } }); //Size the image div to the tallest slide image function imageSizing() { $('#slide_images').css({height: 0}); var numImages = $('.image img').length; var numDone = 0; $('.image img').each(function() { $(this).load(function () { var w = $('#slide_images').outerWidth(); var ratio = w / this.naturalWidth; var curHeight = $('#slide_images').height() || 0; var displayH = this.naturalHeight * ratio; if (displayH > curHeight){ $('#slide_images').css({height: displayH}); } numDone++; if (numDone == numImages) { setSize(); numDone = 0; } $(this).off('load'); }); if (this.complete) { $(this).load(); } }); } if ($('.label').length == 0){ $('#slide_labels').hide(); $('#slide_captions').css({'border-top': 'none'}); } else { labelSizing(); } if ($('.caption').length == 0){ $('#slide_captions').hide(); } else { captionSizing(); } // build the slideline slider buildSlider(); //add ticks to slideline slider setSliderTicks(); //set heights of image container imageSizing(); //This function sets the size of the caption & label areas to the tallest ones function captionSizing() { $('#slide_captions').css({'height': 0}); var captionHeights = []; $('.caption').each(function() { var captionHeight = $(this).children('p').outerHeight(true); captionHeights.push(captionHeight); }); var tallestCaption = Math.max.apply(Math,captionHeights); captionHeights = []; $('#slide_captions').css({'height': tallestCaption + 20}); }; function labelSizing() { $('#slide_labels').css({'height': 0}); var labelHeights = []; $('.label').each(function() { var labelHeight = $(this).outerHeight(true); labelHeights.push(labelHeight); }); var tallestLabel = Math.max.apply(Math,labelHeights); labelHeights = []; $('#slide_labels').css({'height': tallestLabel + 20}); } $(document).on('mouseout', function(e) { e = e ? e : window.event; var from = e.relatedTarget || e.toElement; if (!from || from.nodeName == 'HTML'){ // this detects that the mouse has left the frame - // somehow need to trigger the slider stop event or use a hack } }); $(window).resize(function() { captionSizing(); labelSizing(); imageSizing(); }); window.addEventListener('orientationchange', function() { captionSizing(); labelSizing(); imageSizing(); }); $(window).load(function() { $('.ui-slider-handle').addClass('pulsing'); //FastClick.attach(document.body); }); function setSize(){ //console.log('set size called'); s9.view.size({ height: $(document.body).outerHeight(true) }); } });
{ $('.image').each(function() { if (this.getAttribute('id') == 'image' + start || this.getAttribute('id') == 'image' + end) { return; } $(this).css({opacity: 0}); }); }
identifier_body
iconfont.js
;(function(window) { var svgSprite = '<svg>' + '' + '<symbol id="icon-biaoqian" viewBox="0 0 1024 1024">' + '' + '<path d="M862.265432 448.791399l19.793812-255.17368c2.230808-28.754904-21.728884-52.714596-50.483788-50.483788L576.401776 162.927743l-0.317225-0.316202L110.7555 627.940592l286.497083 286.497083 465.329051-465.330074L862.265432 448.791399zM660.940171 364.253004c-24.186865-24.186865-24.186865-63.403029 0-87.589894 24.186865-24.186865 63.402005-24.186865 87.589894 0s24.186865 63.402005 0 87.589894C724.342176 388.440893 685.127036 388.440893 660.940171 364.253004z" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-024" viewBox="0 0 1024 1024">' + '' + '<path d="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z" fill="#EDC55D" ></path>' + '' + '<path d="M843.632 248.656l-3.664 2.616c-77.856-24.472-169.048 37.816-169.048 37.816l-133.464-4.448-155.704 262.48-141.704 107.136 364.72 361.192C843.216 971.792 1024 763.072 1024 512c0-31.864-3.064-63.016-8.632-93.272l-171.736-170.072z" fill="#D9A93F" ></path>' + '' + '<path d="M726.064 247.984c-55.768 16.704-107.904 48.096-99.624 75.784l15.328-4.584c-3.264-10.952 29.72-38.16 88.888-55.856 59.16-17.72 101.672-13.128 104.952-2.176l15.328-4.584c-8.296-27.704-69.096-25.28-124.872-8.584z" fill="#A6592E" ></path>' + '' + '<path d="M652.656 257.576l-143.488-1.304c-25.512-0.232-60.608 13.944-77.968 31.504L184.504 537.368c-17.36 17.552-16.216 47.112 2.56 65.672l172.904 170.944c18.768 18.56 48.344 19.384 65.704 1.824l246.704-249.584c17.36-17.56 31.128-52.816 30.592-78.328l-2.936-143.488c-0.528-25.528-21.848-46.592-47.376-46.832z m-7.08 101.04c-11.856 12.008-31.904 11.456-44.736-1.24-12.84-12.688-13.624-32.72-1.768-44.728 11.872-12.016 31.92-11.464 44.752 1.232 12.848 12.696 13.632 32.728 1.752 44.736z" fill="#3CB19C" ></path>' + '' + '<path d="M359.92 654.24c-56.264 0-109.92-13.168-158.016-36.512l158.064 156.256c18.768 18.56 48.344 19.384 65.704 1.824l246.704-249.584c17.36-17.56 31.128-52.816 30.592-78.328l-0.576-28.24C647.936 555.896 513.736 654.24 359.92 654.24z" fill="#30A28C" ></path>' + '' + '<path d="M746.72 317.008c-59.16 17.712-101.672 13.112-104.952 2.168l-15.328 4.584c8.296 27.688 69.096 25.264 124.872 8.568 55.784-16.704 107.904-48.096 99.624-75.784l-15.328 4.584c3.28 10.952-29.736 38.16-88.888 55.88z" fill="#A6592E" ></path>' + '' + '<path d="M422.608 646.336a4.816 4.816 0 0 1-4.816-4.808V417.888a4.824 4.824 0 0 1 9.632 0v223.64c0 2.656-2.16 4.808-4.816 4.808z" fill="#FDF1F6" ></path>' + '' + '<path d="M422.608 484.28a4.736 4.736 0 0 1-3.56-1.584l-28.704-31.624a4.824 4.824 0 0 1 0.328-6.808 4.824 4.824 0 0 1 6.8 0.336l28.704 31.632a4.824 4.824 0 0 1-3.568 8.048z" fill="#FDF1F6" ></path>' + '' + '<path d="M422.608 484.28a4.8 4.8 0 0 1-3.56-8.048l28.688-31.632a4.824 4.824 0 0 1 7.144 6.472l-28.704 31.624a4.816 4.816 0 0 1-3.568 1.584zM451.312 616.384a4.816 4.816 0 0 1-3.576-1.576l-28.688-31.624a4.816 4.816 0 0 1 7.128-6.464l28.704 31.632a4.808 4.808 0 0 1-3.568 8.032z" fill="#FDF1F6" ></path>' + '' + '<path d="M393.904 616.384a4.808 4.808 0 0 1-3.56-8.032l28.704-31.632a4.816 4.816 0 0 1 7.128 6.464l-28.704 31.624a4.84 4.84 0 0 1-3.568 1.576zM534.424 534.512H310.784a4.816 4.816 0 1 1 0-9.632h223.64a4.808 4.808 0 1 1 0 9.632z" fill="#FDF1F6" ></path>' + '' + '<path d="M472.84 534.512a4.8 4.8 0 0 1-3.232-8.368l31.624-28.704a4.824 4.824 0 0 1 6.8 0.328 4.8 4.8 0 0 1-0.328 6.8l-31.624 28.704a4.808 4.808 0 0 1-3.24 1.24z" fill="#FDF1F6" ></path>' + '' + '<path d="M504.488 563.216a4.856 4.856 0 0 1-3.248-1.248l-31.624-28.696a4.8 4.8 0 1 1 6.472-7.128l31.624 28.688a4.816 4.816 0 0 1-3.224 8.384z" fill="#FDF1F6" ></path>' + '' + '<path d="M340.736 563.216a4.824 4.824 0 0 1-3.232-8.384l31.624-28.688a4.808 4.808 0 1 1 6.456 7.128l-31.608 28.696a4.808 4.808 0 0 1-3.24 1.248z" fill="#FDF1F6" ></path>' + '' + '<path d="M372.376 534.512a4.872 4.872 0 0 1-3.248-1.24l-31.624-28.704a4.816 4.816 0 0 1 6.472-7.128l31.608 28.704a4.816 4.816 0 0 1-3.208 8.368z" fill="#FDF1F6" ></path>' + '' + '<path d="M343.544 613.584a4.864 4.864 0 0 1-3.44-1.416 4.84 4.84 0 0 1 0-6.792l158.16-158.152a4.824 4.824 0 0 1 6.816 6.808L346.936 612.168a4.736 4.736 0 0 1-3.392 1.416z" fill="#FDF1F6" ></path>' + '' + '<path d="M466.456 490.664l-0.232-0.008a4.784 4.784 0 0 1-4.56-5.032l2.56-52.664a4.856 4.856 0 0 1 5.032-4.568 4.776 4.776 0 0 1 4.56 5.04l-2.544 52.664a4.816 4.816 0 0 1-4.816 4.568z" fill="#FDF1F6" ></path>' + '' + '<path d="M466.456 490.664a4.816 4.816 0 0 1-0.24-9.616l52.656-2.56a4.792 4.792 0 0 1 5.032 4.568 4.8 4.8 0 0 1-4.56 5.048l-52.656 2.552-0.232 0.008z" fill="#FDF1F6" ></path>' + '' + '<path d="M376.184 631.024h-0.248a4.824 4.824 0 0 1-4.56-5.048l2.56-52.648a4.832 4.832 0 0 1 5.032-4.568 4.8 4.8 0 0 1 4.576 5.04l-2.56 52.648a4.808 4.808 0 0 1-4.8 4.576z" fill="#FDF1F6" ></path>' + '' + '<path d="M326.08 580.928a4.808 4.808 0 0 1-0.224-9.608l52.656-2.56a4.88 4.88 0 0 1 5.032 4.568 4.792 4.792 0 0 1-4.576 5.032l-52.64 2.568h-0.248z" fill="#FDF1F6" ></path>' + '' + '<path d="M501.672 613.584a4.816 4.816 0 0 1-3.408-1.416l-158.16-158.136a4.856 4.856 0 0 1 0-6.808 4.856 4.856 0 0 1 6.832 0l158.128 158.152a4.784 4.784 0 0 1 0 6.792 4.752 4.752 0 0 1-3.392 1.416z" fill="#FDF1F6" ></path>' + '' + '<path d="M519.128 580.928l-0.248-0.008-52.656-2.56a4.784 4.784 0 0 1-4.56-5.032 4.832 4.832 0 0 1 5.032-4.568l52.656 2.56a4.784 4.784 0 0 1 4.56 5.032 4.784 4.784 0 0 1-4.784 4.576z" fill="#FDF1F6" ></path>' + '' + '<path d="M469.016 631.024a4.824 4.824 0 0 1-4.816-4.576l-2.544-52.648a4.8 4.8 0 0 1 4.56-5.04 4.872 4.872 0 0 1 5.048 4.568l2.544 52.656a4.816 4.816 0 0 1-4.56 5.04h-0.232z" fill="#FDF1F6" ></path>' + '' + '<path d="M378.752 490.664l-0.232-0.008-52.656-2.552a4.808 4.808 0 0 1 0.472-9.616l52.64 2.56a4.8 4.8 0 0 1-0.224 9.616z" fill="#FDF1F6" ></path>' + '' + '<path d="M378.736 490.664a4.824 4.824 0 0 1-4.8-4.576l-2.544-52.656a4.808 4.808 0 0 1 4.56-5.04 4.848 4.848 0 0 1 5.032 4.568l2.56 52.664a4.792 4.792 0 0 1-4.576 5.032l-0.232 0.008z" fill="#FDF1F6" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-chakangengduo" viewBox="0 0 1027 1024">' + '' + '<path d="M512 0c-282.799601 0-512 229.200399-512 512s229.200399 512 512 512S1024 794.799601 1024 512 794.799601 0 512 0z m1.020937 989.798604c-263.912263 0-477.798604-213.886341-477.798604-477.798604s213.886341-477.798604 477.798604-477.798604 477.798604 213.886341 477.798604 477.798604-213.886341 477.798604-477.798604 477.798604z" fill="" ></path>' + '' + '<path d="M860.139581 512c0-4.594217-2.041874-9.188435-5.104686-12.251246l-191.936191-191.936192c-3.062812-3.062812-7.14656-4.594217-11.740778-4.594217-9.188435 0-16.845464 7.657029-16.845463 16.845464 0 4.594217 1.531406 8.677966 4.594217 11.740777l0.510469 0.510469 161.308075 161.308076h-620.219342c-9.188435 0-16.845464 7.657029-16.845463 16.845463s7.657029 16.845464 16.845463 16.845464h623.792622l-164.881355 164.881356c-3.062812 3.062812-5.104686 7.14656-5.104686 12.251246 0 9.188435 7.657029 16.845464 16.845463 16.845464 4.594217 0 8.677966-2.041874 11.740778-5.104686l180.195414-180.195414 12.251246-12.251246c2.552343-3.062812 4.594217-7.14656 4.594217-11.740778z" fill="" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-chakangengduo1" viewBox="0 0 1024 1024">' + '' + '<path d="M67.54304 958.23872l-24.68352-30.31552 519.63904-423.17824L39.67488 81.51552l24.6016-30.39232 560.25088 453.53472z" ></path>' + '' + '<path d="M435.01056 971.13088l-24.68352-30.31552 519.64416-423.17824-522.82368-423.23456 24.59648-30.3872 560.25088 453.53472z" ></path>' + '' + '</symbol>' + '' + '</svg>' var script = function() { var scripts = document.getElementsByTagName('script') return scripts[scripts.length - 1] }() var shouldInjectCss = script.getAttribute("data-injectcss") /** * document ready */ var ready = function(fn) { if (document.addEventListener) { if (~["complete", "loaded", "interactive"].indexOf(document.readyState)) { setTimeout(fn, 0) } else { var loadFn = function() { document.removeEventListener("DOMContentLoaded", loadFn, false) fn() } document.addEventListener("DOMContentLoaded", loadFn, false) } } else if (document.attachEvent) { IEContentLoaded(window, fn) } function IEContentLoaded(w, fn)
} /** * Insert el before target * * @param {Element} el * @param {Element} target */ var before = function(el, target) { target.parentNode.insertBefore(el, target) } /** * Prepend el to target * * @param {Element} el * @param {Element} target */ var prepend = function(el, target) { if (target.firstChild) { before(el, target.firstChild) } else { target.appendChild(el) } } function appendSvg() { var div, svg div = document.createElement('div') div.innerHTML = svgSprite svgSprite = null svg = div.getElementsByTagName('svg')[0] if (svg) { svg.setAttribute('aria-hidden', 'true') svg.style.position = 'absolute' svg.style.width = 0 svg.style.height = 0 svg.style.overflow = 'hidden' prepend(svg, document.body) } } if (shouldInjectCss && !window.__iconfont__svg__cssinject__) { window.__iconfont__svg__cssinject__ = true try { document.write("<style>.svgfont {display: inline-block;width: 1em;height: 1em;fill: currentColor;vertical-align: -0.1em;font-size:16px;}</style>"); } catch (e) { console && console.log(e) } } ready(appendSvg) })(window)
{ var d = w.document, done = false, // only fire once init = function() { if (!done) { done = true fn() } } // polling for no errors var polling = function() { try { // throws errors until after ondocumentready d.documentElement.doScroll('left') } catch (e) { setTimeout(polling, 50) return } // no errors, fire init() }; polling() // trying to always fire before onload d.onreadystatechange = function() { if (d.readyState == 'complete') { d.onreadystatechange = null init() } } }
identifier_body
iconfont.js
;(function(window) { var svgSprite = '<svg>' + '' + '<symbol id="icon-biaoqian" viewBox="0 0 1024 1024">' + '' + '<path d="M862.265432 448.791399l19.793812-255.17368c2.230808-28.754904-21.728884-52.714596-50.483788-50.483788L576.401776 162.927743l-0.317225-0.316202L110.7555 627.940592l286.497083 286.497083 465.329051-465.330074L862.265432 448.791399zM660.940171 364.253004c-24.186865-24.186865-24.186865-63.403029 0-87.589894 24.186865-24.186865 63.402005-24.186865 87.589894 0s24.186865 63.402005 0 87.589894C724.342176 388.440893 685.127036 388.440893 660.940171 364.253004z" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-024" viewBox="0 0 1024 1024">' + '' + '<path d="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z" fill="#EDC55D" ></path>' + '' + '<path d="M843.632 248.656l-3.664 2.616c-77.856-24.472-169.048 37.816-169.048 37.816l-133.464-4.448-155.704 262.48-141.704 107.136 364.72 361.192C843.216 971.792 1024 763.072 1024 512c0-31.864-3.064-63.016-8.632-93.272l-171.736-170.072z" fill="#D9A93F" ></path>' + '' + '<path d="M726.064 247.984c-55.768 16.704-107.904 48.096-99.624 75.784l15.328-4.584c-3.264-10.952 29.72-38.16 88.888-55.856 59.16-17.72 101.672-13.128 104.952-2.176l15.328-4.584c-8.296-27.704-69.096-25.28-124.872-8.584z" fill="#A6592E" ></path>' + '' + '<path d="M652.656 257.576l-143.488-1.304c-25.512-0.232-60.608 13.944-77.968 31.504L184.504 537.368c-17.36 17.552-16.216 47.112 2.56 65.672l172.904 170.944c18.768 18.56 48.344 19.384 65.704 1.824l246.704-249.584c17.36-17.56 31.128-52.816 30.592-78.328l-2.936-143.488c-0.528-25.528-21.848-46.592-47.376-46.832z m-7.08 101.04c-11.856 12.008-31.904 11.456-44.736-1.24-12.84-12.688-13.624-32.72-1.768-44.728 11.872-12.016 31.92-11.464 44.752 1.232 12.848 12.696 13.632 32.728 1.752 44.736z" fill="#3CB19C" ></path>' + '' + '<path d="M359.92 654.24c-56.264 0-109.92-13.168-158.016-36.512l158.064 156.256c18.768 18.56 48.344 19.384 65.704 1.824l246.704-249.584c17.36-17.56 31.128-52.816 30.592-78.328l-0.576-28.24C647.936 555.896 513.736 654.24 359.92 654.24z" fill="#30A28C" ></path>' + '' + '<path d="M746.72 317.008c-59.16 17.712-101.672 13.112-104.952 2.168l-15.328 4.584c8.296 27.688 69.096 25.264 124.872 8.568 55.784-16.704 107.904-48.096 99.624-75.784l-15.328 4.584c3.28 10.952-29.736 38.16-88.888 55.88z" fill="#A6592E" ></path>' + '' + '<path d="M422.608 646.336a4.816 4.816 0 0 1-4.816-4.808V417.888a4.824 4.824 0 0 1 9.632 0v223.64c0 2.656-2.16 4.808-4.816 4.808z" fill="#FDF1F6" ></path>' + '' + '<path d="M422.608 484.28a4.736 4.736 0 0 1-3.56-1.584l-28.704-31.624a4.824 4.824 0 0 1 0.328-6.808 4.824 4.824 0 0 1 6.8 0.336l28.704 31.632a4.824 4.824 0 0 1-3.568 8.048z" fill="#FDF1F6" ></path>' + '' + '<path d="M422.608 484.28a4.8 4.8 0 0 1-3.56-8.048l28.688-31.632a4.824 4.824 0 0 1 7.144 6.472l-28.704 31.624a4.816 4.816 0 0 1-3.568 1.584zM451.312 616.384a4.816 4.816 0 0 1-3.576-1.576l-28.688-31.624a4.816 4.816 0 0 1 7.128-6.464l28.704 31.632a4.808 4.808 0 0 1-3.568 8.032z" fill="#FDF1F6" ></path>' + '' + '<path d="M393.904 616.384a4.808 4.808 0 0 1-3.56-8.032l28.704-31.632a4.816 4.816 0 0 1 7.128 6.464l-28.704 31.624a4.84 4.84 0 0 1-3.568 1.576zM534.424 534.512H310.784a4.816 4.816 0 1 1 0-9.632h223.64a4.808 4.808 0 1 1 0 9.632z" fill="#FDF1F6" ></path>' + '' + '<path d="M472.84 534.512a4.8 4.8 0 0 1-3.232-8.368l31.624-28.704a4.824 4.824 0 0 1 6.8 0.328 4.8 4.8 0 0 1-0.328 6.8l-31.624 28.704a4.808 4.808 0 0 1-3.24 1.24z" fill="#FDF1F6" ></path>' + '' + '<path d="M504.488 563.216a4.856 4.856 0 0 1-3.248-1.248l-31.624-28.696a4.8 4.8 0 1 1 6.472-7.128l31.624 28.688a4.816 4.816 0 0 1-3.224 8.384z" fill="#FDF1F6" ></path>' + '' + '<path d="M340.736 563.216a4.824 4.824 0 0 1-3.232-8.384l31.624-28.688a4.808 4.808 0 1 1 6.456 7.128l-31.608 28.696a4.808 4.808 0 0 1-3.24 1.248z" fill="#FDF1F6" ></path>' + '' + '<path d="M372.376 534.512a4.872 4.872 0 0 1-3.248-1.24l-31.624-28.704a4.816 4.816 0 0 1 6.472-7.128l31.608 28.704a4.816 4.816 0 0 1-3.208 8.368z" fill="#FDF1F6" ></path>' + '' + '<path d="M343.544 613.584a4.864 4.864 0 0 1-3.44-1.416 4.84 4.84 0 0 1 0-6.792l158.16-158.152a4.824 4.824 0 0 1 6.816 6.808L346.936 612.168a4.736 4.736 0 0 1-3.392 1.416z" fill="#FDF1F6" ></path>' + '' + '<path d="M466.456 490.664l-0.232-0.008a4.784 4.784 0 0 1-4.56-5.032l2.56-52.664a4.856 4.856 0 0 1 5.032-4.568 4.776 4.776 0 0 1 4.56 5.04l-2.544 52.664a4.816 4.816 0 0 1-4.816 4.568z" fill="#FDF1F6" ></path>' + '' + '<path d="M466.456 490.664a4.816 4.816 0 0 1-0.24-9.616l52.656-2.56a4.792 4.792 0 0 1 5.032 4.568 4.8 4.8 0 0 1-4.56 5.048l-52.656 2.552-0.232 0.008z" fill="#FDF1F6" ></path>' + '' + '<path d="M376.184 631.024h-0.248a4.824 4.824 0 0 1-4.56-5.048l2.56-52.648a4.832 4.832 0 0 1 5.032-4.568 4.8 4.8 0 0 1 4.576 5.04l-2.56 52.648a4.808 4.808 0 0 1-4.8 4.576z" fill="#FDF1F6" ></path>' + '' + '<path d="M326.08 580.928a4.808 4.808 0 0 1-0.224-9.608l52.656-2.56a4.88 4.88 0 0 1 5.032 4.568 4.792 4.792 0 0 1-4.576 5.032l-52.64 2.568h-0.248z" fill="#FDF1F6" ></path>' + '' + '<path d="M501.672 613.584a4.816 4.816 0 0 1-3.408-1.416l-158.16-158.136a4.856 4.856 0 0 1 0-6.808 4.856 4.856 0 0 1 6.832 0l158.128 158.152a4.784 4.784 0 0 1 0 6.792 4.752 4.752 0 0 1-3.392 1.416z" fill="#FDF1F6" ></path>' + '' + '<path d="M519.128 580.928l-0.248-0.008-52.656-2.56a4.784 4.784 0 0 1-4.56-5.032 4.832 4.832 0 0 1 5.032-4.568l52.656 2.56a4.784 4.784 0 0 1 4.56 5.032 4.784 4.784 0 0 1-4.784 4.576z" fill="#FDF1F6" ></path>' + '' + '<path d="M469.016 631.024a4.824 4.824 0 0 1-4.816-4.576l-2.544-52.648a4.8 4.8 0 0 1 4.56-5.04 4.872 4.872 0 0 1 5.048 4.568l2.544 52.656a4.816 4.816 0 0 1-4.56 5.04h-0.232z" fill="#FDF1F6" ></path>' + '' + '<path d="M378.752 490.664l-0.232-0.008-52.656-2.552a4.808 4.808 0 0 1 0.472-9.616l52.64 2.56a4.8 4.8 0 0 1-0.224 9.616z" fill="#FDF1F6" ></path>' + '' + '<path d="M378.736 490.664a4.824 4.824 0 0 1-4.8-4.576l-2.544-52.656a4.808 4.808 0 0 1 4.56-5.04 4.848 4.848 0 0 1 5.032 4.568l2.56 52.664a4.792 4.792 0 0 1-4.576 5.032l-0.232 0.008z" fill="#FDF1F6" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-chakangengduo" viewBox="0 0 1027 1024">' + '' + '<path d="M512 0c-282.799601 0-512 229.200399-512 512s229.200399 512 512 512S1024 794.799601 1024 512 794.799601 0 512 0z m1.020937 989.798604c-263.912263 0-477.798604-213.886341-477.798604-477.798604s213.886341-477.798604 477.798604-477.798604 477.798604 213.886341 477.798604 477.798604-213.886341 477.798604-477.798604 477.798604z" fill="" ></path>' + '' + '<path d="M860.139581 512c0-4.594217-2.041874-9.188435-5.104686-12.251246l-191.936191-191.936192c-3.062812-3.062812-7.14656-4.594217-11.740778-4.594217-9.188435 0-16.845464 7.657029-16.845463 16.845464 0 4.594217 1.531406 8.677966 4.594217 11.740777l0.510469 0.510469 161.308075 161.308076h-620.219342c-9.188435 0-16.845464 7.657029-16.845463 16.845463s7.657029 16.845464 16.845463 16.845464h623.792622l-164.881355 164.881356c-3.062812 3.062812-5.104686 7.14656-5.104686 12.251246 0 9.188435 7.657029 16.845464 16.845463 16.845464 4.594217 0 8.677966-2.041874 11.740778-5.104686l180.195414-180.195414 12.251246-12.251246c2.552343-3.062812 4.594217-7.14656 4.594217-11.740778z" fill="" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-chakangengduo1" viewBox="0 0 1024 1024">' + '' + '<path d="M67.54304 958.23872l-24.68352-30.31552 519.63904-423.17824L39.67488 81.51552l24.6016-30.39232 560.25088 453.53472z" ></path>' + '' + '<path d="M435.01056 971.13088l-24.68352-30.31552 519.64416-423.17824-522.82368-423.23456 24.59648-30.3872 560.25088 453.53472z" ></path>' + '' + '</symbol>' + '' + '</svg>' var script = function() { var scripts = document.getElementsByTagName('script') return scripts[scripts.length - 1] }() var shouldInjectCss = script.getAttribute("data-injectcss") /** * document ready */ var ready = function(fn) { if (document.addEventListener) { if (~["complete", "loaded", "interactive"].indexOf(document.readyState)) { setTimeout(fn, 0) } else { var loadFn = function() { document.removeEventListener("DOMContentLoaded", loadFn, false) fn() } document.addEventListener("DOMContentLoaded", loadFn, false) } } else if (document.attachEvent) { IEContentLoaded(window, fn) } function IEContentLoaded(w, fn) { var d = w.document, done = false, // only fire once init = function() { if (!done) { done = true fn() } } // polling for no errors var polling = function() { try { // throws errors until after ondocumentready d.documentElement.doScroll('left') } catch (e) { setTimeout(polling, 50) return } // no errors, fire init() }; polling() // trying to always fire before onload d.onreadystatechange = function() { if (d.readyState == 'complete') { d.onreadystatechange = null init() } } } } /** * Insert el before target * * @param {Element} el * @param {Element} target */ var before = function(el, target) { target.parentNode.insertBefore(el, target) } /** * Prepend el to target * * @param {Element} el * @param {Element} target */ var prepend = function(el, target) { if (target.firstChild)
else { target.appendChild(el) } } function appendSvg() { var div, svg div = document.createElement('div') div.innerHTML = svgSprite svgSprite = null svg = div.getElementsByTagName('svg')[0] if (svg) { svg.setAttribute('aria-hidden', 'true') svg.style.position = 'absolute' svg.style.width = 0 svg.style.height = 0 svg.style.overflow = 'hidden' prepend(svg, document.body) } } if (shouldInjectCss && !window.__iconfont__svg__cssinject__) { window.__iconfont__svg__cssinject__ = true try { document.write("<style>.svgfont {display: inline-block;width: 1em;height: 1em;fill: currentColor;vertical-align: -0.1em;font-size:16px;}</style>"); } catch (e) { console && console.log(e) } } ready(appendSvg) })(window)
{ before(el, target.firstChild) }
conditional_block
iconfont.js
;(function(window) { var svgSprite = '<svg>' + '' + '<symbol id="icon-biaoqian" viewBox="0 0 1024 1024">' + '' + '<path d="M862.265432 448.791399l19.793812-255.17368c2.230808-28.754904-21.728884-52.714596-50.483788-50.483788L576.401776 162.927743l-0.317225-0.316202L110.7555 627.940592l286.497083 286.497083 465.329051-465.330074L862.265432 448.791399zM660.940171 364.253004c-24.186865-24.186865-24.186865-63.403029 0-87.589894 24.186865-24.186865 63.402005-24.186865 87.589894 0s24.186865 63.402005 0 87.589894C724.342176 388.440893 685.127036 388.440893 660.940171 364.253004z" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-024" viewBox="0 0 1024 1024">' + '' + '<path d="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z" fill="#EDC55D" ></path>' + '' + '<path d="M843.632 248.656l-3.664 2.616c-77.856-24.472-169.048 37.816-169.048 37.816l-133.464-4.448-155.704 262.48-141.704 107.136 364.72 361.192C843.216 971.792 1024 763.072 1024 512c0-31.864-3.064-63.016-8.632-93.272l-171.736-170.072z" fill="#D9A93F" ></path>' + '' + '<path d="M726.064 247.984c-55.768 16.704-107.904 48.096-99.624 75.784l15.328-4.584c-3.264-10.952 29.72-38.16 88.888-55.856 59.16-17.72 101.672-13.128 104.952-2.176l15.328-4.584c-8.296-27.704-69.096-25.28-124.872-8.584z" fill="#A6592E" ></path>' + '' + '<path d="M652.656 257.576l-143.488-1.304c-25.512-0.232-60.608 13.944-77.968 31.504L184.504 537.368c-17.36 17.552-16.216 47.112 2.56 65.672l172.904 170.944c18.768 18.56 48.344 19.384 65.704 1.824l246.704-249.584c17.36-17.56 31.128-52.816 30.592-78.328l-2.936-143.488c-0.528-25.528-21.848-46.592-47.376-46.832z m-7.08 101.04c-11.856 12.008-31.904 11.456-44.736-1.24-12.84-12.688-13.624-32.72-1.768-44.728 11.872-12.016 31.92-11.464 44.752 1.232 12.848 12.696 13.632 32.728 1.752 44.736z" fill="#3CB19C" ></path>' + '' + '<path d="M359.92 654.24c-56.264 0-109.92-13.168-158.016-36.512l158.064 156.256c18.768 18.56 48.344 19.384 65.704 1.824l246.704-249.584c17.36-17.56 31.128-52.816 30.592-78.328l-0.576-28.24C647.936 555.896 513.736 654.24 359.92 654.24z" fill="#30A28C" ></path>' + '' + '<path d="M746.72 317.008c-59.16 17.712-101.672 13.112-104.952 2.168l-15.328 4.584c8.296 27.688 69.096 25.264 124.872 8.568 55.784-16.704 107.904-48.096 99.624-75.784l-15.328 4.584c3.28 10.952-29.736 38.16-88.888 55.88z" fill="#A6592E" ></path>' + '' + '<path d="M422.608 646.336a4.816 4.816 0 0 1-4.816-4.808V417.888a4.824 4.824 0 0 1 9.632 0v223.64c0 2.656-2.16 4.808-4.816 4.808z" fill="#FDF1F6" ></path>' + '' + '<path d="M422.608 484.28a4.736 4.736 0 0 1-3.56-1.584l-28.704-31.624a4.824 4.824 0 0 1 0.328-6.808 4.824 4.824 0 0 1 6.8 0.336l28.704 31.632a4.824 4.824 0 0 1-3.568 8.048z" fill="#FDF1F6" ></path>' + '' + '<path d="M422.608 484.28a4.8 4.8 0 0 1-3.56-8.048l28.688-31.632a4.824 4.824 0 0 1 7.144 6.472l-28.704 31.624a4.816 4.816 0 0 1-3.568 1.584zM451.312 616.384a4.816 4.816 0 0 1-3.576-1.576l-28.688-31.624a4.816 4.816 0 0 1 7.128-6.464l28.704 31.632a4.808 4.808 0 0 1-3.568 8.032z" fill="#FDF1F6" ></path>' + '' + '<path d="M393.904 616.384a4.808 4.808 0 0 1-3.56-8.032l28.704-31.632a4.816 4.816 0 0 1 7.128 6.464l-28.704 31.624a4.84 4.84 0 0 1-3.568 1.576zM534.424 534.512H310.784a4.816 4.816 0 1 1 0-9.632h223.64a4.808 4.808 0 1 1 0 9.632z" fill="#FDF1F6" ></path>' + '' + '<path d="M472.84 534.512a4.8 4.8 0 0 1-3.232-8.368l31.624-28.704a4.824 4.824 0 0 1 6.8 0.328 4.8 4.8 0 0 1-0.328 6.8l-31.624 28.704a4.808 4.808 0 0 1-3.24 1.24z" fill="#FDF1F6" ></path>' + '' + '<path d="M504.488 563.216a4.856 4.856 0 0 1-3.248-1.248l-31.624-28.696a4.8 4.8 0 1 1 6.472-7.128l31.624 28.688a4.816 4.816 0 0 1-3.224 8.384z" fill="#FDF1F6" ></path>' + '' + '<path d="M340.736 563.216a4.824 4.824 0 0 1-3.232-8.384l31.624-28.688a4.808 4.808 0 1 1 6.456 7.128l-31.608 28.696a4.808 4.808 0 0 1-3.24 1.248z" fill="#FDF1F6" ></path>' + '' + '<path d="M372.376 534.512a4.872 4.872 0 0 1-3.248-1.24l-31.624-28.704a4.816 4.816 0 0 1 6.472-7.128l31.608 28.704a4.816 4.816 0 0 1-3.208 8.368z" fill="#FDF1F6" ></path>' + '' + '<path d="M343.544 613.584a4.864 4.864 0 0 1-3.44-1.416 4.84 4.84 0 0 1 0-6.792l158.16-158.152a4.824 4.824 0 0 1 6.816 6.808L346.936 612.168a4.736 4.736 0 0 1-3.392 1.416z" fill="#FDF1F6" ></path>' + '' + '<path d="M466.456 490.664l-0.232-0.008a4.784 4.784 0 0 1-4.56-5.032l2.56-52.664a4.856 4.856 0 0 1 5.032-4.568 4.776 4.776 0 0 1 4.56 5.04l-2.544 52.664a4.816 4.816 0 0 1-4.816 4.568z" fill="#FDF1F6" ></path>' + '' + '<path d="M466.456 490.664a4.816 4.816 0 0 1-0.24-9.616l52.656-2.56a4.792 4.792 0 0 1 5.032 4.568 4.8 4.8 0 0 1-4.56 5.048l-52.656 2.552-0.232 0.008z" fill="#FDF1F6" ></path>' + '' + '<path d="M376.184 631.024h-0.248a4.824 4.824 0 0 1-4.56-5.048l2.56-52.648a4.832 4.832 0 0 1 5.032-4.568 4.8 4.8 0 0 1 4.576 5.04l-2.56 52.648a4.808 4.808 0 0 1-4.8 4.576z" fill="#FDF1F6" ></path>' + '' + '<path d="M326.08 580.928a4.808 4.808 0 0 1-0.224-9.608l52.656-2.56a4.88 4.88 0 0 1 5.032 4.568 4.792 4.792 0 0 1-4.576 5.032l-52.64 2.568h-0.248z" fill="#FDF1F6" ></path>' + '' + '<path d="M501.672 613.584a4.816 4.816 0 0 1-3.408-1.416l-158.16-158.136a4.856 4.856 0 0 1 0-6.808 4.856 4.856 0 0 1 6.832 0l158.128 158.152a4.784 4.784 0 0 1 0 6.792 4.752 4.752 0 0 1-3.392 1.416z" fill="#FDF1F6" ></path>' + '' + '<path d="M519.128 580.928l-0.248-0.008-52.656-2.56a4.784 4.784 0 0 1-4.56-5.032 4.832 4.832 0 0 1 5.032-4.568l52.656 2.56a4.784 4.784 0 0 1 4.56 5.032 4.784 4.784 0 0 1-4.784 4.576z" fill="#FDF1F6" ></path>' + '' + '<path d="M469.016 631.024a4.824 4.824 0 0 1-4.816-4.576l-2.544-52.648a4.8 4.8 0 0 1 4.56-5.04 4.872 4.872 0 0 1 5.048 4.568l2.544 52.656a4.816 4.816 0 0 1-4.56 5.04h-0.232z" fill="#FDF1F6" ></path>' + '' + '<path d="M378.752 490.664l-0.232-0.008-52.656-2.552a4.808 4.808 0 0 1 0.472-9.616l52.64 2.56a4.8 4.8 0 0 1-0.224 9.616z" fill="#FDF1F6" ></path>' + '' + '<path d="M378.736 490.664a4.824 4.824 0 0 1-4.8-4.576l-2.544-52.656a4.808 4.808 0 0 1 4.56-5.04 4.848 4.848 0 0 1 5.032 4.568l2.56 52.664a4.792 4.792 0 0 1-4.576 5.032l-0.232 0.008z" fill="#FDF1F6" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-chakangengduo" viewBox="0 0 1027 1024">' + '' + '<path d="M512 0c-282.799601 0-512 229.200399-512 512s229.200399 512 512 512S1024 794.799601 1024 512 794.799601 0 512 0z m1.020937 989.798604c-263.912263 0-477.798604-213.886341-477.798604-477.798604s213.886341-477.798604 477.798604-477.798604 477.798604 213.886341 477.798604 477.798604-213.886341 477.798604-477.798604 477.798604z" fill="" ></path>' + '' + '<path d="M860.139581 512c0-4.594217-2.041874-9.188435-5.104686-12.251246l-191.936191-191.936192c-3.062812-3.062812-7.14656-4.594217-11.740778-4.594217-9.188435 0-16.845464 7.657029-16.845463 16.845464 0 4.594217 1.531406 8.677966 4.594217 11.740777l0.510469 0.510469 161.308075 161.308076h-620.219342c-9.188435 0-16.845464 7.657029-16.845463 16.845463s7.657029 16.845464 16.845463 16.845464h623.792622l-164.881355 164.881356c-3.062812 3.062812-5.104686 7.14656-5.104686 12.251246 0 9.188435 7.657029 16.845464 16.845463 16.845464 4.594217 0 8.677966-2.041874 11.740778-5.104686l180.195414-180.195414 12.251246-12.251246c2.552343-3.062812 4.594217-7.14656 4.594217-11.740778z" fill="" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-chakangengduo1" viewBox="0 0 1024 1024">' + '' + '<path d="M67.54304 958.23872l-24.68352-30.31552 519.63904-423.17824L39.67488 81.51552l24.6016-30.39232 560.25088 453.53472z" ></path>' + '' + '<path d="M435.01056 971.13088l-24.68352-30.31552 519.64416-423.17824-522.82368-423.23456 24.59648-30.3872 560.25088 453.53472z" ></path>' + '' + '</symbol>' + '' + '</svg>' var script = function() { var scripts = document.getElementsByTagName('script') return scripts[scripts.length - 1] }() var shouldInjectCss = script.getAttribute("data-injectcss") /** * document ready */ var ready = function(fn) { if (document.addEventListener) { if (~["complete", "loaded", "interactive"].indexOf(document.readyState)) { setTimeout(fn, 0) } else { var loadFn = function() { document.removeEventListener("DOMContentLoaded", loadFn, false) fn() } document.addEventListener("DOMContentLoaded", loadFn, false) } } else if (document.attachEvent) { IEContentLoaded(window, fn) } function IEContentLoaded(w, fn) { var d = w.document, done = false, // only fire once init = function() { if (!done) { done = true fn() } } // polling for no errors var polling = function() { try { // throws errors until after ondocumentready d.documentElement.doScroll('left') } catch (e) { setTimeout(polling, 50) return } // no errors, fire init() }; polling() // trying to always fire before onload d.onreadystatechange = function() { if (d.readyState == 'complete') { d.onreadystatechange = null init() } } } } /** * Insert el before target * * @param {Element} el * @param {Element} target */ var before = function(el, target) { target.parentNode.insertBefore(el, target) } /** * Prepend el to target * * @param {Element} el * @param {Element} target */ var prepend = function(el, target) { if (target.firstChild) { before(el, target.firstChild) } else { target.appendChild(el) } } function
() { var div, svg div = document.createElement('div') div.innerHTML = svgSprite svgSprite = null svg = div.getElementsByTagName('svg')[0] if (svg) { svg.setAttribute('aria-hidden', 'true') svg.style.position = 'absolute' svg.style.width = 0 svg.style.height = 0 svg.style.overflow = 'hidden' prepend(svg, document.body) } } if (shouldInjectCss && !window.__iconfont__svg__cssinject__) { window.__iconfont__svg__cssinject__ = true try { document.write("<style>.svgfont {display: inline-block;width: 1em;height: 1em;fill: currentColor;vertical-align: -0.1em;font-size:16px;}</style>"); } catch (e) { console && console.log(e) } } ready(appendSvg) })(window)
appendSvg
identifier_name
iconfont.js
;(function(window) { var svgSprite = '<svg>' + '' + '<symbol id="icon-biaoqian" viewBox="0 0 1024 1024">' + '' + '<path d="M862.265432 448.791399l19.793812-255.17368c2.230808-28.754904-21.728884-52.714596-50.483788-50.483788L576.401776 162.927743l-0.317225-0.316202L110.7555 627.940592l286.497083 286.497083 465.329051-465.330074L862.265432 448.791399zM660.940171 364.253004c-24.186865-24.186865-24.186865-63.403029 0-87.589894 24.186865-24.186865 63.402005-24.186865 87.589894 0s24.186865 63.402005 0 87.589894C724.342176 388.440893 685.127036 388.440893 660.940171 364.253004z" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-024" viewBox="0 0 1024 1024">' + '' + '<path d="M512 512m-512 0a512 512 0 1 0 1024 0 512 512 0 1 0-1024 0Z" fill="#EDC55D" ></path>' + '' + '<path d="M843.632 248.656l-3.664 2.616c-77.856-24.472-169.048 37.816-169.048 37.816l-133.464-4.448-155.704 262.48-141.704 107.136 364.72 361.192C843.216 971.792 1024 763.072 1024 512c0-31.864-3.064-63.016-8.632-93.272l-171.736-170.072z" fill="#D9A93F" ></path>' + '' + '<path d="M726.064 247.984c-55.768 16.704-107.904 48.096-99.624 75.784l15.328-4.584c-3.264-10.952 29.72-38.16 88.888-55.856 59.16-17.72 101.672-13.128 104.952-2.176l15.328-4.584c-8.296-27.704-69.096-25.28-124.872-8.584z" fill="#A6592E" ></path>' + '' + '<path d="M652.656 257.576l-143.488-1.304c-25.512-0.232-60.608 13.944-77.968 31.504L184.504 537.368c-17.36 17.552-16.216 47.112 2.56 65.672l172.904 170.944c18.768 18.56 48.344 19.384 65.704 1.824l246.704-249.584c17.36-17.56 31.128-52.816 30.592-78.328l-2.936-143.488c-0.528-25.528-21.848-46.592-47.376-46.832z m-7.08 101.04c-11.856 12.008-31.904 11.456-44.736-1.24-12.84-12.688-13.624-32.72-1.768-44.728 11.872-12.016 31.92-11.464 44.752 1.232 12.848 12.696 13.632 32.728 1.752 44.736z" fill="#3CB19C" ></path>' + '' + '<path d="M359.92 654.24c-56.264 0-109.92-13.168-158.016-36.512l158.064 156.256c18.768 18.56 48.344 19.384 65.704 1.824l246.704-249.584c17.36-17.56 31.128-52.816 30.592-78.328l-0.576-28.24C647.936 555.896 513.736 654.24 359.92 654.24z" fill="#30A28C" ></path>' + '' + '<path d="M746.72 317.008c-59.16 17.712-101.672 13.112-104.952 2.168l-15.328 4.584c8.296 27.688 69.096 25.264 124.872 8.568 55.784-16.704 107.904-48.096 99.624-75.784l-15.328 4.584c3.28 10.952-29.736 38.16-88.888 55.88z" fill="#A6592E" ></path>' + '' + '<path d="M422.608 646.336a4.816 4.816 0 0 1-4.816-4.808V417.888a4.824 4.824 0 0 1 9.632 0v223.64c0 2.656-2.16 4.808-4.816 4.808z" fill="#FDF1F6" ></path>' + '' + '<path d="M422.608 484.28a4.736 4.736 0 0 1-3.56-1.584l-28.704-31.624a4.824 4.824 0 0 1 0.328-6.808 4.824 4.824 0 0 1 6.8 0.336l28.704 31.632a4.824 4.824 0 0 1-3.568 8.048z" fill="#FDF1F6" ></path>' + '' + '<path d="M422.608 484.28a4.8 4.8 0 0 1-3.56-8.048l28.688-31.632a4.824 4.824 0 0 1 7.144 6.472l-28.704 31.624a4.816 4.816 0 0 1-3.568 1.584zM451.312 616.384a4.816 4.816 0 0 1-3.576-1.576l-28.688-31.624a4.816 4.816 0 0 1 7.128-6.464l28.704 31.632a4.808 4.808 0 0 1-3.568 8.032z" fill="#FDF1F6" ></path>' + '' + '<path d="M393.904 616.384a4.808 4.808 0 0 1-3.56-8.032l28.704-31.632a4.816 4.816 0 0 1 7.128 6.464l-28.704 31.624a4.84 4.84 0 0 1-3.568 1.576zM534.424 534.512H310.784a4.816 4.816 0 1 1 0-9.632h223.64a4.808 4.808 0 1 1 0 9.632z" fill="#FDF1F6" ></path>' + '' + '<path d="M472.84 534.512a4.8 4.8 0 0 1-3.232-8.368l31.624-28.704a4.824 4.824 0 0 1 6.8 0.328 4.8 4.8 0 0 1-0.328 6.8l-31.624 28.704a4.808 4.808 0 0 1-3.24 1.24z" fill="#FDF1F6" ></path>' + '' + '<path d="M504.488 563.216a4.856 4.856 0 0 1-3.248-1.248l-31.624-28.696a4.8 4.8 0 1 1 6.472-7.128l31.624 28.688a4.816 4.816 0 0 1-3.224 8.384z" fill="#FDF1F6" ></path>' + '' + '<path d="M340.736 563.216a4.824 4.824 0 0 1-3.232-8.384l31.624-28.688a4.808 4.808 0 1 1 6.456 7.128l-31.608 28.696a4.808 4.808 0 0 1-3.24 1.248z" fill="#FDF1F6" ></path>' + '' + '<path d="M372.376 534.512a4.872 4.872 0 0 1-3.248-1.24l-31.624-28.704a4.816 4.816 0 0 1 6.472-7.128l31.608 28.704a4.816 4.816 0 0 1-3.208 8.368z" fill="#FDF1F6" ></path>' + '' + '<path d="M343.544 613.584a4.864 4.864 0 0 1-3.44-1.416 4.84 4.84 0 0 1 0-6.792l158.16-158.152a4.824 4.824 0 0 1 6.816 6.808L346.936 612.168a4.736 4.736 0 0 1-3.392 1.416z" fill="#FDF1F6" ></path>' + '' + '<path d="M466.456 490.664l-0.232-0.008a4.784 4.784 0 0 1-4.56-5.032l2.56-52.664a4.856 4.856 0 0 1 5.032-4.568 4.776 4.776 0 0 1 4.56 5.04l-2.544 52.664a4.816 4.816 0 0 1-4.816 4.568z" fill="#FDF1F6" ></path>' + '' + '<path d="M466.456 490.664a4.816 4.816 0 0 1-0.24-9.616l52.656-2.56a4.792 4.792 0 0 1 5.032 4.568 4.8 4.8 0 0 1-4.56 5.048l-52.656 2.552-0.232 0.008z" fill="#FDF1F6" ></path>' + '' + '<path d="M376.184 631.024h-0.248a4.824 4.824 0 0 1-4.56-5.048l2.56-52.648a4.832 4.832 0 0 1 5.032-4.568 4.8 4.8 0 0 1 4.576 5.04l-2.56 52.648a4.808 4.808 0 0 1-4.8 4.576z" fill="#FDF1F6" ></path>' + '' + '<path d="M326.08 580.928a4.808 4.808 0 0 1-0.224-9.608l52.656-2.56a4.88 4.88 0 0 1 5.032 4.568 4.792 4.792 0 0 1-4.576 5.032l-52.64 2.568h-0.248z" fill="#FDF1F6" ></path>' + '' + '<path d="M501.672 613.584a4.816 4.816 0 0 1-3.408-1.416l-158.16-158.136a4.856 4.856 0 0 1 0-6.808 4.856 4.856 0 0 1 6.832 0l158.128 158.152a4.784 4.784 0 0 1 0 6.792 4.752 4.752 0 0 1-3.392 1.416z" fill="#FDF1F6" ></path>' + '' + '<path d="M519.128 580.928l-0.248-0.008-52.656-2.56a4.784 4.784 0 0 1-4.56-5.032 4.832 4.832 0 0 1 5.032-4.568l52.656 2.56a4.784 4.784 0 0 1 4.56 5.032 4.784 4.784 0 0 1-4.784 4.576z" fill="#FDF1F6" ></path>' + '' + '<path d="M469.016 631.024a4.824 4.824 0 0 1-4.816-4.576l-2.544-52.648a4.8 4.8 0 0 1 4.56-5.04 4.872 4.872 0 0 1 5.048 4.568l2.544 52.656a4.816 4.816 0 0 1-4.56 5.04h-0.232z" fill="#FDF1F6" ></path>' + '' + '<path d="M378.752 490.664l-0.232-0.008-52.656-2.552a4.808 4.808 0 0 1 0.472-9.616l52.64 2.56a4.8 4.8 0 0 1-0.224 9.616z" fill="#FDF1F6" ></path>' + '' + '<path d="M378.736 490.664a4.824 4.824 0 0 1-4.8-4.576l-2.544-52.656a4.808 4.808 0 0 1 4.56-5.04 4.848 4.848 0 0 1 5.032 4.568l2.56 52.664a4.792 4.792 0 0 1-4.576 5.032l-0.232 0.008z" fill="#FDF1F6" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-chakangengduo" viewBox="0 0 1027 1024">' + '' + '<path d="M512 0c-282.799601 0-512 229.200399-512 512s229.200399 512 512 512S1024 794.799601 1024 512 794.799601 0 512 0z m1.020937 989.798604c-263.912263 0-477.798604-213.886341-477.798604-477.798604s213.886341-477.798604 477.798604-477.798604 477.798604 213.886341 477.798604 477.798604-213.886341 477.798604-477.798604 477.798604z" fill="" ></path>' + '' + '<path d="M860.139581 512c0-4.594217-2.041874-9.188435-5.104686-12.251246l-191.936191-191.936192c-3.062812-3.062812-7.14656-4.594217-11.740778-4.594217-9.188435 0-16.845464 7.657029-16.845463 16.845464 0 4.594217 1.531406 8.677966 4.594217 11.740777l0.510469 0.510469 161.308075 161.308076h-620.219342c-9.188435 0-16.845464 7.657029-16.845463 16.845463s7.657029 16.845464 16.845463 16.845464h623.792622l-164.881355 164.881356c-3.062812 3.062812-5.104686 7.14656-5.104686 12.251246 0 9.188435 7.657029 16.845464 16.845463 16.845464 4.594217 0 8.677966-2.041874 11.740778-5.104686l180.195414-180.195414 12.251246-12.251246c2.552343-3.062812 4.594217-7.14656 4.594217-11.740778z" fill="" ></path>' + '' + '</symbol>' + '' + '<symbol id="icon-chakangengduo1" viewBox="0 0 1024 1024">' + '' + '<path d="M67.54304 958.23872l-24.68352-30.31552 519.63904-423.17824L39.67488 81.51552l24.6016-30.39232 560.25088 453.53472z" ></path>' + '' + '<path d="M435.01056 971.13088l-24.68352-30.31552 519.64416-423.17824-522.82368-423.23456 24.59648-30.3872 560.25088 453.53472z" ></path>' + '' +
'' + '</svg>' var script = function() { var scripts = document.getElementsByTagName('script') return scripts[scripts.length - 1] }() var shouldInjectCss = script.getAttribute("data-injectcss") /** * document ready */ var ready = function(fn) { if (document.addEventListener) { if (~["complete", "loaded", "interactive"].indexOf(document.readyState)) { setTimeout(fn, 0) } else { var loadFn = function() { document.removeEventListener("DOMContentLoaded", loadFn, false) fn() } document.addEventListener("DOMContentLoaded", loadFn, false) } } else if (document.attachEvent) { IEContentLoaded(window, fn) } function IEContentLoaded(w, fn) { var d = w.document, done = false, // only fire once init = function() { if (!done) { done = true fn() } } // polling for no errors var polling = function() { try { // throws errors until after ondocumentready d.documentElement.doScroll('left') } catch (e) { setTimeout(polling, 50) return } // no errors, fire init() }; polling() // trying to always fire before onload d.onreadystatechange = function() { if (d.readyState == 'complete') { d.onreadystatechange = null init() } } } } /** * Insert el before target * * @param {Element} el * @param {Element} target */ var before = function(el, target) { target.parentNode.insertBefore(el, target) } /** * Prepend el to target * * @param {Element} el * @param {Element} target */ var prepend = function(el, target) { if (target.firstChild) { before(el, target.firstChild) } else { target.appendChild(el) } } function appendSvg() { var div, svg div = document.createElement('div') div.innerHTML = svgSprite svgSprite = null svg = div.getElementsByTagName('svg')[0] if (svg) { svg.setAttribute('aria-hidden', 'true') svg.style.position = 'absolute' svg.style.width = 0 svg.style.height = 0 svg.style.overflow = 'hidden' prepend(svg, document.body) } } if (shouldInjectCss && !window.__iconfont__svg__cssinject__) { window.__iconfont__svg__cssinject__ = true try { document.write("<style>.svgfont {display: inline-block;width: 1em;height: 1em;fill: currentColor;vertical-align: -0.1em;font-size:16px;}</style>"); } catch (e) { console && console.log(e) } } ready(appendSvg) })(window)
'</symbol>' +
random_line_split
main.rs
// Copyright (c) Microsoft. All rights reserved. //! This binary is the process entrypoint for aziot-certd, -identityd and -keyd. //! Rather than be three separate binaries, all three services are symlinks to //! this one aziotd binary. The aziotd binary looks at its command-line args to figure out //! which service it's being invoked as, and runs the code of that service accordingly. #![deny(rust_2018_idioms)] #![warn(clippy::all, clippy::pedantic)] #![allow(clippy::default_trait_access, clippy::let_unit_value)] mod error; mod logging; use error::{Error, ErrorKind}; #[tokio::main] async fn main() { logging::init(); if let Err(err) = main_inner().await { log::error!("{}", err.0); let mut source = std::error::Error::source(&err.0); while let Some(err) = source { log::error!("caused by: {}", err); source = std::error::Error::source(err); } log::error!("{:?}", err.1); std::process::exit(1); } } async fn main_inner() -> Result<(), Error> { let mut args = std::env::args_os(); let process_name = process_name_from_args(&mut args)?; match process_name { ProcessName::Certd => { run( aziot_certd::main, "AZIOT_CERTD_CONFIG", "/etc/aziot/certd/config.toml", "AZIOT_CERTD_CONFIG_DIR", "/etc/aziot/certd/config.d", ) .await? } ProcessName::Identityd => { run( aziot_identityd::main, "AZIOT_IDENTITYD_CONFIG", "/etc/aziot/identityd/config.toml", "AZIOT_IDENTITYD_CONFIG_DIR", "/etc/aziot/identityd/config.d", ) .await? } ProcessName::Keyd => { run( aziot_keyd::main, "AZIOT_KEYD_CONFIG", "/etc/aziot/keyd/config.toml", "AZIOT_KEYD_CONFIG_DIR", "/etc/aziot/keyd/config.d", ) .await? } ProcessName::Tpmd => { run( aziot_tpmd::main, "AZIOT_TPMD_CONFIG", "/etc/aziot/tpmd/config.toml", "AZIOT_TPMD_CONFIG_DIR", "/etc/aziot/tpmd/config.d", ) .await? } } Ok(()) } #[derive(Clone, Copy, Debug, PartialEq)] enum ProcessName { Certd, Identityd, Keyd, Tpmd, } /// If the symlink is being used to invoke this binary, the process name can be determined /// from the first arg, ie `argv[0]` in C terms. /// /// An alternative is supported where the binary is invoked as aziotd itself, /// and the process name is instead the next arg, ie `argv[1]` in C terms. /// This is primary useful for local development, so it's only allowed in debug builds. fn process_name_from_args<I>(args: &mut I) -> Result<ProcessName, Error> where I: Iterator, <I as Iterator>::Item: AsRef<std::ffi::OsStr>, { let arg = args.next().ok_or_else(|| { ErrorKind::GetProcessName("could not extract process name from args".into()) })?; // arg could be a single component like "aziot-certd", or a path that ends with "aziot-certd", // so parse it as a Path and get the last component. This does the right thing in either case. let arg = std::path::Path::new(&arg); let process_name = arg.file_name().ok_or_else(|| { ErrorKind::GetProcessName( format!( "could not extract process name from arg {:?}", arg.display(), ) .into(), ) })?; match process_name.to_str() { Some("aziot-certd") => Ok(ProcessName::Certd), Some("aziot-identityd") => Ok(ProcessName::Identityd), Some("aziot-keyd") => Ok(ProcessName::Keyd), Some("aziot-tpmd") => Ok(ProcessName::Tpmd), // The next arg is the process name #[cfg(debug_assertions)] Some("aziotd") => process_name_from_args(args), _ => Err(ErrorKind::GetProcessName( format!("unrecognized process name {:?}", process_name).into(), ) .into()), } } async fn run<TMain, TConfig, TFuture, TServer>( main: TMain, config_env_var: &str, config_file_default: &str, config_directory_env_var: &str, config_directory_default: &str, ) -> Result<(), Error> where TMain: FnOnce(TConfig) -> TFuture, TConfig: serde::de::DeserializeOwned, TFuture: std::future::Future<
hyper::Request<hyper::Body>, Response = hyper::Response<hyper::Body>, Error = std::convert::Infallible, > + Clone + Send + 'static, <TServer as hyper::service::Service<hyper::Request<hyper::Body>>>::Future: Send, { log::info!("Starting service..."); log::info!( "Version - {}", option_env!("PACKAGE_VERSION").unwrap_or("dev build"), ); let config_path: std::path::PathBuf = std::env::var_os(config_env_var).map_or_else(|| config_file_default.into(), Into::into); let config = std::fs::read(&config_path) .map_err(|err| ErrorKind::ReadConfig(Some(config_path.clone()), Box::new(err)))?; let mut config: toml::Value = toml::from_slice(&config) .map_err(|err| ErrorKind::ReadConfig(Some(config_path), Box::new(err)))?; let config_directory_path: std::path::PathBuf = std::env::var_os(config_directory_env_var) .map_or_else(|| config_directory_default.into(), Into::into); match std::fs::read_dir(&config_directory_path) { Ok(entries) => { let mut patch_paths = vec![]; for entry in entries { let entry = entry.map_err(|err| { ErrorKind::ReadConfig(Some(config_directory_path.clone()), Box::new(err)) })?; let entry_file_type = entry.file_type().map_err(|err| { ErrorKind::ReadConfig(Some(config_directory_path.clone()), Box::new(err)) })?; if !entry_file_type.is_file() { continue; } let patch_path = entry.path(); if patch_path.extension().and_then(std::ffi::OsStr::to_str) != Some("toml") { continue; } patch_paths.push(patch_path); } patch_paths.sort(); for patch_path in patch_paths { let patch = std::fs::read(&patch_path).map_err(|err| { ErrorKind::ReadConfig(Some(patch_path.clone()), Box::new(err)) })?; let patch: toml::Value = toml::from_slice(&patch) .map_err(|err| ErrorKind::ReadConfig(Some(patch_path), Box::new(err)))?; merge_toml(&mut config, patch); } } Err(err) if err.kind() == std::io::ErrorKind::NotFound => (), Err(err) => { return Err(ErrorKind::ReadConfig(Some(config_directory_path), Box::new(err)).into()) } } let config: TConfig = serde::Deserialize::deserialize(config) .map_err(|err| ErrorKind::ReadConfig(None, Box::new(err)))?; let (connector, server) = main(config).await.map_err(ErrorKind::Service)?; log::info!("Starting server..."); let mut incoming = connector .incoming() .await .map_err(|err| ErrorKind::Service(Box::new(err)))?; let () = incoming .serve(server) .await .map_err(|err| ErrorKind::Service(Box::new(err)))?; log::info!("Stopped server."); Ok(()) } fn merge_toml(base: &mut toml::Value, patch: toml::Value) { // Similar to JSON patch, except that: // // - Maps are called tables. // - There is no equivalent of null that can be used to remove keys from an object. // - Arrays are merged via concatenating the patch to the base, rather than replacing the base with the patch. if let toml::Value::Table(base) = base { if let toml::Value::Table(patch) = patch { for (key, value) in patch { // Insert a dummy `false` if the original key didn't exist at all. It'll be overwritten by `value` in that case. let original_value = base.entry(key).or_insert(toml::Value::Boolean(false)); merge_toml(original_value, value); } return; } } if let toml::Value::Array(base) = base { if let toml::Value::Array(patch) = patch { base.extend(patch); return; } } *base = patch; } #[cfg(test)] mod tests { #[test] fn process_name_from_args() { // Success test cases let mut test_cases = vec![ (&["aziot-certd"][..], super::ProcessName::Certd), (&["aziot-identityd"][..], super::ProcessName::Identityd), (&["aziot-keyd"][..], super::ProcessName::Keyd), (&["aziot-tpmd"][..], super::ProcessName::Tpmd), ( &["/usr/libexec/aziot/aziot-certd"][..], super::ProcessName::Certd, ), ( &["/usr/libexec/aziot/aziot-identityd"][..], super::ProcessName::Identityd, ), ( &["/usr/libexec/aziot/aziot-keyd"][..], super::ProcessName::Keyd, ), ( &["/usr/libexec/aziot/aziot-tpmd"][..], super::ProcessName::Tpmd, ), ]; // argv[1] fallback is only in release builds. if cfg!(debug_assertions) { test_cases.extend_from_slice(&[ (&["aziotd", "aziot-certd"][..], super::ProcessName::Certd), ( &["aziotd", "aziot-identityd"][..], super::ProcessName::Identityd, ), (&["aziotd", "aziot-keyd"][..], super::ProcessName::Keyd), ( &["/usr/libexec/aziot/aziotd", "aziot-certd"][..], super::ProcessName::Certd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-identityd"][..], super::ProcessName::Identityd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-keyd"][..], super::ProcessName::Keyd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-tpmd"][..], super::ProcessName::Tpmd, ), ]); } for (input, expected) in test_cases { let mut input = input.iter().copied().map(std::ffi::OsStr::new); let actual = super::process_name_from_args(&mut input).unwrap(); assert_eq!(None, input.next()); assert_eq!(expected, actual); } // Failure test cases for &input in &[ // Unrecognized process name in argv[0] &["foo"][..], &["/usr/libexec/aziot/foo"][..], &["/usr/libexec/aziot/foo", "aziot-certd"][..], // Either fails because it's a release build so argv[1] fallback is disabled, // or fails because it's a debug build where argv[1] fallback is enabled // but the process name in argv[1] is unrecognized anyway. &["aziotd", "foo"][..], &["/usr/libexec/aziot/aziotd", "foo"][..], ] { let mut input = input.iter().copied().map(std::ffi::OsStr::new); let _ = super::process_name_from_args(&mut input).unwrap_err(); } } #[test] fn merge_toml() { let base = r#" foo_key = "A" foo_parent_key = { foo_sub_key = "B" } [bar_table] bar_table_key = "C" bar_table_parent_key = { bar_table_sub_key = "D" } [[baz_table_array]] baz_table_array_key = "E" baz_table_array_parent_key = { baz_table_sub_key = "F" } "#; let mut base: toml::Value = toml::from_str(base).unwrap(); let patch = r#" foo_key = "A2" foo_key_new = "A3" foo_parent_key = { foo_sub_key = "B2", foo_sub_key2 = "B3" } foo_parent_key_new = { foo_sub_key = "B4", foo_sub_key2 = "B5" } [bar_table] bar_table_key = "C2" bar_table_key_new = "C3" bar_table_parent_key = { bar_table_sub_key = "D2", bar_table_sub_key2 = "D3" } bar_table_parent_key_new = { bar_table_sub_key = "D4", bar_table_sub_key2 = "D5" } [[baz_table_array]] baz_table_array_key = "G" baz_table_array_parent_key = { baz_table_sub_key = "H" } "#; let patch: toml::Value = toml::from_str(patch).unwrap(); super::merge_toml(&mut base, patch); let expected = r#" foo_key = "A2" foo_key_new = "A3" foo_parent_key = { foo_sub_key = "B2", foo_sub_key2 = "B3" } foo_parent_key_new = { foo_sub_key = "B4", foo_sub_key2 = "B5" } [bar_table] bar_table_key = "C2" bar_table_key_new = "C3" bar_table_parent_key = { bar_table_sub_key = "D2", bar_table_sub_key2 = "D3" } bar_table_parent_key_new = { bar_table_sub_key = "D4", bar_table_sub_key2 = "D5" } [[baz_table_array]] baz_table_array_key = "E" baz_table_array_parent_key = { baz_table_sub_key = "F" } [[baz_table_array]] baz_table_array_key = "G" baz_table_array_parent_key = { baz_table_sub_key = "H" } "#; let expected: toml::Value = toml::from_str(expected).unwrap(); assert_eq!(expected, base); } }
Output = Result<(http_common::Connector, TServer), Box<dyn std::error::Error>>, >, TServer: hyper::service::Service<
random_line_split
main.rs
// Copyright (c) Microsoft. All rights reserved. //! This binary is the process entrypoint for aziot-certd, -identityd and -keyd. //! Rather than be three separate binaries, all three services are symlinks to //! this one aziotd binary. The aziotd binary looks at its command-line args to figure out //! which service it's being invoked as, and runs the code of that service accordingly. #![deny(rust_2018_idioms)] #![warn(clippy::all, clippy::pedantic)] #![allow(clippy::default_trait_access, clippy::let_unit_value)] mod error; mod logging; use error::{Error, ErrorKind}; #[tokio::main] async fn main() { logging::init(); if let Err(err) = main_inner().await { log::error!("{}", err.0); let mut source = std::error::Error::source(&err.0); while let Some(err) = source { log::error!("caused by: {}", err); source = std::error::Error::source(err); } log::error!("{:?}", err.1); std::process::exit(1); } } async fn main_inner() -> Result<(), Error> { let mut args = std::env::args_os(); let process_name = process_name_from_args(&mut args)?; match process_name { ProcessName::Certd => { run( aziot_certd::main, "AZIOT_CERTD_CONFIG", "/etc/aziot/certd/config.toml", "AZIOT_CERTD_CONFIG_DIR", "/etc/aziot/certd/config.d", ) .await? } ProcessName::Identityd => { run( aziot_identityd::main, "AZIOT_IDENTITYD_CONFIG", "/etc/aziot/identityd/config.toml", "AZIOT_IDENTITYD_CONFIG_DIR", "/etc/aziot/identityd/config.d", ) .await? } ProcessName::Keyd => { run( aziot_keyd::main, "AZIOT_KEYD_CONFIG", "/etc/aziot/keyd/config.toml", "AZIOT_KEYD_CONFIG_DIR", "/etc/aziot/keyd/config.d", ) .await? } ProcessName::Tpmd =>
} Ok(()) } #[derive(Clone, Copy, Debug, PartialEq)] enum ProcessName { Certd, Identityd, Keyd, Tpmd, } /// If the symlink is being used to invoke this binary, the process name can be determined /// from the first arg, ie `argv[0]` in C terms. /// /// An alternative is supported where the binary is invoked as aziotd itself, /// and the process name is instead the next arg, ie `argv[1]` in C terms. /// This is primary useful for local development, so it's only allowed in debug builds. fn process_name_from_args<I>(args: &mut I) -> Result<ProcessName, Error> where I: Iterator, <I as Iterator>::Item: AsRef<std::ffi::OsStr>, { let arg = args.next().ok_or_else(|| { ErrorKind::GetProcessName("could not extract process name from args".into()) })?; // arg could be a single component like "aziot-certd", or a path that ends with "aziot-certd", // so parse it as a Path and get the last component. This does the right thing in either case. let arg = std::path::Path::new(&arg); let process_name = arg.file_name().ok_or_else(|| { ErrorKind::GetProcessName( format!( "could not extract process name from arg {:?}", arg.display(), ) .into(), ) })?; match process_name.to_str() { Some("aziot-certd") => Ok(ProcessName::Certd), Some("aziot-identityd") => Ok(ProcessName::Identityd), Some("aziot-keyd") => Ok(ProcessName::Keyd), Some("aziot-tpmd") => Ok(ProcessName::Tpmd), // The next arg is the process name #[cfg(debug_assertions)] Some("aziotd") => process_name_from_args(args), _ => Err(ErrorKind::GetProcessName( format!("unrecognized process name {:?}", process_name).into(), ) .into()), } } async fn run<TMain, TConfig, TFuture, TServer>( main: TMain, config_env_var: &str, config_file_default: &str, config_directory_env_var: &str, config_directory_default: &str, ) -> Result<(), Error> where TMain: FnOnce(TConfig) -> TFuture, TConfig: serde::de::DeserializeOwned, TFuture: std::future::Future< Output = Result<(http_common::Connector, TServer), Box<dyn std::error::Error>>, >, TServer: hyper::service::Service< hyper::Request<hyper::Body>, Response = hyper::Response<hyper::Body>, Error = std::convert::Infallible, > + Clone + Send + 'static, <TServer as hyper::service::Service<hyper::Request<hyper::Body>>>::Future: Send, { log::info!("Starting service..."); log::info!( "Version - {}", option_env!("PACKAGE_VERSION").unwrap_or("dev build"), ); let config_path: std::path::PathBuf = std::env::var_os(config_env_var).map_or_else(|| config_file_default.into(), Into::into); let config = std::fs::read(&config_path) .map_err(|err| ErrorKind::ReadConfig(Some(config_path.clone()), Box::new(err)))?; let mut config: toml::Value = toml::from_slice(&config) .map_err(|err| ErrorKind::ReadConfig(Some(config_path), Box::new(err)))?; let config_directory_path: std::path::PathBuf = std::env::var_os(config_directory_env_var) .map_or_else(|| config_directory_default.into(), Into::into); match std::fs::read_dir(&config_directory_path) { Ok(entries) => { let mut patch_paths = vec![]; for entry in entries { let entry = entry.map_err(|err| { ErrorKind::ReadConfig(Some(config_directory_path.clone()), Box::new(err)) })?; let entry_file_type = entry.file_type().map_err(|err| { ErrorKind::ReadConfig(Some(config_directory_path.clone()), Box::new(err)) })?; if !entry_file_type.is_file() { continue; } let patch_path = entry.path(); if patch_path.extension().and_then(std::ffi::OsStr::to_str) != Some("toml") { continue; } patch_paths.push(patch_path); } patch_paths.sort(); for patch_path in patch_paths { let patch = std::fs::read(&patch_path).map_err(|err| { ErrorKind::ReadConfig(Some(patch_path.clone()), Box::new(err)) })?; let patch: toml::Value = toml::from_slice(&patch) .map_err(|err| ErrorKind::ReadConfig(Some(patch_path), Box::new(err)))?; merge_toml(&mut config, patch); } } Err(err) if err.kind() == std::io::ErrorKind::NotFound => (), Err(err) => { return Err(ErrorKind::ReadConfig(Some(config_directory_path), Box::new(err)).into()) } } let config: TConfig = serde::Deserialize::deserialize(config) .map_err(|err| ErrorKind::ReadConfig(None, Box::new(err)))?; let (connector, server) = main(config).await.map_err(ErrorKind::Service)?; log::info!("Starting server..."); let mut incoming = connector .incoming() .await .map_err(|err| ErrorKind::Service(Box::new(err)))?; let () = incoming .serve(server) .await .map_err(|err| ErrorKind::Service(Box::new(err)))?; log::info!("Stopped server."); Ok(()) } fn merge_toml(base: &mut toml::Value, patch: toml::Value) { // Similar to JSON patch, except that: // // - Maps are called tables. // - There is no equivalent of null that can be used to remove keys from an object. // - Arrays are merged via concatenating the patch to the base, rather than replacing the base with the patch. if let toml::Value::Table(base) = base { if let toml::Value::Table(patch) = patch { for (key, value) in patch { // Insert a dummy `false` if the original key didn't exist at all. It'll be overwritten by `value` in that case. let original_value = base.entry(key).or_insert(toml::Value::Boolean(false)); merge_toml(original_value, value); } return; } } if let toml::Value::Array(base) = base { if let toml::Value::Array(patch) = patch { base.extend(patch); return; } } *base = patch; } #[cfg(test)] mod tests { #[test] fn process_name_from_args() { // Success test cases let mut test_cases = vec![ (&["aziot-certd"][..], super::ProcessName::Certd), (&["aziot-identityd"][..], super::ProcessName::Identityd), (&["aziot-keyd"][..], super::ProcessName::Keyd), (&["aziot-tpmd"][..], super::ProcessName::Tpmd), ( &["/usr/libexec/aziot/aziot-certd"][..], super::ProcessName::Certd, ), ( &["/usr/libexec/aziot/aziot-identityd"][..], super::ProcessName::Identityd, ), ( &["/usr/libexec/aziot/aziot-keyd"][..], super::ProcessName::Keyd, ), ( &["/usr/libexec/aziot/aziot-tpmd"][..], super::ProcessName::Tpmd, ), ]; // argv[1] fallback is only in release builds. if cfg!(debug_assertions) { test_cases.extend_from_slice(&[ (&["aziotd", "aziot-certd"][..], super::ProcessName::Certd), ( &["aziotd", "aziot-identityd"][..], super::ProcessName::Identityd, ), (&["aziotd", "aziot-keyd"][..], super::ProcessName::Keyd), ( &["/usr/libexec/aziot/aziotd", "aziot-certd"][..], super::ProcessName::Certd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-identityd"][..], super::ProcessName::Identityd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-keyd"][..], super::ProcessName::Keyd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-tpmd"][..], super::ProcessName::Tpmd, ), ]); } for (input, expected) in test_cases { let mut input = input.iter().copied().map(std::ffi::OsStr::new); let actual = super::process_name_from_args(&mut input).unwrap(); assert_eq!(None, input.next()); assert_eq!(expected, actual); } // Failure test cases for &input in &[ // Unrecognized process name in argv[0] &["foo"][..], &["/usr/libexec/aziot/foo"][..], &["/usr/libexec/aziot/foo", "aziot-certd"][..], // Either fails because it's a release build so argv[1] fallback is disabled, // or fails because it's a debug build where argv[1] fallback is enabled // but the process name in argv[1] is unrecognized anyway. &["aziotd", "foo"][..], &["/usr/libexec/aziot/aziotd", "foo"][..], ] { let mut input = input.iter().copied().map(std::ffi::OsStr::new); let _ = super::process_name_from_args(&mut input).unwrap_err(); } } #[test] fn merge_toml() { let base = r#" foo_key = "A" foo_parent_key = { foo_sub_key = "B" } [bar_table] bar_table_key = "C" bar_table_parent_key = { bar_table_sub_key = "D" } [[baz_table_array]] baz_table_array_key = "E" baz_table_array_parent_key = { baz_table_sub_key = "F" } "#; let mut base: toml::Value = toml::from_str(base).unwrap(); let patch = r#" foo_key = "A2" foo_key_new = "A3" foo_parent_key = { foo_sub_key = "B2", foo_sub_key2 = "B3" } foo_parent_key_new = { foo_sub_key = "B4", foo_sub_key2 = "B5" } [bar_table] bar_table_key = "C2" bar_table_key_new = "C3" bar_table_parent_key = { bar_table_sub_key = "D2", bar_table_sub_key2 = "D3" } bar_table_parent_key_new = { bar_table_sub_key = "D4", bar_table_sub_key2 = "D5" } [[baz_table_array]] baz_table_array_key = "G" baz_table_array_parent_key = { baz_table_sub_key = "H" } "#; let patch: toml::Value = toml::from_str(patch).unwrap(); super::merge_toml(&mut base, patch); let expected = r#" foo_key = "A2" foo_key_new = "A3" foo_parent_key = { foo_sub_key = "B2", foo_sub_key2 = "B3" } foo_parent_key_new = { foo_sub_key = "B4", foo_sub_key2 = "B5" } [bar_table] bar_table_key = "C2" bar_table_key_new = "C3" bar_table_parent_key = { bar_table_sub_key = "D2", bar_table_sub_key2 = "D3" } bar_table_parent_key_new = { bar_table_sub_key = "D4", bar_table_sub_key2 = "D5" } [[baz_table_array]] baz_table_array_key = "E" baz_table_array_parent_key = { baz_table_sub_key = "F" } [[baz_table_array]] baz_table_array_key = "G" baz_table_array_parent_key = { baz_table_sub_key = "H" } "#; let expected: toml::Value = toml::from_str(expected).unwrap(); assert_eq!(expected, base); } }
{ run( aziot_tpmd::main, "AZIOT_TPMD_CONFIG", "/etc/aziot/tpmd/config.toml", "AZIOT_TPMD_CONFIG_DIR", "/etc/aziot/tpmd/config.d", ) .await? }
conditional_block
main.rs
// Copyright (c) Microsoft. All rights reserved. //! This binary is the process entrypoint for aziot-certd, -identityd and -keyd. //! Rather than be three separate binaries, all three services are symlinks to //! this one aziotd binary. The aziotd binary looks at its command-line args to figure out //! which service it's being invoked as, and runs the code of that service accordingly. #![deny(rust_2018_idioms)] #![warn(clippy::all, clippy::pedantic)] #![allow(clippy::default_trait_access, clippy::let_unit_value)] mod error; mod logging; use error::{Error, ErrorKind}; #[tokio::main] async fn main() { logging::init(); if let Err(err) = main_inner().await { log::error!("{}", err.0); let mut source = std::error::Error::source(&err.0); while let Some(err) = source { log::error!("caused by: {}", err); source = std::error::Error::source(err); } log::error!("{:?}", err.1); std::process::exit(1); } } async fn main_inner() -> Result<(), Error> { let mut args = std::env::args_os(); let process_name = process_name_from_args(&mut args)?; match process_name { ProcessName::Certd => { run( aziot_certd::main, "AZIOT_CERTD_CONFIG", "/etc/aziot/certd/config.toml", "AZIOT_CERTD_CONFIG_DIR", "/etc/aziot/certd/config.d", ) .await? } ProcessName::Identityd => { run( aziot_identityd::main, "AZIOT_IDENTITYD_CONFIG", "/etc/aziot/identityd/config.toml", "AZIOT_IDENTITYD_CONFIG_DIR", "/etc/aziot/identityd/config.d", ) .await? } ProcessName::Keyd => { run( aziot_keyd::main, "AZIOT_KEYD_CONFIG", "/etc/aziot/keyd/config.toml", "AZIOT_KEYD_CONFIG_DIR", "/etc/aziot/keyd/config.d", ) .await? } ProcessName::Tpmd => { run( aziot_tpmd::main, "AZIOT_TPMD_CONFIG", "/etc/aziot/tpmd/config.toml", "AZIOT_TPMD_CONFIG_DIR", "/etc/aziot/tpmd/config.d", ) .await? } } Ok(()) } #[derive(Clone, Copy, Debug, PartialEq)] enum ProcessName { Certd, Identityd, Keyd, Tpmd, } /// If the symlink is being used to invoke this binary, the process name can be determined /// from the first arg, ie `argv[0]` in C terms. /// /// An alternative is supported where the binary is invoked as aziotd itself, /// and the process name is instead the next arg, ie `argv[1]` in C terms. /// This is primary useful for local development, so it's only allowed in debug builds. fn process_name_from_args<I>(args: &mut I) -> Result<ProcessName, Error> where I: Iterator, <I as Iterator>::Item: AsRef<std::ffi::OsStr>, { let arg = args.next().ok_or_else(|| { ErrorKind::GetProcessName("could not extract process name from args".into()) })?; // arg could be a single component like "aziot-certd", or a path that ends with "aziot-certd", // so parse it as a Path and get the last component. This does the right thing in either case. let arg = std::path::Path::new(&arg); let process_name = arg.file_name().ok_or_else(|| { ErrorKind::GetProcessName( format!( "could not extract process name from arg {:?}", arg.display(), ) .into(), ) })?; match process_name.to_str() { Some("aziot-certd") => Ok(ProcessName::Certd), Some("aziot-identityd") => Ok(ProcessName::Identityd), Some("aziot-keyd") => Ok(ProcessName::Keyd), Some("aziot-tpmd") => Ok(ProcessName::Tpmd), // The next arg is the process name #[cfg(debug_assertions)] Some("aziotd") => process_name_from_args(args), _ => Err(ErrorKind::GetProcessName( format!("unrecognized process name {:?}", process_name).into(), ) .into()), } } async fn run<TMain, TConfig, TFuture, TServer>( main: TMain, config_env_var: &str, config_file_default: &str, config_directory_env_var: &str, config_directory_default: &str, ) -> Result<(), Error> where TMain: FnOnce(TConfig) -> TFuture, TConfig: serde::de::DeserializeOwned, TFuture: std::future::Future< Output = Result<(http_common::Connector, TServer), Box<dyn std::error::Error>>, >, TServer: hyper::service::Service< hyper::Request<hyper::Body>, Response = hyper::Response<hyper::Body>, Error = std::convert::Infallible, > + Clone + Send + 'static, <TServer as hyper::service::Service<hyper::Request<hyper::Body>>>::Future: Send, { log::info!("Starting service..."); log::info!( "Version - {}", option_env!("PACKAGE_VERSION").unwrap_or("dev build"), ); let config_path: std::path::PathBuf = std::env::var_os(config_env_var).map_or_else(|| config_file_default.into(), Into::into); let config = std::fs::read(&config_path) .map_err(|err| ErrorKind::ReadConfig(Some(config_path.clone()), Box::new(err)))?; let mut config: toml::Value = toml::from_slice(&config) .map_err(|err| ErrorKind::ReadConfig(Some(config_path), Box::new(err)))?; let config_directory_path: std::path::PathBuf = std::env::var_os(config_directory_env_var) .map_or_else(|| config_directory_default.into(), Into::into); match std::fs::read_dir(&config_directory_path) { Ok(entries) => { let mut patch_paths = vec![]; for entry in entries { let entry = entry.map_err(|err| { ErrorKind::ReadConfig(Some(config_directory_path.clone()), Box::new(err)) })?; let entry_file_type = entry.file_type().map_err(|err| { ErrorKind::ReadConfig(Some(config_directory_path.clone()), Box::new(err)) })?; if !entry_file_type.is_file() { continue; } let patch_path = entry.path(); if patch_path.extension().and_then(std::ffi::OsStr::to_str) != Some("toml") { continue; } patch_paths.push(patch_path); } patch_paths.sort(); for patch_path in patch_paths { let patch = std::fs::read(&patch_path).map_err(|err| { ErrorKind::ReadConfig(Some(patch_path.clone()), Box::new(err)) })?; let patch: toml::Value = toml::from_slice(&patch) .map_err(|err| ErrorKind::ReadConfig(Some(patch_path), Box::new(err)))?; merge_toml(&mut config, patch); } } Err(err) if err.kind() == std::io::ErrorKind::NotFound => (), Err(err) => { return Err(ErrorKind::ReadConfig(Some(config_directory_path), Box::new(err)).into()) } } let config: TConfig = serde::Deserialize::deserialize(config) .map_err(|err| ErrorKind::ReadConfig(None, Box::new(err)))?; let (connector, server) = main(config).await.map_err(ErrorKind::Service)?; log::info!("Starting server..."); let mut incoming = connector .incoming() .await .map_err(|err| ErrorKind::Service(Box::new(err)))?; let () = incoming .serve(server) .await .map_err(|err| ErrorKind::Service(Box::new(err)))?; log::info!("Stopped server."); Ok(()) } fn merge_toml(base: &mut toml::Value, patch: toml::Value) { // Similar to JSON patch, except that: // // - Maps are called tables. // - There is no equivalent of null that can be used to remove keys from an object. // - Arrays are merged via concatenating the patch to the base, rather than replacing the base with the patch. if let toml::Value::Table(base) = base { if let toml::Value::Table(patch) = patch { for (key, value) in patch { // Insert a dummy `false` if the original key didn't exist at all. It'll be overwritten by `value` in that case. let original_value = base.entry(key).or_insert(toml::Value::Boolean(false)); merge_toml(original_value, value); } return; } } if let toml::Value::Array(base) = base { if let toml::Value::Array(patch) = patch { base.extend(patch); return; } } *base = patch; } #[cfg(test)] mod tests { #[test] fn process_name_from_args() { // Success test cases let mut test_cases = vec![ (&["aziot-certd"][..], super::ProcessName::Certd), (&["aziot-identityd"][..], super::ProcessName::Identityd), (&["aziot-keyd"][..], super::ProcessName::Keyd), (&["aziot-tpmd"][..], super::ProcessName::Tpmd), ( &["/usr/libexec/aziot/aziot-certd"][..], super::ProcessName::Certd, ), ( &["/usr/libexec/aziot/aziot-identityd"][..], super::ProcessName::Identityd, ), ( &["/usr/libexec/aziot/aziot-keyd"][..], super::ProcessName::Keyd, ), ( &["/usr/libexec/aziot/aziot-tpmd"][..], super::ProcessName::Tpmd, ), ]; // argv[1] fallback is only in release builds. if cfg!(debug_assertions) { test_cases.extend_from_slice(&[ (&["aziotd", "aziot-certd"][..], super::ProcessName::Certd), ( &["aziotd", "aziot-identityd"][..], super::ProcessName::Identityd, ), (&["aziotd", "aziot-keyd"][..], super::ProcessName::Keyd), ( &["/usr/libexec/aziot/aziotd", "aziot-certd"][..], super::ProcessName::Certd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-identityd"][..], super::ProcessName::Identityd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-keyd"][..], super::ProcessName::Keyd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-tpmd"][..], super::ProcessName::Tpmd, ), ]); } for (input, expected) in test_cases { let mut input = input.iter().copied().map(std::ffi::OsStr::new); let actual = super::process_name_from_args(&mut input).unwrap(); assert_eq!(None, input.next()); assert_eq!(expected, actual); } // Failure test cases for &input in &[ // Unrecognized process name in argv[0] &["foo"][..], &["/usr/libexec/aziot/foo"][..], &["/usr/libexec/aziot/foo", "aziot-certd"][..], // Either fails because it's a release build so argv[1] fallback is disabled, // or fails because it's a debug build where argv[1] fallback is enabled // but the process name in argv[1] is unrecognized anyway. &["aziotd", "foo"][..], &["/usr/libexec/aziot/aziotd", "foo"][..], ] { let mut input = input.iter().copied().map(std::ffi::OsStr::new); let _ = super::process_name_from_args(&mut input).unwrap_err(); } } #[test] fn
() { let base = r#" foo_key = "A" foo_parent_key = { foo_sub_key = "B" } [bar_table] bar_table_key = "C" bar_table_parent_key = { bar_table_sub_key = "D" } [[baz_table_array]] baz_table_array_key = "E" baz_table_array_parent_key = { baz_table_sub_key = "F" } "#; let mut base: toml::Value = toml::from_str(base).unwrap(); let patch = r#" foo_key = "A2" foo_key_new = "A3" foo_parent_key = { foo_sub_key = "B2", foo_sub_key2 = "B3" } foo_parent_key_new = { foo_sub_key = "B4", foo_sub_key2 = "B5" } [bar_table] bar_table_key = "C2" bar_table_key_new = "C3" bar_table_parent_key = { bar_table_sub_key = "D2", bar_table_sub_key2 = "D3" } bar_table_parent_key_new = { bar_table_sub_key = "D4", bar_table_sub_key2 = "D5" } [[baz_table_array]] baz_table_array_key = "G" baz_table_array_parent_key = { baz_table_sub_key = "H" } "#; let patch: toml::Value = toml::from_str(patch).unwrap(); super::merge_toml(&mut base, patch); let expected = r#" foo_key = "A2" foo_key_new = "A3" foo_parent_key = { foo_sub_key = "B2", foo_sub_key2 = "B3" } foo_parent_key_new = { foo_sub_key = "B4", foo_sub_key2 = "B5" } [bar_table] bar_table_key = "C2" bar_table_key_new = "C3" bar_table_parent_key = { bar_table_sub_key = "D2", bar_table_sub_key2 = "D3" } bar_table_parent_key_new = { bar_table_sub_key = "D4", bar_table_sub_key2 = "D5" } [[baz_table_array]] baz_table_array_key = "E" baz_table_array_parent_key = { baz_table_sub_key = "F" } [[baz_table_array]] baz_table_array_key = "G" baz_table_array_parent_key = { baz_table_sub_key = "H" } "#; let expected: toml::Value = toml::from_str(expected).unwrap(); assert_eq!(expected, base); } }
merge_toml
identifier_name
main.rs
// Copyright (c) Microsoft. All rights reserved. //! This binary is the process entrypoint for aziot-certd, -identityd and -keyd. //! Rather than be three separate binaries, all three services are symlinks to //! this one aziotd binary. The aziotd binary looks at its command-line args to figure out //! which service it's being invoked as, and runs the code of that service accordingly. #![deny(rust_2018_idioms)] #![warn(clippy::all, clippy::pedantic)] #![allow(clippy::default_trait_access, clippy::let_unit_value)] mod error; mod logging; use error::{Error, ErrorKind}; #[tokio::main] async fn main() { logging::init(); if let Err(err) = main_inner().await { log::error!("{}", err.0); let mut source = std::error::Error::source(&err.0); while let Some(err) = source { log::error!("caused by: {}", err); source = std::error::Error::source(err); } log::error!("{:?}", err.1); std::process::exit(1); } } async fn main_inner() -> Result<(), Error>
#[derive(Clone, Copy, Debug, PartialEq)] enum ProcessName { Certd, Identityd, Keyd, Tpmd, } /// If the symlink is being used to invoke this binary, the process name can be determined /// from the first arg, ie `argv[0]` in C terms. /// /// An alternative is supported where the binary is invoked as aziotd itself, /// and the process name is instead the next arg, ie `argv[1]` in C terms. /// This is primary useful for local development, so it's only allowed in debug builds. fn process_name_from_args<I>(args: &mut I) -> Result<ProcessName, Error> where I: Iterator, <I as Iterator>::Item: AsRef<std::ffi::OsStr>, { let arg = args.next().ok_or_else(|| { ErrorKind::GetProcessName("could not extract process name from args".into()) })?; // arg could be a single component like "aziot-certd", or a path that ends with "aziot-certd", // so parse it as a Path and get the last component. This does the right thing in either case. let arg = std::path::Path::new(&arg); let process_name = arg.file_name().ok_or_else(|| { ErrorKind::GetProcessName( format!( "could not extract process name from arg {:?}", arg.display(), ) .into(), ) })?; match process_name.to_str() { Some("aziot-certd") => Ok(ProcessName::Certd), Some("aziot-identityd") => Ok(ProcessName::Identityd), Some("aziot-keyd") => Ok(ProcessName::Keyd), Some("aziot-tpmd") => Ok(ProcessName::Tpmd), // The next arg is the process name #[cfg(debug_assertions)] Some("aziotd") => process_name_from_args(args), _ => Err(ErrorKind::GetProcessName( format!("unrecognized process name {:?}", process_name).into(), ) .into()), } } async fn run<TMain, TConfig, TFuture, TServer>( main: TMain, config_env_var: &str, config_file_default: &str, config_directory_env_var: &str, config_directory_default: &str, ) -> Result<(), Error> where TMain: FnOnce(TConfig) -> TFuture, TConfig: serde::de::DeserializeOwned, TFuture: std::future::Future< Output = Result<(http_common::Connector, TServer), Box<dyn std::error::Error>>, >, TServer: hyper::service::Service< hyper::Request<hyper::Body>, Response = hyper::Response<hyper::Body>, Error = std::convert::Infallible, > + Clone + Send + 'static, <TServer as hyper::service::Service<hyper::Request<hyper::Body>>>::Future: Send, { log::info!("Starting service..."); log::info!( "Version - {}", option_env!("PACKAGE_VERSION").unwrap_or("dev build"), ); let config_path: std::path::PathBuf = std::env::var_os(config_env_var).map_or_else(|| config_file_default.into(), Into::into); let config = std::fs::read(&config_path) .map_err(|err| ErrorKind::ReadConfig(Some(config_path.clone()), Box::new(err)))?; let mut config: toml::Value = toml::from_slice(&config) .map_err(|err| ErrorKind::ReadConfig(Some(config_path), Box::new(err)))?; let config_directory_path: std::path::PathBuf = std::env::var_os(config_directory_env_var) .map_or_else(|| config_directory_default.into(), Into::into); match std::fs::read_dir(&config_directory_path) { Ok(entries) => { let mut patch_paths = vec![]; for entry in entries { let entry = entry.map_err(|err| { ErrorKind::ReadConfig(Some(config_directory_path.clone()), Box::new(err)) })?; let entry_file_type = entry.file_type().map_err(|err| { ErrorKind::ReadConfig(Some(config_directory_path.clone()), Box::new(err)) })?; if !entry_file_type.is_file() { continue; } let patch_path = entry.path(); if patch_path.extension().and_then(std::ffi::OsStr::to_str) != Some("toml") { continue; } patch_paths.push(patch_path); } patch_paths.sort(); for patch_path in patch_paths { let patch = std::fs::read(&patch_path).map_err(|err| { ErrorKind::ReadConfig(Some(patch_path.clone()), Box::new(err)) })?; let patch: toml::Value = toml::from_slice(&patch) .map_err(|err| ErrorKind::ReadConfig(Some(patch_path), Box::new(err)))?; merge_toml(&mut config, patch); } } Err(err) if err.kind() == std::io::ErrorKind::NotFound => (), Err(err) => { return Err(ErrorKind::ReadConfig(Some(config_directory_path), Box::new(err)).into()) } } let config: TConfig = serde::Deserialize::deserialize(config) .map_err(|err| ErrorKind::ReadConfig(None, Box::new(err)))?; let (connector, server) = main(config).await.map_err(ErrorKind::Service)?; log::info!("Starting server..."); let mut incoming = connector .incoming() .await .map_err(|err| ErrorKind::Service(Box::new(err)))?; let () = incoming .serve(server) .await .map_err(|err| ErrorKind::Service(Box::new(err)))?; log::info!("Stopped server."); Ok(()) } fn merge_toml(base: &mut toml::Value, patch: toml::Value) { // Similar to JSON patch, except that: // // - Maps are called tables. // - There is no equivalent of null that can be used to remove keys from an object. // - Arrays are merged via concatenating the patch to the base, rather than replacing the base with the patch. if let toml::Value::Table(base) = base { if let toml::Value::Table(patch) = patch { for (key, value) in patch { // Insert a dummy `false` if the original key didn't exist at all. It'll be overwritten by `value` in that case. let original_value = base.entry(key).or_insert(toml::Value::Boolean(false)); merge_toml(original_value, value); } return; } } if let toml::Value::Array(base) = base { if let toml::Value::Array(patch) = patch { base.extend(patch); return; } } *base = patch; } #[cfg(test)] mod tests { #[test] fn process_name_from_args() { // Success test cases let mut test_cases = vec![ (&["aziot-certd"][..], super::ProcessName::Certd), (&["aziot-identityd"][..], super::ProcessName::Identityd), (&["aziot-keyd"][..], super::ProcessName::Keyd), (&["aziot-tpmd"][..], super::ProcessName::Tpmd), ( &["/usr/libexec/aziot/aziot-certd"][..], super::ProcessName::Certd, ), ( &["/usr/libexec/aziot/aziot-identityd"][..], super::ProcessName::Identityd, ), ( &["/usr/libexec/aziot/aziot-keyd"][..], super::ProcessName::Keyd, ), ( &["/usr/libexec/aziot/aziot-tpmd"][..], super::ProcessName::Tpmd, ), ]; // argv[1] fallback is only in release builds. if cfg!(debug_assertions) { test_cases.extend_from_slice(&[ (&["aziotd", "aziot-certd"][..], super::ProcessName::Certd), ( &["aziotd", "aziot-identityd"][..], super::ProcessName::Identityd, ), (&["aziotd", "aziot-keyd"][..], super::ProcessName::Keyd), ( &["/usr/libexec/aziot/aziotd", "aziot-certd"][..], super::ProcessName::Certd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-identityd"][..], super::ProcessName::Identityd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-keyd"][..], super::ProcessName::Keyd, ), ( &["/usr/libexec/aziot/aziotd", "aziot-tpmd"][..], super::ProcessName::Tpmd, ), ]); } for (input, expected) in test_cases { let mut input = input.iter().copied().map(std::ffi::OsStr::new); let actual = super::process_name_from_args(&mut input).unwrap(); assert_eq!(None, input.next()); assert_eq!(expected, actual); } // Failure test cases for &input in &[ // Unrecognized process name in argv[0] &["foo"][..], &["/usr/libexec/aziot/foo"][..], &["/usr/libexec/aziot/foo", "aziot-certd"][..], // Either fails because it's a release build so argv[1] fallback is disabled, // or fails because it's a debug build where argv[1] fallback is enabled // but the process name in argv[1] is unrecognized anyway. &["aziotd", "foo"][..], &["/usr/libexec/aziot/aziotd", "foo"][..], ] { let mut input = input.iter().copied().map(std::ffi::OsStr::new); let _ = super::process_name_from_args(&mut input).unwrap_err(); } } #[test] fn merge_toml() { let base = r#" foo_key = "A" foo_parent_key = { foo_sub_key = "B" } [bar_table] bar_table_key = "C" bar_table_parent_key = { bar_table_sub_key = "D" } [[baz_table_array]] baz_table_array_key = "E" baz_table_array_parent_key = { baz_table_sub_key = "F" } "#; let mut base: toml::Value = toml::from_str(base).unwrap(); let patch = r#" foo_key = "A2" foo_key_new = "A3" foo_parent_key = { foo_sub_key = "B2", foo_sub_key2 = "B3" } foo_parent_key_new = { foo_sub_key = "B4", foo_sub_key2 = "B5" } [bar_table] bar_table_key = "C2" bar_table_key_new = "C3" bar_table_parent_key = { bar_table_sub_key = "D2", bar_table_sub_key2 = "D3" } bar_table_parent_key_new = { bar_table_sub_key = "D4", bar_table_sub_key2 = "D5" } [[baz_table_array]] baz_table_array_key = "G" baz_table_array_parent_key = { baz_table_sub_key = "H" } "#; let patch: toml::Value = toml::from_str(patch).unwrap(); super::merge_toml(&mut base, patch); let expected = r#" foo_key = "A2" foo_key_new = "A3" foo_parent_key = { foo_sub_key = "B2", foo_sub_key2 = "B3" } foo_parent_key_new = { foo_sub_key = "B4", foo_sub_key2 = "B5" } [bar_table] bar_table_key = "C2" bar_table_key_new = "C3" bar_table_parent_key = { bar_table_sub_key = "D2", bar_table_sub_key2 = "D3" } bar_table_parent_key_new = { bar_table_sub_key = "D4", bar_table_sub_key2 = "D5" } [[baz_table_array]] baz_table_array_key = "E" baz_table_array_parent_key = { baz_table_sub_key = "F" } [[baz_table_array]] baz_table_array_key = "G" baz_table_array_parent_key = { baz_table_sub_key = "H" } "#; let expected: toml::Value = toml::from_str(expected).unwrap(); assert_eq!(expected, base); } }
{ let mut args = std::env::args_os(); let process_name = process_name_from_args(&mut args)?; match process_name { ProcessName::Certd => { run( aziot_certd::main, "AZIOT_CERTD_CONFIG", "/etc/aziot/certd/config.toml", "AZIOT_CERTD_CONFIG_DIR", "/etc/aziot/certd/config.d", ) .await? } ProcessName::Identityd => { run( aziot_identityd::main, "AZIOT_IDENTITYD_CONFIG", "/etc/aziot/identityd/config.toml", "AZIOT_IDENTITYD_CONFIG_DIR", "/etc/aziot/identityd/config.d", ) .await? } ProcessName::Keyd => { run( aziot_keyd::main, "AZIOT_KEYD_CONFIG", "/etc/aziot/keyd/config.toml", "AZIOT_KEYD_CONFIG_DIR", "/etc/aziot/keyd/config.d", ) .await? } ProcessName::Tpmd => { run( aziot_tpmd::main, "AZIOT_TPMD_CONFIG", "/etc/aziot/tpmd/config.toml", "AZIOT_TPMD_CONFIG_DIR", "/etc/aziot/tpmd/config.d", ) .await? } } Ok(()) }
identifier_body
blackboxexporter.go
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package blackboxexporter import ( "context" "time" "github.com/Masterminds/semver" appsv1 "k8s.io/api/apps/v1" autoscalingv1 "k8s.io/api/autoscaling/v1" corev1 "k8s.io/api/core/v1" policyv1 "k8s.io/api/policy/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" utilruntime "k8s.io/apimachinery/pkg/util/runtime" vpaautoscalingv1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" "k8s.io/utils/pointer" "sigs.k8s.io/controller-runtime/pkg/client" v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants" resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1" "github.com/gardener/gardener/pkg/client/kubernetes" "github.com/gardener/gardener/pkg/component" "github.com/gardener/gardener/pkg/resourcemanager/controller/garbagecollector/references" "github.com/gardener/gardener/pkg/utils" kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes" "github.com/gardener/gardener/pkg/utils/managedresources" ) const ( // ManagedResourceName is the name of the ManagedResource containing the resource specifications. ManagedResourceName = "shoot-core-blackbox-exporter" labelValue = "blackbox-exporter" labelKeyComponent = "component" ) // Interface contains functions for a blackbox-exporter deployer. type Interface interface { component.DeployWaiter component.MonitoringComponent } // Values is a set of configuration values for the blackbox-exporter. type Values struct { // Image is the container image used for blackbox-exporter. Image string // VPAEnabled marks whether VerticalPodAutoscaler is enabled for the shoot. VPAEnabled bool // KubernetesVersion is the Kubernetes version of the Shoot. KubernetesVersion *semver.Version } // New creates a new instance of DeployWaiter for blackbox-exporter. func New( client client.Client, namespace string, values Values, ) Interface { return &blackboxExporter{ client: client, namespace: namespace, values: values, } } type blackboxExporter struct { client client.Client namespace string values Values } func (b *blackboxExporter) Deploy(ctx context.Context) error { data, err := b.computeResourcesData() if err != nil { return err } return managedresources.CreateForShoot(ctx, b.client, b.namespace, ManagedResourceName, managedresources.LabelValueGardener, false, data) } func (b *blackboxExporter) Destroy(ctx context.Context) error { return managedresources.DeleteForShoot(ctx, b.client, b.namespace, ManagedResourceName) } // TimeoutWaitForManagedResource is the timeout used while waiting for the ManagedResources to become healthy // or deleted. var TimeoutWaitForManagedResource = 2 * time.Minute func (b *blackboxExporter) Wait(ctx context.Context) error { timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource) defer cancel() return managedresources.WaitUntilHealthy(timeoutCtx, b.client, b.namespace, ManagedResourceName) } func (b *blackboxExporter) WaitCleanup(ctx context.Context) error
func (b *blackboxExporter) computeResourcesData() (map[string][]byte, error) { var ( intStrOne = intstr.FromInt(1) registry = managedresources.NewRegistry(kubernetes.ShootScheme, kubernetes.ShootCodec, kubernetes.ShootSerializer) serviceAccount = &corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: getLabels(), }, AutomountServiceAccountToken: pointer.Bool(false), } configMap = &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter-config", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ v1beta1constants.LabelApp: "prometheus", v1beta1constants.LabelRole: v1beta1constants.GardenRoleMonitoring, }, }, Data: map[string]string{ `blackbox.yaml`: `modules: http_kubernetes_service: prober: http timeout: 10s http: headers: Accept: "*/*" Accept-Language: "en-US" tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token preferred_ip_protocol: "ip4" `, }, } ) utilruntime.Must(kubernetesutils.MakeUnique(configMap)) var ( deployment = &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: utils.MergeStringMaps( getLabels(), map[string]string{ resourcesv1alpha1.HighAvailabilityConfigType: resourcesv1alpha1.HighAvailabilityConfigTypeServer, }, ), }, Spec: appsv1.DeploymentSpec{ Replicas: pointer.Int32(1), RevisionHistoryLimit: pointer.Int32(2), Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ labelKeyComponent: labelValue, }, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{}, Labels: utils.MergeStringMaps( getLabels(), map[string]string{ v1beta1constants.LabelNetworkPolicyShootFromSeed: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyToDNS: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyToPublicNetworks: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyShootToAPIServer: v1beta1constants.LabelNetworkPolicyAllowed, }, ), }, Spec: corev1.PodSpec{ ServiceAccountName: serviceAccount.Name, PriorityClassName: "system-cluster-critical", SecurityContext: &corev1.PodSecurityContext{ RunAsUser: pointer.Int64(65534), FSGroup: pointer.Int64(65534), SupplementalGroups: []int64{1}, SeccompProfile: &corev1.SeccompProfile{ Type: corev1.SeccompProfileTypeRuntimeDefault, }, }, Containers: []corev1.Container{ { Name: "blackbox-exporter", Image: b.values.Image, Args: []string{ "--config.file=/etc/blackbox_exporter/blackbox.yaml", "--log.level=debug", }, ImagePullPolicy: corev1.PullIfNotPresent, Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("10m"), corev1.ResourceMemory: resource.MustParse("25Mi"), }, Limits: corev1.ResourceList{ corev1.ResourceMemory: resource.MustParse("128Mi"), }, }, Ports: []corev1.ContainerPort{ { Name: "probe", ContainerPort: int32(9115), Protocol: corev1.ProtocolTCP, }, }, VolumeMounts: []corev1.VolumeMount{ { Name: "blackbox-exporter-config", MountPath: "/etc/blackbox_exporter", }, }, }, }, DNSConfig: &corev1.PodDNSConfig{ Options: []corev1.PodDNSConfigOption{ { Name: "ndots", Value: pointer.String("3"), }, }, }, Volumes: []corev1.Volume{ { Name: "blackbox-exporter-config", VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ Name: configMap.Name, }, }, }, }, }, }, }, }, } service = &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ labelKeyComponent: labelValue, }, }, Spec: corev1.ServiceSpec{ Type: corev1.ServiceTypeClusterIP, Ports: []corev1.ServicePort{ { Name: "probe", Port: int32(9115), Protocol: corev1.ProtocolTCP, }, }, Selector: map[string]string{ labelKeyComponent: labelValue, }, }, } podDisruptionBudget = &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ v1beta1constants.GardenRole: v1beta1constants.GardenRoleMonitoring, labelKeyComponent: labelValue, }, }, Spec: policyv1.PodDisruptionBudgetSpec{ MaxUnavailable: &intStrOne, Selector: deployment.Spec.Selector, }, } vpa *vpaautoscalingv1.VerticalPodAutoscaler ) utilruntime.Must(references.InjectAnnotations(deployment)) if b.values.VPAEnabled { vpaUpdateMode := vpaautoscalingv1.UpdateModeAuto vpaControlledValues := vpaautoscalingv1.ContainerControlledValuesRequestsOnly vpa = &vpaautoscalingv1.VerticalPodAutoscaler{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, }, Spec: vpaautoscalingv1.VerticalPodAutoscalerSpec{ TargetRef: &autoscalingv1.CrossVersionObjectReference{ APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment", Name: deployment.Name, }, UpdatePolicy: &vpaautoscalingv1.PodUpdatePolicy{ UpdateMode: &vpaUpdateMode, }, ResourcePolicy: &vpaautoscalingv1.PodResourcePolicy{ ContainerPolicies: []vpaautoscalingv1.ContainerResourcePolicy{ { ContainerName: vpaautoscalingv1.DefaultContainerResourcePolicy, ControlledValues: &vpaControlledValues, }, }, }, }, } } return registry.AddAllAndSerialize( serviceAccount, configMap, deployment, podDisruptionBudget, service, vpa, ) } func getLabels() map[string]string { return map[string]string{ labelKeyComponent: labelValue, v1beta1constants.GardenRole: v1beta1constants.GardenRoleMonitoring, managedresources.LabelKeyOrigin: managedresources.LabelValueGardener, } }
{ timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource) defer cancel() return managedresources.WaitUntilDeleted(timeoutCtx, b.client, b.namespace, ManagedResourceName) }
identifier_body
blackboxexporter.go
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package blackboxexporter import ( "context" "time" "github.com/Masterminds/semver" appsv1 "k8s.io/api/apps/v1" autoscalingv1 "k8s.io/api/autoscaling/v1" corev1 "k8s.io/api/core/v1" policyv1 "k8s.io/api/policy/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" utilruntime "k8s.io/apimachinery/pkg/util/runtime" vpaautoscalingv1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" "k8s.io/utils/pointer" "sigs.k8s.io/controller-runtime/pkg/client" v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants" resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1" "github.com/gardener/gardener/pkg/client/kubernetes" "github.com/gardener/gardener/pkg/component" "github.com/gardener/gardener/pkg/resourcemanager/controller/garbagecollector/references" "github.com/gardener/gardener/pkg/utils" kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes" "github.com/gardener/gardener/pkg/utils/managedresources" ) const ( // ManagedResourceName is the name of the ManagedResource containing the resource specifications. ManagedResourceName = "shoot-core-blackbox-exporter" labelValue = "blackbox-exporter" labelKeyComponent = "component" ) // Interface contains functions for a blackbox-exporter deployer. type Interface interface { component.DeployWaiter component.MonitoringComponent } // Values is a set of configuration values for the blackbox-exporter. type Values struct { // Image is the container image used for blackbox-exporter. Image string // VPAEnabled marks whether VerticalPodAutoscaler is enabled for the shoot. VPAEnabled bool // KubernetesVersion is the Kubernetes version of the Shoot. KubernetesVersion *semver.Version } // New creates a new instance of DeployWaiter for blackbox-exporter. func New( client client.Client, namespace string, values Values, ) Interface { return &blackboxExporter{ client: client, namespace: namespace, values: values, } } type blackboxExporter struct { client client.Client namespace string values Values } func (b *blackboxExporter) Deploy(ctx context.Context) error { data, err := b.computeResourcesData() if err != nil { return err } return managedresources.CreateForShoot(ctx, b.client, b.namespace, ManagedResourceName, managedresources.LabelValueGardener, false, data) } func (b *blackboxExporter) Destroy(ctx context.Context) error { return managedresources.DeleteForShoot(ctx, b.client, b.namespace, ManagedResourceName) } // TimeoutWaitForManagedResource is the timeout used while waiting for the ManagedResources to become healthy // or deleted. var TimeoutWaitForManagedResource = 2 * time.Minute func (b *blackboxExporter) Wait(ctx context.Context) error { timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource) defer cancel() return managedresources.WaitUntilHealthy(timeoutCtx, b.client, b.namespace, ManagedResourceName) } func (b *blackboxExporter) WaitCleanup(ctx context.Context) error { timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource) defer cancel() return managedresources.WaitUntilDeleted(timeoutCtx, b.client, b.namespace, ManagedResourceName) } func (b *blackboxExporter) computeResourcesData() (map[string][]byte, error) { var ( intStrOne = intstr.FromInt(1) registry = managedresources.NewRegistry(kubernetes.ShootScheme, kubernetes.ShootCodec, kubernetes.ShootSerializer) serviceAccount = &corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: getLabels(), }, AutomountServiceAccountToken: pointer.Bool(false), } configMap = &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter-config", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ v1beta1constants.LabelApp: "prometheus", v1beta1constants.LabelRole: v1beta1constants.GardenRoleMonitoring, }, }, Data: map[string]string{ `blackbox.yaml`: `modules: http_kubernetes_service: prober: http timeout: 10s http: headers: Accept: "*/*" Accept-Language: "en-US" tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token preferred_ip_protocol: "ip4" `, }, } ) utilruntime.Must(kubernetesutils.MakeUnique(configMap)) var ( deployment = &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: utils.MergeStringMaps( getLabels(), map[string]string{ resourcesv1alpha1.HighAvailabilityConfigType: resourcesv1alpha1.HighAvailabilityConfigTypeServer, }, ), }, Spec: appsv1.DeploymentSpec{ Replicas: pointer.Int32(1), RevisionHistoryLimit: pointer.Int32(2), Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ labelKeyComponent: labelValue, }, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{}, Labels: utils.MergeStringMaps( getLabels(), map[string]string{ v1beta1constants.LabelNetworkPolicyShootFromSeed: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyToDNS: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyToPublicNetworks: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyShootToAPIServer: v1beta1constants.LabelNetworkPolicyAllowed, }, ), }, Spec: corev1.PodSpec{ ServiceAccountName: serviceAccount.Name, PriorityClassName: "system-cluster-critical", SecurityContext: &corev1.PodSecurityContext{ RunAsUser: pointer.Int64(65534), FSGroup: pointer.Int64(65534), SupplementalGroups: []int64{1}, SeccompProfile: &corev1.SeccompProfile{ Type: corev1.SeccompProfileTypeRuntimeDefault, }, }, Containers: []corev1.Container{ { Name: "blackbox-exporter", Image: b.values.Image, Args: []string{ "--config.file=/etc/blackbox_exporter/blackbox.yaml", "--log.level=debug", }, ImagePullPolicy: corev1.PullIfNotPresent, Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{
Limits: corev1.ResourceList{ corev1.ResourceMemory: resource.MustParse("128Mi"), }, }, Ports: []corev1.ContainerPort{ { Name: "probe", ContainerPort: int32(9115), Protocol: corev1.ProtocolTCP, }, }, VolumeMounts: []corev1.VolumeMount{ { Name: "blackbox-exporter-config", MountPath: "/etc/blackbox_exporter", }, }, }, }, DNSConfig: &corev1.PodDNSConfig{ Options: []corev1.PodDNSConfigOption{ { Name: "ndots", Value: pointer.String("3"), }, }, }, Volumes: []corev1.Volume{ { Name: "blackbox-exporter-config", VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ Name: configMap.Name, }, }, }, }, }, }, }, }, } service = &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ labelKeyComponent: labelValue, }, }, Spec: corev1.ServiceSpec{ Type: corev1.ServiceTypeClusterIP, Ports: []corev1.ServicePort{ { Name: "probe", Port: int32(9115), Protocol: corev1.ProtocolTCP, }, }, Selector: map[string]string{ labelKeyComponent: labelValue, }, }, } podDisruptionBudget = &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ v1beta1constants.GardenRole: v1beta1constants.GardenRoleMonitoring, labelKeyComponent: labelValue, }, }, Spec: policyv1.PodDisruptionBudgetSpec{ MaxUnavailable: &intStrOne, Selector: deployment.Spec.Selector, }, } vpa *vpaautoscalingv1.VerticalPodAutoscaler ) utilruntime.Must(references.InjectAnnotations(deployment)) if b.values.VPAEnabled { vpaUpdateMode := vpaautoscalingv1.UpdateModeAuto vpaControlledValues := vpaautoscalingv1.ContainerControlledValuesRequestsOnly vpa = &vpaautoscalingv1.VerticalPodAutoscaler{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, }, Spec: vpaautoscalingv1.VerticalPodAutoscalerSpec{ TargetRef: &autoscalingv1.CrossVersionObjectReference{ APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment", Name: deployment.Name, }, UpdatePolicy: &vpaautoscalingv1.PodUpdatePolicy{ UpdateMode: &vpaUpdateMode, }, ResourcePolicy: &vpaautoscalingv1.PodResourcePolicy{ ContainerPolicies: []vpaautoscalingv1.ContainerResourcePolicy{ { ContainerName: vpaautoscalingv1.DefaultContainerResourcePolicy, ControlledValues: &vpaControlledValues, }, }, }, }, } } return registry.AddAllAndSerialize( serviceAccount, configMap, deployment, podDisruptionBudget, service, vpa, ) } func getLabels() map[string]string { return map[string]string{ labelKeyComponent: labelValue, v1beta1constants.GardenRole: v1beta1constants.GardenRoleMonitoring, managedresources.LabelKeyOrigin: managedresources.LabelValueGardener, } }
corev1.ResourceCPU: resource.MustParse("10m"), corev1.ResourceMemory: resource.MustParse("25Mi"), },
random_line_split
blackboxexporter.go
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package blackboxexporter import ( "context" "time" "github.com/Masterminds/semver" appsv1 "k8s.io/api/apps/v1" autoscalingv1 "k8s.io/api/autoscaling/v1" corev1 "k8s.io/api/core/v1" policyv1 "k8s.io/api/policy/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" utilruntime "k8s.io/apimachinery/pkg/util/runtime" vpaautoscalingv1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" "k8s.io/utils/pointer" "sigs.k8s.io/controller-runtime/pkg/client" v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants" resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1" "github.com/gardener/gardener/pkg/client/kubernetes" "github.com/gardener/gardener/pkg/component" "github.com/gardener/gardener/pkg/resourcemanager/controller/garbagecollector/references" "github.com/gardener/gardener/pkg/utils" kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes" "github.com/gardener/gardener/pkg/utils/managedresources" ) const ( // ManagedResourceName is the name of the ManagedResource containing the resource specifications. ManagedResourceName = "shoot-core-blackbox-exporter" labelValue = "blackbox-exporter" labelKeyComponent = "component" ) // Interface contains functions for a blackbox-exporter deployer. type Interface interface { component.DeployWaiter component.MonitoringComponent } // Values is a set of configuration values for the blackbox-exporter. type Values struct { // Image is the container image used for blackbox-exporter. Image string // VPAEnabled marks whether VerticalPodAutoscaler is enabled for the shoot. VPAEnabled bool // KubernetesVersion is the Kubernetes version of the Shoot. KubernetesVersion *semver.Version } // New creates a new instance of DeployWaiter for blackbox-exporter. func New( client client.Client, namespace string, values Values, ) Interface { return &blackboxExporter{ client: client, namespace: namespace, values: values, } } type blackboxExporter struct { client client.Client namespace string values Values } func (b *blackboxExporter) Deploy(ctx context.Context) error { data, err := b.computeResourcesData() if err != nil { return err } return managedresources.CreateForShoot(ctx, b.client, b.namespace, ManagedResourceName, managedresources.LabelValueGardener, false, data) } func (b *blackboxExporter) Destroy(ctx context.Context) error { return managedresources.DeleteForShoot(ctx, b.client, b.namespace, ManagedResourceName) } // TimeoutWaitForManagedResource is the timeout used while waiting for the ManagedResources to become healthy // or deleted. var TimeoutWaitForManagedResource = 2 * time.Minute func (b *blackboxExporter) Wait(ctx context.Context) error { timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource) defer cancel() return managedresources.WaitUntilHealthy(timeoutCtx, b.client, b.namespace, ManagedResourceName) } func (b *blackboxExporter) WaitCleanup(ctx context.Context) error { timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource) defer cancel() return managedresources.WaitUntilDeleted(timeoutCtx, b.client, b.namespace, ManagedResourceName) } func (b *blackboxExporter) computeResourcesData() (map[string][]byte, error) { var ( intStrOne = intstr.FromInt(1) registry = managedresources.NewRegistry(kubernetes.ShootScheme, kubernetes.ShootCodec, kubernetes.ShootSerializer) serviceAccount = &corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: getLabels(), }, AutomountServiceAccountToken: pointer.Bool(false), } configMap = &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter-config", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ v1beta1constants.LabelApp: "prometheus", v1beta1constants.LabelRole: v1beta1constants.GardenRoleMonitoring, }, }, Data: map[string]string{ `blackbox.yaml`: `modules: http_kubernetes_service: prober: http timeout: 10s http: headers: Accept: "*/*" Accept-Language: "en-US" tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token preferred_ip_protocol: "ip4" `, }, } ) utilruntime.Must(kubernetesutils.MakeUnique(configMap)) var ( deployment = &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: utils.MergeStringMaps( getLabels(), map[string]string{ resourcesv1alpha1.HighAvailabilityConfigType: resourcesv1alpha1.HighAvailabilityConfigTypeServer, }, ), }, Spec: appsv1.DeploymentSpec{ Replicas: pointer.Int32(1), RevisionHistoryLimit: pointer.Int32(2), Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ labelKeyComponent: labelValue, }, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{}, Labels: utils.MergeStringMaps( getLabels(), map[string]string{ v1beta1constants.LabelNetworkPolicyShootFromSeed: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyToDNS: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyToPublicNetworks: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyShootToAPIServer: v1beta1constants.LabelNetworkPolicyAllowed, }, ), }, Spec: corev1.PodSpec{ ServiceAccountName: serviceAccount.Name, PriorityClassName: "system-cluster-critical", SecurityContext: &corev1.PodSecurityContext{ RunAsUser: pointer.Int64(65534), FSGroup: pointer.Int64(65534), SupplementalGroups: []int64{1}, SeccompProfile: &corev1.SeccompProfile{ Type: corev1.SeccompProfileTypeRuntimeDefault, }, }, Containers: []corev1.Container{ { Name: "blackbox-exporter", Image: b.values.Image, Args: []string{ "--config.file=/etc/blackbox_exporter/blackbox.yaml", "--log.level=debug", }, ImagePullPolicy: corev1.PullIfNotPresent, Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("10m"), corev1.ResourceMemory: resource.MustParse("25Mi"), }, Limits: corev1.ResourceList{ corev1.ResourceMemory: resource.MustParse("128Mi"), }, }, Ports: []corev1.ContainerPort{ { Name: "probe", ContainerPort: int32(9115), Protocol: corev1.ProtocolTCP, }, }, VolumeMounts: []corev1.VolumeMount{ { Name: "blackbox-exporter-config", MountPath: "/etc/blackbox_exporter", }, }, }, }, DNSConfig: &corev1.PodDNSConfig{ Options: []corev1.PodDNSConfigOption{ { Name: "ndots", Value: pointer.String("3"), }, }, }, Volumes: []corev1.Volume{ { Name: "blackbox-exporter-config", VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ Name: configMap.Name, }, }, }, }, }, }, }, }, } service = &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ labelKeyComponent: labelValue, }, }, Spec: corev1.ServiceSpec{ Type: corev1.ServiceTypeClusterIP, Ports: []corev1.ServicePort{ { Name: "probe", Port: int32(9115), Protocol: corev1.ProtocolTCP, }, }, Selector: map[string]string{ labelKeyComponent: labelValue, }, }, } podDisruptionBudget = &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ v1beta1constants.GardenRole: v1beta1constants.GardenRoleMonitoring, labelKeyComponent: labelValue, }, }, Spec: policyv1.PodDisruptionBudgetSpec{ MaxUnavailable: &intStrOne, Selector: deployment.Spec.Selector, }, } vpa *vpaautoscalingv1.VerticalPodAutoscaler ) utilruntime.Must(references.InjectAnnotations(deployment)) if b.values.VPAEnabled { vpaUpdateMode := vpaautoscalingv1.UpdateModeAuto vpaControlledValues := vpaautoscalingv1.ContainerControlledValuesRequestsOnly vpa = &vpaautoscalingv1.VerticalPodAutoscaler{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, }, Spec: vpaautoscalingv1.VerticalPodAutoscalerSpec{ TargetRef: &autoscalingv1.CrossVersionObjectReference{ APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment", Name: deployment.Name, }, UpdatePolicy: &vpaautoscalingv1.PodUpdatePolicy{ UpdateMode: &vpaUpdateMode, }, ResourcePolicy: &vpaautoscalingv1.PodResourcePolicy{ ContainerPolicies: []vpaautoscalingv1.ContainerResourcePolicy{ { ContainerName: vpaautoscalingv1.DefaultContainerResourcePolicy, ControlledValues: &vpaControlledValues, }, }, }, }, } } return registry.AddAllAndSerialize( serviceAccount, configMap, deployment, podDisruptionBudget, service, vpa, ) } func
() map[string]string { return map[string]string{ labelKeyComponent: labelValue, v1beta1constants.GardenRole: v1beta1constants.GardenRoleMonitoring, managedresources.LabelKeyOrigin: managedresources.LabelValueGardener, } }
getLabels
identifier_name
blackboxexporter.go
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package blackboxexporter import ( "context" "time" "github.com/Masterminds/semver" appsv1 "k8s.io/api/apps/v1" autoscalingv1 "k8s.io/api/autoscaling/v1" corev1 "k8s.io/api/core/v1" policyv1 "k8s.io/api/policy/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" utilruntime "k8s.io/apimachinery/pkg/util/runtime" vpaautoscalingv1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" "k8s.io/utils/pointer" "sigs.k8s.io/controller-runtime/pkg/client" v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants" resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1" "github.com/gardener/gardener/pkg/client/kubernetes" "github.com/gardener/gardener/pkg/component" "github.com/gardener/gardener/pkg/resourcemanager/controller/garbagecollector/references" "github.com/gardener/gardener/pkg/utils" kubernetesutils "github.com/gardener/gardener/pkg/utils/kubernetes" "github.com/gardener/gardener/pkg/utils/managedresources" ) const ( // ManagedResourceName is the name of the ManagedResource containing the resource specifications. ManagedResourceName = "shoot-core-blackbox-exporter" labelValue = "blackbox-exporter" labelKeyComponent = "component" ) // Interface contains functions for a blackbox-exporter deployer. type Interface interface { component.DeployWaiter component.MonitoringComponent } // Values is a set of configuration values for the blackbox-exporter. type Values struct { // Image is the container image used for blackbox-exporter. Image string // VPAEnabled marks whether VerticalPodAutoscaler is enabled for the shoot. VPAEnabled bool // KubernetesVersion is the Kubernetes version of the Shoot. KubernetesVersion *semver.Version } // New creates a new instance of DeployWaiter for blackbox-exporter. func New( client client.Client, namespace string, values Values, ) Interface { return &blackboxExporter{ client: client, namespace: namespace, values: values, } } type blackboxExporter struct { client client.Client namespace string values Values } func (b *blackboxExporter) Deploy(ctx context.Context) error { data, err := b.computeResourcesData() if err != nil
return managedresources.CreateForShoot(ctx, b.client, b.namespace, ManagedResourceName, managedresources.LabelValueGardener, false, data) } func (b *blackboxExporter) Destroy(ctx context.Context) error { return managedresources.DeleteForShoot(ctx, b.client, b.namespace, ManagedResourceName) } // TimeoutWaitForManagedResource is the timeout used while waiting for the ManagedResources to become healthy // or deleted. var TimeoutWaitForManagedResource = 2 * time.Minute func (b *blackboxExporter) Wait(ctx context.Context) error { timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource) defer cancel() return managedresources.WaitUntilHealthy(timeoutCtx, b.client, b.namespace, ManagedResourceName) } func (b *blackboxExporter) WaitCleanup(ctx context.Context) error { timeoutCtx, cancel := context.WithTimeout(ctx, TimeoutWaitForManagedResource) defer cancel() return managedresources.WaitUntilDeleted(timeoutCtx, b.client, b.namespace, ManagedResourceName) } func (b *blackboxExporter) computeResourcesData() (map[string][]byte, error) { var ( intStrOne = intstr.FromInt(1) registry = managedresources.NewRegistry(kubernetes.ShootScheme, kubernetes.ShootCodec, kubernetes.ShootSerializer) serviceAccount = &corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: getLabels(), }, AutomountServiceAccountToken: pointer.Bool(false), } configMap = &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter-config", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ v1beta1constants.LabelApp: "prometheus", v1beta1constants.LabelRole: v1beta1constants.GardenRoleMonitoring, }, }, Data: map[string]string{ `blackbox.yaml`: `modules: http_kubernetes_service: prober: http timeout: 10s http: headers: Accept: "*/*" Accept-Language: "en-US" tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token preferred_ip_protocol: "ip4" `, }, } ) utilruntime.Must(kubernetesutils.MakeUnique(configMap)) var ( deployment = &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: utils.MergeStringMaps( getLabels(), map[string]string{ resourcesv1alpha1.HighAvailabilityConfigType: resourcesv1alpha1.HighAvailabilityConfigTypeServer, }, ), }, Spec: appsv1.DeploymentSpec{ Replicas: pointer.Int32(1), RevisionHistoryLimit: pointer.Int32(2), Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ labelKeyComponent: labelValue, }, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Annotations: map[string]string{}, Labels: utils.MergeStringMaps( getLabels(), map[string]string{ v1beta1constants.LabelNetworkPolicyShootFromSeed: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyToDNS: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyToPublicNetworks: v1beta1constants.LabelNetworkPolicyAllowed, v1beta1constants.LabelNetworkPolicyShootToAPIServer: v1beta1constants.LabelNetworkPolicyAllowed, }, ), }, Spec: corev1.PodSpec{ ServiceAccountName: serviceAccount.Name, PriorityClassName: "system-cluster-critical", SecurityContext: &corev1.PodSecurityContext{ RunAsUser: pointer.Int64(65534), FSGroup: pointer.Int64(65534), SupplementalGroups: []int64{1}, SeccompProfile: &corev1.SeccompProfile{ Type: corev1.SeccompProfileTypeRuntimeDefault, }, }, Containers: []corev1.Container{ { Name: "blackbox-exporter", Image: b.values.Image, Args: []string{ "--config.file=/etc/blackbox_exporter/blackbox.yaml", "--log.level=debug", }, ImagePullPolicy: corev1.PullIfNotPresent, Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("10m"), corev1.ResourceMemory: resource.MustParse("25Mi"), }, Limits: corev1.ResourceList{ corev1.ResourceMemory: resource.MustParse("128Mi"), }, }, Ports: []corev1.ContainerPort{ { Name: "probe", ContainerPort: int32(9115), Protocol: corev1.ProtocolTCP, }, }, VolumeMounts: []corev1.VolumeMount{ { Name: "blackbox-exporter-config", MountPath: "/etc/blackbox_exporter", }, }, }, }, DNSConfig: &corev1.PodDNSConfig{ Options: []corev1.PodDNSConfigOption{ { Name: "ndots", Value: pointer.String("3"), }, }, }, Volumes: []corev1.Volume{ { Name: "blackbox-exporter-config", VolumeSource: corev1.VolumeSource{ ConfigMap: &corev1.ConfigMapVolumeSource{ LocalObjectReference: corev1.LocalObjectReference{ Name: configMap.Name, }, }, }, }, }, }, }, }, } service = &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ labelKeyComponent: labelValue, }, }, Spec: corev1.ServiceSpec{ Type: corev1.ServiceTypeClusterIP, Ports: []corev1.ServicePort{ { Name: "probe", Port: int32(9115), Protocol: corev1.ProtocolTCP, }, }, Selector: map[string]string{ labelKeyComponent: labelValue, }, }, } podDisruptionBudget = &policyv1.PodDisruptionBudget{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, Labels: map[string]string{ v1beta1constants.GardenRole: v1beta1constants.GardenRoleMonitoring, labelKeyComponent: labelValue, }, }, Spec: policyv1.PodDisruptionBudgetSpec{ MaxUnavailable: &intStrOne, Selector: deployment.Spec.Selector, }, } vpa *vpaautoscalingv1.VerticalPodAutoscaler ) utilruntime.Must(references.InjectAnnotations(deployment)) if b.values.VPAEnabled { vpaUpdateMode := vpaautoscalingv1.UpdateModeAuto vpaControlledValues := vpaautoscalingv1.ContainerControlledValuesRequestsOnly vpa = &vpaautoscalingv1.VerticalPodAutoscaler{ ObjectMeta: metav1.ObjectMeta{ Name: "blackbox-exporter", Namespace: metav1.NamespaceSystem, }, Spec: vpaautoscalingv1.VerticalPodAutoscalerSpec{ TargetRef: &autoscalingv1.CrossVersionObjectReference{ APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment", Name: deployment.Name, }, UpdatePolicy: &vpaautoscalingv1.PodUpdatePolicy{ UpdateMode: &vpaUpdateMode, }, ResourcePolicy: &vpaautoscalingv1.PodResourcePolicy{ ContainerPolicies: []vpaautoscalingv1.ContainerResourcePolicy{ { ContainerName: vpaautoscalingv1.DefaultContainerResourcePolicy, ControlledValues: &vpaControlledValues, }, }, }, }, } } return registry.AddAllAndSerialize( serviceAccount, configMap, deployment, podDisruptionBudget, service, vpa, ) } func getLabels() map[string]string { return map[string]string{ labelKeyComponent: labelValue, v1beta1constants.GardenRole: v1beta1constants.GardenRoleMonitoring, managedresources.LabelKeyOrigin: managedresources.LabelValueGardener, } }
{ return err }
conditional_block
mod.rs
mod strata; mod assigners; mod samplers; pub mod serial_storage; use std::cmp::max; use std::ops::Range; use std::sync::Arc; use std::sync::RwLock; use std::thread::spawn; use std::thread::sleep; use std::time::Duration; use evmap; use rand; use commons::bins::Bins; use commons::channel; use commons::channel::Receiver; use commons::channel::Sender; use commons::get_sign; use commons::ExampleWithScore; use commons::Model; use commons::Signal; use labeled_data::LabeledData; use super::Example; use super::TFeature; use self::assigners::Assigners; use self::samplers::Samplers; use self::serial_storage::SerialStorage; use self::strata::Strata; pub const SPEED_TEST: bool = false; pub struct F64 { pub val: f64 } impl PartialEq for F64 { fn eq(&self, other: &F64) -> bool { get_sign(self.val - other.val) == 0 } } impl Eq for F64 {} type WeightTableRead = evmap::ReadHandle<i8, Box<F64>>; pub struct StratifiedStorage { // num_examples: usize, // feature_size: usize, // num_examples_per_block: usize, // disk_buffer_filename: String, // strata: Arc<RwLock<Strata>>, // stats_update_s: Sender<(i8, (i32, f64))>, #[allow(dead_code)] counts_table_r: evmap::ReadHandle<i8, i32>, #[allow(dead_code)] weights_table_r: WeightTableRead, // num_assigners: usize, // num_samplers: usize, // updated_examples_r: Receiver<ExampleWithScore>, updated_examples_s: Sender<ExampleWithScore>, // sampled_examples_s: Sender<(ExampleWithScore, u32)>, // sampling_signal: Receiver<Signal>, // models: Receiver<Model>, positive: String, } impl StratifiedStorage { /// Create the stratified storage structure. /// /// * `num_examples`: the total number of examples in the training data set /// * `feature_size`: the number of features of the training examples /// * `num_examples_per_block`: the number of examples to write back to disk in batch (explained below) /// * `disk_buffer_filename`: the name of the binary file for saving the examples in strata on disk /// If such file does not exist, it will be created /// * `num_assigners`: the number of threads that run the `Assigner`s (explained below) /// * `num_samplers`: the number of threads that run the `Sampler`s (explained below) /// * `sampled_examples`: the channle that the stratified storage sends the sampled examples to /// the buffer loader /// * `sampling_singal`: the channle that the buffer loader sends sampling signals to /// start and stop the samplers as needed /// * `models`: the channel that the booster sends the latest models in /// /// Stratified storage organizes training examples according to their weights /// given current learning model. /// The examples are assigned to different strata so that the weight ratio of the examples /// within the same stratum does not exceed 2. /// Most examples in a stratum are stored on disk, while a small number of examples remains /// in memory to be writen to disk or just read from disk and ready to send out to the sampler. /// /// The overall structure of the stratified storage is as follow: /// /// ![](https://www.lucidchart.com/publicSegments/view/c87b7a50-5442-4a41-a601-3dfb49b16511/image.png) /// /// The `Assigner`s read examples with updated scores from the `Sampler` and write them back to /// the corresponding strata based on their new weights. The examples would be put into the /// `In Queue`s first till a proper number of examples are accumulated that belongs to the /// same strata, at that point they would be written into disk in batch. /// /// Meanwhile, a certain number of examples from each stratum are loaded into the memory /// from the disk and kept in `Out Queue`s. /// The `Sampler`s iteratively select a stratum with a probability that proportional to /// the sum of weights of all examples in that stratum, send its next sampled example to the memory /// buffer, and remove that example from strata. /// /// A `Shared Weight Table` maintains the sum of the weights of all examples in each stratum. /// The `Assigner`s increase the value in the `Shared Weight Table` when a new example is inserted into /// a stratum. /// The `Sampler`s use the weights in the `Shared Weight Table` to decide which stratum to read next and /// send its next sampled example to the memory buffer. After an example is processed, the `Sampler` also /// updates its weight, sends it to right stratum, and updates `Shared Weight Table` accordingly. pub fn new( num_examples: usize, feature_size: usize, positive: String, num_examples_per_block: usize, disk_buffer_filename: &str, num_assigners: usize, num_samplers: usize, sampled_examples: Sender<(ExampleWithScore, u32)>, sampling_signal: Receiver<Signal>, models: Receiver<Model>, channel_size: usize, debug_mode: bool, ) -> StratifiedStorage { let strata = Strata::new(num_examples, feature_size, num_examples_per_block, disk_buffer_filename); let strata = Arc::new(RwLock::new(strata)); let (counts_table_r, mut counts_table_w) = evmap::new(); let (weights_table_r, mut weights_table_w) = evmap::new(); let (updated_examples_s, updated_examples_r) = channel::bounded(channel_size, "updated-examples"); // The messages in the stats channel are very small, so its capacity can be larger. let (stats_update_s, stats_update_r) = channel::bounded(5000000, "stats"); // Update shared weights table (non-blocking) { let counts_table_r = counts_table_r.clone(); let weights_table_r = weights_table_r.clone(); spawn(move || { while let Some((index, (count, weight))) = stats_update_r.recv() { let val = counts_table_r.get_and(&index, |vs| vs[0]); counts_table_w.update(index, val.unwrap_or(0) + count); let cur = weights_table_r.get_and(&index, |vs: &[Box<F64>]| vs[0].val) .unwrap_or(0.0); weights_table_w.update(index, Box::new(F64 { val: cur + weight })); { counts_table_w.refresh(); weights_table_w.refresh(); } } }); } // Monitor the distribution of strata if debug_mode { let counts_table_r = counts_table_r.clone(); let weights_table_r = weights_table_r.clone(); spawn(move || { loop { sleep(Duration::from_millis(5000)); let mut p: Vec<(i8, f64)> = weights_table_r.map_into(|a: &i8, b: &[Box<F64>]| (a.clone(), b[0].val)); p.sort_by(|a, b| (a.0).cmp(&b.0)); let mut c: Vec<(i8, i32)> = counts_table_r.map_into(|a, b| (a.clone(), b[0])); c.sort_by(|a, b| (a.0).cmp(&b.0)); let mut sump: f64 = p.iter().map(|t| t.1).sum(); if get_sign(sump) == 0 { sump = 1.0; } let ps: Vec<String> = p.into_iter() .map(|(idx, w)| (idx, 100.0 * w / sump)) .map(|(idx, w)| format!("({}, {:.2})", idx, w)) .collect(); debug!("strata weights distr, {}, {}", ps.join(", "), sump); let sumc: i32 = max(c.iter().map(|t| t.1).sum(), 1); let cs: Vec<String> = c.into_iter() .map(|(idx, c)| (idx, 100.0 * c as f32 / (sumc as f32))) .map(|(idx, c)| format!("({}, {:.2})", idx, c)) .collect(); debug!("strata counts distr, {}, {}", cs.join(", "), sumc); } }); } let assigners = Assigners::new( updated_examples_r, strata.clone(), stats_update_s.clone(), num_assigners, ); let samplers = Samplers::new( strata.clone(), sampled_examples.clone(), updated_examples_s.clone(), models.clone(), stats_update_s.clone(), weights_table_r.clone(), sampling_signal.clone(), num_samplers, ); assigners.run(); samplers.run(); StratifiedStorage { // num_examples: num_examples, // feature_size: feature_size, // num_examples_per_block: num_examples_per_block, // disk_buffer_filename: String::from(disk_buffer_filename), // strata: strata, // stats_update_s: stats_update_s, counts_table_r: counts_table_r, weights_table_r: weights_table_r, // num_assigners: num_assigners, // num_samplers: num_samplers, // updated_examples_r: updated_examples_r, updated_examples_s: updated_examples_s, // sampled_examples_s: sampled_examples, // sampling_signal: sampling_signal, // models: models, positive: positive, } } pub fn init_stratified_from_file( &self, filename: String, size: usize, batch_size: usize, feature_size: usize, range: Range<usize>, bins: Vec<Bins>, ) { let mut reader = SerialStorage::new( filename.clone(), size, feature_size, true, self.positive.clone(), None, range.clone(), ); let updated_examples_s = self.updated_examples_s.clone(); spawn(move || { let mut index = 0; while index < size { reader.read_raw(batch_size).into_iter().for_each(|data| { let features: Vec<TFeature> = data.feature.iter().enumerate() .map(|(idx, val)| { if range.start <= idx && idx < range.end { bins[idx - range.start].get_split_index(*val) } else { 0 } }).collect(); let mapped_data = LabeledData::new(features, data.label); updated_examples_s.send((mapped_data, (0.0, 0))); }); index += batch_size; } debug!("Raw data on disk has been loaded into the stratified storage, \ filename {}, capacity {}, feature size {}", filename, size, feature_size); }); } } fn sample_weights_table(weights_table_r: &WeightTableRead) -> Option<i8> { let p: Vec<(i8, f64)> = weights_table_r.map_into(|a, b| (a.clone(), b[0].val)); let sum_of_weights: f64 = p.iter().map(|t| t.1).sum(); if get_sign(sum_of_weights) == 0 { None } else { let mut frac = rand::random::<f64>() * sum_of_weights; let mut iter = p.iter(); let mut key_val = &(0, 0.0); while get_sign(frac) >= 0 { key_val = iter.next().expect("get_sample_from: input p is empty"); frac -= key_val.1; } Some(key_val.0) } } #[cfg(test)] mod tests { extern crate env_logger; use std::fs::remove_file; use commons::channel; use std::thread::spawn; use labeled_data::LabeledData; use commons::ExampleWithScore; use commons::Signal; use commons::performance_monitor::PerformanceMonitor; use super::StratifiedStorage; use ::TFeature; #[test] fn test_mean() { let _ = env_logger::try_init(); let filename = "unittest-stratified3.bin"; let batch = 100000; let num_read = 1000000; let (sampled_examples_send, sampled_examples_recv) = channel::bounded(1000, "sampled-examples"); let (_, models_recv) = channel::bounded(10, "updated-models"); let (signal_s, signal_r) = channel::bounded(10, "sampling-signal"); signal_s.send(Signal::START); let stratified_storage = StratifiedStorage::new( batch * 10, 1, "1".to_string(), 10000, filename, 4, 4, sampled_examples_send, signal_r, models_recv, 10, false, ); let updated_examples_send = stratified_storage.updated_examples_s.clone(); let mut pm_load = PerformanceMonitor::new(); pm_load.start(); let loading = spawn(move || { for _ in 0..batch {
updated_examples_send.send(t.clone()); } } println!("Loading speed: {}", (batch * 10) as f32 / pm_load.get_duration()); }); let mut pm_sample = PerformanceMonitor::new(); pm_sample.start(); let mut average = 0.0; for _ in 0..num_read { let recv = sampled_examples_recv.recv().unwrap(); average += (((recv.0).0).feature[0] as f32) * (recv.1 as f32) / (num_read as f32); pm_sample.update(recv.1 as usize); } spawn(move || { println!("Sampling speed: {}", num_read as f32 / pm_sample.get_duration()); }); let answer = (1..11).map(|a| a as f32).map(|a| a * a).sum::<f32>() / ((1..11).sum::<i32>() as f32); loading.join().unwrap(); if (average - answer).abs() > 0.05 { spawn(move || { println!("Average: {}. Expect: {}.", average, answer); }).join().unwrap(); assert!(false); } remove_file(filename).unwrap(); } fn get_example(feature: Vec<TFeature>, weight: f32) -> ExampleWithScore { let label: i8 = 1; let example = LabeledData::new(feature, label); let score = -weight.ln(); (example, (score, 0)) } }
for i in 1..11 { let t = get_example(vec![i as TFeature], i as f32);
random_line_split
mod.rs
mod strata; mod assigners; mod samplers; pub mod serial_storage; use std::cmp::max; use std::ops::Range; use std::sync::Arc; use std::sync::RwLock; use std::thread::spawn; use std::thread::sleep; use std::time::Duration; use evmap; use rand; use commons::bins::Bins; use commons::channel; use commons::channel::Receiver; use commons::channel::Sender; use commons::get_sign; use commons::ExampleWithScore; use commons::Model; use commons::Signal; use labeled_data::LabeledData; use super::Example; use super::TFeature; use self::assigners::Assigners; use self::samplers::Samplers; use self::serial_storage::SerialStorage; use self::strata::Strata; pub const SPEED_TEST: bool = false; pub struct F64 { pub val: f64 } impl PartialEq for F64 { fn eq(&self, other: &F64) -> bool { get_sign(self.val - other.val) == 0 } } impl Eq for F64 {} type WeightTableRead = evmap::ReadHandle<i8, Box<F64>>; pub struct StratifiedStorage { // num_examples: usize, // feature_size: usize, // num_examples_per_block: usize, // disk_buffer_filename: String, // strata: Arc<RwLock<Strata>>, // stats_update_s: Sender<(i8, (i32, f64))>, #[allow(dead_code)] counts_table_r: evmap::ReadHandle<i8, i32>, #[allow(dead_code)] weights_table_r: WeightTableRead, // num_assigners: usize, // num_samplers: usize, // updated_examples_r: Receiver<ExampleWithScore>, updated_examples_s: Sender<ExampleWithScore>, // sampled_examples_s: Sender<(ExampleWithScore, u32)>, // sampling_signal: Receiver<Signal>, // models: Receiver<Model>, positive: String, } impl StratifiedStorage { /// Create the stratified storage structure. /// /// * `num_examples`: the total number of examples in the training data set /// * `feature_size`: the number of features of the training examples /// * `num_examples_per_block`: the number of examples to write back to disk in batch (explained below) /// * `disk_buffer_filename`: the name of the binary file for saving the examples in strata on disk /// If such file does not exist, it will be created /// * `num_assigners`: the number of threads that run the `Assigner`s (explained below) /// * `num_samplers`: the number of threads that run the `Sampler`s (explained below) /// * `sampled_examples`: the channle that the stratified storage sends the sampled examples to /// the buffer loader /// * `sampling_singal`: the channle that the buffer loader sends sampling signals to /// start and stop the samplers as needed /// * `models`: the channel that the booster sends the latest models in /// /// Stratified storage organizes training examples according to their weights /// given current learning model. /// The examples are assigned to different strata so that the weight ratio of the examples /// within the same stratum does not exceed 2. /// Most examples in a stratum are stored on disk, while a small number of examples remains /// in memory to be writen to disk or just read from disk and ready to send out to the sampler. /// /// The overall structure of the stratified storage is as follow: /// /// ![](https://www.lucidchart.com/publicSegments/view/c87b7a50-5442-4a41-a601-3dfb49b16511/image.png) /// /// The `Assigner`s read examples with updated scores from the `Sampler` and write them back to /// the corresponding strata based on their new weights. The examples would be put into the /// `In Queue`s first till a proper number of examples are accumulated that belongs to the /// same strata, at that point they would be written into disk in batch. /// /// Meanwhile, a certain number of examples from each stratum are loaded into the memory /// from the disk and kept in `Out Queue`s. /// The `Sampler`s iteratively select a stratum with a probability that proportional to /// the sum of weights of all examples in that stratum, send its next sampled example to the memory /// buffer, and remove that example from strata. /// /// A `Shared Weight Table` maintains the sum of the weights of all examples in each stratum. /// The `Assigner`s increase the value in the `Shared Weight Table` when a new example is inserted into /// a stratum. /// The `Sampler`s use the weights in the `Shared Weight Table` to decide which stratum to read next and /// send its next sampled example to the memory buffer. After an example is processed, the `Sampler` also /// updates its weight, sends it to right stratum, and updates `Shared Weight Table` accordingly. pub fn new( num_examples: usize, feature_size: usize, positive: String, num_examples_per_block: usize, disk_buffer_filename: &str, num_assigners: usize, num_samplers: usize, sampled_examples: Sender<(ExampleWithScore, u32)>, sampling_signal: Receiver<Signal>, models: Receiver<Model>, channel_size: usize, debug_mode: bool, ) -> StratifiedStorage { let strata = Strata::new(num_examples, feature_size, num_examples_per_block, disk_buffer_filename); let strata = Arc::new(RwLock::new(strata)); let (counts_table_r, mut counts_table_w) = evmap::new(); let (weights_table_r, mut weights_table_w) = evmap::new(); let (updated_examples_s, updated_examples_r) = channel::bounded(channel_size, "updated-examples"); // The messages in the stats channel are very small, so its capacity can be larger. let (stats_update_s, stats_update_r) = channel::bounded(5000000, "stats"); // Update shared weights table (non-blocking) { let counts_table_r = counts_table_r.clone(); let weights_table_r = weights_table_r.clone(); spawn(move || { while let Some((index, (count, weight))) = stats_update_r.recv() { let val = counts_table_r.get_and(&index, |vs| vs[0]); counts_table_w.update(index, val.unwrap_or(0) + count); let cur = weights_table_r.get_and(&index, |vs: &[Box<F64>]| vs[0].val) .unwrap_or(0.0); weights_table_w.update(index, Box::new(F64 { val: cur + weight })); { counts_table_w.refresh(); weights_table_w.refresh(); } } }); } // Monitor the distribution of strata if debug_mode { let counts_table_r = counts_table_r.clone(); let weights_table_r = weights_table_r.clone(); spawn(move || { loop { sleep(Duration::from_millis(5000)); let mut p: Vec<(i8, f64)> = weights_table_r.map_into(|a: &i8, b: &[Box<F64>]| (a.clone(), b[0].val)); p.sort_by(|a, b| (a.0).cmp(&b.0)); let mut c: Vec<(i8, i32)> = counts_table_r.map_into(|a, b| (a.clone(), b[0])); c.sort_by(|a, b| (a.0).cmp(&b.0)); let mut sump: f64 = p.iter().map(|t| t.1).sum(); if get_sign(sump) == 0 { sump = 1.0; } let ps: Vec<String> = p.into_iter() .map(|(idx, w)| (idx, 100.0 * w / sump)) .map(|(idx, w)| format!("({}, {:.2})", idx, w)) .collect(); debug!("strata weights distr, {}, {}", ps.join(", "), sump); let sumc: i32 = max(c.iter().map(|t| t.1).sum(), 1); let cs: Vec<String> = c.into_iter() .map(|(idx, c)| (idx, 100.0 * c as f32 / (sumc as f32))) .map(|(idx, c)| format!("({}, {:.2})", idx, c)) .collect(); debug!("strata counts distr, {}, {}", cs.join(", "), sumc); } }); } let assigners = Assigners::new( updated_examples_r, strata.clone(), stats_update_s.clone(), num_assigners, ); let samplers = Samplers::new( strata.clone(), sampled_examples.clone(), updated_examples_s.clone(), models.clone(), stats_update_s.clone(), weights_table_r.clone(), sampling_signal.clone(), num_samplers, ); assigners.run(); samplers.run(); StratifiedStorage { // num_examples: num_examples, // feature_size: feature_size, // num_examples_per_block: num_examples_per_block, // disk_buffer_filename: String::from(disk_buffer_filename), // strata: strata, // stats_update_s: stats_update_s, counts_table_r: counts_table_r, weights_table_r: weights_table_r, // num_assigners: num_assigners, // num_samplers: num_samplers, // updated_examples_r: updated_examples_r, updated_examples_s: updated_examples_s, // sampled_examples_s: sampled_examples, // sampling_signal: sampling_signal, // models: models, positive: positive, } } pub fn init_stratified_from_file( &self, filename: String, size: usize, batch_size: usize, feature_size: usize, range: Range<usize>, bins: Vec<Bins>, ) { let mut reader = SerialStorage::new( filename.clone(), size, feature_size, true, self.positive.clone(), None, range.clone(), ); let updated_examples_s = self.updated_examples_s.clone(); spawn(move || { let mut index = 0; while index < size { reader.read_raw(batch_size).into_iter().for_each(|data| { let features: Vec<TFeature> = data.feature.iter().enumerate() .map(|(idx, val)| { if range.start <= idx && idx < range.end { bins[idx - range.start].get_split_index(*val) } else { 0 } }).collect(); let mapped_data = LabeledData::new(features, data.label); updated_examples_s.send((mapped_data, (0.0, 0))); }); index += batch_size; } debug!("Raw data on disk has been loaded into the stratified storage, \ filename {}, capacity {}, feature size {}", filename, size, feature_size); }); } } fn sample_weights_table(weights_table_r: &WeightTableRead) -> Option<i8> { let p: Vec<(i8, f64)> = weights_table_r.map_into(|a, b| (a.clone(), b[0].val)); let sum_of_weights: f64 = p.iter().map(|t| t.1).sum(); if get_sign(sum_of_weights) == 0 { None } else { let mut frac = rand::random::<f64>() * sum_of_weights; let mut iter = p.iter(); let mut key_val = &(0, 0.0); while get_sign(frac) >= 0 { key_val = iter.next().expect("get_sample_from: input p is empty"); frac -= key_val.1; } Some(key_val.0) } } #[cfg(test)] mod tests { extern crate env_logger; use std::fs::remove_file; use commons::channel; use std::thread::spawn; use labeled_data::LabeledData; use commons::ExampleWithScore; use commons::Signal; use commons::performance_monitor::PerformanceMonitor; use super::StratifiedStorage; use ::TFeature; #[test] fn
() { let _ = env_logger::try_init(); let filename = "unittest-stratified3.bin"; let batch = 100000; let num_read = 1000000; let (sampled_examples_send, sampled_examples_recv) = channel::bounded(1000, "sampled-examples"); let (_, models_recv) = channel::bounded(10, "updated-models"); let (signal_s, signal_r) = channel::bounded(10, "sampling-signal"); signal_s.send(Signal::START); let stratified_storage = StratifiedStorage::new( batch * 10, 1, "1".to_string(), 10000, filename, 4, 4, sampled_examples_send, signal_r, models_recv, 10, false, ); let updated_examples_send = stratified_storage.updated_examples_s.clone(); let mut pm_load = PerformanceMonitor::new(); pm_load.start(); let loading = spawn(move || { for _ in 0..batch { for i in 1..11 { let t = get_example(vec![i as TFeature], i as f32); updated_examples_send.send(t.clone()); } } println!("Loading speed: {}", (batch * 10) as f32 / pm_load.get_duration()); }); let mut pm_sample = PerformanceMonitor::new(); pm_sample.start(); let mut average = 0.0; for _ in 0..num_read { let recv = sampled_examples_recv.recv().unwrap(); average += (((recv.0).0).feature[0] as f32) * (recv.1 as f32) / (num_read as f32); pm_sample.update(recv.1 as usize); } spawn(move || { println!("Sampling speed: {}", num_read as f32 / pm_sample.get_duration()); }); let answer = (1..11).map(|a| a as f32).map(|a| a * a).sum::<f32>() / ((1..11).sum::<i32>() as f32); loading.join().unwrap(); if (average - answer).abs() > 0.05 { spawn(move || { println!("Average: {}. Expect: {}.", average, answer); }).join().unwrap(); assert!(false); } remove_file(filename).unwrap(); } fn get_example(feature: Vec<TFeature>, weight: f32) -> ExampleWithScore { let label: i8 = 1; let example = LabeledData::new(feature, label); let score = -weight.ln(); (example, (score, 0)) } }
test_mean
identifier_name
mod.rs
mod strata; mod assigners; mod samplers; pub mod serial_storage; use std::cmp::max; use std::ops::Range; use std::sync::Arc; use std::sync::RwLock; use std::thread::spawn; use std::thread::sleep; use std::time::Duration; use evmap; use rand; use commons::bins::Bins; use commons::channel; use commons::channel::Receiver; use commons::channel::Sender; use commons::get_sign; use commons::ExampleWithScore; use commons::Model; use commons::Signal; use labeled_data::LabeledData; use super::Example; use super::TFeature; use self::assigners::Assigners; use self::samplers::Samplers; use self::serial_storage::SerialStorage; use self::strata::Strata; pub const SPEED_TEST: bool = false; pub struct F64 { pub val: f64 } impl PartialEq for F64 { fn eq(&self, other: &F64) -> bool { get_sign(self.val - other.val) == 0 } } impl Eq for F64 {} type WeightTableRead = evmap::ReadHandle<i8, Box<F64>>; pub struct StratifiedStorage { // num_examples: usize, // feature_size: usize, // num_examples_per_block: usize, // disk_buffer_filename: String, // strata: Arc<RwLock<Strata>>, // stats_update_s: Sender<(i8, (i32, f64))>, #[allow(dead_code)] counts_table_r: evmap::ReadHandle<i8, i32>, #[allow(dead_code)] weights_table_r: WeightTableRead, // num_assigners: usize, // num_samplers: usize, // updated_examples_r: Receiver<ExampleWithScore>, updated_examples_s: Sender<ExampleWithScore>, // sampled_examples_s: Sender<(ExampleWithScore, u32)>, // sampling_signal: Receiver<Signal>, // models: Receiver<Model>, positive: String, } impl StratifiedStorage { /// Create the stratified storage structure. /// /// * `num_examples`: the total number of examples in the training data set /// * `feature_size`: the number of features of the training examples /// * `num_examples_per_block`: the number of examples to write back to disk in batch (explained below) /// * `disk_buffer_filename`: the name of the binary file for saving the examples in strata on disk /// If such file does not exist, it will be created /// * `num_assigners`: the number of threads that run the `Assigner`s (explained below) /// * `num_samplers`: the number of threads that run the `Sampler`s (explained below) /// * `sampled_examples`: the channle that the stratified storage sends the sampled examples to /// the buffer loader /// * `sampling_singal`: the channle that the buffer loader sends sampling signals to /// start and stop the samplers as needed /// * `models`: the channel that the booster sends the latest models in /// /// Stratified storage organizes training examples according to their weights /// given current learning model. /// The examples are assigned to different strata so that the weight ratio of the examples /// within the same stratum does not exceed 2. /// Most examples in a stratum are stored on disk, while a small number of examples remains /// in memory to be writen to disk or just read from disk and ready to send out to the sampler. /// /// The overall structure of the stratified storage is as follow: /// /// ![](https://www.lucidchart.com/publicSegments/view/c87b7a50-5442-4a41-a601-3dfb49b16511/image.png) /// /// The `Assigner`s read examples with updated scores from the `Sampler` and write them back to /// the corresponding strata based on their new weights. The examples would be put into the /// `In Queue`s first till a proper number of examples are accumulated that belongs to the /// same strata, at that point they would be written into disk in batch. /// /// Meanwhile, a certain number of examples from each stratum are loaded into the memory /// from the disk and kept in `Out Queue`s. /// The `Sampler`s iteratively select a stratum with a probability that proportional to /// the sum of weights of all examples in that stratum, send its next sampled example to the memory /// buffer, and remove that example from strata. /// /// A `Shared Weight Table` maintains the sum of the weights of all examples in each stratum. /// The `Assigner`s increase the value in the `Shared Weight Table` when a new example is inserted into /// a stratum. /// The `Sampler`s use the weights in the `Shared Weight Table` to decide which stratum to read next and /// send its next sampled example to the memory buffer. After an example is processed, the `Sampler` also /// updates its weight, sends it to right stratum, and updates `Shared Weight Table` accordingly. pub fn new( num_examples: usize, feature_size: usize, positive: String, num_examples_per_block: usize, disk_buffer_filename: &str, num_assigners: usize, num_samplers: usize, sampled_examples: Sender<(ExampleWithScore, u32)>, sampling_signal: Receiver<Signal>, models: Receiver<Model>, channel_size: usize, debug_mode: bool, ) -> StratifiedStorage
pub fn init_stratified_from_file( &self, filename: String, size: usize, batch_size: usize, feature_size: usize, range: Range<usize>, bins: Vec<Bins>, ) { let mut reader = SerialStorage::new( filename.clone(), size, feature_size, true, self.positive.clone(), None, range.clone(), ); let updated_examples_s = self.updated_examples_s.clone(); spawn(move || { let mut index = 0; while index < size { reader.read_raw(batch_size).into_iter().for_each(|data| { let features: Vec<TFeature> = data.feature.iter().enumerate() .map(|(idx, val)| { if range.start <= idx && idx < range.end { bins[idx - range.start].get_split_index(*val) } else { 0 } }).collect(); let mapped_data = LabeledData::new(features, data.label); updated_examples_s.send((mapped_data, (0.0, 0))); }); index += batch_size; } debug!("Raw data on disk has been loaded into the stratified storage, \ filename {}, capacity {}, feature size {}", filename, size, feature_size); }); } } fn sample_weights_table(weights_table_r: &WeightTableRead) -> Option<i8> { let p: Vec<(i8, f64)> = weights_table_r.map_into(|a, b| (a.clone(), b[0].val)); let sum_of_weights: f64 = p.iter().map(|t| t.1).sum(); if get_sign(sum_of_weights) == 0 { None } else { let mut frac = rand::random::<f64>() * sum_of_weights; let mut iter = p.iter(); let mut key_val = &(0, 0.0); while get_sign(frac) >= 0 { key_val = iter.next().expect("get_sample_from: input p is empty"); frac -= key_val.1; } Some(key_val.0) } } #[cfg(test)] mod tests { extern crate env_logger; use std::fs::remove_file; use commons::channel; use std::thread::spawn; use labeled_data::LabeledData; use commons::ExampleWithScore; use commons::Signal; use commons::performance_monitor::PerformanceMonitor; use super::StratifiedStorage; use ::TFeature; #[test] fn test_mean() { let _ = env_logger::try_init(); let filename = "unittest-stratified3.bin"; let batch = 100000; let num_read = 1000000; let (sampled_examples_send, sampled_examples_recv) = channel::bounded(1000, "sampled-examples"); let (_, models_recv) = channel::bounded(10, "updated-models"); let (signal_s, signal_r) = channel::bounded(10, "sampling-signal"); signal_s.send(Signal::START); let stratified_storage = StratifiedStorage::new( batch * 10, 1, "1".to_string(), 10000, filename, 4, 4, sampled_examples_send, signal_r, models_recv, 10, false, ); let updated_examples_send = stratified_storage.updated_examples_s.clone(); let mut pm_load = PerformanceMonitor::new(); pm_load.start(); let loading = spawn(move || { for _ in 0..batch { for i in 1..11 { let t = get_example(vec![i as TFeature], i as f32); updated_examples_send.send(t.clone()); } } println!("Loading speed: {}", (batch * 10) as f32 / pm_load.get_duration()); }); let mut pm_sample = PerformanceMonitor::new(); pm_sample.start(); let mut average = 0.0; for _ in 0..num_read { let recv = sampled_examples_recv.recv().unwrap(); average += (((recv.0).0).feature[0] as f32) * (recv.1 as f32) / (num_read as f32); pm_sample.update(recv.1 as usize); } spawn(move || { println!("Sampling speed: {}", num_read as f32 / pm_sample.get_duration()); }); let answer = (1..11).map(|a| a as f32).map(|a| a * a).sum::<f32>() / ((1..11).sum::<i32>() as f32); loading.join().unwrap(); if (average - answer).abs() > 0.05 { spawn(move || { println!("Average: {}. Expect: {}.", average, answer); }).join().unwrap(); assert!(false); } remove_file(filename).unwrap(); } fn get_example(feature: Vec<TFeature>, weight: f32) -> ExampleWithScore { let label: i8 = 1; let example = LabeledData::new(feature, label); let score = -weight.ln(); (example, (score, 0)) } }
{ let strata = Strata::new(num_examples, feature_size, num_examples_per_block, disk_buffer_filename); let strata = Arc::new(RwLock::new(strata)); let (counts_table_r, mut counts_table_w) = evmap::new(); let (weights_table_r, mut weights_table_w) = evmap::new(); let (updated_examples_s, updated_examples_r) = channel::bounded(channel_size, "updated-examples"); // The messages in the stats channel are very small, so its capacity can be larger. let (stats_update_s, stats_update_r) = channel::bounded(5000000, "stats"); // Update shared weights table (non-blocking) { let counts_table_r = counts_table_r.clone(); let weights_table_r = weights_table_r.clone(); spawn(move || { while let Some((index, (count, weight))) = stats_update_r.recv() { let val = counts_table_r.get_and(&index, |vs| vs[0]); counts_table_w.update(index, val.unwrap_or(0) + count); let cur = weights_table_r.get_and(&index, |vs: &[Box<F64>]| vs[0].val) .unwrap_or(0.0); weights_table_w.update(index, Box::new(F64 { val: cur + weight })); { counts_table_w.refresh(); weights_table_w.refresh(); } } }); } // Monitor the distribution of strata if debug_mode { let counts_table_r = counts_table_r.clone(); let weights_table_r = weights_table_r.clone(); spawn(move || { loop { sleep(Duration::from_millis(5000)); let mut p: Vec<(i8, f64)> = weights_table_r.map_into(|a: &i8, b: &[Box<F64>]| (a.clone(), b[0].val)); p.sort_by(|a, b| (a.0).cmp(&b.0)); let mut c: Vec<(i8, i32)> = counts_table_r.map_into(|a, b| (a.clone(), b[0])); c.sort_by(|a, b| (a.0).cmp(&b.0)); let mut sump: f64 = p.iter().map(|t| t.1).sum(); if get_sign(sump) == 0 { sump = 1.0; } let ps: Vec<String> = p.into_iter() .map(|(idx, w)| (idx, 100.0 * w / sump)) .map(|(idx, w)| format!("({}, {:.2})", idx, w)) .collect(); debug!("strata weights distr, {}, {}", ps.join(", "), sump); let sumc: i32 = max(c.iter().map(|t| t.1).sum(), 1); let cs: Vec<String> = c.into_iter() .map(|(idx, c)| (idx, 100.0 * c as f32 / (sumc as f32))) .map(|(idx, c)| format!("({}, {:.2})", idx, c)) .collect(); debug!("strata counts distr, {}, {}", cs.join(", "), sumc); } }); } let assigners = Assigners::new( updated_examples_r, strata.clone(), stats_update_s.clone(), num_assigners, ); let samplers = Samplers::new( strata.clone(), sampled_examples.clone(), updated_examples_s.clone(), models.clone(), stats_update_s.clone(), weights_table_r.clone(), sampling_signal.clone(), num_samplers, ); assigners.run(); samplers.run(); StratifiedStorage { // num_examples: num_examples, // feature_size: feature_size, // num_examples_per_block: num_examples_per_block, // disk_buffer_filename: String::from(disk_buffer_filename), // strata: strata, // stats_update_s: stats_update_s, counts_table_r: counts_table_r, weights_table_r: weights_table_r, // num_assigners: num_assigners, // num_samplers: num_samplers, // updated_examples_r: updated_examples_r, updated_examples_s: updated_examples_s, // sampled_examples_s: sampled_examples, // sampling_signal: sampling_signal, // models: models, positive: positive, } }
identifier_body
mod.rs
mod strata; mod assigners; mod samplers; pub mod serial_storage; use std::cmp::max; use std::ops::Range; use std::sync::Arc; use std::sync::RwLock; use std::thread::spawn; use std::thread::sleep; use std::time::Duration; use evmap; use rand; use commons::bins::Bins; use commons::channel; use commons::channel::Receiver; use commons::channel::Sender; use commons::get_sign; use commons::ExampleWithScore; use commons::Model; use commons::Signal; use labeled_data::LabeledData; use super::Example; use super::TFeature; use self::assigners::Assigners; use self::samplers::Samplers; use self::serial_storage::SerialStorage; use self::strata::Strata; pub const SPEED_TEST: bool = false; pub struct F64 { pub val: f64 } impl PartialEq for F64 { fn eq(&self, other: &F64) -> bool { get_sign(self.val - other.val) == 0 } } impl Eq for F64 {} type WeightTableRead = evmap::ReadHandle<i8, Box<F64>>; pub struct StratifiedStorage { // num_examples: usize, // feature_size: usize, // num_examples_per_block: usize, // disk_buffer_filename: String, // strata: Arc<RwLock<Strata>>, // stats_update_s: Sender<(i8, (i32, f64))>, #[allow(dead_code)] counts_table_r: evmap::ReadHandle<i8, i32>, #[allow(dead_code)] weights_table_r: WeightTableRead, // num_assigners: usize, // num_samplers: usize, // updated_examples_r: Receiver<ExampleWithScore>, updated_examples_s: Sender<ExampleWithScore>, // sampled_examples_s: Sender<(ExampleWithScore, u32)>, // sampling_signal: Receiver<Signal>, // models: Receiver<Model>, positive: String, } impl StratifiedStorage { /// Create the stratified storage structure. /// /// * `num_examples`: the total number of examples in the training data set /// * `feature_size`: the number of features of the training examples /// * `num_examples_per_block`: the number of examples to write back to disk in batch (explained below) /// * `disk_buffer_filename`: the name of the binary file for saving the examples in strata on disk /// If such file does not exist, it will be created /// * `num_assigners`: the number of threads that run the `Assigner`s (explained below) /// * `num_samplers`: the number of threads that run the `Sampler`s (explained below) /// * `sampled_examples`: the channle that the stratified storage sends the sampled examples to /// the buffer loader /// * `sampling_singal`: the channle that the buffer loader sends sampling signals to /// start and stop the samplers as needed /// * `models`: the channel that the booster sends the latest models in /// /// Stratified storage organizes training examples according to their weights /// given current learning model. /// The examples are assigned to different strata so that the weight ratio of the examples /// within the same stratum does not exceed 2. /// Most examples in a stratum are stored on disk, while a small number of examples remains /// in memory to be writen to disk or just read from disk and ready to send out to the sampler. /// /// The overall structure of the stratified storage is as follow: /// /// ![](https://www.lucidchart.com/publicSegments/view/c87b7a50-5442-4a41-a601-3dfb49b16511/image.png) /// /// The `Assigner`s read examples with updated scores from the `Sampler` and write them back to /// the corresponding strata based on their new weights. The examples would be put into the /// `In Queue`s first till a proper number of examples are accumulated that belongs to the /// same strata, at that point they would be written into disk in batch. /// /// Meanwhile, a certain number of examples from each stratum are loaded into the memory /// from the disk and kept in `Out Queue`s. /// The `Sampler`s iteratively select a stratum with a probability that proportional to /// the sum of weights of all examples in that stratum, send its next sampled example to the memory /// buffer, and remove that example from strata. /// /// A `Shared Weight Table` maintains the sum of the weights of all examples in each stratum. /// The `Assigner`s increase the value in the `Shared Weight Table` when a new example is inserted into /// a stratum. /// The `Sampler`s use the weights in the `Shared Weight Table` to decide which stratum to read next and /// send its next sampled example to the memory buffer. After an example is processed, the `Sampler` also /// updates its weight, sends it to right stratum, and updates `Shared Weight Table` accordingly. pub fn new( num_examples: usize, feature_size: usize, positive: String, num_examples_per_block: usize, disk_buffer_filename: &str, num_assigners: usize, num_samplers: usize, sampled_examples: Sender<(ExampleWithScore, u32)>, sampling_signal: Receiver<Signal>, models: Receiver<Model>, channel_size: usize, debug_mode: bool, ) -> StratifiedStorage { let strata = Strata::new(num_examples, feature_size, num_examples_per_block, disk_buffer_filename); let strata = Arc::new(RwLock::new(strata)); let (counts_table_r, mut counts_table_w) = evmap::new(); let (weights_table_r, mut weights_table_w) = evmap::new(); let (updated_examples_s, updated_examples_r) = channel::bounded(channel_size, "updated-examples"); // The messages in the stats channel are very small, so its capacity can be larger. let (stats_update_s, stats_update_r) = channel::bounded(5000000, "stats"); // Update shared weights table (non-blocking) { let counts_table_r = counts_table_r.clone(); let weights_table_r = weights_table_r.clone(); spawn(move || { while let Some((index, (count, weight))) = stats_update_r.recv() { let val = counts_table_r.get_and(&index, |vs| vs[0]); counts_table_w.update(index, val.unwrap_or(0) + count); let cur = weights_table_r.get_and(&index, |vs: &[Box<F64>]| vs[0].val) .unwrap_or(0.0); weights_table_w.update(index, Box::new(F64 { val: cur + weight })); { counts_table_w.refresh(); weights_table_w.refresh(); } } }); } // Monitor the distribution of strata if debug_mode { let counts_table_r = counts_table_r.clone(); let weights_table_r = weights_table_r.clone(); spawn(move || { loop { sleep(Duration::from_millis(5000)); let mut p: Vec<(i8, f64)> = weights_table_r.map_into(|a: &i8, b: &[Box<F64>]| (a.clone(), b[0].val)); p.sort_by(|a, b| (a.0).cmp(&b.0)); let mut c: Vec<(i8, i32)> = counts_table_r.map_into(|a, b| (a.clone(), b[0])); c.sort_by(|a, b| (a.0).cmp(&b.0)); let mut sump: f64 = p.iter().map(|t| t.1).sum(); if get_sign(sump) == 0 { sump = 1.0; } let ps: Vec<String> = p.into_iter() .map(|(idx, w)| (idx, 100.0 * w / sump)) .map(|(idx, w)| format!("({}, {:.2})", idx, w)) .collect(); debug!("strata weights distr, {}, {}", ps.join(", "), sump); let sumc: i32 = max(c.iter().map(|t| t.1).sum(), 1); let cs: Vec<String> = c.into_iter() .map(|(idx, c)| (idx, 100.0 * c as f32 / (sumc as f32))) .map(|(idx, c)| format!("({}, {:.2})", idx, c)) .collect(); debug!("strata counts distr, {}, {}", cs.join(", "), sumc); } }); } let assigners = Assigners::new( updated_examples_r, strata.clone(), stats_update_s.clone(), num_assigners, ); let samplers = Samplers::new( strata.clone(), sampled_examples.clone(), updated_examples_s.clone(), models.clone(), stats_update_s.clone(), weights_table_r.clone(), sampling_signal.clone(), num_samplers, ); assigners.run(); samplers.run(); StratifiedStorage { // num_examples: num_examples, // feature_size: feature_size, // num_examples_per_block: num_examples_per_block, // disk_buffer_filename: String::from(disk_buffer_filename), // strata: strata, // stats_update_s: stats_update_s, counts_table_r: counts_table_r, weights_table_r: weights_table_r, // num_assigners: num_assigners, // num_samplers: num_samplers, // updated_examples_r: updated_examples_r, updated_examples_s: updated_examples_s, // sampled_examples_s: sampled_examples, // sampling_signal: sampling_signal, // models: models, positive: positive, } } pub fn init_stratified_from_file( &self, filename: String, size: usize, batch_size: usize, feature_size: usize, range: Range<usize>, bins: Vec<Bins>, ) { let mut reader = SerialStorage::new( filename.clone(), size, feature_size, true, self.positive.clone(), None, range.clone(), ); let updated_examples_s = self.updated_examples_s.clone(); spawn(move || { let mut index = 0; while index < size { reader.read_raw(batch_size).into_iter().for_each(|data| { let features: Vec<TFeature> = data.feature.iter().enumerate() .map(|(idx, val)| { if range.start <= idx && idx < range.end
else { 0 } }).collect(); let mapped_data = LabeledData::new(features, data.label); updated_examples_s.send((mapped_data, (0.0, 0))); }); index += batch_size; } debug!("Raw data on disk has been loaded into the stratified storage, \ filename {}, capacity {}, feature size {}", filename, size, feature_size); }); } } fn sample_weights_table(weights_table_r: &WeightTableRead) -> Option<i8> { let p: Vec<(i8, f64)> = weights_table_r.map_into(|a, b| (a.clone(), b[0].val)); let sum_of_weights: f64 = p.iter().map(|t| t.1).sum(); if get_sign(sum_of_weights) == 0 { None } else { let mut frac = rand::random::<f64>() * sum_of_weights; let mut iter = p.iter(); let mut key_val = &(0, 0.0); while get_sign(frac) >= 0 { key_val = iter.next().expect("get_sample_from: input p is empty"); frac -= key_val.1; } Some(key_val.0) } } #[cfg(test)] mod tests { extern crate env_logger; use std::fs::remove_file; use commons::channel; use std::thread::spawn; use labeled_data::LabeledData; use commons::ExampleWithScore; use commons::Signal; use commons::performance_monitor::PerformanceMonitor; use super::StratifiedStorage; use ::TFeature; #[test] fn test_mean() { let _ = env_logger::try_init(); let filename = "unittest-stratified3.bin"; let batch = 100000; let num_read = 1000000; let (sampled_examples_send, sampled_examples_recv) = channel::bounded(1000, "sampled-examples"); let (_, models_recv) = channel::bounded(10, "updated-models"); let (signal_s, signal_r) = channel::bounded(10, "sampling-signal"); signal_s.send(Signal::START); let stratified_storage = StratifiedStorage::new( batch * 10, 1, "1".to_string(), 10000, filename, 4, 4, sampled_examples_send, signal_r, models_recv, 10, false, ); let updated_examples_send = stratified_storage.updated_examples_s.clone(); let mut pm_load = PerformanceMonitor::new(); pm_load.start(); let loading = spawn(move || { for _ in 0..batch { for i in 1..11 { let t = get_example(vec![i as TFeature], i as f32); updated_examples_send.send(t.clone()); } } println!("Loading speed: {}", (batch * 10) as f32 / pm_load.get_duration()); }); let mut pm_sample = PerformanceMonitor::new(); pm_sample.start(); let mut average = 0.0; for _ in 0..num_read { let recv = sampled_examples_recv.recv().unwrap(); average += (((recv.0).0).feature[0] as f32) * (recv.1 as f32) / (num_read as f32); pm_sample.update(recv.1 as usize); } spawn(move || { println!("Sampling speed: {}", num_read as f32 / pm_sample.get_duration()); }); let answer = (1..11).map(|a| a as f32).map(|a| a * a).sum::<f32>() / ((1..11).sum::<i32>() as f32); loading.join().unwrap(); if (average - answer).abs() > 0.05 { spawn(move || { println!("Average: {}. Expect: {}.", average, answer); }).join().unwrap(); assert!(false); } remove_file(filename).unwrap(); } fn get_example(feature: Vec<TFeature>, weight: f32) -> ExampleWithScore { let label: i8 = 1; let example = LabeledData::new(feature, label); let score = -weight.ln(); (example, (score, 0)) } }
{ bins[idx - range.start].get_split_index(*val) }
conditional_block
my.js
function UrlSearch() { var name,value; var str=location.href; //取得整个地址栏 var num=str.indexOf("?") str=str.substr(num+1); //取得所有参数 stringvar.substr(start [, length ] var arr=str.split("&"); //各个参数放到数组里 for(var i=0;i < arr.length;i++){ num=arr[i].indexOf("="); if(num>0){ name=arr[i].substring(0,num); value=arr[i].substr(num+1); this[name]=value; } } } var Request=new UrlSearch(); //实例化 var base_url = "http://dltest.sparkingfuture.com/basic/web/index.php?gid=" +Request.gid ; var turnplate={ restaraunts:[], //大转盘奖品名称 colors:[], //大转盘奖品区块对应背景颜色 srcs:[], //大转盘奖品区块对应的缩略图src outsideRadius:192, //大转盘外圆的半径 textRadius:155, //大转盘奖品位置距离圆心的距离 insideRadius:68, //大转盘内圆的半径 startAngle:0, //开始角度 bRotate:false //false:停止;ture:旋转 }; //动态添加大转盘的奖品与奖品区域背景颜色 turnplate.restaraunts = ["2钻石A", "20金币B", "5钻石C", "实物大奖D "]; turnplate.colors = ["#FFF4D6", "#FFFFFF","#FFF4D6", "#FFFFFF"]; turnplate.srcs = ['static/activity/images/chest-icon-zuan.png','static/activity/images/coin.png','static/activity/images/chest-icon-zuan.png']; var rotateTimeOut = function (){ //超时函数 $('#wheelcanvas').rotate({ angle:0, animateTo:2160, //这里是设置请求超时后返回的角度,所以应该还是回到最原始的位置,2160是因为我要让它转6圈,就是360*6得来的 duration:8000, callback:function (){ alert('网络超时,请检查您的网络设置!'); } }); }; //旋转转盘 item:奖品位置; txt:提示语; function rotateFn(item, txt){ //alert(111); var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length*2)); if(angles<270){ angles = 270 - angles; }else{ angles = 360 - angles + 270; } $('#wheelcanvas').stopRotate(); $('#wheelcanvas').rotate({ angle: 0, //angle是图片上各奖项对应的角度 animateTo:angles+1800, duration: 2000, callback:function (){ // alert(txt); turnplate.bRotate
.bRotate = !turnplate.bRotate; $.ajax({ type : 'get', url : base_url+ '&r=activity/lucky-draw', success : function(res){ var res = res; var item = res.data[0]; var gift = res.data[1]; rotateFn(item, gift); $('.num').html(gift); var str = '<li> 恭喜 <span>123456</span> 获得了 <span class="present">'+ gift +'</span></li>'; var timer; if( gift === 'A' ){ $('.gift img').attr('src','static/activity/images/chest-icon-zuan.png'); // $('.tip').show(); //抽奖完毕后显示奖品 setTimeout(function(){ $('.stop').hide(); $('.tip').show(); $('#tab1').append(str); },2000); }else if( gift === 'B' ){ $('.gift img').attr('src','static/activity/images/coin.png'); // $('.tip').show(); //抽奖完毕后显示奖品 setTimeout(function(){ $('.stop').hide(); $('.tip').show(); $('#tab1').append(str); },2000); }else if( gift === 'C' ){ $('.gift img').attr('src','static/activity/images/coin.png'); // $('.tip').show(); setTimeout(function(){ $('.stop').hide(); $('.tip').show(); $('#tab1').append(str); },2000); }else if( gift === 'D' ){ //获得实物大奖则显示输入框 // $('.big').show(); setTimeout(function(){ $('.stop').hide(); $('.big').show(); $('#tab1').append(str); },2000); } } }); // } // } // }); }); //页面所有元素加载完毕后执行drawRouletteWheel()方法对转盘进行渲染 window.onload=function(){ drawRouletteWheel(); }; function drawRouletteWheel() { var canvas = document.getElementById("wheelcanvas"); if (canvas.getContext) { //根据奖品个数计算圆周角度 var arc = Math.PI / (turnplate.restaraunts.length/2); var ctx = canvas.getContext("2d"); //在给定矩形内清空一个矩形 ctx.clearRect(0,0,422,422); //strokeStyle 属性设置或返回用于笔触的颜色、渐变或模式 ctx.strokeStyle = "#FFBE04"; //font 属性设置或返回画布上文本内容的当前字体属性 ctx.font = '20px Microsoft YaHei'; for(var i = 0; i < turnplate.restaraunts.length; i++) { var angle = turnplate.startAngle + i * arc; ctx.fillStyle = turnplate.colors[i]; ctx.beginPath(); //arc(x,y,r,起始角,结束角,绘制方向) 方法创建弧/曲线(用于创建圆或部分圆) ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false); ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true); ctx.stroke(); ctx.fill(); //锁画布(为了保存之前的画布状态) ctx.save(); //----绘制奖品开始---- ctx.fillStyle = "#E5302F"; var text = turnplate.restaraunts[i]; var line_height = 17; //translate方法重新映射画布上的 (0,0) 位置 ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius); //rotate方法旋转当前的绘图 ctx.rotate(angle + arc / 2 + Math.PI / 2); /** 下面代码根据奖品类型、奖品名称长度渲染不同效果,如字体、颜色、图片效果。(具体根据实际情况改变) **/ // if(text.indexOf("M")>0){ //流量包 // var texts = text.split("M"); // for(var j = 0; j<texts.length; j++){ // ctx.font = j == 0?'bold 20px Microsoft YaHei':'16px Microsoft YaHei'; // if(j == 0){ // ctx.fillText(texts[j]+"M", -ctx.measureText(texts[j]+"M").width / 2, j * line_height); // }else{ // ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height); // } // } // }else if(text.indexOf("M") == -1 && text.length>6){ //奖品名称长度超过一定范围 // text = text.substring(0,6)+"||"+text.substring(6); // var texts = text.split("||"); // for(var j = 0; j<texts.length; j++){ // ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height); // } // }else{ //在画布上绘制填色的文本。文本的默认颜色是黑色 //measureText()方法返回包含一个对象,该对象包含以像素计的指定字体宽度 ctx.fillText(text, -ctx.measureText(text).width / 2, 0); // } var imgs = document.getElementById("imgs").getElementsByTagName('img'); var img = imgs[i]; imgs.onload=function(){ ctx.drawImage(img,-15,10); }; //添加对应图标 if(text.indexOf("金币")>0){ var img= document.getElementById("gold-img"); img.onload=function(){ ctx.drawImage(img,-15,10); }; ctx.drawImage(img,-15,10); }else if(text.indexOf("谢谢参与")>=0){ var img= document.getElementById("sorry-img"); img.onload=function(){ ctx.drawImage(img,-15,10); }; ctx.drawImage(img,-15,10); }else if(text.indexOf("钻石")>=0){ var img= document.getElementById("diamond-img"); img.onload=function(){ ctx.drawImage(img,-15,10); }; ctx.drawImage(img,-15,10); } //把当前画布返回(调整)到上一个save()状态之前 ctx.restore(); //----绘制奖品结束---- } } } // 中奖区的高度和大转盘一致 var h = $('.turnplate').height(); console.log(h); $('.txt').height(h); // 关闭中奖提示框 $('.off').click(function(){ $('.box').hide(); }); // 选项卡切换 $("#content ul").hide(); // Initially hide all content $("#tabs li:first").attr("id","current"); // Activate first tab $("#content ul:first").fadeIn(); // Show first tab content $('#tabs a').click(function(e) { e.preventDefault(); $("#content ul").hide(); //Hide all content $("#tabs li").attr("id",""); //Reset id's $(this).parent().attr("id","current"); // Activate this $('#' + $(this).attr('title')).fadeIn(); // Show content for current tab }); $('.five').click(function(){ // $('.promot .num').html('五'); // $('.promot .count').html('10'); $('.fivebox').show(); $('.no1').click(function(){ $('.fivebox').hide(); }); $('.yes1').click(function(){ $('.fivebox').hide(); $('.stop').show(); if(turnplate.bRotate)return; turnplate.bRotate = !turnplate.bRotate; $.ajax({ type : 'post', url : base_url+ '&r=activity/gold-draw', data : { user_id : 12345 , times : 5 }, success : function(res){ var res = res; console.log(res); //五连抽 var arr = []; var i = 0 ; var item = res.data[i][0]; //奖品的id var gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); var timer = setInterval(function(){ i ++ ; item = res.data[i][0]; //奖品的id gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); if(i>=4){ clearInterval(timer) ; console.log(arr); } },2000); setTimeout(function(){ $('.stop').hide(); var five_gift = arr[0] + ' ' + arr[1] + ' ' + arr[2] + ' ' + arr[3] + ' ' + arr[4]; $('.num').html(five_gift); var str = '<li> 恭喜 <span>123456</span> 获得了 <span class="present">'+ five_gift +'</span></li>'; $('.gifts').show(); $('#tab1').append(str); },10000) } }); }); }); $('.ten').click(function(){ // $('.promot .num').html('十'); // $('.promot .count').html('30'); $('.tenbox').show(); $('.no2').click(function(){ $('.tenbox').hide(); }); $('.yes2').click(function() { $('.tenbox').hide(); $('.stop').show(); if (turnplate.bRotate)return; turnplate.bRotate = !turnplate.bRotate; $.ajax({ type : 'post', url : base_url+ '&r=activity/gold-draw', data : { user_id : 12345 , times : 10 }, success : function(res){ var res = res; console.log(res); //十连抽 var arr = []; var i = 0 ; var item = res.data[i][0]; //奖品的id var gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); var timer = setInterval(function(){ i ++ ; item = res.data[i][0]; //奖品的id gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); if(i>=9){ clearInterval(timer) ; console.log(arr); } },2000); setTimeout(function(){ $('.stop').hide(); var ten_gift = arr[0] + ' ' + arr[1] + ' ' + arr[2] + ' ' + arr[3] + ' ' + arr[4] + ' ' + arr[5] + ' ' + arr[6] + ' ' + arr[7] + ' ' + arr[8] + ' ' + arr[9]; $('.num').html(ten_gift); var str = '<li> 恭喜 <span>123456</span> 获得了 <span class="present">'+ ten_gift +'</span></li>'; $('.gifts').show(); $('#tab1').append(str); },20000) } }); }); });
= !turnplate.bRotate; } }); }; $('.pointer').click(function (){ // $.ajax({ // type : 'post', // url : base_url + '&r=activity/free-draw', // data : { user_id : 12345 }, // success : function(res){ // var res = res; // if( res.ret_code == 2070 ){ // alert('您今日免费抽奖次数已用完,如需再次抽奖,需消耗10钻石/次,请确认是否继续?'); // //后续操作 // }else if( res.ret_code == 0 ){ //第一次免费抽奖 $('.stop').show(); if(turnplate.bRotate) return; turnplate
identifier_body
my.js
function
() { var name,value; var str=location.href; //取得整个地址栏 var num=str.indexOf("?") str=str.substr(num+1); //取得所有参数 stringvar.substr(start [, length ] var arr=str.split("&"); //各个参数放到数组里 for(var i=0;i < arr.length;i++){ num=arr[i].indexOf("="); if(num>0){ name=arr[i].substring(0,num); value=arr[i].substr(num+1); this[name]=value; } } } var Request=new UrlSearch(); //实例化 var base_url = "http://dltest.sparkingfuture.com/basic/web/index.php?gid=" +Request.gid ; var turnplate={ restaraunts:[], //大转盘奖品名称 colors:[], //大转盘奖品区块对应背景颜色 srcs:[], //大转盘奖品区块对应的缩略图src outsideRadius:192, //大转盘外圆的半径 textRadius:155, //大转盘奖品位置距离圆心的距离 insideRadius:68, //大转盘内圆的半径 startAngle:0, //开始角度 bRotate:false //false:停止;ture:旋转 }; //动态添加大转盘的奖品与奖品区域背景颜色 turnplate.restaraunts = ["2钻石A", "20金币B", "5钻石C", "实物大奖D "]; turnplate.colors = ["#FFF4D6", "#FFFFFF","#FFF4D6", "#FFFFFF"]; turnplate.srcs = ['static/activity/images/chest-icon-zuan.png','static/activity/images/coin.png','static/activity/images/chest-icon-zuan.png']; var rotateTimeOut = function (){ //超时函数 $('#wheelcanvas').rotate({ angle:0, animateTo:2160, //这里是设置请求超时后返回的角度,所以应该还是回到最原始的位置,2160是因为我要让它转6圈,就是360*6得来的 duration:8000, callback:function (){ alert('网络超时,请检查您的网络设置!'); } }); }; //旋转转盘 item:奖品位置; txt:提示语; function rotateFn(item, txt){ //alert(111); var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length*2)); if(angles<270){ angles = 270 - angles; }else{ angles = 360 - angles + 270; } $('#wheelcanvas').stopRotate(); $('#wheelcanvas').rotate({ angle: 0, //angle是图片上各奖项对应的角度 animateTo:angles+1800, duration: 2000, callback:function (){ // alert(txt); turnplate.bRotate = !turnplate.bRotate; } }); }; $('.pointer').click(function (){ // $.ajax({ // type : 'post', // url : base_url + '&r=activity/free-draw', // data : { user_id : 12345 }, // success : function(res){ // var res = res; // if( res.ret_code == 2070 ){ // alert('您今日免费抽奖次数已用完,如需再次抽奖,需消耗10钻石/次,请确认是否继续?'); // //后续操作 // }else if( res.ret_code == 0 ){ //第一次免费抽奖 $('.stop').show(); if(turnplate.bRotate) return; turnplate.bRotate = !turnplate.bRotate; $.ajax({ type : 'get', url : base_url+ '&r=activity/lucky-draw', success : function(res){ var res = res; var item = res.data[0]; var gift = res.data[1]; rotateFn(item, gift); $('.num').html(gift); var str = '<li> 恭喜 <span>123456</span> 获得了 <span class="present">'+ gift +'</span></li>'; var timer; if( gift === 'A' ){ $('.gift img').attr('src','static/activity/images/chest-icon-zuan.png'); // $('.tip').show(); //抽奖完毕后显示奖品 setTimeout(function(){ $('.stop').hide(); $('.tip').show(); $('#tab1').append(str); },2000); }else if( gift === 'B' ){ $('.gift img').attr('src','static/activity/images/coin.png'); // $('.tip').show(); //抽奖完毕后显示奖品 setTimeout(function(){ $('.stop').hide(); $('.tip').show(); $('#tab1').append(str); },2000); }else if( gift === 'C' ){ $('.gift img').attr('src','static/activity/images/coin.png'); // $('.tip').show(); setTimeout(function(){ $('.stop').hide(); $('.tip').show(); $('#tab1').append(str); },2000); }else if( gift === 'D' ){ //获得实物大奖则显示输入框 // $('.big').show(); setTimeout(function(){ $('.stop').hide(); $('.big').show(); $('#tab1').append(str); },2000); } } }); // } // } // }); }); //页面所有元素加载完毕后执行drawRouletteWheel()方法对转盘进行渲染 window.onload=function(){ drawRouletteWheel(); }; function drawRouletteWheel() { var canvas = document.getElementById("wheelcanvas"); if (canvas.getContext) { //根据奖品个数计算圆周角度 var arc = Math.PI / (turnplate.restaraunts.length/2); var ctx = canvas.getContext("2d"); //在给定矩形内清空一个矩形 ctx.clearRect(0,0,422,422); //strokeStyle 属性设置或返回用于笔触的颜色、渐变或模式 ctx.strokeStyle = "#FFBE04"; //font 属性设置或返回画布上文本内容的当前字体属性 ctx.font = '20px Microsoft YaHei'; for(var i = 0; i < turnplate.restaraunts.length; i++) { var angle = turnplate.startAngle + i * arc; ctx.fillStyle = turnplate.colors[i]; ctx.beginPath(); //arc(x,y,r,起始角,结束角,绘制方向) 方法创建弧/曲线(用于创建圆或部分圆) ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false); ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true); ctx.stroke(); ctx.fill(); //锁画布(为了保存之前的画布状态) ctx.save(); //----绘制奖品开始---- ctx.fillStyle = "#E5302F"; var text = turnplate.restaraunts[i]; var line_height = 17; //translate方法重新映射画布上的 (0,0) 位置 ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius); //rotate方法旋转当前的绘图 ctx.rotate(angle + arc / 2 + Math.PI / 2); /** 下面代码根据奖品类型、奖品名称长度渲染不同效果,如字体、颜色、图片效果。(具体根据实际情况改变) **/ // if(text.indexOf("M")>0){ //流量包 // var texts = text.split("M"); // for(var j = 0; j<texts.length; j++){ // ctx.font = j == 0?'bold 20px Microsoft YaHei':'16px Microsoft YaHei'; // if(j == 0){ // ctx.fillText(texts[j]+"M", -ctx.measureText(texts[j]+"M").width / 2, j * line_height); // }else{ // ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height); // } // } // }else if(text.indexOf("M") == -1 && text.length>6){ //奖品名称长度超过一定范围 // text = text.substring(0,6)+"||"+text.substring(6); // var texts = text.split("||"); // for(var j = 0; j<texts.length; j++){ // ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height); // } // }else{ //在画布上绘制填色的文本。文本的默认颜色是黑色 //measureText()方法返回包含一个对象,该对象包含以像素计的指定字体宽度 ctx.fillText(text, -ctx.measureText(text).width / 2, 0); // } var imgs = document.getElementById("imgs").getElementsByTagName('img'); var img = imgs[i]; imgs.onload=function(){ ctx.drawImage(img,-15,10); }; //添加对应图标 if(text.indexOf("金币")>0){ var img= document.getElementById("gold-img"); img.onload=function(){ ctx.drawImage(img,-15,10); }; ctx.drawImage(img,-15,10); }else if(text.indexOf("谢谢参与")>=0){ var img= document.getElementById("sorry-img"); img.onload=function(){ ctx.drawImage(img,-15,10); }; ctx.drawImage(img,-15,10); }else if(text.indexOf("钻石")>=0){ var img= document.getElementById("diamond-img"); img.onload=function(){ ctx.drawImage(img,-15,10); }; ctx.drawImage(img,-15,10); } //把当前画布返回(调整)到上一个save()状态之前 ctx.restore(); //----绘制奖品结束---- } } } // 中奖区的高度和大转盘一致 var h = $('.turnplate').height(); console.log(h); $('.txt').height(h); // 关闭中奖提示框 $('.off').click(function(){ $('.box').hide(); }); // 选项卡切换 $("#content ul").hide(); // Initially hide all content $("#tabs li:first").attr("id","current"); // Activate first tab $("#content ul:first").fadeIn(); // Show first tab content $('#tabs a').click(function(e) { e.preventDefault(); $("#content ul").hide(); //Hide all content $("#tabs li").attr("id",""); //Reset id's $(this).parent().attr("id","current"); // Activate this $('#' + $(this).attr('title')).fadeIn(); // Show content for current tab }); $('.five').click(function(){ // $('.promot .num').html('五'); // $('.promot .count').html('10'); $('.fivebox').show(); $('.no1').click(function(){ $('.fivebox').hide(); }); $('.yes1').click(function(){ $('.fivebox').hide(); $('.stop').show(); if(turnplate.bRotate)return; turnplate.bRotate = !turnplate.bRotate; $.ajax({ type : 'post', url : base_url+ '&r=activity/gold-draw', data : { user_id : 12345 , times : 5 }, success : function(res){ var res = res; console.log(res); //五连抽 var arr = []; var i = 0 ; var item = res.data[i][0]; //奖品的id var gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); var timer = setInterval(function(){ i ++ ; item = res.data[i][0]; //奖品的id gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); if(i>=4){ clearInterval(timer) ; console.log(arr); } },2000); setTimeout(function(){ $('.stop').hide(); var five_gift = arr[0] + ' ' + arr[1] + ' ' + arr[2] + ' ' + arr[3] + ' ' + arr[4]; $('.num').html(five_gift); var str = '<li> 恭喜 <span>123456</span> 获得了 <span class="present">'+ five_gift +'</span></li>'; $('.gifts').show(); $('#tab1').append(str); },10000) } }); }); }); $('.ten').click(function(){ // $('.promot .num').html('十'); // $('.promot .count').html('30'); $('.tenbox').show(); $('.no2').click(function(){ $('.tenbox').hide(); }); $('.yes2').click(function() { $('.tenbox').hide(); $('.stop').show(); if (turnplate.bRotate)return; turnplate.bRotate = !turnplate.bRotate; $.ajax({ type : 'post', url : base_url+ '&r=activity/gold-draw', data : { user_id : 12345 , times : 10 }, success : function(res){ var res = res; console.log(res); //十连抽 var arr = []; var i = 0 ; var item = res.data[i][0]; //奖品的id var gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); var timer = setInterval(function(){ i ++ ; item = res.data[i][0]; //奖品的id gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); if(i>=9){ clearInterval(timer) ; console.log(arr); } },2000); setTimeout(function(){ $('.stop').hide(); var ten_gift = arr[0] + ' ' + arr[1] + ' ' + arr[2] + ' ' + arr[3] + ' ' + arr[4] + ' ' + arr[5] + ' ' + arr[6] + ' ' + arr[7] + ' ' + arr[8] + ' ' + arr[9]; $('.num').html(ten_gift); var str = '<li> 恭喜 <span>123456</span> 获得了 <span class="present">'+ ten_gift +'</span></li>'; $('.gifts').show(); $('#tab1').append(str); },20000) } }); }); });
UrlSearch
identifier_name
my.js
function UrlSearch() { var name,value; var str=location.href; //取得整个地址栏 var num=str.indexOf("?") str=str.substr(num+1); //取得所有参数 stringvar.substr(start [, length ] var arr=str.split("&"); //各个参数放到数组里 for(var i=0;i < arr.length;i++){ num=arr[i].indexOf("="); if(num>0){ name=arr[i].substring(0,num); value=arr[i].substr(num+1); this[name]=value; } } } var Request=new UrlSearch(); //实例化 var base_url = "http://dltest.sparkingfuture.com/basic/web/index.php?gid=" +Request.gid ; var turnplate={ restaraunts:[], //大转盘奖品名称 colors:[], //大转盘奖品区块对应背景颜色 srcs:[], //大转盘奖品区块对应的缩略图src outsideRadius:192, //大转盘外圆的半径 textRadius:155, //大转盘奖品位置距离圆心的距离 insideRadius:68, //大转盘内圆的半径 startAngle:0, //开始角度 bRotate:false //false:停止;ture:旋转 }; //动态添加大转盘的奖品与奖品区域背景颜色 turnplate.restaraunts = ["2钻石A", "20金币B", "5钻石C", "实物大奖D "]; turnplate.colors = ["#FFF4D6", "#FFFFFF","#FFF4D6", "#FFFFFF"]; turnplate.srcs = ['static/activity/images/chest-icon-zuan.png','static/activity/images/coin.png','static/activity/images/chest-icon-zuan.png']; var rotateTimeOut = function (){ //超时函数 $('#wheelcanvas').rotate({ angle:0, animateTo:2160, //这里是设置请求超时后返回的角度,所以应该还是回到最原始的位置,2160是因为我要让它转6圈,就是360*6得来的 duration:8000, callback:function (){ alert('网络超时,请检查您的网络设置!'); } }); }; //旋转转盘 item:奖品位置; txt:提示语; function rotateFn(item, txt){ //alert(111); var angles = item * (360 / turnplate.restaraunts.length) - (360 / (turnplate.restaraunts.length*2)); if(angles<270){ angles = 270 - angles; }else{ angles = 360 - angles + 270; } $('#wheelcanvas').stopRotate(); $('#wheelcanvas').rotate({ angle: 0, //angle是图片上各奖项对应的角度 animateTo:angles+1800, duration: 2000, callback:function (){ // alert(txt); turnplate.bRotate = !turnplate.bRotate; } }); }; $('.pointer').click(function (){ // $.ajax({ // type : 'post', // url : base_url + '&r=activity/free-draw', // data : { user_id : 12345 }, // success : function(res){ // var res = res; // if( res.ret_code == 2070 ){ // alert('您今日免费抽奖次数已用完,如需再次抽奖,需消耗10钻石/次,请确认是否继续?'); // //后续操作 // }else if( res.ret_code == 0 ){ //第一次免费抽奖 $('.stop').show(); if(turnplate.bRotate) return; turnplate.bRotate = !turnplate.bRotate; $.ajax({ type : 'get', url : base_url+ '&r=activity/lucky-draw', success : function(res){ var res = res; var item = res.data[0]; var gift = res.data[1]; rotateFn(item, gift); $('.num').html(gift); var str = '<li> 恭喜 <span>123456</span> 获得了 <span class="present">'+ gift +'</span></li>'; var timer; if( gift === 'A' ){ $('.gift img').attr('src','static/activity/images/chest-icon-zuan.png'); // $('.tip').show(); //抽奖完毕后显示奖品 setTimeout(function(){ $('.stop').hide(); $('.tip').show(); $('#tab1').append(str); },2000); }else if( gift === 'B' ){ $('.gift img').attr('src','static/activity/images/coin.png'); // $('.tip').show(); //抽奖完毕后显示奖品 setTimeout(function(){ $('.stop').hide(); $('.tip').show(); $('#tab1').append(str); },2000); }else if( gift === 'C' ){ $('.gift img').attr('src','static/activity/images/coin.png'); // $('.tip').show(); setTimeout(function(){ $('.stop').hide(); $('.tip').show(); $('#tab1').append(str); },2000); }else if( gift === 'D' ){ //获得实物大奖则显示输入框 // $('.big').show(); setTimeout(function(){ $('.stop').hide(); $('.big').show(); $('#tab1').append(str); },2000); } } }); // } // } // }); }); //页面所有元素加载完毕后执行drawRouletteWheel()方法对转盘进行渲染 window.onload=function(){ drawRouletteWheel(); }; function drawRouletteWheel() { var canvas = document.getElementById("wheelcanvas"); if (canvas.getContext) { //根据奖品个数计算圆周角度 var arc = Math.PI / (turnplate.restaraunts.length/2); var ctx = canvas.getContext("2d"); //在给定矩形内清空一个矩形 ctx.clearRect(0,0,422,422); //strokeStyle 属性设置或返回用于笔触的颜色、渐变或模式 ctx.strokeStyle = "#FFBE04"; //font 属性设置或返回画布上文本内容的当前字体属性 ctx.font = '20px Microsoft YaHei'; for(var i = 0; i < turnplate.restaraunts.length; i++) { var angle = turnplate.startAngle + i * arc; ctx.fillStyle = turnplate.colors[i]; ctx.beginPath(); //arc(x,y,r,起始角,结束角,绘制方向) 方法创建弧/曲线(用于创建圆或部分圆) ctx.arc(211, 211, turnplate.outsideRadius, angle, angle + arc, false); ctx.arc(211, 211, turnplate.insideRadius, angle + arc, angle, true); ctx.stroke(); ctx.fill(); //锁画布(为了保存之前的画布状态) ctx.save(); //----绘制奖品开始---- ctx.fillStyle = "#E5302F"; var text = turnplate.restaraunts[i]; var line_height = 17; //translate方法重新映射画布上的 (0,0) 位置 ctx.translate(211 + Math.cos(angle + arc / 2) * turnplate.textRadius, 211 + Math.sin(angle + arc / 2) * turnplate.textRadius); //rotate方法旋转当前的绘图 ctx.rotate(angle + arc / 2 + Math.PI / 2); /** 下面代码根据奖品类型、奖品名称长度渲染不同效果,如字体、颜色、图片效果。(具体根据实际情况改变) **/ // if(text.indexOf("M")>0){ //流量包 // var texts = text.split("M"); // for(var j = 0; j<texts.length; j++){ // ctx.font = j == 0?'bold 20px Microsoft YaHei':'16px Microsoft YaHei'; // if(j == 0){ // ctx.fillText(texts[j]+"M", -ctx.measureText(texts[j]+"M").width / 2, j * line_height); // }else{ // ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height); // } // } // }else if(text.indexOf("M") == -1 && text.length>6){ //奖品名称长度超过一定范围 // text = text.substring(0,6)+"||"+text.substring(6); // var texts = text.split("||"); // for(var j = 0; j<texts.length; j++){ // ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, j * line_height); // } // }else{ //在画布上绘制填色的文本。文本的默认颜色是黑色 //measureText()方法返回包含一个对象,该对象包含以像素计的指定字体宽度 ctx.fillText(text, -ctx.measureText(text).width / 2, 0); // } var imgs = document.getElementById("imgs").getElementsByTagName('img'); var img = imgs[i]; imgs.onload=function(){ ctx.drawImage(img,-15,10); }; //添加对应图标 if(text.indexOf("金币")>0){ var img= document.getElementById("gold-img"); img.onload=function(){ ctx.drawImage(img,-15,10); }; ctx.drawImage(img,-15,10); }else if(text.indexOf("谢谢参与")>=0){ var img= document.getElementById("sorry-img"); img.onload=function(){ ctx.drawImage(img,-15,10); }; ctx.drawImage(img,-15,10); }else if(text.indexOf("钻石")>=0){ var img= document.getElementById("diamond-img"); img.onload=function(){ ctx.drawImage(img,-15,10); }; ctx.drawImage(img,-15,10); } //把当前画布返回(调整)到上一个save()状态之前 ctx.restore(); //----绘制奖品结束---- } }
// 中奖区的高度和大转盘一致 var h = $('.turnplate').height(); console.log(h); $('.txt').height(h); // 关闭中奖提示框 $('.off').click(function(){ $('.box').hide(); }); // 选项卡切换 $("#content ul").hide(); // Initially hide all content $("#tabs li:first").attr("id","current"); // Activate first tab $("#content ul:first").fadeIn(); // Show first tab content $('#tabs a').click(function(e) { e.preventDefault(); $("#content ul").hide(); //Hide all content $("#tabs li").attr("id",""); //Reset id's $(this).parent().attr("id","current"); // Activate this $('#' + $(this).attr('title')).fadeIn(); // Show content for current tab }); $('.five').click(function(){ // $('.promot .num').html('五'); // $('.promot .count').html('10'); $('.fivebox').show(); $('.no1').click(function(){ $('.fivebox').hide(); }); $('.yes1').click(function(){ $('.fivebox').hide(); $('.stop').show(); if(turnplate.bRotate)return; turnplate.bRotate = !turnplate.bRotate; $.ajax({ type : 'post', url : base_url+ '&r=activity/gold-draw', data : { user_id : 12345 , times : 5 }, success : function(res){ var res = res; console.log(res); //五连抽 var arr = []; var i = 0 ; var item = res.data[i][0]; //奖品的id var gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); var timer = setInterval(function(){ i ++ ; item = res.data[i][0]; //奖品的id gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); if(i>=4){ clearInterval(timer) ; console.log(arr); } },2000); setTimeout(function(){ $('.stop').hide(); var five_gift = arr[0] + ' ' + arr[1] + ' ' + arr[2] + ' ' + arr[3] + ' ' + arr[4]; $('.num').html(five_gift); var str = '<li> 恭喜 <span>123456</span> 获得了 <span class="present">'+ five_gift +'</span></li>'; $('.gifts').show(); $('#tab1').append(str); },10000) } }); }); }); $('.ten').click(function(){ // $('.promot .num').html('十'); // $('.promot .count').html('30'); $('.tenbox').show(); $('.no2').click(function(){ $('.tenbox').hide(); }); $('.yes2').click(function() { $('.tenbox').hide(); $('.stop').show(); if (turnplate.bRotate)return; turnplate.bRotate = !turnplate.bRotate; $.ajax({ type : 'post', url : base_url+ '&r=activity/gold-draw', data : { user_id : 12345 , times : 10 }, success : function(res){ var res = res; console.log(res); //十连抽 var arr = []; var i = 0 ; var item = res.data[i][0]; //奖品的id var gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); var timer = setInterval(function(){ i ++ ; item = res.data[i][0]; //奖品的id gift = res.data[i][1]; //奖品的名称 arr.push(gift); rotateFn(item, turnplate.restaraunts[item-1]); if(i>=9){ clearInterval(timer) ; console.log(arr); } },2000); setTimeout(function(){ $('.stop').hide(); var ten_gift = arr[0] + ' ' + arr[1] + ' ' + arr[2] + ' ' + arr[3] + ' ' + arr[4] + ' ' + arr[5] + ' ' + arr[6] + ' ' + arr[7] + ' ' + arr[8] + ' ' + arr[9]; $('.num').html(ten_gift); var str = '<li> 恭喜 <span>123456</span> 获得了 <span class="present">'+ ten_gift +'</span></li>'; $('.gifts').show(); $('#tab1').append(str); },20000) } }); }); });
}
random_line_split
core.go
package ruler import ( "encoding/json" "errors" "github.com/Masterminds/semver" "math" "math/rand" "reflect" "regexp" "strconv" "strings" ) func validLogic(logic string) (string, error) { formatLogic := formatLogicExpression(logic) if formatLogic == Space || formatLogic == EmptyStr { return EmptyStr, nil } // validate the formatLogic string // 1. only contain legal symbol isValidSymbol := isFormatLogicExpressionAllValidSymbol(formatLogic) if !isValidSymbol { return EmptyStr, errors.New("invalid logic expression: invalid symbol") } // 2. check logic expression by trying to calculate result with random bool err := tryToCalculateResultByFormatLogicExpressionWithRandomProbe(formatLogic) if err != nil { return EmptyStr, errors.New("invalid logic expression: can not calculate") } return formatLogic, nil } func injectLogic(rules *Rules, logic string) (*Rules, error) { formatLogic, err := validLogic(logic) if err != nil { return nil, err } if formatLogic == EmptyStr { return rules, nil } // all ids in logic string must be in rules ids isValidIds := isFormatLogicExpressionAllIdsExist(formatLogic, rules) if !isValidIds { return nil, errors.New("invalid logic expression: invalid id") } rules.Logic = formatLogic return rules, nil } func injectExtractInfo(rules *Rules, extractInfo map[string]string) *Rules { if name, ok := extractInfo["name"]; ok { rules.Name = name } if msg, ok := extractInfo["msg"]; ok { rules.Msg = msg } return rules } func newRulesWithJSON(jsonStr []byte) (*Rules, error) { var rules []*Rule err := json.Unmarshal(jsonStr, &rules) if err != nil { return nil, err } return newRulesWithArray(rules), nil } func newRulesWithArray(rules []*Rule) *Rules { // give rule an id var maxID = 1 for _, rule := range rules { if rule.ID > maxID { maxID = rule.ID } } for index := range rules { if rules[index].ID == 0 { maxID++ rules[index].ID = maxID } } return &Rules{ Rules: rules, } } func (rs *Rules) fitWithMapInFact(o map[string]interface{}) (bool, map[int]string, map[int]interface{}) { var results = make(map[int]bool) var tips = make(map[int]string) var values = make(map[int]interface{}) var hasLogic = false var allRuleIDs []int if rs.Logic != EmptyStr { hasLogic = true } for _, rule := range rs.Rules { v := pluck(rule.Key, o) if v != nil && rule.Val != nil { typeV := reflect.TypeOf(v) typeR := reflect.TypeOf(rule.Val) if !typeV.Comparable() || !typeR.Comparable() { return false, nil, nil } } values[rule.ID] = v flag := rule.fit(v) results[rule.ID] = flag if !flag { // fit false, record msg, for no logic expression usage tips[rule.ID] = rule.Msg } allRuleIDs = append(allRuleIDs, rule.ID) } // compute result by considering logic if !hasLogic { for _, flag := range results { if !flag { return false, tips, values } } return true, rs.getTipsByRuleIDs(allRuleIDs), values } answer, ruleIDs, err := rs.calculateExpressionByTree(results) // tree can return fail reasons in fact tips = rs.getTipsByRuleIDs(ruleIDs) if err != nil { return false, nil, values } return answer, tips, values } func (rs *Rules) getTipsByRuleIDs(ids []int) map[int]string { var tips = make(map[int]string) var allTips = make(map[int]string) for _, rule := range rs.Rules { allTips[rule.ID] = rule.Msg } for _, id := range ids { tips[id] = allTips[id] } return tips } func (r *Rule) fit(v interface{}) bool { if check, err := r.checkSemver(v); err == nil { return check } if check, err := r.checkIP(v); err == nil { return check } op := r.Op // judge if need convert to uniform type var ok bool // index-0 actual, index-1 expect var pairStr = make([]string, 2) var pairNum = make([]float64, 2) var isStr, isNum, isObjStr, isRuleStr bool pairStr[0], ok = v.(string) if !ok { pairNum[0] = formatNumber(v) isStr = false isNum = true isObjStr = false } else { isStr = true isNum = false isObjStr = true } pairStr[1], ok = r.Val.(string) if !ok { pairNum[1] = formatNumber(r.Val) isStr = false isRuleStr = false } else { isNum = false isRuleStr = true } var flagOpIn bool // if in || nin if op == "@" || op == "in" || op == "!@" || op == "nin" || op == "<<" || op == "between" { flagOpIn = true if !isObjStr && isRuleStr { pairStr[0] = strconv.FormatFloat(pairNum[0], 'f', -1, 64) } } // if types different, ignore in & nin if !isStr && !isNum && !flagOpIn { return false } switch op { case "=", "eq": if isNum { return pairNum[0] == pairNum[1] } if isStr { return pairStr[0] == pairStr[1] } return false case ">", "gt": if isNum { return pairNum[0] > pairNum[1] } if isStr { return pairStr[0] > pairStr[1] } return false case "<", "lt": if isNum { return pairNum[0] < pairNum[1] } if isStr { return pairStr[0] < pairStr[1] } return false case ">=", "gte": if isNum { return pairNum[0] >= pairNum[1] } if isStr { return pairStr[0] >= pairStr[1] } return false case "<=", "lte": if isNum { return pairNum[0] <= pairNum[1] } if isStr { return pairStr[0] <= pairStr[1] } return false case "!=", "neq": if isNum { return pairNum[0] != pairNum[1] } if isStr { return pairStr[0] != pairStr[1] } return false case "@", "in": return isIn(pairStr[0], pairStr[1], !isObjStr) case "!@", "nin": return !isIn(pairStr[0], pairStr[1], !isObjStr) case "^$", "regex": return checkRegex(pairStr[1], pairStr[0]) case "0", "empty": return v == nil case "1", "nempty": return v != nil case "<<", "between": return isBetween(pairNum[0], pairStr[1]) case "@@", "intersect": return isIntersect(pairStr[1], pairStr[0]) default: return false } } func (r *Rule) checkIP(v interface{}) (bool, error) { if strings.HasSuffix(strings.ToLower(r.Key), "ip") { ver1, ok := r.Val.(string) if ok { ver2, ok := v.(string) if ok { if len(strings.Split(ver2, ".")) == 4 { return CheckIP(ver2, ver1), nil } } } } return false, errors.New("invalid ip") } func (r *Rule) checkSemver(v interface{}) (bool, error) { op := r.Op if strings.HasSuffix(strings.ToLower(r.Key), "version") { ver1, ok := r.Val.(string) if ok { validate := true ver2, ok := v.(string) if ok { /*v1len := len(strings.Split(ver1, ".")) v2len := len(strings.Split(ver2, ".")) if v1len > 4 || v2len > 4 { validate = false } else { if v1len != v2len { if v1len > v2len { for i:=0; i< v1len - v2len; i++ { //ver2 += ".0" } } else if v1len < v2len { for i:=0; i< v2len - v1len; i++ { //ver1 += ".0" } } } }*/ } else { validate = false } if validate { ver1 = repair(ver1) constraint, err := semver.NewConstraint(op + " " + ver1) if err == nil { ver2 = repair(ver2) version, err := semver.NewVersion(ver2) if err == nil { return constraint.Check(version), nil } } } } } return false, errors.New("invalid semver") } func repair(ver string) string { if len(strings.Split(ver, ".")) > 3 { //4位版本号,不含'-',转rc版本 if strings.Index(ver, "-") == -1 { versions := strings.Split(ver, ".") return versions[0] + "." + versions[1] + "." + versions[2] + "-rc." + versions[3] } } return ver } func pluck(key string, o map[string]interface{}) interface{} { if o == nil || key == EmptyStr { return nil } paths := strings.Split(key, ".") var ok bool for index, step := range paths { // last step is object key if index == len(paths)-1 { return o[step] } // explore deeper if o, ok = o[step].(map[string]interface{}); !ok { return nil } } return nil } func formatNumber(v interface{}) float64 { switch t := v.(type) { case uint: return float64(t) case uint8: return float64(t) case uint16: return float64(t) case uint32: return float64(t) case uint64: return float64(t) case int: return float64(t) case int8: return float64(t) case int16: return float64(t) case int32: return float64(t) case int64: return float64(t) case float32: return float64(t) case float64: return t default: return 0 } } func checkRegex(pattern, o string) bool { regex, err := regexp.Compile(pattern) if err != nil { return false } return regex.MatchString(o) } func formatLogicExpression(strRawExpr string) string { var flagPre, flagNow string strBracket := "bracket" strSpace := "space" strNotSpace := "notSpace" strOrigin := strings.ToLower(strRawExpr) runesPretty := make([]rune, 0) for _, c := range strOrigin { if c <= '9' && c >= '0' { flagNow = "num" } else if c <= 'z' && c >= 'a' { flagNow = "char" } else if c == '(' || c == ')' { flagNow = strBracket } else { flagNow = flagPre } if flagNow != flagPre || flagNow == strBracket && flagPre == strBracket { // should insert space here runesPretty = append(runesPretty, []rune(Space)[0]) } runesPretty = append(runesPretty, c) flagPre = flagNow } // remove redundant space flagPre = strNotSpace runesTrim := make([]rune, 0) for _, c := range runesPretty { if c == []rune(Space)[0] { flagNow = strSpace } else { flagNow = strNotSpace } if flagNow == strSpace && flagPre == strSpace { // continuous space continue } else { runesTrim = append(runesTrim, c) } flagPre = flagNow } strPrettyTrim := string(runesTrim) strPrettyTrim = strings.Trim(strPrettyTrim, Space) return strPrettyTrim } func isFormatLogicExpressionAllValidSymbol(strFormatLogic string) bool { listSymbol := strings.Split(strFormatLogic, Space) for _, symbol := range listSymbol { flag := false regex, err := regexp.Compile(PatternNumber) if err != nil { return false } if regex.MatchString(symbol) { // is number ok continue } for _, op := range ValidOperators { if op == symbol { // is operator ok flag = true } } for _, v := range []string{"(", ")"} { if v == symbol { // is bracket ok flag = true } } if !flag { return false } } return true } func isFormatLogicExpressionAllIdsExist(strFormatLogic string, rules *Rules) bool { mapExistIds := make(map[string]bool) for _, eachRule := range rules.Rules { mapExistIds[strconv.Itoa(eachRule.ID)] = true } listSymbol := strings.Split(strFormatLogic, Space) regex, err := regexp.Compile(PatternNumber) if err != nil { return false } for _, symbol := range listSymbol { if regex.MatchString(symbol) { // is id, check it if _, ok := mapExistIds[symbol]; ok { continue } else { return false } } } return true } func tryToCalculateResultByFormatLogicExpressionWithRandomProbe(strFormatLogic string) error { listSymbol := strings.Split(strFormatLogic, Space) regex, err := regexp.Compile(PatternNumber) if err != nil { return err } // random probe mapProbe := make(map[int]bool) for _, symbol := range listSymbol { if regex.MatchString(symbol) { id, iErr := strconv.Atoi(symbol) if iErr != nil { return iErr } randomInt := rand.Intn(10) randomBool := randomInt < 5 mapProbe[id] = randomBool } } // calculate still use reverse_polish_notation r := &Rules{} _, err = r.calculateExpression(strFormatLogic, mapProbe) return err } func numOfOperandInLogic(op string) int8 { mapOperand := map[string]int8{"or": 2, "and": 2, "not": 1} return mapOperand[op] } func computeOneInLogic(op string, v []bool) (bool, error) { switch op { case "or": return v[0] || v[1], nil case "and": return v[0] && v[1], nil case "not": return !v[0], nil default: return false, errors.New("unrecognized op") } } func isIn(needle, haystack string, isNeedleNum bool) bool { // get number of ne
Str string) bool { // compatible to "1, 2, 3" and "1,2,3" vl := strings.Split(objStr, ",") li := strings.Split(ruleStr, ",") for _, o := range li { trimO := strings.Trim(o, " ") for _, v := range vl { trimV := strings.Trim(v, " ") if trimV == trimO { return true } } } return false } func isBetween(obj float64, scope string) bool { scope = strings.Trim(scope, " ") var equalLeft, equalRight bool // [] 双闭区间 result := regexp.MustCompile("^\\[ *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *]$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = true equalRight = true return calculateBetween(obj, result, equalLeft, equalRight) } // [) 左闭右开区间 result = regexp.MustCompile("^\\[ *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *\\)$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = true equalRight = false return calculateBetween(obj, result, equalLeft, equalRight) } // (] 左开右闭区间 result = regexp.MustCompile("^\\( *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *]$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = false equalRight = true return calculateBetween(obj, result, equalLeft, equalRight) } // () 双开区间 result = regexp.MustCompile("^\\( *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *\\)$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = false equalRight = false return calculateBetween(obj, result, equalLeft, equalRight) } return false } func calculateBetween(obj float64, result []string, equalLeft, equalRight bool) bool { var hasLeft, hasRight bool var left, right float64 var err error if result[1] != "" { hasLeft = true left, err = strconv.ParseFloat(result[1], 64) if err != nil { return false } } if result[2] != "" { hasRight = true right, err = strconv.ParseFloat(result[2], 64) if err != nil { return false } } // calculate if !hasLeft && !hasRight { return false } flag := true if hasLeft { if equalLeft { flag = flag && obj >= left } else { flag = flag && obj > left } } if hasRight { if equalRight { flag = flag && obj <= right } else { flag = flag && obj < right } } return flag }
edle var iNum float64 var err error if isNeedleNum { if iNum, err = strconv.ParseFloat(needle, 64); err != nil { return false } } // compatible to "1, 2, 3" and "1,2,3" li := strings.Split(haystack, ",") for _, o := range li { trimO := strings.TrimLeft(o, " ") if isNeedleNum { oNum, err := strconv.ParseFloat(trimO, 64) if err != nil { continue } if math.Abs(iNum-oNum) < 1e-5 { // 考虑浮点精度问题 return true } } else if needle == trimO { return true } } return false } func isIntersect(objStr string, rule
identifier_body
core.go
package ruler import ( "encoding/json" "errors" "github.com/Masterminds/semver" "math" "math/rand" "reflect" "regexp" "strconv" "strings"
func validLogic(logic string) (string, error) { formatLogic := formatLogicExpression(logic) if formatLogic == Space || formatLogic == EmptyStr { return EmptyStr, nil } // validate the formatLogic string // 1. only contain legal symbol isValidSymbol := isFormatLogicExpressionAllValidSymbol(formatLogic) if !isValidSymbol { return EmptyStr, errors.New("invalid logic expression: invalid symbol") } // 2. check logic expression by trying to calculate result with random bool err := tryToCalculateResultByFormatLogicExpressionWithRandomProbe(formatLogic) if err != nil { return EmptyStr, errors.New("invalid logic expression: can not calculate") } return formatLogic, nil } func injectLogic(rules *Rules, logic string) (*Rules, error) { formatLogic, err := validLogic(logic) if err != nil { return nil, err } if formatLogic == EmptyStr { return rules, nil } // all ids in logic string must be in rules ids isValidIds := isFormatLogicExpressionAllIdsExist(formatLogic, rules) if !isValidIds { return nil, errors.New("invalid logic expression: invalid id") } rules.Logic = formatLogic return rules, nil } func injectExtractInfo(rules *Rules, extractInfo map[string]string) *Rules { if name, ok := extractInfo["name"]; ok { rules.Name = name } if msg, ok := extractInfo["msg"]; ok { rules.Msg = msg } return rules } func newRulesWithJSON(jsonStr []byte) (*Rules, error) { var rules []*Rule err := json.Unmarshal(jsonStr, &rules) if err != nil { return nil, err } return newRulesWithArray(rules), nil } func newRulesWithArray(rules []*Rule) *Rules { // give rule an id var maxID = 1 for _, rule := range rules { if rule.ID > maxID { maxID = rule.ID } } for index := range rules { if rules[index].ID == 0 { maxID++ rules[index].ID = maxID } } return &Rules{ Rules: rules, } } func (rs *Rules) fitWithMapInFact(o map[string]interface{}) (bool, map[int]string, map[int]interface{}) { var results = make(map[int]bool) var tips = make(map[int]string) var values = make(map[int]interface{}) var hasLogic = false var allRuleIDs []int if rs.Logic != EmptyStr { hasLogic = true } for _, rule := range rs.Rules { v := pluck(rule.Key, o) if v != nil && rule.Val != nil { typeV := reflect.TypeOf(v) typeR := reflect.TypeOf(rule.Val) if !typeV.Comparable() || !typeR.Comparable() { return false, nil, nil } } values[rule.ID] = v flag := rule.fit(v) results[rule.ID] = flag if !flag { // fit false, record msg, for no logic expression usage tips[rule.ID] = rule.Msg } allRuleIDs = append(allRuleIDs, rule.ID) } // compute result by considering logic if !hasLogic { for _, flag := range results { if !flag { return false, tips, values } } return true, rs.getTipsByRuleIDs(allRuleIDs), values } answer, ruleIDs, err := rs.calculateExpressionByTree(results) // tree can return fail reasons in fact tips = rs.getTipsByRuleIDs(ruleIDs) if err != nil { return false, nil, values } return answer, tips, values } func (rs *Rules) getTipsByRuleIDs(ids []int) map[int]string { var tips = make(map[int]string) var allTips = make(map[int]string) for _, rule := range rs.Rules { allTips[rule.ID] = rule.Msg } for _, id := range ids { tips[id] = allTips[id] } return tips } func (r *Rule) fit(v interface{}) bool { if check, err := r.checkSemver(v); err == nil { return check } if check, err := r.checkIP(v); err == nil { return check } op := r.Op // judge if need convert to uniform type var ok bool // index-0 actual, index-1 expect var pairStr = make([]string, 2) var pairNum = make([]float64, 2) var isStr, isNum, isObjStr, isRuleStr bool pairStr[0], ok = v.(string) if !ok { pairNum[0] = formatNumber(v) isStr = false isNum = true isObjStr = false } else { isStr = true isNum = false isObjStr = true } pairStr[1], ok = r.Val.(string) if !ok { pairNum[1] = formatNumber(r.Val) isStr = false isRuleStr = false } else { isNum = false isRuleStr = true } var flagOpIn bool // if in || nin if op == "@" || op == "in" || op == "!@" || op == "nin" || op == "<<" || op == "between" { flagOpIn = true if !isObjStr && isRuleStr { pairStr[0] = strconv.FormatFloat(pairNum[0], 'f', -1, 64) } } // if types different, ignore in & nin if !isStr && !isNum && !flagOpIn { return false } switch op { case "=", "eq": if isNum { return pairNum[0] == pairNum[1] } if isStr { return pairStr[0] == pairStr[1] } return false case ">", "gt": if isNum { return pairNum[0] > pairNum[1] } if isStr { return pairStr[0] > pairStr[1] } return false case "<", "lt": if isNum { return pairNum[0] < pairNum[1] } if isStr { return pairStr[0] < pairStr[1] } return false case ">=", "gte": if isNum { return pairNum[0] >= pairNum[1] } if isStr { return pairStr[0] >= pairStr[1] } return false case "<=", "lte": if isNum { return pairNum[0] <= pairNum[1] } if isStr { return pairStr[0] <= pairStr[1] } return false case "!=", "neq": if isNum { return pairNum[0] != pairNum[1] } if isStr { return pairStr[0] != pairStr[1] } return false case "@", "in": return isIn(pairStr[0], pairStr[1], !isObjStr) case "!@", "nin": return !isIn(pairStr[0], pairStr[1], !isObjStr) case "^$", "regex": return checkRegex(pairStr[1], pairStr[0]) case "0", "empty": return v == nil case "1", "nempty": return v != nil case "<<", "between": return isBetween(pairNum[0], pairStr[1]) case "@@", "intersect": return isIntersect(pairStr[1], pairStr[0]) default: return false } } func (r *Rule) checkIP(v interface{}) (bool, error) { if strings.HasSuffix(strings.ToLower(r.Key), "ip") { ver1, ok := r.Val.(string) if ok { ver2, ok := v.(string) if ok { if len(strings.Split(ver2, ".")) == 4 { return CheckIP(ver2, ver1), nil } } } } return false, errors.New("invalid ip") } func (r *Rule) checkSemver(v interface{}) (bool, error) { op := r.Op if strings.HasSuffix(strings.ToLower(r.Key), "version") { ver1, ok := r.Val.(string) if ok { validate := true ver2, ok := v.(string) if ok { /*v1len := len(strings.Split(ver1, ".")) v2len := len(strings.Split(ver2, ".")) if v1len > 4 || v2len > 4 { validate = false } else { if v1len != v2len { if v1len > v2len { for i:=0; i< v1len - v2len; i++ { //ver2 += ".0" } } else if v1len < v2len { for i:=0; i< v2len - v1len; i++ { //ver1 += ".0" } } } }*/ } else { validate = false } if validate { ver1 = repair(ver1) constraint, err := semver.NewConstraint(op + " " + ver1) if err == nil { ver2 = repair(ver2) version, err := semver.NewVersion(ver2) if err == nil { return constraint.Check(version), nil } } } } } return false, errors.New("invalid semver") } func repair(ver string) string { if len(strings.Split(ver, ".")) > 3 { //4位版本号,不含'-',转rc版本 if strings.Index(ver, "-") == -1 { versions := strings.Split(ver, ".") return versions[0] + "." + versions[1] + "." + versions[2] + "-rc." + versions[3] } } return ver } func pluck(key string, o map[string]interface{}) interface{} { if o == nil || key == EmptyStr { return nil } paths := strings.Split(key, ".") var ok bool for index, step := range paths { // last step is object key if index == len(paths)-1 { return o[step] } // explore deeper if o, ok = o[step].(map[string]interface{}); !ok { return nil } } return nil } func formatNumber(v interface{}) float64 { switch t := v.(type) { case uint: return float64(t) case uint8: return float64(t) case uint16: return float64(t) case uint32: return float64(t) case uint64: return float64(t) case int: return float64(t) case int8: return float64(t) case int16: return float64(t) case int32: return float64(t) case int64: return float64(t) case float32: return float64(t) case float64: return t default: return 0 } } func checkRegex(pattern, o string) bool { regex, err := regexp.Compile(pattern) if err != nil { return false } return regex.MatchString(o) } func formatLogicExpression(strRawExpr string) string { var flagPre, flagNow string strBracket := "bracket" strSpace := "space" strNotSpace := "notSpace" strOrigin := strings.ToLower(strRawExpr) runesPretty := make([]rune, 0) for _, c := range strOrigin { if c <= '9' && c >= '0' { flagNow = "num" } else if c <= 'z' && c >= 'a' { flagNow = "char" } else if c == '(' || c == ')' { flagNow = strBracket } else { flagNow = flagPre } if flagNow != flagPre || flagNow == strBracket && flagPre == strBracket { // should insert space here runesPretty = append(runesPretty, []rune(Space)[0]) } runesPretty = append(runesPretty, c) flagPre = flagNow } // remove redundant space flagPre = strNotSpace runesTrim := make([]rune, 0) for _, c := range runesPretty { if c == []rune(Space)[0] { flagNow = strSpace } else { flagNow = strNotSpace } if flagNow == strSpace && flagPre == strSpace { // continuous space continue } else { runesTrim = append(runesTrim, c) } flagPre = flagNow } strPrettyTrim := string(runesTrim) strPrettyTrim = strings.Trim(strPrettyTrim, Space) return strPrettyTrim } func isFormatLogicExpressionAllValidSymbol(strFormatLogic string) bool { listSymbol := strings.Split(strFormatLogic, Space) for _, symbol := range listSymbol { flag := false regex, err := regexp.Compile(PatternNumber) if err != nil { return false } if regex.MatchString(symbol) { // is number ok continue } for _, op := range ValidOperators { if op == symbol { // is operator ok flag = true } } for _, v := range []string{"(", ")"} { if v == symbol { // is bracket ok flag = true } } if !flag { return false } } return true } func isFormatLogicExpressionAllIdsExist(strFormatLogic string, rules *Rules) bool { mapExistIds := make(map[string]bool) for _, eachRule := range rules.Rules { mapExistIds[strconv.Itoa(eachRule.ID)] = true } listSymbol := strings.Split(strFormatLogic, Space) regex, err := regexp.Compile(PatternNumber) if err != nil { return false } for _, symbol := range listSymbol { if regex.MatchString(symbol) { // is id, check it if _, ok := mapExistIds[symbol]; ok { continue } else { return false } } } return true } func tryToCalculateResultByFormatLogicExpressionWithRandomProbe(strFormatLogic string) error { listSymbol := strings.Split(strFormatLogic, Space) regex, err := regexp.Compile(PatternNumber) if err != nil { return err } // random probe mapProbe := make(map[int]bool) for _, symbol := range listSymbol { if regex.MatchString(symbol) { id, iErr := strconv.Atoi(symbol) if iErr != nil { return iErr } randomInt := rand.Intn(10) randomBool := randomInt < 5 mapProbe[id] = randomBool } } // calculate still use reverse_polish_notation r := &Rules{} _, err = r.calculateExpression(strFormatLogic, mapProbe) return err } func numOfOperandInLogic(op string) int8 { mapOperand := map[string]int8{"or": 2, "and": 2, "not": 1} return mapOperand[op] } func computeOneInLogic(op string, v []bool) (bool, error) { switch op { case "or": return v[0] || v[1], nil case "and": return v[0] && v[1], nil case "not": return !v[0], nil default: return false, errors.New("unrecognized op") } } func isIn(needle, haystack string, isNeedleNum bool) bool { // get number of needle var iNum float64 var err error if isNeedleNum { if iNum, err = strconv.ParseFloat(needle, 64); err != nil { return false } } // compatible to "1, 2, 3" and "1,2,3" li := strings.Split(haystack, ",") for _, o := range li { trimO := strings.TrimLeft(o, " ") if isNeedleNum { oNum, err := strconv.ParseFloat(trimO, 64) if err != nil { continue } if math.Abs(iNum-oNum) < 1e-5 { // 考虑浮点精度问题 return true } } else if needle == trimO { return true } } return false } func isIntersect(objStr string, ruleStr string) bool { // compatible to "1, 2, 3" and "1,2,3" vl := strings.Split(objStr, ",") li := strings.Split(ruleStr, ",") for _, o := range li { trimO := strings.Trim(o, " ") for _, v := range vl { trimV := strings.Trim(v, " ") if trimV == trimO { return true } } } return false } func isBetween(obj float64, scope string) bool { scope = strings.Trim(scope, " ") var equalLeft, equalRight bool // [] 双闭区间 result := regexp.MustCompile("^\\[ *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *]$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = true equalRight = true return calculateBetween(obj, result, equalLeft, equalRight) } // [) 左闭右开区间 result = regexp.MustCompile("^\\[ *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *\\)$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = true equalRight = false return calculateBetween(obj, result, equalLeft, equalRight) } // (] 左开右闭区间 result = regexp.MustCompile("^\\( *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *]$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = false equalRight = true return calculateBetween(obj, result, equalLeft, equalRight) } // () 双开区间 result = regexp.MustCompile("^\\( *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *\\)$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = false equalRight = false return calculateBetween(obj, result, equalLeft, equalRight) } return false } func calculateBetween(obj float64, result []string, equalLeft, equalRight bool) bool { var hasLeft, hasRight bool var left, right float64 var err error if result[1] != "" { hasLeft = true left, err = strconv.ParseFloat(result[1], 64) if err != nil { return false } } if result[2] != "" { hasRight = true right, err = strconv.ParseFloat(result[2], 64) if err != nil { return false } } // calculate if !hasLeft && !hasRight { return false } flag := true if hasLeft { if equalLeft { flag = flag && obj >= left } else { flag = flag && obj > left } } if hasRight { if equalRight { flag = flag && obj <= right } else { flag = flag && obj < right } } return flag }
)
random_line_split
core.go
package ruler import ( "encoding/json" "errors" "github.com/Masterminds/semver" "math" "math/rand" "reflect" "regexp" "strconv" "strings" ) func validLogic(logic string) (string, error) { formatLogic := formatLogicExpression(logic) if formatLogic == Space || formatLogic == EmptyStr { return EmptyStr, nil } // validate the formatLogic string // 1. only contain legal symbol isValidSymbol := isFormatLogicExpressionAllValidSymbol(formatLogic) if !isValidSymbol { return EmptyStr, errors.New("invalid logic expression: invalid symbol") } // 2. check logic expression by trying to calculate result with random bool err := tryToCalculateResultByFormatLogicExpressionWithRandomProbe(formatLogic) if err != nil { return EmptyStr, errors.New("invalid logic expression: can not calculate") } return formatLogic, nil } func injectLogic(rules *Rules, logic string) (*Rules, error) { formatLogic, err := validLogic(logic) if err != nil { return nil, err } if formatLogic == EmptyStr { return rules, nil } // all ids in logic string must be in rules ids isValidIds := isFormatLogicExpressionAllIdsExist(formatLogic, rules) if !isValidIds { return nil, errors.New("invalid logic expression: invalid id") } rules.Logic = formatLogic return rules, nil } func injectExtractInfo(rules *Rules, extractInfo map[string]string) *Rules { if name, ok := extractInfo["name"]; ok { rules.Name = name } if msg, ok := extractInfo["msg"]; ok { rules.Msg = msg } return rules } func newRulesWithJSON(jsonStr []byte) (*Rules, error) { var rules []*Rule err := json.Unmarshal(jsonStr, &rules) if err != nil { return nil, err } return newRulesWithArray(rules), nil } func newRulesWithArray(rules []*Rule) *Rules { // give rule an id var maxID = 1 for _, rule := range rules { if rule.ID > maxID { maxID = rule.ID } } for index := range rules { if rules[index].ID == 0 { maxID++ rules[index].ID = maxID } } return &Rules{ Rules: rules, } } func (rs *Rules) fitWithMapInFact(o map[string]interface{}) (bool, map[int]string, map[int]interface{}) { var results = make(map[int]bool) var tips = make(map[int]string) var values = make(map[int]interface{}) var hasLogic = false var allRuleIDs []int if rs.Logic != EmptyStr
for _, rule := range rs.Rules { v := pluck(rule.Key, o) if v != nil && rule.Val != nil { typeV := reflect.TypeOf(v) typeR := reflect.TypeOf(rule.Val) if !typeV.Comparable() || !typeR.Comparable() { return false, nil, nil } } values[rule.ID] = v flag := rule.fit(v) results[rule.ID] = flag if !flag { // fit false, record msg, for no logic expression usage tips[rule.ID] = rule.Msg } allRuleIDs = append(allRuleIDs, rule.ID) } // compute result by considering logic if !hasLogic { for _, flag := range results { if !flag { return false, tips, values } } return true, rs.getTipsByRuleIDs(allRuleIDs), values } answer, ruleIDs, err := rs.calculateExpressionByTree(results) // tree can return fail reasons in fact tips = rs.getTipsByRuleIDs(ruleIDs) if err != nil { return false, nil, values } return answer, tips, values } func (rs *Rules) getTipsByRuleIDs(ids []int) map[int]string { var tips = make(map[int]string) var allTips = make(map[int]string) for _, rule := range rs.Rules { allTips[rule.ID] = rule.Msg } for _, id := range ids { tips[id] = allTips[id] } return tips } func (r *Rule) fit(v interface{}) bool { if check, err := r.checkSemver(v); err == nil { return check } if check, err := r.checkIP(v); err == nil { return check } op := r.Op // judge if need convert to uniform type var ok bool // index-0 actual, index-1 expect var pairStr = make([]string, 2) var pairNum = make([]float64, 2) var isStr, isNum, isObjStr, isRuleStr bool pairStr[0], ok = v.(string) if !ok { pairNum[0] = formatNumber(v) isStr = false isNum = true isObjStr = false } else { isStr = true isNum = false isObjStr = true } pairStr[1], ok = r.Val.(string) if !ok { pairNum[1] = formatNumber(r.Val) isStr = false isRuleStr = false } else { isNum = false isRuleStr = true } var flagOpIn bool // if in || nin if op == "@" || op == "in" || op == "!@" || op == "nin" || op == "<<" || op == "between" { flagOpIn = true if !isObjStr && isRuleStr { pairStr[0] = strconv.FormatFloat(pairNum[0], 'f', -1, 64) } } // if types different, ignore in & nin if !isStr && !isNum && !flagOpIn { return false } switch op { case "=", "eq": if isNum { return pairNum[0] == pairNum[1] } if isStr { return pairStr[0] == pairStr[1] } return false case ">", "gt": if isNum { return pairNum[0] > pairNum[1] } if isStr { return pairStr[0] > pairStr[1] } return false case "<", "lt": if isNum { return pairNum[0] < pairNum[1] } if isStr { return pairStr[0] < pairStr[1] } return false case ">=", "gte": if isNum { return pairNum[0] >= pairNum[1] } if isStr { return pairStr[0] >= pairStr[1] } return false case "<=", "lte": if isNum { return pairNum[0] <= pairNum[1] } if isStr { return pairStr[0] <= pairStr[1] } return false case "!=", "neq": if isNum { return pairNum[0] != pairNum[1] } if isStr { return pairStr[0] != pairStr[1] } return false case "@", "in": return isIn(pairStr[0], pairStr[1], !isObjStr) case "!@", "nin": return !isIn(pairStr[0], pairStr[1], !isObjStr) case "^$", "regex": return checkRegex(pairStr[1], pairStr[0]) case "0", "empty": return v == nil case "1", "nempty": return v != nil case "<<", "between": return isBetween(pairNum[0], pairStr[1]) case "@@", "intersect": return isIntersect(pairStr[1], pairStr[0]) default: return false } } func (r *Rule) checkIP(v interface{}) (bool, error) { if strings.HasSuffix(strings.ToLower(r.Key), "ip") { ver1, ok := r.Val.(string) if ok { ver2, ok := v.(string) if ok { if len(strings.Split(ver2, ".")) == 4 { return CheckIP(ver2, ver1), nil } } } } return false, errors.New("invalid ip") } func (r *Rule) checkSemver(v interface{}) (bool, error) { op := r.Op if strings.HasSuffix(strings.ToLower(r.Key), "version") { ver1, ok := r.Val.(string) if ok { validate := true ver2, ok := v.(string) if ok { /*v1len := len(strings.Split(ver1, ".")) v2len := len(strings.Split(ver2, ".")) if v1len > 4 || v2len > 4 { validate = false } else { if v1len != v2len { if v1len > v2len { for i:=0; i< v1len - v2len; i++ { //ver2 += ".0" } } else if v1len < v2len { for i:=0; i< v2len - v1len; i++ { //ver1 += ".0" } } } }*/ } else { validate = false } if validate { ver1 = repair(ver1) constraint, err := semver.NewConstraint(op + " " + ver1) if err == nil { ver2 = repair(ver2) version, err := semver.NewVersion(ver2) if err == nil { return constraint.Check(version), nil } } } } } return false, errors.New("invalid semver") } func repair(ver string) string { if len(strings.Split(ver, ".")) > 3 { //4位版本号,不含'-',转rc版本 if strings.Index(ver, "-") == -1 { versions := strings.Split(ver, ".") return versions[0] + "." + versions[1] + "." + versions[2] + "-rc." + versions[3] } } return ver } func pluck(key string, o map[string]interface{}) interface{} { if o == nil || key == EmptyStr { return nil } paths := strings.Split(key, ".") var ok bool for index, step := range paths { // last step is object key if index == len(paths)-1 { return o[step] } // explore deeper if o, ok = o[step].(map[string]interface{}); !ok { return nil } } return nil } func formatNumber(v interface{}) float64 { switch t := v.(type) { case uint: return float64(t) case uint8: return float64(t) case uint16: return float64(t) case uint32: return float64(t) case uint64: return float64(t) case int: return float64(t) case int8: return float64(t) case int16: return float64(t) case int32: return float64(t) case int64: return float64(t) case float32: return float64(t) case float64: return t default: return 0 } } func checkRegex(pattern, o string) bool { regex, err := regexp.Compile(pattern) if err != nil { return false } return regex.MatchString(o) } func formatLogicExpression(strRawExpr string) string { var flagPre, flagNow string strBracket := "bracket" strSpace := "space" strNotSpace := "notSpace" strOrigin := strings.ToLower(strRawExpr) runesPretty := make([]rune, 0) for _, c := range strOrigin { if c <= '9' && c >= '0' { flagNow = "num" } else if c <= 'z' && c >= 'a' { flagNow = "char" } else if c == '(' || c == ')' { flagNow = strBracket } else { flagNow = flagPre } if flagNow != flagPre || flagNow == strBracket && flagPre == strBracket { // should insert space here runesPretty = append(runesPretty, []rune(Space)[0]) } runesPretty = append(runesPretty, c) flagPre = flagNow } // remove redundant space flagPre = strNotSpace runesTrim := make([]rune, 0) for _, c := range runesPretty { if c == []rune(Space)[0] { flagNow = strSpace } else { flagNow = strNotSpace } if flagNow == strSpace && flagPre == strSpace { // continuous space continue } else { runesTrim = append(runesTrim, c) } flagPre = flagNow } strPrettyTrim := string(runesTrim) strPrettyTrim = strings.Trim(strPrettyTrim, Space) return strPrettyTrim } func isFormatLogicExpressionAllValidSymbol(strFormatLogic string) bool { listSymbol := strings.Split(strFormatLogic, Space) for _, symbol := range listSymbol { flag := false regex, err := regexp.Compile(PatternNumber) if err != nil { return false } if regex.MatchString(symbol) { // is number ok continue } for _, op := range ValidOperators { if op == symbol { // is operator ok flag = true } } for _, v := range []string{"(", ")"} { if v == symbol { // is bracket ok flag = true } } if !flag { return false } } return true } func isFormatLogicExpressionAllIdsExist(strFormatLogic string, rules *Rules) bool { mapExistIds := make(map[string]bool) for _, eachRule := range rules.Rules { mapExistIds[strconv.Itoa(eachRule.ID)] = true } listSymbol := strings.Split(strFormatLogic, Space) regex, err := regexp.Compile(PatternNumber) if err != nil { return false } for _, symbol := range listSymbol { if regex.MatchString(symbol) { // is id, check it if _, ok := mapExistIds[symbol]; ok { continue } else { return false } } } return true } func tryToCalculateResultByFormatLogicExpressionWithRandomProbe(strFormatLogic string) error { listSymbol := strings.Split(strFormatLogic, Space) regex, err := regexp.Compile(PatternNumber) if err != nil { return err } // random probe mapProbe := make(map[int]bool) for _, symbol := range listSymbol { if regex.MatchString(symbol) { id, iErr := strconv.Atoi(symbol) if iErr != nil { return iErr } randomInt := rand.Intn(10) randomBool := randomInt < 5 mapProbe[id] = randomBool } } // calculate still use reverse_polish_notation r := &Rules{} _, err = r.calculateExpression(strFormatLogic, mapProbe) return err } func numOfOperandInLogic(op string) int8 { mapOperand := map[string]int8{"or": 2, "and": 2, "not": 1} return mapOperand[op] } func computeOneInLogic(op string, v []bool) (bool, error) { switch op { case "or": return v[0] || v[1], nil case "and": return v[0] && v[1], nil case "not": return !v[0], nil default: return false, errors.New("unrecognized op") } } func isIn(needle, haystack string, isNeedleNum bool) bool { // get number of needle var iNum float64 var err error if isNeedleNum { if iNum, err = strconv.ParseFloat(needle, 64); err != nil { return false } } // compatible to "1, 2, 3" and "1,2,3" li := strings.Split(haystack, ",") for _, o := range li { trimO := strings.TrimLeft(o, " ") if isNeedleNum { oNum, err := strconv.ParseFloat(trimO, 64) if err != nil { continue } if math.Abs(iNum-oNum) < 1e-5 { // 考虑浮点精度问题 return true } } else if needle == trimO { return true } } return false } func isIntersect(objStr string, ruleStr string) bool { // compatible to "1, 2, 3" and "1,2,3" vl := strings.Split(objStr, ",") li := strings.Split(ruleStr, ",") for _, o := range li { trimO := strings.Trim(o, " ") for _, v := range vl { trimV := strings.Trim(v, " ") if trimV == trimO { return true } } } return false } func isBetween(obj float64, scope string) bool { scope = strings.Trim(scope, " ") var equalLeft, equalRight bool // [] 双闭区间 result := regexp.MustCompile("^\\[ *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *]$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = true equalRight = true return calculateBetween(obj, result, equalLeft, equalRight) } // [) 左闭右开区间 result = regexp.MustCompile("^\\[ *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *\\)$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = true equalRight = false return calculateBetween(obj, result, equalLeft, equalRight) } // (] 左开右闭区间 result = regexp.MustCompile("^\\( *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *]$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = false equalRight = true return calculateBetween(obj, result, equalLeft, equalRight) } // () 双开区间 result = regexp.MustCompile("^\\( *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *\\)$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = false equalRight = false return calculateBetween(obj, result, equalLeft, equalRight) } return false } func calculateBetween(obj float64, result []string, equalLeft, equalRight bool) bool { var hasLeft, hasRight bool var left, right float64 var err error if result[1] != "" { hasLeft = true left, err = strconv.ParseFloat(result[1], 64) if err != nil { return false } } if result[2] != "" { hasRight = true right, err = strconv.ParseFloat(result[2], 64) if err != nil { return false } } // calculate if !hasLeft && !hasRight { return false } flag := true if hasLeft { if equalLeft { flag = flag && obj >= left } else { flag = flag && obj > left } } if hasRight { if equalRight { flag = flag && obj <= right } else { flag = flag && obj < right } } return flag }
{ hasLogic = true }
conditional_block
core.go
package ruler import ( "encoding/json" "errors" "github.com/Masterminds/semver" "math" "math/rand" "reflect" "regexp" "strconv" "strings" ) func validLogic(logic string) (string, error) { formatLogic := formatLogicExpression(logic) if formatLogic == Space || formatLogic == EmptyStr { return EmptyStr, nil } // validate the formatLogic string // 1. only contain legal symbol isValidSymbol := isFormatLogicExpressionAllValidSymbol(formatLogic) if !isValidSymbol { return EmptyStr, errors.New("invalid logic expression: invalid symbol") } // 2. check logic expression by trying to calculate result with random bool err := tryToCalculateResultByFormatLogicExpressionWithRandomProbe(formatLogic) if err != nil { return EmptyStr, errors.New("invalid logic expression: can not calculate") } return formatLogic, nil } func injectLogic(rules *Rules, logic string) (*Rules, error) { formatLogic, err := validLogic(logic) if err != nil { return nil, err } if formatLogic == EmptyStr { return rules, nil } // all ids in logic string must be in rules ids isValidIds := isFormatLogicExpressionAllIdsExist(formatLogic, rules) if !isValidIds { return nil, errors.New("invalid logic expression: invalid id") } rules.Logic = formatLogic return rules, nil } func injectExtractInfo(rules *Rules, extractInfo map[string]string) *Rules { if name, ok := extractInfo["name"]; ok { rules.Name = name } if msg, ok := extractInfo["msg"]; ok { rules.Msg = msg } return rules } func newRulesWithJSON(jsonStr []byte) (*Rules, error) { var rules []*Rule err := json.Unmarshal(jsonStr, &rules) if err != nil { return nil, err } return newRulesWithArray(rules), nil } func newRulesWithArray(rules []*Rule) *Rules { // give rule an id var maxID = 1 for _, rule := range rules { if rule.ID > maxID { maxID = rule.ID } } for index := range rules { if rules[index].ID == 0 { maxID++ rules[index].ID = maxID } } return &Rules{ Rules: rules, } } func (rs *Rules) fitWithMapInFact(o map[string]interface{}) (bool, map[int]string, map[int]interface{}) { var results = make(map[int]bool) var tips = make(map[int]string) var values = make(map[int]interface{}) var hasLogic = false var allRuleIDs []int if rs.Logic != EmptyStr { hasLogic = true } for _, rule := range rs.Rules { v := pluck(rule.Key, o) if v != nil && rule.Val != nil { typeV := reflect.TypeOf(v) typeR := reflect.TypeOf(rule.Val) if !typeV.Comparable() || !typeR.Comparable() { return false, nil, nil } } values[rule.ID] = v flag := rule.fit(v) results[rule.ID] = flag if !flag { // fit false, record msg, for no logic expression usage tips[rule.ID] = rule.Msg } allRuleIDs = append(allRuleIDs, rule.ID) } // compute result by considering logic if !hasLogic { for _, flag := range results { if !flag { return false, tips, values } } return true, rs.getTipsByRuleIDs(allRuleIDs), values } answer, ruleIDs, err := rs.calculateExpressionByTree(results) // tree can return fail reasons in fact tips = rs.getTipsByRuleIDs(ruleIDs) if err != nil { return false, nil, values } return answer, tips, values } func (rs *Rules) getTipsByRuleIDs(ids []int) map[int]string { var tips = make(map[int]string) var allTips = make(map[int]string) for _, rule := range rs.Rules { allTips[rule.ID] = rule.Msg } for _, id := range ids { tips[id] = allTips[id] } return tips } func (r *Rule) fit(v interface{}) bool { if check, err := r.checkSemver(v); err == nil { return check } if check, err := r.checkIP(v); err == nil { return check } op := r.Op // judge if need convert to uniform type var ok bool // index-0 actual, index-1 expect var pairStr = make([]string, 2) var pairNum = make([]float64, 2) var isStr, isNum, isObjStr, isRuleStr bool pairStr[0], ok = v.(string) if !ok { pairNum[0] = formatNumber(v) isStr = false isNum = true isObjStr = false } else { isStr = true isNum = false isObjStr = true } pairStr[1], ok = r.Val.(string) if !ok { pairNum[1] = formatNumber(r.Val) isStr = false isRuleStr = false } else { isNum = false isRuleStr = true } var flagOpIn bool // if in || nin if op == "@" || op == "in" || op == "!@" || op == "nin" || op == "<<" || op == "between" { flagOpIn = true if !isObjStr && isRuleStr { pairStr[0] = strconv.FormatFloat(pairNum[0], 'f', -1, 64) } } // if types different, ignore in & nin if !isStr && !isNum && !flagOpIn { return false } switch op { case "=", "eq": if isNum { return pairNum[0] == pairNum[1] } if isStr { return pairStr[0] == pairStr[1] } return false case ">", "gt": if isNum { return pairNum[0] > pairNum[1] } if isStr { return pairStr[0] > pairStr[1] } return false case "<", "lt": if isNum { return pairNum[0] < pairNum[1] } if isStr { return pairStr[0] < pairStr[1] } return false case ">=", "gte": if isNum { return pairNum[0] >= pairNum[1] } if isStr { return pairStr[0] >= pairStr[1] } return false case "<=", "lte": if isNum { return pairNum[0] <= pairNum[1] } if isStr { return pairStr[0] <= pairStr[1] } return false case "!=", "neq": if isNum { return pairNum[0] != pairNum[1] } if isStr { return pairStr[0] != pairStr[1] } return false case "@", "in": return isIn(pairStr[0], pairStr[1], !isObjStr) case "!@", "nin": return !isIn(pairStr[0], pairStr[1], !isObjStr) case "^$", "regex": return checkRegex(pairStr[1], pairStr[0]) case "0", "empty": return v == nil case "1", "nempty": return v != nil case "<<", "between": return isBetween(pairNum[0], pairStr[1]) case "@@", "intersect": return isIntersect(pairStr[1], pairStr[0]) default: return false } } func (r *Rule) checkIP(v interface{}) (bool, error) { if strings.HasSuffix(strings.ToLower(r.Key), "ip") { ver1, ok := r.Val.(string) if ok { ver2, ok := v.(string) if ok { if len(strings.Split(ver2, ".")) == 4 { return CheckIP(ver2, ver1), nil } } } } return false, errors.New("invalid ip") } func (r *Rule) checkSemver(v interface{}) (bool, error) { op := r.Op if strings.HasSuffix(strings.ToLower(r.Key), "version") { ver1, ok := r.Val.(string) if ok { validate := true ver2, ok := v.(string) if ok { /*v1len := len(strings.Split(ver1, ".")) v2len := len(strings.Split(ver2, ".")) if v1len > 4 || v2len > 4 { validate = false } else { if v1len != v2len { if v1len > v2len { for i:=0; i< v1len - v2len; i++ { //ver2 += ".0" } } else if v1len < v2len { for i:=0; i< v2len - v1len; i++ { //ver1 += ".0" } } } }*/ } else { validate = false } if validate { ver1 = repair(ver1) constraint, err := semver.NewConstraint(op + " " + ver1) if err == nil { ver2 = repair(ver2) version, err := semver.NewVersion(ver2) if err == nil { return constraint.Check(version), nil } } } } } return false, errors.New("invalid semver") } func repair(ver string) string { if len(strings.Split(ver, ".")) > 3 { //4位版本号,不含'-',转rc版本 if strings.Index(ver, "-") == -1 { versions := strings.Split(ver, ".") return versions[0] + "." + versions[1] + "." + versions[2] + "-rc." + versions[3] } } return ver } func pluck(key string, o map[string]interface{}) interface{} { if o == nil || key == EmptyStr { return nil } paths := strings.Split(key, ".") var ok bool for index, step := range paths { // last step is object key if index == len(paths)-1 { return o[step] } // explore deeper if o, ok = o[step].(map[string]interface{}); !ok { return nil } } return nil } func formatNumber(v interface{}) float64 { switch t := v.(type) { case uint: return float64(t) case uint8: return float64(t) case uint16: return float64(t) case uint32: return float64(t) case uint64: return float64(t) case int: return float64(t) case int8: return float64(t) case int16: return float64(t) case int32: return float64(t) case int64: return float64(t) case float32: return float64(t) case float64: return t default: return 0 } } func checkRegex(pattern, o string) bool { regex, err := regexp.Compile(pattern) if err != nil { return false } return regex.MatchString(o) } func formatLogicExpression(strRawExpr string) string { var flagPre, flagNow string strBracket := "bracket" strSpace := "space" strNotSpace := "notSpace" strOrigin := strings.ToLower(strRawExpr) runesPretty := make([]rune, 0) for _, c := range strOrigin { if c <= '9' && c >= '0' { flagNow = "num" } else if c <= 'z' && c >= 'a' { flagNow = "char" } else if c == '(' || c == ')' { flagNow = strBracket } else { flagNow = flagPre } if flagNow != flagPre || flagNow == strBracket && flagPre == strBracket { // should insert space here runesPretty = append(runesPretty, []rune(Space)[0]) } runesPretty = append(runesPretty, c) flagPre = flagNow } // remove redundant space flagPre = strNotSpace runesTrim := make([]rune, 0) for _, c := range runesPretty { if c == []rune(Space)[0] { flagNow = strSpace } else { flagNow = strNotSpace } if flagNow == strSpace && flagPre == strSpace { // continuous space continue } else { runesTrim = append(runesTrim, c) } flagPre = flagNow } strPrettyTrim := string(runesTrim) strPrettyTrim = strings.Trim(strPrettyTrim, Space) return strPrettyTrim } func isFormatLogicExpressionAllValidSymbol(strFormatLogic string) bool { listSymbol := strings.Split(strFormatLogic, Space) for _, symbol := range listSymbol { flag := false regex, err := regexp.Compile(PatternNumber) if err != nil { return false } if regex.MatchString(symbol) { // is number ok continue } for _, op := range ValidOperators { if op == symbol { // is operator ok flag = true } } for _, v := range []string{"(", ")"} { if v == symbol { // is bracket ok flag = true } } if !flag { return false } } return true } func isFormatLogicExpressio
, rules *Rules) bool { mapExistIds := make(map[string]bool) for _, eachRule := range rules.Rules { mapExistIds[strconv.Itoa(eachRule.ID)] = true } listSymbol := strings.Split(strFormatLogic, Space) regex, err := regexp.Compile(PatternNumber) if err != nil { return false } for _, symbol := range listSymbol { if regex.MatchString(symbol) { // is id, check it if _, ok := mapExistIds[symbol]; ok { continue } else { return false } } } return true } func tryToCalculateResultByFormatLogicExpressionWithRandomProbe(strFormatLogic string) error { listSymbol := strings.Split(strFormatLogic, Space) regex, err := regexp.Compile(PatternNumber) if err != nil { return err } // random probe mapProbe := make(map[int]bool) for _, symbol := range listSymbol { if regex.MatchString(symbol) { id, iErr := strconv.Atoi(symbol) if iErr != nil { return iErr } randomInt := rand.Intn(10) randomBool := randomInt < 5 mapProbe[id] = randomBool } } // calculate still use reverse_polish_notation r := &Rules{} _, err = r.calculateExpression(strFormatLogic, mapProbe) return err } func numOfOperandInLogic(op string) int8 { mapOperand := map[string]int8{"or": 2, "and": 2, "not": 1} return mapOperand[op] } func computeOneInLogic(op string, v []bool) (bool, error) { switch op { case "or": return v[0] || v[1], nil case "and": return v[0] && v[1], nil case "not": return !v[0], nil default: return false, errors.New("unrecognized op") } } func isIn(needle, haystack string, isNeedleNum bool) bool { // get number of needle var iNum float64 var err error if isNeedleNum { if iNum, err = strconv.ParseFloat(needle, 64); err != nil { return false } } // compatible to "1, 2, 3" and "1,2,3" li := strings.Split(haystack, ",") for _, o := range li { trimO := strings.TrimLeft(o, " ") if isNeedleNum { oNum, err := strconv.ParseFloat(trimO, 64) if err != nil { continue } if math.Abs(iNum-oNum) < 1e-5 { // 考虑浮点精度问题 return true } } else if needle == trimO { return true } } return false } func isIntersect(objStr string, ruleStr string) bool { // compatible to "1, 2, 3" and "1,2,3" vl := strings.Split(objStr, ",") li := strings.Split(ruleStr, ",") for _, o := range li { trimO := strings.Trim(o, " ") for _, v := range vl { trimV := strings.Trim(v, " ") if trimV == trimO { return true } } } return false } func isBetween(obj float64, scope string) bool { scope = strings.Trim(scope, " ") var equalLeft, equalRight bool // [] 双闭区间 result := regexp.MustCompile("^\\[ *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *]$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = true equalRight = true return calculateBetween(obj, result, equalLeft, equalRight) } // [) 左闭右开区间 result = regexp.MustCompile("^\\[ *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *\\)$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = true equalRight = false return calculateBetween(obj, result, equalLeft, equalRight) } // (] 左开右闭区间 result = regexp.MustCompile("^\\( *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *]$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = false equalRight = true return calculateBetween(obj, result, equalLeft, equalRight) } // () 双开区间 result = regexp.MustCompile("^\\( *(-?\\d*.?\\d*) *, *(-?\\d*.?\\d*) *\\)$").FindStringSubmatch(scope) if len(result) > 2 { equalLeft = false equalRight = false return calculateBetween(obj, result, equalLeft, equalRight) } return false } func calculateBetween(obj float64, result []string, equalLeft, equalRight bool) bool { var hasLeft, hasRight bool var left, right float64 var err error if result[1] != "" { hasLeft = true left, err = strconv.ParseFloat(result[1], 64) if err != nil { return false } } if result[2] != "" { hasRight = true right, err = strconv.ParseFloat(result[2], 64) if err != nil { return false } } // calculate if !hasLeft && !hasRight { return false } flag := true if hasLeft { if equalLeft { flag = flag && obj >= left } else { flag = flag && obj > left } } if hasRight { if equalRight { flag = flag && obj <= right } else { flag = flag && obj < right } } return flag }
nAllIdsExist(strFormatLogic string
identifier_name
main.rs
#![allow(clippy::float_cmp)] #![allow(clippy::inline_always)] #![allow(clippy::many_single_char_names)] #![allow(clippy::needless_lifetimes)] #![allow(clippy::needless_return)] #![allow(clippy::or_fun_call)] #![allow(clippy::too_many_arguments)] #![allow(clippy::redundant_field_names)] #![allow(clippy::enum_variant_names)] #![allow(clippy::cast_lossless)] #![allow(clippy::needless_range_loop)] #![allow(clippy::excessive_precision)] #![allow(clippy::transmute_ptr_to_ptr)] extern crate lazy_static; mod accel; mod algorithm; mod bbox; mod bbox4; mod boundable; mod camera; mod color; mod fp_utils; mod hash; mod hilbert; mod image; mod lerp; mod light; mod math; mod mis; mod parse; mod ray; mod renderer; mod sampling; mod scene; mod shading; mod surface; mod timer; mod tracer; mod transform_stack; use std::{fs::File, io, io::Read, mem, path::Path, str::FromStr}; use clap::{App, Arg}; use nom::bytes::complete::take_until; use kioku::Arena; use crate::{ accel::BVH4Node, bbox::BBox, parse::{parse_scene, DataTree}, renderer::LightPath, surface::SurfaceIntersection, timer::Timer, }; const VERSION: &str = env!("CARGO_PKG_VERSION"); #[allow(clippy::cognitive_complexity)] fn main() { let mut t = Timer::new(); // Parse command line arguments. let args = App::new("Psychopath") .version(VERSION) .about("A slightly psychotic path tracer") .arg( Arg::with_name("input") .short("i") .long("input") .value_name("FILE") .help("Input .psy file") .takes_value(true) .required_unless_one(&["dev", "use_stdin"]), ) .arg( Arg::with_name("spp") .short("s") .long("spp") .value_name("N") .help("Number of samples per pixel") .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("max_bucket_samples") .short("b") .long("spb") .value_name("N") .help("Target number of samples per bucket (determines bucket size)") .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("crop") .long("crop") .value_name("X1 Y1 X2 Y2") .help( "Only render the image between pixel coordinates (X1, Y1) \ and (X2, Y2). Coordinates are zero-indexed and inclusive.", ) .takes_value(true) .number_of_values(4) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be four integers".to_string())) }), ) .arg( Arg::with_name("threads") .short("t") .long("threads") .value_name("N") .help( "Number of threads to render with. Defaults to the number of logical \ cores on the system.", ) .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("stats") .long("stats") .help("Print additional statistics about rendering"), ) .arg( Arg::with_name("dev") .long("dev") .help("Show useful dev/debug info."), ) .arg( Arg::with_name("serialized_output") .long("serialized_output") .help("Serialize and send render output to standard output.") .hidden(true), ) .arg( Arg::with_name("use_stdin") .long("use_stdin") .help("Take scene file in from stdin instead of a file path.") .hidden(true), ) .get_matches(); // Print some misc useful dev info. if args.is_present("dev") { println!( "SurfaceIntersection size: {} bytes", mem::size_of::<SurfaceIntersection>() ); println!("LightPath size: {} bytes", mem::size_of::<LightPath>()); println!("BBox size: {} bytes", mem::size_of::<BBox>()); // println!("BVHNode size: {} bytes", mem::size_of::<BVHNode>()); println!("BVH4Node size: {} bytes", mem::size_of::<BVH4Node>()); return; } let crop = args.values_of("crop").map(|mut vals| { let coords = ( u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), ); if coords.0 > coords.2 { panic!("Argument '--crop': X1 must be less than or equal to X2"); } if coords.1 > coords.3 { panic!("Argument '--crop': Y1 must be less than or equal to Y2"); } coords }); // Parse data tree of scene file if !args.is_present("serialized_output") { println!("Parsing scene file...",); } t.tick(); let psy_contents = if args.is_present("use_stdin") { // Read from stdin let mut input = Vec::new(); let tmp = std::io::stdin(); let mut stdin = tmp.lock(); let mut buf = vec![0u8; 4096]; loop { let count = stdin .read(&mut buf) .expect("Unexpected end of scene input."); let start = if input.len() < 11 { 0 } else { input.len() - 11 }; let end = input.len() + count; input.extend(&buf[..count]); let mut done = false; let mut trunc_len = 0; if let nom::IResult::Ok((remaining, _)) = take_until::<&str, &[u8], ()>("__PSY_EOF__")(&input[start..end]) { done = true; trunc_len = input.len() - remaining.len(); } if done { input.truncate(trunc_len); break; } } String::from_utf8(input).unwrap() } else { // Read from file let mut input = String::new(); let fp = args.value_of("input").unwrap(); let mut f = io::BufReader::new(File::open(fp).unwrap()); let _ = f.read_to_string(&mut input); input }; let dt = DataTree::from_str(&psy_contents).unwrap(); if !args.is_present("serialized_output") { println!("\tParsed scene file in {:.3}s", t.tick()); } // Iterate through scenes and render them if let DataTree::Internal { ref children, .. } = dt { for child in children { t.tick(); if child.type_name() == "Scene" { if !args.is_present("serialized_output") { println!("Building scene..."); } let arena = Arena::new().with_block_size((1 << 20) * 4); let mut r = parse_scene(&arena, child).unwrap_or_else(|e| { e.print(&psy_contents); panic!("Parse error."); }); if let Some(spp) = args.value_of("spp") { if !args.is_present("serialized_output") { println!("\tOverriding scene spp: {}", spp); } r.spp = usize::from_str(spp).unwrap(); } let max_samples_per_bucket = if let Some(max_samples_per_bucket) = args.value_of("max_bucket_samples") { u32::from_str(max_samples_per_bucket).unwrap() } else { 4096 }; let thread_count = if let Some(threads) = args.value_of("threads") { u32::from_str(threads).unwrap() } else { num_cpus::get() as u32 }; if !args.is_present("serialized_output")
if !args.is_present("serialized_output") { println!("Rendering scene with {} threads...", thread_count); } let (mut image, rstats) = r.render( max_samples_per_bucket, crop, thread_count, args.is_present("serialized_output"), ); // Print render stats if !args.is_present("serialized_output") { let rtime = t.tick(); let ntime = rtime as f64 / rstats.total_time; println!("\tRendered scene in {:.3}s", rtime); println!( "\t\tTrace: {:.3}s", ntime * rstats.trace_time ); println!("\t\t\tRays traced: {}", rstats.ray_count); println!( "\t\t\tRays/sec: {}", (rstats.ray_count as f64 / (ntime * rstats.trace_time) as f64) as u64 ); println!("\t\t\tRay/node tests: {}", rstats.accel_node_visits); println!( "\t\tInitial ray generation: {:.3}s", ntime * rstats.initial_ray_generation_time ); println!( "\t\tRay generation: {:.3}s", ntime * rstats.ray_generation_time ); println!( "\t\tSample writing: {:.3}s", ntime * rstats.sample_writing_time ); } // Write to disk if !args.is_present("serialized_output") { println!("Writing image to disk into '{}'...", r.output_file); if r.output_file.ends_with(".png") { image .write_png(Path::new(&r.output_file)) .expect("Failed to write png..."); } else if r.output_file.ends_with(".exr") { image.write_exr(Path::new(&r.output_file)); } else { panic!("Unknown output file extension."); } println!("\tWrote image in {:.3}s", t.tick()); } // Print memory stats if stats are wanted. if args.is_present("stats") { // let arena_stats = arena.stats(); // let mib_occupied = arena_stats.0 as f64 / 1_048_576.0; // let mib_allocated = arena_stats.1 as f64 / 1_048_576.0; // println!("MemArena stats:"); // if mib_occupied >= 1.0 { // println!("\tOccupied: {:.1} MiB", mib_occupied); // } else { // println!("\tOccupied: {:.4} MiB", mib_occupied); // } // if mib_allocated >= 1.0 { // println!("\tUsed: {:.1} MiB", mib_allocated); // } else { // println!("\tUsed: {:.4} MiB", mib_allocated); // } // println!("\tTotal blocks: {}", arena_stats.2); } } } } // End with blank line println!(); }
{ println!("\tBuilt scene in {:.3}s", t.tick()); }
conditional_block
main.rs
#![allow(clippy::float_cmp)] #![allow(clippy::inline_always)] #![allow(clippy::many_single_char_names)] #![allow(clippy::needless_lifetimes)] #![allow(clippy::needless_return)] #![allow(clippy::or_fun_call)] #![allow(clippy::too_many_arguments)] #![allow(clippy::redundant_field_names)] #![allow(clippy::enum_variant_names)] #![allow(clippy::cast_lossless)] #![allow(clippy::needless_range_loop)] #![allow(clippy::excessive_precision)] #![allow(clippy::transmute_ptr_to_ptr)] extern crate lazy_static; mod accel; mod algorithm; mod bbox; mod bbox4; mod boundable; mod camera; mod color; mod fp_utils; mod hash; mod hilbert; mod image; mod lerp; mod light; mod math; mod mis; mod parse; mod ray; mod renderer; mod sampling; mod scene; mod shading; mod surface; mod timer; mod tracer; mod transform_stack; use std::{fs::File, io, io::Read, mem, path::Path, str::FromStr}; use clap::{App, Arg}; use nom::bytes::complete::take_until; use kioku::Arena; use crate::{ accel::BVH4Node, bbox::BBox, parse::{parse_scene, DataTree}, renderer::LightPath, surface::SurfaceIntersection, timer::Timer, }; const VERSION: &str = env!("CARGO_PKG_VERSION"); #[allow(clippy::cognitive_complexity)] fn main()
{ let mut t = Timer::new(); // Parse command line arguments. let args = App::new("Psychopath") .version(VERSION) .about("A slightly psychotic path tracer") .arg( Arg::with_name("input") .short("i") .long("input") .value_name("FILE") .help("Input .psy file") .takes_value(true) .required_unless_one(&["dev", "use_stdin"]), ) .arg( Arg::with_name("spp") .short("s") .long("spp") .value_name("N") .help("Number of samples per pixel") .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("max_bucket_samples") .short("b") .long("spb") .value_name("N") .help("Target number of samples per bucket (determines bucket size)") .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("crop") .long("crop") .value_name("X1 Y1 X2 Y2") .help( "Only render the image between pixel coordinates (X1, Y1) \ and (X2, Y2). Coordinates are zero-indexed and inclusive.", ) .takes_value(true) .number_of_values(4) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be four integers".to_string())) }), ) .arg( Arg::with_name("threads") .short("t") .long("threads") .value_name("N") .help( "Number of threads to render with. Defaults to the number of logical \ cores on the system.", ) .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("stats") .long("stats") .help("Print additional statistics about rendering"), ) .arg( Arg::with_name("dev") .long("dev") .help("Show useful dev/debug info."), ) .arg( Arg::with_name("serialized_output") .long("serialized_output") .help("Serialize and send render output to standard output.") .hidden(true), ) .arg( Arg::with_name("use_stdin") .long("use_stdin") .help("Take scene file in from stdin instead of a file path.") .hidden(true), ) .get_matches(); // Print some misc useful dev info. if args.is_present("dev") { println!( "SurfaceIntersection size: {} bytes", mem::size_of::<SurfaceIntersection>() ); println!("LightPath size: {} bytes", mem::size_of::<LightPath>()); println!("BBox size: {} bytes", mem::size_of::<BBox>()); // println!("BVHNode size: {} bytes", mem::size_of::<BVHNode>()); println!("BVH4Node size: {} bytes", mem::size_of::<BVH4Node>()); return; } let crop = args.values_of("crop").map(|mut vals| { let coords = ( u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), ); if coords.0 > coords.2 { panic!("Argument '--crop': X1 must be less than or equal to X2"); } if coords.1 > coords.3 { panic!("Argument '--crop': Y1 must be less than or equal to Y2"); } coords }); // Parse data tree of scene file if !args.is_present("serialized_output") { println!("Parsing scene file...",); } t.tick(); let psy_contents = if args.is_present("use_stdin") { // Read from stdin let mut input = Vec::new(); let tmp = std::io::stdin(); let mut stdin = tmp.lock(); let mut buf = vec![0u8; 4096]; loop { let count = stdin .read(&mut buf) .expect("Unexpected end of scene input."); let start = if input.len() < 11 { 0 } else { input.len() - 11 }; let end = input.len() + count; input.extend(&buf[..count]); let mut done = false; let mut trunc_len = 0; if let nom::IResult::Ok((remaining, _)) = take_until::<&str, &[u8], ()>("__PSY_EOF__")(&input[start..end]) { done = true; trunc_len = input.len() - remaining.len(); } if done { input.truncate(trunc_len); break; } } String::from_utf8(input).unwrap() } else { // Read from file let mut input = String::new(); let fp = args.value_of("input").unwrap(); let mut f = io::BufReader::new(File::open(fp).unwrap()); let _ = f.read_to_string(&mut input); input }; let dt = DataTree::from_str(&psy_contents).unwrap(); if !args.is_present("serialized_output") { println!("\tParsed scene file in {:.3}s", t.tick()); } // Iterate through scenes and render them if let DataTree::Internal { ref children, .. } = dt { for child in children { t.tick(); if child.type_name() == "Scene" { if !args.is_present("serialized_output") { println!("Building scene..."); } let arena = Arena::new().with_block_size((1 << 20) * 4); let mut r = parse_scene(&arena, child).unwrap_or_else(|e| { e.print(&psy_contents); panic!("Parse error."); }); if let Some(spp) = args.value_of("spp") { if !args.is_present("serialized_output") { println!("\tOverriding scene spp: {}", spp); } r.spp = usize::from_str(spp).unwrap(); } let max_samples_per_bucket = if let Some(max_samples_per_bucket) = args.value_of("max_bucket_samples") { u32::from_str(max_samples_per_bucket).unwrap() } else { 4096 }; let thread_count = if let Some(threads) = args.value_of("threads") { u32::from_str(threads).unwrap() } else { num_cpus::get() as u32 }; if !args.is_present("serialized_output") { println!("\tBuilt scene in {:.3}s", t.tick()); } if !args.is_present("serialized_output") { println!("Rendering scene with {} threads...", thread_count); } let (mut image, rstats) = r.render( max_samples_per_bucket, crop, thread_count, args.is_present("serialized_output"), ); // Print render stats if !args.is_present("serialized_output") { let rtime = t.tick(); let ntime = rtime as f64 / rstats.total_time; println!("\tRendered scene in {:.3}s", rtime); println!( "\t\tTrace: {:.3}s", ntime * rstats.trace_time ); println!("\t\t\tRays traced: {}", rstats.ray_count); println!( "\t\t\tRays/sec: {}", (rstats.ray_count as f64 / (ntime * rstats.trace_time) as f64) as u64 ); println!("\t\t\tRay/node tests: {}", rstats.accel_node_visits); println!( "\t\tInitial ray generation: {:.3}s", ntime * rstats.initial_ray_generation_time ); println!( "\t\tRay generation: {:.3}s", ntime * rstats.ray_generation_time ); println!( "\t\tSample writing: {:.3}s", ntime * rstats.sample_writing_time ); } // Write to disk if !args.is_present("serialized_output") { println!("Writing image to disk into '{}'...", r.output_file); if r.output_file.ends_with(".png") { image .write_png(Path::new(&r.output_file)) .expect("Failed to write png..."); } else if r.output_file.ends_with(".exr") { image.write_exr(Path::new(&r.output_file)); } else { panic!("Unknown output file extension."); } println!("\tWrote image in {:.3}s", t.tick()); } // Print memory stats if stats are wanted. if args.is_present("stats") { // let arena_stats = arena.stats(); // let mib_occupied = arena_stats.0 as f64 / 1_048_576.0; // let mib_allocated = arena_stats.1 as f64 / 1_048_576.0; // println!("MemArena stats:"); // if mib_occupied >= 1.0 { // println!("\tOccupied: {:.1} MiB", mib_occupied); // } else { // println!("\tOccupied: {:.4} MiB", mib_occupied); // } // if mib_allocated >= 1.0 { // println!("\tUsed: {:.1} MiB", mib_allocated); // } else { // println!("\tUsed: {:.4} MiB", mib_allocated); // } // println!("\tTotal blocks: {}", arena_stats.2); } } } } // End with blank line println!(); }
identifier_body
main.rs
#![allow(clippy::float_cmp)] #![allow(clippy::inline_always)] #![allow(clippy::many_single_char_names)] #![allow(clippy::needless_lifetimes)] #![allow(clippy::needless_return)] #![allow(clippy::or_fun_call)] #![allow(clippy::too_many_arguments)] #![allow(clippy::redundant_field_names)] #![allow(clippy::enum_variant_names)] #![allow(clippy::cast_lossless)] #![allow(clippy::needless_range_loop)] #![allow(clippy::excessive_precision)] #![allow(clippy::transmute_ptr_to_ptr)] extern crate lazy_static; mod accel; mod algorithm; mod bbox; mod bbox4; mod boundable; mod camera; mod color; mod fp_utils; mod hash; mod hilbert; mod image; mod lerp; mod light; mod math; mod mis; mod parse; mod ray; mod renderer; mod sampling; mod scene; mod shading; mod surface; mod timer; mod tracer; mod transform_stack; use std::{fs::File, io, io::Read, mem, path::Path, str::FromStr}; use clap::{App, Arg}; use nom::bytes::complete::take_until; use kioku::Arena; use crate::{ accel::BVH4Node, bbox::BBox, parse::{parse_scene, DataTree}, renderer::LightPath, surface::SurfaceIntersection, timer::Timer, }; const VERSION: &str = env!("CARGO_PKG_VERSION"); #[allow(clippy::cognitive_complexity)] fn
() { let mut t = Timer::new(); // Parse command line arguments. let args = App::new("Psychopath") .version(VERSION) .about("A slightly psychotic path tracer") .arg( Arg::with_name("input") .short("i") .long("input") .value_name("FILE") .help("Input .psy file") .takes_value(true) .required_unless_one(&["dev", "use_stdin"]), ) .arg( Arg::with_name("spp") .short("s") .long("spp") .value_name("N") .help("Number of samples per pixel") .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("max_bucket_samples") .short("b") .long("spb") .value_name("N") .help("Target number of samples per bucket (determines bucket size)") .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("crop") .long("crop") .value_name("X1 Y1 X2 Y2") .help( "Only render the image between pixel coordinates (X1, Y1) \ and (X2, Y2). Coordinates are zero-indexed and inclusive.", ) .takes_value(true) .number_of_values(4) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be four integers".to_string())) }), ) .arg( Arg::with_name("threads") .short("t") .long("threads") .value_name("N") .help( "Number of threads to render with. Defaults to the number of logical \ cores on the system.", ) .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("stats") .long("stats") .help("Print additional statistics about rendering"), ) .arg( Arg::with_name("dev") .long("dev") .help("Show useful dev/debug info."), ) .arg( Arg::with_name("serialized_output") .long("serialized_output") .help("Serialize and send render output to standard output.") .hidden(true), ) .arg( Arg::with_name("use_stdin") .long("use_stdin") .help("Take scene file in from stdin instead of a file path.") .hidden(true), ) .get_matches(); // Print some misc useful dev info. if args.is_present("dev") { println!( "SurfaceIntersection size: {} bytes", mem::size_of::<SurfaceIntersection>() ); println!("LightPath size: {} bytes", mem::size_of::<LightPath>()); println!("BBox size: {} bytes", mem::size_of::<BBox>()); // println!("BVHNode size: {} bytes", mem::size_of::<BVHNode>()); println!("BVH4Node size: {} bytes", mem::size_of::<BVH4Node>()); return; } let crop = args.values_of("crop").map(|mut vals| { let coords = ( u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), ); if coords.0 > coords.2 { panic!("Argument '--crop': X1 must be less than or equal to X2"); } if coords.1 > coords.3 { panic!("Argument '--crop': Y1 must be less than or equal to Y2"); } coords }); // Parse data tree of scene file if !args.is_present("serialized_output") { println!("Parsing scene file...",); } t.tick(); let psy_contents = if args.is_present("use_stdin") { // Read from stdin let mut input = Vec::new(); let tmp = std::io::stdin(); let mut stdin = tmp.lock(); let mut buf = vec![0u8; 4096]; loop { let count = stdin .read(&mut buf) .expect("Unexpected end of scene input."); let start = if input.len() < 11 { 0 } else { input.len() - 11 }; let end = input.len() + count; input.extend(&buf[..count]); let mut done = false; let mut trunc_len = 0; if let nom::IResult::Ok((remaining, _)) = take_until::<&str, &[u8], ()>("__PSY_EOF__")(&input[start..end]) { done = true; trunc_len = input.len() - remaining.len(); } if done { input.truncate(trunc_len); break; } } String::from_utf8(input).unwrap() } else { // Read from file let mut input = String::new(); let fp = args.value_of("input").unwrap(); let mut f = io::BufReader::new(File::open(fp).unwrap()); let _ = f.read_to_string(&mut input); input }; let dt = DataTree::from_str(&psy_contents).unwrap(); if !args.is_present("serialized_output") { println!("\tParsed scene file in {:.3}s", t.tick()); } // Iterate through scenes and render them if let DataTree::Internal { ref children, .. } = dt { for child in children { t.tick(); if child.type_name() == "Scene" { if !args.is_present("serialized_output") { println!("Building scene..."); } let arena = Arena::new().with_block_size((1 << 20) * 4); let mut r = parse_scene(&arena, child).unwrap_or_else(|e| { e.print(&psy_contents); panic!("Parse error."); }); if let Some(spp) = args.value_of("spp") { if !args.is_present("serialized_output") { println!("\tOverriding scene spp: {}", spp); } r.spp = usize::from_str(spp).unwrap(); } let max_samples_per_bucket = if let Some(max_samples_per_bucket) = args.value_of("max_bucket_samples") { u32::from_str(max_samples_per_bucket).unwrap() } else { 4096 }; let thread_count = if let Some(threads) = args.value_of("threads") { u32::from_str(threads).unwrap() } else { num_cpus::get() as u32 }; if !args.is_present("serialized_output") { println!("\tBuilt scene in {:.3}s", t.tick()); } if !args.is_present("serialized_output") { println!("Rendering scene with {} threads...", thread_count); } let (mut image, rstats) = r.render( max_samples_per_bucket, crop, thread_count, args.is_present("serialized_output"), ); // Print render stats if !args.is_present("serialized_output") { let rtime = t.tick(); let ntime = rtime as f64 / rstats.total_time; println!("\tRendered scene in {:.3}s", rtime); println!( "\t\tTrace: {:.3}s", ntime * rstats.trace_time ); println!("\t\t\tRays traced: {}", rstats.ray_count); println!( "\t\t\tRays/sec: {}", (rstats.ray_count as f64 / (ntime * rstats.trace_time) as f64) as u64 ); println!("\t\t\tRay/node tests: {}", rstats.accel_node_visits); println!( "\t\tInitial ray generation: {:.3}s", ntime * rstats.initial_ray_generation_time ); println!( "\t\tRay generation: {:.3}s", ntime * rstats.ray_generation_time ); println!( "\t\tSample writing: {:.3}s", ntime * rstats.sample_writing_time ); } // Write to disk if !args.is_present("serialized_output") { println!("Writing image to disk into '{}'...", r.output_file); if r.output_file.ends_with(".png") { image .write_png(Path::new(&r.output_file)) .expect("Failed to write png..."); } else if r.output_file.ends_with(".exr") { image.write_exr(Path::new(&r.output_file)); } else { panic!("Unknown output file extension."); } println!("\tWrote image in {:.3}s", t.tick()); } // Print memory stats if stats are wanted. if args.is_present("stats") { // let arena_stats = arena.stats(); // let mib_occupied = arena_stats.0 as f64 / 1_048_576.0; // let mib_allocated = arena_stats.1 as f64 / 1_048_576.0; // println!("MemArena stats:"); // if mib_occupied >= 1.0 { // println!("\tOccupied: {:.1} MiB", mib_occupied); // } else { // println!("\tOccupied: {:.4} MiB", mib_occupied); // } // if mib_allocated >= 1.0 { // println!("\tUsed: {:.1} MiB", mib_allocated); // } else { // println!("\tUsed: {:.4} MiB", mib_allocated); // } // println!("\tTotal blocks: {}", arena_stats.2); } } } } // End with blank line println!(); }
main
identifier_name
main.rs
#![allow(clippy::float_cmp)] #![allow(clippy::inline_always)] #![allow(clippy::many_single_char_names)] #![allow(clippy::needless_lifetimes)] #![allow(clippy::needless_return)] #![allow(clippy::or_fun_call)] #![allow(clippy::too_many_arguments)] #![allow(clippy::redundant_field_names)] #![allow(clippy::enum_variant_names)] #![allow(clippy::cast_lossless)] #![allow(clippy::needless_range_loop)] #![allow(clippy::excessive_precision)] #![allow(clippy::transmute_ptr_to_ptr)] extern crate lazy_static; mod accel; mod algorithm; mod bbox; mod bbox4; mod boundable; mod camera; mod color; mod fp_utils; mod hash; mod hilbert; mod image; mod lerp; mod light; mod math; mod mis; mod parse; mod ray; mod renderer; mod sampling; mod scene; mod shading; mod surface; mod timer; mod tracer; mod transform_stack; use std::{fs::File, io, io::Read, mem, path::Path, str::FromStr}; use clap::{App, Arg}; use nom::bytes::complete::take_until; use kioku::Arena; use crate::{ accel::BVH4Node, bbox::BBox, parse::{parse_scene, DataTree}, renderer::LightPath, surface::SurfaceIntersection, timer::Timer, }; const VERSION: &str = env!("CARGO_PKG_VERSION"); #[allow(clippy::cognitive_complexity)] fn main() { let mut t = Timer::new(); // Parse command line arguments. let args = App::new("Psychopath") .version(VERSION) .about("A slightly psychotic path tracer") .arg( Arg::with_name("input") .short("i") .long("input") .value_name("FILE") .help("Input .psy file") .takes_value(true) .required_unless_one(&["dev", "use_stdin"]), ) .arg( Arg::with_name("spp") .short("s") .long("spp") .value_name("N") .help("Number of samples per pixel") .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("max_bucket_samples") .short("b") .long("spb") .value_name("N") .help("Target number of samples per bucket (determines bucket size)") .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("crop") .long("crop") .value_name("X1 Y1 X2 Y2") .help( "Only render the image between pixel coordinates (X1, Y1) \ and (X2, Y2). Coordinates are zero-indexed and inclusive.", ) .takes_value(true) .number_of_values(4) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be four integers".to_string())) }), ) .arg( Arg::with_name("threads") .short("t") .long("threads") .value_name("N") .help( "Number of threads to render with. Defaults to the number of logical \ cores on the system.", ) .takes_value(true) .validator(|s| { usize::from_str(&s) .and(Ok(())) .or(Err("must be an integer".to_string())) }), ) .arg( Arg::with_name("stats") .long("stats") .help("Print additional statistics about rendering"), ) .arg( Arg::with_name("dev") .long("dev") .help("Show useful dev/debug info."), ) .arg( Arg::with_name("serialized_output") .long("serialized_output") .help("Serialize and send render output to standard output.") .hidden(true), ) .arg( Arg::with_name("use_stdin") .long("use_stdin") .help("Take scene file in from stdin instead of a file path.") .hidden(true), ) .get_matches(); // Print some misc useful dev info. if args.is_present("dev") { println!( "SurfaceIntersection size: {} bytes", mem::size_of::<SurfaceIntersection>() ); println!("LightPath size: {} bytes", mem::size_of::<LightPath>()); println!("BBox size: {} bytes", mem::size_of::<BBox>()); // println!("BVHNode size: {} bytes", mem::size_of::<BVHNode>()); println!("BVH4Node size: {} bytes", mem::size_of::<BVH4Node>()); return; } let crop = args.values_of("crop").map(|mut vals| { let coords = ( u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), u32::from_str(vals.next().unwrap()).unwrap(), ); if coords.0 > coords.2 { panic!("Argument '--crop': X1 must be less than or equal to X2"); } if coords.1 > coords.3 { panic!("Argument '--crop': Y1 must be less than or equal to Y2"); } coords }); // Parse data tree of scene file if !args.is_present("serialized_output") { println!("Parsing scene file...",); } t.tick(); let psy_contents = if args.is_present("use_stdin") { // Read from stdin let mut input = Vec::new(); let tmp = std::io::stdin(); let mut stdin = tmp.lock(); let mut buf = vec![0u8; 4096]; loop { let count = stdin .read(&mut buf) .expect("Unexpected end of scene input."); let start = if input.len() < 11 { 0 } else { input.len() - 11 }; let end = input.len() + count; input.extend(&buf[..count]); let mut done = false; let mut trunc_len = 0; if let nom::IResult::Ok((remaining, _)) = take_until::<&str, &[u8], ()>("__PSY_EOF__")(&input[start..end]) { done = true; trunc_len = input.len() - remaining.len(); } if done { input.truncate(trunc_len); break; } } String::from_utf8(input).unwrap() } else { // Read from file let mut input = String::new(); let fp = args.value_of("input").unwrap(); let mut f = io::BufReader::new(File::open(fp).unwrap()); let _ = f.read_to_string(&mut input); input }; let dt = DataTree::from_str(&psy_contents).unwrap(); if !args.is_present("serialized_output") { println!("\tParsed scene file in {:.3}s", t.tick()); } // Iterate through scenes and render them if let DataTree::Internal { ref children, .. } = dt { for child in children { t.tick(); if child.type_name() == "Scene" {
let arena = Arena::new().with_block_size((1 << 20) * 4); let mut r = parse_scene(&arena, child).unwrap_or_else(|e| { e.print(&psy_contents); panic!("Parse error."); }); if let Some(spp) = args.value_of("spp") { if !args.is_present("serialized_output") { println!("\tOverriding scene spp: {}", spp); } r.spp = usize::from_str(spp).unwrap(); } let max_samples_per_bucket = if let Some(max_samples_per_bucket) = args.value_of("max_bucket_samples") { u32::from_str(max_samples_per_bucket).unwrap() } else { 4096 }; let thread_count = if let Some(threads) = args.value_of("threads") { u32::from_str(threads).unwrap() } else { num_cpus::get() as u32 }; if !args.is_present("serialized_output") { println!("\tBuilt scene in {:.3}s", t.tick()); } if !args.is_present("serialized_output") { println!("Rendering scene with {} threads...", thread_count); } let (mut image, rstats) = r.render( max_samples_per_bucket, crop, thread_count, args.is_present("serialized_output"), ); // Print render stats if !args.is_present("serialized_output") { let rtime = t.tick(); let ntime = rtime as f64 / rstats.total_time; println!("\tRendered scene in {:.3}s", rtime); println!( "\t\tTrace: {:.3}s", ntime * rstats.trace_time ); println!("\t\t\tRays traced: {}", rstats.ray_count); println!( "\t\t\tRays/sec: {}", (rstats.ray_count as f64 / (ntime * rstats.trace_time) as f64) as u64 ); println!("\t\t\tRay/node tests: {}", rstats.accel_node_visits); println!( "\t\tInitial ray generation: {:.3}s", ntime * rstats.initial_ray_generation_time ); println!( "\t\tRay generation: {:.3}s", ntime * rstats.ray_generation_time ); println!( "\t\tSample writing: {:.3}s", ntime * rstats.sample_writing_time ); } // Write to disk if !args.is_present("serialized_output") { println!("Writing image to disk into '{}'...", r.output_file); if r.output_file.ends_with(".png") { image .write_png(Path::new(&r.output_file)) .expect("Failed to write png..."); } else if r.output_file.ends_with(".exr") { image.write_exr(Path::new(&r.output_file)); } else { panic!("Unknown output file extension."); } println!("\tWrote image in {:.3}s", t.tick()); } // Print memory stats if stats are wanted. if args.is_present("stats") { // let arena_stats = arena.stats(); // let mib_occupied = arena_stats.0 as f64 / 1_048_576.0; // let mib_allocated = arena_stats.1 as f64 / 1_048_576.0; // println!("MemArena stats:"); // if mib_occupied >= 1.0 { // println!("\tOccupied: {:.1} MiB", mib_occupied); // } else { // println!("\tOccupied: {:.4} MiB", mib_occupied); // } // if mib_allocated >= 1.0 { // println!("\tUsed: {:.1} MiB", mib_allocated); // } else { // println!("\tUsed: {:.4} MiB", mib_allocated); // } // println!("\tTotal blocks: {}", arena_stats.2); } } } } // End with blank line println!(); }
if !args.is_present("serialized_output") { println!("Building scene..."); }
random_line_split
index.ts
/** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. */ import {mqtt5, auth, iot} from "aws-iot-device-sdk-v2"; import jquery = require("jquery"); import {once} from "events"; import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers"; import { CognitoIdentityCredentials } from "@aws-sdk/credential-provider-cognito-identity/dist-types/fromCognitoIdentity" // @ts-ignore import Settings = require('./settings'); import {toUtf8} from "@aws-sdk/util-utf8-browser"; const $: JQueryStatic = jquery; function log(msg: string) { let now = new Date(); $('#console').append(`<pre>${now.toString()}: ${msg}</pre>`); } // AWSCognitoCredentialOptions. The credentials options used to create AWSCongnitoCredentialProvider. interface AWSCognitoCredentialOptions { IdentityPoolId : string, Region: string } // AWSCognitoCredentialsProvider. The AWSCognitoCredentialsProvider implements AWS.CognitoIdentityCredentials. class AWSCognitoCredentialsProvider extends auth.CredentialsProvider{ private options: AWSCognitoCredentialOptions; private cachedCredentials? : CognitoIdentityCredentials; constructor(options: AWSCognitoCredentialOptions, expire_interval_in_ms? : number) { super(); this.options = options; setInterval(async ()=>{ await this.refreshCredentials(); },expire_interval_in_ms?? 3600*1000); } getCredentials() : auth.AWSCredentials { return { aws_access_id: this.cachedCredentials?.accessKeyId ?? "", aws_secret_key: this.cachedCredentials?.secretAccessKey ?? "", aws_sts_token: this.cachedCredentials?.sessionToken, aws_region: this.options.Region } } async refreshCredentials() { log('Fetching Cognito credentials'); this.cachedCredentials = await fromCognitoIdentityPool({ // Required. The unique identifier for the identity pool from which an identity should be // retrieved or generated. identityPoolId: this.options.IdentityPoolId, clientConfig: { region: this.options.Region }, })(); } } // For the purposes of this sample, we need to associate certain variables with a particular MQTT5 client // and to do so we use this class to hold all the data for a particular client used in the sample. class SampleMqtt5Client { client? : mqtt5.Mqtt5Client; name? : string; // Sets up the MQTT5 sample client using direct MQTT5 via mTLS with the passed input data. public setupMqtt5Client( provider: AWSCognitoCredentialsProvider, input_endpoint : string, input_region: string, input_clientId : string, input_clientName : string) { this.name = input_clientName; let wsConfig : iot.WebsocketSigv4Config = { credentialsProvider: provider, region: input_region } let builder: iot.AwsIotMqtt5ClientConfigBuilder = iot.AwsIotMqtt5ClientConfigBuilder.newWebsocketMqttBuilderWithSigv4Auth( input_endpoint, wsConfig ) builder.withConnectProperties({ clientId: input_clientId, keepAliveIntervalSeconds: 120 }) this.client = new mqtt5.Mqtt5Client(builder.build()); // Invoked when the client has an error this.client.on('error', (error) => { log("[" + this.name + "] Error: " + error.toString()); }); // Invoked when the client gets a message/publish on a subscribed topic this.client.on("messageReceived",(eventData: mqtt5.MessageReceivedEvent) : void => { log("[" + this.name + "]: Received a publish"); if (eventData.message.topicName) { log("\tPublish received on topic: " + eventData.message.topicName); } if (eventData.message.payload) { log("\tMessage: " + toUtf8(new Uint8Array(eventData.message.payload as ArrayBuffer))); } }); // Invoked when the client connects successfully to the endpoint this.client.on('connectionSuccess', (eventData: mqtt5.ConnectionSuccessEvent) => { log("[" + this.name + "]: Connection success"); }); // Invoked when the client fails to connect to the endpoint this.client.on('connectionFailure', (eventData: mqtt5.ConnectionFailureEvent) => { log("[" + this.name + "]: Connection failed with error: " + eventData.error.toString()); }); // Invoked when the client becomes disconnected this.client.on('disconnection', (eventData: mqtt5.DisconnectionEvent) => { log("[" + this.name + "]: Disconnected"); if (eventData.disconnect) { if (eventData.disconnect.reasonCode == mqtt5.DisconnectReasonCode.SharedSubscriptionsNotSupported)
} }); // Invoked when the client stops this.client.on('stopped', (eventData: mqtt5.StoppedEvent) => { log("[" + this.name + "]: Stopped"); }); } // Helper function to make sample code a little cleaner public async startClient() { const connectionSuccess = once(this.client as mqtt5.Mqtt5Client, "connectionSuccess"); this.client?.start(); await connectionSuccess; } // Helper function to make sample code a little cleaner public async stopClient() { const stopped = once(this.client as mqtt5.Mqtt5Client, "stopped"); this.client?.stop(); await stopped; } } async function runSample() { // Pull data from the command line let input_endpoint : string = Settings.AWS_IOT_ENDPOINT; let input_region : string = Settings.AWS_REGION; let input_clientId : string = Settings.INPUT_CLIENT_ID; let input_topic : string = Settings.INPUT_TOPIC; let input_count : number = Settings.INPUT_COUNT; let input_message : string = Settings.INPUT_MESSAGE; let input_groupIdentifier : string = Settings.INPUT_GROUP_IDENTIFIER; let input_cognitoIdentityPoolId = Settings.AWS_COGNITO_IDENTITY_POOL_ID; // Construct the shared topic let input_shared_topic : string = "$share/" + input_groupIdentifier + "/" + input_topic; /** Set up the credentialsProvider */ const provider = new AWSCognitoCredentialsProvider({ IdentityPoolId: input_cognitoIdentityPoolId, Region: input_region}); /** Make sure the credential provider fetched before setup the connection */ await provider.refreshCredentials(); // Create the MQTT5 clients: one publisher and two subscribers let publisher : SampleMqtt5Client = new SampleMqtt5Client() publisher.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "1", "Publisher"); let subscriber_one : SampleMqtt5Client = new SampleMqtt5Client() subscriber_one.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "2", "Subscriber One"); let subscriber_two : SampleMqtt5Client = new SampleMqtt5Client() subscriber_two.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "3", "Subscriber Two"); try { // Connect all the clients await publisher.startClient(); await subscriber_one.startClient(); await subscriber_two.startClient(); // Subscribe to the shared topic on the two subscribers await subscriber_one.client?.subscribe({subscriptions: [{qos: mqtt5.QoS.AtLeastOnce, topicFilter: input_shared_topic }]}); log("[" + subscriber_one.name + "]: Subscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_one.name + "]: Full subscribed topic is '" + input_shared_topic + "'."); await subscriber_two.client?.subscribe({subscriptions: [{qos: mqtt5.QoS.AtLeastOnce, topicFilter: input_shared_topic }]}); log("[" + subscriber_two.name + "]: Subscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_two.name + "]: Full subscribed topic is '" + input_shared_topic + "'."); // Publish using the publisher client let publishPacket : mqtt5.PublishPacket = { qos: mqtt5.QoS.AtLeastOnce, topicName: input_topic, payload: input_message }; if (input_count > 0) { let count = 0; while (count++ < input_count) { publishPacket.payload = input_message + ": " + count; await publisher.client?.publish(publishPacket); log("[" + publisher.name + "]: Published"); await new Promise(resolve => setTimeout(resolve, 1000)); } // Wait 5 seconds to let the last publish go out before unsubscribing. await new Promise(resolve => setTimeout(resolve, 5000)); } else { log("Skipping publishing messages due to message count being zero..."); } // Unsubscribe from the shared topic on the two subscribers await subscriber_one.client?.unsubscribe({topicFilters: [ input_shared_topic ]}); log("[" + subscriber_one.name + "]: Unsubscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_one.name + "]: Full unsubscribed topic is '" + input_shared_topic + "'."); await subscriber_two.client?.unsubscribe({topicFilters: [ input_shared_topic ]}); log("[" + subscriber_two.name + "]: Unsubscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_two.name + "]: Full unsubscribed topic is '" + input_shared_topic + "'."); // Disconnect all the clients await publisher.stopClient(); await subscriber_one.stopClient(); await subscriber_two.stopClient(); } finally { // Close all the clients publisher.client?.close(); subscriber_one.client?.close(); subscriber_two.client?.close(); log("Sample Complete!"); } } async function main(){ await runSample(); log('Leaving'); } $(document).ready(() => { main(); });
{ log( "[" + this.name + "]: Shared Subscriptions not supported!" + "\nThis sample will not work unless the endpoint being connected to has Shared Subscriptions support."); }
conditional_block
index.ts
/** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. */ import {mqtt5, auth, iot} from "aws-iot-device-sdk-v2"; import jquery = require("jquery"); import {once} from "events"; import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers"; import { CognitoIdentityCredentials } from "@aws-sdk/credential-provider-cognito-identity/dist-types/fromCognitoIdentity" // @ts-ignore import Settings = require('./settings'); import {toUtf8} from "@aws-sdk/util-utf8-browser"; const $: JQueryStatic = jquery; function log(msg: string)
// AWSCognitoCredentialOptions. The credentials options used to create AWSCongnitoCredentialProvider. interface AWSCognitoCredentialOptions { IdentityPoolId : string, Region: string } // AWSCognitoCredentialsProvider. The AWSCognitoCredentialsProvider implements AWS.CognitoIdentityCredentials. class AWSCognitoCredentialsProvider extends auth.CredentialsProvider{ private options: AWSCognitoCredentialOptions; private cachedCredentials? : CognitoIdentityCredentials; constructor(options: AWSCognitoCredentialOptions, expire_interval_in_ms? : number) { super(); this.options = options; setInterval(async ()=>{ await this.refreshCredentials(); },expire_interval_in_ms?? 3600*1000); } getCredentials() : auth.AWSCredentials { return { aws_access_id: this.cachedCredentials?.accessKeyId ?? "", aws_secret_key: this.cachedCredentials?.secretAccessKey ?? "", aws_sts_token: this.cachedCredentials?.sessionToken, aws_region: this.options.Region } } async refreshCredentials() { log('Fetching Cognito credentials'); this.cachedCredentials = await fromCognitoIdentityPool({ // Required. The unique identifier for the identity pool from which an identity should be // retrieved or generated. identityPoolId: this.options.IdentityPoolId, clientConfig: { region: this.options.Region }, })(); } } // For the purposes of this sample, we need to associate certain variables with a particular MQTT5 client // and to do so we use this class to hold all the data for a particular client used in the sample. class SampleMqtt5Client { client? : mqtt5.Mqtt5Client; name? : string; // Sets up the MQTT5 sample client using direct MQTT5 via mTLS with the passed input data. public setupMqtt5Client( provider: AWSCognitoCredentialsProvider, input_endpoint : string, input_region: string, input_clientId : string, input_clientName : string) { this.name = input_clientName; let wsConfig : iot.WebsocketSigv4Config = { credentialsProvider: provider, region: input_region } let builder: iot.AwsIotMqtt5ClientConfigBuilder = iot.AwsIotMqtt5ClientConfigBuilder.newWebsocketMqttBuilderWithSigv4Auth( input_endpoint, wsConfig ) builder.withConnectProperties({ clientId: input_clientId, keepAliveIntervalSeconds: 120 }) this.client = new mqtt5.Mqtt5Client(builder.build()); // Invoked when the client has an error this.client.on('error', (error) => { log("[" + this.name + "] Error: " + error.toString()); }); // Invoked when the client gets a message/publish on a subscribed topic this.client.on("messageReceived",(eventData: mqtt5.MessageReceivedEvent) : void => { log("[" + this.name + "]: Received a publish"); if (eventData.message.topicName) { log("\tPublish received on topic: " + eventData.message.topicName); } if (eventData.message.payload) { log("\tMessage: " + toUtf8(new Uint8Array(eventData.message.payload as ArrayBuffer))); } }); // Invoked when the client connects successfully to the endpoint this.client.on('connectionSuccess', (eventData: mqtt5.ConnectionSuccessEvent) => { log("[" + this.name + "]: Connection success"); }); // Invoked when the client fails to connect to the endpoint this.client.on('connectionFailure', (eventData: mqtt5.ConnectionFailureEvent) => { log("[" + this.name + "]: Connection failed with error: " + eventData.error.toString()); }); // Invoked when the client becomes disconnected this.client.on('disconnection', (eventData: mqtt5.DisconnectionEvent) => { log("[" + this.name + "]: Disconnected"); if (eventData.disconnect) { if (eventData.disconnect.reasonCode == mqtt5.DisconnectReasonCode.SharedSubscriptionsNotSupported) { log( "[" + this.name + "]: Shared Subscriptions not supported!" + "\nThis sample will not work unless the endpoint being connected to has Shared Subscriptions support."); } } }); // Invoked when the client stops this.client.on('stopped', (eventData: mqtt5.StoppedEvent) => { log("[" + this.name + "]: Stopped"); }); } // Helper function to make sample code a little cleaner public async startClient() { const connectionSuccess = once(this.client as mqtt5.Mqtt5Client, "connectionSuccess"); this.client?.start(); await connectionSuccess; } // Helper function to make sample code a little cleaner public async stopClient() { const stopped = once(this.client as mqtt5.Mqtt5Client, "stopped"); this.client?.stop(); await stopped; } } async function runSample() { // Pull data from the command line let input_endpoint : string = Settings.AWS_IOT_ENDPOINT; let input_region : string = Settings.AWS_REGION; let input_clientId : string = Settings.INPUT_CLIENT_ID; let input_topic : string = Settings.INPUT_TOPIC; let input_count : number = Settings.INPUT_COUNT; let input_message : string = Settings.INPUT_MESSAGE; let input_groupIdentifier : string = Settings.INPUT_GROUP_IDENTIFIER; let input_cognitoIdentityPoolId = Settings.AWS_COGNITO_IDENTITY_POOL_ID; // Construct the shared topic let input_shared_topic : string = "$share/" + input_groupIdentifier + "/" + input_topic; /** Set up the credentialsProvider */ const provider = new AWSCognitoCredentialsProvider({ IdentityPoolId: input_cognitoIdentityPoolId, Region: input_region}); /** Make sure the credential provider fetched before setup the connection */ await provider.refreshCredentials(); // Create the MQTT5 clients: one publisher and two subscribers let publisher : SampleMqtt5Client = new SampleMqtt5Client() publisher.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "1", "Publisher"); let subscriber_one : SampleMqtt5Client = new SampleMqtt5Client() subscriber_one.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "2", "Subscriber One"); let subscriber_two : SampleMqtt5Client = new SampleMqtt5Client() subscriber_two.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "3", "Subscriber Two"); try { // Connect all the clients await publisher.startClient(); await subscriber_one.startClient(); await subscriber_two.startClient(); // Subscribe to the shared topic on the two subscribers await subscriber_one.client?.subscribe({subscriptions: [{qos: mqtt5.QoS.AtLeastOnce, topicFilter: input_shared_topic }]}); log("[" + subscriber_one.name + "]: Subscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_one.name + "]: Full subscribed topic is '" + input_shared_topic + "'."); await subscriber_two.client?.subscribe({subscriptions: [{qos: mqtt5.QoS.AtLeastOnce, topicFilter: input_shared_topic }]}); log("[" + subscriber_two.name + "]: Subscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_two.name + "]: Full subscribed topic is '" + input_shared_topic + "'."); // Publish using the publisher client let publishPacket : mqtt5.PublishPacket = { qos: mqtt5.QoS.AtLeastOnce, topicName: input_topic, payload: input_message }; if (input_count > 0) { let count = 0; while (count++ < input_count) { publishPacket.payload = input_message + ": " + count; await publisher.client?.publish(publishPacket); log("[" + publisher.name + "]: Published"); await new Promise(resolve => setTimeout(resolve, 1000)); } // Wait 5 seconds to let the last publish go out before unsubscribing. await new Promise(resolve => setTimeout(resolve, 5000)); } else { log("Skipping publishing messages due to message count being zero..."); } // Unsubscribe from the shared topic on the two subscribers await subscriber_one.client?.unsubscribe({topicFilters: [ input_shared_topic ]}); log("[" + subscriber_one.name + "]: Unsubscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_one.name + "]: Full unsubscribed topic is '" + input_shared_topic + "'."); await subscriber_two.client?.unsubscribe({topicFilters: [ input_shared_topic ]}); log("[" + subscriber_two.name + "]: Unsubscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_two.name + "]: Full unsubscribed topic is '" + input_shared_topic + "'."); // Disconnect all the clients await publisher.stopClient(); await subscriber_one.stopClient(); await subscriber_two.stopClient(); } finally { // Close all the clients publisher.client?.close(); subscriber_one.client?.close(); subscriber_two.client?.close(); log("Sample Complete!"); } } async function main(){ await runSample(); log('Leaving'); } $(document).ready(() => { main(); });
{ let now = new Date(); $('#console').append(`<pre>${now.toString()}: ${msg}</pre>`); }
identifier_body
index.ts
/** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. */ import {mqtt5, auth, iot} from "aws-iot-device-sdk-v2"; import jquery = require("jquery"); import {once} from "events"; import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers"; import { CognitoIdentityCredentials } from "@aws-sdk/credential-provider-cognito-identity/dist-types/fromCognitoIdentity" // @ts-ignore import Settings = require('./settings'); import {toUtf8} from "@aws-sdk/util-utf8-browser"; const $: JQueryStatic = jquery; function log(msg: string) { let now = new Date(); $('#console').append(`<pre>${now.toString()}: ${msg}</pre>`); } // AWSCognitoCredentialOptions. The credentials options used to create AWSCongnitoCredentialProvider. interface AWSCognitoCredentialOptions { IdentityPoolId : string, Region: string } // AWSCognitoCredentialsProvider. The AWSCognitoCredentialsProvider implements AWS.CognitoIdentityCredentials. class AWSCognitoCredentialsProvider extends auth.CredentialsProvider{ private options: AWSCognitoCredentialOptions; private cachedCredentials? : CognitoIdentityCredentials; constructor(options: AWSCognitoCredentialOptions, expire_interval_in_ms? : number) { super(); this.options = options; setInterval(async ()=>{ await this.refreshCredentials(); },expire_interval_in_ms?? 3600*1000); } getCredentials() : auth.AWSCredentials { return { aws_access_id: this.cachedCredentials?.accessKeyId ?? "", aws_secret_key: this.cachedCredentials?.secretAccessKey ?? "", aws_sts_token: this.cachedCredentials?.sessionToken, aws_region: this.options.Region } } async refreshCredentials() { log('Fetching Cognito credentials'); this.cachedCredentials = await fromCognitoIdentityPool({ // Required. The unique identifier for the identity pool from which an identity should be // retrieved or generated. identityPoolId: this.options.IdentityPoolId, clientConfig: { region: this.options.Region }, })(); } } // For the purposes of this sample, we need to associate certain variables with a particular MQTT5 client // and to do so we use this class to hold all the data for a particular client used in the sample. class SampleMqtt5Client { client? : mqtt5.Mqtt5Client; name? : string; // Sets up the MQTT5 sample client using direct MQTT5 via mTLS with the passed input data. public setupMqtt5Client( provider: AWSCognitoCredentialsProvider, input_endpoint : string, input_region: string, input_clientId : string, input_clientName : string) { this.name = input_clientName; let wsConfig : iot.WebsocketSigv4Config = { credentialsProvider: provider, region: input_region } let builder: iot.AwsIotMqtt5ClientConfigBuilder = iot.AwsIotMqtt5ClientConfigBuilder.newWebsocketMqttBuilderWithSigv4Auth( input_endpoint, wsConfig ) builder.withConnectProperties({ clientId: input_clientId, keepAliveIntervalSeconds: 120 }) this.client = new mqtt5.Mqtt5Client(builder.build()); // Invoked when the client has an error this.client.on('error', (error) => { log("[" + this.name + "] Error: " + error.toString()); }); // Invoked when the client gets a message/publish on a subscribed topic this.client.on("messageReceived",(eventData: mqtt5.MessageReceivedEvent) : void => { log("[" + this.name + "]: Received a publish"); if (eventData.message.topicName) { log("\tPublish received on topic: " + eventData.message.topicName); } if (eventData.message.payload) { log("\tMessage: " + toUtf8(new Uint8Array(eventData.message.payload as ArrayBuffer))); } }); // Invoked when the client connects successfully to the endpoint this.client.on('connectionSuccess', (eventData: mqtt5.ConnectionSuccessEvent) => { log("[" + this.name + "]: Connection success"); }); // Invoked when the client fails to connect to the endpoint this.client.on('connectionFailure', (eventData: mqtt5.ConnectionFailureEvent) => { log("[" + this.name + "]: Connection failed with error: " + eventData.error.toString()); }); // Invoked when the client becomes disconnected this.client.on('disconnection', (eventData: mqtt5.DisconnectionEvent) => { log("[" + this.name + "]: Disconnected"); if (eventData.disconnect) { if (eventData.disconnect.reasonCode == mqtt5.DisconnectReasonCode.SharedSubscriptionsNotSupported) { log( "[" + this.name + "]: Shared Subscriptions not supported!" + "\nThis sample will not work unless the endpoint being connected to has Shared Subscriptions support."); } } }); // Invoked when the client stops this.client.on('stopped', (eventData: mqtt5.StoppedEvent) => { log("[" + this.name + "]: Stopped"); }); } // Helper function to make sample code a little cleaner public async startClient() { const connectionSuccess = once(this.client as mqtt5.Mqtt5Client, "connectionSuccess"); this.client?.start(); await connectionSuccess; } // Helper function to make sample code a little cleaner public async stopClient() { const stopped = once(this.client as mqtt5.Mqtt5Client, "stopped"); this.client?.stop(); await stopped; } } async function runSample() { // Pull data from the command line let input_endpoint : string = Settings.AWS_IOT_ENDPOINT; let input_region : string = Settings.AWS_REGION; let input_clientId : string = Settings.INPUT_CLIENT_ID; let input_topic : string = Settings.INPUT_TOPIC; let input_count : number = Settings.INPUT_COUNT; let input_message : string = Settings.INPUT_MESSAGE; let input_groupIdentifier : string = Settings.INPUT_GROUP_IDENTIFIER; let input_cognitoIdentityPoolId = Settings.AWS_COGNITO_IDENTITY_POOL_ID; // Construct the shared topic let input_shared_topic : string = "$share/" + input_groupIdentifier + "/" + input_topic; /** Set up the credentialsProvider */ const provider = new AWSCognitoCredentialsProvider({ IdentityPoolId: input_cognitoIdentityPoolId, Region: input_region}); /** Make sure the credential provider fetched before setup the connection */ await provider.refreshCredentials(); // Create the MQTT5 clients: one publisher and two subscribers let publisher : SampleMqtt5Client = new SampleMqtt5Client() publisher.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "1", "Publisher"); let subscriber_one : SampleMqtt5Client = new SampleMqtt5Client() subscriber_one.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "2", "Subscriber One"); let subscriber_two : SampleMqtt5Client = new SampleMqtt5Client() subscriber_two.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "3", "Subscriber Two");
try { // Connect all the clients await publisher.startClient(); await subscriber_one.startClient(); await subscriber_two.startClient(); // Subscribe to the shared topic on the two subscribers await subscriber_one.client?.subscribe({subscriptions: [{qos: mqtt5.QoS.AtLeastOnce, topicFilter: input_shared_topic }]}); log("[" + subscriber_one.name + "]: Subscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_one.name + "]: Full subscribed topic is '" + input_shared_topic + "'."); await subscriber_two.client?.subscribe({subscriptions: [{qos: mqtt5.QoS.AtLeastOnce, topicFilter: input_shared_topic }]}); log("[" + subscriber_two.name + "]: Subscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_two.name + "]: Full subscribed topic is '" + input_shared_topic + "'."); // Publish using the publisher client let publishPacket : mqtt5.PublishPacket = { qos: mqtt5.QoS.AtLeastOnce, topicName: input_topic, payload: input_message }; if (input_count > 0) { let count = 0; while (count++ < input_count) { publishPacket.payload = input_message + ": " + count; await publisher.client?.publish(publishPacket); log("[" + publisher.name + "]: Published"); await new Promise(resolve => setTimeout(resolve, 1000)); } // Wait 5 seconds to let the last publish go out before unsubscribing. await new Promise(resolve => setTimeout(resolve, 5000)); } else { log("Skipping publishing messages due to message count being zero..."); } // Unsubscribe from the shared topic on the two subscribers await subscriber_one.client?.unsubscribe({topicFilters: [ input_shared_topic ]}); log("[" + subscriber_one.name + "]: Unsubscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_one.name + "]: Full unsubscribed topic is '" + input_shared_topic + "'."); await subscriber_two.client?.unsubscribe({topicFilters: [ input_shared_topic ]}); log("[" + subscriber_two.name + "]: Unsubscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_two.name + "]: Full unsubscribed topic is '" + input_shared_topic + "'."); // Disconnect all the clients await publisher.stopClient(); await subscriber_one.stopClient(); await subscriber_two.stopClient(); } finally { // Close all the clients publisher.client?.close(); subscriber_one.client?.close(); subscriber_two.client?.close(); log("Sample Complete!"); } } async function main(){ await runSample(); log('Leaving'); } $(document).ready(() => { main(); });
random_line_split
index.ts
/** * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. */ import {mqtt5, auth, iot} from "aws-iot-device-sdk-v2"; import jquery = require("jquery"); import {once} from "events"; import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers"; import { CognitoIdentityCredentials } from "@aws-sdk/credential-provider-cognito-identity/dist-types/fromCognitoIdentity" // @ts-ignore import Settings = require('./settings'); import {toUtf8} from "@aws-sdk/util-utf8-browser"; const $: JQueryStatic = jquery; function log(msg: string) { let now = new Date(); $('#console').append(`<pre>${now.toString()}: ${msg}</pre>`); } // AWSCognitoCredentialOptions. The credentials options used to create AWSCongnitoCredentialProvider. interface AWSCognitoCredentialOptions { IdentityPoolId : string, Region: string } // AWSCognitoCredentialsProvider. The AWSCognitoCredentialsProvider implements AWS.CognitoIdentityCredentials. class AWSCognitoCredentialsProvider extends auth.CredentialsProvider{ private options: AWSCognitoCredentialOptions; private cachedCredentials? : CognitoIdentityCredentials; constructor(options: AWSCognitoCredentialOptions, expire_interval_in_ms? : number) { super(); this.options = options; setInterval(async ()=>{ await this.refreshCredentials(); },expire_interval_in_ms?? 3600*1000); } getCredentials() : auth.AWSCredentials { return { aws_access_id: this.cachedCredentials?.accessKeyId ?? "", aws_secret_key: this.cachedCredentials?.secretAccessKey ?? "", aws_sts_token: this.cachedCredentials?.sessionToken, aws_region: this.options.Region } } async refreshCredentials() { log('Fetching Cognito credentials'); this.cachedCredentials = await fromCognitoIdentityPool({ // Required. The unique identifier for the identity pool from which an identity should be // retrieved or generated. identityPoolId: this.options.IdentityPoolId, clientConfig: { region: this.options.Region }, })(); } } // For the purposes of this sample, we need to associate certain variables with a particular MQTT5 client // and to do so we use this class to hold all the data for a particular client used in the sample. class SampleMqtt5Client { client? : mqtt5.Mqtt5Client; name? : string; // Sets up the MQTT5 sample client using direct MQTT5 via mTLS with the passed input data. public
( provider: AWSCognitoCredentialsProvider, input_endpoint : string, input_region: string, input_clientId : string, input_clientName : string) { this.name = input_clientName; let wsConfig : iot.WebsocketSigv4Config = { credentialsProvider: provider, region: input_region } let builder: iot.AwsIotMqtt5ClientConfigBuilder = iot.AwsIotMqtt5ClientConfigBuilder.newWebsocketMqttBuilderWithSigv4Auth( input_endpoint, wsConfig ) builder.withConnectProperties({ clientId: input_clientId, keepAliveIntervalSeconds: 120 }) this.client = new mqtt5.Mqtt5Client(builder.build()); // Invoked when the client has an error this.client.on('error', (error) => { log("[" + this.name + "] Error: " + error.toString()); }); // Invoked when the client gets a message/publish on a subscribed topic this.client.on("messageReceived",(eventData: mqtt5.MessageReceivedEvent) : void => { log("[" + this.name + "]: Received a publish"); if (eventData.message.topicName) { log("\tPublish received on topic: " + eventData.message.topicName); } if (eventData.message.payload) { log("\tMessage: " + toUtf8(new Uint8Array(eventData.message.payload as ArrayBuffer))); } }); // Invoked when the client connects successfully to the endpoint this.client.on('connectionSuccess', (eventData: mqtt5.ConnectionSuccessEvent) => { log("[" + this.name + "]: Connection success"); }); // Invoked when the client fails to connect to the endpoint this.client.on('connectionFailure', (eventData: mqtt5.ConnectionFailureEvent) => { log("[" + this.name + "]: Connection failed with error: " + eventData.error.toString()); }); // Invoked when the client becomes disconnected this.client.on('disconnection', (eventData: mqtt5.DisconnectionEvent) => { log("[" + this.name + "]: Disconnected"); if (eventData.disconnect) { if (eventData.disconnect.reasonCode == mqtt5.DisconnectReasonCode.SharedSubscriptionsNotSupported) { log( "[" + this.name + "]: Shared Subscriptions not supported!" + "\nThis sample will not work unless the endpoint being connected to has Shared Subscriptions support."); } } }); // Invoked when the client stops this.client.on('stopped', (eventData: mqtt5.StoppedEvent) => { log("[" + this.name + "]: Stopped"); }); } // Helper function to make sample code a little cleaner public async startClient() { const connectionSuccess = once(this.client as mqtt5.Mqtt5Client, "connectionSuccess"); this.client?.start(); await connectionSuccess; } // Helper function to make sample code a little cleaner public async stopClient() { const stopped = once(this.client as mqtt5.Mqtt5Client, "stopped"); this.client?.stop(); await stopped; } } async function runSample() { // Pull data from the command line let input_endpoint : string = Settings.AWS_IOT_ENDPOINT; let input_region : string = Settings.AWS_REGION; let input_clientId : string = Settings.INPUT_CLIENT_ID; let input_topic : string = Settings.INPUT_TOPIC; let input_count : number = Settings.INPUT_COUNT; let input_message : string = Settings.INPUT_MESSAGE; let input_groupIdentifier : string = Settings.INPUT_GROUP_IDENTIFIER; let input_cognitoIdentityPoolId = Settings.AWS_COGNITO_IDENTITY_POOL_ID; // Construct the shared topic let input_shared_topic : string = "$share/" + input_groupIdentifier + "/" + input_topic; /** Set up the credentialsProvider */ const provider = new AWSCognitoCredentialsProvider({ IdentityPoolId: input_cognitoIdentityPoolId, Region: input_region}); /** Make sure the credential provider fetched before setup the connection */ await provider.refreshCredentials(); // Create the MQTT5 clients: one publisher and two subscribers let publisher : SampleMqtt5Client = new SampleMqtt5Client() publisher.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "1", "Publisher"); let subscriber_one : SampleMqtt5Client = new SampleMqtt5Client() subscriber_one.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "2", "Subscriber One"); let subscriber_two : SampleMqtt5Client = new SampleMqtt5Client() subscriber_two.setupMqtt5Client(provider, input_endpoint, input_region, input_clientId + "3", "Subscriber Two"); try { // Connect all the clients await publisher.startClient(); await subscriber_one.startClient(); await subscriber_two.startClient(); // Subscribe to the shared topic on the two subscribers await subscriber_one.client?.subscribe({subscriptions: [{qos: mqtt5.QoS.AtLeastOnce, topicFilter: input_shared_topic }]}); log("[" + subscriber_one.name + "]: Subscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_one.name + "]: Full subscribed topic is '" + input_shared_topic + "'."); await subscriber_two.client?.subscribe({subscriptions: [{qos: mqtt5.QoS.AtLeastOnce, topicFilter: input_shared_topic }]}); log("[" + subscriber_two.name + "]: Subscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_two.name + "]: Full subscribed topic is '" + input_shared_topic + "'."); // Publish using the publisher client let publishPacket : mqtt5.PublishPacket = { qos: mqtt5.QoS.AtLeastOnce, topicName: input_topic, payload: input_message }; if (input_count > 0) { let count = 0; while (count++ < input_count) { publishPacket.payload = input_message + ": " + count; await publisher.client?.publish(publishPacket); log("[" + publisher.name + "]: Published"); await new Promise(resolve => setTimeout(resolve, 1000)); } // Wait 5 seconds to let the last publish go out before unsubscribing. await new Promise(resolve => setTimeout(resolve, 5000)); } else { log("Skipping publishing messages due to message count being zero..."); } // Unsubscribe from the shared topic on the two subscribers await subscriber_one.client?.unsubscribe({topicFilters: [ input_shared_topic ]}); log("[" + subscriber_one.name + "]: Unsubscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_one.name + "]: Full unsubscribed topic is '" + input_shared_topic + "'."); await subscriber_two.client?.unsubscribe({topicFilters: [ input_shared_topic ]}); log("[" + subscriber_two.name + "]: Unsubscribed to topic '" + input_topic + "' in shared subscription group '" + input_groupIdentifier + "'."); log("[" + subscriber_two.name + "]: Full unsubscribed topic is '" + input_shared_topic + "'."); // Disconnect all the clients await publisher.stopClient(); await subscriber_one.stopClient(); await subscriber_two.stopClient(); } finally { // Close all the clients publisher.client?.close(); subscriber_one.client?.close(); subscriber_two.client?.close(); log("Sample Complete!"); } } async function main(){ await runSample(); log('Leaving'); } $(document).ready(() => { main(); });
setupMqtt5Client
identifier_name
elements.rs
/* Copyright 2018 Mozilla Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use crate::{ BinaryReader, BinaryReaderError, ConstExpr, ExternalKind, Result, SectionIteratorLimited, SectionReader, SectionWithLimitedItems, ValType, }; use std::ops::Range; /// Represents a core WebAssembly element segment. #[derive(Clone)] pub struct Element<'a> { /// The kind of the element segment. pub kind: ElementKind<'a>, /// The initial elements of the element segment. pub items: ElementItems<'a>, /// The type of the elements. pub ty: ValType, /// The range of the the element segment. pub range: Range<usize>, } /// The kind of element segment. #[derive(Clone)] pub enum ElementKind<'a> { /// The element segment is passive. Passive, /// The element segment is active. Active { /// The index of the table being initialized. table_index: u32, /// The initial expression of the element segment. offset_expr: ConstExpr<'a>, }, /// The element segment is declared. Declared, } /// Represents the items of an element segment. #[derive(Debug, Copy, Clone)] pub struct ElementItems<'a> { exprs: bool, offset: usize, data: &'a [u8], } /// Represents an individual item of an element segment. #[derive(Debug)] pub enum ElementItem<'a> { /// The item is a function index. Func(u32), /// The item is an initialization expression. Expr(ConstExpr<'a>), } impl<'a> ElementItems<'a> { /// Gets an items reader for the items in an element segment. pub fn get_items_reader<'b>(&self) -> Result<ElementItemsReader<'b>> where 'a: 'b, { ElementItemsReader::new(self.data, self.offset, self.exprs) } } /// A reader for element items in an element segment. pub struct ElementItemsReader<'a> { reader: BinaryReader<'a>, count: u32, exprs: bool, } impl<'a> ElementItemsReader<'a> { /// Constructs a new `ElementItemsReader` for the given data and offset. pub fn new(data: &[u8], offset: usize, exprs: bool) -> Result<ElementItemsReader> { let mut reader = BinaryReader::new_with_offset(data, offset); let count = reader.read_var_u32()?; Ok(ElementItemsReader { reader, count, exprs, }) } /// Gets the original position of the reader. pub fn original_position(&self) -> usize { self.reader.original_position() } /// Gets the count of element items in the segment. pub fn get_count(&self) -> u32 { self.count } /// Whether or not initialization expressions are used. pub fn uses_exprs(&self) -> bool { self.exprs } /// Reads an element item from the segment. pub fn read(&mut self) -> Result<ElementItem<'a>> { if self.exprs { let expr = self.reader.read_const_expr()?; Ok(ElementItem::Expr(expr)) } else { let idx = self.reader.read_var_u32()?; Ok(ElementItem::Func(idx)) } } } impl<'a> IntoIterator for ElementItemsReader<'a> { type Item = Result<ElementItem<'a>>; type IntoIter = ElementItemsIterator<'a>; fn into_iter(self) -> Self::IntoIter { let count = self.count; ElementItemsIterator { reader: self, left: count, err: false, } } } /// An iterator over element items in an element segment. pub struct ElementItemsIterator<'a> { reader: ElementItemsReader<'a>, left: u32, err: bool, } impl<'a> Iterator for ElementItemsIterator<'a> { type Item = Result<ElementItem<'a>>; fn next(&mut self) -> Option<Self::Item>
fn size_hint(&self) -> (usize, Option<usize>) { let count = self.reader.get_count() as usize; (count, Some(count)) } } /// A reader for the element section of a WebAssembly module. #[derive(Clone)] pub struct ElementSectionReader<'a> { reader: BinaryReader<'a>, count: u32, } impl<'a> ElementSectionReader<'a> { /// Constructs a new `ElementSectionReader` for the given data and offset. pub fn new(data: &'a [u8], offset: usize) -> Result<ElementSectionReader<'a>> { let mut reader = BinaryReader::new_with_offset(data, offset); let count = reader.read_var_u32()?; Ok(ElementSectionReader { reader, count }) } /// Gets the original position of the section reader. pub fn original_position(&self) -> usize { self.reader.original_position() } /// Gets the count of items in the section. pub fn get_count(&self) -> u32 { self.count } /// Reads content of the element section. /// /// # Examples /// /// ```no_run /// # let data: &[u8] = &[]; /// use wasmparser::{ElementSectionReader, ElementKind}; /// let mut element_reader = ElementSectionReader::new(data, 0).unwrap(); /// for _ in 0..element_reader.get_count() { /// let element = element_reader.read().expect("element"); /// if let ElementKind::Active { offset_expr, .. } = element.kind { /// let mut offset_expr_reader = offset_expr.get_binary_reader(); /// let op = offset_expr_reader.read_operator().expect("op"); /// println!("offset expression: {:?}", op); /// } /// let mut items_reader = element.items.get_items_reader().expect("items reader"); /// for _ in 0..items_reader.get_count() { /// let item = items_reader.read().expect("item"); /// println!(" Item: {:?}", item); /// } /// } /// ``` pub fn read<'b>(&mut self) -> Result<Element<'b>> where 'a: 'b, { let elem_start = self.reader.original_position(); // The current handling of the flags is largely specified in the `bulk-memory` proposal, // which at the time this commend is written has been merged to the main specification // draft. // // Notably, this proposal allows multiple different encodings of the table index 0. `00` // and `02 00` are both valid ways to specify the 0-th table. However it also makes // another encoding of the 0-th memory `80 00` no longer valid. // // We, however maintain this support by parsing `flags` as a LEB128 integer. In that case, // `80 00` encoding is parsed out as `0` and is therefore assigned a `tableidx` 0, even // though the current specification draft does not allow for this. // // See also https://github.com/WebAssembly/spec/issues/1439 let flags = self.reader.read_var_u32()?; if (flags & !0b111) != 0 { return Err(BinaryReaderError::new( "invalid flags byte in element segment", self.reader.original_position() - 1, )); } let kind = if flags & 0b001 != 0 { if flags & 0b010 != 0 { ElementKind::Declared } else { ElementKind::Passive } } else { let table_index = if flags & 0b010 == 0 { 0 } else { self.reader.read_var_u32()? }; let offset_expr = { let expr_offset = self.reader.position; self.reader.skip_const_expr()?; let data = &self.reader.buffer[expr_offset..self.reader.position]; ConstExpr::new(data, self.reader.original_offset + expr_offset) }; ElementKind::Active { table_index, offset_expr, } }; let exprs = flags & 0b100 != 0; let ty = if flags & 0b011 != 0 { if exprs { self.reader.read_val_type()? } else { match self.reader.read_external_kind()? { ExternalKind::Func => ValType::FuncRef, _ => { return Err(BinaryReaderError::new( "only the function external type is supported in elem segment", self.reader.original_position() - 1, )); } } } } else { ValType::FuncRef }; let data_start = self.reader.position; let items_count = self.reader.read_var_u32()?; if exprs { for _ in 0..items_count { self.reader.skip_const_expr()?; } } else { for _ in 0..items_count { self.reader.read_var_u32()?; } } let data_end = self.reader.position; let items = ElementItems { offset: self.reader.original_offset + data_start, data: &self.reader.buffer[data_start..data_end], exprs, }; let elem_end = self.reader.original_position(); let range = elem_start..elem_end; Ok(Element { kind, items, ty, range, }) } } impl<'a> SectionReader for ElementSectionReader<'a> { type Item = Element<'a>; fn read(&mut self) -> Result<Self::Item> { ElementSectionReader::read(self) } fn eof(&self) -> bool { self.reader.eof() } fn original_position(&self) -> usize { ElementSectionReader::original_position(self) } fn range(&self) -> Range<usize> { self.reader.range() } } impl<'a> SectionWithLimitedItems for ElementSectionReader<'a> { fn get_count(&self) -> u32 { ElementSectionReader::get_count(self) } } impl<'a> IntoIterator for ElementSectionReader<'a> { type Item = Result<Element<'a>>; type IntoIter = SectionIteratorLimited<ElementSectionReader<'a>>; fn into_iter(self) -> Self::IntoIter { SectionIteratorLimited::new(self) } }
{ if self.err || self.left == 0 { return None; } let result = self.reader.read(); self.err = result.is_err(); self.left -= 1; Some(result) }
identifier_body
elements.rs
/* Copyright 2018 Mozilla Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use crate::{ BinaryReader, BinaryReaderError, ConstExpr, ExternalKind, Result, SectionIteratorLimited, SectionReader, SectionWithLimitedItems, ValType, }; use std::ops::Range; /// Represents a core WebAssembly element segment. #[derive(Clone)] pub struct Element<'a> { /// The kind of the element segment. pub kind: ElementKind<'a>, /// The initial elements of the element segment. pub items: ElementItems<'a>, /// The type of the elements. pub ty: ValType, /// The range of the the element segment. pub range: Range<usize>, } /// The kind of element segment. #[derive(Clone)] pub enum ElementKind<'a> { /// The element segment is passive. Passive, /// The element segment is active. Active { /// The index of the table being initialized. table_index: u32, /// The initial expression of the element segment. offset_expr: ConstExpr<'a>, }, /// The element segment is declared. Declared, } /// Represents the items of an element segment. #[derive(Debug, Copy, Clone)] pub struct ElementItems<'a> { exprs: bool, offset: usize, data: &'a [u8], } /// Represents an individual item of an element segment. #[derive(Debug)] pub enum ElementItem<'a> { /// The item is a function index. Func(u32), /// The item is an initialization expression. Expr(ConstExpr<'a>), } impl<'a> ElementItems<'a> { /// Gets an items reader for the items in an element segment. pub fn get_items_reader<'b>(&self) -> Result<ElementItemsReader<'b>> where 'a: 'b, { ElementItemsReader::new(self.data, self.offset, self.exprs) } } /// A reader for element items in an element segment. pub struct ElementItemsReader<'a> { reader: BinaryReader<'a>, count: u32, exprs: bool, } impl<'a> ElementItemsReader<'a> { /// Constructs a new `ElementItemsReader` for the given data and offset. pub fn new(data: &[u8], offset: usize, exprs: bool) -> Result<ElementItemsReader> { let mut reader = BinaryReader::new_with_offset(data, offset); let count = reader.read_var_u32()?; Ok(ElementItemsReader { reader, count, exprs, }) } /// Gets the original position of the reader. pub fn original_position(&self) -> usize { self.reader.original_position() } /// Gets the count of element items in the segment. pub fn get_count(&self) -> u32 { self.count } /// Whether or not initialization expressions are used. pub fn uses_exprs(&self) -> bool { self.exprs } /// Reads an element item from the segment. pub fn read(&mut self) -> Result<ElementItem<'a>> { if self.exprs { let expr = self.reader.read_const_expr()?; Ok(ElementItem::Expr(expr)) } else { let idx = self.reader.read_var_u32()?; Ok(ElementItem::Func(idx)) } } } impl<'a> IntoIterator for ElementItemsReader<'a> { type Item = Result<ElementItem<'a>>; type IntoIter = ElementItemsIterator<'a>; fn into_iter(self) -> Self::IntoIter { let count = self.count; ElementItemsIterator { reader: self, left: count, err: false, } } } /// An iterator over element items in an element segment. pub struct ElementItemsIterator<'a> { reader: ElementItemsReader<'a>, left: u32, err: bool, } impl<'a> Iterator for ElementItemsIterator<'a> { type Item = Result<ElementItem<'a>>; fn next(&mut self) -> Option<Self::Item> { if self.err || self.left == 0 { return None; } let result = self.reader.read(); self.err = result.is_err(); self.left -= 1; Some(result) } fn size_hint(&self) -> (usize, Option<usize>) { let count = self.reader.get_count() as usize; (count, Some(count)) } } /// A reader for the element section of a WebAssembly module. #[derive(Clone)] pub struct ElementSectionReader<'a> { reader: BinaryReader<'a>, count: u32, } impl<'a> ElementSectionReader<'a> { /// Constructs a new `ElementSectionReader` for the given data and offset. pub fn new(data: &'a [u8], offset: usize) -> Result<ElementSectionReader<'a>> { let mut reader = BinaryReader::new_with_offset(data, offset); let count = reader.read_var_u32()?; Ok(ElementSectionReader { reader, count }) } /// Gets the original position of the section reader. pub fn original_position(&self) -> usize { self.reader.original_position() } /// Gets the count of items in the section. pub fn get_count(&self) -> u32 { self.count } /// Reads content of the element section. /// /// # Examples /// /// ```no_run /// # let data: &[u8] = &[]; /// use wasmparser::{ElementSectionReader, ElementKind}; /// let mut element_reader = ElementSectionReader::new(data, 0).unwrap(); /// for _ in 0..element_reader.get_count() { /// let element = element_reader.read().expect("element"); /// if let ElementKind::Active { offset_expr, .. } = element.kind { /// let mut offset_expr_reader = offset_expr.get_binary_reader(); /// let op = offset_expr_reader.read_operator().expect("op"); /// println!("offset expression: {:?}", op); /// } /// let mut items_reader = element.items.get_items_reader().expect("items reader");
/// ``` pub fn read<'b>(&mut self) -> Result<Element<'b>> where 'a: 'b, { let elem_start = self.reader.original_position(); // The current handling of the flags is largely specified in the `bulk-memory` proposal, // which at the time this commend is written has been merged to the main specification // draft. // // Notably, this proposal allows multiple different encodings of the table index 0. `00` // and `02 00` are both valid ways to specify the 0-th table. However it also makes // another encoding of the 0-th memory `80 00` no longer valid. // // We, however maintain this support by parsing `flags` as a LEB128 integer. In that case, // `80 00` encoding is parsed out as `0` and is therefore assigned a `tableidx` 0, even // though the current specification draft does not allow for this. // // See also https://github.com/WebAssembly/spec/issues/1439 let flags = self.reader.read_var_u32()?; if (flags & !0b111) != 0 { return Err(BinaryReaderError::new( "invalid flags byte in element segment", self.reader.original_position() - 1, )); } let kind = if flags & 0b001 != 0 { if flags & 0b010 != 0 { ElementKind::Declared } else { ElementKind::Passive } } else { let table_index = if flags & 0b010 == 0 { 0 } else { self.reader.read_var_u32()? }; let offset_expr = { let expr_offset = self.reader.position; self.reader.skip_const_expr()?; let data = &self.reader.buffer[expr_offset..self.reader.position]; ConstExpr::new(data, self.reader.original_offset + expr_offset) }; ElementKind::Active { table_index, offset_expr, } }; let exprs = flags & 0b100 != 0; let ty = if flags & 0b011 != 0 { if exprs { self.reader.read_val_type()? } else { match self.reader.read_external_kind()? { ExternalKind::Func => ValType::FuncRef, _ => { return Err(BinaryReaderError::new( "only the function external type is supported in elem segment", self.reader.original_position() - 1, )); } } } } else { ValType::FuncRef }; let data_start = self.reader.position; let items_count = self.reader.read_var_u32()?; if exprs { for _ in 0..items_count { self.reader.skip_const_expr()?; } } else { for _ in 0..items_count { self.reader.read_var_u32()?; } } let data_end = self.reader.position; let items = ElementItems { offset: self.reader.original_offset + data_start, data: &self.reader.buffer[data_start..data_end], exprs, }; let elem_end = self.reader.original_position(); let range = elem_start..elem_end; Ok(Element { kind, items, ty, range, }) } } impl<'a> SectionReader for ElementSectionReader<'a> { type Item = Element<'a>; fn read(&mut self) -> Result<Self::Item> { ElementSectionReader::read(self) } fn eof(&self) -> bool { self.reader.eof() } fn original_position(&self) -> usize { ElementSectionReader::original_position(self) } fn range(&self) -> Range<usize> { self.reader.range() } } impl<'a> SectionWithLimitedItems for ElementSectionReader<'a> { fn get_count(&self) -> u32 { ElementSectionReader::get_count(self) } } impl<'a> IntoIterator for ElementSectionReader<'a> { type Item = Result<Element<'a>>; type IntoIter = SectionIteratorLimited<ElementSectionReader<'a>>; fn into_iter(self) -> Self::IntoIter { SectionIteratorLimited::new(self) } }
/// for _ in 0..items_reader.get_count() { /// let item = items_reader.read().expect("item"); /// println!(" Item: {:?}", item); /// } /// }
random_line_split
elements.rs
/* Copyright 2018 Mozilla Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use crate::{ BinaryReader, BinaryReaderError, ConstExpr, ExternalKind, Result, SectionIteratorLimited, SectionReader, SectionWithLimitedItems, ValType, }; use std::ops::Range; /// Represents a core WebAssembly element segment. #[derive(Clone)] pub struct Element<'a> { /// The kind of the element segment. pub kind: ElementKind<'a>, /// The initial elements of the element segment. pub items: ElementItems<'a>, /// The type of the elements. pub ty: ValType, /// The range of the the element segment. pub range: Range<usize>, } /// The kind of element segment. #[derive(Clone)] pub enum ElementKind<'a> { /// The element segment is passive. Passive, /// The element segment is active. Active { /// The index of the table being initialized. table_index: u32, /// The initial expression of the element segment. offset_expr: ConstExpr<'a>, }, /// The element segment is declared. Declared, } /// Represents the items of an element segment. #[derive(Debug, Copy, Clone)] pub struct ElementItems<'a> { exprs: bool, offset: usize, data: &'a [u8], } /// Represents an individual item of an element segment. #[derive(Debug)] pub enum ElementItem<'a> { /// The item is a function index. Func(u32), /// The item is an initialization expression. Expr(ConstExpr<'a>), } impl<'a> ElementItems<'a> { /// Gets an items reader for the items in an element segment. pub fn get_items_reader<'b>(&self) -> Result<ElementItemsReader<'b>> where 'a: 'b, { ElementItemsReader::new(self.data, self.offset, self.exprs) } } /// A reader for element items in an element segment. pub struct ElementItemsReader<'a> { reader: BinaryReader<'a>, count: u32, exprs: bool, } impl<'a> ElementItemsReader<'a> { /// Constructs a new `ElementItemsReader` for the given data and offset. pub fn new(data: &[u8], offset: usize, exprs: bool) -> Result<ElementItemsReader> { let mut reader = BinaryReader::new_with_offset(data, offset); let count = reader.read_var_u32()?; Ok(ElementItemsReader { reader, count, exprs, }) } /// Gets the original position of the reader. pub fn original_position(&self) -> usize { self.reader.original_position() } /// Gets the count of element items in the segment. pub fn get_count(&self) -> u32 { self.count } /// Whether or not initialization expressions are used. pub fn uses_exprs(&self) -> bool { self.exprs } /// Reads an element item from the segment. pub fn read(&mut self) -> Result<ElementItem<'a>> { if self.exprs { let expr = self.reader.read_const_expr()?; Ok(ElementItem::Expr(expr)) } else { let idx = self.reader.read_var_u32()?; Ok(ElementItem::Func(idx)) } } } impl<'a> IntoIterator for ElementItemsReader<'a> { type Item = Result<ElementItem<'a>>; type IntoIter = ElementItemsIterator<'a>; fn into_iter(self) -> Self::IntoIter { let count = self.count; ElementItemsIterator { reader: self, left: count, err: false, } } } /// An iterator over element items in an element segment. pub struct ElementItemsIterator<'a> { reader: ElementItemsReader<'a>, left: u32, err: bool, } impl<'a> Iterator for ElementItemsIterator<'a> { type Item = Result<ElementItem<'a>>; fn next(&mut self) -> Option<Self::Item> { if self.err || self.left == 0 { return None; } let result = self.reader.read(); self.err = result.is_err(); self.left -= 1; Some(result) } fn size_hint(&self) -> (usize, Option<usize>) { let count = self.reader.get_count() as usize; (count, Some(count)) } } /// A reader for the element section of a WebAssembly module. #[derive(Clone)] pub struct ElementSectionReader<'a> { reader: BinaryReader<'a>, count: u32, } impl<'a> ElementSectionReader<'a> { /// Constructs a new `ElementSectionReader` for the given data and offset. pub fn new(data: &'a [u8], offset: usize) -> Result<ElementSectionReader<'a>> { let mut reader = BinaryReader::new_with_offset(data, offset); let count = reader.read_var_u32()?; Ok(ElementSectionReader { reader, count }) } /// Gets the original position of the section reader. pub fn original_position(&self) -> usize { self.reader.original_position() } /// Gets the count of items in the section. pub fn get_count(&self) -> u32 { self.count } /// Reads content of the element section. /// /// # Examples /// /// ```no_run /// # let data: &[u8] = &[]; /// use wasmparser::{ElementSectionReader, ElementKind}; /// let mut element_reader = ElementSectionReader::new(data, 0).unwrap(); /// for _ in 0..element_reader.get_count() { /// let element = element_reader.read().expect("element"); /// if let ElementKind::Active { offset_expr, .. } = element.kind { /// let mut offset_expr_reader = offset_expr.get_binary_reader(); /// let op = offset_expr_reader.read_operator().expect("op"); /// println!("offset expression: {:?}", op); /// } /// let mut items_reader = element.items.get_items_reader().expect("items reader"); /// for _ in 0..items_reader.get_count() { /// let item = items_reader.read().expect("item"); /// println!(" Item: {:?}", item); /// } /// } /// ``` pub fn read<'b>(&mut self) -> Result<Element<'b>> where 'a: 'b, { let elem_start = self.reader.original_position(); // The current handling of the flags is largely specified in the `bulk-memory` proposal, // which at the time this commend is written has been merged to the main specification // draft. // // Notably, this proposal allows multiple different encodings of the table index 0. `00` // and `02 00` are both valid ways to specify the 0-th table. However it also makes // another encoding of the 0-th memory `80 00` no longer valid. // // We, however maintain this support by parsing `flags` as a LEB128 integer. In that case, // `80 00` encoding is parsed out as `0` and is therefore assigned a `tableidx` 0, even // though the current specification draft does not allow for this. // // See also https://github.com/WebAssembly/spec/issues/1439 let flags = self.reader.read_var_u32()?; if (flags & !0b111) != 0
let kind = if flags & 0b001 != 0 { if flags & 0b010 != 0 { ElementKind::Declared } else { ElementKind::Passive } } else { let table_index = if flags & 0b010 == 0 { 0 } else { self.reader.read_var_u32()? }; let offset_expr = { let expr_offset = self.reader.position; self.reader.skip_const_expr()?; let data = &self.reader.buffer[expr_offset..self.reader.position]; ConstExpr::new(data, self.reader.original_offset + expr_offset) }; ElementKind::Active { table_index, offset_expr, } }; let exprs = flags & 0b100 != 0; let ty = if flags & 0b011 != 0 { if exprs { self.reader.read_val_type()? } else { match self.reader.read_external_kind()? { ExternalKind::Func => ValType::FuncRef, _ => { return Err(BinaryReaderError::new( "only the function external type is supported in elem segment", self.reader.original_position() - 1, )); } } } } else { ValType::FuncRef }; let data_start = self.reader.position; let items_count = self.reader.read_var_u32()?; if exprs { for _ in 0..items_count { self.reader.skip_const_expr()?; } } else { for _ in 0..items_count { self.reader.read_var_u32()?; } } let data_end = self.reader.position; let items = ElementItems { offset: self.reader.original_offset + data_start, data: &self.reader.buffer[data_start..data_end], exprs, }; let elem_end = self.reader.original_position(); let range = elem_start..elem_end; Ok(Element { kind, items, ty, range, }) } } impl<'a> SectionReader for ElementSectionReader<'a> { type Item = Element<'a>; fn read(&mut self) -> Result<Self::Item> { ElementSectionReader::read(self) } fn eof(&self) -> bool { self.reader.eof() } fn original_position(&self) -> usize { ElementSectionReader::original_position(self) } fn range(&self) -> Range<usize> { self.reader.range() } } impl<'a> SectionWithLimitedItems for ElementSectionReader<'a> { fn get_count(&self) -> u32 { ElementSectionReader::get_count(self) } } impl<'a> IntoIterator for ElementSectionReader<'a> { type Item = Result<Element<'a>>; type IntoIter = SectionIteratorLimited<ElementSectionReader<'a>>; fn into_iter(self) -> Self::IntoIter { SectionIteratorLimited::new(self) } }
{ return Err(BinaryReaderError::new( "invalid flags byte in element segment", self.reader.original_position() - 1, )); }
conditional_block
elements.rs
/* Copyright 2018 Mozilla Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ use crate::{ BinaryReader, BinaryReaderError, ConstExpr, ExternalKind, Result, SectionIteratorLimited, SectionReader, SectionWithLimitedItems, ValType, }; use std::ops::Range; /// Represents a core WebAssembly element segment. #[derive(Clone)] pub struct Element<'a> { /// The kind of the element segment. pub kind: ElementKind<'a>, /// The initial elements of the element segment. pub items: ElementItems<'a>, /// The type of the elements. pub ty: ValType, /// The range of the the element segment. pub range: Range<usize>, } /// The kind of element segment. #[derive(Clone)] pub enum ElementKind<'a> { /// The element segment is passive. Passive, /// The element segment is active. Active { /// The index of the table being initialized. table_index: u32, /// The initial expression of the element segment. offset_expr: ConstExpr<'a>, }, /// The element segment is declared. Declared, } /// Represents the items of an element segment. #[derive(Debug, Copy, Clone)] pub struct ElementItems<'a> { exprs: bool, offset: usize, data: &'a [u8], } /// Represents an individual item of an element segment. #[derive(Debug)] pub enum ElementItem<'a> { /// The item is a function index. Func(u32), /// The item is an initialization expression. Expr(ConstExpr<'a>), } impl<'a> ElementItems<'a> { /// Gets an items reader for the items in an element segment. pub fn get_items_reader<'b>(&self) -> Result<ElementItemsReader<'b>> where 'a: 'b, { ElementItemsReader::new(self.data, self.offset, self.exprs) } } /// A reader for element items in an element segment. pub struct ElementItemsReader<'a> { reader: BinaryReader<'a>, count: u32, exprs: bool, } impl<'a> ElementItemsReader<'a> { /// Constructs a new `ElementItemsReader` for the given data and offset. pub fn new(data: &[u8], offset: usize, exprs: bool) -> Result<ElementItemsReader> { let mut reader = BinaryReader::new_with_offset(data, offset); let count = reader.read_var_u32()?; Ok(ElementItemsReader { reader, count, exprs, }) } /// Gets the original position of the reader. pub fn original_position(&self) -> usize { self.reader.original_position() } /// Gets the count of element items in the segment. pub fn get_count(&self) -> u32 { self.count } /// Whether or not initialization expressions are used. pub fn uses_exprs(&self) -> bool { self.exprs } /// Reads an element item from the segment. pub fn read(&mut self) -> Result<ElementItem<'a>> { if self.exprs { let expr = self.reader.read_const_expr()?; Ok(ElementItem::Expr(expr)) } else { let idx = self.reader.read_var_u32()?; Ok(ElementItem::Func(idx)) } } } impl<'a> IntoIterator for ElementItemsReader<'a> { type Item = Result<ElementItem<'a>>; type IntoIter = ElementItemsIterator<'a>; fn into_iter(self) -> Self::IntoIter { let count = self.count; ElementItemsIterator { reader: self, left: count, err: false, } } } /// An iterator over element items in an element segment. pub struct ElementItemsIterator<'a> { reader: ElementItemsReader<'a>, left: u32, err: bool, } impl<'a> Iterator for ElementItemsIterator<'a> { type Item = Result<ElementItem<'a>>; fn next(&mut self) -> Option<Self::Item> { if self.err || self.left == 0 { return None; } let result = self.reader.read(); self.err = result.is_err(); self.left -= 1; Some(result) } fn size_hint(&self) -> (usize, Option<usize>) { let count = self.reader.get_count() as usize; (count, Some(count)) } } /// A reader for the element section of a WebAssembly module. #[derive(Clone)] pub struct ElementSectionReader<'a> { reader: BinaryReader<'a>, count: u32, } impl<'a> ElementSectionReader<'a> { /// Constructs a new `ElementSectionReader` for the given data and offset. pub fn new(data: &'a [u8], offset: usize) -> Result<ElementSectionReader<'a>> { let mut reader = BinaryReader::new_with_offset(data, offset); let count = reader.read_var_u32()?; Ok(ElementSectionReader { reader, count }) } /// Gets the original position of the section reader. pub fn original_position(&self) -> usize { self.reader.original_position() } /// Gets the count of items in the section. pub fn
(&self) -> u32 { self.count } /// Reads content of the element section. /// /// # Examples /// /// ```no_run /// # let data: &[u8] = &[]; /// use wasmparser::{ElementSectionReader, ElementKind}; /// let mut element_reader = ElementSectionReader::new(data, 0).unwrap(); /// for _ in 0..element_reader.get_count() { /// let element = element_reader.read().expect("element"); /// if let ElementKind::Active { offset_expr, .. } = element.kind { /// let mut offset_expr_reader = offset_expr.get_binary_reader(); /// let op = offset_expr_reader.read_operator().expect("op"); /// println!("offset expression: {:?}", op); /// } /// let mut items_reader = element.items.get_items_reader().expect("items reader"); /// for _ in 0..items_reader.get_count() { /// let item = items_reader.read().expect("item"); /// println!(" Item: {:?}", item); /// } /// } /// ``` pub fn read<'b>(&mut self) -> Result<Element<'b>> where 'a: 'b, { let elem_start = self.reader.original_position(); // The current handling of the flags is largely specified in the `bulk-memory` proposal, // which at the time this commend is written has been merged to the main specification // draft. // // Notably, this proposal allows multiple different encodings of the table index 0. `00` // and `02 00` are both valid ways to specify the 0-th table. However it also makes // another encoding of the 0-th memory `80 00` no longer valid. // // We, however maintain this support by parsing `flags` as a LEB128 integer. In that case, // `80 00` encoding is parsed out as `0` and is therefore assigned a `tableidx` 0, even // though the current specification draft does not allow for this. // // See also https://github.com/WebAssembly/spec/issues/1439 let flags = self.reader.read_var_u32()?; if (flags & !0b111) != 0 { return Err(BinaryReaderError::new( "invalid flags byte in element segment", self.reader.original_position() - 1, )); } let kind = if flags & 0b001 != 0 { if flags & 0b010 != 0 { ElementKind::Declared } else { ElementKind::Passive } } else { let table_index = if flags & 0b010 == 0 { 0 } else { self.reader.read_var_u32()? }; let offset_expr = { let expr_offset = self.reader.position; self.reader.skip_const_expr()?; let data = &self.reader.buffer[expr_offset..self.reader.position]; ConstExpr::new(data, self.reader.original_offset + expr_offset) }; ElementKind::Active { table_index, offset_expr, } }; let exprs = flags & 0b100 != 0; let ty = if flags & 0b011 != 0 { if exprs { self.reader.read_val_type()? } else { match self.reader.read_external_kind()? { ExternalKind::Func => ValType::FuncRef, _ => { return Err(BinaryReaderError::new( "only the function external type is supported in elem segment", self.reader.original_position() - 1, )); } } } } else { ValType::FuncRef }; let data_start = self.reader.position; let items_count = self.reader.read_var_u32()?; if exprs { for _ in 0..items_count { self.reader.skip_const_expr()?; } } else { for _ in 0..items_count { self.reader.read_var_u32()?; } } let data_end = self.reader.position; let items = ElementItems { offset: self.reader.original_offset + data_start, data: &self.reader.buffer[data_start..data_end], exprs, }; let elem_end = self.reader.original_position(); let range = elem_start..elem_end; Ok(Element { kind, items, ty, range, }) } } impl<'a> SectionReader for ElementSectionReader<'a> { type Item = Element<'a>; fn read(&mut self) -> Result<Self::Item> { ElementSectionReader::read(self) } fn eof(&self) -> bool { self.reader.eof() } fn original_position(&self) -> usize { ElementSectionReader::original_position(self) } fn range(&self) -> Range<usize> { self.reader.range() } } impl<'a> SectionWithLimitedItems for ElementSectionReader<'a> { fn get_count(&self) -> u32 { ElementSectionReader::get_count(self) } } impl<'a> IntoIterator for ElementSectionReader<'a> { type Item = Result<Element<'a>>; type IntoIter = SectionIteratorLimited<ElementSectionReader<'a>>; fn into_iter(self) -> Self::IntoIter { SectionIteratorLimited::new(self) } }
get_count
identifier_name
graph_runner.ts
/** * @license * Copyright 2017 Google Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ import {InputProvider} from '../data/input_provider'; import {tidy} from '../globals'; import {NDArrayMath} from '../math'; import {Optimizer} from '../optimizers/optimizer'; import {Scalar, Tensor} from '../tensor'; import {SymbolicTensor} from './graph'; import {CostReduction, FeedEntry, Session} from './session'; const DEFAULT_EVAL_INTERVAL_MS = 1500; const DEFAULT_COST_INTERVAL_MS = 500; const DEFAULT_INFERENCE_EXAMPLE_INTERVAL_MS = 3000; export interface GraphRunnerEventObserver { batchesTrainedCallback?: (totalBatchesTrained: number) => void; avgCostCallback?: (avgCost: Scalar) => void; metricCallback?: (metric: Tensor) => void; inferenceExamplesCallback?: (feeds: FeedEntry[][], inferenceValues: Tensor[]) => void; inferenceExamplesPerSecCallback?: (examplesPerSec: number) => void; trainExamplesPerSecCallback?: (examplesPerSec: number) => void; totalTimeCallback?: (totalTimeSec: number) => void; doneTrainingCallback?: () => void; } export enum MetricReduction { SUM, MEAN } /** * A class that drives the training of a graph model given a dataset. It allows * the user to provide a set of callbacks for measurements like cost, accuracy, * and speed of training. */ export class GraphRunner { private costTensor: SymbolicTensor; private trainFeedEntries: FeedEntry[]; private batchSize: number; private optimizer: Optimizer; private currentTrainLoopNumBatches: number|undefined; private costIntervalMs: number; private metricTensor: SymbolicTensor|undefined; private metricFeedEntries: FeedEntry[]|undefined; private metricBatchSize: number|undefined; private metricReduction: MetricReduction; private metricIntervalMs: number; private inferenceTensor: SymbolicTensor; private inferenceFeedEntries: FeedEntry[]|undefined; private inferenceExampleIntervalMs: number; private inferenceExampleCount: number; // Runtime information. private isTraining: boolean; private totalBatchesTrained: number; private batchesTrainedThisRun: number; private lastComputedMetric: Scalar; private isInferring: boolean; private lastInferTimeoutID: number; private currentInferenceLoopNumPasses: number|undefined; private inferencePassesThisRun: number; private trainStartTimestamp: number; private lastCostTimestamp = 0; private lastEvalTimestamp = 0; private zeroScalar: Scalar; private metricBatchSizeScalar: Scalar; constructor( private math: NDArrayMath, private session: Session, private eventObserver: GraphRunnerEventObserver) { this.resetStatistics(); this.zeroScalar = Scalar.new(0); } resetStatistics() { this.totalBatchesTrained = 0; } /** * Start the training loop with an optional number of batches to train for. * Optionally takes a metric tensor and feed entries to compute periodically. * This can be used for computing accuracy, or a similar metric. */ train( costTensor: SymbolicTensor, trainFeedEntries: FeedEntry[], batchSize: number, optimizer: Optimizer, numBatches?: number, metricTensor?: SymbolicTensor, metricFeedEntries?: FeedEntry[], metricBatchSize?: number, metricReduction = MetricReduction.MEAN, evalIntervalMs = DEFAULT_EVAL_INTERVAL_MS, costIntervalMs = DEFAULT_COST_INTERVAL_MS) { this.costTensor = costTensor; this.trainFeedEntries = trainFeedEntries; this.metricTensor = metricTensor; this.metricFeedEntries = metricFeedEntries; if (metricBatchSize != null && this.metricBatchSize !== metricBatchSize) { if (this.metricBatchSizeScalar != null) { this.metricBatchSizeScalar.dispose(); } this.metricBatchSizeScalar = Scalar.new(metricBatchSize); } this.metricBatchSize = metricBatchSize; this.metricReduction = metricReduction; this.batchSize = batchSize; this.optimizer = optimizer; this.metricIntervalMs = evalIntervalMs; this.costIntervalMs = costIntervalMs; this.currentTrainLoopNumBatches = numBatches; this.batchesTrainedThisRun = 0; this.isTraining = true; this.trainStartTimestamp = performance.now(); this.trainNetwork(); } stopTraining() { this.isTraining = false; } resumeTraining() { this.isTraining = true; this.trainNetwork(); } private trainNetwork() { if (this.batchesTrainedThisRun === this.currentTrainLoopNumBatches) { this.stopTraining(); } if (!this.isTraining) { if (this.eventObserver.doneTrainingCallback != null) { this.eventObserver.doneTrainingCallback(); } return; } const start = performance.now(); const shouldComputeCost = this.eventObserver.avgCostCallback != null && (start - this.lastCostTimestamp > this.costIntervalMs); if (shouldComputeCost) { this.lastCostTimestamp = start; } const costReduction = shouldComputeCost ? CostReduction.MEAN : CostReduction.NONE; tidy(() => { const avgCost = this.session.train( this.costTensor, this.trainFeedEntries, this.batchSize, this.optimizer, costReduction); if (shouldComputeCost) { const trainTime = performance.now() - start; this.eventObserver.avgCostCallback(avgCost); if (this.eventObserver.trainExamplesPerSecCallback != null) { const examplesPerSec = (this.batchSize * 1000 / trainTime); this.eventObserver.trainExamplesPerSecCallback(examplesPerSec); } } if (this.eventObserver.metricCallback != null && this.metricFeedEntries != null && start - this.lastEvalTimestamp > this.metricIntervalMs) { this.lastEvalTimestamp = start; if (this.lastComputedMetric != null) { this.lastComputedMetric.dispose(); } this.lastComputedMetric = this.computeMetric(); this.eventObserver.metricCallback(this.lastComputedMetric); } if (this.eventObserver.totalTimeCallback != null) { this.eventObserver.totalTimeCallback( (start - this.trainStartTimestamp) / 1000); } this.batchesTrainedThisRun++; this.totalBatchesTrained++; if (this.eventObserver.batchesTrainedCallback != null) { this.eventObserver.batchesTrainedCallback(this.totalBatchesTrained); } }); requestAnimationFrame(() => this.trainNetwork()); } infer( inferenceTensor: SymbolicTensor, inferenceFeedEntries: FeedEntry[], inferenceExampleIntervalMs = DEFAULT_INFERENCE_EXAMPLE_INTERVAL_MS, inferenceExampleCount = 5, numPasses?: number) { if (this.eventObserver.inferenceExamplesCallback == null && this.eventObserver.inferenceExamplesPerSecCallback == null) { throw new Error( 'Cannot start inference loop, no inference example or ' + 'examples/sec observer provided.'); } // Make sure the feed values are providers, and not NDArrays. for (let i = 0; i < inferenceFeedEntries.length; i++) { const feedEntry = inferenceFeedEntries[i]; if (feedEntry.data instanceof Tensor) { throw new Error( 'Cannot start inference on the model runner with feed entries of ' + 'type NDArray. Please use InputProviders.'); } } this.inferenceExampleIntervalMs = inferenceExampleIntervalMs; this.inferenceTensor = inferenceTensor; this.inferenceFeedEntries = inferenceFeedEntries; this.inferenceExampleCount = inferenceExampleCount; this.currentInferenceLoopNumPasses = numPasses; if (!this.isInferring) { this.inferencePassesThisRun = 0; requestAnimationFrame(() => this.inferNetwork()); } this.isInferring = true; } private inferNetwork() { if (!this.isInferring || this.inferencePassesThisRun === this.currentInferenceLoopNumPasses) { return; } tidy(() => { const feeds: FeedEntry[][] = []; const inferenceValues: Tensor[] = []; const start = performance.now(); for (let i = 0; i < this.inferenceExampleCount; i++) { // Populate a new FeedEntry[] populated with NDArrays. const ndarrayFeedEntries: FeedEntry[] = []; for (let j = 0; j < this.inferenceFeedEntries.length; j++) { const feedEntry = this.inferenceFeedEntries[j]; const nextCopy = (feedEntry.data as InputProvider).getNextCopy(); ndarrayFeedEntries.push({tensor: feedEntry.tensor, data: nextCopy}); } feeds.push(ndarrayFeedEntries); inferenceValues.push( this.session.eval(this.inferenceTensor, ndarrayFeedEntries)); } if (this.eventObserver.inferenceExamplesPerSecCallback != null) { // Force a GPU download, since inference results are generally needed on // the CPU and it's more fair to include blocking on the GPU to complete // its work for the inference measurement. inferenceValues[inferenceValues.length - 1].dataSync(); const inferenceExamplesPerSecTime = performance.now() - start; const examplesPerSec = (this.inferenceExampleCount * 1000 / inferenceExamplesPerSecTime); this.eventObserver.inferenceExamplesPerSecCallback(examplesPerSec); } if (this.eventObserver.inferenceExamplesCallback != null) { this.eventObserver.inferenceExamplesCallback(feeds, inferenceValues); } this.inferencePassesThisRun++; }); this.lastInferTimeoutID = window.setTimeout( () => this.inferNetwork(), this.inferenceExampleIntervalMs); }
() { this.isInferring = false; window.clearTimeout(this.lastInferTimeoutID); } isInferenceRunning(): boolean { return this.isInferring; } computeMetric(): Scalar { if (this.metricFeedEntries == null) { throw new Error('Cannot compute metric, no metric FeedEntries provided.'); } let metric = this.zeroScalar; return tidy(() => { for (let i = 0; i < this.metricBatchSize; i++) { const metricValue = this.session.eval(this.metricTensor, this.metricFeedEntries) as Tensor; metric = this.math.add(metric, metricValue.toFloat()); } if (this.metricReduction === MetricReduction.MEAN) { metric = this.math.divide(metric, this.metricBatchSizeScalar); } return metric; }); } getTotalBatchesTrained(): number { return this.totalBatchesTrained; } getLastComputedMetric(): Scalar { return this.lastComputedMetric; } setMath(math: NDArrayMath) { this.math = math; } setSession(session: Session) { this.session = session; } setInferenceTensor(inferenceTensor: SymbolicTensor) { this.inferenceTensor = inferenceTensor; } setInferenceExampleCount(inferenceExampleCount: number) { this.inferenceExampleCount = inferenceExampleCount; } }
stopInferring
identifier_name
graph_runner.ts
/** * @license * Copyright 2017 Google Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ import {InputProvider} from '../data/input_provider'; import {tidy} from '../globals'; import {NDArrayMath} from '../math'; import {Optimizer} from '../optimizers/optimizer'; import {Scalar, Tensor} from '../tensor'; import {SymbolicTensor} from './graph'; import {CostReduction, FeedEntry, Session} from './session'; const DEFAULT_EVAL_INTERVAL_MS = 1500; const DEFAULT_COST_INTERVAL_MS = 500; const DEFAULT_INFERENCE_EXAMPLE_INTERVAL_MS = 3000; export interface GraphRunnerEventObserver { batchesTrainedCallback?: (totalBatchesTrained: number) => void; avgCostCallback?: (avgCost: Scalar) => void; metricCallback?: (metric: Tensor) => void; inferenceExamplesCallback?: (feeds: FeedEntry[][], inferenceValues: Tensor[]) => void; inferenceExamplesPerSecCallback?: (examplesPerSec: number) => void; trainExamplesPerSecCallback?: (examplesPerSec: number) => void; totalTimeCallback?: (totalTimeSec: number) => void; doneTrainingCallback?: () => void; } export enum MetricReduction { SUM, MEAN } /** * A class that drives the training of a graph model given a dataset. It allows * the user to provide a set of callbacks for measurements like cost, accuracy, * and speed of training. */ export class GraphRunner { private costTensor: SymbolicTensor; private trainFeedEntries: FeedEntry[]; private batchSize: number; private optimizer: Optimizer; private currentTrainLoopNumBatches: number|undefined; private costIntervalMs: number; private metricTensor: SymbolicTensor|undefined; private metricFeedEntries: FeedEntry[]|undefined; private metricBatchSize: number|undefined; private metricReduction: MetricReduction; private metricIntervalMs: number; private inferenceTensor: SymbolicTensor; private inferenceFeedEntries: FeedEntry[]|undefined; private inferenceExampleIntervalMs: number; private inferenceExampleCount: number; // Runtime information. private isTraining: boolean; private totalBatchesTrained: number; private batchesTrainedThisRun: number; private lastComputedMetric: Scalar; private isInferring: boolean; private lastInferTimeoutID: number; private currentInferenceLoopNumPasses: number|undefined; private inferencePassesThisRun: number; private trainStartTimestamp: number; private lastCostTimestamp = 0; private lastEvalTimestamp = 0; private zeroScalar: Scalar; private metricBatchSizeScalar: Scalar; constructor( private math: NDArrayMath, private session: Session, private eventObserver: GraphRunnerEventObserver) { this.resetStatistics(); this.zeroScalar = Scalar.new(0); } resetStatistics() { this.totalBatchesTrained = 0; } /** * Start the training loop with an optional number of batches to train for. * Optionally takes a metric tensor and feed entries to compute periodically. * This can be used for computing accuracy, or a similar metric. */ train( costTensor: SymbolicTensor, trainFeedEntries: FeedEntry[], batchSize: number, optimizer: Optimizer, numBatches?: number, metricTensor?: SymbolicTensor, metricFeedEntries?: FeedEntry[], metricBatchSize?: number, metricReduction = MetricReduction.MEAN, evalIntervalMs = DEFAULT_EVAL_INTERVAL_MS, costIntervalMs = DEFAULT_COST_INTERVAL_MS) { this.costTensor = costTensor; this.trainFeedEntries = trainFeedEntries; this.metricTensor = metricTensor; this.metricFeedEntries = metricFeedEntries; if (metricBatchSize != null && this.metricBatchSize !== metricBatchSize) { if (this.metricBatchSizeScalar != null) { this.metricBatchSizeScalar.dispose(); } this.metricBatchSizeScalar = Scalar.new(metricBatchSize); } this.metricBatchSize = metricBatchSize; this.metricReduction = metricReduction; this.batchSize = batchSize; this.optimizer = optimizer; this.metricIntervalMs = evalIntervalMs; this.costIntervalMs = costIntervalMs; this.currentTrainLoopNumBatches = numBatches; this.batchesTrainedThisRun = 0; this.isTraining = true; this.trainStartTimestamp = performance.now(); this.trainNetwork(); } stopTraining() { this.isTraining = false; } resumeTraining() { this.isTraining = true; this.trainNetwork(); } private trainNetwork() { if (this.batchesTrainedThisRun === this.currentTrainLoopNumBatches) { this.stopTraining(); } if (!this.isTraining) { if (this.eventObserver.doneTrainingCallback != null) { this.eventObserver.doneTrainingCallback(); } return; } const start = performance.now(); const shouldComputeCost = this.eventObserver.avgCostCallback != null && (start - this.lastCostTimestamp > this.costIntervalMs); if (shouldComputeCost) { this.lastCostTimestamp = start; } const costReduction = shouldComputeCost ? CostReduction.MEAN : CostReduction.NONE; tidy(() => { const avgCost = this.session.train( this.costTensor, this.trainFeedEntries, this.batchSize, this.optimizer, costReduction); if (shouldComputeCost) { const trainTime = performance.now() - start; this.eventObserver.avgCostCallback(avgCost); if (this.eventObserver.trainExamplesPerSecCallback != null) { const examplesPerSec = (this.batchSize * 1000 / trainTime); this.eventObserver.trainExamplesPerSecCallback(examplesPerSec); } } if (this.eventObserver.metricCallback != null && this.metricFeedEntries != null && start - this.lastEvalTimestamp > this.metricIntervalMs) { this.lastEvalTimestamp = start; if (this.lastComputedMetric != null) { this.lastComputedMetric.dispose(); } this.lastComputedMetric = this.computeMetric(); this.eventObserver.metricCallback(this.lastComputedMetric); } if (this.eventObserver.totalTimeCallback != null) { this.eventObserver.totalTimeCallback( (start - this.trainStartTimestamp) / 1000); } this.batchesTrainedThisRun++; this.totalBatchesTrained++; if (this.eventObserver.batchesTrainedCallback != null) { this.eventObserver.batchesTrainedCallback(this.totalBatchesTrained); } }); requestAnimationFrame(() => this.trainNetwork()); } infer( inferenceTensor: SymbolicTensor, inferenceFeedEntries: FeedEntry[], inferenceExampleIntervalMs = DEFAULT_INFERENCE_EXAMPLE_INTERVAL_MS, inferenceExampleCount = 5, numPasses?: number) { if (this.eventObserver.inferenceExamplesCallback == null && this.eventObserver.inferenceExamplesPerSecCallback == null) { throw new Error( 'Cannot start inference loop, no inference example or ' + 'examples/sec observer provided.'); } // Make sure the feed values are providers, and not NDArrays. for (let i = 0; i < inferenceFeedEntries.length; i++) { const feedEntry = inferenceFeedEntries[i]; if (feedEntry.data instanceof Tensor) { throw new Error( 'Cannot start inference on the model runner with feed entries of ' + 'type NDArray. Please use InputProviders.'); } } this.inferenceExampleIntervalMs = inferenceExampleIntervalMs; this.inferenceTensor = inferenceTensor; this.inferenceFeedEntries = inferenceFeedEntries; this.inferenceExampleCount = inferenceExampleCount; this.currentInferenceLoopNumPasses = numPasses; if (!this.isInferring) { this.inferencePassesThisRun = 0; requestAnimationFrame(() => this.inferNetwork()); } this.isInferring = true; } private inferNetwork() { if (!this.isInferring || this.inferencePassesThisRun === this.currentInferenceLoopNumPasses) { return; } tidy(() => { const feeds: FeedEntry[][] = []; const inferenceValues: Tensor[] = []; const start = performance.now(); for (let i = 0; i < this.inferenceExampleCount; i++) { // Populate a new FeedEntry[] populated with NDArrays. const ndarrayFeedEntries: FeedEntry[] = []; for (let j = 0; j < this.inferenceFeedEntries.length; j++) { const feedEntry = this.inferenceFeedEntries[j]; const nextCopy = (feedEntry.data as InputProvider).getNextCopy(); ndarrayFeedEntries.push({tensor: feedEntry.tensor, data: nextCopy}); } feeds.push(ndarrayFeedEntries); inferenceValues.push( this.session.eval(this.inferenceTensor, ndarrayFeedEntries)); } if (this.eventObserver.inferenceExamplesPerSecCallback != null) { // Force a GPU download, since inference results are generally needed on // the CPU and it's more fair to include blocking on the GPU to complete // its work for the inference measurement. inferenceValues[inferenceValues.length - 1].dataSync(); const inferenceExamplesPerSecTime = performance.now() - start; const examplesPerSec = (this.inferenceExampleCount * 1000 / inferenceExamplesPerSecTime); this.eventObserver.inferenceExamplesPerSecCallback(examplesPerSec); } if (this.eventObserver.inferenceExamplesCallback != null) { this.eventObserver.inferenceExamplesCallback(feeds, inferenceValues); } this.inferencePassesThisRun++; }); this.lastInferTimeoutID = window.setTimeout( () => this.inferNetwork(), this.inferenceExampleIntervalMs); } stopInferring() { this.isInferring = false; window.clearTimeout(this.lastInferTimeoutID); } isInferenceRunning(): boolean { return this.isInferring; } computeMetric(): Scalar { if (this.metricFeedEntries == null) { throw new Error('Cannot compute metric, no metric FeedEntries provided.'); } let metric = this.zeroScalar; return tidy(() => { for (let i = 0; i < this.metricBatchSize; i++) { const metricValue = this.session.eval(this.metricTensor, this.metricFeedEntries) as Tensor; metric = this.math.add(metric, metricValue.toFloat()); } if (this.metricReduction === MetricReduction.MEAN)
return metric; }); } getTotalBatchesTrained(): number { return this.totalBatchesTrained; } getLastComputedMetric(): Scalar { return this.lastComputedMetric; } setMath(math: NDArrayMath) { this.math = math; } setSession(session: Session) { this.session = session; } setInferenceTensor(inferenceTensor: SymbolicTensor) { this.inferenceTensor = inferenceTensor; } setInferenceExampleCount(inferenceExampleCount: number) { this.inferenceExampleCount = inferenceExampleCount; } }
{ metric = this.math.divide(metric, this.metricBatchSizeScalar); }
conditional_block
graph_runner.ts
/** * @license * Copyright 2017 Google Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ import {InputProvider} from '../data/input_provider'; import {tidy} from '../globals'; import {NDArrayMath} from '../math'; import {Optimizer} from '../optimizers/optimizer'; import {Scalar, Tensor} from '../tensor'; import {SymbolicTensor} from './graph'; import {CostReduction, FeedEntry, Session} from './session'; const DEFAULT_EVAL_INTERVAL_MS = 1500; const DEFAULT_COST_INTERVAL_MS = 500; const DEFAULT_INFERENCE_EXAMPLE_INTERVAL_MS = 3000; export interface GraphRunnerEventObserver { batchesTrainedCallback?: (totalBatchesTrained: number) => void; avgCostCallback?: (avgCost: Scalar) => void; metricCallback?: (metric: Tensor) => void; inferenceExamplesCallback?: (feeds: FeedEntry[][], inferenceValues: Tensor[]) => void; inferenceExamplesPerSecCallback?: (examplesPerSec: number) => void; trainExamplesPerSecCallback?: (examplesPerSec: number) => void; totalTimeCallback?: (totalTimeSec: number) => void; doneTrainingCallback?: () => void; } export enum MetricReduction { SUM, MEAN } /** * A class that drives the training of a graph model given a dataset. It allows * the user to provide a set of callbacks for measurements like cost, accuracy, * and speed of training. */ export class GraphRunner { private costTensor: SymbolicTensor; private trainFeedEntries: FeedEntry[]; private batchSize: number; private optimizer: Optimizer; private currentTrainLoopNumBatches: number|undefined; private costIntervalMs: number; private metricTensor: SymbolicTensor|undefined; private metricFeedEntries: FeedEntry[]|undefined; private metricBatchSize: number|undefined; private metricReduction: MetricReduction; private metricIntervalMs: number; private inferenceTensor: SymbolicTensor; private inferenceFeedEntries: FeedEntry[]|undefined; private inferenceExampleIntervalMs: number; private inferenceExampleCount: number; // Runtime information. private isTraining: boolean; private totalBatchesTrained: number; private batchesTrainedThisRun: number; private lastComputedMetric: Scalar; private isInferring: boolean; private lastInferTimeoutID: number; private currentInferenceLoopNumPasses: number|undefined; private inferencePassesThisRun: number; private trainStartTimestamp: number; private lastCostTimestamp = 0; private lastEvalTimestamp = 0; private zeroScalar: Scalar; private metricBatchSizeScalar: Scalar; constructor( private math: NDArrayMath, private session: Session, private eventObserver: GraphRunnerEventObserver) { this.resetStatistics(); this.zeroScalar = Scalar.new(0); } resetStatistics() { this.totalBatchesTrained = 0; } /** * Start the training loop with an optional number of batches to train for. * Optionally takes a metric tensor and feed entries to compute periodically. * This can be used for computing accuracy, or a similar metric. */ train( costTensor: SymbolicTensor, trainFeedEntries: FeedEntry[], batchSize: number, optimizer: Optimizer, numBatches?: number, metricTensor?: SymbolicTensor, metricFeedEntries?: FeedEntry[], metricBatchSize?: number, metricReduction = MetricReduction.MEAN, evalIntervalMs = DEFAULT_EVAL_INTERVAL_MS, costIntervalMs = DEFAULT_COST_INTERVAL_MS) { this.costTensor = costTensor; this.trainFeedEntries = trainFeedEntries; this.metricTensor = metricTensor; this.metricFeedEntries = metricFeedEntries; if (metricBatchSize != null && this.metricBatchSize !== metricBatchSize) { if (this.metricBatchSizeScalar != null) { this.metricBatchSizeScalar.dispose(); } this.metricBatchSizeScalar = Scalar.new(metricBatchSize); } this.metricBatchSize = metricBatchSize; this.metricReduction = metricReduction; this.batchSize = batchSize; this.optimizer = optimizer; this.metricIntervalMs = evalIntervalMs; this.costIntervalMs = costIntervalMs; this.currentTrainLoopNumBatches = numBatches; this.batchesTrainedThisRun = 0; this.isTraining = true; this.trainStartTimestamp = performance.now(); this.trainNetwork(); } stopTraining() { this.isTraining = false; } resumeTraining() { this.isTraining = true; this.trainNetwork(); } private trainNetwork() { if (this.batchesTrainedThisRun === this.currentTrainLoopNumBatches) { this.stopTraining(); } if (!this.isTraining) { if (this.eventObserver.doneTrainingCallback != null) { this.eventObserver.doneTrainingCallback(); } return; } const start = performance.now(); const shouldComputeCost = this.eventObserver.avgCostCallback != null && (start - this.lastCostTimestamp > this.costIntervalMs); if (shouldComputeCost) { this.lastCostTimestamp = start; } const costReduction = shouldComputeCost ? CostReduction.MEAN : CostReduction.NONE; tidy(() => { const avgCost = this.session.train( this.costTensor, this.trainFeedEntries, this.batchSize, this.optimizer, costReduction); if (shouldComputeCost) { const trainTime = performance.now() - start; this.eventObserver.avgCostCallback(avgCost); if (this.eventObserver.trainExamplesPerSecCallback != null) { const examplesPerSec = (this.batchSize * 1000 / trainTime); this.eventObserver.trainExamplesPerSecCallback(examplesPerSec); } } if (this.eventObserver.metricCallback != null && this.metricFeedEntries != null && start - this.lastEvalTimestamp > this.metricIntervalMs) { this.lastEvalTimestamp = start; if (this.lastComputedMetric != null) { this.lastComputedMetric.dispose(); } this.lastComputedMetric = this.computeMetric(); this.eventObserver.metricCallback(this.lastComputedMetric); } if (this.eventObserver.totalTimeCallback != null) { this.eventObserver.totalTimeCallback( (start - this.trainStartTimestamp) / 1000); } this.batchesTrainedThisRun++; this.totalBatchesTrained++; if (this.eventObserver.batchesTrainedCallback != null) { this.eventObserver.batchesTrainedCallback(this.totalBatchesTrained); } }); requestAnimationFrame(() => this.trainNetwork()); } infer( inferenceTensor: SymbolicTensor, inferenceFeedEntries: FeedEntry[], inferenceExampleIntervalMs = DEFAULT_INFERENCE_EXAMPLE_INTERVAL_MS, inferenceExampleCount = 5, numPasses?: number) { if (this.eventObserver.inferenceExamplesCallback == null && this.eventObserver.inferenceExamplesPerSecCallback == null) { throw new Error( 'Cannot start inference loop, no inference example or ' + 'examples/sec observer provided.'); } // Make sure the feed values are providers, and not NDArrays. for (let i = 0; i < inferenceFeedEntries.length; i++) { const feedEntry = inferenceFeedEntries[i]; if (feedEntry.data instanceof Tensor) { throw new Error( 'Cannot start inference on the model runner with feed entries of ' + 'type NDArray. Please use InputProviders.'); } } this.inferenceExampleIntervalMs = inferenceExampleIntervalMs; this.inferenceTensor = inferenceTensor; this.inferenceFeedEntries = inferenceFeedEntries; this.inferenceExampleCount = inferenceExampleCount; this.currentInferenceLoopNumPasses = numPasses; if (!this.isInferring) { this.inferencePassesThisRun = 0; requestAnimationFrame(() => this.inferNetwork()); } this.isInferring = true; } private inferNetwork() { if (!this.isInferring || this.inferencePassesThisRun === this.currentInferenceLoopNumPasses) { return; } tidy(() => { const feeds: FeedEntry[][] = []; const inferenceValues: Tensor[] = []; const start = performance.now(); for (let i = 0; i < this.inferenceExampleCount; i++) { // Populate a new FeedEntry[] populated with NDArrays. const ndarrayFeedEntries: FeedEntry[] = []; for (let j = 0; j < this.inferenceFeedEntries.length; j++) { const feedEntry = this.inferenceFeedEntries[j]; const nextCopy = (feedEntry.data as InputProvider).getNextCopy(); ndarrayFeedEntries.push({tensor: feedEntry.tensor, data: nextCopy}); } feeds.push(ndarrayFeedEntries); inferenceValues.push( this.session.eval(this.inferenceTensor, ndarrayFeedEntries)); } if (this.eventObserver.inferenceExamplesPerSecCallback != null) { // Force a GPU download, since inference results are generally needed on // the CPU and it's more fair to include blocking on the GPU to complete // its work for the inference measurement. inferenceValues[inferenceValues.length - 1].dataSync(); const inferenceExamplesPerSecTime = performance.now() - start; const examplesPerSec = (this.inferenceExampleCount * 1000 / inferenceExamplesPerSecTime); this.eventObserver.inferenceExamplesPerSecCallback(examplesPerSec); } if (this.eventObserver.inferenceExamplesCallback != null) { this.eventObserver.inferenceExamplesCallback(feeds, inferenceValues); } this.inferencePassesThisRun++; }); this.lastInferTimeoutID = window.setTimeout( () => this.inferNetwork(), this.inferenceExampleIntervalMs); } stopInferring() { this.isInferring = false; window.clearTimeout(this.lastInferTimeoutID); } isInferenceRunning(): boolean { return this.isInferring; } computeMetric(): Scalar { if (this.metricFeedEntries == null) { throw new Error('Cannot compute metric, no metric FeedEntries provided.'); } let metric = this.zeroScalar; return tidy(() => { for (let i = 0; i < this.metricBatchSize; i++) { const metricValue = this.session.eval(this.metricTensor, this.metricFeedEntries) as Tensor; metric = this.math.add(metric, metricValue.toFloat()); } if (this.metricReduction === MetricReduction.MEAN) { metric = this.math.divide(metric, this.metricBatchSizeScalar); } return metric; }); } getTotalBatchesTrained(): number { return this.totalBatchesTrained; } getLastComputedMetric(): Scalar
setMath(math: NDArrayMath) { this.math = math; } setSession(session: Session) { this.session = session; } setInferenceTensor(inferenceTensor: SymbolicTensor) { this.inferenceTensor = inferenceTensor; } setInferenceExampleCount(inferenceExampleCount: number) { this.inferenceExampleCount = inferenceExampleCount; } }
{ return this.lastComputedMetric; }
identifier_body
graph_runner.ts
/** * @license * Copyright 2017 Google Inc. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ import {InputProvider} from '../data/input_provider'; import {tidy} from '../globals'; import {NDArrayMath} from '../math'; import {Optimizer} from '../optimizers/optimizer'; import {Scalar, Tensor} from '../tensor'; import {SymbolicTensor} from './graph'; import {CostReduction, FeedEntry, Session} from './session'; const DEFAULT_EVAL_INTERVAL_MS = 1500; const DEFAULT_COST_INTERVAL_MS = 500; const DEFAULT_INFERENCE_EXAMPLE_INTERVAL_MS = 3000; export interface GraphRunnerEventObserver { batchesTrainedCallback?: (totalBatchesTrained: number) => void; avgCostCallback?: (avgCost: Scalar) => void; metricCallback?: (metric: Tensor) => void; inferenceExamplesCallback?: (feeds: FeedEntry[][], inferenceValues: Tensor[]) => void; inferenceExamplesPerSecCallback?: (examplesPerSec: number) => void; trainExamplesPerSecCallback?: (examplesPerSec: number) => void; totalTimeCallback?: (totalTimeSec: number) => void; doneTrainingCallback?: () => void; } export enum MetricReduction { SUM, MEAN } /** * A class that drives the training of a graph model given a dataset. It allows * the user to provide a set of callbacks for measurements like cost, accuracy, * and speed of training. */ export class GraphRunner { private costTensor: SymbolicTensor; private trainFeedEntries: FeedEntry[]; private batchSize: number; private optimizer: Optimizer; private currentTrainLoopNumBatches: number|undefined; private costIntervalMs: number; private metricTensor: SymbolicTensor|undefined; private metricFeedEntries: FeedEntry[]|undefined; private metricBatchSize: number|undefined; private metricReduction: MetricReduction; private metricIntervalMs: number; private inferenceTensor: SymbolicTensor; private inferenceFeedEntries: FeedEntry[]|undefined; private inferenceExampleIntervalMs: number; private inferenceExampleCount: number; // Runtime information. private isTraining: boolean; private totalBatchesTrained: number; private batchesTrainedThisRun: number; private lastComputedMetric: Scalar; private isInferring: boolean; private lastInferTimeoutID: number; private currentInferenceLoopNumPasses: number|undefined; private inferencePassesThisRun: number; private trainStartTimestamp: number; private lastCostTimestamp = 0; private lastEvalTimestamp = 0; private zeroScalar: Scalar; private metricBatchSizeScalar: Scalar; constructor( private math: NDArrayMath, private session: Session, private eventObserver: GraphRunnerEventObserver) { this.resetStatistics(); this.zeroScalar = Scalar.new(0); } resetStatistics() { this.totalBatchesTrained = 0; } /** * Start the training loop with an optional number of batches to train for. * Optionally takes a metric tensor and feed entries to compute periodically. * This can be used for computing accuracy, or a similar metric. */ train( costTensor: SymbolicTensor, trainFeedEntries: FeedEntry[], batchSize: number, optimizer: Optimizer, numBatches?: number, metricTensor?: SymbolicTensor, metricFeedEntries?: FeedEntry[], metricBatchSize?: number, metricReduction = MetricReduction.MEAN, evalIntervalMs = DEFAULT_EVAL_INTERVAL_MS, costIntervalMs = DEFAULT_COST_INTERVAL_MS) { this.costTensor = costTensor; this.trainFeedEntries = trainFeedEntries; this.metricTensor = metricTensor; this.metricFeedEntries = metricFeedEntries; if (metricBatchSize != null && this.metricBatchSize !== metricBatchSize) { if (this.metricBatchSizeScalar != null) { this.metricBatchSizeScalar.dispose(); } this.metricBatchSizeScalar = Scalar.new(metricBatchSize); } this.metricBatchSize = metricBatchSize; this.metricReduction = metricReduction; this.batchSize = batchSize; this.optimizer = optimizer; this.metricIntervalMs = evalIntervalMs; this.costIntervalMs = costIntervalMs; this.currentTrainLoopNumBatches = numBatches; this.batchesTrainedThisRun = 0; this.isTraining = true; this.trainStartTimestamp = performance.now(); this.trainNetwork(); } stopTraining() { this.isTraining = false; } resumeTraining() { this.isTraining = true; this.trainNetwork(); } private trainNetwork() { if (this.batchesTrainedThisRun === this.currentTrainLoopNumBatches) { this.stopTraining(); } if (!this.isTraining) { if (this.eventObserver.doneTrainingCallback != null) { this.eventObserver.doneTrainingCallback(); } return; } const start = performance.now(); const shouldComputeCost = this.eventObserver.avgCostCallback != null && (start - this.lastCostTimestamp > this.costIntervalMs); if (shouldComputeCost) { this.lastCostTimestamp = start; } const costReduction = shouldComputeCost ? CostReduction.MEAN : CostReduction.NONE; tidy(() => { const avgCost = this.session.train( this.costTensor, this.trainFeedEntries, this.batchSize, this.optimizer, costReduction); if (shouldComputeCost) { const trainTime = performance.now() - start; this.eventObserver.avgCostCallback(avgCost); if (this.eventObserver.trainExamplesPerSecCallback != null) { const examplesPerSec = (this.batchSize * 1000 / trainTime); this.eventObserver.trainExamplesPerSecCallback(examplesPerSec); } } if (this.eventObserver.metricCallback != null && this.metricFeedEntries != null && start - this.lastEvalTimestamp > this.metricIntervalMs) { this.lastEvalTimestamp = start; if (this.lastComputedMetric != null) { this.lastComputedMetric.dispose(); } this.lastComputedMetric = this.computeMetric(); this.eventObserver.metricCallback(this.lastComputedMetric); } if (this.eventObserver.totalTimeCallback != null) { this.eventObserver.totalTimeCallback( (start - this.trainStartTimestamp) / 1000); } this.batchesTrainedThisRun++; this.totalBatchesTrained++; if (this.eventObserver.batchesTrainedCallback != null) { this.eventObserver.batchesTrainedCallback(this.totalBatchesTrained); } }); requestAnimationFrame(() => this.trainNetwork()); } infer( inferenceTensor: SymbolicTensor, inferenceFeedEntries: FeedEntry[], inferenceExampleIntervalMs = DEFAULT_INFERENCE_EXAMPLE_INTERVAL_MS, inferenceExampleCount = 5, numPasses?: number) { if (this.eventObserver.inferenceExamplesCallback == null && this.eventObserver.inferenceExamplesPerSecCallback == null) { throw new Error( 'Cannot start inference loop, no inference example or ' + 'examples/sec observer provided.'); } // Make sure the feed values are providers, and not NDArrays. for (let i = 0; i < inferenceFeedEntries.length; i++) { const feedEntry = inferenceFeedEntries[i]; if (feedEntry.data instanceof Tensor) { throw new Error( 'Cannot start inference on the model runner with feed entries of ' + 'type NDArray. Please use InputProviders.'); } } this.inferenceExampleIntervalMs = inferenceExampleIntervalMs; this.inferenceTensor = inferenceTensor; this.inferenceFeedEntries = inferenceFeedEntries; this.inferenceExampleCount = inferenceExampleCount; this.currentInferenceLoopNumPasses = numPasses; if (!this.isInferring) { this.inferencePassesThisRun = 0; requestAnimationFrame(() => this.inferNetwork()); } this.isInferring = true; } private inferNetwork() { if (!this.isInferring || this.inferencePassesThisRun === this.currentInferenceLoopNumPasses) { return; } tidy(() => { const feeds: FeedEntry[][] = []; const inferenceValues: Tensor[] = []; const start = performance.now(); for (let i = 0; i < this.inferenceExampleCount; i++) { // Populate a new FeedEntry[] populated with NDArrays. const ndarrayFeedEntries: FeedEntry[] = []; for (let j = 0; j < this.inferenceFeedEntries.length; j++) { const feedEntry = this.inferenceFeedEntries[j]; const nextCopy = (feedEntry.data as InputProvider).getNextCopy(); ndarrayFeedEntries.push({tensor: feedEntry.tensor, data: nextCopy}); } feeds.push(ndarrayFeedEntries); inferenceValues.push( this.session.eval(this.inferenceTensor, ndarrayFeedEntries)); } if (this.eventObserver.inferenceExamplesPerSecCallback != null) { // Force a GPU download, since inference results are generally needed on // the CPU and it's more fair to include blocking on the GPU to complete // its work for the inference measurement. inferenceValues[inferenceValues.length - 1].dataSync(); const inferenceExamplesPerSecTime = performance.now() - start; const examplesPerSec = (this.inferenceExampleCount * 1000 / inferenceExamplesPerSecTime); this.eventObserver.inferenceExamplesPerSecCallback(examplesPerSec); } if (this.eventObserver.inferenceExamplesCallback != null) { this.eventObserver.inferenceExamplesCallback(feeds, inferenceValues); } this.inferencePassesThisRun++; }); this.lastInferTimeoutID = window.setTimeout( () => this.inferNetwork(), this.inferenceExampleIntervalMs); } stopInferring() { this.isInferring = false; window.clearTimeout(this.lastInferTimeoutID); } isInferenceRunning(): boolean { return this.isInferring; } computeMetric(): Scalar { if (this.metricFeedEntries == null) { throw new Error('Cannot compute metric, no metric FeedEntries provided.'); } let metric = this.zeroScalar; return tidy(() => { for (let i = 0; i < this.metricBatchSize; i++) { const metricValue = this.session.eval(this.metricTensor, this.metricFeedEntries) as Tensor; metric = this.math.add(metric, metricValue.toFloat()); } if (this.metricReduction === MetricReduction.MEAN) { metric = this.math.divide(metric, this.metricBatchSizeScalar); } return metric; }); } getTotalBatchesTrained(): number { return this.totalBatchesTrained; }
setMath(math: NDArrayMath) { this.math = math; } setSession(session: Session) { this.session = session; } setInferenceTensor(inferenceTensor: SymbolicTensor) { this.inferenceTensor = inferenceTensor; } setInferenceExampleCount(inferenceExampleCount: number) { this.inferenceExampleCount = inferenceExampleCount; } }
getLastComputedMetric(): Scalar { return this.lastComputedMetric; }
random_line_split
training_modified.py
#!/usr/bin/env python3 import tensorflow as tf from tensorflow.keras.utils import Sequence from glob import glob import os from datetime import datetime # turn off GPU processing because # tensorflow-gpu can lead to trouble if not installed correctly # os.environ["CUDA_VISIBLE_DEVICES"] = "-1" ######## This script is a copy of training.py and enables training on our local GPU in order for us to properly ######## use tensorboard which didn't work on the server and introduce a validation set. It therefore modifies ######## the data generator to only load half the augmented images in one batch and the rest later. There is also ######## a ValDataGenerator that is used for the validation and test data where no augmentation is desired. # This script trains the neural network. # First it defines the data pipeline that loads the images and masks and preprocesses them. # Then it defines the custom loss function. # Afterwards the architecture of the neural network is defined and lastly the training procedure. ########## DATA PIPELINE ########## # turns a RGB mask into 4 2D binary class maps where each cell has value 1 if # the corresponding pixel in the original pixel had the color of the class def to_one_hot(mask): # these are the RGB colors that the masks use, if these values or the colors # in the mask images change every class map will be filled with zeros! colors = [ [227, 26, 28], # Red [65, 117, 5], # Green [106, 61, 154], # Violet [31, 120, 180] # Blue ] one_hot_mask = [] for color in colors: # tf.equal compares every pixel of the mask with the current RGB color # and returns a matrix where a cell is TRUE if the corresponding pixel had the color of the current class # reduce all then turns this boolean matrix into a 2D map class_mask = tf.reduce_all(tf.equal(mask, color), axis=-1) class_mask = tf.cast(class_mask, tf.float32) one_hot_mask.append(class_mask) # after we have all the 2D class maps we stack on top of each other to get one tensor one_hot_encoded_mask = tf.stack(one_hot_mask, axis=-1) return one_hot_encoded_mask # parses the masks, they are converted to float 32 later in to_one_hot def parse_mask(name): mask = tf.io.read_file(name) mask = tf.image.decode_png(mask) # this cuts the borders off, height and width need to be multiples of 32! # mask = tf.image.resize(mask, [256, 256]) # for local testing mask = tf.image.crop_to_bounding_box(mask, offset_height=44, offset_width=304, target_height=992, target_width=1312) return mask # parses the images def parse_image(name): image = tf.io.read_file(name) image = tf.image.decode_jpeg(image) image = tf.image.convert_image_dtype(image, tf.float32) # Neural Nets work with float 32 #image = tf.image.rgb_to_grayscale(image) # this cuts the borders off, height and width need to be multiples of 32! # image = tf.image.resize(image, [256, 256]) # for local testing image = tf.image.crop_to_bounding_box(image, offset_height=44, offset_width=304, target_height=992, target_width=1312) return image # this generator streams the images and masks to the GPU one after another # it gets a list of pairs that correspond to the directory paths of images and their corresponding masks # each batch includes a training image, its mask and the 7 augmented versions of it, which are generated on the fly # batch size means in this case how many original images are loaded for 1 batch, # the actual batch size is 8 times higher. # based on https://www.kaggle.com/mukulkr/camvid-segmentation-using-unet class DataGenerator(Sequence): def __init__(self, pair, batch_size, shuffle): self.pair = pair self.batch_size = batch_size self.shuffle = shuffle self.on_epoch_end() self.map = dict() # returns the length of the data set def __len__(self): return int(tf.math.floor(len(self.pair) / self.batch_size)) * 2 # returns a single batch def __getitem__(self, index): # a list that has the indexes of the pairs from which we want to generate images and masks for the batch index = index // 2 indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size] list_IDs_temp = [k for k in indexes] if index in self.map: return self.__data_generation(self.map[index], second_half=True) self.map[index] = list_IDs_temp X, Y = self.__data_generation(list_IDs_temp) return X, Y # resets the pair indexes after each epoch and shuffles the indexes so that the batches # are in different order for every epoch def on_epoch_end(self): self.indexes = tf.range(len(self.pair)) self.map = dict() if self.shuffle == True: tf.random.shuffle(self.indexes) # generates a batch def __data_generation(self, list_IDs_temp, second_half=False): batch_images = [] batch_masks = [] for i in list_IDs_temp: # parses the image and the mask of the current pair and then generates the augmented versions # it wasn't possible to generate the augmented images beforehand and have completely # random images in every batch image1 = parse_image(self.pair[i][0]) batch_images.append(image1) mask1 = parse_mask(self.pair[i][1]) mask1 = to_one_hot(mask1) batch_masks.append(mask1) image2 = tf.image.flip_left_right(image1) batch_images.append(image2) mask2 = tf.image.flip_left_right(mask1) batch_masks.append(mask2) image3 = tf.image.flip_up_down(image1) batch_images.append(image3) mask3 = tf.image.flip_up_down(mask1) batch_masks.append(mask3) image4 = tf.image.flip_up_down(image1) image4 = tf.image.flip_left_right(image4) batch_images.append(image4) mask4 = tf.image.flip_up_down(mask1) mask4 = tf.image.flip_left_right(mask4) batch_masks.append(mask4) if second_half: # images and masks 1 to 4 but with randomly changed brightness delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image5 = tf.image.adjust_brightness(image1, delta) batch_images.append(image5) batch_masks.append(mask1) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image6 = tf.image.flip_left_right(image1) image6 = tf.image.adjust_brightness(image6, delta) batch_images.append(image6) mask6 = tf.image.flip_left_right(mask1) batch_masks.append(mask6) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image7 = tf.image.flip_up_down(image1) image7 = tf.image.adjust_brightness(image7, delta) batch_images.append(image7) mask7 = tf.image.flip_up_down(mask1) batch_masks.append(mask7) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image8 = tf.image.flip_up_down(image1) image8 = tf.image.flip_left_right(image8) image8 = tf.image.adjust_brightness(image8, delta) batch_images.append(image8) mask8 = tf.image.flip_up_down(mask1) mask8 = tf.image.flip_left_right(mask8) batch_masks.append(mask8) return tf.stack(batch_images[4:]), tf.stack(batch_masks[4:]) # stack the images and masks of the batch into two tensors return tf.stack(batch_images[:4]), tf.stack(batch_masks[:4]) # Data generator that does not augment images, i.e. used for validation and test set class ValDataGenerator(Sequence): def __init__(self, pair, batch_size, shuffle): self.pair = pair self.batch_size = batch_size self.shuffle = shuffle self.on_epoch_end() # returns the length of the data set def __len__(self): return int(tf.math.floor(len(self.pair) / self.batch_size)) # returns a single batch def __getitem__(self, index): # a list that has the indexes of the pairs from which we want to generate images and masks for the batch indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size] list_IDs_temp = [k for k in indexes] X, Y = self.__data_generation(list_IDs_temp) return X, Y # resets the pair indexes after each epoch and shuffles the indexes so that the batches # are in different order for every epoch def on_epoch_end(self): self.indexes = tf.range(len(self.pair)) if self.shuffle == True:
# generates a batch def __data_generation(self, list_IDs_temp, second_half=False): batch_images = [] batch_masks = [] for i in list_IDs_temp: # parses the image and the mask of the current pair and then generates the augmented versions # it wasn't possible to generate the augmented images beforehand and have completely # random images in every batch image1 = parse_image(self.pair[i][0]) batch_images.append(image1) mask1 = parse_mask(self.pair[i][1]) mask1 = to_one_hot(mask1) batch_masks.append(mask1) # stack the images and masks of the batch into two tensors return tf.stack(batch_images), tf.stack(batch_masks) # takes a path to a directory with two sub folders for training images and masks # and returns a list of pairs of paths for images and the corresponding masks def make_pairs(path, set): pairs = [] # sorted is very important since os.path.join somehow shuffles the paths and we need # the image and mask paths to have the exact same order image_paths = sorted(glob(os.path.join(path, set + "_images/*"))) mask_paths = sorted(glob(os.path.join(path, set + "_masks/*"))) #image_paths = sorted(glob(os.path.join(path, "test_images2/*"))) #mask_paths = sorted(glob(os.path.join(path, "test_masks2/*"))) for i in range(len(image_paths)): pairs.append((image_paths[i], mask_paths[i])) return pairs ########## LOSS FUNCTION ########## # based on https://github.com/aruns2120/Semantic-Segmentation-Severstal/blob/master/U-Net/CS2_firstCut.ipynb # the dice coefficient calculates how much the predicted mask and the correct mask overlap def dice_coef(y_true, y_predict, smooth=1): y_true_flat = tf.keras.backend.flatten(y_true) y_pred_flat = tf.keras.backend.flatten(y_predict) intersection = tf.keras.backend.sum(y_true_flat * y_pred_flat) return (2. * intersection + smooth) / (tf.keras.backend.sum(y_true_flat) + tf.keras.backend.sum(y_pred_flat) + smooth) def dice_loss(y_true, y_predict): return (1 - dice_coef(y_true, y_predict)) # weighted variant of pixelwise_crossentropy # based on https://www.gitmemory.com/issue/keras-team/keras/6261/569715992 def pixelwise_crossentropy(y_true, y_predicted):# # weights that scale the error for each class such that they all have equal impact on the loss # important since the data set is very unbalanced # weights represent the inverse of the proportion of pixels corresponding to that class in the whole data set # needs to be divided by 100.0 to keep the error at a similar magnitude during training weight_proton = 132.0 / 100.0 weight_alpha = 91.0 / 100.0 weight_V = 311.0 / 100.0 weight_electron = 71.0 / 100.0 # weight_proton = 1.0 # for local testing # weight_alpha = 1.0 # weight_V = 1.0 # weight_electron = 1.0 weights = [weight_proton, weight_alpha, weight_V, weight_electron] # predicted values get scaled such that they are never exactly 0 or 1 since then the logarithm diverges y_predicted /= tf.keras.backend.sum(y_predicted, axis=-1, keepdims=True) y_predicted = tf.keras.backend.clip(y_predicted, tf.keras.backend.epsilon(), 1. - tf.keras.backend.epsilon()) # compute the weighted cross_entropy loss = y_true * tf.keras.backend.log(y_predicted) loss = -tf.keras.backend.sum(loss * weights, -1) return loss # defines the custom loss function, sum of dice_loss and pixelwise_crossentropy def pce_dice_loss(y_true, y_predict): return pixelwise_crossentropy(y_true, y_predict) + dice_loss(y_true, y_predict) ########## NEURAL NETWORK ########## # based on https://github.com/aruns2120/Semantic-Segmentation-Severstal/blob/master/U-Net/CS2_firstCut.ipynb # defines the single convolutional blocks def conv_block(input, amount_filters, kernel_size): x = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer="he_normal", padding="same")(input) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation("relu")(x) x = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer="he_normal", padding="same")(x) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation("relu")(x) return x # defines the U-Net architecture # amount filters controls the amount of filters in the convolutional layers, needs to be a power of 2! def unet(input, amount_filters): # Encoder conv_block1 = conv_block(input, amount_filters, 3) pooling1 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block1) # randomly deactivates in each training step 20% of neurons, gives better generalization dropout1 = tf.keras.layers.Dropout(0.2)(pooling1) conv_block2 = conv_block(dropout1, amount_filters * 2, 3) pooling2 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block2) dropout2 = tf.keras.layers.Dropout(0.2)(pooling2) conv_block3 = conv_block(dropout2, amount_filters * 4, 3) pooling3 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block3) dropout3 = tf.keras.layers.Dropout(0.2)(pooling3) conv_block4 = conv_block(dropout3, amount_filters * 8, 3) pooling4 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block4) dropout4 = tf.keras.layers.Dropout(0.2)(pooling4) encoded_features = conv_block(dropout4, amount_filters * 16, 3) # Decoder upsample_block1 = tf.keras.layers.UpSampling2D()(encoded_features) upsample_block1 = tf.keras.layers.Conv2D(filters=amount_filters * 8, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block1) upsample_block1 = tf.keras.layers.concatenate([upsample_block1, conv_block4]) # skip connection dropout5 = tf.keras.layers.Dropout(0.2)(upsample_block1) conv_block5 = conv_block(dropout5, amount_filters * 8, 3) upsample_block2 = tf.keras.layers.UpSampling2D()(conv_block5) upsample_block2 = tf.keras.layers.Conv2D(filters=amount_filters * 4, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block2) upsample_block2 = tf.keras.layers.concatenate([upsample_block2, conv_block3]) dropout6 = tf.keras.layers.Dropout(0.2)(upsample_block2) conv_block6 = conv_block(dropout6, amount_filters * 4, 3) upsample_block3 = tf.keras.layers.UpSampling2D()(conv_block6) upsample_block3 = tf.keras.layers.Conv2D(filters=amount_filters * 2, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block3) upsample_block3 = tf.keras.layers.concatenate([upsample_block3, conv_block2]) dropout7 = tf.keras.layers.Dropout(0.2)(upsample_block3) conv_block7 = conv_block(dropout7, amount_filters * 2, 3) upsample_block4 = tf.keras.layers.UpSampling2D()(conv_block7) upsample_block4 = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block4) upsample_block4 = tf.keras.layers.concatenate([upsample_block4, conv_block1]) dropout8 = tf.keras.layers.Dropout(0.2)(upsample_block4) conv_block8 = conv_block(dropout8, amount_filters, 3) # amount of filters in output layer needs to be equal to the amount of classes output = tf.keras.layers.Conv2D(filters=4, kernel_size=(1, 1), activation="sigmoid")(conv_block8) unet = tf.keras.Model(inputs=[input], outputs=[output]) return unet ########## TRAINING ########## # this creates the data generator that is given to the neural net # batch_size 1 means that 1 original image is used for every batch # batch_size needs to be a power of 2! # pairs = make_pairs("/home/mlps_team003/CloudChamberProject") pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/TrainingSet2", "training") batch_size = 1 trainset_length = len(pairs) * 2 steps_per_epoch = trainset_length // batch_size train_generator = DataGenerator(pair=pairs, batch_size=batch_size, shuffle=True) val_pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/ValidationSet", "validation") val_generator = ValDataGenerator(pair=val_pairs, batch_size=batch_size, shuffle=True) test_pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/TestSet", "test") test_generator = ValDataGenerator(pair=test_pairs, batch_size=batch_size, shuffle=True) # creates the neural net # input = tf.keras.layers.Input(shape=[256, 256, 3]) for local testing input = tf.keras.layers.Input(shape=[992, 1312, 3]) # shape of the input images unet = unet(input, 8) # compiles the neural net optimizer = tf.keras.optimizers.Adam() # better than stochastic gradient decent unet.compile(optimizer=optimizer, loss=pce_dice_loss, metrics=[dice_coef, pixelwise_crossentropy]) """ needs to be uncommented to use the weights from the pretrained unsupervised model # loads the pretrained model and extracts the names of the layers pretrained_unet = tf.keras.models.load_model("pretraining.h5") pretrained_layers = pretrained_unet.layers # copies the pretrained weights to our neural net. We cant copy certain layers since the # pretraining NN skip connections which changes the amount of parameters of some layers for layer in pretrained_layers: if (layer.name != "conv2d_11" and layer.name != "conv2d_14" and layer.name != "conv2d_17" and layer.name != "conv2d_20" and layer.name != "conv2d_22"): untrained_layer = unet.get_layer(name=layer.name) # retrieves the untrained layer pretrained_layer = pretrained_unet.get_layer(name=layer.name) # retrieves the trained layer untrained_layer.set_weights(pretrained_layer.get_weights()) # copies weights """ # callback for logging training metrics that can be displayed in Tensorboard # with the command 'Tensorboard --logdir training_logs/train' metrics_logger = tf.keras.callbacks.TensorBoard(log_dir="./logs/" + datetime.now().strftime("%Y%m%d-%H%M%S"), update_freq='epoch', write_images=True) # callback for saving the best model model_checkpoint = tf.keras.callbacks.ModelCheckpoint('checkpoints/pretraining', monitor='val_loss', save_best_only=True) # trains it unet_history = unet.fit( x=train_generator, epochs=40, steps_per_epoch=steps_per_epoch, callbacks=[metrics_logger, model_checkpoint], validation_data=val_generator ) # Evaluates it results = unet.evaluate(test_generator) print("Test loss, dice_coef and pixelwise crossentropy: ", results) # saves the computed weights and the network architecture # when loading the neural net from the h5 file it first needs to be recompiled # since tensorflow has trouble with the custom loss function unet.save("unet.h5")
tf.random.shuffle(self.indexes)
conditional_block
training_modified.py
#!/usr/bin/env python3 import tensorflow as tf from tensorflow.keras.utils import Sequence from glob import glob import os from datetime import datetime # turn off GPU processing because # tensorflow-gpu can lead to trouble if not installed correctly # os.environ["CUDA_VISIBLE_DEVICES"] = "-1" ######## This script is a copy of training.py and enables training on our local GPU in order for us to properly ######## use tensorboard which didn't work on the server and introduce a validation set. It therefore modifies ######## the data generator to only load half the augmented images in one batch and the rest later. There is also ######## a ValDataGenerator that is used for the validation and test data where no augmentation is desired. # This script trains the neural network. # First it defines the data pipeline that loads the images and masks and preprocesses them. # Then it defines the custom loss function. # Afterwards the architecture of the neural network is defined and lastly the training procedure. ########## DATA PIPELINE ########## # turns a RGB mask into 4 2D binary class maps where each cell has value 1 if # the corresponding pixel in the original pixel had the color of the class def to_one_hot(mask): # these are the RGB colors that the masks use, if these values or the colors # in the mask images change every class map will be filled with zeros! colors = [ [227, 26, 28], # Red [65, 117, 5], # Green [106, 61, 154], # Violet [31, 120, 180] # Blue ] one_hot_mask = [] for color in colors: # tf.equal compares every pixel of the mask with the current RGB color # and returns a matrix where a cell is TRUE if the corresponding pixel had the color of the current class # reduce all then turns this boolean matrix into a 2D map class_mask = tf.reduce_all(tf.equal(mask, color), axis=-1) class_mask = tf.cast(class_mask, tf.float32) one_hot_mask.append(class_mask) # after we have all the 2D class maps we stack on top of each other to get one tensor one_hot_encoded_mask = tf.stack(one_hot_mask, axis=-1) return one_hot_encoded_mask # parses the masks, they are converted to float 32 later in to_one_hot def parse_mask(name): mask = tf.io.read_file(name) mask = tf.image.decode_png(mask) # this cuts the borders off, height and width need to be multiples of 32! # mask = tf.image.resize(mask, [256, 256]) # for local testing mask = tf.image.crop_to_bounding_box(mask, offset_height=44, offset_width=304, target_height=992, target_width=1312) return mask # parses the images def parse_image(name): image = tf.io.read_file(name) image = tf.image.decode_jpeg(image) image = tf.image.convert_image_dtype(image, tf.float32) # Neural Nets work with float 32 #image = tf.image.rgb_to_grayscale(image) # this cuts the borders off, height and width need to be multiples of 32! # image = tf.image.resize(image, [256, 256]) # for local testing image = tf.image.crop_to_bounding_box(image, offset_height=44, offset_width=304, target_height=992, target_width=1312) return image # this generator streams the images and masks to the GPU one after another # it gets a list of pairs that correspond to the directory paths of images and their corresponding masks # each batch includes a training image, its mask and the 7 augmented versions of it, which are generated on the fly # batch size means in this case how many original images are loaded for 1 batch, # the actual batch size is 8 times higher. # based on https://www.kaggle.com/mukulkr/camvid-segmentation-using-unet class DataGenerator(Sequence): def __init__(self, pair, batch_size, shuffle): self.pair = pair self.batch_size = batch_size self.shuffle = shuffle self.on_epoch_end() self.map = dict() # returns the length of the data set def __len__(self): return int(tf.math.floor(len(self.pair) / self.batch_size)) * 2 # returns a single batch def __getitem__(self, index): # a list that has the indexes of the pairs from which we want to generate images and masks for the batch index = index // 2 indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size] list_IDs_temp = [k for k in indexes] if index in self.map: return self.__data_generation(self.map[index], second_half=True) self.map[index] = list_IDs_temp X, Y = self.__data_generation(list_IDs_temp) return X, Y # resets the pair indexes after each epoch and shuffles the indexes so that the batches # are in different order for every epoch def on_epoch_end(self): self.indexes = tf.range(len(self.pair)) self.map = dict()
def __data_generation(self, list_IDs_temp, second_half=False): batch_images = [] batch_masks = [] for i in list_IDs_temp: # parses the image and the mask of the current pair and then generates the augmented versions # it wasn't possible to generate the augmented images beforehand and have completely # random images in every batch image1 = parse_image(self.pair[i][0]) batch_images.append(image1) mask1 = parse_mask(self.pair[i][1]) mask1 = to_one_hot(mask1) batch_masks.append(mask1) image2 = tf.image.flip_left_right(image1) batch_images.append(image2) mask2 = tf.image.flip_left_right(mask1) batch_masks.append(mask2) image3 = tf.image.flip_up_down(image1) batch_images.append(image3) mask3 = tf.image.flip_up_down(mask1) batch_masks.append(mask3) image4 = tf.image.flip_up_down(image1) image4 = tf.image.flip_left_right(image4) batch_images.append(image4) mask4 = tf.image.flip_up_down(mask1) mask4 = tf.image.flip_left_right(mask4) batch_masks.append(mask4) if second_half: # images and masks 1 to 4 but with randomly changed brightness delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image5 = tf.image.adjust_brightness(image1, delta) batch_images.append(image5) batch_masks.append(mask1) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image6 = tf.image.flip_left_right(image1) image6 = tf.image.adjust_brightness(image6, delta) batch_images.append(image6) mask6 = tf.image.flip_left_right(mask1) batch_masks.append(mask6) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image7 = tf.image.flip_up_down(image1) image7 = tf.image.adjust_brightness(image7, delta) batch_images.append(image7) mask7 = tf.image.flip_up_down(mask1) batch_masks.append(mask7) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image8 = tf.image.flip_up_down(image1) image8 = tf.image.flip_left_right(image8) image8 = tf.image.adjust_brightness(image8, delta) batch_images.append(image8) mask8 = tf.image.flip_up_down(mask1) mask8 = tf.image.flip_left_right(mask8) batch_masks.append(mask8) return tf.stack(batch_images[4:]), tf.stack(batch_masks[4:]) # stack the images and masks of the batch into two tensors return tf.stack(batch_images[:4]), tf.stack(batch_masks[:4]) # Data generator that does not augment images, i.e. used for validation and test set class ValDataGenerator(Sequence): def __init__(self, pair, batch_size, shuffle): self.pair = pair self.batch_size = batch_size self.shuffle = shuffle self.on_epoch_end() # returns the length of the data set def __len__(self): return int(tf.math.floor(len(self.pair) / self.batch_size)) # returns a single batch def __getitem__(self, index): # a list that has the indexes of the pairs from which we want to generate images and masks for the batch indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size] list_IDs_temp = [k for k in indexes] X, Y = self.__data_generation(list_IDs_temp) return X, Y # resets the pair indexes after each epoch and shuffles the indexes so that the batches # are in different order for every epoch def on_epoch_end(self): self.indexes = tf.range(len(self.pair)) if self.shuffle == True: tf.random.shuffle(self.indexes) # generates a batch def __data_generation(self, list_IDs_temp, second_half=False): batch_images = [] batch_masks = [] for i in list_IDs_temp: # parses the image and the mask of the current pair and then generates the augmented versions # it wasn't possible to generate the augmented images beforehand and have completely # random images in every batch image1 = parse_image(self.pair[i][0]) batch_images.append(image1) mask1 = parse_mask(self.pair[i][1]) mask1 = to_one_hot(mask1) batch_masks.append(mask1) # stack the images and masks of the batch into two tensors return tf.stack(batch_images), tf.stack(batch_masks) # takes a path to a directory with two sub folders for training images and masks # and returns a list of pairs of paths for images and the corresponding masks def make_pairs(path, set): pairs = [] # sorted is very important since os.path.join somehow shuffles the paths and we need # the image and mask paths to have the exact same order image_paths = sorted(glob(os.path.join(path, set + "_images/*"))) mask_paths = sorted(glob(os.path.join(path, set + "_masks/*"))) #image_paths = sorted(glob(os.path.join(path, "test_images2/*"))) #mask_paths = sorted(glob(os.path.join(path, "test_masks2/*"))) for i in range(len(image_paths)): pairs.append((image_paths[i], mask_paths[i])) return pairs ########## LOSS FUNCTION ########## # based on https://github.com/aruns2120/Semantic-Segmentation-Severstal/blob/master/U-Net/CS2_firstCut.ipynb # the dice coefficient calculates how much the predicted mask and the correct mask overlap def dice_coef(y_true, y_predict, smooth=1): y_true_flat = tf.keras.backend.flatten(y_true) y_pred_flat = tf.keras.backend.flatten(y_predict) intersection = tf.keras.backend.sum(y_true_flat * y_pred_flat) return (2. * intersection + smooth) / (tf.keras.backend.sum(y_true_flat) + tf.keras.backend.sum(y_pred_flat) + smooth) def dice_loss(y_true, y_predict): return (1 - dice_coef(y_true, y_predict)) # weighted variant of pixelwise_crossentropy # based on https://www.gitmemory.com/issue/keras-team/keras/6261/569715992 def pixelwise_crossentropy(y_true, y_predicted):# # weights that scale the error for each class such that they all have equal impact on the loss # important since the data set is very unbalanced # weights represent the inverse of the proportion of pixels corresponding to that class in the whole data set # needs to be divided by 100.0 to keep the error at a similar magnitude during training weight_proton = 132.0 / 100.0 weight_alpha = 91.0 / 100.0 weight_V = 311.0 / 100.0 weight_electron = 71.0 / 100.0 # weight_proton = 1.0 # for local testing # weight_alpha = 1.0 # weight_V = 1.0 # weight_electron = 1.0 weights = [weight_proton, weight_alpha, weight_V, weight_electron] # predicted values get scaled such that they are never exactly 0 or 1 since then the logarithm diverges y_predicted /= tf.keras.backend.sum(y_predicted, axis=-1, keepdims=True) y_predicted = tf.keras.backend.clip(y_predicted, tf.keras.backend.epsilon(), 1. - tf.keras.backend.epsilon()) # compute the weighted cross_entropy loss = y_true * tf.keras.backend.log(y_predicted) loss = -tf.keras.backend.sum(loss * weights, -1) return loss # defines the custom loss function, sum of dice_loss and pixelwise_crossentropy def pce_dice_loss(y_true, y_predict): return pixelwise_crossentropy(y_true, y_predict) + dice_loss(y_true, y_predict) ########## NEURAL NETWORK ########## # based on https://github.com/aruns2120/Semantic-Segmentation-Severstal/blob/master/U-Net/CS2_firstCut.ipynb # defines the single convolutional blocks def conv_block(input, amount_filters, kernel_size): x = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer="he_normal", padding="same")(input) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation("relu")(x) x = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer="he_normal", padding="same")(x) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation("relu")(x) return x # defines the U-Net architecture # amount filters controls the amount of filters in the convolutional layers, needs to be a power of 2! def unet(input, amount_filters): # Encoder conv_block1 = conv_block(input, amount_filters, 3) pooling1 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block1) # randomly deactivates in each training step 20% of neurons, gives better generalization dropout1 = tf.keras.layers.Dropout(0.2)(pooling1) conv_block2 = conv_block(dropout1, amount_filters * 2, 3) pooling2 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block2) dropout2 = tf.keras.layers.Dropout(0.2)(pooling2) conv_block3 = conv_block(dropout2, amount_filters * 4, 3) pooling3 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block3) dropout3 = tf.keras.layers.Dropout(0.2)(pooling3) conv_block4 = conv_block(dropout3, amount_filters * 8, 3) pooling4 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block4) dropout4 = tf.keras.layers.Dropout(0.2)(pooling4) encoded_features = conv_block(dropout4, amount_filters * 16, 3) # Decoder upsample_block1 = tf.keras.layers.UpSampling2D()(encoded_features) upsample_block1 = tf.keras.layers.Conv2D(filters=amount_filters * 8, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block1) upsample_block1 = tf.keras.layers.concatenate([upsample_block1, conv_block4]) # skip connection dropout5 = tf.keras.layers.Dropout(0.2)(upsample_block1) conv_block5 = conv_block(dropout5, amount_filters * 8, 3) upsample_block2 = tf.keras.layers.UpSampling2D()(conv_block5) upsample_block2 = tf.keras.layers.Conv2D(filters=amount_filters * 4, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block2) upsample_block2 = tf.keras.layers.concatenate([upsample_block2, conv_block3]) dropout6 = tf.keras.layers.Dropout(0.2)(upsample_block2) conv_block6 = conv_block(dropout6, amount_filters * 4, 3) upsample_block3 = tf.keras.layers.UpSampling2D()(conv_block6) upsample_block3 = tf.keras.layers.Conv2D(filters=amount_filters * 2, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block3) upsample_block3 = tf.keras.layers.concatenate([upsample_block3, conv_block2]) dropout7 = tf.keras.layers.Dropout(0.2)(upsample_block3) conv_block7 = conv_block(dropout7, amount_filters * 2, 3) upsample_block4 = tf.keras.layers.UpSampling2D()(conv_block7) upsample_block4 = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block4) upsample_block4 = tf.keras.layers.concatenate([upsample_block4, conv_block1]) dropout8 = tf.keras.layers.Dropout(0.2)(upsample_block4) conv_block8 = conv_block(dropout8, amount_filters, 3) # amount of filters in output layer needs to be equal to the amount of classes output = tf.keras.layers.Conv2D(filters=4, kernel_size=(1, 1), activation="sigmoid")(conv_block8) unet = tf.keras.Model(inputs=[input], outputs=[output]) return unet ########## TRAINING ########## # this creates the data generator that is given to the neural net # batch_size 1 means that 1 original image is used for every batch # batch_size needs to be a power of 2! # pairs = make_pairs("/home/mlps_team003/CloudChamberProject") pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/TrainingSet2", "training") batch_size = 1 trainset_length = len(pairs) * 2 steps_per_epoch = trainset_length // batch_size train_generator = DataGenerator(pair=pairs, batch_size=batch_size, shuffle=True) val_pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/ValidationSet", "validation") val_generator = ValDataGenerator(pair=val_pairs, batch_size=batch_size, shuffle=True) test_pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/TestSet", "test") test_generator = ValDataGenerator(pair=test_pairs, batch_size=batch_size, shuffle=True) # creates the neural net # input = tf.keras.layers.Input(shape=[256, 256, 3]) for local testing input = tf.keras.layers.Input(shape=[992, 1312, 3]) # shape of the input images unet = unet(input, 8) # compiles the neural net optimizer = tf.keras.optimizers.Adam() # better than stochastic gradient decent unet.compile(optimizer=optimizer, loss=pce_dice_loss, metrics=[dice_coef, pixelwise_crossentropy]) """ needs to be uncommented to use the weights from the pretrained unsupervised model # loads the pretrained model and extracts the names of the layers pretrained_unet = tf.keras.models.load_model("pretraining.h5") pretrained_layers = pretrained_unet.layers # copies the pretrained weights to our neural net. We cant copy certain layers since the # pretraining NN skip connections which changes the amount of parameters of some layers for layer in pretrained_layers: if (layer.name != "conv2d_11" and layer.name != "conv2d_14" and layer.name != "conv2d_17" and layer.name != "conv2d_20" and layer.name != "conv2d_22"): untrained_layer = unet.get_layer(name=layer.name) # retrieves the untrained layer pretrained_layer = pretrained_unet.get_layer(name=layer.name) # retrieves the trained layer untrained_layer.set_weights(pretrained_layer.get_weights()) # copies weights """ # callback for logging training metrics that can be displayed in Tensorboard # with the command 'Tensorboard --logdir training_logs/train' metrics_logger = tf.keras.callbacks.TensorBoard(log_dir="./logs/" + datetime.now().strftime("%Y%m%d-%H%M%S"), update_freq='epoch', write_images=True) # callback for saving the best model model_checkpoint = tf.keras.callbacks.ModelCheckpoint('checkpoints/pretraining', monitor='val_loss', save_best_only=True) # trains it unet_history = unet.fit( x=train_generator, epochs=40, steps_per_epoch=steps_per_epoch, callbacks=[metrics_logger, model_checkpoint], validation_data=val_generator ) # Evaluates it results = unet.evaluate(test_generator) print("Test loss, dice_coef and pixelwise crossentropy: ", results) # saves the computed weights and the network architecture # when loading the neural net from the h5 file it first needs to be recompiled # since tensorflow has trouble with the custom loss function unet.save("unet.h5")
if self.shuffle == True: tf.random.shuffle(self.indexes) # generates a batch
random_line_split
training_modified.py
#!/usr/bin/env python3 import tensorflow as tf from tensorflow.keras.utils import Sequence from glob import glob import os from datetime import datetime # turn off GPU processing because # tensorflow-gpu can lead to trouble if not installed correctly # os.environ["CUDA_VISIBLE_DEVICES"] = "-1" ######## This script is a copy of training.py and enables training on our local GPU in order for us to properly ######## use tensorboard which didn't work on the server and introduce a validation set. It therefore modifies ######## the data generator to only load half the augmented images in one batch and the rest later. There is also ######## a ValDataGenerator that is used for the validation and test data where no augmentation is desired. # This script trains the neural network. # First it defines the data pipeline that loads the images and masks and preprocesses them. # Then it defines the custom loss function. # Afterwards the architecture of the neural network is defined and lastly the training procedure. ########## DATA PIPELINE ########## # turns a RGB mask into 4 2D binary class maps where each cell has value 1 if # the corresponding pixel in the original pixel had the color of the class def to_one_hot(mask): # these are the RGB colors that the masks use, if these values or the colors # in the mask images change every class map will be filled with zeros! colors = [ [227, 26, 28], # Red [65, 117, 5], # Green [106, 61, 154], # Violet [31, 120, 180] # Blue ] one_hot_mask = [] for color in colors: # tf.equal compares every pixel of the mask with the current RGB color # and returns a matrix where a cell is TRUE if the corresponding pixel had the color of the current class # reduce all then turns this boolean matrix into a 2D map class_mask = tf.reduce_all(tf.equal(mask, color), axis=-1) class_mask = tf.cast(class_mask, tf.float32) one_hot_mask.append(class_mask) # after we have all the 2D class maps we stack on top of each other to get one tensor one_hot_encoded_mask = tf.stack(one_hot_mask, axis=-1) return one_hot_encoded_mask # parses the masks, they are converted to float 32 later in to_one_hot def parse_mask(name): mask = tf.io.read_file(name) mask = tf.image.decode_png(mask) # this cuts the borders off, height and width need to be multiples of 32! # mask = tf.image.resize(mask, [256, 256]) # for local testing mask = tf.image.crop_to_bounding_box(mask, offset_height=44, offset_width=304, target_height=992, target_width=1312) return mask # parses the images def parse_image(name): image = tf.io.read_file(name) image = tf.image.decode_jpeg(image) image = tf.image.convert_image_dtype(image, tf.float32) # Neural Nets work with float 32 #image = tf.image.rgb_to_grayscale(image) # this cuts the borders off, height and width need to be multiples of 32! # image = tf.image.resize(image, [256, 256]) # for local testing image = tf.image.crop_to_bounding_box(image, offset_height=44, offset_width=304, target_height=992, target_width=1312) return image # this generator streams the images and masks to the GPU one after another # it gets a list of pairs that correspond to the directory paths of images and their corresponding masks # each batch includes a training image, its mask and the 7 augmented versions of it, which are generated on the fly # batch size means in this case how many original images are loaded for 1 batch, # the actual batch size is 8 times higher. # based on https://www.kaggle.com/mukulkr/camvid-segmentation-using-unet class DataGenerator(Sequence): def __init__(self, pair, batch_size, shuffle): self.pair = pair self.batch_size = batch_size self.shuffle = shuffle self.on_epoch_end() self.map = dict() # returns the length of the data set def __len__(self): return int(tf.math.floor(len(self.pair) / self.batch_size)) * 2 # returns a single batch def __getitem__(self, index): # a list that has the indexes of the pairs from which we want to generate images and masks for the batch index = index // 2 indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size] list_IDs_temp = [k for k in indexes] if index in self.map: return self.__data_generation(self.map[index], second_half=True) self.map[index] = list_IDs_temp X, Y = self.__data_generation(list_IDs_temp) return X, Y # resets the pair indexes after each epoch and shuffles the indexes so that the batches # are in different order for every epoch def on_epoch_end(self): self.indexes = tf.range(len(self.pair)) self.map = dict() if self.shuffle == True: tf.random.shuffle(self.indexes) # generates a batch def __data_generation(self, list_IDs_temp, second_half=False): batch_images = [] batch_masks = [] for i in list_IDs_temp: # parses the image and the mask of the current pair and then generates the augmented versions # it wasn't possible to generate the augmented images beforehand and have completely # random images in every batch image1 = parse_image(self.pair[i][0]) batch_images.append(image1) mask1 = parse_mask(self.pair[i][1]) mask1 = to_one_hot(mask1) batch_masks.append(mask1) image2 = tf.image.flip_left_right(image1) batch_images.append(image2) mask2 = tf.image.flip_left_right(mask1) batch_masks.append(mask2) image3 = tf.image.flip_up_down(image1) batch_images.append(image3) mask3 = tf.image.flip_up_down(mask1) batch_masks.append(mask3) image4 = tf.image.flip_up_down(image1) image4 = tf.image.flip_left_right(image4) batch_images.append(image4) mask4 = tf.image.flip_up_down(mask1) mask4 = tf.image.flip_left_right(mask4) batch_masks.append(mask4) if second_half: # images and masks 1 to 4 but with randomly changed brightness delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image5 = tf.image.adjust_brightness(image1, delta) batch_images.append(image5) batch_masks.append(mask1) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image6 = tf.image.flip_left_right(image1) image6 = tf.image.adjust_brightness(image6, delta) batch_images.append(image6) mask6 = tf.image.flip_left_right(mask1) batch_masks.append(mask6) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image7 = tf.image.flip_up_down(image1) image7 = tf.image.adjust_brightness(image7, delta) batch_images.append(image7) mask7 = tf.image.flip_up_down(mask1) batch_masks.append(mask7) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image8 = tf.image.flip_up_down(image1) image8 = tf.image.flip_left_right(image8) image8 = tf.image.adjust_brightness(image8, delta) batch_images.append(image8) mask8 = tf.image.flip_up_down(mask1) mask8 = tf.image.flip_left_right(mask8) batch_masks.append(mask8) return tf.stack(batch_images[4:]), tf.stack(batch_masks[4:]) # stack the images and masks of the batch into two tensors return tf.stack(batch_images[:4]), tf.stack(batch_masks[:4]) # Data generator that does not augment images, i.e. used for validation and test set class ValDataGenerator(Sequence): def __init__(self, pair, batch_size, shuffle): self.pair = pair self.batch_size = batch_size self.shuffle = shuffle self.on_epoch_end() # returns the length of the data set def
(self): return int(tf.math.floor(len(self.pair) / self.batch_size)) # returns a single batch def __getitem__(self, index): # a list that has the indexes of the pairs from which we want to generate images and masks for the batch indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size] list_IDs_temp = [k for k in indexes] X, Y = self.__data_generation(list_IDs_temp) return X, Y # resets the pair indexes after each epoch and shuffles the indexes so that the batches # are in different order for every epoch def on_epoch_end(self): self.indexes = tf.range(len(self.pair)) if self.shuffle == True: tf.random.shuffle(self.indexes) # generates a batch def __data_generation(self, list_IDs_temp, second_half=False): batch_images = [] batch_masks = [] for i in list_IDs_temp: # parses the image and the mask of the current pair and then generates the augmented versions # it wasn't possible to generate the augmented images beforehand and have completely # random images in every batch image1 = parse_image(self.pair[i][0]) batch_images.append(image1) mask1 = parse_mask(self.pair[i][1]) mask1 = to_one_hot(mask1) batch_masks.append(mask1) # stack the images and masks of the batch into two tensors return tf.stack(batch_images), tf.stack(batch_masks) # takes a path to a directory with two sub folders for training images and masks # and returns a list of pairs of paths for images and the corresponding masks def make_pairs(path, set): pairs = [] # sorted is very important since os.path.join somehow shuffles the paths and we need # the image and mask paths to have the exact same order image_paths = sorted(glob(os.path.join(path, set + "_images/*"))) mask_paths = sorted(glob(os.path.join(path, set + "_masks/*"))) #image_paths = sorted(glob(os.path.join(path, "test_images2/*"))) #mask_paths = sorted(glob(os.path.join(path, "test_masks2/*"))) for i in range(len(image_paths)): pairs.append((image_paths[i], mask_paths[i])) return pairs ########## LOSS FUNCTION ########## # based on https://github.com/aruns2120/Semantic-Segmentation-Severstal/blob/master/U-Net/CS2_firstCut.ipynb # the dice coefficient calculates how much the predicted mask and the correct mask overlap def dice_coef(y_true, y_predict, smooth=1): y_true_flat = tf.keras.backend.flatten(y_true) y_pred_flat = tf.keras.backend.flatten(y_predict) intersection = tf.keras.backend.sum(y_true_flat * y_pred_flat) return (2. * intersection + smooth) / (tf.keras.backend.sum(y_true_flat) + tf.keras.backend.sum(y_pred_flat) + smooth) def dice_loss(y_true, y_predict): return (1 - dice_coef(y_true, y_predict)) # weighted variant of pixelwise_crossentropy # based on https://www.gitmemory.com/issue/keras-team/keras/6261/569715992 def pixelwise_crossentropy(y_true, y_predicted):# # weights that scale the error for each class such that they all have equal impact on the loss # important since the data set is very unbalanced # weights represent the inverse of the proportion of pixels corresponding to that class in the whole data set # needs to be divided by 100.0 to keep the error at a similar magnitude during training weight_proton = 132.0 / 100.0 weight_alpha = 91.0 / 100.0 weight_V = 311.0 / 100.0 weight_electron = 71.0 / 100.0 # weight_proton = 1.0 # for local testing # weight_alpha = 1.0 # weight_V = 1.0 # weight_electron = 1.0 weights = [weight_proton, weight_alpha, weight_V, weight_electron] # predicted values get scaled such that they are never exactly 0 or 1 since then the logarithm diverges y_predicted /= tf.keras.backend.sum(y_predicted, axis=-1, keepdims=True) y_predicted = tf.keras.backend.clip(y_predicted, tf.keras.backend.epsilon(), 1. - tf.keras.backend.epsilon()) # compute the weighted cross_entropy loss = y_true * tf.keras.backend.log(y_predicted) loss = -tf.keras.backend.sum(loss * weights, -1) return loss # defines the custom loss function, sum of dice_loss and pixelwise_crossentropy def pce_dice_loss(y_true, y_predict): return pixelwise_crossentropy(y_true, y_predict) + dice_loss(y_true, y_predict) ########## NEURAL NETWORK ########## # based on https://github.com/aruns2120/Semantic-Segmentation-Severstal/blob/master/U-Net/CS2_firstCut.ipynb # defines the single convolutional blocks def conv_block(input, amount_filters, kernel_size): x = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer="he_normal", padding="same")(input) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation("relu")(x) x = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer="he_normal", padding="same")(x) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation("relu")(x) return x # defines the U-Net architecture # amount filters controls the amount of filters in the convolutional layers, needs to be a power of 2! def unet(input, amount_filters): # Encoder conv_block1 = conv_block(input, amount_filters, 3) pooling1 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block1) # randomly deactivates in each training step 20% of neurons, gives better generalization dropout1 = tf.keras.layers.Dropout(0.2)(pooling1) conv_block2 = conv_block(dropout1, amount_filters * 2, 3) pooling2 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block2) dropout2 = tf.keras.layers.Dropout(0.2)(pooling2) conv_block3 = conv_block(dropout2, amount_filters * 4, 3) pooling3 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block3) dropout3 = tf.keras.layers.Dropout(0.2)(pooling3) conv_block4 = conv_block(dropout3, amount_filters * 8, 3) pooling4 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block4) dropout4 = tf.keras.layers.Dropout(0.2)(pooling4) encoded_features = conv_block(dropout4, amount_filters * 16, 3) # Decoder upsample_block1 = tf.keras.layers.UpSampling2D()(encoded_features) upsample_block1 = tf.keras.layers.Conv2D(filters=amount_filters * 8, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block1) upsample_block1 = tf.keras.layers.concatenate([upsample_block1, conv_block4]) # skip connection dropout5 = tf.keras.layers.Dropout(0.2)(upsample_block1) conv_block5 = conv_block(dropout5, amount_filters * 8, 3) upsample_block2 = tf.keras.layers.UpSampling2D()(conv_block5) upsample_block2 = tf.keras.layers.Conv2D(filters=amount_filters * 4, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block2) upsample_block2 = tf.keras.layers.concatenate([upsample_block2, conv_block3]) dropout6 = tf.keras.layers.Dropout(0.2)(upsample_block2) conv_block6 = conv_block(dropout6, amount_filters * 4, 3) upsample_block3 = tf.keras.layers.UpSampling2D()(conv_block6) upsample_block3 = tf.keras.layers.Conv2D(filters=amount_filters * 2, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block3) upsample_block3 = tf.keras.layers.concatenate([upsample_block3, conv_block2]) dropout7 = tf.keras.layers.Dropout(0.2)(upsample_block3) conv_block7 = conv_block(dropout7, amount_filters * 2, 3) upsample_block4 = tf.keras.layers.UpSampling2D()(conv_block7) upsample_block4 = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block4) upsample_block4 = tf.keras.layers.concatenate([upsample_block4, conv_block1]) dropout8 = tf.keras.layers.Dropout(0.2)(upsample_block4) conv_block8 = conv_block(dropout8, amount_filters, 3) # amount of filters in output layer needs to be equal to the amount of classes output = tf.keras.layers.Conv2D(filters=4, kernel_size=(1, 1), activation="sigmoid")(conv_block8) unet = tf.keras.Model(inputs=[input], outputs=[output]) return unet ########## TRAINING ########## # this creates the data generator that is given to the neural net # batch_size 1 means that 1 original image is used for every batch # batch_size needs to be a power of 2! # pairs = make_pairs("/home/mlps_team003/CloudChamberProject") pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/TrainingSet2", "training") batch_size = 1 trainset_length = len(pairs) * 2 steps_per_epoch = trainset_length // batch_size train_generator = DataGenerator(pair=pairs, batch_size=batch_size, shuffle=True) val_pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/ValidationSet", "validation") val_generator = ValDataGenerator(pair=val_pairs, batch_size=batch_size, shuffle=True) test_pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/TestSet", "test") test_generator = ValDataGenerator(pair=test_pairs, batch_size=batch_size, shuffle=True) # creates the neural net # input = tf.keras.layers.Input(shape=[256, 256, 3]) for local testing input = tf.keras.layers.Input(shape=[992, 1312, 3]) # shape of the input images unet = unet(input, 8) # compiles the neural net optimizer = tf.keras.optimizers.Adam() # better than stochastic gradient decent unet.compile(optimizer=optimizer, loss=pce_dice_loss, metrics=[dice_coef, pixelwise_crossentropy]) """ needs to be uncommented to use the weights from the pretrained unsupervised model # loads the pretrained model and extracts the names of the layers pretrained_unet = tf.keras.models.load_model("pretraining.h5") pretrained_layers = pretrained_unet.layers # copies the pretrained weights to our neural net. We cant copy certain layers since the # pretraining NN skip connections which changes the amount of parameters of some layers for layer in pretrained_layers: if (layer.name != "conv2d_11" and layer.name != "conv2d_14" and layer.name != "conv2d_17" and layer.name != "conv2d_20" and layer.name != "conv2d_22"): untrained_layer = unet.get_layer(name=layer.name) # retrieves the untrained layer pretrained_layer = pretrained_unet.get_layer(name=layer.name) # retrieves the trained layer untrained_layer.set_weights(pretrained_layer.get_weights()) # copies weights """ # callback for logging training metrics that can be displayed in Tensorboard # with the command 'Tensorboard --logdir training_logs/train' metrics_logger = tf.keras.callbacks.TensorBoard(log_dir="./logs/" + datetime.now().strftime("%Y%m%d-%H%M%S"), update_freq='epoch', write_images=True) # callback for saving the best model model_checkpoint = tf.keras.callbacks.ModelCheckpoint('checkpoints/pretraining', monitor='val_loss', save_best_only=True) # trains it unet_history = unet.fit( x=train_generator, epochs=40, steps_per_epoch=steps_per_epoch, callbacks=[metrics_logger, model_checkpoint], validation_data=val_generator ) # Evaluates it results = unet.evaluate(test_generator) print("Test loss, dice_coef and pixelwise crossentropy: ", results) # saves the computed weights and the network architecture # when loading the neural net from the h5 file it first needs to be recompiled # since tensorflow has trouble with the custom loss function unet.save("unet.h5")
__len__
identifier_name
training_modified.py
#!/usr/bin/env python3 import tensorflow as tf from tensorflow.keras.utils import Sequence from glob import glob import os from datetime import datetime # turn off GPU processing because # tensorflow-gpu can lead to trouble if not installed correctly # os.environ["CUDA_VISIBLE_DEVICES"] = "-1" ######## This script is a copy of training.py and enables training on our local GPU in order for us to properly ######## use tensorboard which didn't work on the server and introduce a validation set. It therefore modifies ######## the data generator to only load half the augmented images in one batch and the rest later. There is also ######## a ValDataGenerator that is used for the validation and test data where no augmentation is desired. # This script trains the neural network. # First it defines the data pipeline that loads the images and masks and preprocesses them. # Then it defines the custom loss function. # Afterwards the architecture of the neural network is defined and lastly the training procedure. ########## DATA PIPELINE ########## # turns a RGB mask into 4 2D binary class maps where each cell has value 1 if # the corresponding pixel in the original pixel had the color of the class def to_one_hot(mask): # these are the RGB colors that the masks use, if these values or the colors # in the mask images change every class map will be filled with zeros! colors = [ [227, 26, 28], # Red [65, 117, 5], # Green [106, 61, 154], # Violet [31, 120, 180] # Blue ] one_hot_mask = [] for color in colors: # tf.equal compares every pixel of the mask with the current RGB color # and returns a matrix where a cell is TRUE if the corresponding pixel had the color of the current class # reduce all then turns this boolean matrix into a 2D map class_mask = tf.reduce_all(tf.equal(mask, color), axis=-1) class_mask = tf.cast(class_mask, tf.float32) one_hot_mask.append(class_mask) # after we have all the 2D class maps we stack on top of each other to get one tensor one_hot_encoded_mask = tf.stack(one_hot_mask, axis=-1) return one_hot_encoded_mask # parses the masks, they are converted to float 32 later in to_one_hot def parse_mask(name): mask = tf.io.read_file(name) mask = tf.image.decode_png(mask) # this cuts the borders off, height and width need to be multiples of 32! # mask = tf.image.resize(mask, [256, 256]) # for local testing mask = tf.image.crop_to_bounding_box(mask, offset_height=44, offset_width=304, target_height=992, target_width=1312) return mask # parses the images def parse_image(name): image = tf.io.read_file(name) image = tf.image.decode_jpeg(image) image = tf.image.convert_image_dtype(image, tf.float32) # Neural Nets work with float 32 #image = tf.image.rgb_to_grayscale(image) # this cuts the borders off, height and width need to be multiples of 32! # image = tf.image.resize(image, [256, 256]) # for local testing image = tf.image.crop_to_bounding_box(image, offset_height=44, offset_width=304, target_height=992, target_width=1312) return image # this generator streams the images and masks to the GPU one after another # it gets a list of pairs that correspond to the directory paths of images and their corresponding masks # each batch includes a training image, its mask and the 7 augmented versions of it, which are generated on the fly # batch size means in this case how many original images are loaded for 1 batch, # the actual batch size is 8 times higher. # based on https://www.kaggle.com/mukulkr/camvid-segmentation-using-unet class DataGenerator(Sequence): def __init__(self, pair, batch_size, shuffle): self.pair = pair self.batch_size = batch_size self.shuffle = shuffle self.on_epoch_end() self.map = dict() # returns the length of the data set def __len__(self): return int(tf.math.floor(len(self.pair) / self.batch_size)) * 2 # returns a single batch def __getitem__(self, index): # a list that has the indexes of the pairs from which we want to generate images and masks for the batch index = index // 2 indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size] list_IDs_temp = [k for k in indexes] if index in self.map: return self.__data_generation(self.map[index], second_half=True) self.map[index] = list_IDs_temp X, Y = self.__data_generation(list_IDs_temp) return X, Y # resets the pair indexes after each epoch and shuffles the indexes so that the batches # are in different order for every epoch def on_epoch_end(self): self.indexes = tf.range(len(self.pair)) self.map = dict() if self.shuffle == True: tf.random.shuffle(self.indexes) # generates a batch def __data_generation(self, list_IDs_temp, second_half=False): batch_images = [] batch_masks = [] for i in list_IDs_temp: # parses the image and the mask of the current pair and then generates the augmented versions # it wasn't possible to generate the augmented images beforehand and have completely # random images in every batch image1 = parse_image(self.pair[i][0]) batch_images.append(image1) mask1 = parse_mask(self.pair[i][1]) mask1 = to_one_hot(mask1) batch_masks.append(mask1) image2 = tf.image.flip_left_right(image1) batch_images.append(image2) mask2 = tf.image.flip_left_right(mask1) batch_masks.append(mask2) image3 = tf.image.flip_up_down(image1) batch_images.append(image3) mask3 = tf.image.flip_up_down(mask1) batch_masks.append(mask3) image4 = tf.image.flip_up_down(image1) image4 = tf.image.flip_left_right(image4) batch_images.append(image4) mask4 = tf.image.flip_up_down(mask1) mask4 = tf.image.flip_left_right(mask4) batch_masks.append(mask4) if second_half: # images and masks 1 to 4 but with randomly changed brightness delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image5 = tf.image.adjust_brightness(image1, delta) batch_images.append(image5) batch_masks.append(mask1) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image6 = tf.image.flip_left_right(image1) image6 = tf.image.adjust_brightness(image6, delta) batch_images.append(image6) mask6 = tf.image.flip_left_right(mask1) batch_masks.append(mask6) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image7 = tf.image.flip_up_down(image1) image7 = tf.image.adjust_brightness(image7, delta) batch_images.append(image7) mask7 = tf.image.flip_up_down(mask1) batch_masks.append(mask7) delta = tf.random.uniform(shape=[], minval=-0.5, maxval=0.51) image8 = tf.image.flip_up_down(image1) image8 = tf.image.flip_left_right(image8) image8 = tf.image.adjust_brightness(image8, delta) batch_images.append(image8) mask8 = tf.image.flip_up_down(mask1) mask8 = tf.image.flip_left_right(mask8) batch_masks.append(mask8) return tf.stack(batch_images[4:]), tf.stack(batch_masks[4:]) # stack the images and masks of the batch into two tensors return tf.stack(batch_images[:4]), tf.stack(batch_masks[:4]) # Data generator that does not augment images, i.e. used for validation and test set class ValDataGenerator(Sequence): def __init__(self, pair, batch_size, shuffle): self.pair = pair self.batch_size = batch_size self.shuffle = shuffle self.on_epoch_end() # returns the length of the data set def __len__(self): return int(tf.math.floor(len(self.pair) / self.batch_size)) # returns a single batch def __getitem__(self, index): # a list that has the indexes of the pairs from which we want to generate images and masks for the batch indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size] list_IDs_temp = [k for k in indexes] X, Y = self.__data_generation(list_IDs_temp) return X, Y # resets the pair indexes after each epoch and shuffles the indexes so that the batches # are in different order for every epoch def on_epoch_end(self): self.indexes = tf.range(len(self.pair)) if self.shuffle == True: tf.random.shuffle(self.indexes) # generates a batch def __data_generation(self, list_IDs_temp, second_half=False): batch_images = [] batch_masks = [] for i in list_IDs_temp: # parses the image and the mask of the current pair and then generates the augmented versions # it wasn't possible to generate the augmented images beforehand and have completely # random images in every batch image1 = parse_image(self.pair[i][0]) batch_images.append(image1) mask1 = parse_mask(self.pair[i][1]) mask1 = to_one_hot(mask1) batch_masks.append(mask1) # stack the images and masks of the batch into two tensors return tf.stack(batch_images), tf.stack(batch_masks) # takes a path to a directory with two sub folders for training images and masks # and returns a list of pairs of paths for images and the corresponding masks def make_pairs(path, set): pairs = [] # sorted is very important since os.path.join somehow shuffles the paths and we need # the image and mask paths to have the exact same order image_paths = sorted(glob(os.path.join(path, set + "_images/*"))) mask_paths = sorted(glob(os.path.join(path, set + "_masks/*"))) #image_paths = sorted(glob(os.path.join(path, "test_images2/*"))) #mask_paths = sorted(glob(os.path.join(path, "test_masks2/*"))) for i in range(len(image_paths)): pairs.append((image_paths[i], mask_paths[i])) return pairs ########## LOSS FUNCTION ########## # based on https://github.com/aruns2120/Semantic-Segmentation-Severstal/blob/master/U-Net/CS2_firstCut.ipynb # the dice coefficient calculates how much the predicted mask and the correct mask overlap def dice_coef(y_true, y_predict, smooth=1): y_true_flat = tf.keras.backend.flatten(y_true) y_pred_flat = tf.keras.backend.flatten(y_predict) intersection = tf.keras.backend.sum(y_true_flat * y_pred_flat) return (2. * intersection + smooth) / (tf.keras.backend.sum(y_true_flat) + tf.keras.backend.sum(y_pred_flat) + smooth) def dice_loss(y_true, y_predict): return (1 - dice_coef(y_true, y_predict)) # weighted variant of pixelwise_crossentropy # based on https://www.gitmemory.com/issue/keras-team/keras/6261/569715992 def pixelwise_crossentropy(y_true, y_predicted):# # weights that scale the error for each class such that they all have equal impact on the loss # important since the data set is very unbalanced # weights represent the inverse of the proportion of pixels corresponding to that class in the whole data set # needs to be divided by 100.0 to keep the error at a similar magnitude during training weight_proton = 132.0 / 100.0 weight_alpha = 91.0 / 100.0 weight_V = 311.0 / 100.0 weight_electron = 71.0 / 100.0 # weight_proton = 1.0 # for local testing # weight_alpha = 1.0 # weight_V = 1.0 # weight_electron = 1.0 weights = [weight_proton, weight_alpha, weight_V, weight_electron] # predicted values get scaled such that they are never exactly 0 or 1 since then the logarithm diverges y_predicted /= tf.keras.backend.sum(y_predicted, axis=-1, keepdims=True) y_predicted = tf.keras.backend.clip(y_predicted, tf.keras.backend.epsilon(), 1. - tf.keras.backend.epsilon()) # compute the weighted cross_entropy loss = y_true * tf.keras.backend.log(y_predicted) loss = -tf.keras.backend.sum(loss * weights, -1) return loss # defines the custom loss function, sum of dice_loss and pixelwise_crossentropy def pce_dice_loss(y_true, y_predict):
########## NEURAL NETWORK ########## # based on https://github.com/aruns2120/Semantic-Segmentation-Severstal/blob/master/U-Net/CS2_firstCut.ipynb # defines the single convolutional blocks def conv_block(input, amount_filters, kernel_size): x = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer="he_normal", padding="same")(input) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation("relu")(x) x = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(kernel_size, kernel_size), kernel_initializer="he_normal", padding="same")(x) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.Activation("relu")(x) return x # defines the U-Net architecture # amount filters controls the amount of filters in the convolutional layers, needs to be a power of 2! def unet(input, amount_filters): # Encoder conv_block1 = conv_block(input, amount_filters, 3) pooling1 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block1) # randomly deactivates in each training step 20% of neurons, gives better generalization dropout1 = tf.keras.layers.Dropout(0.2)(pooling1) conv_block2 = conv_block(dropout1, amount_filters * 2, 3) pooling2 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block2) dropout2 = tf.keras.layers.Dropout(0.2)(pooling2) conv_block3 = conv_block(dropout2, amount_filters * 4, 3) pooling3 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block3) dropout3 = tf.keras.layers.Dropout(0.2)(pooling3) conv_block4 = conv_block(dropout3, amount_filters * 8, 3) pooling4 = tf.keras.layers.MaxPooling2D((2, 2))(conv_block4) dropout4 = tf.keras.layers.Dropout(0.2)(pooling4) encoded_features = conv_block(dropout4, amount_filters * 16, 3) # Decoder upsample_block1 = tf.keras.layers.UpSampling2D()(encoded_features) upsample_block1 = tf.keras.layers.Conv2D(filters=amount_filters * 8, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block1) upsample_block1 = tf.keras.layers.concatenate([upsample_block1, conv_block4]) # skip connection dropout5 = tf.keras.layers.Dropout(0.2)(upsample_block1) conv_block5 = conv_block(dropout5, amount_filters * 8, 3) upsample_block2 = tf.keras.layers.UpSampling2D()(conv_block5) upsample_block2 = tf.keras.layers.Conv2D(filters=amount_filters * 4, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block2) upsample_block2 = tf.keras.layers.concatenate([upsample_block2, conv_block3]) dropout6 = tf.keras.layers.Dropout(0.2)(upsample_block2) conv_block6 = conv_block(dropout6, amount_filters * 4, 3) upsample_block3 = tf.keras.layers.UpSampling2D()(conv_block6) upsample_block3 = tf.keras.layers.Conv2D(filters=amount_filters * 2, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block3) upsample_block3 = tf.keras.layers.concatenate([upsample_block3, conv_block2]) dropout7 = tf.keras.layers.Dropout(0.2)(upsample_block3) conv_block7 = conv_block(dropout7, amount_filters * 2, 3) upsample_block4 = tf.keras.layers.UpSampling2D()(conv_block7) upsample_block4 = tf.keras.layers.Conv2D(filters=amount_filters, kernel_size=(2, 2), kernel_initializer="he_normal", padding="same")(upsample_block4) upsample_block4 = tf.keras.layers.concatenate([upsample_block4, conv_block1]) dropout8 = tf.keras.layers.Dropout(0.2)(upsample_block4) conv_block8 = conv_block(dropout8, amount_filters, 3) # amount of filters in output layer needs to be equal to the amount of classes output = tf.keras.layers.Conv2D(filters=4, kernel_size=(1, 1), activation="sigmoid")(conv_block8) unet = tf.keras.Model(inputs=[input], outputs=[output]) return unet ########## TRAINING ########## # this creates the data generator that is given to the neural net # batch_size 1 means that 1 original image is used for every batch # batch_size needs to be a power of 2! # pairs = make_pairs("/home/mlps_team003/CloudChamberProject") pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/TrainingSet2", "training") batch_size = 1 trainset_length = len(pairs) * 2 steps_per_epoch = trainset_length // batch_size train_generator = DataGenerator(pair=pairs, batch_size=batch_size, shuffle=True) val_pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/ValidationSet", "validation") val_generator = ValDataGenerator(pair=val_pairs, batch_size=batch_size, shuffle=True) test_pairs = make_pairs("C:/Users/lukwi/Desktop/mlps/CloudChamberProject/TestSet", "test") test_generator = ValDataGenerator(pair=test_pairs, batch_size=batch_size, shuffle=True) # creates the neural net # input = tf.keras.layers.Input(shape=[256, 256, 3]) for local testing input = tf.keras.layers.Input(shape=[992, 1312, 3]) # shape of the input images unet = unet(input, 8) # compiles the neural net optimizer = tf.keras.optimizers.Adam() # better than stochastic gradient decent unet.compile(optimizer=optimizer, loss=pce_dice_loss, metrics=[dice_coef, pixelwise_crossentropy]) """ needs to be uncommented to use the weights from the pretrained unsupervised model # loads the pretrained model and extracts the names of the layers pretrained_unet = tf.keras.models.load_model("pretraining.h5") pretrained_layers = pretrained_unet.layers # copies the pretrained weights to our neural net. We cant copy certain layers since the # pretraining NN skip connections which changes the amount of parameters of some layers for layer in pretrained_layers: if (layer.name != "conv2d_11" and layer.name != "conv2d_14" and layer.name != "conv2d_17" and layer.name != "conv2d_20" and layer.name != "conv2d_22"): untrained_layer = unet.get_layer(name=layer.name) # retrieves the untrained layer pretrained_layer = pretrained_unet.get_layer(name=layer.name) # retrieves the trained layer untrained_layer.set_weights(pretrained_layer.get_weights()) # copies weights """ # callback for logging training metrics that can be displayed in Tensorboard # with the command 'Tensorboard --logdir training_logs/train' metrics_logger = tf.keras.callbacks.TensorBoard(log_dir="./logs/" + datetime.now().strftime("%Y%m%d-%H%M%S"), update_freq='epoch', write_images=True) # callback for saving the best model model_checkpoint = tf.keras.callbacks.ModelCheckpoint('checkpoints/pretraining', monitor='val_loss', save_best_only=True) # trains it unet_history = unet.fit( x=train_generator, epochs=40, steps_per_epoch=steps_per_epoch, callbacks=[metrics_logger, model_checkpoint], validation_data=val_generator ) # Evaluates it results = unet.evaluate(test_generator) print("Test loss, dice_coef and pixelwise crossentropy: ", results) # saves the computed weights and the network architecture # when loading the neural net from the h5 file it first needs to be recompiled # since tensorflow has trouble with the custom loss function unet.save("unet.h5")
return pixelwise_crossentropy(y_true, y_predict) + dice_loss(y_true, y_predict)
identifier_body
framebuffer.rs
// Copyright (c) 2016 The vulkano developers // Licensed under the Apache License, Version 2.0 // <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, // at your option. All files in the project carrying such // notice may not be copied, modified, or distributed except // according to those terms. use std::error; use std::fmt; use std::marker::PhantomData; use std::mem; use std::ptr; use std::sync::Arc; use smallvec::SmallVec; use device::Device; use device::DeviceOwned; use format::ClearValue; use framebuffer::AttachmentsList; use framebuffer::FramebufferAbstract; use framebuffer::IncompatibleRenderPassAttachmentError; use framebuffer::LayoutAttachmentDescription; use framebuffer::LayoutPassDependencyDescription; use framebuffer::LayoutPassDescription; use framebuffer::RenderPassAbstract; use framebuffer::RenderPassDescClearValues; use framebuffer::RenderPassDescAttachmentsList; use framebuffer::RenderPassDesc; use framebuffer::RenderPassSys; use image::ImageViewAccess; use Error; use OomError; use VulkanObject; use VulkanPointers; use check_errors; use vk; /// Contains the list of images attached to a render pass. /// /// Creating a framebuffer is done by passing the render pass object, the dimensions of the /// framebuffer, and the list of attachments to `Framebuffer::new()`. /// /// Just like all render pass objects implement the `RenderPassAbstract` trait, all framebuffer /// objects implement the `FramebufferAbstract` trait. This means that you can cast any /// `Arc<Framebuffer<..>>` into an `Arc<FramebufferAbstract + Send + Sync>` for easier storage. /// /// ## With a generic list of attachments /// /// The list of attachments passed to `Framebuffer::new()` can be of various types, but one of the /// possibilities is to pass an object of type `Vec<Arc<ImageView + Send + Sync>>`. /// /// > **Note**: If you access a render pass object through the `RenderPassAbstract` trait, passing /// > a `Vec<Arc<ImageView + Send + Sync>>` is the only possible method. /// /// The framebuffer constructor will perform various checks to make sure that the number of images /// is correct and that each image can be used with this render pass. /// /// ```ignore // FIXME: unignore /// # use std::sync::Arc; /// # use vulkano::framebuffer::RenderPassAbstract; /// use vulkano::framebuffer::Framebuffer; /// /// # let render_pass: Arc<RenderPassAbstract + Send + Sync> = return; /// # let my_image: Arc<vulkano::image::ImageViewAccess> = return; /// // let render_pass: Arc<RenderPassAbstract + Send + Sync> = ...; /// let framebuffer = Framebuffer::new(render_pass.clone(), [1024, 768, 1], /// vec![my_image.clone() as Arc<_>]).unwrap(); /// ``` /// /// ## With a specialized list of attachments /// /// The list of attachments can also be of any type `T`, as long as the render pass description /// implements the trait `RenderPassDescAttachmentsList<T>`. /// /// For example if you pass a render pass object that implements /// `RenderPassDescAttachmentsList<Foo>`, then you can pass a `Foo` as the list of attachments. /// /// > **Note**: The reason why `Vec<Arc<ImageView + Send + Sync>>` always works (see previous section) is that /// > render pass descriptions are required to always implement /// > `RenderPassDescAttachmentsList<Vec<Arc<ImageViewAccess + Send + Sync>>>`. /// /// When it comes to the `single_pass_renderpass!` and `ordered_passes_renderpass!` macros, you can /// build a list of attachments by calling `start_attachments()` on the render pass description, /// which will return an object that has a method whose name is the name of the first attachment /// and that can be used to specify it. This method will return another object that has a method /// whose name is the name of the second attachment, and so on. See the documentation of the macros /// for more details. TODO: put link here /// /// ```ignore // FIXME: unignore /// # #[macro_use] extern crate vulkano; /// # fn main() { /// # let device: std::sync::Arc<vulkano::device::Device> = return; /// use std::sync::Arc; /// use vulkano::format::Format; /// use vulkano::framebuffer::Framebuffer; /// /// let render_pass = single_pass_renderpass!(device.clone(), /// attachments: { /// // `foo` is a custom name we give to the first and only attachment. /// foo: { /// load: Clear, /// store: Store, /// format: Format::R8G8B8A8Unorm, /// samples: 1, /// } /// }, /// pass: { /// color: [foo], // Repeat the attachment name here. /// depth_stencil: {} /// } /// ).unwrap(); /// /// # let my_image: Arc<vulkano::image::ImageViewAccess> = return; /// let framebuffer = { /// let atch = render_pass.desc().start_attachments().foo(my_image.clone() as Arc<_>); /// Framebuffer::new(render_pass, [1024, 768, 1], atch).unwrap() /// }; /// # } /// ``` #[derive(Debug)] pub struct Framebuffer<Rp, A> { device: Arc<Device>, render_pass: Rp, framebuffer: vk::Framebuffer, dimensions: [u32; 3], resources: A, } impl<Rp> Framebuffer<Rp, Box<AttachmentsList + Send + Sync>> { /// Builds a new framebuffer. /// /// The `attachments` parameter depends on which render pass implementation is used. // TODO: allow ImageView pub fn new<Ia>(render_pass: Rp, dimensions: [u32; 3], attachments: Ia) -> Result<Arc<Framebuffer<Rp, Box<AttachmentsList + Send + Sync>>>, FramebufferCreationError> where Rp: RenderPassAbstract + RenderPassDescAttachmentsList<Ia> { let device = render_pass.device().clone(); // This function call is supposed to check whether the attachments are valid. // For more safety, we do some additional `debug_assert`s below. let attachments = try!(render_pass.check_attachments_list(attachments)); // TODO: add a debug assertion that checks whether the attachments are compatible // with the RP ; this should be checked by the RenderPassDescAttachmentsList trait // impl, but we can double-check in debug mode // Checking the dimensions against the limits. { let limits = render_pass.device().physical_device().limits(); let limits = [limits.max_framebuffer_width(), limits.max_framebuffer_height(), limits.max_framebuffer_layers()]; if dimensions[0] > limits[0] || dimensions[1] > limits[1] || dimensions[2] > limits[2] { return Err(FramebufferCreationError::DimensionsTooLarge); } } // Checking the dimensions against the attachments. if let Some(dims_constraints) = attachments.intersection_dimensions() { if dims_constraints[0] < dimensions[0] || dims_constraints[1] < dimensions[1] || dims_constraints[2] < dimensions[2] { return Err(FramebufferCreationError::AttachmentTooSmall); } } let ids: SmallVec<[vk::ImageView; 8]> = attachments.raw_image_view_handles().into_iter().map(|v| v.internal_object()).collect(); let framebuffer = unsafe { let vk = render_pass.device().pointers(); let infos = vk::FramebufferCreateInfo { sType: vk::STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, pNext: ptr::null(), flags: 0, // reserved renderPass: render_pass.inner().internal_object(), attachmentCount: ids.len() as u32, pAttachments: ids.as_ptr(), width: dimensions[0], height: dimensions[1], layers: dimensions[2], }; let mut output = mem::uninitialized(); try!(check_errors(vk.CreateFramebuffer(device.internal_object(), &infos, ptr::null(), &mut output))); output }; Ok(Arc::new(Framebuffer { device: device, render_pass: render_pass, framebuffer: framebuffer, dimensions: dimensions, resources: attachments, })) } } impl<Rp, A> Framebuffer<Rp, A> { /// Returns the width, height and layers of this framebuffer. #[inline] pub fn dimensions(&self) -> [u32; 3] { self.dimensions } /// Returns the width of the framebuffer in pixels. #[inline] pub fn width(&self) -> u32 { self.dimensions[0] } /// Returns the height of the framebuffer in pixels. #[inline] pub fn height(&self) -> u32 { self.dimensions[1] } /// Returns the number of layers (or depth) of the framebuffer. #[inline] pub fn layers(&self) -> u32 { self.dimensions[2] } /// Returns the device that was used to create this framebuffer. #[inline] pub fn device(&self) -> &Arc<Device> { &self.device } /// Returns the renderpass that was used to create this framebuffer. #[inline] pub fn render_pass(&self) -> &Rp { &self.render_pass } } unsafe impl<Rp, A> FramebufferAbstract for Framebuffer<Rp, A> where Rp: RenderPassAbstract, A: AttachmentsList { #[inline] fn inner(&self) -> FramebufferSys { FramebufferSys(self.framebuffer, PhantomData) } #[inline] fn dimensions(&self) -> [u32; 3] { self.dimensions } #[inline] fn attachments(&self) -> Vec<&ImageViewAccess> { self.resources.as_image_view_accesses() } } unsafe impl<Rp, A> RenderPassDesc for Framebuffer<Rp, A> where Rp: RenderPassDesc { #[inline] fn num_attachments(&self) -> usize { self.render_pass.num_attachments() } #[inline] fn attachment_desc(&self, num: usize) -> Option<LayoutAttachmentDescription> { self.render_pass.attachment_desc(num) } #[inline] fn num_subpasses(&self) -> usize { self.render_pass.num_subpasses() } #[inline] fn subpass_desc(&self, num: usize) -> Option<LayoutPassDescription> { self.render_pass.subpass_desc(num) } #[inline] fn num_dependencies(&self) -> usize { self.render_pass.num_dependencies() } #[inline] fn dependency_desc(&self, num: usize) -> Option<LayoutPassDependencyDescription> { self.render_pass.dependency_desc(num)
where Rp: RenderPassDescAttachmentsList<At> { #[inline] fn check_attachments_list(&self, atch: At) -> Result<Box<AttachmentsList + Send + Sync>, FramebufferCreationError> { self.render_pass.check_attachments_list(atch) } } unsafe impl<C, Rp, A> RenderPassDescClearValues<C> for Framebuffer<Rp, A> where Rp: RenderPassDescClearValues<C> { #[inline] fn convert_clear_values(&self, vals: C) -> Box<Iterator<Item = ClearValue>> { self.render_pass.convert_clear_values(vals) } } unsafe impl<Rp, A> RenderPassAbstract for Framebuffer<Rp, A> where Rp: RenderPassAbstract { #[inline] fn inner(&self) -> RenderPassSys { self.render_pass.inner() } } unsafe impl<Rp, A> DeviceOwned for Framebuffer<Rp, A> { #[inline] fn device(&self) -> &Arc<Device> { &self.device } } impl<Rp, A> Drop for Framebuffer<Rp, A> { #[inline] fn drop(&mut self) { unsafe { let vk = self.device.pointers(); vk.DestroyFramebuffer(self.device.internal_object(), self.framebuffer, ptr::null()); } } } /// Opaque object that represents the internals of a framebuffer. #[derive(Debug, Copy, Clone)] pub struct FramebufferSys<'a>(vk::Framebuffer, PhantomData<&'a ()>); unsafe impl<'a> VulkanObject for FramebufferSys<'a> { type Object = vk::Framebuffer; #[inline] fn internal_object(&self) -> vk::Framebuffer { self.0 } } /// Error that can happen when creating a framebuffer object. #[derive(Copy, Clone, Debug)] pub enum FramebufferCreationError { /// Out of memory. OomError(OomError), /// The requested dimensions exceed the device's limits. DimensionsTooLarge, /// One of the attachments is too small compared to the requested framebuffer dimensions. AttachmentTooSmall, /// The number of attachments doesn't match the number expected by the render pass. AttachmentsCountMismatch { /// Expected number of attachments. expected: usize, /// Number of attachments that were given. obtained: usize, }, /// One of the images cannot be used as the requested attachment. IncompatibleAttachment { /// Zero-based id of the attachment. attachment_num: usize, /// The problem. error: IncompatibleRenderPassAttachmentError, }, } impl From<OomError> for FramebufferCreationError { #[inline] fn from(err: OomError) -> FramebufferCreationError { FramebufferCreationError::OomError(err) } } impl error::Error for FramebufferCreationError { #[inline] fn description(&self) -> &str { match *self { FramebufferCreationError::OomError(_) => "no memory available", FramebufferCreationError::DimensionsTooLarge => "the dimensions of the framebuffer \ are too large", FramebufferCreationError::AttachmentTooSmall => { "one of the attachments is too small compared to the requested framebuffer \ dimensions" }, FramebufferCreationError::AttachmentsCountMismatch { .. } => { "the number of attachments doesn't match the number expected by the render pass" }, FramebufferCreationError::IncompatibleAttachment { .. } => { "one of the images cannot be used as the requested attachment" }, } } #[inline] fn cause(&self) -> Option<&error::Error> { match *self { FramebufferCreationError::OomError(ref err) => Some(err), FramebufferCreationError::IncompatibleAttachment { ref error, .. } => Some(error), _ => None, } } } impl fmt::Display for FramebufferCreationError { #[inline] fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(fmt, "{}", error::Error::description(self)) } } impl From<Error> for FramebufferCreationError { #[inline] fn from(err: Error) -> FramebufferCreationError { FramebufferCreationError::from(OomError::from(err)) } } /* FIXME: restore #[cfg(test)] mod tests { use format::R8G8B8A8Unorm; use framebuffer::Framebuffer; use framebuffer::FramebufferCreationError; use image::attachment::AttachmentImage; #[test] fn simple_create() { let (device, _) = gfx_dev_and_queue!(); let render_pass = single_pass_renderpass! { attachments: { color: { load: Clear, store: DontCare, format: R8G8B8A8Unorm, } }, pass: { color: [color], depth_stencil: {} } }.unwrap(); let image = AttachmentImage::new(&device, [1024, 768], R8G8B8A8Unorm).unwrap(); let _ = Framebuffer::new(render_pass, [1024, 768, 1], example::AList { color: image.clone() }).unwrap(); } #[test] fn framebuffer_too_large() { let (device, _) = gfx_dev_and_queue!(); let render_pass = example::CustomRenderPass::new(&device, &example::Formats { color: (R8G8B8A8Unorm, 1) }).unwrap(); let image = AttachmentImage::new(&device, [1024, 768], R8G8B8A8Unorm).unwrap(); let alist = example::AList { color: image.clone() }; match Framebuffer::new(render_pass, [0xffffffff, 0xffffffff, 0xffffffff], alist) { Err(FramebufferCreationError::DimensionsTooLarge) => (), _ => panic!() } } #[test] fn attachment_too_small() { let (device, _) = gfx_dev_and_queue!(); let render_pass = example::CustomRenderPass::new(&device, &example::Formats { color: (R8G8B8A8Unorm, 1) }).unwrap(); let image = AttachmentImage::new(&device, [512, 512], R8G8B8A8Unorm).unwrap(); let alist = example::AList { color: image.clone() }; match Framebuffer::new(render_pass, [600, 600, 1], alist) { Err(FramebufferCreationError::AttachmentTooSmall) => (), _ => panic!() } } }*/
} } unsafe impl<At, Rp, A> RenderPassDescAttachmentsList<At> for Framebuffer<Rp, A>
random_line_split
framebuffer.rs
// Copyright (c) 2016 The vulkano developers // Licensed under the Apache License, Version 2.0 // <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, // at your option. All files in the project carrying such // notice may not be copied, modified, or distributed except // according to those terms. use std::error; use std::fmt; use std::marker::PhantomData; use std::mem; use std::ptr; use std::sync::Arc; use smallvec::SmallVec; use device::Device; use device::DeviceOwned; use format::ClearValue; use framebuffer::AttachmentsList; use framebuffer::FramebufferAbstract; use framebuffer::IncompatibleRenderPassAttachmentError; use framebuffer::LayoutAttachmentDescription; use framebuffer::LayoutPassDependencyDescription; use framebuffer::LayoutPassDescription; use framebuffer::RenderPassAbstract; use framebuffer::RenderPassDescClearValues; use framebuffer::RenderPassDescAttachmentsList; use framebuffer::RenderPassDesc; use framebuffer::RenderPassSys; use image::ImageViewAccess; use Error; use OomError; use VulkanObject; use VulkanPointers; use check_errors; use vk; /// Contains the list of images attached to a render pass. /// /// Creating a framebuffer is done by passing the render pass object, the dimensions of the /// framebuffer, and the list of attachments to `Framebuffer::new()`. /// /// Just like all render pass objects implement the `RenderPassAbstract` trait, all framebuffer /// objects implement the `FramebufferAbstract` trait. This means that you can cast any /// `Arc<Framebuffer<..>>` into an `Arc<FramebufferAbstract + Send + Sync>` for easier storage. /// /// ## With a generic list of attachments /// /// The list of attachments passed to `Framebuffer::new()` can be of various types, but one of the /// possibilities is to pass an object of type `Vec<Arc<ImageView + Send + Sync>>`. /// /// > **Note**: If you access a render pass object through the `RenderPassAbstract` trait, passing /// > a `Vec<Arc<ImageView + Send + Sync>>` is the only possible method. /// /// The framebuffer constructor will perform various checks to make sure that the number of images /// is correct and that each image can be used with this render pass. /// /// ```ignore // FIXME: unignore /// # use std::sync::Arc; /// # use vulkano::framebuffer::RenderPassAbstract; /// use vulkano::framebuffer::Framebuffer; /// /// # let render_pass: Arc<RenderPassAbstract + Send + Sync> = return; /// # let my_image: Arc<vulkano::image::ImageViewAccess> = return; /// // let render_pass: Arc<RenderPassAbstract + Send + Sync> = ...; /// let framebuffer = Framebuffer::new(render_pass.clone(), [1024, 768, 1], /// vec![my_image.clone() as Arc<_>]).unwrap(); /// ``` /// /// ## With a specialized list of attachments /// /// The list of attachments can also be of any type `T`, as long as the render pass description /// implements the trait `RenderPassDescAttachmentsList<T>`. /// /// For example if you pass a render pass object that implements /// `RenderPassDescAttachmentsList<Foo>`, then you can pass a `Foo` as the list of attachments. /// /// > **Note**: The reason why `Vec<Arc<ImageView + Send + Sync>>` always works (see previous section) is that /// > render pass descriptions are required to always implement /// > `RenderPassDescAttachmentsList<Vec<Arc<ImageViewAccess + Send + Sync>>>`. /// /// When it comes to the `single_pass_renderpass!` and `ordered_passes_renderpass!` macros, you can /// build a list of attachments by calling `start_attachments()` on the render pass description, /// which will return an object that has a method whose name is the name of the first attachment /// and that can be used to specify it. This method will return another object that has a method /// whose name is the name of the second attachment, and so on. See the documentation of the macros /// for more details. TODO: put link here /// /// ```ignore // FIXME: unignore /// # #[macro_use] extern crate vulkano; /// # fn main() { /// # let device: std::sync::Arc<vulkano::device::Device> = return; /// use std::sync::Arc; /// use vulkano::format::Format; /// use vulkano::framebuffer::Framebuffer; /// /// let render_pass = single_pass_renderpass!(device.clone(), /// attachments: { /// // `foo` is a custom name we give to the first and only attachment. /// foo: { /// load: Clear, /// store: Store, /// format: Format::R8G8B8A8Unorm, /// samples: 1, /// } /// }, /// pass: { /// color: [foo], // Repeat the attachment name here. /// depth_stencil: {} /// } /// ).unwrap(); /// /// # let my_image: Arc<vulkano::image::ImageViewAccess> = return; /// let framebuffer = { /// let atch = render_pass.desc().start_attachments().foo(my_image.clone() as Arc<_>); /// Framebuffer::new(render_pass, [1024, 768, 1], atch).unwrap() /// }; /// # } /// ``` #[derive(Debug)] pub struct Framebuffer<Rp, A> { device: Arc<Device>, render_pass: Rp, framebuffer: vk::Framebuffer, dimensions: [u32; 3], resources: A, } impl<Rp> Framebuffer<Rp, Box<AttachmentsList + Send + Sync>> { /// Builds a new framebuffer. /// /// The `attachments` parameter depends on which render pass implementation is used. // TODO: allow ImageView pub fn
<Ia>(render_pass: Rp, dimensions: [u32; 3], attachments: Ia) -> Result<Arc<Framebuffer<Rp, Box<AttachmentsList + Send + Sync>>>, FramebufferCreationError> where Rp: RenderPassAbstract + RenderPassDescAttachmentsList<Ia> { let device = render_pass.device().clone(); // This function call is supposed to check whether the attachments are valid. // For more safety, we do some additional `debug_assert`s below. let attachments = try!(render_pass.check_attachments_list(attachments)); // TODO: add a debug assertion that checks whether the attachments are compatible // with the RP ; this should be checked by the RenderPassDescAttachmentsList trait // impl, but we can double-check in debug mode // Checking the dimensions against the limits. { let limits = render_pass.device().physical_device().limits(); let limits = [limits.max_framebuffer_width(), limits.max_framebuffer_height(), limits.max_framebuffer_layers()]; if dimensions[0] > limits[0] || dimensions[1] > limits[1] || dimensions[2] > limits[2] { return Err(FramebufferCreationError::DimensionsTooLarge); } } // Checking the dimensions against the attachments. if let Some(dims_constraints) = attachments.intersection_dimensions() { if dims_constraints[0] < dimensions[0] || dims_constraints[1] < dimensions[1] || dims_constraints[2] < dimensions[2] { return Err(FramebufferCreationError::AttachmentTooSmall); } } let ids: SmallVec<[vk::ImageView; 8]> = attachments.raw_image_view_handles().into_iter().map(|v| v.internal_object()).collect(); let framebuffer = unsafe { let vk = render_pass.device().pointers(); let infos = vk::FramebufferCreateInfo { sType: vk::STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, pNext: ptr::null(), flags: 0, // reserved renderPass: render_pass.inner().internal_object(), attachmentCount: ids.len() as u32, pAttachments: ids.as_ptr(), width: dimensions[0], height: dimensions[1], layers: dimensions[2], }; let mut output = mem::uninitialized(); try!(check_errors(vk.CreateFramebuffer(device.internal_object(), &infos, ptr::null(), &mut output))); output }; Ok(Arc::new(Framebuffer { device: device, render_pass: render_pass, framebuffer: framebuffer, dimensions: dimensions, resources: attachments, })) } } impl<Rp, A> Framebuffer<Rp, A> { /// Returns the width, height and layers of this framebuffer. #[inline] pub fn dimensions(&self) -> [u32; 3] { self.dimensions } /// Returns the width of the framebuffer in pixels. #[inline] pub fn width(&self) -> u32 { self.dimensions[0] } /// Returns the height of the framebuffer in pixels. #[inline] pub fn height(&self) -> u32 { self.dimensions[1] } /// Returns the number of layers (or depth) of the framebuffer. #[inline] pub fn layers(&self) -> u32 { self.dimensions[2] } /// Returns the device that was used to create this framebuffer. #[inline] pub fn device(&self) -> &Arc<Device> { &self.device } /// Returns the renderpass that was used to create this framebuffer. #[inline] pub fn render_pass(&self) -> &Rp { &self.render_pass } } unsafe impl<Rp, A> FramebufferAbstract for Framebuffer<Rp, A> where Rp: RenderPassAbstract, A: AttachmentsList { #[inline] fn inner(&self) -> FramebufferSys { FramebufferSys(self.framebuffer, PhantomData) } #[inline] fn dimensions(&self) -> [u32; 3] { self.dimensions } #[inline] fn attachments(&self) -> Vec<&ImageViewAccess> { self.resources.as_image_view_accesses() } } unsafe impl<Rp, A> RenderPassDesc for Framebuffer<Rp, A> where Rp: RenderPassDesc { #[inline] fn num_attachments(&self) -> usize { self.render_pass.num_attachments() } #[inline] fn attachment_desc(&self, num: usize) -> Option<LayoutAttachmentDescription> { self.render_pass.attachment_desc(num) } #[inline] fn num_subpasses(&self) -> usize { self.render_pass.num_subpasses() } #[inline] fn subpass_desc(&self, num: usize) -> Option<LayoutPassDescription> { self.render_pass.subpass_desc(num) } #[inline] fn num_dependencies(&self) -> usize { self.render_pass.num_dependencies() } #[inline] fn dependency_desc(&self, num: usize) -> Option<LayoutPassDependencyDescription> { self.render_pass.dependency_desc(num) } } unsafe impl<At, Rp, A> RenderPassDescAttachmentsList<At> for Framebuffer<Rp, A> where Rp: RenderPassDescAttachmentsList<At> { #[inline] fn check_attachments_list(&self, atch: At) -> Result<Box<AttachmentsList + Send + Sync>, FramebufferCreationError> { self.render_pass.check_attachments_list(atch) } } unsafe impl<C, Rp, A> RenderPassDescClearValues<C> for Framebuffer<Rp, A> where Rp: RenderPassDescClearValues<C> { #[inline] fn convert_clear_values(&self, vals: C) -> Box<Iterator<Item = ClearValue>> { self.render_pass.convert_clear_values(vals) } } unsafe impl<Rp, A> RenderPassAbstract for Framebuffer<Rp, A> where Rp: RenderPassAbstract { #[inline] fn inner(&self) -> RenderPassSys { self.render_pass.inner() } } unsafe impl<Rp, A> DeviceOwned for Framebuffer<Rp, A> { #[inline] fn device(&self) -> &Arc<Device> { &self.device } } impl<Rp, A> Drop for Framebuffer<Rp, A> { #[inline] fn drop(&mut self) { unsafe { let vk = self.device.pointers(); vk.DestroyFramebuffer(self.device.internal_object(), self.framebuffer, ptr::null()); } } } /// Opaque object that represents the internals of a framebuffer. #[derive(Debug, Copy, Clone)] pub struct FramebufferSys<'a>(vk::Framebuffer, PhantomData<&'a ()>); unsafe impl<'a> VulkanObject for FramebufferSys<'a> { type Object = vk::Framebuffer; #[inline] fn internal_object(&self) -> vk::Framebuffer { self.0 } } /// Error that can happen when creating a framebuffer object. #[derive(Copy, Clone, Debug)] pub enum FramebufferCreationError { /// Out of memory. OomError(OomError), /// The requested dimensions exceed the device's limits. DimensionsTooLarge, /// One of the attachments is too small compared to the requested framebuffer dimensions. AttachmentTooSmall, /// The number of attachments doesn't match the number expected by the render pass. AttachmentsCountMismatch { /// Expected number of attachments. expected: usize, /// Number of attachments that were given. obtained: usize, }, /// One of the images cannot be used as the requested attachment. IncompatibleAttachment { /// Zero-based id of the attachment. attachment_num: usize, /// The problem. error: IncompatibleRenderPassAttachmentError, }, } impl From<OomError> for FramebufferCreationError { #[inline] fn from(err: OomError) -> FramebufferCreationError { FramebufferCreationError::OomError(err) } } impl error::Error for FramebufferCreationError { #[inline] fn description(&self) -> &str { match *self { FramebufferCreationError::OomError(_) => "no memory available", FramebufferCreationError::DimensionsTooLarge => "the dimensions of the framebuffer \ are too large", FramebufferCreationError::AttachmentTooSmall => { "one of the attachments is too small compared to the requested framebuffer \ dimensions" }, FramebufferCreationError::AttachmentsCountMismatch { .. } => { "the number of attachments doesn't match the number expected by the render pass" }, FramebufferCreationError::IncompatibleAttachment { .. } => { "one of the images cannot be used as the requested attachment" }, } } #[inline] fn cause(&self) -> Option<&error::Error> { match *self { FramebufferCreationError::OomError(ref err) => Some(err), FramebufferCreationError::IncompatibleAttachment { ref error, .. } => Some(error), _ => None, } } } impl fmt::Display for FramebufferCreationError { #[inline] fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(fmt, "{}", error::Error::description(self)) } } impl From<Error> for FramebufferCreationError { #[inline] fn from(err: Error) -> FramebufferCreationError { FramebufferCreationError::from(OomError::from(err)) } } /* FIXME: restore #[cfg(test)] mod tests { use format::R8G8B8A8Unorm; use framebuffer::Framebuffer; use framebuffer::FramebufferCreationError; use image::attachment::AttachmentImage; #[test] fn simple_create() { let (device, _) = gfx_dev_and_queue!(); let render_pass = single_pass_renderpass! { attachments: { color: { load: Clear, store: DontCare, format: R8G8B8A8Unorm, } }, pass: { color: [color], depth_stencil: {} } }.unwrap(); let image = AttachmentImage::new(&device, [1024, 768], R8G8B8A8Unorm).unwrap(); let _ = Framebuffer::new(render_pass, [1024, 768, 1], example::AList { color: image.clone() }).unwrap(); } #[test] fn framebuffer_too_large() { let (device, _) = gfx_dev_and_queue!(); let render_pass = example::CustomRenderPass::new(&device, &example::Formats { color: (R8G8B8A8Unorm, 1) }).unwrap(); let image = AttachmentImage::new(&device, [1024, 768], R8G8B8A8Unorm).unwrap(); let alist = example::AList { color: image.clone() }; match Framebuffer::new(render_pass, [0xffffffff, 0xffffffff, 0xffffffff], alist) { Err(FramebufferCreationError::DimensionsTooLarge) => (), _ => panic!() } } #[test] fn attachment_too_small() { let (device, _) = gfx_dev_and_queue!(); let render_pass = example::CustomRenderPass::new(&device, &example::Formats { color: (R8G8B8A8Unorm, 1) }).unwrap(); let image = AttachmentImage::new(&device, [512, 512], R8G8B8A8Unorm).unwrap(); let alist = example::AList { color: image.clone() }; match Framebuffer::new(render_pass, [600, 600, 1], alist) { Err(FramebufferCreationError::AttachmentTooSmall) => (), _ => panic!() } } }*/
new
identifier_name
framebuffer.rs
// Copyright (c) 2016 The vulkano developers // Licensed under the Apache License, Version 2.0 // <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, // at your option. All files in the project carrying such // notice may not be copied, modified, or distributed except // according to those terms. use std::error; use std::fmt; use std::marker::PhantomData; use std::mem; use std::ptr; use std::sync::Arc; use smallvec::SmallVec; use device::Device; use device::DeviceOwned; use format::ClearValue; use framebuffer::AttachmentsList; use framebuffer::FramebufferAbstract; use framebuffer::IncompatibleRenderPassAttachmentError; use framebuffer::LayoutAttachmentDescription; use framebuffer::LayoutPassDependencyDescription; use framebuffer::LayoutPassDescription; use framebuffer::RenderPassAbstract; use framebuffer::RenderPassDescClearValues; use framebuffer::RenderPassDescAttachmentsList; use framebuffer::RenderPassDesc; use framebuffer::RenderPassSys; use image::ImageViewAccess; use Error; use OomError; use VulkanObject; use VulkanPointers; use check_errors; use vk; /// Contains the list of images attached to a render pass. /// /// Creating a framebuffer is done by passing the render pass object, the dimensions of the /// framebuffer, and the list of attachments to `Framebuffer::new()`. /// /// Just like all render pass objects implement the `RenderPassAbstract` trait, all framebuffer /// objects implement the `FramebufferAbstract` trait. This means that you can cast any /// `Arc<Framebuffer<..>>` into an `Arc<FramebufferAbstract + Send + Sync>` for easier storage. /// /// ## With a generic list of attachments /// /// The list of attachments passed to `Framebuffer::new()` can be of various types, but one of the /// possibilities is to pass an object of type `Vec<Arc<ImageView + Send + Sync>>`. /// /// > **Note**: If you access a render pass object through the `RenderPassAbstract` trait, passing /// > a `Vec<Arc<ImageView + Send + Sync>>` is the only possible method. /// /// The framebuffer constructor will perform various checks to make sure that the number of images /// is correct and that each image can be used with this render pass. /// /// ```ignore // FIXME: unignore /// # use std::sync::Arc; /// # use vulkano::framebuffer::RenderPassAbstract; /// use vulkano::framebuffer::Framebuffer; /// /// # let render_pass: Arc<RenderPassAbstract + Send + Sync> = return; /// # let my_image: Arc<vulkano::image::ImageViewAccess> = return; /// // let render_pass: Arc<RenderPassAbstract + Send + Sync> = ...; /// let framebuffer = Framebuffer::new(render_pass.clone(), [1024, 768, 1], /// vec![my_image.clone() as Arc<_>]).unwrap(); /// ``` /// /// ## With a specialized list of attachments /// /// The list of attachments can also be of any type `T`, as long as the render pass description /// implements the trait `RenderPassDescAttachmentsList<T>`. /// /// For example if you pass a render pass object that implements /// `RenderPassDescAttachmentsList<Foo>`, then you can pass a `Foo` as the list of attachments. /// /// > **Note**: The reason why `Vec<Arc<ImageView + Send + Sync>>` always works (see previous section) is that /// > render pass descriptions are required to always implement /// > `RenderPassDescAttachmentsList<Vec<Arc<ImageViewAccess + Send + Sync>>>`. /// /// When it comes to the `single_pass_renderpass!` and `ordered_passes_renderpass!` macros, you can /// build a list of attachments by calling `start_attachments()` on the render pass description, /// which will return an object that has a method whose name is the name of the first attachment /// and that can be used to specify it. This method will return another object that has a method /// whose name is the name of the second attachment, and so on. See the documentation of the macros /// for more details. TODO: put link here /// /// ```ignore // FIXME: unignore /// # #[macro_use] extern crate vulkano; /// # fn main() { /// # let device: std::sync::Arc<vulkano::device::Device> = return; /// use std::sync::Arc; /// use vulkano::format::Format; /// use vulkano::framebuffer::Framebuffer; /// /// let render_pass = single_pass_renderpass!(device.clone(), /// attachments: { /// // `foo` is a custom name we give to the first and only attachment. /// foo: { /// load: Clear, /// store: Store, /// format: Format::R8G8B8A8Unorm, /// samples: 1, /// } /// }, /// pass: { /// color: [foo], // Repeat the attachment name here. /// depth_stencil: {} /// } /// ).unwrap(); /// /// # let my_image: Arc<vulkano::image::ImageViewAccess> = return; /// let framebuffer = { /// let atch = render_pass.desc().start_attachments().foo(my_image.clone() as Arc<_>); /// Framebuffer::new(render_pass, [1024, 768, 1], atch).unwrap() /// }; /// # } /// ``` #[derive(Debug)] pub struct Framebuffer<Rp, A> { device: Arc<Device>, render_pass: Rp, framebuffer: vk::Framebuffer, dimensions: [u32; 3], resources: A, } impl<Rp> Framebuffer<Rp, Box<AttachmentsList + Send + Sync>> { /// Builds a new framebuffer. /// /// The `attachments` parameter depends on which render pass implementation is used. // TODO: allow ImageView pub fn new<Ia>(render_pass: Rp, dimensions: [u32; 3], attachments: Ia) -> Result<Arc<Framebuffer<Rp, Box<AttachmentsList + Send + Sync>>>, FramebufferCreationError> where Rp: RenderPassAbstract + RenderPassDescAttachmentsList<Ia> { let device = render_pass.device().clone(); // This function call is supposed to check whether the attachments are valid. // For more safety, we do some additional `debug_assert`s below. let attachments = try!(render_pass.check_attachments_list(attachments)); // TODO: add a debug assertion that checks whether the attachments are compatible // with the RP ; this should be checked by the RenderPassDescAttachmentsList trait // impl, but we can double-check in debug mode // Checking the dimensions against the limits. { let limits = render_pass.device().physical_device().limits(); let limits = [limits.max_framebuffer_width(), limits.max_framebuffer_height(), limits.max_framebuffer_layers()]; if dimensions[0] > limits[0] || dimensions[1] > limits[1] || dimensions[2] > limits[2]
} // Checking the dimensions against the attachments. if let Some(dims_constraints) = attachments.intersection_dimensions() { if dims_constraints[0] < dimensions[0] || dims_constraints[1] < dimensions[1] || dims_constraints[2] < dimensions[2] { return Err(FramebufferCreationError::AttachmentTooSmall); } } let ids: SmallVec<[vk::ImageView; 8]> = attachments.raw_image_view_handles().into_iter().map(|v| v.internal_object()).collect(); let framebuffer = unsafe { let vk = render_pass.device().pointers(); let infos = vk::FramebufferCreateInfo { sType: vk::STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, pNext: ptr::null(), flags: 0, // reserved renderPass: render_pass.inner().internal_object(), attachmentCount: ids.len() as u32, pAttachments: ids.as_ptr(), width: dimensions[0], height: dimensions[1], layers: dimensions[2], }; let mut output = mem::uninitialized(); try!(check_errors(vk.CreateFramebuffer(device.internal_object(), &infos, ptr::null(), &mut output))); output }; Ok(Arc::new(Framebuffer { device: device, render_pass: render_pass, framebuffer: framebuffer, dimensions: dimensions, resources: attachments, })) } } impl<Rp, A> Framebuffer<Rp, A> { /// Returns the width, height and layers of this framebuffer. #[inline] pub fn dimensions(&self) -> [u32; 3] { self.dimensions } /// Returns the width of the framebuffer in pixels. #[inline] pub fn width(&self) -> u32 { self.dimensions[0] } /// Returns the height of the framebuffer in pixels. #[inline] pub fn height(&self) -> u32 { self.dimensions[1] } /// Returns the number of layers (or depth) of the framebuffer. #[inline] pub fn layers(&self) -> u32 { self.dimensions[2] } /// Returns the device that was used to create this framebuffer. #[inline] pub fn device(&self) -> &Arc<Device> { &self.device } /// Returns the renderpass that was used to create this framebuffer. #[inline] pub fn render_pass(&self) -> &Rp { &self.render_pass } } unsafe impl<Rp, A> FramebufferAbstract for Framebuffer<Rp, A> where Rp: RenderPassAbstract, A: AttachmentsList { #[inline] fn inner(&self) -> FramebufferSys { FramebufferSys(self.framebuffer, PhantomData) } #[inline] fn dimensions(&self) -> [u32; 3] { self.dimensions } #[inline] fn attachments(&self) -> Vec<&ImageViewAccess> { self.resources.as_image_view_accesses() } } unsafe impl<Rp, A> RenderPassDesc for Framebuffer<Rp, A> where Rp: RenderPassDesc { #[inline] fn num_attachments(&self) -> usize { self.render_pass.num_attachments() } #[inline] fn attachment_desc(&self, num: usize) -> Option<LayoutAttachmentDescription> { self.render_pass.attachment_desc(num) } #[inline] fn num_subpasses(&self) -> usize { self.render_pass.num_subpasses() } #[inline] fn subpass_desc(&self, num: usize) -> Option<LayoutPassDescription> { self.render_pass.subpass_desc(num) } #[inline] fn num_dependencies(&self) -> usize { self.render_pass.num_dependencies() } #[inline] fn dependency_desc(&self, num: usize) -> Option<LayoutPassDependencyDescription> { self.render_pass.dependency_desc(num) } } unsafe impl<At, Rp, A> RenderPassDescAttachmentsList<At> for Framebuffer<Rp, A> where Rp: RenderPassDescAttachmentsList<At> { #[inline] fn check_attachments_list(&self, atch: At) -> Result<Box<AttachmentsList + Send + Sync>, FramebufferCreationError> { self.render_pass.check_attachments_list(atch) } } unsafe impl<C, Rp, A> RenderPassDescClearValues<C> for Framebuffer<Rp, A> where Rp: RenderPassDescClearValues<C> { #[inline] fn convert_clear_values(&self, vals: C) -> Box<Iterator<Item = ClearValue>> { self.render_pass.convert_clear_values(vals) } } unsafe impl<Rp, A> RenderPassAbstract for Framebuffer<Rp, A> where Rp: RenderPassAbstract { #[inline] fn inner(&self) -> RenderPassSys { self.render_pass.inner() } } unsafe impl<Rp, A> DeviceOwned for Framebuffer<Rp, A> { #[inline] fn device(&self) -> &Arc<Device> { &self.device } } impl<Rp, A> Drop for Framebuffer<Rp, A> { #[inline] fn drop(&mut self) { unsafe { let vk = self.device.pointers(); vk.DestroyFramebuffer(self.device.internal_object(), self.framebuffer, ptr::null()); } } } /// Opaque object that represents the internals of a framebuffer. #[derive(Debug, Copy, Clone)] pub struct FramebufferSys<'a>(vk::Framebuffer, PhantomData<&'a ()>); unsafe impl<'a> VulkanObject for FramebufferSys<'a> { type Object = vk::Framebuffer; #[inline] fn internal_object(&self) -> vk::Framebuffer { self.0 } } /// Error that can happen when creating a framebuffer object. #[derive(Copy, Clone, Debug)] pub enum FramebufferCreationError { /// Out of memory. OomError(OomError), /// The requested dimensions exceed the device's limits. DimensionsTooLarge, /// One of the attachments is too small compared to the requested framebuffer dimensions. AttachmentTooSmall, /// The number of attachments doesn't match the number expected by the render pass. AttachmentsCountMismatch { /// Expected number of attachments. expected: usize, /// Number of attachments that were given. obtained: usize, }, /// One of the images cannot be used as the requested attachment. IncompatibleAttachment { /// Zero-based id of the attachment. attachment_num: usize, /// The problem. error: IncompatibleRenderPassAttachmentError, }, } impl From<OomError> for FramebufferCreationError { #[inline] fn from(err: OomError) -> FramebufferCreationError { FramebufferCreationError::OomError(err) } } impl error::Error for FramebufferCreationError { #[inline] fn description(&self) -> &str { match *self { FramebufferCreationError::OomError(_) => "no memory available", FramebufferCreationError::DimensionsTooLarge => "the dimensions of the framebuffer \ are too large", FramebufferCreationError::AttachmentTooSmall => { "one of the attachments is too small compared to the requested framebuffer \ dimensions" }, FramebufferCreationError::AttachmentsCountMismatch { .. } => { "the number of attachments doesn't match the number expected by the render pass" }, FramebufferCreationError::IncompatibleAttachment { .. } => { "one of the images cannot be used as the requested attachment" }, } } #[inline] fn cause(&self) -> Option<&error::Error> { match *self { FramebufferCreationError::OomError(ref err) => Some(err), FramebufferCreationError::IncompatibleAttachment { ref error, .. } => Some(error), _ => None, } } } impl fmt::Display for FramebufferCreationError { #[inline] fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(fmt, "{}", error::Error::description(self)) } } impl From<Error> for FramebufferCreationError { #[inline] fn from(err: Error) -> FramebufferCreationError { FramebufferCreationError::from(OomError::from(err)) } } /* FIXME: restore #[cfg(test)] mod tests { use format::R8G8B8A8Unorm; use framebuffer::Framebuffer; use framebuffer::FramebufferCreationError; use image::attachment::AttachmentImage; #[test] fn simple_create() { let (device, _) = gfx_dev_and_queue!(); let render_pass = single_pass_renderpass! { attachments: { color: { load: Clear, store: DontCare, format: R8G8B8A8Unorm, } }, pass: { color: [color], depth_stencil: {} } }.unwrap(); let image = AttachmentImage::new(&device, [1024, 768], R8G8B8A8Unorm).unwrap(); let _ = Framebuffer::new(render_pass, [1024, 768, 1], example::AList { color: image.clone() }).unwrap(); } #[test] fn framebuffer_too_large() { let (device, _) = gfx_dev_and_queue!(); let render_pass = example::CustomRenderPass::new(&device, &example::Formats { color: (R8G8B8A8Unorm, 1) }).unwrap(); let image = AttachmentImage::new(&device, [1024, 768], R8G8B8A8Unorm).unwrap(); let alist = example::AList { color: image.clone() }; match Framebuffer::new(render_pass, [0xffffffff, 0xffffffff, 0xffffffff], alist) { Err(FramebufferCreationError::DimensionsTooLarge) => (), _ => panic!() } } #[test] fn attachment_too_small() { let (device, _) = gfx_dev_and_queue!(); let render_pass = example::CustomRenderPass::new(&device, &example::Formats { color: (R8G8B8A8Unorm, 1) }).unwrap(); let image = AttachmentImage::new(&device, [512, 512], R8G8B8A8Unorm).unwrap(); let alist = example::AList { color: image.clone() }; match Framebuffer::new(render_pass, [600, 600, 1], alist) { Err(FramebufferCreationError::AttachmentTooSmall) => (), _ => panic!() } } }*/
{ return Err(FramebufferCreationError::DimensionsTooLarge); }
conditional_block
utils.go
/* * Copyright (c) 2013 Matt Jibson <matt.jibson@gmail.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ package goapp import ( "bytes" "encoding/xml" "fmt" "html" "html/template" "image" _ "image/gif" _ "image/jpeg" _ "image/png" "io/ioutil" "math/rand" "net/http" "net/url" "strings" "time" "appengine" "appengine/blobstore" aimage "appengine/image" "appengine/urlfetch" "appengine/user" "code.google.com/p/go-charset/charset" _ "code.google.com/p/go-charset/data" mpg "github.com/MiniProfiler/go/miniprofiler_gae" "github.com/mjibson/goon" "goapp/atom" "goapp/rdf" "goapp/rss" ) func serveError(w http.ResponseWriter, err error) { http.Error(w, err.Error(), http.StatusInternalServerError) } type Includes struct { Angular string BootstrapCss string BootstrapJs string Jquery string JqueryUI string Underscore string MiniProfiler template.HTML User *User Messages []string GoogleAnalyticsId string GoogleAnalyticsHost string IsDev bool IsAdmin bool StripeKey string StripePlans []Plan } var ( Angular string BootstrapCss string BootstrapJs string Jquery string JqueryUI string Underscore string isDevServer bool ) func init() { angular_ver := "1.0.5" bootstrap_ver := "2.3.1" jquery_ver := "1.9.1" jqueryui_ver := "1.10.3" underscore_ver := "1.4.4" isDevServer = appengine.IsDevAppServer() if appengine.IsDevAppServer() { Angular = fmt.Sprintf("/static/js/angular-%v.js", angular_ver) BootstrapCss = fmt.Sprintf("/static/css/bootstrap-combined-%v.css", bootstrap_ver) BootstrapJs = fmt.Sprintf("/static/js/bootstrap-%v.js", bootstrap_ver) Jquery = fmt.Sprintf("/static/js/jquery-%v.js", jquery_ver) JqueryUI = fmt.Sprintf("/static/js/jquery-ui-%v.js", jqueryui_ver) Underscore = fmt.Sprintf("/static/js/underscore-%v.js", underscore_ver) } else { Angular = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/angularjs/%v/angular.min.js", angular_ver) BootstrapCss = fmt.Sprintf("//netdna.bootstrapcdn.com/twitter-bootstrap/%v/css/bootstrap-combined.min.css", bootstrap_ver) BootstrapJs = fmt.Sprintf("//netdna.bootstrapcdn.com/twitter-bootstrap/%v/js/bootstrap.min.js", bootstrap_ver) Jquery = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/jquery/%v/jquery.min.js", jquery_ver) JqueryUI = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/jqueryui/%v/jquery-ui.min.js", jqueryui_ver) Underscore = fmt.Sprintf("/static/js/underscore-%v.min.js", underscore_ver) } } func includes(c mpg.Context, w http.ResponseWriter, r *http.Request) *Includes { i := &Includes{ Angular: Angular, BootstrapCss: BootstrapCss, BootstrapJs: BootstrapJs, Jquery: Jquery, JqueryUI: JqueryUI, Underscore: Underscore, MiniProfiler: c.Includes(r), GoogleAnalyticsId: GOOGLE_ANALYTICS_ID, GoogleAnalyticsHost: GOOGLE_ANALYTICS_HOST, IsDev: isDevServer, StripeKey: STRIPE_KEY, StripePlans: STRIPE_PLANS, } if cu := user.Current(c); cu != nil { gn := goon.FromContext(c) user := &User{Id: cu.ID} if err := gn.Get(user); err == nil { i.User = user i.IsAdmin = cu.Admin if len(user.Messages) > 0 { i.Messages = user.Messages user.Messages = nil gn.Put(user) } /* if _, err := r.Cookie("update-bug"); err != nil { i.Messages = append(i.Messages, "Go Read had some problems updating feeds. It may take a while for new stories to appear again. Sorry about that.") http.SetCookie(w, &http.Cookie{ Name: "update-bug", Value: "done", Expires: time.Now().Add(time.Hour * 24 * 7), }) } */ } } return i } var dateFormats = []string{ "01-02-2006", "01/02/2006 15:04:05 MST", "02 Jan 2006 15:04 MST", "02 Jan 2006 15:04:05 -0700", "02 Jan 2006 15:04:05 MST", "02 Jan 2006 15:04:05 UT", "02 Jan 2006", "02-01-2006 15:04:05 MST", "02.01.2006 -0700", "02.01.2006 15:04:05", "02/01/2006 15:04:05", "02/01/2006", "06-1-2 15:04", "06/1/2 15:04", "1/2/2006 15:04:05 MST", "1/2/2006 3:04:05 PM", "15:04 02.01.2006 -0700", "2 Jan 2006 15:04:05 MST", "2 Jan 2006", "2 January 2006 15:04:05 -0700", "2 January 2006", "2006 January 02", "2006-01-02 00:00:00.0 15:04:05.0 -0700", "2006-01-02 15:04", "2006-01-02 15:04:05 -0700", "2006-01-02 15:04:05 MST", "2006-01-02 15:04:05-07:00", "2006-01-02 15:04:05Z", "2006-01-02", "2006-01-02T15:04-07:00", "2006-01-02T15:04:05 -0700", "2006-01-02T15:04:05", "2006-01-02T15:04:05-0700", "2006-01-02T15:04:05-07:00", "2006-01-02T15:04:05-07:00:00", "2006-01-02T15:04:05:-0700", "2006-01-02T15:04:05:00", "2006-01-02T15:04:05Z", "2006-1-02T15:04:05Z", "2006-1-2 15:04:05", "2006-1-2", "2006/01/02", "6-1-2 15:04", "6/1/2 15:04", "Jan 02 2006 03:04:05PM", "Jan 2, 2006 15:04:05 MST", "Jan 2, 2006 3:04:05 PM MST", "January 02, 2006 03:04 PM", "January 02, 2006 15:04", "January 02, 2006 15:04:05 MST", "January 02, 2006", "January 2, 2006 03:04 PM", "January 2, 2006 15:04:05 MST", "January 2, 2006 15:04:05", "January 2, 2006", "January 2, 2006, 3:04 p.m.", "Mon 02 Jan 2006 15:04:05 -0700", "Mon 2 Jan 2006 15:04:05 MST", "Mon Jan 2 15:04 2006", "Mon Jan 2 15:04:05 2006 MST", "Mon, 02 Jan 06 15:04:05 MST", "Mon, 02 Jan 2006 15:04 -0700", "Mon, 02 Jan 2006 15:04 MST", "Mon, 02 Jan 2006 15:04:05 --0700", "Mon, 02 Jan 2006 15:04:05 -07", "Mon, 02 Jan 2006 15:04:05 -0700", "Mon, 02 Jan 2006 15:04:05 -07:00", "Mon, 02 Jan 2006 15:04:05 00", "Mon, 02 Jan 2006 15:04:05 MST -0700", "Mon, 02 Jan 2006 15:04:05 MST", "Mon, 02 Jan 2006 15:04:05 MST-07:00", "Mon, 02 Jan 2006 15:04:05 UT", "Mon, 02 Jan 2006 15:04:05 Z", "Mon, 02 Jan 2006 15:04:05", "Mon, 02 Jan 2006 15:04:05MST", "Mon, 02 Jan 2006 3:04:05 PM MST", "Mon, 02 Jan 2006", "Mon, 02 January 2006", "Mon, 2 Jan 06 15:04:05 -0700", "Mon, 2 Jan 06 15:04:05 MST", "Mon, 2 Jan 15:04:05 MST", "Mon, 2 Jan 2006 15:04", "Mon, 2 Jan 2006 15:04:05 -0700 MST", "Mon, 2 Jan 2006 15:04:05 -0700", "Mon, 2 Jan 2006 15:04:05 MST", "Mon, 2 Jan 2006 15:04:05 UT", "Mon, 2 Jan 2006 15:04:05", "Mon, 2 Jan 2006 15:04:05-0700", "Mon, 2 Jan 2006 15:04:05MST", "Mon, 2 Jan 2006 15:4:5 MST", "Mon, 2 Jan 2006", "Mon, 2 Jan 2006, 15:04 -0700", "Mon, 2 January 2006 15:04:05 -0700", "Mon, 2 January 2006 15:04:05 MST", "Mon, 2 January 2006, 15:04 -0700", "Mon, 2 January 2006, 15:04:05 MST", "Mon, 2, Jan 2006 15:4", "Mon, Jan 2 2006 15:04:05 -0700", "Mon, Jan 2 2006 15:04:05 -700", "Mon, January 02, 2006, 15:04:05 MST", "Mon, January 2 2006 15:04:05 -0700", "Mon,02 Jan 2006 15:04:05 -0700", "Mon,02 January 2006 14:04:05 MST", "Monday, 02 January 2006 15:04:05 -0700", "Monday, 02 January 2006 15:04:05 MST", "Monday, 02 January 2006 15:04:05", "Monday, 2 Jan 2006 15:04:05 -0700", "Monday, 2 Jan 2006 15:04:05 MST", "Monday, 2 January 2006 15:04:05 -0700", "Monday, 2 January 2006 15:04:05 MST", "Monday, January 02, 2006", "Monday, January 2, 2006 03:04 PM", "Monday, January 2, 2006 15:04:05 MST", "Monday, January 2, 2006", "Updated January 2, 2006", "mon,2 Jan 2006 15:04:05 MST", time.ANSIC, time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC822, time.RFC822Z, time.RFC850, time.RubyDate, time.UnixDate, } func parseDate(c appengine.Context, feed *Feed, ds ...string) (t time.Time, err error) { for _, d := range ds { d = strings.TrimSpace(d) if d == "" { continue } for _, f := range dateFormats { if t, err = time.Parse(f, d); err == nil { return } } gn := goon.FromContext(c) gn.Put(&DateFormat{ Id: d, Parent: gn.Key(feed), }) } err = fmt.Errorf("could not parse date: %v", strings.Join(ds, ", ")) return } func ParseFeed(c appengine.Context, u string, b []byte) (*Feed, []*Story) { f := Feed{Url: u} var s []*Story a := atom.Feed{} var atomerr, rsserr, rdferr, err error var fb, eb *url.URL d := xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader if atomerr = d.Decode(&a); atomerr == nil { f.Title = a.Title if t, err := parseDate(c, &f, string(a.Updated)); err == nil { f.Updated = t } if fb, err = url.Parse(a.XMLBase); err != nil { fb, _ = url.Parse("") } if len(a.Link) > 0 { f.Link = findBestAtomLink(c, a.Link).Href if l, err := fb.Parse(f.Link); err == nil { f.Link = l.String() } } for _, i := range a.Entry { if eb, err = fb.Parse(i.XMLBase); err != nil { eb = fb } st := Story{ Id: i.ID, Title: i.Title, } if t, err := parseDate(c, &f, string(i.Updated)); err == nil { st.Updated = t } if t, err := parseDate(c, &f, string(i.Published)); err == nil { st.Published = t } if len(i.Link) > 0 { st.Link = findBestAtomLink(c, i.Link).Href if l, err := eb.Parse(st.Link); err == nil { st.Link = l.String() } } if i.Author != nil { st.Author = i.Author.Name } if i.Content != nil { if len(strings.TrimSpace(i.Content.Body)) != 0 { st.content = i.Content.Body } else if len(i.Content.InnerXML) != 0 { st.content = i.Content.InnerXML } } else if i.Summary != nil { st.content = i.Summary.Body } s = append(s, &st) } return parseFix(c, &f, s) } r := rss.Rss{} d = xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader d.DefaultSpace = "DefaultSpace" if rsserr = d.Decode(&r); rsserr == nil { f.Title = r.Title f.Link = r.Link if t, err := parseDate(c, &f, r.LastBuildDate, r.PubDate); err == nil { f.Updated = t } else { c.Warningf("no rss feed date: %v", f.Link) } for _, i := range r.Items { st := Story{ Link: i.Link, Author: i.Author, } if i.Title != "" { st.Title = i.Title } else if i.Description != ""
if i.Content != "" { st.content = i.Content } else if i.Title != "" && i.Description != "" { st.content = i.Description } if i.Guid != nil { st.Id = i.Guid.Guid } if i.Media != nil { st.MediaContent = i.Media.URL } if t, err := parseDate(c, &f, i.PubDate, i.Date, i.Published); err == nil { st.Published = t st.Updated = t } s = append(s, &st) } return parseFix(c, &f, s) } rd := rdf.RDF{} d = xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader if rdferr = d.Decode(&rd); rdferr == nil { if rd.Channel != nil { f.Title = rd.Channel.Title f.Link = rd.Channel.Link if t, err := parseDate(c, &f, rd.Channel.Date); err == nil { f.Updated = t } } for _, i := range rd.Item { st := Story{ Id: i.About, Title: i.Title, Link: i.Link, Author: i.Creator, } st.content = html.UnescapeString(i.Description) if t, err := parseDate(c, &f, i.Date); err == nil { st.Published = t st.Updated = t } s = append(s, &st) } return parseFix(c, &f, s) } c.Warningf("atom parse error: %s", atomerr.Error()) c.Warningf("xml parse error: %s", rsserr.Error()) c.Warningf("rdf parse error: %s", rdferr.Error()) return nil, nil } func findBestAtomLink(c appengine.Context, links []atom.Link) atom.Link { getScore := func(l atom.Link) int { switch { case l.Rel == "hub": return 0 case l.Type == "text/html": return 3 case l.Rel != "self": return 2 default: return 1 } } var bestlink atom.Link bestscore := -1 for _, l := range links { score := getScore(l) if score > bestscore { bestlink = l bestscore = score } } return bestlink } func parseFix(c appengine.Context, f *Feed, ss []*Story) (*Feed, []*Story) { g := goon.FromContext(c) f.Checked = time.Now() fk := g.Key(f) f.Image = loadImage(c, f) if u, err := url.Parse(f.Url); err == nil { if ul, err := u.Parse(f.Link); err == nil { f.Link = ul.String() } } base, err := url.Parse(f.Link) if err != nil { c.Warningf("unable to parse link: %v", f.Link) } for _, s := range ss { s.Parent = fk s.Created = f.Checked if !s.Updated.IsZero() && s.Published.IsZero() { s.Published = s.Updated } if s.Published.IsZero() || f.Checked.Before(s.Published) { s.Published = f.Checked } if !s.Updated.IsZero() { s.Date = s.Updated.Unix() } else { s.Date = s.Published.Unix() } if s.Id == "" { if s.Link != "" { s.Id = s.Link } else if s.Title != "" { s.Id = s.Title } else { c.Errorf("story has no id: %v", s) return nil, nil } } // if a story doesn't have a link, see if its id is a URL if s.Link == "" { if u, err := url.Parse(s.Id); err == nil { s.Link = u.String() } } if base != nil && s.Link != "" { link, err := base.Parse(s.Link) if err == nil { s.Link = link.String() } else { c.Warningf("unable to resolve link: %v", s.Link) } } const keySize = 500 sk := g.Key(s) if kl := len(sk.Encode()); kl > keySize { c.Errorf("key too long: %v, %v, %v", kl, f.Url, s.Id) return nil, nil } su, serr := url.Parse(s.Link) if serr != nil { su = &url.URL{} s.Link = "" } s.content, s.Summary = Sanitize(s.content, su) } return f, ss } func loadImage(c appengine.Context, f *Feed) string { s := f.Link if s == "" { s = f.Url } u, err := url.Parse(s) if err != nil { return "" } u.Path = "/favicon.ico" u.RawQuery = "" u.Fragment = "" g := goon.FromContext(c) i := &Image{Id: u.String()} if err := g.Get(i); err == nil { return i.Url } client := urlfetch.Client(c) r, err := client.Get(u.String()) if err != nil || r.StatusCode != http.StatusOK || r.ContentLength == 0 { return "" } b, err := ioutil.ReadAll(r.Body) r.Body.Close() if err != nil { return "" } buf := bytes.NewBuffer(b) _, t, err := image.DecodeConfig(buf) if err != nil { t = "application/octet-stream" } else { t = "image/" + t } w, err := blobstore.Create(c, t) if err != nil { return "" } if _, err := w.Write(b); err != nil { return "" } if w.Close() != nil { return "" } i.Blob, _ = w.Key() su, err := aimage.ServingURL(c, i.Blob, &aimage.ServingURLOptions{Size: 16}) if err != nil { return "" } i.Url = su.String() g.Put(i) return i.Url } func updateAverage(f *Feed, previousUpdate time.Time, updateCount int) { if previousUpdate.IsZero() || updateCount < 1 { return } // if multiple updates occurred, assume they were evenly spaced interval := time.Since(previousUpdate) / time.Duration(updateCount) // rather than calculate a strict mean, we weight // each new interval, gradually decaying the influence // of older intervals old := float64(f.Average) * (1.0 - NewIntervalWeight) cur := float64(interval) * NewIntervalWeight f.Average = time.Duration(old + cur) } func scheduleNextUpdate(f *Feed) { now := time.Now() if f.Date.IsZero() { f.NextUpdate = now.Add(UpdateDefault) return } // calculate the delay until next check based on average time between updates pause := time.Duration(float64(f.Average) * UpdateFraction) // if we have never found an update, start with a default wait time if pause == 0 { pause = UpdateDefault } // if it has been much longer than expected since the last update, // gradually reduce the frequency of checks since := time.Since(f.Date) if since > pause*UpdateLongFactor { pause = time.Duration(float64(since) / UpdateLongFactor) } // enforce some limits if pause < UpdateMin { pause = UpdateMin } if pause > UpdateMax { pause = UpdateMax } // introduce a little random jitter to break up // convoys of updates jitter := time.Duration(rand.Int63n(int64(UpdateJitter))) if rand.Intn(2) == 0 { pause += jitter } else { pause -= jitter } f.NextUpdate = time.Now().Add(pause) }
{ i.Title = i.Description }
conditional_block
utils.go
/* * Copyright (c) 2013 Matt Jibson <matt.jibson@gmail.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ package goapp import ( "bytes" "encoding/xml" "fmt" "html" "html/template" "image" _ "image/gif" _ "image/jpeg" _ "image/png" "io/ioutil" "math/rand" "net/http" "net/url" "strings" "time" "appengine" "appengine/blobstore" aimage "appengine/image" "appengine/urlfetch" "appengine/user" "code.google.com/p/go-charset/charset" _ "code.google.com/p/go-charset/data" mpg "github.com/MiniProfiler/go/miniprofiler_gae" "github.com/mjibson/goon" "goapp/atom" "goapp/rdf" "goapp/rss" ) func serveError(w http.ResponseWriter, err error) { http.Error(w, err.Error(), http.StatusInternalServerError) } type Includes struct { Angular string BootstrapCss string BootstrapJs string Jquery string JqueryUI string Underscore string MiniProfiler template.HTML User *User Messages []string GoogleAnalyticsId string GoogleAnalyticsHost string IsDev bool IsAdmin bool StripeKey string StripePlans []Plan } var ( Angular string BootstrapCss string BootstrapJs string Jquery string JqueryUI string Underscore string isDevServer bool ) func init() { angular_ver := "1.0.5" bootstrap_ver := "2.3.1" jquery_ver := "1.9.1" jqueryui_ver := "1.10.3" underscore_ver := "1.4.4" isDevServer = appengine.IsDevAppServer() if appengine.IsDevAppServer() { Angular = fmt.Sprintf("/static/js/angular-%v.js", angular_ver) BootstrapCss = fmt.Sprintf("/static/css/bootstrap-combined-%v.css", bootstrap_ver) BootstrapJs = fmt.Sprintf("/static/js/bootstrap-%v.js", bootstrap_ver) Jquery = fmt.Sprintf("/static/js/jquery-%v.js", jquery_ver) JqueryUI = fmt.Sprintf("/static/js/jquery-ui-%v.js", jqueryui_ver) Underscore = fmt.Sprintf("/static/js/underscore-%v.js", underscore_ver) } else { Angular = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/angularjs/%v/angular.min.js", angular_ver) BootstrapCss = fmt.Sprintf("//netdna.bootstrapcdn.com/twitter-bootstrap/%v/css/bootstrap-combined.min.css", bootstrap_ver) BootstrapJs = fmt.Sprintf("//netdna.bootstrapcdn.com/twitter-bootstrap/%v/js/bootstrap.min.js", bootstrap_ver) Jquery = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/jquery/%v/jquery.min.js", jquery_ver) JqueryUI = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/jqueryui/%v/jquery-ui.min.js", jqueryui_ver) Underscore = fmt.Sprintf("/static/js/underscore-%v.min.js", underscore_ver) } } func includes(c mpg.Context, w http.ResponseWriter, r *http.Request) *Includes { i := &Includes{ Angular: Angular, BootstrapCss: BootstrapCss, BootstrapJs: BootstrapJs, Jquery: Jquery, JqueryUI: JqueryUI, Underscore: Underscore, MiniProfiler: c.Includes(r), GoogleAnalyticsId: GOOGLE_ANALYTICS_ID, GoogleAnalyticsHost: GOOGLE_ANALYTICS_HOST, IsDev: isDevServer, StripeKey: STRIPE_KEY, StripePlans: STRIPE_PLANS, } if cu := user.Current(c); cu != nil { gn := goon.FromContext(c) user := &User{Id: cu.ID} if err := gn.Get(user); err == nil { i.User = user i.IsAdmin = cu.Admin if len(user.Messages) > 0 { i.Messages = user.Messages user.Messages = nil gn.Put(user) } /* if _, err := r.Cookie("update-bug"); err != nil { i.Messages = append(i.Messages, "Go Read had some problems updating feeds. It may take a while for new stories to appear again. Sorry about that.") http.SetCookie(w, &http.Cookie{ Name: "update-bug", Value: "done", Expires: time.Now().Add(time.Hour * 24 * 7), }) } */ } } return i } var dateFormats = []string{ "01-02-2006", "01/02/2006 15:04:05 MST", "02 Jan 2006 15:04 MST", "02 Jan 2006 15:04:05 -0700", "02 Jan 2006 15:04:05 MST", "02 Jan 2006 15:04:05 UT", "02 Jan 2006", "02-01-2006 15:04:05 MST", "02.01.2006 -0700", "02.01.2006 15:04:05", "02/01/2006 15:04:05", "02/01/2006", "06-1-2 15:04", "06/1/2 15:04", "1/2/2006 15:04:05 MST", "1/2/2006 3:04:05 PM", "15:04 02.01.2006 -0700", "2 Jan 2006 15:04:05 MST", "2 Jan 2006", "2 January 2006 15:04:05 -0700", "2 January 2006", "2006 January 02", "2006-01-02 00:00:00.0 15:04:05.0 -0700", "2006-01-02 15:04", "2006-01-02 15:04:05 -0700", "2006-01-02 15:04:05 MST", "2006-01-02 15:04:05-07:00", "2006-01-02 15:04:05Z", "2006-01-02", "2006-01-02T15:04-07:00", "2006-01-02T15:04:05 -0700", "2006-01-02T15:04:05", "2006-01-02T15:04:05-0700", "2006-01-02T15:04:05-07:00", "2006-01-02T15:04:05-07:00:00", "2006-01-02T15:04:05:-0700", "2006-01-02T15:04:05:00", "2006-01-02T15:04:05Z", "2006-1-02T15:04:05Z", "2006-1-2 15:04:05", "2006-1-2", "2006/01/02", "6-1-2 15:04", "6/1/2 15:04", "Jan 02 2006 03:04:05PM", "Jan 2, 2006 15:04:05 MST", "Jan 2, 2006 3:04:05 PM MST", "January 02, 2006 03:04 PM", "January 02, 2006 15:04", "January 02, 2006 15:04:05 MST", "January 02, 2006", "January 2, 2006 03:04 PM", "January 2, 2006 15:04:05 MST", "January 2, 2006 15:04:05", "January 2, 2006", "January 2, 2006, 3:04 p.m.", "Mon 02 Jan 2006 15:04:05 -0700", "Mon 2 Jan 2006 15:04:05 MST", "Mon Jan 2 15:04 2006", "Mon Jan 2 15:04:05 2006 MST", "Mon, 02 Jan 06 15:04:05 MST", "Mon, 02 Jan 2006 15:04 -0700", "Mon, 02 Jan 2006 15:04 MST", "Mon, 02 Jan 2006 15:04:05 --0700", "Mon, 02 Jan 2006 15:04:05 -07", "Mon, 02 Jan 2006 15:04:05 -0700", "Mon, 02 Jan 2006 15:04:05 -07:00", "Mon, 02 Jan 2006 15:04:05 00", "Mon, 02 Jan 2006 15:04:05 MST -0700", "Mon, 02 Jan 2006 15:04:05 MST", "Mon, 02 Jan 2006 15:04:05 MST-07:00", "Mon, 02 Jan 2006 15:04:05 UT", "Mon, 02 Jan 2006 15:04:05 Z", "Mon, 02 Jan 2006 15:04:05", "Mon, 02 Jan 2006 15:04:05MST", "Mon, 02 Jan 2006 3:04:05 PM MST", "Mon, 02 Jan 2006", "Mon, 02 January 2006", "Mon, 2 Jan 06 15:04:05 -0700", "Mon, 2 Jan 06 15:04:05 MST", "Mon, 2 Jan 15:04:05 MST", "Mon, 2 Jan 2006 15:04", "Mon, 2 Jan 2006 15:04:05 -0700 MST", "Mon, 2 Jan 2006 15:04:05 -0700", "Mon, 2 Jan 2006 15:04:05 MST", "Mon, 2 Jan 2006 15:04:05 UT", "Mon, 2 Jan 2006 15:04:05", "Mon, 2 Jan 2006 15:04:05-0700", "Mon, 2 Jan 2006 15:04:05MST", "Mon, 2 Jan 2006 15:4:5 MST", "Mon, 2 Jan 2006", "Mon, 2 Jan 2006, 15:04 -0700", "Mon, 2 January 2006 15:04:05 -0700", "Mon, 2 January 2006 15:04:05 MST", "Mon, 2 January 2006, 15:04 -0700", "Mon, 2 January 2006, 15:04:05 MST", "Mon, 2, Jan 2006 15:4", "Mon, Jan 2 2006 15:04:05 -0700", "Mon, Jan 2 2006 15:04:05 -700", "Mon, January 02, 2006, 15:04:05 MST", "Mon, January 2 2006 15:04:05 -0700", "Mon,02 Jan 2006 15:04:05 -0700", "Mon,02 January 2006 14:04:05 MST", "Monday, 02 January 2006 15:04:05 -0700", "Monday, 02 January 2006 15:04:05 MST", "Monday, 02 January 2006 15:04:05", "Monday, 2 Jan 2006 15:04:05 -0700", "Monday, 2 Jan 2006 15:04:05 MST", "Monday, 2 January 2006 15:04:05 -0700", "Monday, 2 January 2006 15:04:05 MST", "Monday, January 02, 2006", "Monday, January 2, 2006 03:04 PM", "Monday, January 2, 2006 15:04:05 MST", "Monday, January 2, 2006", "Updated January 2, 2006", "mon,2 Jan 2006 15:04:05 MST", time.ANSIC, time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC822, time.RFC822Z, time.RFC850, time.RubyDate, time.UnixDate, } func parseDate(c appengine.Context, feed *Feed, ds ...string) (t time.Time, err error) { for _, d := range ds { d = strings.TrimSpace(d) if d == "" { continue } for _, f := range dateFormats { if t, err = time.Parse(f, d); err == nil { return } } gn := goon.FromContext(c) gn.Put(&DateFormat{ Id: d, Parent: gn.Key(feed), }) } err = fmt.Errorf("could not parse date: %v", strings.Join(ds, ", ")) return } func ParseFeed(c appengine.Context, u string, b []byte) (*Feed, []*Story) { f := Feed{Url: u} var s []*Story a := atom.Feed{} var atomerr, rsserr, rdferr, err error var fb, eb *url.URL d := xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader if atomerr = d.Decode(&a); atomerr == nil { f.Title = a.Title if t, err := parseDate(c, &f, string(a.Updated)); err == nil { f.Updated = t } if fb, err = url.Parse(a.XMLBase); err != nil { fb, _ = url.Parse("") } if len(a.Link) > 0 { f.Link = findBestAtomLink(c, a.Link).Href if l, err := fb.Parse(f.Link); err == nil { f.Link = l.String() } } for _, i := range a.Entry { if eb, err = fb.Parse(i.XMLBase); err != nil { eb = fb } st := Story{ Id: i.ID, Title: i.Title, } if t, err := parseDate(c, &f, string(i.Updated)); err == nil { st.Updated = t } if t, err := parseDate(c, &f, string(i.Published)); err == nil { st.Published = t } if len(i.Link) > 0 { st.Link = findBestAtomLink(c, i.Link).Href if l, err := eb.Parse(st.Link); err == nil { st.Link = l.String() } } if i.Author != nil { st.Author = i.Author.Name } if i.Content != nil { if len(strings.TrimSpace(i.Content.Body)) != 0 { st.content = i.Content.Body } else if len(i.Content.InnerXML) != 0 { st.content = i.Content.InnerXML } } else if i.Summary != nil { st.content = i.Summary.Body } s = append(s, &st) } return parseFix(c, &f, s) } r := rss.Rss{} d = xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader d.DefaultSpace = "DefaultSpace" if rsserr = d.Decode(&r); rsserr == nil { f.Title = r.Title f.Link = r.Link if t, err := parseDate(c, &f, r.LastBuildDate, r.PubDate); err == nil { f.Updated = t } else { c.Warningf("no rss feed date: %v", f.Link) } for _, i := range r.Items { st := Story{ Link: i.Link, Author: i.Author, } if i.Title != "" { st.Title = i.Title } else if i.Description != "" { i.Title = i.Description } if i.Content != "" { st.content = i.Content } else if i.Title != "" && i.Description != "" { st.content = i.Description } if i.Guid != nil { st.Id = i.Guid.Guid } if i.Media != nil { st.MediaContent = i.Media.URL } if t, err := parseDate(c, &f, i.PubDate, i.Date, i.Published); err == nil { st.Published = t st.Updated = t } s = append(s, &st) } return parseFix(c, &f, s) } rd := rdf.RDF{} d = xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader if rdferr = d.Decode(&rd); rdferr == nil { if rd.Channel != nil { f.Title = rd.Channel.Title f.Link = rd.Channel.Link if t, err := parseDate(c, &f, rd.Channel.Date); err == nil { f.Updated = t } } for _, i := range rd.Item { st := Story{ Id: i.About, Title: i.Title, Link: i.Link, Author: i.Creator, } st.content = html.UnescapeString(i.Description) if t, err := parseDate(c, &f, i.Date); err == nil { st.Published = t st.Updated = t } s = append(s, &st) } return parseFix(c, &f, s) } c.Warningf("atom parse error: %s", atomerr.Error()) c.Warningf("xml parse error: %s", rsserr.Error()) c.Warningf("rdf parse error: %s", rdferr.Error()) return nil, nil } func findBestAtomLink(c appengine.Context, links []atom.Link) atom.Link { getScore := func(l atom.Link) int { switch { case l.Rel == "hub": return 0 case l.Type == "text/html": return 3 case l.Rel != "self": return 2 default: return 1 } } var bestlink atom.Link bestscore := -1 for _, l := range links { score := getScore(l) if score > bestscore { bestlink = l bestscore = score } } return bestlink } func parseFix(c appengine.Context, f *Feed, ss []*Story) (*Feed, []*Story) { g := goon.FromContext(c) f.Checked = time.Now() fk := g.Key(f) f.Image = loadImage(c, f) if u, err := url.Parse(f.Url); err == nil { if ul, err := u.Parse(f.Link); err == nil { f.Link = ul.String() } } base, err := url.Parse(f.Link) if err != nil { c.Warningf("unable to parse link: %v", f.Link) } for _, s := range ss { s.Parent = fk s.Created = f.Checked if !s.Updated.IsZero() && s.Published.IsZero() { s.Published = s.Updated } if s.Published.IsZero() || f.Checked.Before(s.Published) { s.Published = f.Checked } if !s.Updated.IsZero() { s.Date = s.Updated.Unix() } else { s.Date = s.Published.Unix() } if s.Id == "" { if s.Link != "" { s.Id = s.Link } else if s.Title != "" { s.Id = s.Title } else { c.Errorf("story has no id: %v", s) return nil, nil } } // if a story doesn't have a link, see if its id is a URL if s.Link == "" { if u, err := url.Parse(s.Id); err == nil { s.Link = u.String() } } if base != nil && s.Link != "" {
if err == nil { s.Link = link.String() } else { c.Warningf("unable to resolve link: %v", s.Link) } } const keySize = 500 sk := g.Key(s) if kl := len(sk.Encode()); kl > keySize { c.Errorf("key too long: %v, %v, %v", kl, f.Url, s.Id) return nil, nil } su, serr := url.Parse(s.Link) if serr != nil { su = &url.URL{} s.Link = "" } s.content, s.Summary = Sanitize(s.content, su) } return f, ss } func loadImage(c appengine.Context, f *Feed) string { s := f.Link if s == "" { s = f.Url } u, err := url.Parse(s) if err != nil { return "" } u.Path = "/favicon.ico" u.RawQuery = "" u.Fragment = "" g := goon.FromContext(c) i := &Image{Id: u.String()} if err := g.Get(i); err == nil { return i.Url } client := urlfetch.Client(c) r, err := client.Get(u.String()) if err != nil || r.StatusCode != http.StatusOK || r.ContentLength == 0 { return "" } b, err := ioutil.ReadAll(r.Body) r.Body.Close() if err != nil { return "" } buf := bytes.NewBuffer(b) _, t, err := image.DecodeConfig(buf) if err != nil { t = "application/octet-stream" } else { t = "image/" + t } w, err := blobstore.Create(c, t) if err != nil { return "" } if _, err := w.Write(b); err != nil { return "" } if w.Close() != nil { return "" } i.Blob, _ = w.Key() su, err := aimage.ServingURL(c, i.Blob, &aimage.ServingURLOptions{Size: 16}) if err != nil { return "" } i.Url = su.String() g.Put(i) return i.Url } func updateAverage(f *Feed, previousUpdate time.Time, updateCount int) { if previousUpdate.IsZero() || updateCount < 1 { return } // if multiple updates occurred, assume they were evenly spaced interval := time.Since(previousUpdate) / time.Duration(updateCount) // rather than calculate a strict mean, we weight // each new interval, gradually decaying the influence // of older intervals old := float64(f.Average) * (1.0 - NewIntervalWeight) cur := float64(interval) * NewIntervalWeight f.Average = time.Duration(old + cur) } func scheduleNextUpdate(f *Feed) { now := time.Now() if f.Date.IsZero() { f.NextUpdate = now.Add(UpdateDefault) return } // calculate the delay until next check based on average time between updates pause := time.Duration(float64(f.Average) * UpdateFraction) // if we have never found an update, start with a default wait time if pause == 0 { pause = UpdateDefault } // if it has been much longer than expected since the last update, // gradually reduce the frequency of checks since := time.Since(f.Date) if since > pause*UpdateLongFactor { pause = time.Duration(float64(since) / UpdateLongFactor) } // enforce some limits if pause < UpdateMin { pause = UpdateMin } if pause > UpdateMax { pause = UpdateMax } // introduce a little random jitter to break up // convoys of updates jitter := time.Duration(rand.Int63n(int64(UpdateJitter))) if rand.Intn(2) == 0 { pause += jitter } else { pause -= jitter } f.NextUpdate = time.Now().Add(pause) }
link, err := base.Parse(s.Link)
random_line_split
utils.go
/* * Copyright (c) 2013 Matt Jibson <matt.jibson@gmail.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ package goapp import ( "bytes" "encoding/xml" "fmt" "html" "html/template" "image" _ "image/gif" _ "image/jpeg" _ "image/png" "io/ioutil" "math/rand" "net/http" "net/url" "strings" "time" "appengine" "appengine/blobstore" aimage "appengine/image" "appengine/urlfetch" "appengine/user" "code.google.com/p/go-charset/charset" _ "code.google.com/p/go-charset/data" mpg "github.com/MiniProfiler/go/miniprofiler_gae" "github.com/mjibson/goon" "goapp/atom" "goapp/rdf" "goapp/rss" ) func serveError(w http.ResponseWriter, err error) { http.Error(w, err.Error(), http.StatusInternalServerError) } type Includes struct { Angular string BootstrapCss string BootstrapJs string Jquery string JqueryUI string Underscore string MiniProfiler template.HTML User *User Messages []string GoogleAnalyticsId string GoogleAnalyticsHost string IsDev bool IsAdmin bool StripeKey string StripePlans []Plan } var ( Angular string BootstrapCss string BootstrapJs string Jquery string JqueryUI string Underscore string isDevServer bool ) func init() { angular_ver := "1.0.5" bootstrap_ver := "2.3.1" jquery_ver := "1.9.1" jqueryui_ver := "1.10.3" underscore_ver := "1.4.4" isDevServer = appengine.IsDevAppServer() if appengine.IsDevAppServer() { Angular = fmt.Sprintf("/static/js/angular-%v.js", angular_ver) BootstrapCss = fmt.Sprintf("/static/css/bootstrap-combined-%v.css", bootstrap_ver) BootstrapJs = fmt.Sprintf("/static/js/bootstrap-%v.js", bootstrap_ver) Jquery = fmt.Sprintf("/static/js/jquery-%v.js", jquery_ver) JqueryUI = fmt.Sprintf("/static/js/jquery-ui-%v.js", jqueryui_ver) Underscore = fmt.Sprintf("/static/js/underscore-%v.js", underscore_ver) } else { Angular = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/angularjs/%v/angular.min.js", angular_ver) BootstrapCss = fmt.Sprintf("//netdna.bootstrapcdn.com/twitter-bootstrap/%v/css/bootstrap-combined.min.css", bootstrap_ver) BootstrapJs = fmt.Sprintf("//netdna.bootstrapcdn.com/twitter-bootstrap/%v/js/bootstrap.min.js", bootstrap_ver) Jquery = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/jquery/%v/jquery.min.js", jquery_ver) JqueryUI = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/jqueryui/%v/jquery-ui.min.js", jqueryui_ver) Underscore = fmt.Sprintf("/static/js/underscore-%v.min.js", underscore_ver) } } func includes(c mpg.Context, w http.ResponseWriter, r *http.Request) *Includes { i := &Includes{ Angular: Angular, BootstrapCss: BootstrapCss, BootstrapJs: BootstrapJs, Jquery: Jquery, JqueryUI: JqueryUI, Underscore: Underscore, MiniProfiler: c.Includes(r), GoogleAnalyticsId: GOOGLE_ANALYTICS_ID, GoogleAnalyticsHost: GOOGLE_ANALYTICS_HOST, IsDev: isDevServer, StripeKey: STRIPE_KEY, StripePlans: STRIPE_PLANS, } if cu := user.Current(c); cu != nil { gn := goon.FromContext(c) user := &User{Id: cu.ID} if err := gn.Get(user); err == nil { i.User = user i.IsAdmin = cu.Admin if len(user.Messages) > 0 { i.Messages = user.Messages user.Messages = nil gn.Put(user) } /* if _, err := r.Cookie("update-bug"); err != nil { i.Messages = append(i.Messages, "Go Read had some problems updating feeds. It may take a while for new stories to appear again. Sorry about that.") http.SetCookie(w, &http.Cookie{ Name: "update-bug", Value: "done", Expires: time.Now().Add(time.Hour * 24 * 7), }) } */ } } return i } var dateFormats = []string{ "01-02-2006", "01/02/2006 15:04:05 MST", "02 Jan 2006 15:04 MST", "02 Jan 2006 15:04:05 -0700", "02 Jan 2006 15:04:05 MST", "02 Jan 2006 15:04:05 UT", "02 Jan 2006", "02-01-2006 15:04:05 MST", "02.01.2006 -0700", "02.01.2006 15:04:05", "02/01/2006 15:04:05", "02/01/2006", "06-1-2 15:04", "06/1/2 15:04", "1/2/2006 15:04:05 MST", "1/2/2006 3:04:05 PM", "15:04 02.01.2006 -0700", "2 Jan 2006 15:04:05 MST", "2 Jan 2006", "2 January 2006 15:04:05 -0700", "2 January 2006", "2006 January 02", "2006-01-02 00:00:00.0 15:04:05.0 -0700", "2006-01-02 15:04", "2006-01-02 15:04:05 -0700", "2006-01-02 15:04:05 MST", "2006-01-02 15:04:05-07:00", "2006-01-02 15:04:05Z", "2006-01-02", "2006-01-02T15:04-07:00", "2006-01-02T15:04:05 -0700", "2006-01-02T15:04:05", "2006-01-02T15:04:05-0700", "2006-01-02T15:04:05-07:00", "2006-01-02T15:04:05-07:00:00", "2006-01-02T15:04:05:-0700", "2006-01-02T15:04:05:00", "2006-01-02T15:04:05Z", "2006-1-02T15:04:05Z", "2006-1-2 15:04:05", "2006-1-2", "2006/01/02", "6-1-2 15:04", "6/1/2 15:04", "Jan 02 2006 03:04:05PM", "Jan 2, 2006 15:04:05 MST", "Jan 2, 2006 3:04:05 PM MST", "January 02, 2006 03:04 PM", "January 02, 2006 15:04", "January 02, 2006 15:04:05 MST", "January 02, 2006", "January 2, 2006 03:04 PM", "January 2, 2006 15:04:05 MST", "January 2, 2006 15:04:05", "January 2, 2006", "January 2, 2006, 3:04 p.m.", "Mon 02 Jan 2006 15:04:05 -0700", "Mon 2 Jan 2006 15:04:05 MST", "Mon Jan 2 15:04 2006", "Mon Jan 2 15:04:05 2006 MST", "Mon, 02 Jan 06 15:04:05 MST", "Mon, 02 Jan 2006 15:04 -0700", "Mon, 02 Jan 2006 15:04 MST", "Mon, 02 Jan 2006 15:04:05 --0700", "Mon, 02 Jan 2006 15:04:05 -07", "Mon, 02 Jan 2006 15:04:05 -0700", "Mon, 02 Jan 2006 15:04:05 -07:00", "Mon, 02 Jan 2006 15:04:05 00", "Mon, 02 Jan 2006 15:04:05 MST -0700", "Mon, 02 Jan 2006 15:04:05 MST", "Mon, 02 Jan 2006 15:04:05 MST-07:00", "Mon, 02 Jan 2006 15:04:05 UT", "Mon, 02 Jan 2006 15:04:05 Z", "Mon, 02 Jan 2006 15:04:05", "Mon, 02 Jan 2006 15:04:05MST", "Mon, 02 Jan 2006 3:04:05 PM MST", "Mon, 02 Jan 2006", "Mon, 02 January 2006", "Mon, 2 Jan 06 15:04:05 -0700", "Mon, 2 Jan 06 15:04:05 MST", "Mon, 2 Jan 15:04:05 MST", "Mon, 2 Jan 2006 15:04", "Mon, 2 Jan 2006 15:04:05 -0700 MST", "Mon, 2 Jan 2006 15:04:05 -0700", "Mon, 2 Jan 2006 15:04:05 MST", "Mon, 2 Jan 2006 15:04:05 UT", "Mon, 2 Jan 2006 15:04:05", "Mon, 2 Jan 2006 15:04:05-0700", "Mon, 2 Jan 2006 15:04:05MST", "Mon, 2 Jan 2006 15:4:5 MST", "Mon, 2 Jan 2006", "Mon, 2 Jan 2006, 15:04 -0700", "Mon, 2 January 2006 15:04:05 -0700", "Mon, 2 January 2006 15:04:05 MST", "Mon, 2 January 2006, 15:04 -0700", "Mon, 2 January 2006, 15:04:05 MST", "Mon, 2, Jan 2006 15:4", "Mon, Jan 2 2006 15:04:05 -0700", "Mon, Jan 2 2006 15:04:05 -700", "Mon, January 02, 2006, 15:04:05 MST", "Mon, January 2 2006 15:04:05 -0700", "Mon,02 Jan 2006 15:04:05 -0700", "Mon,02 January 2006 14:04:05 MST", "Monday, 02 January 2006 15:04:05 -0700", "Monday, 02 January 2006 15:04:05 MST", "Monday, 02 January 2006 15:04:05", "Monday, 2 Jan 2006 15:04:05 -0700", "Monday, 2 Jan 2006 15:04:05 MST", "Monday, 2 January 2006 15:04:05 -0700", "Monday, 2 January 2006 15:04:05 MST", "Monday, January 02, 2006", "Monday, January 2, 2006 03:04 PM", "Monday, January 2, 2006 15:04:05 MST", "Monday, January 2, 2006", "Updated January 2, 2006", "mon,2 Jan 2006 15:04:05 MST", time.ANSIC, time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC822, time.RFC822Z, time.RFC850, time.RubyDate, time.UnixDate, } func parseDate(c appengine.Context, feed *Feed, ds ...string) (t time.Time, err error) { for _, d := range ds { d = strings.TrimSpace(d) if d == "" { continue } for _, f := range dateFormats { if t, err = time.Parse(f, d); err == nil { return } } gn := goon.FromContext(c) gn.Put(&DateFormat{ Id: d, Parent: gn.Key(feed), }) } err = fmt.Errorf("could not parse date: %v", strings.Join(ds, ", ")) return } func ParseFeed(c appengine.Context, u string, b []byte) (*Feed, []*Story) { f := Feed{Url: u} var s []*Story a := atom.Feed{} var atomerr, rsserr, rdferr, err error var fb, eb *url.URL d := xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader if atomerr = d.Decode(&a); atomerr == nil { f.Title = a.Title if t, err := parseDate(c, &f, string(a.Updated)); err == nil { f.Updated = t } if fb, err = url.Parse(a.XMLBase); err != nil { fb, _ = url.Parse("") } if len(a.Link) > 0 { f.Link = findBestAtomLink(c, a.Link).Href if l, err := fb.Parse(f.Link); err == nil { f.Link = l.String() } } for _, i := range a.Entry { if eb, err = fb.Parse(i.XMLBase); err != nil { eb = fb } st := Story{ Id: i.ID, Title: i.Title, } if t, err := parseDate(c, &f, string(i.Updated)); err == nil { st.Updated = t } if t, err := parseDate(c, &f, string(i.Published)); err == nil { st.Published = t } if len(i.Link) > 0 { st.Link = findBestAtomLink(c, i.Link).Href if l, err := eb.Parse(st.Link); err == nil { st.Link = l.String() } } if i.Author != nil { st.Author = i.Author.Name } if i.Content != nil { if len(strings.TrimSpace(i.Content.Body)) != 0 { st.content = i.Content.Body } else if len(i.Content.InnerXML) != 0 { st.content = i.Content.InnerXML } } else if i.Summary != nil { st.content = i.Summary.Body } s = append(s, &st) } return parseFix(c, &f, s) } r := rss.Rss{} d = xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader d.DefaultSpace = "DefaultSpace" if rsserr = d.Decode(&r); rsserr == nil { f.Title = r.Title f.Link = r.Link if t, err := parseDate(c, &f, r.LastBuildDate, r.PubDate); err == nil { f.Updated = t } else { c.Warningf("no rss feed date: %v", f.Link) } for _, i := range r.Items { st := Story{ Link: i.Link, Author: i.Author, } if i.Title != "" { st.Title = i.Title } else if i.Description != "" { i.Title = i.Description } if i.Content != "" { st.content = i.Content } else if i.Title != "" && i.Description != "" { st.content = i.Description } if i.Guid != nil { st.Id = i.Guid.Guid } if i.Media != nil { st.MediaContent = i.Media.URL } if t, err := parseDate(c, &f, i.PubDate, i.Date, i.Published); err == nil { st.Published = t st.Updated = t } s = append(s, &st) } return parseFix(c, &f, s) } rd := rdf.RDF{} d = xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader if rdferr = d.Decode(&rd); rdferr == nil { if rd.Channel != nil { f.Title = rd.Channel.Title f.Link = rd.Channel.Link if t, err := parseDate(c, &f, rd.Channel.Date); err == nil { f.Updated = t } } for _, i := range rd.Item { st := Story{ Id: i.About, Title: i.Title, Link: i.Link, Author: i.Creator, } st.content = html.UnescapeString(i.Description) if t, err := parseDate(c, &f, i.Date); err == nil { st.Published = t st.Updated = t } s = append(s, &st) } return parseFix(c, &f, s) } c.Warningf("atom parse error: %s", atomerr.Error()) c.Warningf("xml parse error: %s", rsserr.Error()) c.Warningf("rdf parse error: %s", rdferr.Error()) return nil, nil } func findBestAtomLink(c appengine.Context, links []atom.Link) atom.Link { getScore := func(l atom.Link) int { switch { case l.Rel == "hub": return 0 case l.Type == "text/html": return 3 case l.Rel != "self": return 2 default: return 1 } } var bestlink atom.Link bestscore := -1 for _, l := range links { score := getScore(l) if score > bestscore { bestlink = l bestscore = score } } return bestlink } func parseFix(c appengine.Context, f *Feed, ss []*Story) (*Feed, []*Story) { g := goon.FromContext(c) f.Checked = time.Now() fk := g.Key(f) f.Image = loadImage(c, f) if u, err := url.Parse(f.Url); err == nil { if ul, err := u.Parse(f.Link); err == nil { f.Link = ul.String() } } base, err := url.Parse(f.Link) if err != nil { c.Warningf("unable to parse link: %v", f.Link) } for _, s := range ss { s.Parent = fk s.Created = f.Checked if !s.Updated.IsZero() && s.Published.IsZero() { s.Published = s.Updated } if s.Published.IsZero() || f.Checked.Before(s.Published) { s.Published = f.Checked } if !s.Updated.IsZero() { s.Date = s.Updated.Unix() } else { s.Date = s.Published.Unix() } if s.Id == "" { if s.Link != "" { s.Id = s.Link } else if s.Title != "" { s.Id = s.Title } else { c.Errorf("story has no id: %v", s) return nil, nil } } // if a story doesn't have a link, see if its id is a URL if s.Link == "" { if u, err := url.Parse(s.Id); err == nil { s.Link = u.String() } } if base != nil && s.Link != "" { link, err := base.Parse(s.Link) if err == nil { s.Link = link.String() } else { c.Warningf("unable to resolve link: %v", s.Link) } } const keySize = 500 sk := g.Key(s) if kl := len(sk.Encode()); kl > keySize { c.Errorf("key too long: %v, %v, %v", kl, f.Url, s.Id) return nil, nil } su, serr := url.Parse(s.Link) if serr != nil { su = &url.URL{} s.Link = "" } s.content, s.Summary = Sanitize(s.content, su) } return f, ss } func
(c appengine.Context, f *Feed) string { s := f.Link if s == "" { s = f.Url } u, err := url.Parse(s) if err != nil { return "" } u.Path = "/favicon.ico" u.RawQuery = "" u.Fragment = "" g := goon.FromContext(c) i := &Image{Id: u.String()} if err := g.Get(i); err == nil { return i.Url } client := urlfetch.Client(c) r, err := client.Get(u.String()) if err != nil || r.StatusCode != http.StatusOK || r.ContentLength == 0 { return "" } b, err := ioutil.ReadAll(r.Body) r.Body.Close() if err != nil { return "" } buf := bytes.NewBuffer(b) _, t, err := image.DecodeConfig(buf) if err != nil { t = "application/octet-stream" } else { t = "image/" + t } w, err := blobstore.Create(c, t) if err != nil { return "" } if _, err := w.Write(b); err != nil { return "" } if w.Close() != nil { return "" } i.Blob, _ = w.Key() su, err := aimage.ServingURL(c, i.Blob, &aimage.ServingURLOptions{Size: 16}) if err != nil { return "" } i.Url = su.String() g.Put(i) return i.Url } func updateAverage(f *Feed, previousUpdate time.Time, updateCount int) { if previousUpdate.IsZero() || updateCount < 1 { return } // if multiple updates occurred, assume they were evenly spaced interval := time.Since(previousUpdate) / time.Duration(updateCount) // rather than calculate a strict mean, we weight // each new interval, gradually decaying the influence // of older intervals old := float64(f.Average) * (1.0 - NewIntervalWeight) cur := float64(interval) * NewIntervalWeight f.Average = time.Duration(old + cur) } func scheduleNextUpdate(f *Feed) { now := time.Now() if f.Date.IsZero() { f.NextUpdate = now.Add(UpdateDefault) return } // calculate the delay until next check based on average time between updates pause := time.Duration(float64(f.Average) * UpdateFraction) // if we have never found an update, start with a default wait time if pause == 0 { pause = UpdateDefault } // if it has been much longer than expected since the last update, // gradually reduce the frequency of checks since := time.Since(f.Date) if since > pause*UpdateLongFactor { pause = time.Duration(float64(since) / UpdateLongFactor) } // enforce some limits if pause < UpdateMin { pause = UpdateMin } if pause > UpdateMax { pause = UpdateMax } // introduce a little random jitter to break up // convoys of updates jitter := time.Duration(rand.Int63n(int64(UpdateJitter))) if rand.Intn(2) == 0 { pause += jitter } else { pause -= jitter } f.NextUpdate = time.Now().Add(pause) }
loadImage
identifier_name
utils.go
/* * Copyright (c) 2013 Matt Jibson <matt.jibson@gmail.com> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ package goapp import ( "bytes" "encoding/xml" "fmt" "html" "html/template" "image" _ "image/gif" _ "image/jpeg" _ "image/png" "io/ioutil" "math/rand" "net/http" "net/url" "strings" "time" "appengine" "appengine/blobstore" aimage "appengine/image" "appengine/urlfetch" "appengine/user" "code.google.com/p/go-charset/charset" _ "code.google.com/p/go-charset/data" mpg "github.com/MiniProfiler/go/miniprofiler_gae" "github.com/mjibson/goon" "goapp/atom" "goapp/rdf" "goapp/rss" ) func serveError(w http.ResponseWriter, err error) { http.Error(w, err.Error(), http.StatusInternalServerError) } type Includes struct { Angular string BootstrapCss string BootstrapJs string Jquery string JqueryUI string Underscore string MiniProfiler template.HTML User *User Messages []string GoogleAnalyticsId string GoogleAnalyticsHost string IsDev bool IsAdmin bool StripeKey string StripePlans []Plan } var ( Angular string BootstrapCss string BootstrapJs string Jquery string JqueryUI string Underscore string isDevServer bool ) func init() { angular_ver := "1.0.5" bootstrap_ver := "2.3.1" jquery_ver := "1.9.1" jqueryui_ver := "1.10.3" underscore_ver := "1.4.4" isDevServer = appengine.IsDevAppServer() if appengine.IsDevAppServer() { Angular = fmt.Sprintf("/static/js/angular-%v.js", angular_ver) BootstrapCss = fmt.Sprintf("/static/css/bootstrap-combined-%v.css", bootstrap_ver) BootstrapJs = fmt.Sprintf("/static/js/bootstrap-%v.js", bootstrap_ver) Jquery = fmt.Sprintf("/static/js/jquery-%v.js", jquery_ver) JqueryUI = fmt.Sprintf("/static/js/jquery-ui-%v.js", jqueryui_ver) Underscore = fmt.Sprintf("/static/js/underscore-%v.js", underscore_ver) } else { Angular = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/angularjs/%v/angular.min.js", angular_ver) BootstrapCss = fmt.Sprintf("//netdna.bootstrapcdn.com/twitter-bootstrap/%v/css/bootstrap-combined.min.css", bootstrap_ver) BootstrapJs = fmt.Sprintf("//netdna.bootstrapcdn.com/twitter-bootstrap/%v/js/bootstrap.min.js", bootstrap_ver) Jquery = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/jquery/%v/jquery.min.js", jquery_ver) JqueryUI = fmt.Sprintf("//ajax.googleapis.com/ajax/libs/jqueryui/%v/jquery-ui.min.js", jqueryui_ver) Underscore = fmt.Sprintf("/static/js/underscore-%v.min.js", underscore_ver) } } func includes(c mpg.Context, w http.ResponseWriter, r *http.Request) *Includes
var dateFormats = []string{ "01-02-2006", "01/02/2006 15:04:05 MST", "02 Jan 2006 15:04 MST", "02 Jan 2006 15:04:05 -0700", "02 Jan 2006 15:04:05 MST", "02 Jan 2006 15:04:05 UT", "02 Jan 2006", "02-01-2006 15:04:05 MST", "02.01.2006 -0700", "02.01.2006 15:04:05", "02/01/2006 15:04:05", "02/01/2006", "06-1-2 15:04", "06/1/2 15:04", "1/2/2006 15:04:05 MST", "1/2/2006 3:04:05 PM", "15:04 02.01.2006 -0700", "2 Jan 2006 15:04:05 MST", "2 Jan 2006", "2 January 2006 15:04:05 -0700", "2 January 2006", "2006 January 02", "2006-01-02 00:00:00.0 15:04:05.0 -0700", "2006-01-02 15:04", "2006-01-02 15:04:05 -0700", "2006-01-02 15:04:05 MST", "2006-01-02 15:04:05-07:00", "2006-01-02 15:04:05Z", "2006-01-02", "2006-01-02T15:04-07:00", "2006-01-02T15:04:05 -0700", "2006-01-02T15:04:05", "2006-01-02T15:04:05-0700", "2006-01-02T15:04:05-07:00", "2006-01-02T15:04:05-07:00:00", "2006-01-02T15:04:05:-0700", "2006-01-02T15:04:05:00", "2006-01-02T15:04:05Z", "2006-1-02T15:04:05Z", "2006-1-2 15:04:05", "2006-1-2", "2006/01/02", "6-1-2 15:04", "6/1/2 15:04", "Jan 02 2006 03:04:05PM", "Jan 2, 2006 15:04:05 MST", "Jan 2, 2006 3:04:05 PM MST", "January 02, 2006 03:04 PM", "January 02, 2006 15:04", "January 02, 2006 15:04:05 MST", "January 02, 2006", "January 2, 2006 03:04 PM", "January 2, 2006 15:04:05 MST", "January 2, 2006 15:04:05", "January 2, 2006", "January 2, 2006, 3:04 p.m.", "Mon 02 Jan 2006 15:04:05 -0700", "Mon 2 Jan 2006 15:04:05 MST", "Mon Jan 2 15:04 2006", "Mon Jan 2 15:04:05 2006 MST", "Mon, 02 Jan 06 15:04:05 MST", "Mon, 02 Jan 2006 15:04 -0700", "Mon, 02 Jan 2006 15:04 MST", "Mon, 02 Jan 2006 15:04:05 --0700", "Mon, 02 Jan 2006 15:04:05 -07", "Mon, 02 Jan 2006 15:04:05 -0700", "Mon, 02 Jan 2006 15:04:05 -07:00", "Mon, 02 Jan 2006 15:04:05 00", "Mon, 02 Jan 2006 15:04:05 MST -0700", "Mon, 02 Jan 2006 15:04:05 MST", "Mon, 02 Jan 2006 15:04:05 MST-07:00", "Mon, 02 Jan 2006 15:04:05 UT", "Mon, 02 Jan 2006 15:04:05 Z", "Mon, 02 Jan 2006 15:04:05", "Mon, 02 Jan 2006 15:04:05MST", "Mon, 02 Jan 2006 3:04:05 PM MST", "Mon, 02 Jan 2006", "Mon, 02 January 2006", "Mon, 2 Jan 06 15:04:05 -0700", "Mon, 2 Jan 06 15:04:05 MST", "Mon, 2 Jan 15:04:05 MST", "Mon, 2 Jan 2006 15:04", "Mon, 2 Jan 2006 15:04:05 -0700 MST", "Mon, 2 Jan 2006 15:04:05 -0700", "Mon, 2 Jan 2006 15:04:05 MST", "Mon, 2 Jan 2006 15:04:05 UT", "Mon, 2 Jan 2006 15:04:05", "Mon, 2 Jan 2006 15:04:05-0700", "Mon, 2 Jan 2006 15:04:05MST", "Mon, 2 Jan 2006 15:4:5 MST", "Mon, 2 Jan 2006", "Mon, 2 Jan 2006, 15:04 -0700", "Mon, 2 January 2006 15:04:05 -0700", "Mon, 2 January 2006 15:04:05 MST", "Mon, 2 January 2006, 15:04 -0700", "Mon, 2 January 2006, 15:04:05 MST", "Mon, 2, Jan 2006 15:4", "Mon, Jan 2 2006 15:04:05 -0700", "Mon, Jan 2 2006 15:04:05 -700", "Mon, January 02, 2006, 15:04:05 MST", "Mon, January 2 2006 15:04:05 -0700", "Mon,02 Jan 2006 15:04:05 -0700", "Mon,02 January 2006 14:04:05 MST", "Monday, 02 January 2006 15:04:05 -0700", "Monday, 02 January 2006 15:04:05 MST", "Monday, 02 January 2006 15:04:05", "Monday, 2 Jan 2006 15:04:05 -0700", "Monday, 2 Jan 2006 15:04:05 MST", "Monday, 2 January 2006 15:04:05 -0700", "Monday, 2 January 2006 15:04:05 MST", "Monday, January 02, 2006", "Monday, January 2, 2006 03:04 PM", "Monday, January 2, 2006 15:04:05 MST", "Monday, January 2, 2006", "Updated January 2, 2006", "mon,2 Jan 2006 15:04:05 MST", time.ANSIC, time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC822, time.RFC822Z, time.RFC850, time.RubyDate, time.UnixDate, } func parseDate(c appengine.Context, feed *Feed, ds ...string) (t time.Time, err error) { for _, d := range ds { d = strings.TrimSpace(d) if d == "" { continue } for _, f := range dateFormats { if t, err = time.Parse(f, d); err == nil { return } } gn := goon.FromContext(c) gn.Put(&DateFormat{ Id: d, Parent: gn.Key(feed), }) } err = fmt.Errorf("could not parse date: %v", strings.Join(ds, ", ")) return } func ParseFeed(c appengine.Context, u string, b []byte) (*Feed, []*Story) { f := Feed{Url: u} var s []*Story a := atom.Feed{} var atomerr, rsserr, rdferr, err error var fb, eb *url.URL d := xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader if atomerr = d.Decode(&a); atomerr == nil { f.Title = a.Title if t, err := parseDate(c, &f, string(a.Updated)); err == nil { f.Updated = t } if fb, err = url.Parse(a.XMLBase); err != nil { fb, _ = url.Parse("") } if len(a.Link) > 0 { f.Link = findBestAtomLink(c, a.Link).Href if l, err := fb.Parse(f.Link); err == nil { f.Link = l.String() } } for _, i := range a.Entry { if eb, err = fb.Parse(i.XMLBase); err != nil { eb = fb } st := Story{ Id: i.ID, Title: i.Title, } if t, err := parseDate(c, &f, string(i.Updated)); err == nil { st.Updated = t } if t, err := parseDate(c, &f, string(i.Published)); err == nil { st.Published = t } if len(i.Link) > 0 { st.Link = findBestAtomLink(c, i.Link).Href if l, err := eb.Parse(st.Link); err == nil { st.Link = l.String() } } if i.Author != nil { st.Author = i.Author.Name } if i.Content != nil { if len(strings.TrimSpace(i.Content.Body)) != 0 { st.content = i.Content.Body } else if len(i.Content.InnerXML) != 0 { st.content = i.Content.InnerXML } } else if i.Summary != nil { st.content = i.Summary.Body } s = append(s, &st) } return parseFix(c, &f, s) } r := rss.Rss{} d = xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader d.DefaultSpace = "DefaultSpace" if rsserr = d.Decode(&r); rsserr == nil { f.Title = r.Title f.Link = r.Link if t, err := parseDate(c, &f, r.LastBuildDate, r.PubDate); err == nil { f.Updated = t } else { c.Warningf("no rss feed date: %v", f.Link) } for _, i := range r.Items { st := Story{ Link: i.Link, Author: i.Author, } if i.Title != "" { st.Title = i.Title } else if i.Description != "" { i.Title = i.Description } if i.Content != "" { st.content = i.Content } else if i.Title != "" && i.Description != "" { st.content = i.Description } if i.Guid != nil { st.Id = i.Guid.Guid } if i.Media != nil { st.MediaContent = i.Media.URL } if t, err := parseDate(c, &f, i.PubDate, i.Date, i.Published); err == nil { st.Published = t st.Updated = t } s = append(s, &st) } return parseFix(c, &f, s) } rd := rdf.RDF{} d = xml.NewDecoder(bytes.NewReader(b)) d.CharsetReader = charset.NewReader if rdferr = d.Decode(&rd); rdferr == nil { if rd.Channel != nil { f.Title = rd.Channel.Title f.Link = rd.Channel.Link if t, err := parseDate(c, &f, rd.Channel.Date); err == nil { f.Updated = t } } for _, i := range rd.Item { st := Story{ Id: i.About, Title: i.Title, Link: i.Link, Author: i.Creator, } st.content = html.UnescapeString(i.Description) if t, err := parseDate(c, &f, i.Date); err == nil { st.Published = t st.Updated = t } s = append(s, &st) } return parseFix(c, &f, s) } c.Warningf("atom parse error: %s", atomerr.Error()) c.Warningf("xml parse error: %s", rsserr.Error()) c.Warningf("rdf parse error: %s", rdferr.Error()) return nil, nil } func findBestAtomLink(c appengine.Context, links []atom.Link) atom.Link { getScore := func(l atom.Link) int { switch { case l.Rel == "hub": return 0 case l.Type == "text/html": return 3 case l.Rel != "self": return 2 default: return 1 } } var bestlink atom.Link bestscore := -1 for _, l := range links { score := getScore(l) if score > bestscore { bestlink = l bestscore = score } } return bestlink } func parseFix(c appengine.Context, f *Feed, ss []*Story) (*Feed, []*Story) { g := goon.FromContext(c) f.Checked = time.Now() fk := g.Key(f) f.Image = loadImage(c, f) if u, err := url.Parse(f.Url); err == nil { if ul, err := u.Parse(f.Link); err == nil { f.Link = ul.String() } } base, err := url.Parse(f.Link) if err != nil { c.Warningf("unable to parse link: %v", f.Link) } for _, s := range ss { s.Parent = fk s.Created = f.Checked if !s.Updated.IsZero() && s.Published.IsZero() { s.Published = s.Updated } if s.Published.IsZero() || f.Checked.Before(s.Published) { s.Published = f.Checked } if !s.Updated.IsZero() { s.Date = s.Updated.Unix() } else { s.Date = s.Published.Unix() } if s.Id == "" { if s.Link != "" { s.Id = s.Link } else if s.Title != "" { s.Id = s.Title } else { c.Errorf("story has no id: %v", s) return nil, nil } } // if a story doesn't have a link, see if its id is a URL if s.Link == "" { if u, err := url.Parse(s.Id); err == nil { s.Link = u.String() } } if base != nil && s.Link != "" { link, err := base.Parse(s.Link) if err == nil { s.Link = link.String() } else { c.Warningf("unable to resolve link: %v", s.Link) } } const keySize = 500 sk := g.Key(s) if kl := len(sk.Encode()); kl > keySize { c.Errorf("key too long: %v, %v, %v", kl, f.Url, s.Id) return nil, nil } su, serr := url.Parse(s.Link) if serr != nil { su = &url.URL{} s.Link = "" } s.content, s.Summary = Sanitize(s.content, su) } return f, ss } func loadImage(c appengine.Context, f *Feed) string { s := f.Link if s == "" { s = f.Url } u, err := url.Parse(s) if err != nil { return "" } u.Path = "/favicon.ico" u.RawQuery = "" u.Fragment = "" g := goon.FromContext(c) i := &Image{Id: u.String()} if err := g.Get(i); err == nil { return i.Url } client := urlfetch.Client(c) r, err := client.Get(u.String()) if err != nil || r.StatusCode != http.StatusOK || r.ContentLength == 0 { return "" } b, err := ioutil.ReadAll(r.Body) r.Body.Close() if err != nil { return "" } buf := bytes.NewBuffer(b) _, t, err := image.DecodeConfig(buf) if err != nil { t = "application/octet-stream" } else { t = "image/" + t } w, err := blobstore.Create(c, t) if err != nil { return "" } if _, err := w.Write(b); err != nil { return "" } if w.Close() != nil { return "" } i.Blob, _ = w.Key() su, err := aimage.ServingURL(c, i.Blob, &aimage.ServingURLOptions{Size: 16}) if err != nil { return "" } i.Url = su.String() g.Put(i) return i.Url } func updateAverage(f *Feed, previousUpdate time.Time, updateCount int) { if previousUpdate.IsZero() || updateCount < 1 { return } // if multiple updates occurred, assume they were evenly spaced interval := time.Since(previousUpdate) / time.Duration(updateCount) // rather than calculate a strict mean, we weight // each new interval, gradually decaying the influence // of older intervals old := float64(f.Average) * (1.0 - NewIntervalWeight) cur := float64(interval) * NewIntervalWeight f.Average = time.Duration(old + cur) } func scheduleNextUpdate(f *Feed) { now := time.Now() if f.Date.IsZero() { f.NextUpdate = now.Add(UpdateDefault) return } // calculate the delay until next check based on average time between updates pause := time.Duration(float64(f.Average) * UpdateFraction) // if we have never found an update, start with a default wait time if pause == 0 { pause = UpdateDefault } // if it has been much longer than expected since the last update, // gradually reduce the frequency of checks since := time.Since(f.Date) if since > pause*UpdateLongFactor { pause = time.Duration(float64(since) / UpdateLongFactor) } // enforce some limits if pause < UpdateMin { pause = UpdateMin } if pause > UpdateMax { pause = UpdateMax } // introduce a little random jitter to break up // convoys of updates jitter := time.Duration(rand.Int63n(int64(UpdateJitter))) if rand.Intn(2) == 0 { pause += jitter } else { pause -= jitter } f.NextUpdate = time.Now().Add(pause) }
{ i := &Includes{ Angular: Angular, BootstrapCss: BootstrapCss, BootstrapJs: BootstrapJs, Jquery: Jquery, JqueryUI: JqueryUI, Underscore: Underscore, MiniProfiler: c.Includes(r), GoogleAnalyticsId: GOOGLE_ANALYTICS_ID, GoogleAnalyticsHost: GOOGLE_ANALYTICS_HOST, IsDev: isDevServer, StripeKey: STRIPE_KEY, StripePlans: STRIPE_PLANS, } if cu := user.Current(c); cu != nil { gn := goon.FromContext(c) user := &User{Id: cu.ID} if err := gn.Get(user); err == nil { i.User = user i.IsAdmin = cu.Admin if len(user.Messages) > 0 { i.Messages = user.Messages user.Messages = nil gn.Put(user) } /* if _, err := r.Cookie("update-bug"); err != nil { i.Messages = append(i.Messages, "Go Read had some problems updating feeds. It may take a while for new stories to appear again. Sorry about that.") http.SetCookie(w, &http.Cookie{ Name: "update-bug", Value: "done", Expires: time.Now().Add(time.Hour * 24 * 7), }) } */ } } return i }
identifier_body
main.rs
use std::collections::HashMap; use std::fmt; use std::mem; use std::str::FromStr; mod codewars; fn literals_operator() { println!("1 + 2 = {}", 1i32 + 2); println!("0011 ^ 0110 = {:04b}", 0b0011u32 ^ 0b0110u32); println!("0011 << 2 = {:04b}", 0b0011u32 << 2); println!("0011 >> 2 = {:04b}", 0b0011u32 >> 2); println!("0o7 << 1 = {:04o}", 0o7u32 << 1); println!("0x7 << 1 = {:04x}", 0x7u32 << 1); } fn reverse(pair: (i32, bool)) -> (bool, i32) { let (x, y) = pair; (y, x) } #[derive(Debug)] struct Matrix(f32, f32, f32, f32); fn transpose(matrix: Matrix) -> Matrix { Matrix(matrix.0, matrix.2, matrix.1, matrix.3) } impl fmt::Display for Matrix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "( {}, {} )\n( {}, {} )", self.0, self.1, self.2, self.3) } } fn tuple_activity() { println!("{:?}", reverse((20i32, false))); println!("one element tuple: {:?}", (5u32,)); println!("just an integer: {:?}", (5u32)); let tuple = (1, "hello", 4.5, true); let (a, b, c, d) = tuple; println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d); let matrix = Matrix(1.1, 1.2, 2.1, 2.2); println!("Matrix:\n{}", matrix); println!("Transpose:\n{}", transpose(matrix)); } fn analyze_slice(slice: &[Matrix]) { println!("first element of the slice: \n{}", slice[0]); println!("the slice has {} elements.", slice.len()); } fn arrays_slices() { let x: [Matrix; 2] = [ Matrix(10.2f32, 2.1f32, 3.1f32, 4.5f32), Matrix(5.2f32, 6.1f32, 2.1f32, 8.5f32), ]; println!("Array occupies: {:?} bytes", mem::size_of_val(&x)); analyze_slice(&x[1..2]); } #[derive(Debug, Copy, Clone)] struct Person<'a> { name: &'a str, age: u8, } impl<'a> Person<'a> { fn changeName(&mut self, new_name: &'a str) -> &'a Person { self.name = new_name; self } } #[derive(Debug)] struct Point { x: f32, y: f32, } #[derive(Debug)] struct Triangle { top_left: Point, bottom_right: Point, } struct Unit; struct Pair(i32, i32); fn square(point: Point, val: f32) -> Triangle { Triangle { bottom_right: Point { x: point.x + val, y: point.y - val, }, top_left: point, } } fn struct_test() { let mut person = Person { name: "Peter", age: 30u8, }; let new_name = "Janez"; let changedPerson = person.changeName(new_name); println!("Person: {:?}", changedPerson); let point = Point { x: 20.2, y: 30.3 }; println!("Point: {:?}", point); let bottom_right = Point { x: 10.2, ..point }; println!("bottom_right: {:?}", bottom_right); let top_left = Point { x: 2.2, y: 5.3 }; let triangle = Triangle { bottom_right: bottom_right, top_left: top_left, }; println!("{:?}", triangle); let Triangle { bottom_right: Point { x: x1, y: y1 }, top_left: Point { x: x2, y: y2 }, } = triangle; println!("x1: {}, y1: {}, x2: {}, y2:{}", x1, y1, x2, y2); println!("Area:{}", (x2 - x1) * (y2 - y1)); let unit = Unit; let pair = Pair(20, 30); println!("pair contains {:?} and {:?}", pair.0, pair.1); let Pair(x, y) = pair; println!("pair contains {:?} and {:?}", x, y); println!("{:?}", square(point, 20.2)); } enum WebEvent { PageLoad, PageUnload, KeyPress(char), Paste(String), Click { x: i64, y: i64 }, } use crate::WebEvent::*; impl WebEvent { fn run(&self, m: i32) -> String { match self { PageLoad => return format!("Page loaded in {} seconds.", m), PageUnload => format!("PageUnloaded"), KeyPress(c) => format!("KeyPressed: {}", c), Paste(s) => format!("Pasted: {}", s), Click { x, y } => format!("Clicked on position: {}, {}", x, y), } } } fn inspect(event: WebEvent) { match event { PageLoad => println!("PageLoaded"), PageUnload => println!("PageUnloaded"), KeyPress(c) => println!("KeyPressed: {}", c), Paste(s) => println!("Pasted: {}", s), Click { x, y } => println!("Clicked on position: {}, {}", x, y), } } enum Number_enum { Zero, One, Two, } // enum with explicit discriminator enum Color { Red = 0xff0000, Green = 0x00ff00, Blue = 0x0000ff, } fn test_enum() { let pressed = KeyPress('x'); // `to_owned()` creates an owned `String` from a string slice. let pasted = Paste("my text".to_owned()); let click = Click { x: 20, y: 80 }; let load = PageLoad; let unload = PageUnload; inspect(pressed); inspect(pasted); inspect(click); inspect(load); inspect(unload); let loadSec = WebEvent::PageLoad; println!("{}", &loadSec.run(20)); println!("zero is {}", Number_enum::Zero as i32); println!("one is {}", Number_enum::One as i32); println!("roses are #{:06x}", Color::Red as i32); println!("violets are #{:06x}", Color::Blue as i32); } fn test_var_bind() { let mut x = 2; { let x = "4"; } x = 4; } fn casting() { let decimal = 22.8832_f32; let integer = decimal as u8; println!("Integer: {}", integer); let character = integer as char; println!("character: {}", character); println!("1000 as a u16 is: {:b}", 1000 as u16); let num = 1000; println!("1000 as a u8 is : {:b}", num as u8); println!(" -1 as a u8 is : {:b}", (-1i8) as u8); println!("1000 mod 256 is : {:b}", 1000 % 256); // Unless it already fits, of course. println!(" 128 as a i16 is: {:b} ({})", 128 as i16, 128 as i16); // 128 as u8 -> 128, whose two's complement in eight bits is: let num: i16 = 128; println!(" 128 as a i8 is : {:b} ({})", num as i8, num as i8); println!("127={:b}", 127_i8); println!("-128={:b}", -128_i8); println!("255={:b}", 255_u8); println!("0={:b}", 0_u8); println!("255= {}", 127_u8 as i8); println!("0={:b}", 0_u8 as i8); let x = 1u8; let y = 2u32; let z = 3f32; // Unsuffixed literal, their types depend on how they are used let i = 1; let f = 1.0; // `size_of_val` returns the size of a variable in bytes println!("size of `x` in bytes: {}", std::mem::size_of_val(&x)); println!("size of `y` in bytes: {}", std::mem::size_of_val(&y)); println!("size of `z` in bytes: {}", std::mem::size_of_val(&z)); println!("size of `i` in bytes: {}", std::mem::size_of_val(&i)); println!("size of `f` in bytes: {}", std::mem::size_of_val(&f)); let elem = 5u8; // Create an empty vector (a growable array). let mut vec = Vec::new(); // At this point the compiler doesn't know the exact type of `vec`, it // just knows that it's a vector of something (`Vec<_>`). // Insert `elem` in the vector. vec.push(elem); // Aha! Now the compiler knows that `vec` is a vector of `u8`s (`Vec<u8>`) // TODO ^ Try commenting out the `vec.push(elem)` line println!("{:?}", vec); } use std::convert::From; use std::convert::Into; use std::convert::TryFrom; use std::convert::TryInto; #[derive(Debug)] struct Number { value: i32, } impl From<i32> for Number { fn from(item: i32) -> Self { Number { value: item } } } impl fmt::Display for Number { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Number is: {}", self.value) } } #[derive(Debug, PartialEq)] struct EvenNumber(i32); impl TryFrom<i32> for EvenNumber { type Error = (); fn try_from(value: i32) -> Result<Self, Self::Error> { if value % 2 == 0 { Ok(EvenNumber(value)) } else { Err(()) } } } fn conversion() { let s = "Test"; let myString = String::from(s); let b = myString.into_boxed_str(); let ptr = b.as_ptr(); println!("b:{:?}", ptr); let ref_b = b.as_ref(); println!("s:{:?}", ref_b); let number = Number::from(34_i32); println!("{}", number); let n = 5; let num: i32 = n.into(); println!("My number is {:?}", num); assert_eq!(EvenNumber::try_from(8), Ok(EvenNumber(8))); assert_eq!(EvenNumber::try_from(5), Err(())); println!("{:?}", EvenNumber::try_from(5)); let result: Result<EvenNumber, ()> = 8i32.try_into(); assert_eq!(result, Ok(EvenNumber(8))); let result: Result<EvenNumber, ()> = 5i32.try_into(); assert_eq!(result, Err(())); let parsed: i32 = "5".parse().unwrap(); let turbo_parsed = "10".parse::<i32>().unwrap(); let sum = parsed + turbo_parsed; println!("Sum: {:?}", sum); } fn expression() { let x0 = 2; let sum = { let x = 20_i8; let y = 12; x + y + x0 }; println!("Sum: {:?}", sum) } fn flowControl() { let mut count = 0; loop { count += 1; if count % 2 == 0 { continue; } println!("Count: {}", count); if count > 6 { break; } } let mut optional = Some(20); match optional { Some(i) => { println!("Number: {:?}", i); } _ => { println!("Not present"); } } if let Some(i) = Some(20) { println!("Number is indeed: {}", i); } while let Some(i) = optional { if i == 25
else { println!("`i` is `{:?}`. Try again.", i); optional = Some(i + 1); } } } fn isDivisibleBy(lhs: u32, rhs: u32) -> bool { if (rhs == 0) { return false; } lhs % rhs == 0 } impl Point { fn origin() -> Point { Point { x: 0.0, y: 0.0 } } fn new(x: f32, y: f32) -> Point { Point { x: x, y: y } } fn distance(&self, p: &Point) -> f32 { let dx = self.x - p.x; let dy: f32 = self.y - p.y; (dx * dy + dy + dy).sqrt() } fn translate(&mut self, dx: f32, dy: f32) { self.x += dx; self.y += dy; } } fn functions() { println!("isD: {:?}", isDivisibleBy(20, 4)); println!("isD: {:?}", isDivisibleBy(20, 3)); let mut point = Point::new(22.2, 32.3); let mut origin = Point::origin(); println!("Distance: {:?}", point.distance(&origin)); point.translate(3.0, -2.0); println!("Point: {:?}", point); println!("Distance: {:?}", point.distance(&origin)); let x = 20; let fun1 = |i: i32| -> i32 { i + 1 + x }; let fun2 = |i| i + 1 + x; let i = 1; println!("Inc1: {:?}", fun1(i)); println!("Inc2: {:?}", fun2(i)); let one = || 1; println!("One: {:?}", one()); let color = "Green"; let print = || println!("Color: {:?}", color); print(); let _reborow = &color; print(); let mut count = 0; let mut inc = || { count += 1; println!("Count: {:?}", count); }; inc(); inc(); let reborrow = &mut count; let movable = Box::new(3); let consume = || { println!("Movable: {:?}", movable); mem::drop(movable); }; consume(); let haystack = vec![1, 2, 3]; let contains = move |needle| haystack.contains(needle); println!("{}", contains(&1)); println!("{}", contains(&2)); } fn apply<F>(f: F) where F: FnOnce(), { f(); } fn apply_to_3<F>(f: F) -> i32 where F: Fn(i32) -> i32, { f(3) } fn call_me<F: Fn()>(f: F) { f(); } fn function() { println!("I am function"); } fn functions2() { let x = 30; println!("x: {:?}", x); let y = apply_to_3(|x| x + 20); println!("y: {:?}", y); let greeting = "Hello"; let mut farewel = "Goodby".to_owned(); let diary = || { println!("I said {}", greeting); farewel.push_str("!!!!!"); println!("Than I screemed {}", farewel); println!("Than I sleep"); mem::drop(farewel); }; apply(diary); let double = |x| 2 * x; println!("3 doubled: {}", apply_to_3(double)); let closure = || println!("I am closure"); call_me(function); call_me(closure); } fn create_fn() -> impl Fn() { let text = "Fn".to_owned(); move || println!("This is a text: {}", text) } fn create_fn_mut() -> impl FnMut() { let text = "FnMut".to_owned(); move || println!("This is a text: {}", text) } fn create_fn_once() -> impl FnOnce() { let text = "FnOnce".to_owned(); move || println!("This is a: {}", text) } fn functions3() { let x = create_fn(); x(); let mut y = create_fn_mut(); y(); let z = create_fn_once(); z(); let v1 = vec![1, 2, 3]; let v2 = vec![4, 5, 6]; println!("2 in v1: {}", v1.iter().any(|&x| x == 2)); println!("2 in v2: {}", v2.iter().any(|&x| x == 2)); let a1 = [1, 2, 3]; let a2 = [4, 5, 6]; println!("2 in v1: {}", a1.iter().any(|&x| x == 2)); println!("2 in v2: {}", a2.iter().any(|&x| x == 2)); let mut iter1 = v1.iter(); let mut into_iter = v2.into_iter(); println!("Find 2 in v1: {:?}", iter1.find(|&&x| x == 2)); println!("Find 2 in v1: {:?}", into_iter.find(|&x| x == 2)); let array1 = [1, 2, 3]; let array2 = [4, 5, 6]; println!("Find 2 in v1: {:?}", array1.iter().find(|&&x| x == 2)); println!("Find 2 in v1: {:?}", array2.into_iter().find(|&&x| x == 2)); let index_of_first_even_number = array1.iter().position(|x| x % 2 == 0); println!( "index_of_first_even_number: {}", index_of_first_even_number.unwrap() ); } fn is_odd(n: u32) -> bool { n % 2 == 1 } fn foo() -> () { () } fn higherOrder() { let upper = 1000; let mut acc = 0; for n in 0.. { let n_squared = n * n; if n_squared > upper { break; } else if is_odd(n_squared) { acc += n_squared; } } println!("Sum1: {:?}", acc); let sum2 = (0..) .map(|n| n * n) .take_while(|&n_squared| n_squared < upper) .filter(|&n_squared| n_squared % 2 == 1) .fold(0, |acc, n_squared| acc + n_squared); println!("Sum2: {:?}", sum2); fn sum_odd_numbers(up_to: u32) -> u32 { let mut acc = 0; for i in 0..up_to { // Notice that the return type of this match expression must be u32 // because of the type of the "addition" variable. let addition: u32 = match i % 2 == 1 { // The "i" variable is of type u32, which is perfectly fine. true => i, // On the other hand, the "continue" expression does not return // u32, but it is still fine, because it never returns and therefore // does not violate the type requirements of the match expression. false => continue, }; acc += addition; } acc } println!( "Sum of odd numbers up to 9 (excluding): {}", sum_odd_numbers(9) ); } struct S; struct GenericVal<T>(T); impl GenericVal<i32> {} impl GenericVal<S> {} impl<T> GenericVal<T> {} struct Val { val: f64, } struct GenVal<T> { gen_val: T, } // impl of Val impl Val { fn value(&self) -> &f64 { &self.val } } // impl of GenVal for a generic type `T` impl<T> GenVal<T> { fn value(&self) -> &T { &self.gen_val } } fn generics() { let x = Val { val: 3.0 }; let y = GenVal { gen_val: 3i32 }; println!("{}, {}", x.value(), y.value()); } fn create_box() { let _box = Box::new(3i32); } struct ToDrop; impl Drop for ToDrop { fn drop(&mut self) { println!("ToDrop is being dropped"); } } fn destroy_box(c: Box<i32>) { println!("Destroying a box that contains {}", c); // `c` is destroyed and the memory freed } fn scoping() { /* create_box(); let _box2 = Box::new(5i32); { let _box3 = Box::new(4i32); } let x = ToDrop; { let y = ToDrop; }*/ let x = 5u32; let y = x; println!("x is {}, and y is {}", x, y); let a = Box::new(5i32); let mut b = a; *b = 30i32; //destroy_box(b); println!("{}", b); } fn ownership() { let a = Box::new(5i32); let mut b = a; *b = 4; println!("{}", b); } fn eat_box(boxed: Box<i32>) { println!("{}", boxed); } fn borrow(borrowed: &i32) { println!("{}", borrowed); } fn borrowing() { let boxed = Box::new(5_i32); let stacked = 6_i32; borrow(&boxed); borrow(&stacked); { let refTo: &i32 = &boxed; borrow(refTo); } eat_box(boxed); } #[derive(Clone, Copy)] struct Book { author: &'static str, title: &'static str, year: u32, } fn borrow_book(book: &Book) { println!("I immutably borrowed {} - {}", book.author, book.title); } fn new_edition(book: &mut Book) { book.year = 2014; println!("I mutably borrowed {} - {} edition", book.title, book.year); } fn mutability() { let immutable_book = Book { author: "Ivan Cankar", title: "Hlapci", year: 1910, }; let mut mutable_book = immutable_book; borrow_book(&immutable_book); borrow_book(&mutable_book); new_edition(&mut mutable_book); } struct Location { x: i32, y: i32, z: i32, } fn aliasing() { let mut location = Location { x: 0, y: 0, z: 0 }; let borrow1 = &location; let borrow2 = &location; println!("{} {}", location.x, borrow1.x); //let mut_borow = &mut location; println!( "Location has coordinates: ({}, {}, {})", borrow1.x, borrow2.y, location.z ); let mut_borow = &mut location; mut_borow.x = 10; mut_borow.y = 23; mut_borow.z = 29; let borrow3 = &location; } #[derive(PartialEq, PartialOrd)] struct Centimeters(f64); #[derive(Debug)] struct Inches(i32); impl Inches { fn to_centimeters(&self) -> Centimeters { let &Inches(inches) = self; Centimeters(inches as f64 * 2.54) } } struct Seconds(i32); fn deriveTest() { let one_second = Seconds(1); let foot = Inches(12); println!("One foot equals: {:?}", foot); let meter = Centimeters(100.0); let cmp = if foot.to_centimeters() < meter { "smaller" } else { "bigger" }; println!("One foot is {} than one meter.", cmp); } struct Sheep {} struct Cow {} trait Animal { fn noise(&self) -> &'static str; } impl Animal for Sheep { fn noise(&self) -> &'static str { "baaah" } } impl Animal for Cow { fn noise(&self) -> &'static str { "moooooo" } } fn random_animal(random_number: f64) -> Box<dyn Animal> { if random_number < 0.5 { Box::new(Sheep {}) } else { Box::new(Cow {}) } } fn dyReturn() { let random_number = 0.3444; let animal = random_animal(random_number); println!( "You've randomly chosen an animal, and it says {}", animal.noise() ); } use std::ops; struct Foo; struct Bar; #[derive(Debug)] struct FooBar; #[derive(Debug)] struct BarFoo; impl ops::Add<Bar> for Foo { type Output = FooBar; fn add(self, _rhs: Bar) -> FooBar { println!("> Foo.add(Bar) was called"); FooBar } } impl ops::Add<Foo> for Bar { type Output = BarFoo; fn add(self, rhs: Foo) -> BarFoo { println!("> Bar.add(Foo) was called"); BarFoo } } fn operatorOverloading() { println!("Foo + Bar = {:?}", Foo + Bar); println!("Bar + Foo = {:?}", Bar + Foo); } struct Droppable { name: &'static str, } impl Drop for Droppable { fn drop(&mut self) { println!("> Dropping {}", self.name); } } fn dropping() { let a = Droppable { name: "a" }; { let b = Droppable { name: "b" }; { let c = Droppable { name: "c" }; let d = Droppable { name: "d" }; println!("Exiting block B"); } println!("Just exited block B"); println!("Exiting block A"); } } struct Fibonacci { curr: u32, next: u32, } impl Iterator for Fibonacci { type Item = u32; fn next(&mut self) -> Option<u32> { let new_next = self.curr + self.next; self.curr = self.next; self.next = new_next; Some(self.curr) } } fn fibonacci() -> Fibonacci { Fibonacci { curr: 0, next: 1 } } fn iterTest() { let mut sequence = 0..3; println!("Four consecutive `next` calls on 0..3"); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); for i in fibonacci().take(4) { println!("> {}", i); } for i in fibonacci().skip(4).take(4) { println!("> {}", i); } let array = [1u32, 3, 3, 7]; for i in array.iter() { println!("> {}", i); } } fn main() { codewars::cw01::run(); // literals_operator(); //tuple_activity(); //arrays_slices(); // struct_test(); //test_enum(); //test_var_bind(); //casting(); //conversion(); //expression(); //flowControl(); // functions(); //functions2(); //functions3(); //higherOrder(); //generics(); //scoping(); //ownership(); //borrowing(); //mutability(); // aliasing(); //deriveTest(); //dyReturn(); // operatorOverloading(); // dropping(); iterTest(); }
{ println!("Number is: {}", i); optional = None; }
conditional_block
main.rs
use std::collections::HashMap; use std::fmt; use std::mem; use std::str::FromStr; mod codewars; fn literals_operator() { println!("1 + 2 = {}", 1i32 + 2); println!("0011 ^ 0110 = {:04b}", 0b0011u32 ^ 0b0110u32); println!("0011 << 2 = {:04b}", 0b0011u32 << 2); println!("0011 >> 2 = {:04b}", 0b0011u32 >> 2); println!("0o7 << 1 = {:04o}", 0o7u32 << 1); println!("0x7 << 1 = {:04x}", 0x7u32 << 1); } fn reverse(pair: (i32, bool)) -> (bool, i32) { let (x, y) = pair; (y, x) } #[derive(Debug)] struct Matrix(f32, f32, f32, f32); fn transpose(matrix: Matrix) -> Matrix { Matrix(matrix.0, matrix.2, matrix.1, matrix.3) } impl fmt::Display for Matrix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "( {}, {} )\n( {}, {} )", self.0, self.1, self.2, self.3) } } fn tuple_activity() { println!("{:?}", reverse((20i32, false))); println!("one element tuple: {:?}", (5u32,)); println!("just an integer: {:?}", (5u32)); let tuple = (1, "hello", 4.5, true); let (a, b, c, d) = tuple; println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d); let matrix = Matrix(1.1, 1.2, 2.1, 2.2); println!("Matrix:\n{}", matrix); println!("Transpose:\n{}", transpose(matrix)); } fn analyze_slice(slice: &[Matrix]) { println!("first element of the slice: \n{}", slice[0]); println!("the slice has {} elements.", slice.len()); } fn arrays_slices() { let x: [Matrix; 2] = [ Matrix(10.2f32, 2.1f32, 3.1f32, 4.5f32), Matrix(5.2f32, 6.1f32, 2.1f32, 8.5f32), ]; println!("Array occupies: {:?} bytes", mem::size_of_val(&x)); analyze_slice(&x[1..2]); } #[derive(Debug, Copy, Clone)] struct Person<'a> { name: &'a str, age: u8, } impl<'a> Person<'a> { fn changeName(&mut self, new_name: &'a str) -> &'a Person { self.name = new_name; self } } #[derive(Debug)] struct Point { x: f32, y: f32, } #[derive(Debug)] struct Triangle { top_left: Point, bottom_right: Point, } struct Unit; struct Pair(i32, i32); fn square(point: Point, val: f32) -> Triangle { Triangle { bottom_right: Point { x: point.x + val, y: point.y - val, }, top_left: point, } } fn struct_test() { let mut person = Person { name: "Peter", age: 30u8, }; let new_name = "Janez"; let changedPerson = person.changeName(new_name); println!("Person: {:?}", changedPerson); let point = Point { x: 20.2, y: 30.3 }; println!("Point: {:?}", point); let bottom_right = Point { x: 10.2, ..point }; println!("bottom_right: {:?}", bottom_right); let top_left = Point { x: 2.2, y: 5.3 }; let triangle = Triangle { bottom_right: bottom_right, top_left: top_left, }; println!("{:?}", triangle); let Triangle { bottom_right: Point { x: x1, y: y1 }, top_left: Point { x: x2, y: y2 }, } = triangle; println!("x1: {}, y1: {}, x2: {}, y2:{}", x1, y1, x2, y2); println!("Area:{}", (x2 - x1) * (y2 - y1)); let unit = Unit; let pair = Pair(20, 30); println!("pair contains {:?} and {:?}", pair.0, pair.1); let Pair(x, y) = pair; println!("pair contains {:?} and {:?}", x, y); println!("{:?}", square(point, 20.2)); } enum WebEvent { PageLoad, PageUnload, KeyPress(char), Paste(String), Click { x: i64, y: i64 }, } use crate::WebEvent::*; impl WebEvent { fn run(&self, m: i32) -> String { match self { PageLoad => return format!("Page loaded in {} seconds.", m), PageUnload => format!("PageUnloaded"), KeyPress(c) => format!("KeyPressed: {}", c), Paste(s) => format!("Pasted: {}", s), Click { x, y } => format!("Clicked on position: {}, {}", x, y), } } } fn inspect(event: WebEvent) { match event { PageLoad => println!("PageLoaded"), PageUnload => println!("PageUnloaded"), KeyPress(c) => println!("KeyPressed: {}", c), Paste(s) => println!("Pasted: {}", s), Click { x, y } => println!("Clicked on position: {}, {}", x, y), } } enum Number_enum { Zero, One, Two, } // enum with explicit discriminator enum Color { Red = 0xff0000, Green = 0x00ff00, Blue = 0x0000ff, } fn test_enum() { let pressed = KeyPress('x'); // `to_owned()` creates an owned `String` from a string slice. let pasted = Paste("my text".to_owned()); let click = Click { x: 20, y: 80 }; let load = PageLoad; let unload = PageUnload; inspect(pressed); inspect(pasted); inspect(click); inspect(load); inspect(unload); let loadSec = WebEvent::PageLoad; println!("{}", &loadSec.run(20)); println!("zero is {}", Number_enum::Zero as i32); println!("one is {}", Number_enum::One as i32); println!("roses are #{:06x}", Color::Red as i32); println!("violets are #{:06x}", Color::Blue as i32); } fn test_var_bind() { let mut x = 2; { let x = "4"; } x = 4; } fn casting() { let decimal = 22.8832_f32; let integer = decimal as u8; println!("Integer: {}", integer); let character = integer as char; println!("character: {}", character); println!("1000 as a u16 is: {:b}", 1000 as u16); let num = 1000; println!("1000 as a u8 is : {:b}", num as u8); println!(" -1 as a u8 is : {:b}", (-1i8) as u8); println!("1000 mod 256 is : {:b}", 1000 % 256); // Unless it already fits, of course. println!(" 128 as a i16 is: {:b} ({})", 128 as i16, 128 as i16); // 128 as u8 -> 128, whose two's complement in eight bits is: let num: i16 = 128; println!(" 128 as a i8 is : {:b} ({})", num as i8, num as i8); println!("127={:b}", 127_i8); println!("-128={:b}", -128_i8); println!("255={:b}", 255_u8); println!("0={:b}", 0_u8); println!("255= {}", 127_u8 as i8); println!("0={:b}", 0_u8 as i8); let x = 1u8; let y = 2u32; let z = 3f32; // Unsuffixed literal, their types depend on how they are used let i = 1; let f = 1.0; // `size_of_val` returns the size of a variable in bytes println!("size of `x` in bytes: {}", std::mem::size_of_val(&x)); println!("size of `y` in bytes: {}", std::mem::size_of_val(&y)); println!("size of `z` in bytes: {}", std::mem::size_of_val(&z)); println!("size of `i` in bytes: {}", std::mem::size_of_val(&i)); println!("size of `f` in bytes: {}", std::mem::size_of_val(&f)); let elem = 5u8; // Create an empty vector (a growable array). let mut vec = Vec::new(); // At this point the compiler doesn't know the exact type of `vec`, it // just knows that it's a vector of something (`Vec<_>`). // Insert `elem` in the vector. vec.push(elem); // Aha! Now the compiler knows that `vec` is a vector of `u8`s (`Vec<u8>`) // TODO ^ Try commenting out the `vec.push(elem)` line println!("{:?}", vec); } use std::convert::From; use std::convert::Into; use std::convert::TryFrom; use std::convert::TryInto; #[derive(Debug)] struct Number { value: i32, } impl From<i32> for Number { fn from(item: i32) -> Self { Number { value: item } } } impl fmt::Display for Number { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Number is: {}", self.value) } } #[derive(Debug, PartialEq)] struct EvenNumber(i32); impl TryFrom<i32> for EvenNumber { type Error = (); fn try_from(value: i32) -> Result<Self, Self::Error> { if value % 2 == 0 { Ok(EvenNumber(value)) } else { Err(()) } } } fn conversion() { let s = "Test"; let myString = String::from(s); let b = myString.into_boxed_str(); let ptr = b.as_ptr(); println!("b:{:?}", ptr); let ref_b = b.as_ref(); println!("s:{:?}", ref_b); let number = Number::from(34_i32); println!("{}", number); let n = 5; let num: i32 = n.into(); println!("My number is {:?}", num); assert_eq!(EvenNumber::try_from(8), Ok(EvenNumber(8))); assert_eq!(EvenNumber::try_from(5), Err(())); println!("{:?}", EvenNumber::try_from(5)); let result: Result<EvenNumber, ()> = 8i32.try_into(); assert_eq!(result, Ok(EvenNumber(8))); let result: Result<EvenNumber, ()> = 5i32.try_into(); assert_eq!(result, Err(())); let parsed: i32 = "5".parse().unwrap(); let turbo_parsed = "10".parse::<i32>().unwrap(); let sum = parsed + turbo_parsed; println!("Sum: {:?}", sum); } fn expression() { let x0 = 2; let sum = { let x = 20_i8; let y = 12; x + y + x0 }; println!("Sum: {:?}", sum) } fn flowControl() { let mut count = 0; loop { count += 1; if count % 2 == 0 { continue; } println!("Count: {}", count); if count > 6 { break; } } let mut optional = Some(20); match optional { Some(i) => { println!("Number: {:?}", i); } _ => { println!("Not present"); } } if let Some(i) = Some(20) { println!("Number is indeed: {}", i); } while let Some(i) = optional { if i == 25 { println!("Number is: {}", i); optional = None; } else { println!("`i` is `{:?}`. Try again.", i); optional = Some(i + 1); } } } fn isDivisibleBy(lhs: u32, rhs: u32) -> bool { if (rhs == 0) { return false; } lhs % rhs == 0 } impl Point { fn origin() -> Point { Point { x: 0.0, y: 0.0 } } fn new(x: f32, y: f32) -> Point { Point { x: x, y: y } } fn distance(&self, p: &Point) -> f32 { let dx = self.x - p.x; let dy: f32 = self.y - p.y; (dx * dy + dy + dy).sqrt() } fn translate(&mut self, dx: f32, dy: f32) { self.x += dx; self.y += dy; } } fn functions() { println!("isD: {:?}", isDivisibleBy(20, 4)); println!("isD: {:?}", isDivisibleBy(20, 3)); let mut point = Point::new(22.2, 32.3); let mut origin = Point::origin(); println!("Distance: {:?}", point.distance(&origin)); point.translate(3.0, -2.0); println!("Point: {:?}", point); println!("Distance: {:?}", point.distance(&origin)); let x = 20; let fun1 = |i: i32| -> i32 { i + 1 + x }; let fun2 = |i| i + 1 + x; let i = 1; println!("Inc1: {:?}", fun1(i)); println!("Inc2: {:?}", fun2(i)); let one = || 1; println!("One: {:?}", one()); let color = "Green"; let print = || println!("Color: {:?}", color); print(); let _reborow = &color; print(); let mut count = 0; let mut inc = || { count += 1; println!("Count: {:?}", count); }; inc(); inc(); let reborrow = &mut count; let movable = Box::new(3); let consume = || { println!("Movable: {:?}", movable); mem::drop(movable); }; consume(); let haystack = vec![1, 2, 3]; let contains = move |needle| haystack.contains(needle); println!("{}", contains(&1)); println!("{}", contains(&2)); } fn apply<F>(f: F) where F: FnOnce(), { f(); } fn apply_to_3<F>(f: F) -> i32 where F: Fn(i32) -> i32, { f(3) } fn call_me<F: Fn()>(f: F) { f(); } fn function() { println!("I am function"); } fn functions2() { let x = 30; println!("x: {:?}", x); let y = apply_to_3(|x| x + 20); println!("y: {:?}", y); let greeting = "Hello"; let mut farewel = "Goodby".to_owned(); let diary = || { println!("I said {}", greeting); farewel.push_str("!!!!!"); println!("Than I screemed {}", farewel); println!("Than I sleep"); mem::drop(farewel); }; apply(diary); let double = |x| 2 * x; println!("3 doubled: {}", apply_to_3(double)); let closure = || println!("I am closure"); call_me(function); call_me(closure); } fn create_fn() -> impl Fn() { let text = "Fn".to_owned(); move || println!("This is a text: {}", text) } fn create_fn_mut() -> impl FnMut() { let text = "FnMut".to_owned(); move || println!("This is a text: {}", text) } fn create_fn_once() -> impl FnOnce() { let text = "FnOnce".to_owned(); move || println!("This is a: {}", text) } fn functions3() { let x = create_fn(); x(); let mut y = create_fn_mut(); y(); let z = create_fn_once(); z(); let v1 = vec![1, 2, 3]; let v2 = vec![4, 5, 6]; println!("2 in v1: {}", v1.iter().any(|&x| x == 2)); println!("2 in v2: {}", v2.iter().any(|&x| x == 2)); let a1 = [1, 2, 3]; let a2 = [4, 5, 6];
println!("2 in v1: {}", a1.iter().any(|&x| x == 2)); println!("2 in v2: {}", a2.iter().any(|&x| x == 2)); let mut iter1 = v1.iter(); let mut into_iter = v2.into_iter(); println!("Find 2 in v1: {:?}", iter1.find(|&&x| x == 2)); println!("Find 2 in v1: {:?}", into_iter.find(|&x| x == 2)); let array1 = [1, 2, 3]; let array2 = [4, 5, 6]; println!("Find 2 in v1: {:?}", array1.iter().find(|&&x| x == 2)); println!("Find 2 in v1: {:?}", array2.into_iter().find(|&&x| x == 2)); let index_of_first_even_number = array1.iter().position(|x| x % 2 == 0); println!( "index_of_first_even_number: {}", index_of_first_even_number.unwrap() ); } fn is_odd(n: u32) -> bool { n % 2 == 1 } fn foo() -> () { () } fn higherOrder() { let upper = 1000; let mut acc = 0; for n in 0.. { let n_squared = n * n; if n_squared > upper { break; } else if is_odd(n_squared) { acc += n_squared; } } println!("Sum1: {:?}", acc); let sum2 = (0..) .map(|n| n * n) .take_while(|&n_squared| n_squared < upper) .filter(|&n_squared| n_squared % 2 == 1) .fold(0, |acc, n_squared| acc + n_squared); println!("Sum2: {:?}", sum2); fn sum_odd_numbers(up_to: u32) -> u32 { let mut acc = 0; for i in 0..up_to { // Notice that the return type of this match expression must be u32 // because of the type of the "addition" variable. let addition: u32 = match i % 2 == 1 { // The "i" variable is of type u32, which is perfectly fine. true => i, // On the other hand, the "continue" expression does not return // u32, but it is still fine, because it never returns and therefore // does not violate the type requirements of the match expression. false => continue, }; acc += addition; } acc } println!( "Sum of odd numbers up to 9 (excluding): {}", sum_odd_numbers(9) ); } struct S; struct GenericVal<T>(T); impl GenericVal<i32> {} impl GenericVal<S> {} impl<T> GenericVal<T> {} struct Val { val: f64, } struct GenVal<T> { gen_val: T, } // impl of Val impl Val { fn value(&self) -> &f64 { &self.val } } // impl of GenVal for a generic type `T` impl<T> GenVal<T> { fn value(&self) -> &T { &self.gen_val } } fn generics() { let x = Val { val: 3.0 }; let y = GenVal { gen_val: 3i32 }; println!("{}, {}", x.value(), y.value()); } fn create_box() { let _box = Box::new(3i32); } struct ToDrop; impl Drop for ToDrop { fn drop(&mut self) { println!("ToDrop is being dropped"); } } fn destroy_box(c: Box<i32>) { println!("Destroying a box that contains {}", c); // `c` is destroyed and the memory freed } fn scoping() { /* create_box(); let _box2 = Box::new(5i32); { let _box3 = Box::new(4i32); } let x = ToDrop; { let y = ToDrop; }*/ let x = 5u32; let y = x; println!("x is {}, and y is {}", x, y); let a = Box::new(5i32); let mut b = a; *b = 30i32; //destroy_box(b); println!("{}", b); } fn ownership() { let a = Box::new(5i32); let mut b = a; *b = 4; println!("{}", b); } fn eat_box(boxed: Box<i32>) { println!("{}", boxed); } fn borrow(borrowed: &i32) { println!("{}", borrowed); } fn borrowing() { let boxed = Box::new(5_i32); let stacked = 6_i32; borrow(&boxed); borrow(&stacked); { let refTo: &i32 = &boxed; borrow(refTo); } eat_box(boxed); } #[derive(Clone, Copy)] struct Book { author: &'static str, title: &'static str, year: u32, } fn borrow_book(book: &Book) { println!("I immutably borrowed {} - {}", book.author, book.title); } fn new_edition(book: &mut Book) { book.year = 2014; println!("I mutably borrowed {} - {} edition", book.title, book.year); } fn mutability() { let immutable_book = Book { author: "Ivan Cankar", title: "Hlapci", year: 1910, }; let mut mutable_book = immutable_book; borrow_book(&immutable_book); borrow_book(&mutable_book); new_edition(&mut mutable_book); } struct Location { x: i32, y: i32, z: i32, } fn aliasing() { let mut location = Location { x: 0, y: 0, z: 0 }; let borrow1 = &location; let borrow2 = &location; println!("{} {}", location.x, borrow1.x); //let mut_borow = &mut location; println!( "Location has coordinates: ({}, {}, {})", borrow1.x, borrow2.y, location.z ); let mut_borow = &mut location; mut_borow.x = 10; mut_borow.y = 23; mut_borow.z = 29; let borrow3 = &location; } #[derive(PartialEq, PartialOrd)] struct Centimeters(f64); #[derive(Debug)] struct Inches(i32); impl Inches { fn to_centimeters(&self) -> Centimeters { let &Inches(inches) = self; Centimeters(inches as f64 * 2.54) } } struct Seconds(i32); fn deriveTest() { let one_second = Seconds(1); let foot = Inches(12); println!("One foot equals: {:?}", foot); let meter = Centimeters(100.0); let cmp = if foot.to_centimeters() < meter { "smaller" } else { "bigger" }; println!("One foot is {} than one meter.", cmp); } struct Sheep {} struct Cow {} trait Animal { fn noise(&self) -> &'static str; } impl Animal for Sheep { fn noise(&self) -> &'static str { "baaah" } } impl Animal for Cow { fn noise(&self) -> &'static str { "moooooo" } } fn random_animal(random_number: f64) -> Box<dyn Animal> { if random_number < 0.5 { Box::new(Sheep {}) } else { Box::new(Cow {}) } } fn dyReturn() { let random_number = 0.3444; let animal = random_animal(random_number); println!( "You've randomly chosen an animal, and it says {}", animal.noise() ); } use std::ops; struct Foo; struct Bar; #[derive(Debug)] struct FooBar; #[derive(Debug)] struct BarFoo; impl ops::Add<Bar> for Foo { type Output = FooBar; fn add(self, _rhs: Bar) -> FooBar { println!("> Foo.add(Bar) was called"); FooBar } } impl ops::Add<Foo> for Bar { type Output = BarFoo; fn add(self, rhs: Foo) -> BarFoo { println!("> Bar.add(Foo) was called"); BarFoo } } fn operatorOverloading() { println!("Foo + Bar = {:?}", Foo + Bar); println!("Bar + Foo = {:?}", Bar + Foo); } struct Droppable { name: &'static str, } impl Drop for Droppable { fn drop(&mut self) { println!("> Dropping {}", self.name); } } fn dropping() { let a = Droppable { name: "a" }; { let b = Droppable { name: "b" }; { let c = Droppable { name: "c" }; let d = Droppable { name: "d" }; println!("Exiting block B"); } println!("Just exited block B"); println!("Exiting block A"); } } struct Fibonacci { curr: u32, next: u32, } impl Iterator for Fibonacci { type Item = u32; fn next(&mut self) -> Option<u32> { let new_next = self.curr + self.next; self.curr = self.next; self.next = new_next; Some(self.curr) } } fn fibonacci() -> Fibonacci { Fibonacci { curr: 0, next: 1 } } fn iterTest() { let mut sequence = 0..3; println!("Four consecutive `next` calls on 0..3"); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); for i in fibonacci().take(4) { println!("> {}", i); } for i in fibonacci().skip(4).take(4) { println!("> {}", i); } let array = [1u32, 3, 3, 7]; for i in array.iter() { println!("> {}", i); } } fn main() { codewars::cw01::run(); // literals_operator(); //tuple_activity(); //arrays_slices(); // struct_test(); //test_enum(); //test_var_bind(); //casting(); //conversion(); //expression(); //flowControl(); // functions(); //functions2(); //functions3(); //higherOrder(); //generics(); //scoping(); //ownership(); //borrowing(); //mutability(); // aliasing(); //deriveTest(); //dyReturn(); // operatorOverloading(); // dropping(); iterTest(); }
random_line_split
main.rs
use std::collections::HashMap; use std::fmt; use std::mem; use std::str::FromStr; mod codewars; fn literals_operator() { println!("1 + 2 = {}", 1i32 + 2); println!("0011 ^ 0110 = {:04b}", 0b0011u32 ^ 0b0110u32); println!("0011 << 2 = {:04b}", 0b0011u32 << 2); println!("0011 >> 2 = {:04b}", 0b0011u32 >> 2); println!("0o7 << 1 = {:04o}", 0o7u32 << 1); println!("0x7 << 1 = {:04x}", 0x7u32 << 1); } fn reverse(pair: (i32, bool)) -> (bool, i32) { let (x, y) = pair; (y, x) } #[derive(Debug)] struct Matrix(f32, f32, f32, f32); fn transpose(matrix: Matrix) -> Matrix { Matrix(matrix.0, matrix.2, matrix.1, matrix.3) } impl fmt::Display for Matrix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "( {}, {} )\n( {}, {} )", self.0, self.1, self.2, self.3) } } fn tuple_activity() { println!("{:?}", reverse((20i32, false))); println!("one element tuple: {:?}", (5u32,)); println!("just an integer: {:?}", (5u32)); let tuple = (1, "hello", 4.5, true); let (a, b, c, d) = tuple; println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d); let matrix = Matrix(1.1, 1.2, 2.1, 2.2); println!("Matrix:\n{}", matrix); println!("Transpose:\n{}", transpose(matrix)); } fn analyze_slice(slice: &[Matrix]) { println!("first element of the slice: \n{}", slice[0]); println!("the slice has {} elements.", slice.len()); } fn arrays_slices() { let x: [Matrix; 2] = [ Matrix(10.2f32, 2.1f32, 3.1f32, 4.5f32), Matrix(5.2f32, 6.1f32, 2.1f32, 8.5f32), ]; println!("Array occupies: {:?} bytes", mem::size_of_val(&x)); analyze_slice(&x[1..2]); } #[derive(Debug, Copy, Clone)] struct Person<'a> { name: &'a str, age: u8, } impl<'a> Person<'a> { fn changeName(&mut self, new_name: &'a str) -> &'a Person { self.name = new_name; self } } #[derive(Debug)] struct Point { x: f32, y: f32, } #[derive(Debug)] struct Triangle { top_left: Point, bottom_right: Point, } struct Unit; struct Pair(i32, i32); fn square(point: Point, val: f32) -> Triangle { Triangle { bottom_right: Point { x: point.x + val, y: point.y - val, }, top_left: point, } } fn struct_test() { let mut person = Person { name: "Peter", age: 30u8, }; let new_name = "Janez"; let changedPerson = person.changeName(new_name); println!("Person: {:?}", changedPerson); let point = Point { x: 20.2, y: 30.3 }; println!("Point: {:?}", point); let bottom_right = Point { x: 10.2, ..point }; println!("bottom_right: {:?}", bottom_right); let top_left = Point { x: 2.2, y: 5.3 }; let triangle = Triangle { bottom_right: bottom_right, top_left: top_left, }; println!("{:?}", triangle); let Triangle { bottom_right: Point { x: x1, y: y1 }, top_left: Point { x: x2, y: y2 }, } = triangle; println!("x1: {}, y1: {}, x2: {}, y2:{}", x1, y1, x2, y2); println!("Area:{}", (x2 - x1) * (y2 - y1)); let unit = Unit; let pair = Pair(20, 30); println!("pair contains {:?} and {:?}", pair.0, pair.1); let Pair(x, y) = pair; println!("pair contains {:?} and {:?}", x, y); println!("{:?}", square(point, 20.2)); } enum WebEvent { PageLoad, PageUnload, KeyPress(char), Paste(String), Click { x: i64, y: i64 }, } use crate::WebEvent::*; impl WebEvent { fn run(&self, m: i32) -> String { match self { PageLoad => return format!("Page loaded in {} seconds.", m), PageUnload => format!("PageUnloaded"), KeyPress(c) => format!("KeyPressed: {}", c), Paste(s) => format!("Pasted: {}", s), Click { x, y } => format!("Clicked on position: {}, {}", x, y), } } } fn inspect(event: WebEvent) { match event { PageLoad => println!("PageLoaded"), PageUnload => println!("PageUnloaded"), KeyPress(c) => println!("KeyPressed: {}", c), Paste(s) => println!("Pasted: {}", s), Click { x, y } => println!("Clicked on position: {}, {}", x, y), } } enum Number_enum { Zero, One, Two, } // enum with explicit discriminator enum Color { Red = 0xff0000, Green = 0x00ff00, Blue = 0x0000ff, } fn test_enum() { let pressed = KeyPress('x'); // `to_owned()` creates an owned `String` from a string slice. let pasted = Paste("my text".to_owned()); let click = Click { x: 20, y: 80 }; let load = PageLoad; let unload = PageUnload; inspect(pressed); inspect(pasted); inspect(click); inspect(load); inspect(unload); let loadSec = WebEvent::PageLoad; println!("{}", &loadSec.run(20)); println!("zero is {}", Number_enum::Zero as i32); println!("one is {}", Number_enum::One as i32); println!("roses are #{:06x}", Color::Red as i32); println!("violets are #{:06x}", Color::Blue as i32); } fn test_var_bind() { let mut x = 2; { let x = "4"; } x = 4; } fn casting() { let decimal = 22.8832_f32; let integer = decimal as u8; println!("Integer: {}", integer); let character = integer as char; println!("character: {}", character); println!("1000 as a u16 is: {:b}", 1000 as u16); let num = 1000; println!("1000 as a u8 is : {:b}", num as u8); println!(" -1 as a u8 is : {:b}", (-1i8) as u8); println!("1000 mod 256 is : {:b}", 1000 % 256); // Unless it already fits, of course. println!(" 128 as a i16 is: {:b} ({})", 128 as i16, 128 as i16); // 128 as u8 -> 128, whose two's complement in eight bits is: let num: i16 = 128; println!(" 128 as a i8 is : {:b} ({})", num as i8, num as i8); println!("127={:b}", 127_i8); println!("-128={:b}", -128_i8); println!("255={:b}", 255_u8); println!("0={:b}", 0_u8); println!("255= {}", 127_u8 as i8); println!("0={:b}", 0_u8 as i8); let x = 1u8; let y = 2u32; let z = 3f32; // Unsuffixed literal, their types depend on how they are used let i = 1; let f = 1.0; // `size_of_val` returns the size of a variable in bytes println!("size of `x` in bytes: {}", std::mem::size_of_val(&x)); println!("size of `y` in bytes: {}", std::mem::size_of_val(&y)); println!("size of `z` in bytes: {}", std::mem::size_of_val(&z)); println!("size of `i` in bytes: {}", std::mem::size_of_val(&i)); println!("size of `f` in bytes: {}", std::mem::size_of_val(&f)); let elem = 5u8; // Create an empty vector (a growable array). let mut vec = Vec::new(); // At this point the compiler doesn't know the exact type of `vec`, it // just knows that it's a vector of something (`Vec<_>`). // Insert `elem` in the vector. vec.push(elem); // Aha! Now the compiler knows that `vec` is a vector of `u8`s (`Vec<u8>`) // TODO ^ Try commenting out the `vec.push(elem)` line println!("{:?}", vec); } use std::convert::From; use std::convert::Into; use std::convert::TryFrom; use std::convert::TryInto; #[derive(Debug)] struct Number { value: i32, } impl From<i32> for Number { fn from(item: i32) -> Self { Number { value: item } } } impl fmt::Display for Number { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Number is: {}", self.value) } } #[derive(Debug, PartialEq)] struct EvenNumber(i32); impl TryFrom<i32> for EvenNumber { type Error = (); fn try_from(value: i32) -> Result<Self, Self::Error> { if value % 2 == 0 { Ok(EvenNumber(value)) } else { Err(()) } } } fn conversion() { let s = "Test"; let myString = String::from(s); let b = myString.into_boxed_str(); let ptr = b.as_ptr(); println!("b:{:?}", ptr); let ref_b = b.as_ref(); println!("s:{:?}", ref_b); let number = Number::from(34_i32); println!("{}", number); let n = 5; let num: i32 = n.into(); println!("My number is {:?}", num); assert_eq!(EvenNumber::try_from(8), Ok(EvenNumber(8))); assert_eq!(EvenNumber::try_from(5), Err(())); println!("{:?}", EvenNumber::try_from(5)); let result: Result<EvenNumber, ()> = 8i32.try_into(); assert_eq!(result, Ok(EvenNumber(8))); let result: Result<EvenNumber, ()> = 5i32.try_into(); assert_eq!(result, Err(())); let parsed: i32 = "5".parse().unwrap(); let turbo_parsed = "10".parse::<i32>().unwrap(); let sum = parsed + turbo_parsed; println!("Sum: {:?}", sum); } fn expression() { let x0 = 2; let sum = { let x = 20_i8; let y = 12; x + y + x0 }; println!("Sum: {:?}", sum) } fn flowControl() { let mut count = 0; loop { count += 1; if count % 2 == 0 { continue; } println!("Count: {}", count); if count > 6 { break; } } let mut optional = Some(20); match optional { Some(i) => { println!("Number: {:?}", i); } _ => { println!("Not present"); } } if let Some(i) = Some(20) { println!("Number is indeed: {}", i); } while let Some(i) = optional { if i == 25 { println!("Number is: {}", i); optional = None; } else { println!("`i` is `{:?}`. Try again.", i); optional = Some(i + 1); } } } fn isDivisibleBy(lhs: u32, rhs: u32) -> bool { if (rhs == 0) { return false; } lhs % rhs == 0 } impl Point { fn origin() -> Point { Point { x: 0.0, y: 0.0 } } fn new(x: f32, y: f32) -> Point { Point { x: x, y: y } } fn distance(&self, p: &Point) -> f32 { let dx = self.x - p.x; let dy: f32 = self.y - p.y; (dx * dy + dy + dy).sqrt() } fn translate(&mut self, dx: f32, dy: f32) { self.x += dx; self.y += dy; } } fn functions() { println!("isD: {:?}", isDivisibleBy(20, 4)); println!("isD: {:?}", isDivisibleBy(20, 3)); let mut point = Point::new(22.2, 32.3); let mut origin = Point::origin(); println!("Distance: {:?}", point.distance(&origin)); point.translate(3.0, -2.0); println!("Point: {:?}", point); println!("Distance: {:?}", point.distance(&origin)); let x = 20; let fun1 = |i: i32| -> i32 { i + 1 + x }; let fun2 = |i| i + 1 + x; let i = 1; println!("Inc1: {:?}", fun1(i)); println!("Inc2: {:?}", fun2(i)); let one = || 1; println!("One: {:?}", one()); let color = "Green"; let print = || println!("Color: {:?}", color); print(); let _reborow = &color; print(); let mut count = 0; let mut inc = || { count += 1; println!("Count: {:?}", count); }; inc(); inc(); let reborrow = &mut count; let movable = Box::new(3); let consume = || { println!("Movable: {:?}", movable); mem::drop(movable); }; consume(); let haystack = vec![1, 2, 3]; let contains = move |needle| haystack.contains(needle); println!("{}", contains(&1)); println!("{}", contains(&2)); } fn apply<F>(f: F) where F: FnOnce(), { f(); } fn apply_to_3<F>(f: F) -> i32 where F: Fn(i32) -> i32, { f(3) } fn call_me<F: Fn()>(f: F) { f(); } fn function() { println!("I am function"); } fn functions2() { let x = 30; println!("x: {:?}", x); let y = apply_to_3(|x| x + 20); println!("y: {:?}", y); let greeting = "Hello"; let mut farewel = "Goodby".to_owned(); let diary = || { println!("I said {}", greeting); farewel.push_str("!!!!!"); println!("Than I screemed {}", farewel); println!("Than I sleep"); mem::drop(farewel); }; apply(diary); let double = |x| 2 * x; println!("3 doubled: {}", apply_to_3(double)); let closure = || println!("I am closure"); call_me(function); call_me(closure); } fn create_fn() -> impl Fn() { let text = "Fn".to_owned(); move || println!("This is a text: {}", text) } fn create_fn_mut() -> impl FnMut() { let text = "FnMut".to_owned(); move || println!("This is a text: {}", text) } fn create_fn_once() -> impl FnOnce() { let text = "FnOnce".to_owned(); move || println!("This is a: {}", text) } fn functions3() { let x = create_fn(); x(); let mut y = create_fn_mut(); y(); let z = create_fn_once(); z(); let v1 = vec![1, 2, 3]; let v2 = vec![4, 5, 6]; println!("2 in v1: {}", v1.iter().any(|&x| x == 2)); println!("2 in v2: {}", v2.iter().any(|&x| x == 2)); let a1 = [1, 2, 3]; let a2 = [4, 5, 6]; println!("2 in v1: {}", a1.iter().any(|&x| x == 2)); println!("2 in v2: {}", a2.iter().any(|&x| x == 2)); let mut iter1 = v1.iter(); let mut into_iter = v2.into_iter(); println!("Find 2 in v1: {:?}", iter1.find(|&&x| x == 2)); println!("Find 2 in v1: {:?}", into_iter.find(|&x| x == 2)); let array1 = [1, 2, 3]; let array2 = [4, 5, 6]; println!("Find 2 in v1: {:?}", array1.iter().find(|&&x| x == 2)); println!("Find 2 in v1: {:?}", array2.into_iter().find(|&&x| x == 2)); let index_of_first_even_number = array1.iter().position(|x| x % 2 == 0); println!( "index_of_first_even_number: {}", index_of_first_even_number.unwrap() ); } fn is_odd(n: u32) -> bool { n % 2 == 1 } fn foo() -> () { () } fn higherOrder() { let upper = 1000; let mut acc = 0; for n in 0.. { let n_squared = n * n; if n_squared > upper { break; } else if is_odd(n_squared) { acc += n_squared; } } println!("Sum1: {:?}", acc); let sum2 = (0..) .map(|n| n * n) .take_while(|&n_squared| n_squared < upper) .filter(|&n_squared| n_squared % 2 == 1) .fold(0, |acc, n_squared| acc + n_squared); println!("Sum2: {:?}", sum2); fn sum_odd_numbers(up_to: u32) -> u32 { let mut acc = 0; for i in 0..up_to { // Notice that the return type of this match expression must be u32 // because of the type of the "addition" variable. let addition: u32 = match i % 2 == 1 { // The "i" variable is of type u32, which is perfectly fine. true => i, // On the other hand, the "continue" expression does not return // u32, but it is still fine, because it never returns and therefore // does not violate the type requirements of the match expression. false => continue, }; acc += addition; } acc } println!( "Sum of odd numbers up to 9 (excluding): {}", sum_odd_numbers(9) ); } struct S; struct GenericVal<T>(T); impl GenericVal<i32> {} impl GenericVal<S> {} impl<T> GenericVal<T> {} struct Val { val: f64, } struct GenVal<T> { gen_val: T, } // impl of Val impl Val { fn value(&self) -> &f64 { &self.val } } // impl of GenVal for a generic type `T` impl<T> GenVal<T> { fn value(&self) -> &T { &self.gen_val } } fn generics() { let x = Val { val: 3.0 }; let y = GenVal { gen_val: 3i32 }; println!("{}, {}", x.value(), y.value()); } fn create_box() { let _box = Box::new(3i32); } struct ToDrop; impl Drop for ToDrop { fn drop(&mut self) { println!("ToDrop is being dropped"); } } fn destroy_box(c: Box<i32>) { println!("Destroying a box that contains {}", c); // `c` is destroyed and the memory freed } fn scoping() { /* create_box(); let _box2 = Box::new(5i32); { let _box3 = Box::new(4i32); } let x = ToDrop; { let y = ToDrop; }*/ let x = 5u32; let y = x; println!("x is {}, and y is {}", x, y); let a = Box::new(5i32); let mut b = a; *b = 30i32; //destroy_box(b); println!("{}", b); } fn ownership() { let a = Box::new(5i32); let mut b = a; *b = 4; println!("{}", b); } fn eat_box(boxed: Box<i32>) { println!("{}", boxed); } fn borrow(borrowed: &i32) { println!("{}", borrowed); } fn borrowing() { let boxed = Box::new(5_i32); let stacked = 6_i32; borrow(&boxed); borrow(&stacked); { let refTo: &i32 = &boxed; borrow(refTo); } eat_box(boxed); } #[derive(Clone, Copy)] struct Book { author: &'static str, title: &'static str, year: u32, } fn borrow_book(book: &Book) { println!("I immutably borrowed {} - {}", book.author, book.title); } fn new_edition(book: &mut Book) { book.year = 2014; println!("I mutably borrowed {} - {} edition", book.title, book.year); } fn mutability() { let immutable_book = Book { author: "Ivan Cankar", title: "Hlapci", year: 1910, }; let mut mutable_book = immutable_book; borrow_book(&immutable_book); borrow_book(&mutable_book); new_edition(&mut mutable_book); } struct Location { x: i32, y: i32, z: i32, } fn aliasing() { let mut location = Location { x: 0, y: 0, z: 0 }; let borrow1 = &location; let borrow2 = &location; println!("{} {}", location.x, borrow1.x); //let mut_borow = &mut location; println!( "Location has coordinates: ({}, {}, {})", borrow1.x, borrow2.y, location.z ); let mut_borow = &mut location; mut_borow.x = 10; mut_borow.y = 23; mut_borow.z = 29; let borrow3 = &location; } #[derive(PartialEq, PartialOrd)] struct Centimeters(f64); #[derive(Debug)] struct Inches(i32); impl Inches { fn to_centimeters(&self) -> Centimeters { let &Inches(inches) = self; Centimeters(inches as f64 * 2.54) } } struct Seconds(i32); fn deriveTest() { let one_second = Seconds(1); let foot = Inches(12); println!("One foot equals: {:?}", foot); let meter = Centimeters(100.0); let cmp = if foot.to_centimeters() < meter { "smaller" } else { "bigger" }; println!("One foot is {} than one meter.", cmp); } struct Sheep {} struct Cow {} trait Animal { fn noise(&self) -> &'static str; } impl Animal for Sheep { fn noise(&self) -> &'static str { "baaah" } } impl Animal for Cow { fn noise(&self) -> &'static str { "moooooo" } } fn random_animal(random_number: f64) -> Box<dyn Animal> { if random_number < 0.5 { Box::new(Sheep {}) } else { Box::new(Cow {}) } } fn
() { let random_number = 0.3444; let animal = random_animal(random_number); println!( "You've randomly chosen an animal, and it says {}", animal.noise() ); } use std::ops; struct Foo; struct Bar; #[derive(Debug)] struct FooBar; #[derive(Debug)] struct BarFoo; impl ops::Add<Bar> for Foo { type Output = FooBar; fn add(self, _rhs: Bar) -> FooBar { println!("> Foo.add(Bar) was called"); FooBar } } impl ops::Add<Foo> for Bar { type Output = BarFoo; fn add(self, rhs: Foo) -> BarFoo { println!("> Bar.add(Foo) was called"); BarFoo } } fn operatorOverloading() { println!("Foo + Bar = {:?}", Foo + Bar); println!("Bar + Foo = {:?}", Bar + Foo); } struct Droppable { name: &'static str, } impl Drop for Droppable { fn drop(&mut self) { println!("> Dropping {}", self.name); } } fn dropping() { let a = Droppable { name: "a" }; { let b = Droppable { name: "b" }; { let c = Droppable { name: "c" }; let d = Droppable { name: "d" }; println!("Exiting block B"); } println!("Just exited block B"); println!("Exiting block A"); } } struct Fibonacci { curr: u32, next: u32, } impl Iterator for Fibonacci { type Item = u32; fn next(&mut self) -> Option<u32> { let new_next = self.curr + self.next; self.curr = self.next; self.next = new_next; Some(self.curr) } } fn fibonacci() -> Fibonacci { Fibonacci { curr: 0, next: 1 } } fn iterTest() { let mut sequence = 0..3; println!("Four consecutive `next` calls on 0..3"); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); for i in fibonacci().take(4) { println!("> {}", i); } for i in fibonacci().skip(4).take(4) { println!("> {}", i); } let array = [1u32, 3, 3, 7]; for i in array.iter() { println!("> {}", i); } } fn main() { codewars::cw01::run(); // literals_operator(); //tuple_activity(); //arrays_slices(); // struct_test(); //test_enum(); //test_var_bind(); //casting(); //conversion(); //expression(); //flowControl(); // functions(); //functions2(); //functions3(); //higherOrder(); //generics(); //scoping(); //ownership(); //borrowing(); //mutability(); // aliasing(); //deriveTest(); //dyReturn(); // operatorOverloading(); // dropping(); iterTest(); }
dyReturn
identifier_name
main.rs
use std::collections::HashMap; use std::fmt; use std::mem; use std::str::FromStr; mod codewars; fn literals_operator() { println!("1 + 2 = {}", 1i32 + 2); println!("0011 ^ 0110 = {:04b}", 0b0011u32 ^ 0b0110u32); println!("0011 << 2 = {:04b}", 0b0011u32 << 2); println!("0011 >> 2 = {:04b}", 0b0011u32 >> 2); println!("0o7 << 1 = {:04o}", 0o7u32 << 1); println!("0x7 << 1 = {:04x}", 0x7u32 << 1); } fn reverse(pair: (i32, bool)) -> (bool, i32) { let (x, y) = pair; (y, x) } #[derive(Debug)] struct Matrix(f32, f32, f32, f32); fn transpose(matrix: Matrix) -> Matrix { Matrix(matrix.0, matrix.2, matrix.1, matrix.3) } impl fmt::Display for Matrix { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "( {}, {} )\n( {}, {} )", self.0, self.1, self.2, self.3) } } fn tuple_activity() { println!("{:?}", reverse((20i32, false))); println!("one element tuple: {:?}", (5u32,)); println!("just an integer: {:?}", (5u32)); let tuple = (1, "hello", 4.5, true); let (a, b, c, d) = tuple; println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d); let matrix = Matrix(1.1, 1.2, 2.1, 2.2); println!("Matrix:\n{}", matrix); println!("Transpose:\n{}", transpose(matrix)); } fn analyze_slice(slice: &[Matrix]) { println!("first element of the slice: \n{}", slice[0]); println!("the slice has {} elements.", slice.len()); } fn arrays_slices() { let x: [Matrix; 2] = [ Matrix(10.2f32, 2.1f32, 3.1f32, 4.5f32), Matrix(5.2f32, 6.1f32, 2.1f32, 8.5f32), ]; println!("Array occupies: {:?} bytes", mem::size_of_val(&x)); analyze_slice(&x[1..2]); } #[derive(Debug, Copy, Clone)] struct Person<'a> { name: &'a str, age: u8, } impl<'a> Person<'a> { fn changeName(&mut self, new_name: &'a str) -> &'a Person { self.name = new_name; self } } #[derive(Debug)] struct Point { x: f32, y: f32, } #[derive(Debug)] struct Triangle { top_left: Point, bottom_right: Point, } struct Unit; struct Pair(i32, i32); fn square(point: Point, val: f32) -> Triangle { Triangle { bottom_right: Point { x: point.x + val, y: point.y - val, }, top_left: point, } } fn struct_test() { let mut person = Person { name: "Peter", age: 30u8, }; let new_name = "Janez"; let changedPerson = person.changeName(new_name); println!("Person: {:?}", changedPerson); let point = Point { x: 20.2, y: 30.3 }; println!("Point: {:?}", point); let bottom_right = Point { x: 10.2, ..point }; println!("bottom_right: {:?}", bottom_right); let top_left = Point { x: 2.2, y: 5.3 }; let triangle = Triangle { bottom_right: bottom_right, top_left: top_left, }; println!("{:?}", triangle); let Triangle { bottom_right: Point { x: x1, y: y1 }, top_left: Point { x: x2, y: y2 }, } = triangle; println!("x1: {}, y1: {}, x2: {}, y2:{}", x1, y1, x2, y2); println!("Area:{}", (x2 - x1) * (y2 - y1)); let unit = Unit; let pair = Pair(20, 30); println!("pair contains {:?} and {:?}", pair.0, pair.1); let Pair(x, y) = pair; println!("pair contains {:?} and {:?}", x, y); println!("{:?}", square(point, 20.2)); } enum WebEvent { PageLoad, PageUnload, KeyPress(char), Paste(String), Click { x: i64, y: i64 }, } use crate::WebEvent::*; impl WebEvent { fn run(&self, m: i32) -> String { match self { PageLoad => return format!("Page loaded in {} seconds.", m), PageUnload => format!("PageUnloaded"), KeyPress(c) => format!("KeyPressed: {}", c), Paste(s) => format!("Pasted: {}", s), Click { x, y } => format!("Clicked on position: {}, {}", x, y), } } } fn inspect(event: WebEvent) { match event { PageLoad => println!("PageLoaded"), PageUnload => println!("PageUnloaded"), KeyPress(c) => println!("KeyPressed: {}", c), Paste(s) => println!("Pasted: {}", s), Click { x, y } => println!("Clicked on position: {}, {}", x, y), } } enum Number_enum { Zero, One, Two, } // enum with explicit discriminator enum Color { Red = 0xff0000, Green = 0x00ff00, Blue = 0x0000ff, } fn test_enum() { let pressed = KeyPress('x'); // `to_owned()` creates an owned `String` from a string slice. let pasted = Paste("my text".to_owned()); let click = Click { x: 20, y: 80 }; let load = PageLoad; let unload = PageUnload; inspect(pressed); inspect(pasted); inspect(click); inspect(load); inspect(unload); let loadSec = WebEvent::PageLoad; println!("{}", &loadSec.run(20)); println!("zero is {}", Number_enum::Zero as i32); println!("one is {}", Number_enum::One as i32); println!("roses are #{:06x}", Color::Red as i32); println!("violets are #{:06x}", Color::Blue as i32); } fn test_var_bind() { let mut x = 2; { let x = "4"; } x = 4; } fn casting() { let decimal = 22.8832_f32; let integer = decimal as u8; println!("Integer: {}", integer); let character = integer as char; println!("character: {}", character); println!("1000 as a u16 is: {:b}", 1000 as u16); let num = 1000; println!("1000 as a u8 is : {:b}", num as u8); println!(" -1 as a u8 is : {:b}", (-1i8) as u8); println!("1000 mod 256 is : {:b}", 1000 % 256); // Unless it already fits, of course. println!(" 128 as a i16 is: {:b} ({})", 128 as i16, 128 as i16); // 128 as u8 -> 128, whose two's complement in eight bits is: let num: i16 = 128; println!(" 128 as a i8 is : {:b} ({})", num as i8, num as i8); println!("127={:b}", 127_i8); println!("-128={:b}", -128_i8); println!("255={:b}", 255_u8); println!("0={:b}", 0_u8); println!("255= {}", 127_u8 as i8); println!("0={:b}", 0_u8 as i8); let x = 1u8; let y = 2u32; let z = 3f32; // Unsuffixed literal, their types depend on how they are used let i = 1; let f = 1.0; // `size_of_val` returns the size of a variable in bytes println!("size of `x` in bytes: {}", std::mem::size_of_val(&x)); println!("size of `y` in bytes: {}", std::mem::size_of_val(&y)); println!("size of `z` in bytes: {}", std::mem::size_of_val(&z)); println!("size of `i` in bytes: {}", std::mem::size_of_val(&i)); println!("size of `f` in bytes: {}", std::mem::size_of_val(&f)); let elem = 5u8; // Create an empty vector (a growable array). let mut vec = Vec::new(); // At this point the compiler doesn't know the exact type of `vec`, it // just knows that it's a vector of something (`Vec<_>`). // Insert `elem` in the vector. vec.push(elem); // Aha! Now the compiler knows that `vec` is a vector of `u8`s (`Vec<u8>`) // TODO ^ Try commenting out the `vec.push(elem)` line println!("{:?}", vec); } use std::convert::From; use std::convert::Into; use std::convert::TryFrom; use std::convert::TryInto; #[derive(Debug)] struct Number { value: i32, } impl From<i32> for Number { fn from(item: i32) -> Self { Number { value: item } } } impl fmt::Display for Number { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Number is: {}", self.value) } } #[derive(Debug, PartialEq)] struct EvenNumber(i32); impl TryFrom<i32> for EvenNumber { type Error = (); fn try_from(value: i32) -> Result<Self, Self::Error> { if value % 2 == 0 { Ok(EvenNumber(value)) } else { Err(()) } } } fn conversion() { let s = "Test"; let myString = String::from(s); let b = myString.into_boxed_str(); let ptr = b.as_ptr(); println!("b:{:?}", ptr); let ref_b = b.as_ref(); println!("s:{:?}", ref_b); let number = Number::from(34_i32); println!("{}", number); let n = 5; let num: i32 = n.into(); println!("My number is {:?}", num); assert_eq!(EvenNumber::try_from(8), Ok(EvenNumber(8))); assert_eq!(EvenNumber::try_from(5), Err(())); println!("{:?}", EvenNumber::try_from(5)); let result: Result<EvenNumber, ()> = 8i32.try_into(); assert_eq!(result, Ok(EvenNumber(8))); let result: Result<EvenNumber, ()> = 5i32.try_into(); assert_eq!(result, Err(())); let parsed: i32 = "5".parse().unwrap(); let turbo_parsed = "10".parse::<i32>().unwrap(); let sum = parsed + turbo_parsed; println!("Sum: {:?}", sum); } fn expression() { let x0 = 2; let sum = { let x = 20_i8; let y = 12; x + y + x0 }; println!("Sum: {:?}", sum) } fn flowControl() { let mut count = 0; loop { count += 1; if count % 2 == 0 { continue; } println!("Count: {}", count); if count > 6 { break; } } let mut optional = Some(20); match optional { Some(i) => { println!("Number: {:?}", i); } _ => { println!("Not present"); } } if let Some(i) = Some(20) { println!("Number is indeed: {}", i); } while let Some(i) = optional { if i == 25 { println!("Number is: {}", i); optional = None; } else { println!("`i` is `{:?}`. Try again.", i); optional = Some(i + 1); } } } fn isDivisibleBy(lhs: u32, rhs: u32) -> bool { if (rhs == 0) { return false; } lhs % rhs == 0 } impl Point { fn origin() -> Point { Point { x: 0.0, y: 0.0 } } fn new(x: f32, y: f32) -> Point { Point { x: x, y: y } } fn distance(&self, p: &Point) -> f32 { let dx = self.x - p.x; let dy: f32 = self.y - p.y; (dx * dy + dy + dy).sqrt() } fn translate(&mut self, dx: f32, dy: f32) { self.x += dx; self.y += dy; } } fn functions() { println!("isD: {:?}", isDivisibleBy(20, 4)); println!("isD: {:?}", isDivisibleBy(20, 3)); let mut point = Point::new(22.2, 32.3); let mut origin = Point::origin(); println!("Distance: {:?}", point.distance(&origin)); point.translate(3.0, -2.0); println!("Point: {:?}", point); println!("Distance: {:?}", point.distance(&origin)); let x = 20; let fun1 = |i: i32| -> i32 { i + 1 + x }; let fun2 = |i| i + 1 + x; let i = 1; println!("Inc1: {:?}", fun1(i)); println!("Inc2: {:?}", fun2(i)); let one = || 1; println!("One: {:?}", one()); let color = "Green"; let print = || println!("Color: {:?}", color); print(); let _reborow = &color; print(); let mut count = 0; let mut inc = || { count += 1; println!("Count: {:?}", count); }; inc(); inc(); let reborrow = &mut count; let movable = Box::new(3); let consume = || { println!("Movable: {:?}", movable); mem::drop(movable); }; consume(); let haystack = vec![1, 2, 3]; let contains = move |needle| haystack.contains(needle); println!("{}", contains(&1)); println!("{}", contains(&2)); } fn apply<F>(f: F) where F: FnOnce(), { f(); } fn apply_to_3<F>(f: F) -> i32 where F: Fn(i32) -> i32, { f(3) } fn call_me<F: Fn()>(f: F) { f(); } fn function() { println!("I am function"); } fn functions2()
fn create_fn() -> impl Fn() { let text = "Fn".to_owned(); move || println!("This is a text: {}", text) } fn create_fn_mut() -> impl FnMut() { let text = "FnMut".to_owned(); move || println!("This is a text: {}", text) } fn create_fn_once() -> impl FnOnce() { let text = "FnOnce".to_owned(); move || println!("This is a: {}", text) } fn functions3() { let x = create_fn(); x(); let mut y = create_fn_mut(); y(); let z = create_fn_once(); z(); let v1 = vec![1, 2, 3]; let v2 = vec![4, 5, 6]; println!("2 in v1: {}", v1.iter().any(|&x| x == 2)); println!("2 in v2: {}", v2.iter().any(|&x| x == 2)); let a1 = [1, 2, 3]; let a2 = [4, 5, 6]; println!("2 in v1: {}", a1.iter().any(|&x| x == 2)); println!("2 in v2: {}", a2.iter().any(|&x| x == 2)); let mut iter1 = v1.iter(); let mut into_iter = v2.into_iter(); println!("Find 2 in v1: {:?}", iter1.find(|&&x| x == 2)); println!("Find 2 in v1: {:?}", into_iter.find(|&x| x == 2)); let array1 = [1, 2, 3]; let array2 = [4, 5, 6]; println!("Find 2 in v1: {:?}", array1.iter().find(|&&x| x == 2)); println!("Find 2 in v1: {:?}", array2.into_iter().find(|&&x| x == 2)); let index_of_first_even_number = array1.iter().position(|x| x % 2 == 0); println!( "index_of_first_even_number: {}", index_of_first_even_number.unwrap() ); } fn is_odd(n: u32) -> bool { n % 2 == 1 } fn foo() -> () { () } fn higherOrder() { let upper = 1000; let mut acc = 0; for n in 0.. { let n_squared = n * n; if n_squared > upper { break; } else if is_odd(n_squared) { acc += n_squared; } } println!("Sum1: {:?}", acc); let sum2 = (0..) .map(|n| n * n) .take_while(|&n_squared| n_squared < upper) .filter(|&n_squared| n_squared % 2 == 1) .fold(0, |acc, n_squared| acc + n_squared); println!("Sum2: {:?}", sum2); fn sum_odd_numbers(up_to: u32) -> u32 { let mut acc = 0; for i in 0..up_to { // Notice that the return type of this match expression must be u32 // because of the type of the "addition" variable. let addition: u32 = match i % 2 == 1 { // The "i" variable is of type u32, which is perfectly fine. true => i, // On the other hand, the "continue" expression does not return // u32, but it is still fine, because it never returns and therefore // does not violate the type requirements of the match expression. false => continue, }; acc += addition; } acc } println!( "Sum of odd numbers up to 9 (excluding): {}", sum_odd_numbers(9) ); } struct S; struct GenericVal<T>(T); impl GenericVal<i32> {} impl GenericVal<S> {} impl<T> GenericVal<T> {} struct Val { val: f64, } struct GenVal<T> { gen_val: T, } // impl of Val impl Val { fn value(&self) -> &f64 { &self.val } } // impl of GenVal for a generic type `T` impl<T> GenVal<T> { fn value(&self) -> &T { &self.gen_val } } fn generics() { let x = Val { val: 3.0 }; let y = GenVal { gen_val: 3i32 }; println!("{}, {}", x.value(), y.value()); } fn create_box() { let _box = Box::new(3i32); } struct ToDrop; impl Drop for ToDrop { fn drop(&mut self) { println!("ToDrop is being dropped"); } } fn destroy_box(c: Box<i32>) { println!("Destroying a box that contains {}", c); // `c` is destroyed and the memory freed } fn scoping() { /* create_box(); let _box2 = Box::new(5i32); { let _box3 = Box::new(4i32); } let x = ToDrop; { let y = ToDrop; }*/ let x = 5u32; let y = x; println!("x is {}, and y is {}", x, y); let a = Box::new(5i32); let mut b = a; *b = 30i32; //destroy_box(b); println!("{}", b); } fn ownership() { let a = Box::new(5i32); let mut b = a; *b = 4; println!("{}", b); } fn eat_box(boxed: Box<i32>) { println!("{}", boxed); } fn borrow(borrowed: &i32) { println!("{}", borrowed); } fn borrowing() { let boxed = Box::new(5_i32); let stacked = 6_i32; borrow(&boxed); borrow(&stacked); { let refTo: &i32 = &boxed; borrow(refTo); } eat_box(boxed); } #[derive(Clone, Copy)] struct Book { author: &'static str, title: &'static str, year: u32, } fn borrow_book(book: &Book) { println!("I immutably borrowed {} - {}", book.author, book.title); } fn new_edition(book: &mut Book) { book.year = 2014; println!("I mutably borrowed {} - {} edition", book.title, book.year); } fn mutability() { let immutable_book = Book { author: "Ivan Cankar", title: "Hlapci", year: 1910, }; let mut mutable_book = immutable_book; borrow_book(&immutable_book); borrow_book(&mutable_book); new_edition(&mut mutable_book); } struct Location { x: i32, y: i32, z: i32, } fn aliasing() { let mut location = Location { x: 0, y: 0, z: 0 }; let borrow1 = &location; let borrow2 = &location; println!("{} {}", location.x, borrow1.x); //let mut_borow = &mut location; println!( "Location has coordinates: ({}, {}, {})", borrow1.x, borrow2.y, location.z ); let mut_borow = &mut location; mut_borow.x = 10; mut_borow.y = 23; mut_borow.z = 29; let borrow3 = &location; } #[derive(PartialEq, PartialOrd)] struct Centimeters(f64); #[derive(Debug)] struct Inches(i32); impl Inches { fn to_centimeters(&self) -> Centimeters { let &Inches(inches) = self; Centimeters(inches as f64 * 2.54) } } struct Seconds(i32); fn deriveTest() { let one_second = Seconds(1); let foot = Inches(12); println!("One foot equals: {:?}", foot); let meter = Centimeters(100.0); let cmp = if foot.to_centimeters() < meter { "smaller" } else { "bigger" }; println!("One foot is {} than one meter.", cmp); } struct Sheep {} struct Cow {} trait Animal { fn noise(&self) -> &'static str; } impl Animal for Sheep { fn noise(&self) -> &'static str { "baaah" } } impl Animal for Cow { fn noise(&self) -> &'static str { "moooooo" } } fn random_animal(random_number: f64) -> Box<dyn Animal> { if random_number < 0.5 { Box::new(Sheep {}) } else { Box::new(Cow {}) } } fn dyReturn() { let random_number = 0.3444; let animal = random_animal(random_number); println!( "You've randomly chosen an animal, and it says {}", animal.noise() ); } use std::ops; struct Foo; struct Bar; #[derive(Debug)] struct FooBar; #[derive(Debug)] struct BarFoo; impl ops::Add<Bar> for Foo { type Output = FooBar; fn add(self, _rhs: Bar) -> FooBar { println!("> Foo.add(Bar) was called"); FooBar } } impl ops::Add<Foo> for Bar { type Output = BarFoo; fn add(self, rhs: Foo) -> BarFoo { println!("> Bar.add(Foo) was called"); BarFoo } } fn operatorOverloading() { println!("Foo + Bar = {:?}", Foo + Bar); println!("Bar + Foo = {:?}", Bar + Foo); } struct Droppable { name: &'static str, } impl Drop for Droppable { fn drop(&mut self) { println!("> Dropping {}", self.name); } } fn dropping() { let a = Droppable { name: "a" }; { let b = Droppable { name: "b" }; { let c = Droppable { name: "c" }; let d = Droppable { name: "d" }; println!("Exiting block B"); } println!("Just exited block B"); println!("Exiting block A"); } } struct Fibonacci { curr: u32, next: u32, } impl Iterator for Fibonacci { type Item = u32; fn next(&mut self) -> Option<u32> { let new_next = self.curr + self.next; self.curr = self.next; self.next = new_next; Some(self.curr) } } fn fibonacci() -> Fibonacci { Fibonacci { curr: 0, next: 1 } } fn iterTest() { let mut sequence = 0..3; println!("Four consecutive `next` calls on 0..3"); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); println!("> {:?}", sequence.next()); for i in fibonacci().take(4) { println!("> {}", i); } for i in fibonacci().skip(4).take(4) { println!("> {}", i); } let array = [1u32, 3, 3, 7]; for i in array.iter() { println!("> {}", i); } } fn main() { codewars::cw01::run(); // literals_operator(); //tuple_activity(); //arrays_slices(); // struct_test(); //test_enum(); //test_var_bind(); //casting(); //conversion(); //expression(); //flowControl(); // functions(); //functions2(); //functions3(); //higherOrder(); //generics(); //scoping(); //ownership(); //borrowing(); //mutability(); // aliasing(); //deriveTest(); //dyReturn(); // operatorOverloading(); // dropping(); iterTest(); }
{ let x = 30; println!("x: {:?}", x); let y = apply_to_3(|x| x + 20); println!("y: {:?}", y); let greeting = "Hello"; let mut farewel = "Goodby".to_owned(); let diary = || { println!("I said {}", greeting); farewel.push_str("!!!!!"); println!("Than I screemed {}", farewel); println!("Than I sleep"); mem::drop(farewel); }; apply(diary); let double = |x| 2 * x; println!("3 doubled: {}", apply_to_3(double)); let closure = || println!("I am closure"); call_me(function); call_me(closure); }
identifier_body
dec.rs
//! Check declarations and expressions. use crate::ast::{Cases, DatBind, Dec, ExBindInner, Exp, Label, Long, TyBind}; use crate::intern::StrRef; use crate::loc::Located; use crate::statics::ck::util::{ env_ins, env_merge, generalize, get_env, get_ty_sym, get_val_info, insert_ty_vars, instantiate, }; use crate::statics::ck::{exhaustive, pat, ty}; use crate::statics::types::{ Cx, Env, Error, Item, Pat, Result, State, StrEnv, Ty, TyEnv, TyInfo, TyScheme, TyVar, Tys, ValEnv, ValInfo, }; use std::collections::{BTreeMap, HashMap, HashSet}; fn ck_exp(cx: &Cx, st: &mut State, exp: &Located<Exp<StrRef>>) -> Result<Ty> { // The special constants are as per SML Definition (1). Note that SML Definition (5) is handled by // the parser and SML Definition (7) is handled by having atomic and non-atomic expressions be // part of the same enum. match &exp.val { Exp::DecInt(_) | Exp::HexInt(_) => Ok(Ty::INT), Exp::DecWord(_) | Exp::HexWord(_) => Ok(Ty::WORD), Exp::Real(_) => Ok(Ty::REAL), Exp::String(_) => Ok(Ty::STRING), Exp::Char(_) => Ok(Ty::CHAR), // SML Definition (2). Note that Subst, instantiate, generalize, unify, etc are all borne from // the comment on this rule: "The instantiation of type schemes allows different occurrences of // a single longvid to assume different types." Exp::LongVid(vid) => { let val_info = get_val_info(get_env(&cx.env, vid)?, vid.last)?; Ok(instantiate(st, &val_info.ty_scheme)) } // SML Definition (3) Exp::Record(rows) => { let mut ty_rows = BTreeMap::new(); // SML Definition (6) for row in rows { let ty = ck_exp(cx, st, &row.val)?; if ty_rows.insert(row.lab.val, ty).is_some() { return Err(row.lab.loc.wrap(Error::DuplicateLabel(row.lab.val))); } } Ok(Ty::Record(ty_rows)) } Exp::Select(..) => Err(exp.loc.wrap(Error::Todo("record selectors"))), // SML Definition Appendix A - tuples are sugar for records Exp::Tuple(exps) => { let mut ty_rows = BTreeMap::new(); for (idx, exp) in exps.iter().enumerate() { let ty = ck_exp(cx, st, exp)?; assert!(ty_rows.insert(Label::tuple(idx), ty).is_none()); } Ok(Ty::Record(ty_rows)) } // SML Definition Appendix A - lists are sugar for cons + nil Exp::List(exps) => { let elem = Ty::Var(st.new_ty_var(false)); for exp in exps { let ty = ck_exp(cx, st, exp)?; st.unify(exp.loc, elem.clone(), ty)?; } Ok(Ty::list(elem)) } // SML Definition Appendix A - sequences ignore all but the last expression Exp::Sequence(exps) => { let mut ret = None; for exp in exps { ret = Some(ck_exp(cx, st, exp)?); } Ok(ret.unwrap()) } // SML Definition (4) Exp::Let(dec, exps) => { let gen_syms = st.generated_syms(); let env = ck(cx, st, dec)?; let mut cx = cx.clone(); cx.o_plus(env); let mut last = None; for exp in exps { last = Some((exp.loc, ck_exp(&cx, st, exp)?)); } let (loc, mut ty) = last.unwrap(); ty.apply(&st.subst); if !gen_syms.contains(&ty.ty_names()) { return Err(loc.wrap(Error::TyNameEscape)); } Ok(ty) } // SML Definition (8) Exp::App(func, arg) => { let func_ty = ck_exp(cx, st, func)?; let arg_ty = ck_exp(cx, st, arg)?; // we don't actually _need_ to case on func_ty, since the Var case is actually correct for // _all_ types. we just do this to produce better error messages in the Record and Ctor cases. match func_ty { Ty::Var(tv) => { if st.subst.is_bound(&tv) { Err(exp.loc.wrap(Error::NotArrowTy(func_ty))) } else { let ret_ty = Ty::Var(st.new_ty_var(false)); let arrow_ty = Ty::Arrow(arg_ty.into(), ret_ty.clone().into()); st.unify(exp.loc, func_ty, arrow_ty)?; Ok(ret_ty) } } Ty::Arrow(func_arg_ty, func_ret_ty) => { st.unify(exp.loc, *func_arg_ty, arg_ty)?; Ok(*func_ret_ty) } Ty::Record(_) | Ty::Ctor(_, _) => Err(exp.loc.wrap(Error::NotArrowTy(func_ty))), } } // SML Definition (8). Infix application is the same as `op`ing the infix operator and applying // it to a tuple (lhs, rhs). Exp::InfixApp(lhs, func, rhs) => { let val_info = get_val_info(&cx.env, *func)?; let func_ty = instantiate(st, &val_info.ty_scheme); let lhs_ty = ck_exp(cx, st, lhs)?; let rhs_ty = ck_exp(cx, st, rhs)?; let ret_ty = Ty::Var(st.new_ty_var(false)); let arrow_ty = Ty::Arrow(Ty::pair(lhs_ty, rhs_ty).into(), ret_ty.clone().into()); st.unify(exp.loc, func_ty, arrow_ty)?; Ok(ret_ty) } // SML Definition (9) Exp::Typed(inner, ty) => { let exp_ty = ck_exp(cx, st, inner)?; let ty_ty = ty::ck(cx, &st.tys, ty)?; st.unify(exp.loc, ty_ty, exp_ty.clone())?; Ok(exp_ty) } // SML Definition Appendix A - boolean operators are sugar for `if` Exp::Andalso(lhs, rhs) | Exp::Orelse(lhs, rhs) => { let lhs_ty = ck_exp(cx, st, lhs)?; let rhs_ty = ck_exp(cx, st, rhs)?; st.unify(lhs.loc, Ty::BOOL, lhs_ty)?; st.unify(rhs.loc, Ty::BOOL, rhs_ty)?; Ok(Ty::BOOL) } // SML Definition (10) Exp::Handle(head, cases) => { let head_ty = ck_exp(cx, st, head)?; let (pats, arg_ty, res_ty) = ck_cases(cx, st, cases)?; exhaustive::ck_handle(pats)?; st.unify(exp.loc, Ty::EXN, arg_ty)?; st.unify(exp.loc, head_ty.clone(), res_ty)?; Ok(head_ty) } // SML Definition (11) Exp::Raise(exp) => { let exp_ty = ck_exp(cx, st, exp)?; st.unify(exp.loc, Ty::EXN, exp_ty)?; Ok(Ty::Var(st.new_ty_var(false))) } // SML Definition Appendix A - `if` is sugar for casing Exp::If(cond, then_e, else_e) => { let cond_ty = ck_exp(cx, st, cond)?; let then_ty = ck_exp(cx, st, then_e)?; let else_ty = ck_exp(cx, st, else_e)?; st.unify(cond.loc, Ty::BOOL, cond_ty)?; st.unify(exp.loc, then_ty.clone(), else_ty)?; Ok(then_ty) } Exp::While(..) => Err(exp.loc.wrap(Error::Todo("`while`"))), // SML Definition Appendix A - `case` is sugar for application to a `fn` Exp::Case(head, cases) => { let head_ty = ck_exp(cx, st, head)?; let (pats, arg_ty, res_ty) = ck_cases(cx, st, cases)?; exhaustive::ck_match(pats, exp.loc)?; st.unify(exp.loc, head_ty, arg_ty)?; Ok(res_ty) } // SML Definition (12) Exp::Fn(cases) => { let (pats, arg_ty, res_ty) = ck_cases(cx, st, cases)?; exhaustive::ck_match(pats, exp.loc)?; Ok(Ty::Arrow(arg_ty.into(), res_ty.into())) } } } /// SML Definition (13) fn ck_cases(cx: &Cx, st: &mut State, cases: &Cases<StrRef>) -> Result<(Vec<Located<Pat>>, Ty, Ty)> { let arg_ty = Ty::Var(st.new_ty_var(false)); let res_ty = Ty::Var(st.new_ty_var(false)); let mut pats = Vec::with_capacity(cases.arms.len()); // SML Definition (14) for arm in cases.arms.iter() { let (val_env, pat_ty, pat) = pat::ck(cx, st, &arm.pat)?; pats.push(arm.pat.loc.wrap(pat)); let mut cx = cx.clone(); cx.env.val_env.extend(val_env); let exp_ty = ck_exp(&cx, st, &arm.exp)?; st.unify(arm.pat.loc, arg_ty.clone(), pat_ty)?; st.unify(arm.exp.loc, res_ty.clone(), exp_ty)?; } Ok((pats, arg_ty, res_ty)) } /// Returns `Ok(())` iff `name` is not a forbidden binding name. TODO there are more of these in /// certain situations fn ck_binding(name: Located<StrRef>) -> Result<()> { let val = name.val; if val == StrRef::TRUE || val == StrRef::FALSE || val == StrRef::NIL || val == StrRef::CONS || val == StrRef::REF { return Err(name.loc.wrap(Error::ForbiddenBinding(name.val))); } Ok(()) } struct FunInfo { args: Vec<TyVar>, ret: TyVar, } fn fun_infos_to_ve(fun_infos: &HashMap<StrRef, FunInfo>) -> ValEnv
pub fn ck(cx: &Cx, st: &mut State, dec: &Located<Dec<StrRef>>) -> Result<Env> { match &dec.val { // SML Definition (15) Dec::Val(ty_vars, val_binds) => { let mut cx_cl; let cx = if ty_vars.is_empty() { cx } else { cx_cl = cx.clone(); insert_ty_vars(&mut cx_cl, st, ty_vars)?; &cx_cl }; let mut val_env = ValEnv::new(); // SML Definition (25) for val_bind in val_binds { // SML Definition (26) if val_bind.rec { return Err(dec.loc.wrap(Error::Todo("recursive val binds"))); } let (other, pat_ty, pat) = pat::ck(cx, st, &val_bind.pat)?; for &name in other.keys() { ck_binding(val_bind.pat.loc.wrap(name))?; } let exp_ty = ck_exp(cx, st, &val_bind.exp)?; st.unify(dec.loc, pat_ty.clone(), exp_ty)?; exhaustive::ck_bind(pat, val_bind.pat.loc)?; for (name, mut val_info) in other { generalize(cx, st, ty_vars, &mut val_info.ty_scheme); let name = val_bind.pat.loc.wrap(name); env_ins(&mut val_env, name, val_info, Item::Val)?; } } Ok(val_env.into()) } // SML Definition Appendix A - `fun` is sugar for `val rec` and `case` Dec::Fun(ty_vars, fval_binds) => { let mut cx_cl; let cx = if ty_vars.is_empty() { cx } else { cx_cl = cx.clone(); insert_ty_vars(&mut cx_cl, st, ty_vars)?; &cx_cl }; let mut fun_infos = HashMap::with_capacity(fval_binds.len()); for fval_bind in fval_binds { let first = fval_bind.cases.first().unwrap(); let info = FunInfo { args: first.pats.iter().map(|_| st.new_ty_var(false)).collect(), ret: st.new_ty_var(false), }; // copied from env_ins in util if fun_infos.insert(first.vid.val, info).is_some() { let err = Error::Duplicate(Item::Val, first.vid.val); return Err(first.vid.loc.wrap(err)); } } for fval_bind in fval_binds { let name = fval_bind.cases.first().unwrap().vid.val; let info = fun_infos.get(&name).unwrap(); let mut arg_pats = Vec::with_capacity(fval_bind.cases.len()); for case in fval_bind.cases.iter() { if name != case.vid.val { let err = Error::FunDecNameMismatch(name, case.vid.val); return Err(case.vid.loc.wrap(err)); } if info.args.len() != case.pats.len() { let err = Error::FunDecWrongNumPats(info.args.len(), case.pats.len()); let begin = case.pats.first().unwrap().loc; let end = case.pats.last().unwrap().loc; return Err(begin.span(end).wrap(err)); } let mut pats_val_env = ValEnv::new(); let mut arg_pat = Vec::with_capacity(info.args.len()); for (pat, &tv) in case.pats.iter().zip(info.args.iter()) { let (ve, pat_ty, new_pat) = pat::ck(cx, st, pat)?; st.unify(pat.loc, Ty::Var(tv), pat_ty)?; env_merge(&mut pats_val_env, ve, pat.loc, Item::Val)?; arg_pat.push(new_pat); } let begin = case.pats.first().unwrap().loc; let end = case.pats.last().unwrap().loc; arg_pats.push(begin.span(end).wrap(Pat::record(arg_pat))); if let Some(ty) = &case.ret_ty { let new_ty = ty::ck(cx, &st.tys, ty)?; st.unify(ty.loc, Ty::Var(info.ret), new_ty)?; } let mut cx = cx.clone(); // no dupe checking here - intentionally shadow. cx.env.val_env.extend(fun_infos_to_ve(&fun_infos)); cx.env.val_env.extend(pats_val_env); let body_ty = ck_exp(&cx, st, &case.body)?; st.unify(case.body.loc, Ty::Var(info.ret), body_ty)?; } let begin = fval_bind.cases.first().unwrap().vid.loc; let end = fval_bind.cases.last().unwrap().body.loc; exhaustive::ck_match(arg_pats, begin.span(end))?; } let mut val_env = fun_infos_to_ve(&fun_infos); for val_info in val_env.values_mut() { generalize(cx, st, ty_vars, &mut val_info.ty_scheme); } Ok(val_env.into()) } // SML Definition (16) Dec::Type(ty_binds) => ck_ty_binds(cx, st, ty_binds), // SML Definition (17) Dec::Datatype(dat_binds, ty_binds) => { let mut env = ck_dat_binds(cx.clone(), st, dat_binds)?; // SML Definition Appendix A - `datatype withtype` is sugar for `datatype; type` let mut cx = cx.clone(); cx.o_plus(env.clone()); env.extend(ck_ty_binds(&cx, st, ty_binds)?); Ok(env) } // SML Definition (18) Dec::DatatypeCopy(ty_con, long) => ck_dat_copy(cx, &st.tys, *ty_con, long), // SML Definition (19) Dec::Abstype(..) => Err(dec.loc.wrap(Error::Todo("`abstype`"))), // SML Definition (20) Dec::Exception(ex_binds) => { let mut val_env = ValEnv::new(); for ex_bind in ex_binds { let val_info = match &ex_bind.inner { // SML Definition (30) ExBindInner::Ty(ty) => match ty { None => ValInfo::exn(), Some(ty) => ValInfo::exn_fn(ty::ck(cx, &st.tys, ty)?), }, // SML Definition (31) ExBindInner::Long(vid) => { let val_info = get_val_info(get_env(&cx.env, vid)?, vid.last)?; if !val_info.id_status.is_exn() { return Err(vid.loc().wrap(Error::ExnWrongIdStatus(val_info.id_status))); } val_info.clone() } }; env_ins(&mut val_env, ex_bind.vid, val_info, Item::Val)?; } Ok(val_env.into()) } // SML Definition (21) Dec::Local(fst, snd) => { let fst_env = ck(cx, st, fst)?; let mut cx = cx.clone(); cx.o_plus(fst_env); ck(&cx, st, snd) } // SML Definition (22) Dec::Open(longs) => { let mut env = Env::default(); for long in longs { env.extend(get_env(&cx.env, long)?.clone()); } Ok(env) } // SML Definition (23), SML Definition (24) Dec::Seq(decs) => { let mut cx = cx.clone(); let mut ret = Env::default(); for dec in decs { cx.o_plus(ret.clone()); let env = ck(&cx, st, dec)?; ret.extend(env); } Ok(ret) } Dec::Infix(..) | Dec::Infixr(..) | Dec::Nonfix(..) => Ok(Env::default()), } } /// SML Definition (16) fn ck_ty_binds(cx: &Cx, st: &mut State, ty_binds: &[TyBind<StrRef>]) -> Result<Env> { let mut ty_env = TyEnv::default(); // SML Definition (27) for ty_bind in ty_binds { let mut cx_cl; let cx = if ty_bind.ty_vars.is_empty() { cx } else { cx_cl = cx.clone(); insert_ty_vars(&mut cx_cl, st, &ty_bind.ty_vars)?; &cx_cl }; let ty = ty::ck(cx, &st.tys, &ty_bind.ty)?; let sym = st.new_sym(ty_bind.ty_con); env_ins(&mut ty_env.inner, ty_bind.ty_con, sym, Item::Ty)?; // TODO better equality checks let equality = ty.is_equality(&st.tys); let info = TyInfo { ty_fcn: TyScheme { ty_vars: ty_bind .ty_vars .iter() .map(|tv| { let tv = *cx.ty_vars.get(&tv.val).unwrap(); st.subst.remove_bound(&tv); tv }) .collect(), ty, overload: None, }, val_env: ValEnv::new(), equality, }; st.tys.insert(sym, info); } Ok(ty_env.into()) } /// SML Definition (17), SML Definition (71). The checking for {datatype, constructor} {bindings, /// descriptions} appear to be essentially identical, so we can unite the ASTs and static checking /// functions (i.e. this function). pub fn ck_dat_binds(mut cx: Cx, st: &mut State, dat_binds: &[DatBind<StrRef>]) -> Result<Env> { // these two are across all `DatBind`s. let mut ty_env = TyEnv::default(); let mut val_env = ValEnv::new(); // we must first generate new symbols for _all_ the types being defined, since they are allowed to // reference each other. (apparently? according to SML NJ, but it seems like the Definition does // not indicate this, according to my reading of e.g. SML Definition (28).) let mut syms = Vec::new(); for dat_bind in dat_binds { // create a new symbol for the type being generated with this `DatBind`. let sym = st.new_sym(dat_bind.ty_con); // tell the original context as well as the overall `TyEnv` that we return that this new // datatype does exist, but tell the State that it has just an empty `ValEnv`. also perform dupe // checking on the name of the new type and assert for sanity checking after the dupe check. env_ins(&mut ty_env.inner, dat_bind.ty_con, sym, Item::Ty)?; // no assert is_none since we may be shadowing something from an earlier Dec in this Cx. cx.env.ty_env.inner.insert(dat_bind.ty_con.val, sym); // no mapping from ast ty vars to statics ty vars here. we just need some ty vars to make the // `TyScheme`. pretty much copied from `insert_ty_vars`. let mut set = HashSet::new(); let mut ty_vars = Vec::new(); for tv in dat_bind.ty_vars.iter() { if !set.insert(tv.val.name) { return Err(tv.loc.wrap(Error::Duplicate(Item::TyVar, tv.val.name))); } let new_tv = st.new_ty_var(tv.val.equality); ty_vars.push(new_tv); // no need to `insert_bound` because no unifying occurs. } let ty_args: Vec<_> = ty_vars.iter().copied().map(Ty::Var).collect(); let ty_fcn = TyScheme { ty_vars, ty: Ty::Ctor(ty_args, sym), overload: None, }; st.tys.insert_datatype(sym, ty_fcn); syms.push(sym); } // SML Definition (28), SML Definition (81) for (dat_bind, sym) in dat_binds.iter().zip(syms) { // note that we have to `get` here and then `get_mut` again later because of the borrow checker. let ty_fcn = &st.tys.get(&sym).ty_fcn; let mut cx_cl; let cx = if dat_bind.ty_vars.is_empty() { &cx } else { // it is here that we introduce the mapping from ast ty vars to statics ty vars. we need to do // that in order to check the `ConBind`s. but we cannot introduce the mapping earlier, when we // were generating the statics ty vars and the `Sym`s, because there may be multiple // identically-named ty vars in different `DatBind`s. // // if we wanted we could generate new statics type variables here, but then we'd have to use // those new type variables in the return type of the ctor. it shouldn't matter whether we // generate new type variables here or not (as mentioned, we choose to not) because both the // type function and the ctors of the type will each have a `TyScheme` that binds the type // variables appropriately, so by the magic of alpha conversion they're all distinct anyway. cx_cl = cx.clone(); assert_eq!(dat_bind.ty_vars.len(), ty_fcn.ty_vars.len()); for (ast_tv, &tv) in dat_bind.ty_vars.iter().zip(ty_fcn.ty_vars.iter()) { cx_cl.ty_vars.insert(ast_tv.val, tv); } &cx_cl }; // this ValEnv is specific to this `DatBind`. let mut bind_val_env = ValEnv::new(); let mut equality = true; // SML Definition (29), SML Definition (82) for con_bind in dat_bind.cons.iter() { ck_binding(con_bind.vid)?; // if there is no `of t`, then the type of the ctor is just `T`, where `T` is the new sym type // that is being defined. let mut ty = ty_fcn.ty.clone(); if let Some(arg_ty) = &con_bind.ty { // if there is an `of t`, then the type of the ctor is `t -> T`. we must also update whether // `T` respects equality based on whether `t` does. TODO this doesn't handle the equality // check correctly. let t = ty::ck(cx, &st.tys, arg_ty)?; equality = equality && t.is_equality(&st.tys); ty = Ty::Arrow(t.into(), ty.into()); } let val_info = ValInfo::ctor(TyScheme { ty_vars: ty_fcn.ty_vars.clone(), ty, overload: None, }); // insert the `ValInfo` into the _overall_ `ValEnv` with dupe checking. env_ins(&mut val_env, con_bind.vid, val_info.clone(), Item::Val)?; // _also_ insert the `ValInfo` into the `DatBind`-specific `ValEnv`, but this time dupe // checking is unnecessary (just assert as a sanity check). assert!(bind_val_env.insert(con_bind.vid.val, val_info).is_none()); } // now the `ValEnv` is complete, so we may update `st.tys` with the true definition of this // datatype. st.tys.finish_datatype(&sym, bind_val_env, equality); } Ok(Env { ty_env, val_env, str_env: StrEnv::new(), }) } /// SML Definition (18), SML Definition (72) pub fn ck_dat_copy( cx: &Cx, tys: &Tys, ty_con: Located<StrRef>, long: &Long<StrRef>, ) -> Result<Env> { let sym = get_ty_sym(get_env(&cx.env, long)?, long.last)?; let val_env = tys.get(&sym).val_env.clone(); if val_env.is_empty() { return Err(long.loc().wrap(Error::DatatypeCopyNotDatatype)); } Ok(Env { str_env: StrEnv::new(), ty_env: TyEnv { inner: BTreeMap::from([(ty_con.val, sym)]), }, val_env, }) }
{ fun_infos .iter() .map(|(&name, fun_info)| { let ty = fun_info .args .iter() .rev() .fold(Ty::Var(fun_info.ret), |ac, &tv| { Ty::Arrow(Ty::Var(tv).into(), ac.into()) }); (name, ValInfo::val(TyScheme::mono(ty))) }) .collect() }
identifier_body
dec.rs
//! Check declarations and expressions. use crate::ast::{Cases, DatBind, Dec, ExBindInner, Exp, Label, Long, TyBind}; use crate::intern::StrRef; use crate::loc::Located; use crate::statics::ck::util::{ env_ins, env_merge, generalize, get_env, get_ty_sym, get_val_info, insert_ty_vars, instantiate, }; use crate::statics::ck::{exhaustive, pat, ty}; use crate::statics::types::{ Cx, Env, Error, Item, Pat, Result, State, StrEnv, Ty, TyEnv, TyInfo, TyScheme, TyVar, Tys, ValEnv, ValInfo, }; use std::collections::{BTreeMap, HashMap, HashSet}; fn ck_exp(cx: &Cx, st: &mut State, exp: &Located<Exp<StrRef>>) -> Result<Ty> { // The special constants are as per SML Definition (1). Note that SML Definition (5) is handled by // the parser and SML Definition (7) is handled by having atomic and non-atomic expressions be // part of the same enum. match &exp.val { Exp::DecInt(_) | Exp::HexInt(_) => Ok(Ty::INT), Exp::DecWord(_) | Exp::HexWord(_) => Ok(Ty::WORD), Exp::Real(_) => Ok(Ty::REAL), Exp::String(_) => Ok(Ty::STRING), Exp::Char(_) => Ok(Ty::CHAR), // SML Definition (2). Note that Subst, instantiate, generalize, unify, etc are all borne from // the comment on this rule: "The instantiation of type schemes allows different occurrences of // a single longvid to assume different types." Exp::LongVid(vid) => { let val_info = get_val_info(get_env(&cx.env, vid)?, vid.last)?; Ok(instantiate(st, &val_info.ty_scheme)) } // SML Definition (3) Exp::Record(rows) => { let mut ty_rows = BTreeMap::new(); // SML Definition (6) for row in rows { let ty = ck_exp(cx, st, &row.val)?; if ty_rows.insert(row.lab.val, ty).is_some() { return Err(row.lab.loc.wrap(Error::DuplicateLabel(row.lab.val))); } } Ok(Ty::Record(ty_rows)) } Exp::Select(..) => Err(exp.loc.wrap(Error::Todo("record selectors"))), // SML Definition Appendix A - tuples are sugar for records Exp::Tuple(exps) => { let mut ty_rows = BTreeMap::new(); for (idx, exp) in exps.iter().enumerate() { let ty = ck_exp(cx, st, exp)?; assert!(ty_rows.insert(Label::tuple(idx), ty).is_none()); } Ok(Ty::Record(ty_rows)) } // SML Definition Appendix A - lists are sugar for cons + nil Exp::List(exps) => { let elem = Ty::Var(st.new_ty_var(false)); for exp in exps { let ty = ck_exp(cx, st, exp)?; st.unify(exp.loc, elem.clone(), ty)?; } Ok(Ty::list(elem)) } // SML Definition Appendix A - sequences ignore all but the last expression Exp::Sequence(exps) => { let mut ret = None; for exp in exps { ret = Some(ck_exp(cx, st, exp)?); } Ok(ret.unwrap()) } // SML Definition (4) Exp::Let(dec, exps) => { let gen_syms = st.generated_syms(); let env = ck(cx, st, dec)?; let mut cx = cx.clone(); cx.o_plus(env); let mut last = None; for exp in exps { last = Some((exp.loc, ck_exp(&cx, st, exp)?)); } let (loc, mut ty) = last.unwrap(); ty.apply(&st.subst); if !gen_syms.contains(&ty.ty_names()) { return Err(loc.wrap(Error::TyNameEscape)); } Ok(ty) } // SML Definition (8) Exp::App(func, arg) => { let func_ty = ck_exp(cx, st, func)?; let arg_ty = ck_exp(cx, st, arg)?; // we don't actually _need_ to case on func_ty, since the Var case is actually correct for // _all_ types. we just do this to produce better error messages in the Record and Ctor cases. match func_ty { Ty::Var(tv) => { if st.subst.is_bound(&tv) { Err(exp.loc.wrap(Error::NotArrowTy(func_ty))) } else { let ret_ty = Ty::Var(st.new_ty_var(false)); let arrow_ty = Ty::Arrow(arg_ty.into(), ret_ty.clone().into()); st.unify(exp.loc, func_ty, arrow_ty)?; Ok(ret_ty) } } Ty::Arrow(func_arg_ty, func_ret_ty) => { st.unify(exp.loc, *func_arg_ty, arg_ty)?; Ok(*func_ret_ty) } Ty::Record(_) | Ty::Ctor(_, _) => Err(exp.loc.wrap(Error::NotArrowTy(func_ty))), } } // SML Definition (8). Infix application is the same as `op`ing the infix operator and applying // it to a tuple (lhs, rhs). Exp::InfixApp(lhs, func, rhs) => { let val_info = get_val_info(&cx.env, *func)?; let func_ty = instantiate(st, &val_info.ty_scheme); let lhs_ty = ck_exp(cx, st, lhs)?; let rhs_ty = ck_exp(cx, st, rhs)?; let ret_ty = Ty::Var(st.new_ty_var(false)); let arrow_ty = Ty::Arrow(Ty::pair(lhs_ty, rhs_ty).into(), ret_ty.clone().into()); st.unify(exp.loc, func_ty, arrow_ty)?; Ok(ret_ty) } // SML Definition (9) Exp::Typed(inner, ty) => { let exp_ty = ck_exp(cx, st, inner)?; let ty_ty = ty::ck(cx, &st.tys, ty)?; st.unify(exp.loc, ty_ty, exp_ty.clone())?; Ok(exp_ty) } // SML Definition Appendix A - boolean operators are sugar for `if` Exp::Andalso(lhs, rhs) | Exp::Orelse(lhs, rhs) => { let lhs_ty = ck_exp(cx, st, lhs)?; let rhs_ty = ck_exp(cx, st, rhs)?; st.unify(lhs.loc, Ty::BOOL, lhs_ty)?; st.unify(rhs.loc, Ty::BOOL, rhs_ty)?; Ok(Ty::BOOL) } // SML Definition (10) Exp::Handle(head, cases) => { let head_ty = ck_exp(cx, st, head)?; let (pats, arg_ty, res_ty) = ck_cases(cx, st, cases)?; exhaustive::ck_handle(pats)?; st.unify(exp.loc, Ty::EXN, arg_ty)?; st.unify(exp.loc, head_ty.clone(), res_ty)?; Ok(head_ty) } // SML Definition (11) Exp::Raise(exp) => { let exp_ty = ck_exp(cx, st, exp)?; st.unify(exp.loc, Ty::EXN, exp_ty)?; Ok(Ty::Var(st.new_ty_var(false))) } // SML Definition Appendix A - `if` is sugar for casing Exp::If(cond, then_e, else_e) => { let cond_ty = ck_exp(cx, st, cond)?; let then_ty = ck_exp(cx, st, then_e)?; let else_ty = ck_exp(cx, st, else_e)?; st.unify(cond.loc, Ty::BOOL, cond_ty)?; st.unify(exp.loc, then_ty.clone(), else_ty)?; Ok(then_ty) } Exp::While(..) => Err(exp.loc.wrap(Error::Todo("`while`"))), // SML Definition Appendix A - `case` is sugar for application to a `fn` Exp::Case(head, cases) => { let head_ty = ck_exp(cx, st, head)?; let (pats, arg_ty, res_ty) = ck_cases(cx, st, cases)?; exhaustive::ck_match(pats, exp.loc)?; st.unify(exp.loc, head_ty, arg_ty)?; Ok(res_ty) } // SML Definition (12) Exp::Fn(cases) => { let (pats, arg_ty, res_ty) = ck_cases(cx, st, cases)?; exhaustive::ck_match(pats, exp.loc)?; Ok(Ty::Arrow(arg_ty.into(), res_ty.into())) } } } /// SML Definition (13) fn ck_cases(cx: &Cx, st: &mut State, cases: &Cases<StrRef>) -> Result<(Vec<Located<Pat>>, Ty, Ty)> { let arg_ty = Ty::Var(st.new_ty_var(false)); let res_ty = Ty::Var(st.new_ty_var(false)); let mut pats = Vec::with_capacity(cases.arms.len()); // SML Definition (14) for arm in cases.arms.iter() { let (val_env, pat_ty, pat) = pat::ck(cx, st, &arm.pat)?; pats.push(arm.pat.loc.wrap(pat)); let mut cx = cx.clone(); cx.env.val_env.extend(val_env); let exp_ty = ck_exp(&cx, st, &arm.exp)?; st.unify(arm.pat.loc, arg_ty.clone(), pat_ty)?; st.unify(arm.exp.loc, res_ty.clone(), exp_ty)?; } Ok((pats, arg_ty, res_ty)) } /// Returns `Ok(())` iff `name` is not a forbidden binding name. TODO there are more of these in /// certain situations fn ck_binding(name: Located<StrRef>) -> Result<()> { let val = name.val; if val == StrRef::TRUE || val == StrRef::FALSE || val == StrRef::NIL || val == StrRef::CONS || val == StrRef::REF { return Err(name.loc.wrap(Error::ForbiddenBinding(name.val))); } Ok(()) } struct FunInfo { args: Vec<TyVar>, ret: TyVar, } fn fun_infos_to_ve(fun_infos: &HashMap<StrRef, FunInfo>) -> ValEnv { fun_infos .iter() .map(|(&name, fun_info)| { let ty = fun_info .args .iter() .rev() .fold(Ty::Var(fun_info.ret), |ac, &tv| { Ty::Arrow(Ty::Var(tv).into(), ac.into()) }); (name, ValInfo::val(TyScheme::mono(ty))) }) .collect() } pub fn ck(cx: &Cx, st: &mut State, dec: &Located<Dec<StrRef>>) -> Result<Env> { match &dec.val { // SML Definition (15) Dec::Val(ty_vars, val_binds) => { let mut cx_cl; let cx = if ty_vars.is_empty() { cx } else { cx_cl = cx.clone(); insert_ty_vars(&mut cx_cl, st, ty_vars)?; &cx_cl }; let mut val_env = ValEnv::new(); // SML Definition (25) for val_bind in val_binds { // SML Definition (26) if val_bind.rec { return Err(dec.loc.wrap(Error::Todo("recursive val binds"))); } let (other, pat_ty, pat) = pat::ck(cx, st, &val_bind.pat)?; for &name in other.keys() { ck_binding(val_bind.pat.loc.wrap(name))?; } let exp_ty = ck_exp(cx, st, &val_bind.exp)?; st.unify(dec.loc, pat_ty.clone(), exp_ty)?; exhaustive::ck_bind(pat, val_bind.pat.loc)?; for (name, mut val_info) in other { generalize(cx, st, ty_vars, &mut val_info.ty_scheme); let name = val_bind.pat.loc.wrap(name); env_ins(&mut val_env, name, val_info, Item::Val)?; } } Ok(val_env.into()) } // SML Definition Appendix A - `fun` is sugar for `val rec` and `case`
Dec::Fun(ty_vars, fval_binds) => { let mut cx_cl; let cx = if ty_vars.is_empty() { cx } else { cx_cl = cx.clone(); insert_ty_vars(&mut cx_cl, st, ty_vars)?; &cx_cl }; let mut fun_infos = HashMap::with_capacity(fval_binds.len()); for fval_bind in fval_binds { let first = fval_bind.cases.first().unwrap(); let info = FunInfo { args: first.pats.iter().map(|_| st.new_ty_var(false)).collect(), ret: st.new_ty_var(false), }; // copied from env_ins in util if fun_infos.insert(first.vid.val, info).is_some() { let err = Error::Duplicate(Item::Val, first.vid.val); return Err(first.vid.loc.wrap(err)); } } for fval_bind in fval_binds { let name = fval_bind.cases.first().unwrap().vid.val; let info = fun_infos.get(&name).unwrap(); let mut arg_pats = Vec::with_capacity(fval_bind.cases.len()); for case in fval_bind.cases.iter() { if name != case.vid.val { let err = Error::FunDecNameMismatch(name, case.vid.val); return Err(case.vid.loc.wrap(err)); } if info.args.len() != case.pats.len() { let err = Error::FunDecWrongNumPats(info.args.len(), case.pats.len()); let begin = case.pats.first().unwrap().loc; let end = case.pats.last().unwrap().loc; return Err(begin.span(end).wrap(err)); } let mut pats_val_env = ValEnv::new(); let mut arg_pat = Vec::with_capacity(info.args.len()); for (pat, &tv) in case.pats.iter().zip(info.args.iter()) { let (ve, pat_ty, new_pat) = pat::ck(cx, st, pat)?; st.unify(pat.loc, Ty::Var(tv), pat_ty)?; env_merge(&mut pats_val_env, ve, pat.loc, Item::Val)?; arg_pat.push(new_pat); } let begin = case.pats.first().unwrap().loc; let end = case.pats.last().unwrap().loc; arg_pats.push(begin.span(end).wrap(Pat::record(arg_pat))); if let Some(ty) = &case.ret_ty { let new_ty = ty::ck(cx, &st.tys, ty)?; st.unify(ty.loc, Ty::Var(info.ret), new_ty)?; } let mut cx = cx.clone(); // no dupe checking here - intentionally shadow. cx.env.val_env.extend(fun_infos_to_ve(&fun_infos)); cx.env.val_env.extend(pats_val_env); let body_ty = ck_exp(&cx, st, &case.body)?; st.unify(case.body.loc, Ty::Var(info.ret), body_ty)?; } let begin = fval_bind.cases.first().unwrap().vid.loc; let end = fval_bind.cases.last().unwrap().body.loc; exhaustive::ck_match(arg_pats, begin.span(end))?; } let mut val_env = fun_infos_to_ve(&fun_infos); for val_info in val_env.values_mut() { generalize(cx, st, ty_vars, &mut val_info.ty_scheme); } Ok(val_env.into()) } // SML Definition (16) Dec::Type(ty_binds) => ck_ty_binds(cx, st, ty_binds), // SML Definition (17) Dec::Datatype(dat_binds, ty_binds) => { let mut env = ck_dat_binds(cx.clone(), st, dat_binds)?; // SML Definition Appendix A - `datatype withtype` is sugar for `datatype; type` let mut cx = cx.clone(); cx.o_plus(env.clone()); env.extend(ck_ty_binds(&cx, st, ty_binds)?); Ok(env) } // SML Definition (18) Dec::DatatypeCopy(ty_con, long) => ck_dat_copy(cx, &st.tys, *ty_con, long), // SML Definition (19) Dec::Abstype(..) => Err(dec.loc.wrap(Error::Todo("`abstype`"))), // SML Definition (20) Dec::Exception(ex_binds) => { let mut val_env = ValEnv::new(); for ex_bind in ex_binds { let val_info = match &ex_bind.inner { // SML Definition (30) ExBindInner::Ty(ty) => match ty { None => ValInfo::exn(), Some(ty) => ValInfo::exn_fn(ty::ck(cx, &st.tys, ty)?), }, // SML Definition (31) ExBindInner::Long(vid) => { let val_info = get_val_info(get_env(&cx.env, vid)?, vid.last)?; if !val_info.id_status.is_exn() { return Err(vid.loc().wrap(Error::ExnWrongIdStatus(val_info.id_status))); } val_info.clone() } }; env_ins(&mut val_env, ex_bind.vid, val_info, Item::Val)?; } Ok(val_env.into()) } // SML Definition (21) Dec::Local(fst, snd) => { let fst_env = ck(cx, st, fst)?; let mut cx = cx.clone(); cx.o_plus(fst_env); ck(&cx, st, snd) } // SML Definition (22) Dec::Open(longs) => { let mut env = Env::default(); for long in longs { env.extend(get_env(&cx.env, long)?.clone()); } Ok(env) } // SML Definition (23), SML Definition (24) Dec::Seq(decs) => { let mut cx = cx.clone(); let mut ret = Env::default(); for dec in decs { cx.o_plus(ret.clone()); let env = ck(&cx, st, dec)?; ret.extend(env); } Ok(ret) } Dec::Infix(..) | Dec::Infixr(..) | Dec::Nonfix(..) => Ok(Env::default()), } } /// SML Definition (16) fn ck_ty_binds(cx: &Cx, st: &mut State, ty_binds: &[TyBind<StrRef>]) -> Result<Env> { let mut ty_env = TyEnv::default(); // SML Definition (27) for ty_bind in ty_binds { let mut cx_cl; let cx = if ty_bind.ty_vars.is_empty() { cx } else { cx_cl = cx.clone(); insert_ty_vars(&mut cx_cl, st, &ty_bind.ty_vars)?; &cx_cl }; let ty = ty::ck(cx, &st.tys, &ty_bind.ty)?; let sym = st.new_sym(ty_bind.ty_con); env_ins(&mut ty_env.inner, ty_bind.ty_con, sym, Item::Ty)?; // TODO better equality checks let equality = ty.is_equality(&st.tys); let info = TyInfo { ty_fcn: TyScheme { ty_vars: ty_bind .ty_vars .iter() .map(|tv| { let tv = *cx.ty_vars.get(&tv.val).unwrap(); st.subst.remove_bound(&tv); tv }) .collect(), ty, overload: None, }, val_env: ValEnv::new(), equality, }; st.tys.insert(sym, info); } Ok(ty_env.into()) } /// SML Definition (17), SML Definition (71). The checking for {datatype, constructor} {bindings, /// descriptions} appear to be essentially identical, so we can unite the ASTs and static checking /// functions (i.e. this function). pub fn ck_dat_binds(mut cx: Cx, st: &mut State, dat_binds: &[DatBind<StrRef>]) -> Result<Env> { // these two are across all `DatBind`s. let mut ty_env = TyEnv::default(); let mut val_env = ValEnv::new(); // we must first generate new symbols for _all_ the types being defined, since they are allowed to // reference each other. (apparently? according to SML NJ, but it seems like the Definition does // not indicate this, according to my reading of e.g. SML Definition (28).) let mut syms = Vec::new(); for dat_bind in dat_binds { // create a new symbol for the type being generated with this `DatBind`. let sym = st.new_sym(dat_bind.ty_con); // tell the original context as well as the overall `TyEnv` that we return that this new // datatype does exist, but tell the State that it has just an empty `ValEnv`. also perform dupe // checking on the name of the new type and assert for sanity checking after the dupe check. env_ins(&mut ty_env.inner, dat_bind.ty_con, sym, Item::Ty)?; // no assert is_none since we may be shadowing something from an earlier Dec in this Cx. cx.env.ty_env.inner.insert(dat_bind.ty_con.val, sym); // no mapping from ast ty vars to statics ty vars here. we just need some ty vars to make the // `TyScheme`. pretty much copied from `insert_ty_vars`. let mut set = HashSet::new(); let mut ty_vars = Vec::new(); for tv in dat_bind.ty_vars.iter() { if !set.insert(tv.val.name) { return Err(tv.loc.wrap(Error::Duplicate(Item::TyVar, tv.val.name))); } let new_tv = st.new_ty_var(tv.val.equality); ty_vars.push(new_tv); // no need to `insert_bound` because no unifying occurs. } let ty_args: Vec<_> = ty_vars.iter().copied().map(Ty::Var).collect(); let ty_fcn = TyScheme { ty_vars, ty: Ty::Ctor(ty_args, sym), overload: None, }; st.tys.insert_datatype(sym, ty_fcn); syms.push(sym); } // SML Definition (28), SML Definition (81) for (dat_bind, sym) in dat_binds.iter().zip(syms) { // note that we have to `get` here and then `get_mut` again later because of the borrow checker. let ty_fcn = &st.tys.get(&sym).ty_fcn; let mut cx_cl; let cx = if dat_bind.ty_vars.is_empty() { &cx } else { // it is here that we introduce the mapping from ast ty vars to statics ty vars. we need to do // that in order to check the `ConBind`s. but we cannot introduce the mapping earlier, when we // were generating the statics ty vars and the `Sym`s, because there may be multiple // identically-named ty vars in different `DatBind`s. // // if we wanted we could generate new statics type variables here, but then we'd have to use // those new type variables in the return type of the ctor. it shouldn't matter whether we // generate new type variables here or not (as mentioned, we choose to not) because both the // type function and the ctors of the type will each have a `TyScheme` that binds the type // variables appropriately, so by the magic of alpha conversion they're all distinct anyway. cx_cl = cx.clone(); assert_eq!(dat_bind.ty_vars.len(), ty_fcn.ty_vars.len()); for (ast_tv, &tv) in dat_bind.ty_vars.iter().zip(ty_fcn.ty_vars.iter()) { cx_cl.ty_vars.insert(ast_tv.val, tv); } &cx_cl }; // this ValEnv is specific to this `DatBind`. let mut bind_val_env = ValEnv::new(); let mut equality = true; // SML Definition (29), SML Definition (82) for con_bind in dat_bind.cons.iter() { ck_binding(con_bind.vid)?; // if there is no `of t`, then the type of the ctor is just `T`, where `T` is the new sym type // that is being defined. let mut ty = ty_fcn.ty.clone(); if let Some(arg_ty) = &con_bind.ty { // if there is an `of t`, then the type of the ctor is `t -> T`. we must also update whether // `T` respects equality based on whether `t` does. TODO this doesn't handle the equality // check correctly. let t = ty::ck(cx, &st.tys, arg_ty)?; equality = equality && t.is_equality(&st.tys); ty = Ty::Arrow(t.into(), ty.into()); } let val_info = ValInfo::ctor(TyScheme { ty_vars: ty_fcn.ty_vars.clone(), ty, overload: None, }); // insert the `ValInfo` into the _overall_ `ValEnv` with dupe checking. env_ins(&mut val_env, con_bind.vid, val_info.clone(), Item::Val)?; // _also_ insert the `ValInfo` into the `DatBind`-specific `ValEnv`, but this time dupe // checking is unnecessary (just assert as a sanity check). assert!(bind_val_env.insert(con_bind.vid.val, val_info).is_none()); } // now the `ValEnv` is complete, so we may update `st.tys` with the true definition of this // datatype. st.tys.finish_datatype(&sym, bind_val_env, equality); } Ok(Env { ty_env, val_env, str_env: StrEnv::new(), }) } /// SML Definition (18), SML Definition (72) pub fn ck_dat_copy( cx: &Cx, tys: &Tys, ty_con: Located<StrRef>, long: &Long<StrRef>, ) -> Result<Env> { let sym = get_ty_sym(get_env(&cx.env, long)?, long.last)?; let val_env = tys.get(&sym).val_env.clone(); if val_env.is_empty() { return Err(long.loc().wrap(Error::DatatypeCopyNotDatatype)); } Ok(Env { str_env: StrEnv::new(), ty_env: TyEnv { inner: BTreeMap::from([(ty_con.val, sym)]), }, val_env, }) }
random_line_split
dec.rs
//! Check declarations and expressions. use crate::ast::{Cases, DatBind, Dec, ExBindInner, Exp, Label, Long, TyBind}; use crate::intern::StrRef; use crate::loc::Located; use crate::statics::ck::util::{ env_ins, env_merge, generalize, get_env, get_ty_sym, get_val_info, insert_ty_vars, instantiate, }; use crate::statics::ck::{exhaustive, pat, ty}; use crate::statics::types::{ Cx, Env, Error, Item, Pat, Result, State, StrEnv, Ty, TyEnv, TyInfo, TyScheme, TyVar, Tys, ValEnv, ValInfo, }; use std::collections::{BTreeMap, HashMap, HashSet}; fn ck_exp(cx: &Cx, st: &mut State, exp: &Located<Exp<StrRef>>) -> Result<Ty> { // The special constants are as per SML Definition (1). Note that SML Definition (5) is handled by // the parser and SML Definition (7) is handled by having atomic and non-atomic expressions be // part of the same enum. match &exp.val { Exp::DecInt(_) | Exp::HexInt(_) => Ok(Ty::INT), Exp::DecWord(_) | Exp::HexWord(_) => Ok(Ty::WORD), Exp::Real(_) => Ok(Ty::REAL), Exp::String(_) => Ok(Ty::STRING), Exp::Char(_) => Ok(Ty::CHAR), // SML Definition (2). Note that Subst, instantiate, generalize, unify, etc are all borne from // the comment on this rule: "The instantiation of type schemes allows different occurrences of // a single longvid to assume different types." Exp::LongVid(vid) => { let val_info = get_val_info(get_env(&cx.env, vid)?, vid.last)?; Ok(instantiate(st, &val_info.ty_scheme)) } // SML Definition (3) Exp::Record(rows) => { let mut ty_rows = BTreeMap::new(); // SML Definition (6) for row in rows { let ty = ck_exp(cx, st, &row.val)?; if ty_rows.insert(row.lab.val, ty).is_some() { return Err(row.lab.loc.wrap(Error::DuplicateLabel(row.lab.val))); } } Ok(Ty::Record(ty_rows)) } Exp::Select(..) => Err(exp.loc.wrap(Error::Todo("record selectors"))), // SML Definition Appendix A - tuples are sugar for records Exp::Tuple(exps) => { let mut ty_rows = BTreeMap::new(); for (idx, exp) in exps.iter().enumerate() { let ty = ck_exp(cx, st, exp)?; assert!(ty_rows.insert(Label::tuple(idx), ty).is_none()); } Ok(Ty::Record(ty_rows)) } // SML Definition Appendix A - lists are sugar for cons + nil Exp::List(exps) => { let elem = Ty::Var(st.new_ty_var(false)); for exp in exps { let ty = ck_exp(cx, st, exp)?; st.unify(exp.loc, elem.clone(), ty)?; } Ok(Ty::list(elem)) } // SML Definition Appendix A - sequences ignore all but the last expression Exp::Sequence(exps) => { let mut ret = None; for exp in exps { ret = Some(ck_exp(cx, st, exp)?); } Ok(ret.unwrap()) } // SML Definition (4) Exp::Let(dec, exps) => { let gen_syms = st.generated_syms(); let env = ck(cx, st, dec)?; let mut cx = cx.clone(); cx.o_plus(env); let mut last = None; for exp in exps { last = Some((exp.loc, ck_exp(&cx, st, exp)?)); } let (loc, mut ty) = last.unwrap(); ty.apply(&st.subst); if !gen_syms.contains(&ty.ty_names()) { return Err(loc.wrap(Error::TyNameEscape)); } Ok(ty) } // SML Definition (8) Exp::App(func, arg) => { let func_ty = ck_exp(cx, st, func)?; let arg_ty = ck_exp(cx, st, arg)?; // we don't actually _need_ to case on func_ty, since the Var case is actually correct for // _all_ types. we just do this to produce better error messages in the Record and Ctor cases. match func_ty { Ty::Var(tv) => { if st.subst.is_bound(&tv) { Err(exp.loc.wrap(Error::NotArrowTy(func_ty))) } else { let ret_ty = Ty::Var(st.new_ty_var(false)); let arrow_ty = Ty::Arrow(arg_ty.into(), ret_ty.clone().into()); st.unify(exp.loc, func_ty, arrow_ty)?; Ok(ret_ty) } } Ty::Arrow(func_arg_ty, func_ret_ty) => { st.unify(exp.loc, *func_arg_ty, arg_ty)?; Ok(*func_ret_ty) } Ty::Record(_) | Ty::Ctor(_, _) => Err(exp.loc.wrap(Error::NotArrowTy(func_ty))), } } // SML Definition (8). Infix application is the same as `op`ing the infix operator and applying // it to a tuple (lhs, rhs). Exp::InfixApp(lhs, func, rhs) => { let val_info = get_val_info(&cx.env, *func)?; let func_ty = instantiate(st, &val_info.ty_scheme); let lhs_ty = ck_exp(cx, st, lhs)?; let rhs_ty = ck_exp(cx, st, rhs)?; let ret_ty = Ty::Var(st.new_ty_var(false)); let arrow_ty = Ty::Arrow(Ty::pair(lhs_ty, rhs_ty).into(), ret_ty.clone().into()); st.unify(exp.loc, func_ty, arrow_ty)?; Ok(ret_ty) } // SML Definition (9) Exp::Typed(inner, ty) => { let exp_ty = ck_exp(cx, st, inner)?; let ty_ty = ty::ck(cx, &st.tys, ty)?; st.unify(exp.loc, ty_ty, exp_ty.clone())?; Ok(exp_ty) } // SML Definition Appendix A - boolean operators are sugar for `if` Exp::Andalso(lhs, rhs) | Exp::Orelse(lhs, rhs) => { let lhs_ty = ck_exp(cx, st, lhs)?; let rhs_ty = ck_exp(cx, st, rhs)?; st.unify(lhs.loc, Ty::BOOL, lhs_ty)?; st.unify(rhs.loc, Ty::BOOL, rhs_ty)?; Ok(Ty::BOOL) } // SML Definition (10) Exp::Handle(head, cases) => { let head_ty = ck_exp(cx, st, head)?; let (pats, arg_ty, res_ty) = ck_cases(cx, st, cases)?; exhaustive::ck_handle(pats)?; st.unify(exp.loc, Ty::EXN, arg_ty)?; st.unify(exp.loc, head_ty.clone(), res_ty)?; Ok(head_ty) } // SML Definition (11) Exp::Raise(exp) => { let exp_ty = ck_exp(cx, st, exp)?; st.unify(exp.loc, Ty::EXN, exp_ty)?; Ok(Ty::Var(st.new_ty_var(false))) } // SML Definition Appendix A - `if` is sugar for casing Exp::If(cond, then_e, else_e) => { let cond_ty = ck_exp(cx, st, cond)?; let then_ty = ck_exp(cx, st, then_e)?; let else_ty = ck_exp(cx, st, else_e)?; st.unify(cond.loc, Ty::BOOL, cond_ty)?; st.unify(exp.loc, then_ty.clone(), else_ty)?; Ok(then_ty) } Exp::While(..) => Err(exp.loc.wrap(Error::Todo("`while`"))), // SML Definition Appendix A - `case` is sugar for application to a `fn` Exp::Case(head, cases) => { let head_ty = ck_exp(cx, st, head)?; let (pats, arg_ty, res_ty) = ck_cases(cx, st, cases)?; exhaustive::ck_match(pats, exp.loc)?; st.unify(exp.loc, head_ty, arg_ty)?; Ok(res_ty) } // SML Definition (12) Exp::Fn(cases) => { let (pats, arg_ty, res_ty) = ck_cases(cx, st, cases)?; exhaustive::ck_match(pats, exp.loc)?; Ok(Ty::Arrow(arg_ty.into(), res_ty.into())) } } } /// SML Definition (13) fn
(cx: &Cx, st: &mut State, cases: &Cases<StrRef>) -> Result<(Vec<Located<Pat>>, Ty, Ty)> { let arg_ty = Ty::Var(st.new_ty_var(false)); let res_ty = Ty::Var(st.new_ty_var(false)); let mut pats = Vec::with_capacity(cases.arms.len()); // SML Definition (14) for arm in cases.arms.iter() { let (val_env, pat_ty, pat) = pat::ck(cx, st, &arm.pat)?; pats.push(arm.pat.loc.wrap(pat)); let mut cx = cx.clone(); cx.env.val_env.extend(val_env); let exp_ty = ck_exp(&cx, st, &arm.exp)?; st.unify(arm.pat.loc, arg_ty.clone(), pat_ty)?; st.unify(arm.exp.loc, res_ty.clone(), exp_ty)?; } Ok((pats, arg_ty, res_ty)) } /// Returns `Ok(())` iff `name` is not a forbidden binding name. TODO there are more of these in /// certain situations fn ck_binding(name: Located<StrRef>) -> Result<()> { let val = name.val; if val == StrRef::TRUE || val == StrRef::FALSE || val == StrRef::NIL || val == StrRef::CONS || val == StrRef::REF { return Err(name.loc.wrap(Error::ForbiddenBinding(name.val))); } Ok(()) } struct FunInfo { args: Vec<TyVar>, ret: TyVar, } fn fun_infos_to_ve(fun_infos: &HashMap<StrRef, FunInfo>) -> ValEnv { fun_infos .iter() .map(|(&name, fun_info)| { let ty = fun_info .args .iter() .rev() .fold(Ty::Var(fun_info.ret), |ac, &tv| { Ty::Arrow(Ty::Var(tv).into(), ac.into()) }); (name, ValInfo::val(TyScheme::mono(ty))) }) .collect() } pub fn ck(cx: &Cx, st: &mut State, dec: &Located<Dec<StrRef>>) -> Result<Env> { match &dec.val { // SML Definition (15) Dec::Val(ty_vars, val_binds) => { let mut cx_cl; let cx = if ty_vars.is_empty() { cx } else { cx_cl = cx.clone(); insert_ty_vars(&mut cx_cl, st, ty_vars)?; &cx_cl }; let mut val_env = ValEnv::new(); // SML Definition (25) for val_bind in val_binds { // SML Definition (26) if val_bind.rec { return Err(dec.loc.wrap(Error::Todo("recursive val binds"))); } let (other, pat_ty, pat) = pat::ck(cx, st, &val_bind.pat)?; for &name in other.keys() { ck_binding(val_bind.pat.loc.wrap(name))?; } let exp_ty = ck_exp(cx, st, &val_bind.exp)?; st.unify(dec.loc, pat_ty.clone(), exp_ty)?; exhaustive::ck_bind(pat, val_bind.pat.loc)?; for (name, mut val_info) in other { generalize(cx, st, ty_vars, &mut val_info.ty_scheme); let name = val_bind.pat.loc.wrap(name); env_ins(&mut val_env, name, val_info, Item::Val)?; } } Ok(val_env.into()) } // SML Definition Appendix A - `fun` is sugar for `val rec` and `case` Dec::Fun(ty_vars, fval_binds) => { let mut cx_cl; let cx = if ty_vars.is_empty() { cx } else { cx_cl = cx.clone(); insert_ty_vars(&mut cx_cl, st, ty_vars)?; &cx_cl }; let mut fun_infos = HashMap::with_capacity(fval_binds.len()); for fval_bind in fval_binds { let first = fval_bind.cases.first().unwrap(); let info = FunInfo { args: first.pats.iter().map(|_| st.new_ty_var(false)).collect(), ret: st.new_ty_var(false), }; // copied from env_ins in util if fun_infos.insert(first.vid.val, info).is_some() { let err = Error::Duplicate(Item::Val, first.vid.val); return Err(first.vid.loc.wrap(err)); } } for fval_bind in fval_binds { let name = fval_bind.cases.first().unwrap().vid.val; let info = fun_infos.get(&name).unwrap(); let mut arg_pats = Vec::with_capacity(fval_bind.cases.len()); for case in fval_bind.cases.iter() { if name != case.vid.val { let err = Error::FunDecNameMismatch(name, case.vid.val); return Err(case.vid.loc.wrap(err)); } if info.args.len() != case.pats.len() { let err = Error::FunDecWrongNumPats(info.args.len(), case.pats.len()); let begin = case.pats.first().unwrap().loc; let end = case.pats.last().unwrap().loc; return Err(begin.span(end).wrap(err)); } let mut pats_val_env = ValEnv::new(); let mut arg_pat = Vec::with_capacity(info.args.len()); for (pat, &tv) in case.pats.iter().zip(info.args.iter()) { let (ve, pat_ty, new_pat) = pat::ck(cx, st, pat)?; st.unify(pat.loc, Ty::Var(tv), pat_ty)?; env_merge(&mut pats_val_env, ve, pat.loc, Item::Val)?; arg_pat.push(new_pat); } let begin = case.pats.first().unwrap().loc; let end = case.pats.last().unwrap().loc; arg_pats.push(begin.span(end).wrap(Pat::record(arg_pat))); if let Some(ty) = &case.ret_ty { let new_ty = ty::ck(cx, &st.tys, ty)?; st.unify(ty.loc, Ty::Var(info.ret), new_ty)?; } let mut cx = cx.clone(); // no dupe checking here - intentionally shadow. cx.env.val_env.extend(fun_infos_to_ve(&fun_infos)); cx.env.val_env.extend(pats_val_env); let body_ty = ck_exp(&cx, st, &case.body)?; st.unify(case.body.loc, Ty::Var(info.ret), body_ty)?; } let begin = fval_bind.cases.first().unwrap().vid.loc; let end = fval_bind.cases.last().unwrap().body.loc; exhaustive::ck_match(arg_pats, begin.span(end))?; } let mut val_env = fun_infos_to_ve(&fun_infos); for val_info in val_env.values_mut() { generalize(cx, st, ty_vars, &mut val_info.ty_scheme); } Ok(val_env.into()) } // SML Definition (16) Dec::Type(ty_binds) => ck_ty_binds(cx, st, ty_binds), // SML Definition (17) Dec::Datatype(dat_binds, ty_binds) => { let mut env = ck_dat_binds(cx.clone(), st, dat_binds)?; // SML Definition Appendix A - `datatype withtype` is sugar for `datatype; type` let mut cx = cx.clone(); cx.o_plus(env.clone()); env.extend(ck_ty_binds(&cx, st, ty_binds)?); Ok(env) } // SML Definition (18) Dec::DatatypeCopy(ty_con, long) => ck_dat_copy(cx, &st.tys, *ty_con, long), // SML Definition (19) Dec::Abstype(..) => Err(dec.loc.wrap(Error::Todo("`abstype`"))), // SML Definition (20) Dec::Exception(ex_binds) => { let mut val_env = ValEnv::new(); for ex_bind in ex_binds { let val_info = match &ex_bind.inner { // SML Definition (30) ExBindInner::Ty(ty) => match ty { None => ValInfo::exn(), Some(ty) => ValInfo::exn_fn(ty::ck(cx, &st.tys, ty)?), }, // SML Definition (31) ExBindInner::Long(vid) => { let val_info = get_val_info(get_env(&cx.env, vid)?, vid.last)?; if !val_info.id_status.is_exn() { return Err(vid.loc().wrap(Error::ExnWrongIdStatus(val_info.id_status))); } val_info.clone() } }; env_ins(&mut val_env, ex_bind.vid, val_info, Item::Val)?; } Ok(val_env.into()) } // SML Definition (21) Dec::Local(fst, snd) => { let fst_env = ck(cx, st, fst)?; let mut cx = cx.clone(); cx.o_plus(fst_env); ck(&cx, st, snd) } // SML Definition (22) Dec::Open(longs) => { let mut env = Env::default(); for long in longs { env.extend(get_env(&cx.env, long)?.clone()); } Ok(env) } // SML Definition (23), SML Definition (24) Dec::Seq(decs) => { let mut cx = cx.clone(); let mut ret = Env::default(); for dec in decs { cx.o_plus(ret.clone()); let env = ck(&cx, st, dec)?; ret.extend(env); } Ok(ret) } Dec::Infix(..) | Dec::Infixr(..) | Dec::Nonfix(..) => Ok(Env::default()), } } /// SML Definition (16) fn ck_ty_binds(cx: &Cx, st: &mut State, ty_binds: &[TyBind<StrRef>]) -> Result<Env> { let mut ty_env = TyEnv::default(); // SML Definition (27) for ty_bind in ty_binds { let mut cx_cl; let cx = if ty_bind.ty_vars.is_empty() { cx } else { cx_cl = cx.clone(); insert_ty_vars(&mut cx_cl, st, &ty_bind.ty_vars)?; &cx_cl }; let ty = ty::ck(cx, &st.tys, &ty_bind.ty)?; let sym = st.new_sym(ty_bind.ty_con); env_ins(&mut ty_env.inner, ty_bind.ty_con, sym, Item::Ty)?; // TODO better equality checks let equality = ty.is_equality(&st.tys); let info = TyInfo { ty_fcn: TyScheme { ty_vars: ty_bind .ty_vars .iter() .map(|tv| { let tv = *cx.ty_vars.get(&tv.val).unwrap(); st.subst.remove_bound(&tv); tv }) .collect(), ty, overload: None, }, val_env: ValEnv::new(), equality, }; st.tys.insert(sym, info); } Ok(ty_env.into()) } /// SML Definition (17), SML Definition (71). The checking for {datatype, constructor} {bindings, /// descriptions} appear to be essentially identical, so we can unite the ASTs and static checking /// functions (i.e. this function). pub fn ck_dat_binds(mut cx: Cx, st: &mut State, dat_binds: &[DatBind<StrRef>]) -> Result<Env> { // these two are across all `DatBind`s. let mut ty_env = TyEnv::default(); let mut val_env = ValEnv::new(); // we must first generate new symbols for _all_ the types being defined, since they are allowed to // reference each other. (apparently? according to SML NJ, but it seems like the Definition does // not indicate this, according to my reading of e.g. SML Definition (28).) let mut syms = Vec::new(); for dat_bind in dat_binds { // create a new symbol for the type being generated with this `DatBind`. let sym = st.new_sym(dat_bind.ty_con); // tell the original context as well as the overall `TyEnv` that we return that this new // datatype does exist, but tell the State that it has just an empty `ValEnv`. also perform dupe // checking on the name of the new type and assert for sanity checking after the dupe check. env_ins(&mut ty_env.inner, dat_bind.ty_con, sym, Item::Ty)?; // no assert is_none since we may be shadowing something from an earlier Dec in this Cx. cx.env.ty_env.inner.insert(dat_bind.ty_con.val, sym); // no mapping from ast ty vars to statics ty vars here. we just need some ty vars to make the // `TyScheme`. pretty much copied from `insert_ty_vars`. let mut set = HashSet::new(); let mut ty_vars = Vec::new(); for tv in dat_bind.ty_vars.iter() { if !set.insert(tv.val.name) { return Err(tv.loc.wrap(Error::Duplicate(Item::TyVar, tv.val.name))); } let new_tv = st.new_ty_var(tv.val.equality); ty_vars.push(new_tv); // no need to `insert_bound` because no unifying occurs. } let ty_args: Vec<_> = ty_vars.iter().copied().map(Ty::Var).collect(); let ty_fcn = TyScheme { ty_vars, ty: Ty::Ctor(ty_args, sym), overload: None, }; st.tys.insert_datatype(sym, ty_fcn); syms.push(sym); } // SML Definition (28), SML Definition (81) for (dat_bind, sym) in dat_binds.iter().zip(syms) { // note that we have to `get` here and then `get_mut` again later because of the borrow checker. let ty_fcn = &st.tys.get(&sym).ty_fcn; let mut cx_cl; let cx = if dat_bind.ty_vars.is_empty() { &cx } else { // it is here that we introduce the mapping from ast ty vars to statics ty vars. we need to do // that in order to check the `ConBind`s. but we cannot introduce the mapping earlier, when we // were generating the statics ty vars and the `Sym`s, because there may be multiple // identically-named ty vars in different `DatBind`s. // // if we wanted we could generate new statics type variables here, but then we'd have to use // those new type variables in the return type of the ctor. it shouldn't matter whether we // generate new type variables here or not (as mentioned, we choose to not) because both the // type function and the ctors of the type will each have a `TyScheme` that binds the type // variables appropriately, so by the magic of alpha conversion they're all distinct anyway. cx_cl = cx.clone(); assert_eq!(dat_bind.ty_vars.len(), ty_fcn.ty_vars.len()); for (ast_tv, &tv) in dat_bind.ty_vars.iter().zip(ty_fcn.ty_vars.iter()) { cx_cl.ty_vars.insert(ast_tv.val, tv); } &cx_cl }; // this ValEnv is specific to this `DatBind`. let mut bind_val_env = ValEnv::new(); let mut equality = true; // SML Definition (29), SML Definition (82) for con_bind in dat_bind.cons.iter() { ck_binding(con_bind.vid)?; // if there is no `of t`, then the type of the ctor is just `T`, where `T` is the new sym type // that is being defined. let mut ty = ty_fcn.ty.clone(); if let Some(arg_ty) = &con_bind.ty { // if there is an `of t`, then the type of the ctor is `t -> T`. we must also update whether // `T` respects equality based on whether `t` does. TODO this doesn't handle the equality // check correctly. let t = ty::ck(cx, &st.tys, arg_ty)?; equality = equality && t.is_equality(&st.tys); ty = Ty::Arrow(t.into(), ty.into()); } let val_info = ValInfo::ctor(TyScheme { ty_vars: ty_fcn.ty_vars.clone(), ty, overload: None, }); // insert the `ValInfo` into the _overall_ `ValEnv` with dupe checking. env_ins(&mut val_env, con_bind.vid, val_info.clone(), Item::Val)?; // _also_ insert the `ValInfo` into the `DatBind`-specific `ValEnv`, but this time dupe // checking is unnecessary (just assert as a sanity check). assert!(bind_val_env.insert(con_bind.vid.val, val_info).is_none()); } // now the `ValEnv` is complete, so we may update `st.tys` with the true definition of this // datatype. st.tys.finish_datatype(&sym, bind_val_env, equality); } Ok(Env { ty_env, val_env, str_env: StrEnv::new(), }) } /// SML Definition (18), SML Definition (72) pub fn ck_dat_copy( cx: &Cx, tys: &Tys, ty_con: Located<StrRef>, long: &Long<StrRef>, ) -> Result<Env> { let sym = get_ty_sym(get_env(&cx.env, long)?, long.last)?; let val_env = tys.get(&sym).val_env.clone(); if val_env.is_empty() { return Err(long.loc().wrap(Error::DatatypeCopyNotDatatype)); } Ok(Env { str_env: StrEnv::new(), ty_env: TyEnv { inner: BTreeMap::from([(ty_con.val, sym)]), }, val_env, }) }
ck_cases
identifier_name
gittrack_controller.go
/* Copyright 2018 Pusher Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package gittrack import ( "context" "fmt" "strings" "sync" "time" "github.com/go-logr/logr" farosv1alpha1 "github.com/pusher/faros/pkg/apis/faros/v1alpha1" farosflags "github.com/pusher/faros/pkg/flags" utils "github.com/pusher/faros/pkg/utils" farosclient "github.com/pusher/faros/pkg/utils/client" gitstore "github.com/pusher/git-store" apiv1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" rlogr "sigs.k8s.io/controller-runtime/pkg/runtime/log" "sigs.k8s.io/controller-runtime/pkg/source" ) // Add creates a new GitTrack Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller // and Start it when the Manager is Started. // USER ACTION REQUIRED: update cmd/manager/main.go to call this faros.Add(mgr) to install this Controller func Add(mgr manager.Manager) error { recFn, opts := newReconciler(mgr) return add(mgr, recFn, opts) } // newReconciler returns a new reconcile.Reconciler func newReconciler(mgr manager.Manager) (reconcile.Reconciler, *reconcileGitTrackOpts) { // Create a restMapper (used by informer to look up resource kinds) restMapper, err := utils.NewRestMapper(mgr.GetConfig()) if err != nil { panic(fmt.Errorf("unable to create rest mapper: %v", err)) } gvrs, err := farosflags.ParseIgnoredResources() if err != nil { panic(fmt.Errorf("unable to parse ignored resources: %v", err)) } applier, err := farosclient.NewApplier(mgr.GetConfig(), farosclient.Options{}) if err != nil { panic(fmt.Errorf("unable to create applier: %v", err)) } rec := &ReconcileGitTrack{ Client: mgr.GetClient(), scheme: mgr.GetScheme(), store: gitstore.NewRepoStore(farosflags.RepositoryDir), restMapper: restMapper, recorder: mgr.GetEventRecorderFor("gittrack-controller"), ignoredGVRs: gvrs, lastUpdateTimes: make(map[string]time.Time), mutex: &sync.RWMutex{}, applier: applier, log: rlogr.Log.WithName("gittrack-controller"), gitTrackMode: farosflags.GitTrack, namespace: farosflags.Namespace, clusterGitTrackMode: farosflags.ClusterGitTrack, } opts := &reconcileGitTrackOpts{ gitTrackMode: farosflags.GitTrack, clusterGitTrackMode: farosflags.ClusterGitTrack, } return rec, opts } // reconcileGitTrackOpts is the mode that we're running the reconciler // in. Being able to change these options during runtime is helpful during tests. type reconcileGitTrackOpts struct { clusterGitTrackMode farosflags.ClusterGitTrackMode gitTrackMode farosflags.GitTrackMode } // add adds a new Controller to mgr with r as the reconcile.Reconciler func add(mgr manager.Manager, r reconcile.Reconciler, opts *reconcileGitTrackOpts) error { // Create a new controller c, err := controller.New("gittrack-controller", mgr, controller.Options{Reconciler: r}) if err != nil { return err } if opts.gitTrackMode == farosflags.GTMEnabled { // Watch for changes to GitTrack err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrack{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err } err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.GitTrack{}, }) if err != nil { return err } } if opts.clusterGitTrackMode != farosflags.CGTMDisabled { // Watch for changes to ClusterGitTrack err = c.Watch(&source.Kind{Type: &farosv1alpha1.ClusterGitTrack{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err } err = c.Watch(&source.Kind{Type: &farosv1alpha1.ClusterGitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.ClusterGitTrack{}, }) if err != nil { return err } if opts.clusterGitTrackMode == farosflags.CGTMIncludeNamespaced { err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.ClusterGitTrack{}, }) if err != nil { return err } } } return nil } var _ reconcile.Reconciler = &ReconcileGitTrack{} // ReconcileGitTrack reconciles a GitTrack object type ReconcileGitTrack struct { client.Client scheme *runtime.Scheme store *gitstore.RepoStore restMapper meta.RESTMapper recorder record.EventRecorder ignoredGVRs map[schema.GroupVersionResource]interface{} lastUpdateTimes map[string]time.Time mutex *sync.RWMutex applier farosclient.Client log logr.Logger gitTrackMode farosflags.GitTrackMode namespace string clusterGitTrackMode farosflags.ClusterGitTrackMode } func (r *ReconcileGitTrack) withValues(keysAndValues ...interface{}) *ReconcileGitTrack { reconciler := *r reconciler.log = r.log.WithValues(keysAndValues...) return &reconciler } // fetchInstance attempts to fetch the GitTrack resource by the name in the given Request func (r *ReconcileGitTrack) fetchInstance(req reconcile.Request) (farosv1alpha1.GitTrackInterface, error) { var instance farosv1alpha1.GitTrackInterface if req.Namespace != "" { instance = &farosv1alpha1.GitTrack{} } else { instance = &farosv1alpha1.ClusterGitTrack{} } err := r.Get(context.TODO(), req.NamespacedName, instance) if err != nil { if errors.IsNotFound(err) { // Object not found, return. Created objects are automatically garbage collected. // For additional cleanup logic use finalizers. return nil, nil } // Error reading the object - requeue the request. return nil, err } return instance, nil } // listObjectsByName lists and filters GitTrackObjects by the `faros.pusher.com/owned-by` label, // and returns a map of names to GitTrackObject mappings func (r *ReconcileGitTrack) listObjectsByName(owner farosv1alpha1.GitTrackInterface) (map[string]farosv1alpha1.GitTrackObjectInterface, error) { result := make(map[string]farosv1alpha1.GitTrackObjectInterface) gtos := &farosv1alpha1.GitTrackObjectList{} err := r.List(context.TODO(), gtos) if err != nil { return nil, err } for _, gto := range gtos.Items { if metav1.IsControlledBy(&gto, owner) { result[gto.GetNamespacedName()] = gto.DeepCopy() } } cgtos := &farosv1alpha1.ClusterGitTrackObjectList{} err = r.List(context.TODO(), cgtos) if err != nil { return nil, err } for _, cgto := range cgtos.Items { if metav1.IsControlledBy(&cgto, owner) { result[cgto.GetNamespacedName()] = cgto.DeepCopy() } } return result, nil } // objectResult represents the result of creating or updating a GitTrackObject type objectResult struct { NamespacedName string Error error Ignored bool Reason string InSync bool TimeToDeploy time.Duration } // errorResult is a convenience function for creating an error result func errorResult(namespacedName string, err error) objectResult { return objectResult{NamespacedName: namespacedName, Error: err, Ignored: true} }
} // successResult is a convenience function for creating a success objectResult func successResult(namespacedName string, timeToDeploy time.Duration, inSync bool) objectResult { return objectResult{NamespacedName: namespacedName, TimeToDeploy: timeToDeploy, InSync: inSync} } func (r *ReconcileGitTrack) newGitTrackObjectInterface(name string, u *unstructured.Unstructured) (farosv1alpha1.GitTrackObjectInterface, error) { var instance farosv1alpha1.GitTrackObjectInterface _, namespaced, err := utils.GetAPIResource(r.restMapper, u.GetObjectKind().GroupVersionKind()) if err != nil { return nil, fmt.Errorf("error getting API resource: %v", err) } if namespaced { instance = &farosv1alpha1.GitTrackObject{ TypeMeta: farosv1alpha1.GitTrackObjectTypeMeta, } } else { instance = &farosv1alpha1.ClusterGitTrackObject{ TypeMeta: farosv1alpha1.ClusterGitTrackObjectTypeMeta, } } instance.SetName(name) instance.SetNamespace(u.GetNamespace()) data, err := u.MarshalJSON() if err != nil { return nil, fmt.Errorf("error marshalling JSON: %v", err) } instance.SetSpec(farosv1alpha1.GitTrackObjectSpec{ Name: u.GetName(), Kind: u.GetKind(), Data: data, }) return instance, nil } // objectName constructs a name from an Unstructured object func objectName(u *unstructured.Unstructured) string { return strings.ToLower(fmt.Sprintf("%s-%s", u.GetKind(), strings.Replace(u.GetName(), ":", "-", -1))) } // handleObject either creates or updates a GitTrackObject func (r *ReconcileGitTrack) handleObject(u *unstructured.Unstructured, owner farosv1alpha1.GitTrackInterface) objectResult { name := objectName(u) gto, err := r.newGitTrackObjectInterface(name, u) if err != nil { namespacedName := strings.TrimLeft(fmt.Sprintf("%s/%s", u.GetNamespace(), name), "/") return errorResult(namespacedName, err) } ignored, reason, err := r.ignoreObject(u, owner) if err != nil { return errorResult(gto.GetNamespacedName(), err) } if ignored { return ignoreResult(gto.GetNamespacedName(), reason) } r.mutex.RLock() timeToDeploy := time.Now().Sub(r.lastUpdateTimes[owner.GetSpec().Repository]) r.mutex.RUnlock() if err = controllerutil.SetControllerReference(owner, gto, r.scheme); err != nil { return errorResult(gto.GetNamespacedName(), err) } found := gto.DeepCopyInterface() err = r.Get(context.TODO(), types.NamespacedName{Name: gto.GetName(), Namespace: gto.GetNamespace()}, found) if err != nil && errors.IsNotFound(err) { return r.createChild(name, timeToDeploy, owner, found, gto) } else if err != nil { return errorResult(gto.GetNamespacedName(), fmt.Errorf("failed to get child for '%s': %v", name, err)) } err = checkOwner(owner, found, r.scheme) if err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "ControllerMismatch", "Child '%s' is owned by another controller: %v", name, err) return ignoreResult(gto.GetNamespacedName(), "child is owned by another controller") } inSync := childInSync(found) childUpdated, err := r.updateChild(found, gto) if err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "UpdateFailed", "Failed to update child '%s'", name) return errorResult(gto.GetNamespacedName(), fmt.Errorf("failed to update child resource: %v", err)) } if childUpdated { inSync = false r.log.V(0).Info("Child updated", "child name", name) r.recorder.Eventf(owner, apiv1.EventTypeNormal, "UpdateSuccessful", "Updated child '%s'", name) } return successResult(gto.GetNamespacedName(), timeToDeploy, inSync) } func childInSync(child farosv1alpha1.GitTrackObjectInterface) bool { for _, condition := range child.GetStatus().Conditions { if condition.Type == farosv1alpha1.ObjectInSyncType && condition.Status == apiv1.ConditionTrue { return true } } return false } func (r *ReconcileGitTrack) createChild(name string, timeToDeploy time.Duration, owner farosv1alpha1.GitTrackInterface, foundGTO, childGTO farosv1alpha1.GitTrackObjectInterface) objectResult { r.recorder.Eventf(owner, apiv1.EventTypeNormal, "CreateStarted", "Creating child '%s'", name) if err := r.applier.Apply(context.TODO(), &farosclient.ApplyOptions{}, childGTO); err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "CreateFailed", "Failed to create child '%s'", name) return errorResult(childGTO.GetNamespacedName(), fmt.Errorf("failed to create child for '%s': %v", name, err)) } r.recorder.Eventf(owner, apiv1.EventTypeNormal, "CreateSuccessful", "Created child '%s'", name) r.log.V(0).Info("Child created", "child name", name) return successResult(childGTO.GetNamespacedName(), timeToDeploy, false) } // UpdateChild compares the two GitTrackObjects and updates the foundGTO if the // childGTO func (r *ReconcileGitTrack) updateChild(foundGTO, childGTO farosv1alpha1.GitTrackObjectInterface) (bool, error) { originalResourceVersion := foundGTO.GetResourceVersion() err := r.applier.Apply(context.TODO(), &farosclient.ApplyOptions{}, childGTO) if err != nil { return false, fmt.Errorf("error updating child resource: %v", err) } // Not updated if the resource version hasn't changed if originalResourceVersion == childGTO.GetResourceVersion() { return false, nil } return true, nil } // deleteResources deletes any resources that are present in the given map func (r *ReconcileGitTrack) deleteResources(leftovers map[string]farosv1alpha1.GitTrackObjectInterface) error { if len(leftovers) > 0 { r.log.V(0).Info("Found leftover resources to clean up", "leftover resources", string(len(leftovers))) } for name, obj := range leftovers { if err := r.Delete(context.TODO(), obj); err != nil { return fmt.Errorf("failed to delete child for '%s': '%s'", name, err) } r.log.V(0).Info("Child deleted", "child name", name) } return nil } // checkOwner checks the owner reference of an object from the API to see if it // is owned by the current GitTrack. func checkOwner(owner farosv1alpha1.GitTrackInterface, child farosv1alpha1.GitTrackObjectInterface, s *runtime.Scheme) error { gvk, err := apiutil.GVKForObject(owner, s) if err != nil { return err } for _, ref := range child.GetOwnerReferences() { if ref.Kind == gvk.Kind && ref.UID != owner.GetUID() { return fmt.Errorf("child object is owned by '%s'", ref.Name) } } return nil } // ignoreObject checks whether the unstructured object should be ignored func (r *ReconcileGitTrack) ignoreObject(u *unstructured.Unstructured, owner farosv1alpha1.GitTrackInterface) (bool, string, error) { gvr, namespaced, err := utils.GetAPIResource(r.restMapper, u.GetObjectKind().GroupVersionKind()) if err != nil { return false, "", err } // Ignore namespaced objects not in the namespace managed by the controller if namespaced && r.namespace != "" && r.namespace != u.GetNamespace() { r.log.V(1).Info("Object not in namespace", "object namespace", u.GetNamespace(), "managed namespace", r.namespace) return true, fmt.Sprintf("namespace `%s` is not managed by this Faros", u.GetNamespace()), nil } // Ignore GVKs in the ignoredGVKs set if _, ok := r.ignoredGVRs[gvr]; ok { r.log.V(1).Info("Object group version ignored globally", "group version resource", gvr.String()) return true, fmt.Sprintf("resource `%s.%s/%s` ignored globally by flag", gvr.Resource, gvr.Group, gvr.Version), nil } ownerNamespace := owner.GetNamespace() _, ownerIsGittrack := owner.(*farosv1alpha1.GitTrack) _, ownerIsClusterGittrack := owner.(*farosv1alpha1.ClusterGitTrack) if namespaced { // prevent a gittrack in a namespace from handling objects which are in a different namespace if ownerIsGittrack && ownerNamespace != u.GetNamespace() { return true, fmt.Sprintf("namespace `%s` is not managed by this GitTrack", u.GetNamespace()), nil } else if ownerIsClusterGittrack && r.clusterGitTrackMode == farosflags.CGTMExcludeNamespaced { return true, "namespaced resources cannot be managed by ClusterGitTrack", nil } } else if ownerIsGittrack { // cluster scoped object managed from namespaced gittrack. Disallow return true, "a GitTrack cannot manage a cluster-scoped resource", nil } return false, "", nil } // Reconcile reads the state of the cluster for a GitTrack object and makes changes based on the state read // and what is in the GitTrack.Spec // Automatically generate RBAC rules to allow the Controller to read and write Deployments // +kubebuilder:rbac:groups=faros.pusher.com,resources=gittracks,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=faros.pusher.com,resources=gittrackobjects,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=faros.pusher.com,resources=clustergittrackobjects,verbs=get;list;watch;create;update;patch;delete func (r *ReconcileGitTrack) Reconcile(request reconcile.Request) (reconcile.Result, error) { instance, err := r.fetchInstance(request) if err != nil || instance == nil { return reconcile.Result{}, err } reconciler := r.withValues( "namespace", instance.GetNamespace(), "name", instance.GetName(), ) reconciler.log.V(1).Info("Reconcile started") defer reconciler.log.V(1).Info("Reconcile finished") result := reconciler.handleGitTrack(instance) var errs []error for _, err := range []error{result.parseError, result.gitError, result.gcError, result.upToDateError} { if err != nil { errs = append(errs, err) } } err = reconciler.updateStatus(instance, result.asStatusOpts()) if err != nil { errs = append(errs, fmt.Errorf("error updating status: %v", err)) } err = reconciler.updateMetrics(instance, result.asMetricOpts(instance.GetSpec().Repository)) if err != nil { errs = append(errs, fmt.Errorf("error updating metrics: %v", err)) } if len(errs) > 0 { for _, err := range errs { reconciler.log.Error(err, "error during reconcile") } return reconcile.Result{}, fmt.Errorf("errors encountered during reconcile") } return reconcile.Result{}, nil }
// ignoreResult is a convenience function for creating an ignore objectResult func ignoreResult(namespacedName string, reason string) objectResult { return objectResult{NamespacedName: namespacedName, Ignored: true, Reason: reason}
random_line_split
gittrack_controller.go
/* Copyright 2018 Pusher Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package gittrack import ( "context" "fmt" "strings" "sync" "time" "github.com/go-logr/logr" farosv1alpha1 "github.com/pusher/faros/pkg/apis/faros/v1alpha1" farosflags "github.com/pusher/faros/pkg/flags" utils "github.com/pusher/faros/pkg/utils" farosclient "github.com/pusher/faros/pkg/utils/client" gitstore "github.com/pusher/git-store" apiv1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" rlogr "sigs.k8s.io/controller-runtime/pkg/runtime/log" "sigs.k8s.io/controller-runtime/pkg/source" ) // Add creates a new GitTrack Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller // and Start it when the Manager is Started. // USER ACTION REQUIRED: update cmd/manager/main.go to call this faros.Add(mgr) to install this Controller func Add(mgr manager.Manager) error { recFn, opts := newReconciler(mgr) return add(mgr, recFn, opts) } // newReconciler returns a new reconcile.Reconciler func newReconciler(mgr manager.Manager) (reconcile.Reconciler, *reconcileGitTrackOpts) { // Create a restMapper (used by informer to look up resource kinds) restMapper, err := utils.NewRestMapper(mgr.GetConfig()) if err != nil { panic(fmt.Errorf("unable to create rest mapper: %v", err)) } gvrs, err := farosflags.ParseIgnoredResources() if err != nil
applier, err := farosclient.NewApplier(mgr.GetConfig(), farosclient.Options{}) if err != nil { panic(fmt.Errorf("unable to create applier: %v", err)) } rec := &ReconcileGitTrack{ Client: mgr.GetClient(), scheme: mgr.GetScheme(), store: gitstore.NewRepoStore(farosflags.RepositoryDir), restMapper: restMapper, recorder: mgr.GetEventRecorderFor("gittrack-controller"), ignoredGVRs: gvrs, lastUpdateTimes: make(map[string]time.Time), mutex: &sync.RWMutex{}, applier: applier, log: rlogr.Log.WithName("gittrack-controller"), gitTrackMode: farosflags.GitTrack, namespace: farosflags.Namespace, clusterGitTrackMode: farosflags.ClusterGitTrack, } opts := &reconcileGitTrackOpts{ gitTrackMode: farosflags.GitTrack, clusterGitTrackMode: farosflags.ClusterGitTrack, } return rec, opts } // reconcileGitTrackOpts is the mode that we're running the reconciler // in. Being able to change these options during runtime is helpful during tests. type reconcileGitTrackOpts struct { clusterGitTrackMode farosflags.ClusterGitTrackMode gitTrackMode farosflags.GitTrackMode } // add adds a new Controller to mgr with r as the reconcile.Reconciler func add(mgr manager.Manager, r reconcile.Reconciler, opts *reconcileGitTrackOpts) error { // Create a new controller c, err := controller.New("gittrack-controller", mgr, controller.Options{Reconciler: r}) if err != nil { return err } if opts.gitTrackMode == farosflags.GTMEnabled { // Watch for changes to GitTrack err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrack{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err } err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.GitTrack{}, }) if err != nil { return err } } if opts.clusterGitTrackMode != farosflags.CGTMDisabled { // Watch for changes to ClusterGitTrack err = c.Watch(&source.Kind{Type: &farosv1alpha1.ClusterGitTrack{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err } err = c.Watch(&source.Kind{Type: &farosv1alpha1.ClusterGitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.ClusterGitTrack{}, }) if err != nil { return err } if opts.clusterGitTrackMode == farosflags.CGTMIncludeNamespaced { err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.ClusterGitTrack{}, }) if err != nil { return err } } } return nil } var _ reconcile.Reconciler = &ReconcileGitTrack{} // ReconcileGitTrack reconciles a GitTrack object type ReconcileGitTrack struct { client.Client scheme *runtime.Scheme store *gitstore.RepoStore restMapper meta.RESTMapper recorder record.EventRecorder ignoredGVRs map[schema.GroupVersionResource]interface{} lastUpdateTimes map[string]time.Time mutex *sync.RWMutex applier farosclient.Client log logr.Logger gitTrackMode farosflags.GitTrackMode namespace string clusterGitTrackMode farosflags.ClusterGitTrackMode } func (r *ReconcileGitTrack) withValues(keysAndValues ...interface{}) *ReconcileGitTrack { reconciler := *r reconciler.log = r.log.WithValues(keysAndValues...) return &reconciler } // fetchInstance attempts to fetch the GitTrack resource by the name in the given Request func (r *ReconcileGitTrack) fetchInstance(req reconcile.Request) (farosv1alpha1.GitTrackInterface, error) { var instance farosv1alpha1.GitTrackInterface if req.Namespace != "" { instance = &farosv1alpha1.GitTrack{} } else { instance = &farosv1alpha1.ClusterGitTrack{} } err := r.Get(context.TODO(), req.NamespacedName, instance) if err != nil { if errors.IsNotFound(err) { // Object not found, return. Created objects are automatically garbage collected. // For additional cleanup logic use finalizers. return nil, nil } // Error reading the object - requeue the request. return nil, err } return instance, nil } // listObjectsByName lists and filters GitTrackObjects by the `faros.pusher.com/owned-by` label, // and returns a map of names to GitTrackObject mappings func (r *ReconcileGitTrack) listObjectsByName(owner farosv1alpha1.GitTrackInterface) (map[string]farosv1alpha1.GitTrackObjectInterface, error) { result := make(map[string]farosv1alpha1.GitTrackObjectInterface) gtos := &farosv1alpha1.GitTrackObjectList{} err := r.List(context.TODO(), gtos) if err != nil { return nil, err } for _, gto := range gtos.Items { if metav1.IsControlledBy(&gto, owner) { result[gto.GetNamespacedName()] = gto.DeepCopy() } } cgtos := &farosv1alpha1.ClusterGitTrackObjectList{} err = r.List(context.TODO(), cgtos) if err != nil { return nil, err } for _, cgto := range cgtos.Items { if metav1.IsControlledBy(&cgto, owner) { result[cgto.GetNamespacedName()] = cgto.DeepCopy() } } return result, nil } // objectResult represents the result of creating or updating a GitTrackObject type objectResult struct { NamespacedName string Error error Ignored bool Reason string InSync bool TimeToDeploy time.Duration } // errorResult is a convenience function for creating an error result func errorResult(namespacedName string, err error) objectResult { return objectResult{NamespacedName: namespacedName, Error: err, Ignored: true} } // ignoreResult is a convenience function for creating an ignore objectResult func ignoreResult(namespacedName string, reason string) objectResult { return objectResult{NamespacedName: namespacedName, Ignored: true, Reason: reason} } // successResult is a convenience function for creating a success objectResult func successResult(namespacedName string, timeToDeploy time.Duration, inSync bool) objectResult { return objectResult{NamespacedName: namespacedName, TimeToDeploy: timeToDeploy, InSync: inSync} } func (r *ReconcileGitTrack) newGitTrackObjectInterface(name string, u *unstructured.Unstructured) (farosv1alpha1.GitTrackObjectInterface, error) { var instance farosv1alpha1.GitTrackObjectInterface _, namespaced, err := utils.GetAPIResource(r.restMapper, u.GetObjectKind().GroupVersionKind()) if err != nil { return nil, fmt.Errorf("error getting API resource: %v", err) } if namespaced { instance = &farosv1alpha1.GitTrackObject{ TypeMeta: farosv1alpha1.GitTrackObjectTypeMeta, } } else { instance = &farosv1alpha1.ClusterGitTrackObject{ TypeMeta: farosv1alpha1.ClusterGitTrackObjectTypeMeta, } } instance.SetName(name) instance.SetNamespace(u.GetNamespace()) data, err := u.MarshalJSON() if err != nil { return nil, fmt.Errorf("error marshalling JSON: %v", err) } instance.SetSpec(farosv1alpha1.GitTrackObjectSpec{ Name: u.GetName(), Kind: u.GetKind(), Data: data, }) return instance, nil } // objectName constructs a name from an Unstructured object func objectName(u *unstructured.Unstructured) string { return strings.ToLower(fmt.Sprintf("%s-%s", u.GetKind(), strings.Replace(u.GetName(), ":", "-", -1))) } // handleObject either creates or updates a GitTrackObject func (r *ReconcileGitTrack) handleObject(u *unstructured.Unstructured, owner farosv1alpha1.GitTrackInterface) objectResult { name := objectName(u) gto, err := r.newGitTrackObjectInterface(name, u) if err != nil { namespacedName := strings.TrimLeft(fmt.Sprintf("%s/%s", u.GetNamespace(), name), "/") return errorResult(namespacedName, err) } ignored, reason, err := r.ignoreObject(u, owner) if err != nil { return errorResult(gto.GetNamespacedName(), err) } if ignored { return ignoreResult(gto.GetNamespacedName(), reason) } r.mutex.RLock() timeToDeploy := time.Now().Sub(r.lastUpdateTimes[owner.GetSpec().Repository]) r.mutex.RUnlock() if err = controllerutil.SetControllerReference(owner, gto, r.scheme); err != nil { return errorResult(gto.GetNamespacedName(), err) } found := gto.DeepCopyInterface() err = r.Get(context.TODO(), types.NamespacedName{Name: gto.GetName(), Namespace: gto.GetNamespace()}, found) if err != nil && errors.IsNotFound(err) { return r.createChild(name, timeToDeploy, owner, found, gto) } else if err != nil { return errorResult(gto.GetNamespacedName(), fmt.Errorf("failed to get child for '%s': %v", name, err)) } err = checkOwner(owner, found, r.scheme) if err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "ControllerMismatch", "Child '%s' is owned by another controller: %v", name, err) return ignoreResult(gto.GetNamespacedName(), "child is owned by another controller") } inSync := childInSync(found) childUpdated, err := r.updateChild(found, gto) if err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "UpdateFailed", "Failed to update child '%s'", name) return errorResult(gto.GetNamespacedName(), fmt.Errorf("failed to update child resource: %v", err)) } if childUpdated { inSync = false r.log.V(0).Info("Child updated", "child name", name) r.recorder.Eventf(owner, apiv1.EventTypeNormal, "UpdateSuccessful", "Updated child '%s'", name) } return successResult(gto.GetNamespacedName(), timeToDeploy, inSync) } func childInSync(child farosv1alpha1.GitTrackObjectInterface) bool { for _, condition := range child.GetStatus().Conditions { if condition.Type == farosv1alpha1.ObjectInSyncType && condition.Status == apiv1.ConditionTrue { return true } } return false } func (r *ReconcileGitTrack) createChild(name string, timeToDeploy time.Duration, owner farosv1alpha1.GitTrackInterface, foundGTO, childGTO farosv1alpha1.GitTrackObjectInterface) objectResult { r.recorder.Eventf(owner, apiv1.EventTypeNormal, "CreateStarted", "Creating child '%s'", name) if err := r.applier.Apply(context.TODO(), &farosclient.ApplyOptions{}, childGTO); err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "CreateFailed", "Failed to create child '%s'", name) return errorResult(childGTO.GetNamespacedName(), fmt.Errorf("failed to create child for '%s': %v", name, err)) } r.recorder.Eventf(owner, apiv1.EventTypeNormal, "CreateSuccessful", "Created child '%s'", name) r.log.V(0).Info("Child created", "child name", name) return successResult(childGTO.GetNamespacedName(), timeToDeploy, false) } // UpdateChild compares the two GitTrackObjects and updates the foundGTO if the // childGTO func (r *ReconcileGitTrack) updateChild(foundGTO, childGTO farosv1alpha1.GitTrackObjectInterface) (bool, error) { originalResourceVersion := foundGTO.GetResourceVersion() err := r.applier.Apply(context.TODO(), &farosclient.ApplyOptions{}, childGTO) if err != nil { return false, fmt.Errorf("error updating child resource: %v", err) } // Not updated if the resource version hasn't changed if originalResourceVersion == childGTO.GetResourceVersion() { return false, nil } return true, nil } // deleteResources deletes any resources that are present in the given map func (r *ReconcileGitTrack) deleteResources(leftovers map[string]farosv1alpha1.GitTrackObjectInterface) error { if len(leftovers) > 0 { r.log.V(0).Info("Found leftover resources to clean up", "leftover resources", string(len(leftovers))) } for name, obj := range leftovers { if err := r.Delete(context.TODO(), obj); err != nil { return fmt.Errorf("failed to delete child for '%s': '%s'", name, err) } r.log.V(0).Info("Child deleted", "child name", name) } return nil } // checkOwner checks the owner reference of an object from the API to see if it // is owned by the current GitTrack. func checkOwner(owner farosv1alpha1.GitTrackInterface, child farosv1alpha1.GitTrackObjectInterface, s *runtime.Scheme) error { gvk, err := apiutil.GVKForObject(owner, s) if err != nil { return err } for _, ref := range child.GetOwnerReferences() { if ref.Kind == gvk.Kind && ref.UID != owner.GetUID() { return fmt.Errorf("child object is owned by '%s'", ref.Name) } } return nil } // ignoreObject checks whether the unstructured object should be ignored func (r *ReconcileGitTrack) ignoreObject(u *unstructured.Unstructured, owner farosv1alpha1.GitTrackInterface) (bool, string, error) { gvr, namespaced, err := utils.GetAPIResource(r.restMapper, u.GetObjectKind().GroupVersionKind()) if err != nil { return false, "", err } // Ignore namespaced objects not in the namespace managed by the controller if namespaced && r.namespace != "" && r.namespace != u.GetNamespace() { r.log.V(1).Info("Object not in namespace", "object namespace", u.GetNamespace(), "managed namespace", r.namespace) return true, fmt.Sprintf("namespace `%s` is not managed by this Faros", u.GetNamespace()), nil } // Ignore GVKs in the ignoredGVKs set if _, ok := r.ignoredGVRs[gvr]; ok { r.log.V(1).Info("Object group version ignored globally", "group version resource", gvr.String()) return true, fmt.Sprintf("resource `%s.%s/%s` ignored globally by flag", gvr.Resource, gvr.Group, gvr.Version), nil } ownerNamespace := owner.GetNamespace() _, ownerIsGittrack := owner.(*farosv1alpha1.GitTrack) _, ownerIsClusterGittrack := owner.(*farosv1alpha1.ClusterGitTrack) if namespaced { // prevent a gittrack in a namespace from handling objects which are in a different namespace if ownerIsGittrack && ownerNamespace != u.GetNamespace() { return true, fmt.Sprintf("namespace `%s` is not managed by this GitTrack", u.GetNamespace()), nil } else if ownerIsClusterGittrack && r.clusterGitTrackMode == farosflags.CGTMExcludeNamespaced { return true, "namespaced resources cannot be managed by ClusterGitTrack", nil } } else if ownerIsGittrack { // cluster scoped object managed from namespaced gittrack. Disallow return true, "a GitTrack cannot manage a cluster-scoped resource", nil } return false, "", nil } // Reconcile reads the state of the cluster for a GitTrack object and makes changes based on the state read // and what is in the GitTrack.Spec // Automatically generate RBAC rules to allow the Controller to read and write Deployments // +kubebuilder:rbac:groups=faros.pusher.com,resources=gittracks,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=faros.pusher.com,resources=gittrackobjects,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=faros.pusher.com,resources=clustergittrackobjects,verbs=get;list;watch;create;update;patch;delete func (r *ReconcileGitTrack) Reconcile(request reconcile.Request) (reconcile.Result, error) { instance, err := r.fetchInstance(request) if err != nil || instance == nil { return reconcile.Result{}, err } reconciler := r.withValues( "namespace", instance.GetNamespace(), "name", instance.GetName(), ) reconciler.log.V(1).Info("Reconcile started") defer reconciler.log.V(1).Info("Reconcile finished") result := reconciler.handleGitTrack(instance) var errs []error for _, err := range []error{result.parseError, result.gitError, result.gcError, result.upToDateError} { if err != nil { errs = append(errs, err) } } err = reconciler.updateStatus(instance, result.asStatusOpts()) if err != nil { errs = append(errs, fmt.Errorf("error updating status: %v", err)) } err = reconciler.updateMetrics(instance, result.asMetricOpts(instance.GetSpec().Repository)) if err != nil { errs = append(errs, fmt.Errorf("error updating metrics: %v", err)) } if len(errs) > 0 { for _, err := range errs { reconciler.log.Error(err, "error during reconcile") } return reconcile.Result{}, fmt.Errorf("errors encountered during reconcile") } return reconcile.Result{}, nil }
{ panic(fmt.Errorf("unable to parse ignored resources: %v", err)) }
conditional_block
gittrack_controller.go
/* Copyright 2018 Pusher Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package gittrack import ( "context" "fmt" "strings" "sync" "time" "github.com/go-logr/logr" farosv1alpha1 "github.com/pusher/faros/pkg/apis/faros/v1alpha1" farosflags "github.com/pusher/faros/pkg/flags" utils "github.com/pusher/faros/pkg/utils" farosclient "github.com/pusher/faros/pkg/utils/client" gitstore "github.com/pusher/git-store" apiv1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" rlogr "sigs.k8s.io/controller-runtime/pkg/runtime/log" "sigs.k8s.io/controller-runtime/pkg/source" ) // Add creates a new GitTrack Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller // and Start it when the Manager is Started. // USER ACTION REQUIRED: update cmd/manager/main.go to call this faros.Add(mgr) to install this Controller func Add(mgr manager.Manager) error { recFn, opts := newReconciler(mgr) return add(mgr, recFn, opts) } // newReconciler returns a new reconcile.Reconciler func newReconciler(mgr manager.Manager) (reconcile.Reconciler, *reconcileGitTrackOpts) { // Create a restMapper (used by informer to look up resource kinds) restMapper, err := utils.NewRestMapper(mgr.GetConfig()) if err != nil { panic(fmt.Errorf("unable to create rest mapper: %v", err)) } gvrs, err := farosflags.ParseIgnoredResources() if err != nil { panic(fmt.Errorf("unable to parse ignored resources: %v", err)) } applier, err := farosclient.NewApplier(mgr.GetConfig(), farosclient.Options{}) if err != nil { panic(fmt.Errorf("unable to create applier: %v", err)) } rec := &ReconcileGitTrack{ Client: mgr.GetClient(), scheme: mgr.GetScheme(), store: gitstore.NewRepoStore(farosflags.RepositoryDir), restMapper: restMapper, recorder: mgr.GetEventRecorderFor("gittrack-controller"), ignoredGVRs: gvrs, lastUpdateTimes: make(map[string]time.Time), mutex: &sync.RWMutex{}, applier: applier, log: rlogr.Log.WithName("gittrack-controller"), gitTrackMode: farosflags.GitTrack, namespace: farosflags.Namespace, clusterGitTrackMode: farosflags.ClusterGitTrack, } opts := &reconcileGitTrackOpts{ gitTrackMode: farosflags.GitTrack, clusterGitTrackMode: farosflags.ClusterGitTrack, } return rec, opts } // reconcileGitTrackOpts is the mode that we're running the reconciler // in. Being able to change these options during runtime is helpful during tests. type reconcileGitTrackOpts struct { clusterGitTrackMode farosflags.ClusterGitTrackMode gitTrackMode farosflags.GitTrackMode } // add adds a new Controller to mgr with r as the reconcile.Reconciler func add(mgr manager.Manager, r reconcile.Reconciler, opts *reconcileGitTrackOpts) error { // Create a new controller c, err := controller.New("gittrack-controller", mgr, controller.Options{Reconciler: r}) if err != nil { return err } if opts.gitTrackMode == farosflags.GTMEnabled { // Watch for changes to GitTrack err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrack{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err } err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.GitTrack{}, }) if err != nil { return err } } if opts.clusterGitTrackMode != farosflags.CGTMDisabled { // Watch for changes to ClusterGitTrack err = c.Watch(&source.Kind{Type: &farosv1alpha1.ClusterGitTrack{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err } err = c.Watch(&source.Kind{Type: &farosv1alpha1.ClusterGitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.ClusterGitTrack{}, }) if err != nil { return err } if opts.clusterGitTrackMode == farosflags.CGTMIncludeNamespaced { err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.ClusterGitTrack{}, }) if err != nil { return err } } } return nil } var _ reconcile.Reconciler = &ReconcileGitTrack{} // ReconcileGitTrack reconciles a GitTrack object type ReconcileGitTrack struct { client.Client scheme *runtime.Scheme store *gitstore.RepoStore restMapper meta.RESTMapper recorder record.EventRecorder ignoredGVRs map[schema.GroupVersionResource]interface{} lastUpdateTimes map[string]time.Time mutex *sync.RWMutex applier farosclient.Client log logr.Logger gitTrackMode farosflags.GitTrackMode namespace string clusterGitTrackMode farosflags.ClusterGitTrackMode } func (r *ReconcileGitTrack) withValues(keysAndValues ...interface{}) *ReconcileGitTrack { reconciler := *r reconciler.log = r.log.WithValues(keysAndValues...) return &reconciler } // fetchInstance attempts to fetch the GitTrack resource by the name in the given Request func (r *ReconcileGitTrack) fetchInstance(req reconcile.Request) (farosv1alpha1.GitTrackInterface, error)
// listObjectsByName lists and filters GitTrackObjects by the `faros.pusher.com/owned-by` label, // and returns a map of names to GitTrackObject mappings func (r *ReconcileGitTrack) listObjectsByName(owner farosv1alpha1.GitTrackInterface) (map[string]farosv1alpha1.GitTrackObjectInterface, error) { result := make(map[string]farosv1alpha1.GitTrackObjectInterface) gtos := &farosv1alpha1.GitTrackObjectList{} err := r.List(context.TODO(), gtos) if err != nil { return nil, err } for _, gto := range gtos.Items { if metav1.IsControlledBy(&gto, owner) { result[gto.GetNamespacedName()] = gto.DeepCopy() } } cgtos := &farosv1alpha1.ClusterGitTrackObjectList{} err = r.List(context.TODO(), cgtos) if err != nil { return nil, err } for _, cgto := range cgtos.Items { if metav1.IsControlledBy(&cgto, owner) { result[cgto.GetNamespacedName()] = cgto.DeepCopy() } } return result, nil } // objectResult represents the result of creating or updating a GitTrackObject type objectResult struct { NamespacedName string Error error Ignored bool Reason string InSync bool TimeToDeploy time.Duration } // errorResult is a convenience function for creating an error result func errorResult(namespacedName string, err error) objectResult { return objectResult{NamespacedName: namespacedName, Error: err, Ignored: true} } // ignoreResult is a convenience function for creating an ignore objectResult func ignoreResult(namespacedName string, reason string) objectResult { return objectResult{NamespacedName: namespacedName, Ignored: true, Reason: reason} } // successResult is a convenience function for creating a success objectResult func successResult(namespacedName string, timeToDeploy time.Duration, inSync bool) objectResult { return objectResult{NamespacedName: namespacedName, TimeToDeploy: timeToDeploy, InSync: inSync} } func (r *ReconcileGitTrack) newGitTrackObjectInterface(name string, u *unstructured.Unstructured) (farosv1alpha1.GitTrackObjectInterface, error) { var instance farosv1alpha1.GitTrackObjectInterface _, namespaced, err := utils.GetAPIResource(r.restMapper, u.GetObjectKind().GroupVersionKind()) if err != nil { return nil, fmt.Errorf("error getting API resource: %v", err) } if namespaced { instance = &farosv1alpha1.GitTrackObject{ TypeMeta: farosv1alpha1.GitTrackObjectTypeMeta, } } else { instance = &farosv1alpha1.ClusterGitTrackObject{ TypeMeta: farosv1alpha1.ClusterGitTrackObjectTypeMeta, } } instance.SetName(name) instance.SetNamespace(u.GetNamespace()) data, err := u.MarshalJSON() if err != nil { return nil, fmt.Errorf("error marshalling JSON: %v", err) } instance.SetSpec(farosv1alpha1.GitTrackObjectSpec{ Name: u.GetName(), Kind: u.GetKind(), Data: data, }) return instance, nil } // objectName constructs a name from an Unstructured object func objectName(u *unstructured.Unstructured) string { return strings.ToLower(fmt.Sprintf("%s-%s", u.GetKind(), strings.Replace(u.GetName(), ":", "-", -1))) } // handleObject either creates or updates a GitTrackObject func (r *ReconcileGitTrack) handleObject(u *unstructured.Unstructured, owner farosv1alpha1.GitTrackInterface) objectResult { name := objectName(u) gto, err := r.newGitTrackObjectInterface(name, u) if err != nil { namespacedName := strings.TrimLeft(fmt.Sprintf("%s/%s", u.GetNamespace(), name), "/") return errorResult(namespacedName, err) } ignored, reason, err := r.ignoreObject(u, owner) if err != nil { return errorResult(gto.GetNamespacedName(), err) } if ignored { return ignoreResult(gto.GetNamespacedName(), reason) } r.mutex.RLock() timeToDeploy := time.Now().Sub(r.lastUpdateTimes[owner.GetSpec().Repository]) r.mutex.RUnlock() if err = controllerutil.SetControllerReference(owner, gto, r.scheme); err != nil { return errorResult(gto.GetNamespacedName(), err) } found := gto.DeepCopyInterface() err = r.Get(context.TODO(), types.NamespacedName{Name: gto.GetName(), Namespace: gto.GetNamespace()}, found) if err != nil && errors.IsNotFound(err) { return r.createChild(name, timeToDeploy, owner, found, gto) } else if err != nil { return errorResult(gto.GetNamespacedName(), fmt.Errorf("failed to get child for '%s': %v", name, err)) } err = checkOwner(owner, found, r.scheme) if err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "ControllerMismatch", "Child '%s' is owned by another controller: %v", name, err) return ignoreResult(gto.GetNamespacedName(), "child is owned by another controller") } inSync := childInSync(found) childUpdated, err := r.updateChild(found, gto) if err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "UpdateFailed", "Failed to update child '%s'", name) return errorResult(gto.GetNamespacedName(), fmt.Errorf("failed to update child resource: %v", err)) } if childUpdated { inSync = false r.log.V(0).Info("Child updated", "child name", name) r.recorder.Eventf(owner, apiv1.EventTypeNormal, "UpdateSuccessful", "Updated child '%s'", name) } return successResult(gto.GetNamespacedName(), timeToDeploy, inSync) } func childInSync(child farosv1alpha1.GitTrackObjectInterface) bool { for _, condition := range child.GetStatus().Conditions { if condition.Type == farosv1alpha1.ObjectInSyncType && condition.Status == apiv1.ConditionTrue { return true } } return false } func (r *ReconcileGitTrack) createChild(name string, timeToDeploy time.Duration, owner farosv1alpha1.GitTrackInterface, foundGTO, childGTO farosv1alpha1.GitTrackObjectInterface) objectResult { r.recorder.Eventf(owner, apiv1.EventTypeNormal, "CreateStarted", "Creating child '%s'", name) if err := r.applier.Apply(context.TODO(), &farosclient.ApplyOptions{}, childGTO); err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "CreateFailed", "Failed to create child '%s'", name) return errorResult(childGTO.GetNamespacedName(), fmt.Errorf("failed to create child for '%s': %v", name, err)) } r.recorder.Eventf(owner, apiv1.EventTypeNormal, "CreateSuccessful", "Created child '%s'", name) r.log.V(0).Info("Child created", "child name", name) return successResult(childGTO.GetNamespacedName(), timeToDeploy, false) } // UpdateChild compares the two GitTrackObjects and updates the foundGTO if the // childGTO func (r *ReconcileGitTrack) updateChild(foundGTO, childGTO farosv1alpha1.GitTrackObjectInterface) (bool, error) { originalResourceVersion := foundGTO.GetResourceVersion() err := r.applier.Apply(context.TODO(), &farosclient.ApplyOptions{}, childGTO) if err != nil { return false, fmt.Errorf("error updating child resource: %v", err) } // Not updated if the resource version hasn't changed if originalResourceVersion == childGTO.GetResourceVersion() { return false, nil } return true, nil } // deleteResources deletes any resources that are present in the given map func (r *ReconcileGitTrack) deleteResources(leftovers map[string]farosv1alpha1.GitTrackObjectInterface) error { if len(leftovers) > 0 { r.log.V(0).Info("Found leftover resources to clean up", "leftover resources", string(len(leftovers))) } for name, obj := range leftovers { if err := r.Delete(context.TODO(), obj); err != nil { return fmt.Errorf("failed to delete child for '%s': '%s'", name, err) } r.log.V(0).Info("Child deleted", "child name", name) } return nil } // checkOwner checks the owner reference of an object from the API to see if it // is owned by the current GitTrack. func checkOwner(owner farosv1alpha1.GitTrackInterface, child farosv1alpha1.GitTrackObjectInterface, s *runtime.Scheme) error { gvk, err := apiutil.GVKForObject(owner, s) if err != nil { return err } for _, ref := range child.GetOwnerReferences() { if ref.Kind == gvk.Kind && ref.UID != owner.GetUID() { return fmt.Errorf("child object is owned by '%s'", ref.Name) } } return nil } // ignoreObject checks whether the unstructured object should be ignored func (r *ReconcileGitTrack) ignoreObject(u *unstructured.Unstructured, owner farosv1alpha1.GitTrackInterface) (bool, string, error) { gvr, namespaced, err := utils.GetAPIResource(r.restMapper, u.GetObjectKind().GroupVersionKind()) if err != nil { return false, "", err } // Ignore namespaced objects not in the namespace managed by the controller if namespaced && r.namespace != "" && r.namespace != u.GetNamespace() { r.log.V(1).Info("Object not in namespace", "object namespace", u.GetNamespace(), "managed namespace", r.namespace) return true, fmt.Sprintf("namespace `%s` is not managed by this Faros", u.GetNamespace()), nil } // Ignore GVKs in the ignoredGVKs set if _, ok := r.ignoredGVRs[gvr]; ok { r.log.V(1).Info("Object group version ignored globally", "group version resource", gvr.String()) return true, fmt.Sprintf("resource `%s.%s/%s` ignored globally by flag", gvr.Resource, gvr.Group, gvr.Version), nil } ownerNamespace := owner.GetNamespace() _, ownerIsGittrack := owner.(*farosv1alpha1.GitTrack) _, ownerIsClusterGittrack := owner.(*farosv1alpha1.ClusterGitTrack) if namespaced { // prevent a gittrack in a namespace from handling objects which are in a different namespace if ownerIsGittrack && ownerNamespace != u.GetNamespace() { return true, fmt.Sprintf("namespace `%s` is not managed by this GitTrack", u.GetNamespace()), nil } else if ownerIsClusterGittrack && r.clusterGitTrackMode == farosflags.CGTMExcludeNamespaced { return true, "namespaced resources cannot be managed by ClusterGitTrack", nil } } else if ownerIsGittrack { // cluster scoped object managed from namespaced gittrack. Disallow return true, "a GitTrack cannot manage a cluster-scoped resource", nil } return false, "", nil } // Reconcile reads the state of the cluster for a GitTrack object and makes changes based on the state read // and what is in the GitTrack.Spec // Automatically generate RBAC rules to allow the Controller to read and write Deployments // +kubebuilder:rbac:groups=faros.pusher.com,resources=gittracks,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=faros.pusher.com,resources=gittrackobjects,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=faros.pusher.com,resources=clustergittrackobjects,verbs=get;list;watch;create;update;patch;delete func (r *ReconcileGitTrack) Reconcile(request reconcile.Request) (reconcile.Result, error) { instance, err := r.fetchInstance(request) if err != nil || instance == nil { return reconcile.Result{}, err } reconciler := r.withValues( "namespace", instance.GetNamespace(), "name", instance.GetName(), ) reconciler.log.V(1).Info("Reconcile started") defer reconciler.log.V(1).Info("Reconcile finished") result := reconciler.handleGitTrack(instance) var errs []error for _, err := range []error{result.parseError, result.gitError, result.gcError, result.upToDateError} { if err != nil { errs = append(errs, err) } } err = reconciler.updateStatus(instance, result.asStatusOpts()) if err != nil { errs = append(errs, fmt.Errorf("error updating status: %v", err)) } err = reconciler.updateMetrics(instance, result.asMetricOpts(instance.GetSpec().Repository)) if err != nil { errs = append(errs, fmt.Errorf("error updating metrics: %v", err)) } if len(errs) > 0 { for _, err := range errs { reconciler.log.Error(err, "error during reconcile") } return reconcile.Result{}, fmt.Errorf("errors encountered during reconcile") } return reconcile.Result{}, nil }
{ var instance farosv1alpha1.GitTrackInterface if req.Namespace != "" { instance = &farosv1alpha1.GitTrack{} } else { instance = &farosv1alpha1.ClusterGitTrack{} } err := r.Get(context.TODO(), req.NamespacedName, instance) if err != nil { if errors.IsNotFound(err) { // Object not found, return. Created objects are automatically garbage collected. // For additional cleanup logic use finalizers. return nil, nil } // Error reading the object - requeue the request. return nil, err } return instance, nil }
identifier_body
gittrack_controller.go
/* Copyright 2018 Pusher Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package gittrack import ( "context" "fmt" "strings" "sync" "time" "github.com/go-logr/logr" farosv1alpha1 "github.com/pusher/faros/pkg/apis/faros/v1alpha1" farosflags "github.com/pusher/faros/pkg/flags" utils "github.com/pusher/faros/pkg/utils" farosclient "github.com/pusher/faros/pkg/utils/client" gitstore "github.com/pusher/git-store" apiv1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" rlogr "sigs.k8s.io/controller-runtime/pkg/runtime/log" "sigs.k8s.io/controller-runtime/pkg/source" ) // Add creates a new GitTrack Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller // and Start it when the Manager is Started. // USER ACTION REQUIRED: update cmd/manager/main.go to call this faros.Add(mgr) to install this Controller func Add(mgr manager.Manager) error { recFn, opts := newReconciler(mgr) return add(mgr, recFn, opts) } // newReconciler returns a new reconcile.Reconciler func newReconciler(mgr manager.Manager) (reconcile.Reconciler, *reconcileGitTrackOpts) { // Create a restMapper (used by informer to look up resource kinds) restMapper, err := utils.NewRestMapper(mgr.GetConfig()) if err != nil { panic(fmt.Errorf("unable to create rest mapper: %v", err)) } gvrs, err := farosflags.ParseIgnoredResources() if err != nil { panic(fmt.Errorf("unable to parse ignored resources: %v", err)) } applier, err := farosclient.NewApplier(mgr.GetConfig(), farosclient.Options{}) if err != nil { panic(fmt.Errorf("unable to create applier: %v", err)) } rec := &ReconcileGitTrack{ Client: mgr.GetClient(), scheme: mgr.GetScheme(), store: gitstore.NewRepoStore(farosflags.RepositoryDir), restMapper: restMapper, recorder: mgr.GetEventRecorderFor("gittrack-controller"), ignoredGVRs: gvrs, lastUpdateTimes: make(map[string]time.Time), mutex: &sync.RWMutex{}, applier: applier, log: rlogr.Log.WithName("gittrack-controller"), gitTrackMode: farosflags.GitTrack, namespace: farosflags.Namespace, clusterGitTrackMode: farosflags.ClusterGitTrack, } opts := &reconcileGitTrackOpts{ gitTrackMode: farosflags.GitTrack, clusterGitTrackMode: farosflags.ClusterGitTrack, } return rec, opts } // reconcileGitTrackOpts is the mode that we're running the reconciler // in. Being able to change these options during runtime is helpful during tests. type reconcileGitTrackOpts struct { clusterGitTrackMode farosflags.ClusterGitTrackMode gitTrackMode farosflags.GitTrackMode } // add adds a new Controller to mgr with r as the reconcile.Reconciler func
(mgr manager.Manager, r reconcile.Reconciler, opts *reconcileGitTrackOpts) error { // Create a new controller c, err := controller.New("gittrack-controller", mgr, controller.Options{Reconciler: r}) if err != nil { return err } if opts.gitTrackMode == farosflags.GTMEnabled { // Watch for changes to GitTrack err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrack{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err } err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.GitTrack{}, }) if err != nil { return err } } if opts.clusterGitTrackMode != farosflags.CGTMDisabled { // Watch for changes to ClusterGitTrack err = c.Watch(&source.Kind{Type: &farosv1alpha1.ClusterGitTrack{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err } err = c.Watch(&source.Kind{Type: &farosv1alpha1.ClusterGitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.ClusterGitTrack{}, }) if err != nil { return err } if opts.clusterGitTrackMode == farosflags.CGTMIncludeNamespaced { err = c.Watch(&source.Kind{Type: &farosv1alpha1.GitTrackObject{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &farosv1alpha1.ClusterGitTrack{}, }) if err != nil { return err } } } return nil } var _ reconcile.Reconciler = &ReconcileGitTrack{} // ReconcileGitTrack reconciles a GitTrack object type ReconcileGitTrack struct { client.Client scheme *runtime.Scheme store *gitstore.RepoStore restMapper meta.RESTMapper recorder record.EventRecorder ignoredGVRs map[schema.GroupVersionResource]interface{} lastUpdateTimes map[string]time.Time mutex *sync.RWMutex applier farosclient.Client log logr.Logger gitTrackMode farosflags.GitTrackMode namespace string clusterGitTrackMode farosflags.ClusterGitTrackMode } func (r *ReconcileGitTrack) withValues(keysAndValues ...interface{}) *ReconcileGitTrack { reconciler := *r reconciler.log = r.log.WithValues(keysAndValues...) return &reconciler } // fetchInstance attempts to fetch the GitTrack resource by the name in the given Request func (r *ReconcileGitTrack) fetchInstance(req reconcile.Request) (farosv1alpha1.GitTrackInterface, error) { var instance farosv1alpha1.GitTrackInterface if req.Namespace != "" { instance = &farosv1alpha1.GitTrack{} } else { instance = &farosv1alpha1.ClusterGitTrack{} } err := r.Get(context.TODO(), req.NamespacedName, instance) if err != nil { if errors.IsNotFound(err) { // Object not found, return. Created objects are automatically garbage collected. // For additional cleanup logic use finalizers. return nil, nil } // Error reading the object - requeue the request. return nil, err } return instance, nil } // listObjectsByName lists and filters GitTrackObjects by the `faros.pusher.com/owned-by` label, // and returns a map of names to GitTrackObject mappings func (r *ReconcileGitTrack) listObjectsByName(owner farosv1alpha1.GitTrackInterface) (map[string]farosv1alpha1.GitTrackObjectInterface, error) { result := make(map[string]farosv1alpha1.GitTrackObjectInterface) gtos := &farosv1alpha1.GitTrackObjectList{} err := r.List(context.TODO(), gtos) if err != nil { return nil, err } for _, gto := range gtos.Items { if metav1.IsControlledBy(&gto, owner) { result[gto.GetNamespacedName()] = gto.DeepCopy() } } cgtos := &farosv1alpha1.ClusterGitTrackObjectList{} err = r.List(context.TODO(), cgtos) if err != nil { return nil, err } for _, cgto := range cgtos.Items { if metav1.IsControlledBy(&cgto, owner) { result[cgto.GetNamespacedName()] = cgto.DeepCopy() } } return result, nil } // objectResult represents the result of creating or updating a GitTrackObject type objectResult struct { NamespacedName string Error error Ignored bool Reason string InSync bool TimeToDeploy time.Duration } // errorResult is a convenience function for creating an error result func errorResult(namespacedName string, err error) objectResult { return objectResult{NamespacedName: namespacedName, Error: err, Ignored: true} } // ignoreResult is a convenience function for creating an ignore objectResult func ignoreResult(namespacedName string, reason string) objectResult { return objectResult{NamespacedName: namespacedName, Ignored: true, Reason: reason} } // successResult is a convenience function for creating a success objectResult func successResult(namespacedName string, timeToDeploy time.Duration, inSync bool) objectResult { return objectResult{NamespacedName: namespacedName, TimeToDeploy: timeToDeploy, InSync: inSync} } func (r *ReconcileGitTrack) newGitTrackObjectInterface(name string, u *unstructured.Unstructured) (farosv1alpha1.GitTrackObjectInterface, error) { var instance farosv1alpha1.GitTrackObjectInterface _, namespaced, err := utils.GetAPIResource(r.restMapper, u.GetObjectKind().GroupVersionKind()) if err != nil { return nil, fmt.Errorf("error getting API resource: %v", err) } if namespaced { instance = &farosv1alpha1.GitTrackObject{ TypeMeta: farosv1alpha1.GitTrackObjectTypeMeta, } } else { instance = &farosv1alpha1.ClusterGitTrackObject{ TypeMeta: farosv1alpha1.ClusterGitTrackObjectTypeMeta, } } instance.SetName(name) instance.SetNamespace(u.GetNamespace()) data, err := u.MarshalJSON() if err != nil { return nil, fmt.Errorf("error marshalling JSON: %v", err) } instance.SetSpec(farosv1alpha1.GitTrackObjectSpec{ Name: u.GetName(), Kind: u.GetKind(), Data: data, }) return instance, nil } // objectName constructs a name from an Unstructured object func objectName(u *unstructured.Unstructured) string { return strings.ToLower(fmt.Sprintf("%s-%s", u.GetKind(), strings.Replace(u.GetName(), ":", "-", -1))) } // handleObject either creates or updates a GitTrackObject func (r *ReconcileGitTrack) handleObject(u *unstructured.Unstructured, owner farosv1alpha1.GitTrackInterface) objectResult { name := objectName(u) gto, err := r.newGitTrackObjectInterface(name, u) if err != nil { namespacedName := strings.TrimLeft(fmt.Sprintf("%s/%s", u.GetNamespace(), name), "/") return errorResult(namespacedName, err) } ignored, reason, err := r.ignoreObject(u, owner) if err != nil { return errorResult(gto.GetNamespacedName(), err) } if ignored { return ignoreResult(gto.GetNamespacedName(), reason) } r.mutex.RLock() timeToDeploy := time.Now().Sub(r.lastUpdateTimes[owner.GetSpec().Repository]) r.mutex.RUnlock() if err = controllerutil.SetControllerReference(owner, gto, r.scheme); err != nil { return errorResult(gto.GetNamespacedName(), err) } found := gto.DeepCopyInterface() err = r.Get(context.TODO(), types.NamespacedName{Name: gto.GetName(), Namespace: gto.GetNamespace()}, found) if err != nil && errors.IsNotFound(err) { return r.createChild(name, timeToDeploy, owner, found, gto) } else if err != nil { return errorResult(gto.GetNamespacedName(), fmt.Errorf("failed to get child for '%s': %v", name, err)) } err = checkOwner(owner, found, r.scheme) if err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "ControllerMismatch", "Child '%s' is owned by another controller: %v", name, err) return ignoreResult(gto.GetNamespacedName(), "child is owned by another controller") } inSync := childInSync(found) childUpdated, err := r.updateChild(found, gto) if err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "UpdateFailed", "Failed to update child '%s'", name) return errorResult(gto.GetNamespacedName(), fmt.Errorf("failed to update child resource: %v", err)) } if childUpdated { inSync = false r.log.V(0).Info("Child updated", "child name", name) r.recorder.Eventf(owner, apiv1.EventTypeNormal, "UpdateSuccessful", "Updated child '%s'", name) } return successResult(gto.GetNamespacedName(), timeToDeploy, inSync) } func childInSync(child farosv1alpha1.GitTrackObjectInterface) bool { for _, condition := range child.GetStatus().Conditions { if condition.Type == farosv1alpha1.ObjectInSyncType && condition.Status == apiv1.ConditionTrue { return true } } return false } func (r *ReconcileGitTrack) createChild(name string, timeToDeploy time.Duration, owner farosv1alpha1.GitTrackInterface, foundGTO, childGTO farosv1alpha1.GitTrackObjectInterface) objectResult { r.recorder.Eventf(owner, apiv1.EventTypeNormal, "CreateStarted", "Creating child '%s'", name) if err := r.applier.Apply(context.TODO(), &farosclient.ApplyOptions{}, childGTO); err != nil { r.recorder.Eventf(owner, apiv1.EventTypeWarning, "CreateFailed", "Failed to create child '%s'", name) return errorResult(childGTO.GetNamespacedName(), fmt.Errorf("failed to create child for '%s': %v", name, err)) } r.recorder.Eventf(owner, apiv1.EventTypeNormal, "CreateSuccessful", "Created child '%s'", name) r.log.V(0).Info("Child created", "child name", name) return successResult(childGTO.GetNamespacedName(), timeToDeploy, false) } // UpdateChild compares the two GitTrackObjects and updates the foundGTO if the // childGTO func (r *ReconcileGitTrack) updateChild(foundGTO, childGTO farosv1alpha1.GitTrackObjectInterface) (bool, error) { originalResourceVersion := foundGTO.GetResourceVersion() err := r.applier.Apply(context.TODO(), &farosclient.ApplyOptions{}, childGTO) if err != nil { return false, fmt.Errorf("error updating child resource: %v", err) } // Not updated if the resource version hasn't changed if originalResourceVersion == childGTO.GetResourceVersion() { return false, nil } return true, nil } // deleteResources deletes any resources that are present in the given map func (r *ReconcileGitTrack) deleteResources(leftovers map[string]farosv1alpha1.GitTrackObjectInterface) error { if len(leftovers) > 0 { r.log.V(0).Info("Found leftover resources to clean up", "leftover resources", string(len(leftovers))) } for name, obj := range leftovers { if err := r.Delete(context.TODO(), obj); err != nil { return fmt.Errorf("failed to delete child for '%s': '%s'", name, err) } r.log.V(0).Info("Child deleted", "child name", name) } return nil } // checkOwner checks the owner reference of an object from the API to see if it // is owned by the current GitTrack. func checkOwner(owner farosv1alpha1.GitTrackInterface, child farosv1alpha1.GitTrackObjectInterface, s *runtime.Scheme) error { gvk, err := apiutil.GVKForObject(owner, s) if err != nil { return err } for _, ref := range child.GetOwnerReferences() { if ref.Kind == gvk.Kind && ref.UID != owner.GetUID() { return fmt.Errorf("child object is owned by '%s'", ref.Name) } } return nil } // ignoreObject checks whether the unstructured object should be ignored func (r *ReconcileGitTrack) ignoreObject(u *unstructured.Unstructured, owner farosv1alpha1.GitTrackInterface) (bool, string, error) { gvr, namespaced, err := utils.GetAPIResource(r.restMapper, u.GetObjectKind().GroupVersionKind()) if err != nil { return false, "", err } // Ignore namespaced objects not in the namespace managed by the controller if namespaced && r.namespace != "" && r.namespace != u.GetNamespace() { r.log.V(1).Info("Object not in namespace", "object namespace", u.GetNamespace(), "managed namespace", r.namespace) return true, fmt.Sprintf("namespace `%s` is not managed by this Faros", u.GetNamespace()), nil } // Ignore GVKs in the ignoredGVKs set if _, ok := r.ignoredGVRs[gvr]; ok { r.log.V(1).Info("Object group version ignored globally", "group version resource", gvr.String()) return true, fmt.Sprintf("resource `%s.%s/%s` ignored globally by flag", gvr.Resource, gvr.Group, gvr.Version), nil } ownerNamespace := owner.GetNamespace() _, ownerIsGittrack := owner.(*farosv1alpha1.GitTrack) _, ownerIsClusterGittrack := owner.(*farosv1alpha1.ClusterGitTrack) if namespaced { // prevent a gittrack in a namespace from handling objects which are in a different namespace if ownerIsGittrack && ownerNamespace != u.GetNamespace() { return true, fmt.Sprintf("namespace `%s` is not managed by this GitTrack", u.GetNamespace()), nil } else if ownerIsClusterGittrack && r.clusterGitTrackMode == farosflags.CGTMExcludeNamespaced { return true, "namespaced resources cannot be managed by ClusterGitTrack", nil } } else if ownerIsGittrack { // cluster scoped object managed from namespaced gittrack. Disallow return true, "a GitTrack cannot manage a cluster-scoped resource", nil } return false, "", nil } // Reconcile reads the state of the cluster for a GitTrack object and makes changes based on the state read // and what is in the GitTrack.Spec // Automatically generate RBAC rules to allow the Controller to read and write Deployments // +kubebuilder:rbac:groups=faros.pusher.com,resources=gittracks,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=faros.pusher.com,resources=gittrackobjects,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=faros.pusher.com,resources=clustergittrackobjects,verbs=get;list;watch;create;update;patch;delete func (r *ReconcileGitTrack) Reconcile(request reconcile.Request) (reconcile.Result, error) { instance, err := r.fetchInstance(request) if err != nil || instance == nil { return reconcile.Result{}, err } reconciler := r.withValues( "namespace", instance.GetNamespace(), "name", instance.GetName(), ) reconciler.log.V(1).Info("Reconcile started") defer reconciler.log.V(1).Info("Reconcile finished") result := reconciler.handleGitTrack(instance) var errs []error for _, err := range []error{result.parseError, result.gitError, result.gcError, result.upToDateError} { if err != nil { errs = append(errs, err) } } err = reconciler.updateStatus(instance, result.asStatusOpts()) if err != nil { errs = append(errs, fmt.Errorf("error updating status: %v", err)) } err = reconciler.updateMetrics(instance, result.asMetricOpts(instance.GetSpec().Repository)) if err != nil { errs = append(errs, fmt.Errorf("error updating metrics: %v", err)) } if len(errs) > 0 { for _, err := range errs { reconciler.log.Error(err, "error during reconcile") } return reconcile.Result{}, fmt.Errorf("errors encountered during reconcile") } return reconcile.Result{}, nil }
add
identifier_name
parse.go
package xmlquery import ( "bufio" "encoding/xml" "fmt" "io" "net/http" "regexp" "strings" "github.com/antchfx/xpath" "golang.org/x/net/html/charset" ) var xmlMIMERegex = regexp.MustCompile(`(?i)((application|image|message|model)/((\w|\.|-)+\+?)?|text/)(wb)?xml`) // LoadURL loads the XML document from the specified URL. func LoadURL(url string) (*Node, error)
// Parse returns the parse tree for the XML from the given Reader. func Parse(r io.Reader) (*Node, error) { return ParseWithOptions(r, ParserOptions{}) } // ParseWithOptions is like parse, but with custom options func ParseWithOptions(r io.Reader, options ParserOptions) (*Node, error) { p := createParser(r) options.apply(p) for { _, err := p.parse() if err == io.EOF { return p.doc, nil } if err != nil { return nil, err } } } type parser struct { decoder *xml.Decoder doc *Node level int prev *Node streamElementXPath *xpath.Expr // Under streaming mode, this specifies the xpath to the target element node(s). streamElementFilter *xpath.Expr // If specified, it provides further filtering on the target element. streamNode *Node // Need to remember the last target node So we can clean it up upon next Read() call. streamNodePrev *Node // Need to remember target node's prev so upon target node removal, we can restore correct prev. reader *cachedReader // Need to maintain a reference to the reader, so we can determine whether a node contains CDATA. } func createParser(r io.Reader) *parser { reader := newCachedReader(bufio.NewReader(r)) p := &parser{ decoder: xml.NewDecoder(reader), doc: &Node{Type: DocumentNode}, level: 0, reader: reader, } if p.decoder.CharsetReader == nil { p.decoder.CharsetReader = charset.NewReaderLabel } p.prev = p.doc return p } func (p *parser) parse() (*Node, error) { var streamElementNodeCounter int space2prefix := map[string]string{"http://www.w3.org/XML/1998/namespace": "xml"} for { p.reader.StartCaching() tok, err := p.decoder.Token() p.reader.StopCaching() if err != nil { return nil, err } switch tok := tok.(type) { case xml.StartElement: if p.level == 0 { // mising XML declaration attributes := make([]Attr, 1) attributes[0].Name = xml.Name{Local: "version"} attributes[0].Value = "1.0" node := &Node{ Type: DeclarationNode, Data: "xml", Attr: attributes, level: 1, } AddChild(p.prev, node) p.level = 1 p.prev = node } for _, att := range tok.Attr { if att.Name.Local == "xmlns" { space2prefix[att.Value] = "" // reset empty if exist the default namespace // defaultNamespaceURL = att.Value } else if att.Name.Space == "xmlns" { // maybe there are have duplicate NamespaceURL? space2prefix[att.Value] = att.Name.Local } } if space := tok.Name.Space; space != "" { if _, found := space2prefix[space]; !found && p.decoder.Strict { return nil, fmt.Errorf("xmlquery: invalid XML document, namespace %s is missing", space) } } attributes := make([]Attr, len(tok.Attr)) for i, att := range tok.Attr { name := att.Name if prefix, ok := space2prefix[name.Space]; ok { name.Space = prefix } attributes[i] = Attr{ Name: name, Value: att.Value, NamespaceURI: att.Name.Space, } } node := &Node{ Type: ElementNode, Data: tok.Name.Local, NamespaceURI: tok.Name.Space, Attr: attributes, level: p.level, } if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } if node.NamespaceURI != "" { if v, ok := space2prefix[node.NamespaceURI]; ok { cached := string(p.reader.Cache()) if strings.HasPrefix(cached, fmt.Sprintf("%s:%s", v, node.Data)) || strings.HasPrefix(cached, fmt.Sprintf("<%s:%s", v, node.Data)) { node.Prefix = v } } } // If we're in the streaming mode, we need to remember the node if it is the target node // so that when we finish processing the node's EndElement, we know how/what to return to // caller. Also we need to remove the target node from the tree upon next Read() call so // memory doesn't grow unbounded. if p.streamElementXPath != nil { if p.streamNode == nil { if QuerySelector(p.doc, p.streamElementXPath) != nil { p.streamNode = node p.streamNodePrev = p.prev streamElementNodeCounter = 1 } } else { streamElementNodeCounter++ } } p.prev = node p.level++ case xml.EndElement: p.level-- // If we're in streaming mode, and we already have a potential streaming // target node identified (p.streamNode != nil) then we need to check if // this is the real one we want to return to caller. if p.streamNode != nil { streamElementNodeCounter-- if streamElementNodeCounter == 0 { // Now we know this element node is the at least passing the initial // p.streamElementXPath check and is a potential target node candidate. // We need to have 1 more check with p.streamElementFilter (if given) to // ensure it is really the element node we want. // The reason we need a two-step check process is because the following // situation: // <AAA><BBB>b1</BBB></AAA> // And say the p.streamElementXPath = "/AAA/BBB[. != 'b1']". Now during // xml.StartElement time, the <BBB> node is still empty, so it will pass // the p.streamElementXPath check. However, eventually we know this <BBB> // shouldn't be returned to the caller. Having a second more fine-grained // filter check ensures that. So in this case, the caller should really // setup the stream parser with: // streamElementXPath = "/AAA/BBB[" // streamElementFilter = "/AAA/BBB[. != 'b1']" if p.streamElementFilter == nil || QuerySelector(p.doc, p.streamElementFilter) != nil { return p.streamNode, nil } // otherwise, this isn't our target node, clean things up. // note we also remove the underlying *Node from the node tree, to prevent // future stream node candidate selection error. RemoveFromTree(p.streamNode) p.prev = p.streamNodePrev p.streamNode = nil p.streamNodePrev = nil } } case xml.CharData: // First, normalize the cache... cached := strings.ToUpper(string(p.reader.Cache())) nodeType := TextNode if strings.HasPrefix(cached, "<![CDATA[") || strings.HasPrefix(cached, "![CDATA[") { nodeType = CharDataNode } node := &Node{Type: nodeType, Data: string(tok), level: p.level} if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } case xml.Comment: node := &Node{Type: CommentNode, Data: string(tok), level: p.level} if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } case xml.ProcInst: // Processing Instruction if p.prev.Type != DeclarationNode { p.level++ } node := &Node{Type: DeclarationNode, Data: tok.Target, level: p.level} pairs := strings.Split(string(tok.Inst), " ") for _, pair := range pairs { pair = strings.TrimSpace(pair) if i := strings.Index(pair, "="); i > 0 { AddAttr(node, pair[:i], strings.Trim(pair[i+1:], `"`)) } } if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } p.prev = node case xml.Directive: } } } // StreamParser enables loading and parsing an XML document in a streaming // fashion. type StreamParser struct { p *parser } // CreateStreamParser creates a StreamParser. Argument streamElementXPath is // required. // Argument streamElementFilter is optional and should only be used in advanced // scenarios. // // Scenario 1: simple case: // xml := `<AAA><BBB>b1</BBB><BBB>b2</BBB></AAA>` // sp, err := CreateStreamParser(strings.NewReader(xml), "/AAA/BBB") // if err != nil { // panic(err) // } // for { // n, err := sp.Read() // if err != nil { // break // } // fmt.Println(n.OutputXML(true)) // } // Output will be: // <BBB>b1</BBB> // <BBB>b2</BBB> // // Scenario 2: advanced case: // xml := `<AAA><BBB>b1</BBB><BBB>b2</BBB></AAA>` // sp, err := CreateStreamParser(strings.NewReader(xml), "/AAA/BBB", "/AAA/BBB[. != 'b1']") // if err != nil { // panic(err) // } // for { // n, err := sp.Read() // if err != nil { // break // } // fmt.Println(n.OutputXML(true)) // } // Output will be: // <BBB>b2</BBB> // // As the argument names indicate, streamElementXPath should be used for // providing xpath query pointing to the target element node only, no extra // filtering on the element itself or its children; while streamElementFilter, // if needed, can provide additional filtering on the target element and its // children. // // CreateStreamParser returns an error if either streamElementXPath or // streamElementFilter, if provided, cannot be successfully parsed and compiled // into a valid xpath query. func CreateStreamParser(r io.Reader, streamElementXPath string, streamElementFilter ...string) (*StreamParser, error) { return CreateStreamParserWithOptions(r, ParserOptions{}, streamElementXPath, streamElementFilter...) } // CreateStreamParserWithOptions is like CreateStreamParser, but with custom options func CreateStreamParserWithOptions( r io.Reader, options ParserOptions, streamElementXPath string, streamElementFilter ...string, ) (*StreamParser, error) { elemXPath, err := getQuery(streamElementXPath) if err != nil { return nil, fmt.Errorf("invalid streamElementXPath '%s', err: %s", streamElementXPath, err.Error()) } elemFilter := (*xpath.Expr)(nil) if len(streamElementFilter) > 0 { elemFilter, err = getQuery(streamElementFilter[0]) if err != nil { return nil, fmt.Errorf("invalid streamElementFilter '%s', err: %s", streamElementFilter[0], err.Error()) } } parser := createParser(r) options.apply(parser) sp := &StreamParser{ p: parser, } sp.p.streamElementXPath = elemXPath sp.p.streamElementFilter = elemFilter return sp, nil } // Read returns a target node that satisfies the XPath specified by caller at // StreamParser creation time. If there is no more satisfying target nodes after // reading the rest of the XML document, io.EOF will be returned. At any time, // any XML parsing error encountered will be returned, and the stream parsing // stopped. Calling Read() after an error is returned (including io.EOF) results // undefined behavior. Also note, due to the streaming nature, calling Read() // will automatically remove any previous target node(s) from the document tree. func (sp *StreamParser) Read() (*Node, error) { // Because this is a streaming read, we need to release/remove last // target node from the node tree to free up memory. if sp.p.streamNode != nil { // We need to remove all siblings before the current stream node, // because the document may contain unwanted nodes between the target // ones (for example new line text node), which would otherwise // accumulate as first childs, and slow down the stream over time for sp.p.streamNode.PrevSibling != nil { RemoveFromTree(sp.p.streamNode.PrevSibling) } sp.p.prev = sp.p.streamNode.Parent RemoveFromTree(sp.p.streamNode) sp.p.streamNode = nil sp.p.streamNodePrev = nil } return sp.p.parse() }
{ resp, err := http.Get(url) if err != nil { return nil, err } defer resp.Body.Close() // Make sure the Content-Type has a valid XML MIME type if xmlMIMERegex.MatchString(resp.Header.Get("Content-Type")) { return Parse(resp.Body) } return nil, fmt.Errorf("invalid XML document(%s)", resp.Header.Get("Content-Type")) }
identifier_body
parse.go
package xmlquery import ( "bufio" "encoding/xml" "fmt" "io" "net/http" "regexp" "strings" "github.com/antchfx/xpath" "golang.org/x/net/html/charset" ) var xmlMIMERegex = regexp.MustCompile(`(?i)((application|image|message|model)/((\w|\.|-)+\+?)?|text/)(wb)?xml`) // LoadURL loads the XML document from the specified URL. func LoadURL(url string) (*Node, error) { resp, err := http.Get(url) if err != nil { return nil, err } defer resp.Body.Close() // Make sure the Content-Type has a valid XML MIME type if xmlMIMERegex.MatchString(resp.Header.Get("Content-Type")) { return Parse(resp.Body) } return nil, fmt.Errorf("invalid XML document(%s)", resp.Header.Get("Content-Type")) } // Parse returns the parse tree for the XML from the given Reader. func Parse(r io.Reader) (*Node, error) { return ParseWithOptions(r, ParserOptions{}) } // ParseWithOptions is like parse, but with custom options func
(r io.Reader, options ParserOptions) (*Node, error) { p := createParser(r) options.apply(p) for { _, err := p.parse() if err == io.EOF { return p.doc, nil } if err != nil { return nil, err } } } type parser struct { decoder *xml.Decoder doc *Node level int prev *Node streamElementXPath *xpath.Expr // Under streaming mode, this specifies the xpath to the target element node(s). streamElementFilter *xpath.Expr // If specified, it provides further filtering on the target element. streamNode *Node // Need to remember the last target node So we can clean it up upon next Read() call. streamNodePrev *Node // Need to remember target node's prev so upon target node removal, we can restore correct prev. reader *cachedReader // Need to maintain a reference to the reader, so we can determine whether a node contains CDATA. } func createParser(r io.Reader) *parser { reader := newCachedReader(bufio.NewReader(r)) p := &parser{ decoder: xml.NewDecoder(reader), doc: &Node{Type: DocumentNode}, level: 0, reader: reader, } if p.decoder.CharsetReader == nil { p.decoder.CharsetReader = charset.NewReaderLabel } p.prev = p.doc return p } func (p *parser) parse() (*Node, error) { var streamElementNodeCounter int space2prefix := map[string]string{"http://www.w3.org/XML/1998/namespace": "xml"} for { p.reader.StartCaching() tok, err := p.decoder.Token() p.reader.StopCaching() if err != nil { return nil, err } switch tok := tok.(type) { case xml.StartElement: if p.level == 0 { // mising XML declaration attributes := make([]Attr, 1) attributes[0].Name = xml.Name{Local: "version"} attributes[0].Value = "1.0" node := &Node{ Type: DeclarationNode, Data: "xml", Attr: attributes, level: 1, } AddChild(p.prev, node) p.level = 1 p.prev = node } for _, att := range tok.Attr { if att.Name.Local == "xmlns" { space2prefix[att.Value] = "" // reset empty if exist the default namespace // defaultNamespaceURL = att.Value } else if att.Name.Space == "xmlns" { // maybe there are have duplicate NamespaceURL? space2prefix[att.Value] = att.Name.Local } } if space := tok.Name.Space; space != "" { if _, found := space2prefix[space]; !found && p.decoder.Strict { return nil, fmt.Errorf("xmlquery: invalid XML document, namespace %s is missing", space) } } attributes := make([]Attr, len(tok.Attr)) for i, att := range tok.Attr { name := att.Name if prefix, ok := space2prefix[name.Space]; ok { name.Space = prefix } attributes[i] = Attr{ Name: name, Value: att.Value, NamespaceURI: att.Name.Space, } } node := &Node{ Type: ElementNode, Data: tok.Name.Local, NamespaceURI: tok.Name.Space, Attr: attributes, level: p.level, } if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } if node.NamespaceURI != "" { if v, ok := space2prefix[node.NamespaceURI]; ok { cached := string(p.reader.Cache()) if strings.HasPrefix(cached, fmt.Sprintf("%s:%s", v, node.Data)) || strings.HasPrefix(cached, fmt.Sprintf("<%s:%s", v, node.Data)) { node.Prefix = v } } } // If we're in the streaming mode, we need to remember the node if it is the target node // so that when we finish processing the node's EndElement, we know how/what to return to // caller. Also we need to remove the target node from the tree upon next Read() call so // memory doesn't grow unbounded. if p.streamElementXPath != nil { if p.streamNode == nil { if QuerySelector(p.doc, p.streamElementXPath) != nil { p.streamNode = node p.streamNodePrev = p.prev streamElementNodeCounter = 1 } } else { streamElementNodeCounter++ } } p.prev = node p.level++ case xml.EndElement: p.level-- // If we're in streaming mode, and we already have a potential streaming // target node identified (p.streamNode != nil) then we need to check if // this is the real one we want to return to caller. if p.streamNode != nil { streamElementNodeCounter-- if streamElementNodeCounter == 0 { // Now we know this element node is the at least passing the initial // p.streamElementXPath check and is a potential target node candidate. // We need to have 1 more check with p.streamElementFilter (if given) to // ensure it is really the element node we want. // The reason we need a two-step check process is because the following // situation: // <AAA><BBB>b1</BBB></AAA> // And say the p.streamElementXPath = "/AAA/BBB[. != 'b1']". Now during // xml.StartElement time, the <BBB> node is still empty, so it will pass // the p.streamElementXPath check. However, eventually we know this <BBB> // shouldn't be returned to the caller. Having a second more fine-grained // filter check ensures that. So in this case, the caller should really // setup the stream parser with: // streamElementXPath = "/AAA/BBB[" // streamElementFilter = "/AAA/BBB[. != 'b1']" if p.streamElementFilter == nil || QuerySelector(p.doc, p.streamElementFilter) != nil { return p.streamNode, nil } // otherwise, this isn't our target node, clean things up. // note we also remove the underlying *Node from the node tree, to prevent // future stream node candidate selection error. RemoveFromTree(p.streamNode) p.prev = p.streamNodePrev p.streamNode = nil p.streamNodePrev = nil } } case xml.CharData: // First, normalize the cache... cached := strings.ToUpper(string(p.reader.Cache())) nodeType := TextNode if strings.HasPrefix(cached, "<![CDATA[") || strings.HasPrefix(cached, "![CDATA[") { nodeType = CharDataNode } node := &Node{Type: nodeType, Data: string(tok), level: p.level} if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } case xml.Comment: node := &Node{Type: CommentNode, Data: string(tok), level: p.level} if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } case xml.ProcInst: // Processing Instruction if p.prev.Type != DeclarationNode { p.level++ } node := &Node{Type: DeclarationNode, Data: tok.Target, level: p.level} pairs := strings.Split(string(tok.Inst), " ") for _, pair := range pairs { pair = strings.TrimSpace(pair) if i := strings.Index(pair, "="); i > 0 { AddAttr(node, pair[:i], strings.Trim(pair[i+1:], `"`)) } } if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } p.prev = node case xml.Directive: } } } // StreamParser enables loading and parsing an XML document in a streaming // fashion. type StreamParser struct { p *parser } // CreateStreamParser creates a StreamParser. Argument streamElementXPath is // required. // Argument streamElementFilter is optional and should only be used in advanced // scenarios. // // Scenario 1: simple case: // xml := `<AAA><BBB>b1</BBB><BBB>b2</BBB></AAA>` // sp, err := CreateStreamParser(strings.NewReader(xml), "/AAA/BBB") // if err != nil { // panic(err) // } // for { // n, err := sp.Read() // if err != nil { // break // } // fmt.Println(n.OutputXML(true)) // } // Output will be: // <BBB>b1</BBB> // <BBB>b2</BBB> // // Scenario 2: advanced case: // xml := `<AAA><BBB>b1</BBB><BBB>b2</BBB></AAA>` // sp, err := CreateStreamParser(strings.NewReader(xml), "/AAA/BBB", "/AAA/BBB[. != 'b1']") // if err != nil { // panic(err) // } // for { // n, err := sp.Read() // if err != nil { // break // } // fmt.Println(n.OutputXML(true)) // } // Output will be: // <BBB>b2</BBB> // // As the argument names indicate, streamElementXPath should be used for // providing xpath query pointing to the target element node only, no extra // filtering on the element itself or its children; while streamElementFilter, // if needed, can provide additional filtering on the target element and its // children. // // CreateStreamParser returns an error if either streamElementXPath or // streamElementFilter, if provided, cannot be successfully parsed and compiled // into a valid xpath query. func CreateStreamParser(r io.Reader, streamElementXPath string, streamElementFilter ...string) (*StreamParser, error) { return CreateStreamParserWithOptions(r, ParserOptions{}, streamElementXPath, streamElementFilter...) } // CreateStreamParserWithOptions is like CreateStreamParser, but with custom options func CreateStreamParserWithOptions( r io.Reader, options ParserOptions, streamElementXPath string, streamElementFilter ...string, ) (*StreamParser, error) { elemXPath, err := getQuery(streamElementXPath) if err != nil { return nil, fmt.Errorf("invalid streamElementXPath '%s', err: %s", streamElementXPath, err.Error()) } elemFilter := (*xpath.Expr)(nil) if len(streamElementFilter) > 0 { elemFilter, err = getQuery(streamElementFilter[0]) if err != nil { return nil, fmt.Errorf("invalid streamElementFilter '%s', err: %s", streamElementFilter[0], err.Error()) } } parser := createParser(r) options.apply(parser) sp := &StreamParser{ p: parser, } sp.p.streamElementXPath = elemXPath sp.p.streamElementFilter = elemFilter return sp, nil } // Read returns a target node that satisfies the XPath specified by caller at // StreamParser creation time. If there is no more satisfying target nodes after // reading the rest of the XML document, io.EOF will be returned. At any time, // any XML parsing error encountered will be returned, and the stream parsing // stopped. Calling Read() after an error is returned (including io.EOF) results // undefined behavior. Also note, due to the streaming nature, calling Read() // will automatically remove any previous target node(s) from the document tree. func (sp *StreamParser) Read() (*Node, error) { // Because this is a streaming read, we need to release/remove last // target node from the node tree to free up memory. if sp.p.streamNode != nil { // We need to remove all siblings before the current stream node, // because the document may contain unwanted nodes between the target // ones (for example new line text node), which would otherwise // accumulate as first childs, and slow down the stream over time for sp.p.streamNode.PrevSibling != nil { RemoveFromTree(sp.p.streamNode.PrevSibling) } sp.p.prev = sp.p.streamNode.Parent RemoveFromTree(sp.p.streamNode) sp.p.streamNode = nil sp.p.streamNodePrev = nil } return sp.p.parse() }
ParseWithOptions
identifier_name
parse.go
package xmlquery import ( "bufio" "encoding/xml" "fmt" "io" "net/http" "regexp" "strings" "github.com/antchfx/xpath" "golang.org/x/net/html/charset" ) var xmlMIMERegex = regexp.MustCompile(`(?i)((application|image|message|model)/((\w|\.|-)+\+?)?|text/)(wb)?xml`) // LoadURL loads the XML document from the specified URL. func LoadURL(url string) (*Node, error) { resp, err := http.Get(url) if err != nil { return nil, err } defer resp.Body.Close() // Make sure the Content-Type has a valid XML MIME type if xmlMIMERegex.MatchString(resp.Header.Get("Content-Type")) { return Parse(resp.Body) } return nil, fmt.Errorf("invalid XML document(%s)", resp.Header.Get("Content-Type")) } // Parse returns the parse tree for the XML from the given Reader. func Parse(r io.Reader) (*Node, error) { return ParseWithOptions(r, ParserOptions{}) } // ParseWithOptions is like parse, but with custom options func ParseWithOptions(r io.Reader, options ParserOptions) (*Node, error) { p := createParser(r) options.apply(p) for { _, err := p.parse() if err == io.EOF { return p.doc, nil } if err != nil { return nil, err } } } type parser struct { decoder *xml.Decoder doc *Node level int prev *Node streamElementXPath *xpath.Expr // Under streaming mode, this specifies the xpath to the target element node(s). streamElementFilter *xpath.Expr // If specified, it provides further filtering on the target element. streamNode *Node // Need to remember the last target node So we can clean it up upon next Read() call. streamNodePrev *Node // Need to remember target node's prev so upon target node removal, we can restore correct prev. reader *cachedReader // Need to maintain a reference to the reader, so we can determine whether a node contains CDATA. } func createParser(r io.Reader) *parser { reader := newCachedReader(bufio.NewReader(r)) p := &parser{ decoder: xml.NewDecoder(reader), doc: &Node{Type: DocumentNode}, level: 0, reader: reader, } if p.decoder.CharsetReader == nil { p.decoder.CharsetReader = charset.NewReaderLabel } p.prev = p.doc return p } func (p *parser) parse() (*Node, error) { var streamElementNodeCounter int space2prefix := map[string]string{"http://www.w3.org/XML/1998/namespace": "xml"} for { p.reader.StartCaching() tok, err := p.decoder.Token() p.reader.StopCaching() if err != nil { return nil, err } switch tok := tok.(type) { case xml.StartElement: if p.level == 0 { // mising XML declaration attributes := make([]Attr, 1) attributes[0].Name = xml.Name{Local: "version"} attributes[0].Value = "1.0" node := &Node{ Type: DeclarationNode, Data: "xml", Attr: attributes, level: 1, } AddChild(p.prev, node) p.level = 1 p.prev = node } for _, att := range tok.Attr { if att.Name.Local == "xmlns" { space2prefix[att.Value] = "" // reset empty if exist the default namespace // defaultNamespaceURL = att.Value } else if att.Name.Space == "xmlns" { // maybe there are have duplicate NamespaceURL? space2prefix[att.Value] = att.Name.Local } } if space := tok.Name.Space; space != "" { if _, found := space2prefix[space]; !found && p.decoder.Strict { return nil, fmt.Errorf("xmlquery: invalid XML document, namespace %s is missing", space) } } attributes := make([]Attr, len(tok.Attr)) for i, att := range tok.Attr { name := att.Name if prefix, ok := space2prefix[name.Space]; ok { name.Space = prefix } attributes[i] = Attr{ Name: name, Value: att.Value, NamespaceURI: att.Name.Space, } } node := &Node{ Type: ElementNode, Data: tok.Name.Local, NamespaceURI: tok.Name.Space, Attr: attributes, level: p.level, } if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } if node.NamespaceURI != "" { if v, ok := space2prefix[node.NamespaceURI]; ok { cached := string(p.reader.Cache()) if strings.HasPrefix(cached, fmt.Sprintf("%s:%s", v, node.Data)) || strings.HasPrefix(cached, fmt.Sprintf("<%s:%s", v, node.Data)) { node.Prefix = v } } } // If we're in the streaming mode, we need to remember the node if it is the target node // so that when we finish processing the node's EndElement, we know how/what to return to // caller. Also we need to remove the target node from the tree upon next Read() call so // memory doesn't grow unbounded. if p.streamElementXPath != nil { if p.streamNode == nil { if QuerySelector(p.doc, p.streamElementXPath) != nil { p.streamNode = node p.streamNodePrev = p.prev streamElementNodeCounter = 1 } } else { streamElementNodeCounter++ } } p.prev = node p.level++ case xml.EndElement: p.level-- // If we're in streaming mode, and we already have a potential streaming // target node identified (p.streamNode != nil) then we need to check if // this is the real one we want to return to caller. if p.streamNode != nil { streamElementNodeCounter-- if streamElementNodeCounter == 0 { // Now we know this element node is the at least passing the initial // p.streamElementXPath check and is a potential target node candidate. // We need to have 1 more check with p.streamElementFilter (if given) to // ensure it is really the element node we want. // The reason we need a two-step check process is because the following // situation: // <AAA><BBB>b1</BBB></AAA> // And say the p.streamElementXPath = "/AAA/BBB[. != 'b1']". Now during // xml.StartElement time, the <BBB> node is still empty, so it will pass // the p.streamElementXPath check. However, eventually we know this <BBB> // shouldn't be returned to the caller. Having a second more fine-grained // filter check ensures that. So in this case, the caller should really // setup the stream parser with: // streamElementXPath = "/AAA/BBB[" // streamElementFilter = "/AAA/BBB[. != 'b1']" if p.streamElementFilter == nil || QuerySelector(p.doc, p.streamElementFilter) != nil { return p.streamNode, nil } // otherwise, this isn't our target node, clean things up. // note we also remove the underlying *Node from the node tree, to prevent // future stream node candidate selection error. RemoveFromTree(p.streamNode) p.prev = p.streamNodePrev p.streamNode = nil p.streamNodePrev = nil } } case xml.CharData: // First, normalize the cache... cached := strings.ToUpper(string(p.reader.Cache())) nodeType := TextNode if strings.HasPrefix(cached, "<![CDATA[") || strings.HasPrefix(cached, "![CDATA[") { nodeType = CharDataNode } node := &Node{Type: nodeType, Data: string(tok), level: p.level} if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } case xml.Comment: node := &Node{Type: CommentNode, Data: string(tok), level: p.level} if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } case xml.ProcInst: // Processing Instruction if p.prev.Type != DeclarationNode { p.level++ } node := &Node{Type: DeclarationNode, Data: tok.Target, level: p.level} pairs := strings.Split(string(tok.Inst), " ") for _, pair := range pairs { pair = strings.TrimSpace(pair) if i := strings.Index(pair, "="); i > 0 { AddAttr(node, pair[:i], strings.Trim(pair[i+1:], `"`)) } } if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level
else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } p.prev = node case xml.Directive: } } } // StreamParser enables loading and parsing an XML document in a streaming // fashion. type StreamParser struct { p *parser } // CreateStreamParser creates a StreamParser. Argument streamElementXPath is // required. // Argument streamElementFilter is optional and should only be used in advanced // scenarios. // // Scenario 1: simple case: // xml := `<AAA><BBB>b1</BBB><BBB>b2</BBB></AAA>` // sp, err := CreateStreamParser(strings.NewReader(xml), "/AAA/BBB") // if err != nil { // panic(err) // } // for { // n, err := sp.Read() // if err != nil { // break // } // fmt.Println(n.OutputXML(true)) // } // Output will be: // <BBB>b1</BBB> // <BBB>b2</BBB> // // Scenario 2: advanced case: // xml := `<AAA><BBB>b1</BBB><BBB>b2</BBB></AAA>` // sp, err := CreateStreamParser(strings.NewReader(xml), "/AAA/BBB", "/AAA/BBB[. != 'b1']") // if err != nil { // panic(err) // } // for { // n, err := sp.Read() // if err != nil { // break // } // fmt.Println(n.OutputXML(true)) // } // Output will be: // <BBB>b2</BBB> // // As the argument names indicate, streamElementXPath should be used for // providing xpath query pointing to the target element node only, no extra // filtering on the element itself or its children; while streamElementFilter, // if needed, can provide additional filtering on the target element and its // children. // // CreateStreamParser returns an error if either streamElementXPath or // streamElementFilter, if provided, cannot be successfully parsed and compiled // into a valid xpath query. func CreateStreamParser(r io.Reader, streamElementXPath string, streamElementFilter ...string) (*StreamParser, error) { return CreateStreamParserWithOptions(r, ParserOptions{}, streamElementXPath, streamElementFilter...) } // CreateStreamParserWithOptions is like CreateStreamParser, but with custom options func CreateStreamParserWithOptions( r io.Reader, options ParserOptions, streamElementXPath string, streamElementFilter ...string, ) (*StreamParser, error) { elemXPath, err := getQuery(streamElementXPath) if err != nil { return nil, fmt.Errorf("invalid streamElementXPath '%s', err: %s", streamElementXPath, err.Error()) } elemFilter := (*xpath.Expr)(nil) if len(streamElementFilter) > 0 { elemFilter, err = getQuery(streamElementFilter[0]) if err != nil { return nil, fmt.Errorf("invalid streamElementFilter '%s', err: %s", streamElementFilter[0], err.Error()) } } parser := createParser(r) options.apply(parser) sp := &StreamParser{ p: parser, } sp.p.streamElementXPath = elemXPath sp.p.streamElementFilter = elemFilter return sp, nil } // Read returns a target node that satisfies the XPath specified by caller at // StreamParser creation time. If there is no more satisfying target nodes after // reading the rest of the XML document, io.EOF will be returned. At any time, // any XML parsing error encountered will be returned, and the stream parsing // stopped. Calling Read() after an error is returned (including io.EOF) results // undefined behavior. Also note, due to the streaming nature, calling Read() // will automatically remove any previous target node(s) from the document tree. func (sp *StreamParser) Read() (*Node, error) { // Because this is a streaming read, we need to release/remove last // target node from the node tree to free up memory. if sp.p.streamNode != nil { // We need to remove all siblings before the current stream node, // because the document may contain unwanted nodes between the target // ones (for example new line text node), which would otherwise // accumulate as first childs, and slow down the stream over time for sp.p.streamNode.PrevSibling != nil { RemoveFromTree(sp.p.streamNode.PrevSibling) } sp.p.prev = sp.p.streamNode.Parent RemoveFromTree(sp.p.streamNode) sp.p.streamNode = nil sp.p.streamNodePrev = nil } return sp.p.parse() }
{ AddChild(p.prev, node) }
conditional_block
parse.go
package xmlquery import ( "bufio" "encoding/xml" "fmt" "io" "net/http" "regexp" "strings" "github.com/antchfx/xpath" "golang.org/x/net/html/charset" ) var xmlMIMERegex = regexp.MustCompile(`(?i)((application|image|message|model)/((\w|\.|-)+\+?)?|text/)(wb)?xml`) // LoadURL loads the XML document from the specified URL. func LoadURL(url string) (*Node, error) { resp, err := http.Get(url) if err != nil { return nil, err } defer resp.Body.Close() // Make sure the Content-Type has a valid XML MIME type if xmlMIMERegex.MatchString(resp.Header.Get("Content-Type")) { return Parse(resp.Body) } return nil, fmt.Errorf("invalid XML document(%s)", resp.Header.Get("Content-Type")) } // Parse returns the parse tree for the XML from the given Reader. func Parse(r io.Reader) (*Node, error) { return ParseWithOptions(r, ParserOptions{}) } // ParseWithOptions is like parse, but with custom options func ParseWithOptions(r io.Reader, options ParserOptions) (*Node, error) { p := createParser(r) options.apply(p) for { _, err := p.parse() if err == io.EOF { return p.doc, nil } if err != nil { return nil, err } } } type parser struct { decoder *xml.Decoder doc *Node level int prev *Node streamElementXPath *xpath.Expr // Under streaming mode, this specifies the xpath to the target element node(s). streamElementFilter *xpath.Expr // If specified, it provides further filtering on the target element. streamNode *Node // Need to remember the last target node So we can clean it up upon next Read() call. streamNodePrev *Node // Need to remember target node's prev so upon target node removal, we can restore correct prev. reader *cachedReader // Need to maintain a reference to the reader, so we can determine whether a node contains CDATA. } func createParser(r io.Reader) *parser { reader := newCachedReader(bufio.NewReader(r))
decoder: xml.NewDecoder(reader), doc: &Node{Type: DocumentNode}, level: 0, reader: reader, } if p.decoder.CharsetReader == nil { p.decoder.CharsetReader = charset.NewReaderLabel } p.prev = p.doc return p } func (p *parser) parse() (*Node, error) { var streamElementNodeCounter int space2prefix := map[string]string{"http://www.w3.org/XML/1998/namespace": "xml"} for { p.reader.StartCaching() tok, err := p.decoder.Token() p.reader.StopCaching() if err != nil { return nil, err } switch tok := tok.(type) { case xml.StartElement: if p.level == 0 { // mising XML declaration attributes := make([]Attr, 1) attributes[0].Name = xml.Name{Local: "version"} attributes[0].Value = "1.0" node := &Node{ Type: DeclarationNode, Data: "xml", Attr: attributes, level: 1, } AddChild(p.prev, node) p.level = 1 p.prev = node } for _, att := range tok.Attr { if att.Name.Local == "xmlns" { space2prefix[att.Value] = "" // reset empty if exist the default namespace // defaultNamespaceURL = att.Value } else if att.Name.Space == "xmlns" { // maybe there are have duplicate NamespaceURL? space2prefix[att.Value] = att.Name.Local } } if space := tok.Name.Space; space != "" { if _, found := space2prefix[space]; !found && p.decoder.Strict { return nil, fmt.Errorf("xmlquery: invalid XML document, namespace %s is missing", space) } } attributes := make([]Attr, len(tok.Attr)) for i, att := range tok.Attr { name := att.Name if prefix, ok := space2prefix[name.Space]; ok { name.Space = prefix } attributes[i] = Attr{ Name: name, Value: att.Value, NamespaceURI: att.Name.Space, } } node := &Node{ Type: ElementNode, Data: tok.Name.Local, NamespaceURI: tok.Name.Space, Attr: attributes, level: p.level, } if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } if node.NamespaceURI != "" { if v, ok := space2prefix[node.NamespaceURI]; ok { cached := string(p.reader.Cache()) if strings.HasPrefix(cached, fmt.Sprintf("%s:%s", v, node.Data)) || strings.HasPrefix(cached, fmt.Sprintf("<%s:%s", v, node.Data)) { node.Prefix = v } } } // If we're in the streaming mode, we need to remember the node if it is the target node // so that when we finish processing the node's EndElement, we know how/what to return to // caller. Also we need to remove the target node from the tree upon next Read() call so // memory doesn't grow unbounded. if p.streamElementXPath != nil { if p.streamNode == nil { if QuerySelector(p.doc, p.streamElementXPath) != nil { p.streamNode = node p.streamNodePrev = p.prev streamElementNodeCounter = 1 } } else { streamElementNodeCounter++ } } p.prev = node p.level++ case xml.EndElement: p.level-- // If we're in streaming mode, and we already have a potential streaming // target node identified (p.streamNode != nil) then we need to check if // this is the real one we want to return to caller. if p.streamNode != nil { streamElementNodeCounter-- if streamElementNodeCounter == 0 { // Now we know this element node is the at least passing the initial // p.streamElementXPath check and is a potential target node candidate. // We need to have 1 more check with p.streamElementFilter (if given) to // ensure it is really the element node we want. // The reason we need a two-step check process is because the following // situation: // <AAA><BBB>b1</BBB></AAA> // And say the p.streamElementXPath = "/AAA/BBB[. != 'b1']". Now during // xml.StartElement time, the <BBB> node is still empty, so it will pass // the p.streamElementXPath check. However, eventually we know this <BBB> // shouldn't be returned to the caller. Having a second more fine-grained // filter check ensures that. So in this case, the caller should really // setup the stream parser with: // streamElementXPath = "/AAA/BBB[" // streamElementFilter = "/AAA/BBB[. != 'b1']" if p.streamElementFilter == nil || QuerySelector(p.doc, p.streamElementFilter) != nil { return p.streamNode, nil } // otherwise, this isn't our target node, clean things up. // note we also remove the underlying *Node from the node tree, to prevent // future stream node candidate selection error. RemoveFromTree(p.streamNode) p.prev = p.streamNodePrev p.streamNode = nil p.streamNodePrev = nil } } case xml.CharData: // First, normalize the cache... cached := strings.ToUpper(string(p.reader.Cache())) nodeType := TextNode if strings.HasPrefix(cached, "<![CDATA[") || strings.HasPrefix(cached, "![CDATA[") { nodeType = CharDataNode } node := &Node{Type: nodeType, Data: string(tok), level: p.level} if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } case xml.Comment: node := &Node{Type: CommentNode, Data: string(tok), level: p.level} if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } case xml.ProcInst: // Processing Instruction if p.prev.Type != DeclarationNode { p.level++ } node := &Node{Type: DeclarationNode, Data: tok.Target, level: p.level} pairs := strings.Split(string(tok.Inst), " ") for _, pair := range pairs { pair = strings.TrimSpace(pair) if i := strings.Index(pair, "="); i > 0 { AddAttr(node, pair[:i], strings.Trim(pair[i+1:], `"`)) } } if p.level == p.prev.level { AddSibling(p.prev, node) } else if p.level > p.prev.level { AddChild(p.prev, node) } else if p.level < p.prev.level { for i := p.prev.level - p.level; i > 1; i-- { p.prev = p.prev.Parent } AddSibling(p.prev.Parent, node) } p.prev = node case xml.Directive: } } } // StreamParser enables loading and parsing an XML document in a streaming // fashion. type StreamParser struct { p *parser } // CreateStreamParser creates a StreamParser. Argument streamElementXPath is // required. // Argument streamElementFilter is optional and should only be used in advanced // scenarios. // // Scenario 1: simple case: // xml := `<AAA><BBB>b1</BBB><BBB>b2</BBB></AAA>` // sp, err := CreateStreamParser(strings.NewReader(xml), "/AAA/BBB") // if err != nil { // panic(err) // } // for { // n, err := sp.Read() // if err != nil { // break // } // fmt.Println(n.OutputXML(true)) // } // Output will be: // <BBB>b1</BBB> // <BBB>b2</BBB> // // Scenario 2: advanced case: // xml := `<AAA><BBB>b1</BBB><BBB>b2</BBB></AAA>` // sp, err := CreateStreamParser(strings.NewReader(xml), "/AAA/BBB", "/AAA/BBB[. != 'b1']") // if err != nil { // panic(err) // } // for { // n, err := sp.Read() // if err != nil { // break // } // fmt.Println(n.OutputXML(true)) // } // Output will be: // <BBB>b2</BBB> // // As the argument names indicate, streamElementXPath should be used for // providing xpath query pointing to the target element node only, no extra // filtering on the element itself or its children; while streamElementFilter, // if needed, can provide additional filtering on the target element and its // children. // // CreateStreamParser returns an error if either streamElementXPath or // streamElementFilter, if provided, cannot be successfully parsed and compiled // into a valid xpath query. func CreateStreamParser(r io.Reader, streamElementXPath string, streamElementFilter ...string) (*StreamParser, error) { return CreateStreamParserWithOptions(r, ParserOptions{}, streamElementXPath, streamElementFilter...) } // CreateStreamParserWithOptions is like CreateStreamParser, but with custom options func CreateStreamParserWithOptions( r io.Reader, options ParserOptions, streamElementXPath string, streamElementFilter ...string, ) (*StreamParser, error) { elemXPath, err := getQuery(streamElementXPath) if err != nil { return nil, fmt.Errorf("invalid streamElementXPath '%s', err: %s", streamElementXPath, err.Error()) } elemFilter := (*xpath.Expr)(nil) if len(streamElementFilter) > 0 { elemFilter, err = getQuery(streamElementFilter[0]) if err != nil { return nil, fmt.Errorf("invalid streamElementFilter '%s', err: %s", streamElementFilter[0], err.Error()) } } parser := createParser(r) options.apply(parser) sp := &StreamParser{ p: parser, } sp.p.streamElementXPath = elemXPath sp.p.streamElementFilter = elemFilter return sp, nil } // Read returns a target node that satisfies the XPath specified by caller at // StreamParser creation time. If there is no more satisfying target nodes after // reading the rest of the XML document, io.EOF will be returned. At any time, // any XML parsing error encountered will be returned, and the stream parsing // stopped. Calling Read() after an error is returned (including io.EOF) results // undefined behavior. Also note, due to the streaming nature, calling Read() // will automatically remove any previous target node(s) from the document tree. func (sp *StreamParser) Read() (*Node, error) { // Because this is a streaming read, we need to release/remove last // target node from the node tree to free up memory. if sp.p.streamNode != nil { // We need to remove all siblings before the current stream node, // because the document may contain unwanted nodes between the target // ones (for example new line text node), which would otherwise // accumulate as first childs, and slow down the stream over time for sp.p.streamNode.PrevSibling != nil { RemoveFromTree(sp.p.streamNode.PrevSibling) } sp.p.prev = sp.p.streamNode.Parent RemoveFromTree(sp.p.streamNode) sp.p.streamNode = nil sp.p.streamNodePrev = nil } return sp.p.parse() }
p := &parser{
random_line_split
RoomNotifs.ts
/* Copyright 2016, 2019, 2023 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import { NotificationCountType, ConditionKind, PushRuleActionName, PushRuleKind, TweakName, } from "matrix-js-sdk/src/matrix"; import type { IPushRule, Room, MatrixClient } from "matrix-js-sdk/src/matrix"; import { NotificationColor } from "./stores/notifications/NotificationColor"; import { getUnsentMessages } from "./components/structures/RoomStatusBar"; import { doesRoomHaveUnreadMessages, doesRoomOrThreadHaveUnreadMessages } from "./Unread"; import { EffectiveMembership, getEffectiveMembership } from "./utils/membership"; import SettingsStore from "./settings/SettingsStore"; export enum RoomNotifState { AllMessagesLoud = "all_messages_loud", AllMessages = "all_messages", MentionsOnly = "mentions_only", Mute = "mute", } export function getRoomNotifsState(client: MatrixClient, roomId: string): RoomNotifState | null
export function setRoomNotifsState(client: MatrixClient, roomId: string, newState: RoomNotifState): Promise<void> { if (newState === RoomNotifState.Mute) { return setRoomNotifsStateMuted(client, roomId); } else { return setRoomNotifsStateUnmuted(client, roomId, newState); } } export function getUnreadNotificationCount(room: Room, type: NotificationCountType, threadId?: string): number { let notificationCount = !!threadId ? room.getThreadUnreadNotificationCount(threadId, type) : room.getUnreadNotificationCount(type); // Check notification counts in the old room just in case there's some lost // there. We only go one level down to avoid performance issues, and theory // is that 1st generation rooms will have already been read by the 3rd generation. const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors"); const predecessor = room.findPredecessor(msc3946ProcessDynamicPredecessor); // Exclude threadId, as the same thread can't continue over a room upgrade if (!threadId && predecessor?.roomId) { const oldRoomId = predecessor.roomId; const oldRoom = room.client.getRoom(oldRoomId); if (oldRoom) { // We only ever care if there's highlights in the old room. No point in // notifying the user for unread messages because they would have extreme // difficulty changing their notification preferences away from "All Messages" // and "Noisy". notificationCount += oldRoom.getUnreadNotificationCount(NotificationCountType.Highlight); } } return notificationCount; } function setRoomNotifsStateMuted(cli: MatrixClient, roomId: string): Promise<any> { const promises: Promise<unknown>[] = []; // delete the room rule const roomRule = cli.getRoomPushRule("global", roomId); if (roomRule) { promises.push(cli.deletePushRule("global", PushRuleKind.RoomSpecific, roomRule.rule_id)); } // add/replace an override rule to squelch everything in this room // NB. We use the room ID as the name of this rule too, although this // is an override rule, not a room rule: it still pertains to this room // though, so using the room ID as the rule ID is logical and prevents // duplicate copies of the rule. promises.push( cli.addPushRule("global", PushRuleKind.Override, roomId, { conditions: [ { kind: ConditionKind.EventMatch, key: "room_id", pattern: roomId, }, ], actions: [PushRuleActionName.DontNotify], }), ); return Promise.all(promises); } function setRoomNotifsStateUnmuted(cli: MatrixClient, roomId: string, newState: RoomNotifState): Promise<any> { const promises: Promise<unknown>[] = []; const overrideMuteRule = findOverrideMuteRule(cli, roomId); if (overrideMuteRule) { promises.push(cli.deletePushRule("global", PushRuleKind.Override, overrideMuteRule.rule_id)); } if (newState === RoomNotifState.AllMessages) { const roomRule = cli.getRoomPushRule("global", roomId); if (roomRule) { promises.push(cli.deletePushRule("global", PushRuleKind.RoomSpecific, roomRule.rule_id)); } } else if (newState === RoomNotifState.MentionsOnly) { promises.push( cli.addPushRule("global", PushRuleKind.RoomSpecific, roomId, { actions: [PushRuleActionName.DontNotify], }), ); } else if (newState === RoomNotifState.AllMessagesLoud) { promises.push( cli.addPushRule("global", PushRuleKind.RoomSpecific, roomId, { actions: [ PushRuleActionName.Notify, { set_tweak: TweakName.Sound, value: "default", }, ], }), ); } return Promise.all(promises); } function findOverrideMuteRule(cli: MatrixClient | undefined, roomId: string): IPushRule | null { if (!cli?.pushRules?.global?.override) { return null; } for (const rule of cli.pushRules.global.override) { if (rule.enabled && isRuleRoomMuteRuleForRoomId(roomId, rule)) { return rule; } } return null; } /** * Checks if a given rule is a room mute rule as implemented by EW * - matches every event in one room (one condition that is an event match on roomId) * - silences notifications (one action that is `DontNotify`) * @param rule - push rule * @returns {boolean} - true when rule mutes a room */ export function isRuleMaybeRoomMuteRule(rule: IPushRule): boolean { return ( // matches every event in one room rule.conditions?.length === 1 && rule.conditions[0].kind === ConditionKind.EventMatch && rule.conditions[0].key === "room_id" && // silences notifications isMuteRule(rule) ); } /** * Checks if a given rule is a room mute rule as implemented by EW * @param roomId - id of room to match * @param rule - push rule * @returns {boolean} true when rule mutes the given room */ function isRuleRoomMuteRuleForRoomId(roomId: string, rule: IPushRule): boolean { if (!isRuleMaybeRoomMuteRule(rule)) { return false; } // isRuleMaybeRoomMuteRule checks this condition exists const cond = rule.conditions![0]!; return cond.pattern === roomId; } function isMuteRule(rule: IPushRule): boolean { // DontNotify is equivalent to the empty actions array return ( rule.actions.length === 0 || (rule.actions.length === 1 && rule.actions[0] === PushRuleActionName.DontNotify) ); } export function determineUnreadState( room?: Room, threadId?: string, ): { color: NotificationColor; symbol: string | null; count: number } { if (!room) { return { symbol: null, count: 0, color: NotificationColor.None }; } if (getUnsentMessages(room, threadId).length > 0) { return { symbol: "!", count: 1, color: NotificationColor.Unsent }; } if (getEffectiveMembership(room.getMyMembership()) === EffectiveMembership.Invite) { return { symbol: "!", count: 1, color: NotificationColor.Red }; } if (getRoomNotifsState(room.client, room.roomId) === RoomNotifState.Mute) { return { symbol: null, count: 0, color: NotificationColor.None }; } const redNotifs = getUnreadNotificationCount(room, NotificationCountType.Highlight, threadId); const greyNotifs = getUnreadNotificationCount(room, NotificationCountType.Total, threadId); const trueCount = greyNotifs || redNotifs; if (redNotifs > 0) { return { symbol: null, count: trueCount, color: NotificationColor.Red }; } if (greyNotifs > 0) { return { symbol: null, count: trueCount, color: NotificationColor.Grey }; } // We don't have any notified messages, but we might have unread messages. Let's find out. let hasUnread: boolean; if (threadId) hasUnread = doesRoomOrThreadHaveUnreadMessages(room.getThread(threadId)!); else hasUnread = doesRoomHaveUnreadMessages(room); return { symbol: null, count: trueCount, color: hasUnread ? NotificationColor.Bold : NotificationColor.None, }; }
{ if (client.isGuest()) return RoomNotifState.AllMessages; // look through the override rules for a rule affecting this room: // if one exists, it will take precedence. const muteRule = findOverrideMuteRule(client, roomId); if (muteRule) { return RoomNotifState.Mute; } // for everything else, look at the room rule. let roomRule: IPushRule | undefined; try { roomRule = client.getRoomPushRule("global", roomId); } catch (err) { // Possible that the client doesn't have pushRules yet. If so, it // hasn't started either, so indicate that this room is not notifying. return null; } // XXX: We have to assume the default is to notify for all messages // (in particular this will be 'wrong' for one to one rooms because // they will notify loudly for all messages) if (!roomRule?.enabled) return RoomNotifState.AllMessages; // a mute at the room level will still allow mentions // to notify if (isMuteRule(roomRule)) return RoomNotifState.MentionsOnly; const actionsObject = PushProcessor.actionListToActionsObject(roomRule.actions); if (actionsObject.tweaks.sound) return RoomNotifState.AllMessagesLoud; return null; }
identifier_body
RoomNotifs.ts
/* Copyright 2016, 2019, 2023 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import { NotificationCountType, ConditionKind, PushRuleActionName, PushRuleKind, TweakName, } from "matrix-js-sdk/src/matrix"; import type { IPushRule, Room, MatrixClient } from "matrix-js-sdk/src/matrix"; import { NotificationColor } from "./stores/notifications/NotificationColor"; import { getUnsentMessages } from "./components/structures/RoomStatusBar"; import { doesRoomHaveUnreadMessages, doesRoomOrThreadHaveUnreadMessages } from "./Unread"; import { EffectiveMembership, getEffectiveMembership } from "./utils/membership"; import SettingsStore from "./settings/SettingsStore"; export enum RoomNotifState { AllMessagesLoud = "all_messages_loud", AllMessages = "all_messages", MentionsOnly = "mentions_only", Mute = "mute", } export function getRoomNotifsState(client: MatrixClient, roomId: string): RoomNotifState | null { if (client.isGuest()) return RoomNotifState.AllMessages; // look through the override rules for a rule affecting this room: // if one exists, it will take precedence. const muteRule = findOverrideMuteRule(client, roomId); if (muteRule) { return RoomNotifState.Mute; } // for everything else, look at the room rule. let roomRule: IPushRule | undefined; try { roomRule = client.getRoomPushRule("global", roomId); } catch (err) { // Possible that the client doesn't have pushRules yet. If so, it // hasn't started either, so indicate that this room is not notifying. return null; } // XXX: We have to assume the default is to notify for all messages // (in particular this will be 'wrong' for one to one rooms because // they will notify loudly for all messages) if (!roomRule?.enabled) return RoomNotifState.AllMessages; // a mute at the room level will still allow mentions // to notify if (isMuteRule(roomRule)) return RoomNotifState.MentionsOnly; const actionsObject = PushProcessor.actionListToActionsObject(roomRule.actions); if (actionsObject.tweaks.sound) return RoomNotifState.AllMessagesLoud; return null; } export function setRoomNotifsState(client: MatrixClient, roomId: string, newState: RoomNotifState): Promise<void> { if (newState === RoomNotifState.Mute) { return setRoomNotifsStateMuted(client, roomId); } else { return setRoomNotifsStateUnmuted(client, roomId, newState); } } export function getUnreadNotificationCount(room: Room, type: NotificationCountType, threadId?: string): number { let notificationCount = !!threadId ? room.getThreadUnreadNotificationCount(threadId, type) : room.getUnreadNotificationCount(type); // Check notification counts in the old room just in case there's some lost // there. We only go one level down to avoid performance issues, and theory // is that 1st generation rooms will have already been read by the 3rd generation. const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors"); const predecessor = room.findPredecessor(msc3946ProcessDynamicPredecessor); // Exclude threadId, as the same thread can't continue over a room upgrade if (!threadId && predecessor?.roomId) { const oldRoomId = predecessor.roomId; const oldRoom = room.client.getRoom(oldRoomId); if (oldRoom) { // We only ever care if there's highlights in the old room. No point in // notifying the user for unread messages because they would have extreme // difficulty changing their notification preferences away from "All Messages" // and "Noisy". notificationCount += oldRoom.getUnreadNotificationCount(NotificationCountType.Highlight); } } return notificationCount; } function setRoomNotifsStateMuted(cli: MatrixClient, roomId: string): Promise<any> { const promises: Promise<unknown>[] = []; // delete the room rule const roomRule = cli.getRoomPushRule("global", roomId); if (roomRule) { promises.push(cli.deletePushRule("global", PushRuleKind.RoomSpecific, roomRule.rule_id)); } // add/replace an override rule to squelch everything in this room // NB. We use the room ID as the name of this rule too, although this // is an override rule, not a room rule: it still pertains to this room // though, so using the room ID as the rule ID is logical and prevents // duplicate copies of the rule. promises.push( cli.addPushRule("global", PushRuleKind.Override, roomId, { conditions: [ { kind: ConditionKind.EventMatch, key: "room_id", pattern: roomId, }, ], actions: [PushRuleActionName.DontNotify], }), ); return Promise.all(promises); } function setRoomNotifsStateUnmuted(cli: MatrixClient, roomId: string, newState: RoomNotifState): Promise<any> { const promises: Promise<unknown>[] = []; const overrideMuteRule = findOverrideMuteRule(cli, roomId); if (overrideMuteRule) { promises.push(cli.deletePushRule("global", PushRuleKind.Override, overrideMuteRule.rule_id)); } if (newState === RoomNotifState.AllMessages) { const roomRule = cli.getRoomPushRule("global", roomId); if (roomRule) { promises.push(cli.deletePushRule("global", PushRuleKind.RoomSpecific, roomRule.rule_id)); } } else if (newState === RoomNotifState.MentionsOnly) { promises.push( cli.addPushRule("global", PushRuleKind.RoomSpecific, roomId, { actions: [PushRuleActionName.DontNotify], }), ); } else if (newState === RoomNotifState.AllMessagesLoud) { promises.push( cli.addPushRule("global", PushRuleKind.RoomSpecific, roomId, { actions: [ PushRuleActionName.Notify, { set_tweak: TweakName.Sound, value: "default", }, ], }), ); } return Promise.all(promises); } function findOverrideMuteRule(cli: MatrixClient | undefined, roomId: string): IPushRule | null { if (!cli?.pushRules?.global?.override) { return null; } for (const rule of cli.pushRules.global.override) { if (rule.enabled && isRuleRoomMuteRuleForRoomId(roomId, rule)) { return rule; } } return null; } /** * Checks if a given rule is a room mute rule as implemented by EW * - matches every event in one room (one condition that is an event match on roomId) * - silences notifications (one action that is `DontNotify`) * @param rule - push rule * @returns {boolean} - true when rule mutes a room */ export function isRuleMaybeRoomMuteRule(rule: IPushRule): boolean { return ( // matches every event in one room rule.conditions?.length === 1 && rule.conditions[0].kind === ConditionKind.EventMatch && rule.conditions[0].key === "room_id" && // silences notifications isMuteRule(rule) ); } /** * Checks if a given rule is a room mute rule as implemented by EW * @param roomId - id of room to match * @param rule - push rule * @returns {boolean} true when rule mutes the given room */ function isRuleRoomMuteRuleForRoomId(roomId: string, rule: IPushRule): boolean { if (!isRuleMaybeRoomMuteRule(rule)) { return false; } // isRuleMaybeRoomMuteRule checks this condition exists const cond = rule.conditions![0]!; return cond.pattern === roomId; } function
(rule: IPushRule): boolean { // DontNotify is equivalent to the empty actions array return ( rule.actions.length === 0 || (rule.actions.length === 1 && rule.actions[0] === PushRuleActionName.DontNotify) ); } export function determineUnreadState( room?: Room, threadId?: string, ): { color: NotificationColor; symbol: string | null; count: number } { if (!room) { return { symbol: null, count: 0, color: NotificationColor.None }; } if (getUnsentMessages(room, threadId).length > 0) { return { symbol: "!", count: 1, color: NotificationColor.Unsent }; } if (getEffectiveMembership(room.getMyMembership()) === EffectiveMembership.Invite) { return { symbol: "!", count: 1, color: NotificationColor.Red }; } if (getRoomNotifsState(room.client, room.roomId) === RoomNotifState.Mute) { return { symbol: null, count: 0, color: NotificationColor.None }; } const redNotifs = getUnreadNotificationCount(room, NotificationCountType.Highlight, threadId); const greyNotifs = getUnreadNotificationCount(room, NotificationCountType.Total, threadId); const trueCount = greyNotifs || redNotifs; if (redNotifs > 0) { return { symbol: null, count: trueCount, color: NotificationColor.Red }; } if (greyNotifs > 0) { return { symbol: null, count: trueCount, color: NotificationColor.Grey }; } // We don't have any notified messages, but we might have unread messages. Let's find out. let hasUnread: boolean; if (threadId) hasUnread = doesRoomOrThreadHaveUnreadMessages(room.getThread(threadId)!); else hasUnread = doesRoomHaveUnreadMessages(room); return { symbol: null, count: trueCount, color: hasUnread ? NotificationColor.Bold : NotificationColor.None, }; }
isMuteRule
identifier_name
RoomNotifs.ts
/* Copyright 2016, 2019, 2023 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { PushProcessor } from "matrix-js-sdk/src/pushprocessor"; import { NotificationCountType, ConditionKind, PushRuleActionName, PushRuleKind, TweakName, } from "matrix-js-sdk/src/matrix"; import type { IPushRule, Room, MatrixClient } from "matrix-js-sdk/src/matrix"; import { NotificationColor } from "./stores/notifications/NotificationColor"; import { getUnsentMessages } from "./components/structures/RoomStatusBar"; import { doesRoomHaveUnreadMessages, doesRoomOrThreadHaveUnreadMessages } from "./Unread"; import { EffectiveMembership, getEffectiveMembership } from "./utils/membership"; import SettingsStore from "./settings/SettingsStore"; export enum RoomNotifState { AllMessagesLoud = "all_messages_loud", AllMessages = "all_messages", MentionsOnly = "mentions_only", Mute = "mute", } export function getRoomNotifsState(client: MatrixClient, roomId: string): RoomNotifState | null { if (client.isGuest()) return RoomNotifState.AllMessages; // look through the override rules for a rule affecting this room: // if one exists, it will take precedence. const muteRule = findOverrideMuteRule(client, roomId); if (muteRule) { return RoomNotifState.Mute; } // for everything else, look at the room rule. let roomRule: IPushRule | undefined; try { roomRule = client.getRoomPushRule("global", roomId); } catch (err) { // Possible that the client doesn't have pushRules yet. If so, it // hasn't started either, so indicate that this room is not notifying. return null; } // XXX: We have to assume the default is to notify for all messages // (in particular this will be 'wrong' for one to one rooms because // they will notify loudly for all messages) if (!roomRule?.enabled) return RoomNotifState.AllMessages; // a mute at the room level will still allow mentions // to notify if (isMuteRule(roomRule)) return RoomNotifState.MentionsOnly; const actionsObject = PushProcessor.actionListToActionsObject(roomRule.actions); if (actionsObject.tweaks.sound) return RoomNotifState.AllMessagesLoud; return null; } export function setRoomNotifsState(client: MatrixClient, roomId: string, newState: RoomNotifState): Promise<void> { if (newState === RoomNotifState.Mute) { return setRoomNotifsStateMuted(client, roomId); } else { return setRoomNotifsStateUnmuted(client, roomId, newState); } } export function getUnreadNotificationCount(room: Room, type: NotificationCountType, threadId?: string): number { let notificationCount = !!threadId ? room.getThreadUnreadNotificationCount(threadId, type) : room.getUnreadNotificationCount(type); // Check notification counts in the old room just in case there's some lost // there. We only go one level down to avoid performance issues, and theory // is that 1st generation rooms will have already been read by the 3rd generation. const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors"); const predecessor = room.findPredecessor(msc3946ProcessDynamicPredecessor); // Exclude threadId, as the same thread can't continue over a room upgrade if (!threadId && predecessor?.roomId) { const oldRoomId = predecessor.roomId; const oldRoom = room.client.getRoom(oldRoomId); if (oldRoom) { // We only ever care if there's highlights in the old room. No point in // notifying the user for unread messages because they would have extreme // difficulty changing their notification preferences away from "All Messages" // and "Noisy". notificationCount += oldRoom.getUnreadNotificationCount(NotificationCountType.Highlight); } } return notificationCount; } function setRoomNotifsStateMuted(cli: MatrixClient, roomId: string): Promise<any> { const promises: Promise<unknown>[] = []; // delete the room rule const roomRule = cli.getRoomPushRule("global", roomId); if (roomRule) { promises.push(cli.deletePushRule("global", PushRuleKind.RoomSpecific, roomRule.rule_id)); } // add/replace an override rule to squelch everything in this room // NB. We use the room ID as the name of this rule too, although this // is an override rule, not a room rule: it still pertains to this room // though, so using the room ID as the rule ID is logical and prevents // duplicate copies of the rule. promises.push( cli.addPushRule("global", PushRuleKind.Override, roomId, { conditions: [ { kind: ConditionKind.EventMatch, key: "room_id", pattern: roomId, }, ], actions: [PushRuleActionName.DontNotify], }), ); return Promise.all(promises); } function setRoomNotifsStateUnmuted(cli: MatrixClient, roomId: string, newState: RoomNotifState): Promise<any> { const promises: Promise<unknown>[] = []; const overrideMuteRule = findOverrideMuteRule(cli, roomId); if (overrideMuteRule) { promises.push(cli.deletePushRule("global", PushRuleKind.Override, overrideMuteRule.rule_id)); } if (newState === RoomNotifState.AllMessages) { const roomRule = cli.getRoomPushRule("global", roomId); if (roomRule) { promises.push(cli.deletePushRule("global", PushRuleKind.RoomSpecific, roomRule.rule_id)); } } else if (newState === RoomNotifState.MentionsOnly) { promises.push( cli.addPushRule("global", PushRuleKind.RoomSpecific, roomId, { actions: [PushRuleActionName.DontNotify], }), ); } else if (newState === RoomNotifState.AllMessagesLoud) { promises.push( cli.addPushRule("global", PushRuleKind.RoomSpecific, roomId, { actions: [ PushRuleActionName.Notify, { set_tweak: TweakName.Sound, value: "default", }, ], }), ); } return Promise.all(promises); } function findOverrideMuteRule(cli: MatrixClient | undefined, roomId: string): IPushRule | null { if (!cli?.pushRules?.global?.override) { return null; } for (const rule of cli.pushRules.global.override) { if (rule.enabled && isRuleRoomMuteRuleForRoomId(roomId, rule)) { return rule; } } return null; } /** * Checks if a given rule is a room mute rule as implemented by EW * - matches every event in one room (one condition that is an event match on roomId) * - silences notifications (one action that is `DontNotify`) * @param rule - push rule * @returns {boolean} - true when rule mutes a room */ export function isRuleMaybeRoomMuteRule(rule: IPushRule): boolean { return ( // matches every event in one room rule.conditions?.length === 1 && rule.conditions[0].kind === ConditionKind.EventMatch && rule.conditions[0].key === "room_id" && // silences notifications isMuteRule(rule) ); } /** * Checks if a given rule is a room mute rule as implemented by EW * @param roomId - id of room to match * @param rule - push rule * @returns {boolean} true when rule mutes the given room */ function isRuleRoomMuteRuleForRoomId(roomId: string, rule: IPushRule): boolean { if (!isRuleMaybeRoomMuteRule(rule)) { return false; } // isRuleMaybeRoomMuteRule checks this condition exists const cond = rule.conditions![0]!; return cond.pattern === roomId; }
rule.actions.length === 0 || (rule.actions.length === 1 && rule.actions[0] === PushRuleActionName.DontNotify) ); } export function determineUnreadState( room?: Room, threadId?: string, ): { color: NotificationColor; symbol: string | null; count: number } { if (!room) { return { symbol: null, count: 0, color: NotificationColor.None }; } if (getUnsentMessages(room, threadId).length > 0) { return { symbol: "!", count: 1, color: NotificationColor.Unsent }; } if (getEffectiveMembership(room.getMyMembership()) === EffectiveMembership.Invite) { return { symbol: "!", count: 1, color: NotificationColor.Red }; } if (getRoomNotifsState(room.client, room.roomId) === RoomNotifState.Mute) { return { symbol: null, count: 0, color: NotificationColor.None }; } const redNotifs = getUnreadNotificationCount(room, NotificationCountType.Highlight, threadId); const greyNotifs = getUnreadNotificationCount(room, NotificationCountType.Total, threadId); const trueCount = greyNotifs || redNotifs; if (redNotifs > 0) { return { symbol: null, count: trueCount, color: NotificationColor.Red }; } if (greyNotifs > 0) { return { symbol: null, count: trueCount, color: NotificationColor.Grey }; } // We don't have any notified messages, but we might have unread messages. Let's find out. let hasUnread: boolean; if (threadId) hasUnread = doesRoomOrThreadHaveUnreadMessages(room.getThread(threadId)!); else hasUnread = doesRoomHaveUnreadMessages(room); return { symbol: null, count: trueCount, color: hasUnread ? NotificationColor.Bold : NotificationColor.None, }; }
function isMuteRule(rule: IPushRule): boolean { // DontNotify is equivalent to the empty actions array return (
random_line_split
parser.rs
// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License.. //! A private parser implementation of IPv4, IPv6, and socket addresses. //! //! This module is "publicly exported" through the `FromStr` implementations //! below. use crate::error::Error; use crate::fmt; use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use crate::str::FromStr; trait ReadNumberHelper: crate::marker::Sized { const ZERO: Self; fn checked_mul(&self, other: u32) -> Option<Self>; fn checked_add(&self, other: u32) -> Option<Self>; } macro_rules! impl_helper { ($($t:ty)*) => ($(impl ReadNumberHelper for $t { const ZERO: Self = 0; #[inline] fn checked_mul(&self, other: u32) -> Option<Self> { Self::checked_mul(*self, other.try_into().ok()?) } #[inline] fn checked_add(&self, other: u32) -> Option<Self> { Self::checked_add(*self, other.try_into().ok()?) } })*) } impl_helper! { u8 u16 u32 } struct Parser<'a> { // Parsing as ASCII, so can use byte array. state: &'a [u8], } impl<'a> Parser<'a> { fn new(input: &'a [u8]) -> Parser<'a> { Parser { state: input } } /// Run a parser, and restore the pre-parse state if it fails. fn read_atomically<T, F>(&mut self, inner: F) -> Option<T> where F: FnOnce(&mut Parser<'_>) -> Option<T>, { let state = self.state; let result = inner(self); if result.is_none() { self.state = state; } result } /// Run a parser, but fail if the entire input wasn't consumed. /// Doesn't run atomically. fn parse_with<T, F>(&mut self, inner: F, kind: AddrKind) -> Result<T, AddrParseError> where F: FnOnce(&mut Parser<'_>) -> Option<T>, { let result = inner(self); if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(kind)) } /// Peek the next character from the input fn peek_char(&self) -> Option<char> { self.state.first().map(|&b| char::from(b)) } /// Read the next character from the input fn read_char(&mut self) -> Option<char> { self.state.split_first().map(|(&b, tail)| {
#[must_use] /// Read the next character from the input if it matches the target. fn read_given_char(&mut self, target: char) -> Option<()> { self.read_atomically(|p| { p.read_char().and_then(|c| if c == target { Some(()) } else { None }) }) } /// Helper for reading separators in an indexed loop. Reads the separator /// character iff index > 0, then runs the parser. When used in a loop, /// the separator character will only be read on index > 0 (see /// read_ipv4_addr for an example) fn read_separator<T, F>(&mut self, sep: char, index: usize, inner: F) -> Option<T> where F: FnOnce(&mut Parser<'_>) -> Option<T>, { self.read_atomically(move |p| { if index > 0 { p.read_given_char(sep)?; } inner(p) }) } // Read a number off the front of the input in the given radix, stopping // at the first non-digit character or eof. Fails if the number has more // digits than max_digits or if there is no number. #[allow(clippy::if_same_then_else)] fn read_number<T: ReadNumberHelper>( &mut self, radix: u32, max_digits: Option<usize>, allow_zero_prefix: bool, ) -> Option<T> { self.read_atomically(move |p| { let mut result = T::ZERO; let mut digit_count = 0; let has_leading_zero = p.peek_char() == Some('0'); while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) { result = result.checked_mul(radix)?; result = result.checked_add(digit)?; digit_count += 1; if let Some(max_digits) = max_digits { if digit_count > max_digits { return None; } } } if digit_count == 0 { None } else if !allow_zero_prefix && has_leading_zero && digit_count > 1 { None } else { Some(result) } }) } /// Read an IPv4 address. fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> { self.read_atomically(|p| { let mut groups = [0; 4]; for (i, slot) in groups.iter_mut().enumerate() { *slot = p.read_separator('.', i, |p| { // Disallow octal number in IP string. // https://tools.ietf.org/html/rfc6943#section-3.1.1 p.read_number(10, Some(3), false) })?; } Some(groups.into()) }) } /// Read an IPv6 Address. fn read_ipv6_addr(&mut self) -> Option<Ipv6Addr> { /// Read a chunk of an IPv6 address into `groups`. Returns the number /// of groups read, along with a bool indicating if an embedded /// trailing IPv4 address was read. Specifically, read a series of /// colon-separated IPv6 groups (0x0000 - 0xFFFF), with an optional /// trailing embedded IPv4 address. fn read_groups(p: &mut Parser<'_>, groups: &mut [u16]) -> (usize, bool) { let limit = groups.len(); for (i, slot) in groups.iter_mut().enumerate() { // Try to read a trailing embedded IPv4 address. There must be // at least two groups left. if i < limit - 1 { let ipv4 = p.read_separator(':', i, |p| p.read_ipv4_addr()); if let Some(v4_addr) = ipv4 { let [one, two, three, four] = v4_addr.octets(); groups[i] = u16::from_be_bytes([one, two]); groups[i + 1] = u16::from_be_bytes([three, four]); return (i + 2, true); } } let group = p.read_separator(':', i, |p| p.read_number(16, Some(4), true)); match group { Some(g) => *slot = g, None => return (i, false), } } (groups.len(), false) } self.read_atomically(|p| { // Read the front part of the address; either the whole thing, or up // to the first :: let mut head = [0; 8]; let (head_size, head_ipv4) = read_groups(p, &mut head); if head_size == 8 { return Some(head.into()); } // IPv4 part is not allowed before `::` if head_ipv4 { return None; } // Read `::` if previous code parsed less than 8 groups. // `::` indicates one or more groups of 16 bits of zeros. p.read_given_char(':')?; p.read_given_char(':')?; // Read the back part of the address. The :: must contain at least one // set of zeroes, so our max length is 7. let mut tail = [0; 7]; let limit = 8 - (head_size + 1); let (tail_size, _) = read_groups(p, &mut tail[..limit]); // Concat the head and tail of the IP address head[(8 - tail_size)..8].copy_from_slice(&tail[..tail_size]); Some(head.into()) }) } /// Read an IP Address, either IPv4 or IPv6. fn read_ip_addr(&mut self) -> Option<IpAddr> { self.read_ipv4_addr().map(IpAddr::V4).or_else(move || self.read_ipv6_addr().map(IpAddr::V6)) } /// Read a `:` followed by a port in base 10. fn read_port(&mut self) -> Option<u16> { self.read_atomically(|p| { p.read_given_char(':')?; p.read_number(10, None, true) }) } /// Read a `%` followed by a scope ID in base 10. fn read_scope_id(&mut self) -> Option<u32> { self.read_atomically(|p| { p.read_given_char('%')?; p.read_number(10, None, true) }) } /// Read an IPv4 address with a port. fn read_socket_addr_v4(&mut self) -> Option<SocketAddrV4> { self.read_atomically(|p| { let ip = p.read_ipv4_addr()?; let port = p.read_port()?; Some(SocketAddrV4::new(ip, port)) }) } /// Read an IPv6 address with a port. fn read_socket_addr_v6(&mut self) -> Option<SocketAddrV6> { self.read_atomically(|p| { p.read_given_char('[')?; let ip = p.read_ipv6_addr()?; let scope_id = p.read_scope_id().unwrap_or(0); p.read_given_char(']')?; let port = p.read_port()?; Some(SocketAddrV6::new(ip, port, 0, scope_id)) }) } /// Read an IP address with a port fn read_socket_addr(&mut self) -> Option<SocketAddr> { self.read_socket_addr_v4() .map(SocketAddr::V4) .or_else(|| self.read_socket_addr_v6().map(SocketAddr::V6)) } } impl IpAddr { /// Parse an IP address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); /// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); /// /// assert_eq!(IpAddr::parse_ascii(b"127.0.0.1"), Ok(localhost_v4)); /// assert_eq!(IpAddr::parse_ascii(b"::1"), Ok(localhost_v6)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_ip_addr(), AddrKind::Ip) } } impl FromStr for IpAddr { type Err = AddrParseError; fn from_str(s: &str) -> Result<IpAddr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl Ipv4Addr { /// Parse an IPv4 address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::Ipv4Addr; /// /// let localhost = Ipv4Addr::new(127, 0, 0, 1); /// /// assert_eq!(Ipv4Addr::parse_ascii(b"127.0.0.1"), Ok(localhost)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { // don't try to parse if too long if b.len() > 15 { Err(AddrParseError(AddrKind::Ipv4)) } else { Parser::new(b).parse_with(|p| p.read_ipv4_addr(), AddrKind::Ipv4) } } } impl FromStr for Ipv4Addr { type Err = AddrParseError; fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl Ipv6Addr { /// Parse an IPv6 address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::Ipv6Addr; /// /// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1); /// /// assert_eq!(Ipv6Addr::parse_ascii(b"::1"), Ok(localhost)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_ipv6_addr(), AddrKind::Ipv6) } } impl FromStr for Ipv6Addr { type Err = AddrParseError; fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl SocketAddrV4 { /// Parse an IPv4 socket address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{Ipv4Addr, SocketAddrV4}; /// /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); /// /// assert_eq!(SocketAddrV4::parse_ascii(b"127.0.0.1:8080"), Ok(socket)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_socket_addr_v4(), AddrKind::SocketV4) } } impl FromStr for SocketAddrV4 { type Err = AddrParseError; fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl SocketAddrV6 { /// Parse an IPv6 socket address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{Ipv6Addr, SocketAddrV6}; /// /// let socket = SocketAddrV6::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1), 8080, 0, 0); /// /// assert_eq!(SocketAddrV6::parse_ascii(b"[2001:db8::1]:8080"), Ok(socket)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_socket_addr_v6(), AddrKind::SocketV6) } } impl FromStr for SocketAddrV6 { type Err = AddrParseError; fn from_str(s: &str) -> Result<SocketAddrV6, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl SocketAddr { /// Parse a socket address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; /// /// let socket_v4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); /// let socket_v6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 8080); /// /// assert_eq!(SocketAddr::parse_ascii(b"127.0.0.1:8080"), Ok(socket_v4)); /// assert_eq!(SocketAddr::parse_ascii(b"[::1]:8080"), Ok(socket_v6)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_socket_addr(), AddrKind::Socket) } } impl FromStr for SocketAddr { type Err = AddrParseError; fn from_str(s: &str) -> Result<SocketAddr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } #[derive(Debug, Clone, PartialEq, Eq)] enum AddrKind { Ip, Ipv4, Ipv6, Socket, SocketV4, SocketV6, } /// An error which can be returned when parsing an IP address or a socket address. /// /// This error is used as the error type for the [`FromStr`] implementation for /// [`IpAddr`], [`Ipv4Addr`], [`Ipv6Addr`], [`SocketAddr`], [`SocketAddrV4`], and /// [`SocketAddrV6`]. /// /// # Potential causes /// /// `AddrParseError` may be thrown because the provided string does not parse as the given type, /// often because it includes information only handled by a different address type. /// /// ```should_panic /// use std::net::IpAddr; /// let _foo: IpAddr = "127.0.0.1:8080".parse().expect("Cannot handle the socket port"); /// ``` /// /// [`IpAddr`] doesn't handle the port. Use [`SocketAddr`] instead. /// /// ``` /// use std::net::SocketAddr; /// /// // No problem, the `panic!` message has disappeared. /// let _foo: SocketAddr = "127.0.0.1:8080".parse().expect("unreachable panic"); /// ``` #[derive(Debug, Clone, PartialEq, Eq)] pub struct AddrParseError(AddrKind); impl fmt::Display for AddrParseError { #[allow(deprecated, deprecated_in_future)] fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.write_str(self.description()) } } impl Error for AddrParseError { #[allow(deprecated)] fn description(&self) -> &str { match self.0 { AddrKind::Ip => "invalid IP address syntax", AddrKind::Ipv4 => "invalid IPv4 address syntax", AddrKind::Ipv6 => "invalid IPv6 address syntax", AddrKind::Socket => "invalid socket address syntax", AddrKind::SocketV4 => "invalid IPv4 socket address syntax", AddrKind::SocketV6 => "invalid IPv6 socket address syntax", } } }
self.state = tail; char::from(b) }) }
random_line_split
parser.rs
// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License.. //! A private parser implementation of IPv4, IPv6, and socket addresses. //! //! This module is "publicly exported" through the `FromStr` implementations //! below. use crate::error::Error; use crate::fmt; use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use crate::str::FromStr; trait ReadNumberHelper: crate::marker::Sized { const ZERO: Self; fn checked_mul(&self, other: u32) -> Option<Self>; fn checked_add(&self, other: u32) -> Option<Self>; } macro_rules! impl_helper { ($($t:ty)*) => ($(impl ReadNumberHelper for $t { const ZERO: Self = 0; #[inline] fn checked_mul(&self, other: u32) -> Option<Self> { Self::checked_mul(*self, other.try_into().ok()?) } #[inline] fn checked_add(&self, other: u32) -> Option<Self> { Self::checked_add(*self, other.try_into().ok()?) } })*) } impl_helper! { u8 u16 u32 } struct Parser<'a> { // Parsing as ASCII, so can use byte array. state: &'a [u8], } impl<'a> Parser<'a> { fn new(input: &'a [u8]) -> Parser<'a> { Parser { state: input } } /// Run a parser, and restore the pre-parse state if it fails. fn read_atomically<T, F>(&mut self, inner: F) -> Option<T> where F: FnOnce(&mut Parser<'_>) -> Option<T>, { let state = self.state; let result = inner(self); if result.is_none() { self.state = state; } result } /// Run a parser, but fail if the entire input wasn't consumed. /// Doesn't run atomically. fn parse_with<T, F>(&mut self, inner: F, kind: AddrKind) -> Result<T, AddrParseError> where F: FnOnce(&mut Parser<'_>) -> Option<T>, { let result = inner(self); if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(kind)) } /// Peek the next character from the input fn peek_char(&self) -> Option<char> { self.state.first().map(|&b| char::from(b)) } /// Read the next character from the input fn read_char(&mut self) -> Option<char> { self.state.split_first().map(|(&b, tail)| { self.state = tail; char::from(b) }) } #[must_use] /// Read the next character from the input if it matches the target. fn read_given_char(&mut self, target: char) -> Option<()> { self.read_atomically(|p| { p.read_char().and_then(|c| if c == target { Some(()) } else { None }) }) } /// Helper for reading separators in an indexed loop. Reads the separator /// character iff index > 0, then runs the parser. When used in a loop, /// the separator character will only be read on index > 0 (see /// read_ipv4_addr for an example) fn read_separator<T, F>(&mut self, sep: char, index: usize, inner: F) -> Option<T> where F: FnOnce(&mut Parser<'_>) -> Option<T>, { self.read_atomically(move |p| { if index > 0 { p.read_given_char(sep)?; } inner(p) }) } // Read a number off the front of the input in the given radix, stopping // at the first non-digit character or eof. Fails if the number has more // digits than max_digits or if there is no number. #[allow(clippy::if_same_then_else)] fn read_number<T: ReadNumberHelper>( &mut self, radix: u32, max_digits: Option<usize>, allow_zero_prefix: bool, ) -> Option<T> { self.read_atomically(move |p| { let mut result = T::ZERO; let mut digit_count = 0; let has_leading_zero = p.peek_char() == Some('0'); while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) { result = result.checked_mul(radix)?; result = result.checked_add(digit)?; digit_count += 1; if let Some(max_digits) = max_digits { if digit_count > max_digits { return None; } } } if digit_count == 0 { None } else if !allow_zero_prefix && has_leading_zero && digit_count > 1 { None } else { Some(result) } }) } /// Read an IPv4 address. fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> { self.read_atomically(|p| { let mut groups = [0; 4]; for (i, slot) in groups.iter_mut().enumerate() { *slot = p.read_separator('.', i, |p| { // Disallow octal number in IP string. // https://tools.ietf.org/html/rfc6943#section-3.1.1 p.read_number(10, Some(3), false) })?; } Some(groups.into()) }) } /// Read an IPv6 Address. fn read_ipv6_addr(&mut self) -> Option<Ipv6Addr> { /// Read a chunk of an IPv6 address into `groups`. Returns the number /// of groups read, along with a bool indicating if an embedded /// trailing IPv4 address was read. Specifically, read a series of /// colon-separated IPv6 groups (0x0000 - 0xFFFF), with an optional /// trailing embedded IPv4 address. fn read_groups(p: &mut Parser<'_>, groups: &mut [u16]) -> (usize, bool) { let limit = groups.len(); for (i, slot) in groups.iter_mut().enumerate() { // Try to read a trailing embedded IPv4 address. There must be // at least two groups left. if i < limit - 1 { let ipv4 = p.read_separator(':', i, |p| p.read_ipv4_addr()); if let Some(v4_addr) = ipv4 { let [one, two, three, four] = v4_addr.octets(); groups[i] = u16::from_be_bytes([one, two]); groups[i + 1] = u16::from_be_bytes([three, four]); return (i + 2, true); } } let group = p.read_separator(':', i, |p| p.read_number(16, Some(4), true)); match group { Some(g) => *slot = g, None => return (i, false), } } (groups.len(), false) } self.read_atomically(|p| { // Read the front part of the address; either the whole thing, or up // to the first :: let mut head = [0; 8]; let (head_size, head_ipv4) = read_groups(p, &mut head); if head_size == 8 { return Some(head.into()); } // IPv4 part is not allowed before `::` if head_ipv4 { return None; } // Read `::` if previous code parsed less than 8 groups. // `::` indicates one or more groups of 16 bits of zeros. p.read_given_char(':')?; p.read_given_char(':')?; // Read the back part of the address. The :: must contain at least one // set of zeroes, so our max length is 7. let mut tail = [0; 7]; let limit = 8 - (head_size + 1); let (tail_size, _) = read_groups(p, &mut tail[..limit]); // Concat the head and tail of the IP address head[(8 - tail_size)..8].copy_from_slice(&tail[..tail_size]); Some(head.into()) }) } /// Read an IP Address, either IPv4 or IPv6. fn read_ip_addr(&mut self) -> Option<IpAddr> { self.read_ipv4_addr().map(IpAddr::V4).or_else(move || self.read_ipv6_addr().map(IpAddr::V6)) } /// Read a `:` followed by a port in base 10. fn read_port(&mut self) -> Option<u16> { self.read_atomically(|p| { p.read_given_char(':')?; p.read_number(10, None, true) }) } /// Read a `%` followed by a scope ID in base 10. fn read_scope_id(&mut self) -> Option<u32> { self.read_atomically(|p| { p.read_given_char('%')?; p.read_number(10, None, true) }) } /// Read an IPv4 address with a port. fn read_socket_addr_v4(&mut self) -> Option<SocketAddrV4> { self.read_atomically(|p| { let ip = p.read_ipv4_addr()?; let port = p.read_port()?; Some(SocketAddrV4::new(ip, port)) }) } /// Read an IPv6 address with a port. fn read_socket_addr_v6(&mut self) -> Option<SocketAddrV6> { self.read_atomically(|p| { p.read_given_char('[')?; let ip = p.read_ipv6_addr()?; let scope_id = p.read_scope_id().unwrap_or(0); p.read_given_char(']')?; let port = p.read_port()?; Some(SocketAddrV6::new(ip, port, 0, scope_id)) }) } /// Read an IP address with a port fn read_socket_addr(&mut self) -> Option<SocketAddr> { self.read_socket_addr_v4() .map(SocketAddr::V4) .or_else(|| self.read_socket_addr_v6().map(SocketAddr::V6)) } } impl IpAddr { /// Parse an IP address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); /// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); /// /// assert_eq!(IpAddr::parse_ascii(b"127.0.0.1"), Ok(localhost_v4)); /// assert_eq!(IpAddr::parse_ascii(b"::1"), Ok(localhost_v6)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_ip_addr(), AddrKind::Ip) } } impl FromStr for IpAddr { type Err = AddrParseError; fn from_str(s: &str) -> Result<IpAddr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl Ipv4Addr { /// Parse an IPv4 address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::Ipv4Addr; /// /// let localhost = Ipv4Addr::new(127, 0, 0, 1); /// /// assert_eq!(Ipv4Addr::parse_ascii(b"127.0.0.1"), Ok(localhost)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { // don't try to parse if too long if b.len() > 15 { Err(AddrParseError(AddrKind::Ipv4)) } else { Parser::new(b).parse_with(|p| p.read_ipv4_addr(), AddrKind::Ipv4) } } } impl FromStr for Ipv4Addr { type Err = AddrParseError; fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl Ipv6Addr { /// Parse an IPv6 address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::Ipv6Addr; /// /// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1); /// /// assert_eq!(Ipv6Addr::parse_ascii(b"::1"), Ok(localhost)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_ipv6_addr(), AddrKind::Ipv6) } } impl FromStr for Ipv6Addr { type Err = AddrParseError; fn
(s: &str) -> Result<Ipv6Addr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl SocketAddrV4 { /// Parse an IPv4 socket address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{Ipv4Addr, SocketAddrV4}; /// /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); /// /// assert_eq!(SocketAddrV4::parse_ascii(b"127.0.0.1:8080"), Ok(socket)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_socket_addr_v4(), AddrKind::SocketV4) } } impl FromStr for SocketAddrV4 { type Err = AddrParseError; fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl SocketAddrV6 { /// Parse an IPv6 socket address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{Ipv6Addr, SocketAddrV6}; /// /// let socket = SocketAddrV6::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1), 8080, 0, 0); /// /// assert_eq!(SocketAddrV6::parse_ascii(b"[2001:db8::1]:8080"), Ok(socket)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_socket_addr_v6(), AddrKind::SocketV6) } } impl FromStr for SocketAddrV6 { type Err = AddrParseError; fn from_str(s: &str) -> Result<SocketAddrV6, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl SocketAddr { /// Parse a socket address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; /// /// let socket_v4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); /// let socket_v6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 8080); /// /// assert_eq!(SocketAddr::parse_ascii(b"127.0.0.1:8080"), Ok(socket_v4)); /// assert_eq!(SocketAddr::parse_ascii(b"[::1]:8080"), Ok(socket_v6)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_socket_addr(), AddrKind::Socket) } } impl FromStr for SocketAddr { type Err = AddrParseError; fn from_str(s: &str) -> Result<SocketAddr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } #[derive(Debug, Clone, PartialEq, Eq)] enum AddrKind { Ip, Ipv4, Ipv6, Socket, SocketV4, SocketV6, } /// An error which can be returned when parsing an IP address or a socket address. /// /// This error is used as the error type for the [`FromStr`] implementation for /// [`IpAddr`], [`Ipv4Addr`], [`Ipv6Addr`], [`SocketAddr`], [`SocketAddrV4`], and /// [`SocketAddrV6`]. /// /// # Potential causes /// /// `AddrParseError` may be thrown because the provided string does not parse as the given type, /// often because it includes information only handled by a different address type. /// /// ```should_panic /// use std::net::IpAddr; /// let _foo: IpAddr = "127.0.0.1:8080".parse().expect("Cannot handle the socket port"); /// ``` /// /// [`IpAddr`] doesn't handle the port. Use [`SocketAddr`] instead. /// /// ``` /// use std::net::SocketAddr; /// /// // No problem, the `panic!` message has disappeared. /// let _foo: SocketAddr = "127.0.0.1:8080".parse().expect("unreachable panic"); /// ``` #[derive(Debug, Clone, PartialEq, Eq)] pub struct AddrParseError(AddrKind); impl fmt::Display for AddrParseError { #[allow(deprecated, deprecated_in_future)] fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.write_str(self.description()) } } impl Error for AddrParseError { #[allow(deprecated)] fn description(&self) -> &str { match self.0 { AddrKind::Ip => "invalid IP address syntax", AddrKind::Ipv4 => "invalid IPv4 address syntax", AddrKind::Ipv6 => "invalid IPv6 address syntax", AddrKind::Socket => "invalid socket address syntax", AddrKind::SocketV4 => "invalid IPv4 socket address syntax", AddrKind::SocketV6 => "invalid IPv6 socket address syntax", } } }
from_str
identifier_name
parser.rs
// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License.. //! A private parser implementation of IPv4, IPv6, and socket addresses. //! //! This module is "publicly exported" through the `FromStr` implementations //! below. use crate::error::Error; use crate::fmt; use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use crate::str::FromStr; trait ReadNumberHelper: crate::marker::Sized { const ZERO: Self; fn checked_mul(&self, other: u32) -> Option<Self>; fn checked_add(&self, other: u32) -> Option<Self>; } macro_rules! impl_helper { ($($t:ty)*) => ($(impl ReadNumberHelper for $t { const ZERO: Self = 0; #[inline] fn checked_mul(&self, other: u32) -> Option<Self> { Self::checked_mul(*self, other.try_into().ok()?) } #[inline] fn checked_add(&self, other: u32) -> Option<Self> { Self::checked_add(*self, other.try_into().ok()?) } })*) } impl_helper! { u8 u16 u32 } struct Parser<'a> { // Parsing as ASCII, so can use byte array. state: &'a [u8], } impl<'a> Parser<'a> { fn new(input: &'a [u8]) -> Parser<'a> { Parser { state: input } } /// Run a parser, and restore the pre-parse state if it fails. fn read_atomically<T, F>(&mut self, inner: F) -> Option<T> where F: FnOnce(&mut Parser<'_>) -> Option<T>, { let state = self.state; let result = inner(self); if result.is_none() { self.state = state; } result } /// Run a parser, but fail if the entire input wasn't consumed. /// Doesn't run atomically. fn parse_with<T, F>(&mut self, inner: F, kind: AddrKind) -> Result<T, AddrParseError> where F: FnOnce(&mut Parser<'_>) -> Option<T>, { let result = inner(self); if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(kind)) } /// Peek the next character from the input fn peek_char(&self) -> Option<char> { self.state.first().map(|&b| char::from(b)) } /// Read the next character from the input fn read_char(&mut self) -> Option<char> { self.state.split_first().map(|(&b, tail)| { self.state = tail; char::from(b) }) } #[must_use] /// Read the next character from the input if it matches the target. fn read_given_char(&mut self, target: char) -> Option<()> { self.read_atomically(|p| { p.read_char().and_then(|c| if c == target { Some(()) } else { None }) }) } /// Helper for reading separators in an indexed loop. Reads the separator /// character iff index > 0, then runs the parser. When used in a loop, /// the separator character will only be read on index > 0 (see /// read_ipv4_addr for an example) fn read_separator<T, F>(&mut self, sep: char, index: usize, inner: F) -> Option<T> where F: FnOnce(&mut Parser<'_>) -> Option<T>,
// Read a number off the front of the input in the given radix, stopping // at the first non-digit character or eof. Fails if the number has more // digits than max_digits or if there is no number. #[allow(clippy::if_same_then_else)] fn read_number<T: ReadNumberHelper>( &mut self, radix: u32, max_digits: Option<usize>, allow_zero_prefix: bool, ) -> Option<T> { self.read_atomically(move |p| { let mut result = T::ZERO; let mut digit_count = 0; let has_leading_zero = p.peek_char() == Some('0'); while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) { result = result.checked_mul(radix)?; result = result.checked_add(digit)?; digit_count += 1; if let Some(max_digits) = max_digits { if digit_count > max_digits { return None; } } } if digit_count == 0 { None } else if !allow_zero_prefix && has_leading_zero && digit_count > 1 { None } else { Some(result) } }) } /// Read an IPv4 address. fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> { self.read_atomically(|p| { let mut groups = [0; 4]; for (i, slot) in groups.iter_mut().enumerate() { *slot = p.read_separator('.', i, |p| { // Disallow octal number in IP string. // https://tools.ietf.org/html/rfc6943#section-3.1.1 p.read_number(10, Some(3), false) })?; } Some(groups.into()) }) } /// Read an IPv6 Address. fn read_ipv6_addr(&mut self) -> Option<Ipv6Addr> { /// Read a chunk of an IPv6 address into `groups`. Returns the number /// of groups read, along with a bool indicating if an embedded /// trailing IPv4 address was read. Specifically, read a series of /// colon-separated IPv6 groups (0x0000 - 0xFFFF), with an optional /// trailing embedded IPv4 address. fn read_groups(p: &mut Parser<'_>, groups: &mut [u16]) -> (usize, bool) { let limit = groups.len(); for (i, slot) in groups.iter_mut().enumerate() { // Try to read a trailing embedded IPv4 address. There must be // at least two groups left. if i < limit - 1 { let ipv4 = p.read_separator(':', i, |p| p.read_ipv4_addr()); if let Some(v4_addr) = ipv4 { let [one, two, three, four] = v4_addr.octets(); groups[i] = u16::from_be_bytes([one, two]); groups[i + 1] = u16::from_be_bytes([three, four]); return (i + 2, true); } } let group = p.read_separator(':', i, |p| p.read_number(16, Some(4), true)); match group { Some(g) => *slot = g, None => return (i, false), } } (groups.len(), false) } self.read_atomically(|p| { // Read the front part of the address; either the whole thing, or up // to the first :: let mut head = [0; 8]; let (head_size, head_ipv4) = read_groups(p, &mut head); if head_size == 8 { return Some(head.into()); } // IPv4 part is not allowed before `::` if head_ipv4 { return None; } // Read `::` if previous code parsed less than 8 groups. // `::` indicates one or more groups of 16 bits of zeros. p.read_given_char(':')?; p.read_given_char(':')?; // Read the back part of the address. The :: must contain at least one // set of zeroes, so our max length is 7. let mut tail = [0; 7]; let limit = 8 - (head_size + 1); let (tail_size, _) = read_groups(p, &mut tail[..limit]); // Concat the head and tail of the IP address head[(8 - tail_size)..8].copy_from_slice(&tail[..tail_size]); Some(head.into()) }) } /// Read an IP Address, either IPv4 or IPv6. fn read_ip_addr(&mut self) -> Option<IpAddr> { self.read_ipv4_addr().map(IpAddr::V4).or_else(move || self.read_ipv6_addr().map(IpAddr::V6)) } /// Read a `:` followed by a port in base 10. fn read_port(&mut self) -> Option<u16> { self.read_atomically(|p| { p.read_given_char(':')?; p.read_number(10, None, true) }) } /// Read a `%` followed by a scope ID in base 10. fn read_scope_id(&mut self) -> Option<u32> { self.read_atomically(|p| { p.read_given_char('%')?; p.read_number(10, None, true) }) } /// Read an IPv4 address with a port. fn read_socket_addr_v4(&mut self) -> Option<SocketAddrV4> { self.read_atomically(|p| { let ip = p.read_ipv4_addr()?; let port = p.read_port()?; Some(SocketAddrV4::new(ip, port)) }) } /// Read an IPv6 address with a port. fn read_socket_addr_v6(&mut self) -> Option<SocketAddrV6> { self.read_atomically(|p| { p.read_given_char('[')?; let ip = p.read_ipv6_addr()?; let scope_id = p.read_scope_id().unwrap_or(0); p.read_given_char(']')?; let port = p.read_port()?; Some(SocketAddrV6::new(ip, port, 0, scope_id)) }) } /// Read an IP address with a port fn read_socket_addr(&mut self) -> Option<SocketAddr> { self.read_socket_addr_v4() .map(SocketAddr::V4) .or_else(|| self.read_socket_addr_v6().map(SocketAddr::V6)) } } impl IpAddr { /// Parse an IP address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); /// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); /// /// assert_eq!(IpAddr::parse_ascii(b"127.0.0.1"), Ok(localhost_v4)); /// assert_eq!(IpAddr::parse_ascii(b"::1"), Ok(localhost_v6)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_ip_addr(), AddrKind::Ip) } } impl FromStr for IpAddr { type Err = AddrParseError; fn from_str(s: &str) -> Result<IpAddr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl Ipv4Addr { /// Parse an IPv4 address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::Ipv4Addr; /// /// let localhost = Ipv4Addr::new(127, 0, 0, 1); /// /// assert_eq!(Ipv4Addr::parse_ascii(b"127.0.0.1"), Ok(localhost)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { // don't try to parse if too long if b.len() > 15 { Err(AddrParseError(AddrKind::Ipv4)) } else { Parser::new(b).parse_with(|p| p.read_ipv4_addr(), AddrKind::Ipv4) } } } impl FromStr for Ipv4Addr { type Err = AddrParseError; fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl Ipv6Addr { /// Parse an IPv6 address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::Ipv6Addr; /// /// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1); /// /// assert_eq!(Ipv6Addr::parse_ascii(b"::1"), Ok(localhost)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_ipv6_addr(), AddrKind::Ipv6) } } impl FromStr for Ipv6Addr { type Err = AddrParseError; fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl SocketAddrV4 { /// Parse an IPv4 socket address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{Ipv4Addr, SocketAddrV4}; /// /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); /// /// assert_eq!(SocketAddrV4::parse_ascii(b"127.0.0.1:8080"), Ok(socket)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_socket_addr_v4(), AddrKind::SocketV4) } } impl FromStr for SocketAddrV4 { type Err = AddrParseError; fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl SocketAddrV6 { /// Parse an IPv6 socket address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{Ipv6Addr, SocketAddrV6}; /// /// let socket = SocketAddrV6::new(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1), 8080, 0, 0); /// /// assert_eq!(SocketAddrV6::parse_ascii(b"[2001:db8::1]:8080"), Ok(socket)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_socket_addr_v6(), AddrKind::SocketV6) } } impl FromStr for SocketAddrV6 { type Err = AddrParseError; fn from_str(s: &str) -> Result<SocketAddrV6, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } impl SocketAddr { /// Parse a socket address from a slice of bytes. /// /// ``` /// #![feature(addr_parse_ascii)] /// /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; /// /// let socket_v4 = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); /// let socket_v6 = SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 8080); /// /// assert_eq!(SocketAddr::parse_ascii(b"127.0.0.1:8080"), Ok(socket_v4)); /// assert_eq!(SocketAddr::parse_ascii(b"[::1]:8080"), Ok(socket_v6)); /// ``` pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> { Parser::new(b).parse_with(|p| p.read_socket_addr(), AddrKind::Socket) } } impl FromStr for SocketAddr { type Err = AddrParseError; fn from_str(s: &str) -> Result<SocketAddr, AddrParseError> { Self::parse_ascii(s.as_bytes()) } } #[derive(Debug, Clone, PartialEq, Eq)] enum AddrKind { Ip, Ipv4, Ipv6, Socket, SocketV4, SocketV6, } /// An error which can be returned when parsing an IP address or a socket address. /// /// This error is used as the error type for the [`FromStr`] implementation for /// [`IpAddr`], [`Ipv4Addr`], [`Ipv6Addr`], [`SocketAddr`], [`SocketAddrV4`], and /// [`SocketAddrV6`]. /// /// # Potential causes /// /// `AddrParseError` may be thrown because the provided string does not parse as the given type, /// often because it includes information only handled by a different address type. /// /// ```should_panic /// use std::net::IpAddr; /// let _foo: IpAddr = "127.0.0.1:8080".parse().expect("Cannot handle the socket port"); /// ``` /// /// [`IpAddr`] doesn't handle the port. Use [`SocketAddr`] instead. /// /// ``` /// use std::net::SocketAddr; /// /// // No problem, the `panic!` message has disappeared. /// let _foo: SocketAddr = "127.0.0.1:8080".parse().expect("unreachable panic"); /// ``` #[derive(Debug, Clone, PartialEq, Eq)] pub struct AddrParseError(AddrKind); impl fmt::Display for AddrParseError { #[allow(deprecated, deprecated_in_future)] fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.write_str(self.description()) } } impl Error for AddrParseError { #[allow(deprecated)] fn description(&self) -> &str { match self.0 { AddrKind::Ip => "invalid IP address syntax", AddrKind::Ipv4 => "invalid IPv4 address syntax", AddrKind::Ipv6 => "invalid IPv6 address syntax", AddrKind::Socket => "invalid socket address syntax", AddrKind::SocketV4 => "invalid IPv4 socket address syntax", AddrKind::SocketV6 => "invalid IPv6 socket address syntax", } } }
{ self.read_atomically(move |p| { if index > 0 { p.read_given_char(sep)?; } inner(p) }) }
identifier_body
cubic64.rs
// Copyright 2012 Google Inc. // Copyright 2020 Yevhenii Reizner // // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. use super::point64::{Point64, SearchAxis}; use super::quad64; use super::Scalar64; #[cfg(all(not(feature = "std"), feature = "no-std-float"))] use tiny_skia_path::NoStdFloat; pub const POINT_COUNT: usize = 4; const PI: f64 = 3.141592653589793; pub struct Cubic64Pair { pub points: [Point64; 7], } pub struct Cubic64 { pub points: [Point64; POINT_COUNT], } impl Cubic64 { pub fn new(points: [Point64; POINT_COUNT]) -> Self { Cubic64 { points } } pub fn as_f64_slice(&self) -> [f64; POINT_COUNT * 2] { [ self.points[0].x, self.points[0].y, self.points[1].x, self.points[1].y, self.points[2].x, self.points[2].y, self.points[3].x, self.points[3].y, ] } pub fn point_at_t(&self, t: f64) -> Point64 { if t == 0.0 { return self.points[0]; } if t == 1.0 { return self.points[3]; } let one_t = 1.0 - t; let one_t2 = one_t * one_t; let a = one_t2 * one_t; let b = 3.0 * one_t2 * t; let t2 = t * t; let c = 3.0 * one_t * t2; let d = t2 * t; Point64::from_xy( a * self.points[0].x + b * self.points[1].x + c * self.points[2].x + d * self.points[3].x, a * self.points[0].y + b * self.points[1].y + c * self.points[2].y + d * self.points[3].y, ) } pub fn search_roots( &self, mut extrema: usize, axis_intercept: f64, x_axis: SearchAxis, extreme_ts: &mut [f64; 6], valid_roots: &mut [f64], ) -> usize { extrema += self.find_inflections(&mut extreme_ts[extrema..]); extreme_ts[extrema] = 0.0; extrema += 1; extreme_ts[extrema] = 1.0; debug_assert!(extrema < 6); extreme_ts[0..extrema].sort_by(cmp_f64); let mut valid_count = 0; let mut index = 0; while index < extrema { let min = extreme_ts[index]; index += 1; let max = extreme_ts[index]; if min == max { continue; } let new_t = self.binary_search(min, max, axis_intercept, x_axis); if new_t >= 0.0 { if valid_count >= 3 { return 0; } valid_roots[valid_count] = new_t; valid_count += 1; } } valid_count } fn find_inflections(&self, t_values: &mut [f64]) -> usize { let ax = self.points[1].x - self.points[0].x; let ay = self.points[1].y - self.points[0].y; let bx = self.points[2].x - 2.0 * self.points[1].x + self.points[0].x; let by = self.points[2].y - 2.0 * self.points[1].y + self.points[0].y; let cx = self.points[3].x + 3.0 * (self.points[1].x - self.points[2].x) - self.points[0].x; let cy = self.points[3].y + 3.0 * (self.points[1].y - self.points[2].y) - self.points[0].y; quad64::roots_valid_t( bx * cy - by * cx, ax * cy - ay * cx, ax * by - ay * bx, t_values, ) } // give up when changing t no longer moves point // also, copy point rather than recompute it when it does change fn binary_search(&self, min: f64, max: f64, axis_intercept: f64, x_axis: SearchAxis) -> f64 { let mut t = (min + max) / 2.0; let mut step = (t - min) / 2.0; let mut cubic_at_t = self.point_at_t(t); let mut calc_pos = cubic_at_t.axis_coord(x_axis); let mut calc_dist = calc_pos - axis_intercept; loop { let prior_t = min.max(t - step); let less_pt = self.point_at_t(prior_t); if less_pt.x.approximately_equal_half(cubic_at_t.x) && less_pt.y.approximately_equal_half(cubic_at_t.y) { return -1.0; // binary search found no point at this axis intercept } let less_dist = less_pt.axis_coord(x_axis) - axis_intercept; let last_step = step; step /= 2.0; let ok = if calc_dist > 0.0 { calc_dist > less_dist } else { calc_dist < less_dist }; if ok { t = prior_t; } else
let test_at_t = self.point_at_t(t); cubic_at_t = test_at_t; calc_pos = cubic_at_t.axis_coord(x_axis); calc_dist = calc_pos - axis_intercept; if calc_pos.approximately_equal(axis_intercept) { break; } } t } pub fn chop_at(&self, t: f64) -> Cubic64Pair { let mut dst = [Point64::zero(); 7]; if t == 0.5 { dst[0] = self.points[0]; dst[1].x = (self.points[0].x + self.points[1].x) / 2.0; dst[1].y = (self.points[0].y + self.points[1].y) / 2.0; dst[2].x = (self.points[0].x + 2.0 * self.points[1].x + self.points[2].x) / 4.0; dst[2].y = (self.points[0].y + 2.0 * self.points[1].y + self.points[2].y) / 4.0; dst[3].x = (self.points[0].x + 3.0 * (self.points[1].x + self.points[2].x) + self.points[3].x) / 8.0; dst[3].y = (self.points[0].y + 3.0 * (self.points[1].y + self.points[2].y) + self.points[3].y) / 8.0; dst[4].x = (self.points[1].x + 2.0 * self.points[2].x + self.points[3].x) / 4.0; dst[4].y = (self.points[1].y + 2.0 * self.points[2].y + self.points[3].y) / 4.0; dst[5].x = (self.points[2].x + self.points[3].x) / 2.0; dst[5].y = (self.points[2].y + self.points[3].y) / 2.0; dst[6] = self.points[3]; Cubic64Pair { points: dst } } else { interp_cubic_coords_x(&self.points, t, &mut dst); interp_cubic_coords_y(&self.points, t, &mut dst); Cubic64Pair { points: dst } } } } pub fn coefficients(src: &[f64]) -> (f64, f64, f64, f64) { let mut a = src[6]; // d let mut b = src[4] * 3.0; // 3*c let mut c = src[2] * 3.0; // 3*b let d = src[0]; // a a -= d - c + b; // A = -a + 3*b - 3*c + d b += 3.0 * d - 2.0 * c; // B = 3*a - 6*b + 3*c c -= 3.0 * d; // C = -3*a + 3*b (a, b, c, d) } // from SkGeometry.cpp (and Numeric Solutions, 5.6) pub fn roots_valid_t(a: f64, b: f64, c: f64, d: f64, t: &mut [f64; 3]) -> usize { let mut s = [0.0; 3]; let real_roots = roots_real(a, b, c, d, &mut s); let mut found_roots = quad64::push_valid_ts(&s, real_roots, t); 'outer: for index in 0..real_roots { let t_value = s[index]; if !t_value.approximately_one_or_less() && t_value.between(1.0, 1.00005) { for idx2 in 0..found_roots { if t[idx2].approximately_equal(1.0) { continue 'outer; } } debug_assert!(found_roots < 3); t[found_roots] = 1.0; found_roots += 1; } else if !t_value.approximately_zero_or_more() && t_value.between(-0.00005, 0.0) { for idx2 in 0..found_roots { if t[idx2].approximately_equal(0.0) { continue 'outer; } } debug_assert!(found_roots < 3); t[found_roots] = 0.0; found_roots += 1; } } found_roots } fn roots_real(a: f64, b: f64, c: f64, d: f64, s: &mut [f64; 3]) -> usize { if a.approximately_zero() && a.approximately_zero_when_compared_to(b) && a.approximately_zero_when_compared_to(c) && a.approximately_zero_when_compared_to(d) { // we're just a quadratic return quad64::roots_real(b, c, d, s); } if d.approximately_zero_when_compared_to(a) && d.approximately_zero_when_compared_to(b) && d.approximately_zero_when_compared_to(c) { // 0 is one root let mut num = quad64::roots_real(a, b, c, s); for i in 0..num { if s[i].approximately_zero() { return num; } } s[num] = 0.0; num += 1; return num; } if (a + b + c + d).approximately_zero() { // 1 is one root let mut num = quad64::roots_real(a, a + b, -d, s); for i in 0..num { if s[i].almost_dequal_ulps(1.0) { return num; } } s[num] = 1.0; num += 1; return num; } let (a, b, c) = { let inv_a = 1.0 / a; let a = b * inv_a; let b = c * inv_a; let c = d * inv_a; (a, b, c) }; let a2 = a * a; let q = (a2 - b * 3.0) / 9.0; let r = (2.0 * a2 * a - 9.0 * a * b + 27.0 * c) / 54.0; let r2 = r * r; let q3 = q * q * q; let r2_minus_q3 = r2 - q3; let adiv3 = a / 3.0; let mut offset = 0; if r2_minus_q3 < 0.0 { // we have 3 real roots // the divide/root can, due to finite precisions, be slightly outside of -1...1 let theta = (r / q3.sqrt()).bound(-1.0, 1.0).acos(); let neg2_root_q = -2.0 * q.sqrt(); let mut rr = neg2_root_q * (theta / 3.0).cos() - adiv3; s[offset] = rr; offset += 1; rr = neg2_root_q * ((theta + 2.0 * PI) / 3.0).cos() - adiv3; if !s[0].almost_dequal_ulps(rr) { s[offset] = rr; offset += 1; } rr = neg2_root_q * ((theta - 2.0 * PI) / 3.0).cos() - adiv3; if !s[0].almost_dequal_ulps(rr) && (offset == 1 || !s[1].almost_dequal_ulps(rr)) { s[offset] = rr; offset += 1; } } else { // we have 1 real root let sqrt_r2_minus_q3 = r2_minus_q3.sqrt(); let mut a = r.abs() + sqrt_r2_minus_q3; a = super::cube_root(a); if r > 0.0 { a = -a; } if a != 0.0 { a += q / a; } let mut r2 = a - adiv3; s[offset] = r2; offset += 1; if r2.almost_dequal_ulps(q3) { r2 = -a / 2.0 - adiv3; if !s[0].almost_dequal_ulps(r2) { s[offset] = r2; offset += 1; } } } offset } // Cubic64'(t) = At^2 + Bt + C, where // A = 3(-a + 3(b - c) + d) // B = 6(a - 2b + c) // C = 3(b - a) // Solve for t, keeping only those that fit between 0 < t < 1 pub fn find_extrema(src: &[f64], t_values: &mut [f64]) -> usize { // we divide A,B,C by 3 to simplify let a = src[0]; let b = src[2]; let c = src[4]; let d = src[6]; let a2 = d - a + 3.0 * (b - c); let b2 = 2.0 * (a - b - b + c); let c2 = b - a; quad64::roots_valid_t(a2, b2, c2, t_values) } // Skia doesn't seems to care about NaN/inf during sorting, so we don't too. fn cmp_f64(a: &f64, b: &f64) -> core::cmp::Ordering { if a < b { core::cmp::Ordering::Less } else if a > b { core::cmp::Ordering::Greater } else { core::cmp::Ordering::Equal } } // classic one t subdivision fn interp_cubic_coords_x(src: &[Point64; 4], t: f64, dst: &mut [Point64; 7]) { use super::interp; let ab = interp(src[0].x, src[1].x, t); let bc = interp(src[1].x, src[2].x, t); let cd = interp(src[2].x, src[3].x, t); let abc = interp(ab, bc, t); let bcd = interp(bc, cd, t); let abcd = interp(abc, bcd, t); dst[0].x = src[0].x; dst[1].x = ab; dst[2].x = abc; dst[3].x = abcd; dst[4].x = bcd; dst[5].x = cd; dst[6].x = src[3].x; } fn interp_cubic_coords_y(src: &[Point64; 4], t: f64, dst: &mut [Point64; 7]) { use super::interp; let ab = interp(src[0].y, src[1].y, t); let bc = interp(src[1].y, src[2].y, t); let cd = interp(src[2].y, src[3].y, t); let abc = interp(ab, bc, t); let bcd = interp(bc, cd, t); let abcd = interp(abc, bcd, t); dst[0].y = src[0].y; dst[1].y = ab; dst[2].y = abc; dst[3].y = abcd; dst[4].y = bcd; dst[5].y = cd; dst[6].y = src[3].y; }
{ let next_t = t + last_step; if next_t > max { return -1.0; } let more_pt = self.point_at_t(next_t); if more_pt.x.approximately_equal_half(cubic_at_t.x) && more_pt.y.approximately_equal_half(cubic_at_t.y) { return -1.0; // binary search found no point at this axis intercept } let more_dist = more_pt.axis_coord(x_axis) - axis_intercept; let ok = if calc_dist > 0.0 { calc_dist <= more_dist } else { calc_dist >= more_dist }; if ok { continue; } t = next_t; }
conditional_block
cubic64.rs
// Copyright 2012 Google Inc. // Copyright 2020 Yevhenii Reizner // // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. use super::point64::{Point64, SearchAxis}; use super::quad64; use super::Scalar64; #[cfg(all(not(feature = "std"), feature = "no-std-float"))] use tiny_skia_path::NoStdFloat; pub const POINT_COUNT: usize = 4; const PI: f64 = 3.141592653589793; pub struct Cubic64Pair { pub points: [Point64; 7], } pub struct Cubic64 { pub points: [Point64; POINT_COUNT], } impl Cubic64 { pub fn new(points: [Point64; POINT_COUNT]) -> Self { Cubic64 { points } } pub fn as_f64_slice(&self) -> [f64; POINT_COUNT * 2] { [ self.points[0].x, self.points[0].y, self.points[1].x, self.points[1].y, self.points[2].x, self.points[2].y, self.points[3].x, self.points[3].y, ] } pub fn point_at_t(&self, t: f64) -> Point64 { if t == 0.0 { return self.points[0]; } if t == 1.0 { return self.points[3]; } let one_t = 1.0 - t; let one_t2 = one_t * one_t; let a = one_t2 * one_t; let b = 3.0 * one_t2 * t; let t2 = t * t; let c = 3.0 * one_t * t2; let d = t2 * t; Point64::from_xy( a * self.points[0].x + b * self.points[1].x + c * self.points[2].x + d * self.points[3].x, a * self.points[0].y + b * self.points[1].y + c * self.points[2].y + d * self.points[3].y, ) } pub fn search_roots( &self, mut extrema: usize, axis_intercept: f64, x_axis: SearchAxis, extreme_ts: &mut [f64; 6], valid_roots: &mut [f64], ) -> usize { extrema += self.find_inflections(&mut extreme_ts[extrema..]); extreme_ts[extrema] = 0.0; extrema += 1; extreme_ts[extrema] = 1.0; debug_assert!(extrema < 6); extreme_ts[0..extrema].sort_by(cmp_f64); let mut valid_count = 0; let mut index = 0; while index < extrema { let min = extreme_ts[index]; index += 1; let max = extreme_ts[index]; if min == max { continue; } let new_t = self.binary_search(min, max, axis_intercept, x_axis); if new_t >= 0.0 { if valid_count >= 3 { return 0; } valid_roots[valid_count] = new_t; valid_count += 1; } } valid_count } fn find_inflections(&self, t_values: &mut [f64]) -> usize { let ax = self.points[1].x - self.points[0].x; let ay = self.points[1].y - self.points[0].y; let bx = self.points[2].x - 2.0 * self.points[1].x + self.points[0].x; let by = self.points[2].y - 2.0 * self.points[1].y + self.points[0].y; let cx = self.points[3].x + 3.0 * (self.points[1].x - self.points[2].x) - self.points[0].x; let cy = self.points[3].y + 3.0 * (self.points[1].y - self.points[2].y) - self.points[0].y; quad64::roots_valid_t( bx * cy - by * cx, ax * cy - ay * cx, ax * by - ay * bx, t_values, ) } // give up when changing t no longer moves point // also, copy point rather than recompute it when it does change fn binary_search(&self, min: f64, max: f64, axis_intercept: f64, x_axis: SearchAxis) -> f64 { let mut t = (min + max) / 2.0; let mut step = (t - min) / 2.0; let mut cubic_at_t = self.point_at_t(t); let mut calc_pos = cubic_at_t.axis_coord(x_axis); let mut calc_dist = calc_pos - axis_intercept; loop { let prior_t = min.max(t - step); let less_pt = self.point_at_t(prior_t); if less_pt.x.approximately_equal_half(cubic_at_t.x) && less_pt.y.approximately_equal_half(cubic_at_t.y) { return -1.0; // binary search found no point at this axis intercept } let less_dist = less_pt.axis_coord(x_axis) - axis_intercept; let last_step = step; step /= 2.0; let ok = if calc_dist > 0.0 { calc_dist > less_dist } else { calc_dist < less_dist }; if ok { t = prior_t; } else { let next_t = t + last_step; if next_t > max { return -1.0; } let more_pt = self.point_at_t(next_t); if more_pt.x.approximately_equal_half(cubic_at_t.x) && more_pt.y.approximately_equal_half(cubic_at_t.y) { return -1.0; // binary search found no point at this axis intercept } let more_dist = more_pt.axis_coord(x_axis) - axis_intercept; let ok = if calc_dist > 0.0 { calc_dist <= more_dist } else { calc_dist >= more_dist }; if ok { continue; } t = next_t; } let test_at_t = self.point_at_t(t); cubic_at_t = test_at_t; calc_pos = cubic_at_t.axis_coord(x_axis); calc_dist = calc_pos - axis_intercept; if calc_pos.approximately_equal(axis_intercept) { break; } } t } pub fn chop_at(&self, t: f64) -> Cubic64Pair { let mut dst = [Point64::zero(); 7]; if t == 0.5 { dst[0] = self.points[0]; dst[1].x = (self.points[0].x + self.points[1].x) / 2.0; dst[1].y = (self.points[0].y + self.points[1].y) / 2.0; dst[2].x = (self.points[0].x + 2.0 * self.points[1].x + self.points[2].x) / 4.0; dst[2].y = (self.points[0].y + 2.0 * self.points[1].y + self.points[2].y) / 4.0; dst[3].x = (self.points[0].x + 3.0 * (self.points[1].x + self.points[2].x) + self.points[3].x) / 8.0; dst[3].y = (self.points[0].y + 3.0 * (self.points[1].y + self.points[2].y) + self.points[3].y) / 8.0; dst[4].x = (self.points[1].x + 2.0 * self.points[2].x + self.points[3].x) / 4.0; dst[4].y = (self.points[1].y + 2.0 * self.points[2].y + self.points[3].y) / 4.0; dst[5].x = (self.points[2].x + self.points[3].x) / 2.0; dst[5].y = (self.points[2].y + self.points[3].y) / 2.0; dst[6] = self.points[3]; Cubic64Pair { points: dst } } else { interp_cubic_coords_x(&self.points, t, &mut dst); interp_cubic_coords_y(&self.points, t, &mut dst); Cubic64Pair { points: dst } } } } pub fn coefficients(src: &[f64]) -> (f64, f64, f64, f64) { let mut a = src[6]; // d let mut b = src[4] * 3.0; // 3*c let mut c = src[2] * 3.0; // 3*b let d = src[0]; // a a -= d - c + b; // A = -a + 3*b - 3*c + d b += 3.0 * d - 2.0 * c; // B = 3*a - 6*b + 3*c c -= 3.0 * d; // C = -3*a + 3*b (a, b, c, d) } // from SkGeometry.cpp (and Numeric Solutions, 5.6) pub fn roots_valid_t(a: f64, b: f64, c: f64, d: f64, t: &mut [f64; 3]) -> usize { let mut s = [0.0; 3]; let real_roots = roots_real(a, b, c, d, &mut s); let mut found_roots = quad64::push_valid_ts(&s, real_roots, t); 'outer: for index in 0..real_roots { let t_value = s[index]; if !t_value.approximately_one_or_less() && t_value.between(1.0, 1.00005) { for idx2 in 0..found_roots { if t[idx2].approximately_equal(1.0) { continue 'outer; } } debug_assert!(found_roots < 3); t[found_roots] = 1.0; found_roots += 1; } else if !t_value.approximately_zero_or_more() && t_value.between(-0.00005, 0.0) { for idx2 in 0..found_roots { if t[idx2].approximately_equal(0.0) { continue 'outer; } } debug_assert!(found_roots < 3); t[found_roots] = 0.0; found_roots += 1; } } found_roots } fn roots_real(a: f64, b: f64, c: f64, d: f64, s: &mut [f64; 3]) -> usize { if a.approximately_zero() && a.approximately_zero_when_compared_to(b) && a.approximately_zero_when_compared_to(c) && a.approximately_zero_when_compared_to(d) { // we're just a quadratic return quad64::roots_real(b, c, d, s); } if d.approximately_zero_when_compared_to(a) && d.approximately_zero_when_compared_to(b) && d.approximately_zero_when_compared_to(c) { // 0 is one root let mut num = quad64::roots_real(a, b, c, s); for i in 0..num { if s[i].approximately_zero() { return num; } } s[num] = 0.0; num += 1;
} if (a + b + c + d).approximately_zero() { // 1 is one root let mut num = quad64::roots_real(a, a + b, -d, s); for i in 0..num { if s[i].almost_dequal_ulps(1.0) { return num; } } s[num] = 1.0; num += 1; return num; } let (a, b, c) = { let inv_a = 1.0 / a; let a = b * inv_a; let b = c * inv_a; let c = d * inv_a; (a, b, c) }; let a2 = a * a; let q = (a2 - b * 3.0) / 9.0; let r = (2.0 * a2 * a - 9.0 * a * b + 27.0 * c) / 54.0; let r2 = r * r; let q3 = q * q * q; let r2_minus_q3 = r2 - q3; let adiv3 = a / 3.0; let mut offset = 0; if r2_minus_q3 < 0.0 { // we have 3 real roots // the divide/root can, due to finite precisions, be slightly outside of -1...1 let theta = (r / q3.sqrt()).bound(-1.0, 1.0).acos(); let neg2_root_q = -2.0 * q.sqrt(); let mut rr = neg2_root_q * (theta / 3.0).cos() - adiv3; s[offset] = rr; offset += 1; rr = neg2_root_q * ((theta + 2.0 * PI) / 3.0).cos() - adiv3; if !s[0].almost_dequal_ulps(rr) { s[offset] = rr; offset += 1; } rr = neg2_root_q * ((theta - 2.0 * PI) / 3.0).cos() - adiv3; if !s[0].almost_dequal_ulps(rr) && (offset == 1 || !s[1].almost_dequal_ulps(rr)) { s[offset] = rr; offset += 1; } } else { // we have 1 real root let sqrt_r2_minus_q3 = r2_minus_q3.sqrt(); let mut a = r.abs() + sqrt_r2_minus_q3; a = super::cube_root(a); if r > 0.0 { a = -a; } if a != 0.0 { a += q / a; } let mut r2 = a - adiv3; s[offset] = r2; offset += 1; if r2.almost_dequal_ulps(q3) { r2 = -a / 2.0 - adiv3; if !s[0].almost_dequal_ulps(r2) { s[offset] = r2; offset += 1; } } } offset } // Cubic64'(t) = At^2 + Bt + C, where // A = 3(-a + 3(b - c) + d) // B = 6(a - 2b + c) // C = 3(b - a) // Solve for t, keeping only those that fit between 0 < t < 1 pub fn find_extrema(src: &[f64], t_values: &mut [f64]) -> usize { // we divide A,B,C by 3 to simplify let a = src[0]; let b = src[2]; let c = src[4]; let d = src[6]; let a2 = d - a + 3.0 * (b - c); let b2 = 2.0 * (a - b - b + c); let c2 = b - a; quad64::roots_valid_t(a2, b2, c2, t_values) } // Skia doesn't seems to care about NaN/inf during sorting, so we don't too. fn cmp_f64(a: &f64, b: &f64) -> core::cmp::Ordering { if a < b { core::cmp::Ordering::Less } else if a > b { core::cmp::Ordering::Greater } else { core::cmp::Ordering::Equal } } // classic one t subdivision fn interp_cubic_coords_x(src: &[Point64; 4], t: f64, dst: &mut [Point64; 7]) { use super::interp; let ab = interp(src[0].x, src[1].x, t); let bc = interp(src[1].x, src[2].x, t); let cd = interp(src[2].x, src[3].x, t); let abc = interp(ab, bc, t); let bcd = interp(bc, cd, t); let abcd = interp(abc, bcd, t); dst[0].x = src[0].x; dst[1].x = ab; dst[2].x = abc; dst[3].x = abcd; dst[4].x = bcd; dst[5].x = cd; dst[6].x = src[3].x; } fn interp_cubic_coords_y(src: &[Point64; 4], t: f64, dst: &mut [Point64; 7]) { use super::interp; let ab = interp(src[0].y, src[1].y, t); let bc = interp(src[1].y, src[2].y, t); let cd = interp(src[2].y, src[3].y, t); let abc = interp(ab, bc, t); let bcd = interp(bc, cd, t); let abcd = interp(abc, bcd, t); dst[0].y = src[0].y; dst[1].y = ab; dst[2].y = abc; dst[3].y = abcd; dst[4].y = bcd; dst[5].y = cd; dst[6].y = src[3].y; }
return num;
random_line_split
cubic64.rs
// Copyright 2012 Google Inc. // Copyright 2020 Yevhenii Reizner // // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. use super::point64::{Point64, SearchAxis}; use super::quad64; use super::Scalar64; #[cfg(all(not(feature = "std"), feature = "no-std-float"))] use tiny_skia_path::NoStdFloat; pub const POINT_COUNT: usize = 4; const PI: f64 = 3.141592653589793; pub struct Cubic64Pair { pub points: [Point64; 7], } pub struct Cubic64 { pub points: [Point64; POINT_COUNT], } impl Cubic64 { pub fn new(points: [Point64; POINT_COUNT]) -> Self { Cubic64 { points } } pub fn as_f64_slice(&self) -> [f64; POINT_COUNT * 2] { [ self.points[0].x, self.points[0].y, self.points[1].x, self.points[1].y, self.points[2].x, self.points[2].y, self.points[3].x, self.points[3].y, ] } pub fn point_at_t(&self, t: f64) -> Point64 { if t == 0.0 { return self.points[0]; } if t == 1.0 { return self.points[3]; } let one_t = 1.0 - t; let one_t2 = one_t * one_t; let a = one_t2 * one_t; let b = 3.0 * one_t2 * t; let t2 = t * t; let c = 3.0 * one_t * t2; let d = t2 * t; Point64::from_xy( a * self.points[0].x + b * self.points[1].x + c * self.points[2].x + d * self.points[3].x, a * self.points[0].y + b * self.points[1].y + c * self.points[2].y + d * self.points[3].y, ) } pub fn search_roots( &self, mut extrema: usize, axis_intercept: f64, x_axis: SearchAxis, extreme_ts: &mut [f64; 6], valid_roots: &mut [f64], ) -> usize { extrema += self.find_inflections(&mut extreme_ts[extrema..]); extreme_ts[extrema] = 0.0; extrema += 1; extreme_ts[extrema] = 1.0; debug_assert!(extrema < 6); extreme_ts[0..extrema].sort_by(cmp_f64); let mut valid_count = 0; let mut index = 0; while index < extrema { let min = extreme_ts[index]; index += 1; let max = extreme_ts[index]; if min == max { continue; } let new_t = self.binary_search(min, max, axis_intercept, x_axis); if new_t >= 0.0 { if valid_count >= 3 { return 0; } valid_roots[valid_count] = new_t; valid_count += 1; } } valid_count } fn find_inflections(&self, t_values: &mut [f64]) -> usize { let ax = self.points[1].x - self.points[0].x; let ay = self.points[1].y - self.points[0].y; let bx = self.points[2].x - 2.0 * self.points[1].x + self.points[0].x; let by = self.points[2].y - 2.0 * self.points[1].y + self.points[0].y; let cx = self.points[3].x + 3.0 * (self.points[1].x - self.points[2].x) - self.points[0].x; let cy = self.points[3].y + 3.0 * (self.points[1].y - self.points[2].y) - self.points[0].y; quad64::roots_valid_t( bx * cy - by * cx, ax * cy - ay * cx, ax * by - ay * bx, t_values, ) } // give up when changing t no longer moves point // also, copy point rather than recompute it when it does change fn binary_search(&self, min: f64, max: f64, axis_intercept: f64, x_axis: SearchAxis) -> f64 { let mut t = (min + max) / 2.0; let mut step = (t - min) / 2.0; let mut cubic_at_t = self.point_at_t(t); let mut calc_pos = cubic_at_t.axis_coord(x_axis); let mut calc_dist = calc_pos - axis_intercept; loop { let prior_t = min.max(t - step); let less_pt = self.point_at_t(prior_t); if less_pt.x.approximately_equal_half(cubic_at_t.x) && less_pt.y.approximately_equal_half(cubic_at_t.y) { return -1.0; // binary search found no point at this axis intercept } let less_dist = less_pt.axis_coord(x_axis) - axis_intercept; let last_step = step; step /= 2.0; let ok = if calc_dist > 0.0 { calc_dist > less_dist } else { calc_dist < less_dist }; if ok { t = prior_t; } else { let next_t = t + last_step; if next_t > max { return -1.0; } let more_pt = self.point_at_t(next_t); if more_pt.x.approximately_equal_half(cubic_at_t.x) && more_pt.y.approximately_equal_half(cubic_at_t.y) { return -1.0; // binary search found no point at this axis intercept } let more_dist = more_pt.axis_coord(x_axis) - axis_intercept; let ok = if calc_dist > 0.0 { calc_dist <= more_dist } else { calc_dist >= more_dist }; if ok { continue; } t = next_t; } let test_at_t = self.point_at_t(t); cubic_at_t = test_at_t; calc_pos = cubic_at_t.axis_coord(x_axis); calc_dist = calc_pos - axis_intercept; if calc_pos.approximately_equal(axis_intercept) { break; } } t } pub fn chop_at(&self, t: f64) -> Cubic64Pair { let mut dst = [Point64::zero(); 7]; if t == 0.5 { dst[0] = self.points[0]; dst[1].x = (self.points[0].x + self.points[1].x) / 2.0; dst[1].y = (self.points[0].y + self.points[1].y) / 2.0; dst[2].x = (self.points[0].x + 2.0 * self.points[1].x + self.points[2].x) / 4.0; dst[2].y = (self.points[0].y + 2.0 * self.points[1].y + self.points[2].y) / 4.0; dst[3].x = (self.points[0].x + 3.0 * (self.points[1].x + self.points[2].x) + self.points[3].x) / 8.0; dst[3].y = (self.points[0].y + 3.0 * (self.points[1].y + self.points[2].y) + self.points[3].y) / 8.0; dst[4].x = (self.points[1].x + 2.0 * self.points[2].x + self.points[3].x) / 4.0; dst[4].y = (self.points[1].y + 2.0 * self.points[2].y + self.points[3].y) / 4.0; dst[5].x = (self.points[2].x + self.points[3].x) / 2.0; dst[5].y = (self.points[2].y + self.points[3].y) / 2.0; dst[6] = self.points[3]; Cubic64Pair { points: dst } } else { interp_cubic_coords_x(&self.points, t, &mut dst); interp_cubic_coords_y(&self.points, t, &mut dst); Cubic64Pair { points: dst } } } } pub fn
(src: &[f64]) -> (f64, f64, f64, f64) { let mut a = src[6]; // d let mut b = src[4] * 3.0; // 3*c let mut c = src[2] * 3.0; // 3*b let d = src[0]; // a a -= d - c + b; // A = -a + 3*b - 3*c + d b += 3.0 * d - 2.0 * c; // B = 3*a - 6*b + 3*c c -= 3.0 * d; // C = -3*a + 3*b (a, b, c, d) } // from SkGeometry.cpp (and Numeric Solutions, 5.6) pub fn roots_valid_t(a: f64, b: f64, c: f64, d: f64, t: &mut [f64; 3]) -> usize { let mut s = [0.0; 3]; let real_roots = roots_real(a, b, c, d, &mut s); let mut found_roots = quad64::push_valid_ts(&s, real_roots, t); 'outer: for index in 0..real_roots { let t_value = s[index]; if !t_value.approximately_one_or_less() && t_value.between(1.0, 1.00005) { for idx2 in 0..found_roots { if t[idx2].approximately_equal(1.0) { continue 'outer; } } debug_assert!(found_roots < 3); t[found_roots] = 1.0; found_roots += 1; } else if !t_value.approximately_zero_or_more() && t_value.between(-0.00005, 0.0) { for idx2 in 0..found_roots { if t[idx2].approximately_equal(0.0) { continue 'outer; } } debug_assert!(found_roots < 3); t[found_roots] = 0.0; found_roots += 1; } } found_roots } fn roots_real(a: f64, b: f64, c: f64, d: f64, s: &mut [f64; 3]) -> usize { if a.approximately_zero() && a.approximately_zero_when_compared_to(b) && a.approximately_zero_when_compared_to(c) && a.approximately_zero_when_compared_to(d) { // we're just a quadratic return quad64::roots_real(b, c, d, s); } if d.approximately_zero_when_compared_to(a) && d.approximately_zero_when_compared_to(b) && d.approximately_zero_when_compared_to(c) { // 0 is one root let mut num = quad64::roots_real(a, b, c, s); for i in 0..num { if s[i].approximately_zero() { return num; } } s[num] = 0.0; num += 1; return num; } if (a + b + c + d).approximately_zero() { // 1 is one root let mut num = quad64::roots_real(a, a + b, -d, s); for i in 0..num { if s[i].almost_dequal_ulps(1.0) { return num; } } s[num] = 1.0; num += 1; return num; } let (a, b, c) = { let inv_a = 1.0 / a; let a = b * inv_a; let b = c * inv_a; let c = d * inv_a; (a, b, c) }; let a2 = a * a; let q = (a2 - b * 3.0) / 9.0; let r = (2.0 * a2 * a - 9.0 * a * b + 27.0 * c) / 54.0; let r2 = r * r; let q3 = q * q * q; let r2_minus_q3 = r2 - q3; let adiv3 = a / 3.0; let mut offset = 0; if r2_minus_q3 < 0.0 { // we have 3 real roots // the divide/root can, due to finite precisions, be slightly outside of -1...1 let theta = (r / q3.sqrt()).bound(-1.0, 1.0).acos(); let neg2_root_q = -2.0 * q.sqrt(); let mut rr = neg2_root_q * (theta / 3.0).cos() - adiv3; s[offset] = rr; offset += 1; rr = neg2_root_q * ((theta + 2.0 * PI) / 3.0).cos() - adiv3; if !s[0].almost_dequal_ulps(rr) { s[offset] = rr; offset += 1; } rr = neg2_root_q * ((theta - 2.0 * PI) / 3.0).cos() - adiv3; if !s[0].almost_dequal_ulps(rr) && (offset == 1 || !s[1].almost_dequal_ulps(rr)) { s[offset] = rr; offset += 1; } } else { // we have 1 real root let sqrt_r2_minus_q3 = r2_minus_q3.sqrt(); let mut a = r.abs() + sqrt_r2_minus_q3; a = super::cube_root(a); if r > 0.0 { a = -a; } if a != 0.0 { a += q / a; } let mut r2 = a - adiv3; s[offset] = r2; offset += 1; if r2.almost_dequal_ulps(q3) { r2 = -a / 2.0 - adiv3; if !s[0].almost_dequal_ulps(r2) { s[offset] = r2; offset += 1; } } } offset } // Cubic64'(t) = At^2 + Bt + C, where // A = 3(-a + 3(b - c) + d) // B = 6(a - 2b + c) // C = 3(b - a) // Solve for t, keeping only those that fit between 0 < t < 1 pub fn find_extrema(src: &[f64], t_values: &mut [f64]) -> usize { // we divide A,B,C by 3 to simplify let a = src[0]; let b = src[2]; let c = src[4]; let d = src[6]; let a2 = d - a + 3.0 * (b - c); let b2 = 2.0 * (a - b - b + c); let c2 = b - a; quad64::roots_valid_t(a2, b2, c2, t_values) } // Skia doesn't seems to care about NaN/inf during sorting, so we don't too. fn cmp_f64(a: &f64, b: &f64) -> core::cmp::Ordering { if a < b { core::cmp::Ordering::Less } else if a > b { core::cmp::Ordering::Greater } else { core::cmp::Ordering::Equal } } // classic one t subdivision fn interp_cubic_coords_x(src: &[Point64; 4], t: f64, dst: &mut [Point64; 7]) { use super::interp; let ab = interp(src[0].x, src[1].x, t); let bc = interp(src[1].x, src[2].x, t); let cd = interp(src[2].x, src[3].x, t); let abc = interp(ab, bc, t); let bcd = interp(bc, cd, t); let abcd = interp(abc, bcd, t); dst[0].x = src[0].x; dst[1].x = ab; dst[2].x = abc; dst[3].x = abcd; dst[4].x = bcd; dst[5].x = cd; dst[6].x = src[3].x; } fn interp_cubic_coords_y(src: &[Point64; 4], t: f64, dst: &mut [Point64; 7]) { use super::interp; let ab = interp(src[0].y, src[1].y, t); let bc = interp(src[1].y, src[2].y, t); let cd = interp(src[2].y, src[3].y, t); let abc = interp(ab, bc, t); let bcd = interp(bc, cd, t); let abcd = interp(abc, bcd, t); dst[0].y = src[0].y; dst[1].y = ab; dst[2].y = abc; dst[3].y = abcd; dst[4].y = bcd; dst[5].y = cd; dst[6].y = src[3].y; }
coefficients
identifier_name
cubic64.rs
// Copyright 2012 Google Inc. // Copyright 2020 Yevhenii Reizner // // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. use super::point64::{Point64, SearchAxis}; use super::quad64; use super::Scalar64; #[cfg(all(not(feature = "std"), feature = "no-std-float"))] use tiny_skia_path::NoStdFloat; pub const POINT_COUNT: usize = 4; const PI: f64 = 3.141592653589793; pub struct Cubic64Pair { pub points: [Point64; 7], } pub struct Cubic64 { pub points: [Point64; POINT_COUNT], } impl Cubic64 { pub fn new(points: [Point64; POINT_COUNT]) -> Self { Cubic64 { points } } pub fn as_f64_slice(&self) -> [f64; POINT_COUNT * 2] { [ self.points[0].x, self.points[0].y, self.points[1].x, self.points[1].y, self.points[2].x, self.points[2].y, self.points[3].x, self.points[3].y, ] } pub fn point_at_t(&self, t: f64) -> Point64 { if t == 0.0 { return self.points[0]; } if t == 1.0 { return self.points[3]; } let one_t = 1.0 - t; let one_t2 = one_t * one_t; let a = one_t2 * one_t; let b = 3.0 * one_t2 * t; let t2 = t * t; let c = 3.0 * one_t * t2; let d = t2 * t; Point64::from_xy( a * self.points[0].x + b * self.points[1].x + c * self.points[2].x + d * self.points[3].x, a * self.points[0].y + b * self.points[1].y + c * self.points[2].y + d * self.points[3].y, ) } pub fn search_roots( &self, mut extrema: usize, axis_intercept: f64, x_axis: SearchAxis, extreme_ts: &mut [f64; 6], valid_roots: &mut [f64], ) -> usize { extrema += self.find_inflections(&mut extreme_ts[extrema..]); extreme_ts[extrema] = 0.0; extrema += 1; extreme_ts[extrema] = 1.0; debug_assert!(extrema < 6); extreme_ts[0..extrema].sort_by(cmp_f64); let mut valid_count = 0; let mut index = 0; while index < extrema { let min = extreme_ts[index]; index += 1; let max = extreme_ts[index]; if min == max { continue; } let new_t = self.binary_search(min, max, axis_intercept, x_axis); if new_t >= 0.0 { if valid_count >= 3 { return 0; } valid_roots[valid_count] = new_t; valid_count += 1; } } valid_count } fn find_inflections(&self, t_values: &mut [f64]) -> usize { let ax = self.points[1].x - self.points[0].x; let ay = self.points[1].y - self.points[0].y; let bx = self.points[2].x - 2.0 * self.points[1].x + self.points[0].x; let by = self.points[2].y - 2.0 * self.points[1].y + self.points[0].y; let cx = self.points[3].x + 3.0 * (self.points[1].x - self.points[2].x) - self.points[0].x; let cy = self.points[3].y + 3.0 * (self.points[1].y - self.points[2].y) - self.points[0].y; quad64::roots_valid_t( bx * cy - by * cx, ax * cy - ay * cx, ax * by - ay * bx, t_values, ) } // give up when changing t no longer moves point // also, copy point rather than recompute it when it does change fn binary_search(&self, min: f64, max: f64, axis_intercept: f64, x_axis: SearchAxis) -> f64 { let mut t = (min + max) / 2.0; let mut step = (t - min) / 2.0; let mut cubic_at_t = self.point_at_t(t); let mut calc_pos = cubic_at_t.axis_coord(x_axis); let mut calc_dist = calc_pos - axis_intercept; loop { let prior_t = min.max(t - step); let less_pt = self.point_at_t(prior_t); if less_pt.x.approximately_equal_half(cubic_at_t.x) && less_pt.y.approximately_equal_half(cubic_at_t.y) { return -1.0; // binary search found no point at this axis intercept } let less_dist = less_pt.axis_coord(x_axis) - axis_intercept; let last_step = step; step /= 2.0; let ok = if calc_dist > 0.0 { calc_dist > less_dist } else { calc_dist < less_dist }; if ok { t = prior_t; } else { let next_t = t + last_step; if next_t > max { return -1.0; } let more_pt = self.point_at_t(next_t); if more_pt.x.approximately_equal_half(cubic_at_t.x) && more_pt.y.approximately_equal_half(cubic_at_t.y) { return -1.0; // binary search found no point at this axis intercept } let more_dist = more_pt.axis_coord(x_axis) - axis_intercept; let ok = if calc_dist > 0.0 { calc_dist <= more_dist } else { calc_dist >= more_dist }; if ok { continue; } t = next_t; } let test_at_t = self.point_at_t(t); cubic_at_t = test_at_t; calc_pos = cubic_at_t.axis_coord(x_axis); calc_dist = calc_pos - axis_intercept; if calc_pos.approximately_equal(axis_intercept) { break; } } t } pub fn chop_at(&self, t: f64) -> Cubic64Pair { let mut dst = [Point64::zero(); 7]; if t == 0.5 { dst[0] = self.points[0]; dst[1].x = (self.points[0].x + self.points[1].x) / 2.0; dst[1].y = (self.points[0].y + self.points[1].y) / 2.0; dst[2].x = (self.points[0].x + 2.0 * self.points[1].x + self.points[2].x) / 4.0; dst[2].y = (self.points[0].y + 2.0 * self.points[1].y + self.points[2].y) / 4.0; dst[3].x = (self.points[0].x + 3.0 * (self.points[1].x + self.points[2].x) + self.points[3].x) / 8.0; dst[3].y = (self.points[0].y + 3.0 * (self.points[1].y + self.points[2].y) + self.points[3].y) / 8.0; dst[4].x = (self.points[1].x + 2.0 * self.points[2].x + self.points[3].x) / 4.0; dst[4].y = (self.points[1].y + 2.0 * self.points[2].y + self.points[3].y) / 4.0; dst[5].x = (self.points[2].x + self.points[3].x) / 2.0; dst[5].y = (self.points[2].y + self.points[3].y) / 2.0; dst[6] = self.points[3]; Cubic64Pair { points: dst } } else { interp_cubic_coords_x(&self.points, t, &mut dst); interp_cubic_coords_y(&self.points, t, &mut dst); Cubic64Pair { points: dst } } } } pub fn coefficients(src: &[f64]) -> (f64, f64, f64, f64) { let mut a = src[6]; // d let mut b = src[4] * 3.0; // 3*c let mut c = src[2] * 3.0; // 3*b let d = src[0]; // a a -= d - c + b; // A = -a + 3*b - 3*c + d b += 3.0 * d - 2.0 * c; // B = 3*a - 6*b + 3*c c -= 3.0 * d; // C = -3*a + 3*b (a, b, c, d) } // from SkGeometry.cpp (and Numeric Solutions, 5.6) pub fn roots_valid_t(a: f64, b: f64, c: f64, d: f64, t: &mut [f64; 3]) -> usize
fn roots_real(a: f64, b: f64, c: f64, d: f64, s: &mut [f64; 3]) -> usize { if a.approximately_zero() && a.approximately_zero_when_compared_to(b) && a.approximately_zero_when_compared_to(c) && a.approximately_zero_when_compared_to(d) { // we're just a quadratic return quad64::roots_real(b, c, d, s); } if d.approximately_zero_when_compared_to(a) && d.approximately_zero_when_compared_to(b) && d.approximately_zero_when_compared_to(c) { // 0 is one root let mut num = quad64::roots_real(a, b, c, s); for i in 0..num { if s[i].approximately_zero() { return num; } } s[num] = 0.0; num += 1; return num; } if (a + b + c + d).approximately_zero() { // 1 is one root let mut num = quad64::roots_real(a, a + b, -d, s); for i in 0..num { if s[i].almost_dequal_ulps(1.0) { return num; } } s[num] = 1.0; num += 1; return num; } let (a, b, c) = { let inv_a = 1.0 / a; let a = b * inv_a; let b = c * inv_a; let c = d * inv_a; (a, b, c) }; let a2 = a * a; let q = (a2 - b * 3.0) / 9.0; let r = (2.0 * a2 * a - 9.0 * a * b + 27.0 * c) / 54.0; let r2 = r * r; let q3 = q * q * q; let r2_minus_q3 = r2 - q3; let adiv3 = a / 3.0; let mut offset = 0; if r2_minus_q3 < 0.0 { // we have 3 real roots // the divide/root can, due to finite precisions, be slightly outside of -1...1 let theta = (r / q3.sqrt()).bound(-1.0, 1.0).acos(); let neg2_root_q = -2.0 * q.sqrt(); let mut rr = neg2_root_q * (theta / 3.0).cos() - adiv3; s[offset] = rr; offset += 1; rr = neg2_root_q * ((theta + 2.0 * PI) / 3.0).cos() - adiv3; if !s[0].almost_dequal_ulps(rr) { s[offset] = rr; offset += 1; } rr = neg2_root_q * ((theta - 2.0 * PI) / 3.0).cos() - adiv3; if !s[0].almost_dequal_ulps(rr) && (offset == 1 || !s[1].almost_dequal_ulps(rr)) { s[offset] = rr; offset += 1; } } else { // we have 1 real root let sqrt_r2_minus_q3 = r2_minus_q3.sqrt(); let mut a = r.abs() + sqrt_r2_minus_q3; a = super::cube_root(a); if r > 0.0 { a = -a; } if a != 0.0 { a += q / a; } let mut r2 = a - adiv3; s[offset] = r2; offset += 1; if r2.almost_dequal_ulps(q3) { r2 = -a / 2.0 - adiv3; if !s[0].almost_dequal_ulps(r2) { s[offset] = r2; offset += 1; } } } offset } // Cubic64'(t) = At^2 + Bt + C, where // A = 3(-a + 3(b - c) + d) // B = 6(a - 2b + c) // C = 3(b - a) // Solve for t, keeping only those that fit between 0 < t < 1 pub fn find_extrema(src: &[f64], t_values: &mut [f64]) -> usize { // we divide A,B,C by 3 to simplify let a = src[0]; let b = src[2]; let c = src[4]; let d = src[6]; let a2 = d - a + 3.0 * (b - c); let b2 = 2.0 * (a - b - b + c); let c2 = b - a; quad64::roots_valid_t(a2, b2, c2, t_values) } // Skia doesn't seems to care about NaN/inf during sorting, so we don't too. fn cmp_f64(a: &f64, b: &f64) -> core::cmp::Ordering { if a < b { core::cmp::Ordering::Less } else if a > b { core::cmp::Ordering::Greater } else { core::cmp::Ordering::Equal } } // classic one t subdivision fn interp_cubic_coords_x(src: &[Point64; 4], t: f64, dst: &mut [Point64; 7]) { use super::interp; let ab = interp(src[0].x, src[1].x, t); let bc = interp(src[1].x, src[2].x, t); let cd = interp(src[2].x, src[3].x, t); let abc = interp(ab, bc, t); let bcd = interp(bc, cd, t); let abcd = interp(abc, bcd, t); dst[0].x = src[0].x; dst[1].x = ab; dst[2].x = abc; dst[3].x = abcd; dst[4].x = bcd; dst[5].x = cd; dst[6].x = src[3].x; } fn interp_cubic_coords_y(src: &[Point64; 4], t: f64, dst: &mut [Point64; 7]) { use super::interp; let ab = interp(src[0].y, src[1].y, t); let bc = interp(src[1].y, src[2].y, t); let cd = interp(src[2].y, src[3].y, t); let abc = interp(ab, bc, t); let bcd = interp(bc, cd, t); let abcd = interp(abc, bcd, t); dst[0].y = src[0].y; dst[1].y = ab; dst[2].y = abc; dst[3].y = abcd; dst[4].y = bcd; dst[5].y = cd; dst[6].y = src[3].y; }
{ let mut s = [0.0; 3]; let real_roots = roots_real(a, b, c, d, &mut s); let mut found_roots = quad64::push_valid_ts(&s, real_roots, t); 'outer: for index in 0..real_roots { let t_value = s[index]; if !t_value.approximately_one_or_less() && t_value.between(1.0, 1.00005) { for idx2 in 0..found_roots { if t[idx2].approximately_equal(1.0) { continue 'outer; } } debug_assert!(found_roots < 3); t[found_roots] = 1.0; found_roots += 1; } else if !t_value.approximately_zero_or_more() && t_value.between(-0.00005, 0.0) { for idx2 in 0..found_roots { if t[idx2].approximately_equal(0.0) { continue 'outer; } } debug_assert!(found_roots < 3); t[found_roots] = 0.0; found_roots += 1; } } found_roots }
identifier_body
main.py
import tkinter as tk from tkinter import * import datetime from functools import partial import requests import pandas as pd import numpy as np import sys import os import tkinter.ttk from mlxtend.preprocessing import TransactionEncoder from mlxtend.frequent_patterns import apriori te=TransactionEncoder() dff=pd.read_csv("./database.csv") ind=110 det_ind=200 arrec=[] mycolor = '#%02x%02x%02x' % (50, 50, 50) added_count=0 newent_count=0 twilio_account_id="API Key" tkinter_umlauts=['odiaeresis', 'adiaeresis', 'udiaeresis', 'Odiaeresis', 'Adiaeresis', 'Udiaeresis', 'ssharp'] class AutocompleteEntry(tk.Entry): """ Subclass of tk.Entry that features autocompletion. To enable autocompletion use set_completion_list(list) to define a list of possible strings to hit. To cycle through hits use down and up arrow keys. """ def set_completion_list(self, completion_list): self._completion_list = sorted(completion_list, key=str.lower) # Work with a sorted list self._hits = [] self._hit_index = 0 self.position = 0 self.bind('<KeyRelease>', self.handle_keyrelease) def autocomplete(self, delta=0): """autocomplete the Entry, delta may be 0/1/-1 to cycle through possible hits""" if delta: # need to delete selection otherwise we would fix the current position self.delete(self.position, tk.END) else: # set position to end so selection starts where textentry ended self.position = len(self.get()) # collect hits _hits = [] for element in self._completion_list: if element.lower().startswith(self.get().lower()): # Match case-insensitively _hits.append(element) # if we have a new hit list, keep this in mind if _hits != self._hits: self._hit_index = 0 self._hits=_hits # only allow cycling if we are in a known hit list if _hits == self._hits and self._hits: self._hit_index = (self._hit_index + delta) % len(self._hits) # now finally perform the auto completion if self._hits: self.delete(0,tk.END) self.insert(0,self._hits[self._hit_index]) self.select_range(self.position,tk.END) entry1.delete(0,tk.END) entry1.insert(0,self.get()) def handle_keyrelease(self, event): """event handler for the keyrelease event on this widget""" if event.keysym == "BackSpace": self.delete(self.index(tk.INSERT), tk.END) self.position = self.index(tk.END) if event.keysym == "Left": if self.position < self.index(tk.END): # delete the selection self.delete(self.position, tk.END) else: self.position = self.position-1 # delete one character self.delete(self.position, tk.END) if event.keysym == "Right": self.position = self.index(tk.END) # go to end (no selection) if event.keysym == "Down": self.autocomplete(1) # cycle to next hit if event.keysym == "Up": self.autocomplete(-1) # cycle to previous hit if len(event.keysym) == 1 or event.keysym in tkinter_umlauts: self.autocomplete() overall_user=dff.iloc[:,0] overall_user=np.array(overall_user) overall_user=list(overall_user) overall_phone=dff.iloc[:,1] overall_phone=np.array(overall_phone) overall_phone=list(overall_phone) overall_date=dff.iloc[:,2] overall_date=np.array(overall_date) overall_date=list(overall_date) overall_time=dff.iloc[:,3] overall_time=np.array(overall_time) overall_time=list(overall_time) overall_name=dff.iloc[:,4] overall_name=np.array(overall_name) overall_name=list(overall_name) overall_price=dff.iloc[:,5] overall_price=np.array(overall_price) overall_price=list(overall_price) overall_quantity=dff.iloc[:,6] overall_quantity=np.array(overall_quantity) overall_quantity=list(overall_quantity) overall_amount=dff.iloc[:,7] overall_amount=np.array(overall_amount) overall_amount=list(overall_amount) overall_cno=dff.iloc[:,8] overall_cno=np.array(overall_cno) overall_cno=list(overall_cno) cno=dff["Customer No"][len(overall_cno)-1] + 1 curr_user=[] curr_phone=[] curr_date=[] curr_time=[] curr_name=[] curr_price=[] curr_quantity=[] curr_amount=[] curr_cno=[] def print_bill(): if os.path.isfile('print.txt'): os.remove('print.txt') with open('print.txt','a') as file: file.write('\t\tThank you for shopping\t\t\n') file.write('\t\t-----------------------\t\t\n') file.write(f'{curr_date[0]}\t\t\t{curr_time[0]}\n') file.write(f'Customer Name: {curr_user[0]}\n') file.write(f'Customer Phone: {curr_phone[0]}\n') file.write('Product\t\t\tQuantity\t\tPrice\t\t\tAmount\n') for i in range(len(curr_name)): with open('print.txt','a') as file: file.write(f'{curr_name[i]}\t\t\t{curr_quantity[i]}\t\t\t{curr_price[i]}\t\t\t{curr_amount[i]}\n') with open('print.txt','a') as file: file.write(f'Payable Amount:\tRs.{sum(curr_amount)}\n') os.startfile("print.txt", "print") #print bill using printer window1=tk.Tk() window1.configure(background="Light blue") window1.title("Supermarket Recommendation System") window1.geometry('600x600') now = datetime.datetime.now() date=now.strftime("%Y-%m-%d") time=now.strftime("%H:%M:%S") timee=tk.Label(window1,text=time, bg="Light blue", fg=mycolor) timee.place(x=200,y=15) datee=tk.Label(window1,text=date,bg="Light blue", fg=mycolor) datee.place(x=300,y=15) e11=tk.Label(window1,text="Name : ",bg="Light blue", fg=mycolor) e11.place(x=50,y=45) e22=tk.Label(window1,text="Phone Number : ",bg="Light blue", fg=mycolor) e22.place(x=270,y=45) e1=tk.Entry(window1) e1.place(x=100,y=45) e2=tk.Entry(window1) e2.place(x=380,y=45) l1=tk.Label(window1,text="Item name",bg="Light blue", fg=mycolor) l1.place(x=10, y=80) l2=tk.Label(window1,text="Price",bg="Light blue", fg=mycolor) l2.place(x=110, y=80) l3=tk.Label(window1,text="Quantity",bg="Light blue", fg=mycolor) l3.place(x=210, y=80) l3=tk.Label(window1,text="Amount",bg="Light blue", fg=mycolor) l3.place(x=310, y=80) def store() : global added_count added_count=added_count+1 global e1,e2 usern=e1.get() phno=e2.get() x=entry1.get() y=entry2.get() z=entry3.get() y=int(y) z=int(z) w=z*y l4=tk.Label(window1,text=(str(w)+"Rs."),bg="Light blue", fg=mycolor) l4.place(x=310,y=ind) l5=tk.Label(window1,text="Added.",bg="Light blue", fg=mycolor) l5.place(x=410,y=ind) curr_user.append(usern) curr_phone.append(phno) curr_date.append(date) curr_time.append(time) curr_name.append(x) curr_price.append(y) curr_quantity.append(z) curr_amount.append(w) curr_cno.append(cno) def newent() : global newent_count newent_count=newent_count+1 if(newent_count!=added_count+1 and newent_count!=0): store() global ind ind=ind+20 global entry1,entry2,entry3 entry1=tk.Entry(window1) entry1.place(x=10,y=ind) entry = AutocompleteEntry(entry1) test_list=list(set(pd.read_csv("./database.csv")['Name'])) if(np.nan in test_list): test_list.remove(np.nan) entry.set_completion_list(test_list) entry.pack() entry.focus_set() entry2=tk.Entry(window1) entry2.place(x=110,y=ind) entry3=tk.Entry(window1) entry3.place(x=210,y=ind) button1=tk.Button(window1,text="Add",command=store,fg="White", bg=mycolor) button1.place(x=400,y=430) button1=tk.Button(window1,text="New item",command=newent, fg="White", bg=mycolor) button1.place(x=400,y=400) '''Below function requires changes for different users''' def send_text() : text="Thank you for shopping with us! Here's your bill: " for i in range(len(curr_name)): text+=str(curr_name[i])+" - Rs."+str(curr_amount[i])+"\n" total_amount=0 for k in curr_amount : total_amount=total_amount+k text+="Total: "+str(total_amount) from twilio.rest import Client '''Create Twilio Account to get account_sid and auth_token''' account_sid = 'Account_sid' auth_token = 'Acc_Token' client = Client(account_sid, auth_token) '''from_ = 'whatsapp:+the number assigned by twilio',''' message = client.messages.create( from_='whatsapp:+000000000', body=text, to='whatsapp:+91'+curr_phone[0] ) print(message.sid) def subm() : global ind overall_user.extend(curr_user) overall_phone.extend(curr_phone) overall_date.extend(curr_date) overall_time.extend(curr_time) overall_name.extend(curr_name) overall_price.extend(curr_price) overall_quantity.extend(curr_quantity) overall_amount.extend(curr_amount) overall_cno.extend(curr_cno) df=pd.DataFrame({"UserName":overall_user,"Phone":overall_phone,"Date":overall_date,"Time":overall_time,"Name":overall_name,"Price":overall_price,"Quantity":overall_quantity,"Amount":overall_amount,"Customer No" : overall_cno }) df.to_csv("./database.csv",index=False) ans=0 for k in curr_amount : ans=ans+k op=tk.Label(window1,text="Submission successful. Thank you for shopping! Click below button to print bill",bg="Light blue", fg=mycolor) op.place(x=50,y=ind+50) op1=tk.Label(window1,text=("Total amount : "+ str(ans) + "Rs."),bg="Light blue", fg=mycolor) op1.place(x=50,y=ind+80) button1=tk.Button(window1,text="Print Bill",command=print_bill, fg="White", bg=mycolor) button1.place(x=0,y=400) send_text() button3=tk.Button(window1,text="Submit",command=subm, fg="White", bg=mycolor) button3.place(x=400,y=460) lg=[] def recm() : df_new=pd.read_csv("./database.csv") for i in range(cno+1) : lg=[] for z in df_new.index : if df_new.iloc[z][8]==i : lg.append(df_new.iloc[z][4]) arrec.append(lg) booldata=te.fit(arrec).transform(arrec) dff_new=pd.DataFrame(booldata,columns=te.columns_) freq_items=apriori(dff_new,min_support=0.05,use_colnames=True) freq_items['Length']=freq_items['itemsets'].apply(lambda x: len(x)) recc=freq_items[(freq_items['Length']>=2) & (freq_items['support']>=0.02)] op=(recc.iloc[:,1].to_string(index=False)).split('\n') window_rec=tk.Tk() window_rec.title("Recommendations") window_rec.configure(background=mycolor) window_rec.geometry('300x300') for zz in op : l1=tk.Label(window_rec,text=zz,fg="White", bg=mycolor) l1.pack() button4=tk.Button(window1,text="Recommend",command=recm,fg="White", bg=mycolor) button4.place(x=400,y=490) f=0 def det() : w11=tk.Tk() w11.title("Find Details") w11.configure(background=mycolor) w11.geometry('600x600') l12=tk.Label(w11,text="Username",fg="White", bg=mycolor) l12.place(x=100,y=50) e12=tk.Entry(w11) e12.place(x=160,y=50) l22=tk.Label(w11,text="Phone",fg="White", bg=mycolor) l22.place(x=100,y=80) e22=tk.Entry(w11) e22.place(x=160,y=80) def det2() : df_d=pd.read_csv("./database.csv") global det_ind zzz=e12.get() yyy=e22.get() laa1=tk.Label(w11,text="Date",fg="White", bg=mycolor) laa2=tk.Label(w11,text="Time",fg="White", bg=mycolor) laa3=tk.Label(w11,text="Product",fg="White", bg=mycolor) laa4=tk.Label(w11,text="Price",fg="White", bg=mycolor) laa5=tk.Label(w11,text="Quantity",fg="White", bg=mycolor) laa6=tk.Label(w11,text="Amount",fg="White", bg=mycolor) laa1.place(x=30,y=160) laa2.place(x=100,y=160) laa3.place(x=170,y=160) laa4.place(x=240,y=160) laa5.place(x=310,y=160) laa6.place(x=380,y=160) global f for j in df_d.index : if (df_d.iloc[j][0]==zzz) & (df_d.iloc[j][1]==int(yyy)) :
if f==0 : la7=tk.Label(w11,text="Not Found!",bg="White", fg=mycolor) la7.place(x=170,y=400) button6=tk.Button(w11,text="Submit",command=det2,fg="White", bg=mycolor) button6.place(x=170,y=115) button5=tk.Button(window1,text="Find Customer Details",command=det,fg="White", bg=mycolor) button5.place(x=400,y=520) window1.mainloop()
f=1 la1=tk.Label(w11,text=df_d.iloc[j][2],fg="White", bg=mycolor) la2=tk.Label(w11,text=df_d.iloc[j][3],fg="White", bg=mycolor) la3=tk.Label(w11,text=df_d.iloc[j][4],fg="White", bg=mycolor) la4=tk.Label(w11,text=df_d.iloc[j][5],fg="White", bg=mycolor) la5=tk.Label(w11,text=df_d.iloc[j][6],fg="White", bg=mycolor) la6=tk.Label(w11,text=df_d.iloc[j][7],fg="White", bg=mycolor) la1.place(x=30,y=det_ind) la2.place(x=100,y=det_ind) la3.place(x=170,y=det_ind) la4.place(x=240,y=det_ind) la5.place(x=310,y=det_ind) la6.place(x=380,y=det_ind) det_ind=det_ind+30
conditional_block
main.py
import tkinter as tk from tkinter import * import datetime from functools import partial import requests import pandas as pd import numpy as np import sys import os import tkinter.ttk from mlxtend.preprocessing import TransactionEncoder from mlxtend.frequent_patterns import apriori te=TransactionEncoder() dff=pd.read_csv("./database.csv") ind=110 det_ind=200 arrec=[] mycolor = '#%02x%02x%02x' % (50, 50, 50) added_count=0 newent_count=0 twilio_account_id="API Key" tkinter_umlauts=['odiaeresis', 'adiaeresis', 'udiaeresis', 'Odiaeresis', 'Adiaeresis', 'Udiaeresis', 'ssharp'] class AutocompleteEntry(tk.Entry): """ Subclass of tk.Entry that features autocompletion. To enable autocompletion use set_completion_list(list) to define a list of possible strings to hit. To cycle through hits use down and up arrow keys. """ def set_completion_list(self, completion_list): self._completion_list = sorted(completion_list, key=str.lower) # Work with a sorted list self._hits = [] self._hit_index = 0 self.position = 0 self.bind('<KeyRelease>', self.handle_keyrelease) def autocomplete(self, delta=0): """autocomplete the Entry, delta may be 0/1/-1 to cycle through possible hits""" if delta: # need to delete selection otherwise we would fix the current position self.delete(self.position, tk.END) else: # set position to end so selection starts where textentry ended self.position = len(self.get()) # collect hits _hits = [] for element in self._completion_list: if element.lower().startswith(self.get().lower()): # Match case-insensitively _hits.append(element) # if we have a new hit list, keep this in mind if _hits != self._hits: self._hit_index = 0 self._hits=_hits # only allow cycling if we are in a known hit list if _hits == self._hits and self._hits: self._hit_index = (self._hit_index + delta) % len(self._hits) # now finally perform the auto completion if self._hits: self.delete(0,tk.END) self.insert(0,self._hits[self._hit_index]) self.select_range(self.position,tk.END) entry1.delete(0,tk.END) entry1.insert(0,self.get()) def handle_keyrelease(self, event): """event handler for the keyrelease event on this widget""" if event.keysym == "BackSpace": self.delete(self.index(tk.INSERT), tk.END) self.position = self.index(tk.END) if event.keysym == "Left": if self.position < self.index(tk.END): # delete the selection self.delete(self.position, tk.END) else: self.position = self.position-1 # delete one character self.delete(self.position, tk.END) if event.keysym == "Right": self.position = self.index(tk.END) # go to end (no selection) if event.keysym == "Down": self.autocomplete(1) # cycle to next hit if event.keysym == "Up": self.autocomplete(-1) # cycle to previous hit if len(event.keysym) == 1 or event.keysym in tkinter_umlauts: self.autocomplete() overall_user=dff.iloc[:,0] overall_user=np.array(overall_user) overall_user=list(overall_user) overall_phone=dff.iloc[:,1] overall_phone=np.array(overall_phone) overall_phone=list(overall_phone) overall_date=dff.iloc[:,2] overall_date=np.array(overall_date) overall_date=list(overall_date) overall_time=dff.iloc[:,3] overall_time=np.array(overall_time) overall_time=list(overall_time) overall_name=dff.iloc[:,4] overall_name=np.array(overall_name) overall_name=list(overall_name) overall_price=dff.iloc[:,5] overall_price=np.array(overall_price) overall_price=list(overall_price) overall_quantity=dff.iloc[:,6] overall_quantity=np.array(overall_quantity) overall_quantity=list(overall_quantity) overall_amount=dff.iloc[:,7] overall_amount=np.array(overall_amount) overall_amount=list(overall_amount) overall_cno=dff.iloc[:,8] overall_cno=np.array(overall_cno) overall_cno=list(overall_cno) cno=dff["Customer No"][len(overall_cno)-1] + 1 curr_user=[] curr_phone=[] curr_date=[] curr_time=[] curr_name=[] curr_price=[] curr_quantity=[] curr_amount=[] curr_cno=[] def print_bill(): if os.path.isfile('print.txt'): os.remove('print.txt') with open('print.txt','a') as file: file.write('\t\tThank you for shopping\t\t\n') file.write('\t\t-----------------------\t\t\n') file.write(f'{curr_date[0]}\t\t\t{curr_time[0]}\n') file.write(f'Customer Name: {curr_user[0]}\n') file.write(f'Customer Phone: {curr_phone[0]}\n') file.write('Product\t\t\tQuantity\t\tPrice\t\t\tAmount\n') for i in range(len(curr_name)): with open('print.txt','a') as file: file.write(f'{curr_name[i]}\t\t\t{curr_quantity[i]}\t\t\t{curr_price[i]}\t\t\t{curr_amount[i]}\n') with open('print.txt','a') as file: file.write(f'Payable Amount:\tRs.{sum(curr_amount)}\n') os.startfile("print.txt", "print") #print bill using printer window1=tk.Tk() window1.configure(background="Light blue") window1.title("Supermarket Recommendation System") window1.geometry('600x600') now = datetime.datetime.now() date=now.strftime("%Y-%m-%d") time=now.strftime("%H:%M:%S") timee=tk.Label(window1,text=time, bg="Light blue", fg=mycolor) timee.place(x=200,y=15) datee=tk.Label(window1,text=date,bg="Light blue", fg=mycolor) datee.place(x=300,y=15) e11=tk.Label(window1,text="Name : ",bg="Light blue", fg=mycolor) e11.place(x=50,y=45) e22=tk.Label(window1,text="Phone Number : ",bg="Light blue", fg=mycolor) e22.place(x=270,y=45) e1=tk.Entry(window1) e1.place(x=100,y=45) e2=tk.Entry(window1) e2.place(x=380,y=45) l1=tk.Label(window1,text="Item name",bg="Light blue", fg=mycolor) l1.place(x=10, y=80) l2=tk.Label(window1,text="Price",bg="Light blue", fg=mycolor) l2.place(x=110, y=80) l3=tk.Label(window1,text="Quantity",bg="Light blue", fg=mycolor) l3.place(x=210, y=80) l3=tk.Label(window1,text="Amount",bg="Light blue", fg=mycolor) l3.place(x=310, y=80) def store() : global added_count added_count=added_count+1 global e1,e2 usern=e1.get() phno=e2.get() x=entry1.get() y=entry2.get() z=entry3.get() y=int(y) z=int(z) w=z*y l4=tk.Label(window1,text=(str(w)+"Rs."),bg="Light blue", fg=mycolor) l4.place(x=310,y=ind) l5=tk.Label(window1,text="Added.",bg="Light blue", fg=mycolor) l5.place(x=410,y=ind) curr_user.append(usern) curr_phone.append(phno) curr_date.append(date) curr_time.append(time) curr_name.append(x) curr_price.append(y) curr_quantity.append(z) curr_amount.append(w) curr_cno.append(cno) def newent() : global newent_count newent_count=newent_count+1 if(newent_count!=added_count+1 and newent_count!=0): store() global ind ind=ind+20 global entry1,entry2,entry3 entry1=tk.Entry(window1) entry1.place(x=10,y=ind) entry = AutocompleteEntry(entry1) test_list=list(set(pd.read_csv("./database.csv")['Name'])) if(np.nan in test_list): test_list.remove(np.nan) entry.set_completion_list(test_list) entry.pack() entry.focus_set() entry2=tk.Entry(window1) entry2.place(x=110,y=ind) entry3=tk.Entry(window1) entry3.place(x=210,y=ind) button1=tk.Button(window1,text="Add",command=store,fg="White", bg=mycolor) button1.place(x=400,y=430) button1=tk.Button(window1,text="New item",command=newent, fg="White", bg=mycolor) button1.place(x=400,y=400) '''Below function requires changes for different users''' def send_text() : text="Thank you for shopping with us! Here's your bill: " for i in range(len(curr_name)): text+=str(curr_name[i])+" - Rs."+str(curr_amount[i])+"\n" total_amount=0 for k in curr_amount : total_amount=total_amount+k text+="Total: "+str(total_amount) from twilio.rest import Client '''Create Twilio Account to get account_sid and auth_token''' account_sid = 'Account_sid' auth_token = 'Acc_Token' client = Client(account_sid, auth_token) '''from_ = 'whatsapp:+the number assigned by twilio',''' message = client.messages.create( from_='whatsapp:+000000000', body=text, to='whatsapp:+91'+curr_phone[0] ) print(message.sid) def
() : global ind overall_user.extend(curr_user) overall_phone.extend(curr_phone) overall_date.extend(curr_date) overall_time.extend(curr_time) overall_name.extend(curr_name) overall_price.extend(curr_price) overall_quantity.extend(curr_quantity) overall_amount.extend(curr_amount) overall_cno.extend(curr_cno) df=pd.DataFrame({"UserName":overall_user,"Phone":overall_phone,"Date":overall_date,"Time":overall_time,"Name":overall_name,"Price":overall_price,"Quantity":overall_quantity,"Amount":overall_amount,"Customer No" : overall_cno }) df.to_csv("./database.csv",index=False) ans=0 for k in curr_amount : ans=ans+k op=tk.Label(window1,text="Submission successful. Thank you for shopping! Click below button to print bill",bg="Light blue", fg=mycolor) op.place(x=50,y=ind+50) op1=tk.Label(window1,text=("Total amount : "+ str(ans) + "Rs."),bg="Light blue", fg=mycolor) op1.place(x=50,y=ind+80) button1=tk.Button(window1,text="Print Bill",command=print_bill, fg="White", bg=mycolor) button1.place(x=0,y=400) send_text() button3=tk.Button(window1,text="Submit",command=subm, fg="White", bg=mycolor) button3.place(x=400,y=460) lg=[] def recm() : df_new=pd.read_csv("./database.csv") for i in range(cno+1) : lg=[] for z in df_new.index : if df_new.iloc[z][8]==i : lg.append(df_new.iloc[z][4]) arrec.append(lg) booldata=te.fit(arrec).transform(arrec) dff_new=pd.DataFrame(booldata,columns=te.columns_) freq_items=apriori(dff_new,min_support=0.05,use_colnames=True) freq_items['Length']=freq_items['itemsets'].apply(lambda x: len(x)) recc=freq_items[(freq_items['Length']>=2) & (freq_items['support']>=0.02)] op=(recc.iloc[:,1].to_string(index=False)).split('\n') window_rec=tk.Tk() window_rec.title("Recommendations") window_rec.configure(background=mycolor) window_rec.geometry('300x300') for zz in op : l1=tk.Label(window_rec,text=zz,fg="White", bg=mycolor) l1.pack() button4=tk.Button(window1,text="Recommend",command=recm,fg="White", bg=mycolor) button4.place(x=400,y=490) f=0 def det() : w11=tk.Tk() w11.title("Find Details") w11.configure(background=mycolor) w11.geometry('600x600') l12=tk.Label(w11,text="Username",fg="White", bg=mycolor) l12.place(x=100,y=50) e12=tk.Entry(w11) e12.place(x=160,y=50) l22=tk.Label(w11,text="Phone",fg="White", bg=mycolor) l22.place(x=100,y=80) e22=tk.Entry(w11) e22.place(x=160,y=80) def det2() : df_d=pd.read_csv("./database.csv") global det_ind zzz=e12.get() yyy=e22.get() laa1=tk.Label(w11,text="Date",fg="White", bg=mycolor) laa2=tk.Label(w11,text="Time",fg="White", bg=mycolor) laa3=tk.Label(w11,text="Product",fg="White", bg=mycolor) laa4=tk.Label(w11,text="Price",fg="White", bg=mycolor) laa5=tk.Label(w11,text="Quantity",fg="White", bg=mycolor) laa6=tk.Label(w11,text="Amount",fg="White", bg=mycolor) laa1.place(x=30,y=160) laa2.place(x=100,y=160) laa3.place(x=170,y=160) laa4.place(x=240,y=160) laa5.place(x=310,y=160) laa6.place(x=380,y=160) global f for j in df_d.index : if (df_d.iloc[j][0]==zzz) & (df_d.iloc[j][1]==int(yyy)) : f=1 la1=tk.Label(w11,text=df_d.iloc[j][2],fg="White", bg=mycolor) la2=tk.Label(w11,text=df_d.iloc[j][3],fg="White", bg=mycolor) la3=tk.Label(w11,text=df_d.iloc[j][4],fg="White", bg=mycolor) la4=tk.Label(w11,text=df_d.iloc[j][5],fg="White", bg=mycolor) la5=tk.Label(w11,text=df_d.iloc[j][6],fg="White", bg=mycolor) la6=tk.Label(w11,text=df_d.iloc[j][7],fg="White", bg=mycolor) la1.place(x=30,y=det_ind) la2.place(x=100,y=det_ind) la3.place(x=170,y=det_ind) la4.place(x=240,y=det_ind) la5.place(x=310,y=det_ind) la6.place(x=380,y=det_ind) det_ind=det_ind+30 if f==0 : la7=tk.Label(w11,text="Not Found!",bg="White", fg=mycolor) la7.place(x=170,y=400) button6=tk.Button(w11,text="Submit",command=det2,fg="White", bg=mycolor) button6.place(x=170,y=115) button5=tk.Button(window1,text="Find Customer Details",command=det,fg="White", bg=mycolor) button5.place(x=400,y=520) window1.mainloop()
subm
identifier_name
main.py
import tkinter as tk from tkinter import * import datetime from functools import partial import requests import pandas as pd import numpy as np import sys import os import tkinter.ttk from mlxtend.preprocessing import TransactionEncoder from mlxtend.frequent_patterns import apriori te=TransactionEncoder() dff=pd.read_csv("./database.csv") ind=110 det_ind=200 arrec=[] mycolor = '#%02x%02x%02x' % (50, 50, 50) added_count=0 newent_count=0 twilio_account_id="API Key" tkinter_umlauts=['odiaeresis', 'adiaeresis', 'udiaeresis', 'Odiaeresis', 'Adiaeresis', 'Udiaeresis', 'ssharp'] class AutocompleteEntry(tk.Entry): """ Subclass of tk.Entry that features autocompletion. To enable autocompletion use set_completion_list(list) to define a list of possible strings to hit. To cycle through hits use down and up arrow keys. """ def set_completion_list(self, completion_list):
def autocomplete(self, delta=0): """autocomplete the Entry, delta may be 0/1/-1 to cycle through possible hits""" if delta: # need to delete selection otherwise we would fix the current position self.delete(self.position, tk.END) else: # set position to end so selection starts where textentry ended self.position = len(self.get()) # collect hits _hits = [] for element in self._completion_list: if element.lower().startswith(self.get().lower()): # Match case-insensitively _hits.append(element) # if we have a new hit list, keep this in mind if _hits != self._hits: self._hit_index = 0 self._hits=_hits # only allow cycling if we are in a known hit list if _hits == self._hits and self._hits: self._hit_index = (self._hit_index + delta) % len(self._hits) # now finally perform the auto completion if self._hits: self.delete(0,tk.END) self.insert(0,self._hits[self._hit_index]) self.select_range(self.position,tk.END) entry1.delete(0,tk.END) entry1.insert(0,self.get()) def handle_keyrelease(self, event): """event handler for the keyrelease event on this widget""" if event.keysym == "BackSpace": self.delete(self.index(tk.INSERT), tk.END) self.position = self.index(tk.END) if event.keysym == "Left": if self.position < self.index(tk.END): # delete the selection self.delete(self.position, tk.END) else: self.position = self.position-1 # delete one character self.delete(self.position, tk.END) if event.keysym == "Right": self.position = self.index(tk.END) # go to end (no selection) if event.keysym == "Down": self.autocomplete(1) # cycle to next hit if event.keysym == "Up": self.autocomplete(-1) # cycle to previous hit if len(event.keysym) == 1 or event.keysym in tkinter_umlauts: self.autocomplete() overall_user=dff.iloc[:,0] overall_user=np.array(overall_user) overall_user=list(overall_user) overall_phone=dff.iloc[:,1] overall_phone=np.array(overall_phone) overall_phone=list(overall_phone) overall_date=dff.iloc[:,2] overall_date=np.array(overall_date) overall_date=list(overall_date) overall_time=dff.iloc[:,3] overall_time=np.array(overall_time) overall_time=list(overall_time) overall_name=dff.iloc[:,4] overall_name=np.array(overall_name) overall_name=list(overall_name) overall_price=dff.iloc[:,5] overall_price=np.array(overall_price) overall_price=list(overall_price) overall_quantity=dff.iloc[:,6] overall_quantity=np.array(overall_quantity) overall_quantity=list(overall_quantity) overall_amount=dff.iloc[:,7] overall_amount=np.array(overall_amount) overall_amount=list(overall_amount) overall_cno=dff.iloc[:,8] overall_cno=np.array(overall_cno) overall_cno=list(overall_cno) cno=dff["Customer No"][len(overall_cno)-1] + 1 curr_user=[] curr_phone=[] curr_date=[] curr_time=[] curr_name=[] curr_price=[] curr_quantity=[] curr_amount=[] curr_cno=[] def print_bill(): if os.path.isfile('print.txt'): os.remove('print.txt') with open('print.txt','a') as file: file.write('\t\tThank you for shopping\t\t\n') file.write('\t\t-----------------------\t\t\n') file.write(f'{curr_date[0]}\t\t\t{curr_time[0]}\n') file.write(f'Customer Name: {curr_user[0]}\n') file.write(f'Customer Phone: {curr_phone[0]}\n') file.write('Product\t\t\tQuantity\t\tPrice\t\t\tAmount\n') for i in range(len(curr_name)): with open('print.txt','a') as file: file.write(f'{curr_name[i]}\t\t\t{curr_quantity[i]}\t\t\t{curr_price[i]}\t\t\t{curr_amount[i]}\n') with open('print.txt','a') as file: file.write(f'Payable Amount:\tRs.{sum(curr_amount)}\n') os.startfile("print.txt", "print") #print bill using printer window1=tk.Tk() window1.configure(background="Light blue") window1.title("Supermarket Recommendation System") window1.geometry('600x600') now = datetime.datetime.now() date=now.strftime("%Y-%m-%d") time=now.strftime("%H:%M:%S") timee=tk.Label(window1,text=time, bg="Light blue", fg=mycolor) timee.place(x=200,y=15) datee=tk.Label(window1,text=date,bg="Light blue", fg=mycolor) datee.place(x=300,y=15) e11=tk.Label(window1,text="Name : ",bg="Light blue", fg=mycolor) e11.place(x=50,y=45) e22=tk.Label(window1,text="Phone Number : ",bg="Light blue", fg=mycolor) e22.place(x=270,y=45) e1=tk.Entry(window1) e1.place(x=100,y=45) e2=tk.Entry(window1) e2.place(x=380,y=45) l1=tk.Label(window1,text="Item name",bg="Light blue", fg=mycolor) l1.place(x=10, y=80) l2=tk.Label(window1,text="Price",bg="Light blue", fg=mycolor) l2.place(x=110, y=80) l3=tk.Label(window1,text="Quantity",bg="Light blue", fg=mycolor) l3.place(x=210, y=80) l3=tk.Label(window1,text="Amount",bg="Light blue", fg=mycolor) l3.place(x=310, y=80) def store() : global added_count added_count=added_count+1 global e1,e2 usern=e1.get() phno=e2.get() x=entry1.get() y=entry2.get() z=entry3.get() y=int(y) z=int(z) w=z*y l4=tk.Label(window1,text=(str(w)+"Rs."),bg="Light blue", fg=mycolor) l4.place(x=310,y=ind) l5=tk.Label(window1,text="Added.",bg="Light blue", fg=mycolor) l5.place(x=410,y=ind) curr_user.append(usern) curr_phone.append(phno) curr_date.append(date) curr_time.append(time) curr_name.append(x) curr_price.append(y) curr_quantity.append(z) curr_amount.append(w) curr_cno.append(cno) def newent() : global newent_count newent_count=newent_count+1 if(newent_count!=added_count+1 and newent_count!=0): store() global ind ind=ind+20 global entry1,entry2,entry3 entry1=tk.Entry(window1) entry1.place(x=10,y=ind) entry = AutocompleteEntry(entry1) test_list=list(set(pd.read_csv("./database.csv")['Name'])) if(np.nan in test_list): test_list.remove(np.nan) entry.set_completion_list(test_list) entry.pack() entry.focus_set() entry2=tk.Entry(window1) entry2.place(x=110,y=ind) entry3=tk.Entry(window1) entry3.place(x=210,y=ind) button1=tk.Button(window1,text="Add",command=store,fg="White", bg=mycolor) button1.place(x=400,y=430) button1=tk.Button(window1,text="New item",command=newent, fg="White", bg=mycolor) button1.place(x=400,y=400) '''Below function requires changes for different users''' def send_text() : text="Thank you for shopping with us! Here's your bill: " for i in range(len(curr_name)): text+=str(curr_name[i])+" - Rs."+str(curr_amount[i])+"\n" total_amount=0 for k in curr_amount : total_amount=total_amount+k text+="Total: "+str(total_amount) from twilio.rest import Client '''Create Twilio Account to get account_sid and auth_token''' account_sid = 'Account_sid' auth_token = 'Acc_Token' client = Client(account_sid, auth_token) '''from_ = 'whatsapp:+the number assigned by twilio',''' message = client.messages.create( from_='whatsapp:+000000000', body=text, to='whatsapp:+91'+curr_phone[0] ) print(message.sid) def subm() : global ind overall_user.extend(curr_user) overall_phone.extend(curr_phone) overall_date.extend(curr_date) overall_time.extend(curr_time) overall_name.extend(curr_name) overall_price.extend(curr_price) overall_quantity.extend(curr_quantity) overall_amount.extend(curr_amount) overall_cno.extend(curr_cno) df=pd.DataFrame({"UserName":overall_user,"Phone":overall_phone,"Date":overall_date,"Time":overall_time,"Name":overall_name,"Price":overall_price,"Quantity":overall_quantity,"Amount":overall_amount,"Customer No" : overall_cno }) df.to_csv("./database.csv",index=False) ans=0 for k in curr_amount : ans=ans+k op=tk.Label(window1,text="Submission successful. Thank you for shopping! Click below button to print bill",bg="Light blue", fg=mycolor) op.place(x=50,y=ind+50) op1=tk.Label(window1,text=("Total amount : "+ str(ans) + "Rs."),bg="Light blue", fg=mycolor) op1.place(x=50,y=ind+80) button1=tk.Button(window1,text="Print Bill",command=print_bill, fg="White", bg=mycolor) button1.place(x=0,y=400) send_text() button3=tk.Button(window1,text="Submit",command=subm, fg="White", bg=mycolor) button3.place(x=400,y=460) lg=[] def recm() : df_new=pd.read_csv("./database.csv") for i in range(cno+1) : lg=[] for z in df_new.index : if df_new.iloc[z][8]==i : lg.append(df_new.iloc[z][4]) arrec.append(lg) booldata=te.fit(arrec).transform(arrec) dff_new=pd.DataFrame(booldata,columns=te.columns_) freq_items=apriori(dff_new,min_support=0.05,use_colnames=True) freq_items['Length']=freq_items['itemsets'].apply(lambda x: len(x)) recc=freq_items[(freq_items['Length']>=2) & (freq_items['support']>=0.02)] op=(recc.iloc[:,1].to_string(index=False)).split('\n') window_rec=tk.Tk() window_rec.title("Recommendations") window_rec.configure(background=mycolor) window_rec.geometry('300x300') for zz in op : l1=tk.Label(window_rec,text=zz,fg="White", bg=mycolor) l1.pack() button4=tk.Button(window1,text="Recommend",command=recm,fg="White", bg=mycolor) button4.place(x=400,y=490) f=0 def det() : w11=tk.Tk() w11.title("Find Details") w11.configure(background=mycolor) w11.geometry('600x600') l12=tk.Label(w11,text="Username",fg="White", bg=mycolor) l12.place(x=100,y=50) e12=tk.Entry(w11) e12.place(x=160,y=50) l22=tk.Label(w11,text="Phone",fg="White", bg=mycolor) l22.place(x=100,y=80) e22=tk.Entry(w11) e22.place(x=160,y=80) def det2() : df_d=pd.read_csv("./database.csv") global det_ind zzz=e12.get() yyy=e22.get() laa1=tk.Label(w11,text="Date",fg="White", bg=mycolor) laa2=tk.Label(w11,text="Time",fg="White", bg=mycolor) laa3=tk.Label(w11,text="Product",fg="White", bg=mycolor) laa4=tk.Label(w11,text="Price",fg="White", bg=mycolor) laa5=tk.Label(w11,text="Quantity",fg="White", bg=mycolor) laa6=tk.Label(w11,text="Amount",fg="White", bg=mycolor) laa1.place(x=30,y=160) laa2.place(x=100,y=160) laa3.place(x=170,y=160) laa4.place(x=240,y=160) laa5.place(x=310,y=160) laa6.place(x=380,y=160) global f for j in df_d.index : if (df_d.iloc[j][0]==zzz) & (df_d.iloc[j][1]==int(yyy)) : f=1 la1=tk.Label(w11,text=df_d.iloc[j][2],fg="White", bg=mycolor) la2=tk.Label(w11,text=df_d.iloc[j][3],fg="White", bg=mycolor) la3=tk.Label(w11,text=df_d.iloc[j][4],fg="White", bg=mycolor) la4=tk.Label(w11,text=df_d.iloc[j][5],fg="White", bg=mycolor) la5=tk.Label(w11,text=df_d.iloc[j][6],fg="White", bg=mycolor) la6=tk.Label(w11,text=df_d.iloc[j][7],fg="White", bg=mycolor) la1.place(x=30,y=det_ind) la2.place(x=100,y=det_ind) la3.place(x=170,y=det_ind) la4.place(x=240,y=det_ind) la5.place(x=310,y=det_ind) la6.place(x=380,y=det_ind) det_ind=det_ind+30 if f==0 : la7=tk.Label(w11,text="Not Found!",bg="White", fg=mycolor) la7.place(x=170,y=400) button6=tk.Button(w11,text="Submit",command=det2,fg="White", bg=mycolor) button6.place(x=170,y=115) button5=tk.Button(window1,text="Find Customer Details",command=det,fg="White", bg=mycolor) button5.place(x=400,y=520) window1.mainloop()
self._completion_list = sorted(completion_list, key=str.lower) # Work with a sorted list self._hits = [] self._hit_index = 0 self.position = 0 self.bind('<KeyRelease>', self.handle_keyrelease)
identifier_body
main.py
import tkinter as tk from tkinter import *
from functools import partial import requests import pandas as pd import numpy as np import sys import os import tkinter.ttk from mlxtend.preprocessing import TransactionEncoder from mlxtend.frequent_patterns import apriori te=TransactionEncoder() dff=pd.read_csv("./database.csv") ind=110 det_ind=200 arrec=[] mycolor = '#%02x%02x%02x' % (50, 50, 50) added_count=0 newent_count=0 twilio_account_id="API Key" tkinter_umlauts=['odiaeresis', 'adiaeresis', 'udiaeresis', 'Odiaeresis', 'Adiaeresis', 'Udiaeresis', 'ssharp'] class AutocompleteEntry(tk.Entry): """ Subclass of tk.Entry that features autocompletion. To enable autocompletion use set_completion_list(list) to define a list of possible strings to hit. To cycle through hits use down and up arrow keys. """ def set_completion_list(self, completion_list): self._completion_list = sorted(completion_list, key=str.lower) # Work with a sorted list self._hits = [] self._hit_index = 0 self.position = 0 self.bind('<KeyRelease>', self.handle_keyrelease) def autocomplete(self, delta=0): """autocomplete the Entry, delta may be 0/1/-1 to cycle through possible hits""" if delta: # need to delete selection otherwise we would fix the current position self.delete(self.position, tk.END) else: # set position to end so selection starts where textentry ended self.position = len(self.get()) # collect hits _hits = [] for element in self._completion_list: if element.lower().startswith(self.get().lower()): # Match case-insensitively _hits.append(element) # if we have a new hit list, keep this in mind if _hits != self._hits: self._hit_index = 0 self._hits=_hits # only allow cycling if we are in a known hit list if _hits == self._hits and self._hits: self._hit_index = (self._hit_index + delta) % len(self._hits) # now finally perform the auto completion if self._hits: self.delete(0,tk.END) self.insert(0,self._hits[self._hit_index]) self.select_range(self.position,tk.END) entry1.delete(0,tk.END) entry1.insert(0,self.get()) def handle_keyrelease(self, event): """event handler for the keyrelease event on this widget""" if event.keysym == "BackSpace": self.delete(self.index(tk.INSERT), tk.END) self.position = self.index(tk.END) if event.keysym == "Left": if self.position < self.index(tk.END): # delete the selection self.delete(self.position, tk.END) else: self.position = self.position-1 # delete one character self.delete(self.position, tk.END) if event.keysym == "Right": self.position = self.index(tk.END) # go to end (no selection) if event.keysym == "Down": self.autocomplete(1) # cycle to next hit if event.keysym == "Up": self.autocomplete(-1) # cycle to previous hit if len(event.keysym) == 1 or event.keysym in tkinter_umlauts: self.autocomplete() overall_user=dff.iloc[:,0] overall_user=np.array(overall_user) overall_user=list(overall_user) overall_phone=dff.iloc[:,1] overall_phone=np.array(overall_phone) overall_phone=list(overall_phone) overall_date=dff.iloc[:,2] overall_date=np.array(overall_date) overall_date=list(overall_date) overall_time=dff.iloc[:,3] overall_time=np.array(overall_time) overall_time=list(overall_time) overall_name=dff.iloc[:,4] overall_name=np.array(overall_name) overall_name=list(overall_name) overall_price=dff.iloc[:,5] overall_price=np.array(overall_price) overall_price=list(overall_price) overall_quantity=dff.iloc[:,6] overall_quantity=np.array(overall_quantity) overall_quantity=list(overall_quantity) overall_amount=dff.iloc[:,7] overall_amount=np.array(overall_amount) overall_amount=list(overall_amount) overall_cno=dff.iloc[:,8] overall_cno=np.array(overall_cno) overall_cno=list(overall_cno) cno=dff["Customer No"][len(overall_cno)-1] + 1 curr_user=[] curr_phone=[] curr_date=[] curr_time=[] curr_name=[] curr_price=[] curr_quantity=[] curr_amount=[] curr_cno=[] def print_bill(): if os.path.isfile('print.txt'): os.remove('print.txt') with open('print.txt','a') as file: file.write('\t\tThank you for shopping\t\t\n') file.write('\t\t-----------------------\t\t\n') file.write(f'{curr_date[0]}\t\t\t{curr_time[0]}\n') file.write(f'Customer Name: {curr_user[0]}\n') file.write(f'Customer Phone: {curr_phone[0]}\n') file.write('Product\t\t\tQuantity\t\tPrice\t\t\tAmount\n') for i in range(len(curr_name)): with open('print.txt','a') as file: file.write(f'{curr_name[i]}\t\t\t{curr_quantity[i]}\t\t\t{curr_price[i]}\t\t\t{curr_amount[i]}\n') with open('print.txt','a') as file: file.write(f'Payable Amount:\tRs.{sum(curr_amount)}\n') os.startfile("print.txt", "print") #print bill using printer window1=tk.Tk() window1.configure(background="Light blue") window1.title("Supermarket Recommendation System") window1.geometry('600x600') now = datetime.datetime.now() date=now.strftime("%Y-%m-%d") time=now.strftime("%H:%M:%S") timee=tk.Label(window1,text=time, bg="Light blue", fg=mycolor) timee.place(x=200,y=15) datee=tk.Label(window1,text=date,bg="Light blue", fg=mycolor) datee.place(x=300,y=15) e11=tk.Label(window1,text="Name : ",bg="Light blue", fg=mycolor) e11.place(x=50,y=45) e22=tk.Label(window1,text="Phone Number : ",bg="Light blue", fg=mycolor) e22.place(x=270,y=45) e1=tk.Entry(window1) e1.place(x=100,y=45) e2=tk.Entry(window1) e2.place(x=380,y=45) l1=tk.Label(window1,text="Item name",bg="Light blue", fg=mycolor) l1.place(x=10, y=80) l2=tk.Label(window1,text="Price",bg="Light blue", fg=mycolor) l2.place(x=110, y=80) l3=tk.Label(window1,text="Quantity",bg="Light blue", fg=mycolor) l3.place(x=210, y=80) l3=tk.Label(window1,text="Amount",bg="Light blue", fg=mycolor) l3.place(x=310, y=80) def store() : global added_count added_count=added_count+1 global e1,e2 usern=e1.get() phno=e2.get() x=entry1.get() y=entry2.get() z=entry3.get() y=int(y) z=int(z) w=z*y l4=tk.Label(window1,text=(str(w)+"Rs."),bg="Light blue", fg=mycolor) l4.place(x=310,y=ind) l5=tk.Label(window1,text="Added.",bg="Light blue", fg=mycolor) l5.place(x=410,y=ind) curr_user.append(usern) curr_phone.append(phno) curr_date.append(date) curr_time.append(time) curr_name.append(x) curr_price.append(y) curr_quantity.append(z) curr_amount.append(w) curr_cno.append(cno) def newent() : global newent_count newent_count=newent_count+1 if(newent_count!=added_count+1 and newent_count!=0): store() global ind ind=ind+20 global entry1,entry2,entry3 entry1=tk.Entry(window1) entry1.place(x=10,y=ind) entry = AutocompleteEntry(entry1) test_list=list(set(pd.read_csv("./database.csv")['Name'])) if(np.nan in test_list): test_list.remove(np.nan) entry.set_completion_list(test_list) entry.pack() entry.focus_set() entry2=tk.Entry(window1) entry2.place(x=110,y=ind) entry3=tk.Entry(window1) entry3.place(x=210,y=ind) button1=tk.Button(window1,text="Add",command=store,fg="White", bg=mycolor) button1.place(x=400,y=430) button1=tk.Button(window1,text="New item",command=newent, fg="White", bg=mycolor) button1.place(x=400,y=400) '''Below function requires changes for different users''' def send_text() : text="Thank you for shopping with us! Here's your bill: " for i in range(len(curr_name)): text+=str(curr_name[i])+" - Rs."+str(curr_amount[i])+"\n" total_amount=0 for k in curr_amount : total_amount=total_amount+k text+="Total: "+str(total_amount) from twilio.rest import Client '''Create Twilio Account to get account_sid and auth_token''' account_sid = 'Account_sid' auth_token = 'Acc_Token' client = Client(account_sid, auth_token) '''from_ = 'whatsapp:+the number assigned by twilio',''' message = client.messages.create( from_='whatsapp:+000000000', body=text, to='whatsapp:+91'+curr_phone[0] ) print(message.sid) def subm() : global ind overall_user.extend(curr_user) overall_phone.extend(curr_phone) overall_date.extend(curr_date) overall_time.extend(curr_time) overall_name.extend(curr_name) overall_price.extend(curr_price) overall_quantity.extend(curr_quantity) overall_amount.extend(curr_amount) overall_cno.extend(curr_cno) df=pd.DataFrame({"UserName":overall_user,"Phone":overall_phone,"Date":overall_date,"Time":overall_time,"Name":overall_name,"Price":overall_price,"Quantity":overall_quantity,"Amount":overall_amount,"Customer No" : overall_cno }) df.to_csv("./database.csv",index=False) ans=0 for k in curr_amount : ans=ans+k op=tk.Label(window1,text="Submission successful. Thank you for shopping! Click below button to print bill",bg="Light blue", fg=mycolor) op.place(x=50,y=ind+50) op1=tk.Label(window1,text=("Total amount : "+ str(ans) + "Rs."),bg="Light blue", fg=mycolor) op1.place(x=50,y=ind+80) button1=tk.Button(window1,text="Print Bill",command=print_bill, fg="White", bg=mycolor) button1.place(x=0,y=400) send_text() button3=tk.Button(window1,text="Submit",command=subm, fg="White", bg=mycolor) button3.place(x=400,y=460) lg=[] def recm() : df_new=pd.read_csv("./database.csv") for i in range(cno+1) : lg=[] for z in df_new.index : if df_new.iloc[z][8]==i : lg.append(df_new.iloc[z][4]) arrec.append(lg) booldata=te.fit(arrec).transform(arrec) dff_new=pd.DataFrame(booldata,columns=te.columns_) freq_items=apriori(dff_new,min_support=0.05,use_colnames=True) freq_items['Length']=freq_items['itemsets'].apply(lambda x: len(x)) recc=freq_items[(freq_items['Length']>=2) & (freq_items['support']>=0.02)] op=(recc.iloc[:,1].to_string(index=False)).split('\n') window_rec=tk.Tk() window_rec.title("Recommendations") window_rec.configure(background=mycolor) window_rec.geometry('300x300') for zz in op : l1=tk.Label(window_rec,text=zz,fg="White", bg=mycolor) l1.pack() button4=tk.Button(window1,text="Recommend",command=recm,fg="White", bg=mycolor) button4.place(x=400,y=490) f=0 def det() : w11=tk.Tk() w11.title("Find Details") w11.configure(background=mycolor) w11.geometry('600x600') l12=tk.Label(w11,text="Username",fg="White", bg=mycolor) l12.place(x=100,y=50) e12=tk.Entry(w11) e12.place(x=160,y=50) l22=tk.Label(w11,text="Phone",fg="White", bg=mycolor) l22.place(x=100,y=80) e22=tk.Entry(w11) e22.place(x=160,y=80) def det2() : df_d=pd.read_csv("./database.csv") global det_ind zzz=e12.get() yyy=e22.get() laa1=tk.Label(w11,text="Date",fg="White", bg=mycolor) laa2=tk.Label(w11,text="Time",fg="White", bg=mycolor) laa3=tk.Label(w11,text="Product",fg="White", bg=mycolor) laa4=tk.Label(w11,text="Price",fg="White", bg=mycolor) laa5=tk.Label(w11,text="Quantity",fg="White", bg=mycolor) laa6=tk.Label(w11,text="Amount",fg="White", bg=mycolor) laa1.place(x=30,y=160) laa2.place(x=100,y=160) laa3.place(x=170,y=160) laa4.place(x=240,y=160) laa5.place(x=310,y=160) laa6.place(x=380,y=160) global f for j in df_d.index : if (df_d.iloc[j][0]==zzz) & (df_d.iloc[j][1]==int(yyy)) : f=1 la1=tk.Label(w11,text=df_d.iloc[j][2],fg="White", bg=mycolor) la2=tk.Label(w11,text=df_d.iloc[j][3],fg="White", bg=mycolor) la3=tk.Label(w11,text=df_d.iloc[j][4],fg="White", bg=mycolor) la4=tk.Label(w11,text=df_d.iloc[j][5],fg="White", bg=mycolor) la5=tk.Label(w11,text=df_d.iloc[j][6],fg="White", bg=mycolor) la6=tk.Label(w11,text=df_d.iloc[j][7],fg="White", bg=mycolor) la1.place(x=30,y=det_ind) la2.place(x=100,y=det_ind) la3.place(x=170,y=det_ind) la4.place(x=240,y=det_ind) la5.place(x=310,y=det_ind) la6.place(x=380,y=det_ind) det_ind=det_ind+30 if f==0 : la7=tk.Label(w11,text="Not Found!",bg="White", fg=mycolor) la7.place(x=170,y=400) button6=tk.Button(w11,text="Submit",command=det2,fg="White", bg=mycolor) button6.place(x=170,y=115) button5=tk.Button(window1,text="Find Customer Details",command=det,fg="White", bg=mycolor) button5.place(x=400,y=520) window1.mainloop()
import datetime
random_line_split
MapServiceDownload.py
#------------------------------------------------------------- # Name: Map Service Download # Purpose: Downloads the data used in a map service layer by querying the json # and converting to a feature class. # Existing Mode - Will delete and append records, so field names need to be the same. # New Mode - Copies data over (including archive datasets if needed). Requires no locks on geodatabase datasets being overwritten. # Author: Shaun Weston (shaun_weston@eagle.co.nz) # Date Created: 14/08/2013 # Last Updated: 24/11/2015 # Copyright: (c) Eagle Technology # ArcGIS Version: 10.3+ # Python Version: 2.7 #-------------------------------- # Import modules import os import sys import logging import smtplib import arcpy import json import urllib import urllib2 import uuid import math # Enable data to be overwritten arcpy.env.overwriteOutput = True # Set global variables enableLogging = "false" # Use logger.info("Example..."), logger.warning("Example..."), logger.error("Example...") logFile = "" # os.path.join(os.path.dirname(__file__), "Example.log") sendErrorEmail = "false" emailTo = "" emailUser = "" emailPassword = "" emailSubject = "" emailMessage = "" enableProxy = "false" requestProtocol = "http" # http or https proxyURL = "" output = None # Start of main function def mainFunction(mapServiceLayer,outputFeatureClass,updateMode): # Get parameters from ArcGIS Desktop tool by seperating by comma e.g. (var1 is 1st parameter,var2 is 2nd parameter,var3 is 3rd parameter) try: # --------------------------------------- Start of code --------------------------------------- # # Querying thet map service to get the count of records arcpy.AddMessage("Querying the map service...") mapServiceQuery1 = mapServiceLayer + "/query?where=1%3D1&returnIdsOnly=true&f=pjson" urlResponse = urllib.urlopen(mapServiceQuery1); # Get json for the response - Object IDs mapServiceQuery1JSONData = json.loads(urlResponse.read()) objectIDs = mapServiceQuery1JSONData["objectIds"] objectIDs.sort() arcpy.AddMessage("Number of records in the layer - " + str(len(objectIDs)) + "...") # Set the number of records per request and the number of requests that need to be made maxRecords = 1000 # If under maxRecords, just need to make one request if (len(objectIDs) < maxRecords): requestsToMake = 1 else: # Calculate the number of requests - Always round up requestsToMake = math.ceil(float(len(objectIDs)) / float(maxRecords)) arcpy.AddMessage("Downloading data to " + arcpy.env.scratchFolder + "...") # For every request count = 0 while (int(requestsToMake) > count): # Create the query startObjectID = int(objectIDs[count*maxRecords]) # If at the final request or if there is only one request that needs to be made if ((int(requestsToMake) == (count+1)) or (requestsToMake == 1)): # Get the last object ID endObjectID = int(objectIDs[len(objectIDs)-1]) serviceQuery = "OBJECTID>%3D" + str(startObjectID) + "+AND+OBJECTID<%3D" + str(endObjectID) else: # Start object ID plus 1000 records endObjectID = int(objectIDs[(count*maxRecords)+maxRecords]) serviceQuery = "OBJECTID>%3D" + str(startObjectID) + "+AND+OBJECTID<" + str(endObjectID) # Query the map service to data in json format try:
# Download the data fileChunk = 16 * 1024 downloadedFile = os.path.join(arcpy.env.scratchFolder, "Data-" + str(uuid.uuid1()) + ".json") with open(downloadedFile, 'wb') as file: downloadCount = 0 while True: chunk = response.read(fileChunk) # If data size is small if ((downloadCount == 0) and (len(chunk) < 1000)): # Log error and end download arcpy.AddError("No data returned, check the URL...") sys.exit() if not chunk: break # Write chunk to output file file.write(chunk) downloadCount = downloadCount + 1 file.close() # If it's the first request if (count == 0): # Create new dataset arcpy.JSONToFeatures_conversion(downloadedFile, os.path.join(arcpy.env.scratchGDB, "Dataset")) else: # Create dataset and load into existing arcpy.JSONToFeatures_conversion(downloadedFile, "in_memory\\DatasetTemp") arcpy.Append_management("in_memory\\DatasetTemp", os.path.join(arcpy.env.scratchGDB, "Dataset"), "NO_TEST", "", "") # If at the final request or if there is only one request that needs to be made if ((int(requestsToMake) == (count+1)) or (requestsToMake == 1)): arcpy.AddMessage("Downloaded and converted JSON for " + str(len(objectIDs)) + " of " + str(len(objectIDs)) + " features...") else: arcpy.AddMessage("Downloaded and converted JSON for " + str((count+1)*maxRecords) + " of " + str(len(objectIDs)) + " features...") count = count + 1 # Convert JSON to feature class arcpy.AddMessage("Copying over final dataset...") # Overwrite dataset if (updateMode.lower() == "new"): # Get record count recordCount = arcpy.GetCount_management(os.path.join(arcpy.env.scratchGDB, "Dataset")) arcpy.AddMessage("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Logging if (enableLogging == "true"): # Log record count logger.info("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Load in data if (recordCount > 0): arcpy.CopyFeatures_management(os.path.join(arcpy.env.scratchGDB, "Dataset"), outputFeatureClass, "", "0", "0", "0") # Delete and append else: # Get record count recordCount = arcpy.GetCount_management(os.path.join(arcpy.env.scratchGDB, "Dataset")) arcpy.AddMessage("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Logging if (enableLogging == "true"): # Log record count logger.info("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Load in data if (recordCount > 0): arcpy.DeleteFeatures_management(outputFeatureClass) arcpy.Append_management(os.path.join(arcpy.env.scratchGDB, "Dataset"), outputFeatureClass, "NO_TEST", "", "") # --------------------------------------- End of code --------------------------------------- # # If called from gp tool return the arcpy parameter if __name__ == '__main__': # Return the output if there is any if output: arcpy.SetParameterAsText(1, output) # Otherwise return the result else: # Return the output if there is any if output: return output # Logging if (enableLogging == "true"): # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] # If arcpy error except arcpy.ExecuteError: # Build and show the error message errorMessage = arcpy.GetMessages(2) arcpy.AddError(errorMessage) # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage) # If python error except Exception as e: errorMessage = "" # Build and show the error message for i in range(len(e.args)): if (i == 0): errorMessage = unicode(e.args[i]).encode('utf-8') else: errorMessage = errorMessage + " " + unicode(e.args[i]).encode('utf-8') arcpy.AddError(errorMessage) # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage) # End of main function # Start of set logging function def setLogging(logFile): # Create a logger logger = logging.getLogger(os.path.basename(__file__)) logger.setLevel(logging.DEBUG) # Setup log message handler logMessage = logging.FileHandler(logFile) # Setup the log formatting logFormat = logging.Formatter("%(asctime)s: %(levelname)s - %(message)s", "%d/%m/%Y - %H:%M:%S") # Add formatter to log message handler logMessage.setFormatter(logFormat) # Add log message handler to logger logger.addHandler(logMessage) return logger, logMessage # End of set logging function # Start of send email function def sendEmail(message): # Send an email arcpy.AddMessage("Sending email...") # Server and port information smtpServer = smtplib.SMTP("smtp.gmail.com",587) smtpServer.ehlo() smtpServer.starttls() smtpServer.ehlo # Login with sender email address and password smtpServer.login(emailUser, emailPassword) # Email content header = 'To:' + emailTo + '\n' + 'From: ' + emailUser + '\n' + 'Subject:' + emailSubject + '\n' body = header + '\n' + emailMessage + '\n' + '\n' + message # Send the email and close the connection smtpServer.sendmail(emailUser, emailTo, body) # End of send email function # This test allows the script to be used from the operating # system command prompt (stand-alone), in a Python IDE, # as a geoprocessing script tool, or as a module imported in # another script if __name__ == '__main__': # Arguments are optional - If running from ArcGIS Desktop tool, parameters will be loaded into *argv argv = tuple(arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount())) # Logging if (enableLogging == "true"): # Setup logging logger, logMessage = setLogging(logFile) # Log start of process logger.info("Process started.") # Setup the use of a proxy for requests if (enableProxy == "true"): # Setup the proxy proxy = urllib2.ProxyHandler({requestProtocol : proxyURL}) openURL = urllib2.build_opener(proxy) # Install the proxy urllib2.install_opener(openURL) mainFunction(*argv)
mapServiceQuery2 = mapServiceLayer + "/query?where=" + serviceQuery + "&returnCountOnly=false&returnIdsOnly=false&returnGeometry=true&outFields=*&f=pjson" response = urllib2.urlopen(mapServiceQuery2) except urllib2.URLError, e: arcpy.AddError("There was an error: %r" % e)
random_line_split
MapServiceDownload.py
#------------------------------------------------------------- # Name: Map Service Download # Purpose: Downloads the data used in a map service layer by querying the json # and converting to a feature class. # Existing Mode - Will delete and append records, so field names need to be the same. # New Mode - Copies data over (including archive datasets if needed). Requires no locks on geodatabase datasets being overwritten. # Author: Shaun Weston (shaun_weston@eagle.co.nz) # Date Created: 14/08/2013 # Last Updated: 24/11/2015 # Copyright: (c) Eagle Technology # ArcGIS Version: 10.3+ # Python Version: 2.7 #-------------------------------- # Import modules import os import sys import logging import smtplib import arcpy import json import urllib import urllib2 import uuid import math # Enable data to be overwritten arcpy.env.overwriteOutput = True # Set global variables enableLogging = "false" # Use logger.info("Example..."), logger.warning("Example..."), logger.error("Example...") logFile = "" # os.path.join(os.path.dirname(__file__), "Example.log") sendErrorEmail = "false" emailTo = "" emailUser = "" emailPassword = "" emailSubject = "" emailMessage = "" enableProxy = "false" requestProtocol = "http" # http or https proxyURL = "" output = None # Start of main function def mainFunction(mapServiceLayer,outputFeatureClass,updateMode): # Get parameters from ArcGIS Desktop tool by seperating by comma e.g. (var1 is 1st parameter,var2 is 2nd parameter,var3 is 3rd parameter) try: # --------------------------------------- Start of code --------------------------------------- # # Querying thet map service to get the count of records arcpy.AddMessage("Querying the map service...") mapServiceQuery1 = mapServiceLayer + "/query?where=1%3D1&returnIdsOnly=true&f=pjson" urlResponse = urllib.urlopen(mapServiceQuery1); # Get json for the response - Object IDs mapServiceQuery1JSONData = json.loads(urlResponse.read()) objectIDs = mapServiceQuery1JSONData["objectIds"] objectIDs.sort() arcpy.AddMessage("Number of records in the layer - " + str(len(objectIDs)) + "...") # Set the number of records per request and the number of requests that need to be made maxRecords = 1000 # If under maxRecords, just need to make one request if (len(objectIDs) < maxRecords): requestsToMake = 1 else: # Calculate the number of requests - Always round up requestsToMake = math.ceil(float(len(objectIDs)) / float(maxRecords)) arcpy.AddMessage("Downloading data to " + arcpy.env.scratchFolder + "...") # For every request count = 0 while (int(requestsToMake) > count): # Create the query startObjectID = int(objectIDs[count*maxRecords]) # If at the final request or if there is only one request that needs to be made if ((int(requestsToMake) == (count+1)) or (requestsToMake == 1)): # Get the last object ID endObjectID = int(objectIDs[len(objectIDs)-1]) serviceQuery = "OBJECTID>%3D" + str(startObjectID) + "+AND+OBJECTID<%3D" + str(endObjectID) else: # Start object ID plus 1000 records endObjectID = int(objectIDs[(count*maxRecords)+maxRecords]) serviceQuery = "OBJECTID>%3D" + str(startObjectID) + "+AND+OBJECTID<" + str(endObjectID) # Query the map service to data in json format try: mapServiceQuery2 = mapServiceLayer + "/query?where=" + serviceQuery + "&returnCountOnly=false&returnIdsOnly=false&returnGeometry=true&outFields=*&f=pjson" response = urllib2.urlopen(mapServiceQuery2) except urllib2.URLError, e: arcpy.AddError("There was an error: %r" % e) # Download the data fileChunk = 16 * 1024 downloadedFile = os.path.join(arcpy.env.scratchFolder, "Data-" + str(uuid.uuid1()) + ".json") with open(downloadedFile, 'wb') as file: downloadCount = 0 while True: chunk = response.read(fileChunk) # If data size is small if ((downloadCount == 0) and (len(chunk) < 1000)): # Log error and end download arcpy.AddError("No data returned, check the URL...") sys.exit() if not chunk: break # Write chunk to output file file.write(chunk) downloadCount = downloadCount + 1 file.close() # If it's the first request if (count == 0): # Create new dataset arcpy.JSONToFeatures_conversion(downloadedFile, os.path.join(arcpy.env.scratchGDB, "Dataset")) else: # Create dataset and load into existing arcpy.JSONToFeatures_conversion(downloadedFile, "in_memory\\DatasetTemp") arcpy.Append_management("in_memory\\DatasetTemp", os.path.join(arcpy.env.scratchGDB, "Dataset"), "NO_TEST", "", "") # If at the final request or if there is only one request that needs to be made if ((int(requestsToMake) == (count+1)) or (requestsToMake == 1)): arcpy.AddMessage("Downloaded and converted JSON for " + str(len(objectIDs)) + " of " + str(len(objectIDs)) + " features...") else: arcpy.AddMessage("Downloaded and converted JSON for " + str((count+1)*maxRecords) + " of " + str(len(objectIDs)) + " features...") count = count + 1 # Convert JSON to feature class arcpy.AddMessage("Copying over final dataset...") # Overwrite dataset if (updateMode.lower() == "new"): # Get record count recordCount = arcpy.GetCount_management(os.path.join(arcpy.env.scratchGDB, "Dataset")) arcpy.AddMessage("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Logging if (enableLogging == "true"): # Log record count logger.info("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Load in data if (recordCount > 0): arcpy.CopyFeatures_management(os.path.join(arcpy.env.scratchGDB, "Dataset"), outputFeatureClass, "", "0", "0", "0") # Delete and append else: # Get record count recordCount = arcpy.GetCount_management(os.path.join(arcpy.env.scratchGDB, "Dataset")) arcpy.AddMessage("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Logging if (enableLogging == "true"): # Log record count logger.info("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Load in data if (recordCount > 0): arcpy.DeleteFeatures_management(outputFeatureClass) arcpy.Append_management(os.path.join(arcpy.env.scratchGDB, "Dataset"), outputFeatureClass, "NO_TEST", "", "") # --------------------------------------- End of code --------------------------------------- # # If called from gp tool return the arcpy parameter if __name__ == '__main__': # Return the output if there is any if output: arcpy.SetParameterAsText(1, output) # Otherwise return the result else: # Return the output if there is any if output: return output # Logging if (enableLogging == "true"): # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] # If arcpy error except arcpy.ExecuteError: # Build and show the error message errorMessage = arcpy.GetMessages(2) arcpy.AddError(errorMessage) # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage) # If python error except Exception as e: errorMessage = "" # Build and show the error message for i in range(len(e.args)): if (i == 0): errorMessage = unicode(e.args[i]).encode('utf-8') else: errorMessage = errorMessage + " " + unicode(e.args[i]).encode('utf-8') arcpy.AddError(errorMessage) # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage) # End of main function # Start of set logging function def setLogging(logFile): # Create a logger
# End of set logging function # Start of send email function def sendEmail(message): # Send an email arcpy.AddMessage("Sending email...") # Server and port information smtpServer = smtplib.SMTP("smtp.gmail.com",587) smtpServer.ehlo() smtpServer.starttls() smtpServer.ehlo # Login with sender email address and password smtpServer.login(emailUser, emailPassword) # Email content header = 'To:' + emailTo + '\n' + 'From: ' + emailUser + '\n' + 'Subject:' + emailSubject + '\n' body = header + '\n' + emailMessage + '\n' + '\n' + message # Send the email and close the connection smtpServer.sendmail(emailUser, emailTo, body) # End of send email function # This test allows the script to be used from the operating # system command prompt (stand-alone), in a Python IDE, # as a geoprocessing script tool, or as a module imported in # another script if __name__ == '__main__': # Arguments are optional - If running from ArcGIS Desktop tool, parameters will be loaded into *argv argv = tuple(arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount())) # Logging if (enableLogging == "true"): # Setup logging logger, logMessage = setLogging(logFile) # Log start of process logger.info("Process started.") # Setup the use of a proxy for requests if (enableProxy == "true"): # Setup the proxy proxy = urllib2.ProxyHandler({requestProtocol : proxyURL}) openURL = urllib2.build_opener(proxy) # Install the proxy urllib2.install_opener(openURL) mainFunction(*argv)
logger = logging.getLogger(os.path.basename(__file__)) logger.setLevel(logging.DEBUG) # Setup log message handler logMessage = logging.FileHandler(logFile) # Setup the log formatting logFormat = logging.Formatter("%(asctime)s: %(levelname)s - %(message)s", "%d/%m/%Y - %H:%M:%S") # Add formatter to log message handler logMessage.setFormatter(logFormat) # Add log message handler to logger logger.addHandler(logMessage) return logger, logMessage
identifier_body
MapServiceDownload.py
#------------------------------------------------------------- # Name: Map Service Download # Purpose: Downloads the data used in a map service layer by querying the json # and converting to a feature class. # Existing Mode - Will delete and append records, so field names need to be the same. # New Mode - Copies data over (including archive datasets if needed). Requires no locks on geodatabase datasets being overwritten. # Author: Shaun Weston (shaun_weston@eagle.co.nz) # Date Created: 14/08/2013 # Last Updated: 24/11/2015 # Copyright: (c) Eagle Technology # ArcGIS Version: 10.3+ # Python Version: 2.7 #-------------------------------- # Import modules import os import sys import logging import smtplib import arcpy import json import urllib import urllib2 import uuid import math # Enable data to be overwritten arcpy.env.overwriteOutput = True # Set global variables enableLogging = "false" # Use logger.info("Example..."), logger.warning("Example..."), logger.error("Example...") logFile = "" # os.path.join(os.path.dirname(__file__), "Example.log") sendErrorEmail = "false" emailTo = "" emailUser = "" emailPassword = "" emailSubject = "" emailMessage = "" enableProxy = "false" requestProtocol = "http" # http or https proxyURL = "" output = None # Start of main function def mainFunction(mapServiceLayer,outputFeatureClass,updateMode): # Get parameters from ArcGIS Desktop tool by seperating by comma e.g. (var1 is 1st parameter,var2 is 2nd parameter,var3 is 3rd parameter) try: # --------------------------------------- Start of code --------------------------------------- # # Querying thet map service to get the count of records arcpy.AddMessage("Querying the map service...") mapServiceQuery1 = mapServiceLayer + "/query?where=1%3D1&returnIdsOnly=true&f=pjson" urlResponse = urllib.urlopen(mapServiceQuery1); # Get json for the response - Object IDs mapServiceQuery1JSONData = json.loads(urlResponse.read()) objectIDs = mapServiceQuery1JSONData["objectIds"] objectIDs.sort() arcpy.AddMessage("Number of records in the layer - " + str(len(objectIDs)) + "...") # Set the number of records per request and the number of requests that need to be made maxRecords = 1000 # If under maxRecords, just need to make one request if (len(objectIDs) < maxRecords): requestsToMake = 1 else: # Calculate the number of requests - Always round up requestsToMake = math.ceil(float(len(objectIDs)) / float(maxRecords)) arcpy.AddMessage("Downloading data to " + arcpy.env.scratchFolder + "...") # For every request count = 0 while (int(requestsToMake) > count): # Create the query startObjectID = int(objectIDs[count*maxRecords]) # If at the final request or if there is only one request that needs to be made if ((int(requestsToMake) == (count+1)) or (requestsToMake == 1)): # Get the last object ID endObjectID = int(objectIDs[len(objectIDs)-1]) serviceQuery = "OBJECTID>%3D" + str(startObjectID) + "+AND+OBJECTID<%3D" + str(endObjectID) else: # Start object ID plus 1000 records endObjectID = int(objectIDs[(count*maxRecords)+maxRecords]) serviceQuery = "OBJECTID>%3D" + str(startObjectID) + "+AND+OBJECTID<" + str(endObjectID) # Query the map service to data in json format try: mapServiceQuery2 = mapServiceLayer + "/query?where=" + serviceQuery + "&returnCountOnly=false&returnIdsOnly=false&returnGeometry=true&outFields=*&f=pjson" response = urllib2.urlopen(mapServiceQuery2) except urllib2.URLError, e: arcpy.AddError("There was an error: %r" % e) # Download the data fileChunk = 16 * 1024 downloadedFile = os.path.join(arcpy.env.scratchFolder, "Data-" + str(uuid.uuid1()) + ".json") with open(downloadedFile, 'wb') as file: downloadCount = 0 while True: chunk = response.read(fileChunk) # If data size is small if ((downloadCount == 0) and (len(chunk) < 1000)): # Log error and end download arcpy.AddError("No data returned, check the URL...") sys.exit() if not chunk: break # Write chunk to output file file.write(chunk) downloadCount = downloadCount + 1 file.close() # If it's the first request if (count == 0): # Create new dataset arcpy.JSONToFeatures_conversion(downloadedFile, os.path.join(arcpy.env.scratchGDB, "Dataset")) else: # Create dataset and load into existing arcpy.JSONToFeatures_conversion(downloadedFile, "in_memory\\DatasetTemp") arcpy.Append_management("in_memory\\DatasetTemp", os.path.join(arcpy.env.scratchGDB, "Dataset"), "NO_TEST", "", "") # If at the final request or if there is only one request that needs to be made if ((int(requestsToMake) == (count+1)) or (requestsToMake == 1)): arcpy.AddMessage("Downloaded and converted JSON for " + str(len(objectIDs)) + " of " + str(len(objectIDs)) + " features...") else: arcpy.AddMessage("Downloaded and converted JSON for " + str((count+1)*maxRecords) + " of " + str(len(objectIDs)) + " features...") count = count + 1 # Convert JSON to feature class arcpy.AddMessage("Copying over final dataset...") # Overwrite dataset if (updateMode.lower() == "new"): # Get record count recordCount = arcpy.GetCount_management(os.path.join(arcpy.env.scratchGDB, "Dataset")) arcpy.AddMessage("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Logging if (enableLogging == "true"): # Log record count logger.info("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Load in data if (recordCount > 0): arcpy.CopyFeatures_management(os.path.join(arcpy.env.scratchGDB, "Dataset"), outputFeatureClass, "", "0", "0", "0") # Delete and append else: # Get record count recordCount = arcpy.GetCount_management(os.path.join(arcpy.env.scratchGDB, "Dataset")) arcpy.AddMessage("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Logging if (enableLogging == "true"): # Log record count logger.info("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Load in data if (recordCount > 0): arcpy.DeleteFeatures_management(outputFeatureClass) arcpy.Append_management(os.path.join(arcpy.env.scratchGDB, "Dataset"), outputFeatureClass, "NO_TEST", "", "") # --------------------------------------- End of code --------------------------------------- # # If called from gp tool return the arcpy parameter if __name__ == '__main__': # Return the output if there is any if output: arcpy.SetParameterAsText(1, output) # Otherwise return the result else: # Return the output if there is any if output: return output # Logging if (enableLogging == "true"): # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] # If arcpy error except arcpy.ExecuteError: # Build and show the error message errorMessage = arcpy.GetMessages(2) arcpy.AddError(errorMessage) # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage) # If python error except Exception as e: errorMessage = "" # Build and show the error message for i in range(len(e.args)): if (i == 0): errorMessage = unicode(e.args[i]).encode('utf-8') else: errorMessage = errorMessage + " " + unicode(e.args[i]).encode('utf-8') arcpy.AddError(errorMessage) # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage) # End of main function # Start of set logging function def setLogging(logFile): # Create a logger logger = logging.getLogger(os.path.basename(__file__)) logger.setLevel(logging.DEBUG) # Setup log message handler logMessage = logging.FileHandler(logFile) # Setup the log formatting logFormat = logging.Formatter("%(asctime)s: %(levelname)s - %(message)s", "%d/%m/%Y - %H:%M:%S") # Add formatter to log message handler logMessage.setFormatter(logFormat) # Add log message handler to logger logger.addHandler(logMessage) return logger, logMessage # End of set logging function # Start of send email function def
(message): # Send an email arcpy.AddMessage("Sending email...") # Server and port information smtpServer = smtplib.SMTP("smtp.gmail.com",587) smtpServer.ehlo() smtpServer.starttls() smtpServer.ehlo # Login with sender email address and password smtpServer.login(emailUser, emailPassword) # Email content header = 'To:' + emailTo + '\n' + 'From: ' + emailUser + '\n' + 'Subject:' + emailSubject + '\n' body = header + '\n' + emailMessage + '\n' + '\n' + message # Send the email and close the connection smtpServer.sendmail(emailUser, emailTo, body) # End of send email function # This test allows the script to be used from the operating # system command prompt (stand-alone), in a Python IDE, # as a geoprocessing script tool, or as a module imported in # another script if __name__ == '__main__': # Arguments are optional - If running from ArcGIS Desktop tool, parameters will be loaded into *argv argv = tuple(arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount())) # Logging if (enableLogging == "true"): # Setup logging logger, logMessage = setLogging(logFile) # Log start of process logger.info("Process started.") # Setup the use of a proxy for requests if (enableProxy == "true"): # Setup the proxy proxy = urllib2.ProxyHandler({requestProtocol : proxyURL}) openURL = urllib2.build_opener(proxy) # Install the proxy urllib2.install_opener(openURL) mainFunction(*argv)
sendEmail
identifier_name
MapServiceDownload.py
#------------------------------------------------------------- # Name: Map Service Download # Purpose: Downloads the data used in a map service layer by querying the json # and converting to a feature class. # Existing Mode - Will delete and append records, so field names need to be the same. # New Mode - Copies data over (including archive datasets if needed). Requires no locks on geodatabase datasets being overwritten. # Author: Shaun Weston (shaun_weston@eagle.co.nz) # Date Created: 14/08/2013 # Last Updated: 24/11/2015 # Copyright: (c) Eagle Technology # ArcGIS Version: 10.3+ # Python Version: 2.7 #-------------------------------- # Import modules import os import sys import logging import smtplib import arcpy import json import urllib import urllib2 import uuid import math # Enable data to be overwritten arcpy.env.overwriteOutput = True # Set global variables enableLogging = "false" # Use logger.info("Example..."), logger.warning("Example..."), logger.error("Example...") logFile = "" # os.path.join(os.path.dirname(__file__), "Example.log") sendErrorEmail = "false" emailTo = "" emailUser = "" emailPassword = "" emailSubject = "" emailMessage = "" enableProxy = "false" requestProtocol = "http" # http or https proxyURL = "" output = None # Start of main function def mainFunction(mapServiceLayer,outputFeatureClass,updateMode): # Get parameters from ArcGIS Desktop tool by seperating by comma e.g. (var1 is 1st parameter,var2 is 2nd parameter,var3 is 3rd parameter) try: # --------------------------------------- Start of code --------------------------------------- # # Querying thet map service to get the count of records arcpy.AddMessage("Querying the map service...") mapServiceQuery1 = mapServiceLayer + "/query?where=1%3D1&returnIdsOnly=true&f=pjson" urlResponse = urllib.urlopen(mapServiceQuery1); # Get json for the response - Object IDs mapServiceQuery1JSONData = json.loads(urlResponse.read()) objectIDs = mapServiceQuery1JSONData["objectIds"] objectIDs.sort() arcpy.AddMessage("Number of records in the layer - " + str(len(objectIDs)) + "...") # Set the number of records per request and the number of requests that need to be made maxRecords = 1000 # If under maxRecords, just need to make one request if (len(objectIDs) < maxRecords): requestsToMake = 1 else: # Calculate the number of requests - Always round up requestsToMake = math.ceil(float(len(objectIDs)) / float(maxRecords)) arcpy.AddMessage("Downloading data to " + arcpy.env.scratchFolder + "...") # For every request count = 0 while (int(requestsToMake) > count): # Create the query startObjectID = int(objectIDs[count*maxRecords]) # If at the final request or if there is only one request that needs to be made if ((int(requestsToMake) == (count+1)) or (requestsToMake == 1)): # Get the last object ID endObjectID = int(objectIDs[len(objectIDs)-1]) serviceQuery = "OBJECTID>%3D" + str(startObjectID) + "+AND+OBJECTID<%3D" + str(endObjectID) else: # Start object ID plus 1000 records endObjectID = int(objectIDs[(count*maxRecords)+maxRecords]) serviceQuery = "OBJECTID>%3D" + str(startObjectID) + "+AND+OBJECTID<" + str(endObjectID) # Query the map service to data in json format try: mapServiceQuery2 = mapServiceLayer + "/query?where=" + serviceQuery + "&returnCountOnly=false&returnIdsOnly=false&returnGeometry=true&outFields=*&f=pjson" response = urllib2.urlopen(mapServiceQuery2) except urllib2.URLError, e: arcpy.AddError("There was an error: %r" % e) # Download the data fileChunk = 16 * 1024 downloadedFile = os.path.join(arcpy.env.scratchFolder, "Data-" + str(uuid.uuid1()) + ".json") with open(downloadedFile, 'wb') as file: downloadCount = 0 while True: chunk = response.read(fileChunk) # If data size is small if ((downloadCount == 0) and (len(chunk) < 1000)): # Log error and end download arcpy.AddError("No data returned, check the URL...") sys.exit() if not chunk:
# Write chunk to output file file.write(chunk) downloadCount = downloadCount + 1 file.close() # If it's the first request if (count == 0): # Create new dataset arcpy.JSONToFeatures_conversion(downloadedFile, os.path.join(arcpy.env.scratchGDB, "Dataset")) else: # Create dataset and load into existing arcpy.JSONToFeatures_conversion(downloadedFile, "in_memory\\DatasetTemp") arcpy.Append_management("in_memory\\DatasetTemp", os.path.join(arcpy.env.scratchGDB, "Dataset"), "NO_TEST", "", "") # If at the final request or if there is only one request that needs to be made if ((int(requestsToMake) == (count+1)) or (requestsToMake == 1)): arcpy.AddMessage("Downloaded and converted JSON for " + str(len(objectIDs)) + " of " + str(len(objectIDs)) + " features...") else: arcpy.AddMessage("Downloaded and converted JSON for " + str((count+1)*maxRecords) + " of " + str(len(objectIDs)) + " features...") count = count + 1 # Convert JSON to feature class arcpy.AddMessage("Copying over final dataset...") # Overwrite dataset if (updateMode.lower() == "new"): # Get record count recordCount = arcpy.GetCount_management(os.path.join(arcpy.env.scratchGDB, "Dataset")) arcpy.AddMessage("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Logging if (enableLogging == "true"): # Log record count logger.info("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Load in data if (recordCount > 0): arcpy.CopyFeatures_management(os.path.join(arcpy.env.scratchGDB, "Dataset"), outputFeatureClass, "", "0", "0", "0") # Delete and append else: # Get record count recordCount = arcpy.GetCount_management(os.path.join(arcpy.env.scratchGDB, "Dataset")) arcpy.AddMessage("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Logging if (enableLogging == "true"): # Log record count logger.info("Number of records for " + outputFeatureClass + " - " + str(recordCount)) # Load in data if (recordCount > 0): arcpy.DeleteFeatures_management(outputFeatureClass) arcpy.Append_management(os.path.join(arcpy.env.scratchGDB, "Dataset"), outputFeatureClass, "NO_TEST", "", "") # --------------------------------------- End of code --------------------------------------- # # If called from gp tool return the arcpy parameter if __name__ == '__main__': # Return the output if there is any if output: arcpy.SetParameterAsText(1, output) # Otherwise return the result else: # Return the output if there is any if output: return output # Logging if (enableLogging == "true"): # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] # If arcpy error except arcpy.ExecuteError: # Build and show the error message errorMessage = arcpy.GetMessages(2) arcpy.AddError(errorMessage) # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage) # If python error except Exception as e: errorMessage = "" # Build and show the error message for i in range(len(e.args)): if (i == 0): errorMessage = unicode(e.args[i]).encode('utf-8') else: errorMessage = errorMessage + " " + unicode(e.args[i]).encode('utf-8') arcpy.AddError(errorMessage) # Logging if (enableLogging == "true"): # Log error logger.error(errorMessage) # Log end of process logger.info("Process ended.") # Remove file handler and close log file logMessage.flush() logMessage.close() logger.handlers = [] if (sendErrorEmail == "true"): # Send email sendEmail(errorMessage) # End of main function # Start of set logging function def setLogging(logFile): # Create a logger logger = logging.getLogger(os.path.basename(__file__)) logger.setLevel(logging.DEBUG) # Setup log message handler logMessage = logging.FileHandler(logFile) # Setup the log formatting logFormat = logging.Formatter("%(asctime)s: %(levelname)s - %(message)s", "%d/%m/%Y - %H:%M:%S") # Add formatter to log message handler logMessage.setFormatter(logFormat) # Add log message handler to logger logger.addHandler(logMessage) return logger, logMessage # End of set logging function # Start of send email function def sendEmail(message): # Send an email arcpy.AddMessage("Sending email...") # Server and port information smtpServer = smtplib.SMTP("smtp.gmail.com",587) smtpServer.ehlo() smtpServer.starttls() smtpServer.ehlo # Login with sender email address and password smtpServer.login(emailUser, emailPassword) # Email content header = 'To:' + emailTo + '\n' + 'From: ' + emailUser + '\n' + 'Subject:' + emailSubject + '\n' body = header + '\n' + emailMessage + '\n' + '\n' + message # Send the email and close the connection smtpServer.sendmail(emailUser, emailTo, body) # End of send email function # This test allows the script to be used from the operating # system command prompt (stand-alone), in a Python IDE, # as a geoprocessing script tool, or as a module imported in # another script if __name__ == '__main__': # Arguments are optional - If running from ArcGIS Desktop tool, parameters will be loaded into *argv argv = tuple(arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount())) # Logging if (enableLogging == "true"): # Setup logging logger, logMessage = setLogging(logFile) # Log start of process logger.info("Process started.") # Setup the use of a proxy for requests if (enableProxy == "true"): # Setup the proxy proxy = urllib2.ProxyHandler({requestProtocol : proxyURL}) openURL = urllib2.build_opener(proxy) # Install the proxy urllib2.install_opener(openURL) mainFunction(*argv)
break
conditional_block
GatewayToAdventure.py
import argparse from time import sleep as s class bcolors: """Base class to provide color to user prompts and texts.""" CYAN = '\033[96m' GREEN = '\033[92m' WHITE = '\033[37m' PINK = '\033[35m' YELLOW = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' BLUE = '\033[94m' class Quiz: """Base class to store dictionary of questions and answers for the game quizzes""" def __init__(self, qNADict={"Ques": "Ans"}): self.questionAndAnswer = qNADict class QualifyingQuiz(Quiz): """Child class to Quiz class that stores dictionary of questions and answers for the qualifying quiz""" def __init__(self, qNADict={"Ques1": "Ans1"}): self.questionAndAnswer = qNADict class SurvivalQuiz(Quiz): """Child class to Quiz class that stores dictionary of questions and answers for the Survival quizzes""" def __init__(self, qNADict={"Ques2": "Ans2"}): self.questionAndAnswer = qNADict class GameWorld: """Base class for creating different worlds in the game""" def __init__(self, name='gameWorld', typeW='all'): self.worldName = name self.worldType = typeW self.essentials = [] self.qualifyingQuiz = QualifyingQuiz() self.survivalQuiz1 = SurvivalQuiz() self.survivalQuiz2 = SurvivalQuiz() def qualifyingQuizMethod(self, questionAndAnswer): """Returns number of correct answers for Qualifying quiz. Compares User response with the desired answer from the existing dictionary""" questionsRight = 0 for question in questionAndAnswer: answer = input(f"{bcolors.CYAN} {question[0]}\n: {bcolors.ENDC}") if answer == question[1]: questionsRight += 1 elif answer.capitalize() == "Quit": # User wants to Quit the Game now print(f"User wants to quit the game now") questionsRight = "Quit" break return questionsRight def survivalQuizMethod(self, questionAndAnswer): """Returns 1 if User correctly answers all the question of the Survival quiz, else 0. Compares User response with the desired answer from the existing dictionary""" questionsRight = 1 for question in questionAndAnswer: answer = input(f"\n{bcolors.CYAN}{question[0]}\n:{bcolors.ENDC}") if answer == question[1]: questionsRight *= 1 elif answer != question[1]: questionsRight *= 0 elif answer.capitalize() == "Quit": # User wants to Quit the Game now print(f"User wants to quit the game now") questionsRight = "Quit" break return questionsRight def getEssentialList(self): """Returns list of essentials for a particular world""" return self.essentials def __str__(self): return self.worldName class Nixoterra(GameWorld): """Child class for Snow World - Nixoterra""" #name = "Nixoterra" qNADictQ = { "Q1: What is the freezing point of snow?\n a. 0 degrees celcius\n b. 30 degrees celcius\n c. 100 degrees celcius\n d. -5 degrees celcius": "d", "Q2: Which ball do we use in the snow ball activity at snow world?\n a. Leather Ball\n b. Foot Ball\n c. Snow Ball\n d. All of the above": "d", "Q3: Which cartoon character does snow world have?\n a. Penguin\n b. Olaf\n c. Snowman\n d. All of the above": "d" } qNADictS1 = { "Q1: Describe a typical workweek for a snowboard instructor\n a. Sleep all week long\n b. Play with Ice\n c. Instruct and provide training to groups/individuals": "c", "Q2: what's your level of experience with edging, waxing and mounting skis and snowboard?\n a. Novice\n b. Proficient\n c. expert": "c" } qNADictS2 = { "Q1: What equipment is/are required for working as an experienced snowmaker\n a. Snow guns\n b. hoses\n c. hydrants\n d. All of the above": "d", "Q2: Can you safely lift 50 lbs\n a. Yes\n b. No": "a" } def __init__(self): self.worldName = "Nixoterra" self.worldType = "SnowWorld" self.essentials = ["Mittens", "Snowboots", "Ski Goggles", "Wool Socks", "Hand warmer"] self.story = "Ice, Chill, Snow, Cold, Freezerealm, Glacia. You name it. \nThey are all names for the mystic Nixoterra. Allow me, Nixette, to guide you through this world. This land is the mother of all snow and ice. Explore this world carefully, because the temperatures are- well, piercing. But other than that, just chill." #ToDo: Fill real name of capital city self.qualifyingQuiz = QualifyingQuiz(Nixoterra.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Nixoterra.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Nixoterra.qNADictS2) class Silvia(GameWorld): """Child class for Forest World - Silvia""" #name = "Silvia" qNADictQ = { "Q1: Can you use a GPS device effectively?\n a. Yes\n b. No": "a", "Q2: Which of the following statement is true?\n a. Forest is on land\n b. Forest is under water": "a", "Q3: What NOT to do in a Forest world?\n a. Start fire\n b. Climb on Tree\n c. Enjoy Nature": "a" } qNADictS1 = { "Q1: Name one of the heavy equipments used in the forest operations\n a. Dump trucks\n b. Toycar\n c. Teddybear": "a", "Q2: Which is NOT one of the 4 forest types?\n a. Temperate\n b. Tropical\n c. Subtropical\n d. Arboreal" : "d" } qNADictS2 = { "Q1: Forest soil is a natural water filter\n a. True\n b. False": "a", "Q2: What is the definition of a forest?\n a. A public green area in a town, used for recreation.\n b. An area rich in biodiversity\n c. A large area of land densely populated by trees\n d. A flat area covered in plants" : "c" } def __init__(self): self.worldName = "Silvia" self.worldType = "ForestWorld" self.essentials = ["Mosquito Repellent", "Backpack", "Poncho", "Hiking boots", "Trail shoes"] self.story = "Hoy there! I'm Bryn, and I will be your personal guide through this wonderful world of trees and plants. \nWelcome to the forest, young explorer! These trees are sacred creatures who descended from the heavens. Enjoy your time in this wonderful place that you can call home" self.qualifyingQuiz = QualifyingQuiz(Silvia.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Silvia.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Silvia.qNADictS2) class Aquamundi(GameWorld): """Child class for Water World - Aquamundi""" #name = "Aquamundi" qNADictQ = { "Q1: Spell Water\n a. Water\n b. Waiter": "a", "Q2: Is water same as wind?\n a. Yes\n b. No": "b", "Q3: Do you know how to swim?\n a. Yes\n b. No": "a" } qNADictS1 = { "Q1: Why do you want to be a Dive instructor?\n a. I like the word Instructor\n b. I love teaching diving": "b", "Q2: Do you have a Captain's license?\n a. Yes\n b. No": "a" } qNADictS2 = { "Q1: In case of any emergency call, what will be your first step?\n a. Ignore\n b. Immediately act on it": "b", "Q2: What is a large natural stream of flowing water that ends in a sea?\n a. Lake\n b. Ocean\n c. Glacier\n d. River": "d" } def __init__(self): self.worldName = "Aquamundi" self.worldType = "WaterWorld" self.essentials = ["Swimcap", "Bandana", "Float suit", "Rash guard", "Alternate Air Source"] self.story = "I see, you chose the water world. Great choice! I'm Aquanna, and I will be your guide in Aquamundi. If you are wondering when this endless ocean ends, don't waste your time. \nThis is a huge world, filled with nothing but water. Let's hop on our big boat and start this adventure!" self.qualifyingQuiz = QualifyingQuiz(Aquamundi.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Aquamundi.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Aquamundi.qNADictS2) class Montelocus(GameWorld): """Child class for Mountain World - Montelocus""" #name = "Montelocus" qNADictQ = { "Q1: Do you know how to use mountain gear?\n a. Yes\n b. No": "a", "Q2: Are you a wayfinder or a lost sheep?\n a. Wayfinder\n b. Lostsheep": "a", "Q3: Are you afraid of heights?\n a. Yes\n b. No": "b" } qNADictS1 = { "Q1: What would you select to climb in tricky mountain conditions?\n a. Mountaineering boots\n b. mountains": "a", "Q2: What would you do if someone calls for help?\n a. It's their problem, I will just ignore\n b. Go and help them": "b" } qNADictS2 = { "Q1: Which of the following is the highest part of a mountain?\n a. Peak\n b. Base\n c. Slope\n d. None of the above": "a", "Q2: What should you do if you fall off a mountain during Climbing?\n a. Have a quick snack\n b. Make a quick phone call\n c. Take a selfie and post it on instagram\n d. Use a harness": "d" } def __init__(self): self.worldName = "Montelocus" self.worldType = "MountainWorld" self.essentials = ["Climbing Helmet", "Ropes", "Harness", "Mountaineering Boots", "Crampons"] self.story = "Isn't it beautiful, the divine mountain range of Montelocus. Oh hey! I didn't see you there! I'm Cashel, and I will lead you through the wonders of this rocky world, with mountains everywhere. \nThese mountains are said to be a gift from the sky. Take a moment to appreciate this particularly beautiful aspect of nature! \nAnyway, see you around!" self.qualifyingQuiz = QualifyingQuiz(Montelocus.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Montelocus.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Montelocus.qNADictS2) class User: """Class for storing user information and tracking user activity throughout multiple stages of the game""" def __init__(self, name, location, system, coins=100): self.name = name self.location = location # not sure if we need it self.system = system self.coins = coins self.gems = 0 self.assets = [] def __str__(self): return self.name def collectGem(self): """Increments gem count of user by 1, if the user earns gem""" self.gems += 1 def buyGem(self, amount): """Increments gem count of user by 1 if the buys the gem by spending coins""" returnVal = False if self.spendCoin(amount=25): self.gems += 1 returnVal = True return returnVal def earnCoin(self, amount): """Increments count of coins by a certain amount""" self.coins += amount def spendCoin(self, amount): """Decreases count of coins by a certain amount""" returnVal = False if self.coins >= amount: self.coins -= amount returnVal = True return returnVal def getCoins(self): """Returns number of coins of the user""" return self.coins def getGems(self): """Returns number of gems of the user""" return self.gems def buyEssentials(self, amount=5): """Returns list of essential items bought by the user for the selected world and displays the list of user's assets.""" ls = self.location.getEssentialList() print(f"{bcolors.WHITE}\nGreat job so far! Now, it's time to arm yourself with some essentials you would need to survive in the {self.location.worldType}.{bcolors.ENDC}") print(f"\n{bcolors.CYAN}Following are the essential items for {self.location.worldName}. Please choose a minimum of 3 items to proceed.{bcolors.ENDC}") outputList = [str(i+1) + ". " + ls[i] + "\n" for i in range(len(ls))] print(f"\n{bcolors.CYAN}{''.join(outputList)}{bcolors.ENDC}") sizeEssentialList = len(ls) essentialsList = [] choiceInput = False while choiceInput is False: choices = input(f"{bcolors.CYAN}Input your selection as numbers 1, 2, 3, 4, or 5 separated by comma: {bcolors.ENDC}") choiceInput = True choices = choices.split(',') for choice in choices: if choice not in ('1', '2', '3', '4', '5', 'quit', 'QUIT', 'Quit'): print(f"\n{bcolors.PINK}Please enter a valid Input{bcolors.ENDC}\n") choiceInput = False break for choice in choices: if choice.capitalize() == "Quit": # User input "Quit" at this stage. So, just quit the game. return choices try: # Convert input to integer for index in essentialList item choices = [int(i) for i in choices] except ValueError: # If input is not a number, Quit gracefully! print("Input is not a number. Quit") return essentialsList if max(choices) > sizeEssentialList: print(f"Invalid input! Input is not in essentialList") return essentialsList for j in choices: if self.spendCoin(amount): essentialsList.append(ls[j-1]) else: print(f"You don't have enough money to buy {j}. You only have {self.coins} coins left.") break self.assets = essentialsList print(f"\n{bcolors.WHITE}Thank you for buying the essentials. Now you are officially ready to enter into the {self.location.worldType}.\nHere is your current asset bag with essential items and the available coins.{bcolors.ENDC}") print(f"\n{bcolors.YELLOW}Asset Bag Contents: {self.assets}\nCoins: {self.coins}{bcolors.ENDC}") return self.assets def getAssets(self): """Returns list of assets of the user""" return self.assets def takeSurvivalQuiz(self, quiz): """Returns the result of survival quiz""" if quiz == 'survivalQuiz1': print(f"\n{bcolors.WHITE}Starting Survival quiz1 for {self.location.worldName} in 3 2 1....{bcolors.ENDC}") s(2) print(f"\n{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd': {bcolors.ENDC}") userSurvival = [(i, self.location.survivalQuiz1.questionAndAnswer.get(i)) for i in self.location.survivalQuiz1.questionAndAnswer.keys()] elif quiz == 'survivalQuiz2': print(f"\n{bcolors.WHITE}Starting Survival quiz2 for {self.location.worldName} in 3 2 1....{bcolors.ENDC}") s(2) #print(f"\n{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd': {bcolors.ENDC}") userSurvival = [(i, self.location.survivalQuiz2.questionAndAnswer.get(i)) for i in self.location.survivalQuiz2.questionAndAnswer.keys()] else: # Wrong argument, should not have reached here. Return anyway! userSurvival = "Quit" survivalResults = self.location.survivalQuizMethod(userSurvival) return survivalResults def navigateToCapitalcity(self, amount=20): """Increments number if gems by 1 if the user succesfully navigates to the capital city""" navSuccess = False if self.spendCoin(amount): self.gems += 1 navSuccess = True return navSuccess class GameSystem: """This is the game engine where other classes are ingested. This class controls the whole game system and stores system information""" def __init__(self): self.coins = 100 self.name = "Gateway To Adventure" self.minCorrectAnswers = 2 self.minGemsWin = 4 self.minCoinsWin = 25 self.minGemForCapitalCity = 2 self.buyGemAmount = 25 self.minEssentials = 3 self.maxAssetNum = 5 self.survivalWinCoins = 10 self.userName = "Empty" def __str__(self): return self.name def getUserLocation(self, userName, worldList): """Returns user location after User selects a world""" validInput = False itemizedWorldList = [ "\n" + str(i+1) + ". " + worldList[i] for i in range(len(worldList))] while validInput is False: print(f"{bcolors.CYAN}Choose a world from the following: {bcolors.BOLD}{''.join(itemizedWorldList)}{bcolors.ENDC}") userLocation = input(f"{bcolors.CYAN}Please input the exact name of the world: {bcolors.ENDC}") userLocation = userLocation.capitalize() if userLocation in worldList: validInput = True else: if userLocation == "Quit": validInput = True else: print(f"{bcolors.FAIL}Invalid world name! Please enter a correct World Name {bcolors.ENDC}") validInput = False return userLocation def selectWorld(self, userLocation, userName, availableWorldList): """Returns world type and the result of qualifying quiz, after User selects the world""" gameReturn = False if userLocation in availableWorldList: if userLocation == "Nixoterra": world = Nixoterra() elif userLocation == "Silvia": world = Silvia() elif userLocation == "Aquamundi": world = Aquamundi() elif userLocation == "Montelocus": world = Montelocus() else: # Should not have reached here! print(f"{bcolors.FAIL}Quitting as {userName} wants to quit the game.{bcolors.ENDC}") return gameReturn print(f"{bcolors.WHITE}Thank you for choosing the {world.worldType}.\n\n-------------\n{world.story}\n-------------\nNow, Let's see if you are ready for the selected world :-)\n\nStarting Qualifying quiz for {userLocation} in 3 2 1...{bcolors.ENDC}\n") s(2) print(f"{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd'{bcolors.ENDC}") userQualifying = [(i, world.qualifyingQuiz.questionAndAnswer.get(i)) for i in world.qualifyingQuiz.questionAndAnswer.keys()] qualifyingResults = world.qualifyingQuizMethod(userQualifying) if self.checkReturn(qualifyingResults, userName): return gameReturn else: print(f"{bcolors.FAIL}You have already failed in the Qualifying quiz of {userLocation}. You can't reattempt thw Qualifyiong quiz.{bcolors.ENDC}") return world, qualifyingResults def checkReturn(self, returnVal, name): """Returns True if user enters 'quit' at any stage of the game""" quitGame = False if str(returnVal).capitalize() == "Quit": print(f"{bcolors.FAIL}Quitting as {name} wants to quit the game.{bcolors.ENDC}") # User entered Quit, exit and return accordingly quitGame = True return quitGame def evaluateSurvialQuiz(self, player): """Evaluates and displays result of survival quiz and also displays the user's current asset""" print(f"\n{bcolors.WHITE}Let's start earning some more coins by answering survival questions that will help you survive in the world{bcolors.ENDC}") quizList = ['survivalQuiz1', 'survivalQuiz2'] perkReattempt = True for quiz in quizList: survivalResults = player.takeSurvivalQuiz(quiz) if self.checkReturn(survivalResults, player.name): return False if not survivalResults:
else: player.earnCoin(self.survivalWinCoins * survivalResults) player.collectGem() print(f"{bcolors.YELLOW}Congrats {player.name}, you have cleared {quiz}{bcolors.ENDC}") print(f"{bcolors.YELLOW}current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}{bcolors.ENDC}") return True def gameStart(self): """Returns true if the user successfully completes the game and handles invalid user input. Also, starts the game and create instances of user and world""" print(f"{bcolors.WHITE}Hello, and welcome to the {bcolors.BOLD}Gateway of Adventure!{bcolors.ENDC} {bcolors.WHITE} Let's begin the fun ride!\nYou can quit the game at any stage of the game by typing Quit. Good Luck!{bcolors.ENDC}") gameReturn = False worldList = ["Nixoterra", "Silvia", "Aquamundi", "Montelocus"] userInput = input(f"Would you like to view the instruction manual? (y/n): ") while userInput.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") userInput = input(f"Would you like to view the instruction manual? (y/n): ") if userInput.lower() == "y": print(f"\n{bcolors.WHITE}Open this link to view the Instruction Manual:\n{bcolors.BLUE}{bcolors.UNDERLINE}https://github.prod.oc.2u.com/UCB-INFO-PYTHON/Jyoti_KumariREPO/blob/master/SUBMISSIONS/project_1/Gateway%20to%20Adventure%20-%20%20Instruction%20Manual.pdf{bcolors.ENDC}") elif userInput.lower() == "quit": self.checkReturn(userInput, "guest") return gameReturn if game.userName == "Empty": userName = input(f"\n{bcolors.CYAN}What is your name? {bcolors.ENDC}") self.userName = userName else: userName = self.userName #print(f"{bcolors.FAIL}You can type Quit to quit at any stage of the game. {bcolors.ENDC}") userLocation = self.getUserLocation(userName, worldList) if not userLocation: return gameReturn #userLocation = userLocation.lower() if self.checkReturn(userLocation, userName): return gameReturn # Valid User Location found # Let's select the world and do the Qualifying quiz for the world loopReturn = False while True: world, qualifyingResults = self.selectWorld(userLocation, userName, worldList) if not world: loopReturn = False break print(f"\n{bcolors.YELLOW}{userName}, you got {qualifyingResults} questions right.{bcolors.ENDC}\n") if qualifyingResults < self.minCorrectAnswers: # User failed the qualifying quiz print(f"{bcolors.FAIL}You failed in Qualifying quiz of {userLocation} {bcolors.ENDC}") worldList.remove(userLocation) if len(worldList): print(f"{bcolors.WHITE}\nTry selecting another world to proceed. You can type Quit to quit at any stage of the game.{bcolors.ENDC}\n") userLocation = self.getUserLocation(userName, worldList) if not userLocation: loopReturn = False break if self.checkReturn(userLocation, userName): loopReturn = False break else: loopReturn = False break else: loopReturn = True print(f"{bcolors.YELLOW}Congratulations! you now qualify to enter {userLocation}\n\n{world.story}\n\nYou have just won 100 coins!{bcolors.ENDC}") break if not loopReturn: return gameReturn # User Qualified the quiz for the world # Let's initialize Classes for World and player player = User(userName, world, self) #print(f"worldName: {world.worldName}\nworldType: {world.worldType}\n") #print(f"Player Name: {player.name}\nPlayer Location: {player.location}\nPlayer Coins: {player.getCoins()}\n" ) essentialsList = player.buyEssentials() if not essentialsList: # No item selected from the essential list # Player can't survive in the world and Exit the world with message print(f"{bcolors.FAIL}Quitting as {userName} didn't select any essential items to enter the new world.{bcolors.ENDC}") return gameReturn if self.checkReturn(essentialsList, userName): return gameReturn #print(f"{userName} Assets: ", player.getAssets()) if len(player.getAssets()) < self.minEssentials: print(f"{bcolors.FAIL}{userName} needs to select at least {self.minEssentials} from the list.{bcolors.ENDC}") return gameReturn userInput = input(f"\n{bcolors.WHITE}{userName}, you have successfully entered into the {world.worldType}. As a next step, you would need to buy a gem for 25 coins to further explore this world.\nDo you want to proceed (y/n)?: {bcolors.ENDC}") while userInput.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") userInput = input(f"\n{bcolors.WHITE}{userName}, you have successfully entered into the {world.worldType}. As a next step, you would need to buy a gem for 25 coins to further explore this world.\nDo you want to proceed (y/n)?: {bcolors.ENDC}") if userInput.lower() == 'y': if player.buyGem(self.buyGemAmount): print(f"{bcolors.YELLOW}Thank you {userName}, You now have {player.getGems()} gems and {player.getCoins()} coins{bcolors.ENDC}") else: self.checkReturn("quit", userName) return gameReturn s(2) # Conduct and Evaluate Survial Quiz for player if not self.evaluateSurvialQuiz(player): return gameReturn # Navigate to Capital City if player.getGems() >= self.minGemForCapitalCity: print(f"{bcolors.WHITE}\nYay! you are a rockstar! Let's proceed to the next stage of the your journey by going to the very beautiful Capital City of {world.worldName}.{bcolors.ENDC}") s(2) print(f"{bcolors.WHITE}\nEntering into the Capital City 3 2 1....{bcolors.ENDC}\n") s(2) if len(player.getAssets()) == self.maxAssetNum : player.navigateToCapitalcity(10) else: player.navigateToCapitalcity() print(f"{bcolors.YELLOW}{userName} has reached the capital city of {userLocation}{bcolors.ENDC}") print(f"{bcolors.YELLOW}current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}{bcolors.ENDC}") response = input(f"\n{bcolors.WHITE}Do you want to buy addditional Gem for 25 coins? (y/n) {bcolors.ENDC}").lower() while response.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") response = input(f"\n{bcolors.WHITE}Do you want to buy addditional Gem for 25 coins? (y/n) {bcolors.ENDC}").lower() if response == 'y': player.buyGem(25) print(f"user bought one gem") elif response == 'n': print(f"{bcolors.WHITE}{userName} declined offer{bcolors.ENDC}\n") else: self.checkReturn("quit", userName) return gameReturn # Check if Player has enough gems to win the game if player.getGems() >= self.minGemsWin: gameReturn = True # Check if Player has enough coins to win the game if player.getCoins() >= self.minCoinsWin: print(f"{bcolors.GREEN}{userName} has {player.getCoins()} coins and {player.getGems()} gems!") print(f"{bcolors.GREEN}Congratulations {userName}, you Won!{bcolors.ENDC}") else: print(f"{bcolors.PINK}Sorry {userName}, you lost.") print(f"{userName} has {player.getCoins()} coins left") print(f"You needed {self.minCoinsWin} coins to win the game!{bcolors.ENDC}") else: print(f"{bcolors.PINK}Sorry {userName}, you lost.") print(f"{userName} has {player.getCoins()} coins left") print(f"You needed {self.minGemsWin} gems to win the game!{bcolors.ENDC}") gameReturn = True return gameReturn def gameQuit(self): "Displays Goodbye message if the user has selected to quit the game" print(f"{bcolors.PINK}Goodbye {self.userName}, see you again!{bcolors.ENDC}") parser = argparse.ArgumentParser(description = 'Step 1: Select a world --> Step 2: Take Qualifying quiz --> Step 3: Buy essentials(a minimum of 3 items) --> Step 4: Enter World and take 2 survival quizzes, one by one --> Step 5: Go to Capital City to get a gem or buy a gem --> Step 6: You win or lose depending on, if you have 25 coins and 4 gems at a minimum') args = parser.parse_args() game = GameSystem() gameRestart = False gameRestart = game.gameStart() while gameRestart: gameRestart = input(f"\nDo you want to replay the Game!? (y/n): ") if gameRestart.lower() == 'y': gameRestart = game.gameStart() else: gameRestart = False game.gameQuit()
print(f"{bcolors.FAIL}Sorry, you failed.{bcolors.ENDC}") if len(player.getAssets()) > self.minEssentials and perkReattempt: print(f"{bcolors.WHITE}You are eligible for 1 reattempt.{bcolors.ENDC}") perkReattempt = False survivalResults = player.takeSurvivalQuiz(quiz) if self.checkReturn(survivalResults, player.name): return False if not survivalResults: print(f"{bcolors.FAIL}Sorry, you failed again.{bcolors.ENDC}") else: player.earnCoin(self.survivalWinCoins * survivalResults) player.collectGem() print(f"{bcolors.YELLOW}Congrats! {player.name}, you have cleared {quiz}{bcolors.ENDC}") #print(f"current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}")
conditional_block
GatewayToAdventure.py
import argparse from time import sleep as s class bcolors: """Base class to provide color to user prompts and texts.""" CYAN = '\033[96m' GREEN = '\033[92m' WHITE = '\033[37m' PINK = '\033[35m' YELLOW = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' BLUE = '\033[94m' class Quiz: """Base class to store dictionary of questions and answers for the game quizzes""" def __init__(self, qNADict={"Ques": "Ans"}): self.questionAndAnswer = qNADict class QualifyingQuiz(Quiz): """Child class to Quiz class that stores dictionary of questions and answers for the qualifying quiz""" def __init__(self, qNADict={"Ques1": "Ans1"}): self.questionAndAnswer = qNADict class SurvivalQuiz(Quiz):
class GameWorld: """Base class for creating different worlds in the game""" def __init__(self, name='gameWorld', typeW='all'): self.worldName = name self.worldType = typeW self.essentials = [] self.qualifyingQuiz = QualifyingQuiz() self.survivalQuiz1 = SurvivalQuiz() self.survivalQuiz2 = SurvivalQuiz() def qualifyingQuizMethod(self, questionAndAnswer): """Returns number of correct answers for Qualifying quiz. Compares User response with the desired answer from the existing dictionary""" questionsRight = 0 for question in questionAndAnswer: answer = input(f"{bcolors.CYAN} {question[0]}\n: {bcolors.ENDC}") if answer == question[1]: questionsRight += 1 elif answer.capitalize() == "Quit": # User wants to Quit the Game now print(f"User wants to quit the game now") questionsRight = "Quit" break return questionsRight def survivalQuizMethod(self, questionAndAnswer): """Returns 1 if User correctly answers all the question of the Survival quiz, else 0. Compares User response with the desired answer from the existing dictionary""" questionsRight = 1 for question in questionAndAnswer: answer = input(f"\n{bcolors.CYAN}{question[0]}\n:{bcolors.ENDC}") if answer == question[1]: questionsRight *= 1 elif answer != question[1]: questionsRight *= 0 elif answer.capitalize() == "Quit": # User wants to Quit the Game now print(f"User wants to quit the game now") questionsRight = "Quit" break return questionsRight def getEssentialList(self): """Returns list of essentials for a particular world""" return self.essentials def __str__(self): return self.worldName class Nixoterra(GameWorld): """Child class for Snow World - Nixoterra""" #name = "Nixoterra" qNADictQ = { "Q1: What is the freezing point of snow?\n a. 0 degrees celcius\n b. 30 degrees celcius\n c. 100 degrees celcius\n d. -5 degrees celcius": "d", "Q2: Which ball do we use in the snow ball activity at snow world?\n a. Leather Ball\n b. Foot Ball\n c. Snow Ball\n d. All of the above": "d", "Q3: Which cartoon character does snow world have?\n a. Penguin\n b. Olaf\n c. Snowman\n d. All of the above": "d" } qNADictS1 = { "Q1: Describe a typical workweek for a snowboard instructor\n a. Sleep all week long\n b. Play with Ice\n c. Instruct and provide training to groups/individuals": "c", "Q2: what's your level of experience with edging, waxing and mounting skis and snowboard?\n a. Novice\n b. Proficient\n c. expert": "c" } qNADictS2 = { "Q1: What equipment is/are required for working as an experienced snowmaker\n a. Snow guns\n b. hoses\n c. hydrants\n d. All of the above": "d", "Q2: Can you safely lift 50 lbs\n a. Yes\n b. No": "a" } def __init__(self): self.worldName = "Nixoterra" self.worldType = "SnowWorld" self.essentials = ["Mittens", "Snowboots", "Ski Goggles", "Wool Socks", "Hand warmer"] self.story = "Ice, Chill, Snow, Cold, Freezerealm, Glacia. You name it. \nThey are all names for the mystic Nixoterra. Allow me, Nixette, to guide you through this world. This land is the mother of all snow and ice. Explore this world carefully, because the temperatures are- well, piercing. But other than that, just chill." #ToDo: Fill real name of capital city self.qualifyingQuiz = QualifyingQuiz(Nixoterra.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Nixoterra.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Nixoterra.qNADictS2) class Silvia(GameWorld): """Child class for Forest World - Silvia""" #name = "Silvia" qNADictQ = { "Q1: Can you use a GPS device effectively?\n a. Yes\n b. No": "a", "Q2: Which of the following statement is true?\n a. Forest is on land\n b. Forest is under water": "a", "Q3: What NOT to do in a Forest world?\n a. Start fire\n b. Climb on Tree\n c. Enjoy Nature": "a" } qNADictS1 = { "Q1: Name one of the heavy equipments used in the forest operations\n a. Dump trucks\n b. Toycar\n c. Teddybear": "a", "Q2: Which is NOT one of the 4 forest types?\n a. Temperate\n b. Tropical\n c. Subtropical\n d. Arboreal" : "d" } qNADictS2 = { "Q1: Forest soil is a natural water filter\n a. True\n b. False": "a", "Q2: What is the definition of a forest?\n a. A public green area in a town, used for recreation.\n b. An area rich in biodiversity\n c. A large area of land densely populated by trees\n d. A flat area covered in plants" : "c" } def __init__(self): self.worldName = "Silvia" self.worldType = "ForestWorld" self.essentials = ["Mosquito Repellent", "Backpack", "Poncho", "Hiking boots", "Trail shoes"] self.story = "Hoy there! I'm Bryn, and I will be your personal guide through this wonderful world of trees and plants. \nWelcome to the forest, young explorer! These trees are sacred creatures who descended from the heavens. Enjoy your time in this wonderful place that you can call home" self.qualifyingQuiz = QualifyingQuiz(Silvia.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Silvia.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Silvia.qNADictS2) class Aquamundi(GameWorld): """Child class for Water World - Aquamundi""" #name = "Aquamundi" qNADictQ = { "Q1: Spell Water\n a. Water\n b. Waiter": "a", "Q2: Is water same as wind?\n a. Yes\n b. No": "b", "Q3: Do you know how to swim?\n a. Yes\n b. No": "a" } qNADictS1 = { "Q1: Why do you want to be a Dive instructor?\n a. I like the word Instructor\n b. I love teaching diving": "b", "Q2: Do you have a Captain's license?\n a. Yes\n b. No": "a" } qNADictS2 = { "Q1: In case of any emergency call, what will be your first step?\n a. Ignore\n b. Immediately act on it": "b", "Q2: What is a large natural stream of flowing water that ends in a sea?\n a. Lake\n b. Ocean\n c. Glacier\n d. River": "d" } def __init__(self): self.worldName = "Aquamundi" self.worldType = "WaterWorld" self.essentials = ["Swimcap", "Bandana", "Float suit", "Rash guard", "Alternate Air Source"] self.story = "I see, you chose the water world. Great choice! I'm Aquanna, and I will be your guide in Aquamundi. If you are wondering when this endless ocean ends, don't waste your time. \nThis is a huge world, filled with nothing but water. Let's hop on our big boat and start this adventure!" self.qualifyingQuiz = QualifyingQuiz(Aquamundi.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Aquamundi.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Aquamundi.qNADictS2) class Montelocus(GameWorld): """Child class for Mountain World - Montelocus""" #name = "Montelocus" qNADictQ = { "Q1: Do you know how to use mountain gear?\n a. Yes\n b. No": "a", "Q2: Are you a wayfinder or a lost sheep?\n a. Wayfinder\n b. Lostsheep": "a", "Q3: Are you afraid of heights?\n a. Yes\n b. No": "b" } qNADictS1 = { "Q1: What would you select to climb in tricky mountain conditions?\n a. Mountaineering boots\n b. mountains": "a", "Q2: What would you do if someone calls for help?\n a. It's their problem, I will just ignore\n b. Go and help them": "b" } qNADictS2 = { "Q1: Which of the following is the highest part of a mountain?\n a. Peak\n b. Base\n c. Slope\n d. None of the above": "a", "Q2: What should you do if you fall off a mountain during Climbing?\n a. Have a quick snack\n b. Make a quick phone call\n c. Take a selfie and post it on instagram\n d. Use a harness": "d" } def __init__(self): self.worldName = "Montelocus" self.worldType = "MountainWorld" self.essentials = ["Climbing Helmet", "Ropes", "Harness", "Mountaineering Boots", "Crampons"] self.story = "Isn't it beautiful, the divine mountain range of Montelocus. Oh hey! I didn't see you there! I'm Cashel, and I will lead you through the wonders of this rocky world, with mountains everywhere. \nThese mountains are said to be a gift from the sky. Take a moment to appreciate this particularly beautiful aspect of nature! \nAnyway, see you around!" self.qualifyingQuiz = QualifyingQuiz(Montelocus.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Montelocus.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Montelocus.qNADictS2) class User: """Class for storing user information and tracking user activity throughout multiple stages of the game""" def __init__(self, name, location, system, coins=100): self.name = name self.location = location # not sure if we need it self.system = system self.coins = coins self.gems = 0 self.assets = [] def __str__(self): return self.name def collectGem(self): """Increments gem count of user by 1, if the user earns gem""" self.gems += 1 def buyGem(self, amount): """Increments gem count of user by 1 if the buys the gem by spending coins""" returnVal = False if self.spendCoin(amount=25): self.gems += 1 returnVal = True return returnVal def earnCoin(self, amount): """Increments count of coins by a certain amount""" self.coins += amount def spendCoin(self, amount): """Decreases count of coins by a certain amount""" returnVal = False if self.coins >= amount: self.coins -= amount returnVal = True return returnVal def getCoins(self): """Returns number of coins of the user""" return self.coins def getGems(self): """Returns number of gems of the user""" return self.gems def buyEssentials(self, amount=5): """Returns list of essential items bought by the user for the selected world and displays the list of user's assets.""" ls = self.location.getEssentialList() print(f"{bcolors.WHITE}\nGreat job so far! Now, it's time to arm yourself with some essentials you would need to survive in the {self.location.worldType}.{bcolors.ENDC}") print(f"\n{bcolors.CYAN}Following are the essential items for {self.location.worldName}. Please choose a minimum of 3 items to proceed.{bcolors.ENDC}") outputList = [str(i+1) + ". " + ls[i] + "\n" for i in range(len(ls))] print(f"\n{bcolors.CYAN}{''.join(outputList)}{bcolors.ENDC}") sizeEssentialList = len(ls) essentialsList = [] choiceInput = False while choiceInput is False: choices = input(f"{bcolors.CYAN}Input your selection as numbers 1, 2, 3, 4, or 5 separated by comma: {bcolors.ENDC}") choiceInput = True choices = choices.split(',') for choice in choices: if choice not in ('1', '2', '3', '4', '5', 'quit', 'QUIT', 'Quit'): print(f"\n{bcolors.PINK}Please enter a valid Input{bcolors.ENDC}\n") choiceInput = False break for choice in choices: if choice.capitalize() == "Quit": # User input "Quit" at this stage. So, just quit the game. return choices try: # Convert input to integer for index in essentialList item choices = [int(i) for i in choices] except ValueError: # If input is not a number, Quit gracefully! print("Input is not a number. Quit") return essentialsList if max(choices) > sizeEssentialList: print(f"Invalid input! Input is not in essentialList") return essentialsList for j in choices: if self.spendCoin(amount): essentialsList.append(ls[j-1]) else: print(f"You don't have enough money to buy {j}. You only have {self.coins} coins left.") break self.assets = essentialsList print(f"\n{bcolors.WHITE}Thank you for buying the essentials. Now you are officially ready to enter into the {self.location.worldType}.\nHere is your current asset bag with essential items and the available coins.{bcolors.ENDC}") print(f"\n{bcolors.YELLOW}Asset Bag Contents: {self.assets}\nCoins: {self.coins}{bcolors.ENDC}") return self.assets def getAssets(self): """Returns list of assets of the user""" return self.assets def takeSurvivalQuiz(self, quiz): """Returns the result of survival quiz""" if quiz == 'survivalQuiz1': print(f"\n{bcolors.WHITE}Starting Survival quiz1 for {self.location.worldName} in 3 2 1....{bcolors.ENDC}") s(2) print(f"\n{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd': {bcolors.ENDC}") userSurvival = [(i, self.location.survivalQuiz1.questionAndAnswer.get(i)) for i in self.location.survivalQuiz1.questionAndAnswer.keys()] elif quiz == 'survivalQuiz2': print(f"\n{bcolors.WHITE}Starting Survival quiz2 for {self.location.worldName} in 3 2 1....{bcolors.ENDC}") s(2) #print(f"\n{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd': {bcolors.ENDC}") userSurvival = [(i, self.location.survivalQuiz2.questionAndAnswer.get(i)) for i in self.location.survivalQuiz2.questionAndAnswer.keys()] else: # Wrong argument, should not have reached here. Return anyway! userSurvival = "Quit" survivalResults = self.location.survivalQuizMethod(userSurvival) return survivalResults def navigateToCapitalcity(self, amount=20): """Increments number if gems by 1 if the user succesfully navigates to the capital city""" navSuccess = False if self.spendCoin(amount): self.gems += 1 navSuccess = True return navSuccess class GameSystem: """This is the game engine where other classes are ingested. This class controls the whole game system and stores system information""" def __init__(self): self.coins = 100 self.name = "Gateway To Adventure" self.minCorrectAnswers = 2 self.minGemsWin = 4 self.minCoinsWin = 25 self.minGemForCapitalCity = 2 self.buyGemAmount = 25 self.minEssentials = 3 self.maxAssetNum = 5 self.survivalWinCoins = 10 self.userName = "Empty" def __str__(self): return self.name def getUserLocation(self, userName, worldList): """Returns user location after User selects a world""" validInput = False itemizedWorldList = [ "\n" + str(i+1) + ". " + worldList[i] for i in range(len(worldList))] while validInput is False: print(f"{bcolors.CYAN}Choose a world from the following: {bcolors.BOLD}{''.join(itemizedWorldList)}{bcolors.ENDC}") userLocation = input(f"{bcolors.CYAN}Please input the exact name of the world: {bcolors.ENDC}") userLocation = userLocation.capitalize() if userLocation in worldList: validInput = True else: if userLocation == "Quit": validInput = True else: print(f"{bcolors.FAIL}Invalid world name! Please enter a correct World Name {bcolors.ENDC}") validInput = False return userLocation def selectWorld(self, userLocation, userName, availableWorldList): """Returns world type and the result of qualifying quiz, after User selects the world""" gameReturn = False if userLocation in availableWorldList: if userLocation == "Nixoterra": world = Nixoterra() elif userLocation == "Silvia": world = Silvia() elif userLocation == "Aquamundi": world = Aquamundi() elif userLocation == "Montelocus": world = Montelocus() else: # Should not have reached here! print(f"{bcolors.FAIL}Quitting as {userName} wants to quit the game.{bcolors.ENDC}") return gameReturn print(f"{bcolors.WHITE}Thank you for choosing the {world.worldType}.\n\n-------------\n{world.story}\n-------------\nNow, Let's see if you are ready for the selected world :-)\n\nStarting Qualifying quiz for {userLocation} in 3 2 1...{bcolors.ENDC}\n") s(2) print(f"{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd'{bcolors.ENDC}") userQualifying = [(i, world.qualifyingQuiz.questionAndAnswer.get(i)) for i in world.qualifyingQuiz.questionAndAnswer.keys()] qualifyingResults = world.qualifyingQuizMethod(userQualifying) if self.checkReturn(qualifyingResults, userName): return gameReturn else: print(f"{bcolors.FAIL}You have already failed in the Qualifying quiz of {userLocation}. You can't reattempt thw Qualifyiong quiz.{bcolors.ENDC}") return world, qualifyingResults def checkReturn(self, returnVal, name): """Returns True if user enters 'quit' at any stage of the game""" quitGame = False if str(returnVal).capitalize() == "Quit": print(f"{bcolors.FAIL}Quitting as {name} wants to quit the game.{bcolors.ENDC}") # User entered Quit, exit and return accordingly quitGame = True return quitGame def evaluateSurvialQuiz(self, player): """Evaluates and displays result of survival quiz and also displays the user's current asset""" print(f"\n{bcolors.WHITE}Let's start earning some more coins by answering survival questions that will help you survive in the world{bcolors.ENDC}") quizList = ['survivalQuiz1', 'survivalQuiz2'] perkReattempt = True for quiz in quizList: survivalResults = player.takeSurvivalQuiz(quiz) if self.checkReturn(survivalResults, player.name): return False if not survivalResults: print(f"{bcolors.FAIL}Sorry, you failed.{bcolors.ENDC}") if len(player.getAssets()) > self.minEssentials and perkReattempt: print(f"{bcolors.WHITE}You are eligible for 1 reattempt.{bcolors.ENDC}") perkReattempt = False survivalResults = player.takeSurvivalQuiz(quiz) if self.checkReturn(survivalResults, player.name): return False if not survivalResults: print(f"{bcolors.FAIL}Sorry, you failed again.{bcolors.ENDC}") else: player.earnCoin(self.survivalWinCoins * survivalResults) player.collectGem() print(f"{bcolors.YELLOW}Congrats! {player.name}, you have cleared {quiz}{bcolors.ENDC}") #print(f"current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}") else: player.earnCoin(self.survivalWinCoins * survivalResults) player.collectGem() print(f"{bcolors.YELLOW}Congrats {player.name}, you have cleared {quiz}{bcolors.ENDC}") print(f"{bcolors.YELLOW}current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}{bcolors.ENDC}") return True def gameStart(self): """Returns true if the user successfully completes the game and handles invalid user input. Also, starts the game and create instances of user and world""" print(f"{bcolors.WHITE}Hello, and welcome to the {bcolors.BOLD}Gateway of Adventure!{bcolors.ENDC} {bcolors.WHITE} Let's begin the fun ride!\nYou can quit the game at any stage of the game by typing Quit. Good Luck!{bcolors.ENDC}") gameReturn = False worldList = ["Nixoterra", "Silvia", "Aquamundi", "Montelocus"] userInput = input(f"Would you like to view the instruction manual? (y/n): ") while userInput.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") userInput = input(f"Would you like to view the instruction manual? (y/n): ") if userInput.lower() == "y": print(f"\n{bcolors.WHITE}Open this link to view the Instruction Manual:\n{bcolors.BLUE}{bcolors.UNDERLINE}https://github.prod.oc.2u.com/UCB-INFO-PYTHON/Jyoti_KumariREPO/blob/master/SUBMISSIONS/project_1/Gateway%20to%20Adventure%20-%20%20Instruction%20Manual.pdf{bcolors.ENDC}") elif userInput.lower() == "quit": self.checkReturn(userInput, "guest") return gameReturn if game.userName == "Empty": userName = input(f"\n{bcolors.CYAN}What is your name? {bcolors.ENDC}") self.userName = userName else: userName = self.userName #print(f"{bcolors.FAIL}You can type Quit to quit at any stage of the game. {bcolors.ENDC}") userLocation = self.getUserLocation(userName, worldList) if not userLocation: return gameReturn #userLocation = userLocation.lower() if self.checkReturn(userLocation, userName): return gameReturn # Valid User Location found # Let's select the world and do the Qualifying quiz for the world loopReturn = False while True: world, qualifyingResults = self.selectWorld(userLocation, userName, worldList) if not world: loopReturn = False break print(f"\n{bcolors.YELLOW}{userName}, you got {qualifyingResults} questions right.{bcolors.ENDC}\n") if qualifyingResults < self.minCorrectAnswers: # User failed the qualifying quiz print(f"{bcolors.FAIL}You failed in Qualifying quiz of {userLocation} {bcolors.ENDC}") worldList.remove(userLocation) if len(worldList): print(f"{bcolors.WHITE}\nTry selecting another world to proceed. You can type Quit to quit at any stage of the game.{bcolors.ENDC}\n") userLocation = self.getUserLocation(userName, worldList) if not userLocation: loopReturn = False break if self.checkReturn(userLocation, userName): loopReturn = False break else: loopReturn = False break else: loopReturn = True print(f"{bcolors.YELLOW}Congratulations! you now qualify to enter {userLocation}\n\n{world.story}\n\nYou have just won 100 coins!{bcolors.ENDC}") break if not loopReturn: return gameReturn # User Qualified the quiz for the world # Let's initialize Classes for World and player player = User(userName, world, self) #print(f"worldName: {world.worldName}\nworldType: {world.worldType}\n") #print(f"Player Name: {player.name}\nPlayer Location: {player.location}\nPlayer Coins: {player.getCoins()}\n" ) essentialsList = player.buyEssentials() if not essentialsList: # No item selected from the essential list # Player can't survive in the world and Exit the world with message print(f"{bcolors.FAIL}Quitting as {userName} didn't select any essential items to enter the new world.{bcolors.ENDC}") return gameReturn if self.checkReturn(essentialsList, userName): return gameReturn #print(f"{userName} Assets: ", player.getAssets()) if len(player.getAssets()) < self.minEssentials: print(f"{bcolors.FAIL}{userName} needs to select at least {self.minEssentials} from the list.{bcolors.ENDC}") return gameReturn userInput = input(f"\n{bcolors.WHITE}{userName}, you have successfully entered into the {world.worldType}. As a next step, you would need to buy a gem for 25 coins to further explore this world.\nDo you want to proceed (y/n)?: {bcolors.ENDC}") while userInput.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") userInput = input(f"\n{bcolors.WHITE}{userName}, you have successfully entered into the {world.worldType}. As a next step, you would need to buy a gem for 25 coins to further explore this world.\nDo you want to proceed (y/n)?: {bcolors.ENDC}") if userInput.lower() == 'y': if player.buyGem(self.buyGemAmount): print(f"{bcolors.YELLOW}Thank you {userName}, You now have {player.getGems()} gems and {player.getCoins()} coins{bcolors.ENDC}") else: self.checkReturn("quit", userName) return gameReturn s(2) # Conduct and Evaluate Survial Quiz for player if not self.evaluateSurvialQuiz(player): return gameReturn # Navigate to Capital City if player.getGems() >= self.minGemForCapitalCity: print(f"{bcolors.WHITE}\nYay! you are a rockstar! Let's proceed to the next stage of the your journey by going to the very beautiful Capital City of {world.worldName}.{bcolors.ENDC}") s(2) print(f"{bcolors.WHITE}\nEntering into the Capital City 3 2 1....{bcolors.ENDC}\n") s(2) if len(player.getAssets()) == self.maxAssetNum : player.navigateToCapitalcity(10) else: player.navigateToCapitalcity() print(f"{bcolors.YELLOW}{userName} has reached the capital city of {userLocation}{bcolors.ENDC}") print(f"{bcolors.YELLOW}current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}{bcolors.ENDC}") response = input(f"\n{bcolors.WHITE}Do you want to buy addditional Gem for 25 coins? (y/n) {bcolors.ENDC}").lower() while response.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") response = input(f"\n{bcolors.WHITE}Do you want to buy addditional Gem for 25 coins? (y/n) {bcolors.ENDC}").lower() if response == 'y': player.buyGem(25) print(f"user bought one gem") elif response == 'n': print(f"{bcolors.WHITE}{userName} declined offer{bcolors.ENDC}\n") else: self.checkReturn("quit", userName) return gameReturn # Check if Player has enough gems to win the game if player.getGems() >= self.minGemsWin: gameReturn = True # Check if Player has enough coins to win the game if player.getCoins() >= self.minCoinsWin: print(f"{bcolors.GREEN}{userName} has {player.getCoins()} coins and {player.getGems()} gems!") print(f"{bcolors.GREEN}Congratulations {userName}, you Won!{bcolors.ENDC}") else: print(f"{bcolors.PINK}Sorry {userName}, you lost.") print(f"{userName} has {player.getCoins()} coins left") print(f"You needed {self.minCoinsWin} coins to win the game!{bcolors.ENDC}") else: print(f"{bcolors.PINK}Sorry {userName}, you lost.") print(f"{userName} has {player.getCoins()} coins left") print(f"You needed {self.minGemsWin} gems to win the game!{bcolors.ENDC}") gameReturn = True return gameReturn def gameQuit(self): "Displays Goodbye message if the user has selected to quit the game" print(f"{bcolors.PINK}Goodbye {self.userName}, see you again!{bcolors.ENDC}") parser = argparse.ArgumentParser(description = 'Step 1: Select a world --> Step 2: Take Qualifying quiz --> Step 3: Buy essentials(a minimum of 3 items) --> Step 4: Enter World and take 2 survival quizzes, one by one --> Step 5: Go to Capital City to get a gem or buy a gem --> Step 6: You win or lose depending on, if you have 25 coins and 4 gems at a minimum') args = parser.parse_args() game = GameSystem() gameRestart = False gameRestart = game.gameStart() while gameRestart: gameRestart = input(f"\nDo you want to replay the Game!? (y/n): ") if gameRestart.lower() == 'y': gameRestart = game.gameStart() else: gameRestart = False game.gameQuit()
"""Child class to Quiz class that stores dictionary of questions and answers for the Survival quizzes""" def __init__(self, qNADict={"Ques2": "Ans2"}): self.questionAndAnswer = qNADict
identifier_body
GatewayToAdventure.py
import argparse from time import sleep as s class bcolors: """Base class to provide color to user prompts and texts.""" CYAN = '\033[96m' GREEN = '\033[92m' WHITE = '\033[37m' PINK = '\033[35m' YELLOW = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' BLUE = '\033[94m' class
: """Base class to store dictionary of questions and answers for the game quizzes""" def __init__(self, qNADict={"Ques": "Ans"}): self.questionAndAnswer = qNADict class QualifyingQuiz(Quiz): """Child class to Quiz class that stores dictionary of questions and answers for the qualifying quiz""" def __init__(self, qNADict={"Ques1": "Ans1"}): self.questionAndAnswer = qNADict class SurvivalQuiz(Quiz): """Child class to Quiz class that stores dictionary of questions and answers for the Survival quizzes""" def __init__(self, qNADict={"Ques2": "Ans2"}): self.questionAndAnswer = qNADict class GameWorld: """Base class for creating different worlds in the game""" def __init__(self, name='gameWorld', typeW='all'): self.worldName = name self.worldType = typeW self.essentials = [] self.qualifyingQuiz = QualifyingQuiz() self.survivalQuiz1 = SurvivalQuiz() self.survivalQuiz2 = SurvivalQuiz() def qualifyingQuizMethod(self, questionAndAnswer): """Returns number of correct answers for Qualifying quiz. Compares User response with the desired answer from the existing dictionary""" questionsRight = 0 for question in questionAndAnswer: answer = input(f"{bcolors.CYAN} {question[0]}\n: {bcolors.ENDC}") if answer == question[1]: questionsRight += 1 elif answer.capitalize() == "Quit": # User wants to Quit the Game now print(f"User wants to quit the game now") questionsRight = "Quit" break return questionsRight def survivalQuizMethod(self, questionAndAnswer): """Returns 1 if User correctly answers all the question of the Survival quiz, else 0. Compares User response with the desired answer from the existing dictionary""" questionsRight = 1 for question in questionAndAnswer: answer = input(f"\n{bcolors.CYAN}{question[0]}\n:{bcolors.ENDC}") if answer == question[1]: questionsRight *= 1 elif answer != question[1]: questionsRight *= 0 elif answer.capitalize() == "Quit": # User wants to Quit the Game now print(f"User wants to quit the game now") questionsRight = "Quit" break return questionsRight def getEssentialList(self): """Returns list of essentials for a particular world""" return self.essentials def __str__(self): return self.worldName class Nixoterra(GameWorld): """Child class for Snow World - Nixoterra""" #name = "Nixoterra" qNADictQ = { "Q1: What is the freezing point of snow?\n a. 0 degrees celcius\n b. 30 degrees celcius\n c. 100 degrees celcius\n d. -5 degrees celcius": "d", "Q2: Which ball do we use in the snow ball activity at snow world?\n a. Leather Ball\n b. Foot Ball\n c. Snow Ball\n d. All of the above": "d", "Q3: Which cartoon character does snow world have?\n a. Penguin\n b. Olaf\n c. Snowman\n d. All of the above": "d" } qNADictS1 = { "Q1: Describe a typical workweek for a snowboard instructor\n a. Sleep all week long\n b. Play with Ice\n c. Instruct and provide training to groups/individuals": "c", "Q2: what's your level of experience with edging, waxing and mounting skis and snowboard?\n a. Novice\n b. Proficient\n c. expert": "c" } qNADictS2 = { "Q1: What equipment is/are required for working as an experienced snowmaker\n a. Snow guns\n b. hoses\n c. hydrants\n d. All of the above": "d", "Q2: Can you safely lift 50 lbs\n a. Yes\n b. No": "a" } def __init__(self): self.worldName = "Nixoterra" self.worldType = "SnowWorld" self.essentials = ["Mittens", "Snowboots", "Ski Goggles", "Wool Socks", "Hand warmer"] self.story = "Ice, Chill, Snow, Cold, Freezerealm, Glacia. You name it. \nThey are all names for the mystic Nixoterra. Allow me, Nixette, to guide you through this world. This land is the mother of all snow and ice. Explore this world carefully, because the temperatures are- well, piercing. But other than that, just chill." #ToDo: Fill real name of capital city self.qualifyingQuiz = QualifyingQuiz(Nixoterra.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Nixoterra.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Nixoterra.qNADictS2) class Silvia(GameWorld): """Child class for Forest World - Silvia""" #name = "Silvia" qNADictQ = { "Q1: Can you use a GPS device effectively?\n a. Yes\n b. No": "a", "Q2: Which of the following statement is true?\n a. Forest is on land\n b. Forest is under water": "a", "Q3: What NOT to do in a Forest world?\n a. Start fire\n b. Climb on Tree\n c. Enjoy Nature": "a" } qNADictS1 = { "Q1: Name one of the heavy equipments used in the forest operations\n a. Dump trucks\n b. Toycar\n c. Teddybear": "a", "Q2: Which is NOT one of the 4 forest types?\n a. Temperate\n b. Tropical\n c. Subtropical\n d. Arboreal" : "d" } qNADictS2 = { "Q1: Forest soil is a natural water filter\n a. True\n b. False": "a", "Q2: What is the definition of a forest?\n a. A public green area in a town, used for recreation.\n b. An area rich in biodiversity\n c. A large area of land densely populated by trees\n d. A flat area covered in plants" : "c" } def __init__(self): self.worldName = "Silvia" self.worldType = "ForestWorld" self.essentials = ["Mosquito Repellent", "Backpack", "Poncho", "Hiking boots", "Trail shoes"] self.story = "Hoy there! I'm Bryn, and I will be your personal guide through this wonderful world of trees and plants. \nWelcome to the forest, young explorer! These trees are sacred creatures who descended from the heavens. Enjoy your time in this wonderful place that you can call home" self.qualifyingQuiz = QualifyingQuiz(Silvia.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Silvia.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Silvia.qNADictS2) class Aquamundi(GameWorld): """Child class for Water World - Aquamundi""" #name = "Aquamundi" qNADictQ = { "Q1: Spell Water\n a. Water\n b. Waiter": "a", "Q2: Is water same as wind?\n a. Yes\n b. No": "b", "Q3: Do you know how to swim?\n a. Yes\n b. No": "a" } qNADictS1 = { "Q1: Why do you want to be a Dive instructor?\n a. I like the word Instructor\n b. I love teaching diving": "b", "Q2: Do you have a Captain's license?\n a. Yes\n b. No": "a" } qNADictS2 = { "Q1: In case of any emergency call, what will be your first step?\n a. Ignore\n b. Immediately act on it": "b", "Q2: What is a large natural stream of flowing water that ends in a sea?\n a. Lake\n b. Ocean\n c. Glacier\n d. River": "d" } def __init__(self): self.worldName = "Aquamundi" self.worldType = "WaterWorld" self.essentials = ["Swimcap", "Bandana", "Float suit", "Rash guard", "Alternate Air Source"] self.story = "I see, you chose the water world. Great choice! I'm Aquanna, and I will be your guide in Aquamundi. If you are wondering when this endless ocean ends, don't waste your time. \nThis is a huge world, filled with nothing but water. Let's hop on our big boat and start this adventure!" self.qualifyingQuiz = QualifyingQuiz(Aquamundi.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Aquamundi.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Aquamundi.qNADictS2) class Montelocus(GameWorld): """Child class for Mountain World - Montelocus""" #name = "Montelocus" qNADictQ = { "Q1: Do you know how to use mountain gear?\n a. Yes\n b. No": "a", "Q2: Are you a wayfinder or a lost sheep?\n a. Wayfinder\n b. Lostsheep": "a", "Q3: Are you afraid of heights?\n a. Yes\n b. No": "b" } qNADictS1 = { "Q1: What would you select to climb in tricky mountain conditions?\n a. Mountaineering boots\n b. mountains": "a", "Q2: What would you do if someone calls for help?\n a. It's their problem, I will just ignore\n b. Go and help them": "b" } qNADictS2 = { "Q1: Which of the following is the highest part of a mountain?\n a. Peak\n b. Base\n c. Slope\n d. None of the above": "a", "Q2: What should you do if you fall off a mountain during Climbing?\n a. Have a quick snack\n b. Make a quick phone call\n c. Take a selfie and post it on instagram\n d. Use a harness": "d" } def __init__(self): self.worldName = "Montelocus" self.worldType = "MountainWorld" self.essentials = ["Climbing Helmet", "Ropes", "Harness", "Mountaineering Boots", "Crampons"] self.story = "Isn't it beautiful, the divine mountain range of Montelocus. Oh hey! I didn't see you there! I'm Cashel, and I will lead you through the wonders of this rocky world, with mountains everywhere. \nThese mountains are said to be a gift from the sky. Take a moment to appreciate this particularly beautiful aspect of nature! \nAnyway, see you around!" self.qualifyingQuiz = QualifyingQuiz(Montelocus.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Montelocus.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Montelocus.qNADictS2) class User: """Class for storing user information and tracking user activity throughout multiple stages of the game""" def __init__(self, name, location, system, coins=100): self.name = name self.location = location # not sure if we need it self.system = system self.coins = coins self.gems = 0 self.assets = [] def __str__(self): return self.name def collectGem(self): """Increments gem count of user by 1, if the user earns gem""" self.gems += 1 def buyGem(self, amount): """Increments gem count of user by 1 if the buys the gem by spending coins""" returnVal = False if self.spendCoin(amount=25): self.gems += 1 returnVal = True return returnVal def earnCoin(self, amount): """Increments count of coins by a certain amount""" self.coins += amount def spendCoin(self, amount): """Decreases count of coins by a certain amount""" returnVal = False if self.coins >= amount: self.coins -= amount returnVal = True return returnVal def getCoins(self): """Returns number of coins of the user""" return self.coins def getGems(self): """Returns number of gems of the user""" return self.gems def buyEssentials(self, amount=5): """Returns list of essential items bought by the user for the selected world and displays the list of user's assets.""" ls = self.location.getEssentialList() print(f"{bcolors.WHITE}\nGreat job so far! Now, it's time to arm yourself with some essentials you would need to survive in the {self.location.worldType}.{bcolors.ENDC}") print(f"\n{bcolors.CYAN}Following are the essential items for {self.location.worldName}. Please choose a minimum of 3 items to proceed.{bcolors.ENDC}") outputList = [str(i+1) + ". " + ls[i] + "\n" for i in range(len(ls))] print(f"\n{bcolors.CYAN}{''.join(outputList)}{bcolors.ENDC}") sizeEssentialList = len(ls) essentialsList = [] choiceInput = False while choiceInput is False: choices = input(f"{bcolors.CYAN}Input your selection as numbers 1, 2, 3, 4, or 5 separated by comma: {bcolors.ENDC}") choiceInput = True choices = choices.split(',') for choice in choices: if choice not in ('1', '2', '3', '4', '5', 'quit', 'QUIT', 'Quit'): print(f"\n{bcolors.PINK}Please enter a valid Input{bcolors.ENDC}\n") choiceInput = False break for choice in choices: if choice.capitalize() == "Quit": # User input "Quit" at this stage. So, just quit the game. return choices try: # Convert input to integer for index in essentialList item choices = [int(i) for i in choices] except ValueError: # If input is not a number, Quit gracefully! print("Input is not a number. Quit") return essentialsList if max(choices) > sizeEssentialList: print(f"Invalid input! Input is not in essentialList") return essentialsList for j in choices: if self.spendCoin(amount): essentialsList.append(ls[j-1]) else: print(f"You don't have enough money to buy {j}. You only have {self.coins} coins left.") break self.assets = essentialsList print(f"\n{bcolors.WHITE}Thank you for buying the essentials. Now you are officially ready to enter into the {self.location.worldType}.\nHere is your current asset bag with essential items and the available coins.{bcolors.ENDC}") print(f"\n{bcolors.YELLOW}Asset Bag Contents: {self.assets}\nCoins: {self.coins}{bcolors.ENDC}") return self.assets def getAssets(self): """Returns list of assets of the user""" return self.assets def takeSurvivalQuiz(self, quiz): """Returns the result of survival quiz""" if quiz == 'survivalQuiz1': print(f"\n{bcolors.WHITE}Starting Survival quiz1 for {self.location.worldName} in 3 2 1....{bcolors.ENDC}") s(2) print(f"\n{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd': {bcolors.ENDC}") userSurvival = [(i, self.location.survivalQuiz1.questionAndAnswer.get(i)) for i in self.location.survivalQuiz1.questionAndAnswer.keys()] elif quiz == 'survivalQuiz2': print(f"\n{bcolors.WHITE}Starting Survival quiz2 for {self.location.worldName} in 3 2 1....{bcolors.ENDC}") s(2) #print(f"\n{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd': {bcolors.ENDC}") userSurvival = [(i, self.location.survivalQuiz2.questionAndAnswer.get(i)) for i in self.location.survivalQuiz2.questionAndAnswer.keys()] else: # Wrong argument, should not have reached here. Return anyway! userSurvival = "Quit" survivalResults = self.location.survivalQuizMethod(userSurvival) return survivalResults def navigateToCapitalcity(self, amount=20): """Increments number if gems by 1 if the user succesfully navigates to the capital city""" navSuccess = False if self.spendCoin(amount): self.gems += 1 navSuccess = True return navSuccess class GameSystem: """This is the game engine where other classes are ingested. This class controls the whole game system and stores system information""" def __init__(self): self.coins = 100 self.name = "Gateway To Adventure" self.minCorrectAnswers = 2 self.minGemsWin = 4 self.minCoinsWin = 25 self.minGemForCapitalCity = 2 self.buyGemAmount = 25 self.minEssentials = 3 self.maxAssetNum = 5 self.survivalWinCoins = 10 self.userName = "Empty" def __str__(self): return self.name def getUserLocation(self, userName, worldList): """Returns user location after User selects a world""" validInput = False itemizedWorldList = [ "\n" + str(i+1) + ". " + worldList[i] for i in range(len(worldList))] while validInput is False: print(f"{bcolors.CYAN}Choose a world from the following: {bcolors.BOLD}{''.join(itemizedWorldList)}{bcolors.ENDC}") userLocation = input(f"{bcolors.CYAN}Please input the exact name of the world: {bcolors.ENDC}") userLocation = userLocation.capitalize() if userLocation in worldList: validInput = True else: if userLocation == "Quit": validInput = True else: print(f"{bcolors.FAIL}Invalid world name! Please enter a correct World Name {bcolors.ENDC}") validInput = False return userLocation def selectWorld(self, userLocation, userName, availableWorldList): """Returns world type and the result of qualifying quiz, after User selects the world""" gameReturn = False if userLocation in availableWorldList: if userLocation == "Nixoterra": world = Nixoterra() elif userLocation == "Silvia": world = Silvia() elif userLocation == "Aquamundi": world = Aquamundi() elif userLocation == "Montelocus": world = Montelocus() else: # Should not have reached here! print(f"{bcolors.FAIL}Quitting as {userName} wants to quit the game.{bcolors.ENDC}") return gameReturn print(f"{bcolors.WHITE}Thank you for choosing the {world.worldType}.\n\n-------------\n{world.story}\n-------------\nNow, Let's see if you are ready for the selected world :-)\n\nStarting Qualifying quiz for {userLocation} in 3 2 1...{bcolors.ENDC}\n") s(2) print(f"{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd'{bcolors.ENDC}") userQualifying = [(i, world.qualifyingQuiz.questionAndAnswer.get(i)) for i in world.qualifyingQuiz.questionAndAnswer.keys()] qualifyingResults = world.qualifyingQuizMethod(userQualifying) if self.checkReturn(qualifyingResults, userName): return gameReturn else: print(f"{bcolors.FAIL}You have already failed in the Qualifying quiz of {userLocation}. You can't reattempt thw Qualifyiong quiz.{bcolors.ENDC}") return world, qualifyingResults def checkReturn(self, returnVal, name): """Returns True if user enters 'quit' at any stage of the game""" quitGame = False if str(returnVal).capitalize() == "Quit": print(f"{bcolors.FAIL}Quitting as {name} wants to quit the game.{bcolors.ENDC}") # User entered Quit, exit and return accordingly quitGame = True return quitGame def evaluateSurvialQuiz(self, player): """Evaluates and displays result of survival quiz and also displays the user's current asset""" print(f"\n{bcolors.WHITE}Let's start earning some more coins by answering survival questions that will help you survive in the world{bcolors.ENDC}") quizList = ['survivalQuiz1', 'survivalQuiz2'] perkReattempt = True for quiz in quizList: survivalResults = player.takeSurvivalQuiz(quiz) if self.checkReturn(survivalResults, player.name): return False if not survivalResults: print(f"{bcolors.FAIL}Sorry, you failed.{bcolors.ENDC}") if len(player.getAssets()) > self.minEssentials and perkReattempt: print(f"{bcolors.WHITE}You are eligible for 1 reattempt.{bcolors.ENDC}") perkReattempt = False survivalResults = player.takeSurvivalQuiz(quiz) if self.checkReturn(survivalResults, player.name): return False if not survivalResults: print(f"{bcolors.FAIL}Sorry, you failed again.{bcolors.ENDC}") else: player.earnCoin(self.survivalWinCoins * survivalResults) player.collectGem() print(f"{bcolors.YELLOW}Congrats! {player.name}, you have cleared {quiz}{bcolors.ENDC}") #print(f"current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}") else: player.earnCoin(self.survivalWinCoins * survivalResults) player.collectGem() print(f"{bcolors.YELLOW}Congrats {player.name}, you have cleared {quiz}{bcolors.ENDC}") print(f"{bcolors.YELLOW}current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}{bcolors.ENDC}") return True def gameStart(self): """Returns true if the user successfully completes the game and handles invalid user input. Also, starts the game and create instances of user and world""" print(f"{bcolors.WHITE}Hello, and welcome to the {bcolors.BOLD}Gateway of Adventure!{bcolors.ENDC} {bcolors.WHITE} Let's begin the fun ride!\nYou can quit the game at any stage of the game by typing Quit. Good Luck!{bcolors.ENDC}") gameReturn = False worldList = ["Nixoterra", "Silvia", "Aquamundi", "Montelocus"] userInput = input(f"Would you like to view the instruction manual? (y/n): ") while userInput.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") userInput = input(f"Would you like to view the instruction manual? (y/n): ") if userInput.lower() == "y": print(f"\n{bcolors.WHITE}Open this link to view the Instruction Manual:\n{bcolors.BLUE}{bcolors.UNDERLINE}https://github.prod.oc.2u.com/UCB-INFO-PYTHON/Jyoti_KumariREPO/blob/master/SUBMISSIONS/project_1/Gateway%20to%20Adventure%20-%20%20Instruction%20Manual.pdf{bcolors.ENDC}") elif userInput.lower() == "quit": self.checkReturn(userInput, "guest") return gameReturn if game.userName == "Empty": userName = input(f"\n{bcolors.CYAN}What is your name? {bcolors.ENDC}") self.userName = userName else: userName = self.userName #print(f"{bcolors.FAIL}You can type Quit to quit at any stage of the game. {bcolors.ENDC}") userLocation = self.getUserLocation(userName, worldList) if not userLocation: return gameReturn #userLocation = userLocation.lower() if self.checkReturn(userLocation, userName): return gameReturn # Valid User Location found # Let's select the world and do the Qualifying quiz for the world loopReturn = False while True: world, qualifyingResults = self.selectWorld(userLocation, userName, worldList) if not world: loopReturn = False break print(f"\n{bcolors.YELLOW}{userName}, you got {qualifyingResults} questions right.{bcolors.ENDC}\n") if qualifyingResults < self.minCorrectAnswers: # User failed the qualifying quiz print(f"{bcolors.FAIL}You failed in Qualifying quiz of {userLocation} {bcolors.ENDC}") worldList.remove(userLocation) if len(worldList): print(f"{bcolors.WHITE}\nTry selecting another world to proceed. You can type Quit to quit at any stage of the game.{bcolors.ENDC}\n") userLocation = self.getUserLocation(userName, worldList) if not userLocation: loopReturn = False break if self.checkReturn(userLocation, userName): loopReturn = False break else: loopReturn = False break else: loopReturn = True print(f"{bcolors.YELLOW}Congratulations! you now qualify to enter {userLocation}\n\n{world.story}\n\nYou have just won 100 coins!{bcolors.ENDC}") break if not loopReturn: return gameReturn # User Qualified the quiz for the world # Let's initialize Classes for World and player player = User(userName, world, self) #print(f"worldName: {world.worldName}\nworldType: {world.worldType}\n") #print(f"Player Name: {player.name}\nPlayer Location: {player.location}\nPlayer Coins: {player.getCoins()}\n" ) essentialsList = player.buyEssentials() if not essentialsList: # No item selected from the essential list # Player can't survive in the world and Exit the world with message print(f"{bcolors.FAIL}Quitting as {userName} didn't select any essential items to enter the new world.{bcolors.ENDC}") return gameReturn if self.checkReturn(essentialsList, userName): return gameReturn #print(f"{userName} Assets: ", player.getAssets()) if len(player.getAssets()) < self.minEssentials: print(f"{bcolors.FAIL}{userName} needs to select at least {self.minEssentials} from the list.{bcolors.ENDC}") return gameReturn userInput = input(f"\n{bcolors.WHITE}{userName}, you have successfully entered into the {world.worldType}. As a next step, you would need to buy a gem for 25 coins to further explore this world.\nDo you want to proceed (y/n)?: {bcolors.ENDC}") while userInput.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") userInput = input(f"\n{bcolors.WHITE}{userName}, you have successfully entered into the {world.worldType}. As a next step, you would need to buy a gem for 25 coins to further explore this world.\nDo you want to proceed (y/n)?: {bcolors.ENDC}") if userInput.lower() == 'y': if player.buyGem(self.buyGemAmount): print(f"{bcolors.YELLOW}Thank you {userName}, You now have {player.getGems()} gems and {player.getCoins()} coins{bcolors.ENDC}") else: self.checkReturn("quit", userName) return gameReturn s(2) # Conduct and Evaluate Survial Quiz for player if not self.evaluateSurvialQuiz(player): return gameReturn # Navigate to Capital City if player.getGems() >= self.minGemForCapitalCity: print(f"{bcolors.WHITE}\nYay! you are a rockstar! Let's proceed to the next stage of the your journey by going to the very beautiful Capital City of {world.worldName}.{bcolors.ENDC}") s(2) print(f"{bcolors.WHITE}\nEntering into the Capital City 3 2 1....{bcolors.ENDC}\n") s(2) if len(player.getAssets()) == self.maxAssetNum : player.navigateToCapitalcity(10) else: player.navigateToCapitalcity() print(f"{bcolors.YELLOW}{userName} has reached the capital city of {userLocation}{bcolors.ENDC}") print(f"{bcolors.YELLOW}current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}{bcolors.ENDC}") response = input(f"\n{bcolors.WHITE}Do you want to buy addditional Gem for 25 coins? (y/n) {bcolors.ENDC}").lower() while response.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") response = input(f"\n{bcolors.WHITE}Do you want to buy addditional Gem for 25 coins? (y/n) {bcolors.ENDC}").lower() if response == 'y': player.buyGem(25) print(f"user bought one gem") elif response == 'n': print(f"{bcolors.WHITE}{userName} declined offer{bcolors.ENDC}\n") else: self.checkReturn("quit", userName) return gameReturn # Check if Player has enough gems to win the game if player.getGems() >= self.minGemsWin: gameReturn = True # Check if Player has enough coins to win the game if player.getCoins() >= self.minCoinsWin: print(f"{bcolors.GREEN}{userName} has {player.getCoins()} coins and {player.getGems()} gems!") print(f"{bcolors.GREEN}Congratulations {userName}, you Won!{bcolors.ENDC}") else: print(f"{bcolors.PINK}Sorry {userName}, you lost.") print(f"{userName} has {player.getCoins()} coins left") print(f"You needed {self.minCoinsWin} coins to win the game!{bcolors.ENDC}") else: print(f"{bcolors.PINK}Sorry {userName}, you lost.") print(f"{userName} has {player.getCoins()} coins left") print(f"You needed {self.minGemsWin} gems to win the game!{bcolors.ENDC}") gameReturn = True return gameReturn def gameQuit(self): "Displays Goodbye message if the user has selected to quit the game" print(f"{bcolors.PINK}Goodbye {self.userName}, see you again!{bcolors.ENDC}") parser = argparse.ArgumentParser(description = 'Step 1: Select a world --> Step 2: Take Qualifying quiz --> Step 3: Buy essentials(a minimum of 3 items) --> Step 4: Enter World and take 2 survival quizzes, one by one --> Step 5: Go to Capital City to get a gem or buy a gem --> Step 6: You win or lose depending on, if you have 25 coins and 4 gems at a minimum') args = parser.parse_args() game = GameSystem() gameRestart = False gameRestart = game.gameStart() while gameRestart: gameRestart = input(f"\nDo you want to replay the Game!? (y/n): ") if gameRestart.lower() == 'y': gameRestart = game.gameStart() else: gameRestart = False game.gameQuit()
Quiz
identifier_name
GatewayToAdventure.py
import argparse from time import sleep as s class bcolors: """Base class to provide color to user prompts and texts.""" CYAN = '\033[96m' GREEN = '\033[92m' WHITE = '\033[37m' PINK = '\033[35m' YELLOW = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' BLUE = '\033[94m' class Quiz: """Base class to store dictionary of questions and answers for the game quizzes""" def __init__(self, qNADict={"Ques": "Ans"}): self.questionAndAnswer = qNADict class QualifyingQuiz(Quiz): """Child class to Quiz class that stores dictionary of questions and answers for the qualifying quiz""" def __init__(self, qNADict={"Ques1": "Ans1"}): self.questionAndAnswer = qNADict class SurvivalQuiz(Quiz): """Child class to Quiz class that stores dictionary of questions and answers for the Survival quizzes""" def __init__(self, qNADict={"Ques2": "Ans2"}): self.questionAndAnswer = qNADict class GameWorld: """Base class for creating different worlds in the game""" def __init__(self, name='gameWorld', typeW='all'): self.worldName = name self.worldType = typeW self.essentials = [] self.qualifyingQuiz = QualifyingQuiz() self.survivalQuiz1 = SurvivalQuiz() self.survivalQuiz2 = SurvivalQuiz() def qualifyingQuizMethod(self, questionAndAnswer): """Returns number of correct answers for Qualifying quiz. Compares User response with the desired answer from the existing dictionary""" questionsRight = 0 for question in questionAndAnswer: answer = input(f"{bcolors.CYAN} {question[0]}\n: {bcolors.ENDC}") if answer == question[1]: questionsRight += 1 elif answer.capitalize() == "Quit": # User wants to Quit the Game now print(f"User wants to quit the game now") questionsRight = "Quit" break return questionsRight def survivalQuizMethod(self, questionAndAnswer): """Returns 1 if User correctly answers all the question of the Survival quiz, else 0. Compares User response with the desired answer from the existing dictionary""" questionsRight = 1 for question in questionAndAnswer: answer = input(f"\n{bcolors.CYAN}{question[0]}\n:{bcolors.ENDC}") if answer == question[1]: questionsRight *= 1 elif answer != question[1]: questionsRight *= 0 elif answer.capitalize() == "Quit": # User wants to Quit the Game now print(f"User wants to quit the game now") questionsRight = "Quit" break return questionsRight def getEssentialList(self): """Returns list of essentials for a particular world""" return self.essentials def __str__(self): return self.worldName class Nixoterra(GameWorld): """Child class for Snow World - Nixoterra""" #name = "Nixoterra" qNADictQ = { "Q1: What is the freezing point of snow?\n a. 0 degrees celcius\n b. 30 degrees celcius\n c. 100 degrees celcius\n d. -5 degrees celcius": "d", "Q2: Which ball do we use in the snow ball activity at snow world?\n a. Leather Ball\n b. Foot Ball\n c. Snow Ball\n d. All of the above": "d", "Q3: Which cartoon character does snow world have?\n a. Penguin\n b. Olaf\n c. Snowman\n d. All of the above": "d" } qNADictS1 = { "Q1: Describe a typical workweek for a snowboard instructor\n a. Sleep all week long\n b. Play with Ice\n c. Instruct and provide training to groups/individuals": "c", "Q2: what's your level of experience with edging, waxing and mounting skis and snowboard?\n a. Novice\n b. Proficient\n c. expert": "c" } qNADictS2 = { "Q1: What equipment is/are required for working as an experienced snowmaker\n a. Snow guns\n b. hoses\n c. hydrants\n d. All of the above": "d", "Q2: Can you safely lift 50 lbs\n a. Yes\n b. No": "a" } def __init__(self): self.worldName = "Nixoterra" self.worldType = "SnowWorld" self.essentials = ["Mittens", "Snowboots", "Ski Goggles", "Wool Socks", "Hand warmer"] self.story = "Ice, Chill, Snow, Cold, Freezerealm, Glacia. You name it. \nThey are all names for the mystic Nixoterra. Allow me, Nixette, to guide you through this world. This land is the mother of all snow and ice. Explore this world carefully, because the temperatures are- well, piercing. But other than that, just chill." #ToDo: Fill real name of capital city self.qualifyingQuiz = QualifyingQuiz(Nixoterra.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Nixoterra.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Nixoterra.qNADictS2) class Silvia(GameWorld): """Child class for Forest World - Silvia""" #name = "Silvia" qNADictQ = { "Q1: Can you use a GPS device effectively?\n a. Yes\n b. No": "a", "Q2: Which of the following statement is true?\n a. Forest is on land\n b. Forest is under water": "a", "Q3: What NOT to do in a Forest world?\n a. Start fire\n b. Climb on Tree\n c. Enjoy Nature": "a" } qNADictS1 = { "Q1: Name one of the heavy equipments used in the forest operations\n a. Dump trucks\n b. Toycar\n c. Teddybear": "a", "Q2: Which is NOT one of the 4 forest types?\n a. Temperate\n b. Tropical\n c. Subtropical\n d. Arboreal" : "d" } qNADictS2 = { "Q1: Forest soil is a natural water filter\n a. True\n b. False": "a", "Q2: What is the definition of a forest?\n a. A public green area in a town, used for recreation.\n b. An area rich in biodiversity\n c. A large area of land densely populated by trees\n d. A flat area covered in plants" : "c" } def __init__(self): self.worldName = "Silvia" self.worldType = "ForestWorld" self.essentials = ["Mosquito Repellent", "Backpack", "Poncho", "Hiking boots", "Trail shoes"] self.story = "Hoy there! I'm Bryn, and I will be your personal guide through this wonderful world of trees and plants. \nWelcome to the forest, young explorer! These trees are sacred creatures who descended from the heavens. Enjoy your time in this wonderful place that you can call home" self.qualifyingQuiz = QualifyingQuiz(Silvia.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Silvia.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Silvia.qNADictS2) class Aquamundi(GameWorld): """Child class for Water World - Aquamundi""" #name = "Aquamundi" qNADictQ = { "Q1: Spell Water\n a. Water\n b. Waiter": "a", "Q2: Is water same as wind?\n a. Yes\n b. No": "b", "Q3: Do you know how to swim?\n a. Yes\n b. No": "a" } qNADictS1 = { "Q1: Why do you want to be a Dive instructor?\n a. I like the word Instructor\n b. I love teaching diving": "b", "Q2: Do you have a Captain's license?\n a. Yes\n b. No": "a" } qNADictS2 = { "Q1: In case of any emergency call, what will be your first step?\n a. Ignore\n b. Immediately act on it": "b", "Q2: What is a large natural stream of flowing water that ends in a sea?\n a. Lake\n b. Ocean\n c. Glacier\n d. River": "d" } def __init__(self): self.worldName = "Aquamundi" self.worldType = "WaterWorld" self.essentials = ["Swimcap", "Bandana", "Float suit", "Rash guard", "Alternate Air Source"] self.story = "I see, you chose the water world. Great choice! I'm Aquanna, and I will be your guide in Aquamundi. If you are wondering when this endless ocean ends, don't waste your time. \nThis is a huge world, filled with nothing but water. Let's hop on our big boat and start this adventure!" self.qualifyingQuiz = QualifyingQuiz(Aquamundi.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Aquamundi.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Aquamundi.qNADictS2) class Montelocus(GameWorld): """Child class for Mountain World - Montelocus""" #name = "Montelocus" qNADictQ = { "Q1: Do you know how to use mountain gear?\n a. Yes\n b. No": "a", "Q2: Are you a wayfinder or a lost sheep?\n a. Wayfinder\n b. Lostsheep": "a", "Q3: Are you afraid of heights?\n a. Yes\n b. No": "b" } qNADictS1 = { "Q1: What would you select to climb in tricky mountain conditions?\n a. Mountaineering boots\n b. mountains": "a", "Q2: What would you do if someone calls for help?\n a. It's their problem, I will just ignore\n b. Go and help them": "b" } qNADictS2 = { "Q1: Which of the following is the highest part of a mountain?\n a. Peak\n b. Base\n c. Slope\n d. None of the above": "a", "Q2: What should you do if you fall off a mountain during Climbing?\n a. Have a quick snack\n b. Make a quick phone call\n c. Take a selfie and post it on instagram\n d. Use a harness": "d" } def __init__(self): self.worldName = "Montelocus" self.worldType = "MountainWorld" self.essentials = ["Climbing Helmet", "Ropes", "Harness", "Mountaineering Boots", "Crampons"] self.story = "Isn't it beautiful, the divine mountain range of Montelocus. Oh hey! I didn't see you there! I'm Cashel, and I will lead you through the wonders of this rocky world, with mountains everywhere. \nThese mountains are said to be a gift from the sky. Take a moment to appreciate this particularly beautiful aspect of nature! \nAnyway, see you around!" self.qualifyingQuiz = QualifyingQuiz(Montelocus.qNADictQ) self.survivalQuiz1 = SurvivalQuiz(Montelocus.qNADictS1) self.survivalQuiz2 = SurvivalQuiz(Montelocus.qNADictS2) class User: """Class for storing user information and tracking user activity throughout multiple stages of the game""" def __init__(self, name, location, system, coins=100): self.name = name self.location = location # not sure if we need it self.system = system self.coins = coins self.gems = 0 self.assets = [] def __str__(self): return self.name def collectGem(self): """Increments gem count of user by 1, if the user earns gem""" self.gems += 1 def buyGem(self, amount): """Increments gem count of user by 1 if the buys the gem by spending coins""" returnVal = False if self.spendCoin(amount=25): self.gems += 1 returnVal = True return returnVal def earnCoin(self, amount): """Increments count of coins by a certain amount""" self.coins += amount def spendCoin(self, amount): """Decreases count of coins by a certain amount""" returnVal = False if self.coins >= amount: self.coins -= amount returnVal = True return returnVal def getCoins(self): """Returns number of coins of the user""" return self.coins def getGems(self): """Returns number of gems of the user""" return self.gems def buyEssentials(self, amount=5): """Returns list of essential items bought by the user for the selected world and displays the list of user's assets.""" ls = self.location.getEssentialList() print(f"{bcolors.WHITE}\nGreat job so far! Now, it's time to arm yourself with some essentials you would need to survive in the {self.location.worldType}.{bcolors.ENDC}") print(f"\n{bcolors.CYAN}Following are the essential items for {self.location.worldName}. Please choose a minimum of 3 items to proceed.{bcolors.ENDC}") outputList = [str(i+1) + ". " + ls[i] + "\n" for i in range(len(ls))] print(f"\n{bcolors.CYAN}{''.join(outputList)}{bcolors.ENDC}") sizeEssentialList = len(ls) essentialsList = [] choiceInput = False while choiceInput is False: choices = input(f"{bcolors.CYAN}Input your selection as numbers 1, 2, 3, 4, or 5 separated by comma: {bcolors.ENDC}") choiceInput = True choices = choices.split(',') for choice in choices: if choice not in ('1', '2', '3', '4', '5', 'quit', 'QUIT', 'Quit'): print(f"\n{bcolors.PINK}Please enter a valid Input{bcolors.ENDC}\n") choiceInput = False break for choice in choices: if choice.capitalize() == "Quit": # User input "Quit" at this stage. So, just quit the game. return choices try: # Convert input to integer for index in essentialList item choices = [int(i) for i in choices] except ValueError: # If input is not a number, Quit gracefully! print("Input is not a number. Quit") return essentialsList if max(choices) > sizeEssentialList: print(f"Invalid input! Input is not in essentialList") return essentialsList for j in choices: if self.spendCoin(amount): essentialsList.append(ls[j-1]) else: print(f"You don't have enough money to buy {j}. You only have {self.coins} coins left.") break self.assets = essentialsList print(f"\n{bcolors.WHITE}Thank you for buying the essentials. Now you are officially ready to enter into the {self.location.worldType}.\nHere is your current asset bag with essential items and the available coins.{bcolors.ENDC}") print(f"\n{bcolors.YELLOW}Asset Bag Contents: {self.assets}\nCoins: {self.coins}{bcolors.ENDC}") return self.assets def getAssets(self): """Returns list of assets of the user""" return self.assets def takeSurvivalQuiz(self, quiz): """Returns the result of survival quiz""" if quiz == 'survivalQuiz1': print(f"\n{bcolors.WHITE}Starting Survival quiz1 for {self.location.worldName} in 3 2 1....{bcolors.ENDC}") s(2) print(f"\n{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd': {bcolors.ENDC}") userSurvival = [(i, self.location.survivalQuiz1.questionAndAnswer.get(i)) for i in self.location.survivalQuiz1.questionAndAnswer.keys()] elif quiz == 'survivalQuiz2': print(f"\n{bcolors.WHITE}Starting Survival quiz2 for {self.location.worldName} in 3 2 1....{bcolors.ENDC}") s(2) #print(f"\n{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd': {bcolors.ENDC}") userSurvival = [(i, self.location.survivalQuiz2.questionAndAnswer.get(i)) for i in self.location.survivalQuiz2.questionAndAnswer.keys()] else: # Wrong argument, should not have reached here. Return anyway! userSurvival = "Quit" survivalResults = self.location.survivalQuizMethod(userSurvival) return survivalResults def navigateToCapitalcity(self, amount=20): """Increments number if gems by 1 if the user succesfully navigates to the capital city""" navSuccess = False if self.spendCoin(amount): self.gems += 1 navSuccess = True return navSuccess class GameSystem: """This is the game engine where other classes are ingested. This class controls the whole game system and stores system information""" def __init__(self): self.coins = 100 self.name = "Gateway To Adventure" self.minCorrectAnswers = 2 self.minGemsWin = 4 self.minCoinsWin = 25 self.minGemForCapitalCity = 2 self.buyGemAmount = 25 self.minEssentials = 3 self.maxAssetNum = 5 self.survivalWinCoins = 10 self.userName = "Empty" def __str__(self): return self.name def getUserLocation(self, userName, worldList): """Returns user location after User selects a world""" validInput = False itemizedWorldList = [ "\n" + str(i+1) + ". " + worldList[i] for i in range(len(worldList))] while validInput is False: print(f"{bcolors.CYAN}Choose a world from the following: {bcolors.BOLD}{''.join(itemizedWorldList)}{bcolors.ENDC}") userLocation = input(f"{bcolors.CYAN}Please input the exact name of the world: {bcolors.ENDC}")
if userLocation == "Quit": validInput = True else: print(f"{bcolors.FAIL}Invalid world name! Please enter a correct World Name {bcolors.ENDC}") validInput = False return userLocation def selectWorld(self, userLocation, userName, availableWorldList): """Returns world type and the result of qualifying quiz, after User selects the world""" gameReturn = False if userLocation in availableWorldList: if userLocation == "Nixoterra": world = Nixoterra() elif userLocation == "Silvia": world = Silvia() elif userLocation == "Aquamundi": world = Aquamundi() elif userLocation == "Montelocus": world = Montelocus() else: # Should not have reached here! print(f"{bcolors.FAIL}Quitting as {userName} wants to quit the game.{bcolors.ENDC}") return gameReturn print(f"{bcolors.WHITE}Thank you for choosing the {world.worldType}.\n\n-------------\n{world.story}\n-------------\nNow, Let's see if you are ready for the selected world :-)\n\nStarting Qualifying quiz for {userLocation} in 3 2 1...{bcolors.ENDC}\n") s(2) print(f"{bcolors.CYAN}To answer the following questions, input your choice by entering 'a', 'b', 'c', or 'd'{bcolors.ENDC}") userQualifying = [(i, world.qualifyingQuiz.questionAndAnswer.get(i)) for i in world.qualifyingQuiz.questionAndAnswer.keys()] qualifyingResults = world.qualifyingQuizMethod(userQualifying) if self.checkReturn(qualifyingResults, userName): return gameReturn else: print(f"{bcolors.FAIL}You have already failed in the Qualifying quiz of {userLocation}. You can't reattempt thw Qualifyiong quiz.{bcolors.ENDC}") return world, qualifyingResults def checkReturn(self, returnVal, name): """Returns True if user enters 'quit' at any stage of the game""" quitGame = False if str(returnVal).capitalize() == "Quit": print(f"{bcolors.FAIL}Quitting as {name} wants to quit the game.{bcolors.ENDC}") # User entered Quit, exit and return accordingly quitGame = True return quitGame def evaluateSurvialQuiz(self, player): """Evaluates and displays result of survival quiz and also displays the user's current asset""" print(f"\n{bcolors.WHITE}Let's start earning some more coins by answering survival questions that will help you survive in the world{bcolors.ENDC}") quizList = ['survivalQuiz1', 'survivalQuiz2'] perkReattempt = True for quiz in quizList: survivalResults = player.takeSurvivalQuiz(quiz) if self.checkReturn(survivalResults, player.name): return False if not survivalResults: print(f"{bcolors.FAIL}Sorry, you failed.{bcolors.ENDC}") if len(player.getAssets()) > self.minEssentials and perkReattempt: print(f"{bcolors.WHITE}You are eligible for 1 reattempt.{bcolors.ENDC}") perkReattempt = False survivalResults = player.takeSurvivalQuiz(quiz) if self.checkReturn(survivalResults, player.name): return False if not survivalResults: print(f"{bcolors.FAIL}Sorry, you failed again.{bcolors.ENDC}") else: player.earnCoin(self.survivalWinCoins * survivalResults) player.collectGem() print(f"{bcolors.YELLOW}Congrats! {player.name}, you have cleared {quiz}{bcolors.ENDC}") #print(f"current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}") else: player.earnCoin(self.survivalWinCoins * survivalResults) player.collectGem() print(f"{bcolors.YELLOW}Congrats {player.name}, you have cleared {quiz}{bcolors.ENDC}") print(f"{bcolors.YELLOW}current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}{bcolors.ENDC}") return True def gameStart(self): """Returns true if the user successfully completes the game and handles invalid user input. Also, starts the game and create instances of user and world""" print(f"{bcolors.WHITE}Hello, and welcome to the {bcolors.BOLD}Gateway of Adventure!{bcolors.ENDC} {bcolors.WHITE} Let's begin the fun ride!\nYou can quit the game at any stage of the game by typing Quit. Good Luck!{bcolors.ENDC}") gameReturn = False worldList = ["Nixoterra", "Silvia", "Aquamundi", "Montelocus"] userInput = input(f"Would you like to view the instruction manual? (y/n): ") while userInput.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") userInput = input(f"Would you like to view the instruction manual? (y/n): ") if userInput.lower() == "y": print(f"\n{bcolors.WHITE}Open this link to view the Instruction Manual:\n{bcolors.BLUE}{bcolors.UNDERLINE}https://github.prod.oc.2u.com/UCB-INFO-PYTHON/Jyoti_KumariREPO/blob/master/SUBMISSIONS/project_1/Gateway%20to%20Adventure%20-%20%20Instruction%20Manual.pdf{bcolors.ENDC}") elif userInput.lower() == "quit": self.checkReturn(userInput, "guest") return gameReturn if game.userName == "Empty": userName = input(f"\n{bcolors.CYAN}What is your name? {bcolors.ENDC}") self.userName = userName else: userName = self.userName #print(f"{bcolors.FAIL}You can type Quit to quit at any stage of the game. {bcolors.ENDC}") userLocation = self.getUserLocation(userName, worldList) if not userLocation: return gameReturn #userLocation = userLocation.lower() if self.checkReturn(userLocation, userName): return gameReturn # Valid User Location found # Let's select the world and do the Qualifying quiz for the world loopReturn = False while True: world, qualifyingResults = self.selectWorld(userLocation, userName, worldList) if not world: loopReturn = False break print(f"\n{bcolors.YELLOW}{userName}, you got {qualifyingResults} questions right.{bcolors.ENDC}\n") if qualifyingResults < self.minCorrectAnswers: # User failed the qualifying quiz print(f"{bcolors.FAIL}You failed in Qualifying quiz of {userLocation} {bcolors.ENDC}") worldList.remove(userLocation) if len(worldList): print(f"{bcolors.WHITE}\nTry selecting another world to proceed. You can type Quit to quit at any stage of the game.{bcolors.ENDC}\n") userLocation = self.getUserLocation(userName, worldList) if not userLocation: loopReturn = False break if self.checkReturn(userLocation, userName): loopReturn = False break else: loopReturn = False break else: loopReturn = True print(f"{bcolors.YELLOW}Congratulations! you now qualify to enter {userLocation}\n\n{world.story}\n\nYou have just won 100 coins!{bcolors.ENDC}") break if not loopReturn: return gameReturn # User Qualified the quiz for the world # Let's initialize Classes for World and player player = User(userName, world, self) #print(f"worldName: {world.worldName}\nworldType: {world.worldType}\n") #print(f"Player Name: {player.name}\nPlayer Location: {player.location}\nPlayer Coins: {player.getCoins()}\n" ) essentialsList = player.buyEssentials() if not essentialsList: # No item selected from the essential list # Player can't survive in the world and Exit the world with message print(f"{bcolors.FAIL}Quitting as {userName} didn't select any essential items to enter the new world.{bcolors.ENDC}") return gameReturn if self.checkReturn(essentialsList, userName): return gameReturn #print(f"{userName} Assets: ", player.getAssets()) if len(player.getAssets()) < self.minEssentials: print(f"{bcolors.FAIL}{userName} needs to select at least {self.minEssentials} from the list.{bcolors.ENDC}") return gameReturn userInput = input(f"\n{bcolors.WHITE}{userName}, you have successfully entered into the {world.worldType}. As a next step, you would need to buy a gem for 25 coins to further explore this world.\nDo you want to proceed (y/n)?: {bcolors.ENDC}") while userInput.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") userInput = input(f"\n{bcolors.WHITE}{userName}, you have successfully entered into the {world.worldType}. As a next step, you would need to buy a gem for 25 coins to further explore this world.\nDo you want to proceed (y/n)?: {bcolors.ENDC}") if userInput.lower() == 'y': if player.buyGem(self.buyGemAmount): print(f"{bcolors.YELLOW}Thank you {userName}, You now have {player.getGems()} gems and {player.getCoins()} coins{bcolors.ENDC}") else: self.checkReturn("quit", userName) return gameReturn s(2) # Conduct and Evaluate Survial Quiz for player if not self.evaluateSurvialQuiz(player): return gameReturn # Navigate to Capital City if player.getGems() >= self.minGemForCapitalCity: print(f"{bcolors.WHITE}\nYay! you are a rockstar! Let's proceed to the next stage of the your journey by going to the very beautiful Capital City of {world.worldName}.{bcolors.ENDC}") s(2) print(f"{bcolors.WHITE}\nEntering into the Capital City 3 2 1....{bcolors.ENDC}\n") s(2) if len(player.getAssets()) == self.maxAssetNum : player.navigateToCapitalcity(10) else: player.navigateToCapitalcity() print(f"{bcolors.YELLOW}{userName} has reached the capital city of {userLocation}{bcolors.ENDC}") print(f"{bcolors.YELLOW}current coins: {player.getCoins()}\ncurrent Gems:{player.getGems()}{bcolors.ENDC}") response = input(f"\n{bcolors.WHITE}Do you want to buy addditional Gem for 25 coins? (y/n) {bcolors.ENDC}").lower() while response.lower() not in ('y', 'n', 'quit'): print(f"\n{bcolors.PINK}Please enter a valid input{bcolors.ENDC}\n") response = input(f"\n{bcolors.WHITE}Do you want to buy addditional Gem for 25 coins? (y/n) {bcolors.ENDC}").lower() if response == 'y': player.buyGem(25) print(f"user bought one gem") elif response == 'n': print(f"{bcolors.WHITE}{userName} declined offer{bcolors.ENDC}\n") else: self.checkReturn("quit", userName) return gameReturn # Check if Player has enough gems to win the game if player.getGems() >= self.minGemsWin: gameReturn = True # Check if Player has enough coins to win the game if player.getCoins() >= self.minCoinsWin: print(f"{bcolors.GREEN}{userName} has {player.getCoins()} coins and {player.getGems()} gems!") print(f"{bcolors.GREEN}Congratulations {userName}, you Won!{bcolors.ENDC}") else: print(f"{bcolors.PINK}Sorry {userName}, you lost.") print(f"{userName} has {player.getCoins()} coins left") print(f"You needed {self.minCoinsWin} coins to win the game!{bcolors.ENDC}") else: print(f"{bcolors.PINK}Sorry {userName}, you lost.") print(f"{userName} has {player.getCoins()} coins left") print(f"You needed {self.minGemsWin} gems to win the game!{bcolors.ENDC}") gameReturn = True return gameReturn def gameQuit(self): "Displays Goodbye message if the user has selected to quit the game" print(f"{bcolors.PINK}Goodbye {self.userName}, see you again!{bcolors.ENDC}") parser = argparse.ArgumentParser(description = 'Step 1: Select a world --> Step 2: Take Qualifying quiz --> Step 3: Buy essentials(a minimum of 3 items) --> Step 4: Enter World and take 2 survival quizzes, one by one --> Step 5: Go to Capital City to get a gem or buy a gem --> Step 6: You win or lose depending on, if you have 25 coins and 4 gems at a minimum') args = parser.parse_args() game = GameSystem() gameRestart = False gameRestart = game.gameStart() while gameRestart: gameRestart = input(f"\nDo you want to replay the Game!? (y/n): ") if gameRestart.lower() == 'y': gameRestart = game.gameStart() else: gameRestart = False game.gameQuit()
userLocation = userLocation.capitalize() if userLocation in worldList: validInput = True else:
random_line_split
eqivalence.go
/* Copyright 2016 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Package equivalence defines Pod equivalence classes and the equivalence class // cache. package equivalence import ( "fmt" "hash/fnv" "sync" "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/kubernetes/pkg/scheduler/algorithm" "k8s.io/kubernetes/pkg/scheduler/algorithm/predicates" schedulercache "k8s.io/kubernetes/pkg/scheduler/cache" hashutil "k8s.io/kubernetes/pkg/util/hash" "github.com/golang/glog" ) // Cache saves and reuses the output of predicate functions. Use RunPredicate to // get or update the cached results. An appropriate Invalidate* function should // be called when some predicate results are no longer valid. // // Internally, results are keyed by node name, predicate name, and "equivalence // class". (Equivalence class is defined in the `Class` type.) Saved results // will be reused until an appropriate invalidation function is called. type Cache struct { mu sync.RWMutex cache nodeMap } // NewCache returns an empty Cache. func NewCache() *Cache { return &Cache{ cache: make(nodeMap), } } // Class represents a set of pods which are equivalent from the perspective of // the scheduler. i.e. the scheduler would make the same decision for any pod // from the same class. type Class struct { // Equivalence hash hash uint64 } // NewClass returns the equivalence class for a given Pod. The returned Class // objects will be equal for two Pods in the same class. nil values should not // be considered equal to each other. // // NOTE: Make sure to compare types of Class and not *Class. // TODO(misterikkit): Return error instead of nil *Class. func NewClass(pod *v1.Pod) *Class { equivalencePod := getEquivalencePod(pod) if equivalencePod != nil { hash := fnv.New32a() hashutil.DeepHashObject(hash, equivalencePod) return &Class{ hash: uint64(hash.Sum32()), } } return nil } // nodeMap stores PredicateCaches with node name as the key. type nodeMap map[string]predicateMap // predicateMap stores resultMaps with predicate name as the key. type predicateMap map[string]resultMap // resultMap stores PredicateResult with pod equivalence hash as the key. type resultMap map[uint64]predicateResult // predicateResult stores the output of a FitPredicate. type predicateResult struct { Fit bool FailReasons []algorithm.PredicateFailureReason } // RunPredicate returns a cached predicate result. In case of a cache miss, the predicate will be // run and its results cached for the next call. // // NOTE: RunPredicate will not update the equivalence cache if the given NodeInfo is stale. func (c *Cache) RunPredicate( pred algorithm.FitPredicate, predicateKey string, pod *v1.Pod, meta algorithm.PredicateMetadata, nodeInfo *schedulercache.NodeInfo, equivClass *Class, cache schedulercache.Cache, ) (bool, []algorithm.PredicateFailureReason, error) { if nodeInfo == nil || nodeInfo.Node() == nil { // This may happen during tests. return false, []algorithm.PredicateFailureReason{}, fmt.Errorf("nodeInfo is nil or node is invalid") } result, ok := c.lookupResult(pod.GetName(), nodeInfo.Node().GetName(), predicateKey, equivClass.hash) if ok { return result.Fit, result.FailReasons, nil } fit, reasons, err := pred(pod, meta, nodeInfo) if err != nil { return fit, reasons, err } if cache != nil { c.updateResult(pod.GetName(), predicateKey, fit, reasons, equivClass.hash, cache, nodeInfo) } return fit, reasons, nil } // updateResult updates the cached result of a predicate. func (c *Cache) updateResult( podName, predicateKey string, fit bool, reasons []algorithm.PredicateFailureReason, equivalenceHash uint64, cache schedulercache.Cache, nodeInfo *schedulercache.NodeInfo, ) { c.mu.Lock() defer c.mu.Unlock() if nodeInfo == nil || nodeInfo.Node() == nil { // This may happen during tests. return } // Skip update if NodeInfo is stale. if !cache.IsUpToDate(nodeInfo) { return } nodeName := nodeInfo.Node().GetName() if _, exist := c.cache[nodeName]; !exist { c.cache[nodeName] = make(predicateMap) } predicateItem := predicateResult{ Fit: fit, FailReasons: reasons, } // if cached predicate map already exists, just update the predicate by key if predicates, ok := c.cache[nodeName][predicateKey]; ok { // maps in golang are references, no need to add them back predicates[equivalenceHash] = predicateItem } else { c.cache[nodeName][predicateKey] = resultMap{ equivalenceHash: predicateItem, } } glog.V(5).Infof("Cache update: node=%s,predicate=%s,pod=%s,value=%v", nodeName, predicateKey, podName, predicateItem) } // lookupResult returns cached predicate results and a bool saying whether a // cache entry was found. func (c *Cache) lookupResult( podName, nodeName, predicateKey string, equivalenceHash uint64, ) (value predicateResult, ok bool) { c.mu.RLock() defer c.mu.RUnlock() glog.V(5).Infof("Cache lookup: node=%s,predicate=%s,pod=%s", nodeName, predicateKey, podName) value, ok = c.cache[nodeName][predicateKey][equivalenceHash] return value, ok } // InvalidatePredicates clears all cached results for the given predicates. func (c *Cache) InvalidatePredicates(predicateKeys sets.String)
// InvalidatePredicatesOnNode clears cached results for the given predicates on one node. func (c *Cache) InvalidatePredicatesOnNode(nodeName string, predicateKeys sets.String) { if len(predicateKeys) == 0 { return } c.mu.Lock() defer c.mu.Unlock() for predicateKey := range predicateKeys { delete(c.cache[nodeName], predicateKey) } glog.V(5).Infof("Cache invalidation: node=%s,predicates=%v", nodeName, predicateKeys) } // InvalidateAllPredicatesOnNode clears all cached results for one node. func (c *Cache) InvalidateAllPredicatesOnNode(nodeName string) { c.mu.Lock() defer c.mu.Unlock() delete(c.cache, nodeName) glog.V(5).Infof("Cache invalidation: node=%s,predicates=*", nodeName) } // InvalidateCachedPredicateItemForPodAdd is a wrapper of // InvalidateCachedPredicateItem for pod add case // TODO: This does not belong with the equivalence cache implementation. func (c *Cache) InvalidateCachedPredicateItemForPodAdd(pod *v1.Pod, nodeName string) { // MatchInterPodAffinity: we assume scheduler can make sure newly bound pod // will not break the existing inter pod affinity. So we does not need to // invalidate MatchInterPodAffinity when pod added. // // But when a pod is deleted, existing inter pod affinity may become invalid. // (e.g. this pod was preferred by some else, or vice versa) // // NOTE: assumptions above will not stand when we implemented features like // RequiredDuringSchedulingRequiredDuringExecution. // NoDiskConflict: the newly scheduled pod fits to existing pods on this node, // it will also fits to equivalence class of existing pods // GeneralPredicates: will always be affected by adding a new pod invalidPredicates := sets.NewString(predicates.GeneralPred) // MaxPDVolumeCountPredicate: we check the volumes of pod to make decision. for _, vol := range pod.Spec.Volumes { if vol.PersistentVolumeClaim != nil { invalidPredicates.Insert(predicates.MaxEBSVolumeCountPred, predicates.MaxGCEPDVolumeCountPred, predicates.MaxAzureDiskVolumeCountPred) } else { if vol.AWSElasticBlockStore != nil { invalidPredicates.Insert(predicates.MaxEBSVolumeCountPred) } if vol.GCEPersistentDisk != nil { invalidPredicates.Insert(predicates.MaxGCEPDVolumeCountPred) } if vol.AzureDisk != nil { invalidPredicates.Insert(predicates.MaxAzureDiskVolumeCountPred) } } } c.InvalidatePredicatesOnNode(nodeName, invalidPredicates) } // equivalencePod is the set of pod attributes which must match for two pods to // be considered equivalent for scheduling purposes. For correctness, this must // include any Pod field which is used by a FitPredicate. // // NOTE: For equivalence hash to be formally correct, lists and maps in the // equivalencePod should be normalized. (e.g. by sorting them) However, the vast // majority of equivalent pod classes are expected to be created from a single // pod template, so they will all have the same ordering. type equivalencePod struct { Namespace *string Labels map[string]string Affinity *v1.Affinity Containers []v1.Container // See note about ordering InitContainers []v1.Container // See note about ordering NodeName *string NodeSelector map[string]string Tolerations []v1.Toleration Volumes []v1.Volume // See note about ordering } // getEquivalencePod returns a normalized representation of a pod so that two // "equivalent" pods will hash to the same value. func getEquivalencePod(pod *v1.Pod) *equivalencePod { ep := &equivalencePod{ Namespace: &pod.Namespace, Labels: pod.Labels, Affinity: pod.Spec.Affinity, Containers: pod.Spec.Containers, InitContainers: pod.Spec.InitContainers, NodeName: &pod.Spec.NodeName, NodeSelector: pod.Spec.NodeSelector, Tolerations: pod.Spec.Tolerations, Volumes: pod.Spec.Volumes, } // DeepHashObject considers nil and empty slices to be different. Normalize them. if len(ep.Containers) == 0 { ep.Containers = nil } if len(ep.InitContainers) == 0 { ep.InitContainers = nil } if len(ep.Tolerations) == 0 { ep.Tolerations = nil } if len(ep.Volumes) == 0 { ep.Volumes = nil } // Normalize empty maps also. if len(ep.Labels) == 0 { ep.Labels = nil } if len(ep.NodeSelector) == 0 { ep.NodeSelector = nil } // TODO(misterikkit): Also normalize nested maps and slices. return ep }
{ if len(predicateKeys) == 0 { return } c.mu.Lock() defer c.mu.Unlock() // c.cache uses nodeName as key, so we just iterate it and invalid given predicates for _, predicates := range c.cache { for predicateKey := range predicateKeys { delete(predicates, predicateKey) } } glog.V(5).Infof("Cache invalidation: node=*,predicates=%v", predicateKeys) }
identifier_body
eqivalence.go
/* Copyright 2016 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Package equivalence defines Pod equivalence classes and the equivalence class // cache. package equivalence import ( "fmt" "hash/fnv" "sync" "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/kubernetes/pkg/scheduler/algorithm" "k8s.io/kubernetes/pkg/scheduler/algorithm/predicates" schedulercache "k8s.io/kubernetes/pkg/scheduler/cache" hashutil "k8s.io/kubernetes/pkg/util/hash" "github.com/golang/glog" ) // Cache saves and reuses the output of predicate functions. Use RunPredicate to // get or update the cached results. An appropriate Invalidate* function should // be called when some predicate results are no longer valid. // // Internally, results are keyed by node name, predicate name, and "equivalence // class". (Equivalence class is defined in the `Class` type.) Saved results // will be reused until an appropriate invalidation function is called. type Cache struct { mu sync.RWMutex cache nodeMap } // NewCache returns an empty Cache. func NewCache() *Cache { return &Cache{ cache: make(nodeMap), } } // Class represents a set of pods which are equivalent from the perspective of // the scheduler. i.e. the scheduler would make the same decision for any pod // from the same class. type Class struct { // Equivalence hash hash uint64 } // NewClass returns the equivalence class for a given Pod. The returned Class // objects will be equal for two Pods in the same class. nil values should not // be considered equal to each other. // // NOTE: Make sure to compare types of Class and not *Class. // TODO(misterikkit): Return error instead of nil *Class. func NewClass(pod *v1.Pod) *Class { equivalencePod := getEquivalencePod(pod) if equivalencePod != nil { hash := fnv.New32a() hashutil.DeepHashObject(hash, equivalencePod) return &Class{ hash: uint64(hash.Sum32()), } } return nil } // nodeMap stores PredicateCaches with node name as the key. type nodeMap map[string]predicateMap // predicateMap stores resultMaps with predicate name as the key. type predicateMap map[string]resultMap // resultMap stores PredicateResult with pod equivalence hash as the key. type resultMap map[uint64]predicateResult // predicateResult stores the output of a FitPredicate. type predicateResult struct { Fit bool FailReasons []algorithm.PredicateFailureReason } // RunPredicate returns a cached predicate result. In case of a cache miss, the predicate will be // run and its results cached for the next call. // // NOTE: RunPredicate will not update the equivalence cache if the given NodeInfo is stale. func (c *Cache) RunPredicate( pred algorithm.FitPredicate, predicateKey string, pod *v1.Pod, meta algorithm.PredicateMetadata, nodeInfo *schedulercache.NodeInfo, equivClass *Class, cache schedulercache.Cache, ) (bool, []algorithm.PredicateFailureReason, error) { if nodeInfo == nil || nodeInfo.Node() == nil { // This may happen during tests. return false, []algorithm.PredicateFailureReason{}, fmt.Errorf("nodeInfo is nil or node is invalid") } result, ok := c.lookupResult(pod.GetName(), nodeInfo.Node().GetName(), predicateKey, equivClass.hash) if ok { return result.Fit, result.FailReasons, nil } fit, reasons, err := pred(pod, meta, nodeInfo) if err != nil { return fit, reasons, err } if cache != nil { c.updateResult(pod.GetName(), predicateKey, fit, reasons, equivClass.hash, cache, nodeInfo) } return fit, reasons, nil } // updateResult updates the cached result of a predicate. func (c *Cache) updateResult( podName, predicateKey string, fit bool, reasons []algorithm.PredicateFailureReason, equivalenceHash uint64, cache schedulercache.Cache, nodeInfo *schedulercache.NodeInfo, ) { c.mu.Lock() defer c.mu.Unlock() if nodeInfo == nil || nodeInfo.Node() == nil { // This may happen during tests. return } // Skip update if NodeInfo is stale. if !cache.IsUpToDate(nodeInfo) { return } nodeName := nodeInfo.Node().GetName() if _, exist := c.cache[nodeName]; !exist { c.cache[nodeName] = make(predicateMap) } predicateItem := predicateResult{ Fit: fit, FailReasons: reasons, } // if cached predicate map already exists, just update the predicate by key if predicates, ok := c.cache[nodeName][predicateKey]; ok { // maps in golang are references, no need to add them back predicates[equivalenceHash] = predicateItem } else { c.cache[nodeName][predicateKey] = resultMap{ equivalenceHash: predicateItem, } } glog.V(5).Infof("Cache update: node=%s,predicate=%s,pod=%s,value=%v", nodeName, predicateKey, podName, predicateItem) } // lookupResult returns cached predicate results and a bool saying whether a // cache entry was found. func (c *Cache) lookupResult( podName, nodeName, predicateKey string, equivalenceHash uint64, ) (value predicateResult, ok bool) { c.mu.RLock() defer c.mu.RUnlock() glog.V(5).Infof("Cache lookup: node=%s,predicate=%s,pod=%s", nodeName, predicateKey, podName) value, ok = c.cache[nodeName][predicateKey][equivalenceHash] return value, ok } // InvalidatePredicates clears all cached results for the given predicates. func (c *Cache) InvalidatePredicates(predicateKeys sets.String) { if len(predicateKeys) == 0 { return } c.mu.Lock() defer c.mu.Unlock() // c.cache uses nodeName as key, so we just iterate it and invalid given predicates for _, predicates := range c.cache { for predicateKey := range predicateKeys { delete(predicates, predicateKey) } } glog.V(5).Infof("Cache invalidation: node=*,predicates=%v", predicateKeys) } // InvalidatePredicatesOnNode clears cached results for the given predicates on one node. func (c *Cache) InvalidatePredicatesOnNode(nodeName string, predicateKeys sets.String) { if len(predicateKeys) == 0 { return } c.mu.Lock() defer c.mu.Unlock() for predicateKey := range predicateKeys { delete(c.cache[nodeName], predicateKey) } glog.V(5).Infof("Cache invalidation: node=%s,predicates=%v", nodeName, predicateKeys) } // InvalidateAllPredicatesOnNode clears all cached results for one node. func (c *Cache) InvalidateAllPredicatesOnNode(nodeName string) { c.mu.Lock() defer c.mu.Unlock() delete(c.cache, nodeName) glog.V(5).Infof("Cache invalidation: node=%s,predicates=*", nodeName) } // InvalidateCachedPredicateItemForPodAdd is a wrapper of // InvalidateCachedPredicateItem for pod add case // TODO: This does not belong with the equivalence cache implementation. func (c *Cache) InvalidateCachedPredicateItemForPodAdd(pod *v1.Pod, nodeName string) { // MatchInterPodAffinity: we assume scheduler can make sure newly bound pod // will not break the existing inter pod affinity. So we does not need to // invalidate MatchInterPodAffinity when pod added. // // But when a pod is deleted, existing inter pod affinity may become invalid. // (e.g. this pod was preferred by some else, or vice versa) // // NOTE: assumptions above will not stand when we implemented features like // RequiredDuringSchedulingRequiredDuringExecution.
// NoDiskConflict: the newly scheduled pod fits to existing pods on this node, // it will also fits to equivalence class of existing pods // GeneralPredicates: will always be affected by adding a new pod invalidPredicates := sets.NewString(predicates.GeneralPred) // MaxPDVolumeCountPredicate: we check the volumes of pod to make decision. for _, vol := range pod.Spec.Volumes { if vol.PersistentVolumeClaim != nil { invalidPredicates.Insert(predicates.MaxEBSVolumeCountPred, predicates.MaxGCEPDVolumeCountPred, predicates.MaxAzureDiskVolumeCountPred) } else { if vol.AWSElasticBlockStore != nil { invalidPredicates.Insert(predicates.MaxEBSVolumeCountPred) } if vol.GCEPersistentDisk != nil { invalidPredicates.Insert(predicates.MaxGCEPDVolumeCountPred) } if vol.AzureDisk != nil { invalidPredicates.Insert(predicates.MaxAzureDiskVolumeCountPred) } } } c.InvalidatePredicatesOnNode(nodeName, invalidPredicates) } // equivalencePod is the set of pod attributes which must match for two pods to // be considered equivalent for scheduling purposes. For correctness, this must // include any Pod field which is used by a FitPredicate. // // NOTE: For equivalence hash to be formally correct, lists and maps in the // equivalencePod should be normalized. (e.g. by sorting them) However, the vast // majority of equivalent pod classes are expected to be created from a single // pod template, so they will all have the same ordering. type equivalencePod struct { Namespace *string Labels map[string]string Affinity *v1.Affinity Containers []v1.Container // See note about ordering InitContainers []v1.Container // See note about ordering NodeName *string NodeSelector map[string]string Tolerations []v1.Toleration Volumes []v1.Volume // See note about ordering } // getEquivalencePod returns a normalized representation of a pod so that two // "equivalent" pods will hash to the same value. func getEquivalencePod(pod *v1.Pod) *equivalencePod { ep := &equivalencePod{ Namespace: &pod.Namespace, Labels: pod.Labels, Affinity: pod.Spec.Affinity, Containers: pod.Spec.Containers, InitContainers: pod.Spec.InitContainers, NodeName: &pod.Spec.NodeName, NodeSelector: pod.Spec.NodeSelector, Tolerations: pod.Spec.Tolerations, Volumes: pod.Spec.Volumes, } // DeepHashObject considers nil and empty slices to be different. Normalize them. if len(ep.Containers) == 0 { ep.Containers = nil } if len(ep.InitContainers) == 0 { ep.InitContainers = nil } if len(ep.Tolerations) == 0 { ep.Tolerations = nil } if len(ep.Volumes) == 0 { ep.Volumes = nil } // Normalize empty maps also. if len(ep.Labels) == 0 { ep.Labels = nil } if len(ep.NodeSelector) == 0 { ep.NodeSelector = nil } // TODO(misterikkit): Also normalize nested maps and slices. return ep }
random_line_split
eqivalence.go
/* Copyright 2016 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Package equivalence defines Pod equivalence classes and the equivalence class // cache. package equivalence import ( "fmt" "hash/fnv" "sync" "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/kubernetes/pkg/scheduler/algorithm" "k8s.io/kubernetes/pkg/scheduler/algorithm/predicates" schedulercache "k8s.io/kubernetes/pkg/scheduler/cache" hashutil "k8s.io/kubernetes/pkg/util/hash" "github.com/golang/glog" ) // Cache saves and reuses the output of predicate functions. Use RunPredicate to // get or update the cached results. An appropriate Invalidate* function should // be called when some predicate results are no longer valid. // // Internally, results are keyed by node name, predicate name, and "equivalence // class". (Equivalence class is defined in the `Class` type.) Saved results // will be reused until an appropriate invalidation function is called. type Cache struct { mu sync.RWMutex cache nodeMap } // NewCache returns an empty Cache. func NewCache() *Cache { return &Cache{ cache: make(nodeMap), } } // Class represents a set of pods which are equivalent from the perspective of // the scheduler. i.e. the scheduler would make the same decision for any pod // from the same class. type Class struct { // Equivalence hash hash uint64 } // NewClass returns the equivalence class for a given Pod. The returned Class // objects will be equal for two Pods in the same class. nil values should not // be considered equal to each other. // // NOTE: Make sure to compare types of Class and not *Class. // TODO(misterikkit): Return error instead of nil *Class. func NewClass(pod *v1.Pod) *Class { equivalencePod := getEquivalencePod(pod) if equivalencePod != nil { hash := fnv.New32a() hashutil.DeepHashObject(hash, equivalencePod) return &Class{ hash: uint64(hash.Sum32()), } } return nil } // nodeMap stores PredicateCaches with node name as the key. type nodeMap map[string]predicateMap // predicateMap stores resultMaps with predicate name as the key. type predicateMap map[string]resultMap // resultMap stores PredicateResult with pod equivalence hash as the key. type resultMap map[uint64]predicateResult // predicateResult stores the output of a FitPredicate. type predicateResult struct { Fit bool FailReasons []algorithm.PredicateFailureReason } // RunPredicate returns a cached predicate result. In case of a cache miss, the predicate will be // run and its results cached for the next call. // // NOTE: RunPredicate will not update the equivalence cache if the given NodeInfo is stale. func (c *Cache) RunPredicate( pred algorithm.FitPredicate, predicateKey string, pod *v1.Pod, meta algorithm.PredicateMetadata, nodeInfo *schedulercache.NodeInfo, equivClass *Class, cache schedulercache.Cache, ) (bool, []algorithm.PredicateFailureReason, error) { if nodeInfo == nil || nodeInfo.Node() == nil { // This may happen during tests. return false, []algorithm.PredicateFailureReason{}, fmt.Errorf("nodeInfo is nil or node is invalid") } result, ok := c.lookupResult(pod.GetName(), nodeInfo.Node().GetName(), predicateKey, equivClass.hash) if ok { return result.Fit, result.FailReasons, nil } fit, reasons, err := pred(pod, meta, nodeInfo) if err != nil { return fit, reasons, err } if cache != nil { c.updateResult(pod.GetName(), predicateKey, fit, reasons, equivClass.hash, cache, nodeInfo) } return fit, reasons, nil } // updateResult updates the cached result of a predicate. func (c *Cache) updateResult( podName, predicateKey string, fit bool, reasons []algorithm.PredicateFailureReason, equivalenceHash uint64, cache schedulercache.Cache, nodeInfo *schedulercache.NodeInfo, ) { c.mu.Lock() defer c.mu.Unlock() if nodeInfo == nil || nodeInfo.Node() == nil { // This may happen during tests. return } // Skip update if NodeInfo is stale. if !cache.IsUpToDate(nodeInfo) { return } nodeName := nodeInfo.Node().GetName() if _, exist := c.cache[nodeName]; !exist { c.cache[nodeName] = make(predicateMap) } predicateItem := predicateResult{ Fit: fit, FailReasons: reasons, } // if cached predicate map already exists, just update the predicate by key if predicates, ok := c.cache[nodeName][predicateKey]; ok { // maps in golang are references, no need to add them back predicates[equivalenceHash] = predicateItem } else { c.cache[nodeName][predicateKey] = resultMap{ equivalenceHash: predicateItem, } } glog.V(5).Infof("Cache update: node=%s,predicate=%s,pod=%s,value=%v", nodeName, predicateKey, podName, predicateItem) } // lookupResult returns cached predicate results and a bool saying whether a // cache entry was found. func (c *Cache) lookupResult( podName, nodeName, predicateKey string, equivalenceHash uint64, ) (value predicateResult, ok bool) { c.mu.RLock() defer c.mu.RUnlock() glog.V(5).Infof("Cache lookup: node=%s,predicate=%s,pod=%s", nodeName, predicateKey, podName) value, ok = c.cache[nodeName][predicateKey][equivalenceHash] return value, ok } // InvalidatePredicates clears all cached results for the given predicates. func (c *Cache) InvalidatePredicates(predicateKeys sets.String) { if len(predicateKeys) == 0 { return } c.mu.Lock() defer c.mu.Unlock() // c.cache uses nodeName as key, so we just iterate it and invalid given predicates for _, predicates := range c.cache { for predicateKey := range predicateKeys { delete(predicates, predicateKey) } } glog.V(5).Infof("Cache invalidation: node=*,predicates=%v", predicateKeys) } // InvalidatePredicatesOnNode clears cached results for the given predicates on one node. func (c *Cache) InvalidatePredicatesOnNode(nodeName string, predicateKeys sets.String) { if len(predicateKeys) == 0 { return } c.mu.Lock() defer c.mu.Unlock() for predicateKey := range predicateKeys { delete(c.cache[nodeName], predicateKey) } glog.V(5).Infof("Cache invalidation: node=%s,predicates=%v", nodeName, predicateKeys) } // InvalidateAllPredicatesOnNode clears all cached results for one node. func (c *Cache) InvalidateAllPredicatesOnNode(nodeName string) { c.mu.Lock() defer c.mu.Unlock() delete(c.cache, nodeName) glog.V(5).Infof("Cache invalidation: node=%s,predicates=*", nodeName) } // InvalidateCachedPredicateItemForPodAdd is a wrapper of // InvalidateCachedPredicateItem for pod add case // TODO: This does not belong with the equivalence cache implementation. func (c *Cache)
(pod *v1.Pod, nodeName string) { // MatchInterPodAffinity: we assume scheduler can make sure newly bound pod // will not break the existing inter pod affinity. So we does not need to // invalidate MatchInterPodAffinity when pod added. // // But when a pod is deleted, existing inter pod affinity may become invalid. // (e.g. this pod was preferred by some else, or vice versa) // // NOTE: assumptions above will not stand when we implemented features like // RequiredDuringSchedulingRequiredDuringExecution. // NoDiskConflict: the newly scheduled pod fits to existing pods on this node, // it will also fits to equivalence class of existing pods // GeneralPredicates: will always be affected by adding a new pod invalidPredicates := sets.NewString(predicates.GeneralPred) // MaxPDVolumeCountPredicate: we check the volumes of pod to make decision. for _, vol := range pod.Spec.Volumes { if vol.PersistentVolumeClaim != nil { invalidPredicates.Insert(predicates.MaxEBSVolumeCountPred, predicates.MaxGCEPDVolumeCountPred, predicates.MaxAzureDiskVolumeCountPred) } else { if vol.AWSElasticBlockStore != nil { invalidPredicates.Insert(predicates.MaxEBSVolumeCountPred) } if vol.GCEPersistentDisk != nil { invalidPredicates.Insert(predicates.MaxGCEPDVolumeCountPred) } if vol.AzureDisk != nil { invalidPredicates.Insert(predicates.MaxAzureDiskVolumeCountPred) } } } c.InvalidatePredicatesOnNode(nodeName, invalidPredicates) } // equivalencePod is the set of pod attributes which must match for two pods to // be considered equivalent for scheduling purposes. For correctness, this must // include any Pod field which is used by a FitPredicate. // // NOTE: For equivalence hash to be formally correct, lists and maps in the // equivalencePod should be normalized. (e.g. by sorting them) However, the vast // majority of equivalent pod classes are expected to be created from a single // pod template, so they will all have the same ordering. type equivalencePod struct { Namespace *string Labels map[string]string Affinity *v1.Affinity Containers []v1.Container // See note about ordering InitContainers []v1.Container // See note about ordering NodeName *string NodeSelector map[string]string Tolerations []v1.Toleration Volumes []v1.Volume // See note about ordering } // getEquivalencePod returns a normalized representation of a pod so that two // "equivalent" pods will hash to the same value. func getEquivalencePod(pod *v1.Pod) *equivalencePod { ep := &equivalencePod{ Namespace: &pod.Namespace, Labels: pod.Labels, Affinity: pod.Spec.Affinity, Containers: pod.Spec.Containers, InitContainers: pod.Spec.InitContainers, NodeName: &pod.Spec.NodeName, NodeSelector: pod.Spec.NodeSelector, Tolerations: pod.Spec.Tolerations, Volumes: pod.Spec.Volumes, } // DeepHashObject considers nil and empty slices to be different. Normalize them. if len(ep.Containers) == 0 { ep.Containers = nil } if len(ep.InitContainers) == 0 { ep.InitContainers = nil } if len(ep.Tolerations) == 0 { ep.Tolerations = nil } if len(ep.Volumes) == 0 { ep.Volumes = nil } // Normalize empty maps also. if len(ep.Labels) == 0 { ep.Labels = nil } if len(ep.NodeSelector) == 0 { ep.NodeSelector = nil } // TODO(misterikkit): Also normalize nested maps and slices. return ep }
InvalidateCachedPredicateItemForPodAdd
identifier_name
eqivalence.go
/* Copyright 2016 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Package equivalence defines Pod equivalence classes and the equivalence class // cache. package equivalence import ( "fmt" "hash/fnv" "sync" "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/kubernetes/pkg/scheduler/algorithm" "k8s.io/kubernetes/pkg/scheduler/algorithm/predicates" schedulercache "k8s.io/kubernetes/pkg/scheduler/cache" hashutil "k8s.io/kubernetes/pkg/util/hash" "github.com/golang/glog" ) // Cache saves and reuses the output of predicate functions. Use RunPredicate to // get or update the cached results. An appropriate Invalidate* function should // be called when some predicate results are no longer valid. // // Internally, results are keyed by node name, predicate name, and "equivalence // class". (Equivalence class is defined in the `Class` type.) Saved results // will be reused until an appropriate invalidation function is called. type Cache struct { mu sync.RWMutex cache nodeMap } // NewCache returns an empty Cache. func NewCache() *Cache { return &Cache{ cache: make(nodeMap), } } // Class represents a set of pods which are equivalent from the perspective of // the scheduler. i.e. the scheduler would make the same decision for any pod // from the same class. type Class struct { // Equivalence hash hash uint64 } // NewClass returns the equivalence class for a given Pod. The returned Class // objects will be equal for two Pods in the same class. nil values should not // be considered equal to each other. // // NOTE: Make sure to compare types of Class and not *Class. // TODO(misterikkit): Return error instead of nil *Class. func NewClass(pod *v1.Pod) *Class { equivalencePod := getEquivalencePod(pod) if equivalencePod != nil { hash := fnv.New32a() hashutil.DeepHashObject(hash, equivalencePod) return &Class{ hash: uint64(hash.Sum32()), } } return nil } // nodeMap stores PredicateCaches with node name as the key. type nodeMap map[string]predicateMap // predicateMap stores resultMaps with predicate name as the key. type predicateMap map[string]resultMap // resultMap stores PredicateResult with pod equivalence hash as the key. type resultMap map[uint64]predicateResult // predicateResult stores the output of a FitPredicate. type predicateResult struct { Fit bool FailReasons []algorithm.PredicateFailureReason } // RunPredicate returns a cached predicate result. In case of a cache miss, the predicate will be // run and its results cached for the next call. // // NOTE: RunPredicate will not update the equivalence cache if the given NodeInfo is stale. func (c *Cache) RunPredicate( pred algorithm.FitPredicate, predicateKey string, pod *v1.Pod, meta algorithm.PredicateMetadata, nodeInfo *schedulercache.NodeInfo, equivClass *Class, cache schedulercache.Cache, ) (bool, []algorithm.PredicateFailureReason, error) { if nodeInfo == nil || nodeInfo.Node() == nil { // This may happen during tests. return false, []algorithm.PredicateFailureReason{}, fmt.Errorf("nodeInfo is nil or node is invalid") } result, ok := c.lookupResult(pod.GetName(), nodeInfo.Node().GetName(), predicateKey, equivClass.hash) if ok { return result.Fit, result.FailReasons, nil } fit, reasons, err := pred(pod, meta, nodeInfo) if err != nil { return fit, reasons, err } if cache != nil { c.updateResult(pod.GetName(), predicateKey, fit, reasons, equivClass.hash, cache, nodeInfo) } return fit, reasons, nil } // updateResult updates the cached result of a predicate. func (c *Cache) updateResult( podName, predicateKey string, fit bool, reasons []algorithm.PredicateFailureReason, equivalenceHash uint64, cache schedulercache.Cache, nodeInfo *schedulercache.NodeInfo, ) { c.mu.Lock() defer c.mu.Unlock() if nodeInfo == nil || nodeInfo.Node() == nil { // This may happen during tests. return } // Skip update if NodeInfo is stale. if !cache.IsUpToDate(nodeInfo) { return } nodeName := nodeInfo.Node().GetName() if _, exist := c.cache[nodeName]; !exist { c.cache[nodeName] = make(predicateMap) } predicateItem := predicateResult{ Fit: fit, FailReasons: reasons, } // if cached predicate map already exists, just update the predicate by key if predicates, ok := c.cache[nodeName][predicateKey]; ok { // maps in golang are references, no need to add them back predicates[equivalenceHash] = predicateItem } else { c.cache[nodeName][predicateKey] = resultMap{ equivalenceHash: predicateItem, } } glog.V(5).Infof("Cache update: node=%s,predicate=%s,pod=%s,value=%v", nodeName, predicateKey, podName, predicateItem) } // lookupResult returns cached predicate results and a bool saying whether a // cache entry was found. func (c *Cache) lookupResult( podName, nodeName, predicateKey string, equivalenceHash uint64, ) (value predicateResult, ok bool) { c.mu.RLock() defer c.mu.RUnlock() glog.V(5).Infof("Cache lookup: node=%s,predicate=%s,pod=%s", nodeName, predicateKey, podName) value, ok = c.cache[nodeName][predicateKey][equivalenceHash] return value, ok } // InvalidatePredicates clears all cached results for the given predicates. func (c *Cache) InvalidatePredicates(predicateKeys sets.String) { if len(predicateKeys) == 0 { return } c.mu.Lock() defer c.mu.Unlock() // c.cache uses nodeName as key, so we just iterate it and invalid given predicates for _, predicates := range c.cache { for predicateKey := range predicateKeys { delete(predicates, predicateKey) } } glog.V(5).Infof("Cache invalidation: node=*,predicates=%v", predicateKeys) } // InvalidatePredicatesOnNode clears cached results for the given predicates on one node. func (c *Cache) InvalidatePredicatesOnNode(nodeName string, predicateKeys sets.String) { if len(predicateKeys) == 0 { return } c.mu.Lock() defer c.mu.Unlock() for predicateKey := range predicateKeys
glog.V(5).Infof("Cache invalidation: node=%s,predicates=%v", nodeName, predicateKeys) } // InvalidateAllPredicatesOnNode clears all cached results for one node. func (c *Cache) InvalidateAllPredicatesOnNode(nodeName string) { c.mu.Lock() defer c.mu.Unlock() delete(c.cache, nodeName) glog.V(5).Infof("Cache invalidation: node=%s,predicates=*", nodeName) } // InvalidateCachedPredicateItemForPodAdd is a wrapper of // InvalidateCachedPredicateItem for pod add case // TODO: This does not belong with the equivalence cache implementation. func (c *Cache) InvalidateCachedPredicateItemForPodAdd(pod *v1.Pod, nodeName string) { // MatchInterPodAffinity: we assume scheduler can make sure newly bound pod // will not break the existing inter pod affinity. So we does not need to // invalidate MatchInterPodAffinity when pod added. // // But when a pod is deleted, existing inter pod affinity may become invalid. // (e.g. this pod was preferred by some else, or vice versa) // // NOTE: assumptions above will not stand when we implemented features like // RequiredDuringSchedulingRequiredDuringExecution. // NoDiskConflict: the newly scheduled pod fits to existing pods on this node, // it will also fits to equivalence class of existing pods // GeneralPredicates: will always be affected by adding a new pod invalidPredicates := sets.NewString(predicates.GeneralPred) // MaxPDVolumeCountPredicate: we check the volumes of pod to make decision. for _, vol := range pod.Spec.Volumes { if vol.PersistentVolumeClaim != nil { invalidPredicates.Insert(predicates.MaxEBSVolumeCountPred, predicates.MaxGCEPDVolumeCountPred, predicates.MaxAzureDiskVolumeCountPred) } else { if vol.AWSElasticBlockStore != nil { invalidPredicates.Insert(predicates.MaxEBSVolumeCountPred) } if vol.GCEPersistentDisk != nil { invalidPredicates.Insert(predicates.MaxGCEPDVolumeCountPred) } if vol.AzureDisk != nil { invalidPredicates.Insert(predicates.MaxAzureDiskVolumeCountPred) } } } c.InvalidatePredicatesOnNode(nodeName, invalidPredicates) } // equivalencePod is the set of pod attributes which must match for two pods to // be considered equivalent for scheduling purposes. For correctness, this must // include any Pod field which is used by a FitPredicate. // // NOTE: For equivalence hash to be formally correct, lists and maps in the // equivalencePod should be normalized. (e.g. by sorting them) However, the vast // majority of equivalent pod classes are expected to be created from a single // pod template, so they will all have the same ordering. type equivalencePod struct { Namespace *string Labels map[string]string Affinity *v1.Affinity Containers []v1.Container // See note about ordering InitContainers []v1.Container // See note about ordering NodeName *string NodeSelector map[string]string Tolerations []v1.Toleration Volumes []v1.Volume // See note about ordering } // getEquivalencePod returns a normalized representation of a pod so that two // "equivalent" pods will hash to the same value. func getEquivalencePod(pod *v1.Pod) *equivalencePod { ep := &equivalencePod{ Namespace: &pod.Namespace, Labels: pod.Labels, Affinity: pod.Spec.Affinity, Containers: pod.Spec.Containers, InitContainers: pod.Spec.InitContainers, NodeName: &pod.Spec.NodeName, NodeSelector: pod.Spec.NodeSelector, Tolerations: pod.Spec.Tolerations, Volumes: pod.Spec.Volumes, } // DeepHashObject considers nil and empty slices to be different. Normalize them. if len(ep.Containers) == 0 { ep.Containers = nil } if len(ep.InitContainers) == 0 { ep.InitContainers = nil } if len(ep.Tolerations) == 0 { ep.Tolerations = nil } if len(ep.Volumes) == 0 { ep.Volumes = nil } // Normalize empty maps also. if len(ep.Labels) == 0 { ep.Labels = nil } if len(ep.NodeSelector) == 0 { ep.NodeSelector = nil } // TODO(misterikkit): Also normalize nested maps and slices. return ep }
{ delete(c.cache[nodeName], predicateKey) }
conditional_block
image_mapper.go
package release import ( "bytes" "fmt" "io/ioutil" "os" "path/filepath" "regexp" "sort" "strings" "github.com/blang/semver" "github.com/ghodss/yaml" imageapi "github.com/openshift/api/image/v1" imagereference "github.com/openshift/library-go/pkg/image/reference" "k8s.io/klog/v2" ) type Payload struct { path string references *imageapi.ImageStream } func NewPayload(path string) *Payload { return &Payload{path: path} } func (p *Payload) Path() string { return p.path } // Rewrite updates the image stream to point to the locations described by the provided function. // If a new ID appears in the returned reference, it will be used instead of the existing digest. // All references in manifest files will be updated and then the image stream will be written to // the correct location with any updated metadata. func (p *Payload) Rewrite(allowTags bool, fn func(component string) imagereference.DockerImageReference) error { is, err := p.References() if err != nil { return err } replacements, err := ReplacementsForImageStream(is, allowTags, fn) if err != nil { return err } mapper, err := NewExactMapper(replacements) if err != nil { return err } files, err := ioutil.ReadDir(p.path) if err != nil { return err } for _, file := range files { if file.IsDir() { continue } if filepath.Base(file.Name()) == "image-references" { continue } path := filepath.Join(p.path, file.Name()) data, err := ioutil.ReadFile(path) if err != nil { return err } out, err := mapper(data) if err != nil { return fmt.Errorf("unable to rewrite the contents of %s: %v", path, err) } if bytes.Equal(data, out) { continue } klog.V(6).Infof("Rewrote\n%s\n\nto\n\n%s\n", string(data), string(out)) if err := ioutil.WriteFile(path, out, file.Mode()); err != nil { return err } } return nil } func (p *Payload) References() (*imageapi.ImageStream, error) { if p.references != nil { return p.references, nil } is, err := parseImageStream(filepath.Join(p.path, "image-references")) if err != nil { return nil, err } p.references = is return is, nil } func parseImageStream(path string) (*imageapi.ImageStream, error) { data, err := ioutil.ReadFile(path) if os.IsNotExist(err) { return nil, err } if err != nil { return nil, fmt.Errorf("unable to read release image info from release contents: %v", err) } return readReleaseImageReferences(data) } func readReleaseImageReferences(data []byte) (*imageapi.ImageStream, error) { is := &imageapi.ImageStream{} if err := yaml.Unmarshal(data, &is); err != nil { return nil, fmt.Errorf("unable to load release image-references: %v", err) } if is.Kind != "ImageStream" || is.APIVersion != "image.openshift.io/v1" { return nil, fmt.Errorf("unrecognized image-references in release payload") } return is, nil } type ManifestMapper func(data []byte) ([]byte, error) func NewTransformFromImageStreamFile(path string, input *imageapi.ImageStream, allowMissingImages bool) (ManifestMapper, error) { is, err := parseImageStream(path) if err != nil { return nil, err } versions, tagsByName, references, err := loadImageStreamTransforms(input, is, allowMissingImages, path) if err != nil { return nil, err } imageMapper, err := NewImageMapper(references) if err != nil { return nil, err } versionMapper := NewComponentVersionsMapper(input.Name, versions, tagsByName) return func(data []byte) ([]byte, error) { data, err := imageMapper(data) if err != nil { return nil, err } return versionMapper(data) }, nil } func loadImageStreamTransforms(input, local *imageapi.ImageStream, allowMissingImages bool, src string) (ComponentVersions, map[string][]string, map[string]ImageReference, error)
type ImageReference struct { SourceRepository string TargetPullSpec string } func NopManifestMapper(data []byte) ([]byte, error) { return data, nil } // patternImageFormat attempts to match a docker pull spec by prefix (%s) and capture the // prefix and either a tag or digest. It requires leading and trailing whitespace, quotes, or // end of file. const patternImageFormat = `([\W]|^)(%s)(:[\w][\w.-]{0,127}|@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{2,})?([\s"']|$)` func NewImageMapper(images map[string]ImageReference) (ManifestMapper, error) { repositories := make([]string, 0, len(images)) bySource := make(map[string]string) for name, ref := range images { if len(ref.SourceRepository) == 0 { return nil, fmt.Errorf("an empty source repository is not allowed for name %q", name) } if existing, ok := bySource[ref.SourceRepository]; ok { return nil, fmt.Errorf("the source repository %q was defined more than once (for %q and %q)", ref.SourceRepository, existing, name) } bySource[ref.SourceRepository] = name repositories = append(repositories, regexp.QuoteMeta(ref.SourceRepository)) } if len(repositories) == 0 { klog.V(5).Infof("No images are mapped, will not replace any contents") return NopManifestMapper, nil } pattern := fmt.Sprintf(patternImageFormat, strings.Join(repositories, "|")) re := regexp.MustCompile(pattern) return func(data []byte) ([]byte, error) { out := re.ReplaceAllFunc(data, func(in []byte) []byte { parts := re.FindSubmatch(in) repository := string(parts[2]) name, ok := bySource[repository] if !ok { klog.V(4).Infof("found potential image %q, but no matching definition", repository) return in } ref := images[name] suffix := parts[3] klog.V(2).Infof("found repository %q with locator %q in the input, switching to %q (from pattern %s)", string(repository), string(suffix), ref.TargetPullSpec, pattern) switch { case len(suffix) == 0: // we found a repository, but no tag or digest (implied latest), or we got an exact match return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) case suffix[0] == '@': // we got a digest return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) default: // TODO: we didn't get a digest, so we have to decide what to replace return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) } }) return out, nil }, nil } // exactImageFormat attempts to match a string on word boundaries const exactImageFormat = `\b%s\b` func NewExactMapper(mappings map[string]string) (ManifestMapper, error) { patterns := make(map[string]*regexp.Regexp) for from, to := range mappings { pattern := fmt.Sprintf(exactImageFormat, regexp.QuoteMeta(from)) re, err := regexp.Compile(pattern) if err != nil { return nil, err } patterns[to] = re } return func(data []byte) ([]byte, error) { for to, pattern := range patterns { data = pattern.ReplaceAll(data, []byte(to)) } return data, nil }, nil } func ComponentReferencesForImageStream(is *imageapi.ImageStream) (func(string) imagereference.DockerImageReference, error) { components := make(map[string]imagereference.DockerImageReference) for _, tag := range is.Spec.Tags { if tag.From == nil || tag.From.Kind != "DockerImage" { continue } ref, err := imagereference.Parse(tag.From.Name) if err != nil { return nil, fmt.Errorf("reference for %q is invalid: %v", tag.Name, err) } components[tag.Name] = ref } return func(component string) imagereference.DockerImageReference { ref, ok := components[component] if !ok { panic(fmt.Errorf("unknown component %s", component)) } return ref }, nil } const ( componentVersionFormat = `([\W]|^)0\.0\.1-snapshot([a-z0-9\-]*)` ) // NewComponentVersionsMapper substitutes strings of the form 0.0.1-snapshot with releaseName and strings // of the form 0.0.1-snapshot-[component] with the version value located in versions, or returns an error. // tagsByName allows the caller to return an error if references are ambiguous (two tags declare different // version values) - if that replacement is detected and tagsByName[component] has more than one entry, // then an error is returned by the ManifestMapper. // If the input release name is not a semver, a request for `0.0.1-snapshot` will be left unmodified. func NewComponentVersionsMapper(releaseName string, versions ComponentVersions, tagsByName map[string][]string) ManifestMapper { if v, err := semver.Parse(releaseName); err == nil { v.Build = nil releaseName = v.String() } else { releaseName = "" } re, err := regexp.Compile(componentVersionFormat) if err != nil { return func([]byte) ([]byte, error) { return nil, fmt.Errorf("component versions mapper regex: %v", err) } } return func(data []byte) ([]byte, error) { var missing []string var conflicts []string data = re.ReplaceAllFunc(data, func(part []byte) []byte { matches := re.FindSubmatch(part) if matches == nil { return part } key := string(matches[2]) if len(key) == 0 && len(releaseName) > 0 { buf := &bytes.Buffer{} buf.Write(matches[1]) buf.WriteString(releaseName) return buf.Bytes() } if !strings.HasPrefix(key, "-") { return part } key = key[1:] value, ok := versions[key] if !ok { missing = append(missing, key) return part } if len(tagsByName[key]) > 1 { conflicts = append(conflicts, key) return part } buf := &bytes.Buffer{} buf.Write(matches[1]) buf.WriteString(value.Version) return buf.Bytes() }) if len(missing) > 0 { switch len(missing) { case 1: if len(missing[0]) == 0 { return nil, fmt.Errorf("empty version references are not allowed") } return nil, fmt.Errorf("unknown version reference %q", missing[0]) default: return nil, fmt.Errorf("unknown version references: %s", strings.Join(missing, ", ")) } } if len(conflicts) > 0 { allImageTags := tagsByName[conflicts[0]] sort.Strings(allImageTags) return nil, fmt.Errorf("the version for %q is inconsistent across the referenced images: %s", conflicts[0], strings.Join(allImageTags, ", ")) } return data, nil } } var ( // reAllowedVersionKey limits the allowed component name to a strict subset reAllowedVersionKey = regexp.MustCompile(`^[a-z0-9]+[\-a-z0-9]*[a-z0-9]+$`) // reAllowedDisplayNameKey limits the allowed component name to a strict subset reAllowedDisplayNameKey = regexp.MustCompile(`^[a-zA-Z0-9\-\:\s\(\)]+$`) ) // ComponentVersion includes the version and optional display name. type ComponentVersion struct { Version string DisplayName string } // String returns the version of this component. func (v ComponentVersion) String() string { return v.Version } // ComponentVersions is a map of component names to semantic versions. Names are // lowercase alphanumeric and dashes. Semantic versions will have all build // labels removed, but prerelease segments are preserved. type ComponentVersions map[string]ComponentVersion // OrderedKeys returns the keys in this map in lexigraphic order. func (v ComponentVersions) OrderedKeys() []string { keys := make([]string, 0, len(v)) for k := range v { keys = append(keys, k) } sort.Strings(keys) return keys } func (v ComponentVersions) String() string { return v.VersionLabel() } // VersionLabel formats the ComponentVersions into a valid // versions label. func (v ComponentVersions) VersionLabel() string { var keys []string for k := range v { keys = append(keys, k) } sort.Strings(keys) buf := &bytes.Buffer{} for i, k := range keys { if i != 0 { buf.WriteRune(',') } fmt.Fprintf(buf, "%s=%s", k, v[k].Version) } return buf.String() } // DisplayNameLabel formats the ComponentVersions into a valid display // name label. func (v ComponentVersions) DisplayNameLabel() string { var keys []string for k := range v { keys = append(keys, k) } sort.Strings(keys) buf := &bytes.Buffer{} for i, k := range keys { if i != 0 { buf.WriteRune(',') } if len(v[k].DisplayName) == 0 { continue } fmt.Fprintf(buf, "%s=%s", k, v[k].DisplayName) } return buf.String() } // parseComponentVersionsLabel returns the version labels specified in the string or // an error. Labels are comma-delimited, key=value pairs, and surrounding whitespace is // ignored. Names must be a-z, 0-9, or have interior dashes. All values must be // semantic versions. The displayNames label is optional (if provided) and will be combined // with the valid versions. func parseComponentVersionsLabel(label, displayNames string) (ComponentVersions, error) { label = strings.TrimSpace(label) if len(label) == 0 { return nil, nil } var names map[string]string if len(displayNames) > 0 { names = make(map[string]string) for _, pair := range strings.Split(displayNames, ",") { pair = strings.TrimSpace(pair) parts := strings.SplitN(pair, "=", 2) if len(parts) == 1 { return nil, fmt.Errorf("the display name pair %q must be NAME=DISPLAYNAME", pair) } if len(parts[0]) < 2 { return nil, fmt.Errorf("the version name %q must be at least 2 characters", parts[0]) } if !reAllowedVersionKey.MatchString(parts[0]) { return nil, fmt.Errorf("the version name %q must only be ASCII alphanumerics and internal hyphens", parts[0]) } if !reAllowedDisplayNameKey.MatchString(parts[1]) { return nil, fmt.Errorf("the display name %q must only be alphanumerics, spaces, and symbols in [():-]", parts[1]) } names[parts[0]] = parts[1] } } labels := make(ComponentVersions) if len(label) == 0 { return nil, fmt.Errorf("the version pair must be NAME=VERSION") } for _, pair := range strings.Split(label, ",") { pair = strings.TrimSpace(pair) parts := strings.SplitN(pair, "=", 2) if len(parts) == 1 { return nil, fmt.Errorf("the version pair %q must be NAME=VERSION", pair) } if len(parts[0]) < 2 { return nil, fmt.Errorf("the version name %q must be at least 2 characters", parts[0]) } if !reAllowedVersionKey.MatchString(parts[0]) { return nil, fmt.Errorf("the version name %q must only be ASCII alphanumerics and internal hyphens", parts[0]) } v, err := semver.Parse(parts[1]) if err != nil { return nil, fmt.Errorf("the version pair %q must have a valid semantic version: %v", pair, err) } v.Build = nil labels[parts[0]] = ComponentVersion{ Version: v.String(), DisplayName: names[parts[0]], } } return labels, nil } func ReplacementsForImageStream(is *imageapi.ImageStream, allowTags bool, fn func(component string) imagereference.DockerImageReference) (map[string]string, error) { replacements := make(map[string]string) for i := range is.Spec.Tags { tag := &is.Spec.Tags[i] if tag.From == nil || tag.From.Kind != "DockerImage" { continue } oldImage := tag.From.Name oldRef, err := imagereference.Parse(oldImage) if err != nil { return nil, fmt.Errorf("unable to parse image reference for tag %q from payload: %v", tag.Name, err) } if len(oldRef.Tag) > 0 || len(oldRef.ID) == 0 { if !allowTags { return nil, fmt.Errorf("image reference tag %q in payload does not point to an image digest - unable to rewrite payload", tag.Name) } } ref := fn(tag.Name) if !allowTags { if len(ref.ID) == 0 { ref.Tag = "" ref.ID = oldRef.ID } } newImage := ref.Exact() replacements[oldImage] = newImage tag.From.Name = newImage } if klog.V(5).Enabled() { for k, v := range replacements { klog.Infof("Mapping %s -> %s", k, v) } } return replacements, nil }
{ references := make(map[string]ImageReference) for _, tag := range local.Spec.Tags { if tag.From == nil || tag.From.Kind != "DockerImage" { continue } if len(tag.From.Name) == 0 { return nil, nil, nil, fmt.Errorf("no from.name for the tag %s", tag.Name) } ref := ImageReference{SourceRepository: tag.From.Name} for _, inputTag := range input.Spec.Tags { if inputTag.Name == tag.Name { ref.TargetPullSpec = inputTag.From.Name break } } if len(ref.TargetPullSpec) == 0 { if allowMissingImages { klog.V(2).Infof("Image file %q referenced an image %q that is not part of the input images, skipping", src, tag.From.Name) continue } return nil, nil, nil, fmt.Errorf("no input image tag named %q", tag.Name) } references[tag.Name] = ref } // load all version values from the input stream, including any defaults, to perform // version substitution in the returned manifests. versions := make(ComponentVersions) tagsByName := make(map[string][]string) for _, tag := range input.Spec.Tags { if _, ok := references[tag.Name]; !ok { continue } value, ok := tag.Annotations[annotationBuildVersions] if !ok { continue } displayNameValue := tag.Annotations[annotationBuildVersionsDisplayNames] klog.V(4).Infof("Found build versions from %s: %s (%s)", tag.Name, value, displayNameValue) items, err := parseComponentVersionsLabel(value, displayNameValue) if err != nil { return nil, nil, nil, fmt.Errorf("input image stream has an invalid version annotation for tag %q: %v", tag.Name, value) } for k, v := range items { existing, ok := versions[k] if ok { if existing.Version != v.Version { return nil, nil, nil, fmt.Errorf("input image stream has multiple versions defined for version %s: %s defines %s but was already set to %s on %s", k, tag.Name, v, existing, strings.Join(tagsByName[k], ", ")) } } else { versions[k] = v klog.V(4).Infof("Found version %s=%s from %s", k, v.Version, tag.Name) } tagsByName[k] = append(tagsByName[k], tag.Name) } } defaults, err := parseComponentVersionsLabel(input.Annotations[annotationBuildVersions], input.Annotations[annotationBuildVersionsDisplayNames]) if err != nil { return nil, nil, nil, fmt.Errorf("unable to read default versions label on input image stream: %v", err) } for k, v := range defaults { if _, ok := versions[k]; !ok { versions[k] = v } } return versions, tagsByName, references, nil }
identifier_body
image_mapper.go
package release import ( "bytes" "fmt" "io/ioutil" "os" "path/filepath" "regexp" "sort" "strings" "github.com/blang/semver" "github.com/ghodss/yaml" imageapi "github.com/openshift/api/image/v1" imagereference "github.com/openshift/library-go/pkg/image/reference" "k8s.io/klog/v2" ) type Payload struct { path string references *imageapi.ImageStream } func NewPayload(path string) *Payload { return &Payload{path: path} } func (p *Payload) Path() string { return p.path } // Rewrite updates the image stream to point to the locations described by the provided function. // If a new ID appears in the returned reference, it will be used instead of the existing digest. // All references in manifest files will be updated and then the image stream will be written to // the correct location with any updated metadata. func (p *Payload) Rewrite(allowTags bool, fn func(component string) imagereference.DockerImageReference) error { is, err := p.References() if err != nil { return err } replacements, err := ReplacementsForImageStream(is, allowTags, fn) if err != nil { return err } mapper, err := NewExactMapper(replacements) if err != nil { return err } files, err := ioutil.ReadDir(p.path) if err != nil { return err } for _, file := range files { if file.IsDir() { continue } if filepath.Base(file.Name()) == "image-references" { continue } path := filepath.Join(p.path, file.Name()) data, err := ioutil.ReadFile(path) if err != nil { return err } out, err := mapper(data) if err != nil { return fmt.Errorf("unable to rewrite the contents of %s: %v", path, err) } if bytes.Equal(data, out) { continue } klog.V(6).Infof("Rewrote\n%s\n\nto\n\n%s\n", string(data), string(out)) if err := ioutil.WriteFile(path, out, file.Mode()); err != nil { return err } } return nil } func (p *Payload) References() (*imageapi.ImageStream, error) { if p.references != nil { return p.references, nil } is, err := parseImageStream(filepath.Join(p.path, "image-references")) if err != nil { return nil, err } p.references = is return is, nil } func parseImageStream(path string) (*imageapi.ImageStream, error) { data, err := ioutil.ReadFile(path) if os.IsNotExist(err) { return nil, err } if err != nil { return nil, fmt.Errorf("unable to read release image info from release contents: %v", err) } return readReleaseImageReferences(data) } func readReleaseImageReferences(data []byte) (*imageapi.ImageStream, error) { is := &imageapi.ImageStream{} if err := yaml.Unmarshal(data, &is); err != nil { return nil, fmt.Errorf("unable to load release image-references: %v", err) } if is.Kind != "ImageStream" || is.APIVersion != "image.openshift.io/v1" { return nil, fmt.Errorf("unrecognized image-references in release payload") } return is, nil } type ManifestMapper func(data []byte) ([]byte, error) func NewTransformFromImageStreamFile(path string, input *imageapi.ImageStream, allowMissingImages bool) (ManifestMapper, error) { is, err := parseImageStream(path) if err != nil { return nil, err } versions, tagsByName, references, err := loadImageStreamTransforms(input, is, allowMissingImages, path) if err != nil { return nil, err } imageMapper, err := NewImageMapper(references) if err != nil { return nil, err } versionMapper := NewComponentVersionsMapper(input.Name, versions, tagsByName) return func(data []byte) ([]byte, error) { data, err := imageMapper(data) if err != nil { return nil, err } return versionMapper(data) }, nil } func loadImageStreamTransforms(input, local *imageapi.ImageStream, allowMissingImages bool, src string) (ComponentVersions, map[string][]string, map[string]ImageReference, error) { references := make(map[string]ImageReference) for _, tag := range local.Spec.Tags { if tag.From == nil || tag.From.Kind != "DockerImage" { continue } if len(tag.From.Name) == 0 { return nil, nil, nil, fmt.Errorf("no from.name for the tag %s", tag.Name) } ref := ImageReference{SourceRepository: tag.From.Name} for _, inputTag := range input.Spec.Tags { if inputTag.Name == tag.Name { ref.TargetPullSpec = inputTag.From.Name break } } if len(ref.TargetPullSpec) == 0 { if allowMissingImages { klog.V(2).Infof("Image file %q referenced an image %q that is not part of the input images, skipping", src, tag.From.Name) continue } return nil, nil, nil, fmt.Errorf("no input image tag named %q", tag.Name) } references[tag.Name] = ref } // load all version values from the input stream, including any defaults, to perform // version substitution in the returned manifests. versions := make(ComponentVersions) tagsByName := make(map[string][]string) for _, tag := range input.Spec.Tags { if _, ok := references[tag.Name]; !ok { continue } value, ok := tag.Annotations[annotationBuildVersions] if !ok { continue } displayNameValue := tag.Annotations[annotationBuildVersionsDisplayNames] klog.V(4).Infof("Found build versions from %s: %s (%s)", tag.Name, value, displayNameValue) items, err := parseComponentVersionsLabel(value, displayNameValue) if err != nil { return nil, nil, nil, fmt.Errorf("input image stream has an invalid version annotation for tag %q: %v", tag.Name, value) } for k, v := range items { existing, ok := versions[k] if ok { if existing.Version != v.Version { return nil, nil, nil, fmt.Errorf("input image stream has multiple versions defined for version %s: %s defines %s but was already set to %s on %s", k, tag.Name, v, existing, strings.Join(tagsByName[k], ", ")) } } else { versions[k] = v klog.V(4).Infof("Found version %s=%s from %s", k, v.Version, tag.Name) } tagsByName[k] = append(tagsByName[k], tag.Name) } } defaults, err := parseComponentVersionsLabel(input.Annotations[annotationBuildVersions], input.Annotations[annotationBuildVersionsDisplayNames]) if err != nil { return nil, nil, nil, fmt.Errorf("unable to read default versions label on input image stream: %v", err) } for k, v := range defaults { if _, ok := versions[k]; !ok { versions[k] = v } } return versions, tagsByName, references, nil } type ImageReference struct { SourceRepository string TargetPullSpec string } func NopManifestMapper(data []byte) ([]byte, error) { return data, nil } // patternImageFormat attempts to match a docker pull spec by prefix (%s) and capture the // prefix and either a tag or digest. It requires leading and trailing whitespace, quotes, or // end of file. const patternImageFormat = `([\W]|^)(%s)(:[\w][\w.-]{0,127}|@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{2,})?([\s"']|$)` func
(images map[string]ImageReference) (ManifestMapper, error) { repositories := make([]string, 0, len(images)) bySource := make(map[string]string) for name, ref := range images { if len(ref.SourceRepository) == 0 { return nil, fmt.Errorf("an empty source repository is not allowed for name %q", name) } if existing, ok := bySource[ref.SourceRepository]; ok { return nil, fmt.Errorf("the source repository %q was defined more than once (for %q and %q)", ref.SourceRepository, existing, name) } bySource[ref.SourceRepository] = name repositories = append(repositories, regexp.QuoteMeta(ref.SourceRepository)) } if len(repositories) == 0 { klog.V(5).Infof("No images are mapped, will not replace any contents") return NopManifestMapper, nil } pattern := fmt.Sprintf(patternImageFormat, strings.Join(repositories, "|")) re := regexp.MustCompile(pattern) return func(data []byte) ([]byte, error) { out := re.ReplaceAllFunc(data, func(in []byte) []byte { parts := re.FindSubmatch(in) repository := string(parts[2]) name, ok := bySource[repository] if !ok { klog.V(4).Infof("found potential image %q, but no matching definition", repository) return in } ref := images[name] suffix := parts[3] klog.V(2).Infof("found repository %q with locator %q in the input, switching to %q (from pattern %s)", string(repository), string(suffix), ref.TargetPullSpec, pattern) switch { case len(suffix) == 0: // we found a repository, but no tag or digest (implied latest), or we got an exact match return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) case suffix[0] == '@': // we got a digest return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) default: // TODO: we didn't get a digest, so we have to decide what to replace return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) } }) return out, nil }, nil } // exactImageFormat attempts to match a string on word boundaries const exactImageFormat = `\b%s\b` func NewExactMapper(mappings map[string]string) (ManifestMapper, error) { patterns := make(map[string]*regexp.Regexp) for from, to := range mappings { pattern := fmt.Sprintf(exactImageFormat, regexp.QuoteMeta(from)) re, err := regexp.Compile(pattern) if err != nil { return nil, err } patterns[to] = re } return func(data []byte) ([]byte, error) { for to, pattern := range patterns { data = pattern.ReplaceAll(data, []byte(to)) } return data, nil }, nil } func ComponentReferencesForImageStream(is *imageapi.ImageStream) (func(string) imagereference.DockerImageReference, error) { components := make(map[string]imagereference.DockerImageReference) for _, tag := range is.Spec.Tags { if tag.From == nil || tag.From.Kind != "DockerImage" { continue } ref, err := imagereference.Parse(tag.From.Name) if err != nil { return nil, fmt.Errorf("reference for %q is invalid: %v", tag.Name, err) } components[tag.Name] = ref } return func(component string) imagereference.DockerImageReference { ref, ok := components[component] if !ok { panic(fmt.Errorf("unknown component %s", component)) } return ref }, nil } const ( componentVersionFormat = `([\W]|^)0\.0\.1-snapshot([a-z0-9\-]*)` ) // NewComponentVersionsMapper substitutes strings of the form 0.0.1-snapshot with releaseName and strings // of the form 0.0.1-snapshot-[component] with the version value located in versions, or returns an error. // tagsByName allows the caller to return an error if references are ambiguous (two tags declare different // version values) - if that replacement is detected and tagsByName[component] has more than one entry, // then an error is returned by the ManifestMapper. // If the input release name is not a semver, a request for `0.0.1-snapshot` will be left unmodified. func NewComponentVersionsMapper(releaseName string, versions ComponentVersions, tagsByName map[string][]string) ManifestMapper { if v, err := semver.Parse(releaseName); err == nil { v.Build = nil releaseName = v.String() } else { releaseName = "" } re, err := regexp.Compile(componentVersionFormat) if err != nil { return func([]byte) ([]byte, error) { return nil, fmt.Errorf("component versions mapper regex: %v", err) } } return func(data []byte) ([]byte, error) { var missing []string var conflicts []string data = re.ReplaceAllFunc(data, func(part []byte) []byte { matches := re.FindSubmatch(part) if matches == nil { return part } key := string(matches[2]) if len(key) == 0 && len(releaseName) > 0 { buf := &bytes.Buffer{} buf.Write(matches[1]) buf.WriteString(releaseName) return buf.Bytes() } if !strings.HasPrefix(key, "-") { return part } key = key[1:] value, ok := versions[key] if !ok { missing = append(missing, key) return part } if len(tagsByName[key]) > 1 { conflicts = append(conflicts, key) return part } buf := &bytes.Buffer{} buf.Write(matches[1]) buf.WriteString(value.Version) return buf.Bytes() }) if len(missing) > 0 { switch len(missing) { case 1: if len(missing[0]) == 0 { return nil, fmt.Errorf("empty version references are not allowed") } return nil, fmt.Errorf("unknown version reference %q", missing[0]) default: return nil, fmt.Errorf("unknown version references: %s", strings.Join(missing, ", ")) } } if len(conflicts) > 0 { allImageTags := tagsByName[conflicts[0]] sort.Strings(allImageTags) return nil, fmt.Errorf("the version for %q is inconsistent across the referenced images: %s", conflicts[0], strings.Join(allImageTags, ", ")) } return data, nil } } var ( // reAllowedVersionKey limits the allowed component name to a strict subset reAllowedVersionKey = regexp.MustCompile(`^[a-z0-9]+[\-a-z0-9]*[a-z0-9]+$`) // reAllowedDisplayNameKey limits the allowed component name to a strict subset reAllowedDisplayNameKey = regexp.MustCompile(`^[a-zA-Z0-9\-\:\s\(\)]+$`) ) // ComponentVersion includes the version and optional display name. type ComponentVersion struct { Version string DisplayName string } // String returns the version of this component. func (v ComponentVersion) String() string { return v.Version } // ComponentVersions is a map of component names to semantic versions. Names are // lowercase alphanumeric and dashes. Semantic versions will have all build // labels removed, but prerelease segments are preserved. type ComponentVersions map[string]ComponentVersion // OrderedKeys returns the keys in this map in lexigraphic order. func (v ComponentVersions) OrderedKeys() []string { keys := make([]string, 0, len(v)) for k := range v { keys = append(keys, k) } sort.Strings(keys) return keys } func (v ComponentVersions) String() string { return v.VersionLabel() } // VersionLabel formats the ComponentVersions into a valid // versions label. func (v ComponentVersions) VersionLabel() string { var keys []string for k := range v { keys = append(keys, k) } sort.Strings(keys) buf := &bytes.Buffer{} for i, k := range keys { if i != 0 { buf.WriteRune(',') } fmt.Fprintf(buf, "%s=%s", k, v[k].Version) } return buf.String() } // DisplayNameLabel formats the ComponentVersions into a valid display // name label. func (v ComponentVersions) DisplayNameLabel() string { var keys []string for k := range v { keys = append(keys, k) } sort.Strings(keys) buf := &bytes.Buffer{} for i, k := range keys { if i != 0 { buf.WriteRune(',') } if len(v[k].DisplayName) == 0 { continue } fmt.Fprintf(buf, "%s=%s", k, v[k].DisplayName) } return buf.String() } // parseComponentVersionsLabel returns the version labels specified in the string or // an error. Labels are comma-delimited, key=value pairs, and surrounding whitespace is // ignored. Names must be a-z, 0-9, or have interior dashes. All values must be // semantic versions. The displayNames label is optional (if provided) and will be combined // with the valid versions. func parseComponentVersionsLabel(label, displayNames string) (ComponentVersions, error) { label = strings.TrimSpace(label) if len(label) == 0 { return nil, nil } var names map[string]string if len(displayNames) > 0 { names = make(map[string]string) for _, pair := range strings.Split(displayNames, ",") { pair = strings.TrimSpace(pair) parts := strings.SplitN(pair, "=", 2) if len(parts) == 1 { return nil, fmt.Errorf("the display name pair %q must be NAME=DISPLAYNAME", pair) } if len(parts[0]) < 2 { return nil, fmt.Errorf("the version name %q must be at least 2 characters", parts[0]) } if !reAllowedVersionKey.MatchString(parts[0]) { return nil, fmt.Errorf("the version name %q must only be ASCII alphanumerics and internal hyphens", parts[0]) } if !reAllowedDisplayNameKey.MatchString(parts[1]) { return nil, fmt.Errorf("the display name %q must only be alphanumerics, spaces, and symbols in [():-]", parts[1]) } names[parts[0]] = parts[1] } } labels := make(ComponentVersions) if len(label) == 0 { return nil, fmt.Errorf("the version pair must be NAME=VERSION") } for _, pair := range strings.Split(label, ",") { pair = strings.TrimSpace(pair) parts := strings.SplitN(pair, "=", 2) if len(parts) == 1 { return nil, fmt.Errorf("the version pair %q must be NAME=VERSION", pair) } if len(parts[0]) < 2 { return nil, fmt.Errorf("the version name %q must be at least 2 characters", parts[0]) } if !reAllowedVersionKey.MatchString(parts[0]) { return nil, fmt.Errorf("the version name %q must only be ASCII alphanumerics and internal hyphens", parts[0]) } v, err := semver.Parse(parts[1]) if err != nil { return nil, fmt.Errorf("the version pair %q must have a valid semantic version: %v", pair, err) } v.Build = nil labels[parts[0]] = ComponentVersion{ Version: v.String(), DisplayName: names[parts[0]], } } return labels, nil } func ReplacementsForImageStream(is *imageapi.ImageStream, allowTags bool, fn func(component string) imagereference.DockerImageReference) (map[string]string, error) { replacements := make(map[string]string) for i := range is.Spec.Tags { tag := &is.Spec.Tags[i] if tag.From == nil || tag.From.Kind != "DockerImage" { continue } oldImage := tag.From.Name oldRef, err := imagereference.Parse(oldImage) if err != nil { return nil, fmt.Errorf("unable to parse image reference for tag %q from payload: %v", tag.Name, err) } if len(oldRef.Tag) > 0 || len(oldRef.ID) == 0 { if !allowTags { return nil, fmt.Errorf("image reference tag %q in payload does not point to an image digest - unable to rewrite payload", tag.Name) } } ref := fn(tag.Name) if !allowTags { if len(ref.ID) == 0 { ref.Tag = "" ref.ID = oldRef.ID } } newImage := ref.Exact() replacements[oldImage] = newImage tag.From.Name = newImage } if klog.V(5).Enabled() { for k, v := range replacements { klog.Infof("Mapping %s -> %s", k, v) } } return replacements, nil }
NewImageMapper
identifier_name
image_mapper.go
package release import ( "bytes" "fmt" "io/ioutil" "os" "path/filepath" "regexp" "sort" "strings" "github.com/blang/semver" "github.com/ghodss/yaml" imageapi "github.com/openshift/api/image/v1" imagereference "github.com/openshift/library-go/pkg/image/reference" "k8s.io/klog/v2" ) type Payload struct { path string references *imageapi.ImageStream } func NewPayload(path string) *Payload { return &Payload{path: path} } func (p *Payload) Path() string { return p.path } // Rewrite updates the image stream to point to the locations described by the provided function. // If a new ID appears in the returned reference, it will be used instead of the existing digest. // All references in manifest files will be updated and then the image stream will be written to // the correct location with any updated metadata. func (p *Payload) Rewrite(allowTags bool, fn func(component string) imagereference.DockerImageReference) error { is, err := p.References() if err != nil { return err } replacements, err := ReplacementsForImageStream(is, allowTags, fn) if err != nil { return err } mapper, err := NewExactMapper(replacements) if err != nil { return err } files, err := ioutil.ReadDir(p.path) if err != nil { return err } for _, file := range files { if file.IsDir() { continue } if filepath.Base(file.Name()) == "image-references" { continue } path := filepath.Join(p.path, file.Name()) data, err := ioutil.ReadFile(path) if err != nil { return err } out, err := mapper(data) if err != nil { return fmt.Errorf("unable to rewrite the contents of %s: %v", path, err) } if bytes.Equal(data, out) { continue } klog.V(6).Infof("Rewrote\n%s\n\nto\n\n%s\n", string(data), string(out)) if err := ioutil.WriteFile(path, out, file.Mode()); err != nil { return err } } return nil } func (p *Payload) References() (*imageapi.ImageStream, error) { if p.references != nil { return p.references, nil } is, err := parseImageStream(filepath.Join(p.path, "image-references")) if err != nil { return nil, err } p.references = is return is, nil } func parseImageStream(path string) (*imageapi.ImageStream, error) { data, err := ioutil.ReadFile(path) if os.IsNotExist(err) { return nil, err } if err != nil { return nil, fmt.Errorf("unable to read release image info from release contents: %v", err) } return readReleaseImageReferences(data) } func readReleaseImageReferences(data []byte) (*imageapi.ImageStream, error) { is := &imageapi.ImageStream{} if err := yaml.Unmarshal(data, &is); err != nil { return nil, fmt.Errorf("unable to load release image-references: %v", err) } if is.Kind != "ImageStream" || is.APIVersion != "image.openshift.io/v1" { return nil, fmt.Errorf("unrecognized image-references in release payload") } return is, nil } type ManifestMapper func(data []byte) ([]byte, error) func NewTransformFromImageStreamFile(path string, input *imageapi.ImageStream, allowMissingImages bool) (ManifestMapper, error) { is, err := parseImageStream(path) if err != nil { return nil, err } versions, tagsByName, references, err := loadImageStreamTransforms(input, is, allowMissingImages, path) if err != nil { return nil, err } imageMapper, err := NewImageMapper(references) if err != nil { return nil, err } versionMapper := NewComponentVersionsMapper(input.Name, versions, tagsByName) return func(data []byte) ([]byte, error) { data, err := imageMapper(data) if err != nil { return nil, err } return versionMapper(data) }, nil } func loadImageStreamTransforms(input, local *imageapi.ImageStream, allowMissingImages bool, src string) (ComponentVersions, map[string][]string, map[string]ImageReference, error) { references := make(map[string]ImageReference) for _, tag := range local.Spec.Tags { if tag.From == nil || tag.From.Kind != "DockerImage" { continue } if len(tag.From.Name) == 0 { return nil, nil, nil, fmt.Errorf("no from.name for the tag %s", tag.Name) } ref := ImageReference{SourceRepository: tag.From.Name} for _, inputTag := range input.Spec.Tags { if inputTag.Name == tag.Name { ref.TargetPullSpec = inputTag.From.Name break } } if len(ref.TargetPullSpec) == 0 { if allowMissingImages { klog.V(2).Infof("Image file %q referenced an image %q that is not part of the input images, skipping", src, tag.From.Name) continue } return nil, nil, nil, fmt.Errorf("no input image tag named %q", tag.Name) } references[tag.Name] = ref } // load all version values from the input stream, including any defaults, to perform // version substitution in the returned manifests. versions := make(ComponentVersions) tagsByName := make(map[string][]string) for _, tag := range input.Spec.Tags { if _, ok := references[tag.Name]; !ok { continue } value, ok := tag.Annotations[annotationBuildVersions] if !ok { continue } displayNameValue := tag.Annotations[annotationBuildVersionsDisplayNames] klog.V(4).Infof("Found build versions from %s: %s (%s)", tag.Name, value, displayNameValue) items, err := parseComponentVersionsLabel(value, displayNameValue) if err != nil { return nil, nil, nil, fmt.Errorf("input image stream has an invalid version annotation for tag %q: %v", tag.Name, value) } for k, v := range items { existing, ok := versions[k] if ok { if existing.Version != v.Version { return nil, nil, nil, fmt.Errorf("input image stream has multiple versions defined for version %s: %s defines %s but was already set to %s on %s", k, tag.Name, v, existing, strings.Join(tagsByName[k], ", ")) } } else { versions[k] = v klog.V(4).Infof("Found version %s=%s from %s", k, v.Version, tag.Name) } tagsByName[k] = append(tagsByName[k], tag.Name) } } defaults, err := parseComponentVersionsLabel(input.Annotations[annotationBuildVersions], input.Annotations[annotationBuildVersionsDisplayNames]) if err != nil { return nil, nil, nil, fmt.Errorf("unable to read default versions label on input image stream: %v", err) } for k, v := range defaults { if _, ok := versions[k]; !ok { versions[k] = v } } return versions, tagsByName, references, nil } type ImageReference struct { SourceRepository string TargetPullSpec string } func NopManifestMapper(data []byte) ([]byte, error) { return data, nil } // patternImageFormat attempts to match a docker pull spec by prefix (%s) and capture the // prefix and either a tag or digest. It requires leading and trailing whitespace, quotes, or // end of file. const patternImageFormat = `([\W]|^)(%s)(:[\w][\w.-]{0,127}|@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{2,})?([\s"']|$)` func NewImageMapper(images map[string]ImageReference) (ManifestMapper, error) { repositories := make([]string, 0, len(images)) bySource := make(map[string]string) for name, ref := range images { if len(ref.SourceRepository) == 0 { return nil, fmt.Errorf("an empty source repository is not allowed for name %q", name) } if existing, ok := bySource[ref.SourceRepository]; ok { return nil, fmt.Errorf("the source repository %q was defined more than once (for %q and %q)", ref.SourceRepository, existing, name) } bySource[ref.SourceRepository] = name repositories = append(repositories, regexp.QuoteMeta(ref.SourceRepository)) } if len(repositories) == 0 { klog.V(5).Infof("No images are mapped, will not replace any contents") return NopManifestMapper, nil } pattern := fmt.Sprintf(patternImageFormat, strings.Join(repositories, "|")) re := regexp.MustCompile(pattern) return func(data []byte) ([]byte, error) { out := re.ReplaceAllFunc(data, func(in []byte) []byte { parts := re.FindSubmatch(in) repository := string(parts[2]) name, ok := bySource[repository] if !ok { klog.V(4).Infof("found potential image %q, but no matching definition", repository) return in } ref := images[name] suffix := parts[3] klog.V(2).Infof("found repository %q with locator %q in the input, switching to %q (from pattern %s)", string(repository), string(suffix), ref.TargetPullSpec, pattern) switch { case len(suffix) == 0: // we found a repository, but no tag or digest (implied latest), or we got an exact match return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) case suffix[0] == '@': // we got a digest return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) default: // TODO: we didn't get a digest, so we have to decide what to replace return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) } }) return out, nil }, nil } // exactImageFormat attempts to match a string on word boundaries const exactImageFormat = `\b%s\b` func NewExactMapper(mappings map[string]string) (ManifestMapper, error) { patterns := make(map[string]*regexp.Regexp) for from, to := range mappings { pattern := fmt.Sprintf(exactImageFormat, regexp.QuoteMeta(from)) re, err := regexp.Compile(pattern) if err != nil { return nil, err } patterns[to] = re } return func(data []byte) ([]byte, error) { for to, pattern := range patterns { data = pattern.ReplaceAll(data, []byte(to)) } return data, nil }, nil } func ComponentReferencesForImageStream(is *imageapi.ImageStream) (func(string) imagereference.DockerImageReference, error) { components := make(map[string]imagereference.DockerImageReference) for _, tag := range is.Spec.Tags { if tag.From == nil || tag.From.Kind != "DockerImage" { continue } ref, err := imagereference.Parse(tag.From.Name) if err != nil { return nil, fmt.Errorf("reference for %q is invalid: %v", tag.Name, err) } components[tag.Name] = ref } return func(component string) imagereference.DockerImageReference { ref, ok := components[component] if !ok { panic(fmt.Errorf("unknown component %s", component)) } return ref }, nil } const ( componentVersionFormat = `([\W]|^)0\.0\.1-snapshot([a-z0-9\-]*)` ) // NewComponentVersionsMapper substitutes strings of the form 0.0.1-snapshot with releaseName and strings // of the form 0.0.1-snapshot-[component] with the version value located in versions, or returns an error. // tagsByName allows the caller to return an error if references are ambiguous (two tags declare different // version values) - if that replacement is detected and tagsByName[component] has more than one entry, // then an error is returned by the ManifestMapper. // If the input release name is not a semver, a request for `0.0.1-snapshot` will be left unmodified. func NewComponentVersionsMapper(releaseName string, versions ComponentVersions, tagsByName map[string][]string) ManifestMapper { if v, err := semver.Parse(releaseName); err == nil { v.Build = nil releaseName = v.String() } else { releaseName = "" } re, err := regexp.Compile(componentVersionFormat) if err != nil { return func([]byte) ([]byte, error) { return nil, fmt.Errorf("component versions mapper regex: %v", err) } } return func(data []byte) ([]byte, error) { var missing []string var conflicts []string data = re.ReplaceAllFunc(data, func(part []byte) []byte { matches := re.FindSubmatch(part) if matches == nil { return part } key := string(matches[2]) if len(key) == 0 && len(releaseName) > 0 { buf := &bytes.Buffer{} buf.Write(matches[1]) buf.WriteString(releaseName) return buf.Bytes() } if !strings.HasPrefix(key, "-") { return part } key = key[1:] value, ok := versions[key] if !ok { missing = append(missing, key) return part } if len(tagsByName[key]) > 1 { conflicts = append(conflicts, key) return part } buf := &bytes.Buffer{} buf.Write(matches[1]) buf.WriteString(value.Version) return buf.Bytes() }) if len(missing) > 0 { switch len(missing) { case 1: if len(missing[0]) == 0 { return nil, fmt.Errorf("empty version references are not allowed") } return nil, fmt.Errorf("unknown version reference %q", missing[0]) default: return nil, fmt.Errorf("unknown version references: %s", strings.Join(missing, ", ")) } } if len(conflicts) > 0 { allImageTags := tagsByName[conflicts[0]] sort.Strings(allImageTags) return nil, fmt.Errorf("the version for %q is inconsistent across the referenced images: %s", conflicts[0], strings.Join(allImageTags, ", ")) } return data, nil } } var ( // reAllowedVersionKey limits the allowed component name to a strict subset reAllowedVersionKey = regexp.MustCompile(`^[a-z0-9]+[\-a-z0-9]*[a-z0-9]+$`) // reAllowedDisplayNameKey limits the allowed component name to a strict subset reAllowedDisplayNameKey = regexp.MustCompile(`^[a-zA-Z0-9\-\:\s\(\)]+$`) ) // ComponentVersion includes the version and optional display name. type ComponentVersion struct { Version string DisplayName string } // String returns the version of this component. func (v ComponentVersion) String() string { return v.Version } // ComponentVersions is a map of component names to semantic versions. Names are // lowercase alphanumeric and dashes. Semantic versions will have all build // labels removed, but prerelease segments are preserved. type ComponentVersions map[string]ComponentVersion // OrderedKeys returns the keys in this map in lexigraphic order. func (v ComponentVersions) OrderedKeys() []string { keys := make([]string, 0, len(v)) for k := range v { keys = append(keys, k) } sort.Strings(keys) return keys } func (v ComponentVersions) String() string { return v.VersionLabel() } // VersionLabel formats the ComponentVersions into a valid // versions label. func (v ComponentVersions) VersionLabel() string { var keys []string for k := range v { keys = append(keys, k) } sort.Strings(keys) buf := &bytes.Buffer{} for i, k := range keys { if i != 0 { buf.WriteRune(',') } fmt.Fprintf(buf, "%s=%s", k, v[k].Version) } return buf.String() } // DisplayNameLabel formats the ComponentVersions into a valid display // name label. func (v ComponentVersions) DisplayNameLabel() string { var keys []string for k := range v { keys = append(keys, k) } sort.Strings(keys) buf := &bytes.Buffer{} for i, k := range keys { if i != 0 { buf.WriteRune(',') } if len(v[k].DisplayName) == 0 { continue } fmt.Fprintf(buf, "%s=%s", k, v[k].DisplayName) } return buf.String() } // parseComponentVersionsLabel returns the version labels specified in the string or // an error. Labels are comma-delimited, key=value pairs, and surrounding whitespace is // ignored. Names must be a-z, 0-9, or have interior dashes. All values must be // semantic versions. The displayNames label is optional (if provided) and will be combined // with the valid versions. func parseComponentVersionsLabel(label, displayNames string) (ComponentVersions, error) { label = strings.TrimSpace(label) if len(label) == 0 { return nil, nil } var names map[string]string if len(displayNames) > 0 { names = make(map[string]string) for _, pair := range strings.Split(displayNames, ",") { pair = strings.TrimSpace(pair) parts := strings.SplitN(pair, "=", 2) if len(parts) == 1 { return nil, fmt.Errorf("the display name pair %q must be NAME=DISPLAYNAME", pair) } if len(parts[0]) < 2
if !reAllowedVersionKey.MatchString(parts[0]) { return nil, fmt.Errorf("the version name %q must only be ASCII alphanumerics and internal hyphens", parts[0]) } if !reAllowedDisplayNameKey.MatchString(parts[1]) { return nil, fmt.Errorf("the display name %q must only be alphanumerics, spaces, and symbols in [():-]", parts[1]) } names[parts[0]] = parts[1] } } labels := make(ComponentVersions) if len(label) == 0 { return nil, fmt.Errorf("the version pair must be NAME=VERSION") } for _, pair := range strings.Split(label, ",") { pair = strings.TrimSpace(pair) parts := strings.SplitN(pair, "=", 2) if len(parts) == 1 { return nil, fmt.Errorf("the version pair %q must be NAME=VERSION", pair) } if len(parts[0]) < 2 { return nil, fmt.Errorf("the version name %q must be at least 2 characters", parts[0]) } if !reAllowedVersionKey.MatchString(parts[0]) { return nil, fmt.Errorf("the version name %q must only be ASCII alphanumerics and internal hyphens", parts[0]) } v, err := semver.Parse(parts[1]) if err != nil { return nil, fmt.Errorf("the version pair %q must have a valid semantic version: %v", pair, err) } v.Build = nil labels[parts[0]] = ComponentVersion{ Version: v.String(), DisplayName: names[parts[0]], } } return labels, nil } func ReplacementsForImageStream(is *imageapi.ImageStream, allowTags bool, fn func(component string) imagereference.DockerImageReference) (map[string]string, error) { replacements := make(map[string]string) for i := range is.Spec.Tags { tag := &is.Spec.Tags[i] if tag.From == nil || tag.From.Kind != "DockerImage" { continue } oldImage := tag.From.Name oldRef, err := imagereference.Parse(oldImage) if err != nil { return nil, fmt.Errorf("unable to parse image reference for tag %q from payload: %v", tag.Name, err) } if len(oldRef.Tag) > 0 || len(oldRef.ID) == 0 { if !allowTags { return nil, fmt.Errorf("image reference tag %q in payload does not point to an image digest - unable to rewrite payload", tag.Name) } } ref := fn(tag.Name) if !allowTags { if len(ref.ID) == 0 { ref.Tag = "" ref.ID = oldRef.ID } } newImage := ref.Exact() replacements[oldImage] = newImage tag.From.Name = newImage } if klog.V(5).Enabled() { for k, v := range replacements { klog.Infof("Mapping %s -> %s", k, v) } } return replacements, nil }
{ return nil, fmt.Errorf("the version name %q must be at least 2 characters", parts[0]) }
conditional_block
image_mapper.go
package release import ( "bytes" "fmt" "io/ioutil" "os" "path/filepath" "regexp" "sort" "strings" "github.com/blang/semver" "github.com/ghodss/yaml" imageapi "github.com/openshift/api/image/v1" imagereference "github.com/openshift/library-go/pkg/image/reference" "k8s.io/klog/v2" ) type Payload struct { path string references *imageapi.ImageStream } func NewPayload(path string) *Payload { return &Payload{path: path} } func (p *Payload) Path() string { return p.path } // Rewrite updates the image stream to point to the locations described by the provided function. // If a new ID appears in the returned reference, it will be used instead of the existing digest. // All references in manifest files will be updated and then the image stream will be written to // the correct location with any updated metadata. func (p *Payload) Rewrite(allowTags bool, fn func(component string) imagereference.DockerImageReference) error { is, err := p.References() if err != nil { return err } replacements, err := ReplacementsForImageStream(is, allowTags, fn) if err != nil { return err } mapper, err := NewExactMapper(replacements) if err != nil { return err } files, err := ioutil.ReadDir(p.path) if err != nil { return err } for _, file := range files { if file.IsDir() { continue } if filepath.Base(file.Name()) == "image-references" { continue } path := filepath.Join(p.path, file.Name()) data, err := ioutil.ReadFile(path) if err != nil { return err } out, err := mapper(data) if err != nil { return fmt.Errorf("unable to rewrite the contents of %s: %v", path, err) } if bytes.Equal(data, out) { continue } klog.V(6).Infof("Rewrote\n%s\n\nto\n\n%s\n", string(data), string(out)) if err := ioutil.WriteFile(path, out, file.Mode()); err != nil { return err } } return nil } func (p *Payload) References() (*imageapi.ImageStream, error) { if p.references != nil { return p.references, nil } is, err := parseImageStream(filepath.Join(p.path, "image-references")) if err != nil { return nil, err } p.references = is return is, nil } func parseImageStream(path string) (*imageapi.ImageStream, error) { data, err := ioutil.ReadFile(path) if os.IsNotExist(err) { return nil, err } if err != nil { return nil, fmt.Errorf("unable to read release image info from release contents: %v", err) } return readReleaseImageReferences(data) } func readReleaseImageReferences(data []byte) (*imageapi.ImageStream, error) { is := &imageapi.ImageStream{} if err := yaml.Unmarshal(data, &is); err != nil { return nil, fmt.Errorf("unable to load release image-references: %v", err) } if is.Kind != "ImageStream" || is.APIVersion != "image.openshift.io/v1" { return nil, fmt.Errorf("unrecognized image-references in release payload") } return is, nil } type ManifestMapper func(data []byte) ([]byte, error) func NewTransformFromImageStreamFile(path string, input *imageapi.ImageStream, allowMissingImages bool) (ManifestMapper, error) { is, err := parseImageStream(path) if err != nil { return nil, err } versions, tagsByName, references, err := loadImageStreamTransforms(input, is, allowMissingImages, path) if err != nil { return nil, err } imageMapper, err := NewImageMapper(references) if err != nil { return nil, err } versionMapper := NewComponentVersionsMapper(input.Name, versions, tagsByName) return func(data []byte) ([]byte, error) { data, err := imageMapper(data) if err != nil { return nil, err } return versionMapper(data) }, nil } func loadImageStreamTransforms(input, local *imageapi.ImageStream, allowMissingImages bool, src string) (ComponentVersions, map[string][]string, map[string]ImageReference, error) { references := make(map[string]ImageReference) for _, tag := range local.Spec.Tags { if tag.From == nil || tag.From.Kind != "DockerImage" { continue } if len(tag.From.Name) == 0 { return nil, nil, nil, fmt.Errorf("no from.name for the tag %s", tag.Name) } ref := ImageReference{SourceRepository: tag.From.Name} for _, inputTag := range input.Spec.Tags { if inputTag.Name == tag.Name { ref.TargetPullSpec = inputTag.From.Name break } } if len(ref.TargetPullSpec) == 0 { if allowMissingImages { klog.V(2).Infof("Image file %q referenced an image %q that is not part of the input images, skipping", src, tag.From.Name) continue } return nil, nil, nil, fmt.Errorf("no input image tag named %q", tag.Name) } references[tag.Name] = ref } // load all version values from the input stream, including any defaults, to perform // version substitution in the returned manifests. versions := make(ComponentVersions) tagsByName := make(map[string][]string) for _, tag := range input.Spec.Tags { if _, ok := references[tag.Name]; !ok { continue } value, ok := tag.Annotations[annotationBuildVersions] if !ok { continue } displayNameValue := tag.Annotations[annotationBuildVersionsDisplayNames] klog.V(4).Infof("Found build versions from %s: %s (%s)", tag.Name, value, displayNameValue) items, err := parseComponentVersionsLabel(value, displayNameValue) if err != nil { return nil, nil, nil, fmt.Errorf("input image stream has an invalid version annotation for tag %q: %v", tag.Name, value) } for k, v := range items { existing, ok := versions[k] if ok { if existing.Version != v.Version { return nil, nil, nil, fmt.Errorf("input image stream has multiple versions defined for version %s: %s defines %s but was already set to %s on %s", k, tag.Name, v, existing, strings.Join(tagsByName[k], ", ")) } } else { versions[k] = v klog.V(4).Infof("Found version %s=%s from %s", k, v.Version, tag.Name) } tagsByName[k] = append(tagsByName[k], tag.Name) } } defaults, err := parseComponentVersionsLabel(input.Annotations[annotationBuildVersions], input.Annotations[annotationBuildVersionsDisplayNames]) if err != nil { return nil, nil, nil, fmt.Errorf("unable to read default versions label on input image stream: %v", err) } for k, v := range defaults { if _, ok := versions[k]; !ok { versions[k] = v } } return versions, tagsByName, references, nil } type ImageReference struct { SourceRepository string TargetPullSpec string } func NopManifestMapper(data []byte) ([]byte, error) { return data, nil } // patternImageFormat attempts to match a docker pull spec by prefix (%s) and capture the // prefix and either a tag or digest. It requires leading and trailing whitespace, quotes, or // end of file. const patternImageFormat = `([\W]|^)(%s)(:[\w][\w.-]{0,127}|@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{2,})?([\s"']|$)` func NewImageMapper(images map[string]ImageReference) (ManifestMapper, error) { repositories := make([]string, 0, len(images)) bySource := make(map[string]string) for name, ref := range images { if len(ref.SourceRepository) == 0 { return nil, fmt.Errorf("an empty source repository is not allowed for name %q", name) } if existing, ok := bySource[ref.SourceRepository]; ok { return nil, fmt.Errorf("the source repository %q was defined more than once (for %q and %q)", ref.SourceRepository, existing, name) } bySource[ref.SourceRepository] = name repositories = append(repositories, regexp.QuoteMeta(ref.SourceRepository)) } if len(repositories) == 0 { klog.V(5).Infof("No images are mapped, will not replace any contents") return NopManifestMapper, nil } pattern := fmt.Sprintf(patternImageFormat, strings.Join(repositories, "|")) re := regexp.MustCompile(pattern) return func(data []byte) ([]byte, error) { out := re.ReplaceAllFunc(data, func(in []byte) []byte { parts := re.FindSubmatch(in) repository := string(parts[2]) name, ok := bySource[repository] if !ok { klog.V(4).Infof("found potential image %q, but no matching definition", repository) return in } ref := images[name] suffix := parts[3] klog.V(2).Infof("found repository %q with locator %q in the input, switching to %q (from pattern %s)", string(repository), string(suffix), ref.TargetPullSpec, pattern) switch { case len(suffix) == 0: // we found a repository, but no tag or digest (implied latest), or we got an exact match return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) case suffix[0] == '@': // we got a digest return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) default: // TODO: we didn't get a digest, so we have to decide what to replace return []byte(string(parts[1]) + ref.TargetPullSpec + string(parts[4])) } }) return out, nil }, nil } // exactImageFormat attempts to match a string on word boundaries const exactImageFormat = `\b%s\b` func NewExactMapper(mappings map[string]string) (ManifestMapper, error) { patterns := make(map[string]*regexp.Regexp) for from, to := range mappings { pattern := fmt.Sprintf(exactImageFormat, regexp.QuoteMeta(from)) re, err := regexp.Compile(pattern) if err != nil { return nil, err } patterns[to] = re } return func(data []byte) ([]byte, error) { for to, pattern := range patterns { data = pattern.ReplaceAll(data, []byte(to)) } return data, nil }, nil } func ComponentReferencesForImageStream(is *imageapi.ImageStream) (func(string) imagereference.DockerImageReference, error) { components := make(map[string]imagereference.DockerImageReference) for _, tag := range is.Spec.Tags { if tag.From == nil || tag.From.Kind != "DockerImage" { continue } ref, err := imagereference.Parse(tag.From.Name) if err != nil { return nil, fmt.Errorf("reference for %q is invalid: %v", tag.Name, err) } components[tag.Name] = ref } return func(component string) imagereference.DockerImageReference { ref, ok := components[component] if !ok { panic(fmt.Errorf("unknown component %s", component)) } return ref }, nil } const ( componentVersionFormat = `([\W]|^)0\.0\.1-snapshot([a-z0-9\-]*)` ) // NewComponentVersionsMapper substitutes strings of the form 0.0.1-snapshot with releaseName and strings // of the form 0.0.1-snapshot-[component] with the version value located in versions, or returns an error. // tagsByName allows the caller to return an error if references are ambiguous (two tags declare different // version values) - if that replacement is detected and tagsByName[component] has more than one entry, // then an error is returned by the ManifestMapper. // If the input release name is not a semver, a request for `0.0.1-snapshot` will be left unmodified. func NewComponentVersionsMapper(releaseName string, versions ComponentVersions, tagsByName map[string][]string) ManifestMapper { if v, err := semver.Parse(releaseName); err == nil { v.Build = nil releaseName = v.String() } else { releaseName = "" } re, err := regexp.Compile(componentVersionFormat) if err != nil { return func([]byte) ([]byte, error) { return nil, fmt.Errorf("component versions mapper regex: %v", err) } } return func(data []byte) ([]byte, error) { var missing []string var conflicts []string data = re.ReplaceAllFunc(data, func(part []byte) []byte { matches := re.FindSubmatch(part) if matches == nil { return part } key := string(matches[2]) if len(key) == 0 && len(releaseName) > 0 { buf := &bytes.Buffer{} buf.Write(matches[1]) buf.WriteString(releaseName) return buf.Bytes() } if !strings.HasPrefix(key, "-") { return part } key = key[1:] value, ok := versions[key] if !ok { missing = append(missing, key) return part } if len(tagsByName[key]) > 1 { conflicts = append(conflicts, key) return part } buf := &bytes.Buffer{} buf.Write(matches[1]) buf.WriteString(value.Version) return buf.Bytes() }) if len(missing) > 0 { switch len(missing) { case 1: if len(missing[0]) == 0 { return nil, fmt.Errorf("empty version references are not allowed") } return nil, fmt.Errorf("unknown version reference %q", missing[0]) default: return nil, fmt.Errorf("unknown version references: %s", strings.Join(missing, ", ")) } } if len(conflicts) > 0 { allImageTags := tagsByName[conflicts[0]] sort.Strings(allImageTags) return nil, fmt.Errorf("the version for %q is inconsistent across the referenced images: %s", conflicts[0], strings.Join(allImageTags, ", ")) } return data, nil } }
// reAllowedVersionKey limits the allowed component name to a strict subset reAllowedVersionKey = regexp.MustCompile(`^[a-z0-9]+[\-a-z0-9]*[a-z0-9]+$`) // reAllowedDisplayNameKey limits the allowed component name to a strict subset reAllowedDisplayNameKey = regexp.MustCompile(`^[a-zA-Z0-9\-\:\s\(\)]+$`) ) // ComponentVersion includes the version and optional display name. type ComponentVersion struct { Version string DisplayName string } // String returns the version of this component. func (v ComponentVersion) String() string { return v.Version } // ComponentVersions is a map of component names to semantic versions. Names are // lowercase alphanumeric and dashes. Semantic versions will have all build // labels removed, but prerelease segments are preserved. type ComponentVersions map[string]ComponentVersion // OrderedKeys returns the keys in this map in lexigraphic order. func (v ComponentVersions) OrderedKeys() []string { keys := make([]string, 0, len(v)) for k := range v { keys = append(keys, k) } sort.Strings(keys) return keys } func (v ComponentVersions) String() string { return v.VersionLabel() } // VersionLabel formats the ComponentVersions into a valid // versions label. func (v ComponentVersions) VersionLabel() string { var keys []string for k := range v { keys = append(keys, k) } sort.Strings(keys) buf := &bytes.Buffer{} for i, k := range keys { if i != 0 { buf.WriteRune(',') } fmt.Fprintf(buf, "%s=%s", k, v[k].Version) } return buf.String() } // DisplayNameLabel formats the ComponentVersions into a valid display // name label. func (v ComponentVersions) DisplayNameLabel() string { var keys []string for k := range v { keys = append(keys, k) } sort.Strings(keys) buf := &bytes.Buffer{} for i, k := range keys { if i != 0 { buf.WriteRune(',') } if len(v[k].DisplayName) == 0 { continue } fmt.Fprintf(buf, "%s=%s", k, v[k].DisplayName) } return buf.String() } // parseComponentVersionsLabel returns the version labels specified in the string or // an error. Labels are comma-delimited, key=value pairs, and surrounding whitespace is // ignored. Names must be a-z, 0-9, or have interior dashes. All values must be // semantic versions. The displayNames label is optional (if provided) and will be combined // with the valid versions. func parseComponentVersionsLabel(label, displayNames string) (ComponentVersions, error) { label = strings.TrimSpace(label) if len(label) == 0 { return nil, nil } var names map[string]string if len(displayNames) > 0 { names = make(map[string]string) for _, pair := range strings.Split(displayNames, ",") { pair = strings.TrimSpace(pair) parts := strings.SplitN(pair, "=", 2) if len(parts) == 1 { return nil, fmt.Errorf("the display name pair %q must be NAME=DISPLAYNAME", pair) } if len(parts[0]) < 2 { return nil, fmt.Errorf("the version name %q must be at least 2 characters", parts[0]) } if !reAllowedVersionKey.MatchString(parts[0]) { return nil, fmt.Errorf("the version name %q must only be ASCII alphanumerics and internal hyphens", parts[0]) } if !reAllowedDisplayNameKey.MatchString(parts[1]) { return nil, fmt.Errorf("the display name %q must only be alphanumerics, spaces, and symbols in [():-]", parts[1]) } names[parts[0]] = parts[1] } } labels := make(ComponentVersions) if len(label) == 0 { return nil, fmt.Errorf("the version pair must be NAME=VERSION") } for _, pair := range strings.Split(label, ",") { pair = strings.TrimSpace(pair) parts := strings.SplitN(pair, "=", 2) if len(parts) == 1 { return nil, fmt.Errorf("the version pair %q must be NAME=VERSION", pair) } if len(parts[0]) < 2 { return nil, fmt.Errorf("the version name %q must be at least 2 characters", parts[0]) } if !reAllowedVersionKey.MatchString(parts[0]) { return nil, fmt.Errorf("the version name %q must only be ASCII alphanumerics and internal hyphens", parts[0]) } v, err := semver.Parse(parts[1]) if err != nil { return nil, fmt.Errorf("the version pair %q must have a valid semantic version: %v", pair, err) } v.Build = nil labels[parts[0]] = ComponentVersion{ Version: v.String(), DisplayName: names[parts[0]], } } return labels, nil } func ReplacementsForImageStream(is *imageapi.ImageStream, allowTags bool, fn func(component string) imagereference.DockerImageReference) (map[string]string, error) { replacements := make(map[string]string) for i := range is.Spec.Tags { tag := &is.Spec.Tags[i] if tag.From == nil || tag.From.Kind != "DockerImage" { continue } oldImage := tag.From.Name oldRef, err := imagereference.Parse(oldImage) if err != nil { return nil, fmt.Errorf("unable to parse image reference for tag %q from payload: %v", tag.Name, err) } if len(oldRef.Tag) > 0 || len(oldRef.ID) == 0 { if !allowTags { return nil, fmt.Errorf("image reference tag %q in payload does not point to an image digest - unable to rewrite payload", tag.Name) } } ref := fn(tag.Name) if !allowTags { if len(ref.ID) == 0 { ref.Tag = "" ref.ID = oldRef.ID } } newImage := ref.Exact() replacements[oldImage] = newImage tag.From.Name = newImage } if klog.V(5).Enabled() { for k, v := range replacements { klog.Infof("Mapping %s -> %s", k, v) } } return replacements, nil }
var (
random_line_split