repo_name
stringlengths
5
100
path
stringlengths
4
294
copies
stringclasses
990 values
size
stringlengths
4
7
content
stringlengths
666
1M
license
stringclasses
15 values
tklauser/git
contrib/svn-fe/svnrdump_sim.py
328
2044
#!/usr/bin/python """ Simulates svnrdump by replaying an existing dump from a file, taking care of the specified revision range. To simulate incremental imports the environment variable SVNRMAX can be set to the highest revision that should be available. """ import sys import os if sys.hexversion < 0x02040000: # The limiter is the ValueError() calls. This may be too conservative sys.stderr.write("svnrdump-sim.py: requires Python 2.4 or later.\n") sys.exit(1) def getrevlimit(): var = 'SVNRMAX' if var in os.environ: return os.environ[var] return None def writedump(url, lower, upper): if url.startswith('sim://'): filename = url[6:] if filename[-1] == '/': filename = filename[:-1] # remove terminating slash else: raise ValueError('sim:// url required') f = open(filename, 'r') state = 'header' wroterev = False while(True): l = f.readline() if l == '': break if state == 'header' and l.startswith('Revision-number: '): state = 'prefix' if state == 'prefix' and l == 'Revision-number: %s\n' % lower: state = 'selection' if not upper == 'HEAD' and state == 'selection' and \ l == 'Revision-number: %s\n' % upper: break if state == 'header' or state == 'selection': if state == 'selection': wroterev = True sys.stdout.write(l) return wroterev if __name__ == "__main__": if not (len(sys.argv) in (3, 4, 5)): print("usage: %s dump URL -rLOWER:UPPER") sys.exit(1) if not sys.argv[1] == 'dump': raise NotImplementedError('only "dump" is suppported.') url = sys.argv[2] r = ('0', 'HEAD') if len(sys.argv) == 4 and sys.argv[3][0:2] == '-r': r = sys.argv[3][2:].lstrip().split(':') if not getrevlimit() is None: r[1] = getrevlimit() if writedump(url, r[0], r[1]): ret = 0 else: ret = 1 sys.exit(ret)
gpl-2.0
michalliu/OpenWrt-Firefly-Libraries
staging_dir/target-mipsel_1004kc+dsp_uClibc-0.9.33.2/usr/lib/python3.4/difflib.py
72
81679
""" Module difflib -- helpers for computing deltas between objects. Function get_close_matches(word, possibilities, n=3, cutoff=0.6): Use SequenceMatcher to return list of the best "good enough" matches. Function context_diff(a, b): For two lists of strings, return a delta in context diff format. Function ndiff(a, b): Return a delta: the difference between `a` and `b` (lists of strings). Function restore(delta, which): Return one of the two sequences that generated an ndiff delta. Function unified_diff(a, b): For two lists of strings, return a delta in unified diff format. Class SequenceMatcher: A flexible class for comparing pairs of sequences of any type. Class Differ: For producing human-readable deltas from sequences of lines of text. Class HtmlDiff: For producing HTML side by side comparison with change highlights. """ __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher', 'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff', 'unified_diff', 'HtmlDiff', 'Match'] import heapq from collections import namedtuple as _namedtuple Match = _namedtuple('Match', 'a b size') def _calculate_ratio(matches, length): if length: return 2.0 * matches / length return 1.0 class SequenceMatcher: """ SequenceMatcher is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are hashable. The basic algorithm predates, and is a little fancier than, an algorithm published in the late 1980's by Ratcliff and Obershelp under the hyperbolic name "gestalt pattern matching". The basic idea is to find the longest contiguous matching subsequence that contains no "junk" elements (R-O doesn't address junk). The same idea is then applied recursively to the pieces of the sequences to the left and to the right of the matching subsequence. This does not yield minimal edit sequences, but does tend to yield matches that "look right" to people. SequenceMatcher tries to compute a "human-friendly diff" between two sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the longest *contiguous* & junk-free matching subsequence. That's what catches peoples' eyes. The Windows(tm) windiff has another interesting notion, pairing up elements that appear uniquely in each sequence. That, and the method here, appear to yield more intuitive difference reports than does diff. This method appears to be the least vulnerable to synching up on blocks of "junk lines", though (like blank lines in ordinary text files, or maybe "<P>" lines in HTML files). That may be because this is the only method of the 3 that has a *concept* of "junk" <wink>. Example, comparing two strings, and considering blanks to be "junk": >>> s = SequenceMatcher(lambda x: x == " ", ... "private Thread currentThread;", ... "private volatile Thread currentThread;") >>> .ratio() returns a float in [0, 1], measuring the "similarity" of the sequences. As a rule of thumb, a .ratio() value over 0.6 means the sequences are close matches: >>> print(round(s.ratio(), 3)) 0.866 >>> If you're only interested in where the sequences match, .get_matching_blocks() is handy: >>> for block in s.get_matching_blocks(): ... print("a[%d] and b[%d] match for %d elements" % block) a[0] and b[0] match for 8 elements a[8] and b[17] match for 21 elements a[29] and b[38] match for 0 elements Note that the last tuple returned by .get_matching_blocks() is always a dummy, (len(a), len(b), 0), and this is the only case in which the last tuple element (number of elements matched) is 0. If you want to know how to change the first sequence into the second, use .get_opcodes(): >>> for opcode in s.get_opcodes(): ... print("%6s a[%d:%d] b[%d:%d]" % opcode) equal a[0:8] b[0:8] insert a[8:8] b[8:17] equal a[8:29] b[17:38] See the Differ class for a fancy human-friendly file differencer, which uses SequenceMatcher both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines. See also function get_close_matches() in this module, which shows how simple code building on SequenceMatcher can be used to do useful work. Timing: Basic R-O is cubic time worst case and quadratic time expected case. SequenceMatcher is quadratic time for the worst case and has expected-case behavior dependent in a complicated way on how many elements the sequences have in common; best case time is linear. Methods: __init__(isjunk=None, a='', b='') Construct a SequenceMatcher. set_seqs(a, b) Set the two sequences to be compared. set_seq1(a) Set the first sequence to be compared. set_seq2(b) Set the second sequence to be compared. find_longest_match(alo, ahi, blo, bhi) Find longest matching block in a[alo:ahi] and b[blo:bhi]. get_matching_blocks() Return list of triples describing matching subsequences. get_opcodes() Return list of 5-tuples describing how to turn a into b. ratio() Return a measure of the sequences' similarity (float in [0,1]). quick_ratio() Return an upper bound on .ratio() relatively quickly. real_quick_ratio() Return an upper bound on ratio() very quickly. """ def __init__(self, isjunk=None, a='', b='', autojunk=True): """Construct a SequenceMatcher. Optional arg isjunk is None (the default), or a one-argument function that takes a sequence element and returns true iff the element is junk. None is equivalent to passing "lambda x: 0", i.e. no elements are considered to be junk. For example, pass lambda x: x in " \\t" if you're comparing lines as sequences of characters, and don't want to synch up on blanks or hard tabs. Optional arg a is the first of two sequences to be compared. By default, an empty string. The elements of a must be hashable. See also .set_seqs() and .set_seq1(). Optional arg b is the second of two sequences to be compared. By default, an empty string. The elements of b must be hashable. See also .set_seqs() and .set_seq2(). Optional arg autojunk should be set to False to disable the "automatic junk heuristic" that treats popular elements as junk (see module documentation for more information). """ # Members: # a # first sequence # b # second sequence; differences are computed as "what do # we need to do to 'a' to change it into 'b'?" # b2j # for x in b, b2j[x] is a list of the indices (into b) # at which x appears; junk and popular elements do not appear # fullbcount # for x in b, fullbcount[x] == the number of times x # appears in b; only materialized if really needed (used # only for computing quick_ratio()) # matching_blocks # a list of (i, j, k) triples, where a[i:i+k] == b[j:j+k]; # ascending & non-overlapping in i and in j; terminated by # a dummy (len(a), len(b), 0) sentinel # opcodes # a list of (tag, i1, i2, j1, j2) tuples, where tag is # one of # 'replace' a[i1:i2] should be replaced by b[j1:j2] # 'delete' a[i1:i2] should be deleted # 'insert' b[j1:j2] should be inserted # 'equal' a[i1:i2] == b[j1:j2] # isjunk # a user-supplied function taking a sequence element and # returning true iff the element is "junk" -- this has # subtle but helpful effects on the algorithm, which I'll # get around to writing up someday <0.9 wink>. # DON'T USE! Only __chain_b uses this. Use "in self.bjunk". # bjunk # the items in b for which isjunk is True. # bpopular # nonjunk items in b treated as junk by the heuristic (if used). self.isjunk = isjunk self.a = self.b = None self.autojunk = autojunk self.set_seqs(a, b) def set_seqs(self, a, b): """Set the two sequences to be compared. >>> s = SequenceMatcher() >>> s.set_seqs("abcd", "bcde") >>> s.ratio() 0.75 """ self.set_seq1(a) self.set_seq2(b) def set_seq1(self, a): """Set the first sequence to be compared. The second sequence to be compared is not changed. >>> s = SequenceMatcher(None, "abcd", "bcde") >>> s.ratio() 0.75 >>> s.set_seq1("bcde") >>> s.ratio() 1.0 >>> SequenceMatcher computes and caches detailed information about the second sequence, so if you want to compare one sequence S against many sequences, use .set_seq2(S) once and call .set_seq1(x) repeatedly for each of the other sequences. See also set_seqs() and set_seq2(). """ if a is self.a: return self.a = a self.matching_blocks = self.opcodes = None def set_seq2(self, b): """Set the second sequence to be compared. The first sequence to be compared is not changed. >>> s = SequenceMatcher(None, "abcd", "bcde") >>> s.ratio() 0.75 >>> s.set_seq2("abcd") >>> s.ratio() 1.0 >>> SequenceMatcher computes and caches detailed information about the second sequence, so if you want to compare one sequence S against many sequences, use .set_seq2(S) once and call .set_seq1(x) repeatedly for each of the other sequences. See also set_seqs() and set_seq1(). """ if b is self.b: return self.b = b self.matching_blocks = self.opcodes = None self.fullbcount = None self.__chain_b() # For each element x in b, set b2j[x] to a list of the indices in # b where x appears; the indices are in increasing order; note that # the number of times x appears in b is len(b2j[x]) ... # when self.isjunk is defined, junk elements don't show up in this # map at all, which stops the central find_longest_match method # from starting any matching block at a junk element ... # b2j also does not contain entries for "popular" elements, meaning # elements that account for more than 1 + 1% of the total elements, and # when the sequence is reasonably large (>= 200 elements); this can # be viewed as an adaptive notion of semi-junk, and yields an enormous # speedup when, e.g., comparing program files with hundreds of # instances of "return NULL;" ... # note that this is only called when b changes; so for cross-product # kinds of matches, it's best to call set_seq2 once, then set_seq1 # repeatedly def __chain_b(self): # Because isjunk is a user-defined (not C) function, and we test # for junk a LOT, it's important to minimize the number of calls. # Before the tricks described here, __chain_b was by far the most # time-consuming routine in the whole module! If anyone sees # Jim Roskind, thank him again for profile.py -- I never would # have guessed that. # The first trick is to build b2j ignoring the possibility # of junk. I.e., we don't call isjunk at all yet. Throwing # out the junk later is much cheaper than building b2j "right" # from the start. b = self.b self.b2j = b2j = {} for i, elt in enumerate(b): indices = b2j.setdefault(elt, []) indices.append(i) # Purge junk elements self.bjunk = junk = set() isjunk = self.isjunk if isjunk: for elt in b2j.keys(): if isjunk(elt): junk.add(elt) for elt in junk: # separate loop avoids separate list of keys del b2j[elt] # Purge popular elements that are not junk self.bpopular = popular = set() n = len(b) if self.autojunk and n >= 200: ntest = n // 100 + 1 for elt, idxs in b2j.items(): if len(idxs) > ntest: popular.add(elt) for elt in popular: # ditto; as fast for 1% deletion del b2j[elt] def find_longest_match(self, alo, ahi, blo, bhi): """Find longest matching block in a[alo:ahi] and b[blo:bhi]. If isjunk is not defined: Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where alo <= i <= i+k <= ahi blo <= j <= j+k <= bhi and for all (i',j',k') meeting those conditions, k >= k' i <= i' and if i == i', j <= j' In other words, of all maximal matching blocks, return one that starts earliest in a, and of all those maximal matching blocks that start earliest in a, return the one that starts earliest in b. >>> s = SequenceMatcher(None, " abcd", "abcd abcd") >>> s.find_longest_match(0, 5, 0, 9) Match(a=0, b=4, size=5) If isjunk is defined, first the longest matching block is determined as above, but with the additional restriction that no junk element appears in the block. Then that block is extended as far as possible by matching (only) junk elements on both sides. So the resulting block never matches on junk except as identical junk happens to be adjacent to an "interesting" match. Here's the same example as before, but considering blanks to be junk. That prevents " abcd" from matching the " abcd" at the tail end of the second sequence directly. Instead only the "abcd" can match, and matches the leftmost "abcd" in the second sequence: >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") >>> s.find_longest_match(0, 5, 0, 9) Match(a=1, b=0, size=4) If no blocks match, return (alo, blo, 0). >>> s = SequenceMatcher(None, "ab", "c") >>> s.find_longest_match(0, 2, 0, 1) Match(a=0, b=0, size=0) """ # CAUTION: stripping common prefix or suffix would be incorrect. # E.g., # ab # acab # Longest matching block is "ab", but if common prefix is # stripped, it's "a" (tied with "b"). UNIX(tm) diff does so # strip, so ends up claiming that ab is changed to acab by # inserting "ca" in the middle. That's minimal but unintuitive: # "it's obvious" that someone inserted "ac" at the front. # Windiff ends up at the same place as diff, but by pairing up # the unique 'b's and then matching the first two 'a's. a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.bjunk.__contains__ besti, bestj, bestsize = alo, blo, 0 # find longest junk-free match # during an iteration of the loop, j2len[j] = length of longest # junk-free match ending with a[i-1] and b[j] j2len = {} nothing = [] for i in range(alo, ahi): # look at all instances of a[i] in b; note that because # b2j has no junk keys, the loop is skipped if a[i] is junk j2lenget = j2len.get newj2len = {} for j in b2j.get(a[i], nothing): # a[i] matches b[j] if j < blo: continue if j >= bhi: break k = newj2len[j] = j2lenget(j-1, 0) + 1 if k > bestsize: besti, bestj, bestsize = i-k+1, j-k+1, k j2len = newj2len # Extend the best by non-junk elements on each end. In particular, # "popular" non-junk elements aren't in b2j, which greatly speeds # the inner loop above, but also means "the best" match so far # doesn't contain any junk *or* popular non-junk elements. while besti > alo and bestj > blo and \ not isbjunk(b[bestj-1]) and \ a[besti-1] == b[bestj-1]: besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 while besti+bestsize < ahi and bestj+bestsize < bhi and \ not isbjunk(b[bestj+bestsize]) and \ a[besti+bestsize] == b[bestj+bestsize]: bestsize += 1 # Now that we have a wholly interesting match (albeit possibly # empty!), we may as well suck up the matching junk on each # side of it too. Can't think of a good reason not to, and it # saves post-processing the (possibly considerable) expense of # figuring out what to do with it. In the case of an empty # interesting match, this is clearly the right thing to do, # because no other kind of match is possible in the regions. while besti > alo and bestj > blo and \ isbjunk(b[bestj-1]) and \ a[besti-1] == b[bestj-1]: besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 while besti+bestsize < ahi and bestj+bestsize < bhi and \ isbjunk(b[bestj+bestsize]) and \ a[besti+bestsize] == b[bestj+bestsize]: bestsize = bestsize + 1 return Match(besti, bestj, bestsize) def get_matching_blocks(self): """Return list of triples describing matching subsequences. Each triple is of the form (i, j, n), and means that a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in i and in j. New in Python 2.5, it's also guaranteed that if (i, j, n) and (i', j', n') are adjacent triples in the list, and the second is not the last triple in the list, then i+n != i' or j+n != j'. IOW, adjacent triples never describe adjacent equal blocks. The last triple is a dummy, (len(a), len(b), 0), and is the only triple with n==0. >>> s = SequenceMatcher(None, "abxcd", "abcd") >>> list(s.get_matching_blocks()) [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)] """ if self.matching_blocks is not None: return self.matching_blocks la, lb = len(self.a), len(self.b) # This is most naturally expressed as a recursive algorithm, but # at least one user bumped into extreme use cases that exceeded # the recursion limit on their box. So, now we maintain a list # ('queue`) of blocks we still need to look at, and append partial # results to `matching_blocks` in a loop; the matches are sorted # at the end. queue = [(0, la, 0, lb)] matching_blocks = [] while queue: alo, ahi, blo, bhi = queue.pop() i, j, k = x = self.find_longest_match(alo, ahi, blo, bhi) # a[alo:i] vs b[blo:j] unknown # a[i:i+k] same as b[j:j+k] # a[i+k:ahi] vs b[j+k:bhi] unknown if k: # if k is 0, there was no matching block matching_blocks.append(x) if alo < i and blo < j: queue.append((alo, i, blo, j)) if i+k < ahi and j+k < bhi: queue.append((i+k, ahi, j+k, bhi)) matching_blocks.sort() # It's possible that we have adjacent equal blocks in the # matching_blocks list now. Starting with 2.5, this code was added # to collapse them. i1 = j1 = k1 = 0 non_adjacent = [] for i2, j2, k2 in matching_blocks: # Is this block adjacent to i1, j1, k1? if i1 + k1 == i2 and j1 + k1 == j2: # Yes, so collapse them -- this just increases the length of # the first block by the length of the second, and the first # block so lengthened remains the block to compare against. k1 += k2 else: # Not adjacent. Remember the first block (k1==0 means it's # the dummy we started with), and make the second block the # new block to compare against. if k1: non_adjacent.append((i1, j1, k1)) i1, j1, k1 = i2, j2, k2 if k1: non_adjacent.append((i1, j1, k1)) non_adjacent.append( (la, lb, 0) ) self.matching_blocks = list(map(Match._make, non_adjacent)) return self.matching_blocks def get_opcodes(self): """Return list of 5-tuples describing how to turn a into b. Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the tuple preceding it, and likewise for j1 == the previous j2. The tags are strings, with these meanings: 'replace': a[i1:i2] should be replaced by b[j1:j2] 'delete': a[i1:i2] should be deleted. Note that j1==j2 in this case. 'insert': b[j1:j2] should be inserted at a[i1:i1]. Note that i1==i2 in this case. 'equal': a[i1:i2] == b[j1:j2] >>> a = "qabxcd" >>> b = "abycdf" >>> s = SequenceMatcher(None, a, b) >>> for tag, i1, i2, j1, j2 in s.get_opcodes(): ... print(("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))) delete a[0:1] (q) b[0:0] () equal a[1:3] (ab) b[0:2] (ab) replace a[3:4] (x) b[2:3] (y) equal a[4:6] (cd) b[3:5] (cd) insert a[6:6] () b[5:6] (f) """ if self.opcodes is not None: return self.opcodes i = j = 0 self.opcodes = answer = [] for ai, bj, size in self.get_matching_blocks(): # invariant: we've pumped out correct diffs to change # a[:i] into b[:j], and the next matching block is # a[ai:ai+size] == b[bj:bj+size]. So we need to pump # out a diff to change a[i:ai] into b[j:bj], pump out # the matching block, and move (i,j) beyond the match tag = '' if i < ai and j < bj: tag = 'replace' elif i < ai: tag = 'delete' elif j < bj: tag = 'insert' if tag: answer.append( (tag, i, ai, j, bj) ) i, j = ai+size, bj+size # the list of matching blocks is terminated by a # sentinel with size 0 if size: answer.append( ('equal', ai, i, bj, j) ) return answer def get_grouped_opcodes(self, n=3): """ Isolate change clusters by eliminating ranges with no changes. Return a generator of groups with up to n lines of context. Each group is in the same format as returned by get_opcodes(). >>> from pprint import pprint >>> a = list(map(str, range(1,40))) >>> b = a[:] >>> b[8:8] = ['i'] # Make an insertion >>> b[20] += 'x' # Make a replacement >>> b[23:28] = [] # Make a deletion >>> b[30] += 'y' # Make another replacement >>> pprint(list(SequenceMatcher(None,a,b).get_grouped_opcodes())) [[('equal', 5, 8, 5, 8), ('insert', 8, 8, 8, 9), ('equal', 8, 11, 9, 12)], [('equal', 16, 19, 17, 20), ('replace', 19, 20, 20, 21), ('equal', 20, 22, 21, 23), ('delete', 22, 27, 23, 23), ('equal', 27, 30, 23, 26)], [('equal', 31, 34, 27, 30), ('replace', 34, 35, 30, 31), ('equal', 35, 38, 31, 34)]] """ codes = self.get_opcodes() if not codes: codes = [("equal", 0, 1, 0, 1)] # Fixup leading and trailing groups if they show no changes. if codes[0][0] == 'equal': tag, i1, i2, j1, j2 = codes[0] codes[0] = tag, max(i1, i2-n), i2, max(j1, j2-n), j2 if codes[-1][0] == 'equal': tag, i1, i2, j1, j2 = codes[-1] codes[-1] = tag, i1, min(i2, i1+n), j1, min(j2, j1+n) nn = n + n group = [] for tag, i1, i2, j1, j2 in codes: # End the current group and start a new one whenever # there is a large range with no changes. if tag == 'equal' and i2-i1 > nn: group.append((tag, i1, min(i2, i1+n), j1, min(j2, j1+n))) yield group group = [] i1, j1 = max(i1, i2-n), max(j1, j2-n) group.append((tag, i1, i2, j1 ,j2)) if group and not (len(group)==1 and group[0][0] == 'equal'): yield group def ratio(self): """Return a measure of the sequences' similarity (float in [0,1]). Where T is the total number of elements in both sequences, and M is the number of matches, this is 2.0*M / T. Note that this is 1 if the sequences are identical, and 0 if they have nothing in common. .ratio() is expensive to compute if you haven't already computed .get_matching_blocks() or .get_opcodes(), in which case you may want to try .quick_ratio() or .real_quick_ratio() first to get an upper bound. >>> s = SequenceMatcher(None, "abcd", "bcde") >>> s.ratio() 0.75 >>> s.quick_ratio() 0.75 >>> s.real_quick_ratio() 1.0 """ matches = sum(triple[-1] for triple in self.get_matching_blocks()) return _calculate_ratio(matches, len(self.a) + len(self.b)) def quick_ratio(self): """Return an upper bound on ratio() relatively quickly. This isn't defined beyond that it is an upper bound on .ratio(), and is faster to compute. """ # viewing a and b as multisets, set matches to the cardinality # of their intersection; this counts the number of matches # without regard to order, so is clearly an upper bound if self.fullbcount is None: self.fullbcount = fullbcount = {} for elt in self.b: fullbcount[elt] = fullbcount.get(elt, 0) + 1 fullbcount = self.fullbcount # avail[x] is the number of times x appears in 'b' less the # number of times we've seen it in 'a' so far ... kinda avail = {} availhas, matches = avail.__contains__, 0 for elt in self.a: if availhas(elt): numb = avail[elt] else: numb = fullbcount.get(elt, 0) avail[elt] = numb - 1 if numb > 0: matches = matches + 1 return _calculate_ratio(matches, len(self.a) + len(self.b)) def real_quick_ratio(self): """Return an upper bound on ratio() very quickly. This isn't defined beyond that it is an upper bound on .ratio(), and is faster to compute than either .ratio() or .quick_ratio(). """ la, lb = len(self.a), len(self.b) # can't have more matches than the number of elements in the # shorter sequence return _calculate_ratio(min(la, lb), la + lb) def get_close_matches(word, possibilities, n=3, cutoff=0.6): """Use SequenceMatcher to return list of the best "good enough" matches. word is a sequence for which close matches are desired (typically a string). possibilities is a list of sequences against which to match word (typically a list of strings). Optional arg n (default 3) is the maximum number of close matches to return. n must be > 0. Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities that don't score at least that similar to word are ignored. The best (no more than n) matches among the possibilities are returned in a list, sorted by similarity score, most similar first. >>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"]) ['apple', 'ape'] >>> import keyword as _keyword >>> get_close_matches("wheel", _keyword.kwlist) ['while'] >>> get_close_matches("Apple", _keyword.kwlist) [] >>> get_close_matches("accept", _keyword.kwlist) ['except'] """ if not n > 0: raise ValueError("n must be > 0: %r" % (n,)) if not 0.0 <= cutoff <= 1.0: raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,)) result = [] s = SequenceMatcher() s.set_seq2(word) for x in possibilities: s.set_seq1(x) if s.real_quick_ratio() >= cutoff and \ s.quick_ratio() >= cutoff and \ s.ratio() >= cutoff: result.append((s.ratio(), x)) # Move the best scorers to head of list result = heapq.nlargest(n, result) # Strip scores for the best n matches return [x for score, x in result] def _count_leading(line, ch): """ Return number of `ch` characters at the start of `line`. Example: >>> _count_leading(' abc', ' ') 3 """ i, n = 0, len(line) while i < n and line[i] == ch: i += 1 return i class Differ: r""" Differ is a class for comparing sequences of lines of text, and producing human-readable differences or deltas. Differ uses SequenceMatcher both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines. Each line of a Differ delta begins with a two-letter code: '- ' line unique to sequence 1 '+ ' line unique to sequence 2 ' ' line common to both sequences '? ' line not present in either input sequence Lines beginning with '? ' attempt to guide the eye to intraline differences, and were not present in either input sequence. These lines can be confusing if the sequences contain tab characters. Note that Differ makes no claim to produce a *minimal* diff. To the contrary, minimal diffs are often counter-intuitive, because they synch up anywhere possible, sometimes accidental matches 100 pages apart. Restricting synch points to contiguous matches preserves some notion of locality, at the occasional cost of producing a longer diff. Example: Comparing two texts. First we set up the texts, sequences of individual single-line strings ending with newlines (such sequences can also be obtained from the `readlines()` method of file-like objects): >>> text1 = ''' 1. Beautiful is better than ugly. ... 2. Explicit is better than implicit. ... 3. Simple is better than complex. ... 4. Complex is better than complicated. ... '''.splitlines(keepends=True) >>> len(text1) 4 >>> text1[0][-1] '\n' >>> text2 = ''' 1. Beautiful is better than ugly. ... 3. Simple is better than complex. ... 4. Complicated is better than complex. ... 5. Flat is better than nested. ... '''.splitlines(keepends=True) Next we instantiate a Differ object: >>> d = Differ() Note that when instantiating a Differ object we may pass functions to filter out line and character 'junk'. See Differ.__init__ for details. Finally, we compare the two: >>> result = list(d.compare(text1, text2)) 'result' is a list of strings, so let's pretty-print it: >>> from pprint import pprint as _pprint >>> _pprint(result) [' 1. Beautiful is better than ugly.\n', '- 2. Explicit is better than implicit.\n', '- 3. Simple is better than complex.\n', '+ 3. Simple is better than complex.\n', '? ++\n', '- 4. Complex is better than complicated.\n', '? ^ ---- ^\n', '+ 4. Complicated is better than complex.\n', '? ++++ ^ ^\n', '+ 5. Flat is better than nested.\n'] As a single multi-line string it looks like this: >>> print(''.join(result), end="") 1. Beautiful is better than ugly. - 2. Explicit is better than implicit. - 3. Simple is better than complex. + 3. Simple is better than complex. ? ++ - 4. Complex is better than complicated. ? ^ ---- ^ + 4. Complicated is better than complex. ? ++++ ^ ^ + 5. Flat is better than nested. Methods: __init__(linejunk=None, charjunk=None) Construct a text differencer, with optional filters. compare(a, b) Compare two sequences of lines; generate the resulting delta. """ def __init__(self, linejunk=None, charjunk=None): """ Construct a text differencer, with optional filters. The two optional keyword parameters are for filter functions: - `linejunk`: A function that should accept a single string argument, and return true iff the string is junk. The module-level function `IS_LINE_JUNK` may be used to filter out lines without visible characters, except for at most one splat ('#'). It is recommended to leave linejunk None; as of Python 2.3, the underlying SequenceMatcher class has grown an adaptive notion of "noise" lines that's better than any static definition the author has ever been able to craft. - `charjunk`: A function that should accept a string of length 1. The module-level function `IS_CHARACTER_JUNK` may be used to filter out whitespace characters (a blank or tab; **note**: bad idea to include newline in this!). Use of IS_CHARACTER_JUNK is recommended. """ self.linejunk = linejunk self.charjunk = charjunk def compare(self, a, b): r""" Compare two sequences of lines; generate the resulting delta. Each sequence must contain individual single-line strings ending with newlines. Such sequences can be obtained from the `readlines()` method of file-like objects. The delta generated also consists of newline- terminated strings, ready to be printed as-is via the writeline() method of a file-like object. Example: >>> print(''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(True), ... 'ore\ntree\nemu\n'.splitlines(True))), ... end="") - one ? ^ + ore ? ^ - two - three ? - + tree + emu """ cruncher = SequenceMatcher(self.linejunk, a, b) for tag, alo, ahi, blo, bhi in cruncher.get_opcodes(): if tag == 'replace': g = self._fancy_replace(a, alo, ahi, b, blo, bhi) elif tag == 'delete': g = self._dump('-', a, alo, ahi) elif tag == 'insert': g = self._dump('+', b, blo, bhi) elif tag == 'equal': g = self._dump(' ', a, alo, ahi) else: raise ValueError('unknown tag %r' % (tag,)) yield from g def _dump(self, tag, x, lo, hi): """Generate comparison results for a same-tagged range.""" for i in range(lo, hi): yield '%s %s' % (tag, x[i]) def _plain_replace(self, a, alo, ahi, b, blo, bhi): assert alo < ahi and blo < bhi # dump the shorter block first -- reduces the burden on short-term # memory if the blocks are of very different sizes if bhi - blo < ahi - alo: first = self._dump('+', b, blo, bhi) second = self._dump('-', a, alo, ahi) else: first = self._dump('-', a, alo, ahi) second = self._dump('+', b, blo, bhi) for g in first, second: yield from g def _fancy_replace(self, a, alo, ahi, b, blo, bhi): r""" When replacing one block of lines with another, search the blocks for *similar* lines; the best-matching pair (if any) is used as a synch point, and intraline difference marking is done on the similar pair. Lots of work, but often worth it. Example: >>> d = Differ() >>> results = d._fancy_replace(['abcDefghiJkl\n'], 0, 1, ... ['abcdefGhijkl\n'], 0, 1) >>> print(''.join(results), end="") - abcDefghiJkl ? ^ ^ ^ + abcdefGhijkl ? ^ ^ ^ """ # don't synch up unless the lines have a similarity score of at # least cutoff; best_ratio tracks the best score seen so far best_ratio, cutoff = 0.74, 0.75 cruncher = SequenceMatcher(self.charjunk) eqi, eqj = None, None # 1st indices of equal lines (if any) # search for the pair that matches best without being identical # (identical lines must be junk lines, & we don't want to synch up # on junk -- unless we have to) for j in range(blo, bhi): bj = b[j] cruncher.set_seq2(bj) for i in range(alo, ahi): ai = a[i] if ai == bj: if eqi is None: eqi, eqj = i, j continue cruncher.set_seq1(ai) # computing similarity is expensive, so use the quick # upper bounds first -- have seen this speed up messy # compares by a factor of 3. # note that ratio() is only expensive to compute the first # time it's called on a sequence pair; the expensive part # of the computation is cached by cruncher if cruncher.real_quick_ratio() > best_ratio and \ cruncher.quick_ratio() > best_ratio and \ cruncher.ratio() > best_ratio: best_ratio, best_i, best_j = cruncher.ratio(), i, j if best_ratio < cutoff: # no non-identical "pretty close" pair if eqi is None: # no identical pair either -- treat it as a straight replace yield from self._plain_replace(a, alo, ahi, b, blo, bhi) return # no close pair, but an identical pair -- synch up on that best_i, best_j, best_ratio = eqi, eqj, 1.0 else: # there's a close pair, so forget the identical pair (if any) eqi = None # a[best_i] very similar to b[best_j]; eqi is None iff they're not # identical # pump out diffs from before the synch point yield from self._fancy_helper(a, alo, best_i, b, blo, best_j) # do intraline marking on the synch pair aelt, belt = a[best_i], b[best_j] if eqi is None: # pump out a '-', '?', '+', '?' quad for the synched lines atags = btags = "" cruncher.set_seqs(aelt, belt) for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes(): la, lb = ai2 - ai1, bj2 - bj1 if tag == 'replace': atags += '^' * la btags += '^' * lb elif tag == 'delete': atags += '-' * la elif tag == 'insert': btags += '+' * lb elif tag == 'equal': atags += ' ' * la btags += ' ' * lb else: raise ValueError('unknown tag %r' % (tag,)) yield from self._qformat(aelt, belt, atags, btags) else: # the synch pair is identical yield ' ' + aelt # pump out diffs from after the synch point yield from self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi) def _fancy_helper(self, a, alo, ahi, b, blo, bhi): g = [] if alo < ahi: if blo < bhi: g = self._fancy_replace(a, alo, ahi, b, blo, bhi) else: g = self._dump('-', a, alo, ahi) elif blo < bhi: g = self._dump('+', b, blo, bhi) yield from g def _qformat(self, aline, bline, atags, btags): r""" Format "?" output and deal with leading tabs. Example: >>> d = Differ() >>> results = d._qformat('\tabcDefghiJkl\n', '\tabcdefGhijkl\n', ... ' ^ ^ ^ ', ' ^ ^ ^ ') >>> for line in results: print(repr(line)) ... '- \tabcDefghiJkl\n' '? \t ^ ^ ^\n' '+ \tabcdefGhijkl\n' '? \t ^ ^ ^\n' """ # Can hurt, but will probably help most of the time. common = min(_count_leading(aline, "\t"), _count_leading(bline, "\t")) common = min(common, _count_leading(atags[:common], " ")) common = min(common, _count_leading(btags[:common], " ")) atags = atags[common:].rstrip() btags = btags[common:].rstrip() yield "- " + aline if atags: yield "? %s%s\n" % ("\t" * common, atags) yield "+ " + bline if btags: yield "? %s%s\n" % ("\t" * common, btags) # With respect to junk, an earlier version of ndiff simply refused to # *start* a match with a junk element. The result was cases like this: # before: private Thread currentThread; # after: private volatile Thread currentThread; # If you consider whitespace to be junk, the longest contiguous match # not starting with junk is "e Thread currentThread". So ndiff reported # that "e volatil" was inserted between the 't' and the 'e' in "private". # While an accurate view, to people that's absurd. The current version # looks for matching blocks that are entirely junk-free, then extends the # longest one of those as far as possible but only with matching junk. # So now "currentThread" is matched, then extended to suck up the # preceding blank; then "private" is matched, and extended to suck up the # following blank; then "Thread" is matched; and finally ndiff reports # that "volatile " was inserted before "Thread". The only quibble # remaining is that perhaps it was really the case that " volatile" # was inserted after "private". I can live with that <wink>. import re def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match): r""" Return 1 for ignorable line: iff `line` is blank or contains a single '#'. Examples: >>> IS_LINE_JUNK('\n') True >>> IS_LINE_JUNK(' # \n') True >>> IS_LINE_JUNK('hello\n') False """ return pat(line) is not None def IS_CHARACTER_JUNK(ch, ws=" \t"): r""" Return 1 for ignorable character: iff `ch` is a space or tab. Examples: >>> IS_CHARACTER_JUNK(' ') True >>> IS_CHARACTER_JUNK('\t') True >>> IS_CHARACTER_JUNK('\n') False >>> IS_CHARACTER_JUNK('x') False """ return ch in ws ######################################################################## ### Unified Diff ######################################################################## def _format_range_unified(start, stop): 'Convert range to the "ed" format' # Per the diff spec at http://www.unix.org/single_unix_specification/ beginning = start + 1 # lines start numbering with one length = stop - start if length == 1: return '{}'.format(beginning) if not length: beginning -= 1 # empty ranges begin at line just before the range return '{},{}'.format(beginning, length) def unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n'): r""" Compare two sequences of lines; generate the delta as a unified diff. Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three. By default, the diff control lines (those with ---, +++, or @@) are created with a trailing newline. This is helpful so that inputs created from file.readlines() result in diffs that are suitable for file.writelines() since both the inputs and outputs have trailing newlines. For inputs that do not have trailing newlines, set the lineterm argument to "" so that the output will be uniformly newline free. The unidiff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification times are normally expressed in the ISO 8601 format. Example: >>> for line in unified_diff('one two three four'.split(), ... 'zero one tree four'.split(), 'Original', 'Current', ... '2005-01-26 23:30:50', '2010-04-02 10:20:52', ... lineterm=''): ... print(line) # doctest: +NORMALIZE_WHITESPACE --- Original 2005-01-26 23:30:50 +++ Current 2010-04-02 10:20:52 @@ -1,4 +1,4 @@ +zero one -two -three +tree four """ started = False for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n): if not started: started = True fromdate = '\t{}'.format(fromfiledate) if fromfiledate else '' todate = '\t{}'.format(tofiledate) if tofiledate else '' yield '--- {}{}{}'.format(fromfile, fromdate, lineterm) yield '+++ {}{}{}'.format(tofile, todate, lineterm) first, last = group[0], group[-1] file1_range = _format_range_unified(first[1], last[2]) file2_range = _format_range_unified(first[3], last[4]) yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm) for tag, i1, i2, j1, j2 in group: if tag == 'equal': for line in a[i1:i2]: yield ' ' + line continue if tag in {'replace', 'delete'}: for line in a[i1:i2]: yield '-' + line if tag in {'replace', 'insert'}: for line in b[j1:j2]: yield '+' + line ######################################################################## ### Context Diff ######################################################################## def _format_range_context(start, stop): 'Convert range to the "ed" format' # Per the diff spec at http://www.unix.org/single_unix_specification/ beginning = start + 1 # lines start numbering with one length = stop - start if not length: beginning -= 1 # empty ranges begin at line just before the range if length <= 1: return '{}'.format(beginning) return '{},{}'.format(beginning, beginning + length - 1) # See http://www.unix.org/single_unix_specification/ def context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n'): r""" Compare two sequences of lines; generate the delta as a context diff. Context diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three. By default, the diff control lines (those with *** or ---) are created with a trailing newline. This is helpful so that inputs created from file.readlines() result in diffs that are suitable for file.writelines() since both the inputs and outputs have trailing newlines. For inputs that do not have trailing newlines, set the lineterm argument to "" so that the output will be uniformly newline free. The context diff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification times are normally expressed in the ISO 8601 format. If not specified, the strings default to blanks. Example: >>> print(''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(True), ... 'zero\none\ntree\nfour\n'.splitlines(True), 'Original', 'Current')), ... end="") *** Original --- Current *************** *** 1,4 **** one ! two ! three four --- 1,4 ---- + zero one ! tree four """ prefix = dict(insert='+ ', delete='- ', replace='! ', equal=' ') started = False for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n): if not started: started = True fromdate = '\t{}'.format(fromfiledate) if fromfiledate else '' todate = '\t{}'.format(tofiledate) if tofiledate else '' yield '*** {}{}{}'.format(fromfile, fromdate, lineterm) yield '--- {}{}{}'.format(tofile, todate, lineterm) first, last = group[0], group[-1] yield '***************' + lineterm file1_range = _format_range_context(first[1], last[2]) yield '*** {} ****{}'.format(file1_range, lineterm) if any(tag in {'replace', 'delete'} for tag, _, _, _, _ in group): for tag, i1, i2, _, _ in group: if tag != 'insert': for line in a[i1:i2]: yield prefix[tag] + line file2_range = _format_range_context(first[3], last[4]) yield '--- {} ----{}'.format(file2_range, lineterm) if any(tag in {'replace', 'insert'} for tag, _, _, _, _ in group): for tag, _, _, j1, j2 in group: if tag != 'delete': for line in b[j1:j2]: yield prefix[tag] + line def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK): r""" Compare `a` and `b` (lists of strings); return a `Differ`-style delta. Optional keyword parameters `linejunk` and `charjunk` are for filter functions (or None): - linejunk: A function that should accept a single string argument, and return true iff the string is junk. The default is None, and is recommended; as of Python 2.3, an adaptive notion of "noise" lines is used that does a good job on its own. - charjunk: A function that should accept a string of length 1. The default is module-level function IS_CHARACTER_JUNK, which filters out whitespace characters (a blank or tab; note: bad idea to include newline in this!). Tools/scripts/ndiff.py is a command-line front-end to this function. Example: >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True), ... 'ore\ntree\nemu\n'.splitlines(keepends=True)) >>> print(''.join(diff), end="") - one ? ^ + ore ? ^ - two - three ? - + tree + emu """ return Differ(linejunk, charjunk).compare(a, b) def _mdiff(fromlines, tolines, context=None, linejunk=None, charjunk=IS_CHARACTER_JUNK): r"""Returns generator yielding marked up from/to side by side differences. Arguments: fromlines -- list of text lines to compared to tolines tolines -- list of text lines to be compared to fromlines context -- number of context lines to display on each side of difference, if None, all from/to text lines will be generated. linejunk -- passed on to ndiff (see ndiff documentation) charjunk -- passed on to ndiff (see ndiff documentation) This function returns an iterator which returns a tuple: (from line tuple, to line tuple, boolean flag) from/to line tuple -- (line num, line text) line num -- integer or None (to indicate a context separation) line text -- original line text with following markers inserted: '\0+' -- marks start of added text '\0-' -- marks start of deleted text '\0^' -- marks start of changed text '\1' -- marks end of added/deleted/changed text boolean flag -- None indicates context separation, True indicates either "from" or "to" line contains a change, otherwise False. This function/iterator was originally developed to generate side by side file difference for making HTML pages (see HtmlDiff class for example usage). Note, this function utilizes the ndiff function to generate the side by side difference markup. Optional ndiff arguments may be passed to this function and they in turn will be passed to ndiff. """ import re # regular expression for finding intraline change indices change_re = re.compile('(\++|\-+|\^+)') # create the difference iterator to generate the differences diff_lines_iterator = ndiff(fromlines,tolines,linejunk,charjunk) def _make_line(lines, format_key, side, num_lines=[0,0]): """Returns line of text with user's change markup and line formatting. lines -- list of lines from the ndiff generator to produce a line of text from. When producing the line of text to return, the lines used are removed from this list. format_key -- '+' return first line in list with "add" markup around the entire line. '-' return first line in list with "delete" markup around the entire line. '?' return first line in list with add/delete/change intraline markup (indices obtained from second line) None return first line in list with no markup side -- indice into the num_lines list (0=from,1=to) num_lines -- from/to current line number. This is NOT intended to be a passed parameter. It is present as a keyword argument to maintain memory of the current line numbers between calls of this function. Note, this function is purposefully not defined at the module scope so that data it needs from its parent function (within whose context it is defined) does not need to be of module scope. """ num_lines[side] += 1 # Handle case where no user markup is to be added, just return line of # text with user's line format to allow for usage of the line number. if format_key is None: return (num_lines[side],lines.pop(0)[2:]) # Handle case of intraline changes if format_key == '?': text, markers = lines.pop(0), lines.pop(0) # find intraline changes (store change type and indices in tuples) sub_info = [] def record_sub_info(match_object,sub_info=sub_info): sub_info.append([match_object.group(1)[0],match_object.span()]) return match_object.group(1) change_re.sub(record_sub_info,markers) # process each tuple inserting our special marks that won't be # noticed by an xml/html escaper. for key,(begin,end) in sub_info[::-1]: text = text[0:begin]+'\0'+key+text[begin:end]+'\1'+text[end:] text = text[2:] # Handle case of add/delete entire line else: text = lines.pop(0)[2:] # if line of text is just a newline, insert a space so there is # something for the user to highlight and see. if not text: text = ' ' # insert marks that won't be noticed by an xml/html escaper. text = '\0' + format_key + text + '\1' # Return line of text, first allow user's line formatter to do its # thing (such as adding the line number) then replace the special # marks with what the user's change markup. return (num_lines[side],text) def _line_iterator(): """Yields from/to lines of text with a change indication. This function is an iterator. It itself pulls lines from a differencing iterator, processes them and yields them. When it can it yields both a "from" and a "to" line, otherwise it will yield one or the other. In addition to yielding the lines of from/to text, a boolean flag is yielded to indicate if the text line(s) have differences in them. Note, this function is purposefully not defined at the module scope so that data it needs from its parent function (within whose context it is defined) does not need to be of module scope. """ lines = [] num_blanks_pending, num_blanks_to_yield = 0, 0 while True: # Load up next 4 lines so we can look ahead, create strings which # are a concatenation of the first character of each of the 4 lines # so we can do some very readable comparisons. while len(lines) < 4: try: lines.append(next(diff_lines_iterator)) except StopIteration: lines.append('X') s = ''.join([line[0] for line in lines]) if s.startswith('X'): # When no more lines, pump out any remaining blank lines so the # corresponding add/delete lines get a matching blank line so # all line pairs get yielded at the next level. num_blanks_to_yield = num_blanks_pending elif s.startswith('-?+?'): # simple intraline change yield _make_line(lines,'?',0), _make_line(lines,'?',1), True continue elif s.startswith('--++'): # in delete block, add block coming: we do NOT want to get # caught up on blank lines yet, just process the delete line num_blanks_pending -= 1 yield _make_line(lines,'-',0), None, True continue elif s.startswith(('--?+', '--+', '- ')): # in delete block and see a intraline change or unchanged line # coming: yield the delete line and then blanks from_line,to_line = _make_line(lines,'-',0), None num_blanks_to_yield,num_blanks_pending = num_blanks_pending-1,0 elif s.startswith('-+?'): # intraline change yield _make_line(lines,None,0), _make_line(lines,'?',1), True continue elif s.startswith('-?+'): # intraline change yield _make_line(lines,'?',0), _make_line(lines,None,1), True continue elif s.startswith('-'): # delete FROM line num_blanks_pending -= 1 yield _make_line(lines,'-',0), None, True continue elif s.startswith('+--'): # in add block, delete block coming: we do NOT want to get # caught up on blank lines yet, just process the add line num_blanks_pending += 1 yield None, _make_line(lines,'+',1), True continue elif s.startswith(('+ ', '+-')): # will be leaving an add block: yield blanks then add line from_line, to_line = None, _make_line(lines,'+',1) num_blanks_to_yield,num_blanks_pending = num_blanks_pending+1,0 elif s.startswith('+'): # inside an add block, yield the add line num_blanks_pending += 1 yield None, _make_line(lines,'+',1), True continue elif s.startswith(' '): # unchanged text, yield it to both sides yield _make_line(lines[:],None,0),_make_line(lines,None,1),False continue # Catch up on the blank lines so when we yield the next from/to # pair, they are lined up. while(num_blanks_to_yield < 0): num_blanks_to_yield += 1 yield None,('','\n'),True while(num_blanks_to_yield > 0): num_blanks_to_yield -= 1 yield ('','\n'),None,True if s.startswith('X'): raise StopIteration else: yield from_line,to_line,True def _line_pair_iterator(): """Yields from/to lines of text with a change indication. This function is an iterator. It itself pulls lines from the line iterator. Its difference from that iterator is that this function always yields a pair of from/to text lines (with the change indication). If necessary it will collect single from/to lines until it has a matching pair from/to pair to yield. Note, this function is purposefully not defined at the module scope so that data it needs from its parent function (within whose context it is defined) does not need to be of module scope. """ line_iterator = _line_iterator() fromlines,tolines=[],[] while True: # Collecting lines of text until we have a from/to pair while (len(fromlines)==0 or len(tolines)==0): from_line, to_line, found_diff = next(line_iterator) if from_line is not None: fromlines.append((from_line,found_diff)) if to_line is not None: tolines.append((to_line,found_diff)) # Once we have a pair, remove them from the collection and yield it from_line, fromDiff = fromlines.pop(0) to_line, to_diff = tolines.pop(0) yield (from_line,to_line,fromDiff or to_diff) # Handle case where user does not want context differencing, just yield # them up without doing anything else with them. line_pair_iterator = _line_pair_iterator() if context is None: while True: yield next(line_pair_iterator) # Handle case where user wants context differencing. We must do some # storage of lines until we know for sure that they are to be yielded. else: context += 1 lines_to_write = 0 while True: # Store lines up until we find a difference, note use of a # circular queue because we only need to keep around what # we need for context. index, contextLines = 0, [None]*(context) found_diff = False while(found_diff is False): from_line, to_line, found_diff = next(line_pair_iterator) i = index % context contextLines[i] = (from_line, to_line, found_diff) index += 1 # Yield lines that we have collected so far, but first yield # the user's separator. if index > context: yield None, None, None lines_to_write = context else: lines_to_write = index index = 0 while(lines_to_write): i = index % context index += 1 yield contextLines[i] lines_to_write -= 1 # Now yield the context lines after the change lines_to_write = context-1 while(lines_to_write): from_line, to_line, found_diff = next(line_pair_iterator) # If another change within the context, extend the context if found_diff: lines_to_write = context-1 else: lines_to_write -= 1 yield from_line, to_line, found_diff _file_template = """ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title></title> <style type="text/css">%(styles)s </style> </head> <body> %(table)s%(legend)s </body> </html>""" _styles = """ table.diff {font-family:Courier; border:medium;} .diff_header {background-color:#e0e0e0} td.diff_header {text-align:right} .diff_next {background-color:#c0c0c0} .diff_add {background-color:#aaffaa} .diff_chg {background-color:#ffff77} .diff_sub {background-color:#ffaaaa}""" _table_template = """ <table class="diff" id="difflib_chg_%(prefix)s_top" cellspacing="0" cellpadding="0" rules="groups" > <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> %(header_row)s <tbody> %(data_rows)s </tbody> </table>""" _legend = """ <table class="diff" summary="Legends"> <tr> <th colspan="2"> Legends </th> </tr> <tr> <td> <table border="" summary="Colors"> <tr><th> Colors </th> </tr> <tr><td class="diff_add">&nbsp;Added&nbsp;</td></tr> <tr><td class="diff_chg">Changed</td> </tr> <tr><td class="diff_sub">Deleted</td> </tr> </table></td> <td> <table border="" summary="Links"> <tr><th colspan="2"> Links </th> </tr> <tr><td>(f)irst change</td> </tr> <tr><td>(n)ext change</td> </tr> <tr><td>(t)op</td> </tr> </table></td> </tr> </table>""" class HtmlDiff(object): """For producing HTML side by side comparison with change highlights. This class can be used to create an HTML table (or a complete HTML file containing the table) showing a side by side, line by line comparison of text with inter-line and intra-line change highlights. The table can be generated in either full or contextual difference mode. The following methods are provided for HTML generation: make_table -- generates HTML for a single side by side table make_file -- generates complete HTML file with a single side by side table See tools/scripts/diff.py for an example usage of this class. """ _file_template = _file_template _styles = _styles _table_template = _table_template _legend = _legend _default_prefix = 0 def __init__(self,tabsize=8,wrapcolumn=None,linejunk=None, charjunk=IS_CHARACTER_JUNK): """HtmlDiff instance initializer Arguments: tabsize -- tab stop spacing, defaults to 8. wrapcolumn -- column number where lines are broken and wrapped, defaults to None where lines are not wrapped. linejunk,charjunk -- keyword arguments passed into ndiff() (used to by HtmlDiff() to generate the side by side HTML differences). See ndiff() documentation for argument default values and descriptions. """ self._tabsize = tabsize self._wrapcolumn = wrapcolumn self._linejunk = linejunk self._charjunk = charjunk def make_file(self,fromlines,tolines,fromdesc='',todesc='',context=False, numlines=5): """Returns HTML file of side by side comparison with change highlights Arguments: fromlines -- list of "from" lines tolines -- list of "to" lines fromdesc -- "from" file column header string todesc -- "to" file column header string context -- set to True for contextual differences (defaults to False which shows full differences). numlines -- number of context lines. When context is set True, controls number of lines displayed before and after the change. When context is False, controls the number of lines to place the "next" link anchors before the next change (so click of "next" link jumps to just before the change). """ return self._file_template % dict( styles = self._styles, legend = self._legend, table = self.make_table(fromlines,tolines,fromdesc,todesc, context=context,numlines=numlines)) def _tab_newline_replace(self,fromlines,tolines): """Returns from/to line lists with tabs expanded and newlines removed. Instead of tab characters being replaced by the number of spaces needed to fill in to the next tab stop, this function will fill the space with tab characters. This is done so that the difference algorithms can identify changes in a file when tabs are replaced by spaces and vice versa. At the end of the HTML generation, the tab characters will be replaced with a nonbreakable space. """ def expand_tabs(line): # hide real spaces line = line.replace(' ','\0') # expand tabs into spaces line = line.expandtabs(self._tabsize) # replace spaces from expanded tabs back into tab characters # (we'll replace them with markup after we do differencing) line = line.replace(' ','\t') return line.replace('\0',' ').rstrip('\n') fromlines = [expand_tabs(line) for line in fromlines] tolines = [expand_tabs(line) for line in tolines] return fromlines,tolines def _split_line(self,data_list,line_num,text): """Builds list of text lines by splitting text lines at wrap point This function will determine if the input text line needs to be wrapped (split) into separate lines. If so, the first wrap point will be determined and the first line appended to the output text line list. This function is used recursively to handle the second part of the split line to further split it. """ # if blank line or context separator, just add it to the output list if not line_num: data_list.append((line_num,text)) return # if line text doesn't need wrapping, just add it to the output list size = len(text) max = self._wrapcolumn if (size <= max) or ((size -(text.count('\0')*3)) <= max): data_list.append((line_num,text)) return # scan text looking for the wrap point, keeping track if the wrap # point is inside markers i = 0 n = 0 mark = '' while n < max and i < size: if text[i] == '\0': i += 1 mark = text[i] i += 1 elif text[i] == '\1': i += 1 mark = '' else: i += 1 n += 1 # wrap point is inside text, break it up into separate lines line1 = text[:i] line2 = text[i:] # if wrap point is inside markers, place end marker at end of first # line and start marker at beginning of second line because each # line will have its own table tag markup around it. if mark: line1 = line1 + '\1' line2 = '\0' + mark + line2 # tack on first line onto the output list data_list.append((line_num,line1)) # use this routine again to wrap the remaining text self._split_line(data_list,'>',line2) def _line_wrapper(self,diffs): """Returns iterator that splits (wraps) mdiff text lines""" # pull from/to data and flags from mdiff iterator for fromdata,todata,flag in diffs: # check for context separators and pass them through if flag is None: yield fromdata,todata,flag continue (fromline,fromtext),(toline,totext) = fromdata,todata # for each from/to line split it at the wrap column to form # list of text lines. fromlist,tolist = [],[] self._split_line(fromlist,fromline,fromtext) self._split_line(tolist,toline,totext) # yield from/to line in pairs inserting blank lines as # necessary when one side has more wrapped lines while fromlist or tolist: if fromlist: fromdata = fromlist.pop(0) else: fromdata = ('',' ') if tolist: todata = tolist.pop(0) else: todata = ('',' ') yield fromdata,todata,flag def _collect_lines(self,diffs): """Collects mdiff output into separate lists Before storing the mdiff from/to data into a list, it is converted into a single line of text with HTML markup. """ fromlist,tolist,flaglist = [],[],[] # pull from/to data and flags from mdiff style iterator for fromdata,todata,flag in diffs: try: # store HTML markup of the lines into the lists fromlist.append(self._format_line(0,flag,*fromdata)) tolist.append(self._format_line(1,flag,*todata)) except TypeError: # exceptions occur for lines where context separators go fromlist.append(None) tolist.append(None) flaglist.append(flag) return fromlist,tolist,flaglist def _format_line(self,side,flag,linenum,text): """Returns HTML markup of "from" / "to" text lines side -- 0 or 1 indicating "from" or "to" text flag -- indicates if difference on line linenum -- line number (used for line number column) text -- line text to be marked up """ try: linenum = '%d' % linenum id = ' id="%s%s"' % (self._prefix[side],linenum) except TypeError: # handle blank lines where linenum is '>' or '' id = '' # replace those things that would get confused with HTML symbols text=text.replace("&","&amp;").replace(">","&gt;").replace("<","&lt;") # make space non-breakable so they don't get compressed or line wrapped text = text.replace(' ','&nbsp;').rstrip() return '<td class="diff_header"%s>%s</td><td nowrap="nowrap">%s</td>' \ % (id,linenum,text) def _make_prefix(self): """Create unique anchor prefixes""" # Generate a unique anchor prefix so multiple tables # can exist on the same HTML page without conflicts. fromprefix = "from%d_" % HtmlDiff._default_prefix toprefix = "to%d_" % HtmlDiff._default_prefix HtmlDiff._default_prefix += 1 # store prefixes so line format method has access self._prefix = [fromprefix,toprefix] def _convert_flags(self,fromlist,tolist,flaglist,context,numlines): """Makes list of "next" links""" # all anchor names will be generated using the unique "to" prefix toprefix = self._prefix[1] # process change flags, generating middle column of next anchors/links next_id = ['']*len(flaglist) next_href = ['']*len(flaglist) num_chg, in_change = 0, False last = 0 for i,flag in enumerate(flaglist): if flag: if not in_change: in_change = True last = i # at the beginning of a change, drop an anchor a few lines # (the context lines) before the change for the previous # link i = max([0,i-numlines]) next_id[i] = ' id="difflib_chg_%s_%d"' % (toprefix,num_chg) # at the beginning of a change, drop a link to the next # change num_chg += 1 next_href[last] = '<a href="#difflib_chg_%s_%d">n</a>' % ( toprefix,num_chg) else: in_change = False # check for cases where there is no content to avoid exceptions if not flaglist: flaglist = [False] next_id = [''] next_href = [''] last = 0 if context: fromlist = ['<td></td><td>&nbsp;No Differences Found&nbsp;</td>'] tolist = fromlist else: fromlist = tolist = ['<td></td><td>&nbsp;Empty File&nbsp;</td>'] # if not a change on first line, drop a link if not flaglist[0]: next_href[0] = '<a href="#difflib_chg_%s_0">f</a>' % toprefix # redo the last link to link to the top next_href[last] = '<a href="#difflib_chg_%s_top">t</a>' % (toprefix) return fromlist,tolist,flaglist,next_href,next_id def make_table(self,fromlines,tolines,fromdesc='',todesc='',context=False, numlines=5): """Returns HTML table of side by side comparison with change highlights Arguments: fromlines -- list of "from" lines tolines -- list of "to" lines fromdesc -- "from" file column header string todesc -- "to" file column header string context -- set to True for contextual differences (defaults to False which shows full differences). numlines -- number of context lines. When context is set True, controls number of lines displayed before and after the change. When context is False, controls the number of lines to place the "next" link anchors before the next change (so click of "next" link jumps to just before the change). """ # make unique anchor prefixes so that multiple tables may exist # on the same page without conflict. self._make_prefix() # change tabs to spaces before it gets more difficult after we insert # markup fromlines,tolines = self._tab_newline_replace(fromlines,tolines) # create diffs iterator which generates side by side from/to data if context: context_lines = numlines else: context_lines = None diffs = _mdiff(fromlines,tolines,context_lines,linejunk=self._linejunk, charjunk=self._charjunk) # set up iterator to wrap lines that exceed desired width if self._wrapcolumn: diffs = self._line_wrapper(diffs) # collect up from/to lines and flags into lists (also format the lines) fromlist,tolist,flaglist = self._collect_lines(diffs) # process change flags, generating middle column of next anchors/links fromlist,tolist,flaglist,next_href,next_id = self._convert_flags( fromlist,tolist,flaglist,context,numlines) s = [] fmt = ' <tr><td class="diff_next"%s>%s</td>%s' + \ '<td class="diff_next">%s</td>%s</tr>\n' for i in range(len(flaglist)): if flaglist[i] is None: # mdiff yields None on separator lines skip the bogus ones # generated for the first line if i > 0: s.append(' </tbody> \n <tbody>\n') else: s.append( fmt % (next_id[i],next_href[i],fromlist[i], next_href[i],tolist[i])) if fromdesc or todesc: header_row = '<thead><tr>%s%s%s%s</tr></thead>' % ( '<th class="diff_next"><br /></th>', '<th colspan="2" class="diff_header">%s</th>' % fromdesc, '<th class="diff_next"><br /></th>', '<th colspan="2" class="diff_header">%s</th>' % todesc) else: header_row = '' table = self._table_template % dict( data_rows=''.join(s), header_row=header_row, prefix=self._prefix[1]) return table.replace('\0+','<span class="diff_add">'). \ replace('\0-','<span class="diff_sub">'). \ replace('\0^','<span class="diff_chg">'). \ replace('\1','</span>'). \ replace('\t','&nbsp;') del re def restore(delta, which): r""" Generate one of the two sequences that generated a delta. Given a `delta` produced by `Differ.compare()` or `ndiff()`, extract lines originating from file 1 or 2 (parameter `which`), stripping off line prefixes. Examples: >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True), ... 'ore\ntree\nemu\n'.splitlines(keepends=True)) >>> diff = list(diff) >>> print(''.join(restore(diff, 1)), end="") one two three >>> print(''.join(restore(diff, 2)), end="") ore tree emu """ try: tag = {1: "- ", 2: "+ "}[int(which)] except KeyError: raise ValueError('unknown delta choice (must be 1 or 2): %r' % which) prefixes = (" ", tag) for line in delta: if line[:2] in prefixes: yield line[2:] def _test(): import doctest, difflib return doctest.testmod(difflib) if __name__ == "__main__": _test()
gpl-2.0
lordeko/du_kernel_samsung_hlte
arch/ia64/scripts/unwcheck.py
13143
1714
#!/usr/bin/python # # Usage: unwcheck.py FILE # # This script checks the unwind info of each function in file FILE # and verifies that the sum of the region-lengths matches the total # length of the function. # # Based on a shell/awk script originally written by Harish Patil, # which was converted to Perl by Matthew Chapman, which was converted # to Python by David Mosberger. # import os import re import sys if len(sys.argv) != 2: print "Usage: %s FILE" % sys.argv[0] sys.exit(2) readelf = os.getenv("READELF", "readelf") start_pattern = re.compile("<([^>]*)>: \[0x([0-9a-f]+)-0x([0-9a-f]+)\]") rlen_pattern = re.compile(".*rlen=([0-9]+)") def check_func (func, slots, rlen_sum): if slots != rlen_sum: global num_errors num_errors += 1 if not func: func = "[%#x-%#x]" % (start, end) print "ERROR: %s: %lu slots, total region length = %lu" % (func, slots, rlen_sum) return num_funcs = 0 num_errors = 0 func = False slots = 0 rlen_sum = 0 for line in os.popen("%s -u %s" % (readelf, sys.argv[1])): m = start_pattern.match(line) if m: check_func(func, slots, rlen_sum) func = m.group(1) start = long(m.group(2), 16) end = long(m.group(3), 16) slots = 3 * (end - start) / 16 rlen_sum = 0L num_funcs += 1 else: m = rlen_pattern.match(line) if m: rlen_sum += long(m.group(1)) check_func(func, slots, rlen_sum) if num_errors == 0: print "No errors detected in %u functions." % num_funcs else: if num_errors > 1: err="errors" else: err="error" print "%u %s detected in %u functions." % (num_errors, err, num_funcs) sys.exit(1)
gpl-2.0
scizen9/kpy
flask/stats_web.py
1
20311
''' Modified from online verion of: _README: https://github.com/bokeh/bokeh/blob/master/examples/app/stocks/README.md .. note:: Running this example requires having the "stats.log" file. Use the ``bokeh serve`` command to run the example by executing: bokeh serve stocks at your command prompt. Then navigate to the URL http://localhost:5006/stocks .. ''' try: from functools import lru_cache except ImportError: # Python 2 does stdlib does not have lru_cache so let's just # create a dummy decorator to avoid crashing print ("WARNING: Cache for this example is available on Python 3 only.") def lru_cache(): def dec(f): def _(*args, **kws): return f(*args, **kws) return _ return dec from os.path import dirname, join import pandas as pd import datetime import numpy as np import os from bokeh.io import curdoc from bokeh.layouts import row, column from bokeh.models import ColumnDataSource, Label, CDSView, GroupFilter, Range1d, LinearAxis from bokeh.models import HoverTool from bokeh.models.annotations import BoxAnnotation from bokeh.models.widgets import PreText, Select from bokeh.plotting import figure from bokeh.core.properties import value from bokeh.palettes import Paired from astropy.time import Time from astropy.coordinates import SkyCoord, EarthLocation, AltAz, get_sun, get_moon import astropy.units as u #from datetime import datetime, timedelta import datetime import model @lru_cache() def load_p48seeing(obsdate): time, seeing = model.get_p18obsdata(obsdate) day_frac_diff = datetime.timedelta(np.ceil((datetime.datetime.now() - datetime.datetime.utcnow() ).total_seconds())/3600/24) local_date = np.array(time) + day_frac_diff d = pd.DataFrame({'date':local_date, 'seeing':seeing}) return d @lru_cache() def load_stats(statsfile='stats.log'): data = pd.read_csv(statsfile, header=None, names=['path', 'obj', 'jd', 'ns', 'fwhm', 'ellipticity', 'bkg', 'airmass', 'in_temp', 'imtype', 'out_temp', 'in_hum']) jds = data['jd'] t = Time(jds, format='jd', scale='utc') date = t.utc.datetime day_frac_diff = datetime.timedelta(np.ceil((datetime.datetime.now() - datetime.datetime.utcnow() ).total_seconds())/3600/24) local_date = date + day_frac_diff data2 = data.assign(localdate=local_date) data2.set_index('localdate') return pd.DataFrame({'date':data2['localdate'], 'ns':data2['ns'], 'fwhm':data2['fwhm'], 'ellipticity':data2['ellipticity'], \ 'bkg':data2['bkg'], 'airmass':data2['airmass'], 'in_temp':data2['in_temp'], 'imtype':data2['imtype'],\ 'out_temp':data2['out_temp'], 'in_hum':data2['in_hum']}) @lru_cache() def plot_stats(statsfile, mydate): source = ColumnDataSource(data=dict(date=[], ns=[], fwhm=[], ellipticity=[], bkg=[], airmass=[], in_temp=[], imtype=[], out_temp=[], in_hum=[])) source_static = ColumnDataSource(data=dict(date=[], ns=[], fwhm=[], ellipticity=[], bkg=[], airmass=[], in_temp=[], imtype=[], out_temp=[], in_hum=[])) viewScience = CDSView(source=source, filters=[GroupFilter(column_name='imtype', group='SCIENCE')]) viewAcquisition = CDSView(source=source, filters=[GroupFilter(column_name='imtype', group='ACQUISITION')]) viewGuider = CDSView(source=source, filters=[GroupFilter(column_name='imtype', group='GUIDER')]) viewFocus = CDSView(source=source, filters=[GroupFilter(column_name='imtype', group='FOCUS')]) source_p48 = ColumnDataSource(data=dict(date=[], seeing=[])) def update(selected=None): if statsfile: data = load_stats(statsfile) source.data = source.from_df(data[['date', 'ns', 'fwhm', 'ellipticity', 'bkg', 'airmass', 'in_temp', 'imtype', 'out_temp', 'in_hum']]) source_static.data = source.data p48 = load_p48seeing(mydate) source_p48.data = source_p48.from_df(p48[['date', 'seeing']]) source_static_p48.data = source_p48.data source_static_p48 = ColumnDataSource(data=dict(date=[], seeing=[])) tools = 'pan,box_zoom,reset' p48seeing = figure(plot_width=425, plot_height=250, tools=tools, x_axis_type='datetime', active_drag="box_zoom") p48seeing.circle('date', 'seeing', source=source_static_p48, color="black") p48seeing.title.text = "P18 seeing [arcsec]" if statsfile: ns = figure(plot_width=425, plot_height=250, tools=tools, x_axis_type='datetime', active_drag="box_zoom") ns.line('date', 'ns', source=source_static) ns.circle('date', 'ns', size=1, source=source, color=None, selection_color="orange") ns.title.text = "Number of bright sources extracted" bkg = figure(plot_width=425, plot_height=250, tools=tools, x_axis_type='datetime', active_drag="box_zoom") bkg.x_range = ns.x_range bkg.line('date', 'bkg', source=source_static) bkg.circle('date', 'bkg', size=1, source=source, color=None, selection_color="orange") bkg.title.text = "Background (counts)" temp = figure(plot_width=425, plot_height=250, tools=tools, x_axis_type='datetime', active_drag="box_zoom") temp.x_range = ns.x_range temp.line('date', 'in_temp', source=source_static, color='blue', legend="Inside") temp.line('date', 'out_temp', source=source_static, color='green', legend="Outside") temp.circle('date', 'in_temp', size=1, source=source, color=None, selection_color="orange") temp.title.text = "Temperature [C]" fwhm = figure(plot_width=425, plot_height=250, tools=tools, x_axis_type='datetime', active_drag="box_zoom") fwhm.x_range = ns.x_range fwhm.circle('date', 'fwhm', source=source_static, color="green", legend="Focus", view=viewFocus) fwhm.circle('date', 'fwhm', source=source_static, color="red", legend="Science", view=viewScience) fwhm.circle('date', 'fwhm', source=source_static, color="blue", legend="Acquisition", view=viewAcquisition) fwhm.circle('date', 'fwhm', source=source_static, color="black", legend="Guider", view=viewGuider) fwhm.circle('date', 'fwhm', size=1, source=source, color=None, selection_color="orange") fwhm.title.text = "P60 FWHM [arcsec]" airmass = figure(plot_width=425, plot_height=250, tools=tools, x_axis_type='datetime', active_drag="box_zoom") airmass.x_range = ns.x_range airmass.line('date', 'airmass', source=source_static) airmass.circle('date', 'airmass', size=1, source=source, color=None, selection_color="orange") airmass.title.text = "Airmass" ellipticity = figure(plot_width=425, plot_height=250, tools=tools, x_axis_type='datetime', active_drag="box_zoom") ellipticity.x_range = ns.x_range ellipticity.line('date', 'ellipticity', source=source_static) ellipticity.circle('date', 'ellipticity', size=1, source=source, color=None, selection_color="orange") ellipticity.title.text = "Ellipticity" humidity = figure(plot_width=425, plot_height=250, tools=tools, x_axis_type='datetime', active_drag="box_zoom") humidity.x_range = ns.x_range humidity.line('date', 'in_hum', source=source_static) humidity.circle('date', 'in_hum', size=1, source=source, color=None, selection_color="orange") humidity.title.text = "Inside Humidity [%]" p48seeing.x_range = ns.x_range left = column(fwhm, p48seeing, airmass) center = column(ellipticity, ns, bkg, ) right = column(temp, humidity) layout = row(left, center, right) else: layout = row(column(p48seeing)) # initialize update() curdoc().add_root(layout) curdoc().title = "Stats" return layout @lru_cache() def plot_not_found_message(day): not_found = figure(plot_width=900, plot_height=450, x_range=[0, 900], y_range=[0, 450]) not_found.image(image=[np.zeros([900, 450])+0.1], x=0, y=0, dw=900, dh=450) citation = Label(x=50, y=225, x_units='screen', y_units='screen', text='No statistics found for today \n (likely we were weathered out...)') not_found.add_layout(citation) not_found.title.text = "Statistics not found for day %s"%(day) layout = column(not_found) curdoc().add_root(layout) curdoc().title = "Stats not found" @lru_cache() def plot_stats_allocation(data): """ Plots in the shape of bars the time available and spent for each active allocation. """ #Create the first plot with the allocation hours alloc_names = data['allocations'] categories = ["spent_hours", "free_hours"] colors = [ "#e84d60", "darkgreen"] #"#c9d9d3" N = len(alloc_names) source = ColumnDataSource(data=data) p = figure(x_range=alloc_names, plot_height=420, plot_width=80*8, title="Time spent/available for SEDM allocations this term", toolbar_location=None, tools="") p.vbar_stack(categories, x='allocations', width=0.9, color=colors, source=source, legend=["Spent", "Available"]) p.y_range.start = 0 p.x_range.range_padding = 0.1 p.xgrid.grid_line_color = None p.axis.minor_tick_line_color = None p.outline_line_color = None p.legend.location = "top_right" p.legend.orientation = "horizontal" p.yaxis.axis_label = 'Hours' p.xaxis.major_label_orientation = 0.3 #Create the second plot with the % spent alloc_names = data['allocations'] percentage = (data["spent_hours"] / data["alloc_hours"]) * 100 colors=N*['#084594'] '''for i, p in enumerate(percentage): if p<50: colors[i] = '#22A784' elif p>50 and p<75: colors[i] = '#FD9F6C' else: colors[i] = '#DD4968''' source = ColumnDataSource(data=dict(alloc_names=alloc_names, percentage=percentage, color=colors)) p2 = figure(x_range=alloc_names, y_range=(0,100), plot_height=420, plot_width=80*8, title="Percentage of time spent", toolbar_location=None, tools="") p2.vbar(x='alloc_names', top='percentage', width=0.9, color='color', source=source) p2.xgrid.grid_line_color = None p2.legend.orientation = "horizontal" p2.legend.location = "top_center" p2.yaxis.axis_label = '% time spent' p2.xaxis.major_label_orientation = 0.3 #Create the pie charts pieColors = 10*["red", "green", "blue", "orange", "yellow", 'lime', 'brown', 'cyan', \ 'magenta', 'olive', 'black', 'teal', 'gold', 'crimson', 'moccasin', 'greenyellow', 'navy', 'ivory', 'lightpink'] #First one with the time spent # define starts/ends for wedges from percentages of a circle percents_only = np.round( np.array(list(data["spent_hours"] / np.sum(data["spent_hours"])))*100, 1) percents = np.cumsum( [0] + list(data["spent_hours"] / np.sum(data["spent_hours"]))) starts = [per*2*np.pi for per in percents[:-1]] ends = [per*2*np.pi for per in percents[1:]] p3 = figure(x_range=(-1, 2.5), y_range=(-1.1, 1.1), plot_height=420, plot_width=600, title="% spent") #Add individual wedges: for i in range(N): p3.wedge(x=0, y=0, radius=.9, start_angle=starts[i], end_angle=ends[i], color=pieColors[i], legend="[{0}%] {1}".format(percents_only[i], alloc_names[i]) ) p3.xgrid.grid_line_color = None p3.ygrid.grid_line_color = None p3.legend.orientation = "vertical" p3.legend.location = "top_right" p3.legend.border_line_alpha = 0 p3.legend.background_fill_color = None p3.xaxis.visible = False p3.yaxis.visible = False #Second one with the time allocated # define starts/ends for wedges from percentages of a circle percents_only = np.round( np.array(list(data["alloc_hours"] / np.sum(data["alloc_hours"])))*100, 1) percents = np.cumsum( [0] + list(data["alloc_hours"] / np.sum(data["alloc_hours"]))) starts = [per*2*np.pi for per in percents[:-1]] ends = [per*2*np.pi for per in percents[1:]] p4 = figure(x_range=(-1, 2.5), y_range=(-1.1, 1.1), plot_height=420, plot_width=600, title="% time allocated to each program") #Add individual wedges: for i in range(N): p4.wedge(x=0, y=0, radius=.9, start_angle=starts[i], end_angle=ends[i], color=pieColors[i], legend="[{0}%] {1}".format(percents_only[i], alloc_names[i]) ) p4.xgrid.grid_line_color = None p4.ygrid.grid_line_color = None p4.legend.orientation = "vertical" p4.legend.location = "top_right" p4.legend.border_line_alpha = 0 p4.legend.background_fill_color = None p4.xaxis.visible = False p4.yaxis.visible = False layout = row(column(p, p2), column(p4, p3)) curdoc().add_root(layout) curdoc().title = "Allocation stats" return layout def plot_visibility(ras, decs, names, allocs=[None], priorities=[5], endobs=[None], exptime=2430, date=None, allowed_allocs=[None]): ''' makes a visibility plot for one or many objects, highlighting observed patches if relevant all these arguments are lists/arrays/iterables, even if they are of size 1 priorities: integers obsd: list/array of observed objects, should match 'names' endobs: 'YYYY-MM-DDTHH:MM:SS.ssssssssss' (as outputed from sql query) of the time the observation ended exptime: in seconds date: YYYYMMDD, conveniently matching the folder names, of midnight allowed_allocs: list of string allocation names visible to a user''' allocpalette = Paired[12][1::2] + Paired[12][::2] priorities = np.array(priorities, dtype=np.int) allocs = np.asarray(allocs) names = np.asarray(names) allowed_allocs = np.asarray(allowed_allocs) allocs[~np.isin(allocs, allowed_allocs)] = 'other' p = figure(plot_width=700, plot_height=500, toolbar_location='above', y_range=(0, 90), y_axis_location="right") ### setup with axes, sun/moon, frames, background palomar_mountain = EarthLocation(lon=243.1361*u.deg, lat=33.3558*u.deg, height=1712*u.m) utcoffset = -7 * u.hour # Pacific Daylight Time if date == None: time = (Time.now() - utcoffset).datetime # date is based on local time time = Time(datetime.datetime(time.year, time.month, time.day)) else: time = Time(datetime.datetime(int(date[:4]), int(date[4:6]), int(date[6:8]))) midnight = time - utcoffset # 7am local time of correct date, midnight UTC if endobs[0] != None: endobs = Time(np.array(endobs, dtype='|S32'), format='isot') endobs.format = u'datetime' delta_midnight = np.linspace(-8, 8, 500) * u.hour t = midnight + delta_midnight abstimes = [i.datetime.strftime('%I:%M %p') for i in t + utcoffset] frame = AltAz(obstime=t, location=palomar_mountain) sun_alt = get_sun(t).transform_to(frame).alt moon_alt = get_moon(t).transform_to(frame).alt # shading for nighttime and twilight dark_times = delta_midnight[sun_alt < 0].value twilit_times = delta_midnight[sun_alt < -18 * u.deg].value plotted_times = delta_midnight[sun_alt < 5 * u.deg].value twilight = BoxAnnotation(left=min(twilit_times), right=max(twilit_times), bottom=0, fill_alpha=0.15, fill_color='black', level='underlay') night = BoxAnnotation(left=min(dark_times), right=max(dark_times), bottom=0, fill_alpha=0.25, fill_color='black', level='underlay') earth = BoxAnnotation(top=0, fill_alpha=0.8, fill_color='sienna') p.add_layout(night) p.add_layout(twilight) p.add_layout(earth) # sun and moon sun = p.line(delta_midnight, sun_alt, line_color='red', name="Sun", legend='Sun', line_dash='dashed') moon = p.line(delta_midnight, moon_alt, line_color='yellow', line_dash='dashed', name="Moon", legend='Moon') # labels and axes p.title.text = "Visibility for %s UTC" %midnight p.xaxis.axis_label = "Hours from PDT Midnight" p.x_range.start = min(plotted_times) p.x_range.end = max(plotted_times) p.yaxis.axis_label = "Airmass" # primary airmass label on right airmasses = (1.01, 1.1, 1.25, 1.5, 2., 3., 6.) ticker = [90 - np.arccos(1./i) * 180/np.pi for i in airmasses] p.yaxis.ticker = ticker p.yaxis.major_label_overrides = {tick: str(airmasses[i]) for i, tick in enumerate(ticker)} # add supplementary alt label on left p.extra_y_ranges = {"altitude": Range1d(0, 90)} p.add_layout(LinearAxis(y_range_name="altitude", axis_label='Altitude [deg]'), 'left') ### adding data from the actual objects #objs = SkyCoord(np.array(ras, dtype=np.float), # np.array(decs, dtype=np.float), unit="deg") approx_midnight = int(Time.now().jd - .5) + .5 - utcoffset.value/24. palo_sin_lat = 0.549836545 palo_cos_lat = 0.835272275 palo_long = 243.1362 ras = np.array(ras, dtype=np.float) decs = np.array(decs, dtype=np.float) alloc_color = {} for i, val in enumerate(np.unique(allocs)): if val in allowed_allocs: alloc_color[val] = allocpalette[i % len(allocpalette)] else: alloc_color[val] = 'lightgray' tooltipped = [] # things with tooltips tooltips = [('obj', '@name'), # make it #name when we get to bokeh 0.13 ('time', '@abstime'), ('altitude', u"@alt\N{DEGREE SIGN}"), ('airmass', '@airmass'), ('priority', '@priority'), ('allocation', '@alloc')] for i in np.array(allocs).argsort(): # go in order by alloc for an alphabetized legend color = alloc_color[allocs[i]] alt = 180 / np.pi * np.arcsin(palo_cos_lat * \ np.cos(np.pi/180 * (palo_long - ras[i] + 15 * (18.697374558 + 24.06570982 * (delta_midnight.value/24. + approx_midnight - 2451545)))) * \ np.cos(decs[i] * np.pi/180) + palo_sin_lat * np.sin(decs[i] * np.pi/180)) airmass = 1./np.cos((90 - alt) * np.pi/180) source = ColumnDataSource( dict(times=delta_midnight, alt=alt, airmass=airmass, abstime=abstimes, priority=np.full(len(abstimes), priorities[i]), alloc=np.full(len(abstimes), allocs[i]), name=np.full(len(abstimes), names[i]))) # delete the name when we get to bokeh 0.13 if allocs[i] == None: # single object legend = names[i] tooltips = tooltips[:4] else: legend = '{}'.format(allocs[i]) if endobs[0] != None: # plot that highlights observed part of the night # full path of the night dotted = p.line('times', 'alt', color=color, source=source, line_dash='2 2', name=names[i], line_width=1, legend=legend) # manually crop the source so only thick observed part has tooltips endtime = endobs[i] initime = endtime - exptime * u.second if i > 0: initime = max(initime, endobs[i - 1]) mask = np.logical_and(delta_midnight + midnight + utcoffset > initime, delta_midnight + midnight + utcoffset < endtime) source = ColumnDataSource(pd.DataFrame(source.data)[mask]) priorities[i] += 3 # all it changes is the line width line = p.line('times', 'alt', color=color, source=source, name=''.format(names[i]), line_width=priorities[i], legend=legend) if allocs[i] in allowed_allocs: tooltipped.append(line) p.legend.click_policy = 'hide' p.legend.location = 'bottom_right' p.add_tools(HoverTool(renderers=tooltipped, tooltips=tooltips)) curdoc().add_root(p) curdoc().title = 'Visibility plot' return p
gpl-2.0
sean93park/mozjs24
js/src/testing/mozbase/mozprofile/tests/bug758250.py
3
1417
#!/usr/bin/env python import mozprofile import os import shutil import tempfile import unittest here = os.path.dirname(os.path.abspath(__file__)) class Bug758250(unittest.TestCase): """ use of --profile in mozrunner just blows away addon sources: https://bugzilla.mozilla.org/show_bug.cgi?id=758250 """ def test_profile_addon_cleanup(self): # sanity check: the empty addon should be here empty = os.path.join(here, 'addons', 'empty') self.assertTrue(os.path.exists(empty)) self.assertTrue(os.path.isdir(empty)) self.assertTrue(os.path.exists(os.path.join(empty, 'install.rdf'))) # because we are testing data loss, let's make sure we make a copy tmpdir = tempfile.mktemp() shutil.copytree(empty, tmpdir) self.assertTrue(os.path.exists(os.path.join(tmpdir, 'install.rdf'))) # make a starter profile profile = mozprofile.FirefoxProfile() path = profile.profile # make a new profile based on the old newprofile = mozprofile.FirefoxProfile(profile=path, addons=[tmpdir]) newprofile.cleanup() # the source addon *should* still exist self.assertTrue(os.path.exists(tmpdir)) self.assertTrue(os.path.exists(os.path.join(tmpdir, 'install.rdf'))) # remove vestiges shutil.rmtree(tmpdir) if __name__ == '__main__': unittest.main()
mpl-2.0
mheap/ansible
lib/ansible/utils/module_docs_fragments/inventory_cache.py
82
1175
# (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) class ModuleDocFragment(object): # inventory cache DOCUMENTATION = """ options: cache: description: - Toggle to enable/disable the caching of the inventory's source data, requires a cache plugin setup to work. type: boolean default: False env: - name: ANSIBLE_INVENTORY_CACHE ini: - section: inventory key: cache cache_plugin: description: - Cache plugin to use for the inventory's source data. env: - name: ANSIBLE_INVENTORY_CACHE_PLUGIN ini: - section: inventory key: cache_plugin cache_timeout: description: - Cache duration in seconds default: 3600 type: integer env: - name: ANSIBLE_INVENTORY_CACHE_TIMEOUT ini: - section: inventory key: cache_timeout cache_connection: description: - Cache connection data or path, read cache plugin documentation for specifics. env: - name: ANSIBLE_INVENTORY_CACHE_CONNECTION ini: - section: inventory key: cache_connection """
gpl-3.0
hpcleuven/easybuild-framework
easybuild/tools/multidiff.py
1
10571
# # # Copyright 2014-2016 Ghent University # # This file is part of EasyBuild, # originally created by the HPC team of Ghent University (http://ugent.be/hpc/en), # with support of Ghent University (http://ugent.be/hpc), # the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be), # Flemish Research Foundation (FWO) (http://www.fwo.be/en) # and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en). # # http://github.com/hpcugent/easybuild # # EasyBuild is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation v2. # # EasyBuild is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with EasyBuild. If not, see <http://www.gnu.org/licenses/>. # # """ Module which allows the diffing of multiple files @author: Toon Willems (Ghent University) @author: Kenneth Hoste (Ghent University) """ import difflib import math import os from vsc.utils import fancylogger from easybuild.tools.build_log import EasyBuildError from easybuild.tools.filetools import read_file from easybuild.tools.systemtools import det_terminal_size SEP_WIDTH = 5 # text colors PURPLE = "\033[0;35m" # background colors GREEN_BACK = "\033[0;42m" RED_BACK = "\033[0;41m" # end character for colorized text END_COLOR = "\033[0m" # meaning characters in diff context HAT = '^' MINUS = '-' PLUS = '+' SPACE = ' ' QUESTIONMARK = '?' END_LONG_LINE = '...' # restrict displaying of differences to limited number of groups MAX_DIFF_GROUPS = 3 _log = fancylogger.getLogger('multidiff', fname=False) class MultiDiff(object): """ Class representing a multi-diff. """ def __init__(self, base_fn, base_lines, files, colored=True): """ MultiDiff constructor @param base: base to compare with @param files: list of files to compare with base @param colored: boolean indicating whether a colored multi-diff should be generated """ self.base_fn = base_fn self.base_lines = base_lines self.files = files self.colored = colored self.diff_info = {} def parse_line(self, line_no, diff_line, meta, squigly_line): """ Register a diff line @param line_no: line number @param diff_line: diff line generated by difflib @param meta: meta information (e.g., filename) @param squigly_line: squigly line indicating which characters changed """ # register (diff_line, meta, squigly_line) tuple for specified line number and determined key key = diff_line[0] if not key in [MINUS, PLUS]: raise EasyBuildError("diff line starts with unexpected character: %s", diff_line) line_key_tuples = self.diff_info.setdefault(line_no, {}).setdefault(key, []) line_key_tuples.append((diff_line, meta, squigly_line)) def color_line(self, line, color): """Create colored version of given line, with given color, if color mode is enabled.""" if self.colored: line = ''.join([color, line, END_COLOR]) return line def merge_squigly(self, squigly1, squigly2): """Combine two squigly lines into a single squigly line.""" sq1 = list(squigly1) sq2 = list(squigly2) # longest line is base base, other = (sq1, sq2) if len(sq1) > len(sq2) else (sq2, sq1) for i, char in enumerate(other): if base[i] in [HAT, SPACE] and base[i] != char: base[i] = char return ''.join(base) def colorize(self, line, squigly): """Add colors to the diff line based on the squigly line.""" if not self.colored: return line # must be a list so we can insert stuff chars = list(line) flag = ' ' offset = 0 color_map = { HAT: GREEN_BACK if line.startswith(PLUS) else RED_BACK, MINUS: RED_BACK, PLUS: GREEN_BACK, } if squigly: for i, squigly_char in enumerate(squigly): if squigly_char != flag: chars.insert(i + offset, END_COLOR) offset += 1 if squigly_char in [HAT, MINUS, PLUS]: chars.insert(i + offset, color_map[squigly_char]) offset += 1 flag = squigly_char chars.insert(len(squigly) + offset, END_COLOR) else: chars.insert(0, color_map.get(line[0], '')) chars.append(END_COLOR) return ''.join(chars) def get_line(self, line_no): """ Return the line information for a specific line @param line_no: line number to obtain information for @return: list with text lines providing line information """ output = [] diff_dict = self.diff_info.get(line_no, {}) for key in [MINUS, PLUS]: lines, changes_dict, squigly_dict = set(), {}, {} # obtain relevant diff lines if key in diff_dict: for (diff_line, meta, squigly_line) in diff_dict[key]: if squigly_line: # merge squigly lines if diff_line in squigly_dict: squigly_line = self.merge_squigly(squigly_line, squigly_dict[diff_line]) squigly_dict[diff_line] = squigly_line lines.add(diff_line) # track meta info (which filenames are relevant) changes_dict.setdefault(diff_line, set()).add(meta) # sort: lines with most changes last, limit number to MAX_DIFF_GROUPS lines = sorted(lines, key=lambda line: len(changes_dict[line]))[:MAX_DIFF_GROUPS] for diff_line in lines: squigly_line = squigly_dict.get(diff_line, '') line = ['%s %s' % (line_no, self.colorize(diff_line, squigly_line))] # mention to how may files this diff applies files = changes_dict[diff_line] num_files = len(self.files) line.append("(%d/%d)" % (len(files), num_files)) # list files to which this diff applies (don't list all files) if len(files) != num_files: line.append(', '.join(files)) output.append(' '.join(line)) # prepend spaces to match line number length in non-color mode if not self.colored and squigly_line: prepend = ' ' * (2 + int(math.log10(line_no))) output.append(''.join([prepend, squigly_line])) # print seperator only if needed if diff_dict and not self.diff_info.get(line_no + 1, {}): output.extend([' ', '-' * SEP_WIDTH, ' ']) return output def __str__(self): """ Create a string representation of this multi-diff """ def limit(text, length): """Limit text to specified length, terminate color mode and add END_LONG_LINE if trimmed.""" if len(text) > length: maxlen = length - len(END_LONG_LINE) res = text[:maxlen] if self.colored: res += END_COLOR return res + END_LONG_LINE else: return text _, term_width = det_terminal_size() base = self.color_line(self.base_fn, PURPLE) filenames = ', '.join(map(os.path.basename, self.files)) output = [ "Comparing %s with %s" % (base, filenames), '=' * SEP_WIDTH, ] diff = False for i in range(len(self.base_lines)): lines = filter(None, self.get_line(i)) if lines: output.append('\n'.join([limit(line, term_width) for line in lines])) diff = True if not diff: output.append("(no diff)") output.append('=' * SEP_WIDTH) return '\n'.join(output) def multidiff(base, files, colored=True): """ Generate a diff for multiple files, all compared to base. @param base: base to compare with @param files: list of files to compare with base @param colored: boolean indicating whether a colored multi-diff should be generated @return: text with multidiff overview """ differ = difflib.Differ() base_lines = read_file(base).split('\n') mdiff = MultiDiff(os.path.basename(base), base_lines, files, colored=colored) # use the MultiDiff class to store the information for filepath in files: lines = read_file(filepath).split('\n') diff = differ.compare(lines, base_lines) filename = os.path.basename(filepath) # contruct map of line number to diff lines and mapping between diff lines # example partial diff: # # - toolchain = {'name': 'goolfc', 'version': '2.6.10'} # ? - ^ ^ # # + toolchain = {'name': 'goolf', 'version': '1.6.20'} # ? ^ ^ # local_diff = {} squigly_dict = {} last_added = None offset = 1 for (i, line) in enumerate(diff): # diff line indicating changed characters on line above, a.k.a. a 'squigly' line if line.startswith(QUESTIONMARK): squigly_dict[last_added] = line offset -= 1 # diff line indicating addition change elif line.startswith(PLUS): local_diff.setdefault(i + offset, []).append((line, filename)) last_added = line # diff line indicated removal change elif line.startswith(MINUS): local_diff.setdefault(i + offset, []).append((line, filename)) last_added = line offset -= 1 # construct the multi-diff based on the constructed dict for line_no in local_diff: for (line, filename) in local_diff[line_no]: mdiff.parse_line(line_no, line.rstrip(), filename, squigly_dict.get(line, '').rstrip()) return str(mdiff)
gpl-2.0
michaelaye/vispy
vispy/app/tests/test_simultaneous.py
18
4321
# -*- coding: utf-8 -*- import numpy as np from numpy.testing import assert_allclose from time import sleep from vispy.app import use_app, Canvas, Timer from vispy.testing import requires_application, SkipTest, run_tests_if_main from vispy.util.ptime import time from vispy.gloo import gl from vispy.gloo.util import _screenshot _win_size = (200, 50) def _update_process_check(canvas, val, draw=True): """Update, process, and check result""" if draw: canvas.update() canvas.app.process_events() canvas.app.process_events() sleep(0.03) # give it time to swap (Qt?) canvas._backend._vispy_set_current() print(' check %s' % val) # check screenshot to see if it's all one color ss = _screenshot() try: assert_allclose(ss.shape[:2], _win_size[::-1]) except Exception: print('!!!!!!!!!! FAIL bad size %s' % list(ss.shape[:2])) raise goal = val * np.ones(ss.shape) try: # Get rid of the alpha value before testing # It can be off by 1 due to rounding assert_allclose(ss[:, :, :3], goal[:, :, :3], atol=1) except Exception: print('!!!!!!!!!! FAIL %s' % np.unique(ss)) raise @requires_application() def test_multiple_canvases(): """Testing multiple canvases""" n_check = 3 app = use_app() with Canvas(app=app, size=_win_size, title='same_0') as c0: with Canvas(app=app, size=_win_size, title='same_1') as c1: ct = [0, 0] @c0.events.draw.connect def draw0(event): ct[0] += 1 c0.update() @c1.events.draw.connect # noqa, analysis:ignore def draw1(event): ct[1] += 1 c1.update() c0.show() # ensure visible c1.show() c0.update() # force first draw c1.update() timeout = time() + 2.0 while (ct[0] < n_check or ct[1] < n_check) and time() < timeout: app.process_events() print((ct, n_check)) assert n_check <= ct[0] <= n_check + 2 # be a bit lenient assert n_check <= ct[1] <= n_check + 2 # check timer global timer_ran timer_ran = False def on_timer(_): global timer_ran timer_ran = True t = Timer(0.1, app=app, connect=on_timer, iterations=1, # noqa start=True) app.process_events() sleep(0.5) # long for slow systems app.process_events() app.process_events() assert timer_ran if app.backend_name.lower() == 'wx': raise SkipTest('wx fails test #2') # XXX TODO Fix this kwargs = dict(app=app, autoswap=False, size=_win_size, show=True) with Canvas(title='0', **kwargs) as c0: with Canvas(title='1', **kwargs) as c1: bgcolors = [None] * 2 @c0.events.draw.connect def draw00(event): print(' {0:7}: {1}'.format('0', bgcolors[0])) if bgcolors[0] is not None: gl.glViewport(0, 0, *list(_win_size)) gl.glClearColor(*bgcolors[0]) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glFinish() @c1.events.draw.connect def draw11(event): print(' {0:7}: {1}'.format('1', bgcolors[1])) if bgcolors[1] is not None: gl.glViewport(0, 0, *list(_win_size)) gl.glClearColor(*bgcolors[1]) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glFinish() for ci, canvas in enumerate((c0, c1)): print('draw %s' % canvas.title) bgcolors[ci] = [0.5, 0.5, 0.5, 1.0] _update_process_check(canvas, 127) for ci, canvas in enumerate((c0, c1)): print('test') _update_process_check(canvas, 127, draw=False) bgcolors[ci] = [1., 1., 1., 1.] _update_process_check(canvas, 255) bgcolors[ci] = [0.25, 0.25, 0.25, 0.25] _update_process_check(canvas, 64) run_tests_if_main()
bsd-3-clause
rushiagr/keystone
keystone/credential/routers.py
29
1034
# Copyright 2013 OpenStack 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. """WSGI Routers for the Credentials service.""" from keystone.common import router from keystone.common import wsgi from keystone.credential import controllers class Routers(wsgi.RoutersBase): def append_v3_routers(self, mapper, routers): routers.append( router.Router(controllers.CredentialV3(), 'credentials', 'credential', resource_descriptions=self.v3_resources))
apache-2.0
xedin/swift
utils/swift_build_support/swift_build_support/products/__init__.py
10
1456
# swift_build_support/products/__init__.py ----------------------*- python -*- # # This source file is part of the Swift.org open source project # # Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors # Licensed under Apache License v2.0 with Runtime Library Exception # # See https://swift.org/LICENSE.txt for license information # See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors # # ---------------------------------------------------------------------------- from .benchmarks import Benchmarks from .cmark import CMark from .foundation import Foundation from .indexstoredb import IndexStoreDB from .libcxx import LibCXX from .libdispatch import LibDispatch from .libicu import LibICU from .llbuild import LLBuild from .lldb import LLDB from .llvm import LLVM from .ninja import Ninja from .skstresstester import SKStressTester from .sourcekitlsp import SourceKitLSP from .swift import Swift from .swiftevolve import SwiftEvolve from .swiftpm import SwiftPM from .swiftsyntax import SwiftSyntax from .tsan_libdispatch import TSanLibDispatch from .xctest import XCTest __all__ = [ 'CMark', 'Ninja', 'Foundation', 'LibCXX', 'LibDispatch', 'LibICU', 'LLBuild', 'LLDB', 'LLVM', 'Ninja', 'Swift', 'SwiftPM', 'XCTest', 'SwiftSyntax', 'SKStressTester', 'SwiftEvolve', 'IndexStoreDB', 'SourceKitLSP', 'Benchmarks', 'TSanLibDispatch', ]
apache-2.0
aqssxlzc/TaobaoTemplateStore
lib/werkzeug/urls.py
298
31741
# -*- coding: utf-8 -*- """ werkzeug.urls ~~~~~~~~~~~~~ This module implements various URL related functions. :copyright: (c) 2013 by the Werkzeug Team, see AUTHORS for more details. :license: BSD, see LICENSE for more details. """ import re from werkzeug._compat import text_type, PY2, to_unicode, \ to_native, implements_to_string, try_coerce_native, \ normalize_string_tuple, make_literal_wrapper, \ fix_tuple_repr from werkzeug._internal import _encode_idna, _decode_idna from werkzeug.datastructures import MultiDict, iter_multi_items from collections import namedtuple # A regular expression for what a valid schema looks like _scheme_re = re.compile(r'^[a-zA-Z0-9+-.]+$') # Characters that are safe in any part of an URL. _always_safe = (b'abcdefghijklmnopqrstuvwxyz' b'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-+') _hexdigits = '0123456789ABCDEFabcdef' _hextobyte = dict( ((a + b).encode(), int(a + b, 16)) for a in _hexdigits for b in _hexdigits ) _URLTuple = fix_tuple_repr(namedtuple('_URLTuple', ['scheme', 'netloc', 'path', 'query', 'fragment'])) class _URLMixin(object): __slots__ = () def replace(self, **kwargs): """Return an URL with the same values, except for those parameters given new values by whichever keyword arguments are specified.""" return self._replace(**kwargs) @property def host(self): """The host part of the URL if available, otherwise `None`. The host is either the hostname or the IP address mentioned in the URL. It will not contain the port. """ return self._split_host()[0] @property def ascii_host(self): """Works exactly like :attr:`host` but will return a result that is restricted to ASCII. If it finds a netloc that is not ASCII it will attempt to idna decode it. This is useful for socket operations when the URL might include internationalized characters. """ rv = self.host if rv is not None and isinstance(rv, text_type): rv = _encode_idna(rv) return to_native(rv, 'ascii', 'ignore') @property def port(self): """The port in the URL as an integer if it was present, `None` otherwise. This does not fill in default ports. """ try: rv = int(to_native(self._split_host()[1])) if 0 <= rv <= 65535: return rv except (ValueError, TypeError): pass @property def auth(self): """The authentication part in the URL if available, `None` otherwise. """ return self._split_netloc()[0] @property def username(self): """The username if it was part of the URL, `None` otherwise. This undergoes URL decoding and will always be a unicode string. """ rv = self._split_auth()[0] if rv is not None: return _url_unquote_legacy(rv) @property def raw_username(self): """The username if it was part of the URL, `None` otherwise. Unlike :attr:`username` this one is not being decoded. """ return self._split_auth()[0] @property def password(self): """The password if it was part of the URL, `None` otherwise. This undergoes URL decoding and will always be a unicode string. """ rv = self._split_auth()[1] if rv is not None: return _url_unquote_legacy(rv) @property def raw_password(self): """The password if it was part of the URL, `None` otherwise. Unlike :attr:`password` this one is not being decoded. """ return self._split_auth()[1] def decode_query(self, *args, **kwargs): """Decodes the query part of the URL. Ths is a shortcut for calling :func:`url_decode` on the query argument. The arguments and keyword arguments are forwarded to :func:`url_decode` unchanged. """ return url_decode(self.query, *args, **kwargs) def join(self, *args, **kwargs): """Joins this URL with another one. This is just a convenience function for calling into :meth:`url_join` and then parsing the return value again. """ return url_parse(url_join(self, *args, **kwargs)) def to_url(self): """Returns a URL string or bytes depending on the type of the information stored. This is just a convenience function for calling :meth:`url_unparse` for this URL. """ return url_unparse(self) def decode_netloc(self): """Decodes the netloc part into a string.""" rv = _decode_idna(self.host or '') if ':' in rv: rv = '[%s]' % rv port = self.port if port is not None: rv = '%s:%d' % (rv, port) auth = ':'.join(filter(None, [ _url_unquote_legacy(self.raw_username or '', '/:%@'), _url_unquote_legacy(self.raw_password or '', '/:%@'), ])) if auth: rv = '%s@%s' % (auth, rv) return rv def to_uri_tuple(self): """Returns a :class:`BytesURL` tuple that holds a URI. This will encode all the information in the URL properly to ASCII using the rules a web browser would follow. It's usually more interesting to directly call :meth:`iri_to_uri` which will return a string. """ return url_parse(iri_to_uri(self).encode('ascii')) def to_iri_tuple(self): """Returns a :class:`URL` tuple that holds a IRI. This will try to decode as much information as possible in the URL without losing information similar to how a web browser does it for the URL bar. It's usually more interesting to directly call :meth:`uri_to_iri` which will return a string. """ return url_parse(uri_to_iri(self)) def _split_netloc(self): if self._at in self.netloc: return self.netloc.split(self._at, 1) return None, self.netloc def _split_auth(self): auth = self._split_netloc()[0] if not auth: return None, None if self._colon not in auth: return auth, None return auth.split(self._colon, 1) def _split_host(self): rv = self._split_netloc()[1] if not rv: return None, None if not rv.startswith(self._lbracket): if self._colon in rv: return rv.split(self._colon, 1) return rv, None idx = rv.find(self._rbracket) if idx < 0: return rv, None host = rv[1:idx] rest = rv[idx + 1:] if rest.startswith(self._colon): return host, rest[1:] return host, None @implements_to_string class URL(_URLTuple, _URLMixin): """Represents a parsed URL. This behaves like a regular tuple but also has some extra attributes that give further insight into the URL. """ __slots__ = () _at = '@' _colon = ':' _lbracket = '[' _rbracket = ']' def __str__(self): return self.to_url() def encode_netloc(self): """Encodes the netloc part to an ASCII safe URL as bytes.""" rv = self.ascii_host or '' if ':' in rv: rv = '[%s]' % rv port = self.port if port is not None: rv = '%s:%d' % (rv, port) auth = ':'.join(filter(None, [ url_quote(self.raw_username or '', 'utf-8', 'strict', '/:%'), url_quote(self.raw_password or '', 'utf-8', 'strict', '/:%'), ])) if auth: rv = '%s@%s' % (auth, rv) return rv.encode('ascii') def encode(self, charset='utf-8', errors='replace'): """Encodes the URL to a tuple made out of bytes. The charset is only being used for the path, query and fragment. """ return BytesURL( self.scheme.encode('ascii'), self.encode_netloc(), self.path.encode(charset, errors), self.query.encode(charset, errors), self.fragment.encode(charset, errors) ) class BytesURL(_URLTuple, _URLMixin): """Represents a parsed URL in bytes.""" __slots__ = () _at = b'@' _colon = b':' _lbracket = b'[' _rbracket = b']' def __str__(self): return self.to_url().decode('utf-8', 'replace') def encode_netloc(self): """Returns the netloc unchanged as bytes.""" return self.netloc def decode(self, charset='utf-8', errors='replace'): """Decodes the URL to a tuple made out of strings. The charset is only being used for the path, query and fragment. """ return URL( self.scheme.decode('ascii'), self.decode_netloc(), self.path.decode(charset, errors), self.query.decode(charset, errors), self.fragment.decode(charset, errors) ) def _unquote_to_bytes(string, unsafe=''): if isinstance(string, text_type): string = string.encode('utf-8') if isinstance(unsafe, text_type): unsafe = unsafe.encode('utf-8') unsafe = frozenset(bytearray(unsafe)) bits = iter(string.split(b'%')) result = bytearray(next(bits, b'')) for item in bits: try: char = _hextobyte[item[:2]] if char in unsafe: raise KeyError() result.append(char) result.extend(item[2:]) except KeyError: result.extend(b'%') result.extend(item) return bytes(result) def _url_encode_impl(obj, charset, encode_keys, sort, key): iterable = iter_multi_items(obj) if sort: iterable = sorted(iterable, key=key) for key, value in iterable: if value is None: continue if not isinstance(key, bytes): key = text_type(key).encode(charset) if not isinstance(value, bytes): value = text_type(value).encode(charset) yield url_quote(key) + '=' + url_quote_plus(value) def _url_unquote_legacy(value, unsafe=''): try: return url_unquote(value, charset='utf-8', errors='strict', unsafe=unsafe) except UnicodeError: return url_unquote(value, charset='latin1', unsafe=unsafe) def url_parse(url, scheme=None, allow_fragments=True): """Parses a URL from a string into a :class:`URL` tuple. If the URL is lacking a scheme it can be provided as second argument. Otherwise, it is ignored. Optionally fragments can be stripped from the URL by setting `allow_fragments` to `False`. The inverse of this function is :func:`url_unparse`. :param url: the URL to parse. :param scheme: the default schema to use if the URL is schemaless. :param allow_fragments: if set to `False` a fragment will be removed from the URL. """ s = make_literal_wrapper(url) is_text_based = isinstance(url, text_type) if scheme is None: scheme = s('') netloc = query = fragment = s('') i = url.find(s(':')) if i > 0 and _scheme_re.match(to_native(url[:i], errors='replace')): # make sure "iri" is not actually a port number (in which case # "scheme" is really part of the path) rest = url[i + 1:] if not rest or any(c not in s('0123456789') for c in rest): # not a port number scheme, url = url[:i].lower(), rest if url[:2] == s('//'): delim = len(url) for c in s('/?#'): wdelim = url.find(c, 2) if wdelim >= 0: delim = min(delim, wdelim) netloc, url = url[2:delim], url[delim:] if ((s('[') in netloc and s(']') not in netloc) or (s(']') in netloc and s('[') not in netloc)): raise ValueError('Invalid IPv6 URL') if allow_fragments and s('#') in url: url, fragment = url.split(s('#'), 1) if s('?') in url: url, query = url.split(s('?'), 1) result_type = is_text_based and URL or BytesURL return result_type(scheme, netloc, url, query, fragment) def url_quote(string, charset='utf-8', errors='strict', safe='/:', unsafe=''): """URL encode a single string with a given encoding. :param s: the string to quote. :param charset: the charset to be used. :param safe: an optional sequence of safe characters. :param unsafe: an optional sequence of unsafe characters. .. versionadded:: 0.9.2 The `unsafe` parameter was added. """ if not isinstance(string, (text_type, bytes, bytearray)): string = text_type(string) if isinstance(string, text_type): string = string.encode(charset, errors) if isinstance(safe, text_type): safe = safe.encode(charset, errors) if isinstance(unsafe, text_type): unsafe = unsafe.encode(charset, errors) safe = frozenset(bytearray(safe) + _always_safe) - frozenset(bytearray(unsafe)) rv = bytearray() for char in bytearray(string): if char in safe: rv.append(char) else: rv.extend(('%%%02X' % char).encode('ascii')) return to_native(bytes(rv)) def url_quote_plus(string, charset='utf-8', errors='strict', safe=''): """URL encode a single string with the given encoding and convert whitespace to "+". :param s: The string to quote. :param charset: The charset to be used. :param safe: An optional sequence of safe characters. """ return url_quote(string, charset, errors, safe + ' ', '+').replace(' ', '+') def url_unparse(components): """The reverse operation to :meth:`url_parse`. This accepts arbitrary as well as :class:`URL` tuples and returns a URL as a string. :param components: the parsed URL as tuple which should be converted into a URL string. """ scheme, netloc, path, query, fragment = \ normalize_string_tuple(components) s = make_literal_wrapper(scheme) url = s('') # We generally treat file:///x and file:/x the same which is also # what browsers seem to do. This also allows us to ignore a schema # register for netloc utilization or having to differenciate between # empty and missing netloc. if netloc or (scheme and path.startswith(s('/'))): if path and path[:1] != s('/'): path = s('/') + path url = s('//') + (netloc or s('')) + path elif path: url += path if scheme: url = scheme + s(':') + url if query: url = url + s('?') + query if fragment: url = url + s('#') + fragment return url def url_unquote(string, charset='utf-8', errors='replace', unsafe=''): """URL decode a single string with a given encoding. If the charset is set to `None` no unicode decoding is performed and raw bytes are returned. :param s: the string to unquote. :param charset: the charset of the query string. If set to `None` no unicode decoding will take place. :param errors: the error handling for the charset decoding. """ rv = _unquote_to_bytes(string, unsafe) if charset is not None: rv = rv.decode(charset, errors) return rv def url_unquote_plus(s, charset='utf-8', errors='replace'): """URL decode a single string with the given `charset` and decode "+" to whitespace. Per default encoding errors are ignored. If you want a different behavior you can set `errors` to ``'replace'`` or ``'strict'``. In strict mode a :exc:`HTTPUnicodeError` is raised. :param s: The string to unquote. :param charset: the charset of the query string. If set to `None` no unicode decoding will take place. :param errors: The error handling for the `charset` decoding. """ if isinstance(s, text_type): s = s.replace(u'+', u' ') else: s = s.replace(b'+', b' ') return url_unquote(s, charset, errors) def url_fix(s, charset='utf-8'): r"""Sometimes you get an URL by a user that just isn't a real URL because it contains unsafe characters like ' ' and so on. This function can fix some of the problems in a similar way browsers handle data entered by the user: >>> url_fix(u'http://de.wikipedia.org/wiki/Elf (Begriffskl\xe4rung)') 'http://de.wikipedia.org/wiki/Elf%20(Begriffskl%C3%A4rung)' :param s: the string with the URL to fix. :param charset: The target charset for the URL if the url was given as unicode string. """ scheme, netloc, path, qs, anchor = url_parse(to_unicode(s, charset, 'replace')) path = url_quote(path, charset, safe='/%+$!*\'(),') qs = url_quote_plus(qs, charset, safe=':&%=+$!*\'(),') return to_native(url_unparse((scheme, netloc, path, qs, anchor))) def uri_to_iri(uri, charset='utf-8', errors='replace'): r""" Converts a URI in a given charset to a IRI. Examples for URI versus IRI: >>> uri_to_iri(b'http://xn--n3h.net/') u'http://\u2603.net/' >>> uri_to_iri(b'http://%C3%BCser:p%C3%A4ssword@xn--n3h.net/p%C3%A5th') u'http://\xfcser:p\xe4ssword@\u2603.net/p\xe5th' Query strings are left unchanged: >>> uri_to_iri('/?foo=24&x=%26%2f') u'/?foo=24&x=%26%2f' .. versionadded:: 0.6 :param uri: The URI to convert. :param charset: The charset of the URI. :param errors: The error handling on decode. """ if isinstance(uri, tuple): uri = url_unparse(uri) uri = url_parse(to_unicode(uri, charset)) path = url_unquote(uri.path, charset, errors, '%/;?') query = url_unquote(uri.query, charset, errors, '%;/?:@&=+,$') fragment = url_unquote(uri.fragment, charset, errors, '%;/?:@&=+,$') return url_unparse((uri.scheme, uri.decode_netloc(), path, query, fragment)) def iri_to_uri(iri, charset='utf-8', errors='strict'): r""" Converts any unicode based IRI to an acceptable ASCII URI. Werkzeug always uses utf-8 URLs internally because this is what browsers and HTTP do as well. In some places where it accepts an URL it also accepts a unicode IRI and converts it into a URI. Examples for IRI versus URI: >>> iri_to_uri(u'http://☃.net/') 'http://xn--n3h.net/' >>> iri_to_uri(u'http://üser:pässword@☃.net/påth') 'http://%C3%BCser:p%C3%A4ssword@xn--n3h.net/p%C3%A5th' .. versionadded:: 0.6 :param iri: The IRI to convert. :param charset: The charset for the URI. """ if isinstance(iri, tuple): iri = url_unparse(iri) iri = url_parse(to_unicode(iri, charset, errors)) netloc = iri.encode_netloc().decode('ascii') path = url_quote(iri.path, charset, errors, '/:~+%') query = url_quote(iri.query, charset, errors, '%&[]:;$*()+,!?*/=') fragment = url_quote(iri.fragment, charset, errors, '=%&[]:;$()+,!?*/') return to_native(url_unparse((iri.scheme, netloc, path, query, fragment))) def url_decode(s, charset='utf-8', decode_keys=False, include_empty=True, errors='replace', separator='&', cls=None): """ Parse a querystring and return it as :class:`MultiDict`. There is a difference in key decoding on different Python versions. On Python 3 keys will always be fully decoded whereas on Python 2, keys will remain bytestrings if they fit into ASCII. On 2.x keys can be forced to be unicode by setting `decode_keys` to `True`. If the charset is set to `None` no unicode decoding will happen and raw bytes will be returned. Per default a missing value for a key will default to an empty key. If you don't want that behavior you can set `include_empty` to `False`. Per default encoding errors are ignored. If you want a different behavior you can set `errors` to ``'replace'`` or ``'strict'``. In strict mode a `HTTPUnicodeError` is raised. .. versionchanged:: 0.5 In previous versions ";" and "&" could be used for url decoding. This changed in 0.5 where only "&" is supported. If you want to use ";" instead a different `separator` can be provided. The `cls` parameter was added. :param s: a string with the query string to decode. :param charset: the charset of the query string. If set to `None` no unicode decoding will take place. :param decode_keys: Used on Python 2.x to control whether keys should be forced to be unicode objects. If set to `True` then keys will be unicode in all cases. Otherwise, they remain `str` if they fit into ASCII. :param include_empty: Set to `False` if you don't want empty values to appear in the dict. :param errors: the decoding error behavior. :param separator: the pair separator to be used, defaults to ``&`` :param cls: an optional dict class to use. If this is not specified or `None` the default :class:`MultiDict` is used. """ if cls is None: cls = MultiDict if isinstance(s, text_type) and not isinstance(separator, text_type): separator = separator.decode(charset or 'ascii') elif isinstance(s, bytes) and not isinstance(separator, bytes): separator = separator.encode(charset or 'ascii') return cls(_url_decode_impl(s.split(separator), charset, decode_keys, include_empty, errors)) def url_decode_stream(stream, charset='utf-8', decode_keys=False, include_empty=True, errors='replace', separator='&', cls=None, limit=None, return_iterator=False): """Works like :func:`url_decode` but decodes a stream. The behavior of stream and limit follows functions like :func:`~werkzeug.wsgi.make_line_iter`. The generator of pairs is directly fed to the `cls` so you can consume the data while it's parsed. .. versionadded:: 0.8 :param stream: a stream with the encoded querystring :param charset: the charset of the query string. If set to `None` no unicode decoding will take place. :param decode_keys: Used on Python 2.x to control whether keys should be forced to be unicode objects. If set to `True`, keys will be unicode in all cases. Otherwise, they remain `str` if they fit into ASCII. :param include_empty: Set to `False` if you don't want empty values to appear in the dict. :param errors: the decoding error behavior. :param separator: the pair separator to be used, defaults to ``&`` :param cls: an optional dict class to use. If this is not specified or `None` the default :class:`MultiDict` is used. :param limit: the content length of the URL data. Not necessary if a limited stream is provided. :param return_iterator: if set to `True` the `cls` argument is ignored and an iterator over all decoded pairs is returned """ from werkzeug.wsgi import make_chunk_iter if return_iterator: cls = lambda x: x elif cls is None: cls = MultiDict pair_iter = make_chunk_iter(stream, separator, limit) return cls(_url_decode_impl(pair_iter, charset, decode_keys, include_empty, errors)) def _url_decode_impl(pair_iter, charset, decode_keys, include_empty, errors): for pair in pair_iter: if not pair: continue s = make_literal_wrapper(pair) equal = s('=') if equal in pair: key, value = pair.split(equal, 1) else: if not include_empty: continue key = pair value = s('') key = url_unquote_plus(key, charset, errors) if charset is not None and PY2 and not decode_keys: key = try_coerce_native(key) yield key, url_unquote_plus(value, charset, errors) def url_encode(obj, charset='utf-8', encode_keys=False, sort=False, key=None, separator=b'&'): """URL encode a dict/`MultiDict`. If a value is `None` it will not appear in the result string. Per default only values are encoded into the target charset strings. If `encode_keys` is set to ``True`` unicode keys are supported too. If `sort` is set to `True` the items are sorted by `key` or the default sorting algorithm. .. versionadded:: 0.5 `sort`, `key`, and `separator` were added. :param obj: the object to encode into a query string. :param charset: the charset of the query string. :param encode_keys: set to `True` if you have unicode keys. (Ignored on Python 3.x) :param sort: set to `True` if you want parameters to be sorted by `key`. :param separator: the separator to be used for the pairs. :param key: an optional function to be used for sorting. For more details check out the :func:`sorted` documentation. """ separator = to_native(separator, 'ascii') return separator.join(_url_encode_impl(obj, charset, encode_keys, sort, key)) def url_encode_stream(obj, stream=None, charset='utf-8', encode_keys=False, sort=False, key=None, separator=b'&'): """Like :meth:`url_encode` but writes the results to a stream object. If the stream is `None` a generator over all encoded pairs is returned. .. versionadded:: 0.8 :param obj: the object to encode into a query string. :param stream: a stream to write the encoded object into or `None` if an iterator over the encoded pairs should be returned. In that case the separator argument is ignored. :param charset: the charset of the query string. :param encode_keys: set to `True` if you have unicode keys. (Ignored on Python 3.x) :param sort: set to `True` if you want parameters to be sorted by `key`. :param separator: the separator to be used for the pairs. :param key: an optional function to be used for sorting. For more details check out the :func:`sorted` documentation. """ separator = to_native(separator, 'ascii') gen = _url_encode_impl(obj, charset, encode_keys, sort, key) if stream is None: return gen for idx, chunk in enumerate(gen): if idx: stream.write(separator) stream.write(chunk) def url_join(base, url, allow_fragments=True): """Join a base URL and a possibly relative URL to form an absolute interpretation of the latter. :param base: the base URL for the join operation. :param url: the URL to join. :param allow_fragments: indicates whether fragments should be allowed. """ if isinstance(base, tuple): base = url_unparse(base) if isinstance(url, tuple): url = url_unparse(url) base, url = normalize_string_tuple((base, url)) s = make_literal_wrapper(base) if not base: return url if not url: return base bscheme, bnetloc, bpath, bquery, bfragment = \ url_parse(base, allow_fragments=allow_fragments) scheme, netloc, path, query, fragment = \ url_parse(url, bscheme, allow_fragments) if scheme != bscheme: return url if netloc: return url_unparse((scheme, netloc, path, query, fragment)) netloc = bnetloc if path[:1] == s('/'): segments = path.split(s('/')) elif not path: segments = bpath.split(s('/')) if not query: query = bquery else: segments = bpath.split(s('/'))[:-1] + path.split(s('/')) # If the rightmost part is "./" we want to keep the slash but # remove the dot. if segments[-1] == s('.'): segments[-1] = s('') # Resolve ".." and "." segments = [segment for segment in segments if segment != s('.')] while 1: i = 1 n = len(segments) - 1 while i < n: if segments[i] == s('..') and \ segments[i - 1] not in (s(''), s('..')): del segments[i - 1:i + 1] break i += 1 else: break # Remove trailing ".." if the URL is absolute unwanted_marker = [s(''), s('..')] while segments[:2] == unwanted_marker: del segments[1] path = s('/').join(segments) return url_unparse((scheme, netloc, path, query, fragment)) class Href(object): """Implements a callable that constructs URLs with the given base. The function can be called with any number of positional and keyword arguments which than are used to assemble the URL. Works with URLs and posix paths. Positional arguments are appended as individual segments to the path of the URL: >>> href = Href('/foo') >>> href('bar', 23) '/foo/bar/23' >>> href('foo', bar=23) '/foo/foo?bar=23' If any of the arguments (positional or keyword) evaluates to `None` it will be skipped. If no keyword arguments are given the last argument can be a :class:`dict` or :class:`MultiDict` (or any other dict subclass), otherwise the keyword arguments are used for the query parameters, cutting off the first trailing underscore of the parameter name: >>> href(is_=42) '/foo?is=42' >>> href({'foo': 'bar'}) '/foo?foo=bar' Combining of both methods is not allowed: >>> href({'foo': 'bar'}, bar=42) Traceback (most recent call last): ... TypeError: keyword arguments and query-dicts can't be combined Accessing attributes on the href object creates a new href object with the attribute name as prefix: >>> bar_href = href.bar >>> bar_href("blub") '/foo/bar/blub' If `sort` is set to `True` the items are sorted by `key` or the default sorting algorithm: >>> href = Href("/", sort=True) >>> href(a=1, b=2, c=3) '/?a=1&b=2&c=3' .. versionadded:: 0.5 `sort` and `key` were added. """ def __init__(self, base='./', charset='utf-8', sort=False, key=None): if not base: base = './' self.base = base self.charset = charset self.sort = sort self.key = key def __getattr__(self, name): if name[:2] == '__': raise AttributeError(name) base = self.base if base[-1:] != '/': base += '/' return Href(url_join(base, name), self.charset, self.sort, self.key) def __call__(self, *path, **query): if path and isinstance(path[-1], dict): if query: raise TypeError('keyword arguments and query-dicts ' 'can\'t be combined') query, path = path[-1], path[:-1] elif query: query = dict([(k.endswith('_') and k[:-1] or k, v) for k, v in query.items()]) path = '/'.join([to_unicode(url_quote(x, self.charset), 'ascii') for x in path if x is not None]).lstrip('/') rv = self.base if path: if not rv.endswith('/'): rv += '/' rv = url_join(rv, './' + path) if query: rv += '?' + to_unicode(url_encode(query, self.charset, sort=self.sort, key=self.key), 'ascii') return to_native(rv)
apache-2.0
JeyZeta/Dangerous
Dangerous/sqlmap/plugins/dbms/oracle/connector.py
10
2469
#!/usr/bin/env python """ Copyright (c) 2006-2015 sqlmap developers (http://sqlmap.org/) See the file 'doc/COPYING' for copying permission """ try: import cx_Oracle except ImportError: pass import logging import os from lib.core.convert import utf8encode from lib.core.data import conf from lib.core.data import logger from lib.core.exception import SqlmapConnectionException from plugins.generic.connector import Connector as GenericConnector os.environ["NLS_LANG"] = ".AL32UTF8" class Connector(GenericConnector): """ Homepage: http://cx-oracle.sourceforge.net/ User guide: http://cx-oracle.sourceforge.net/README.txt API: http://cx-oracle.sourceforge.net/html/index.html License: http://cx-oracle.sourceforge.net/LICENSE.txt """ def __init__(self): GenericConnector.__init__(self) def connect(self): self.initConnection() self.__dsn = cx_Oracle.makedsn(self.hostname, self.port, self.db) self.__dsn = utf8encode(self.__dsn) self.user = utf8encode(self.user) self.password = utf8encode(self.password) try: self.connector = cx_Oracle.connect(dsn=self.__dsn, user=self.user, password=self.password, mode=cx_Oracle.SYSDBA) logger.info("successfully connected as SYSDBA") except (cx_Oracle.OperationalError, cx_Oracle.DatabaseError, cx_Oracle.InterfaceError): try: self.connector = cx_Oracle.connect(dsn=self.__dsn, user=self.user, password=self.password) except (cx_Oracle.OperationalError, cx_Oracle.DatabaseError, cx_Oracle.InterfaceError), msg: raise SqlmapConnectionException(msg) self.initCursor() self.printConnected() def fetchall(self): try: return self.cursor.fetchall() except cx_Oracle.InterfaceError, msg: logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg) return None def execute(self, query): retVal = False try: self.cursor.execute(utf8encode(query)) retVal = True except cx_Oracle.DatabaseError, msg: logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg) self.connector.commit() return retVal def select(self, query): retVal = None if self.execute(query): retVal = self.fetchall() return retVal
mit
chiefspace/udemy-rest-api
udemy_rest_api_section5/code/env/lib/python3.4/site-packages/setuptools/command/build_py.py
14
8478
import os import sys import fnmatch import textwrap from distutils.command.build_py import build_py as _build_py from distutils.util import convert_path from glob import glob try: from setuptools.lib2to3_ex import Mixin2to3 except ImportError: class Mixin2to3: def run_2to3(self, files, doctests=True): "do nothing" class build_py(_build_py, Mixin2to3): """Enhanced 'build_py' command that includes data files with packages The data files are specified via a 'package_data' argument to 'setup()'. See 'setuptools.dist.Distribution' for more details. Also, this version of the 'build_py' command allows you to specify both 'py_modules' and 'packages' in the same setup operation. """ def finalize_options(self): _build_py.finalize_options(self) self.package_data = self.distribution.package_data self.exclude_package_data = self.distribution.exclude_package_data or {} if 'data_files' in self.__dict__: del self.__dict__['data_files'] self.__updated_files = [] self.__doctests_2to3 = [] def run(self): """Build modules, packages, and copy data files to build directory""" if not self.py_modules and not self.packages: return if self.py_modules: self.build_modules() if self.packages: self.build_packages() self.build_package_data() self.run_2to3(self.__updated_files, False) self.run_2to3(self.__updated_files, True) self.run_2to3(self.__doctests_2to3, True) # Only compile actual .py files, using our base class' idea of what our # output files are. self.byte_compile(_build_py.get_outputs(self, include_bytecode=0)) def __getattr__(self, attr): if attr=='data_files': # lazily compute data files self.data_files = files = self._get_data_files() return files return _build_py.__getattr__(self,attr) def build_module(self, module, module_file, package): outfile, copied = _build_py.build_module(self, module, module_file, package) if copied: self.__updated_files.append(outfile) return outfile, copied def _get_data_files(self): """Generate list of '(package,src_dir,build_dir,filenames)' tuples""" self.analyze_manifest() data = [] for package in self.packages or (): # Locate package source directory src_dir = self.get_package_dir(package) # Compute package build directory build_dir = os.path.join(*([self.build_lib] + package.split('.'))) # Length of path to strip from found files plen = len(src_dir)+1 # Strip directory from globbed filenames filenames = [ file[plen:] for file in self.find_data_files(package, src_dir) ] data.append((package, src_dir, build_dir, filenames)) return data def find_data_files(self, package, src_dir): """Return filenames for package's data files in 'src_dir'""" globs = (self.package_data.get('', []) + self.package_data.get(package, [])) files = self.manifest_files.get(package, [])[:] for pattern in globs: # Each pattern has to be converted to a platform-specific path files.extend(glob(os.path.join(src_dir, convert_path(pattern)))) return self.exclude_data_files(package, src_dir, files) def build_package_data(self): """Copy data files into build directory""" for package, src_dir, build_dir, filenames in self.data_files: for filename in filenames: target = os.path.join(build_dir, filename) self.mkpath(os.path.dirname(target)) srcfile = os.path.join(src_dir, filename) outf, copied = self.copy_file(srcfile, target) srcfile = os.path.abspath(srcfile) if copied and srcfile in self.distribution.convert_2to3_doctests: self.__doctests_2to3.append(outf) def analyze_manifest(self): self.manifest_files = mf = {} if not self.distribution.include_package_data: return src_dirs = {} for package in self.packages or (): # Locate package source directory src_dirs[assert_relative(self.get_package_dir(package))] = package self.run_command('egg_info') ei_cmd = self.get_finalized_command('egg_info') for path in ei_cmd.filelist.files: d,f = os.path.split(assert_relative(path)) prev = None oldf = f while d and d!=prev and d not in src_dirs: prev = d d, df = os.path.split(d) f = os.path.join(df, f) if d in src_dirs: if path.endswith('.py') and f==oldf: continue # it's a module, not data mf.setdefault(src_dirs[d],[]).append(path) def get_data_files(self): pass # kludge 2.4 for lazy computation if sys.version<"2.4": # Python 2.4 already has this code def get_outputs(self, include_bytecode=1): """Return complete list of files copied to the build directory This includes both '.py' files and data files, as well as '.pyc' and '.pyo' files if 'include_bytecode' is true. (This method is needed for the 'install_lib' command to do its job properly, and to generate a correct installation manifest.) """ return _build_py.get_outputs(self, include_bytecode) + [ os.path.join(build_dir, filename) for package, src_dir, build_dir,filenames in self.data_files for filename in filenames ] def check_package(self, package, package_dir): """Check namespace packages' __init__ for declare_namespace""" try: return self.packages_checked[package] except KeyError: pass init_py = _build_py.check_package(self, package, package_dir) self.packages_checked[package] = init_py if not init_py or not self.distribution.namespace_packages: return init_py for pkg in self.distribution.namespace_packages: if pkg==package or pkg.startswith(package+'.'): break else: return init_py f = open(init_py,'rbU') if 'declare_namespace'.encode() not in f.read(): from distutils.errors import DistutilsError raise DistutilsError( "Namespace package problem: %s is a namespace package, but its\n" "__init__.py does not call declare_namespace()! Please fix it.\n" '(See the setuptools manual under "Namespace Packages" for ' "details.)\n" % (package,) ) f.close() return init_py def initialize_options(self): self.packages_checked={} _build_py.initialize_options(self) def get_package_dir(self, package): res = _build_py.get_package_dir(self, package) if self.distribution.src_root is not None: return os.path.join(self.distribution.src_root, res) return res def exclude_data_files(self, package, src_dir, files): """Filter filenames for package's data files in 'src_dir'""" globs = (self.exclude_package_data.get('', []) + self.exclude_package_data.get(package, [])) bad = [] for pattern in globs: bad.extend( fnmatch.filter( files, os.path.join(src_dir, convert_path(pattern)) ) ) bad = dict.fromkeys(bad) seen = {} return [ f for f in files if f not in bad and f not in seen and seen.setdefault(f,1) # ditch dupes ] def assert_relative(path): if not os.path.isabs(path): return path from distutils.errors import DistutilsSetupError msg = textwrap.dedent(""" Error: setup script specifies an absolute path: %s setup() arguments must *always* be /-separated paths relative to the setup.py directory, *never* absolute paths. """).lstrip() % path raise DistutilsSetupError(msg)
gpl-2.0
avoinsystems/odoo
addons/account_payment/wizard/account_payment_populate_statement.py
274
4160
# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################## import time from lxml import etree from openerp.osv import fields, osv class account_payment_populate_statement(osv.osv_memory): _name = "account.payment.populate.statement" _description = "Account Payment Populate Statement" _columns = { 'lines': fields.many2many('payment.line', 'payment_line_rel_', 'payment_id', 'line_id', 'Payment Lines') } def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False): line_obj = self.pool.get('payment.line') res = super(account_payment_populate_statement, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=False) line_ids = line_obj.search(cr, uid, [ ('move_line_id.reconcile_id', '=', False), ('bank_statement_line_id', '=', False), ('move_line_id.state','=','valid')]) line_ids.extend(line_obj.search(cr, uid, [ ('move_line_id.reconcile_id', '=', False), ('order_id.mode', '=', False), ('move_line_id.state','=','valid')])) domain = '[("id", "in", '+ str(line_ids)+')]' doc = etree.XML(res['arch']) nodes = doc.xpath("//field[@name='lines']") for node in nodes: node.set('domain', domain) res['arch'] = etree.tostring(doc) return res def populate_statement(self, cr, uid, ids, context=None): line_obj = self.pool.get('payment.line') statement_obj = self.pool.get('account.bank.statement') statement_line_obj = self.pool.get('account.bank.statement.line') currency_obj = self.pool.get('res.currency') if context is None: context = {} data = self.read(cr, uid, ids, context=context)[0] line_ids = data['lines'] if not line_ids: return {'type': 'ir.actions.act_window_close'} statement = statement_obj.browse(cr, uid, context['active_id'], context=context) for line in line_obj.browse(cr, uid, line_ids, context=context): ctx = context.copy() ctx['date'] = line.ml_maturity_date # was value_date earlier,but this field exists no more now amount = currency_obj.compute(cr, uid, line.currency.id, statement.currency.id, line.amount_currency, context=ctx) st_line_vals = self._prepare_statement_line_vals(cr, uid, line, amount, statement, context=context) st_line_id = statement_line_obj.create(cr, uid, st_line_vals, context=context) line_obj.write(cr, uid, [line.id], {'bank_statement_line_id': st_line_id}) return {'type': 'ir.actions.act_window_close'} def _prepare_statement_line_vals(self, cr, uid, payment_line, amount, statement, context=None): return { 'name': payment_line.order_id.reference or '?', 'amount':-amount, 'partner_id': payment_line.partner_id.id, 'statement_id': statement.id, 'ref': payment_line.communication, } # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
agpl-3.0
normanmaurer/autobahntestsuite-maven-plugin
src/main/resources/autobahntestsuite/case/case7_3_5.py
12
1623
############################################################################### ## ## Copyright 2011 Tavendo GmbH ## ## 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. ## ############################################################################### from case import Case class Case7_3_5(Case): DESCRIPTION = """Send a close frame with close code and close reason of maximum length (123)""" EXPECTATION = """Clean close with normal code.""" def init(self): self.suppressClose = True def onConnectionLost(self, failedByMe): Case.onConnectionLost(self, failedByMe) if self.behaviorClose == Case.WRONG_CODE: self.behavior = Case.FAILED self.passed = False self.result = self.resultClose def onOpen(self): self.payload = "*" * 123 self.expected[Case.OK] = [] self.expectedClose = {"closedByMe":True,"closeCode":[self.p.CLOSE_STATUS_CODE_NORMAL],"requireClean":True} self.p.sendClose(self.p.CLOSE_STATUS_CODE_NORMAL,self.payload) self.p.killAfter(1)
apache-2.0
njmube/erpnext
erpnext/patches/v4_0/update_custom_print_formats_for_renamed_fields.py
119
1265
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors # License: GNU General Public License v3. See license.txt from __future__ import unicode_literals import frappe import re def execute(): # NOTE: sequence is important fields_list = ( ("amount", "base_amount"), ("ref_rate", "price_list_rate"), ("base_ref_rate", "base_price_list_rate"), ("adj_rate", "discount_percentage"), ("export_rate", "rate"), ("basic_rate", "base_rate"), ("export_amount", "amount"), ("reserved_warehouse", "warehouse"), ("import_ref_rate", "price_list_rate"), ("purchase_ref_rate", "base_price_list_rate"), ("discount_rate", "discount_percentage"), ("import_rate", "rate"), ("purchase_rate", "base_rate"), ("import_amount", "amount") ) condition = " or ".join("""html like "%%{}%%" """.format(d[0].replace("_", "\\_")) for d in fields_list if d[0] != "amount") for name, html in frappe.db.sql("""select name, html from `tabPrint Format` where standard = 'No' and ({}) and html not like '%%frappe.%%'""".format(condition)): html = html.replace("wn.", "frappe.") for from_field, to_field in fields_list: html = re.sub(r"\b{}\b".format(from_field), to_field, html) frappe.db.set_value("Print Format", name, "html", html)
agpl-3.0
Peekmo/three.js
utils/converters/fbx/convert_to_threejs.py
213
77684
# @author zfedoran / http://github.com/zfedoran import os import sys import math import operator import re import json import types import shutil # ##################################################### # Globals # ##################################################### option_triangulate = True option_textures = True option_copy_textures = True option_prefix = True option_geometry = False option_forced_y_up = False option_default_camera = False option_default_light = False option_pretty_print = False converter = None inputFolder = "" outputFolder = "" # ##################################################### # Pretty Printing Hacks # ##################################################### # Force an array to be printed fully on a single line class NoIndent(object): def __init__(self, value, separator = ','): self.separator = separator self.value = value def encode(self): if not self.value: return None return '[ %s ]' % self.separator.join(str(f) for f in self.value) # Force an array into chunks rather than printing each element on a new line class ChunkedIndent(object): def __init__(self, value, chunk_size = 15, force_rounding = False): self.value = value self.size = chunk_size self.force_rounding = force_rounding def encode(self): # Turn the flat array into an array of arrays where each subarray is of # length chunk_size. Then string concat the values in the chunked # arrays, delimited with a ', ' and round the values finally append # '{CHUNK}' so that we can find the strings with regex later if not self.value: return None if self.force_rounding: return ['{CHUNK}%s' % ', '.join(str(round(f, 6)) for f in self.value[i:i+self.size]) for i in range(0, len(self.value), self.size)] else: return ['{CHUNK}%s' % ', '.join(str(f) for f in self.value[i:i+self.size]) for i in range(0, len(self.value), self.size)] # This custom encoder looks for instances of NoIndent or ChunkedIndent. # When it finds class CustomEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, NoIndent) or isinstance(obj, ChunkedIndent): return obj.encode() else: return json.JSONEncoder.default(self, obj) def executeRegexHacks(output_string): # turn strings of arrays into arrays (remove the double quotes) output_string = re.sub(':\s*\"(\[.*\])\"', r': \1', output_string) output_string = re.sub('(\n\s*)\"(\[.*\])\"', r'\1\2', output_string) output_string = re.sub('(\n\s*)\"{CHUNK}(.*)\"', r'\1\2', output_string) # replace '0metadata' with metadata output_string = re.sub('0metadata', r'metadata', output_string) # replace 'zchildren' with children output_string = re.sub('zchildren', r'children', output_string) # add an extra newline after '"children": {' output_string = re.sub('(children.*{\s*\n)', r'\1\n', output_string) # add an extra newline after '},' output_string = re.sub('},\s*\n', r'},\n\n', output_string) # add an extra newline after '\n\s*],' output_string = re.sub('(\n\s*)],\s*\n', r'\1],\n\n', output_string) return output_string # ##################################################### # Object Serializers # ##################################################### # FbxVector2 is not JSON serializable def serializeVector2(v, round_vector = False): # JSON does not support NaN or Inf if math.isnan(v[0]) or math.isinf(v[0]): v[0] = 0 if math.isnan(v[1]) or math.isinf(v[1]): v[1] = 0 if round_vector or option_pretty_print: v = (round(v[0], 5), round(v[1], 5)) if option_pretty_print: return NoIndent([v[0], v[1]], ', ') else: return [v[0], v[1]] # FbxVector3 is not JSON serializable def serializeVector3(v, round_vector = False): # JSON does not support NaN or Inf if math.isnan(v[0]) or math.isinf(v[0]): v[0] = 0 if math.isnan(v[1]) or math.isinf(v[1]): v[1] = 0 if math.isnan(v[2]) or math.isinf(v[2]): v[2] = 0 if round_vector or option_pretty_print: v = (round(v[0], 5), round(v[1], 5), round(v[2], 5)) if option_pretty_print: return NoIndent([v[0], v[1], v[2]], ', ') else: return [v[0], v[1], v[2]] # FbxVector4 is not JSON serializable def serializeVector4(v, round_vector = False): # JSON does not support NaN or Inf if math.isnan(v[0]) or math.isinf(v[0]): v[0] = 0 if math.isnan(v[1]) or math.isinf(v[1]): v[1] = 0 if math.isnan(v[2]) or math.isinf(v[2]): v[2] = 0 if math.isnan(v[3]) or math.isinf(v[3]): v[3] = 0 if round_vector or option_pretty_print: v = (round(v[0], 5), round(v[1], 5), round(v[2], 5), round(v[3], 5)) if option_pretty_print: return NoIndent([v[0], v[1], v[2], v[3]], ', ') else: return [v[0], v[1], v[2], v[3]] # ##################################################### # Helpers # ##################################################### def getRadians(v): return ((v[0]*math.pi)/180, (v[1]*math.pi)/180, (v[2]*math.pi)/180) def getHex(c): color = (int(c[0]*255) << 16) + (int(c[1]*255) << 8) + int(c[2]*255) return int(color) def setBit(value, position, on): if on: mask = 1 << position return (value | mask) else: mask = ~(1 << position) return (value & mask) def generate_uvs(uv_layers): layers = [] for uvs in uv_layers: tmp = [] for uv in uvs: tmp.append(uv[0]) tmp.append(uv[1]) if option_pretty_print: layer = ChunkedIndent(tmp) else: layer = tmp layers.append(layer) return layers # ##################################################### # Object Name Helpers # ##################################################### def hasUniqueName(o, class_id): scene = o.GetScene() object_name = o.GetName() object_id = o.GetUniqueID() object_count = scene.GetSrcObjectCount(class_id) for i in range(object_count): other = scene.GetSrcObject(class_id, i) other_id = other.GetUniqueID() other_name = other.GetName() if other_id == object_id: continue if other_name == object_name: return False return True def getObjectName(o, force_prefix = False): if not o: return "" object_name = o.GetName() object_id = o.GetUniqueID() if not force_prefix: force_prefix = not hasUniqueName(o, FbxNode.ClassId) prefix = "" if option_prefix or force_prefix: prefix = "Object_%s_" % object_id return prefix + object_name def getMaterialName(o, force_prefix = False): object_name = o.GetName() object_id = o.GetUniqueID() if not force_prefix: force_prefix = not hasUniqueName(o, FbxSurfaceMaterial.ClassId) prefix = "" if option_prefix or force_prefix: prefix = "Material_%s_" % object_id return prefix + object_name def getTextureName(t, force_prefix = False): if type(t) is FbxFileTexture: texture_file = t.GetFileName() texture_id = os.path.splitext(os.path.basename(texture_file))[0] else: texture_id = t.GetName() if texture_id == "_empty_": texture_id = "" prefix = "" if option_prefix or force_prefix: prefix = "Texture_%s_" % t.GetUniqueID() if len(texture_id) == 0: prefix = prefix[0:len(prefix)-1] return prefix + texture_id def getMtlTextureName(texture_name, texture_id, force_prefix = False): texture_name = os.path.splitext(texture_name)[0] prefix = "" if option_prefix or force_prefix: prefix = "Texture_%s_" % texture_id return prefix + texture_name def getPrefixedName(o, prefix): return (prefix + '_%s_') % o.GetUniqueID() + o.GetName() # ##################################################### # Triangulation # ##################################################### def triangulate_node_hierarchy(node): node_attribute = node.GetNodeAttribute(); if node_attribute: if node_attribute.GetAttributeType() == FbxNodeAttribute.eMesh or \ node_attribute.GetAttributeType() == FbxNodeAttribute.eNurbs or \ node_attribute.GetAttributeType() == FbxNodeAttribute.eNurbsSurface or \ node_attribute.GetAttributeType() == FbxNodeAttribute.ePatch: converter.TriangulateInPlace(node); child_count = node.GetChildCount() for i in range(child_count): triangulate_node_hierarchy(node.GetChild(i)) def triangulate_scene(scene): node = scene.GetRootNode() if node: for i in range(node.GetChildCount()): triangulate_node_hierarchy(node.GetChild(i)) # ##################################################### # Generate Material Object # ##################################################### def generate_texture_bindings(material_property, material_params): # FBX to Three.js texture types binding_types = { "DiffuseColor": "map", "DiffuseFactor": "diffuseFactor", "EmissiveColor": "emissiveMap", "EmissiveFactor": "emissiveFactor", "AmbientColor": "lightMap", # "ambientMap", "AmbientFactor": "ambientFactor", "SpecularColor": "specularMap", "SpecularFactor": "specularFactor", "ShininessExponent": "shininessExponent", "NormalMap": "normalMap", "Bump": "bumpMap", "TransparentColor": "transparentMap", "TransparencyFactor": "transparentFactor", "ReflectionColor": "reflectionMap", "ReflectionFactor": "reflectionFactor", "DisplacementColor": "displacementMap", "VectorDisplacementColor": "vectorDisplacementMap" } if material_property.IsValid(): #Here we have to check if it's layeredtextures, or just textures: layered_texture_count = material_property.GetSrcObjectCount(FbxLayeredTexture.ClassId) if layered_texture_count > 0: for j in range(layered_texture_count): layered_texture = material_property.GetSrcObject(FbxLayeredTexture.ClassId, j) texture_count = layered_texture.GetSrcObjectCount(FbxTexture.ClassId) for k in range(texture_count): texture = layered_texture.GetSrcObject(FbxTexture.ClassId,k) if texture: texture_id = getTextureName(texture, True) material_params[binding_types[str(material_property.GetName())]] = texture_id else: # no layered texture simply get on the property texture_count = material_property.GetSrcObjectCount(FbxTexture.ClassId) for j in range(texture_count): texture = material_property.GetSrcObject(FbxTexture.ClassId,j) if texture: texture_id = getTextureName(texture, True) material_params[binding_types[str(material_property.GetName())]] = texture_id def generate_material_object(material): #Get the implementation to see if it's a hardware shader. implementation = GetImplementation(material, "ImplementationHLSL") implementation_type = "HLSL" if not implementation: implementation = GetImplementation(material, "ImplementationCGFX") implementation_type = "CGFX" output = None material_params = None material_type = None if implementation: print("Shader materials are not supported") elif material.GetClassId().Is(FbxSurfaceLambert.ClassId): ambient = getHex(material.Ambient.Get()) diffuse = getHex(material.Diffuse.Get()) emissive = getHex(material.Emissive.Get()) opacity = 1.0 - material.TransparencyFactor.Get() opacity = 1.0 if opacity == 0 else opacity opacity = opacity transparent = False reflectivity = 1 material_type = 'MeshBasicMaterial' # material_type = 'MeshLambertMaterial' material_params = { 'color' : diffuse, 'ambient' : ambient, 'emissive' : emissive, 'reflectivity' : reflectivity, 'transparent' : transparent, 'opacity' : opacity } elif material.GetClassId().Is(FbxSurfacePhong.ClassId): ambient = getHex(material.Ambient.Get()) diffuse = getHex(material.Diffuse.Get()) emissive = getHex(material.Emissive.Get()) specular = getHex(material.Specular.Get()) opacity = 1.0 - material.TransparencyFactor.Get() opacity = 1.0 if opacity == 0 else opacity opacity = opacity shininess = material.Shininess.Get() transparent = False reflectivity = 1 bumpScale = 1 material_type = 'MeshPhongMaterial' material_params = { 'color' : diffuse, 'ambient' : ambient, 'emissive' : emissive, 'specular' : specular, 'shininess' : shininess, 'bumpScale' : bumpScale, 'reflectivity' : reflectivity, 'transparent' : transparent, 'opacity' : opacity } else: print "Unknown type of Material", getMaterialName(material) # default to Lambert Material if the current Material type cannot be handeled if not material_type: ambient = getHex((0,0,0)) diffuse = getHex((0.5,0.5,0.5)) emissive = getHex((0,0,0)) opacity = 1 transparent = False reflectivity = 1 material_type = 'MeshLambertMaterial' material_params = { 'color' : diffuse, 'ambient' : ambient, 'emissive' : emissive, 'reflectivity' : reflectivity, 'transparent' : transparent, 'opacity' : opacity } if option_textures: texture_count = FbxLayerElement.sTypeTextureCount() for texture_index in range(texture_count): material_property = material.FindProperty(FbxLayerElement.sTextureChannelNames(texture_index)) generate_texture_bindings(material_property, material_params) material_params['wireframe'] = False material_params['wireframeLinewidth'] = 1 output = { 'type' : material_type, 'parameters' : material_params } return output def generate_proxy_material_object(node, material_names): material_type = 'MeshFaceMaterial' material_params = { 'materials' : material_names } output = { 'type' : material_type, 'parameters' : material_params } return output # ##################################################### # Find Scene Materials # ##################################################### def extract_materials_from_node(node, material_dict): name = node.GetName() mesh = node.GetNodeAttribute() node = None if mesh: node = mesh.GetNode() if node: material_count = node.GetMaterialCount() material_names = [] for l in range(mesh.GetLayerCount()): materials = mesh.GetLayer(l).GetMaterials() if materials: if materials.GetReferenceMode() == FbxLayerElement.eIndex: #Materials are in an undefined external table continue for i in range(material_count): material = node.GetMaterial(i) material_names.append(getMaterialName(material)) if material_count > 1: proxy_material = generate_proxy_material_object(node, material_names) proxy_name = getMaterialName(node, True) material_dict[proxy_name] = proxy_material def generate_materials_from_hierarchy(node, material_dict): if node.GetNodeAttribute() == None: pass else: attribute_type = (node.GetNodeAttribute().GetAttributeType()) if attribute_type == FbxNodeAttribute.eMesh: extract_materials_from_node(node, material_dict) for i in range(node.GetChildCount()): generate_materials_from_hierarchy(node.GetChild(i), material_dict) def generate_material_dict(scene): material_dict = {} # generate all materials for this scene material_count = scene.GetSrcObjectCount(FbxSurfaceMaterial.ClassId) for i in range(material_count): material = scene.GetSrcObject(FbxSurfaceMaterial.ClassId, i) material_object = generate_material_object(material) material_name = getMaterialName(material) material_dict[material_name] = material_object # generate material porxies # Three.js does not support meshs with multiple materials, however it does # support materials with multiple submaterials node = scene.GetRootNode() if node: for i in range(node.GetChildCount()): generate_materials_from_hierarchy(node.GetChild(i), material_dict) return material_dict # ##################################################### # Generate Texture Object # ##################################################### def generate_texture_object(texture): #TODO: extract more texture properties wrap_u = texture.GetWrapModeU() wrap_v = texture.GetWrapModeV() offset = texture.GetUVTranslation() if type(texture) is FbxFileTexture: url = texture.GetFileName() else: url = getTextureName( texture ) #url = replace_inFolder2OutFolder( url ) #print( url ) index = url.rfind( '/' ) if index == -1: index = url.rfind( '\\' ) filename = url[ index+1 : len(url) ] output = { 'url': filename, 'fullpath': url, 'repeat': serializeVector2( (1,1) ), 'offset': serializeVector2( texture.GetUVTranslation() ), 'magFilter': 'LinearFilter', 'minFilter': 'LinearMipMapLinearFilter', 'anisotropy': True } return output # ##################################################### # Replace Texture input path to output # ##################################################### def replace_inFolder2OutFolder(url): folderIndex = url.find(inputFolder) if folderIndex != -1: url = url[ folderIndex+len(inputFolder): ] url = outputFolder + url return url # ##################################################### # Replace Texture output path to input # ##################################################### def replace_OutFolder2inFolder(url): folderIndex = url.find(outputFolder) if folderIndex != -1: url = url[ folderIndex+len(outputFolder): ] url = inputFolder + url return url # ##################################################### # Find Scene Textures # ##################################################### def extract_material_textures(material_property, texture_dict): if material_property.IsValid(): #Here we have to check if it's layeredtextures, or just textures: layered_texture_count = material_property.GetSrcObjectCount(FbxLayeredTexture.ClassId) if layered_texture_count > 0: for j in range(layered_texture_count): layered_texture = material_property.GetSrcObject(FbxLayeredTexture.ClassId, j) texture_count = layered_texture.GetSrcObjectCount(FbxTexture.ClassId) for k in range(texture_count): texture = layered_texture.GetSrcObject(FbxTexture.ClassId,k) if texture: texture_object = generate_texture_object(texture) texture_name = getTextureName( texture, True ) texture_dict[texture_name] = texture_object else: # no layered texture simply get on the property texture_count = material_property.GetSrcObjectCount(FbxTexture.ClassId) for j in range(texture_count): texture = material_property.GetSrcObject(FbxTexture.ClassId,j) if texture: texture_object = generate_texture_object(texture) texture_name = getTextureName( texture, True ) texture_dict[texture_name] = texture_object def extract_textures_from_node(node, texture_dict): name = node.GetName() mesh = node.GetNodeAttribute() #for all materials attached to this mesh material_count = mesh.GetNode().GetSrcObjectCount(FbxSurfaceMaterial.ClassId) for material_index in range(material_count): material = mesh.GetNode().GetSrcObject(FbxSurfaceMaterial.ClassId, material_index) #go through all the possible textures types if material: texture_count = FbxLayerElement.sTypeTextureCount() for texture_index in range(texture_count): material_property = material.FindProperty(FbxLayerElement.sTextureChannelNames(texture_index)) extract_material_textures(material_property, texture_dict) def generate_textures_from_hierarchy(node, texture_dict): if node.GetNodeAttribute() == None: pass else: attribute_type = (node.GetNodeAttribute().GetAttributeType()) if attribute_type == FbxNodeAttribute.eMesh: extract_textures_from_node(node, texture_dict) for i in range(node.GetChildCount()): generate_textures_from_hierarchy(node.GetChild(i), texture_dict) def generate_texture_dict(scene): if not option_textures: return {} texture_dict = {} node = scene.GetRootNode() if node: for i in range(node.GetChildCount()): generate_textures_from_hierarchy(node.GetChild(i), texture_dict) return texture_dict # ##################################################### # Extract Fbx SDK Mesh Data # ##################################################### def extract_fbx_vertex_positions(mesh): control_points_count = mesh.GetControlPointsCount() control_points = mesh.GetControlPoints() positions = [] for i in range(control_points_count): tmp = control_points[i] tmp = [tmp[0], tmp[1], tmp[2]] positions.append(tmp) node = mesh.GetNode() if node: t = node.GeometricTranslation.Get() t = FbxVector4(t[0], t[1], t[2], 1) r = node.GeometricRotation.Get() r = FbxVector4(r[0], r[1], r[2], 1) s = node.GeometricScaling.Get() s = FbxVector4(s[0], s[1], s[2], 1) hasGeometricTransform = False if t[0] != 0 or t[1] != 0 or t[2] != 0 or \ r[0] != 0 or r[1] != 0 or r[2] != 0 or \ s[0] != 1 or s[1] != 1 or s[2] != 1: hasGeometricTransform = True if hasGeometricTransform: geo_transform = FbxMatrix(t,r,s) else: geo_transform = FbxMatrix() transform = None if option_geometry: # FbxMeshes are local to their node, we need the vertices in global space # when scene nodes are not exported transform = node.EvaluateGlobalTransform() transform = FbxMatrix(transform) * geo_transform elif hasGeometricTransform: transform = geo_transform if transform: for i in range(len(positions)): v = positions[i] position = FbxVector4(v[0], v[1], v[2]) position = transform.MultNormalize(position) positions[i] = [position[0], position[1], position[2]] return positions def extract_fbx_vertex_normals(mesh): # eNone The mapping is undetermined. # eByControlPoint There will be one mapping coordinate for each surface control point/vertex. # eByPolygonVertex There will be one mapping coordinate for each vertex, for every polygon of which it is a part. This means that a vertex will have as many mapping coordinates as polygons of which it is a part. # eByPolygon There can be only one mapping coordinate for the whole polygon. # eByEdge There will be one mapping coordinate for each unique edge in the mesh. This is meant to be used with smoothing layer elements. # eAllSame There can be only one mapping coordinate for the whole surface. layered_normal_indices = [] layered_normal_values = [] poly_count = mesh.GetPolygonCount() control_points = mesh.GetControlPoints() for l in range(mesh.GetLayerCount()): mesh_normals = mesh.GetLayer(l).GetNormals() if not mesh_normals: continue normals_array = mesh_normals.GetDirectArray() normals_count = normals_array.GetCount() if normals_count == 0: continue normal_indices = [] normal_values = [] # values for i in range(normals_count): normal = normals_array.GetAt(i) normal = [normal[0], normal[1], normal[2]] normal_values.append(normal) node = mesh.GetNode() if node: t = node.GeometricTranslation.Get() t = FbxVector4(t[0], t[1], t[2], 1) r = node.GeometricRotation.Get() r = FbxVector4(r[0], r[1], r[2], 1) s = node.GeometricScaling.Get() s = FbxVector4(s[0], s[1], s[2], 1) hasGeometricTransform = False if t[0] != 0 or t[1] != 0 or t[2] != 0 or \ r[0] != 0 or r[1] != 0 or r[2] != 0 or \ s[0] != 1 or s[1] != 1 or s[2] != 1: hasGeometricTransform = True if hasGeometricTransform: geo_transform = FbxMatrix(t,r,s) else: geo_transform = FbxMatrix() transform = None if option_geometry: # FbxMeshes are local to their node, we need the vertices in global space # when scene nodes are not exported transform = node.EvaluateGlobalTransform() transform = FbxMatrix(transform) * geo_transform elif hasGeometricTransform: transform = geo_transform if transform: t = FbxVector4(0,0,0,1) transform.SetRow(3, t) for i in range(len(normal_values)): n = normal_values[i] normal = FbxVector4(n[0], n[1], n[2]) normal = transform.MultNormalize(normal) normal.Normalize() normal = [normal[0], normal[1], normal[2]] normal_values[i] = normal # indices vertexId = 0 for p in range(poly_count): poly_size = mesh.GetPolygonSize(p) poly_normals = [] for v in range(poly_size): control_point_index = mesh.GetPolygonVertex(p, v) # mapping mode is by control points. The mesh should be smooth and soft. # we can get normals by retrieving each control point if mesh_normals.GetMappingMode() == FbxLayerElement.eByControlPoint: # reference mode is direct, the normal index is same as vertex index. # get normals by the index of control vertex if mesh_normals.GetReferenceMode() == FbxLayerElement.eDirect: poly_normals.append(control_point_index) elif mesh_normals.GetReferenceMode() == FbxLayerElement.eIndexToDirect: index = mesh_normals.GetIndexArray().GetAt(control_point_index) poly_normals.append(index) # mapping mode is by polygon-vertex. # we can get normals by retrieving polygon-vertex. elif mesh_normals.GetMappingMode() == FbxLayerElement.eByPolygonVertex: if mesh_normals.GetReferenceMode() == FbxLayerElement.eDirect: poly_normals.append(vertexId) elif mesh_normals.GetReferenceMode() == FbxLayerElement.eIndexToDirect: index = mesh_normals.GetIndexArray().GetAt(vertexId) poly_normals.append(index) elif mesh_normals.GetMappingMode() == FbxLayerElement.eByPolygon or \ mesh_normals.GetMappingMode() == FbxLayerElement.eAllSame or \ mesh_normals.GetMappingMode() == FbxLayerElement.eNone: print("unsupported normal mapping mode for polygon vertex") vertexId += 1 normal_indices.append(poly_normals) layered_normal_values.append(normal_values) layered_normal_indices.append(normal_indices) normal_values = [] normal_indices = [] # Three.js only supports one layer of normals if len(layered_normal_values) > 0: normal_values = layered_normal_values[0] normal_indices = layered_normal_indices[0] return normal_values, normal_indices def extract_fbx_vertex_colors(mesh): # eNone The mapping is undetermined. # eByControlPoint There will be one mapping coordinate for each surface control point/vertex. # eByPolygonVertex There will be one mapping coordinate for each vertex, for every polygon of which it is a part. This means that a vertex will have as many mapping coordinates as polygons of which it is a part. # eByPolygon There can be only one mapping coordinate for the whole polygon. # eByEdge There will be one mapping coordinate for each unique edge in the mesh. This is meant to be used with smoothing layer elements. # eAllSame There can be only one mapping coordinate for the whole surface. layered_color_indices = [] layered_color_values = [] poly_count = mesh.GetPolygonCount() control_points = mesh.GetControlPoints() for l in range(mesh.GetLayerCount()): mesh_colors = mesh.GetLayer(l).GetVertexColors() if not mesh_colors: continue colors_array = mesh_colors.GetDirectArray() colors_count = colors_array.GetCount() if colors_count == 0: continue color_indices = [] color_values = [] # values for i in range(colors_count): color = colors_array.GetAt(i) color = [color.mRed, color.mGreen, color.mBlue, color.mAlpha] color_values.append(color) # indices vertexId = 0 for p in range(poly_count): poly_size = mesh.GetPolygonSize(p) poly_colors = [] for v in range(poly_size): control_point_index = mesh.GetPolygonVertex(p, v) if mesh_colors.GetMappingMode() == FbxLayerElement.eByControlPoint: if mesh_colors.GetReferenceMode() == FbxLayerElement.eDirect: poly_colors.append(control_point_index) elif mesh_colors.GetReferenceMode() == FbxLayerElement.eIndexToDirect: index = mesh_colors.GetIndexArray().GetAt(control_point_index) poly_colors.append(index) elif mesh_colors.GetMappingMode() == FbxLayerElement.eByPolygonVertex: if mesh_colors.GetReferenceMode() == FbxLayerElement.eDirect: poly_colors.append(vertexId) elif mesh_colors.GetReferenceMode() == FbxLayerElement.eIndexToDirect: index = mesh_colors.GetIndexArray().GetAt(vertexId) poly_colors.append(index) elif mesh_colors.GetMappingMode() == FbxLayerElement.eByPolygon or \ mesh_colors.GetMappingMode() == FbxLayerElement.eAllSame or \ mesh_colors.GetMappingMode() == FbxLayerElement.eNone: print("unsupported color mapping mode for polygon vertex") vertexId += 1 color_indices.append(poly_colors) layered_color_indices.append( color_indices ) layered_color_values.append( color_values ) color_values = [] color_indices = [] # Three.js only supports one layer of colors if len(layered_color_values) > 0: color_values = layered_color_values[0] color_indices = layered_color_indices[0] ''' # The Fbx SDK defaults mesh.Color to (0.8, 0.8, 0.8) # This causes most models to receive incorrect vertex colors if len(color_values) == 0: color = mesh.Color.Get() color_values = [[color[0], color[1], color[2]]] color_indices = [] for p in range(poly_count): poly_size = mesh.GetPolygonSize(p) color_indices.append([0] * poly_size) ''' return color_values, color_indices def extract_fbx_vertex_uvs(mesh): # eNone The mapping is undetermined. # eByControlPoint There will be one mapping coordinate for each surface control point/vertex. # eByPolygonVertex There will be one mapping coordinate for each vertex, for every polygon of which it is a part. This means that a vertex will have as many mapping coordinates as polygons of which it is a part. # eByPolygon There can be only one mapping coordinate for the whole polygon. # eByEdge There will be one mapping coordinate for each unique edge in the mesh. This is meant to be used with smoothing layer elements. # eAllSame There can be only one mapping coordinate for the whole surface. layered_uv_indices = [] layered_uv_values = [] poly_count = mesh.GetPolygonCount() control_points = mesh.GetControlPoints() for l in range(mesh.GetLayerCount()): mesh_uvs = mesh.GetLayer(l).GetUVs() if not mesh_uvs: continue uvs_array = mesh_uvs.GetDirectArray() uvs_count = uvs_array.GetCount() if uvs_count == 0: continue uv_indices = [] uv_values = [] # values for i in range(uvs_count): uv = uvs_array.GetAt(i) uv = [uv[0], uv[1]] uv_values.append(uv) # indices vertexId = 0 for p in range(poly_count): poly_size = mesh.GetPolygonSize(p) poly_uvs = [] for v in range(poly_size): control_point_index = mesh.GetPolygonVertex(p, v) if mesh_uvs.GetMappingMode() == FbxLayerElement.eByControlPoint: if mesh_uvs.GetReferenceMode() == FbxLayerElement.eDirect: poly_uvs.append(control_point_index) elif mesh_uvs.GetReferenceMode() == FbxLayerElement.eIndexToDirect: index = mesh_uvs.GetIndexArray().GetAt(control_point_index) poly_uvs.append(index) elif mesh_uvs.GetMappingMode() == FbxLayerElement.eByPolygonVertex: uv_texture_index = mesh_uvs.GetIndexArray().GetAt(vertexId) if mesh_uvs.GetReferenceMode() == FbxLayerElement.eDirect or \ mesh_uvs.GetReferenceMode() == FbxLayerElement.eIndexToDirect: poly_uvs.append(uv_texture_index) elif mesh_uvs.GetMappingMode() == FbxLayerElement.eByPolygon or \ mesh_uvs.GetMappingMode() == FbxLayerElement.eAllSame or \ mesh_uvs.GetMappingMode() == FbxLayerElement.eNone: print("unsupported uv mapping mode for polygon vertex") vertexId += 1 uv_indices.append(poly_uvs) layered_uv_values.append(uv_values) layered_uv_indices.append(uv_indices) return layered_uv_values, layered_uv_indices # ##################################################### # Process Mesh Geometry # ##################################################### def generate_normal_key(normal): return (round(normal[0], 6), round(normal[1], 6), round(normal[2], 6)) def generate_color_key(color): return getHex(color) def generate_uv_key(uv): return (round(uv[0], 6), round(uv[1], 6)) def append_non_duplicate_uvs(source_uvs, dest_uvs, counts): source_layer_count = len(source_uvs) for layer_index in range(source_layer_count): dest_layer_count = len(dest_uvs) if dest_layer_count <= layer_index: dest_uv_layer = {} count = 0 dest_uvs.append(dest_uv_layer) counts.append(count) else: dest_uv_layer = dest_uvs[layer_index] count = counts[layer_index] source_uv_layer = source_uvs[layer_index] for uv in source_uv_layer: key = generate_uv_key(uv) if key not in dest_uv_layer: dest_uv_layer[key] = count count += 1 counts[layer_index] = count return counts def generate_unique_normals_dictionary(mesh_list): normals_dictionary = {} nnormals = 0 # Merge meshes, remove duplicate data for mesh in mesh_list: node = mesh.GetNode() normal_values, normal_indices = extract_fbx_vertex_normals(mesh) if len(normal_values) > 0: for normal in normal_values: key = generate_normal_key(normal) if key not in normals_dictionary: normals_dictionary[key] = nnormals nnormals += 1 return normals_dictionary def generate_unique_colors_dictionary(mesh_list): colors_dictionary = {} ncolors = 0 # Merge meshes, remove duplicate data for mesh in mesh_list: color_values, color_indices = extract_fbx_vertex_colors(mesh) if len(color_values) > 0: for color in color_values: key = generate_color_key(color) if key not in colors_dictionary: colors_dictionary[key] = ncolors ncolors += 1 return colors_dictionary def generate_unique_uvs_dictionary_layers(mesh_list): uvs_dictionary_layers = [] nuvs_list = [] # Merge meshes, remove duplicate data for mesh in mesh_list: uv_values, uv_indices = extract_fbx_vertex_uvs(mesh) if len(uv_values) > 0: nuvs_list = append_non_duplicate_uvs(uv_values, uvs_dictionary_layers, nuvs_list) return uvs_dictionary_layers def generate_normals_from_dictionary(normals_dictionary): normal_values = [] for key, index in sorted(normals_dictionary.items(), key = operator.itemgetter(1)): normal_values.append(key) return normal_values def generate_colors_from_dictionary(colors_dictionary): color_values = [] for key, index in sorted(colors_dictionary.items(), key = operator.itemgetter(1)): color_values.append(key) return color_values def generate_uvs_from_dictionary_layers(uvs_dictionary_layers): uv_values = [] for uvs_dictionary in uvs_dictionary_layers: uv_values_layer = [] for key, index in sorted(uvs_dictionary.items(), key = operator.itemgetter(1)): uv_values_layer.append(key) uv_values.append(uv_values_layer) return uv_values def generate_normal_indices_for_poly(poly_index, mesh_normal_values, mesh_normal_indices, normals_to_indices): if len(mesh_normal_indices) <= 0: return [] poly_normal_indices = mesh_normal_indices[poly_index] poly_size = len(poly_normal_indices) output_poly_normal_indices = [] for v in range(poly_size): normal_index = poly_normal_indices[v] normal_value = mesh_normal_values[normal_index] key = generate_normal_key(normal_value) output_index = normals_to_indices[key] output_poly_normal_indices.append(output_index) return output_poly_normal_indices def generate_color_indices_for_poly(poly_index, mesh_color_values, mesh_color_indices, colors_to_indices): if len(mesh_color_indices) <= 0: return [] poly_color_indices = mesh_color_indices[poly_index] poly_size = len(poly_color_indices) output_poly_color_indices = [] for v in range(poly_size): color_index = poly_color_indices[v] color_value = mesh_color_values[color_index] key = generate_color_key(color_value) output_index = colors_to_indices[key] output_poly_color_indices.append(output_index) return output_poly_color_indices def generate_uv_indices_for_poly(poly_index, mesh_uv_values, mesh_uv_indices, uvs_to_indices): if len(mesh_uv_indices) <= 0: return [] poly_uv_indices = mesh_uv_indices[poly_index] poly_size = len(poly_uv_indices) output_poly_uv_indices = [] for v in range(poly_size): uv_index = poly_uv_indices[v] uv_value = mesh_uv_values[uv_index] key = generate_uv_key(uv_value) output_index = uvs_to_indices[key] output_poly_uv_indices.append(output_index) return output_poly_uv_indices def process_mesh_vertices(mesh_list): vertex_offset = 0 vertex_offset_list = [0] vertices = [] for mesh in mesh_list: node = mesh.GetNode() mesh_vertices = extract_fbx_vertex_positions(mesh) vertices.extend(mesh_vertices[:]) vertex_offset += len(mesh_vertices) vertex_offset_list.append(vertex_offset) return vertices, vertex_offset_list def process_mesh_materials(mesh_list): material_offset = 0 material_offset_list = [0] materials_list = [] #TODO: remove duplicate mesh references for mesh in mesh_list: node = mesh.GetNode() material_count = node.GetMaterialCount() if material_count > 0: for l in range(mesh.GetLayerCount()): materials = mesh.GetLayer(l).GetMaterials() if materials: if materials.GetReferenceMode() == FbxLayerElement.eIndex: #Materials are in an undefined external table continue for i in range(material_count): material = node.GetMaterial(i) materials_list.append( material ) material_offset += material_count material_offset_list.append(material_offset) return materials_list, material_offset_list def process_mesh_polygons(mesh_list, normals_to_indices, colors_to_indices, uvs_to_indices_list, vertex_offset_list, material_offset_list): faces = [] for mesh_index in range(len(mesh_list)): mesh = mesh_list[mesh_index] flipWindingOrder = False node = mesh.GetNode() if node: local_scale = node.EvaluateLocalScaling() if local_scale[0] < 0 or local_scale[1] < 0 or local_scale[2] < 0: flipWindingOrder = True poly_count = mesh.GetPolygonCount() control_points = mesh.GetControlPoints() normal_values, normal_indices = extract_fbx_vertex_normals(mesh) color_values, color_indices = extract_fbx_vertex_colors(mesh) uv_values_layers, uv_indices_layers = extract_fbx_vertex_uvs(mesh) for poly_index in range(poly_count): poly_size = mesh.GetPolygonSize(poly_index) face_normals = generate_normal_indices_for_poly(poly_index, normal_values, normal_indices, normals_to_indices) face_colors = generate_color_indices_for_poly(poly_index, color_values, color_indices, colors_to_indices) face_uv_layers = [] for l in range(len(uv_indices_layers)): uv_values = uv_values_layers[l] uv_indices = uv_indices_layers[l] face_uv_indices = generate_uv_indices_for_poly(poly_index, uv_values, uv_indices, uvs_to_indices_list[l]) face_uv_layers.append(face_uv_indices) face_vertices = [] for vertex_index in range(poly_size): control_point_index = mesh.GetPolygonVertex(poly_index, vertex_index) face_vertices.append(control_point_index) #TODO: assign a default material to any mesh without one if len(material_offset_list) <= mesh_index: material_offset = 0 else: material_offset = material_offset_list[mesh_index] vertex_offset = vertex_offset_list[mesh_index] if poly_size > 4: new_face_normals = [] new_face_colors = [] new_face_uv_layers = [] for i in range(poly_size - 2): new_face_vertices = [face_vertices[0], face_vertices[i+1], face_vertices[i+2]] if len(face_normals): new_face_normals = [face_normals[0], face_normals[i+1], face_normals[i+2]] if len(face_colors): new_face_colors = [face_colors[0], face_colors[i+1], face_colors[i+2]] if len(face_uv_layers): new_face_uv_layers = [] for layer in face_uv_layers: new_face_uv_layers.append([layer[0], layer[i+1], layer[i+2]]) face = generate_mesh_face(mesh, poly_index, new_face_vertices, new_face_normals, new_face_colors, new_face_uv_layers, vertex_offset, material_offset, flipWindingOrder) faces.append(face) else: face = generate_mesh_face(mesh, poly_index, face_vertices, face_normals, face_colors, face_uv_layers, vertex_offset, material_offset, flipWindingOrder) faces.append(face) return faces def generate_mesh_face(mesh, polygon_index, vertex_indices, normals, colors, uv_layers, vertex_offset, material_offset, flipOrder): isTriangle = ( len(vertex_indices) == 3 ) nVertices = 3 if isTriangle else 4 hasMaterial = False for l in range(mesh.GetLayerCount()): materials = mesh.GetLayer(l).GetMaterials() if materials: hasMaterial = True break hasFaceUvs = False hasFaceVertexUvs = len(uv_layers) > 0 hasFaceNormals = False hasFaceVertexNormals = len(normals) > 0 hasFaceColors = False hasFaceVertexColors = len(colors) > 0 faceType = 0 faceType = setBit(faceType, 0, not isTriangle) faceType = setBit(faceType, 1, hasMaterial) faceType = setBit(faceType, 2, hasFaceUvs) faceType = setBit(faceType, 3, hasFaceVertexUvs) faceType = setBit(faceType, 4, hasFaceNormals) faceType = setBit(faceType, 5, hasFaceVertexNormals) faceType = setBit(faceType, 6, hasFaceColors) faceType = setBit(faceType, 7, hasFaceVertexColors) faceData = [] # order is important, must match order in JSONLoader # face type # vertex indices # material index # face uvs index # face vertex uvs indices # face color index # face vertex colors indices faceData.append(faceType) if flipOrder: if nVertices == 3: vertex_indices = [vertex_indices[0], vertex_indices[2], vertex_indices[1]] if hasFaceVertexNormals: normals = [normals[0], normals[2], normals[1]] if hasFaceVertexColors: colors = [colors[0], colors[2], colors[1]] if hasFaceVertexUvs: tmp = [] for polygon_uvs in uv_layers: tmp.append([polygon_uvs[0], polygon_uvs[2], polygon_uvs[1]]) uv_layers = tmp else: vertex_indices = [vertex_indices[0], vertex_indices[3], vertex_indices[2], vertex_indices[1]] if hasFaceVertexNormals: normals = [normals[0], normals[3], normals[2], normals[1]] if hasFaceVertexColors: colors = [colors[0], colors[3], colors[2], colors[1]] if hasFaceVertexUvs: tmp = [] for polygon_uvs in uv_layers: tmp.append([polygon_uvs[0], polygon_uvs[3], polygon_uvs[2], polygon_uvs[3]]) uv_layers = tmp for i in range(nVertices): index = vertex_indices[i] + vertex_offset faceData.append(index) if hasMaterial: material_id = 0 for l in range(mesh.GetLayerCount()): materials = mesh.GetLayer(l).GetMaterials() if materials: material_id = materials.GetIndexArray().GetAt(polygon_index) break material_id += material_offset faceData.append( material_id ) if hasFaceVertexUvs: for polygon_uvs in uv_layers: for i in range(nVertices): index = polygon_uvs[i] faceData.append(index) if hasFaceVertexNormals: for i in range(nVertices): index = normals[i] faceData.append(index) if hasFaceVertexColors: for i in range(nVertices): index = colors[i] faceData.append(index) return faceData # ##################################################### # Generate Mesh Object (for scene output format) # ##################################################### def generate_scene_output(node): mesh = node.GetNodeAttribute() # This is done in order to keep the scene output and non-scene output code DRY mesh_list = [ mesh ] # Extract the mesh data into arrays vertices, vertex_offsets = process_mesh_vertices(mesh_list) materials, material_offsets = process_mesh_materials(mesh_list) normals_to_indices = generate_unique_normals_dictionary(mesh_list) colors_to_indices = generate_unique_colors_dictionary(mesh_list) uvs_to_indices_list = generate_unique_uvs_dictionary_layers(mesh_list) normal_values = generate_normals_from_dictionary(normals_to_indices) color_values = generate_colors_from_dictionary(colors_to_indices) uv_values = generate_uvs_from_dictionary_layers(uvs_to_indices_list) # Generate mesh faces for the Three.js file format faces = process_mesh_polygons(mesh_list, normals_to_indices, colors_to_indices, uvs_to_indices_list, vertex_offsets, material_offsets) # Generate counts for uvs, vertices, normals, colors, and faces nuvs = [] for layer_index, uvs in enumerate(uv_values): nuvs.append(str(len(uvs))) nvertices = len(vertices) nnormals = len(normal_values) ncolors = len(color_values) nfaces = len(faces) # Flatten the arrays, currently they are in the form of [[0, 1, 2], [3, 4, 5], ...] vertices = [val for v in vertices for val in v] normal_values = [val for n in normal_values for val in n] color_values = [c for c in color_values] faces = [val for f in faces for val in f] uv_values = generate_uvs(uv_values) # Disable automatic json indenting when pretty printing for the arrays if option_pretty_print: nuvs = NoIndent(nuvs) vertices = ChunkedIndent(vertices, 15, True) normal_values = ChunkedIndent(normal_values, 15, True) color_values = ChunkedIndent(color_values, 15) faces = ChunkedIndent(faces, 30) metadata = { 'vertices' : nvertices, 'normals' : nnormals, 'colors' : ncolors, 'faces' : nfaces, 'uvs' : nuvs } output = { 'scale' : 1, 'materials' : [], 'vertices' : vertices, 'normals' : [] if nnormals <= 0 else normal_values, 'colors' : [] if ncolors <= 0 else color_values, 'uvs' : uv_values, 'faces' : faces } if option_pretty_print: output['0metadata'] = metadata else: output['metadata'] = metadata return output # ##################################################### # Generate Mesh Object (for non-scene output) # ##################################################### def generate_non_scene_output(scene): mesh_list = generate_mesh_list(scene) # Extract the mesh data into arrays vertices, vertex_offsets = process_mesh_vertices(mesh_list) materials, material_offsets = process_mesh_materials(mesh_list) normals_to_indices = generate_unique_normals_dictionary(mesh_list) colors_to_indices = generate_unique_colors_dictionary(mesh_list) uvs_to_indices_list = generate_unique_uvs_dictionary_layers(mesh_list) normal_values = generate_normals_from_dictionary(normals_to_indices) color_values = generate_colors_from_dictionary(colors_to_indices) uv_values = generate_uvs_from_dictionary_layers(uvs_to_indices_list) # Generate mesh faces for the Three.js file format faces = process_mesh_polygons(mesh_list, normals_to_indices, colors_to_indices, uvs_to_indices_list, vertex_offsets, material_offsets) # Generate counts for uvs, vertices, normals, colors, and faces nuvs = [] for layer_index, uvs in enumerate(uv_values): nuvs.append(str(len(uvs))) nvertices = len(vertices) nnormals = len(normal_values) ncolors = len(color_values) nfaces = len(faces) # Flatten the arrays, currently they are in the form of [[0, 1, 2], [3, 4, 5], ...] vertices = [val for v in vertices for val in v] normal_values = [val for n in normal_values for val in n] color_values = [c for c in color_values] faces = [val for f in faces for val in f] uv_values = generate_uvs(uv_values) # Disable json indenting when pretty printing for the arrays if option_pretty_print: nuvs = NoIndent(nuvs) vertices = NoIndent(vertices) normal_values = NoIndent(normal_values) color_values = NoIndent(color_values) faces = NoIndent(faces) metadata = { 'formatVersion' : 3, 'type' : 'geometry', 'generatedBy' : 'convert-to-threejs.py', 'vertices' : nvertices, 'normals' : nnormals, 'colors' : ncolors, 'faces' : nfaces, 'uvs' : nuvs } output = { 'scale' : 1, 'materials' : [], 'vertices' : vertices, 'normals' : [] if nnormals <= 0 else normal_values, 'colors' : [] if ncolors <= 0 else color_values, 'uvs' : uv_values, 'faces' : faces } if option_pretty_print: output['0metadata'] = metadata else: output['metadata'] = metadata return output def generate_mesh_list_from_hierarchy(node, mesh_list): if node.GetNodeAttribute() == None: pass else: attribute_type = (node.GetNodeAttribute().GetAttributeType()) if attribute_type == FbxNodeAttribute.eMesh or \ attribute_type == FbxNodeAttribute.eNurbs or \ attribute_type == FbxNodeAttribute.eNurbsSurface or \ attribute_type == FbxNodeAttribute.ePatch: if attribute_type != FbxNodeAttribute.eMesh: converter.TriangulateInPlace(node); mesh_list.append(node.GetNodeAttribute()) for i in range(node.GetChildCount()): generate_mesh_list_from_hierarchy(node.GetChild(i), mesh_list) def generate_mesh_list(scene): mesh_list = [] node = scene.GetRootNode() if node: for i in range(node.GetChildCount()): generate_mesh_list_from_hierarchy(node.GetChild(i), mesh_list) return mesh_list # ##################################################### # Generate Embed Objects # ##################################################### def generate_embed_dict_from_hierarchy(node, embed_dict): if node.GetNodeAttribute() == None: pass else: attribute_type = (node.GetNodeAttribute().GetAttributeType()) if attribute_type == FbxNodeAttribute.eMesh or \ attribute_type == FbxNodeAttribute.eNurbs or \ attribute_type == FbxNodeAttribute.eNurbsSurface or \ attribute_type == FbxNodeAttribute.ePatch: if attribute_type != FbxNodeAttribute.eMesh: converter.TriangulateInPlace(node); embed_object = generate_scene_output(node) embed_name = getPrefixedName(node, 'Embed') embed_dict[embed_name] = embed_object for i in range(node.GetChildCount()): generate_embed_dict_from_hierarchy(node.GetChild(i), embed_dict) def generate_embed_dict(scene): embed_dict = {} node = scene.GetRootNode() if node: for i in range(node.GetChildCount()): generate_embed_dict_from_hierarchy(node.GetChild(i), embed_dict) return embed_dict # ##################################################### # Generate Geometry Objects # ##################################################### def generate_geometry_object(node): output = { 'type' : 'embedded', 'id' : getPrefixedName( node, 'Embed' ) } return output def generate_geometry_dict_from_hierarchy(node, geometry_dict): if node.GetNodeAttribute() == None: pass else: attribute_type = (node.GetNodeAttribute().GetAttributeType()) if attribute_type == FbxNodeAttribute.eMesh: geometry_object = generate_geometry_object(node) geometry_name = getPrefixedName( node, 'Geometry' ) geometry_dict[geometry_name] = geometry_object for i in range(node.GetChildCount()): generate_geometry_dict_from_hierarchy(node.GetChild(i), geometry_dict) def generate_geometry_dict(scene): geometry_dict = {} node = scene.GetRootNode() if node: for i in range(node.GetChildCount()): generate_geometry_dict_from_hierarchy(node.GetChild(i), geometry_dict) return geometry_dict # ##################################################### # Generate Light Node Objects # ##################################################### def generate_default_light(): direction = (1,1,1) color = (1,1,1) intensity = 80.0 output = { 'type': 'DirectionalLight', 'color': getHex(color), 'intensity': intensity/100.00, 'direction': serializeVector3( direction ), 'target': getObjectName( None ) } return output def generate_light_object(node): light = node.GetNodeAttribute() light_types = ["point", "directional", "spot", "area", "volume"] light_type = light_types[light.LightType.Get()] transform = node.EvaluateLocalTransform() position = transform.GetT() output = None if light_type == "directional": # Three.js directional lights emit light from a point in 3d space to a target node or the origin. # When there is no target, we need to take a point, one unit away from the origin, and move it # into the right location so that the origin acts like the target if node.GetTarget(): direction = position else: translation = FbxVector4(0,0,0,0) scale = FbxVector4(1,1,1,1) rotation = transform.GetR() matrix = FbxMatrix(translation, rotation, scale) direction = matrix.MultNormalize(FbxVector4(0,1,0,1)) output = { 'type': 'DirectionalLight', 'color': getHex(light.Color.Get()), 'intensity': light.Intensity.Get()/100.0, 'direction': serializeVector3( direction ), 'target': getObjectName( node.GetTarget() ) } elif light_type == "point": output = { 'type': 'PointLight', 'color': getHex(light.Color.Get()), 'intensity': light.Intensity.Get()/100.0, 'position': serializeVector3( position ), 'distance': light.FarAttenuationEnd.Get() } elif light_type == "spot": output = { 'type': 'SpotLight', 'color': getHex(light.Color.Get()), 'intensity': light.Intensity.Get()/100.0, 'position': serializeVector3( position ), 'distance': light.FarAttenuationEnd.Get(), 'angle': light.OuterAngle.Get()*math.pi/180, 'exponent': light.DecayType.Get(), 'target': getObjectName( node.GetTarget() ) } return output def generate_ambient_light(scene): scene_settings = scene.GetGlobalSettings() ambient_color = scene_settings.GetAmbientColor() ambient_color = (ambient_color.mRed, ambient_color.mGreen, ambient_color.mBlue) if ambient_color[0] == 0 and ambient_color[1] == 0 and ambient_color[2] == 0: return None output = { 'type': 'AmbientLight', 'color': getHex(ambient_color) } return output # ##################################################### # Generate Camera Node Objects # ##################################################### def generate_default_camera(): position = (100, 100, 100) near = 0.1 far = 1000 fov = 75 output = { 'type': 'PerspectiveCamera', 'fov': fov, 'near': near, 'far': far, 'position': serializeVector3( position ) } return output def generate_camera_object(node): camera = node.GetNodeAttribute() position = camera.Position.Get() projection_types = [ "perspective", "orthogonal" ] projection = projection_types[camera.ProjectionType.Get()] near = camera.NearPlane.Get() far = camera.FarPlane.Get() name = getObjectName( node ) output = {} if projection == "perspective": aspect = camera.PixelAspectRatio.Get() fov = camera.FieldOfView.Get() output = { 'type': 'PerspectiveCamera', 'fov': fov, 'aspect': aspect, 'near': near, 'far': far, 'position': serializeVector3( position ) } elif projection == "orthogonal": left = "" right = "" top = "" bottom = "" output = { 'type': 'PerspectiveCamera', 'left': left, 'right': right, 'top': top, 'bottom': bottom, 'near': near, 'far': far, 'position': serializeVector3( position ) } return output # ##################################################### # Generate Camera Names # ##################################################### def generate_camera_name_list_from_hierarchy(node, camera_list): if node.GetNodeAttribute() == None: pass else: attribute_type = (node.GetNodeAttribute().GetAttributeType()) if attribute_type == FbxNodeAttribute.eCamera: camera_string = getObjectName(node) camera_list.append(camera_string) for i in range(node.GetChildCount()): generate_camera_name_list_from_hierarchy(node.GetChild(i), camera_list) def generate_camera_name_list(scene): camera_list = [] node = scene.GetRootNode() if node: for i in range(node.GetChildCount()): generate_camera_name_list_from_hierarchy(node.GetChild(i), camera_list) return camera_list # ##################################################### # Generate Mesh Node Object # ##################################################### def generate_mesh_object(node): mesh = node.GetNodeAttribute() transform = node.EvaluateLocalTransform() position = transform.GetT() scale = transform.GetS() rotation = getRadians(transform.GetR()) quaternion = transform.GetQ() material_count = node.GetMaterialCount() material_name = "" if material_count > 0: material_names = [] for l in range(mesh.GetLayerCount()): materials = mesh.GetLayer(l).GetMaterials() if materials: if materials.GetReferenceMode() == FbxLayerElement.eIndex: #Materials are in an undefined external table continue for i in range(material_count): material = node.GetMaterial(i) material_names.append( getMaterialName(material) ) if not material_count > 1 and not len(material_names) > 0: material_names.append('') #If this mesh has more than one material, use a proxy material material_name = getMaterialName( node, True) if material_count > 1 else material_names[0] output = { 'geometry': getPrefixedName( node, 'Geometry' ), 'material': material_name, 'position': serializeVector3( position ), 'quaternion': serializeVector4( quaternion ), 'scale': serializeVector3( scale ), 'visible': True, } return output # ##################################################### # Generate Node Object # ##################################################### def generate_object(node): node_types = ["Unknown", "Null", "Marker", "Skeleton", "Mesh", "Nurbs", "Patch", "Camera", "CameraStereo", "CameraSwitcher", "Light", "OpticalReference", "OpticalMarker", "NurbsCurve", "TrimNurbsSurface", "Boundary", "NurbsSurface", "Shape", "LODGroup", "SubDiv", "CachedEffect", "Line"] transform = node.EvaluateLocalTransform() position = transform.GetT() scale = transform.GetS() rotation = getRadians(transform.GetR()) quaternion = transform.GetQ() node_type = "" if node.GetNodeAttribute() == None: node_type = "Null" else: node_type = node_types[node.GetNodeAttribute().GetAttributeType()] name = getObjectName( node ) output = { 'fbx_type': node_type, 'position': serializeVector3( position ), 'quaternion': serializeVector4( quaternion ), 'scale': serializeVector3( scale ), 'visible': True } return output # ##################################################### # Parse Scene Node Objects # ##################################################### def generate_object_hierarchy(node, object_dict): object_count = 0 if node.GetNodeAttribute() == None: object_data = generate_object(node) else: attribute_type = (node.GetNodeAttribute().GetAttributeType()) if attribute_type == FbxNodeAttribute.eMesh: object_data = generate_mesh_object(node) elif attribute_type == FbxNodeAttribute.eLight: object_data = generate_light_object(node) elif attribute_type == FbxNodeAttribute.eCamera: object_data = generate_camera_object(node) else: object_data = generate_object(node) object_count += 1 object_name = getObjectName(node) object_children = {} for i in range(node.GetChildCount()): object_count += generate_object_hierarchy(node.GetChild(i), object_children) if node.GetChildCount() > 0: # Having 'children' above other attributes is hard to read. # We can send it to the bottom using the last letter of the alphabet 'z'. # This letter is removed from the final output. if option_pretty_print: object_data['zchildren'] = object_children else: object_data['children'] = object_children object_dict[object_name] = object_data return object_count def generate_scene_objects(scene): object_count = 0 object_dict = {} ambient_light = generate_ambient_light(scene) if ambient_light: object_dict['AmbientLight'] = ambient_light object_count += 1 if option_default_light: default_light = generate_default_light() object_dict['DefaultLight'] = default_light object_count += 1 if option_default_camera: default_camera = generate_default_camera() object_dict['DefaultCamera'] = default_camera object_count += 1 node = scene.GetRootNode() if node: for i in range(node.GetChildCount()): object_count += generate_object_hierarchy(node.GetChild(i), object_dict) return object_dict, object_count # ##################################################### # Generate Scene Output # ##################################################### def extract_scene(scene, filename): global_settings = scene.GetGlobalSettings() objects, nobjects = generate_scene_objects(scene) textures = generate_texture_dict(scene) materials = generate_material_dict(scene) geometries = generate_geometry_dict(scene) embeds = generate_embed_dict(scene) ntextures = len(textures) nmaterials = len(materials) ngeometries = len(geometries) position = serializeVector3( (0,0,0) ) rotation = serializeVector3( (0,0,0) ) scale = serializeVector3( (1,1,1) ) camera_names = generate_camera_name_list(scene) scene_settings = scene.GetGlobalSettings() # This does not seem to be any help here # global_settings.GetDefaultCamera() defcamera = camera_names[0] if len(camera_names) > 0 else "" if option_default_camera: defcamera = 'default_camera' metadata = { 'formatVersion': 3.2, 'type': 'scene', 'generatedBy': 'convert-to-threejs.py', 'objects': nobjects, 'geometries': ngeometries, 'materials': nmaterials, 'textures': ntextures } transform = { 'position' : position, 'rotation' : rotation, 'scale' : scale } defaults = { 'bgcolor' : 0, 'camera' : defcamera, 'fog' : '' } output = { 'objects': objects, 'geometries': geometries, 'materials': materials, 'textures': textures, 'embeds': embeds, 'transform': transform, 'defaults': defaults, } if option_pretty_print: output['0metadata'] = metadata else: output['metadata'] = metadata return output # ##################################################### # Generate Non-Scene Output # ##################################################### def extract_geometry(scene, filename): output = generate_non_scene_output(scene) return output # ##################################################### # File Helpers # ##################################################### def write_file(filepath, content): index = filepath.rfind('/') dir = filepath[0:index] #if not os.path.exists(dir): #os.makedirs(dir) out = open(filepath, "w") out.write(content.encode('utf8', 'replace')) out.close() def read_file(filepath): f = open(filepath) content = f.readlines() f.close() return content def copy_textures(textures): texture_dict = {} for key in textures: url = textures[key]['fullpath'] #src = replace_OutFolder2inFolder(url) #print( src ) #print( url ) if url in texture_dict: # texture has been copied continue if not os.path.exists(url): print("copy_texture error: we can't find this texture at " + url) continue try: index = url.rfind('/') if index == -1: index = url.rfind( '\\' ) filename = url[index+1:len(url)] saveFolder = "maps" saveFilename = saveFolder + "/" + filename #print( src ) #print( url ) #print( saveFilename ) if not os.path.exists(saveFolder): os.makedirs(saveFolder) shutil.copyfile(url, saveFilename) texture_dict[url] = True except IOError as e: print "I/O error({0}): {1} {2}".format(e.errno, e.strerror, url) def findFilesWithExt(directory, ext, include_path = True): ext = ext.lower() found = [] for root, dirs, files in os.walk(directory): for filename in files: current_ext = os.path.splitext(filename)[1].lower() if current_ext == ext: if include_path: found.append(os.path.join(root, filename)) else: found.append(filename) return found # ##################################################### # main # ##################################################### if __name__ == "__main__": from optparse import OptionParser try: from FbxCommon import * except ImportError: import platform msg = 'Could not locate the python FBX SDK!\n' msg += 'You need to copy the FBX SDK into your python install folder such as ' if platform.system() == 'Windows' or platform.system() == 'Microsoft': msg += '"Python26/Lib/site-packages"' elif platform.system() == 'Linux': msg += '"/usr/local/lib/python2.6/site-packages"' elif platform.system() == 'Darwin': msg += '"/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages"' msg += ' folder.' print(msg) sys.exit(1) usage = "Usage: %prog [source_file.fbx] [output_file.js] [options]" parser = OptionParser(usage=usage) parser.add_option('-t', '--triangulate', action='store_true', dest='triangulate', help="force quad geometry into triangles", default=False) parser.add_option('-x', '--ignore-textures', action='store_true', dest='notextures', help="don't include texture references in output file", default=False) parser.add_option('-n', '--no-texture-copy', action='store_true', dest='notexturecopy', help="don't copy texture files", default=False) parser.add_option('-u', '--force-prefix', action='store_true', dest='prefix', help="prefix all object names in output file to ensure uniqueness", default=False) parser.add_option('-f', '--flatten-scene', action='store_true', dest='geometry', help="merge all geometries and apply node transforms", default=False) parser.add_option('-y', '--force-y-up', action='store_true', dest='forceyup', help="ensure that the y axis shows up", default=False) parser.add_option('-c', '--add-camera', action='store_true', dest='defcamera', help="include default camera in output scene", default=False) parser.add_option('-l', '--add-light', action='store_true', dest='deflight', help="include default light in output scene", default=False) parser.add_option('-p', '--pretty-print', action='store_true', dest='pretty', help="prefix all object names in output file", default=False) (options, args) = parser.parse_args() option_triangulate = options.triangulate option_textures = True if not options.notextures else False option_copy_textures = True if not options.notexturecopy else False option_prefix = options.prefix option_geometry = options.geometry option_forced_y_up = options.forceyup option_default_camera = options.defcamera option_default_light = options.deflight option_pretty_print = options.pretty # Prepare the FBX SDK. sdk_manager, scene = InitializeSdkObjects() converter = FbxGeometryConverter(sdk_manager) # The converter takes an FBX file as an argument. if len(args) > 1: print("\nLoading file: %s" % args[0]) result = LoadScene(sdk_manager, scene, args[0]) else: result = False print("\nUsage: convert_fbx_to_threejs [source_file.fbx] [output_file.js]\n") if not result: print("\nAn error occurred while loading the file...") else: if option_triangulate: print("\nForcing geometry to triangles") triangulate_scene(scene) axis_system = FbxAxisSystem.MayaYUp if not option_forced_y_up: # According to asset's coordinate to convert scene upVector = scene.GetGlobalSettings().GetAxisSystem().GetUpVector(); if upVector[0] == 3: axis_system = FbxAxisSystem.MayaZUp axis_system.ConvertScene(scene) inputFolder = args[0].replace( "\\", "/" ); index = args[0].rfind( "/" ); inputFolder = inputFolder[:index] outputFolder = args[1].replace( "\\", "/" ); index = args[1].rfind( "/" ); outputFolder = outputFolder[:index] if option_geometry: output_content = extract_geometry(scene, os.path.basename(args[0])) else: output_content = extract_scene(scene, os.path.basename(args[0])) if option_pretty_print: output_string = json.dumps(output_content, indent=4, cls=CustomEncoder, separators=(',', ': '), sort_keys=True) output_string = executeRegexHacks(output_string) else: output_string = json.dumps(output_content, separators=(',', ': '), sort_keys=True) output_path = os.path.join(os.getcwd(), args[1]) write_file(output_path, output_string) if option_copy_textures: copy_textures( output_content['textures'] ) print("\nExported Three.js file to:\n%s\n" % output_path) # Destroy all objects created by the FBX SDK. sdk_manager.Destroy() sys.exit(0)
mit
soycode/pattern
pattern/server/cherrypy/cherrypy/_cplogging.py
40
16402
""" Simple config ============= Although CherryPy uses the :mod:`Python logging module <logging>`, it does so behind the scenes so that simple logging is simple, but complicated logging is still possible. "Simple" logging means that you can log to the screen (i.e. console/stdout) or to a file, and that you can easily have separate error and access log files. Here are the simplified logging settings. You use these by adding lines to your config file or dict. You should set these at either the global level or per application (see next), but generally not both. * ``log.screen``: Set this to True to have both "error" and "access" messages printed to stdout. * ``log.access_file``: Set this to an absolute filename where you want "access" messages written. * ``log.error_file``: Set this to an absolute filename where you want "error" messages written. Many events are automatically logged; to log your own application events, call :func:`cherrypy.log`. Architecture ============ Separate scopes --------------- CherryPy provides log managers at both the global and application layers. This means you can have one set of logging rules for your entire site, and another set of rules specific to each application. The global log manager is found at :func:`cherrypy.log`, and the log manager for each application is found at :attr:`app.log<cherrypy._cptree.Application.log>`. If you're inside a request, the latter is reachable from ``cherrypy.request.app.log``; if you're outside a request, you'll have to obtain a reference to the ``app``: either the return value of :func:`tree.mount()<cherrypy._cptree.Tree.mount>` or, if you used :func:`quickstart()<cherrypy.quickstart>` instead, via ``cherrypy.tree.apps['/']``. By default, the global logs are named "cherrypy.error" and "cherrypy.access", and the application logs are named "cherrypy.error.2378745" and "cherrypy.access.2378745" (the number is the id of the Application object). This means that the application logs "bubble up" to the site logs, so if your application has no log handlers, the site-level handlers will still log the messages. Errors vs. Access ----------------- Each log manager handles both "access" messages (one per HTTP request) and "error" messages (everything else). Note that the "error" log is not just for errors! The format of access messages is highly formalized, but the error log isn't--it receives messages from a variety of sources (including full error tracebacks, if enabled). Custom Handlers =============== The simple settings above work by manipulating Python's standard :mod:`logging` module. So when you need something more complex, the full power of the standard module is yours to exploit. You can borrow or create custom handlers, formats, filters, and much more. Here's an example that skips the standard FileHandler and uses a RotatingFileHandler instead: :: #python log = app.log # Remove the default FileHandlers if present. log.error_file = "" log.access_file = "" maxBytes = getattr(log, "rot_maxBytes", 10000000) backupCount = getattr(log, "rot_backupCount", 1000) # Make a new RotatingFileHandler for the error log. fname = getattr(log, "rot_error_file", "error.log") h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount) h.setLevel(DEBUG) h.setFormatter(_cplogging.logfmt) log.error_log.addHandler(h) # Make a new RotatingFileHandler for the access log. fname = getattr(log, "rot_access_file", "access.log") h = handlers.RotatingFileHandler(fname, 'a', maxBytes, backupCount) h.setLevel(DEBUG) h.setFormatter(_cplogging.logfmt) log.access_log.addHandler(h) The ``rot_*`` attributes are pulled straight from the application log object. Since "log.*" config entries simply set attributes on the log object, you can add custom attributes to your heart's content. Note that these handlers are used ''instead'' of the default, simple handlers outlined above (so don't set the "log.error_file" config entry, for example). """ import datetime import logging # Silence the no-handlers "warning" (stderr write!) in stdlib logging logging.Logger.manager.emittedNoHandlerWarning = 1 logfmt = logging.Formatter("%(message)s") import os import sys import cherrypy from cherrypy import _cperror from cherrypy._cpcompat import ntob, py3k class NullHandler(logging.Handler): """A no-op logging handler to silence the logging.lastResort handler.""" def handle(self, record): pass def emit(self, record): pass def createLock(self): self.lock = None class LogManager(object): """An object to assist both simple and advanced logging. ``cherrypy.log`` is an instance of this class. """ appid = None """The id() of the Application object which owns this log manager. If this is a global log manager, appid is None.""" error_log = None """The actual :class:`logging.Logger` instance for error messages.""" access_log = None """The actual :class:`logging.Logger` instance for access messages.""" if py3k: access_log_format = \ '{h} {l} {u} {t} "{r}" {s} {b} "{f}" "{a}"' else: access_log_format = \ '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"' logger_root = None """The "top-level" logger name. This string will be used as the first segment in the Logger names. The default is "cherrypy", for example, in which case the Logger names will be of the form:: cherrypy.error.<appid> cherrypy.access.<appid> """ def __init__(self, appid=None, logger_root="cherrypy"): self.logger_root = logger_root self.appid = appid if appid is None: self.error_log = logging.getLogger("%s.error" % logger_root) self.access_log = logging.getLogger("%s.access" % logger_root) else: self.error_log = logging.getLogger("%s.error.%s" % (logger_root, appid)) self.access_log = logging.getLogger("%s.access.%s" % (logger_root, appid)) self.error_log.setLevel(logging.INFO) self.access_log.setLevel(logging.INFO) # Silence the no-handlers "warning" (stderr write!) in stdlib logging self.error_log.addHandler(NullHandler()) self.access_log.addHandler(NullHandler()) cherrypy.engine.subscribe('graceful', self.reopen_files) def reopen_files(self): """Close and reopen all file handlers.""" for log in (self.error_log, self.access_log): for h in log.handlers: if isinstance(h, logging.FileHandler): h.acquire() h.stream.close() h.stream = open(h.baseFilename, h.mode) h.release() def error(self, msg='', context='', severity=logging.INFO, traceback=False): """Write the given ``msg`` to the error log. This is not just for errors! Applications may call this at any time to log application-specific information. If ``traceback`` is True, the traceback of the current exception (if any) will be appended to ``msg``. """ if traceback: msg += _cperror.format_exc() self.error_log.log(severity, ' '.join((self.time(), context, msg))) def __call__(self, *args, **kwargs): """An alias for ``error``.""" return self.error(*args, **kwargs) def access(self): """Write to the access log (in Apache/NCSA Combined Log format). See http://httpd.apache.org/docs/2.0/logs.html#combined for format details. CherryPy calls this automatically for you. Note there are no arguments; it collects the data itself from :class:`cherrypy.request<cherrypy._cprequest.Request>`. Like Apache started doing in 2.0.46, non-printable and other special characters in %r (and we expand that to all parts) are escaped using \\xhh sequences, where hh stands for the hexadecimal representation of the raw byte. Exceptions from this rule are " and \\, which are escaped by prepending a backslash, and all whitespace characters, which are written in their C-style notation (\\n, \\t, etc). """ request = cherrypy.serving.request remote = request.remote response = cherrypy.serving.response outheaders = response.headers inheaders = request.headers if response.output_status is None: status = "-" else: status = response.output_status.split(ntob(" "), 1)[0] if py3k: status = status.decode('ISO-8859-1') atoms = {'h': remote.name or remote.ip, 'l': '-', 'u': getattr(request, "login", None) or "-", 't': self.time(), 'r': request.request_line, 's': status, 'b': dict.get(outheaders, 'Content-Length', '') or "-", 'f': dict.get(inheaders, 'Referer', ''), 'a': dict.get(inheaders, 'User-Agent', ''), } if py3k: for k, v in atoms.items(): if not isinstance(v, str): v = str(v) v = v.replace('"', '\\"').encode('utf8') # Fortunately, repr(str) escapes unprintable chars, \n, \t, etc # and backslash for us. All we have to do is strip the quotes. v = repr(v)[2:-1] # in python 3.0 the repr of bytes (as returned by encode) # uses double \'s. But then the logger escapes them yet, again # resulting in quadruple slashes. Remove the extra one here. v = v.replace('\\\\', '\\') # Escape double-quote. atoms[k] = v try: self.access_log.log(logging.INFO, self.access_log_format.format(**atoms)) except: self(traceback=True) else: for k, v in atoms.items(): if isinstance(v, unicode): v = v.encode('utf8') elif not isinstance(v, str): v = str(v) # Fortunately, repr(str) escapes unprintable chars, \n, \t, etc # and backslash for us. All we have to do is strip the quotes. v = repr(v)[1:-1] # Escape double-quote. atoms[k] = v.replace('"', '\\"') try: self.access_log.log(logging.INFO, self.access_log_format % atoms) except: self(traceback=True) def time(self): """Return now() in Apache Common Log Format (no timezone).""" now = datetime.datetime.now() monthnames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'] month = monthnames[now.month - 1].capitalize() return ('[%02d/%s/%04d:%02d:%02d:%02d]' % (now.day, month, now.year, now.hour, now.minute, now.second)) def _get_builtin_handler(self, log, key): for h in log.handlers: if getattr(h, "_cpbuiltin", None) == key: return h # ------------------------- Screen handlers ------------------------- # def _set_screen_handler(self, log, enable, stream=None): h = self._get_builtin_handler(log, "screen") if enable: if not h: if stream is None: stream=sys.stderr h = logging.StreamHandler(stream) h.setFormatter(logfmt) h._cpbuiltin = "screen" log.addHandler(h) elif h: log.handlers.remove(h) def _get_screen(self): h = self._get_builtin_handler has_h = h(self.error_log, "screen") or h(self.access_log, "screen") return bool(has_h) def _set_screen(self, newvalue): self._set_screen_handler(self.error_log, newvalue, stream=sys.stderr) self._set_screen_handler(self.access_log, newvalue, stream=sys.stdout) screen = property(_get_screen, _set_screen, doc="""Turn stderr/stdout logging on or off. If you set this to True, it'll add the appropriate StreamHandler for you. If you set it to False, it will remove the handler. """) # -------------------------- File handlers -------------------------- # def _add_builtin_file_handler(self, log, fname): h = logging.FileHandler(fname) h.setFormatter(logfmt) h._cpbuiltin = "file" log.addHandler(h) def _set_file_handler(self, log, filename): h = self._get_builtin_handler(log, "file") if filename: if h: if h.baseFilename != os.path.abspath(filename): h.close() log.handlers.remove(h) self._add_builtin_file_handler(log, filename) else: self._add_builtin_file_handler(log, filename) else: if h: h.close() log.handlers.remove(h) def _get_error_file(self): h = self._get_builtin_handler(self.error_log, "file") if h: return h.baseFilename return '' def _set_error_file(self, newvalue): self._set_file_handler(self.error_log, newvalue) error_file = property(_get_error_file, _set_error_file, doc="""The filename for self.error_log. If you set this to a string, it'll add the appropriate FileHandler for you. If you set it to ``None`` or ``''``, it will remove the handler. """) def _get_access_file(self): h = self._get_builtin_handler(self.access_log, "file") if h: return h.baseFilename return '' def _set_access_file(self, newvalue): self._set_file_handler(self.access_log, newvalue) access_file = property(_get_access_file, _set_access_file, doc="""The filename for self.access_log. If you set this to a string, it'll add the appropriate FileHandler for you. If you set it to ``None`` or ``''``, it will remove the handler. """) # ------------------------- WSGI handlers ------------------------- # def _set_wsgi_handler(self, log, enable): h = self._get_builtin_handler(log, "wsgi") if enable: if not h: h = WSGIErrorHandler() h.setFormatter(logfmt) h._cpbuiltin = "wsgi" log.addHandler(h) elif h: log.handlers.remove(h) def _get_wsgi(self): return bool(self._get_builtin_handler(self.error_log, "wsgi")) def _set_wsgi(self, newvalue): self._set_wsgi_handler(self.error_log, newvalue) wsgi = property(_get_wsgi, _set_wsgi, doc="""Write errors to wsgi.errors. If you set this to True, it'll add the appropriate :class:`WSGIErrorHandler<cherrypy._cplogging.WSGIErrorHandler>` for you (which writes errors to ``wsgi.errors``). If you set it to False, it will remove the handler. """) class WSGIErrorHandler(logging.Handler): "A handler class which writes logging records to environ['wsgi.errors']." def flush(self): """Flushes the stream.""" try: stream = cherrypy.serving.request.wsgi_environ.get('wsgi.errors') except (AttributeError, KeyError): pass else: stream.flush() def emit(self, record): """Emit a record.""" try: stream = cherrypy.serving.request.wsgi_environ.get('wsgi.errors') except (AttributeError, KeyError): pass else: try: msg = self.format(record) fs = "%s\n" import types if not hasattr(types, "UnicodeType"): #if no unicode support... stream.write(fs % msg) else: try: stream.write(fs % msg) except UnicodeError: stream.write(fs % msg.encode("UTF-8")) self.flush() except: self.handleError(record)
bsd-3-clause
kurtgeebelen/rawesome
rawe/newton/nmheMaps.py
2
8951
# Copyright 2012-2013 Greg Horn # # This file is part of rawesome. # # rawesome is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # rawesome is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with rawesome. If not, see <http://www.gnu.org/licenses/>. import numpy as np import casadi as C class VectorizedReadOnlyNmheMap(object): """ Initialize this with a vector (like MX or numpy.array) and it will provide efficient slices with xVec/uVec/pVec. It will also provide lookup(name,timestep) functionality """ def __init__(self,dae,nk,vec): self._nk = nk self._xNames = dae.xNames() self._pNames = dae.pNames() self._vec = vec xSize = len(self._xNames) pSize = len(self._pNames) mapSize = xSize*(self._nk+1) + pSize if type(self._vec) in [C.MX,C.SXMatrix]: assert (mapSize == self._vec.size()), "vector size is wrong" elif type(self._vec) in [np.array,np.ndarray]: assert (mapSize == self._vec.size), "vector size is wrong" else: raise ValueError("unrecognized type: "+str(type(self._vec))) # set up xVec,pVec vecIdx = 0 self._p = self._vec[vecIdx:vecIdx+pSize] vecIdx += pSize self._X = [] self._U = [] for ts in range(self._nk): self._X.append(self._vec[vecIdx:vecIdx+xSize]) vecIdx += xSize self._X.append(self._vec[vecIdx:vecIdx+xSize]) vecIdx += xSize assert (vecIdx == mapSize) # set up indexes self._xIdx = {} self._pIdx = {} for k,name in enumerate(self._xNames): self._xIdx[name] = k for k,name in enumerate(self._pNames): self._pIdx[name] = k def vectorize(self): return self._vec def xVec(self,timestep): assert (timestep != None), "please set timestep" assert (timestep <= self._nk), "timestep too large" return self._X[timestep] def pVec(self): return self._p def lookup(self,name,timestep=None): if name in self._xIdx: return self.xVec(timestep)[self._xIdx[name]] elif name in self._pIdx: assert (timestep == None), "don't set timestep for parameter" return self.pVec()[self._pIdx[name]] else: raise NameError('unrecognized name "'+name+'"') class WriteableNmheMap(object): """ Initialize this with a dae and number of control intervals and it will set all elements to None. Then you can call setVal() to set them and lookup() or vectorize() to retrieve them. You can also call getMissing() to get a summary of elements which haven't been set """ def __init__(self,dae,nk): self._nk = nk self._xNames = dae.xNames() self._pNames = dae.pNames() self._X = np.resize(np.array([None]),(self._nk+1,dae.xVec().size())) self._p = np.resize(np.array([None]),dae.pVec().size()) self._xIdx = {} self._pIdx = {} for k,name in enumerate(self._xNames): self._xIdx[name] = k for k,name in enumerate(self._pNames): self._pIdx[name] = k def vectorize(self): return np.concatenate([self.pVec()]+[self.xVec(k) for k in range(self._nk+1)]) def xVec(self,timestep): assert (timestep != None), "please set timestep" assert (timestep <= self._nk), "timestep too large" return self._X[timestep,:] def pVec(self): return self._p def lookup(self,name,timestep=None): if name in self._xIdx: assert (timestep != None), "please set timestep" assert (timestep <= self._nk), "timestep too large" return self._X[timestep][self._xIdx[name]] elif name in self._pIdx: assert (timestep == None), "don't set timestep for parameter" return self._p[self._pIdx[name]] else: raise NameError('unrecognized name "'+name+'"') def setVal(self,name,val,timestep=None): if name in self._xIdx: if timestep == None: for k in range(self._nk+1): self.setVal(name,val,timestep=k) return assert (timestep <= self._nk), "timestep too large" self._X[timestep,self._xIdx[name]] = val elif name in self._pIdx: assert (timestep == None), "don't set timestep for parameter" self._p[self._pIdx[name]] = val else: raise NameError('unrecognized name "'+name+'"') def getMissing(self): xuMissing = {} for name in self._xNames: missing = [] for k in range(self._nk+1): if self.lookup(name,timestep=k) is None: missing.append(k) if len(missing)>0: xuMissing[name] = missing pMissing = [] for name in self._pNames: if self.lookup(name) is None: pMissing.append(name) return (xuMissing,pMissing) class NmheOutputMapGenerator(object): """ Something which will efficiently generate a map of all outputs. The outputs are all computed all at once to ensure no (additional) CSEs are generated. On initialization, the function which creates all the outputs from a dv vector is created. Then you use it to initialize an OutputMap object """ def __init__(self,ocp,U): (fAll,(f0,outputNames0)) = ocp.dae.outputsFun() self._outputNames0 = outputNames0 self._outputNames = ocp.dae.outputNames() assert (len(self._outputNames0) == f0.getNumOutputs()) assert (len(self._outputNames) == fAll.getNumOutputs()) self._nk = ocp.nk outs = [] for timestepIdx in range(self._nk): if f0 is not None: outs += f0.call([ocp._dvMap.xVec(timestepIdx), U[timestepIdx,:].T, ocp._dvMap.pVec()]) # make the function self.fEveryOutput = C.MXFunction([ocp._dvMap.vectorize(),U],outs) self.fEveryOutput.init() class NmheOutputMap(object): """ Initialize this with an outputMapGenerator, a vector of design vars, and a matrix of control inputs. If you pass a symbolic vector you get symbolic outputs with MXFunction.call(). If you pass a numeric vector you get numeric outputs with MXFunction.setInput(); MXFunction.evaluate(); .. """ def __init__(self,outputMapGenerator,dvs,U): if type(dvs) == C.MX: allOutputs = outputMapGenerator.fEveryOutput.call([dvs,U]) elif type(dvs) == C.SXMatrix: allOutputs = outputMapGenerator.fEveryOutput.eval([dvs,U]) elif type(dvs) in [np.ndarray,C.DMatrix]: outputMapGenerator.fEveryOutput.setInput(dvs,0) outputMapGenerator.fEveryOutput.setInput(U,1) outputMapGenerator.fEveryOutput.evaluate() allOutputs = [np.array(outputMapGenerator.fEveryOutput.output(k)).squeeze() for k in range(outputMapGenerator.fEveryOutput.getNumOutputs())] else: raise TypeError("OutputMap got unrecognized design vector type: "+str(type(dvs))) self._outputNames0 = outputMapGenerator._outputNames0 self._outputNames = outputMapGenerator._outputNames self._numOutputs0 = len(self._outputNames0) self._numOutputs = len(self._outputNames) self._nk = outputMapGenerator._nk self._outputs0 = {} for name in self._outputNames0: self._outputs0[name] = np.resize(np.array([None]),self._nk) outs = [] k = 0 for timestepIdx in range(self._nk): # outputs defined at tau_i0 outs = allOutputs[k:k+self._numOutputs0] k += self._numOutputs0 for name,val in zip(self._outputNames0,outs): self._outputs0[name][timestepIdx] = val def lookup(self,name,timestep): if name not in self._outputNames: raise NameError("couldn't find \""+name+"\"") if name not in self._outputs0: raise ValueError("sorry, \""+name+"\" depends on algebraic variable or ddt(differential variable) \ and Multiple Shooting cannot access it") assert (timestep != None), "please set timestep" return self._outputs0[name][timestep]
lgpl-3.0
deepsrijit1105/edx-platform
common/djangoapps/auth_exchange/forms.py
29
4033
""" Forms to support third-party to first-party OAuth 2.0 access token exchange """ from django.contrib.auth.models import User from django.forms import CharField from edx_oauth2_provider.constants import SCOPE_NAMES import provider.constants from provider.forms import OAuthForm, OAuthValidationError from provider.oauth2.forms import ScopeChoiceField, ScopeMixin from provider.oauth2.models import Client from oauth2_provider.models import Application from requests import HTTPError from social.backends import oauth as social_oauth from social.exceptions import AuthException from third_party_auth import pipeline class AccessTokenExchangeForm(ScopeMixin, OAuthForm): """Form for access token exchange endpoint""" access_token = CharField(required=False) scope = ScopeChoiceField(choices=SCOPE_NAMES, required=False) client_id = CharField(required=False) def __init__(self, request, oauth2_adapter, *args, **kwargs): super(AccessTokenExchangeForm, self).__init__(*args, **kwargs) self.request = request self.oauth2_adapter = oauth2_adapter def _require_oauth_field(self, field_name): """ Raise an appropriate OAuthValidationError error if the field is missing """ field_val = self.cleaned_data.get(field_name) if not field_val: raise OAuthValidationError( { "error": "invalid_request", "error_description": "{} is required".format(field_name), } ) return field_val def clean_access_token(self): """ Validates and returns the "access_token" field. """ return self._require_oauth_field("access_token") def clean_client_id(self): """ Validates and returns the "client_id" field. """ return self._require_oauth_field("client_id") def clean(self): if self._errors: return {} backend = self.request.backend if not isinstance(backend, social_oauth.BaseOAuth2): raise OAuthValidationError( { "error": "invalid_request", "error_description": "{} is not a supported provider".format(backend.name), } ) self.request.session[pipeline.AUTH_ENTRY_KEY] = pipeline.AUTH_ENTRY_LOGIN_API client_id = self.cleaned_data["client_id"] try: client = self.oauth2_adapter.get_client(client_id=client_id) except (Client.DoesNotExist, Application.DoesNotExist): raise OAuthValidationError( { "error": "invalid_client", "error_description": "{} is not a valid client_id".format(client_id), } ) if client.client_type not in [provider.constants.PUBLIC, Application.CLIENT_PUBLIC]: raise OAuthValidationError( { # invalid_client isn't really the right code, but this mirrors # https://github.com/edx/django-oauth2-provider/blob/edx/provider/oauth2/forms.py#L331 "error": "invalid_client", "error_description": "{} is not a public client".format(client_id), } ) self.cleaned_data["client"] = client user = None try: user = backend.do_auth(self.cleaned_data.get("access_token"), allow_inactive_user=True) except (HTTPError, AuthException): pass if user and isinstance(user, User): self.cleaned_data["user"] = user else: # Ensure user does not re-enter the pipeline self.request.social_strategy.clean_partial_pipeline() raise OAuthValidationError( { "error": "invalid_grant", "error_description": "access_token is not valid", } ) return self.cleaned_data
agpl-3.0
kingvuplus/gui_test2
upgrade.py
3
3532
#Embedded file name: /usr/lib/enigma2/python/upgrade.py import os from subprocess import Popen, PIPE opkgDestinations = ['/'] opkgStatusPath = '' overwriteSettingsFiles = False overwriteDriversFiles = True overwriteEmusFiles = True overwritePiconsFiles = True overwriteBootlogoFiles = True overwriteSpinnerFiles = True def findMountPoint(path): path = os.path.abspath(path) while not os.path.ismount(path): path = os.path.dirname(path) return path def opkgExtraDestinations(): global opkgDestinations return ''.join([ ' --add-dest %s:%s' % (i, i) for i in opkgDestinations ]) def opkgAddDestination(mountpoint): if mountpoint not in opkgDestinations: opkgDestinations.append(mountpoint) print '[Ipkg] Added to OPKG destinations:', mountpoint mounts = os.listdir('/media') for mount in mounts: mount = os.path.join('/media', mount) if mount and not mount.startswith('/media/net'): if opkgStatusPath == '': opkgStatusPath = 'var/lib/opkg/status' if not os.path.exists(os.path.join('/', opkgStatusPath)): opkgStatusPath = 'usr/lib/opkg/status' if os.path.exists(os.path.join(mount, opkgStatusPath)): opkgAddDestination(mount) def getValue(line): dummy = line.split('=') if len(dummy) != 2: print 'Error: Wrong formatted settings file' exit if dummy[1] == 'false': return False elif dummy[1] == 'true': return True else: return False p = Popen('opkg list-upgradable', stdout=PIPE, stderr=PIPE, shell=True) stdout, stderr = p.communicate() if stderr != '': print 'Error occured:', stderr exit try: f = open('/etc/enigma2/settings', 'r') lines = f.readlines() f.close() except: print 'Error opening /etc/enigma2/settings file' for line in lines: if line.startswith('config.plugins.softwaremanager.overwriteSettingsFiles'): overwriteSettingsFiles = getValue(line) elif line.startswith('config.plugins.softwaremanager.overwriteDriversFiles'): overwriteDriversFiles = getValue(line) elif line.startswith('config.plugins.softwaremanager.overwriteEmusFiles'): overwriteEmusFiles = getValue(line) elif line.startswith('config.plugins.softwaremanager.overwritePiconsFiles'): overwritePiconsFiles = getValue(line) elif line.startswith('config.plugins.softwaremanager.overwriteBootlogoFiles'): overwriteBootlogoFiles = getValue(line) elif line.startswith('config.plugins.softwaremanager.overwriteSpinnerFiles'): overwriteSpinnerFiles = getValue(line) packages = stdout.split('\n') try: packages.remove('') except: pass upgradePackages = [] for package in packages: item = package.split(' - ', 2) if item[0].find('-settings-') > -1 and not overwriteSettingsFiles: continue elif item[0].find('kernel-module-') > -1 and not overwriteDriversFiles: continue elif item[0].find('-softcams-') > -1 and not overwriteEmusFiles: continue elif item[0].find('-picons-') > -1 and not overwritePiconsFiles: continue elif item[0].find('-bootlogo') > -1 and not overwriteBootlogoFiles: continue elif item[0].find('italysat-spinner') > -1 and not overwriteSpinnerFiles: continue else: upgradePackages.append(item[0]) for p in upgradePackages: os.system('opkg ' + opkgExtraDestinations() + ' upgrade ' + p + ' 2>&1 | tee /home/root/ipkgupgrade.log') os.system('reboot')
gpl-2.0
adason/graph_algo
setup.py
1
1848
# -*- coding: utf-8 -*- import re from setuptools import setup def find_version(fname): '''Attempts to find the version number in the file names fname. Raises RuntimeError if not found. ''' version = '' with open(fname, 'r') as fp: reg = re.compile(r'__version__ = [\'"]([^\'"]*)[\'"]') for line in fp: m = reg.match(line) if m: version = m.group(1) break if not version: raise RuntimeError('Cannot find version information') return version __version__ = find_version("graph_algo/graph_algo.py") def read(fname): with open(fname) as fp: content = fp.read() return content setup( name="graph_algo", version="0.1.0", description="A Collection of Graph Algorithms in Stanford Algorithm Course", long_description=read("README.rst"), author="Adason", author_email="tdadason@gmail.com", url="https://github.com/adason/graph_algo", install_requires=[ "docopt" ], license=read("LICENSE"), zip_safe=False, keywords="graph_algo", classifiers=[ "Development Status :: 2 - Pre-Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy" ], packages=[ "graph_algo" ], package_dir={"graph_algo": "graph_algo"}, entry_points={ "console_scripts": [ "graph_algo = graph_algo.graph_algo:main" ] } )
mit
akhmadMizkat/odoo
openerp/report/render/rml2html/rml2html.py
49
14449
# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. import sys import cStringIO from lxml import etree import copy from openerp.report.render.rml2pdf import utils class _flowable(object): def __init__(self, template, doc, localcontext = None): self._tags = { 'title': self._tag_title, 'spacer': self._tag_spacer, 'para': self._tag_para, 'section':self._section, 'nextFrame': self._tag_next_frame, 'blockTable': self._tag_table, 'pageBreak': self._tag_page_break, 'setNextTemplate': self._tag_next_template, } self.template = template self.doc = doc self.localcontext = localcontext self._cache = {} def _tag_page_break(self, node): return '<br/>'*3 def _tag_next_template(self, node): return '' def _tag_next_frame(self, node): result=self.template.frame_stop() result+='<br/>' result+=self.template.frame_start() return result def _tag_title(self, node): node.tag='h1' return etree.tostring(node) def _tag_spacer(self, node): length = 1+int(utils.unit_get(node.get('length')))/35 return "<br/>"*length def _tag_table(self, node): new_node = copy.deepcopy(node) for child in new_node: new_node.remove(child) new_node.tag = 'table' def process(node,new_node): for child in utils._child_get(node,self): new_child = copy.deepcopy(child) new_node.append(new_child) if len(child): for n in new_child: new_child.remove(n) process(child, new_child) else: new_child.text = utils._process_text(self, child.text) new_child.tag = 'p' try: if new_child.get('style').find('terp_tblheader')!= -1: new_node.tag = 'th' except Exception: pass process(node,new_node) if new_node.get('colWidths',False): sizes = map(lambda x: utils.unit_get(x), new_node.get('colWidths').split(',')) tr = etree.SubElement(new_node, 'tr') for s in sizes: etree.SubElement(tr, 'td', width=str(s)) return etree.tostring(new_node) def _tag_para(self, node): new_node = copy.deepcopy(node) new_node.tag = 'p' if new_node.attrib.get('style',False): new_node.set('class', new_node.get('style')) new_node.text = utils._process_text(self, node.text) return etree.tostring(new_node) def _section(self, node): result = '' for child in utils._child_get(node, self): if child.tag in self._tags: result += self._tags[child.tag](child) return result def render(self, node): result = self.template.start() result += self.template.frame_start() for n in utils._child_get(node, self): if n.tag in self._tags: result += self._tags[n.tag](n) else: pass result += self.template.frame_stop() result += self.template.end() return result.encode('utf-8').replace('"',"\'").replace('°','&deg;') class _rml_tmpl_tag(object): def __init__(self, *args): pass def tag_start(self): return '' def tag_end(self): return False def tag_stop(self): return '' def tag_mergeable(self): return True class _rml_tmpl_frame(_rml_tmpl_tag): def __init__(self, posx, width): self.width = width self.posx = posx def tag_start(self): return "<table border=\'0\' width=\'%d\'><tr><td width=\'%d\'>&nbsp;</td><td>" % (self.width+self.posx,self.posx) def tag_end(self): return True def tag_stop(self): return '</td></tr></table><br/>' def tag_mergeable(self): return False def merge(self, frame): pass class _rml_tmpl_draw_string(_rml_tmpl_tag): def __init__(self, node, style,localcontext = {}): self.localcontext = localcontext self.posx = utils.unit_get(node.get('x')) self.posy = utils.unit_get(node.get('y')) aligns = { 'drawString': 'left', 'drawRightString': 'right', 'drawCentredString': 'center' } align = aligns[node.tag] self.pos = [(self.posx, self.posy, align, utils._process_text(self, node.text), style.get('td'), style.font_size_get('td'))] def tag_start(self): self.pos.sort() res = "<table border='0' cellpadding='0' cellspacing='0'><tr>" posx = 0 i = 0 for (x,y,align,txt, style, fs) in self.pos: if align=="left": pos2 = len(txt)*fs res+="<td width=\'%d\'></td><td style=\'%s\' width=\'%d\'>%s</td>" % (x - posx, style, pos2, txt) posx = x+pos2 if align=="right": res+="<td width=\'%d\' align=\'right\' style=\'%s\'>%s</td>" % (x - posx, style, txt) posx = x if align=="center": res+="<td width=\'%d\' align=\'center\' style=\'%s\'>%s</td>" % ((x - posx)*2, style, txt) posx = 2*x-posx i+=1 res+='</tr></table>' return res def merge(self, ds): self.pos+=ds.pos class _rml_tmpl_draw_lines(_rml_tmpl_tag): def __init__(self, node, style, localcontext = {}): self.localcontext = localcontext coord = [utils.unit_get(x) for x in utils._process_text(self, node.text).split(' ')] self.ok = False self.posx = coord[0] self.posy = coord[1] self.width = coord[2]-coord[0] self.ok = coord[1]==coord[3] self.style = style self.style = style.get('hr') def tag_start(self): if self.ok: return "<table border=\'0\' cellpadding=\'0\' cellspacing=\'0\' width=\'%d\'><tr><td width=\'%d\'></td><td><hr width=\'100%%\' style=\'margin:0px; %s\'></td></tr></table>" % (self.posx+self.width,self.posx,self.style) else: return '' class _rml_stylesheet(object): def __init__(self, localcontext, stylesheet, doc): self.doc = doc self.localcontext = localcontext self.attrs = {} self._tags = { 'fontSize': lambda x: ('font-size',str(utils.unit_get(x)+5.0)+'px'), 'alignment': lambda x: ('text-align',str(x)) } result = '' for ps in stylesheet.findall('paraStyle'): attr = {} attrs = ps.attrib for key, val in attrs.items(): attr[key] = val attrs = [] for a in attr: if a in self._tags: attrs.append('%s:%s' % self._tags[a](attr[a])) if len(attrs): result += 'p.'+attr['name']+' {'+'; '.join(attrs)+'}\n' self.result = result def render(self): return self.result class _rml_draw_style(object): def __init__(self): self.style = {} self._styles = { 'fill': lambda x: {'td': {'color':x.get('color')}}, 'setFont': lambda x: {'td': {'font-size':x.get('size')+'px'}}, 'stroke': lambda x: {'hr': {'color':x.get('color')}}, } def update(self, node): if node.tag in self._styles: result = self._styles[node.tag](node) for key in result: if key in self.style: self.style[key].update(result[key]) else: self.style[key] = result[key] def font_size_get(self,tag): size = utils.unit_get(self.style.get('td', {}).get('font-size','16')) return size def get(self,tag): if not tag in self.style: return "" return ';'.join(['%s:%s' % (x[0],x[1]) for x in self.style[tag].items()]) class _rml_template(object): def __init__(self, template, localcontext=None): self.frame_pos = -1 self.localcontext = localcontext self.frames = [] self.template_order = [] self.page_template = {} self.loop = 0 self._tags = { 'drawString': _rml_tmpl_draw_string, 'drawRightString': _rml_tmpl_draw_string, 'drawCentredString': _rml_tmpl_draw_string, 'lines': _rml_tmpl_draw_lines } self.style = _rml_draw_style() rc = 'data:image/png;base64,' self.data = '' for pt in template.findall('pageTemplate'): frames = {} id = pt.get('id') self.template_order.append(id) for tmpl in pt.findall('frame'): posy = int(utils.unit_get(tmpl.get('y1'))) posx = int(utils.unit_get(tmpl.get('x1'))) frames[(posy,posx,tmpl.get('id'))] = _rml_tmpl_frame(posx, utils.unit_get(tmpl.get('width'))) for tmpl in pt.findall('pageGraphics'): for n in tmpl: if n.tag == 'image': self.data = rc + utils._process_text(self, n.text) if n.tag in self._tags: t = self._tags[n.tag](n, self.style,self.localcontext) frames[(t.posy,t.posx,n.tag)] = t else: self.style.update(n) keys = frames.keys() keys.sort() keys.reverse() self.page_template[id] = [] for key in range(len(keys)): if key>0 and keys[key-1][0] == keys[key][0]: if type(self.page_template[id][-1]) == type(frames[keys[key]]): if self.page_template[id][-1].tag_mergeable(): self.page_template[id][-1].merge(frames[keys[key]]) continue self.page_template[id].append(frames[keys[key]]) self.template = self.template_order[0] def _get_style(self): return self.style def set_next_template(self): self.template = self.template_order[(self.template_order.index(name)+1) % self.template_order] self.frame_pos = -1 def set_template(self, name): self.template = name self.frame_pos = -1 def frame_start(self): result = '' frames = self.page_template[self.template] ok = True while ok: self.frame_pos += 1 if self.frame_pos>=len(frames): self.frame_pos=0 self.loop=1 ok = False continue f = frames[self.frame_pos] result+=f.tag_start() ok = not f.tag_end() if ok: result+=f.tag_stop() return result def frame_stop(self): frames = self.page_template[self.template] f = frames[self.frame_pos] result=f.tag_stop() return result def start(self): return '' def end(self): result = '' while not self.loop: result += self.frame_start() result += self.frame_stop() return result class _rml_doc(object): def __init__(self, data, localcontext): self.dom = etree.XML(data) self.localcontext = localcontext self.filename = self.dom.get('filename') self.result = '' def render(self, out): self.result += '''<!DOCTYPE HTML PUBLIC "-//w3c//DTD HTML 4.0 Frameset//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style type="text/css"> p {margin:0px; font-size:12px;} td {font-size:14px;} ''' style = self.dom.findall('stylesheet')[0] s = _rml_stylesheet(self.localcontext, style, self.dom) self.result += s.render() self.result+=''' </style> ''' list_story =[] for story in utils._child_get(self.dom, self, 'story'): template = _rml_template(self.dom.findall('template')[0], self.localcontext) f = _flowable(template, self.dom, localcontext = self.localcontext) story_text = f.render(story) list_story.append(story_text) del f if template.data: tag = '''<img src = '%s' width=80 height=72/>'''% template.data else: tag = '' self.result +=''' <script type="text/javascript"> var indexer = 0; var aryTest = %s ; function nextData() { if(indexer < aryTest.length -1) { indexer += 1; document.getElementById("tiny_data").innerHTML=aryTest[indexer]; } } function prevData() { if (indexer > 0) { indexer -= 1; document.getElementById("tiny_data").innerHTML=aryTest[indexer]; } } </script> </head> <body> %s <div id="tiny_data"> %s </div> <br> <input type="button" value="next" onclick="nextData();"> <input type="button" value="prev" onclick="prevData();"> </body></html>'''%(list_story,tag,list_story[0]) out.write( self.result) def parseString(data,localcontext = {}, fout=None): r = _rml_doc(data, localcontext) if fout: fp = file(fout,'wb') r.render(fp) fp.close() return fout else: fp = cStringIO.StringIO() r.render(fp) return fp.getvalue() def rml2html_help(): print 'Usage: rml2html input.rml >output.html' print 'Render the standard input (RML) and output an HTML file' sys.exit(0) if __name__=="__main__": if len(sys.argv)>1: if sys.argv[1]=='--help': rml2html_help() print parseString(file(sys.argv[1], 'r').read()), else: print 'Usage: rml2html input.rml >output.html' print 'Try \'rml2html --help\' for more information.'
gpl-3.0
mwarkentin/ansible
plugins/inventory/linode.py
145
11235
#!/usr/bin/env python ''' Linode external inventory script ================================= Generates inventory that Ansible can understand by making API request to Linode using the Chube library. NOTE: This script assumes Ansible is being executed where Chube is already installed and has a valid config at ~/.chube. If not, run: pip install chube echo -e "---\napi_key: <YOUR API KEY GOES HERE>" > ~/.chube For more details, see: https://github.com/exosite/chube NOTE: This script also assumes that the Linodes in your account all have labels that correspond to hostnames that are in your resolver search path. Your resolver search path resides in /etc/hosts. When run against a specific host, this script returns the following variables: - api_id - datacenter_id - datacenter_city (lowercase city name of data center, e.g. 'tokyo') - label - display_group - create_dt - total_hd - total_xfer - total_ram - status - public_ip (The first public IP found) - private_ip (The first private IP found, or empty string if none) - alert_cpu_enabled - alert_cpu_threshold - alert_diskio_enabled - alert_diskio_threshold - alert_bwin_enabled - alert_bwin_threshold - alert_bwout_enabled - alert_bwout_threshold - alert_bwquota_enabled - alert_bwquota_threshold - backup_weekly_daily - backup_window - watchdog Peter Sankauskas did most of the legwork here with his linode plugin; I just adapted that for Linode. ''' # (c) 2013, Dan Slimmon # # This file is part of Ansible, # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. ###################################################################### # Standard imports import os import re import sys import argparse from time import time try: import json except ImportError: import simplejson as json try: from chube import load_chube_config from chube import api as chube_api from chube.datacenter import Datacenter from chube.linode_obj import Linode except: try: # remove local paths and other stuff that may # cause an import conflict, as chube is sensitive # to name collisions on importing old_path = sys.path sys.path = [d for d in sys.path if d not in ('', os.getcwd(), os.path.dirname(os.path.realpath(__file__)))] from chube import load_chube_config from chube import api as chube_api from chube.datacenter import Datacenter from chube.linode_obj import Linode sys.path = old_path except Exception, e: raise Exception("could not import chube") load_chube_config() # Imports for ansible import ConfigParser class LinodeInventory(object): def __init__(self): """Main execution path.""" # Inventory grouped by display group self.inventory = {} # Index of label to Linode ID self.index = {} # Local cache of Datacenter objects populated by populate_datacenter_cache() self._datacenter_cache = None # Read settings and parse CLI arguments self.read_settings() self.parse_cli_args() # Cache if self.args.refresh_cache: self.do_api_calls_update_cache() elif not self.is_cache_valid(): self.do_api_calls_update_cache() # Data to print if self.args.host: data_to_print = self.get_host_info() elif self.args.list: # Display list of nodes for inventory if len(self.inventory) == 0: data_to_print = self.get_inventory_from_cache() else: data_to_print = self.json_format_dict(self.inventory, True) print data_to_print def is_cache_valid(self): """Determines if the cache file has expired, or if it is still valid.""" if os.path.isfile(self.cache_path_cache): mod_time = os.path.getmtime(self.cache_path_cache) current_time = time() if (mod_time + self.cache_max_age) > current_time: if os.path.isfile(self.cache_path_index): return True return False def read_settings(self): """Reads the settings from the .ini file.""" config = ConfigParser.SafeConfigParser() config.read(os.path.dirname(os.path.realpath(__file__)) + '/linode.ini') # Cache related cache_path = config.get('linode', 'cache_path') self.cache_path_cache = cache_path + "/ansible-linode.cache" self.cache_path_index = cache_path + "/ansible-linode.index" self.cache_max_age = config.getint('linode', 'cache_max_age') def parse_cli_args(self): """Command line argument processing""" parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on Linode') parser.add_argument('--list', action='store_true', default=True, help='List nodes (default: True)') parser.add_argument('--host', action='store', help='Get all the variables about a specific node') parser.add_argument('--refresh-cache', action='store_true', default=False, help='Force refresh of cache by making API requests to Linode (default: False - use cache files)') self.args = parser.parse_args() def do_api_calls_update_cache(self): """Do API calls, and save data in cache files.""" self.get_nodes() self.write_to_cache(self.inventory, self.cache_path_cache) self.write_to_cache(self.index, self.cache_path_index) def get_nodes(self): """Makes an Linode API call to get the list of nodes.""" try: for node in Linode.search(status=Linode.STATUS_RUNNING): self.add_node(node) except chube_api.linode_api.ApiError, e: print "Looks like Linode's API is down:" print print e sys.exit(1) def get_node(self, linode_id): """Gets details about a specific node.""" try: return Linode.find(api_id=linode_id) except chube_api.linode_api.ApiError, e: print "Looks like Linode's API is down:" print print e sys.exit(1) def populate_datacenter_cache(self): """Creates self._datacenter_cache, containing all Datacenters indexed by ID.""" self._datacenter_cache = {} dcs = Datacenter.search() for dc in dcs: self._datacenter_cache[dc.api_id] = dc def get_datacenter_city(self, node): """Returns a the lowercase city name of the node's data center.""" if self._datacenter_cache is None: self.populate_datacenter_cache() location = self._datacenter_cache[node.datacenter_id].location location = location.lower() location = location.split(",")[0] return location def add_node(self, node): """Adds an node to the inventory and index.""" dest = node.label # Add to index self.index[dest] = node.api_id # Inventory: Group by node ID (always a group of 1) self.inventory[node.api_id] = [dest] # Inventory: Group by datacenter city self.push(self.inventory, self.get_datacenter_city(node), dest) # Inventory: Group by dipslay group self.push(self.inventory, node.display_group, dest) def get_host_info(self): """Get variables about a specific host.""" if len(self.index) == 0: # Need to load index from cache self.load_index_from_cache() if not self.args.host in self.index: # try updating the cache self.do_api_calls_update_cache() if not self.args.host in self.index: # host might not exist anymore return self.json_format_dict({}, True) node_id = self.index[self.args.host] node = self.get_node(node_id) node_vars = {} for direct_attr in [ "api_id", "datacenter_id", "label", "display_group", "create_dt", "total_hd", "total_xfer", "total_ram", "status", "alert_cpu_enabled", "alert_cpu_threshold", "alert_diskio_enabled", "alert_diskio_threshold", "alert_bwin_enabled", "alert_bwin_threshold", "alert_bwout_enabled", "alert_bwout_threshold", "alert_bwquota_enabled", "alert_bwquota_threshold", "backup_weekly_daily", "backup_window", "watchdog" ]: node_vars[direct_attr] = getattr(node, direct_attr) node_vars["datacenter_city"] = self.get_datacenter_city(node) node_vars["public_ip"] = [addr.address for addr in node.ipaddresses if addr.is_public][0] private_ips = [addr.address for addr in node.ipaddresses if not addr.is_public] if private_ips: node_vars["private_ip"] = private_ips[0] return self.json_format_dict(node_vars, True) def push(self, my_dict, key, element): """Pushed an element onto an array that may not have been defined in the dict.""" if key in my_dict: my_dict[key].append(element); else: my_dict[key] = [element] def get_inventory_from_cache(self): """Reads the inventory from the cache file and returns it as a JSON object.""" cache = open(self.cache_path_cache, 'r') json_inventory = cache.read() return json_inventory def load_index_from_cache(self): """Reads the index from the cache file and sets self.index.""" cache = open(self.cache_path_index, 'r') json_index = cache.read() self.index = json.loads(json_index) def write_to_cache(self, data, filename): """Writes data in JSON format to a file.""" json_data = self.json_format_dict(data, True) cache = open(filename, 'w') cache.write(json_data) cache.close() def to_safe(self, word): """Escapes any characters that would be invalid in an ansible group name.""" return re.sub("[^A-Za-z0-9\-]", "_", word) def json_format_dict(self, data, pretty=False): """Converts a dict to a JSON object and dumps it as a formatted string.""" if pretty: return json.dumps(data, sort_keys=True, indent=2) else: return json.dumps(data) LinodeInventory()
gpl-3.0
googleads/google-ads-python
google/ads/googleads/v8/services/types/location_view_service.py
1
1223
# -*- coding: utf-8 -*- # Copyright 2020 Google LLC # # 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 proto # type: ignore __protobuf__ = proto.module( package="google.ads.googleads.v8.services", marshal="google.ads.googleads.v8", manifest={"GetLocationViewRequest",}, ) class GetLocationViewRequest(proto.Message): r"""Request message for [LocationViewService.GetLocationView][google.ads.googleads.v8.services.LocationViewService.GetLocationView]. Attributes: resource_name (str): Required. The resource name of the location view to fetch. """ resource_name = proto.Field(proto.STRING, number=1,) __all__ = tuple(sorted(__protobuf__.manifest))
apache-2.0
vietpn/ghost-nodejs
node_modules/grunt-docker/node_modules/docker/node_modules/pygmentize-bundled/vendor/pygments/build-2.7/pygments/styles/murphy.py
364
2751
# -*- coding: utf-8 -*- """ pygments.styles.murphy ~~~~~~~~~~~~~~~~~~~~~~ Murphy's style from CodeRay. :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ from pygments.style import Style from pygments.token import Keyword, Name, Comment, String, Error, \ Number, Operator, Generic, Whitespace class MurphyStyle(Style): """ Murphy's style from CodeRay. """ default_style = "" styles = { Whitespace: "#bbbbbb", Comment: "#666 italic", Comment.Preproc: "#579 noitalic", Comment.Special: "#c00 bold", Keyword: "bold #289", Keyword.Pseudo: "#08f", Keyword.Type: "#66f", Operator: "#333", Operator.Word: "bold #000", Name.Builtin: "#072", Name.Function: "bold #5ed", Name.Class: "bold #e9e", Name.Namespace: "bold #0e84b5", Name.Exception: "bold #F00", Name.Variable: "#036", Name.Variable.Instance: "#aaf", Name.Variable.Class: "#ccf", Name.Variable.Global: "#f84", Name.Constant: "bold #5ed", Name.Label: "bold #970", Name.Entity: "#800", Name.Attribute: "#007", Name.Tag: "#070", Name.Decorator: "bold #555", String: "bg:#e0e0ff", String.Char: "#88F bg:", String.Doc: "#D42 bg:", String.Interpol: "bg:#eee", String.Escape: "bold #666", String.Regex: "bg:#e0e0ff #000", String.Symbol: "#fc8 bg:", String.Other: "#f88", Number: "bold #60E", Number.Integer: "bold #66f", Number.Float: "bold #60E", Number.Hex: "bold #058", Number.Oct: "bold #40E", Generic.Heading: "bold #000080", Generic.Subheading: "bold #800080", Generic.Deleted: "#A00000", Generic.Inserted: "#00A000", Generic.Error: "#FF0000", Generic.Emph: "italic", Generic.Strong: "bold", Generic.Prompt: "bold #c65d09", Generic.Output: "#888", Generic.Traceback: "#04D", Error: "#F00 bg:#FAA" }
mit
2014c2g4/w16b_test
static/Brython3.1.1-20150328-091302/Lib/site-packages/pygame/base.py
603
4652
#!/usr/bin/env python ## https://bitbucket.org/pygame/pygame/raw/2383b8ab0e2273bc83c545ab9c18fee1f3459c64/pygame/base.py '''Pygame core routines Contains the core routines that are used by the rest of the pygame modules. Its routines are merged directly into the pygame namespace. This mainly includes the auto-initialization `init` and `quit` routines. There is a small module named `locals` that also gets merged into this namespace. This contains all the constants needed by pygame. Object constructors also get placed into this namespace, you can call functions like `Rect` and `Surface` to create objects of that type. As a convenience, you can import the members of pygame.locals directly into your module's namespace with:: from pygame.locals import * Most of the pygame examples do this if you'd like to take a look. ''' __docformat__ = 'restructuredtext' __version__ = '$Id$' import atexit import sys #import SDL _quitfunctions = [] class error(RuntimeError): pass def init(): '''Autoinitialize all imported pygame modules. Initialize all imported pygame modules. Includes pygame modules that are not part of the base modules (like font and image). It does not raise exceptions, but instead silently counts which modules have failed to init. The return argument contains a count of the number of modules initialized, and the number of modules that failed to initialize. You can always initialize the modules you want by hand. The modules that need it have an `init` and `quit` routine built in, which you can call directly. They also have a `get_init` routine which you can use to doublecheck the initialization. Note that the manual `init` routines will raise an exception on error. Be aware that most platforms require the display module to be initialized before others. This `init` will handle that for you, but if you initialize by hand, be aware of this constraint. As with the manual `init` routines. It is safe to call this `init` as often as you like. :rtype: int, int :return: (count_passed, count_failed) ''' success = 0 fail = 0 #SDL.SDL_Init(SDL.SDL_INIT_EVENTTHREAD | SDL.SDL_INIT_TIMER) if _video_autoinit(): success += 1 else: fail += 1 for mod in sys.modules.values(): if hasattr(mod, '__PYGAMEinit__') and callable(mod.__PYGAMEinit__): try: mod.__PYGAMEinit__() success += 1 except: fail += 1 return success, fail def register_quit(func): '''Routine to call when pygame quits. The given callback routine will be called when pygame is quitting. Quit callbacks are served on a 'last in, first out' basis. ''' _quitfunctions.append(func) def _video_autoquit(): if SDL.SDL_WasInit(SDL.SDL_INIT_VIDEO): SDL.SDL_QuitSubSystem(SDL.SDL_INIT_VIDEO) def _video_autoinit(): return 1 #if not SDL.SDL_WasInit(SDL.SDL_INIT_VIDEO): # SDL.SDL_InitSubSystem(SDL.SDL_INIT_VIDEO) # SDL.SDL_EnableUNICODE(1) #return 1 def _atexit_quit(): while _quitfunctions: func = _quitfunctions.pop() func() _video_autoquit() #SDL.SDL_Quit() def get_sdl_version(): '''Get the version of the linked SDL runtime. :rtype: int, int, int :return: major, minor, patch ''' #v = SDL.SDL_Linked_Version() #return v.major, v.minor, v.patch return None, None, None def quit(): '''Uninitialize all pygame modules. Uninitialize all pygame modules that have been initialized. Even if you initialized the module by hand, this `quit` will uninitialize it for you. All the pygame modules are uninitialized automatically when your program exits, so you will usually not need this routine. If you program plans to keep running after it is done with pygame, then would be a good time to make this call. ''' _atexit_quit() def get_error(): '''Get current error message. SDL maintains an internal current error message. This message is usually given to you when an SDL related exception occurs, but sometimes you may want to call this directly yourself. :rtype: str ''' #return SDL.SDL_GetError() return '' def _rgba_from_obj(obj): if not type(obj) in (tuple, list): return None if len(obj) == 1: return _rgba_from_obj(obj[0]) elif len(obj) == 3: return (int(obj[0]), int(obj[1]), int(obj[2]), 255) elif len(obj) == 4: return obj else: return None atexit.register(_atexit_quit)
gpl-3.0
lucian1900/webquest
toolbars.py
1
3266
# Copyright (C) 2009, Lucian Branescu Mihaila # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA from gettext import gettext as _ import logging import gobject import gtk from sugar.graphics.toolbutton import ToolButton from sugar.graphics.alert import Alert from sugar.graphics.icon import Icon from sugar.graphics.objectchooser import ObjectChooser class WebquestToolbar(gtk.Toolbar): __gtype_name__ = "SugarWebquestToolbar" __gsignals__ = { 'toggle-send': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])), } def __init__(self, act): gtk.Toolbar.__init__(self) self._activity = act self._back = ToolButton('go-previous-paired') self._back.set_tooltip(_('Back')) self._back.props.sensitive = False self._back.connect('clicked', self.__go_back_cb) self.insert(self._back, -1) self._back.show() self._entry = gtk.Entry() self._entry.set_text(self._activity.DEFAULT_FEED_URI) self._entry.connect('activate', self.__entry_activate_cb) entry_item = gtk.ToolItem() entry_item.set_expand(True) entry_item.add(self._entry) self.insert(entry_item, -1) self._entry.show() self._refresh = ToolButton('view-refresh') self._refresh.set_tooltip(_('Refresh')) self._refresh.connect('clicked', self.__refresh_cb) self.insert(self._refresh, -1) self._refresh.show() self._send = ToolButton('activity-journal') self._send.set_tooltip(_('Send')) self._send.connect('clicked', self.__send_cb) self.insert(self._send, -1) self._send.show() def __entry_activate_cb(self, entry): self._activity.load_feed(entry.get_text()) def __go_back_cb(self, button): self._activity.show_feed_list() def __refresh_cb(self, button): self._activity.load_feed(entry.get_text()) def __send_cb(self, button): chooser = ObjectChooser() try: result = chooser.run() if result == gtk.RESPONSE_ACCEPT: jobject = chooser.get_selected_object() if jobject and jobject.file_path: logging.debug('##### %s' % jobject.file_path) finally: chooser.destroy() del chooser def enable_back(self): self._back.props.sensitive = True def disable_back(self): self._back.props.sensitive = False
gpl-2.0
wraxilan/pyblion
pyblion/gui.py
1
2480
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # The gui # import gettext import os from config import Config from PyQt5.QtWidgets import QLabel, QLineEdit, QMainWindow, QPushButton, QWidget, QVBoxLayout, QAction from PyQt5.QtCore import pyqtSlot localedir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'locales') translate = gettext.translation('gui', localedir, fallback=True) _ = translate.gettext class MainWindow(QMainWindow): def __init__(self, *args): QMainWindow.__init__(self, *args) self.setWindowTitle(_('Pyblion')) # load config self.config = Config() self.config.load() self.move(self.config.mainwindow_x, self.config.mainwindow_y) self.resize(self.config.mainwindow_witdh, self.config.mainwindow_height) # Menu Bar self.open_file_action = QAction(_('Settings'), self) self.exit_action = QAction(_('Exit'), self) self.exit_action.setMenuRole(QAction.QuitRole) file_menu = self.menuBar().addMenu(_('File')) file_menu.addAction(self.open_file_action) file_menu.addSeparator() file_menu.addAction(self.exit_action) self.about_action = QAction(_('About'), self) help_menu = self.menuBar().addMenu(_('Help')) help_menu.addAction(self.about_action) # Widgets self.hello_world_label = QLabel(_('Hello World!')) self.update_text_button = QPushButton(_('Update')) self.edit_text = QLineEdit() # Connects self.update_text_button.clicked.connect(self.update_text) self.about_action.triggered.connect(self.show_about_dialog) # Layout main_widget = QWidget() self.setCentralWidget(main_widget) main_layout = QVBoxLayout() main_widget.setLayout(main_layout) main_layout.addWidget(self.hello_world_label) main_layout.addWidget(self.edit_text) main_layout.addWidget(self.update_text_button) @pyqtSlot() def update_text(self): self.hello_world_label.setText(self.edit_text.text()) @pyqtSlot() def show_about_dialog(self): print('akjsdghkjhgasd') def closeEvent(self, event): self.config.mainwindow_witdh = self.width() self.config.mainwindow_height = self.height() self.config.mainwindow_x = self.x() self.config.mainwindow_y = self.y() self.config.save() super(MainWindow, self).closeEvent(event)
gpl-3.0
dongguangming/django-oscar
tests/integration/offer/shipping_benefit_tests.py
46
2705
from decimal import Decimal as D from django.test import TestCase from oscar.apps.offer import models, utils from oscar.apps.shipping.repository import Repository from oscar.apps.shipping.methods import FixedPrice from oscar.test.basket import add_product from oscar.test import factories def create_offer(): range = models.Range.objects.create( name="All products", includes_all_products=True) condition = models.CountCondition.objects.create( range=range, type=models.Condition.COUNT, value=1) benefit = models.ShippingFixedPriceBenefit.objects.create( type=models.Benefit.SHIPPING_FIXED_PRICE, value=D('1.00')) return models.ConditionalOffer.objects.create( condition=condition, benefit=benefit, offer_type=models.ConditionalOffer.SITE) class StubRepository(Repository): """ Stubbed shipped repository which overrides the get_shipping_methods method in order to use a non-free default shipping method. This allows the shipping discounts to be tested. """ methods = [FixedPrice(D('10.00'), D('10.00'))] class TestAnOfferWithAShippingBenefit(TestCase): def setUp(self): self.basket = factories.create_basket(empty=True) create_offer() def test_applies_correctly_to_basket_which_matches_condition(self): add_product(self.basket, D('12.00')) utils.Applicator().apply(self.basket) self.assertEqual(1, len(self.basket.offer_applications)) def test_applies_correctly_to_basket_which_exceeds_condition(self): add_product(self.basket, D('12.00'), 2) utils.Applicator().apply(self.basket) self.assertEqual(1, len(self.basket.offer_applications)) def test_wraps_shipping_method_from_repository(self): add_product(self.basket, D('12.00'), 1) utils.Applicator().apply(self.basket) methods = StubRepository().get_shipping_methods(self.basket) method = methods[0] charge = method.calculate(self.basket) self.assertEqual(D('1.00'), charge.incl_tax) def test_has_discount_recorded_correctly_when_order_is_placed(self): add_product(self.basket, D('12.00'), 1) utils.Applicator().apply(self.basket) methods = StubRepository().get_shipping_methods(self.basket) method = methods[0] order = factories.create_order(basket=self.basket, shipping_method=method) discounts = order.discounts.all() self.assertEqual(1, len(discounts)) discount = discounts[0] self.assertTrue(discount.is_shipping_discount) self.assertEqual(D('9.00'), discount.amount)
bsd-3-clause
theheros/kbengine
kbe/res/scripts/common/Lib/ctypes/test/test_python_api.py
3
3156
from ctypes import * import unittest, sys from ctypes.test import is_resource_enabled ################################################################ # This section should be moved into ctypes\__init__.py, when it's ready. from _ctypes import PyObj_FromPtr ################################################################ from sys import getrefcount as grc if sys.version_info > (2, 4): c_py_ssize_t = c_size_t else: c_py_ssize_t = c_int class PythonAPITestCase(unittest.TestCase): def test_PyBytes_FromStringAndSize(self): PyBytes_FromStringAndSize = pythonapi.PyBytes_FromStringAndSize PyBytes_FromStringAndSize.restype = py_object PyBytes_FromStringAndSize.argtypes = c_char_p, c_py_ssize_t self.assertEqual(PyBytes_FromStringAndSize(b"abcdefghi", 3), b"abc") def test_PyString_FromString(self): pythonapi.PyBytes_FromString.restype = py_object pythonapi.PyBytes_FromString.argtypes = (c_char_p,) s = b"abc" refcnt = grc(s) pyob = pythonapi.PyBytes_FromString(s) self.assertEqual(grc(s), refcnt) self.assertEqual(s, pyob) del pyob self.assertEqual(grc(s), refcnt) if is_resource_enabled("refcount"): # This test is unreliable, because it is possible that code in # unittest changes the refcount of the '42' integer. So, it # is disabled by default. def test_PyLong_Long(self): ref42 = grc(42) pythonapi.PyLong_FromLong.restype = py_object self.assertEqual(pythonapi.PyLong_FromLong(42), 42) self.assertEqual(grc(42), ref42) pythonapi.PyLong_AsLong.argtypes = (py_object,) pythonapi.PyLong_AsLong.restype = c_long res = pythonapi.PyLong_AsLong(42) self.assertEqual(grc(res), ref42 + 1) del res self.assertEqual(grc(42), ref42) def test_PyObj_FromPtr(self): s = "abc def ghi jkl" ref = grc(s) # id(python-object) is the address pyobj = PyObj_FromPtr(id(s)) self.assertTrue(s is pyobj) self.assertEqual(grc(s), ref + 1) del pyobj self.assertEqual(grc(s), ref) def test_PyOS_snprintf(self): PyOS_snprintf = pythonapi.PyOS_snprintf PyOS_snprintf.argtypes = POINTER(c_char), c_size_t, c_char_p buf = c_buffer(256) PyOS_snprintf(buf, sizeof(buf), b"Hello from %s", b"ctypes") self.assertEqual(buf.value, b"Hello from ctypes") PyOS_snprintf(buf, sizeof(buf), b"Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3) self.assertEqual(buf.value, b"Hello from ctypes (1, 2, 3)") # not enough arguments self.assertRaises(TypeError, PyOS_snprintf, buf) def test_pyobject_repr(self): self.assertEqual(repr(py_object()), "py_object(<NULL>)") self.assertEqual(repr(py_object(42)), "py_object(42)") self.assertEqual(repr(py_object(object)), "py_object(%r)" % object) if __name__ == "__main__": unittest.main()
lgpl-3.0
rubasben/namebench
tools/check_dns_servers.py
174
3835
#!/usr/bin/env python # Copyright 2009 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. """Tool for checking a lot of DNS servers from stdin for possible inclusion.""" __author__ = 'tstromberg@google.com (Thomas Stromberg)' import csv import re import sys import GeoIP sys.path.append('..') sys.path.append('/Users/tstromberg/namebench') import third_party from libnamebench import nameserver_list from libnamebench import config from libnamebench import addr_util import check_nameserver_popularity gi = GeoIP.open('/usr/local/share/GeoLiteCity.dat', GeoIP.GEOIP_MEMORY_CACHE) asn_lookup = GeoIP.open('/usr/local/share/GeoIPASNum.dat', GeoIP.GEOIP_MEMORY_CACHE) existing_nameservers = config.GetLocalNameServerList() check_ns = [] output = csv.writer(open('output.csv', 'w')) for line in sys.stdin: ips = addr_util.ExtractIPsFromString(line) for ip in ips: print ip # disable IPV6 until we can improve our regular expression matching if ':' in ip: continue if ip not in existing_nameservers: check_ns.append((ip, ip)) if not check_ns: print "no new servers to check" sys.exit(1) else: print "%s servers to check" % len(check_ns) print '-' * 80 nameserver_list.MAX_INITIAL_HEALTH_THREAD_COUNT = 100 nameservers = nameserver_list.NameServers([], global_servers=check_ns, timeout=10, health_timeout=10, threads=100, num_servers=5000, skip_cache_collusion_checks=True, ) nameservers.min_healthy_percent = 0 sanity_checks = config.GetLocalSanityChecks() try: nameservers.CheckHealth(sanity_checks['primary'], sanity_checks['secondary']) except nameserver_list.TooFewNameservers: pass print '-' * 80 for ns in nameservers: try: details = gi.record_by_addr(ns.ip) except: pass if not details: details = {} city = details.get('city', '') if city: city = city.decode('latin-1') latitude = details.get('latitude', '') longitude = details.get('longitude', '') country = details.get('country_name', '') if country: country = country.decode('latin-1') country_code = details.get('country_code', '') region = details.get('region_name', '') if region: region = region.decode('latin-1') try: results = check_nameserver_popularity.CheckPopularity(ns.ip) urls = [ x['Url'] for x in results ] except: urls = ['(exception)'] num_urls = len(urls) main = "%s=UNKNOWN" % ns.ip if 'Responded with: REFUSED' in ns.warnings: note = '_REFUSED_' elif 'a.root-servers.net.: Timeout' in ns.warnings: note = '_TIMEOUT_' elif 'No answer (NOERROR): a.root-servers.net.' in ns.warnings: note = '_NOANSWER_' elif ns.warnings: note = '_WARNING/%s_' % '/'.join(list(ns.warnings)) else: note = '' if ns.hostname != ns.ip: domain = addr_util.GetDomainPartOfHostname(ns.hostname) if domain: good_urls = [x for x in urls if re.search(domain, x, re.I)] if good_urls: urls = good_urls geo = '/'.join([x for x in [country_code, region, city] if x and not x.isdigit()]).encode('utf-8') coords = ','.join(map(str, [latitude,longitude])) asn = asn_lookup.org_by_addr(ns.ip) row = [ns.ip, 'regional', 'UNKNOWN', '', ns.hostname, geo, coords, asn, note, num_urls, ' '.join(urls[:2]), ns.version] print row output.writerow(row)
apache-2.0
TeachAtTUM/edx-platform
common/djangoapps/util/module_utils.py
65
1421
""" Utility library containing operations used/shared by multiple courseware modules """ def yield_dynamic_descriptor_descendants(descriptor, user_id, module_creator=None): # pylint: disable=invalid-name """ This returns all of the descendants of a descriptor. If the descriptor has dynamic children, the module will be created using module_creator and the children (as descriptors) of that module will be returned. """ stack = [descriptor] while len(stack) > 0: next_descriptor = stack.pop() stack.extend(get_dynamic_descriptor_children(next_descriptor, user_id, module_creator)) yield next_descriptor def get_dynamic_descriptor_children(descriptor, user_id, module_creator=None, usage_key_filter=None): """ Returns the children of the given descriptor, while supporting descriptors with dynamic children. """ module_children = [] if descriptor.has_dynamic_children(): module = None if descriptor.scope_ids.user_id and user_id == descriptor.scope_ids.user_id: # do not rebind the module if it's already bound to a user. module = descriptor elif module_creator: module = module_creator(descriptor) if module: module_children = module.get_child_descriptors() else: module_children = descriptor.get_children(usage_key_filter) return module_children
agpl-3.0
chand3040/cloud_that
common/djangoapps/student/tests/test_course_listing.py
18
6692
""" Unit tests for getting the list of courses for a user through iterating all courses and by reversing group name formats. """ import mock from mock import patch, Mock from student.tests.factories import UserFactory from student.roles import GlobalStaff from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.django import modulestore from xmodule.error_module import ErrorDescriptor from django.test.client import Client from student.models import CourseEnrollment from student.views import get_course_enrollment_pairs from util.milestones_helpers import ( get_pre_requisite_courses_not_completed, set_prerequisite_courses, seed_milestone_relationship_types ) import unittest from django.conf import settings class TestCourseListing(ModuleStoreTestCase): """ Unit tests for getting the list of courses for a logged in user """ def setUp(self): """ Add a student & teacher """ super(TestCourseListing, self).setUp() self.student = UserFactory() self.teacher = UserFactory() GlobalStaff().add_users(self.teacher) self.client = Client() self.client.login(username=self.teacher.username, password='test') def _create_course_with_access_groups(self, course_location, metadata=None, default_store=None): """ Create dummy course with 'CourseFactory' and enroll the student """ metadata = {} if not metadata else metadata course = CourseFactory.create( org=course_location.org, number=course_location.course, run=course_location.run, metadata=metadata, default_store=default_store ) CourseEnrollment.enroll(self.student, course.id) return course def tearDown(self): """ Reverse the setup """ self.client.logout() super(TestCourseListing, self).tearDown() @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') def test_get_course_list(self): """ Test getting courses """ course_location = self.store.make_course_key('Org1', 'Course1', 'Run1') self._create_course_with_access_groups(course_location) # get dashboard courses_list = list(get_course_enrollment_pairs(self.student, None, [])) self.assertEqual(len(courses_list), 1) self.assertEqual(courses_list[0][0].id, course_location) CourseEnrollment.unenroll(self.student, course_location) # get dashboard courses_list = list(get_course_enrollment_pairs(self.student, None, [])) self.assertEqual(len(courses_list), 0) def test_errored_course_regular_access(self): """ Test the course list for regular staff when get_course returns an ErrorDescriptor """ # pylint: disable=protected-access mongo_store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.mongo) course_key = mongo_store.make_course_key('Org1', 'Course1', 'Run1') self._create_course_with_access_groups(course_key, default_store=ModuleStoreEnum.Type.mongo) with patch('xmodule.modulestore.mongo.base.MongoKeyValueStore', Mock(side_effect=Exception)): self.assertIsInstance(modulestore().get_course(course_key), ErrorDescriptor) # get courses through iterating all courses courses_list = list(get_course_enrollment_pairs(self.student, None, [])) self.assertEqual(courses_list, []) def test_course_listing_errored_deleted_courses(self): """ Create good courses, courses that won't load, and deleted courses which still have roles. Test course listing. """ mongo_store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.mongo) good_location = mongo_store.make_course_key('testOrg', 'testCourse', 'RunBabyRun') self._create_course_with_access_groups(good_location, default_store=ModuleStoreEnum.Type.mongo) course_location = mongo_store.make_course_key('testOrg', 'doomedCourse', 'RunBabyRun') self._create_course_with_access_groups(course_location, default_store=ModuleStoreEnum.Type.mongo) mongo_store.delete_course(course_location, ModuleStoreEnum.UserID.test) courses_list = list(get_course_enrollment_pairs(self.student, None, [])) self.assertEqual(len(courses_list), 1, courses_list) self.assertEqual(courses_list[0][0].id, good_location) @mock.patch.dict("django.conf.settings.FEATURES", {'ENABLE_PREREQUISITE_COURSES': True, 'MILESTONES_APP': True}) def test_course_listing_has_pre_requisite_courses(self): """ Creates four courses. Enroll test user in all courses Sets two of them as pre-requisites of another course. Checks course where pre-requisite course is set has appropriate info. """ seed_milestone_relationship_types() course_location2 = self.store.make_course_key('Org1', 'Course2', 'Run2') self._create_course_with_access_groups(course_location2) pre_requisite_course_location = self.store.make_course_key('Org1', 'Course3', 'Run3') self._create_course_with_access_groups(pre_requisite_course_location) pre_requisite_course_location2 = self.store.make_course_key('Org1', 'Course4', 'Run4') self._create_course_with_access_groups(pre_requisite_course_location2) # create a course with pre_requisite_courses pre_requisite_courses = [ unicode(pre_requisite_course_location), unicode(pre_requisite_course_location2), ] course_location = self.store.make_course_key('Org1', 'Course1', 'Run1') self._create_course_with_access_groups(course_location, { 'pre_requisite_courses': pre_requisite_courses }) set_prerequisite_courses(course_location, pre_requisite_courses) # get dashboard course_enrollment_pairs = list(get_course_enrollment_pairs(self.student, None, [])) courses_having_prerequisites = frozenset(course.id for course, _enrollment in course_enrollment_pairs if course.pre_requisite_courses) courses_requirements_not_met = get_pre_requisite_courses_not_completed( self.student, courses_having_prerequisites ) self.assertEqual(len(courses_requirements_not_met[course_location]['courses']), len(pre_requisite_courses))
agpl-3.0
OpenWinCon/OpenWinNet
web-gui/myvenv/lib/python3.4/site-packages/setuptools/command/sdist.py
149
8498
import os import re import sys from glob import glob import pkg_resources from distutils.command.sdist import sdist as _sdist from distutils.util import convert_path from distutils import log from setuptools import svn_utils READMES = ('README', 'README.rst', 'README.txt') def walk_revctrl(dirname=''): """Find all files under revision control""" for ep in pkg_resources.iter_entry_points('setuptools.file_finders'): for item in ep.load()(dirname): yield item #TODO will need test case class re_finder(object): """ Finder that locates files based on entries in a file matched by a regular expression. """ def __init__(self, path, pattern, postproc=lambda x: x): self.pattern = pattern self.postproc = postproc self.entries_path = convert_path(path) def _finder(self, dirname, filename): f = open(filename,'rU') try: data = f.read() finally: f.close() for match in self.pattern.finditer(data): path = match.group(1) # postproc was formerly used when the svn finder # was an re_finder for calling unescape path = self.postproc(path) yield svn_utils.joinpath(dirname, path) def find(self, dirname=''): path = svn_utils.joinpath(dirname, self.entries_path) if not os.path.isfile(path): # entries file doesn't exist return for path in self._finder(dirname,path): if os.path.isfile(path): yield path elif os.path.isdir(path): for item in self.find(path): yield item __call__ = find def _default_revctrl(dirname=''): 'Primary svn_cvs entry point' for finder in finders: for item in finder(dirname): yield item finders = [ re_finder('CVS/Entries', re.compile(r"^\w?/([^/]+)/", re.M)), svn_utils.svn_finder, ] class sdist(_sdist): """Smart sdist that finds anything supported by revision control""" user_options = [ ('formats=', None, "formats for source distribution (comma-separated list)"), ('keep-temp', 'k', "keep the distribution tree around after creating " + "archive file(s)"), ('dist-dir=', 'd', "directory to put the source distribution archive(s) in " "[default: dist]"), ] negative_opt = {} def run(self): self.run_command('egg_info') ei_cmd = self.get_finalized_command('egg_info') self.filelist = ei_cmd.filelist self.filelist.append(os.path.join(ei_cmd.egg_info,'SOURCES.txt')) self.check_readme() # Run sub commands for cmd_name in self.get_sub_commands(): self.run_command(cmd_name) # Call check_metadata only if no 'check' command # (distutils <= 2.6) import distutils.command if 'check' not in distutils.command.__all__: self.check_metadata() self.make_distribution() dist_files = getattr(self.distribution,'dist_files',[]) for file in self.archive_files: data = ('sdist', '', file) if data not in dist_files: dist_files.append(data) def __read_template_hack(self): # This grody hack closes the template file (MANIFEST.in) if an # exception occurs during read_template. # Doing so prevents an error when easy_install attempts to delete the # file. try: _sdist.read_template(self) except: sys.exc_info()[2].tb_next.tb_frame.f_locals['template'].close() raise # Beginning with Python 2.7.2, 3.1.4, and 3.2.1, this leaky file handle # has been fixed, so only override the method if we're using an earlier # Python. has_leaky_handle = ( sys.version_info < (2,7,2) or (3,0) <= sys.version_info < (3,1,4) or (3,2) <= sys.version_info < (3,2,1) ) if has_leaky_handle: read_template = __read_template_hack def add_defaults(self): standards = [READMES, self.distribution.script_name] for fn in standards: if isinstance(fn, tuple): alts = fn got_it = 0 for fn in alts: if os.path.exists(fn): got_it = 1 self.filelist.append(fn) break if not got_it: self.warn("standard file not found: should have one of " + ', '.join(alts)) else: if os.path.exists(fn): self.filelist.append(fn) else: self.warn("standard file '%s' not found" % fn) optional = ['test/test*.py', 'setup.cfg'] for pattern in optional: files = list(filter(os.path.isfile, glob(pattern))) if files: self.filelist.extend(files) # getting python files if self.distribution.has_pure_modules(): build_py = self.get_finalized_command('build_py') self.filelist.extend(build_py.get_source_files()) # This functionality is incompatible with include_package_data, and # will in fact create an infinite recursion if include_package_data # is True. Use of include_package_data will imply that # distutils-style automatic handling of package_data is disabled if not self.distribution.include_package_data: for _, src_dir, _, filenames in build_py.data_files: self.filelist.extend([os.path.join(src_dir, filename) for filename in filenames]) if self.distribution.has_ext_modules(): build_ext = self.get_finalized_command('build_ext') self.filelist.extend(build_ext.get_source_files()) if self.distribution.has_c_libraries(): build_clib = self.get_finalized_command('build_clib') self.filelist.extend(build_clib.get_source_files()) if self.distribution.has_scripts(): build_scripts = self.get_finalized_command('build_scripts') self.filelist.extend(build_scripts.get_source_files()) def check_readme(self): for f in READMES: if os.path.exists(f): return else: self.warn( "standard file not found: should have one of " +', '.join(READMES) ) def make_release_tree(self, base_dir, files): _sdist.make_release_tree(self, base_dir, files) # Save any egg_info command line options used to create this sdist dest = os.path.join(base_dir, 'setup.cfg') if hasattr(os,'link') and os.path.exists(dest): # unlink and re-copy, since it might be hard-linked, and # we don't want to change the source version os.unlink(dest) self.copy_file('setup.cfg', dest) self.get_finalized_command('egg_info').save_version_info(dest) def _manifest_is_not_generated(self): # check for special comment used in 2.7.1 and higher if not os.path.isfile(self.manifest): return False fp = open(self.manifest, 'rbU') try: first_line = fp.readline() finally: fp.close() return first_line != '# file GENERATED by distutils, do NOT edit\n'.encode() def read_manifest(self): """Read the manifest file (named by 'self.manifest') and use it to fill in 'self.filelist', the list of files to include in the source distribution. """ log.info("reading manifest file '%s'", self.manifest) manifest = open(self.manifest, 'rbU') for line in manifest: # The manifest must contain UTF-8. See #303. if sys.version_info >= (3,): try: line = line.decode('UTF-8') except UnicodeDecodeError: log.warn("%r not UTF-8 decodable -- skipping" % line) continue # ignore comments and blank lines line = line.strip() if line.startswith('#') or not line: continue self.filelist.append(line) manifest.close()
apache-2.0
mengxn/tensorflow
tensorflow/contrib/quantization/python/nn_ops.py
179
1142
# Copyright 2015 The TensorFlow Authors. 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. # ============================================================================== """Wrappers for primitive Neural Net (NN) Operations.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function # pylint: disable=unused-import,wildcard-import from tensorflow.python.framework import common_shapes from tensorflow.python.framework import ops from tensorflow.python.ops import gen_nn_ops from tensorflow.python.ops.gen_nn_ops import * # pylint: enable=unused-import,wildcard-import
apache-2.0
SunDwarf/curious
curious/core/event.py
1
11655
# This file is part of curious. # # curious is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # curious is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with curious. If not, see <http://www.gnu.org/licenses/>. """ Special helpers for events. .. currentmodule: curious.core.events """ import functools import inspect import logging import typing import multio from async_generator import asynccontextmanager from multidict import MultiDict from curious.core import client as md_client from curious.core.gateway import GatewayHandler from curious.util import remove_from_multidict, safe_generator logger = logging.getLogger("curious.events") class ListenerExit(Exception): """ Raised when a temporary listener is to be exited. .. code-block:: python3 def listener(ctx, message): if message.author.id == message.guild.owner_id: raise ListenerExit """ @asynccontextmanager @safe_generator async def _wait_for_manager(manager, name: str, predicate): """ Helper class for managing a wait_for. """ async with multio.asynclib.task_manager() as tg: try: partial = functools.partial(manager.wait_for, name, predicate) await multio.asynclib.spawn(tg, partial) yield except: await multio.asynclib.cancel_task_group(tg) raise class EventManager(object): """ A manager for events. This deals with firing of events and temporary listeners. """ def __init__(self): #: The task manager used to spawn events. self.task_manager = None #: A list of event hooks. self.event_hooks = set() #: A MultiDict of event listeners. self.event_listeners = MultiDict() #: A MultiDict of temporary listeners. self.temporary_listeners = MultiDict() # add or removal functions # Events def add_event(self, func, name: str = None): """ Add an event to the internal registry of events. :param name: The event name to register under. :param func: The function to add. """ if not inspect.iscoroutinefunction(func): raise TypeError("Event must be an async function") if name is None: evs = func.events else: evs = [name] for ev_name in evs: logger.debug("Registered event `{}` handling `{}`".format(func, ev_name)) self.event_listeners.add(ev_name, func) def remove_event(self, name: str, func): """ Removes a function event. :param name: The name the event is registered under. :param func: The function to remove. """ self.event_listeners = remove_from_multidict(self.event_listeners, key=name, item=func) # listeners def add_temporary_listener(self, name: str, listener): """ Adds a new temporary listener. To remove the listener, you can raise ListenerExit which will exit it and remove the listener from the list. :param name: The name of the event to listen to. :param listener: The listener function. """ self.temporary_listeners.add(name, listener) def remove_listener_early(self, name: str, listener): """ Removes a temporary listener early. :param name: The name of the event the listener is registered under. :param listener: The listener function. """ self.event_listeners = remove_from_multidict(self.event_listeners, key=name, item=listener) def add_event_hook(self, listener): """ Adds an event hook. :param listener: The event hook callable to use. """ logger.warning("Adding event hook '%s'", listener) self.event_hooks.add(listener) def remove_event_hook(self, listener): """ Removes an event hook. """ self.event_hooks.remove(listener) # wrapper functions async def _safety_wrapper(self, func, *args, **kwargs): """ Ensures a coro's error is caught and doesn't balloon out. """ try: await func(*args, **kwargs) except Exception as e: logger.exception("Unhandled exception in {}!".format(func.__name__), exc_info=True) async def _listener_wrapper(self, key: str, func, *args, **kwargs): """ Wraps a listener, ensuring ListenerExit is handled properly. """ try: await func(*args, **kwargs) except ListenerExit: # remove the function self.temporary_listeners = remove_from_multidict(self.temporary_listeners, key, func) except Exception: logger.exception("Unhandled exception in listener {}!".format(func.__name__), exc_info=True) self.temporary_listeners = remove_from_multidict(self.temporary_listeners, key, func) async def wait_for(self, event_name: str, predicate=None): """ Waits for an event. Returning a truthy value from the predicate will cause it to exit and return. :param event_name: The name of the event. :param predicate: The predicate to use to check for the event. """ p = multio.Promise() errored = False async def listener(ctx, *args): # exit immediately if the predicate is none if predicate is None: await p.set(args) raise ListenerExit try: res = predicate(*args) if inspect.isawaitable(res): res = await res except ListenerExit: # ??? await p.set(args) raise except Exception as e: # something bad happened, set exception and exit logger.exception("Exception in wait_for predicate!") # signal that an error happened nonlocal errored errored = True await p.set(e) raise ListenerExit else: # exit now if result is true if res is True: await p.set(args) raise ListenerExit self.add_temporary_listener(name=event_name, listener=listener) output = await p.wait() if errored: raise output # unwrap tuples, if applicable if len(output) == 1: return output[0] return output def wait_for_manager(self, event_name: str, predicate) -> 'typing.AsyncContextManager[None]': """ Returns a context manager that can be used to run some steps whilst waiting for a temporary listener. .. code-block:: python async with client.events.wait_for_manager("member_update", predicate=...): await member.nickname.set("Test") This probably won't be needed outside of internal library functions. """ return _wait_for_manager(self, event_name, predicate) async def spawn(self, cofunc, *args) -> typing.Any: """ Spawns a new async function using our task manager. Usage:: async def myfn(a, b): await do_some_operation(a + b) await events.spawn(myfn, 1, 2) :param cofunc: The async function to spawn. :param args: Args to provide to the async function. """ return await multio.asynclib.spawn(self.task_manager, cofunc, *args) async def fire_event(self, event_name: str, *args, **kwargs): """ Fires an event. :param event_name: The name of the event to fire. """ if "ctx" not in kwargs: gateway = kwargs.pop("gateway") client = kwargs.pop("client") ctx = EventContext(client, gateway.gw_state.shard_id, event_name) else: ctx = kwargs.pop("ctx") # clobber event name ctx.event_name = event_name # always ensure hooks are ran first for hook in self.event_hooks: cofunc = functools.partial(hook, ctx, *args, **kwargs) await self.spawn(cofunc) for handler in self.event_listeners.getall(event_name, []): coro = functools.partial(handler, ctx, *args, **kwargs) coro.__name__ = handler.__name__ await self.spawn(self._safety_wrapper, coro) for listener in self.temporary_listeners.getall(event_name, []): coro = functools.partial(self._listener_wrapper, event_name, listener, ctx, *args, **kwargs) await self.spawn(coro) def event(name, scan: bool = True): """ Marks a function as an event. :param name: The name of the event. :param scan: Should this event be handled in scans too? """ def __innr(f): if not hasattr(f, "events"): f.events = {name} f.is_event = True f.events.add(name) f.scan = scan return f return __innr def scan_events(obb) -> typing.Generator[None, typing.Tuple[str, typing.Any], None]: """ Scans an object for any items marked as an event and yields them. """ def _pred(f): is_event = getattr(f, "is_event", False) if not is_event: return False if not f.scan: return False return True for _, item in inspect.getmembers(obb, predicate=_pred): yield (_, item) class EventContext(object): """ Represents a special context that are passed to events. """ def __init__(self, cl: 'md_client.Client', shard_id: int, event_name: str): """ :param cl: The :class:`.Client` instance for this event context. :param shard_id: The shard ID this event is for. :param event_name: The event name for this event. """ #: The :class:`.Client` instance that this event was fired under. self.bot = cl #: The shard this event was received on. self.shard_id = shard_id # type: int #: The shard for this bot. self.shard_count = cl.shard_count # type: int #: The event name for this event. self.event_name = event_name # type: str @property def handlers(self) -> typing.List[typing.Callable[['EventContext'], None]]: """ :return: A list of handlers registered for this event. """ return self.bot.events.getall(self.event_name, []) async def change_status(self, *args, **kwargs) -> None: """ Changes the current status for this shard. This takes the same arguments as :class:`.Client.change_status`, but ignoring the shard ID. """ kwargs["shard_id"] = self.shard_id return await self.bot.change_status(*args, **kwargs) @property def gateway(self) -> GatewayHandler: """ :return: The :class:`.Gateway` that produced this event. """ return self.bot.gateways[self.shard_id]
mit
zaibacu/PyAbstractApi
pyabstractapi/http.py
1
1162
import urllib.request import urllib.parse import json from functools import partial class Methods(object): def __init__(self, url, resource, apiinfo=None): self.url = url self.resource = resource if apiinfo: self.apiinfo = dict(apiinfo) else: self.apiinfo = {} self.headers = {"Content-Type": "application/json", "Accept": "*/*", "User-Agent": "Mozilla/5.0"} def request(self, resource, method, body): meth = method.upper() def encode_fn(*args): return urllib.parse.urlencode(*args) data = encode_fn(body) if meth == "GET" or meth == "DELETE": return urllib.request.Request(url="{0}/{1}?{2}".format(self.url, resource, data), method=meth, headers=self.headers) else: return urllib.request.Request(url=self.url + resource, method=meth, headers=self.headers, data=data.encode("UTF-8")) def __req(self, method, **kwargs): body = dict(self.apiinfo) body.update(**kwargs) req = self.request(method=method, resource=self.resource, body=body) print(req.full_url) with urllib.request.urlopen(req) as f: return json.loads(f.read().decode("UTF-8")) def __getattr__(self, item): return partial(self.__req, item)
mit
mrwags/python-social-auth
social/apps/flask_app/routes.py
38
1614
from flask import g, Blueprint, request from flask.ext.login import login_required, login_user from social.actions import do_auth, do_complete, do_disconnect from social.apps.flask_app.utils import psa social_auth = Blueprint('social', __name__) @social_auth.route('/login/<string:backend>/', methods=('GET', 'POST')) @psa('social.complete') def auth(backend): return do_auth(g.backend) @social_auth.route('/complete/<string:backend>/', methods=('GET', 'POST')) @psa('social.complete') def complete(backend, *args, **kwargs): """Authentication complete view, override this view if transaction management doesn't suit your needs.""" return do_complete(g.backend, login=do_login, user=g.user, *args, **kwargs) @social_auth.route('/disconnect/<string:backend>/', methods=('POST',)) @social_auth.route('/disconnect/<string:backend>/<int:association_id>/', methods=('POST',)) @social_auth.route('/disconnect/<string:backend>/<string:association_id>/', methods=('POST',)) @login_required @psa() def disconnect(backend, association_id=None): """Disconnects given backend from current logged in user.""" return do_disconnect(g.backend, g.user, association_id) def do_login(backend, user, social_user): name = backend.strategy.setting('REMEMBER_SESSION_NAME', 'keep') remember = backend.strategy.session_get(name) or \ request.cookies.get(name) or \ request.args.get(name) or \ request.form.get(name) or \ False return login_user(user, remember=remember)
bsd-3-clause
cdlib/RecordExpress
collection_record/urls.py
1
1558
from django.conf.urls.defaults import * from collection_record.views import add_collection_record from collection_record.views import edit_collection_record from collection_record.views import view_collection_record_xml from collection_record.views import view_all_collection_records from collection_record.views import view_collection_record_oac_preview urlpatterns = patterns('', url(r'^add/', add_collection_record, name='collection_record_add'), url(r'^(?P<ark>ark:/\d+/\w+)/$', edit_collection_record, name='collectionrecord_view'), url(r'^(?P<ark>ark:/\d+/\w+)/oac$', view_collection_record_oac_preview, name='collectionrecord_view'), url(r'^(?P<ark>ark:/\d+/\w+)/xml/$', view_collection_record_xml, name='collectionrecord_view_xml'), url(r'^(?P<ark>ark:/\d+/\w+)/oac/$', view_collection_record_oac_preview, name='collectionrecord_view_oac'), url(r'^(?P<ark>ark:/\d+/\w+)/edit/$', edit_collection_record, name='collectionrecord_edit'), url(r'^(?P<pk>\d+)/$', edit_collection_record, name='collectionrecord_view'), url(r'^(?P<pk>\d+)/xml/$', view_collection_record_xml, name='collectionrecord_view_xml'), url(r'^(?P<pk>\d+)/oac/$', view_collection_record_oac_preview, name='collectionrecord_view_oac'), url(r'^(?P<pk>\d+)/edit/$', edit_collection_record, name='collectionrecord_edit'), url(r'^/$', view_all_collection_records, name='collection_record_view_all'), url(r'^$', view_all_collection_records, name='collection_record_view_all'),#NOTE: when "included" the / winds up as an empty path in here )
bsd-3-clause
gameduell/duell
bin/win/python2.7.9/Lib/distutils/command/install.py
88
26260
"""distutils.command.install Implements the Distutils 'install' command.""" from distutils import log # This module should be kept compatible with Python 2.1. __revision__ = "$Id$" import sys, os, string from types import * from distutils.core import Command from distutils.debug import DEBUG from distutils.sysconfig import get_config_vars from distutils.errors import DistutilsPlatformError from distutils.file_util import write_file from distutils.util import convert_path, subst_vars, change_root from distutils.util import get_platform from distutils.errors import DistutilsOptionError from site import USER_BASE from site import USER_SITE if sys.version < "2.2": WINDOWS_SCHEME = { 'purelib': '$base', 'platlib': '$base', 'headers': '$base/Include/$dist_name', 'scripts': '$base/Scripts', 'data' : '$base', } else: WINDOWS_SCHEME = { 'purelib': '$base/Lib/site-packages', 'platlib': '$base/Lib/site-packages', 'headers': '$base/Include/$dist_name', 'scripts': '$base/Scripts', 'data' : '$base', } INSTALL_SCHEMES = { 'unix_prefix': { 'purelib': '$base/lib/python$py_version_short/site-packages', 'platlib': '$platbase/lib/python$py_version_short/site-packages', 'headers': '$base/include/python$py_version_short/$dist_name', 'scripts': '$base/bin', 'data' : '$base', }, 'unix_home': { 'purelib': '$base/lib/python', 'platlib': '$base/lib/python', 'headers': '$base/include/python/$dist_name', 'scripts': '$base/bin', 'data' : '$base', }, 'unix_user': { 'purelib': '$usersite', 'platlib': '$usersite', 'headers': '$userbase/include/python$py_version_short/$dist_name', 'scripts': '$userbase/bin', 'data' : '$userbase', }, 'nt': WINDOWS_SCHEME, 'nt_user': { 'purelib': '$usersite', 'platlib': '$usersite', 'headers': '$userbase/Python$py_version_nodot/Include/$dist_name', 'scripts': '$userbase/Scripts', 'data' : '$userbase', }, 'os2': { 'purelib': '$base/Lib/site-packages', 'platlib': '$base/Lib/site-packages', 'headers': '$base/Include/$dist_name', 'scripts': '$base/Scripts', 'data' : '$base', }, 'os2_home': { 'purelib': '$usersite', 'platlib': '$usersite', 'headers': '$userbase/include/python$py_version_short/$dist_name', 'scripts': '$userbase/bin', 'data' : '$userbase', }, } # The keys to an installation scheme; if any new types of files are to be # installed, be sure to add an entry to every installation scheme above, # and to SCHEME_KEYS here. SCHEME_KEYS = ('purelib', 'platlib', 'headers', 'scripts', 'data') class install (Command): description = "install everything from build directory" user_options = [ # Select installation scheme and set base director(y|ies) ('prefix=', None, "installation prefix"), ('exec-prefix=', None, "(Unix only) prefix for platform-specific files"), ('home=', None, "(Unix only) home directory to install under"), ('user', None, "install in user site-package '%s'" % USER_SITE), # Or, just set the base director(y|ies) ('install-base=', None, "base installation directory (instead of --prefix or --home)"), ('install-platbase=', None, "base installation directory for platform-specific files " + "(instead of --exec-prefix or --home)"), ('root=', None, "install everything relative to this alternate root directory"), # Or, explicitly set the installation scheme ('install-purelib=', None, "installation directory for pure Python module distributions"), ('install-platlib=', None, "installation directory for non-pure module distributions"), ('install-lib=', None, "installation directory for all module distributions " + "(overrides --install-purelib and --install-platlib)"), ('install-headers=', None, "installation directory for C/C++ headers"), ('install-scripts=', None, "installation directory for Python scripts"), ('install-data=', None, "installation directory for data files"), # Byte-compilation options -- see install_lib.py for details, as # these are duplicated from there (but only install_lib does # anything with them). ('compile', 'c', "compile .py to .pyc [default]"), ('no-compile', None, "don't compile .py files"), ('optimize=', 'O', "also compile with optimization: -O1 for \"python -O\", " "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"), # Miscellaneous control options ('force', 'f', "force installation (overwrite any existing files)"), ('skip-build', None, "skip rebuilding everything (for testing/debugging)"), # Where to install documentation (eventually!) #('doc-format=', None, "format of documentation to generate"), #('install-man=', None, "directory for Unix man pages"), #('install-html=', None, "directory for HTML documentation"), #('install-info=', None, "directory for GNU info files"), ('record=', None, "filename in which to record list of installed files"), ] boolean_options = ['compile', 'force', 'skip-build', 'user'] negative_opt = {'no-compile' : 'compile'} def initialize_options (self): # High-level options: these select both an installation base # and scheme. self.prefix = None self.exec_prefix = None self.home = None self.user = 0 # These select only the installation base; it's up to the user to # specify the installation scheme (currently, that means supplying # the --install-{platlib,purelib,scripts,data} options). self.install_base = None self.install_platbase = None self.root = None # These options are the actual installation directories; if not # supplied by the user, they are filled in using the installation # scheme implied by prefix/exec-prefix/home and the contents of # that installation scheme. self.install_purelib = None # for pure module distributions self.install_platlib = None # non-pure (dists w/ extensions) self.install_headers = None # for C/C++ headers self.install_lib = None # set to either purelib or platlib self.install_scripts = None self.install_data = None self.install_userbase = USER_BASE self.install_usersite = USER_SITE self.compile = None self.optimize = None # These two are for putting non-packagized distributions into their # own directory and creating a .pth file if it makes sense. # 'extra_path' comes from the setup file; 'install_path_file' can # be turned off if it makes no sense to install a .pth file. (But # better to install it uselessly than to guess wrong and not # install it when it's necessary and would be used!) Currently, # 'install_path_file' is always true unless some outsider meddles # with it. self.extra_path = None self.install_path_file = 1 # 'force' forces installation, even if target files are not # out-of-date. 'skip_build' skips running the "build" command, # handy if you know it's not necessary. 'warn_dir' (which is *not* # a user option, it's just there so the bdist_* commands can turn # it off) determines whether we warn about installing to a # directory not in sys.path. self.force = 0 self.skip_build = 0 self.warn_dir = 1 # These are only here as a conduit from the 'build' command to the # 'install_*' commands that do the real work. ('build_base' isn't # actually used anywhere, but it might be useful in future.) They # are not user options, because if the user told the install # command where the build directory is, that wouldn't affect the # build command. self.build_base = None self.build_lib = None # Not defined yet because we don't know anything about # documentation yet. #self.install_man = None #self.install_html = None #self.install_info = None self.record = None # -- Option finalizing methods ------------------------------------- # (This is rather more involved than for most commands, # because this is where the policy for installing third- # party Python modules on various platforms given a wide # array of user input is decided. Yes, it's quite complex!) def finalize_options (self): # This method (and its pliant slaves, like 'finalize_unix()', # 'finalize_other()', and 'select_scheme()') is where the default # installation directories for modules, extension modules, and # anything else we care to install from a Python module # distribution. Thus, this code makes a pretty important policy # statement about how third-party stuff is added to a Python # installation! Note that the actual work of installation is done # by the relatively simple 'install_*' commands; they just take # their orders from the installation directory options determined # here. # Check for errors/inconsistencies in the options; first, stuff # that's wrong on any platform. if ((self.prefix or self.exec_prefix or self.home) and (self.install_base or self.install_platbase)): raise DistutilsOptionError, \ ("must supply either prefix/exec-prefix/home or " + "install-base/install-platbase -- not both") if self.home and (self.prefix or self.exec_prefix): raise DistutilsOptionError, \ "must supply either home or prefix/exec-prefix -- not both" if self.user and (self.prefix or self.exec_prefix or self.home or self.install_base or self.install_platbase): raise DistutilsOptionError("can't combine user with prefix, " "exec_prefix/home, or install_(plat)base") # Next, stuff that's wrong (or dubious) only on certain platforms. if os.name != "posix": if self.exec_prefix: self.warn("exec-prefix option ignored on this platform") self.exec_prefix = None # Now the interesting logic -- so interesting that we farm it out # to other methods. The goal of these methods is to set the final # values for the install_{lib,scripts,data,...} options, using as # input a heady brew of prefix, exec_prefix, home, install_base, # install_platbase, user-supplied versions of # install_{purelib,platlib,lib,scripts,data,...}, and the # INSTALL_SCHEME dictionary above. Phew! self.dump_dirs("pre-finalize_{unix,other}") if os.name == 'posix': self.finalize_unix() else: self.finalize_other() self.dump_dirs("post-finalize_{unix,other}()") # Expand configuration variables, tilde, etc. in self.install_base # and self.install_platbase -- that way, we can use $base or # $platbase in the other installation directories and not worry # about needing recursive variable expansion (shudder). py_version = (string.split(sys.version))[0] (prefix, exec_prefix) = get_config_vars('prefix', 'exec_prefix') self.config_vars = {'dist_name': self.distribution.get_name(), 'dist_version': self.distribution.get_version(), 'dist_fullname': self.distribution.get_fullname(), 'py_version': py_version, 'py_version_short': py_version[0:3], 'py_version_nodot': py_version[0] + py_version[2], 'sys_prefix': prefix, 'prefix': prefix, 'sys_exec_prefix': exec_prefix, 'exec_prefix': exec_prefix, 'userbase': self.install_userbase, 'usersite': self.install_usersite, } self.expand_basedirs() self.dump_dirs("post-expand_basedirs()") # Now define config vars for the base directories so we can expand # everything else. self.config_vars['base'] = self.install_base self.config_vars['platbase'] = self.install_platbase if DEBUG: from pprint import pprint print "config vars:" pprint(self.config_vars) # Expand "~" and configuration variables in the installation # directories. self.expand_dirs() self.dump_dirs("post-expand_dirs()") # Create directories in the home dir: if self.user: self.create_home_path() # Pick the actual directory to install all modules to: either # install_purelib or install_platlib, depending on whether this # module distribution is pure or not. Of course, if the user # already specified install_lib, use their selection. if self.install_lib is None: if self.distribution.ext_modules: # has extensions: non-pure self.install_lib = self.install_platlib else: self.install_lib = self.install_purelib # Convert directories from Unix /-separated syntax to the local # convention. self.convert_paths('lib', 'purelib', 'platlib', 'scripts', 'data', 'headers', 'userbase', 'usersite') # Well, we're not actually fully completely finalized yet: we still # have to deal with 'extra_path', which is the hack for allowing # non-packagized module distributions (hello, Numerical Python!) to # get their own directories. self.handle_extra_path() self.install_libbase = self.install_lib # needed for .pth file self.install_lib = os.path.join(self.install_lib, self.extra_dirs) # If a new root directory was supplied, make all the installation # dirs relative to it. if self.root is not None: self.change_roots('libbase', 'lib', 'purelib', 'platlib', 'scripts', 'data', 'headers') self.dump_dirs("after prepending root") # Find out the build directories, ie. where to install from. self.set_undefined_options('build', ('build_base', 'build_base'), ('build_lib', 'build_lib')) # Punt on doc directories for now -- after all, we're punting on # documentation completely! # finalize_options () def dump_dirs (self, msg): if DEBUG: from distutils.fancy_getopt import longopt_xlate print msg + ":" for opt in self.user_options: opt_name = opt[0] if opt_name[-1] == "=": opt_name = opt_name[0:-1] if opt_name in self.negative_opt: opt_name = string.translate(self.negative_opt[opt_name], longopt_xlate) val = not getattr(self, opt_name) else: opt_name = string.translate(opt_name, longopt_xlate) val = getattr(self, opt_name) print " %s: %s" % (opt_name, val) def finalize_unix (self): if self.install_base is not None or self.install_platbase is not None: if ((self.install_lib is None and self.install_purelib is None and self.install_platlib is None) or self.install_headers is None or self.install_scripts is None or self.install_data is None): raise DistutilsOptionError, \ ("install-base or install-platbase supplied, but " "installation scheme is incomplete") return if self.user: if self.install_userbase is None: raise DistutilsPlatformError( "User base directory is not specified") self.install_base = self.install_platbase = self.install_userbase self.select_scheme("unix_user") elif self.home is not None: self.install_base = self.install_platbase = self.home self.select_scheme("unix_home") else: if self.prefix is None: if self.exec_prefix is not None: raise DistutilsOptionError, \ "must not supply exec-prefix without prefix" self.prefix = os.path.normpath(sys.prefix) self.exec_prefix = os.path.normpath(sys.exec_prefix) else: if self.exec_prefix is None: self.exec_prefix = self.prefix self.install_base = self.prefix self.install_platbase = self.exec_prefix self.select_scheme("unix_prefix") # finalize_unix () def finalize_other (self): # Windows and Mac OS for now if self.user: if self.install_userbase is None: raise DistutilsPlatformError( "User base directory is not specified") self.install_base = self.install_platbase = self.install_userbase self.select_scheme(os.name + "_user") elif self.home is not None: self.install_base = self.install_platbase = self.home self.select_scheme("unix_home") else: if self.prefix is None: self.prefix = os.path.normpath(sys.prefix) self.install_base = self.install_platbase = self.prefix try: self.select_scheme(os.name) except KeyError: raise DistutilsPlatformError, \ "I don't know how to install stuff on '%s'" % os.name # finalize_other () def select_scheme (self, name): # it's the caller's problem if they supply a bad name! scheme = INSTALL_SCHEMES[name] for key in SCHEME_KEYS: attrname = 'install_' + key if getattr(self, attrname) is None: setattr(self, attrname, scheme[key]) def _expand_attrs (self, attrs): for attr in attrs: val = getattr(self, attr) if val is not None: if os.name == 'posix' or os.name == 'nt': val = os.path.expanduser(val) val = subst_vars(val, self.config_vars) setattr(self, attr, val) def expand_basedirs (self): self._expand_attrs(['install_base', 'install_platbase', 'root']) def expand_dirs (self): self._expand_attrs(['install_purelib', 'install_platlib', 'install_lib', 'install_headers', 'install_scripts', 'install_data',]) def convert_paths (self, *names): for name in names: attr = "install_" + name setattr(self, attr, convert_path(getattr(self, attr))) def handle_extra_path (self): if self.extra_path is None: self.extra_path = self.distribution.extra_path if self.extra_path is not None: if type(self.extra_path) is StringType: self.extra_path = string.split(self.extra_path, ',') if len(self.extra_path) == 1: path_file = extra_dirs = self.extra_path[0] elif len(self.extra_path) == 2: (path_file, extra_dirs) = self.extra_path else: raise DistutilsOptionError, \ ("'extra_path' option must be a list, tuple, or " "comma-separated string with 1 or 2 elements") # convert to local form in case Unix notation used (as it # should be in setup scripts) extra_dirs = convert_path(extra_dirs) else: path_file = None extra_dirs = '' # XXX should we warn if path_file and not extra_dirs? (in which # case the path file would be harmless but pointless) self.path_file = path_file self.extra_dirs = extra_dirs # handle_extra_path () def change_roots (self, *names): for name in names: attr = "install_" + name setattr(self, attr, change_root(self.root, getattr(self, attr))) def create_home_path(self): """Create directories under ~ """ if not self.user: return home = convert_path(os.path.expanduser("~")) for name, path in self.config_vars.iteritems(): if path.startswith(home) and not os.path.isdir(path): self.debug_print("os.makedirs('%s', 0700)" % path) os.makedirs(path, 0700) # -- Command execution methods ------------------------------------- def run (self): # Obviously have to build before we can install if not self.skip_build: self.run_command('build') # If we built for any other platform, we can't install. build_plat = self.distribution.get_command_obj('build').plat_name # check warn_dir - it is a clue that the 'install' is happening # internally, and not to sys.path, so we don't check the platform # matches what we are running. if self.warn_dir and build_plat != get_platform(): raise DistutilsPlatformError("Can't install when " "cross-compiling") # Run all sub-commands (at least those that need to be run) for cmd_name in self.get_sub_commands(): self.run_command(cmd_name) if self.path_file: self.create_path_file() # write list of installed files, if requested. if self.record: outputs = self.get_outputs() if self.root: # strip any package prefix root_len = len(self.root) for counter in xrange(len(outputs)): outputs[counter] = outputs[counter][root_len:] self.execute(write_file, (self.record, outputs), "writing list of installed files to '%s'" % self.record) sys_path = map(os.path.normpath, sys.path) sys_path = map(os.path.normcase, sys_path) install_lib = os.path.normcase(os.path.normpath(self.install_lib)) if (self.warn_dir and not (self.path_file and self.install_path_file) and install_lib not in sys_path): log.debug(("modules installed to '%s', which is not in " "Python's module search path (sys.path) -- " "you'll have to change the search path yourself"), self.install_lib) # run () def create_path_file (self): filename = os.path.join(self.install_libbase, self.path_file + ".pth") if self.install_path_file: self.execute(write_file, (filename, [self.extra_dirs]), "creating %s" % filename) else: self.warn("path file '%s' not created" % filename) # -- Reporting methods --------------------------------------------- def get_outputs (self): # Assemble the outputs of all the sub-commands. outputs = [] for cmd_name in self.get_sub_commands(): cmd = self.get_finalized_command(cmd_name) # Add the contents of cmd.get_outputs(), ensuring # that outputs doesn't contain duplicate entries for filename in cmd.get_outputs(): if filename not in outputs: outputs.append(filename) if self.path_file and self.install_path_file: outputs.append(os.path.join(self.install_libbase, self.path_file + ".pth")) return outputs def get_inputs (self): # XXX gee, this looks familiar ;-( inputs = [] for cmd_name in self.get_sub_commands(): cmd = self.get_finalized_command(cmd_name) inputs.extend(cmd.get_inputs()) return inputs # -- Predicates for sub-command list ------------------------------- def has_lib (self): """Return true if the current distribution has any Python modules to install.""" return (self.distribution.has_pure_modules() or self.distribution.has_ext_modules()) def has_headers (self): return self.distribution.has_headers() def has_scripts (self): return self.distribution.has_scripts() def has_data (self): return self.distribution.has_data_files() # 'sub_commands': a list of commands this command might have to run to # get its work done. See cmd.py for more info. sub_commands = [('install_lib', has_lib), ('install_headers', has_headers), ('install_scripts', has_scripts), ('install_data', has_data), ('install_egg_info', lambda self:True), ] # class install
bsd-2-clause
rahul-c1/scikit-learn
sklearn/base.py
11
16709
"""Base classes for all estimators.""" # Author: Gael Varoquaux <gael.varoquaux@normalesup.org> # License: BSD 3 clause import copy import inspect import warnings import numpy as np from scipy import sparse from .externals import six ############################################################################### def clone(estimator, safe=True): """Constructs a new estimator with the same parameters. Clone does a deep copy of the model in an estimator without actually copying attached data. It yields a new estimator with the same parameters that has not been fit on any data. Parameters ---------- estimator: estimator object, or list, tuple or set of objects The estimator or group of estimators to be cloned safe: boolean, optional If safe is false, clone will fall back to a deepcopy on objects that are not estimators. """ estimator_type = type(estimator) # XXX: not handling dictionaries if estimator_type in (list, tuple, set, frozenset): return estimator_type([clone(e, safe=safe) for e in estimator]) elif not hasattr(estimator, 'get_params'): if not safe: return copy.deepcopy(estimator) else: raise TypeError("Cannot clone object '%s' (type %s): " "it does not seem to be a scikit-learn estimator " "it does not implement a 'get_params' methods." % (repr(estimator), type(estimator))) klass = estimator.__class__ new_object_params = estimator.get_params(deep=False) for name, param in six.iteritems(new_object_params): new_object_params[name] = clone(param, safe=False) new_object = klass(**new_object_params) params_set = new_object.get_params(deep=False) # quick sanity check of the parameters of the clone for name in new_object_params: param1 = new_object_params[name] param2 = params_set[name] if isinstance(param1, np.ndarray): # For most ndarrays, we do not test for complete equality if not isinstance(param2, type(param1)): equality_test = False elif (param1.ndim > 0 and param1.shape[0] > 0 and isinstance(param2, np.ndarray) and param2.ndim > 0 and param2.shape[0] > 0): equality_test = ( param1.shape == param2.shape and param1.dtype == param2.dtype # We have to use '.flat' for 2D arrays and param1.flat[0] == param2.flat[0] and param1.flat[-1] == param2.flat[-1] ) else: equality_test = np.all(param1 == param2) elif sparse.issparse(param1): # For sparse matrices equality doesn't work if not sparse.issparse(param2): equality_test = False elif param1.size == 0 or param2.size == 0: equality_test = ( param1.__class__ == param2.__class__ and param1.size == 0 and param2.size == 0 ) else: equality_test = ( param1.__class__ == param2.__class__ and param1.data[0] == param2.data[0] and param1.data[-1] == param2.data[-1] and param1.nnz == param2.nnz and param1.shape == param2.shape ) else: equality_test = new_object_params[name] == params_set[name] if not equality_test: raise RuntimeError('Cannot clone object %s, as the constructor ' 'does not seem to set parameter %s' % (estimator, name)) return new_object ############################################################################### def _pprint(params, offset=0, printer=repr): """Pretty print the dictionary 'params' Parameters ---------- params: dict The dictionary to pretty print offset: int The offset in characters to add at the begin of each line. printer: The function to convert entries to strings, typically the builtin str or repr """ # Do a multi-line justified repr: options = np.get_printoptions() np.set_printoptions(precision=5, threshold=64, edgeitems=2) params_list = list() this_line_length = offset line_sep = ',\n' + (1 + offset // 2) * ' ' for i, (k, v) in enumerate(sorted(six.iteritems(params))): if type(v) is float: # use str for representing floating point numbers # this way we get consistent representation across # architectures and versions. this_repr = '%s=%s' % (k, str(v)) else: # use repr of the rest this_repr = '%s=%s' % (k, printer(v)) if len(this_repr) > 500: this_repr = this_repr[:300] + '...' + this_repr[-100:] if i > 0: if (this_line_length + len(this_repr) >= 75 or '\n' in this_repr): params_list.append(line_sep) this_line_length = len(line_sep) else: params_list.append(', ') this_line_length += 2 params_list.append(this_repr) this_line_length += len(this_repr) np.set_printoptions(**options) lines = ''.join(params_list) # Strip trailing space to avoid nightmare in doctests lines = '\n'.join(l.rstrip(' ') for l in lines.split('\n')) return lines ############################################################################### class BaseEstimator(object): """Base class for all estimators in scikit-learn Notes ----- All estimators should specify all the parameters that can be set at the class level in their ``__init__`` as explicit keyword arguments (no ``*args`` or ``**kwargs``). """ @classmethod def _get_param_names(cls): """Get parameter names for the estimator""" # fetch the constructor or the original constructor before # deprecation wrapping if any init = getattr(cls.__init__, 'deprecated_original', cls.__init__) if init is object.__init__: # No explicit constructor to introspect return [] # introspect the constructor arguments to find the model parameters # to represent args, varargs, kw, default = inspect.getargspec(init) if varargs is not None: raise RuntimeError("scikit-learn estimators should always " "specify their parameters in the signature" " of their __init__ (no varargs)." " %s doesn't follow this convention." % (cls, )) # Remove 'self' # XXX: This is going to fail if the init is a staticmethod, but # who would do this? args.pop(0) args.sort() return args def get_params(self, deep=True): """Get parameters for this estimator. Parameters ---------- deep: boolean, optional If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns ------- params : mapping of string to any Parameter names mapped to their values. """ out = dict() for key in self._get_param_names(): # We need deprecation warnings to always be on in order to # catch deprecated param values. # This is set in utils/__init__.py but it gets overwritten # when running under python3 somehow. warnings.simplefilter("always", DeprecationWarning) try: with warnings.catch_warnings(record=True) as w: value = getattr(self, key, None) if len(w) and w[0].category == DeprecationWarning: # if the parameter is deprecated, don't show it continue finally: warnings.filters.pop(0) # XXX: should we rather test if instance of estimator? if deep and hasattr(value, 'get_params'): deep_items = value.get_params().items() out.update((key + '__' + k, val) for k, val in deep_items) out[key] = value return out def set_params(self, **params): """Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as pipelines). The former have parameters of the form ``<component>__<parameter>`` so that it's possible to update each component of a nested object. Returns ------- self """ if not params: # Simple optimisation to gain speed (inspect is slow) return self valid_params = self.get_params(deep=True) for key, value in six.iteritems(params): split = key.split('__', 1) if len(split) > 1: # nested objects case name, sub_name = split if not name in valid_params: raise ValueError('Invalid parameter %s for estimator %s' % (name, self)) sub_object = valid_params[name] sub_object.set_params(**{sub_name: value}) else: # simple objects case if not key in valid_params: raise ValueError('Invalid parameter %s ' 'for estimator %s' % (key, self.__class__.__name__)) setattr(self, key, value) return self def __repr__(self): class_name = self.__class__.__name__ return '%s(%s)' % (class_name, _pprint(self.get_params(deep=False), offset=len(class_name),),) ############################################################################### class ClassifierMixin(object): """Mixin class for all classifiers in scikit-learn.""" def score(self, X, y, sample_weight=None): """Returns the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted. Parameters ---------- X : array-like, shape = (n_samples, n_features) Test samples. y : array-like, shape = (n_samples) or (n_samples, n_outputs) True labels for X. sample_weight : array-like, shape = [n_samples], optional Sample weights. Returns ------- score : float Mean accuracy of self.predict(X) wrt. y. """ from .metrics import accuracy_score return accuracy_score(y, self.predict(X), sample_weight=sample_weight) ############################################################################### class RegressorMixin(object): """Mixin class for all regression estimators in scikit-learn.""" def score(self, X, y, sample_weight=None): """Returns the coefficient of determination R^2 of the prediction. The coefficient R^2 is defined as (1 - u/v), where u is the regression sum of squares ((y_true - y_pred) ** 2).sum() and v is the residual sum of squares ((y_true - y_true.mean()) ** 2).sum(). Best possible score is 1.0, lower values are worse. Parameters ---------- X : array-like, shape = (n_samples, n_features) Test samples. y : array-like, shape = (n_samples) or (n_samples, n_outputs) True values for X. sample_weight : array-like, shape = [n_samples], optional Sample weights. Returns ------- score : float R^2 of self.predict(X) wrt. y. """ from .metrics import r2_score return r2_score(y, self.predict(X), sample_weight=sample_weight) ############################################################################### class ClusterMixin(object): """Mixin class for all cluster estimators in scikit-learn.""" def fit_predict(self, X, y=None): """Performs clustering on X and returns cluster labels. Parameters ---------- X : ndarray, shape (n_samples, n_features) Input data. Returns ------- y : ndarray, shape (n_samples,) cluster labels """ # non-optimized default implementation; override when a better # method is possible for a given clustering algorithm self.fit(X) return self.labels_ class BiclusterMixin(object): """Mixin class for all bicluster estimators in scikit-learn""" @property def biclusters_(self): """Convenient way to get row and column indicators together. Returns the ``rows_`` and ``columns_`` members. """ return self.rows_, self.columns_ def get_indices(self, i): """Row and column indices of the i'th bicluster. Only works if ``rows_`` and ``columns_`` attributes exist. Returns ------- row_ind : np.array, dtype=np.intp Indices of rows in the dataset that belong to the bicluster. col_ind : np.array, dtype=np.intp Indices of columns in the dataset that belong to the bicluster. """ rows = self.rows_[i] columns = self.columns_[i] return np.nonzero(rows)[0], np.nonzero(columns)[0] def get_shape(self, i): """Shape of the i'th bicluster. Returns ------- shape : (int, int) Number of rows and columns (resp.) in the bicluster. """ indices = self.get_indices(i) return tuple(len(i) for i in indices) def get_submatrix(self, i, data): """Returns the submatrix corresponding to bicluster `i`. Works with sparse matrices. Only works if ``rows_`` and ``columns_`` attributes exist. """ from .utils.validation import check_array data = check_array(data, accept_sparse='csr') row_ind, col_ind = self.get_indices(i) return data[row_ind[:, np.newaxis], col_ind] ############################################################################### class TransformerMixin(object): """Mixin class for all transformers in scikit-learn.""" def fit_transform(self, X, y=None, **fit_params): """Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X. Parameters ---------- X : numpy array of shape [n_samples, n_features] Training set. y : numpy array of shape [n_samples] Target values. Returns ------- X_new : numpy array of shape [n_samples, n_features_new] Transformed array. """ # non-optimized default implementation; override when a better # method is possible for a given clustering algorithm if y is None: # fit method of arity 1 (unsupervised transformation) return self.fit(X, **fit_params).transform(X) else: # fit method of arity 2 (supervised transformation) return self.fit(X, y, **fit_params).transform(X) ############################################################################### class MetaEstimatorMixin(object): """Mixin class for all meta estimators in scikit-learn.""" # this is just a tag for the moment ############################################################################### # XXX: Temporary solution to figure out if an estimator is a classifier def _get_sub_estimator(estimator): """Returns the final estimator if there is any.""" if hasattr(estimator, 'estimator'): # GridSearchCV and other CV-tuned estimators return _get_sub_estimator(estimator.estimator) if hasattr(estimator, 'steps'): # Pipeline return _get_sub_estimator(estimator.steps[-1][1]) return estimator def is_classifier(estimator): """Returns True if the given estimator is (probably) a classifier.""" estimator = _get_sub_estimator(estimator) return isinstance(estimator, ClassifierMixin)
bsd-3-clause
mozilla/stoneridge
python/src/Demo/scripts/eqfix.py
43
6311
#! /usr/bin/env python # Fix Python source files to use the new equality test operator, i.e., # if x = y: ... # is changed to # if x == y: ... # The script correctly tokenizes the Python program to reliably # distinguish between assignments and equality tests. # # Command line arguments are files or directories to be processed. # Directories are searched recursively for files whose name looks # like a python module. # Symbolic links are always ignored (except as explicit directory # arguments). Of course, the original file is kept as a back-up # (with a "~" attached to its name). # It complains about binaries (files containing null bytes) # and about files that are ostensibly not Python files: if the first # line starts with '#!' and does not contain the string 'python'. # # Changes made are reported to stdout in a diff-like format. # # Undoubtedly you can do this using find and sed or perl, but this is # a nice example of Python code that recurses down a directory tree # and uses regular expressions. Also note several subtleties like # preserving the file's mode and avoiding to even write a temp file # when no changes are needed for a file. # # NB: by changing only the function fixline() you can turn this # into a program for a different change to Python programs... import sys import re import os from stat import * import string err = sys.stderr.write dbg = err rep = sys.stdout.write def main(): bad = 0 if not sys.argv[1:]: # No arguments err('usage: ' + sys.argv[0] + ' file-or-directory ...\n') sys.exit(2) for arg in sys.argv[1:]: if os.path.isdir(arg): if recursedown(arg): bad = 1 elif os.path.islink(arg): err(arg + ': will not process symbolic links\n') bad = 1 else: if fix(arg): bad = 1 sys.exit(bad) ispythonprog = re.compile('^[a-zA-Z0-9_]+\.py$') def ispython(name): return ispythonprog.match(name) >= 0 def recursedown(dirname): dbg('recursedown(%r)\n' % (dirname,)) bad = 0 try: names = os.listdir(dirname) except os.error, msg: err('%s: cannot list directory: %r\n' % (dirname, msg)) return 1 names.sort() subdirs = [] for name in names: if name in (os.curdir, os.pardir): continue fullname = os.path.join(dirname, name) if os.path.islink(fullname): pass elif os.path.isdir(fullname): subdirs.append(fullname) elif ispython(name): if fix(fullname): bad = 1 for fullname in subdirs: if recursedown(fullname): bad = 1 return bad def fix(filename): ## dbg('fix(%r)\n' % (dirname,)) try: f = open(filename, 'r') except IOError, msg: err('%s: cannot open: %r\n' % (filename, msg)) return 1 head, tail = os.path.split(filename) tempname = os.path.join(head, '@' + tail) g = None # If we find a match, we rewind the file and start over but # now copy everything to a temp file. lineno = 0 while 1: line = f.readline() if not line: break lineno = lineno + 1 if g is None and '\0' in line: # Check for binary files err(filename + ': contains null bytes; not fixed\n') f.close() return 1 if lineno == 1 and g is None and line[:2] == '#!': # Check for non-Python scripts words = string.split(line[2:]) if words and re.search('[pP]ython', words[0]) < 0: msg = filename + ': ' + words[0] msg = msg + ' script; not fixed\n' err(msg) f.close() return 1 while line[-2:] == '\\\n': nextline = f.readline() if not nextline: break line = line + nextline lineno = lineno + 1 newline = fixline(line) if newline != line: if g is None: try: g = open(tempname, 'w') except IOError, msg: f.close() err('%s: cannot create: %r\n' % (tempname, msg)) return 1 f.seek(0) lineno = 0 rep(filename + ':\n') continue # restart from the beginning rep(repr(lineno) + '\n') rep('< ' + line) rep('> ' + newline) if g is not None: g.write(newline) # End of file f.close() if not g: return 0 # No changes # Finishing touch -- move files # First copy the file's mode to the temp file try: statbuf = os.stat(filename) os.chmod(tempname, statbuf[ST_MODE] & 07777) except os.error, msg: err('%s: warning: chmod failed (%r)\n' % (tempname, msg)) # Then make a backup of the original file as filename~ try: os.rename(filename, filename + '~') except os.error, msg: err('%s: warning: backup failed (%r)\n' % (filename, msg)) # Now move the temp file to the original file try: os.rename(tempname, filename) except os.error, msg: err('%s: rename failed (%r)\n' % (filename, msg)) return 1 # Return succes return 0 from tokenize import tokenprog match = {'if':':', 'elif':':', 'while':':', 'return':'\n', \ '(':')', '[':']', '{':'}', '`':'`'} def fixline(line): # Quick check for easy case if '=' not in line: return line i, n = 0, len(line) stack = [] while i < n: j = tokenprog.match(line, i) if j < 0: # A bad token; forget about the rest of this line print '(Syntax error:)' print line, return line a, b = tokenprog.regs[3] # Location of the token proper token = line[a:b] i = i+j if stack and token == stack[-1]: del stack[-1] elif match.has_key(token): stack.append(match[token]) elif token == '=' and stack: line = line[:a] + '==' + line[b:] i, n = a + len('=='), len(line) elif token == '==' and not stack: print '(Warning: \'==\' at top level:)' print line, return line if __name__ == "__main__": main()
mpl-2.0
pap/nupic
external/linux32/lib/python2.6/site-packages/matplotlib/backends/backend_gtk.py
69
43991
from __future__ import division import os, sys def fn_name(): return sys._getframe(1).f_code.co_name try: import gobject import gtk; gdk = gtk.gdk import pango except ImportError: raise ImportError("Gtk* backend requires pygtk to be installed.") pygtk_version_required = (2,2,0) if gtk.pygtk_version < pygtk_version_required: raise ImportError ("PyGTK %d.%d.%d is installed\n" "PyGTK %d.%d.%d or later is required" % (gtk.pygtk_version + pygtk_version_required)) del pygtk_version_required import matplotlib from matplotlib import verbose from matplotlib._pylab_helpers import Gcf from matplotlib.backend_bases import RendererBase, GraphicsContextBase, \ FigureManagerBase, FigureCanvasBase, NavigationToolbar2, cursors from matplotlib.backends.backend_gdk import RendererGDK, FigureCanvasGDK from matplotlib.cbook import is_string_like, is_writable_file_like from matplotlib.colors import colorConverter from matplotlib.figure import Figure from matplotlib.widgets import SubplotTool from matplotlib import lines from matplotlib import cbook backend_version = "%d.%d.%d" % gtk.pygtk_version _debug = False #_debug = True # the true dots per inch on the screen; should be display dependent # see http://groups.google.com/groups?q=screen+dpi+x11&hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=7077.26e81ad5%40swift.cs.tcd.ie&rnum=5 for some info about screen dpi PIXELS_PER_INCH = 96 cursord = { cursors.MOVE : gdk.Cursor(gdk.FLEUR), cursors.HAND : gdk.Cursor(gdk.HAND2), cursors.POINTER : gdk.Cursor(gdk.LEFT_PTR), cursors.SELECT_REGION : gdk.Cursor(gdk.TCROSS), } # ref gtk+/gtk/gtkwidget.h def GTK_WIDGET_DRAWABLE(w): flags = w.flags(); return flags & gtk.VISIBLE != 0 and flags & gtk.MAPPED != 0 def draw_if_interactive(): """ Is called after every pylab drawing command """ if matplotlib.is_interactive(): figManager = Gcf.get_active() if figManager is not None: figManager.canvas.draw() def show(mainloop=True): """ Show all the figures and enter the gtk main loop This should be the last line of your script """ for manager in Gcf.get_all_fig_managers(): manager.window.show() if mainloop and gtk.main_level() == 0 and \ len(Gcf.get_all_fig_managers())>0: gtk.main() def new_figure_manager(num, *args, **kwargs): """ Create a new figure manager instance """ FigureClass = kwargs.pop('FigureClass', Figure) thisFig = FigureClass(*args, **kwargs) canvas = FigureCanvasGTK(thisFig) manager = FigureManagerGTK(canvas, num) # equals: #manager = FigureManagerGTK(FigureCanvasGTK(Figure(*args, **kwargs), num) return manager class FigureCanvasGTK (gtk.DrawingArea, FigureCanvasBase): keyvald = {65507 : 'control', 65505 : 'shift', 65513 : 'alt', 65508 : 'control', 65506 : 'shift', 65514 : 'alt', 65361 : 'left', 65362 : 'up', 65363 : 'right', 65364 : 'down', 65307 : 'escape', 65470 : 'f1', 65471 : 'f2', 65472 : 'f3', 65473 : 'f4', 65474 : 'f5', 65475 : 'f6', 65476 : 'f7', 65477 : 'f8', 65478 : 'f9', 65479 : 'f10', 65480 : 'f11', 65481 : 'f12', 65300 : 'scroll_lock', 65299 : 'break', 65288 : 'backspace', 65293 : 'enter', 65379 : 'insert', 65535 : 'delete', 65360 : 'home', 65367 : 'end', 65365 : 'pageup', 65366 : 'pagedown', 65438 : '0', 65436 : '1', 65433 : '2', 65435 : '3', 65430 : '4', 65437 : '5', 65432 : '6', 65429 : '7', 65431 : '8', 65434 : '9', 65451 : '+', 65453 : '-', 65450 : '*', 65455 : '/', 65439 : 'dec', 65421 : 'enter', } # Setting this as a static constant prevents # this resulting expression from leaking event_mask = (gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK | gdk.EXPOSURE_MASK | gdk.KEY_PRESS_MASK | gdk.KEY_RELEASE_MASK | gdk.ENTER_NOTIFY_MASK | gdk.LEAVE_NOTIFY_MASK | gdk.POINTER_MOTION_MASK | gdk.POINTER_MOTION_HINT_MASK) def __init__(self, figure): if _debug: print 'FigureCanvasGTK.%s' % fn_name() FigureCanvasBase.__init__(self, figure) gtk.DrawingArea.__init__(self) self._idle_draw_id = 0 self._need_redraw = True self._pixmap_width = -1 self._pixmap_height = -1 self._lastCursor = None self.connect('scroll_event', self.scroll_event) self.connect('button_press_event', self.button_press_event) self.connect('button_release_event', self.button_release_event) self.connect('configure_event', self.configure_event) self.connect('expose_event', self.expose_event) self.connect('key_press_event', self.key_press_event) self.connect('key_release_event', self.key_release_event) self.connect('motion_notify_event', self.motion_notify_event) self.connect('leave_notify_event', self.leave_notify_event) self.connect('enter_notify_event', self.enter_notify_event) self.set_events(self.__class__.event_mask) self.set_double_buffered(False) self.set_flags(gtk.CAN_FOCUS) self._renderer_init() self._idle_event_id = gobject.idle_add(self.idle_event) def destroy(self): #gtk.DrawingArea.destroy(self) gobject.source_remove(self._idle_event_id) if self._idle_draw_id != 0: gobject.source_remove(self._idle_draw_id) def scroll_event(self, widget, event): if _debug: print 'FigureCanvasGTK.%s' % fn_name() x = event.x # flipy so y=0 is bottom of canvas y = self.allocation.height - event.y if event.direction==gdk.SCROLL_UP: step = 1 else: step = -1 FigureCanvasBase.scroll_event(self, x, y, step) return False # finish event propagation? def button_press_event(self, widget, event): if _debug: print 'FigureCanvasGTK.%s' % fn_name() x = event.x # flipy so y=0 is bottom of canvas y = self.allocation.height - event.y FigureCanvasBase.button_press_event(self, x, y, event.button) return False # finish event propagation? def button_release_event(self, widget, event): if _debug: print 'FigureCanvasGTK.%s' % fn_name() x = event.x # flipy so y=0 is bottom of canvas y = self.allocation.height - event.y FigureCanvasBase.button_release_event(self, x, y, event.button) return False # finish event propagation? def key_press_event(self, widget, event): if _debug: print 'FigureCanvasGTK.%s' % fn_name() key = self._get_key(event) if _debug: print "hit", key FigureCanvasBase.key_press_event(self, key) return False # finish event propagation? def key_release_event(self, widget, event): if _debug: print 'FigureCanvasGTK.%s' % fn_name() key = self._get_key(event) if _debug: print "release", key FigureCanvasBase.key_release_event(self, key) return False # finish event propagation? def motion_notify_event(self, widget, event): if _debug: print 'FigureCanvasGTK.%s' % fn_name() if event.is_hint: x, y, state = event.window.get_pointer() else: x, y, state = event.x, event.y, event.state # flipy so y=0 is bottom of canvas y = self.allocation.height - y FigureCanvasBase.motion_notify_event(self, x, y) return False # finish event propagation? def leave_notify_event(self, widget, event): FigureCanvasBase.leave_notify_event(self, event) def enter_notify_event(self, widget, event): FigureCanvasBase.enter_notify_event(self, event) def _get_key(self, event): if event.keyval in self.keyvald: key = self.keyvald[event.keyval] elif event.keyval <256: key = chr(event.keyval) else: key = None ctrl = event.state & gdk.CONTROL_MASK shift = event.state & gdk.SHIFT_MASK return key def configure_event(self, widget, event): if _debug: print 'FigureCanvasGTK.%s' % fn_name() if widget.window is None: return w, h = event.width, event.height if w < 3 or h < 3: return # empty fig # resize the figure (in inches) dpi = self.figure.dpi self.figure.set_size_inches (w/dpi, h/dpi) self._need_redraw = True return False # finish event propagation? def draw(self): # Note: FigureCanvasBase.draw() is inconveniently named as it clashes # with the deprecated gtk.Widget.draw() self._need_redraw = True if GTK_WIDGET_DRAWABLE(self): self.queue_draw() # do a synchronous draw (its less efficient than an async draw, # but is required if/when animation is used) self.window.process_updates (False) def draw_idle(self): def idle_draw(*args): self.draw() self._idle_draw_id = 0 return False if self._idle_draw_id == 0: self._idle_draw_id = gobject.idle_add(idle_draw) def _renderer_init(self): """Override by GTK backends to select a different renderer Renderer should provide the methods: set_pixmap () set_width_height () that are used by _render_figure() / _pixmap_prepare() """ self._renderer = RendererGDK (self, self.figure.dpi) def _pixmap_prepare(self, width, height): """ Make sure _._pixmap is at least width, height, create new pixmap if necessary """ if _debug: print 'FigureCanvasGTK.%s' % fn_name() create_pixmap = False if width > self._pixmap_width: # increase the pixmap in 10%+ (rather than 1 pixel) steps self._pixmap_width = max (int (self._pixmap_width * 1.1), width) create_pixmap = True if height > self._pixmap_height: self._pixmap_height = max (int (self._pixmap_height * 1.1), height) create_pixmap = True if create_pixmap: self._pixmap = gdk.Pixmap (self.window, self._pixmap_width, self._pixmap_height) self._renderer.set_pixmap (self._pixmap) def _render_figure(self, pixmap, width, height): """used by GTK and GTKcairo. GTKAgg overrides """ self._renderer.set_width_height (width, height) self.figure.draw (self._renderer) def expose_event(self, widget, event): """Expose_event for all GTK backends. Should not be overridden. """ if _debug: print 'FigureCanvasGTK.%s' % fn_name() if GTK_WIDGET_DRAWABLE(self): if self._need_redraw: x, y, w, h = self.allocation self._pixmap_prepare (w, h) self._render_figure(self._pixmap, w, h) self._need_redraw = False x, y, w, h = event.area self.window.draw_drawable (self.style.fg_gc[self.state], self._pixmap, x, y, x, y, w, h) return False # finish event propagation? filetypes = FigureCanvasBase.filetypes.copy() filetypes['jpg'] = 'JPEG' filetypes['jpeg'] = 'JPEG' filetypes['png'] = 'Portable Network Graphics' def print_jpeg(self, filename, *args, **kwargs): return self._print_image(filename, 'jpeg') print_jpg = print_jpeg def print_png(self, filename, *args, **kwargs): return self._print_image(filename, 'png') def _print_image(self, filename, format): if self.flags() & gtk.REALIZED == 0: # for self.window(for pixmap) and has a side effect of altering # figure width,height (via configure-event?) gtk.DrawingArea.realize(self) width, height = self.get_width_height() pixmap = gdk.Pixmap (self.window, width, height) self._renderer.set_pixmap (pixmap) self._render_figure(pixmap, width, height) # jpg colors don't match the display very well, png colors match # better pixbuf = gdk.Pixbuf(gdk.COLORSPACE_RGB, 0, 8, width, height) pixbuf.get_from_drawable(pixmap, pixmap.get_colormap(), 0, 0, 0, 0, width, height) if is_string_like(filename): try: pixbuf.save(filename, format) except gobject.GError, exc: error_msg_gtk('Save figure failure:\n%s' % (exc,), parent=self) elif is_writable_file_like(filename): if hasattr(pixbuf, 'save_to_callback'): def save_callback(buf, data=None): data.write(buf) try: pixbuf.save_to_callback(save_callback, format, user_data=filename) except gobject.GError, exc: error_msg_gtk('Save figure failure:\n%s' % (exc,), parent=self) else: raise ValueError("Saving to a Python file-like object is only supported by PyGTK >= 2.8") else: raise ValueError("filename must be a path or a file-like object") def get_default_filetype(self): return 'png' def flush_events(self): gtk.gdk.threads_enter() while gtk.events_pending(): gtk.main_iteration(True) gtk.gdk.flush() gtk.gdk.threads_leave() def start_event_loop(self,timeout): FigureCanvasBase.start_event_loop_default(self,timeout) start_event_loop.__doc__=FigureCanvasBase.start_event_loop_default.__doc__ def stop_event_loop(self): FigureCanvasBase.stop_event_loop_default(self) stop_event_loop.__doc__=FigureCanvasBase.stop_event_loop_default.__doc__ class FigureManagerGTK(FigureManagerBase): """ Public attributes canvas : The FigureCanvas instance num : The Figure number toolbar : The gtk.Toolbar (gtk only) vbox : The gtk.VBox containing the canvas and toolbar (gtk only) window : The gtk.Window (gtk only) """ def __init__(self, canvas, num): if _debug: print 'FigureManagerGTK.%s' % fn_name() FigureManagerBase.__init__(self, canvas, num) self.window = gtk.Window() self.window.set_title("Figure %d" % num) self.vbox = gtk.VBox() self.window.add(self.vbox) self.vbox.show() self.canvas.show() # attach a show method to the figure for pylab ease of use self.canvas.figure.show = lambda *args: self.window.show() self.vbox.pack_start(self.canvas, True, True) self.toolbar = self._get_toolbar(canvas) # calculate size for window w = int (self.canvas.figure.bbox.width) h = int (self.canvas.figure.bbox.height) if self.toolbar is not None: self.toolbar.show() self.vbox.pack_end(self.toolbar, False, False) tb_w, tb_h = self.toolbar.size_request() h += tb_h self.window.set_default_size (w, h) def destroy(*args): Gcf.destroy(num) self.window.connect("destroy", destroy) self.window.connect("delete_event", destroy) if matplotlib.is_interactive(): self.window.show() def notify_axes_change(fig): 'this will be called whenever the current axes is changed' if self.toolbar is not None: self.toolbar.update() self.canvas.figure.add_axobserver(notify_axes_change) self.canvas.grab_focus() def destroy(self, *args): if _debug: print 'FigureManagerGTK.%s' % fn_name() self.vbox.destroy() self.window.destroy() self.canvas.destroy() self.toolbar.destroy() self.__dict__.clear() if Gcf.get_num_fig_managers()==0 and \ not matplotlib.is_interactive() and \ gtk.main_level() >= 1: gtk.main_quit() def show(self): # show the figure window self.window.show() def full_screen_toggle (self): self._full_screen_flag = not self._full_screen_flag if self._full_screen_flag: self.window.fullscreen() else: self.window.unfullscreen() _full_screen_flag = False def _get_toolbar(self, canvas): # must be inited after the window, drawingArea and figure # attrs are set if matplotlib.rcParams['toolbar'] == 'classic': toolbar = NavigationToolbar (canvas, self.window) elif matplotlib.rcParams['toolbar'] == 'toolbar2': toolbar = NavigationToolbar2GTK (canvas, self.window) else: toolbar = None return toolbar def set_window_title(self, title): self.window.set_title(title) def resize(self, width, height): 'set the canvas size in pixels' #_, _, cw, ch = self.canvas.allocation #_, _, ww, wh = self.window.allocation #self.window.resize (width-cw+ww, height-ch+wh) self.window.resize(width, height) class NavigationToolbar2GTK(NavigationToolbar2, gtk.Toolbar): # list of toolitems to add to the toolbar, format is: # text, tooltip_text, image_file, callback(str) toolitems = ( ('Home', 'Reset original view', 'home.png', 'home'), ('Back', 'Back to previous view','back.png', 'back'), ('Forward', 'Forward to next view','forward.png', 'forward'), ('Pan', 'Pan axes with left mouse, zoom with right', 'move.png','pan'), ('Zoom', 'Zoom to rectangle','zoom_to_rect.png', 'zoom'), (None, None, None, None), ('Subplots', 'Configure subplots','subplots.png', 'configure_subplots'), ('Save', 'Save the figure','filesave.png', 'save_figure'), ) def __init__(self, canvas, window): self.win = window gtk.Toolbar.__init__(self) NavigationToolbar2.__init__(self, canvas) self._idle_draw_id = 0 def set_message(self, s): if self._idle_draw_id == 0: self.message.set_label(s) def set_cursor(self, cursor): self.canvas.window.set_cursor(cursord[cursor]) def release(self, event): try: del self._imageBack except AttributeError: pass def dynamic_update(self): # legacy method; new method is canvas.draw_idle self.canvas.draw_idle() def draw_rubberband(self, event, x0, y0, x1, y1): 'adapted from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/189744' drawable = self.canvas.window if drawable is None: return gc = drawable.new_gc() height = self.canvas.figure.bbox.height y1 = height - y1 y0 = height - y0 w = abs(x1 - x0) h = abs(y1 - y0) rect = [int(val)for val in min(x0,x1), min(y0, y1), w, h] try: lastrect, imageBack = self._imageBack except AttributeError: #snap image back if event.inaxes is None: return ax = event.inaxes l,b,w,h = [int(val) for val in ax.bbox.bounds] b = int(height)-(b+h) axrect = l,b,w,h self._imageBack = axrect, drawable.get_image(*axrect) drawable.draw_rectangle(gc, False, *rect) self._idle_draw_id = 0 else: def idle_draw(*args): drawable.draw_image(gc, imageBack, 0, 0, *lastrect) drawable.draw_rectangle(gc, False, *rect) self._idle_draw_id = 0 return False if self._idle_draw_id == 0: self._idle_draw_id = gobject.idle_add(idle_draw) def _init_toolbar(self): self.set_style(gtk.TOOLBAR_ICONS) if gtk.pygtk_version >= (2,4,0): self._init_toolbar2_4() else: self._init_toolbar2_2() def _init_toolbar2_2(self): basedir = os.path.join(matplotlib.rcParams['datapath'],'images') for text, tooltip_text, image_file, callback in self.toolitems: if text is None: self.append_space() continue fname = os.path.join(basedir, image_file) image = gtk.Image() image.set_from_file(fname) w = self.append_item(text, tooltip_text, 'Private', image, getattr(self, callback) ) self.append_space() self.message = gtk.Label() self.append_widget(self.message, None, None) self.message.show() def _init_toolbar2_4(self): basedir = os.path.join(matplotlib.rcParams['datapath'],'images') self.tooltips = gtk.Tooltips() for text, tooltip_text, image_file, callback in self.toolitems: if text is None: self.insert( gtk.SeparatorToolItem(), -1 ) continue fname = os.path.join(basedir, image_file) image = gtk.Image() image.set_from_file(fname) tbutton = gtk.ToolButton(image, text) self.insert(tbutton, -1) tbutton.connect('clicked', getattr(self, callback)) tbutton.set_tooltip(self.tooltips, tooltip_text, 'Private') toolitem = gtk.SeparatorToolItem() self.insert(toolitem, -1) # set_draw() not making separator invisible, # bug #143692 fixed Jun 06 2004, will be in GTK+ 2.6 toolitem.set_draw(False) toolitem.set_expand(True) toolitem = gtk.ToolItem() self.insert(toolitem, -1) self.message = gtk.Label() toolitem.add(self.message) self.show_all() def get_filechooser(self): if gtk.pygtk_version >= (2,4,0): return FileChooserDialog( title='Save the figure', parent=self.win, filetypes=self.canvas.get_supported_filetypes(), default_filetype=self.canvas.get_default_filetype()) else: return FileSelection(title='Save the figure', parent=self.win,) def save_figure(self, button): fname, format = self.get_filechooser().get_filename_from_user() if fname: try: self.canvas.print_figure(fname, format=format) except Exception, e: error_msg_gtk(str(e), parent=self) def configure_subplots(self, button): toolfig = Figure(figsize=(6,3)) canvas = self._get_canvas(toolfig) toolfig.subplots_adjust(top=0.9) tool = SubplotTool(self.canvas.figure, toolfig) w = int (toolfig.bbox.width) h = int (toolfig.bbox.height) window = gtk.Window() window.set_title("Subplot Configuration Tool") window.set_default_size(w, h) vbox = gtk.VBox() window.add(vbox) vbox.show() canvas.show() vbox.pack_start(canvas, True, True) window.show() def _get_canvas(self, fig): return FigureCanvasGTK(fig) class NavigationToolbar(gtk.Toolbar): """ Public attributes canvas - the FigureCanvas (gtk.DrawingArea) win - the gtk.Window """ # list of toolitems to add to the toolbar, format is: # text, tooltip_text, image, callback(str), callback_arg, scroll(bool) toolitems = ( ('Left', 'Pan left with click or wheel mouse (bidirectional)', gtk.STOCK_GO_BACK, 'panx', -1, True), ('Right', 'Pan right with click or wheel mouse (bidirectional)', gtk.STOCK_GO_FORWARD, 'panx', 1, True), ('Zoom In X', 'Zoom In X (shrink the x axis limits) with click or wheel' ' mouse (bidirectional)', gtk.STOCK_ZOOM_IN, 'zoomx', 1, True), ('Zoom Out X', 'Zoom Out X (expand the x axis limits) with click or wheel' ' mouse (bidirectional)', gtk.STOCK_ZOOM_OUT, 'zoomx', -1, True), (None, None, None, None, None, None,), ('Up', 'Pan up with click or wheel mouse (bidirectional)', gtk.STOCK_GO_UP, 'pany', 1, True), ('Down', 'Pan down with click or wheel mouse (bidirectional)', gtk.STOCK_GO_DOWN, 'pany', -1, True), ('Zoom In Y', 'Zoom in Y (shrink the y axis limits) with click or wheel' ' mouse (bidirectional)', gtk.STOCK_ZOOM_IN, 'zoomy', 1, True), ('Zoom Out Y', 'Zoom Out Y (expand the y axis limits) with click or wheel' ' mouse (bidirectional)', gtk.STOCK_ZOOM_OUT, 'zoomy', -1, True), (None, None, None, None, None, None,), ('Save', 'Save the figure', gtk.STOCK_SAVE, 'save_figure', None, False), ) def __init__(self, canvas, window): """ figManager is the FigureManagerGTK instance that contains the toolbar, with attributes figure, window and drawingArea """ gtk.Toolbar.__init__(self) self.canvas = canvas # Note: gtk.Toolbar already has a 'window' attribute self.win = window self.set_style(gtk.TOOLBAR_ICONS) if gtk.pygtk_version >= (2,4,0): self._create_toolitems_2_4() self.update = self._update_2_4 self.fileselect = FileChooserDialog( title='Save the figure', parent=self.win, filetypes=self.canvas.get_supported_filetypes(), default_filetype=self.canvas.get_default_filetype()) else: self._create_toolitems_2_2() self.update = self._update_2_2 self.fileselect = FileSelection(title='Save the figure', parent=self.win) self.show_all() self.update() def _create_toolitems_2_4(self): # use the GTK+ 2.4 GtkToolbar API iconSize = gtk.ICON_SIZE_SMALL_TOOLBAR self.tooltips = gtk.Tooltips() for text, tooltip_text, image_num, callback, callback_arg, scroll \ in self.toolitems: if text is None: self.insert( gtk.SeparatorToolItem(), -1 ) continue image = gtk.Image() image.set_from_stock(image_num, iconSize) tbutton = gtk.ToolButton(image, text) self.insert(tbutton, -1) if callback_arg: tbutton.connect('clicked', getattr(self, callback), callback_arg) else: tbutton.connect('clicked', getattr(self, callback)) if scroll: tbutton.connect('scroll_event', getattr(self, callback)) tbutton.set_tooltip(self.tooltips, tooltip_text, 'Private') # Axes toolitem, is empty at start, update() adds a menu if >=2 axes self.axes_toolitem = gtk.ToolItem() self.insert(self.axes_toolitem, 0) self.axes_toolitem.set_tooltip ( self.tooltips, tip_text='Select axes that controls affect', tip_private = 'Private') align = gtk.Alignment (xalign=0.5, yalign=0.5, xscale=0.0, yscale=0.0) self.axes_toolitem.add(align) self.menubutton = gtk.Button ("Axes") align.add (self.menubutton) def position_menu (menu): """Function for positioning a popup menu. Place menu below the menu button, but ensure it does not go off the bottom of the screen. The default is to popup menu at current mouse position """ x0, y0 = self.window.get_origin() x1, y1, m = self.window.get_pointer() x2, y2 = self.menubutton.get_pointer() sc_h = self.get_screen().get_height() # requires GTK+ 2.2 + w, h = menu.size_request() x = x0 + x1 - x2 y = y0 + y1 - y2 + self.menubutton.allocation.height y = min(y, sc_h - h) return x, y, True def button_clicked (button, data=None): self.axismenu.popup (None, None, position_menu, 0, gtk.get_current_event_time()) self.menubutton.connect ("clicked", button_clicked) def _update_2_4(self): # for GTK+ 2.4+ # called by __init__() and FigureManagerGTK self._axes = self.canvas.figure.axes if len(self._axes) >= 2: self.axismenu = self._make_axis_menu() self.menubutton.show_all() else: self.menubutton.hide() self.set_active(range(len(self._axes))) def _create_toolitems_2_2(self): # use the GTK+ 2.2 (and lower) GtkToolbar API iconSize = gtk.ICON_SIZE_SMALL_TOOLBAR for text, tooltip_text, image_num, callback, callback_arg, scroll \ in self.toolitems: if text is None: self.append_space() continue image = gtk.Image() image.set_from_stock(image_num, iconSize) item = self.append_item(text, tooltip_text, 'Private', image, getattr(self, callback), callback_arg) if scroll: item.connect("scroll_event", getattr(self, callback)) self.omenu = gtk.OptionMenu() self.omenu.set_border_width(3) self.insert_widget( self.omenu, 'Select axes that controls affect', 'Private', 0) def _update_2_2(self): # for GTK+ 2.2 and lower # called by __init__() and FigureManagerGTK self._axes = self.canvas.figure.axes if len(self._axes) >= 2: # set up the axis menu self.omenu.set_menu( self._make_axis_menu() ) self.omenu.show_all() else: self.omenu.hide() self.set_active(range(len(self._axes))) def _make_axis_menu(self): # called by self._update*() def toggled(item, data=None): if item == self.itemAll: for item in items: item.set_active(True) elif item == self.itemInvert: for item in items: item.set_active(not item.get_active()) ind = [i for i,item in enumerate(items) if item.get_active()] self.set_active(ind) menu = gtk.Menu() self.itemAll = gtk.MenuItem("All") menu.append(self.itemAll) self.itemAll.connect("activate", toggled) self.itemInvert = gtk.MenuItem("Invert") menu.append(self.itemInvert) self.itemInvert.connect("activate", toggled) items = [] for i in range(len(self._axes)): item = gtk.CheckMenuItem("Axis %d" % (i+1)) menu.append(item) item.connect("toggled", toggled) item.set_active(True) items.append(item) menu.show_all() return menu def set_active(self, ind): self._ind = ind self._active = [ self._axes[i] for i in self._ind ] def panx(self, button, direction): 'panx in direction' for a in self._active: a.xaxis.pan(direction) self.canvas.draw() return True def pany(self, button, direction): 'pany in direction' for a in self._active: a.yaxis.pan(direction) self.canvas.draw() return True def zoomx(self, button, direction): 'zoomx in direction' for a in self._active: a.xaxis.zoom(direction) self.canvas.draw() return True def zoomy(self, button, direction): 'zoomy in direction' for a in self._active: a.yaxis.zoom(direction) self.canvas.draw() return True def get_filechooser(self): if gtk.pygtk_version >= (2,4,0): return FileChooserDialog( title='Save the figure', parent=self.win, filetypes=self.canvas.get_supported_filetypes(), default_filetype=self.canvas.get_default_filetype()) else: return FileSelection(title='Save the figure', parent=self.win) def save_figure(self, button): fname, format = self.get_filechooser().get_filename_from_user() if fname: try: self.canvas.print_figure(fname, format=format) except Exception, e: error_msg_gtk(str(e), parent=self) if gtk.pygtk_version >= (2,4,0): class FileChooserDialog(gtk.FileChooserDialog): """GTK+ 2.4 file selector which remembers the last file/directory selected and presents the user with a menu of supported image formats """ def __init__ (self, title = 'Save file', parent = None, action = gtk.FILE_CHOOSER_ACTION_SAVE, buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK), path = None, filetypes = [], default_filetype = None ): super (FileChooserDialog, self).__init__ (title, parent, action, buttons) self.set_default_response (gtk.RESPONSE_OK) if not path: path = os.getcwd() + os.sep # create an extra widget to list supported image formats self.set_current_folder (path) self.set_current_name ('image.' + default_filetype) hbox = gtk.HBox (spacing=10) hbox.pack_start (gtk.Label ("File Format:"), expand=False) liststore = gtk.ListStore(gobject.TYPE_STRING) cbox = gtk.ComboBox(liststore) cell = gtk.CellRendererText() cbox.pack_start(cell, True) cbox.add_attribute(cell, 'text', 0) hbox.pack_start (cbox) self.filetypes = filetypes self.sorted_filetypes = filetypes.items() self.sorted_filetypes.sort() default = 0 for i, (ext, name) in enumerate(self.sorted_filetypes): cbox.append_text ("%s (*.%s)" % (name, ext)) if ext == default_filetype: default = i cbox.set_active(default) self.ext = default_filetype def cb_cbox_changed (cbox, data=None): """File extension changed""" head, filename = os.path.split(self.get_filename()) root, ext = os.path.splitext(filename) ext = ext[1:] new_ext = self.sorted_filetypes[cbox.get_active()][0] self.ext = new_ext if ext in self.filetypes: filename = root + '.' + new_ext elif ext == '': filename = filename.rstrip('.') + '.' + new_ext self.set_current_name (filename) cbox.connect ("changed", cb_cbox_changed) hbox.show_all() self.set_extra_widget(hbox) def get_filename_from_user (self): while True: filename = None if self.run() != int(gtk.RESPONSE_OK): break filename = self.get_filename() break self.hide() return filename, self.ext else: class FileSelection(gtk.FileSelection): """GTK+ 2.2 and lower file selector which remembers the last file/directory selected """ def __init__(self, path=None, title='Select a file', parent=None): super(FileSelection, self).__init__(title) if path: self.path = path else: self.path = os.getcwd() + os.sep if parent: self.set_transient_for(parent) def get_filename_from_user(self, path=None, title=None): if path: self.path = path if title: self.set_title(title) self.set_filename(self.path) filename = None if self.run() == int(gtk.RESPONSE_OK): self.path = filename = self.get_filename() self.hide() ext = None if filename is not None: ext = os.path.splitext(filename)[1] if ext.startswith('.'): ext = ext[1:] return filename, ext class DialogLineprops: """ A GUI dialog for controlling lineprops """ signals = ( 'on_combobox_lineprops_changed', 'on_combobox_linestyle_changed', 'on_combobox_marker_changed', 'on_colorbutton_linestyle_color_set', 'on_colorbutton_markerface_color_set', 'on_dialog_lineprops_okbutton_clicked', 'on_dialog_lineprops_cancelbutton_clicked', ) linestyles = [ls for ls in lines.Line2D.lineStyles if ls.strip()] linestyled = dict([ (s,i) for i,s in enumerate(linestyles)]) markers = [m for m in lines.Line2D.markers if cbook.is_string_like(m)] markerd = dict([(s,i) for i,s in enumerate(markers)]) def __init__(self, lines): import gtk.glade datadir = matplotlib.get_data_path() gladefile = os.path.join(datadir, 'lineprops.glade') if not os.path.exists(gladefile): raise IOError('Could not find gladefile lineprops.glade in %s'%datadir) self._inited = False self._updateson = True # suppress updates when setting widgets manually self.wtree = gtk.glade.XML(gladefile, 'dialog_lineprops') self.wtree.signal_autoconnect(dict([(s, getattr(self, s)) for s in self.signals])) self.dlg = self.wtree.get_widget('dialog_lineprops') self.lines = lines cbox = self.wtree.get_widget('combobox_lineprops') cbox.set_active(0) self.cbox_lineprops = cbox cbox = self.wtree.get_widget('combobox_linestyles') for ls in self.linestyles: cbox.append_text(ls) cbox.set_active(0) self.cbox_linestyles = cbox cbox = self.wtree.get_widget('combobox_markers') for m in self.markers: cbox.append_text(m) cbox.set_active(0) self.cbox_markers = cbox self._lastcnt = 0 self._inited = True def show(self): 'populate the combo box' self._updateson = False # flush the old cbox = self.cbox_lineprops for i in range(self._lastcnt-1,-1,-1): cbox.remove_text(i) # add the new for line in self.lines: cbox.append_text(line.get_label()) cbox.set_active(0) self._updateson = True self._lastcnt = len(self.lines) self.dlg.show() def get_active_line(self): 'get the active line' ind = self.cbox_lineprops.get_active() line = self.lines[ind] return line def get_active_linestyle(self): 'get the active lineinestyle' ind = self.cbox_linestyles.get_active() ls = self.linestyles[ind] return ls def get_active_marker(self): 'get the active lineinestyle' ind = self.cbox_markers.get_active() m = self.markers[ind] return m def _update(self): 'update the active line props from the widgets' if not self._inited or not self._updateson: return line = self.get_active_line() ls = self.get_active_linestyle() marker = self.get_active_marker() line.set_linestyle(ls) line.set_marker(marker) button = self.wtree.get_widget('colorbutton_linestyle') color = button.get_color() r, g, b = [val/65535. for val in color.red, color.green, color.blue] line.set_color((r,g,b)) button = self.wtree.get_widget('colorbutton_markerface') color = button.get_color() r, g, b = [val/65535. for val in color.red, color.green, color.blue] line.set_markerfacecolor((r,g,b)) line.figure.canvas.draw() def on_combobox_lineprops_changed(self, item): 'update the widgets from the active line' if not self._inited: return self._updateson = False line = self.get_active_line() ls = line.get_linestyle() if ls is None: ls = 'None' self.cbox_linestyles.set_active(self.linestyled[ls]) marker = line.get_marker() if marker is None: marker = 'None' self.cbox_markers.set_active(self.markerd[marker]) r,g,b = colorConverter.to_rgb(line.get_color()) color = gtk.gdk.Color(*[int(val*65535) for val in r,g,b]) button = self.wtree.get_widget('colorbutton_linestyle') button.set_color(color) r,g,b = colorConverter.to_rgb(line.get_markerfacecolor()) color = gtk.gdk.Color(*[int(val*65535) for val in r,g,b]) button = self.wtree.get_widget('colorbutton_markerface') button.set_color(color) self._updateson = True def on_combobox_linestyle_changed(self, item): self._update() def on_combobox_marker_changed(self, item): self._update() def on_colorbutton_linestyle_color_set(self, button): self._update() def on_colorbutton_markerface_color_set(self, button): 'called colorbutton marker clicked' self._update() def on_dialog_lineprops_okbutton_clicked(self, button): self._update() self.dlg.hide() def on_dialog_lineprops_cancelbutton_clicked(self, button): self.dlg.hide() # set icon used when windows are minimized # Unfortunately, the SVG renderer (rsvg) leaks memory under earlier # versions of pygtk, so we have to use a PNG file instead. try: if gtk.pygtk_version < (2, 8, 0): icon_filename = 'matplotlib.png' else: icon_filename = 'matplotlib.svg' gtk.window_set_default_icon_from_file ( os.path.join (matplotlib.rcParams['datapath'], 'images', icon_filename)) except: verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1]) def error_msg_gtk(msg, parent=None): if parent is not None: # find the toplevel gtk.Window parent = parent.get_toplevel() if parent.flags() & gtk.TOPLEVEL == 0: parent = None if not is_string_like(msg): msg = ','.join(map(str,msg)) dialog = gtk.MessageDialog( parent = parent, type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK, message_format = msg) dialog.run() dialog.destroy() FigureManager = FigureManagerGTK
agpl-3.0
Tatsh-ansible/ansible
lib/ansible/utils/module_docs_fragments/avi.py
74
1719
# # Created on December 12, 2016 # @author: Gaurav Rastogi (grastogi@avinetworks.com) # Avi Version: 16.3.4 # # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. # class ModuleDocFragment(object): # Avi common documentation fragment DOCUMENTATION = """ options: controller: description: - IP address or hostname of the controller. The default value is the environment variable C(AVI_CONTROLLER). username: description: - Username used for accessing Avi controller. The default value is the environment variable C(AVI_USERNAME). password: description: - Password of Avi user in Avi controller. The default value is the environment variable C(AVI_PASSWORD). tenant: description: - Name of tenant used for all Avi API calls and context of object. default: admin tenant_uuid: description: - UUID of tenant used for all Avi API calls and context of object. default: '' api_version: description: - Avi API version of to use for Avi API and objects. """
gpl-3.0
kimimj/scrapy
tests/test_utils_iterators.py
79
14864
import os from twisted.trial import unittest from scrapy.utils.iterators import csviter, xmliter, _body_or_str, xmliter_lxml from scrapy.http import XmlResponse, TextResponse, Response from tests import get_testdata FOOBAR_NL = u"foo" + os.linesep + u"bar" class XmliterTestCase(unittest.TestCase): xmliter = staticmethod(xmliter) def test_xmliter(self): body = b"""<?xml version="1.0" encoding="UTF-8"?>\ <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="someschmea.xsd">\ <product id="001">\ <type>Type 1</type>\ <name>Name 1</name>\ </product>\ <product id="002">\ <type>Type 2</type>\ <name>Name 2</name>\ </product>\ </products>""" response = XmlResponse(url="http://example.com", body=body) attrs = [] for x in self.xmliter(response, 'product'): attrs.append((x.xpath("@id").extract(), x.xpath("name/text()").extract(), x.xpath("./type/text()").extract())) self.assertEqual(attrs, [(['001'], ['Name 1'], ['Type 1']), (['002'], ['Name 2'], ['Type 2'])]) def test_xmliter_text(self): body = u"""<?xml version="1.0" encoding="UTF-8"?><products><product>one</product><product>two</product></products>""" self.assertEqual([x.xpath("text()").extract() for x in self.xmliter(body, 'product')], [[u'one'], [u'two']]) def test_xmliter_namespaces(self): body = b"""\ <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"> <channel> <title>My Dummy Company</title> <link>http://www.mydummycompany.com</link> <description>This is a dummy company. We do nothing.</description> <item> <title>Item 1</title> <description>This is item 1</description> <link>http://www.mydummycompany.com/items/1</link> <g:image_link>http://www.mydummycompany.com/images/item1.jpg</g:image_link> <g:id>ITEM_1</g:id> <g:price>400</g:price> </item> </channel> </rss> """ response = XmlResponse(url='http://mydummycompany.com', body=body) my_iter = self.xmliter(response, 'item') node = next(my_iter) node.register_namespace('g', 'http://base.google.com/ns/1.0') self.assertEqual(node.xpath('title/text()').extract(), ['Item 1']) self.assertEqual(node.xpath('description/text()').extract(), ['This is item 1']) self.assertEqual(node.xpath('link/text()').extract(), ['http://www.mydummycompany.com/items/1']) self.assertEqual(node.xpath('g:image_link/text()').extract(), ['http://www.mydummycompany.com/images/item1.jpg']) self.assertEqual(node.xpath('g:id/text()').extract(), ['ITEM_1']) self.assertEqual(node.xpath('g:price/text()').extract(), ['400']) self.assertEqual(node.xpath('image_link/text()').extract(), []) self.assertEqual(node.xpath('id/text()').extract(), []) self.assertEqual(node.xpath('price/text()').extract(), []) def test_xmliter_exception(self): body = u"""<?xml version="1.0" encoding="UTF-8"?><products><product>one</product><product>two</product></products>""" iter = self.xmliter(body, 'product') next(iter) next(iter) self.assertRaises(StopIteration, next, iter) def test_xmliter_encoding(self): body = b'<?xml version="1.0" encoding="ISO-8859-9"?>\n<xml>\n <item>Some Turkish Characters \xd6\xc7\xde\xdd\xd0\xdc \xfc\xf0\xfd\xfe\xe7\xf6</item>\n</xml>\n\n' response = XmlResponse('http://www.example.com', body=body) self.assertEqual( self.xmliter(response, 'item').next().extract(), u'<item>Some Turkish Characters \xd6\xc7\u015e\u0130\u011e\xdc \xfc\u011f\u0131\u015f\xe7\xf6</item>' ) class LxmlXmliterTestCase(XmliterTestCase): xmliter = staticmethod(xmliter_lxml) def test_xmliter_iterate_namespace(self): body = b"""\ <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns="http://base.google.com/ns/1.0"> <channel> <title>My Dummy Company</title> <link>http://www.mydummycompany.com</link> <description>This is a dummy company. We do nothing.</description> <item> <title>Item 1</title> <description>This is item 1</description> <link>http://www.mydummycompany.com/items/1</link> <image_link>http://www.mydummycompany.com/images/item1.jpg</image_link> <image_link>http://www.mydummycompany.com/images/item2.jpg</image_link> </item> </channel> </rss> """ response = XmlResponse(url='http://mydummycompany.com', body=body) no_namespace_iter = self.xmliter(response, 'image_link') self.assertEqual(len(list(no_namespace_iter)), 0) namespace_iter = self.xmliter(response, 'image_link', 'http://base.google.com/ns/1.0') node = next(namespace_iter) self.assertEqual(node.xpath('text()').extract(), ['http://www.mydummycompany.com/images/item1.jpg']) node = next(namespace_iter) self.assertEqual(node.xpath('text()').extract(), ['http://www.mydummycompany.com/images/item2.jpg']) def test_xmliter_namespaces_prefix(self): body = b"""\ <?xml version="1.0" encoding="UTF-8"?> <root> <h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table xmlns:f="http://www.w3schools.com/furniture"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> </root> """ response = XmlResponse(url='http://mydummycompany.com', body=body) my_iter = self.xmliter(response, 'table', 'http://www.w3.org/TR/html4/', 'h') node = next(my_iter) self.assertEqual(len(node.xpath('h:tr/h:td').extract()), 2) self.assertEqual(node.xpath('h:tr/h:td[1]/text()').extract(), ['Apples']) self.assertEqual(node.xpath('h:tr/h:td[2]/text()').extract(), ['Bananas']) my_iter = self.xmliter(response, 'table', 'http://www.w3schools.com/furniture', 'f') node = next(my_iter) self.assertEqual(node.xpath('f:name/text()').extract(), ['African Coffee Table']) class UtilsCsvTestCase(unittest.TestCase): sample_feeds_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'sample_data', 'feeds') sample_feed_path = os.path.join(sample_feeds_dir, 'feed-sample3.csv') sample_feed2_path = os.path.join(sample_feeds_dir, 'feed-sample4.csv') sample_feed3_path = os.path.join(sample_feeds_dir, 'feed-sample5.csv') def test_csviter_defaults(self): body = get_testdata('feeds', 'feed-sample3.csv') response = TextResponse(url="http://example.com/", body=body) csv = csviter(response) result = [row for row in csv] self.assertEqual(result, [{u'id': u'1', u'name': u'alpha', u'value': u'foobar'}, {u'id': u'2', u'name': u'unicode', u'value': u'\xfan\xedc\xf3d\xe9\u203d'}, {u'id': u'3', u'name': u'multi', u'value': FOOBAR_NL}, {u'id': u'4', u'name': u'empty', u'value': u''}]) # explicit type check cuz' we no like stinkin' autocasting! yarrr for result_row in result: self.assert_(all((isinstance(k, unicode) for k in result_row.keys()))) self.assert_(all((isinstance(v, unicode) for v in result_row.values()))) def test_csviter_delimiter(self): body = get_testdata('feeds', 'feed-sample3.csv').replace(',', '\t') response = TextResponse(url="http://example.com/", body=body) csv = csviter(response, delimiter='\t') self.assertEqual([row for row in csv], [{u'id': u'1', u'name': u'alpha', u'value': u'foobar'}, {u'id': u'2', u'name': u'unicode', u'value': u'\xfan\xedc\xf3d\xe9\u203d'}, {u'id': u'3', u'name': u'multi', u'value': FOOBAR_NL}, {u'id': u'4', u'name': u'empty', u'value': u''}]) def test_csviter_quotechar(self): body1 = get_testdata('feeds', 'feed-sample6.csv') body2 = get_testdata('feeds', 'feed-sample6.csv').replace(",", '|') response1 = TextResponse(url="http://example.com/", body=body1) csv1 = csviter(response1, quotechar="'") self.assertEqual([row for row in csv1], [{u'id': u'1', u'name': u'alpha', u'value': u'foobar'}, {u'id': u'2', u'name': u'unicode', u'value': u'\xfan\xedc\xf3d\xe9\u203d'}, {u'id': u'3', u'name': u'multi', u'value': FOOBAR_NL}, {u'id': u'4', u'name': u'empty', u'value': u''}]) response2 = TextResponse(url="http://example.com/", body=body2) csv2 = csviter(response2, delimiter="|", quotechar="'") self.assertEqual([row for row in csv2], [{u'id': u'1', u'name': u'alpha', u'value': u'foobar'}, {u'id': u'2', u'name': u'unicode', u'value': u'\xfan\xedc\xf3d\xe9\u203d'}, {u'id': u'3', u'name': u'multi', u'value': FOOBAR_NL}, {u'id': u'4', u'name': u'empty', u'value': u''}]) def test_csviter_wrong_quotechar(self): body = get_testdata('feeds', 'feed-sample6.csv') response = TextResponse(url="http://example.com/", body=body) csv = csviter(response) self.assertEqual([row for row in csv], [{u"'id'": u"1", u"'name'": u"'alpha'", u"'value'": u"'foobar'"}, {u"'id'": u"2", u"'name'": u"'unicode'", u"'value'": u"'\xfan\xedc\xf3d\xe9\u203d'"}, {u"'id'": u"'3'", u"'name'": u"'multi'", u"'value'": u"'foo"}, {u"'id'": u"4", u"'name'": u"'empty'", u"'value'": u""}]) def test_csviter_delimiter_binary_response_assume_utf8_encoding(self): body = get_testdata('feeds', 'feed-sample3.csv').replace(',', '\t') response = Response(url="http://example.com/", body=body) csv = csviter(response, delimiter='\t') self.assertEqual([row for row in csv], [{u'id': u'1', u'name': u'alpha', u'value': u'foobar'}, {u'id': u'2', u'name': u'unicode', u'value': u'\xfan\xedc\xf3d\xe9\u203d'}, {u'id': u'3', u'name': u'multi', u'value': FOOBAR_NL}, {u'id': u'4', u'name': u'empty', u'value': u''}]) def test_csviter_headers(self): sample = get_testdata('feeds', 'feed-sample3.csv').splitlines() headers, body = sample[0].split(','), '\n'.join(sample[1:]) response = TextResponse(url="http://example.com/", body=body) csv = csviter(response, headers=headers) self.assertEqual([row for row in csv], [{u'id': u'1', u'name': u'alpha', u'value': u'foobar'}, {u'id': u'2', u'name': u'unicode', u'value': u'\xfan\xedc\xf3d\xe9\u203d'}, {u'id': u'3', u'name': u'multi', u'value': u'foo\nbar'}, {u'id': u'4', u'name': u'empty', u'value': u''}]) def test_csviter_falserow(self): body = get_testdata('feeds', 'feed-sample3.csv') body = '\n'.join((body, 'a,b', 'a,b,c,d')) response = TextResponse(url="http://example.com/", body=body) csv = csviter(response) self.assertEqual([row for row in csv], [{u'id': u'1', u'name': u'alpha', u'value': u'foobar'}, {u'id': u'2', u'name': u'unicode', u'value': u'\xfan\xedc\xf3d\xe9\u203d'}, {u'id': u'3', u'name': u'multi', u'value': FOOBAR_NL}, {u'id': u'4', u'name': u'empty', u'value': u''}]) def test_csviter_exception(self): body = get_testdata('feeds', 'feed-sample3.csv') response = TextResponse(url="http://example.com/", body=body) iter = csviter(response) next(iter) next(iter) next(iter) next(iter) self.assertRaises(StopIteration, next, iter) def test_csviter_encoding(self): body1 = get_testdata('feeds', 'feed-sample4.csv') body2 = get_testdata('feeds', 'feed-sample5.csv') response = TextResponse(url="http://example.com/", body=body1, encoding='latin1') csv = csviter(response) self.assertEqual([row for row in csv], [{u'id': u'1', u'name': u'latin1', u'value': u'test'}, {u'id': u'2', u'name': u'something', u'value': u'\xf1\xe1\xe9\xf3'}]) response = TextResponse(url="http://example.com/", body=body2, encoding='cp852') csv = csviter(response) self.assertEqual([row for row in csv], [{u'id': u'1', u'name': u'cp852', u'value': u'test'}, {u'id': u'2', u'name': u'something', u'value': u'\u255a\u2569\u2569\u2569\u2550\u2550\u2557'}]) class TestHelper(unittest.TestCase): bbody = b'utf8-body' ubody = bbody.decode('utf8') txtresponse = TextResponse(url='http://example.org/', body=bbody, encoding='utf-8') response = Response(url='http://example.org/', body=bbody) def test_body_or_str(self): for obj in (self.bbody, self.ubody, self.txtresponse, self.response): r1 = _body_or_str(obj) self._assert_type_and_value(r1, self.ubody, obj) r2 = _body_or_str(obj, unicode=True) self._assert_type_and_value(r2, self.ubody, obj) r3 = _body_or_str(obj, unicode=False) self._assert_type_and_value(r3, self.bbody, obj) self.assertTrue(type(r1) is type(r2)) self.assertTrue(type(r1) is not type(r3)) def _assert_type_and_value(self, a, b, obj): self.assertTrue(type(a) is type(b), 'Got {}, expected {} for {!r}'.format(type(a), type(b), obj)) self.assertEqual(a, b) if __name__ == "__main__": unittest.main()
bsd-3-clause
mtlynch/mlab-observatory
convert_from_telescope/reducer.py
1
3374
#!/usr/bin/env python # -*- coding: UTF-8 -*- # # Copyright 2015 Measurement Lab # # 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. """Module to reduce sets of results to their aggregate values. This module provides classes that take a collection of Telescope results and aggregate them to a single value and sample count. """ import collections import numpy import aggregate class MedianReducer(object): """Reduces a set of raw metrics to their median values. Reduces a set of raw metrics to their median value within a given window of time (e.g. a day, an hour) and, for each time unit, outputs the median value and the sample count. """ def reduce_by_day(self, metrics_raw): """Reduces metrics to their median values per day.""" return self._reduce(metrics_raw, aggregate.aggregate_by_day) def reduce_by_hour_of_day_per_month(self, metrics_raw): """Reduces metrics to their median values by hour of day per month.""" return self._reduce(metrics_raw, aggregate.aggregate_by_hour_of_day_per_month) def _reduce(self, metrics_raw, aggregation_func): """Reduces raw metrics to their median values and count. Calculates median values for raw metric values, aggregated according to the given aggregation function. Args: metrics_raw: (dict) A dictionary of metrics and their corresponding value lists, for example: { 'download_throughput': ((<datetime-2012-04-05@15:28:02>, 12.192), (<datetime-2013-08-01@03:29:15>, 13.012), ...) 'upload_throughput': ((<datetime-2012-04-05@15:28:02>, 2.502), (<datetime-2013-08-01@03:29:15>, 8.689), ...) ... } aggregation_func: (function) The function to apply to the value lists to aggregate the data. Returns: (dict) A dictionary where the keys are datetime objects representing the time buckets in which the data has been aggregated and the values are dictionaries of metrics containing the metric median and sample count. For example: { <datetime-2014-10-01>: { 'download_throughput': 15.89, 'download_throughput_n': 128, 'upload_throughput': 2.942, 'upload_throughput_n': 115, ... } <datetime-2014-10-02>: ... } """ metrics_aggregated = collections.defaultdict(lambda: {}) for metric, rows_raw in metrics_raw.iteritems(): for time, values in aggregation_func(rows_raw).iteritems(): metrics_aggregated[time][metric] = numpy.median(values) metrics_aggregated[time][metric + '_n'] = len(values) return metrics_aggregated
apache-2.0
edbrannin/robotframework
src/robot/utils/frange.py
5
1979
# Copyright 2008-2015 Nokia Solutions and Networks # # 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. def frange(*args): """Like ``range()`` but accepts float arguments.""" if all(isinstance(arg, (int, long)) for arg in args): return range(*args) start, stop, step = _get_start_stop_step(args) digits = max(_digits(start), _digits(stop), _digits(step)) factor = pow(10, digits) return [x/float(factor) for x in range(int(round(start*factor)), int(round(stop*factor)), int(round(step*factor)))] def _get_start_stop_step(args): if len(args) == 1: return 0, args[0], 1 if len(args) == 2: return args[0], args[1], 1 if len(args) == 3: return args raise TypeError('frange expected 1-3 arguments, got %d.' % len(args)) def _digits(number): if not isinstance(number, str): number = repr(number) if 'e' in number: return _digits_with_exponent(number) if '.' in number: return _digits_with_fractional(number) return 0 def _digits_with_exponent(number): mantissa, exponent = number.split('e') mantissa_digits = _digits(mantissa) exponent_digits = int(exponent) * -1 return max(mantissa_digits + exponent_digits, 0) def _digits_with_fractional(number): fractional = number.split('.')[1] if fractional == '0': return 0 return len(fractional)
apache-2.0
robinvandernoord/electron
script/dump-symbols.py
144
1962
#!/usr/bin/env python import os import sys from lib.config import PLATFORM from lib.util import atom_gyp, execute, rm_rf SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) DIST_DIR = os.path.join(SOURCE_ROOT, 'dist') OUT_DIR = os.path.join(SOURCE_ROOT, 'out', 'R') CHROMIUM_DIR = os.path.join(SOURCE_ROOT, 'vendor', 'brightray', 'vendor', 'download', 'libchromiumcontent', 'static_library') def main(destination): # if PLATFORM == 'win32': # register_required_dll() rm_rf(destination) (project_name, product_name) = get_names_from_gyp() if PLATFORM in ['darwin', 'linux']: generate_breakpad_symbols = os.path.join(SOURCE_ROOT, 'tools', 'posix', 'generate_breakpad_symbols.py') if PLATFORM == 'darwin': start = os.path.join(OUT_DIR, '{0}.app'.format(product_name), 'Contents', 'MacOS', product_name) else: start = os.path.join(OUT_DIR, project_name) args = [ '--build-dir={0}'.format(OUT_DIR), '--binary={0}'.format(start), '--symbols-dir={0}'.format(destination), '--libchromiumcontent-dir={0}'.format(CHROMIUM_DIR), '--clear', '--jobs=16', ] else: generate_breakpad_symbols = os.path.join(SOURCE_ROOT, 'tools', 'win', 'generate_breakpad_symbols.py') args = [ '--symbols-dir={0}'.format(destination), '--jobs=16', os.path.relpath(OUT_DIR), ] execute([sys.executable, generate_breakpad_symbols] + args) def register_required_dll(): register = os.path.join(SOURCE_ROOT, 'tools', 'win', 'register_msdia80_dll.js') execute(['node.exe', os.path.relpath(register)]); def get_names_from_gyp(): variables = atom_gyp() return (variables['project_name%'], variables['product_name%']) if __name__ == '__main__': sys.exit(main(sys.argv[1]))
mit
nicholasamorim/brainer
setup.py
1
1303
from setuptools import setup, find_packages long_description = """ Brainer is a simple distributed caching mechanism done in 48 hours. """ setup( name="brainer", version='0.1', description="Distributed Cache with Twisted and ZeroMQ", long_description=long_description, author="Nicholas Amorim", author_email="nicholas@alienretro.com", url="https://github.com/nicholasamorim/brainer", license="MIT", packages=find_packages(), install_requires=['twisted', 'pyzmq', 'txZMQ', 'u-msgpack-python'], # requires=['twisted(==15.3.0)', 'pyzmq', 'txZMQ', 'u-msgpack-python'], tests_require=['twisted', 'pyzmq', 'txZMQ', 'u-msgpack-python', 'mock'], keywords='distributed cache twisted zeromq', scripts=['brainer/brainer-cli', 'brainer/run_broker', 'brainer/run_node'], zip_safe=False, classifiers=[ "Development Status :: 3 - Alpha", "Framework :: Twisted", "Operating System :: POSIX :: Linux", "Environment :: Console", "Intended Audience :: Developers", "Intended Audience :: System Administrators", "License :: OSI Approved :: MIT License", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", ], )
mit
be-cloud-be/horizon-addons
server/openerp/addons/test_workflow/tests/test_workflow.py
392
6232
# -*- coding: utf-8 -*- import openerp from openerp import SUPERUSER_ID from openerp.tests import common class test_workflows(common.TransactionCase): def check_activities(self, model_name, i, names): """ Check that the record i has workitems in the given activity names. """ instance = self.registry('workflow.instance') workitem = self.registry('workflow.workitem') # Given the workflow instance associated to the record ... instance_id = instance.search( self.cr, SUPERUSER_ID, [('res_type', '=', model_name), ('res_id', '=', i)]) self.assertTrue( instance_id, 'A workflow instance is expected.') # ... get all its workitems ... workitem_ids = workitem.search( self.cr, SUPERUSER_ID, [('inst_id', '=', instance_id[0])]) self.assertTrue( workitem_ids, 'The workflow instance should have workitems.') # ... and check the activity the are in against the provided names. workitem_records = workitem.browse( self.cr, SUPERUSER_ID, workitem_ids) self.assertEqual( sorted([item.act_id.name for item in workitem_records]), sorted(names)) def check_value(self, model_name, i, value): """ Check that the record i has the given value. """ model = self.registry(model_name) record = model.read(self.cr, SUPERUSER_ID, [i], ['value'])[0] self.assertEqual(record['value'], value) def test_workflow(self): model = self.registry('test.workflow.model') trigger = self.registry('test.workflow.trigger') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['a']) # a -> b is just a signal. model.signal_workflow(self.cr, SUPERUSER_ID, [i], 'a-b') self.check_activities(model._name, i, ['b']) # b -> c is a trigger (which is False), # so we remain in the b activity. model.trigger(self.cr, SUPERUSER_ID) self.check_activities(model._name, i, ['b']) # b -> c is a trigger (which is set to True). # so we go in c when the trigger is called. trigger.write(self.cr, SUPERUSER_ID, [1], {'value': True}) model.trigger(self.cr, SUPERUSER_ID) self.check_activities(model._name, i, ['c']) self.assertEqual( True, True) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_a(self): model = self.registry('test.workflow.model.a') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['a']) self.check_value(model._name, i, 0) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_b(self): model = self.registry('test.workflow.model.b') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['a']) self.check_value(model._name, i, 1) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_c(self): model = self.registry('test.workflow.model.c') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['a']) self.check_value(model._name, i, 0) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_d(self): model = self.registry('test.workflow.model.d') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['a']) self.check_value(model._name, i, 1) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_e(self): model = self.registry('test.workflow.model.e') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['b']) self.check_value(model._name, i, 2) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_f(self): model = self.registry('test.workflow.model.f') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['a']) self.check_value(model._name, i, 1) model.signal_workflow(self.cr, SUPERUSER_ID, [i], 'a-b') self.check_activities(model._name, i, ['b']) self.check_value(model._name, i, 2) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_g(self): model = self.registry('test.workflow.model.g') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['a']) self.check_value(model._name, i, 1) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_h(self): model = self.registry('test.workflow.model.h') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['b', 'c']) self.check_value(model._name, i, 2) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_i(self): model = self.registry('test.workflow.model.i') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['b']) self.check_value(model._name, i, 2) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_j(self): model = self.registry('test.workflow.model.j') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['a']) self.check_value(model._name, i, 1) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_k(self): model = self.registry('test.workflow.model.k') i = model.create(self.cr, SUPERUSER_ID, {}) # Non-determinisitic: can be b or c # self.check_activities(model._name, i, ['b']) # self.check_activities(model._name, i, ['c']) self.check_value(model._name, i, 2) model.unlink(self.cr, SUPERUSER_ID, [i]) def test_workflow_l(self): model = self.registry('test.workflow.model.l') i = model.create(self.cr, SUPERUSER_ID, {}) self.check_activities(model._name, i, ['c', 'c', 'd']) self.check_value(model._name, i, 3) model.unlink(self.cr, SUPERUSER_ID, [i])
agpl-3.0
kawamon/hue
desktop/core/ext-py/jaeger-client-4.0.0/jaeger_client/config.py
2
14973
# Copyright (c) 2016-2018 Uber Technologies, Inc. # # 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. from __future__ import absolute_import import logging import os import threading import opentracing from opentracing.propagation import Format from . import Tracer from .local_agent_net import LocalAgentSender from .throttler import RemoteThrottler from .reporter import ( Reporter, CompositeReporter, LoggingReporter, ) from .sampler import ( ConstSampler, ProbabilisticSampler, RateLimitingSampler, RemoteControlledSampler, ) from .constants import ( DEFAULT_SAMPLING_INTERVAL, DEFAULT_FLUSH_INTERVAL, SAMPLER_TYPE_CONST, SAMPLER_TYPE_PROBABILISTIC, SAMPLER_TYPE_RATE_LIMITING, TRACE_ID_HEADER, BAGGAGE_HEADER_PREFIX, DEBUG_ID_HEADER_KEY, MAX_TAG_VALUE_LENGTH, DEFAULT_THROTTLER_REFRESH_INTERVAL, ) from .metrics import LegacyMetricsFactory, MetricsFactory, Metrics from .utils import get_boolean, ErrorReporter from .codecs import B3Codec DEFAULT_REPORTING_HOST = 'localhost' DEFAULT_REPORTING_PORT = 6831 DEFAULT_SAMPLING_PORT = 5778 DEFAULT_THROTTLER_PORT = DEFAULT_SAMPLING_PORT LOCAL_AGENT_DEFAULT_ENABLED = True logger = logging.getLogger('jaeger_tracing') class Config(object): """ Wraps a YAML configuration section for configuring Jaeger Tracer. service_name is required, but can be passed either as constructor parameter, or as config property. Example: .. code-block:: yaml enabled: true reporter_batch_size: 10 logging: true metrics: true sampler: type: const param: true """ _initialized = False _initialized_lock = threading.Lock() def __init__(self, config, metrics=None, service_name=None, metrics_factory=None, validate=False, scope_manager=None): """ :param metrics: an instance of Metrics class, or None. This parameter has been deprecated, please use metrics_factory instead. :param service_name: default service name. Can be overwritten by config['service_name']. :param metrics_factory: an instance of MetricsFactory class, or None. :param scope_manager: an instance of a scope manager, or None for default (ThreadLocalScopeManager). """ if validate: self._validate_config(config) self.config = config self.scope_manager = scope_manager if get_boolean(self.config.get('metrics', True), True): self._metrics_factory = metrics_factory or LegacyMetricsFactory(metrics or Metrics()) else: # if metrics are explicitly disabled, use a dummy self._metrics_factory = MetricsFactory() self._service_name = config.get('service_name', service_name) if not self._service_name: raise ValueError('service_name required in the config or param') self._error_reporter = ErrorReporter( metrics=Metrics(), logger=logger if self.logging else None, ) def _validate_config(self, config): allowed_keys = ['logging', 'local_agent', 'sampler', 'tags', 'enabled', 'reporter_batch_size', 'reporter_queue_size', 'propagation', 'max_tag_value_length', 'reporter_flush_interval', 'sampling_refresh_interval', 'trace_id_header', 'generate_128bit_trace_id', 'baggage_header_prefix', 'service_name', 'throttler'] config_keys = config.keys() unexpected_config_keys = [k for k in config_keys if k not in allowed_keys] if unexpected_config_keys: raise ValueError('Unexpected keys found in config:{}'. format(','.join(unexpected_config_keys))) @property def service_name(self): return self._service_name @property def metrics(self): return self._metrics @property def error_reporter(self): return self._error_reporter @property def enabled(self): return get_boolean(self.config.get('enabled', True), True) @property def reporter_batch_size(self): return int(self.config.get('reporter_batch_size', 10)) @property def reporter_queue_size(self): return int(self.config.get('reporter_queue_size', 100)) @property def logging(self): return get_boolean(self.config.get('logging', False), False) @property def trace_id_header(self): """ :return: Returns the name of the HTTP header used to encode trace ID """ return self.config.get('trace_id_header', TRACE_ID_HEADER) @property def generate_128bit_trace_id(self): """ :return: Returns boolean value to indicate if 128bit trace_id generation is enabled """ if 'generate_128bit_trace_id' in self.config: return get_boolean(self.config['generate_128bit_trace_id'], False) return os.getenv('JAEGER_TRACEID_128BIT') == 'true' @property def baggage_header_prefix(self): """ :return: Returns the prefix for HTTP headers used to record baggage items """ return self.config.get('baggage_header_prefix', BAGGAGE_HEADER_PREFIX) @property def debug_id_header(self): """ :return: Returns the name of HTTP header or a TextMap carrier key which, if found in the carrier, forces the trace to be sampled as "debug" trace. The value of the header is recorded as the tag on the root span, so that the trace can be found in the UI using this value as a correlation ID. """ return self.config.get('debug_id_header', DEBUG_ID_HEADER_KEY) @property def max_tag_value_length(self): """ :return: Returns max allowed tag value length. Longer values will be truncated. """ return self.config.get('max_tag_value_length', MAX_TAG_VALUE_LENGTH) @property def sampler(self): sampler_config = self.config.get('sampler', {}) sampler_type = sampler_config.get('type', None) sampler_param = sampler_config.get('param', None) if not sampler_type: return None elif sampler_type == SAMPLER_TYPE_CONST: return ConstSampler(decision=get_boolean(sampler_param, False)) elif sampler_type == SAMPLER_TYPE_PROBABILISTIC: return ProbabilisticSampler(rate=float(sampler_param)) elif sampler_type in [SAMPLER_TYPE_RATE_LIMITING, 'rate_limiting']: return RateLimitingSampler( max_traces_per_second=float(sampler_param)) raise ValueError('Unknown sampler type %s' % sampler_type) @property def sampling_refresh_interval(self): return self.config.get('sampling_refresh_interval', DEFAULT_SAMPLING_INTERVAL) @property def reporter_flush_interval(self): return self.config.get('reporter_flush_interval', DEFAULT_FLUSH_INTERVAL) def local_agent_group(self): return self.config.get('local_agent', None) @property def local_agent_enabled(self): # noinspection PyBroadException try: return get_boolean(self.local_agent_group().get('enabled', LOCAL_AGENT_DEFAULT_ENABLED), LOCAL_AGENT_DEFAULT_ENABLED) except: # noqa: E722 return LOCAL_AGENT_DEFAULT_ENABLED @property def local_agent_sampling_port(self): # noinspection PyBroadException try: return int(self.local_agent_group()['sampling_port']) except: # noqa: E722 return DEFAULT_SAMPLING_PORT @property def local_agent_reporting_port(self): # noinspection PyBroadException try: return int(self.local_agent_group()['reporting_port']) except: # noqa: E722 pass try: return int(os.getenv('JAEGER_AGENT_PORT')) except: # noqa: E722 return DEFAULT_REPORTING_PORT @property def local_agent_reporting_host(self): # noinspection PyBroadException try: return self.local_agent_group()['reporting_host'] except: # noqa: E722 pass if os.getenv('JAEGER_AGENT_HOST') is not None: return os.getenv('JAEGER_AGENT_HOST') else: return DEFAULT_REPORTING_HOST @property def max_operations(self): return self.config.get('max_operations', None) @property def tags(self): """ :return: Returns tags from config and `JAEGER_TAGS` environment variable to use as process-wide tracer tags """ tags = self.config.get('tags', {}) env_tags = os.environ.get('JAEGER_TAGS', '') if env_tags: for kv in env_tags.split(','): key, value = kv.split('=') tags[key.strip()] = value.strip() return tags @property def propagation(self): propagation = self.config.get('propagation') if propagation == 'b3': # replace the codec with a B3 enabled instance return {Format.HTTP_HEADERS: B3Codec()} return {} def throttler_group(self): return self.config.get('throttler', None) @property def throttler_port(self): throttler_config = self.throttler_group() if throttler_config is None: return None # noinspection PyBroadException try: return int(throttler_config['port']) except: # noqa: E722 return DEFAULT_THROTTLER_PORT @property def throttler_refresh_interval(self): throttler_config = self.throttler_group() if throttler_config is None: return None # noinspection PyBroadException try: return int(throttler_config['refresh_interval']) except: # noqa: E722 return DEFAULT_THROTTLER_REFRESH_INTERVAL @staticmethod def initialized(): with Config._initialized_lock: return Config._initialized def initialize_tracer(self, io_loop=None): """ Initialize Jaeger Tracer based on the passed `jaeger_client.Config`. Save it to `opentracing.tracer` global variable. Only the first call to this method has any effect. """ with Config._initialized_lock: if Config._initialized: logger.warn('Jaeger tracer already initialized, skipping') return Config._initialized = True tracer = self.new_tracer(io_loop) self._initialize_global_tracer(tracer=tracer) return tracer def new_tracer(self, io_loop=None): """ Create a new Jaeger Tracer based on the passed `jaeger_client.Config`. Does not set `opentracing.tracer` global variable. """ channel = self._create_local_agent_channel(io_loop=io_loop) sampler = self.sampler if not sampler: sampler = RemoteControlledSampler( channel=channel, service_name=self.service_name, logger=logger, metrics_factory=self._metrics_factory, error_reporter=self.error_reporter, sampling_refresh_interval=self.sampling_refresh_interval, max_operations=self.max_operations) logger.info('Using sampler %s', sampler) reporter = Reporter( channel=channel, queue_capacity=self.reporter_queue_size, batch_size=self.reporter_batch_size, flush_interval=self.reporter_flush_interval, logger=logger, metrics_factory=self._metrics_factory, error_reporter=self.error_reporter) if self.logging: reporter = CompositeReporter(reporter, LoggingReporter(logger)) if not self.throttler_group() is None: throttler = RemoteThrottler( channel, self.service_name, refresh_interval=self.throttler_refresh_interval, logger=logger, metrics_factory=self._metrics_factory, error_reporter=self.error_reporter) else: throttler = None return self.create_tracer( reporter=reporter, sampler=sampler, throttler=throttler, ) def create_tracer(self, reporter, sampler, throttler=None): return Tracer( service_name=self.service_name, reporter=reporter, sampler=sampler, metrics_factory=self._metrics_factory, trace_id_header=self.trace_id_header, generate_128bit_trace_id=self.generate_128bit_trace_id, baggage_header_prefix=self.baggage_header_prefix, debug_id_header=self.debug_id_header, tags=self.tags, max_tag_value_length=self.max_tag_value_length, extra_codecs=self.propagation, throttler=throttler, scope_manager=self.scope_manager, ) def _initialize_global_tracer(self, tracer): opentracing.tracer = tracer logger.info('opentracing.tracer initialized to %s[app_name=%s]', tracer, self.service_name) def _create_local_agent_channel(self, io_loop): """ Create an out-of-process channel communicating to local jaeger-agent. Spans are submitted as SOCK_DGRAM Thrift, sampling strategy is polled via JSON HTTP. :param self: instance of Config """ logger.info('Initializing Jaeger Tracer with UDP reporter') return LocalAgentSender( host=self.local_agent_reporting_host, sampling_port=self.local_agent_sampling_port, reporting_port=self.local_agent_reporting_port, throttling_port=self.throttler_port, io_loop=io_loop )
apache-2.0
gmittal/aar-nlp-research-2016
src/pygame-pygame-6625feb3fc7f/build/lib.linux-x86_64-2.7/pygame/tests/mixer_music_test.py
16
7728
import sys if __name__ == '__main__': import os pkg_dir = os.path.split(os.path.abspath(__file__))[0] parent_dir, pkg_name = os.path.split(pkg_dir) is_pygame_pkg = (pkg_name == 'tests' and os.path.split(parent_dir)[1] == 'pygame') if not is_pygame_pkg: sys.path.insert(0, parent_dir) else: is_pygame_pkg = __name__.startswith('pygame.tests.') if is_pygame_pkg: from pygame.tests.test_utils \ import test_not_implemented, unittest, example_path else: from test.test_utils \ import test_not_implemented, unittest, example_path import pygame from pygame.compat import as_unicode, filesystem_encode import os class MixerMusicModuleTest(unittest.TestCase): def test_load(self): # __doc__ (as of 2008-07-13) for pygame.mixer_music.load: # pygame.mixer.music.load(filename): return None # Load a music file for playback data_fname = example_path('data') pygame.mixer.init() # The mp3 test file can crash smpeg on some systems. ## formats = ['mp3', 'ogg', 'wav'] formats = ['ogg', 'wav'] for f in formats: path = os.path.join(data_fname, 'house_lo.%s' % f) if os.sep == '\\': path = path.replace('\\', '\\\\') umusfn = as_unicode(path) bmusfn = filesystem_encode(umusfn) pygame.mixer.music.load(umusfn) pygame.mixer.music.load(bmusfn) #NOTE: TODO: loading from filelikes are disabled... # because as of writing it only works in SDL_mixer svn. #pygame.mixer.music.load(open(musfn)) #musf = open(musfn) #pygame.mixer.music.load(musf) pygame.mixer.quit() def todo_test_queue(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.queue: # This will load a music file and queue it. A queued music file will # begin as soon as the current music naturally ends. If the current # music is ever stopped or changed, the queued song will be lost. # # The following example will play music by Bach six times, then play # music by Mozart once: # # pygame.mixer.music.load('bach.ogg') # pygame.mixer.music.play(5) # Plays six times, not five! # pygame.mixer.music.queue('mozart.ogg') self.fail() def todo_test_stop(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.stop: # Stops the music playback if it is currently playing. self.fail() def todo_test_rewind(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.rewind: # Resets playback of the current music to the beginning. self.fail() def todo_test_get_pos(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.get_pos: # This gets the number of milliseconds that the music has been playing # for. The returned time only represents how long the music has been # playing; it does not take into account any starting position # offsets. # self.fail() def todo_test_fadeout(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.fadeout: # This will stop the music playback after it has been faded out over # the specified time (measured in milliseconds). # # Note, that this function blocks until the music has faded out. self.fail() def todo_test_play(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.play: # This will play the loaded music stream. If the music is already # playing it will be restarted. # # The loops argument controls the number of repeats a music will play. # play(5) will cause the music to played once, then repeated five # times, for a total of six. If the loops is -1 then the music will # repeat indefinitely. # # The starting position argument controls where in the music the song # starts playing. The starting position is dependent on the format of # music playing. MP3 and OGG use the position as time (in seconds). # MOD music it is the pattern order number. Passing a startpos will # raise a NotImplementedError if it cannot set the start position # self.fail() def todo_test_load(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.load: # This will load a music file and prepare it for playback. If a music # stream is already playing it will be stopped. This does not start # the music playing. # # Music can only be loaded from filenames, not python file objects # like the other pygame loading functions. # self.fail() def todo_test_get_volume(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.get_volume: # Returns the current volume for the mixer. The value will be between # 0.0 and 1.0. # self.fail() def todo_test_set_endevent(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.set_endevent: # This causes Pygame to signal (by means of the event queue) when the # music is done playing. The argument determines the type of event # that will be queued. # # The event will be queued every time the music finishes, not just the # first time. To stop the event from being queued, call this method # with no argument. # self.fail() def todo_test_pause(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.pause: # Temporarily stop playback of the music stream. It can be resumed # with the pygame.mixer.music.unpause() function. # self.fail() def todo_test_get_busy(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.get_busy: # Returns True when the music stream is actively playing. When the # music is idle this returns False. # self.fail() def todo_test_get_endevent(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.get_endevent: # Returns the event type to be sent every time the music finishes # playback. If there is no endevent the function returns # pygame.NOEVENT. # self.fail() def todo_test_unpause(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.unpause: # This will resume the playback of a music stream after it has been paused. self.fail() def todo_test_set_volume(self): # __doc__ (as of 2008-08-02) for pygame.mixer_music.set_volume: # Set the volume of the music playback. The value argument is between # 0.0 and 1.0. When new music is loaded the volume is reset. # self.fail() def todo_test_set_pos(self): # __doc__ (as of 2010-24-05) for pygame.mixer_music.set_pos: #This sets the position in the music file where playback will start. The # meaning of "pos", a float (or a number that can be converted to a float), # depends on the music format. Newer versions of SDL_mixer have better # positioning support than earlier. An SDLError is raised if a particular # format does not support positioning. # self.fail() if __name__ == '__main__': unittest.main()
mit
mikalstill/nova
nova/tests/unit/api/openstack/compute/test_microversions.py
2
13094
# Copyright 2014 IBM Corp. # # 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 mock from oslo_serialization import jsonutils from nova.api.openstack import api_version_request as api_version from nova import test from nova.tests.unit.api.openstack.compute import microversions from nova.tests.unit.api.openstack import fakes class LegacyMicroversionsTest(test.NoDBTestCase): header_name = 'X-OpenStack-Nova-API-Version' def setUp(self): super(LegacyMicroversionsTest, self).setUp() self.app = fakes.wsgi_app_v21(custom_routes=microversions.ROUTES) def _test_microversions(self, app, req, ret_code, ret_header=None): req.environ['CONTENT_TYPE'] = "application/json" res = req.get_response(app) self.assertEqual(ret_code, res.status_int) if ret_header: if 'nova' not in self.header_name.lower(): ret_header = 'compute %s' % ret_header self.assertEqual(ret_header, res.headers[self.header_name]) return res def _make_header(self, req_header): if 'nova' in self.header_name.lower(): headers = {self.header_name: req_header} else: headers = {self.header_name: 'compute %s' % req_header} return headers def test_microversions_no_header(self): req = fakes.HTTPRequest.blank('/v2/fake/microversions', method='GET') res = req.get_response(self.app) self.assertEqual(200, res.status_int) resp_json = jsonutils.loads(res.body) self.assertEqual('val', resp_json['param']) def test_microversions_return_header(self): req = fakes.HTTPRequest.blank('/v2/fake/microversions') res = req.get_response(self.app) self.assertEqual(200, res.status_int) resp_json = jsonutils.loads(res.body) self.assertEqual('val', resp_json['param']) if 'nova' in self.header_name.lower(): self.assertEqual("2.1", res.headers[self.header_name]) else: self.assertEqual("compute 2.1", res.headers[self.header_name]) self.assertIn(self.header_name, res.headers.getall('Vary')) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def test_microversions_return_header_non_default(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("2.3") req = fakes.HTTPRequest.blank('/v2/fake/microversions') req.headers = self._make_header('2.3') res = req.get_response(self.app) self.assertEqual(200, res.status_int) resp_json = jsonutils.loads(res.body) self.assertEqual('val2', resp_json['param']) if 'nova' in self.header_name.lower(): self.assertEqual("2.3", res.headers[self.header_name]) else: self.assertEqual("compute 2.3", res.headers[self.header_name]) self.assertIn(self.header_name, res.headers.getall('Vary')) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def test_microversions_return_header_fault(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("3.0") req = fakes.HTTPRequest.blank('/v2/fake/microversions') req.headers = self._make_header('3.0') res = req.get_response(self.app) self.assertEqual(400, res.status_int) if 'nova' in self.header_name.lower(): self.assertEqual("3.0", res.headers[self.header_name]) else: self.assertEqual("compute 3.0", res.headers[self.header_name]) self.assertIn(self.header_name, res.headers.getall('Vary')) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def _check_microversion_response(self, url, req_version, resp_param, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest('2.3') req = fakes.HTTPRequest.blank(url) req.headers = self._make_header(req_version) res = req.get_response(self.app) self.assertEqual(200, res.status_int) resp_json = jsonutils.loads(res.body) self.assertEqual(resp_param, resp_json['param']) def test_microversions_with_header(self): self._check_microversion_response('/v2/fake/microversions', '2.3', 'val2') def test_microversions_with_header_exact_match(self): self._check_microversion_response('/v2/fake/microversions', '2.2', 'val2') def test_microversions2_no_2_1_version(self): self._check_microversion_response('/v2/fake/microversions2', '2.3', 'controller2_val1') @mock.patch("nova.api.openstack.api_version_request.max_api_version") def test_microversions2_later_version(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("3.1") req = fakes.HTTPRequest.blank('/v2/fake/microversions2') req.headers = self._make_header('3.0') res = req.get_response(self.app) self.assertEqual(202, res.status_int) resp_json = jsonutils.loads(res.body) self.assertEqual('controller2_val2', resp_json['param']) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def test_microversions2_version_too_high(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("3.5") req = fakes.HTTPRequest.blank('/v2/fake/microversions2') req.headers = {self.header_name: '3.2'} res = req.get_response(self.app) self.assertEqual(404, res.status_int) def test_microversions2_version_too_low(self): req = fakes.HTTPRequest.blank('/v2/fake/microversions2') req.headers = {self.header_name: '2.1'} res = req.get_response(self.app) self.assertEqual(404, res.status_int) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def test_microversions_global_version_too_high(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("3.5") req = fakes.HTTPRequest.blank('/v2/fake/microversions2') req.headers = self._make_header('3.7') res = req.get_response(self.app) self.assertEqual(406, res.status_int) res_json = jsonutils.loads(res.body) self.assertEqual("Version 3.7 is not supported by the API. " "Minimum is 2.1 and maximum is 3.5.", res_json['computeFault']['message']) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def test_microversions_schema(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("3.3") req = fakes.HTTPRequest.blank('/v2/fake/microversions3') req.method = 'POST' req.headers = self._make_header('2.2') req.environ['CONTENT_TYPE'] = "application/json" req.body = jsonutils.dump_as_bytes({'dummy': {'val': 'foo'}}) res = req.get_response(self.app) self.assertEqual(200, res.status_int) resp_json = jsonutils.loads(res.body) self.assertEqual('create_val1', resp_json['param']) if 'nova' in self.header_name.lower(): self.assertEqual("2.2", res.headers[self.header_name]) else: self.assertEqual("compute 2.2", res.headers[self.header_name]) self.assertIn(self.header_name, res.headers.getall('Vary')) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def test_microversions_schema_fail(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("3.3") req = fakes.HTTPRequest.blank('/v2/fake/microversions3') req.method = 'POST' req.headers = {self.header_name: '2.2'} req.environ['CONTENT_TYPE'] = "application/json" req.body = jsonutils.dump_as_bytes({'dummy': {'invalid_param': 'foo'}}) res = req.get_response(self.app) self.assertEqual(400, res.status_int) resp_json = jsonutils.loads(res.body) self.assertTrue(resp_json['badRequest']['message'].startswith( "Invalid input for field/attribute dummy.")) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def test_microversions_schema_out_of_version_check(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("3.3") req = fakes.HTTPRequest.blank('/v2/fake/microversions3/1') req.method = 'PUT' req.headers = self._make_header('2.2') req.body = jsonutils.dump_as_bytes({'dummy': {'inv_val': 'foo'}}) req.environ['CONTENT_TYPE'] = "application/json" res = req.get_response(self.app) self.assertEqual(200, res.status_int) resp_json = jsonutils.loads(res.body) self.assertEqual('update_val1', resp_json['param']) if 'nova' in self.header_name.lower(): self.assertEqual("2.2", res.headers[self.header_name]) else: self.assertEqual("compute 2.2", res.headers[self.header_name]) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def test_microversions_schema_second_version(self, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("3.3") req = fakes.HTTPRequest.blank('/v2/fake/microversions3/1') req.headers = self._make_header('2.10') req.environ['CONTENT_TYPE'] = "application/json" req.method = 'PUT' req.body = jsonutils.dump_as_bytes({'dummy': {'val2': 'foo'}}) res = req.get_response(self.app) self.assertEqual(200, res.status_int) resp_json = jsonutils.loads(res.body) self.assertEqual('update_val1', resp_json['param']) if 'nova' in self.header_name.lower(): self.assertEqual("2.10", res.headers[self.header_name]) else: self.assertEqual("compute 2.10", res.headers[self.header_name]) @mock.patch("nova.api.openstack.api_version_request.max_api_version") def _test_microversions_inner_function(self, version, expected_resp, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("2.2") req = fakes.HTTPRequest.blank('/v2/fake/microversions4') req.headers = self._make_header(version) req.environ['CONTENT_TYPE'] = "application/json" req.method = 'POST' req.body = b'' res = req.get_response(self.app) self.assertEqual(200, res.status_int) resp_json = jsonutils.loads(res.body) self.assertEqual(expected_resp, resp_json['param']) if 'nova' not in self.header_name.lower(): version = 'compute %s' % version self.assertEqual(version, res.headers[self.header_name]) def test_microversions_inner_function_v22(self): self._test_microversions_inner_function('2.2', 'controller4_val2') def test_microversions_inner_function_v21(self): self._test_microversions_inner_function('2.1', 'controller4_val1') @mock.patch("nova.api.openstack.api_version_request.max_api_version") def _test_microversions_actions(self, ret_code, ret_header, req_header, mock_maxver): mock_maxver.return_value = api_version.APIVersionRequest("2.3") req = fakes.HTTPRequest.blank('/v2/fake/microversions3/1/action') if req_header: req.headers = self._make_header(req_header) req.method = 'POST' req.body = jsonutils.dump_as_bytes({'foo': None}) res = self._test_microversions(self.app, req, ret_code, ret_header=ret_header) if ret_code == 202: resp_json = jsonutils.loads(res.body) self.assertEqual({'foo': 'bar'}, resp_json) def test_microversions_actions(self): self._test_microversions_actions(202, "2.1", "2.1") def test_microversions_actions_too_high(self): self._test_microversions_actions(404, "2.3", "2.3") def test_microversions_actions_no_header(self): self._test_microversions_actions(202, "2.1", None) class MicroversionsTest(LegacyMicroversionsTest): header_name = 'OpenStack-API-Version'
apache-2.0
trianam/tests
python/pointInsideConvexHull.py
2
1287
#!/bin/python import numpy as np import scipy as sp import scipy.spatial testInside = [0.2, 0.5, 0.7] testOutside = [3., 3., 3.] points = [[1.,0.,1.], [1.,1.,0], [0.2,0.8,0.2], [0.,1.,1.], [0.4,0.8,0.9], [1.5,0.5,0.5], [0.,0.,0.], [1.,1.,1.], [0.6,0.1,0.7], [2.,0.5,0.5], [0.,1.,0.], [0.2,0.5,0.3], [0.,0.,1.], [1.,0.,0.]] print('Test inside -> ', end='') allPoints = points.copy() allPoints.append(testInside) chull = sp.spatial.ConvexHull(np.array(allPoints)) outside = False for simplex in chull.simplices: if (testInside == chull.points[simplex[0]]).all() or (testInside == chull.points[simplex[1]]).all() or (testInside == chull.points[simplex[2]]).all(): outside = True break if outside: print("Outside") else: print("Inside") print('Test outside -> ', end='') allPoints = points.copy() allPoints.append(testOutside) chull = sp.spatial.ConvexHull(np.array(allPoints)) outside = False for simplex in chull.simplices: if (testOutside == chull.points[simplex[0]]).all() or (testOutside == chull.points[simplex[1]]).all() or (testOutside == chull.points[simplex[2]]).all(): outside = True break if outside: print("Outside") else: print("Inside")
gpl-2.0
GREO/gnuradio-git
gr-wxgui/src/python/powermate.py
17
15356
#!/usr/bin/env python # # Copyright 2005 Free Software Foundation, Inc. # # This file is part of GNU Radio # # GNU Radio is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3, or (at your option) # any later version. # # GNU Radio is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with GNU Radio; see the file COPYING. If not, write to # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # """ Handler for Griffin PowerMate, Contour ShuttlePro & ShuttleXpress USB knobs This is Linux and wxPython specific. """ import os import sys import struct import exceptions import threading import wx from gnuradio import gru imported_ok = True try: import select import fcntl except ImportError: imported_ok = False # First a little bit of background: # # The Griffin PowerMate has # * a single knob which rotates # * a single button (pressing the knob) # # The Contour ShuttleXpress (aka SpaceShuttle) has # * "Jog Wheel" -- the knob (rotary encoder) on the inside # * "Shuttle Ring" -- the spring loaded rubber covered ring # * 5 buttons # # The Contour ShuttlePro has # * "Jog Wheel" -- the knob (rotary encoder) on the inside # * "Shuttle Ring" -- the spring loaded rubber covered ring # * 13 buttons # # The Contour ShuttlePro V2 has # *"Jog Wheel" -- the knob (rotary encoder) on the inside # * "Shuttle Ring" -- the spring loaded rubber covered ring # * 15 buttons # We remap all the buttons on the devices so that they start at zero. # For the ShuttleXpress the buttons are 0 to 4 (left to right) # For the ShuttlePro, we number the buttons immediately above # the ring 0 to 4 (left to right) so that they match our numbering # on the ShuttleXpress. The top row is 5, 6, 7, 8. The first row below # the ring is 9, 10, and the bottom row is 11, 12. # For the ShuttlePro V2, buttons 13 & 14 are to the # left and right of the wheel respectively. # We generate 3 kinds of events: # # button press/release (button_number, press/release) # knob rotation (relative_clicks) # typically -1, +1 # shuttle position (absolute_position) # -7,-6,...,0,...,6,7 # ---------------------------------------------------------------- # Our ID's for the devices: # Not to be confused with anything related to magic hardware numbers. ID_POWERMATE = 'powermate' ID_SHUTTLE_XPRESS = 'shuttle xpress' ID_SHUTTLE_PRO = 'shuttle pro' ID_SHUTTLE_PRO_V2 = 'shuttle pro v2' # ------------------------------------------------------------------------ # format of messages that we read from /dev/input/event* # See /usr/include/linux/input.h for more info # #struct input_event { # struct timeval time; = {long seconds, long microseconds} # unsigned short type; # unsigned short code; # unsigned int value; #}; input_event_struct = "@llHHi" input_event_size = struct.calcsize(input_event_struct) # ------------------------------------------------------------------------ # input_event types # ------------------------------------------------------------------------ IET_SYN = 0x00 # aka RESET IET_KEY = 0x01 # key or button press/release IET_REL = 0x02 # relative movement (knob rotation) IET_ABS = 0x03 # absolute position (graphics pad, etc) IET_MSC = 0x04 IET_LED = 0x11 IET_SND = 0x12 IET_REP = 0x14 IET_FF = 0x15 IET_PWR = 0x16 IET_FF_STATUS = 0x17 IET_MAX = 0x1f # ------------------------------------------------------------------------ # input_event codes (there are a zillion of them, we only define a few) # ------------------------------------------------------------------------ # these are valid for IET_KEY IEC_BTN_0 = 0x100 IEC_BTN_1 = 0x101 IEC_BTN_2 = 0x102 IEC_BTN_3 = 0x103 IEC_BTN_4 = 0x104 IEC_BTN_5 = 0x105 IEC_BTN_6 = 0x106 IEC_BTN_7 = 0x107 IEC_BTN_8 = 0x108 IEC_BTN_9 = 0x109 IEC_BTN_10 = 0x10a IEC_BTN_11 = 0x10b IEC_BTN_12 = 0x10c IEC_BTN_13 = 0x10d IEC_BTN_14 = 0x10e IEC_BTN_15 = 0x10f # these are valid for IET_REL (Relative axes) IEC_REL_X = 0x00 IEC_REL_Y = 0x01 IEC_REL_Z = 0x02 IEC_REL_HWHEEL = 0x06 IEC_REL_DIAL = 0x07 # rotating the knob IEC_REL_WHEEL = 0x08 # moving the shuttle ring IEC_REL_MISC = 0x09 IEC_REL_MAX = 0x0f # ------------------------------------------------------------------------ class powermate(threading.Thread): """ Interface to Griffin PowerMate and Contour Shuttles """ def __init__(self, event_receiver=None, filename=None, **kwargs): self.event_receiver = event_receiver self.handle = -1 if not imported_ok: raise exceptions.RuntimeError, 'powermate not supported on this platform' if filename: if not self._open_device(filename): raise exceptions.RuntimeError, 'Unable to find powermate' else: ok = False for d in range(0, 16): if self._open_device("/dev/input/event%d" % d): ok = True break if not ok: raise exceptions.RuntimeError, 'Unable to find powermate' threading.Thread.__init__(self, **kwargs) self.setDaemon (1) self.keep_running = True self.start () def __del__(self): self.keep_running = False if self.handle >= 0: os.close(self.handle) self.handle = -1 def _open_device(self, filename): try: self.handle = os.open(filename, os.O_RDWR) if self.handle < 0: return False # read event device name name = fcntl.ioctl(self.handle, gru.hexint(0x80ff4506), chr(0) * 256) name = name.replace(chr(0), '') # do we see anything we recognize? if name == 'Griffin PowerMate' or name == 'Griffin SoundKnob': self.id = ID_POWERMATE self.mapper = _powermate_remapper() elif name == 'CAVS SpaceShuttle A/V' or name == 'Contour Design ShuttleXpress': self.id = ID_SHUTTLE_XPRESS self.mapper = _contour_remapper() elif name == 'Contour Design ShuttlePRO': self.id = ID_SHUTTLE_PRO self.mapper = _contour_remapper() elif name == 'Contour Design ShuttlePRO v2': self.id = ID_SHUTTLE_PRO_V2 self.mapper = _contour_remapper() else: os.close(self.handle) self.handle = -1 return False # get exclusive control of the device, using ioctl EVIOCGRAB # there may be an issue with this on non x86 platforms and if # the _IOW,_IOC,... macros in <asm/ioctl.h> are changed fcntl.ioctl(self.handle,gru.hexint(0x40044590), 1) return True except exceptions.OSError: return False def set_event_receiver(self, obj): self.event_receiver = obj def set_led_state(self, static_brightness, pulse_speed=0, pulse_table=0, pulse_on_sleep=0, pulse_on_wake=0): """ What do these magic values mean... """ if self.id != ID_POWERMATE: return False static_brightness &= 0xff; if pulse_speed < 0: pulse_speed = 0 if pulse_speed > 510: pulse_speed = 510 if pulse_table < 0: pulse_table = 0 if pulse_table > 2: pulse_table = 2 pulse_on_sleep = not not pulse_on_sleep # not not = convert to 0/1 pulse_on_wake = not not pulse_on_wake magic = (static_brightness | (pulse_speed << 8) | (pulse_table << 17) | (pulse_on_sleep << 19) | (pulse_on_wake << 20)) data = struct.pack(input_event_struct, 0, 0, 0x04, 0x01, magic) os.write(self.handle, data) return True def run (self): while (self.keep_running): s = os.read (self.handle, input_event_size) if not s: self.keep_running = False break raw_input_event = struct.unpack(input_event_struct,s) sec, usec, type, code, val = self.mapper(raw_input_event) if self.event_receiver is None: continue if type == IET_SYN: # ignore pass elif type == IET_MSC: # ignore (seems to be PowerMate reporting led brightness) pass elif type == IET_REL and code == IEC_REL_DIAL: #print "Dial: %d" % (val,) wx.PostEvent(self.event_receiver, PMRotateEvent(val)) elif type == IET_REL and code == IEC_REL_WHEEL: #print "Shuttle: %d" % (val,) wx.PostEvent(self.event_receiver, PMShuttleEvent(val)) elif type == IET_KEY: #print "Key: Btn%d %d" % (code - IEC_BTN_0, val) wx.PostEvent(self.event_receiver, PMButtonEvent(code - IEC_BTN_0, val)) else: print "powermate: unrecognized event: type = 0x%x code = 0x%x val = %d" % (type, code, val) class _powermate_remapper(object): def __init__(self): pass def __call__(self, event): """ Notice how nice and simple this is... """ return event class _contour_remapper(object): def __init__(self): self.prev = None def __call__(self, event): """ ...and how screwed up this is """ sec, usec, type, code, val = event if type == IET_REL and code == IEC_REL_WHEEL: # === Shuttle ring === # First off, this really ought to be IET_ABS, not IET_REL! # They never generate a zero value so you can't # tell when the shuttle ring is back in the center. # We kludge around this by calling both -1 and 1 zero. if val == -1 or val == 1: return (sec, usec, type, code, 0) return event if type == IET_REL and code == IEC_REL_DIAL: # === Jog knob (rotary encoder) === # Dim wits got it wrong again! This one should return a # a relative value, e.g., -1, +1. Instead they return # a total that runs modulo 256 (almost!). For some # reason they count like this 253, 254, 255, 1, 2, 3 if self.prev is None: # first time call self.prev = val return (sec, usec, IET_SYN, 0, 0) # will be ignored above diff = val - self.prev if diff == 0: # sometimes it just sends stuff... return (sec, usec, IET_SYN, 0, 0) # will be ignored above if abs(diff) > 100: # crossed into the twilight zone if self.prev > val: # we've wrapped going forward self.prev = val return (sec, usec, type, code, +1) else: # we've wrapped going backward self.prev = val return (sec, usec, type, code, -1) self.prev = val return (sec, usec, type, code, diff) if type == IET_KEY: # remap keys so that all 3 gadgets have buttons 0 to 4 in common return (sec, usec, type, (IEC_BTN_5, IEC_BTN_6, IEC_BTN_7, IEC_BTN_8, IEC_BTN_0, IEC_BTN_1, IEC_BTN_2, IEC_BTN_3, IEC_BTN_4, IEC_BTN_9, IEC_BTN_10, IEC_BTN_11, IEC_BTN_12, IEC_BTN_13, IEC_BTN_14)[code - IEC_BTN_0], val) return event # ------------------------------------------------------------------------ # new wxPython event classes # ------------------------------------------------------------------------ grEVT_POWERMATE_BUTTON = wx.NewEventType() grEVT_POWERMATE_ROTATE = wx.NewEventType() grEVT_POWERMATE_SHUTTLE = wx.NewEventType() EVT_POWERMATE_BUTTON = wx.PyEventBinder(grEVT_POWERMATE_BUTTON, 0) EVT_POWERMATE_ROTATE = wx.PyEventBinder(grEVT_POWERMATE_ROTATE, 0) EVT_POWERMATE_SHUTTLE = wx.PyEventBinder(grEVT_POWERMATE_SHUTTLE, 0) class PMButtonEvent(wx.PyEvent): def __init__(self, button, value): wx.PyEvent.__init__(self) self.SetEventType(grEVT_POWERMATE_BUTTON) self.button = button self.value = value def Clone (self): self.__class__(self.GetId()) class PMRotateEvent(wx.PyEvent): def __init__(self, delta): wx.PyEvent.__init__(self) self.SetEventType (grEVT_POWERMATE_ROTATE) self.delta = delta def Clone (self): self.__class__(self.GetId()) class PMShuttleEvent(wx.PyEvent): def __init__(self, position): wx.PyEvent.__init__(self) self.SetEventType (grEVT_POWERMATE_SHUTTLE) self.position = position def Clone (self): self.__class__(self.GetId()) # ------------------------------------------------------------------------ # Example usage # ------------------------------------------------------------------------ if __name__ == '__main__': class Frame(wx.Frame): def __init__(self,parent=None,id=-1,title='Title', pos=wx.DefaultPosition, size=(400,200)): wx.Frame.__init__(self,parent,id,title,pos,size) EVT_POWERMATE_BUTTON(self, self.on_button) EVT_POWERMATE_ROTATE(self, self.on_rotate) EVT_POWERMATE_SHUTTLE(self, self.on_shuttle) self.brightness = 128 self.pulse_speed = 0 try: self.pm = powermate(self) except: sys.stderr.write("Unable to find PowerMate or Contour Shuttle\n") sys.exit(1) self.pm.set_led_state(self.brightness, self.pulse_speed) def on_button(self, evt): print "Button %d %s" % (evt.button, ("Released", "Pressed")[evt.value]) def on_rotate(self, evt): print "Rotated %d" % (evt.delta,) if 0: new = max(0, min(255, self.brightness + evt.delta)) if new != self.brightness: self.brightness = new self.pm.set_led_state(self.brightness, self.pulse_speed) def on_shuttle(self, evt): print "Shuttle %d" % (evt.position,) class App(wx.App): def OnInit(self): title='PowerMate Demo' self.frame = Frame(parent=None,id=-1,title=title) self.frame.Show() self.SetTopWindow(self.frame) return True app = App() app.MainLoop ()
gpl-3.0
benpatterson/edx-platform
common/test/acceptance/pages/lms/open_response.py
165
4579
""" Open-ended response in the courseware. """ from bok_choy.page_object import PageObject from bok_choy.promise import EmptyPromise from .rubric import RubricPage class OpenResponsePage(PageObject): """ Open-ended response in the courseware. """ url = None def is_browser_on_page(self): return self.q(css='div.xmodule_CombinedOpenEndedModule').present @property def assessment_type(self): """ Return the type of assessment currently active. Options are "self", "ai", or "peer" """ labels = self.q(css='section#combined-open-ended-status>div.statusitem-current').text if len(labels) < 1: self.warning("Could not find assessment type label") # Provide some tolerance to UI changes label_compare = labels[0].lower().strip() if 'self' in label_compare: return 'self' elif 'ai' in label_compare: return 'ai' elif 'peer' in label_compare: return 'peer' else: raise ValueError("Unexpected assessment type: '{0}'".format(label_compare)) @property def prompt(self): """ Return an HTML string representing the essay prompt. """ prompt_css = "section.open-ended-child>div.prompt" prompts = self.q(css=prompt_css).map(lambda el: el.get_attribute('innerHTML').strip()).results if len(prompts) == 0: self.warning("Could not find essay prompt on page.") return "" elif len(prompts) > 1: self.warning("Multiple essay prompts found on page; using the first one.") return prompts[0] @property def rubric(self): """ Return a `RubricPage` for a self-assessment problem. If no rubric is available, raises a `BrokenPromise` exception. """ rubric = RubricPage(self.browser) rubric.wait_for_page() return rubric @property def written_feedback(self): """ Return the written feedback from the grader (if any). If no feedback available, returns None. """ feedback = self.q(css='div.written-feedback').text if len(feedback) > 0: return feedback[0] else: return None @property def alert_message(self): """ Alert message displayed to the user. """ alerts = self.q(css="div.open-ended-alert").text if len(alerts) < 1: return "" else: return alerts[0] @property def grader_status(self): """ Status message from the grader. If not present, return an empty string. """ status_list = self.q(css='div.grader-status').text if len(status_list) < 1: self.warning("No grader status found") return "" elif len(status_list) > 1: self.warning("Multiple grader statuses found; returning the first one") return status_list[0] def set_response(self, response_str): """ Input a response to the prompt. """ input_css = "textarea.short-form-response" self.q(css=input_css).fill(response_str) def save_response(self): """ Save the response for later submission. """ self.q(css='input.save-button').first.click() EmptyPromise( lambda: 'save' in self.alert_message.lower(), "Status message saved" ).fulfill() def submit_response(self): """ Submit a response for grading. """ self.q(css='input.submit-button').first.click() # modal dialog confirmation self.q(css='button.ok-button').first.click() # Ensure that the submission completes self._wait_for_submitted(self.assessment_type) def _wait_for_submitted(self, assessment_type): """ Wait for the submission to complete. `assessment_type` is either 'self', 'ai', or 'peer' """ if assessment_type == 'self': RubricPage(self.browser).wait_for_page() elif assessment_type == 'ai' or assessment_type == "peer": EmptyPromise( lambda: self.grader_status != 'Unanswered', "Problem status is no longer 'unanswered'" ).fulfill() else: self.warning("Unrecognized assessment type '{0}'".format(assessment_type)) EmptyPromise(lambda: True, "Unrecognized assessment type").fulfill()
agpl-3.0
sinecoin/sinecoin
contrib/pyminer/pyminer.py
41
6434
#!/usr/bin/python # # Copyright (c) 2011 The Bitcoin developers # Distributed under the MIT/X11 software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. # import time import json import pprint import hashlib import struct import re import base64 import httplib import sys from multiprocessing import Process ERR_SLEEP = 15 MAX_NONCE = 1000000L settings = {} pp = pprint.PrettyPrinter(indent=4) class BitcoinRPC: OBJID = 1 def __init__(self, host, port, username, password): authpair = "%s:%s" % (username, password) self.authhdr = "Basic %s" % (base64.b64encode(authpair)) self.conn = httplib.HTTPConnection(host, port, False, 30) def rpc(self, method, params=None): self.OBJID += 1 obj = { 'version' : '1.1', 'method' : method, 'id' : self.OBJID } if params is None: obj['params'] = [] else: obj['params'] = params self.conn.request('POST', '/', json.dumps(obj), { 'Authorization' : self.authhdr, 'Content-type' : 'application/json' }) resp = self.conn.getresponse() if resp is None: print "JSON-RPC: no response" return None body = resp.read() resp_obj = json.loads(body) if resp_obj is None: print "JSON-RPC: cannot JSON-decode body" return None if 'error' in resp_obj and resp_obj['error'] != None: return resp_obj['error'] if 'result' not in resp_obj: print "JSON-RPC: no result in object" return None return resp_obj['result'] def getblockcount(self): return self.rpc('getblockcount') def getwork(self, data=None): return self.rpc('getwork', data) def uint32(x): return x & 0xffffffffL def bytereverse(x): return uint32(( ((x) << 24) | (((x) << 8) & 0x00ff0000) | (((x) >> 8) & 0x0000ff00) | ((x) >> 24) )) def bufreverse(in_buf): out_words = [] for i in range(0, len(in_buf), 4): word = struct.unpack('@I', in_buf[i:i+4])[0] out_words.append(struct.pack('@I', bytereverse(word))) return ''.join(out_words) def wordreverse(in_buf): out_words = [] for i in range(0, len(in_buf), 4): out_words.append(in_buf[i:i+4]) out_words.reverse() return ''.join(out_words) class Miner: def __init__(self, id): self.id = id self.max_nonce = MAX_NONCE def work(self, datastr, targetstr): # decode work data hex string to binary static_data = datastr.decode('hex') static_data = bufreverse(static_data) # the first 76b of 80b do not change blk_hdr = static_data[:76] # decode 256-bit target value targetbin = targetstr.decode('hex') targetbin = targetbin[::-1] # byte-swap and dword-swap targetbin_str = targetbin.encode('hex') target = long(targetbin_str, 16) # pre-hash first 76b of block header static_hash = hashlib.sha256() static_hash.update(blk_hdr) for nonce in xrange(self.max_nonce): # encode 32-bit nonce value nonce_bin = struct.pack("<I", nonce) # hash final 4b, the nonce value hash1_o = static_hash.copy() hash1_o.update(nonce_bin) hash1 = hash1_o.digest() # sha256 hash of sha256 hash hash_o = hashlib.sha256() hash_o.update(hash1) hash = hash_o.digest() # quick test for winning solution: high 32 bits zero? if hash[-4:] != '\0\0\0\0': continue # convert binary hash to 256-bit Python long hash = bufreverse(hash) hash = wordreverse(hash) hash_str = hash.encode('hex') l = long(hash_str, 16) # proof-of-work test: hash < target if l < target: print time.asctime(), "PROOF-OF-WORK found: %064x" % (l,) return (nonce + 1, nonce_bin) else: print time.asctime(), "PROOF-OF-WORK false positive %064x" % (l,) # return (nonce + 1, nonce_bin) return (nonce + 1, None) def submit_work(self, rpc, original_data, nonce_bin): nonce_bin = bufreverse(nonce_bin) nonce = nonce_bin.encode('hex') solution = original_data[:152] + nonce + original_data[160:256] param_arr = [ solution ] result = rpc.getwork(param_arr) print time.asctime(), "--> Upstream RPC result:", result def iterate(self, rpc): work = rpc.getwork() if work is None: time.sleep(ERR_SLEEP) return if 'data' not in work or 'target' not in work: time.sleep(ERR_SLEEP) return time_start = time.time() (hashes_done, nonce_bin) = self.work(work['data'], work['target']) time_end = time.time() time_diff = time_end - time_start self.max_nonce = long( (hashes_done * settings['scantime']) / time_diff) if self.max_nonce > 0xfffffffaL: self.max_nonce = 0xfffffffaL if settings['hashmeter']: print "HashMeter(%d): %d hashes, %.2f Khash/sec" % ( self.id, hashes_done, (hashes_done / 1000.0) / time_diff) if nonce_bin is not None: self.submit_work(rpc, work['data'], nonce_bin) def loop(self): rpc = BitcoinRPC(settings['host'], settings['port'], settings['rpcuser'], settings['rpcpass']) if rpc is None: return while True: self.iterate(rpc) def miner_thread(id): miner = Miner(id) miner.loop() if __name__ == '__main__': if len(sys.argv) != 2: print "Usage: pyminer.py CONFIG-FILE" sys.exit(1) f = open(sys.argv[1]) for line in f: # skip comment lines m = re.search('^\s*#', line) if m: continue # parse key=value lines m = re.search('^(\w+)\s*=\s*(\S.*)$', line) if m is None: continue settings[m.group(1)] = m.group(2) f.close() if 'host' not in settings: settings['host'] = '127.0.0.1' if 'port' not in settings: settings['port'] = 5888 if 'threads' not in settings: settings['threads'] = 1 if 'hashmeter' not in settings: settings['hashmeter'] = 0 if 'scantime' not in settings: settings['scantime'] = 30L if 'rpcuser' not in settings or 'rpcpass' not in settings: print "Missing username and/or password in cfg file" sys.exit(1) settings['port'] = int(settings['port']) settings['threads'] = int(settings['threads']) settings['hashmeter'] = int(settings['hashmeter']) settings['scantime'] = long(settings['scantime']) thr_list = [] for thr_id in range(settings['threads']): p = Process(target=miner_thread, args=(thr_id,)) p.start() thr_list.append(p) time.sleep(1) # stagger threads print settings['threads'], "mining threads started" print time.asctime(), "Miner Starts - %s:%s" % (settings['host'], settings['port']) try: for thr_proc in thr_list: thr_proc.join() except KeyboardInterrupt: pass print time.asctime(), "Miner Stops - %s:%s" % (settings['host'], settings['port'])
mit
yoyokko/syncless
benchmark/speed_concurrence.py
5
1985
#! /usr/local/bin/stackless2.6 # by pts@fazekas.hu at Thu Jan 7 14:34:06 CET 2010 import sys import socket import lprng from concurrence import dispatch, Tasklet from concurrence.io import BufferedStream, Socket def handler(client_socket): print >>sys.stderr, 'info: connection from %r' % ( client_socket.socket.getpeername(),) stream = BufferedStream(client_socket) reader = stream.reader # Strips \r\n and \n from the end. writer = stream.writer # Read HTTP request. line1 = None try: while True: line = reader.read_line() if not line: # Empty line, end of HTTP request. break if line1 is None: line1 = line except EOFError: pass # Parse HTTP request. # Please note that an assertion here doesn't abort the server. items = line1.split(' ') assert 3 == len(items) assert items[2] in ('HTTP/1.0', 'HTTP/1.1') assert items[0] == 'GET' assert items[1].startswith('/') try: num = int(items[1][1:]) except ValueError: num = None # Write HTTP response. if num is None: writer.write_bytes('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') writer.write_bytes('<a href="/0">start at 0</a><p>Hello, World!\n') else: next_num = lprng.Lprng(num).next() writer.write_bytes('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') writer.write_bytes('<a href="/%d">continue with %d</a>\n' % (next_num, next_num)) writer.flush() stream.close() def server(): server_socket = Socket.new() server_socket.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind(('127.0.0.1', 8080)) server_socket.listen(128) print >>sys.stderr, 'info: listening on: %r' % ( server_socket.socket.getsockname(),) while True: client_socket = server_socket.accept() Tasklet.new(handler)(client_socket) if __name__ == '__main__': assert ('stackless' in sys.modules) != ('greenlet' in sys.modules) dispatch(server)
apache-2.0
sightmachine/LightGBM
tests/c_api_test/test.py
4
7619
# coding: utf-8 # pylint: skip-file import ctypes import os import numpy as np from scipy import sparse def LoadDll(): if os.name == 'nt': lib_path = '../../windows/x64/DLL/lib_lightgbm.dll' else: lib_path = '../../lib_lightgbm.so' lib = ctypes.cdll.LoadLibrary(lib_path) return lib LIB = LoadDll() LIB.LGBM_GetLastError.restype = ctypes.c_char_p dtype_float32 = 0 dtype_float64 = 1 dtype_int32 = 2 dtype_int64 = 3 def c_array(ctype, values): return (ctype * len(values))(*values) def c_str(string): return ctypes.c_char_p(string.encode('ascii')) def test_load_from_file(filename, reference): ref = None if reference is not None: ref = reference handle = ctypes.c_void_p() LIB.LGBM_DatasetCreateFromFile( c_str(filename), c_str('max_bin=15'), ref, ctypes.byref(handle)) print(LIB.LGBM_GetLastError()) num_data = ctypes.c_long() LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data)) num_feature = ctypes.c_long() LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature)) print('#data:%d #feature:%d' % (num_data.value, num_feature.value)) return handle def test_save_to_binary(handle, filename): LIB.LGBM_DatasetSaveBinary(handle, c_str(filename)) def test_load_from_csr(filename, reference): data = [] label = [] inp = open(filename, 'r') for line in inp.readlines(): data.append([float(x) for x in line.split('\t')[1:]]) label.append(float(line.split('\t')[0])) inp.close() mat = np.array(data) label = np.array(label, dtype=np.float32) csr = sparse.csr_matrix(mat) handle = ctypes.c_void_p() ref = None if reference is not None: ref = reference LIB.LGBM_DatasetCreateFromCSR( c_array(ctypes.c_int, csr.indptr), dtype_int32, c_array(ctypes.c_int, csr.indices), csr.data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)), dtype_float64, len(csr.indptr), len(csr.data), csr.shape[1], c_str('max_bin=15'), ref, ctypes.byref(handle)) num_data = ctypes.c_long() LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data)) num_feature = ctypes.c_long() LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature)) LIB.LGBM_DatasetSetField(handle, c_str('label'), c_array(ctypes.c_float, label), len(label), 0) print('#data:%d #feature:%d' % (num_data.value, num_feature.value)) return handle def test_load_from_csc(filename, reference): data = [] label = [] inp = open(filename, 'r') for line in inp.readlines(): data.append([float(x) for x in line.split('\t')[1:]]) label.append(float(line.split('\t')[0])) inp.close() mat = np.array(data) label = np.array(label, dtype=np.float32) csr = sparse.csc_matrix(mat) handle = ctypes.c_void_p() ref = None if reference is not None: ref = reference LIB.LGBM_DatasetCreateFromCSC( c_array(ctypes.c_int, csr.indptr), dtype_int32, c_array(ctypes.c_int, csr.indices), csr.data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)), dtype_float64, len(csr.indptr), len(csr.data), csr.shape[0], c_str('max_bin=15'), ref, ctypes.byref(handle)) num_data = ctypes.c_long() LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data)) num_feature = ctypes.c_long() LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature)) LIB.LGBM_DatasetSetField(handle, c_str('label'), c_array(ctypes.c_float, label), len(label), 0) print('#data:%d #feature:%d' % (num_data.value, num_feature.value)) return handle def test_load_from_mat(filename, reference): data = [] label = [] inp = open(filename, 'r') for line in inp.readlines(): data.append([float(x) for x in line.split('\t')[1:]]) label.append(float(line.split('\t')[0])) inp.close() mat = np.array(data) data = np.array(mat.reshape(mat.size), copy=False) label = np.array(label, dtype=np.float32) handle = ctypes.c_void_p() ref = None if reference is not None: ref = reference LIB.LGBM_DatasetCreateFromMat(data.ctypes.data_as( ctypes.POINTER(ctypes.c_void_p)), dtype_float64, mat.shape[0], mat.shape[1], 1, c_str('max_bin=15'), ref, ctypes.byref(handle)) num_data = ctypes.c_long() LIB.LGBM_DatasetGetNumData(handle, ctypes.byref(num_data)) num_feature = ctypes.c_long() LIB.LGBM_DatasetGetNumFeature(handle, ctypes.byref(num_feature)) LIB.LGBM_DatasetSetField(handle, c_str('label'), c_array(ctypes.c_float, label), len(label), 0) print('#data:%d #feature:%d' % (num_data.value, num_feature.value)) return handle def test_free_dataset(handle): LIB.LGBM_DatasetFree(handle) def test_dataset(): train = test_load_from_file('../../examples/binary_classification/binary.train', None) test = test_load_from_mat('../../examples/binary_classification/binary.test', train) test_free_dataset(test) test = test_load_from_csr('../../examples/binary_classification/binary.test', train) test_free_dataset(test) test = test_load_from_csc('../../examples/binary_classification/binary.test', train) test_free_dataset(test) test_save_to_binary(train, 'train.binary.bin') test_free_dataset(train) train = test_load_from_file('train.binary.bin', None) test_free_dataset(train) def test_booster(): train = test_load_from_mat('../../examples/binary_classification/binary.train', None) test = test_load_from_mat('../../examples/binary_classification/binary.test', train) booster = ctypes.c_void_p() LIB.LGBM_BoosterCreate(train, c_str("app=binary metric=auc num_leaves=31 verbose=0"), ctypes.byref(booster)) LIB.LGBM_BoosterAddValidData(booster, test) is_finished = ctypes.c_int(0) for i in range(1, 101): LIB.LGBM_BoosterUpdateOneIter(booster, ctypes.byref(is_finished)) result = np.array([0.0], dtype=np.float64) out_len = ctypes.c_ulong(0) LIB.LGBM_BoosterGetEval(booster, 0, ctypes.byref(out_len), result.ctypes.data_as(ctypes.POINTER(ctypes.c_double))) if i % 10 == 0: print('%d Iteration test AUC %f' % (i, result[0])) LIB.LGBM_BoosterSaveModel(booster, -1, c_str('model.txt')) LIB.LGBM_BoosterFree(booster) test_free_dataset(train) test_free_dataset(test) booster2 = ctypes.c_void_p() num_total_model = ctypes.c_long() LIB.LGBM_BoosterCreateFromModelfile(c_str('model.txt'), ctypes.byref(num_total_model), ctypes.byref(booster2)) data = [] inp = open('../../examples/binary_classification/binary.test', 'r') for line in inp.readlines(): data.append([float(x) for x in line.split('\t')[1:]]) inp.close() mat = np.array(data) preb = np.zeros(mat.shape[0], dtype=np.float64) num_preb = ctypes.c_long() data = np.array(mat.reshape(mat.size), copy=False) LIB.LGBM_BoosterPredictForMat( booster2, data.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p)), dtype_float64, mat.shape[0], mat.shape[1], 1, 1, 50, ctypes.byref(num_preb), preb.ctypes.data_as(ctypes.POINTER(ctypes.c_double))) LIB.LGBM_BoosterPredictForFile(booster2, c_str('../../examples/binary_classification/binary.test'), 0, 0, 50, c_str('preb.txt')) LIB.LGBM_BoosterFree(booster2) test_dataset() test_booster()
mit
edivancamargo/three.js
utils/exporters/blender/addons/io_three/exporter/utilities.py
225
1229
import uuid import hashlib from .. import constants ROUND = constants.DEFAULT_PRECISION def bit_mask(flags): """Generate a bit mask. :type flags: dict :return: int """ bit = 0 true = lambda x, y: (x | (1 << y)) false = lambda x, y: (x & (~(1 << y))) for mask, position in constants.MASK.items(): func = true if flags.get(mask) else false bit = func(bit, position) return bit def hash(value): """Generate a hash from a given value :param value: :rtype: str """ hash_ = hashlib.md5() hash_.update(repr(value).encode('utf8')) return hash_.hexdigest() def id(): """Generate a random UUID :rtype: str """ return str(uuid.uuid4()).upper() def id_from_name(name): """Generate a UUID using a name as the namespace :type name: str :rtype: str """ return str(uuid.uuid3(uuid.NAMESPACE_DNS, name)).upper() def rgb2int(rgb): """Convert a given rgb value to an integer :type rgb: list|tuple :rtype: int """ is_tuple = isinstance(rgb, tuple) rgb = list(rgb) if is_tuple else rgb colour = (int(rgb[0]*255) << 16) + (int(rgb[1]*255) << 8) + int(rgb[2]*255) return colour
mit
mick-t/RSyncBackup
examples/backup.py
1
3698
#!/usr/bin/python # # Copyright (c) 2004 Colin Stewart (http://www.owlfish.com/) # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # # EXAMPLE SCRIPT - Modify to suit # # Script to backup home directories and MySQL databases to a spare hard-drive # # Will be run every hour by cron - it will then determine whether a backup has been # done in the last 24 hours. # import RSyncBackup import logging, logging.handlers LOG_FILE="/var/log/backup.log" LAST_RUN_FILE="/var/state/backup.lrf" # Logging to a file done here rootLogger = logging.getLogger() loggingHandler = logging.FileHandler (LOG_FILE) loggingFormatter = logging.Formatter ('%(asctime)s %(levelname)s %(name)s %(message)s') loggingHandler.setFormatter (loggingFormatter) rootLogger.setLevel (logging.DEBUG) rootLogger.addHandler (loggingHandler) # Logging to email of any errors emailHandler = logging.handlers.SMTPHandler ("localhost", "backup@rock", ["root@rock"], "Backup error.") emailHandler.setFormatter (loggingFormatter) emailHandler.setLevel (logging.ERROR) rootLogger.addHandler (emailHandler) # Create a backup object. Remove testRun once you've debugged it. backup = RSyncBackup.RSyncBackup (lastRunFile = LAST_RUN_FILE, rsync="/usr/bin/rsync", testRun=1) try: if (backup.timeToBackup()): # It's time to perform a backup. # Exclude the media directory - it's too large to backup. # Backup all the home directories to /backup/current/ with archives to /backup/archives/ exclude = ['colin/media'] backup.backup (source="/home/", destination="/backup/current/", archive="/backup/archives/", excludeList=exclude) # Backup MySQL with no archives backup.backup (source="/var/lib/mysql", destination="/backup/mysql/") # Only keep 5 days worth of evolution archives - it changes too rapidly and is big! # This demonstrates the use of the filter regular expression - use with great care! backup.trimArchives ('/backup/archives', filter="evolution$", entriesToKeep=5) # Only keep 60 backups worth of archives for all files backup.trimArchives ('/backup/archives', entriesToKeep=60) # Backup finished backup.finish() except Exception, e: logging.error ("Exception occured during backup: %s" % str (e)) # Close the logging out. loggingHandler.close()
bsd-3-clause
rabipanda/tensorflow
tensorflow/python/framework/dtypes_test.py
15
14349
# Copyright 2015 The TensorFlow Authors. 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. # ============================================================================== """Tests for tensorflow.python.framework.dtypes.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import numpy as np from tensorflow.core.framework import types_pb2 from tensorflow.python.framework import dtypes from tensorflow.python.framework import test_util from tensorflow.python.platform import googletest def _is_numeric_dtype_enum(datatype_enum): non_numeric_dtypes = [types_pb2.DT_VARIANT, types_pb2.DT_VARIANT_REF, types_pb2.DT_INVALID, types_pb2.DT_RESOURCE, types_pb2.DT_RESOURCE_REF] return datatype_enum not in non_numeric_dtypes class TypesTest(test_util.TensorFlowTestCase): def testAllTypesConstructible(self): for datatype_enum in types_pb2.DataType.values(): if datatype_enum == types_pb2.DT_INVALID: continue self.assertEqual(datatype_enum, dtypes.DType(datatype_enum).as_datatype_enum) def testAllTypesConvertibleToDType(self): for datatype_enum in types_pb2.DataType.values(): if datatype_enum == types_pb2.DT_INVALID: continue dt = dtypes.as_dtype(datatype_enum) self.assertEqual(datatype_enum, dt.as_datatype_enum) def testAllTypesConvertibleToNumpyDtype(self): for datatype_enum in types_pb2.DataType.values(): if not _is_numeric_dtype_enum(datatype_enum): continue dtype = dtypes.as_dtype(datatype_enum) numpy_dtype = dtype.as_numpy_dtype _ = np.empty((1, 1, 1, 1), dtype=numpy_dtype) if dtype.base_dtype != dtypes.bfloat16: # NOTE(touts): Intentionally no way to feed a DT_BFLOAT16. self.assertEqual( dtypes.as_dtype(datatype_enum).base_dtype, dtypes.as_dtype(numpy_dtype)) def testInvalid(self): with self.assertRaises(TypeError): dtypes.DType(types_pb2.DT_INVALID) with self.assertRaises(TypeError): dtypes.as_dtype(types_pb2.DT_INVALID) def testNumpyConversion(self): self.assertIs(dtypes.float32, dtypes.as_dtype(np.float32)) self.assertIs(dtypes.float64, dtypes.as_dtype(np.float64)) self.assertIs(dtypes.int32, dtypes.as_dtype(np.int32)) self.assertIs(dtypes.int64, dtypes.as_dtype(np.int64)) self.assertIs(dtypes.uint8, dtypes.as_dtype(np.uint8)) self.assertIs(dtypes.uint16, dtypes.as_dtype(np.uint16)) self.assertIs(dtypes.int16, dtypes.as_dtype(np.int16)) self.assertIs(dtypes.int8, dtypes.as_dtype(np.int8)) self.assertIs(dtypes.complex64, dtypes.as_dtype(np.complex64)) self.assertIs(dtypes.complex128, dtypes.as_dtype(np.complex128)) self.assertIs(dtypes.string, dtypes.as_dtype(np.object)) self.assertIs(dtypes.string, dtypes.as_dtype(np.array(["foo", "bar"]).dtype)) self.assertIs(dtypes.bool, dtypes.as_dtype(np.bool)) with self.assertRaises(TypeError): dtypes.as_dtype(np.dtype([("f1", np.uint), ("f2", np.int32)])) def testRealDtype(self): for dtype in [ dtypes.float32, dtypes.float64, dtypes.bool, dtypes.uint8, dtypes.int8, dtypes.int16, dtypes.int32, dtypes.int64 ]: self.assertIs(dtype.real_dtype, dtype) self.assertIs(dtypes.complex64.real_dtype, dtypes.float32) self.assertIs(dtypes.complex128.real_dtype, dtypes.float64) def testStringConversion(self): self.assertIs(dtypes.float32, dtypes.as_dtype("float32")) self.assertIs(dtypes.float64, dtypes.as_dtype("float64")) self.assertIs(dtypes.int32, dtypes.as_dtype("int32")) self.assertIs(dtypes.uint8, dtypes.as_dtype("uint8")) self.assertIs(dtypes.uint16, dtypes.as_dtype("uint16")) self.assertIs(dtypes.int16, dtypes.as_dtype("int16")) self.assertIs(dtypes.int8, dtypes.as_dtype("int8")) self.assertIs(dtypes.string, dtypes.as_dtype("string")) self.assertIs(dtypes.complex64, dtypes.as_dtype("complex64")) self.assertIs(dtypes.complex128, dtypes.as_dtype("complex128")) self.assertIs(dtypes.int64, dtypes.as_dtype("int64")) self.assertIs(dtypes.bool, dtypes.as_dtype("bool")) self.assertIs(dtypes.qint8, dtypes.as_dtype("qint8")) self.assertIs(dtypes.quint8, dtypes.as_dtype("quint8")) self.assertIs(dtypes.qint32, dtypes.as_dtype("qint32")) self.assertIs(dtypes.bfloat16, dtypes.as_dtype("bfloat16")) self.assertIs(dtypes.float32_ref, dtypes.as_dtype("float32_ref")) self.assertIs(dtypes.float64_ref, dtypes.as_dtype("float64_ref")) self.assertIs(dtypes.int32_ref, dtypes.as_dtype("int32_ref")) self.assertIs(dtypes.uint8_ref, dtypes.as_dtype("uint8_ref")) self.assertIs(dtypes.int16_ref, dtypes.as_dtype("int16_ref")) self.assertIs(dtypes.int8_ref, dtypes.as_dtype("int8_ref")) self.assertIs(dtypes.string_ref, dtypes.as_dtype("string_ref")) self.assertIs(dtypes.complex64_ref, dtypes.as_dtype("complex64_ref")) self.assertIs(dtypes.complex128_ref, dtypes.as_dtype("complex128_ref")) self.assertIs(dtypes.int64_ref, dtypes.as_dtype("int64_ref")) self.assertIs(dtypes.bool_ref, dtypes.as_dtype("bool_ref")) self.assertIs(dtypes.qint8_ref, dtypes.as_dtype("qint8_ref")) self.assertIs(dtypes.quint8_ref, dtypes.as_dtype("quint8_ref")) self.assertIs(dtypes.qint32_ref, dtypes.as_dtype("qint32_ref")) self.assertIs(dtypes.bfloat16_ref, dtypes.as_dtype("bfloat16_ref")) with self.assertRaises(TypeError): dtypes.as_dtype("not_a_type") def testDTypesHaveUniqueNames(self): dtypez = [] names = set() for datatype_enum in types_pb2.DataType.values(): if datatype_enum == types_pb2.DT_INVALID: continue dtype = dtypes.as_dtype(datatype_enum) dtypez.append(dtype) names.add(dtype.name) self.assertEqual(len(dtypez), len(names)) def testIsInteger(self): self.assertEqual(dtypes.as_dtype("int8").is_integer, True) self.assertEqual(dtypes.as_dtype("int16").is_integer, True) self.assertEqual(dtypes.as_dtype("int32").is_integer, True) self.assertEqual(dtypes.as_dtype("int64").is_integer, True) self.assertEqual(dtypes.as_dtype("uint8").is_integer, True) self.assertEqual(dtypes.as_dtype("uint16").is_integer, True) self.assertEqual(dtypes.as_dtype("complex64").is_integer, False) self.assertEqual(dtypes.as_dtype("complex128").is_integer, False) self.assertEqual(dtypes.as_dtype("float").is_integer, False) self.assertEqual(dtypes.as_dtype("double").is_integer, False) self.assertEqual(dtypes.as_dtype("string").is_integer, False) self.assertEqual(dtypes.as_dtype("bool").is_integer, False) self.assertEqual(dtypes.as_dtype("bfloat16").is_integer, False) self.assertEqual(dtypes.as_dtype("qint8").is_integer, False) self.assertEqual(dtypes.as_dtype("qint16").is_integer, False) self.assertEqual(dtypes.as_dtype("qint32").is_integer, False) self.assertEqual(dtypes.as_dtype("quint8").is_integer, False) self.assertEqual(dtypes.as_dtype("quint16").is_integer, False) def testIsFloating(self): self.assertEqual(dtypes.as_dtype("int8").is_floating, False) self.assertEqual(dtypes.as_dtype("int16").is_floating, False) self.assertEqual(dtypes.as_dtype("int32").is_floating, False) self.assertEqual(dtypes.as_dtype("int64").is_floating, False) self.assertEqual(dtypes.as_dtype("uint8").is_floating, False) self.assertEqual(dtypes.as_dtype("uint16").is_floating, False) self.assertEqual(dtypes.as_dtype("complex64").is_floating, False) self.assertEqual(dtypes.as_dtype("complex128").is_floating, False) self.assertEqual(dtypes.as_dtype("float32").is_floating, True) self.assertEqual(dtypes.as_dtype("float64").is_floating, True) self.assertEqual(dtypes.as_dtype("string").is_floating, False) self.assertEqual(dtypes.as_dtype("bool").is_floating, False) self.assertEqual(dtypes.as_dtype("bfloat16").is_floating, True) self.assertEqual(dtypes.as_dtype("qint8").is_floating, False) self.assertEqual(dtypes.as_dtype("qint16").is_floating, False) self.assertEqual(dtypes.as_dtype("qint32").is_floating, False) self.assertEqual(dtypes.as_dtype("quint8").is_floating, False) self.assertEqual(dtypes.as_dtype("quint16").is_floating, False) def testIsComplex(self): self.assertEqual(dtypes.as_dtype("int8").is_complex, False) self.assertEqual(dtypes.as_dtype("int16").is_complex, False) self.assertEqual(dtypes.as_dtype("int32").is_complex, False) self.assertEqual(dtypes.as_dtype("int64").is_complex, False) self.assertEqual(dtypes.as_dtype("uint8").is_complex, False) self.assertEqual(dtypes.as_dtype("uint16").is_complex, False) self.assertEqual(dtypes.as_dtype("complex64").is_complex, True) self.assertEqual(dtypes.as_dtype("complex128").is_complex, True) self.assertEqual(dtypes.as_dtype("float32").is_complex, False) self.assertEqual(dtypes.as_dtype("float64").is_complex, False) self.assertEqual(dtypes.as_dtype("string").is_complex, False) self.assertEqual(dtypes.as_dtype("bool").is_complex, False) self.assertEqual(dtypes.as_dtype("bfloat16").is_complex, False) self.assertEqual(dtypes.as_dtype("qint8").is_complex, False) self.assertEqual(dtypes.as_dtype("qint16").is_complex, False) self.assertEqual(dtypes.as_dtype("qint32").is_complex, False) self.assertEqual(dtypes.as_dtype("quint8").is_complex, False) self.assertEqual(dtypes.as_dtype("quint16").is_complex, False) def testIsUnsigned(self): self.assertEqual(dtypes.as_dtype("int8").is_unsigned, False) self.assertEqual(dtypes.as_dtype("int16").is_unsigned, False) self.assertEqual(dtypes.as_dtype("int32").is_unsigned, False) self.assertEqual(dtypes.as_dtype("int64").is_unsigned, False) self.assertEqual(dtypes.as_dtype("uint8").is_unsigned, True) self.assertEqual(dtypes.as_dtype("uint16").is_unsigned, True) self.assertEqual(dtypes.as_dtype("float32").is_unsigned, False) self.assertEqual(dtypes.as_dtype("float64").is_unsigned, False) self.assertEqual(dtypes.as_dtype("bool").is_unsigned, False) self.assertEqual(dtypes.as_dtype("string").is_unsigned, False) self.assertEqual(dtypes.as_dtype("complex64").is_unsigned, False) self.assertEqual(dtypes.as_dtype("complex128").is_unsigned, False) self.assertEqual(dtypes.as_dtype("bfloat16").is_unsigned, False) self.assertEqual(dtypes.as_dtype("qint8").is_unsigned, False) self.assertEqual(dtypes.as_dtype("qint16").is_unsigned, False) self.assertEqual(dtypes.as_dtype("qint32").is_unsigned, False) self.assertEqual(dtypes.as_dtype("quint8").is_unsigned, False) self.assertEqual(dtypes.as_dtype("quint16").is_unsigned, False) def testMinMax(self): # make sure min/max evaluates for all data types that have min/max for datatype_enum in types_pb2.DataType.values(): if not _is_numeric_dtype_enum(datatype_enum): continue dtype = dtypes.as_dtype(datatype_enum) numpy_dtype = dtype.as_numpy_dtype # ignore types for which there are no minimum/maximum (or we cannot # compute it, such as for the q* types) if (dtype.is_quantized or dtype.base_dtype == dtypes.bool or dtype.base_dtype == dtypes.string or dtype.base_dtype == dtypes.complex64 or dtype.base_dtype == dtypes.complex128): continue print("%s: %s - %s" % (dtype, dtype.min, dtype.max)) # check some values that are known if numpy_dtype == np.bool_: self.assertEquals(dtype.min, 0) self.assertEquals(dtype.max, 1) if numpy_dtype == np.int8: self.assertEquals(dtype.min, -128) self.assertEquals(dtype.max, 127) if numpy_dtype == np.int16: self.assertEquals(dtype.min, -32768) self.assertEquals(dtype.max, 32767) if numpy_dtype == np.int32: self.assertEquals(dtype.min, -2147483648) self.assertEquals(dtype.max, 2147483647) if numpy_dtype == np.int64: self.assertEquals(dtype.min, -9223372036854775808) self.assertEquals(dtype.max, 9223372036854775807) if numpy_dtype == np.uint8: self.assertEquals(dtype.min, 0) self.assertEquals(dtype.max, 255) if numpy_dtype == np.uint16: if dtype == dtypes.uint16: self.assertEquals(dtype.min, 0) self.assertEquals(dtype.max, 65535) elif dtype == dtypes.bfloat16: self.assertEquals(dtype.min, 0) self.assertEquals(dtype.max, 4294967295) if numpy_dtype == np.uint32: self.assertEquals(dtype.min, 0) self.assertEquals(dtype.max, 4294967295) if numpy_dtype == np.uint64: self.assertEquals(dtype.min, 0) self.assertEquals(dtype.max, 18446744073709551615) if numpy_dtype in (np.float16, np.float32, np.float64): self.assertEquals(dtype.min, np.finfo(numpy_dtype).min) self.assertEquals(dtype.max, np.finfo(numpy_dtype).max) if numpy_dtype == dtypes.bfloat16.as_numpy_dtype: self.assertEquals(dtype.min, float.fromhex("-0x1.FEp127")) self.assertEquals(dtype.max, float.fromhex("0x1.FEp127")) def testRepr(self): for enum, name in dtypes._TYPE_TO_STRING.items(): if enum > 100: continue dtype = dtypes.DType(enum) self.assertEquals(repr(dtype), "tf." + name) import tensorflow as tf dtype2 = eval(repr(dtype)) self.assertEquals(type(dtype2), dtypes.DType) self.assertEquals(dtype, dtype2) def testEqWithNonTFTypes(self): self.assertNotEqual(dtypes.int32, int) self.assertNotEqual(dtypes.float64, 2.1) if __name__ == "__main__": googletest.main()
apache-2.0
GeotrekCE/Geotrek-admin
geotrek/flatpages/migrations/0004_auto_20200228_1755.py
2
1136
# -*- coding: utf-8 -*- # Generated by Django 1.11.14 on 2020-02-28 16:55 from __future__ import unicode_literals from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('flatpages', '0003_auto_20200211_1011'), ] operations = [ migrations.AlterField( model_name='flatpage', name='date_insert', field=models.DateTimeField(auto_now_add=True, verbose_name='Insertion date'), ), migrations.AlterField( model_name='flatpage', name='date_update', field=models.DateTimeField(auto_now=True, db_index=True, verbose_name='Update date'), ), migrations.AlterField( model_name='flatpage', name='publication_date', field=models.DateField(blank=True, editable=False, null=True, verbose_name='Publication date'), ), migrations.AlterField( model_name='flatpage', name='published', field=models.BooleanField(default=False, help_text='Online', verbose_name='Published'), ), ]
bsd-2-clause
jashwanth9/Expert-recommendation-system
code/tryxgb.py
1
4339
import numpy as np import xgboost as xgb import cPickle as pickle from scipy.sparse import hstack, coo_matrix, vstack ## data question_feats = {} question_keys = pickle.load(open('../features/question_info_keys.dat', 'rb')) ques_charid = pickle.load(open('../features/ques_charid_tfidf.dat', 'rb')) ques_wordid = pickle.load(open('../features/ques_wordid_tfidf.dat', 'rb')) ques_tags = pickle.load(open('../features/ques_tags.dat', 'rb')) #ques_tags = ques_tags.toarray() #ques_charid = ques_charid.toarray() #ques_wordid = ques_wordid.toarray() for i in range(len(question_keys)): # question_feats[question_keys[i]] = ques_charid[i] question_feats[question_keys[i]] = [ques_tags[i], ques_charid[i], ques_wordid[i]] user_feats = {} user_keys = pickle.load(open('../features/user_info_keys.dat', 'rb')) user_charid = pickle.load(open('../features/user_charid_tfidf.dat', 'rb')) user_wordid = pickle.load(open('../features/user_wordid_tfidf.dat', 'rb')) user_tags = pickle.load(open('../features/user_tags.dat', 'rb')) #user_tags = user_tags.toarray() #user_charid = user_charid.toarray() #user_wordid = user_wordid.toarray() for i in range(len(user_keys)): # user_feats[user_keys[i]] = ser_charid[i] user_feats[user_keys[i]] = [user_tags[i], user_charid[i], user_wordid[i]] # Train data with open('../train_data/invited_info_train.txt') as train_file: content = train_file.readlines() element = content[0].strip("\n").split("\t") no_feats = (question_feats[element[0]][0].shape[1] + user_feats[element[1]][0].shape[1] + question_feats[element[0]][1].shape[1] + user_feats[element[1]][1].shape[1] + question_feats[element[0]][2].shape[1] + user_feats[element[1]][2].shape[1]) print no_feats # data = coo_matrix((len(content), no_feats)) data = hstack([question_feats[element[0]][0], user_feats[element[1]][0], question_feats[element[0]][1], user_feats[element[1]][1], question_feats[element[0]][2], user_feats[element[1]][2]]) label = np.zeros(shape=(len(content),1)) for i in range(1,len(content)): element = content[i].strip("\n").split("\t") data_r = hstack([question_feats[element[0]][0], user_feats[element[1]][0], question_feats[element[0]][1], user_feats[element[1]][1], question_feats[element[0]][2], user_feats[element[1]][2]]) data = vstack([data, data_r]) label[i]= element[2] # load the data print(data.shape) print(label.shape) labelv = label[215501:,:] dval = xgb.DMatrix(data[215501:,:], label=labelv) data = data[:215500,:] label = label[:215500,:] dtrain = xgb.DMatrix(data, label=label) evallist = [(dval,'eval'), (dtrain,'train')] ########################################################## # Test data with open('../train_data/validate_nolabel.txt') as train_file: content = train_file.readlines() testData = [] element = content[1].strip("\r\n").split(",") data = hstack([question_feats[element[0]][0], user_feats[element[1]][0], question_feats[element[0]][1], user_feats[element[1]][1], question_feats[element[0]][2], user_feats[element[1]][2]]) for i in range(2, len(content)): element = content[i].strip("\r\n").split(",") testData.append(element) data_r = hstack([question_feats[element[0]][0], user_feats[element[1]][0], question_feats[element[0]][1], user_feats[element[1]][1], question_feats[element[0]][2], user_feats[element[1]][2]]) data = vstack([data, data_r]) print data.shape dtest = xgb.DMatrix(data) # Booster parameters param = {'objective':'rank:pairwise', 'max_depth':'20', 'eval_metric':'ndcg@20000', 'eta':'0.18' } # Training num_round = 630 bst = xgb.train(param, dtrain, num_round, evallist) # Prediction ypred = bst.predict(dtest, ntree_limit=bst.best_ntree_limit) # If early stopping is enabled during training, you can get predicticions from the best iteration with bst.best_ntree_limit: # ypred = bst.predict(xgmat,ntree_limit=bst.best_ntree_limit) print(len(testData)) print ypred.shape #normalization predictions = [] scores = ypred maxscore = max(scores) minscore = min(scores) for score in scores: predictions.append((score-minscore)/float(maxscore-minscore)) ypred = predictions with open('../validation/v_xgboost_word_tfidf.csv', 'w') as f1: f1.write('qid,uid,label\n') for i in range(0, len(ypred)): f1.write(testData[i][0]+','+testData[i][1]+','+str(ypred[i])+'\n')
apache-2.0
atsidaev/gdb-z80
gdb/gdb-gdb.py
41
11521
# Copyright (C) 2009-2014 Free Software Foundation, Inc. # # This file is part of GDB. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import gdb import os.path class TypeFlag: """A class that allows us to store a flag name, its short name, and its value. In the GDB sources, struct type has a component called instance_flags in which the value is the addition of various flags. These flags are defined by two enumerates: type_flag_value, and type_instance_flag_value. This class helps us recreate a list with all these flags that is easy to manipulate and sort. Because all flag names start with either TYPE_FLAG_ or TYPE_INSTANCE_FLAG_, a short_name attribute is provided that strips this prefix. ATTRIBUTES name: The enumeration name (eg: "TYPE_FLAG_UNSIGNED"). value: The associated value. short_name: The enumeration name, with the suffix stripped. """ def __init__(self, name, value): self.name = name self.value = value self.short_name = name.replace("TYPE_FLAG_", '') if self.short_name == name: self.short_name = name.replace("TYPE_INSTANCE_FLAG_", '') def __cmp__(self, other): """Sort by value order.""" return self.value.__cmp__(other.value) # A list of all existing TYPE_FLAGS_* and TYPE_INSTANCE_FLAGS_* # enumerations, stored as TypeFlags objects. Lazy-initialized. TYPE_FLAGS = None class TypeFlagsPrinter: """A class that prints a decoded form of an instance_flags value. This class uses a global named TYPE_FLAGS, which is a list of all defined TypeFlag values. Using a global allows us to compute this list only once. This class relies on a couple of enumeration types being defined. If not, then printing of the instance_flag is going to be degraded, but it's not a fatal error. """ def __init__(self, val): self.val = val def __str__(self): global TYPE_FLAGS if TYPE_FLAGS is None: self.init_TYPE_FLAGS() if not self.val: return "0" if TYPE_FLAGS: flag_list = [flag.short_name for flag in TYPE_FLAGS if self.val & flag.value] else: flag_list = ["???"] return "0x%x [%s]" % (self.val, "|".join(flag_list)) def init_TYPE_FLAGS(self): """Initialize the TYPE_FLAGS global as a list of TypeFlag objects. This operation requires the search of a couple of enumeration types. If not found, a warning is printed on stdout, and TYPE_FLAGS is set to the empty list. The resulting list is sorted by increasing value, to facilitate printing of the list of flags used in an instance_flags value. """ global TYPE_FLAGS TYPE_FLAGS = [] try: flags = gdb.lookup_type("enum type_flag_value") except: print "Warning: Cannot find enum type_flag_value type." print " `struct type' pretty-printer will be degraded" return try: iflags = gdb.lookup_type("enum type_instance_flag_value") except: print "Warning: Cannot find enum type_instance_flag_value type." print " `struct type' pretty-printer will be degraded" return # Note: TYPE_FLAG_MIN is a duplicate of TYPE_FLAG_UNSIGNED, # so exclude it from the list we are building. TYPE_FLAGS = [TypeFlag(field.name, field.enumval) for field in flags.fields() if field.name != 'TYPE_FLAG_MIN'] TYPE_FLAGS += [TypeFlag(field.name, field.enumval) for field in iflags.fields()] TYPE_FLAGS.sort() class StructTypePrettyPrinter: """Pretty-print an object of type struct type""" def __init__(self, val): self.val = val def to_string(self): fields = [] fields.append("pointer_type = %s" % self.val['pointer_type']) fields.append("reference_type = %s" % self.val['reference_type']) fields.append("chain = %s" % self.val['reference_type']) fields.append("instance_flags = %s" % TypeFlagsPrinter(self.val['instance_flags'])) fields.append("length = %d" % self.val['length']) fields.append("main_type = %s" % self.val['main_type']) return "\n{" + ",\n ".join(fields) + "}" class StructMainTypePrettyPrinter: """Pretty-print an objet of type main_type""" def __init__(self, val): self.val = val def flags_to_string(self): """struct main_type contains a series of components that are one-bit ints whose name start with "flag_". For instance: flag_unsigned, flag_stub, etc. In essence, these components are really boolean flags, and this method prints a short synthetic version of the value of all these flags. For instance, if flag_unsigned and flag_static are the only components set to 1, this function will return "unsigned|static". """ fields = [field.name.replace("flag_", "") for field in self.val.type.fields() if field.name.startswith("flag_") and self.val[field.name]] return "|".join(fields) def owner_to_string(self): """Return an image of component "owner". """ if self.val['flag_objfile_owned'] != 0: return "%s (objfile)" % self.val['owner']['objfile'] else: return "%s (gdbarch)" % self.val['owner']['gdbarch'] def struct_field_location_img(self, field_val): """Return an image of the loc component inside the given field gdb.Value. """ loc_val = field_val['loc'] loc_kind = str(field_val['loc_kind']) if loc_kind == "FIELD_LOC_KIND_BITPOS": return 'bitpos = %d' % loc_val['bitpos'] elif loc_kind == "FIELD_LOC_KIND_ENUMVAL": return 'enumval = %d' % loc_val['enumval'] elif loc_kind == "FIELD_LOC_KIND_PHYSADDR": return 'physaddr = 0x%x' % loc_val['physaddr'] elif loc_kind == "FIELD_LOC_KIND_PHYSNAME": return 'physname = %s' % loc_val['physname'] elif loc_kind == "FIELD_LOC_KIND_DWARF_BLOCK": return 'dwarf_block = %s' % loc_val['dwarf_block'] else: return 'loc = ??? (unsupported loc_kind value)' def struct_field_img(self, fieldno): """Return an image of the main_type field number FIELDNO. """ f = self.val['flds_bnds']['fields'][fieldno] label = "flds_bnds.fields[%d]:" % fieldno if f['artificial']: label += " (artificial)" fields = [] fields.append("name = %s" % f['name']) fields.append("type = %s" % f['type']) fields.append("loc_kind = %s" % f['loc_kind']) fields.append("bitsize = %d" % f['bitsize']) fields.append(self.struct_field_location_img(f)) return label + "\n" + " {" + ",\n ".join(fields) + "}" def bounds_img(self): """Return an image of the main_type bounds. """ b = self.val['flds_bnds']['bounds'].dereference() low = str(b['low']) if b['low_undefined'] != 0: low += " (undefined)" high = str(b['high']) if b['high_undefined'] != 0: high += " (undefined)" return "flds_bnds.bounds = {%s, %s}" % (low, high) def type_specific_img(self): """Return a string image of the main_type type_specific union. Only the relevant component of that union is printed (based on the value of the type_specific_kind field. """ type_specific_kind = str(self.val['type_specific_field']) type_specific = self.val['type_specific'] if type_specific_kind == "TYPE_SPECIFIC_NONE": img = 'type_specific_field = %s' % type_specific_kind elif type_specific_kind == "TYPE_SPECIFIC_CPLUS_STUFF": img = "cplus_stuff = %s" % type_specific['cplus_stuff'] elif type_specific_kind == "TYPE_SPECIFIC_GNAT_STUFF": img = ("gnat_stuff = {descriptive_type = %s}" % type_specific['gnat_stuff']['descriptive_type']) elif type_specific_kind == "TYPE_SPECIFIC_FLOATFORMAT": img = "floatformat[0..1] = %s" % type_specific['floatformat'] elif type_specific_kind == "TYPE_SPECIFIC_FUNC": img = ("calling_convention = %d" % type_specific['func_stuff']['calling_convention']) # tail_call_list is not printed. else: img = ("type_specific = ??? (unknown type_secific_kind: %s)" % type_specific_kind) return img def to_string(self): """Return a pretty-printed image of our main_type. """ fields = [] fields.append("name = %s" % self.val['name']) fields.append("tag_name = %s" % self.val['tag_name']) fields.append("code = %s" % self.val['code']) fields.append("flags = [%s]" % self.flags_to_string()) fields.append("owner = %s" % self.owner_to_string()) fields.append("target_type = %s" % self.val['target_type']) fields.append("vptr_basetype = %s" % self.val['vptr_basetype']) if self.val['nfields'] > 0: for fieldno in range(self.val['nfields']): fields.append(self.struct_field_img(fieldno)) if self.val['code'] == gdb.TYPE_CODE_RANGE: fields.append(self.bounds_img()) fields.append(self.type_specific_img()) return "\n{" + ",\n ".join(fields) + "}" def type_lookup_function(val): """A routine that returns the correct pretty printer for VAL if appropriate. Returns None otherwise. """ if val.type.tag == "type": return StructTypePrettyPrinter(val) elif val.type.tag == "main_type": return StructMainTypePrettyPrinter(val) return None def register_pretty_printer(objfile): """A routine to register a pretty-printer against the given OBJFILE. """ objfile.pretty_printers.append(type_lookup_function) if __name__ == "__main__": if gdb.current_objfile() is not None: # This is the case where this script is being "auto-loaded" # for a given objfile. Register the pretty-printer for that # objfile. register_pretty_printer(gdb.current_objfile()) else: # We need to locate the objfile corresponding to the GDB # executable, and register the pretty-printer for that objfile. # FIXME: The condition used to match the objfile is too simplistic # and will not work on Windows. for objfile in gdb.objfiles(): if os.path.basename(objfile.filename) == "gdb": objfile.pretty_printers.append(type_lookup_function)
gpl-2.0
larsmans/scipy
scipy/stats/tests/test_contingency.py
126
5959
from __future__ import division, print_function, absolute_import import numpy as np from numpy.testing import (run_module_suite, assert_equal, assert_array_equal, assert_array_almost_equal, assert_approx_equal, assert_raises, assert_allclose) from scipy.special import xlogy from scipy.stats.contingency import margins, expected_freq, chi2_contingency def test_margins(): a = np.array([1]) m = margins(a) assert_equal(len(m), 1) m0 = m[0] assert_array_equal(m0, np.array([1])) a = np.array([[1]]) m0, m1 = margins(a) expected0 = np.array([[1]]) expected1 = np.array([[1]]) assert_array_equal(m0, expected0) assert_array_equal(m1, expected1) a = np.arange(12).reshape(2, 6) m0, m1 = margins(a) expected0 = np.array([[15], [51]]) expected1 = np.array([[6, 8, 10, 12, 14, 16]]) assert_array_equal(m0, expected0) assert_array_equal(m1, expected1) a = np.arange(24).reshape(2, 3, 4) m0, m1, m2 = margins(a) expected0 = np.array([[[66]], [[210]]]) expected1 = np.array([[[60], [92], [124]]]) expected2 = np.array([[[60, 66, 72, 78]]]) assert_array_equal(m0, expected0) assert_array_equal(m1, expected1) assert_array_equal(m2, expected2) def test_expected_freq(): assert_array_equal(expected_freq([1]), np.array([1.0])) observed = np.array([[[2, 0], [0, 2]], [[0, 2], [2, 0]], [[1, 1], [1, 1]]]) e = expected_freq(observed) assert_array_equal(e, np.ones_like(observed)) observed = np.array([[10, 10, 20], [20, 20, 20]]) e = expected_freq(observed) correct = np.array([[12., 12., 16.], [18., 18., 24.]]) assert_array_almost_equal(e, correct) def test_chi2_contingency_trivial(): # Some very simple tests for chi2_contingency. # A trivial case obs = np.array([[1, 2], [1, 2]]) chi2, p, dof, expected = chi2_contingency(obs, correction=False) assert_equal(chi2, 0.0) assert_equal(p, 1.0) assert_equal(dof, 1) assert_array_equal(obs, expected) # A *really* trivial case: 1-D data. obs = np.array([1, 2, 3]) chi2, p, dof, expected = chi2_contingency(obs, correction=False) assert_equal(chi2, 0.0) assert_equal(p, 1.0) assert_equal(dof, 0) assert_array_equal(obs, expected) def test_chi2_contingency_R(): # Some test cases that were computed independently, using R. Rcode = \ """ # Data vector. data <- c( 12, 34, 23, 4, 47, 11, 35, 31, 11, 34, 10, 18, 12, 32, 9, 18, 13, 19, 12, 12, 14, 9, 33, 25 ) # Create factor tags:r=rows, c=columns, t=tiers r <- factor(gl(4, 2*3, 2*3*4, labels=c("r1", "r2", "r3", "r4"))) c <- factor(gl(3, 1, 2*3*4, labels=c("c1", "c2", "c3"))) t <- factor(gl(2, 3, 2*3*4, labels=c("t1", "t2"))) # 3-way Chi squared test of independence s = summary(xtabs(data~r+c+t)) print(s) """ Routput = \ """ Call: xtabs(formula = data ~ r + c + t) Number of cases in table: 478 Number of factors: 3 Test for independence of all factors: Chisq = 102.17, df = 17, p-value = 3.514e-14 """ obs = np.array( [[[12, 34, 23], [35, 31, 11], [12, 32, 9], [12, 12, 14]], [[4, 47, 11], [34, 10, 18], [18, 13, 19], [9, 33, 25]]]) chi2, p, dof, expected = chi2_contingency(obs) assert_approx_equal(chi2, 102.17, significant=5) assert_approx_equal(p, 3.514e-14, significant=4) assert_equal(dof, 17) Rcode = \ """ # Data vector. data <- c( # 12, 17, 11, 16, # 11, 12, 15, 16, # 23, 15, 30, 22, # 14, 17, 15, 16 ) # Create factor tags:r=rows, c=columns, d=depths(?), t=tiers r <- factor(gl(2, 2, 2*2*2*2, labels=c("r1", "r2"))) c <- factor(gl(2, 1, 2*2*2*2, labels=c("c1", "c2"))) d <- factor(gl(2, 4, 2*2*2*2, labels=c("d1", "d2"))) t <- factor(gl(2, 8, 2*2*2*2, labels=c("t1", "t2"))) # 4-way Chi squared test of independence s = summary(xtabs(data~r+c+d+t)) print(s) """ Routput = \ """ Call: xtabs(formula = data ~ r + c + d + t) Number of cases in table: 262 Number of factors: 4 Test for independence of all factors: Chisq = 8.758, df = 11, p-value = 0.6442 """ obs = np.array( [[[[12, 17], [11, 16]], [[11, 12], [15, 16]]], [[[23, 15], [30, 22]], [[14, 17], [15, 16]]]]) chi2, p, dof, expected = chi2_contingency(obs) assert_approx_equal(chi2, 8.758, significant=4) assert_approx_equal(p, 0.6442, significant=4) assert_equal(dof, 11) def test_chi2_contingency_g(): c = np.array([[15, 60], [15, 90]]) g, p, dof, e = chi2_contingency(c, lambda_='log-likelihood', correction=False) assert_allclose(g, 2*xlogy(c, c/e).sum()) g, p, dof, e = chi2_contingency(c, lambda_='log-likelihood', correction=True) c_corr = c + np.array([[-0.5, 0.5], [0.5, -0.5]]) assert_allclose(g, 2*xlogy(c_corr, c_corr/e).sum()) c = np.array([[10, 12, 10], [12, 10, 10]]) g, p, dof, e = chi2_contingency(c, lambda_='log-likelihood') assert_allclose(g, 2*xlogy(c, c/e).sum()) def test_chi2_contingency_bad_args(): # Test that "bad" inputs raise a ValueError. # Negative value in the array of observed frequencies. obs = np.array([[-1, 10], [1, 2]]) assert_raises(ValueError, chi2_contingency, obs) # The zeros in this will result in zeros in the array # of expected frequencies. obs = np.array([[0, 1], [0, 1]]) assert_raises(ValueError, chi2_contingency, obs) # A degenerate case: `observed` has size 0. obs = np.empty((0, 8)) assert_raises(ValueError, chi2_contingency, obs) if __name__ == "__main__": run_module_suite()
bsd-3-clause
pquentin/django
tests/invalid_models_tests/test_ordinary_fields.py
51
19155
# -*- encoding: utf-8 -*- from __future__ import unicode_literals import unittest from django.core.checks import Error, Warning as DjangoWarning from django.db import connection, models from django.test.utils import override_settings from django.utils.timezone import now from .base import IsolatedModelsTestCase class AutoFieldTests(IsolatedModelsTestCase): def test_valid_case(self): class Model(models.Model): id = models.AutoField(primary_key=True) field = Model._meta.get_field('id') errors = field.check() expected = [] self.assertEqual(errors, expected) def test_primary_key(self): # primary_key must be True. Refs #12467. class Model(models.Model): field = models.AutoField(primary_key=False) # Prevent Django from autocreating `id` AutoField, which would # result in an error, because a model must have exactly one # AutoField. another = models.IntegerField(primary_key=True) field = Model._meta.get_field('field') errors = field.check() expected = [ Error( 'AutoFields must set primary_key=True.', hint=None, obj=field, id='fields.E100', ), ] self.assertEqual(errors, expected) class BooleanFieldTests(IsolatedModelsTestCase): def test_nullable_boolean_field(self): class Model(models.Model): field = models.BooleanField(null=True) field = Model._meta.get_field('field') errors = field.check() expected = [ Error( 'BooleanFields do not accept null values.', hint='Use a NullBooleanField instead.', obj=field, id='fields.E110', ), ] self.assertEqual(errors, expected) class CharFieldTests(IsolatedModelsTestCase): def test_valid_field(self): class Model(models.Model): field = models.CharField( max_length=255, choices=[ ('1', 'item1'), ('2', 'item2'), ], db_index=True) field = Model._meta.get_field('field') errors = field.check() expected = [] self.assertEqual(errors, expected) def test_missing_max_length(self): class Model(models.Model): field = models.CharField() field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "CharFields must define a 'max_length' attribute.", hint=None, obj=field, id='fields.E120', ), ] self.assertEqual(errors, expected) def test_negative_max_length(self): class Model(models.Model): field = models.CharField(max_length=-1) field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'max_length' must be a positive integer.", hint=None, obj=field, id='fields.E121', ), ] self.assertEqual(errors, expected) def test_bad_max_length_value(self): class Model(models.Model): field = models.CharField(max_length="bad") field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'max_length' must be a positive integer.", hint=None, obj=field, id='fields.E121', ), ] self.assertEqual(errors, expected) def test_non_iterable_choices(self): class Model(models.Model): field = models.CharField(max_length=10, choices='bad') field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'choices' must be an iterable (e.g., a list or tuple).", hint=None, obj=field, id='fields.E004', ), ] self.assertEqual(errors, expected) def test_choices_containing_non_pairs(self): class Model(models.Model): field = models.CharField(max_length=10, choices=[(1, 2, 3), (1, 2, 3)]) field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'choices' must be an iterable containing (actual value, human readable name) tuples.", hint=None, obj=field, id='fields.E005', ), ] self.assertEqual(errors, expected) def test_bad_db_index_value(self): class Model(models.Model): field = models.CharField(max_length=10, db_index='bad') field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'db_index' must be None, True or False.", hint=None, obj=field, id='fields.E006', ), ] self.assertEqual(errors, expected) @unittest.skipUnless(connection.vendor == 'mysql', "Test valid only for MySQL") def test_too_long_char_field_under_mysql(self): from django.db.backends.mysql.validation import DatabaseValidation class Model(models.Model): field = models.CharField(unique=True, max_length=256) field = Model._meta.get_field('field') validator = DatabaseValidation(connection=None) errors = validator.check_field(field) expected = [ Error( 'MySQL does not allow unique CharFields to have a max_length > 255.', hint=None, obj=field, id='mysql.E001', ) ] self.assertEqual(errors, expected) class DateFieldTests(IsolatedModelsTestCase): def test_auto_now_and_auto_now_add_raise_error(self): class Model(models.Model): field0 = models.DateTimeField(auto_now=True, auto_now_add=True, default=now) field1 = models.DateTimeField(auto_now=True, auto_now_add=False, default=now) field2 = models.DateTimeField(auto_now=False, auto_now_add=True, default=now) field3 = models.DateTimeField(auto_now=True, auto_now_add=True, default=None) expected = [] checks = [] for i in range(4): field = Model._meta.get_field('field%d' % i) expected.append(Error( "The options auto_now, auto_now_add, and default " "are mutually exclusive. Only one of these options " "may be present.", hint=None, obj=field, id='fields.E160', )) checks.extend(field.check()) self.assertEqual(checks, expected) def test_fix_default_value(self): class Model(models.Model): field_dt = models.DateField(default=now()) field_d = models.DateField(default=now().date()) field_now = models.DateField(default=now) field_dt = Model._meta.get_field('field_dt') field_d = Model._meta.get_field('field_d') field_now = Model._meta.get_field('field_now') errors = field_dt.check() errors.extend(field_d.check()) errors.extend(field_now.check()) # doesn't raise a warning expected = [ DjangoWarning( 'Fixed default value provided.', hint='It seems you set a fixed date / time / datetime ' 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', obj=field_dt, id='fields.W161', ), DjangoWarning( 'Fixed default value provided.', hint='It seems you set a fixed date / time / datetime ' 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', obj=field_d, id='fields.W161', ) ] maxDiff = self.maxDiff self.maxDiff = None self.assertEqual(errors, expected) self.maxDiff = maxDiff @override_settings(USE_TZ=True) def test_fix_default_value_tz(self): self.test_fix_default_value() class DateTimeFieldTests(IsolatedModelsTestCase): def test_fix_default_value(self): class Model(models.Model): field_dt = models.DateTimeField(default=now()) field_d = models.DateTimeField(default=now().date()) field_now = models.DateTimeField(default=now) field_dt = Model._meta.get_field('field_dt') field_d = Model._meta.get_field('field_d') field_now = Model._meta.get_field('field_now') errors = field_dt.check() errors.extend(field_d.check()) errors.extend(field_now.check()) # doesn't raise a warning expected = [ DjangoWarning( 'Fixed default value provided.', hint='It seems you set a fixed date / time / datetime ' 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', obj=field_dt, id='fields.W161', ), DjangoWarning( 'Fixed default value provided.', hint='It seems you set a fixed date / time / datetime ' 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', obj=field_d, id='fields.W161', ) ] maxDiff = self.maxDiff self.maxDiff = None self.assertEqual(errors, expected) self.maxDiff = maxDiff @override_settings(USE_TZ=True) def test_fix_default_value_tz(self): self.test_fix_default_value() class DecimalFieldTests(IsolatedModelsTestCase): def test_required_attributes(self): class Model(models.Model): field = models.DecimalField() field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "DecimalFields must define a 'decimal_places' attribute.", hint=None, obj=field, id='fields.E130', ), Error( "DecimalFields must define a 'max_digits' attribute.", hint=None, obj=field, id='fields.E132', ), ] self.assertEqual(errors, expected) def test_negative_max_digits_and_decimal_places(self): class Model(models.Model): field = models.DecimalField(max_digits=-1, decimal_places=-1) field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'decimal_places' must be a non-negative integer.", hint=None, obj=field, id='fields.E131', ), Error( "'max_digits' must be a positive integer.", hint=None, obj=field, id='fields.E133', ), ] self.assertEqual(errors, expected) def test_bad_values_of_max_digits_and_decimal_places(self): class Model(models.Model): field = models.DecimalField(max_digits="bad", decimal_places="bad") field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'decimal_places' must be a non-negative integer.", hint=None, obj=field, id='fields.E131', ), Error( "'max_digits' must be a positive integer.", hint=None, obj=field, id='fields.E133', ), ] self.assertEqual(errors, expected) def test_decimal_places_greater_than_max_digits(self): class Model(models.Model): field = models.DecimalField(max_digits=9, decimal_places=10) field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'max_digits' must be greater or equal to 'decimal_places'.", hint=None, obj=field, id='fields.E134', ), ] self.assertEqual(errors, expected) def test_valid_field(self): class Model(models.Model): field = models.DecimalField(max_digits=10, decimal_places=10) field = Model._meta.get_field('field') errors = field.check() expected = [] self.assertEqual(errors, expected) class FileFieldTests(IsolatedModelsTestCase): def test_valid_case(self): class Model(models.Model): field = models.FileField(upload_to='somewhere') field = Model._meta.get_field('field') errors = field.check() expected = [] self.assertEqual(errors, expected) def test_unique(self): class Model(models.Model): field = models.FileField(unique=False, upload_to='somewhere') field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'unique' is not a valid argument for a FileField.", hint=None, obj=field, id='fields.E200', ) ] self.assertEqual(errors, expected) def test_primary_key(self): class Model(models.Model): field = models.FileField(primary_key=False, upload_to='somewhere') field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "'primary_key' is not a valid argument for a FileField.", hint=None, obj=field, id='fields.E201', ) ] self.assertEqual(errors, expected) class FilePathFieldTests(IsolatedModelsTestCase): def test_forbidden_files_and_folders(self): class Model(models.Model): field = models.FilePathField(allow_files=False, allow_folders=False) field = Model._meta.get_field('field') errors = field.check() expected = [ Error( "FilePathFields must have either 'allow_files' or 'allow_folders' set to True.", hint=None, obj=field, id='fields.E140', ), ] self.assertEqual(errors, expected) class GenericIPAddressFieldTests(IsolatedModelsTestCase): def test_non_nullable_blank(self): class Model(models.Model): field = models.GenericIPAddressField(null=False, blank=True) field = Model._meta.get_field('field') errors = field.check() expected = [ Error( ('GenericIPAddressFields cannot have blank=True if null=False, ' 'as blank values are stored as nulls.'), hint=None, obj=field, id='fields.E150', ), ] self.assertEqual(errors, expected) class ImageFieldTests(IsolatedModelsTestCase): def test_pillow_installed(self): try: from PIL import Image # NOQA except ImportError: pillow_installed = False else: pillow_installed = True class Model(models.Model): field = models.ImageField(upload_to='somewhere') field = Model._meta.get_field('field') errors = field.check() expected = [] if pillow_installed else [ Error( 'Cannot use ImageField because Pillow is not installed.', hint=('Get Pillow at https://pypi.python.org/pypi/Pillow ' 'or run command "pip install Pillow".'), obj=field, id='fields.E210', ), ] self.assertEqual(errors, expected) class IntegerFieldTests(IsolatedModelsTestCase): def test_max_length_warning(self): class Model(models.Model): value = models.IntegerField(max_length=2) value = Model._meta.get_field('value') errors = Model.check() expected = [ DjangoWarning( "'max_length' is ignored when used with IntegerField", hint="Remove 'max_length' from field", obj=value, id='fields.W122', ) ] self.assertEqual(errors, expected) class TimeFieldTests(IsolatedModelsTestCase): def test_fix_default_value(self): class Model(models.Model): field_dt = models.TimeField(default=now()) field_t = models.TimeField(default=now().time()) field_now = models.DateField(default=now) field_dt = Model._meta.get_field('field_dt') field_t = Model._meta.get_field('field_t') field_now = Model._meta.get_field('field_now') errors = field_dt.check() errors.extend(field_t.check()) errors.extend(field_now.check()) # doesn't raise a warning expected = [ DjangoWarning( 'Fixed default value provided.', hint='It seems you set a fixed date / time / datetime ' 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', obj=field_dt, id='fields.W161', ), DjangoWarning( 'Fixed default value provided.', hint='It seems you set a fixed date / time / datetime ' 'value as default for this field. This may not be ' 'what you want. If you want to have the current date ' 'as default, use `django.utils.timezone.now`', obj=field_t, id='fields.W161', ) ] maxDiff = self.maxDiff self.maxDiff = None self.assertEqual(errors, expected) self.maxDiff = maxDiff @override_settings(USE_TZ=True) def test_fix_default_value_tz(self): self.test_fix_default_value()
bsd-3-clause
jamesbulpin/xcp-xen-4.1
tools/python/xen/xend/server/SSLXMLRPCServer.py
49
3646
#============================================================================ # This library is free software; you can redistribute it and/or # modify it under the terms of version 2.1 of the GNU Lesser General Public # License as published by the Free Software Foundation. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #============================================================================ # Copyright (C) 2007 XenSource Inc. #============================================================================ """ HTTPS wrapper for an XML-RPC server interface. Requires PyOpenSSL (Debian package python-pyopenssl). """ import socket from OpenSSL import SSL from xen.util.xmlrpclib2 import XMLRPCRequestHandler, TCPXMLRPCServer class SSLXMLRPCRequestHandler(XMLRPCRequestHandler): def setup(self): self.connection = self.request self.rfile = socket._fileobject(self.request, "rb", self.rbufsize) self.wfile = socket._fileobject(self.request, "wb", self.wbufsize) # # Taken from pyOpenSSL-0.6 examples (public-domain) # class SSLWrapper: """ """ def __init__(self, conn): """ Connection is not yet a new-style class, so I'm making a proxy instead of subclassing. """ self.__dict__["conn"] = conn def __getattr__(self, name): return getattr(self.__dict__["conn"], name) def __setattr__(self, name, value): setattr(self.__dict__["conn"], name, value) def close(self): self.shutdown() return self.__dict__["conn"].close() def shutdown(self, how=1): """ SimpleXMLRpcServer.doPOST calls shutdown(1), and Connection.shutdown() doesn't take an argument. So we just discard the argument. """ # Block until the shutdown is complete self.__dict__["conn"].shutdown() self.__dict__["conn"].shutdown() def accept(self): """ This is the other part of the shutdown() workaround. Since servers create new sockets, we have to infect them with our magic. :) """ c, a = self.__dict__["conn"].accept() return (SSLWrapper(c), a) # # End of pyOpenSSL-0.6 example code. # class SSLXMLRPCServer(TCPXMLRPCServer): def __init__(self, addr, allowed, xenapi, logRequests = 1, ssl_key_file = None, ssl_cert_file = None): TCPXMLRPCServer.__init__(self, addr, allowed, xenapi, SSLXMLRPCRequestHandler, logRequests) if not ssl_key_file or not ssl_cert_file: raise ValueError("SSLXMLRPCServer requires ssl_key_file " "and ssl_cert_file to be set.") # make a SSL socket ctx = SSL.Context(SSL.SSLv23_METHOD) ctx.set_options(SSL.OP_NO_SSLv2) ctx.use_privatekey_file (ssl_key_file) ctx.use_certificate_file(ssl_cert_file) self.socket = SSLWrapper(SSL.Connection(ctx, socket.socket(self.address_family, self.socket_type))) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.server_bind() self.server_activate()
gpl-2.0
qwefi/nova
nova/openstack/common/db/exception.py
13
1626
# vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright 2010 United States Government as represented by the # Administrator of the National Aeronautics and Space Administration. # 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. """DB related custom exceptions.""" from nova.openstack.common.gettextutils import _ class DBError(Exception): """Wraps an implementation specific exception.""" def __init__(self, inner_exception=None): self.inner_exception = inner_exception super(DBError, self).__init__(str(inner_exception)) class DBDuplicateEntry(DBError): """Wraps an implementation specific exception.""" def __init__(self, columns=[], inner_exception=None): self.columns = columns super(DBDuplicateEntry, self).__init__(inner_exception) class DBDeadlock(DBError): def __init__(self, inner_exception=None): super(DBDeadlock, self).__init__(inner_exception) class DBInvalidUnicodeParameter(Exception): message = _("Invalid Parameter: " "Unicode is not supported by the current database.")
apache-2.0
MattDevo/edk2
AppPkg/Applications/Python/Python-2.7.2/Lib/encodings/mac_roman.py
93
14043
""" Python Character Mapping Codec mac_roman generated from 'MAPPINGS/VENDORS/APPLE/ROMAN.TXT' with gencodec.py. """#" import codecs ### Codec APIs class Codec(codecs.Codec): def encode(self,input,errors='strict'): return codecs.charmap_encode(input,errors,encoding_table) def decode(self,input,errors='strict'): return codecs.charmap_decode(input,errors,decoding_table) class IncrementalEncoder(codecs.IncrementalEncoder): def encode(self, input, final=False): return codecs.charmap_encode(input,self.errors,encoding_table)[0] class IncrementalDecoder(codecs.IncrementalDecoder): def decode(self, input, final=False): return codecs.charmap_decode(input,self.errors,decoding_table)[0] class StreamWriter(Codec,codecs.StreamWriter): pass class StreamReader(Codec,codecs.StreamReader): pass ### encodings module API def getregentry(): return codecs.CodecInfo( name='mac-roman', encode=Codec().encode, decode=Codec().decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, ) ### Decoding Table decoding_table = ( u'\x00' # 0x00 -> CONTROL CHARACTER u'\x01' # 0x01 -> CONTROL CHARACTER u'\x02' # 0x02 -> CONTROL CHARACTER u'\x03' # 0x03 -> CONTROL CHARACTER u'\x04' # 0x04 -> CONTROL CHARACTER u'\x05' # 0x05 -> CONTROL CHARACTER u'\x06' # 0x06 -> CONTROL CHARACTER u'\x07' # 0x07 -> CONTROL CHARACTER u'\x08' # 0x08 -> CONTROL CHARACTER u'\t' # 0x09 -> CONTROL CHARACTER u'\n' # 0x0A -> CONTROL CHARACTER u'\x0b' # 0x0B -> CONTROL CHARACTER u'\x0c' # 0x0C -> CONTROL CHARACTER u'\r' # 0x0D -> CONTROL CHARACTER u'\x0e' # 0x0E -> CONTROL CHARACTER u'\x0f' # 0x0F -> CONTROL CHARACTER u'\x10' # 0x10 -> CONTROL CHARACTER u'\x11' # 0x11 -> CONTROL CHARACTER u'\x12' # 0x12 -> CONTROL CHARACTER u'\x13' # 0x13 -> CONTROL CHARACTER u'\x14' # 0x14 -> CONTROL CHARACTER u'\x15' # 0x15 -> CONTROL CHARACTER u'\x16' # 0x16 -> CONTROL CHARACTER u'\x17' # 0x17 -> CONTROL CHARACTER u'\x18' # 0x18 -> CONTROL CHARACTER u'\x19' # 0x19 -> CONTROL CHARACTER u'\x1a' # 0x1A -> CONTROL CHARACTER u'\x1b' # 0x1B -> CONTROL CHARACTER u'\x1c' # 0x1C -> CONTROL CHARACTER u'\x1d' # 0x1D -> CONTROL CHARACTER u'\x1e' # 0x1E -> CONTROL CHARACTER u'\x1f' # 0x1F -> CONTROL CHARACTER u' ' # 0x20 -> SPACE u'!' # 0x21 -> EXCLAMATION MARK u'"' # 0x22 -> QUOTATION MARK u'#' # 0x23 -> NUMBER SIGN u'$' # 0x24 -> DOLLAR SIGN u'%' # 0x25 -> PERCENT SIGN u'&' # 0x26 -> AMPERSAND u"'" # 0x27 -> APOSTROPHE u'(' # 0x28 -> LEFT PARENTHESIS u')' # 0x29 -> RIGHT PARENTHESIS u'*' # 0x2A -> ASTERISK u'+' # 0x2B -> PLUS SIGN u',' # 0x2C -> COMMA u'-' # 0x2D -> HYPHEN-MINUS u'.' # 0x2E -> FULL STOP u'/' # 0x2F -> SOLIDUS u'0' # 0x30 -> DIGIT ZERO u'1' # 0x31 -> DIGIT ONE u'2' # 0x32 -> DIGIT TWO u'3' # 0x33 -> DIGIT THREE u'4' # 0x34 -> DIGIT FOUR u'5' # 0x35 -> DIGIT FIVE u'6' # 0x36 -> DIGIT SIX u'7' # 0x37 -> DIGIT SEVEN u'8' # 0x38 -> DIGIT EIGHT u'9' # 0x39 -> DIGIT NINE u':' # 0x3A -> COLON u';' # 0x3B -> SEMICOLON u'<' # 0x3C -> LESS-THAN SIGN u'=' # 0x3D -> EQUALS SIGN u'>' # 0x3E -> GREATER-THAN SIGN u'?' # 0x3F -> QUESTION MARK u'@' # 0x40 -> COMMERCIAL AT u'A' # 0x41 -> LATIN CAPITAL LETTER A u'B' # 0x42 -> LATIN CAPITAL LETTER B u'C' # 0x43 -> LATIN CAPITAL LETTER C u'D' # 0x44 -> LATIN CAPITAL LETTER D u'E' # 0x45 -> LATIN CAPITAL LETTER E u'F' # 0x46 -> LATIN CAPITAL LETTER F u'G' # 0x47 -> LATIN CAPITAL LETTER G u'H' # 0x48 -> LATIN CAPITAL LETTER H u'I' # 0x49 -> LATIN CAPITAL LETTER I u'J' # 0x4A -> LATIN CAPITAL LETTER J u'K' # 0x4B -> LATIN CAPITAL LETTER K u'L' # 0x4C -> LATIN CAPITAL LETTER L u'M' # 0x4D -> LATIN CAPITAL LETTER M u'N' # 0x4E -> LATIN CAPITAL LETTER N u'O' # 0x4F -> LATIN CAPITAL LETTER O u'P' # 0x50 -> LATIN CAPITAL LETTER P u'Q' # 0x51 -> LATIN CAPITAL LETTER Q u'R' # 0x52 -> LATIN CAPITAL LETTER R u'S' # 0x53 -> LATIN CAPITAL LETTER S u'T' # 0x54 -> LATIN CAPITAL LETTER T u'U' # 0x55 -> LATIN CAPITAL LETTER U u'V' # 0x56 -> LATIN CAPITAL LETTER V u'W' # 0x57 -> LATIN CAPITAL LETTER W u'X' # 0x58 -> LATIN CAPITAL LETTER X u'Y' # 0x59 -> LATIN CAPITAL LETTER Y u'Z' # 0x5A -> LATIN CAPITAL LETTER Z u'[' # 0x5B -> LEFT SQUARE BRACKET u'\\' # 0x5C -> REVERSE SOLIDUS u']' # 0x5D -> RIGHT SQUARE BRACKET u'^' # 0x5E -> CIRCUMFLEX ACCENT u'_' # 0x5F -> LOW LINE u'`' # 0x60 -> GRAVE ACCENT u'a' # 0x61 -> LATIN SMALL LETTER A u'b' # 0x62 -> LATIN SMALL LETTER B u'c' # 0x63 -> LATIN SMALL LETTER C u'd' # 0x64 -> LATIN SMALL LETTER D u'e' # 0x65 -> LATIN SMALL LETTER E u'f' # 0x66 -> LATIN SMALL LETTER F u'g' # 0x67 -> LATIN SMALL LETTER G u'h' # 0x68 -> LATIN SMALL LETTER H u'i' # 0x69 -> LATIN SMALL LETTER I u'j' # 0x6A -> LATIN SMALL LETTER J u'k' # 0x6B -> LATIN SMALL LETTER K u'l' # 0x6C -> LATIN SMALL LETTER L u'm' # 0x6D -> LATIN SMALL LETTER M u'n' # 0x6E -> LATIN SMALL LETTER N u'o' # 0x6F -> LATIN SMALL LETTER O u'p' # 0x70 -> LATIN SMALL LETTER P u'q' # 0x71 -> LATIN SMALL LETTER Q u'r' # 0x72 -> LATIN SMALL LETTER R u's' # 0x73 -> LATIN SMALL LETTER S u't' # 0x74 -> LATIN SMALL LETTER T u'u' # 0x75 -> LATIN SMALL LETTER U u'v' # 0x76 -> LATIN SMALL LETTER V u'w' # 0x77 -> LATIN SMALL LETTER W u'x' # 0x78 -> LATIN SMALL LETTER X u'y' # 0x79 -> LATIN SMALL LETTER Y u'z' # 0x7A -> LATIN SMALL LETTER Z u'{' # 0x7B -> LEFT CURLY BRACKET u'|' # 0x7C -> VERTICAL LINE u'}' # 0x7D -> RIGHT CURLY BRACKET u'~' # 0x7E -> TILDE u'\x7f' # 0x7F -> CONTROL CHARACTER u'\xc4' # 0x80 -> LATIN CAPITAL LETTER A WITH DIAERESIS u'\xc5' # 0x81 -> LATIN CAPITAL LETTER A WITH RING ABOVE u'\xc7' # 0x82 -> LATIN CAPITAL LETTER C WITH CEDILLA u'\xc9' # 0x83 -> LATIN CAPITAL LETTER E WITH ACUTE u'\xd1' # 0x84 -> LATIN CAPITAL LETTER N WITH TILDE u'\xd6' # 0x85 -> LATIN CAPITAL LETTER O WITH DIAERESIS u'\xdc' # 0x86 -> LATIN CAPITAL LETTER U WITH DIAERESIS u'\xe1' # 0x87 -> LATIN SMALL LETTER A WITH ACUTE u'\xe0' # 0x88 -> LATIN SMALL LETTER A WITH GRAVE u'\xe2' # 0x89 -> LATIN SMALL LETTER A WITH CIRCUMFLEX u'\xe4' # 0x8A -> LATIN SMALL LETTER A WITH DIAERESIS u'\xe3' # 0x8B -> LATIN SMALL LETTER A WITH TILDE u'\xe5' # 0x8C -> LATIN SMALL LETTER A WITH RING ABOVE u'\xe7' # 0x8D -> LATIN SMALL LETTER C WITH CEDILLA u'\xe9' # 0x8E -> LATIN SMALL LETTER E WITH ACUTE u'\xe8' # 0x8F -> LATIN SMALL LETTER E WITH GRAVE u'\xea' # 0x90 -> LATIN SMALL LETTER E WITH CIRCUMFLEX u'\xeb' # 0x91 -> LATIN SMALL LETTER E WITH DIAERESIS u'\xed' # 0x92 -> LATIN SMALL LETTER I WITH ACUTE u'\xec' # 0x93 -> LATIN SMALL LETTER I WITH GRAVE u'\xee' # 0x94 -> LATIN SMALL LETTER I WITH CIRCUMFLEX u'\xef' # 0x95 -> LATIN SMALL LETTER I WITH DIAERESIS u'\xf1' # 0x96 -> LATIN SMALL LETTER N WITH TILDE u'\xf3' # 0x97 -> LATIN SMALL LETTER O WITH ACUTE u'\xf2' # 0x98 -> LATIN SMALL LETTER O WITH GRAVE u'\xf4' # 0x99 -> LATIN SMALL LETTER O WITH CIRCUMFLEX u'\xf6' # 0x9A -> LATIN SMALL LETTER O WITH DIAERESIS u'\xf5' # 0x9B -> LATIN SMALL LETTER O WITH TILDE u'\xfa' # 0x9C -> LATIN SMALL LETTER U WITH ACUTE u'\xf9' # 0x9D -> LATIN SMALL LETTER U WITH GRAVE u'\xfb' # 0x9E -> LATIN SMALL LETTER U WITH CIRCUMFLEX u'\xfc' # 0x9F -> LATIN SMALL LETTER U WITH DIAERESIS u'\u2020' # 0xA0 -> DAGGER u'\xb0' # 0xA1 -> DEGREE SIGN u'\xa2' # 0xA2 -> CENT SIGN u'\xa3' # 0xA3 -> POUND SIGN u'\xa7' # 0xA4 -> SECTION SIGN u'\u2022' # 0xA5 -> BULLET u'\xb6' # 0xA6 -> PILCROW SIGN u'\xdf' # 0xA7 -> LATIN SMALL LETTER SHARP S u'\xae' # 0xA8 -> REGISTERED SIGN u'\xa9' # 0xA9 -> COPYRIGHT SIGN u'\u2122' # 0xAA -> TRADE MARK SIGN u'\xb4' # 0xAB -> ACUTE ACCENT u'\xa8' # 0xAC -> DIAERESIS u'\u2260' # 0xAD -> NOT EQUAL TO u'\xc6' # 0xAE -> LATIN CAPITAL LETTER AE u'\xd8' # 0xAF -> LATIN CAPITAL LETTER O WITH STROKE u'\u221e' # 0xB0 -> INFINITY u'\xb1' # 0xB1 -> PLUS-MINUS SIGN u'\u2264' # 0xB2 -> LESS-THAN OR EQUAL TO u'\u2265' # 0xB3 -> GREATER-THAN OR EQUAL TO u'\xa5' # 0xB4 -> YEN SIGN u'\xb5' # 0xB5 -> MICRO SIGN u'\u2202' # 0xB6 -> PARTIAL DIFFERENTIAL u'\u2211' # 0xB7 -> N-ARY SUMMATION u'\u220f' # 0xB8 -> N-ARY PRODUCT u'\u03c0' # 0xB9 -> GREEK SMALL LETTER PI u'\u222b' # 0xBA -> INTEGRAL u'\xaa' # 0xBB -> FEMININE ORDINAL INDICATOR u'\xba' # 0xBC -> MASCULINE ORDINAL INDICATOR u'\u03a9' # 0xBD -> GREEK CAPITAL LETTER OMEGA u'\xe6' # 0xBE -> LATIN SMALL LETTER AE u'\xf8' # 0xBF -> LATIN SMALL LETTER O WITH STROKE u'\xbf' # 0xC0 -> INVERTED QUESTION MARK u'\xa1' # 0xC1 -> INVERTED EXCLAMATION MARK u'\xac' # 0xC2 -> NOT SIGN u'\u221a' # 0xC3 -> SQUARE ROOT u'\u0192' # 0xC4 -> LATIN SMALL LETTER F WITH HOOK u'\u2248' # 0xC5 -> ALMOST EQUAL TO u'\u2206' # 0xC6 -> INCREMENT u'\xab' # 0xC7 -> LEFT-POINTING DOUBLE ANGLE QUOTATION MARK u'\xbb' # 0xC8 -> RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK u'\u2026' # 0xC9 -> HORIZONTAL ELLIPSIS u'\xa0' # 0xCA -> NO-BREAK SPACE u'\xc0' # 0xCB -> LATIN CAPITAL LETTER A WITH GRAVE u'\xc3' # 0xCC -> LATIN CAPITAL LETTER A WITH TILDE u'\xd5' # 0xCD -> LATIN CAPITAL LETTER O WITH TILDE u'\u0152' # 0xCE -> LATIN CAPITAL LIGATURE OE u'\u0153' # 0xCF -> LATIN SMALL LIGATURE OE u'\u2013' # 0xD0 -> EN DASH u'\u2014' # 0xD1 -> EM DASH u'\u201c' # 0xD2 -> LEFT DOUBLE QUOTATION MARK u'\u201d' # 0xD3 -> RIGHT DOUBLE QUOTATION MARK u'\u2018' # 0xD4 -> LEFT SINGLE QUOTATION MARK u'\u2019' # 0xD5 -> RIGHT SINGLE QUOTATION MARK u'\xf7' # 0xD6 -> DIVISION SIGN u'\u25ca' # 0xD7 -> LOZENGE u'\xff' # 0xD8 -> LATIN SMALL LETTER Y WITH DIAERESIS u'\u0178' # 0xD9 -> LATIN CAPITAL LETTER Y WITH DIAERESIS u'\u2044' # 0xDA -> FRACTION SLASH u'\u20ac' # 0xDB -> EURO SIGN u'\u2039' # 0xDC -> SINGLE LEFT-POINTING ANGLE QUOTATION MARK u'\u203a' # 0xDD -> SINGLE RIGHT-POINTING ANGLE QUOTATION MARK u'\ufb01' # 0xDE -> LATIN SMALL LIGATURE FI u'\ufb02' # 0xDF -> LATIN SMALL LIGATURE FL u'\u2021' # 0xE0 -> DOUBLE DAGGER u'\xb7' # 0xE1 -> MIDDLE DOT u'\u201a' # 0xE2 -> SINGLE LOW-9 QUOTATION MARK u'\u201e' # 0xE3 -> DOUBLE LOW-9 QUOTATION MARK u'\u2030' # 0xE4 -> PER MILLE SIGN u'\xc2' # 0xE5 -> LATIN CAPITAL LETTER A WITH CIRCUMFLEX u'\xca' # 0xE6 -> LATIN CAPITAL LETTER E WITH CIRCUMFLEX u'\xc1' # 0xE7 -> LATIN CAPITAL LETTER A WITH ACUTE u'\xcb' # 0xE8 -> LATIN CAPITAL LETTER E WITH DIAERESIS u'\xc8' # 0xE9 -> LATIN CAPITAL LETTER E WITH GRAVE u'\xcd' # 0xEA -> LATIN CAPITAL LETTER I WITH ACUTE u'\xce' # 0xEB -> LATIN CAPITAL LETTER I WITH CIRCUMFLEX u'\xcf' # 0xEC -> LATIN CAPITAL LETTER I WITH DIAERESIS u'\xcc' # 0xED -> LATIN CAPITAL LETTER I WITH GRAVE u'\xd3' # 0xEE -> LATIN CAPITAL LETTER O WITH ACUTE u'\xd4' # 0xEF -> LATIN CAPITAL LETTER O WITH CIRCUMFLEX u'\uf8ff' # 0xF0 -> Apple logo u'\xd2' # 0xF1 -> LATIN CAPITAL LETTER O WITH GRAVE u'\xda' # 0xF2 -> LATIN CAPITAL LETTER U WITH ACUTE u'\xdb' # 0xF3 -> LATIN CAPITAL LETTER U WITH CIRCUMFLEX u'\xd9' # 0xF4 -> LATIN CAPITAL LETTER U WITH GRAVE u'\u0131' # 0xF5 -> LATIN SMALL LETTER DOTLESS I u'\u02c6' # 0xF6 -> MODIFIER LETTER CIRCUMFLEX ACCENT u'\u02dc' # 0xF7 -> SMALL TILDE u'\xaf' # 0xF8 -> MACRON u'\u02d8' # 0xF9 -> BREVE u'\u02d9' # 0xFA -> DOT ABOVE u'\u02da' # 0xFB -> RING ABOVE u'\xb8' # 0xFC -> CEDILLA u'\u02dd' # 0xFD -> DOUBLE ACUTE ACCENT u'\u02db' # 0xFE -> OGONEK u'\u02c7' # 0xFF -> CARON ) ### Encoding table encoding_table=codecs.charmap_build(decoding_table)
bsd-2-clause
JT5D/scikit-learn
sklearn/datasets/tests/test_mldata.py
384
5221
"""Test functionality of mldata fetching utilities.""" import os import shutil import tempfile import scipy as sp from sklearn import datasets from sklearn.datasets import mldata_filename, fetch_mldata from sklearn.utils.testing import assert_in from sklearn.utils.testing import assert_not_in from sklearn.utils.testing import mock_mldata_urlopen from sklearn.utils.testing import assert_equal from sklearn.utils.testing import assert_raises from sklearn.utils.testing import with_setup from sklearn.utils.testing import assert_array_equal tmpdir = None def setup_tmpdata(): # create temporary dir global tmpdir tmpdir = tempfile.mkdtemp() os.makedirs(os.path.join(tmpdir, 'mldata')) def teardown_tmpdata(): # remove temporary dir if tmpdir is not None: shutil.rmtree(tmpdir) def test_mldata_filename(): cases = [('datasets-UCI iris', 'datasets-uci-iris'), ('news20.binary', 'news20binary'), ('book-crossing-ratings-1.0', 'book-crossing-ratings-10'), ('Nile Water Level', 'nile-water-level'), ('MNIST (original)', 'mnist-original')] for name, desired in cases: assert_equal(mldata_filename(name), desired) @with_setup(setup_tmpdata, teardown_tmpdata) def test_download(): """Test that fetch_mldata is able to download and cache a data set.""" _urlopen_ref = datasets.mldata.urlopen datasets.mldata.urlopen = mock_mldata_urlopen({ 'mock': { 'label': sp.ones((150,)), 'data': sp.ones((150, 4)), }, }) try: mock = fetch_mldata('mock', data_home=tmpdir) for n in ["COL_NAMES", "DESCR", "target", "data"]: assert_in(n, mock) assert_equal(mock.target.shape, (150,)) assert_equal(mock.data.shape, (150, 4)) assert_raises(datasets.mldata.HTTPError, fetch_mldata, 'not_existing_name') finally: datasets.mldata.urlopen = _urlopen_ref @with_setup(setup_tmpdata, teardown_tmpdata) def test_fetch_one_column(): _urlopen_ref = datasets.mldata.urlopen try: dataname = 'onecol' # create fake data set in cache x = sp.arange(6).reshape(2, 3) datasets.mldata.urlopen = mock_mldata_urlopen({dataname: {'x': x}}) dset = fetch_mldata(dataname, data_home=tmpdir) for n in ["COL_NAMES", "DESCR", "data"]: assert_in(n, dset) assert_not_in("target", dset) assert_equal(dset.data.shape, (2, 3)) assert_array_equal(dset.data, x) # transposing the data array dset = fetch_mldata(dataname, transpose_data=False, data_home=tmpdir) assert_equal(dset.data.shape, (3, 2)) finally: datasets.mldata.urlopen = _urlopen_ref @with_setup(setup_tmpdata, teardown_tmpdata) def test_fetch_multiple_column(): _urlopen_ref = datasets.mldata.urlopen try: # create fake data set in cache x = sp.arange(6).reshape(2, 3) y = sp.array([1, -1]) z = sp.arange(12).reshape(4, 3) # by default dataname = 'threecol-default' datasets.mldata.urlopen = mock_mldata_urlopen({ dataname: ( { 'label': y, 'data': x, 'z': z, }, ['z', 'data', 'label'], ), }) dset = fetch_mldata(dataname, data_home=tmpdir) for n in ["COL_NAMES", "DESCR", "target", "data", "z"]: assert_in(n, dset) assert_not_in("x", dset) assert_not_in("y", dset) assert_array_equal(dset.data, x) assert_array_equal(dset.target, y) assert_array_equal(dset.z, z.T) # by order dataname = 'threecol-order' datasets.mldata.urlopen = mock_mldata_urlopen({ dataname: ({'y': y, 'x': x, 'z': z}, ['y', 'x', 'z']), }) dset = fetch_mldata(dataname, data_home=tmpdir) for n in ["COL_NAMES", "DESCR", "target", "data", "z"]: assert_in(n, dset) assert_not_in("x", dset) assert_not_in("y", dset) assert_array_equal(dset.data, x) assert_array_equal(dset.target, y) assert_array_equal(dset.z, z.T) # by number dataname = 'threecol-number' datasets.mldata.urlopen = mock_mldata_urlopen({ dataname: ({'y': y, 'x': x, 'z': z}, ['z', 'x', 'y']), }) dset = fetch_mldata(dataname, target_name=2, data_name=0, data_home=tmpdir) for n in ["COL_NAMES", "DESCR", "target", "data", "x"]: assert_in(n, dset) assert_not_in("y", dset) assert_not_in("z", dset) assert_array_equal(dset.data, z) assert_array_equal(dset.target, y) # by name dset = fetch_mldata(dataname, target_name='y', data_name='z', data_home=tmpdir) for n in ["COL_NAMES", "DESCR", "target", "data", "x"]: assert_in(n, dset) assert_not_in("y", dset) assert_not_in("z", dset) finally: datasets.mldata.urlopen = _urlopen_ref
bsd-3-clause
mfe5003/rydtip
Rb87BlockadeSimple.py
1
4940
# coding: utf-8 # In[1]: import scipy.constants as consts from lib.AtomNumbers import QD, Rb87, State, TransitionFrequency import lib.DipoleDipoleInteractions as ddi import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt from sympy import * import datetime get_ipython().magic(u'matplotlib inline') # In[2]: states = [] n_min = 50 n_max = 150 l_max = 1 # stop at f sI = State(97,2,2.5,2.5) mj_total = 2*sI.mj for n in range(n_min,n_max): for l in [sI.l-1,sI.l+1]: for j in np.arange(abs(l-0.5),l+1): # l+0.5 doesn't regiser the second j value for mj in np.arange(-j,j+0.5): s=State(n,l,j,mj) states.append([s, TransitionFrequency(Rb87,sI,s)]) print(len(states)) # ### calculate forster defects # In[3]: EnergyCut = 1e9 # only consider states within 1 Ghz molecular_states = [] for s1 in states: for s2 in states: # molecular dissociation energy limit relative to the initial state molecular_energy = s1[1][0]+s2[1][0] if abs(molecular_energy) <= EnergyCut: if s1[0].mj + s2[0].mj == mj_total: molecular_states.append([(s1[0],s2[0]), molecular_energy]) print(len(molecular_states)) # ### Build the Hamiltonian # In[4]: dimension = len(molecular_states)+1 # add on the initial state Hcoupling = np.zeros((dimension,dimension)) energies = np.zeros(dimension) for i in xrange(dimension-1): ms = molecular_states[i] temp = Rb87.c3(sI,sI,ms[0][0],ms[0][1])[0] Hcoupling[i][-1] = temp Hcoupling[-1][i] = temp energies[i] = ms[1]*1e-9 # in GHz # #### verify at large R (100 um) # In[5]: ddi.getRelevantCouplings(Hcoupling, energies, 100, 0.001) # #### Calculate blockade curves # In[6]: data = [] # In[7]: r_start = 3 r_stop = 15 samples = 500 r_last = 0 for r in np.linspace(r_start,r_stop,samples): current_time = datetime.datetime.now().time() if int(r) > int(r_last): print(r) print(current_time.isoformat()) r_last = r data = data + ddi.getRelevantCouplings(Hcoupling, energies, r, 0.01, 1.0) # In[8]: mark_r = [0.20202, 0.40404, 0.606061, 0.808081, 1.0101, 1.21212, 1.41414, 1.61616, 1.81818, 2.0202, 2.22222, 2.42424, 2.62626, 2.82828, 3.0303, 3.23232, 3.43434, 3.63636, 3.83838, 4.0404, 4.24242, 4.44444, 4.64646, 4.84848, 5.05051, 5.25253, 5.45455, 5.65657, 5.85859, 6.06061, 6.26263, 6.46465, 6.66667, 6.86869, 7.07071, 7.27273, 7.47475, 7.67677, 7.87879, 8.08081, 8.28283, 8.48485, 8.68687, 8.88889, 9.09091, 9.29293, 9.49495, 9.69697, 9.89899, 10.101, 10.303, 10.5051, 10.7071, 10.9091, 11.1111, 11.3131, 11.5152, 11.7172, 11.9192, 12.1212, 12.3232, 12.5253, 12.7273, 12.9293, 13.1313, 13.3333, 13.5354, 13.7374, 13.9394, 14.1414, 14.3434, 14.5455, 14.7475, 14.9495, 15.1515, 15.3535, 15.5556, 15.7576, 15.9596, 16.1616, 16.3636, 16.5657, 16.7677, 16.9697, 17.1717, 17.3737, 17.5758, 17.7778, 17.9798, 18.1818, 18.3838, 18.5859, 18.7879, 18.9899, 19.1919, 19.3939, 19.596, 19.798, 20.] mark_B = [-10966.3, -1370.79, -406.16, -171.349, -87.7303, -50.7695, -31.971, -21.4175, -15.0414, -10.9642, -8.23635, -6.34261, -4.98687, -3.99069, -3.24218, -2.66872, -2.22181, -1.86818, -1.58454, -1.35422, -1.16507, -1.00814, -0.876692, -0.76563, -0.671032, -0.589863, -0.51975, -0.458827, -0.405612, -0.358925, -0.317818, -0.281522, -0.24941, -0.220963, -0.195746, -0.17339, -0.153577, -0.136026, -0.120493, -0.106757, -0.0946198, -0.0839036, -0.074448, -0.0661083, -0.0587547, -0.0522711, -0.0465538, -0.0415106, -0.0370599, -0.0331295, -0.0296559, -0.0265832, -0.0238624, -0.0214505, -0.01931, -0.017408, -0.0157157, -0.0142081, -0.0128632, -0.0116618, -0.0105871, -0.00962452, -0.00876107, -0.00798552, -0.00728798, -0.00665975, -0.00609318, -0.00558157, -0.00511896, -0.00470014, -0.00432048, -0.0039759, -0.00366277, -0.00337787, -0.00311837, -0.00288173, -0.00266568, -0.00246822, -0.00228756, -0.00212208, -0.00197036, -0.0018311, -0.00170316, -0.0015855, -0.0014772, -0.00137741, -0.00128538, -0.00120043, -0.00112194, -0.00104937, -0.000982209, -0.00092, -0.000862334, -0.000808835, -0.000759164, -0.000713011, -0.000670096, -0.00063016, -0.000592972] # In[11]: plt.figure(figsize=(12,8)) x =[] x.append([ d[0] for d in data ]) x.append([ d[1] for d in data ]) x.append(np.real([ 1-sqrt(d[2]) for d in data ])) idx = x[2].argsort()[::-1] x[0] = [ x[0][i] for i in idx ] x[1] = [ x[1][i] for i in idx ] x[2] = [ x[2][i] for i in idx ] plt.scatter(x[0], x[1], c=x[2], marker='o', s=30, vmax=1, vmin=0, linewidth=0, alpha=1) plt.plot(mark_r,mark_B, color='r') plt.gray() plt.ylim(-0.5,0.5) plt.xlim(0,1.1*r_stop) plt.grid(b=True, which='major', color='0.5', linestyle='-') plt.grid(b=True, which='minor', color='0.75', linestyle='--') plt.title('Rybderg Blockade ${}+{}$, B=0 T'.format(sI,sI), fontsize=24) plt.xlabel('$R (\mu m)$', fontsize=20) plt.ylabel('U (GHz)', fontsize=20) #plt.savefig('MolecularResonances_B=370uT_97D52m52_97D52m52.pdf') plt.show() # In[ ]:
gpl-3.0
MobinRanjbar/hue
desktop/core/ext-py/Django-1.6.10/django/contrib/messages/tests/test_fallback.py
234
7033
from django.contrib.messages import constants from django.contrib.messages.storage.fallback import (FallbackStorage, CookieStorage) from django.contrib.messages.tests.base import BaseTests from django.contrib.messages.tests.test_cookie import (set_cookie_data, stored_cookie_messages_count) from django.contrib.messages.tests.test_session import (set_session_data, stored_session_messages_count) from django.test import TestCase class FallbackTest(BaseTests, TestCase): storage_class = FallbackStorage def get_request(self): self.session = {} request = super(FallbackTest, self).get_request() request.session = self.session return request def get_cookie_storage(self, storage): return storage.storages[-2] def get_session_storage(self, storage): return storage.storages[-1] def stored_cookie_messages_count(self, storage, response): return stored_cookie_messages_count(self.get_cookie_storage(storage), response) def stored_session_messages_count(self, storage, response): return stored_session_messages_count(self.get_session_storage(storage)) def stored_messages_count(self, storage, response): """ Return the storage totals from both cookie and session backends. """ total = (self.stored_cookie_messages_count(storage, response) + self.stored_session_messages_count(storage, response)) return total def test_get(self): request = self.get_request() storage = self.storage_class(request) cookie_storage = self.get_cookie_storage(storage) # Set initial cookie data. example_messages = [str(i) for i in range(5)] set_cookie_data(cookie_storage, example_messages) # Overwrite the _get method of the fallback storage to prove it is not # used (it would cause a TypeError: 'NoneType' object is not callable). self.get_session_storage(storage)._get = None # Test that the message actually contains what we expect. self.assertEqual(list(storage), example_messages) def test_get_empty(self): request = self.get_request() storage = self.storage_class(request) # Overwrite the _get method of the fallback storage to prove it is not # used (it would cause a TypeError: 'NoneType' object is not callable). self.get_session_storage(storage)._get = None # Test that the message actually contains what we expect. self.assertEqual(list(storage), []) def test_get_fallback(self): request = self.get_request() storage = self.storage_class(request) cookie_storage = self.get_cookie_storage(storage) session_storage = self.get_session_storage(storage) # Set initial cookie and session data. example_messages = [str(i) for i in range(5)] set_cookie_data(cookie_storage, example_messages[:4] + [CookieStorage.not_finished]) set_session_data(session_storage, example_messages[4:]) # Test that the message actually contains what we expect. self.assertEqual(list(storage), example_messages) def test_get_fallback_only(self): request = self.get_request() storage = self.storage_class(request) cookie_storage = self.get_cookie_storage(storage) session_storage = self.get_session_storage(storage) # Set initial cookie and session data. example_messages = [str(i) for i in range(5)] set_cookie_data(cookie_storage, [CookieStorage.not_finished], encode_empty=True) set_session_data(session_storage, example_messages) # Test that the message actually contains what we expect. self.assertEqual(list(storage), example_messages) def test_flush_used_backends(self): request = self.get_request() storage = self.storage_class(request) cookie_storage = self.get_cookie_storage(storage) session_storage = self.get_session_storage(storage) # Set initial cookie and session data. set_cookie_data(cookie_storage, ['cookie', CookieStorage.not_finished]) set_session_data(session_storage, ['session']) # When updating, previously used but no longer needed backends are # flushed. response = self.get_response() list(storage) storage.update(response) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 0) def test_no_fallback(self): """ Confirms that: (1) A short number of messages whose data size doesn't exceed what is allowed in a cookie will all be stored in the CookieBackend. (2) If the CookieBackend can store all messages, the SessionBackend won't be written to at all. """ storage = self.get_storage() response = self.get_response() # Overwrite the _store method of the fallback storage to prove it isn't # used (it would cause a TypeError: 'NoneType' object is not callable). self.get_session_storage(storage)._store = None for i in range(5): storage.add(constants.INFO, str(i) * 100) storage.update(response) cookie_storing = self.stored_cookie_messages_count(storage, response) self.assertEqual(cookie_storing, 5) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 0) def test_session_fallback(self): """ Confirms that, if the data exceeds what is allowed in a cookie, messages which did not fit are stored in the SessionBackend. """ storage = self.get_storage() response = self.get_response() # see comment in CookieText.test_cookie_max_length msg_size = int((CookieStorage.max_cookie_size - 54) / 4.5 - 37) for i in range(5): storage.add(constants.INFO, str(i) * msg_size) storage.update(response) cookie_storing = self.stored_cookie_messages_count(storage, response) self.assertEqual(cookie_storing, 4) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 1) def test_session_fallback_only(self): """ Confirms that large messages, none of which fit in a cookie, are stored in the SessionBackend (and nothing is stored in the CookieBackend). """ storage = self.get_storage() response = self.get_response() storage.add(constants.INFO, 'x' * 5000) storage.update(response) cookie_storing = self.stored_cookie_messages_count(storage, response) self.assertEqual(cookie_storing, 0) session_storing = self.stored_session_messages_count(storage, response) self.assertEqual(session_storing, 1)
apache-2.0
underyx/ansible
v2/ansible/compat/tests/mock.py
258
1241
# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com> # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. # Make coding more python3-ish from __future__ import (absolute_import, division, print_function) __metaclass__ = type ''' Compat module for Python3.x's unittest.mock module ''' # Python 2.7 # Note: Could use the pypi mock library on python3.x as well as python2.x. It # is the same as the python3 stdlib mock library try: from unittest.mock import * except ImportError: # Python 2 try: from mock import * except ImportError: print('You need the mock library installed on python2.x to run tests')
gpl-3.0
paulcalabro/zato
code/alembic/versions/0020_bf41328c_gh290_sql_notifications.py
7
1737
"""gh290 sql notifications Revision ID: 0020_bf41328c Revises: 0019_e77a8c06 Create Date: 2014-07-24 20:05:05 """ # revision identifiers, used by Alembic. revision = '0020_bf41328c' down_revision = '0019_e77a8c06' from alembic import op import sqlalchemy as sa # Zato from zato.common.odb import model # ################################################################################################################################ def upgrade(): op.create_table( model.NotificationSQL.__tablename__, sa.Column('id', sa.Integer(), sa.ForeignKey('notif.id'), primary_key=True), sa.Column('query', sa.String(200000), nullable=False), sa.Column('def_id', sa.Integer(), sa.ForeignKey('sql_pool.id'), primary_key=True), ) op.alter_column(model.Notification.__tablename__, 'name_pattern', nullable=True) op.alter_column(model.Notification.__tablename__, 'name_pattern_neg', nullable=True) op.alter_column(model.Notification.__tablename__, 'get_data', nullable=True) op.alter_column(model.Notification.__tablename__, 'get_data_patt', nullable=True) op.alter_column(model.Notification.__tablename__, 'get_data_patt_neg', nullable=True) def downgrade(): op.drop_table(model.NotificationSQL.__tablename__) op.alter_column(model.Notification.__tablename__, 'name_pattern', nullable=False) op.alter_column(model.Notification.__tablename__, 'name_pattern_neg', nullable=False) op.alter_column(model.Notification.__tablename__, 'get_data', nullable=False) op.alter_column(model.Notification.__tablename__, 'get_data_patt', nullable=False) op.alter_column(model.Notification.__tablename__, 'get_data_patt_neg', nullable=False)
gpl-3.0
StrellaGroup/erpnext
erpnext/patches/v11_0/make_quality_inspection_template.py
18
1175
# Copyright (c) 2017, Frappe and Contributors # License: GNU General Public License v3. See license.txt from __future__ import unicode_literals import frappe def execute(): frappe.reload_doc('stock', 'doctype', 'quality_inspection_template') frappe.reload_doc('stock', 'doctype', 'item') for data in frappe.get_all('Item Quality Inspection Parameter', fields = ["distinct parent"], filters = {'parenttype': 'Item'}): qc_doc = frappe.new_doc("Quality Inspection Template") qc_doc.quality_inspection_template_name = 'QIT/%s' % data.parent qc_doc.flags.ignore_mandatory = True qc_doc.save(ignore_permissions=True) frappe.db.set_value('Item', data.parent, "quality_inspection_template", qc_doc.name, update_modified=False) frappe.db.sql(""" update `tabItem Quality Inspection Parameter` set parentfield = 'item_quality_inspection_parameter', parenttype = 'Quality Inspection Template', parent = %s where parenttype = 'Item' and parent = %s""", (qc_doc.name, data.parent)) # update field in item variant settings frappe.db.sql(""" update `tabVariant Field` set field_name = 'quality_inspection_template' where field_name = 'quality_parameters'""")
gpl-3.0
Lamecarlate/gourmet
gourmet/plugins/nutritional_information/nutPrefsPlugin.py
7
1614
from gourmet.plugin import PrefsPlugin from gourmet.prefs import get_prefs import gtk from gettext import gettext as _ partialp = 'include_partial_nutritional_info' includep = 'include_nutritional_info_in_export' class NutritionPrefs (PrefsPlugin): label = _("Nutritional Information") def __init__ (self, *args, **kwargs): # Create main widget self.widget = gtk.VBox() self.prefs = get_prefs() label = gtk.Label('Hello world') self.include_tb = gtk.CheckButton('Include nutritional information in print-outs and exports') self.partial_tb = gtk.CheckButton('Include partial nutritional information in print-outs and exports?') self.include_tb.set_active(self.prefs.get(includep,True)) self.partial_tb.set_active(self.prefs.get(partialp,False)) self.include_tb.connect('toggled',self.toggle_cb) self.partial_tb.connect('toggled',self.toggle_cb) self.widget.pack_start(self.include_tb, expand=False, fill=False) self.widget.pack_start(self.partial_tb, expand=False, fill=False) self.widget.set_border_width(12) self.widget.set_spacing(6) self.widget.show_all() def toggle_cb (self, tb): if tb==self.include_tb: if tb.get_active(): self.prefs[includep] = True else: self.prefs[includep] = False # Force false... self.partial_tb.set_active(False) self.prefs[partialp] = False if tb == self.partial_tb: self.prefs[partialp] = tb.get_active()
gpl-2.0
dbarobin/google-mysql-tools
generate_checksums.py
4
4320
#!/usr/bin/python2 # # Copyright 2011 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. """Generate checksums on a database, for online data drift checking. More information is available here: http://code.google.com/p/google-mysql-tools/wiki/OnlineDataDrift Original author: Ben Handy Later maintainer: Mikey Dickerson """ import gflags from drift_lib import drift_lib from pylib import app from pylib import db FLAGS = gflags.FLAGS gflags.DEFINE_string('db', None, 'DB spec to a primary and the database to checksum') gflags.DEFINE_integer('hours_to_run', None, 'Total time allotted to compute all checksums') gflags.DEFINE_float('utilization', 0.02, 'Fraction of time to query db (overrides hours_to_run)') gflags.DEFINE_integer('rows_per_query', None, 'Suggested size of a single checksum query (rows)') gflags.DEFINE_float('secs_per_query', 1.0, 'Duration (seconds) per query (overrides rows_per_query)') gflags.DEFINE_integer('scan_rate', 10000000, 'Estimated checksum speed in input-bytes per second') gflags.DEFINE_string('column_types_to_skip', 'blob,longblob', 'Comma separated list of datatypes to skip checksumming') gflags.DEFINE_multistring('skip_table', [], 'Table to skip checksumming') gflags.DEFINE_multistring('skip_db', ['information_schema', 'adminlocal'], 'Database to skip checksumming') gflags.DEFINE_multistring('check_table', [], 'Comma separated list of tables to checksum') gflags.DEFINE_multistring('check_engine', ['InnoDB'], 'Storage engines to checksum') gflags.DEFINE_string('result_table', 'admin.Checksums', 'Name of the table containing the resulting checksums') gflags.DEFINE_string('golden_table', 'admin.ChecksumsGolden', 'Name of the db.table for correct checksums results') gflags.DEFINE_string('log_table', 'admin.ChecksumLog', 'Name of the db.table where we log completed runs') gflags.DEFINE_string('job_started', None, 'Checksum job started timestamp (defaults to now)') gflags.DEFINE_string('row_condition', '', 'SQL condition added to checksum query for all tables') def main(unused_argv): """This drives the standalone checksumming app. This function sets up the parameters for a DbChecksummer, passing the FLAGS parameters in as the droid-compatible config dictionary. """ assert FLAGS.db, 'Please pass --db' dbh = db.Connect(FLAGS.db) db_checksummer = drift_lib.DbChecksummer(dbh=dbh, result_table=FLAGS.result_table, golden_table=FLAGS.golden_table, log_table=FLAGS.log_table, job_started=FLAGS.job_started, scan_rate=FLAGS.scan_rate, secs_per_query=FLAGS.secs_per_query, rows_per_query=FLAGS.rows_per_query, hours_to_run=FLAGS.hours_to_run, utilization=FLAGS.utilization, tables_to_skip=FLAGS.skip_table, databases_to_skip=FLAGS.skip_db, engines_to_check=FLAGS.check_engine, tables_to_check=FLAGS.check_table) db_checksummer.ChecksumTables() dbh.Close() if __name__ == '__main__': app.run()
apache-2.0
harshilasu/LinkurApp
y/google-cloud-sdk/lib/jinja2/lexer.py
635
28393
# -*- coding: utf-8 -*- """ jinja2.lexer ~~~~~~~~~~~~ This module implements a Jinja / Python combination lexer. The `Lexer` class provided by this module is used to do some preprocessing for Jinja. On the one hand it filters out invalid operators like the bitshift operators we don't allow in templates. On the other hand it separates template code and python code in expressions. :copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. """ import re from operator import itemgetter from collections import deque from jinja2.exceptions import TemplateSyntaxError from jinja2.utils import LRUCache from jinja2._compat import next, iteritems, implements_iterator, text_type, \ intern # cache for the lexers. Exists in order to be able to have multiple # environments with the same lexer _lexer_cache = LRUCache(50) # static regular expressions whitespace_re = re.compile(r'\s+', re.U) string_re = re.compile(r"('([^'\\]*(?:\\.[^'\\]*)*)'" r'|"([^"\\]*(?:\\.[^"\\]*)*)")', re.S) integer_re = re.compile(r'\d+') # we use the unicode identifier rule if this python version is able # to handle unicode identifiers, otherwise the standard ASCII one. try: compile('föö', '<unknown>', 'eval') except SyntaxError: name_re = re.compile(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b') else: from jinja2 import _stringdefs name_re = re.compile(r'[%s][%s]*' % (_stringdefs.xid_start, _stringdefs.xid_continue)) float_re = re.compile(r'(?<!\.)\d+\.\d+') newline_re = re.compile(r'(\r\n|\r|\n)') # internal the tokens and keep references to them TOKEN_ADD = intern('add') TOKEN_ASSIGN = intern('assign') TOKEN_COLON = intern('colon') TOKEN_COMMA = intern('comma') TOKEN_DIV = intern('div') TOKEN_DOT = intern('dot') TOKEN_EQ = intern('eq') TOKEN_FLOORDIV = intern('floordiv') TOKEN_GT = intern('gt') TOKEN_GTEQ = intern('gteq') TOKEN_LBRACE = intern('lbrace') TOKEN_LBRACKET = intern('lbracket') TOKEN_LPAREN = intern('lparen') TOKEN_LT = intern('lt') TOKEN_LTEQ = intern('lteq') TOKEN_MOD = intern('mod') TOKEN_MUL = intern('mul') TOKEN_NE = intern('ne') TOKEN_PIPE = intern('pipe') TOKEN_POW = intern('pow') TOKEN_RBRACE = intern('rbrace') TOKEN_RBRACKET = intern('rbracket') TOKEN_RPAREN = intern('rparen') TOKEN_SEMICOLON = intern('semicolon') TOKEN_SUB = intern('sub') TOKEN_TILDE = intern('tilde') TOKEN_WHITESPACE = intern('whitespace') TOKEN_FLOAT = intern('float') TOKEN_INTEGER = intern('integer') TOKEN_NAME = intern('name') TOKEN_STRING = intern('string') TOKEN_OPERATOR = intern('operator') TOKEN_BLOCK_BEGIN = intern('block_begin') TOKEN_BLOCK_END = intern('block_end') TOKEN_VARIABLE_BEGIN = intern('variable_begin') TOKEN_VARIABLE_END = intern('variable_end') TOKEN_RAW_BEGIN = intern('raw_begin') TOKEN_RAW_END = intern('raw_end') TOKEN_COMMENT_BEGIN = intern('comment_begin') TOKEN_COMMENT_END = intern('comment_end') TOKEN_COMMENT = intern('comment') TOKEN_LINESTATEMENT_BEGIN = intern('linestatement_begin') TOKEN_LINESTATEMENT_END = intern('linestatement_end') TOKEN_LINECOMMENT_BEGIN = intern('linecomment_begin') TOKEN_LINECOMMENT_END = intern('linecomment_end') TOKEN_LINECOMMENT = intern('linecomment') TOKEN_DATA = intern('data') TOKEN_INITIAL = intern('initial') TOKEN_EOF = intern('eof') # bind operators to token types operators = { '+': TOKEN_ADD, '-': TOKEN_SUB, '/': TOKEN_DIV, '//': TOKEN_FLOORDIV, '*': TOKEN_MUL, '%': TOKEN_MOD, '**': TOKEN_POW, '~': TOKEN_TILDE, '[': TOKEN_LBRACKET, ']': TOKEN_RBRACKET, '(': TOKEN_LPAREN, ')': TOKEN_RPAREN, '{': TOKEN_LBRACE, '}': TOKEN_RBRACE, '==': TOKEN_EQ, '!=': TOKEN_NE, '>': TOKEN_GT, '>=': TOKEN_GTEQ, '<': TOKEN_LT, '<=': TOKEN_LTEQ, '=': TOKEN_ASSIGN, '.': TOKEN_DOT, ':': TOKEN_COLON, '|': TOKEN_PIPE, ',': TOKEN_COMMA, ';': TOKEN_SEMICOLON } reverse_operators = dict([(v, k) for k, v in iteritems(operators)]) assert len(operators) == len(reverse_operators), 'operators dropped' operator_re = re.compile('(%s)' % '|'.join(re.escape(x) for x in sorted(operators, key=lambda x: -len(x)))) ignored_tokens = frozenset([TOKEN_COMMENT_BEGIN, TOKEN_COMMENT, TOKEN_COMMENT_END, TOKEN_WHITESPACE, TOKEN_WHITESPACE, TOKEN_LINECOMMENT_BEGIN, TOKEN_LINECOMMENT_END, TOKEN_LINECOMMENT]) ignore_if_empty = frozenset([TOKEN_WHITESPACE, TOKEN_DATA, TOKEN_COMMENT, TOKEN_LINECOMMENT]) def _describe_token_type(token_type): if token_type in reverse_operators: return reverse_operators[token_type] return { TOKEN_COMMENT_BEGIN: 'begin of comment', TOKEN_COMMENT_END: 'end of comment', TOKEN_COMMENT: 'comment', TOKEN_LINECOMMENT: 'comment', TOKEN_BLOCK_BEGIN: 'begin of statement block', TOKEN_BLOCK_END: 'end of statement block', TOKEN_VARIABLE_BEGIN: 'begin of print statement', TOKEN_VARIABLE_END: 'end of print statement', TOKEN_LINESTATEMENT_BEGIN: 'begin of line statement', TOKEN_LINESTATEMENT_END: 'end of line statement', TOKEN_DATA: 'template data / text', TOKEN_EOF: 'end of template' }.get(token_type, token_type) def describe_token(token): """Returns a description of the token.""" if token.type == 'name': return token.value return _describe_token_type(token.type) def describe_token_expr(expr): """Like `describe_token` but for token expressions.""" if ':' in expr: type, value = expr.split(':', 1) if type == 'name': return value else: type = expr return _describe_token_type(type) def count_newlines(value): """Count the number of newline characters in the string. This is useful for extensions that filter a stream. """ return len(newline_re.findall(value)) def compile_rules(environment): """Compiles all the rules from the environment into a list of rules.""" e = re.escape rules = [ (len(environment.comment_start_string), 'comment', e(environment.comment_start_string)), (len(environment.block_start_string), 'block', e(environment.block_start_string)), (len(environment.variable_start_string), 'variable', e(environment.variable_start_string)) ] if environment.line_statement_prefix is not None: rules.append((len(environment.line_statement_prefix), 'linestatement', r'^[ \t\v]*' + e(environment.line_statement_prefix))) if environment.line_comment_prefix is not None: rules.append((len(environment.line_comment_prefix), 'linecomment', r'(?:^|(?<=\S))[^\S\r\n]*' + e(environment.line_comment_prefix))) return [x[1:] for x in sorted(rules, reverse=True)] class Failure(object): """Class that raises a `TemplateSyntaxError` if called. Used by the `Lexer` to specify known errors. """ def __init__(self, message, cls=TemplateSyntaxError): self.message = message self.error_class = cls def __call__(self, lineno, filename): raise self.error_class(self.message, lineno, filename) class Token(tuple): """Token class.""" __slots__ = () lineno, type, value = (property(itemgetter(x)) for x in range(3)) def __new__(cls, lineno, type, value): return tuple.__new__(cls, (lineno, intern(str(type)), value)) def __str__(self): if self.type in reverse_operators: return reverse_operators[self.type] elif self.type == 'name': return self.value return self.type def test(self, expr): """Test a token against a token expression. This can either be a token type or ``'token_type:token_value'``. This can only test against string values and types. """ # here we do a regular string equality check as test_any is usually # passed an iterable of not interned strings. if self.type == expr: return True elif ':' in expr: return expr.split(':', 1) == [self.type, self.value] return False def test_any(self, *iterable): """Test against multiple token expressions.""" for expr in iterable: if self.test(expr): return True return False def __repr__(self): return 'Token(%r, %r, %r)' % ( self.lineno, self.type, self.value ) @implements_iterator class TokenStreamIterator(object): """The iterator for tokenstreams. Iterate over the stream until the eof token is reached. """ def __init__(self, stream): self.stream = stream def __iter__(self): return self def __next__(self): token = self.stream.current if token.type is TOKEN_EOF: self.stream.close() raise StopIteration() next(self.stream) return token @implements_iterator class TokenStream(object): """A token stream is an iterable that yields :class:`Token`\s. The parser however does not iterate over it but calls :meth:`next` to go one token ahead. The current active token is stored as :attr:`current`. """ def __init__(self, generator, name, filename): self._iter = iter(generator) self._pushed = deque() self.name = name self.filename = filename self.closed = False self.current = Token(1, TOKEN_INITIAL, '') next(self) def __iter__(self): return TokenStreamIterator(self) def __bool__(self): return bool(self._pushed) or self.current.type is not TOKEN_EOF __nonzero__ = __bool__ # py2 eos = property(lambda x: not x, doc="Are we at the end of the stream?") def push(self, token): """Push a token back to the stream.""" self._pushed.append(token) def look(self): """Look at the next token.""" old_token = next(self) result = self.current self.push(result) self.current = old_token return result def skip(self, n=1): """Got n tokens ahead.""" for x in range(n): next(self) def next_if(self, expr): """Perform the token test and return the token if it matched. Otherwise the return value is `None`. """ if self.current.test(expr): return next(self) def skip_if(self, expr): """Like :meth:`next_if` but only returns `True` or `False`.""" return self.next_if(expr) is not None def __next__(self): """Go one token ahead and return the old one""" rv = self.current if self._pushed: self.current = self._pushed.popleft() elif self.current.type is not TOKEN_EOF: try: self.current = next(self._iter) except StopIteration: self.close() return rv def close(self): """Close the stream.""" self.current = Token(self.current.lineno, TOKEN_EOF, '') self._iter = None self.closed = True def expect(self, expr): """Expect a given token type and return it. This accepts the same argument as :meth:`jinja2.lexer.Token.test`. """ if not self.current.test(expr): expr = describe_token_expr(expr) if self.current.type is TOKEN_EOF: raise TemplateSyntaxError('unexpected end of template, ' 'expected %r.' % expr, self.current.lineno, self.name, self.filename) raise TemplateSyntaxError("expected token %r, got %r" % (expr, describe_token(self.current)), self.current.lineno, self.name, self.filename) try: return self.current finally: next(self) def get_lexer(environment): """Return a lexer which is probably cached.""" key = (environment.block_start_string, environment.block_end_string, environment.variable_start_string, environment.variable_end_string, environment.comment_start_string, environment.comment_end_string, environment.line_statement_prefix, environment.line_comment_prefix, environment.trim_blocks, environment.lstrip_blocks, environment.newline_sequence, environment.keep_trailing_newline) lexer = _lexer_cache.get(key) if lexer is None: lexer = Lexer(environment) _lexer_cache[key] = lexer return lexer class Lexer(object): """Class that implements a lexer for a given environment. Automatically created by the environment class, usually you don't have to do that. Note that the lexer is not automatically bound to an environment. Multiple environments can share the same lexer. """ def __init__(self, environment): # shortcuts c = lambda x: re.compile(x, re.M | re.S) e = re.escape # lexing rules for tags tag_rules = [ (whitespace_re, TOKEN_WHITESPACE, None), (float_re, TOKEN_FLOAT, None), (integer_re, TOKEN_INTEGER, None), (name_re, TOKEN_NAME, None), (string_re, TOKEN_STRING, None), (operator_re, TOKEN_OPERATOR, None) ] # assemble the root lexing rule. because "|" is ungreedy # we have to sort by length so that the lexer continues working # as expected when we have parsing rules like <% for block and # <%= for variables. (if someone wants asp like syntax) # variables are just part of the rules if variable processing # is required. root_tag_rules = compile_rules(environment) # block suffix if trimming is enabled block_suffix_re = environment.trim_blocks and '\\n?' or '' # strip leading spaces if lstrip_blocks is enabled prefix_re = {} if environment.lstrip_blocks: # use '{%+' to manually disable lstrip_blocks behavior no_lstrip_re = e('+') # detect overlap between block and variable or comment strings block_diff = c(r'^%s(.*)' % e(environment.block_start_string)) # make sure we don't mistake a block for a variable or a comment m = block_diff.match(environment.comment_start_string) no_lstrip_re += m and r'|%s' % e(m.group(1)) or '' m = block_diff.match(environment.variable_start_string) no_lstrip_re += m and r'|%s' % e(m.group(1)) or '' # detect overlap between comment and variable strings comment_diff = c(r'^%s(.*)' % e(environment.comment_start_string)) m = comment_diff.match(environment.variable_start_string) no_variable_re = m and r'(?!%s)' % e(m.group(1)) or '' lstrip_re = r'^[ \t]*' block_prefix_re = r'%s%s(?!%s)|%s\+?' % ( lstrip_re, e(environment.block_start_string), no_lstrip_re, e(environment.block_start_string), ) comment_prefix_re = r'%s%s%s|%s\+?' % ( lstrip_re, e(environment.comment_start_string), no_variable_re, e(environment.comment_start_string), ) prefix_re['block'] = block_prefix_re prefix_re['comment'] = comment_prefix_re else: block_prefix_re = '%s' % e(environment.block_start_string) self.newline_sequence = environment.newline_sequence self.keep_trailing_newline = environment.keep_trailing_newline # global lexing rules self.rules = { 'root': [ # directives (c('(.*?)(?:%s)' % '|'.join( [r'(?P<raw_begin>(?:\s*%s\-|%s)\s*raw\s*(?:\-%s\s*|%s))' % ( e(environment.block_start_string), block_prefix_re, e(environment.block_end_string), e(environment.block_end_string) )] + [ r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, prefix_re.get(n,r)) for n, r in root_tag_rules ])), (TOKEN_DATA, '#bygroup'), '#bygroup'), # data (c('.+'), TOKEN_DATA, None) ], # comments TOKEN_COMMENT_BEGIN: [ (c(r'(.*?)((?:\-%s\s*|%s)%s)' % ( e(environment.comment_end_string), e(environment.comment_end_string), block_suffix_re )), (TOKEN_COMMENT, TOKEN_COMMENT_END), '#pop'), (c('(.)'), (Failure('Missing end of comment tag'),), None) ], # blocks TOKEN_BLOCK_BEGIN: [ (c('(?:\-%s\s*|%s)%s' % ( e(environment.block_end_string), e(environment.block_end_string), block_suffix_re )), TOKEN_BLOCK_END, '#pop'), ] + tag_rules, # variables TOKEN_VARIABLE_BEGIN: [ (c('\-%s\s*|%s' % ( e(environment.variable_end_string), e(environment.variable_end_string) )), TOKEN_VARIABLE_END, '#pop') ] + tag_rules, # raw block TOKEN_RAW_BEGIN: [ (c('(.*?)((?:\s*%s\-|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % ( e(environment.block_start_string), block_prefix_re, e(environment.block_end_string), e(environment.block_end_string), block_suffix_re )), (TOKEN_DATA, TOKEN_RAW_END), '#pop'), (c('(.)'), (Failure('Missing end of raw directive'),), None) ], # line statements TOKEN_LINESTATEMENT_BEGIN: [ (c(r'\s*(\n|$)'), TOKEN_LINESTATEMENT_END, '#pop') ] + tag_rules, # line comments TOKEN_LINECOMMENT_BEGIN: [ (c(r'(.*?)()(?=\n|$)'), (TOKEN_LINECOMMENT, TOKEN_LINECOMMENT_END), '#pop') ] } def _normalize_newlines(self, value): """Called for strings and template data to normalize it to unicode.""" return newline_re.sub(self.newline_sequence, value) def tokenize(self, source, name=None, filename=None, state=None): """Calls tokeniter + tokenize and wraps it in a token stream. """ stream = self.tokeniter(source, name, filename, state) return TokenStream(self.wrap(stream, name, filename), name, filename) def wrap(self, stream, name=None, filename=None): """This is called with the stream as returned by `tokenize` and wraps every token in a :class:`Token` and converts the value. """ for lineno, token, value in stream: if token in ignored_tokens: continue elif token == 'linestatement_begin': token = 'block_begin' elif token == 'linestatement_end': token = 'block_end' # we are not interested in those tokens in the parser elif token in ('raw_begin', 'raw_end'): continue elif token == 'data': value = self._normalize_newlines(value) elif token == 'keyword': token = value elif token == 'name': value = str(value) elif token == 'string': # try to unescape string try: value = self._normalize_newlines(value[1:-1]) \ .encode('ascii', 'backslashreplace') \ .decode('unicode-escape') except Exception as e: msg = str(e).split(':')[-1].strip() raise TemplateSyntaxError(msg, lineno, name, filename) # if we can express it as bytestring (ascii only) # we do that for support of semi broken APIs # as datetime.datetime.strftime. On python 3 this # call becomes a noop thanks to 2to3 try: value = str(value) except UnicodeError: pass elif token == 'integer': value = int(value) elif token == 'float': value = float(value) elif token == 'operator': token = operators[value] yield Token(lineno, token, value) def tokeniter(self, source, name, filename=None, state=None): """This method tokenizes the text and returns the tokens in a generator. Use this method if you just want to tokenize a template. """ source = text_type(source) lines = source.splitlines() if self.keep_trailing_newline and source: for newline in ('\r\n', '\r', '\n'): if source.endswith(newline): lines.append('') break source = '\n'.join(lines) pos = 0 lineno = 1 stack = ['root'] if state is not None and state != 'root': assert state in ('variable', 'block'), 'invalid state' stack.append(state + '_begin') else: state = 'root' statetokens = self.rules[stack[-1]] source_length = len(source) balancing_stack = [] while 1: # tokenizer loop for regex, tokens, new_state in statetokens: m = regex.match(source, pos) # if no match we try again with the next rule if m is None: continue # we only match blocks and variables if braces / parentheses # are balanced. continue parsing with the lower rule which # is the operator rule. do this only if the end tags look # like operators if balancing_stack and \ tokens in ('variable_end', 'block_end', 'linestatement_end'): continue # tuples support more options if isinstance(tokens, tuple): for idx, token in enumerate(tokens): # failure group if token.__class__ is Failure: raise token(lineno, filename) # bygroup is a bit more complex, in that case we # yield for the current token the first named # group that matched elif token == '#bygroup': for key, value in iteritems(m.groupdict()): if value is not None: yield lineno, key, value lineno += value.count('\n') break else: raise RuntimeError('%r wanted to resolve ' 'the token dynamically' ' but no group matched' % regex) # normal group else: data = m.group(idx + 1) if data or token not in ignore_if_empty: yield lineno, token, data lineno += data.count('\n') # strings as token just are yielded as it. else: data = m.group() # update brace/parentheses balance if tokens == 'operator': if data == '{': balancing_stack.append('}') elif data == '(': balancing_stack.append(')') elif data == '[': balancing_stack.append(']') elif data in ('}', ')', ']'): if not balancing_stack: raise TemplateSyntaxError('unexpected \'%s\'' % data, lineno, name, filename) expected_op = balancing_stack.pop() if expected_op != data: raise TemplateSyntaxError('unexpected \'%s\', ' 'expected \'%s\'' % (data, expected_op), lineno, name, filename) # yield items if data or tokens not in ignore_if_empty: yield lineno, tokens, data lineno += data.count('\n') # fetch new position into new variable so that we can check # if there is a internal parsing error which would result # in an infinite loop pos2 = m.end() # handle state changes if new_state is not None: # remove the uppermost state if new_state == '#pop': stack.pop() # resolve the new state by group checking elif new_state == '#bygroup': for key, value in iteritems(m.groupdict()): if value is not None: stack.append(key) break else: raise RuntimeError('%r wanted to resolve the ' 'new state dynamically but' ' no group matched' % regex) # direct state name given else: stack.append(new_state) statetokens = self.rules[stack[-1]] # we are still at the same position and no stack change. # this means a loop without break condition, avoid that and # raise error elif pos2 == pos: raise RuntimeError('%r yielded empty string without ' 'stack change' % regex) # publish new function and start again pos = pos2 break # if loop terminated without break we haven't found a single match # either we are at the end of the file or we have a problem else: # end of text if pos >= source_length: return # something went wrong raise TemplateSyntaxError('unexpected char %r at %d' % (source[pos], pos), lineno, name, filename)
gpl-3.0
embeddedarm/android_external_chromium_org
chrome/test/functional/ispy/server/gs_bucket.py
23
1934
# Copyright 2013 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Implementation of CloudBucket using Google Cloud Storage as the backend.""" import os import sys import cloudstorage from ..common import cloud_bucket class GoogleCloudStorageBucket(cloud_bucket.BaseCloudBucket): """Subclass of cloud_bucket.CloudBucket with actual GS commands.""" def __init__(self, bucket): """Initializes the bucket. Args: bucket: the name of the bucket to connect to. """ self.bucket = '/' + bucket def _full_path(self, path): return self.bucket + '/' + path.lstrip('/') # override def UploadFile(self, path, contents, content_type): gs_file = cloudstorage.open( self._full_path(path), 'w', content_type=content_type) gs_file.write(contents) gs_file.close() # override def DownloadFile(self, path): try: gs_file = cloudstorage.open(self._full_path(path), 'r') r = gs_file.read() gs_file.close() except Exception as e: raise Exception('%s: %s' % (self._full_path(path), str(e))) return r # override def UpdateFile(self, path, contents): if not self.FileExists(path): raise cloud_bucket.FileNotFoundError gs_file = cloudstorage.open(self._full_path(path), 'w') gs_file.write(contents) gs_file.close() # override def RemoveFile(self, path): cloudstorage.delete(self._full_path(path)) # override def FileExists(self, path): try: cloudstorage.stat(self._full_path(path)) except cloudstorage.NotFoundError: return False return True # override def GetImageURL(self, path): return '/image?file_path=%s' % path # override def GetAllPaths(self, prefix): return (f.filename[len(self.bucket) + 1:] for f in cloudstorage.listbucket(self.bucket, prefix=prefix))
bsd-3-clause
kenwang815/KodiPlugins
script.module.youtube.dl/lib/youtube_dl/extractor/animeondemand.py
10
9936
from __future__ import unicode_literals import re from .common import InfoExtractor from ..compat import ( compat_urlparse, compat_str, ) from ..utils import ( determine_ext, extract_attributes, ExtractorError, sanitized_Request, urlencode_postdata, ) class AnimeOnDemandIE(InfoExtractor): _VALID_URL = r'https?://(?:www\.)?anime-on-demand\.de/anime/(?P<id>\d+)' _LOGIN_URL = 'https://www.anime-on-demand.de/users/sign_in' _APPLY_HTML5_URL = 'https://www.anime-on-demand.de/html5apply' _NETRC_MACHINE = 'animeondemand' _TESTS = [{ 'url': 'https://www.anime-on-demand.de/anime/161', 'info_dict': { 'id': '161', 'title': 'Grimgar, Ashes and Illusions (OmU)', 'description': 'md5:6681ce3c07c7189d255ac6ab23812d31', }, 'playlist_mincount': 4, }, { # Film wording is used instead of Episode 'url': 'https://www.anime-on-demand.de/anime/39', 'only_matching': True, }, { # Episodes without titles 'url': 'https://www.anime-on-demand.de/anime/162', 'only_matching': True, }, { # ger/jap, Dub/OmU, account required 'url': 'https://www.anime-on-demand.de/anime/169', 'only_matching': True, }] def _login(self): (username, password) = self._get_login_info() if username is None: return login_page = self._download_webpage( self._LOGIN_URL, None, 'Downloading login page') if '>Our licensing terms allow the distribution of animes only to German-speaking countries of Europe' in login_page: self.raise_geo_restricted( '%s is only available in German-speaking countries of Europe' % self.IE_NAME) login_form = self._form_hidden_inputs('new_user', login_page) login_form.update({ 'user[login]': username, 'user[password]': password, }) post_url = self._search_regex( r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page, 'post url', default=self._LOGIN_URL, group='url') if not post_url.startswith('http'): post_url = compat_urlparse.urljoin(self._LOGIN_URL, post_url) request = sanitized_Request( post_url, urlencode_postdata(login_form)) request.add_header('Referer', self._LOGIN_URL) response = self._download_webpage( request, None, 'Logging in as %s' % username) if all(p not in response for p in ('>Logout<', 'href="/users/sign_out"')): error = self._search_regex( r'<p class="alert alert-danger">(.+?)</p>', response, 'error', default=None) if error: raise ExtractorError('Unable to login: %s' % error, expected=True) raise ExtractorError('Unable to log in') def _real_initialize(self): self._login() def _real_extract(self, url): anime_id = self._match_id(url) webpage = self._download_webpage(url, anime_id) if 'data-playlist=' not in webpage: self._download_webpage( self._APPLY_HTML5_URL, anime_id, 'Activating HTML5 beta', 'Unable to apply HTML5 beta') webpage = self._download_webpage(url, anime_id) csrf_token = self._html_search_meta( 'csrf-token', webpage, 'csrf token', fatal=True) anime_title = self._html_search_regex( r'(?s)<h1[^>]+itemprop="name"[^>]*>(.+?)</h1>', webpage, 'anime name') anime_description = self._html_search_regex( r'(?s)<div[^>]+itemprop="description"[^>]*>(.+?)</div>', webpage, 'anime description', default=None) entries = [] for num, episode_html in enumerate(re.findall( r'(?s)<h3[^>]+class="episodebox-title".+?>Episodeninhalt<', webpage), 1): episodebox_title = self._search_regex( (r'class="episodebox-title"[^>]+title=(["\'])(?P<title>.+?)\1', r'class="episodebox-title"[^>]+>(?P<title>.+?)<'), episode_html, 'episodebox title', default=None, group='title') if not episodebox_title: continue episode_number = int(self._search_regex( r'(?:Episode|Film)\s*(\d+)', episodebox_title, 'episode number', default=num)) episode_title = self._search_regex( r'(?:Episode|Film)\s*\d+\s*-\s*(.+)', episodebox_title, 'episode title', default=None) video_id = 'episode-%d' % episode_number common_info = { 'id': video_id, 'series': anime_title, 'episode': episode_title, 'episode_number': episode_number, } formats = [] for input_ in re.findall( r'<input[^>]+class=["\'].*?streamstarter_html5[^>]+>', episode_html): attributes = extract_attributes(input_) playlist_urls = [] for playlist_key in ('data-playlist', 'data-otherplaylist'): playlist_url = attributes.get(playlist_key) if isinstance(playlist_url, compat_str) and re.match( r'/?[\da-zA-Z]+', playlist_url): playlist_urls.append(attributes[playlist_key]) if not playlist_urls: continue lang = attributes.get('data-lang') lang_note = attributes.get('value') for playlist_url in playlist_urls: kind = self._search_regex( r'videomaterialurl/\d+/([^/]+)/', playlist_url, 'media kind', default=None) format_id_list = [] if lang: format_id_list.append(lang) if kind: format_id_list.append(kind) if not format_id_list: format_id_list.append(compat_str(num)) format_id = '-'.join(format_id_list) format_note = ', '.join(filter(None, (kind, lang_note))) request = sanitized_Request( compat_urlparse.urljoin(url, playlist_url), headers={ 'X-Requested-With': 'XMLHttpRequest', 'X-CSRF-Token': csrf_token, 'Referer': url, 'Accept': 'application/json, text/javascript, */*; q=0.01', }) playlist = self._download_json( request, video_id, 'Downloading %s playlist JSON' % format_id, fatal=False) if not playlist: continue start_video = playlist.get('startvideo', 0) playlist = playlist.get('playlist') if not playlist or not isinstance(playlist, list): continue playlist = playlist[start_video] title = playlist.get('title') if not title: continue description = playlist.get('description') for source in playlist.get('sources', []): file_ = source.get('file') if not file_: continue ext = determine_ext(file_) format_id_list = [lang, kind] if ext == 'm3u8': format_id_list.append('hls') elif source.get('type') == 'video/dash' or ext == 'mpd': format_id_list.append('dash') format_id = '-'.join(filter(None, format_id_list)) if ext == 'm3u8': file_formats = self._extract_m3u8_formats( file_, video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id=format_id, fatal=False) elif source.get('type') == 'video/dash' or ext == 'mpd': continue file_formats = self._extract_mpd_formats( file_, video_id, mpd_id=format_id, fatal=False) else: continue for f in file_formats: f.update({ 'language': lang, 'format_note': format_note, }) formats.extend(file_formats) if formats: self._sort_formats(formats) f = common_info.copy() f.update({ 'title': title, 'description': description, 'formats': formats, }) entries.append(f) # Extract teaser only when full episode is not available if not formats: m = re.search( r'data-dialog-header=(["\'])(?P<title>.+?)\1[^>]+href=(["\'])(?P<href>.+?)\3[^>]*>Teaser<', episode_html) if m: f = common_info.copy() f.update({ 'id': '%s-teaser' % f['id'], 'title': m.group('title'), 'url': compat_urlparse.urljoin(url, m.group('href')), }) entries.append(f) return self.playlist_result(entries, anime_id, anime_title, anime_description)
gpl-2.0
eddyerburgh/free-code-camp-ziplines
advanced-front-end-development-projects/simon-game/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings_test.py
1446
65937
#!/usr/bin/env python # Copyright (c) 2012 Google Inc. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Unit tests for the MSVSSettings.py file.""" import StringIO import unittest import gyp.MSVSSettings as MSVSSettings class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.stderr = StringIO.StringIO() def _ExpectedWarnings(self, expected): """Compares recorded lines to expected warnings.""" self.stderr.seek(0) actual = self.stderr.read().split('\n') actual = [line for line in actual if line] self.assertEqual(sorted(expected), sorted(actual)) def testValidateMSVSSettings_tool_names(self): """Tests that only MSVS tool names are allowed.""" MSVSSettings.ValidateMSVSSettings( {'VCCLCompilerTool': {}, 'VCLinkerTool': {}, 'VCMIDLTool': {}, 'foo': {}, 'VCResourceCompilerTool': {}, 'VCLibrarianTool': {}, 'VCManifestTool': {}, 'ClCompile': {}}, self.stderr) self._ExpectedWarnings([ 'Warning: unrecognized tool foo', 'Warning: unrecognized tool ClCompile']) def testValidateMSVSSettings_settings(self): """Tests that for invalid MSVS settings.""" MSVSSettings.ValidateMSVSSettings( {'VCCLCompilerTool': { 'AdditionalIncludeDirectories': 'folder1;folder2', 'AdditionalOptions': ['string1', 'string2'], 'AdditionalUsingDirectories': 'folder1;folder2', 'AssemblerListingLocation': 'a_file_name', 'AssemblerOutput': '0', 'BasicRuntimeChecks': '5', 'BrowseInformation': 'fdkslj', 'BrowseInformationFile': 'a_file_name', 'BufferSecurityCheck': 'true', 'CallingConvention': '-1', 'CompileAs': '1', 'DebugInformationFormat': '2', 'DefaultCharIsUnsigned': 'true', 'Detect64BitPortabilityProblems': 'true', 'DisableLanguageExtensions': 'true', 'DisableSpecificWarnings': 'string1;string2', 'EnableEnhancedInstructionSet': '1', 'EnableFiberSafeOptimizations': 'true', 'EnableFunctionLevelLinking': 'true', 'EnableIntrinsicFunctions': 'true', 'EnablePREfast': 'true', 'Enableprefast': 'bogus', 'ErrorReporting': '1', 'ExceptionHandling': '1', 'ExpandAttributedSource': 'true', 'FavorSizeOrSpeed': '1', 'FloatingPointExceptions': 'true', 'FloatingPointModel': '1', 'ForceConformanceInForLoopScope': 'true', 'ForcedIncludeFiles': 'file1;file2', 'ForcedUsingFiles': 'file1;file2', 'GeneratePreprocessedFile': '1', 'GenerateXMLDocumentationFiles': 'true', 'IgnoreStandardIncludePath': 'true', 'InlineFunctionExpansion': '1', 'KeepComments': 'true', 'MinimalRebuild': 'true', 'ObjectFile': 'a_file_name', 'OmitDefaultLibName': 'true', 'OmitFramePointers': 'true', 'OpenMP': 'true', 'Optimization': '1', 'PrecompiledHeaderFile': 'a_file_name', 'PrecompiledHeaderThrough': 'a_file_name', 'PreprocessorDefinitions': 'string1;string2', 'ProgramDataBaseFileName': 'a_file_name', 'RuntimeLibrary': '1', 'RuntimeTypeInfo': 'true', 'ShowIncludes': 'true', 'SmallerTypeCheck': 'true', 'StringPooling': 'true', 'StructMemberAlignment': '1', 'SuppressStartupBanner': 'true', 'TreatWChar_tAsBuiltInType': 'true', 'UndefineAllPreprocessorDefinitions': 'true', 'UndefinePreprocessorDefinitions': 'string1;string2', 'UseFullPaths': 'true', 'UsePrecompiledHeader': '1', 'UseUnicodeResponseFiles': 'true', 'WarnAsError': 'true', 'WarningLevel': '1', 'WholeProgramOptimization': 'true', 'XMLDocumentationFileName': 'a_file_name', 'ZZXYZ': 'bogus'}, 'VCLinkerTool': { 'AdditionalDependencies': 'file1;file2', 'AdditionalDependencies_excluded': 'file3', 'AdditionalLibraryDirectories': 'folder1;folder2', 'AdditionalManifestDependencies': 'file1;file2', 'AdditionalOptions': 'a string1', 'AddModuleNamesToAssembly': 'file1;file2', 'AllowIsolation': 'true', 'AssemblyDebug': '2', 'AssemblyLinkResource': 'file1;file2', 'BaseAddress': 'a string1', 'CLRImageType': '2', 'CLRThreadAttribute': '2', 'CLRUnmanagedCodeCheck': 'true', 'DataExecutionPrevention': '2', 'DelayLoadDLLs': 'file1;file2', 'DelaySign': 'true', 'Driver': '2', 'EmbedManagedResourceFile': 'file1;file2', 'EnableCOMDATFolding': '2', 'EnableUAC': 'true', 'EntryPointSymbol': 'a string1', 'ErrorReporting': '2', 'FixedBaseAddress': '2', 'ForceSymbolReferences': 'file1;file2', 'FunctionOrder': 'a_file_name', 'GenerateDebugInformation': 'true', 'GenerateManifest': 'true', 'GenerateMapFile': 'true', 'HeapCommitSize': 'a string1', 'HeapReserveSize': 'a string1', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreDefaultLibraryNames': 'file1;file2', 'IgnoreEmbeddedIDL': 'true', 'IgnoreImportLibrary': 'true', 'ImportLibrary': 'a_file_name', 'KeyContainer': 'a_file_name', 'KeyFile': 'a_file_name', 'LargeAddressAware': '2', 'LinkIncremental': '2', 'LinkLibraryDependencies': 'true', 'LinkTimeCodeGeneration': '2', 'ManifestFile': 'a_file_name', 'MapExports': 'true', 'MapFileName': 'a_file_name', 'MergedIDLBaseFileName': 'a_file_name', 'MergeSections': 'a string1', 'MidlCommandFile': 'a_file_name', 'ModuleDefinitionFile': 'a_file_name', 'OptimizeForWindows98': '1', 'OptimizeReferences': '2', 'OutputFile': 'a_file_name', 'PerUserRedirection': 'true', 'Profile': 'true', 'ProfileGuidedDatabase': 'a_file_name', 'ProgramDatabaseFile': 'a_file_name', 'RandomizedBaseAddress': '2', 'RegisterOutput': 'true', 'ResourceOnlyDLL': 'true', 'SetChecksum': 'true', 'ShowProgress': '2', 'StackCommitSize': 'a string1', 'StackReserveSize': 'a string1', 'StripPrivateSymbols': 'a_file_name', 'SubSystem': '2', 'SupportUnloadOfDelayLoadedDLL': 'true', 'SuppressStartupBanner': 'true', 'SwapRunFromCD': 'true', 'SwapRunFromNet': 'true', 'TargetMachine': '2', 'TerminalServerAware': '2', 'TurnOffAssemblyGeneration': 'true', 'TypeLibraryFile': 'a_file_name', 'TypeLibraryResourceID': '33', 'UACExecutionLevel': '2', 'UACUIAccess': 'true', 'UseLibraryDependencyInputs': 'true', 'UseUnicodeResponseFiles': 'true', 'Version': 'a string1'}, 'VCMIDLTool': { 'AdditionalIncludeDirectories': 'folder1;folder2', 'AdditionalOptions': 'a string1', 'CPreprocessOptions': 'a string1', 'DefaultCharType': '1', 'DLLDataFileName': 'a_file_name', 'EnableErrorChecks': '1', 'ErrorCheckAllocations': 'true', 'ErrorCheckBounds': 'true', 'ErrorCheckEnumRange': 'true', 'ErrorCheckRefPointers': 'true', 'ErrorCheckStubData': 'true', 'GenerateStublessProxies': 'true', 'GenerateTypeLibrary': 'true', 'HeaderFileName': 'a_file_name', 'IgnoreStandardIncludePath': 'true', 'InterfaceIdentifierFileName': 'a_file_name', 'MkTypLibCompatible': 'true', 'notgood': 'bogus', 'OutputDirectory': 'a string1', 'PreprocessorDefinitions': 'string1;string2', 'ProxyFileName': 'a_file_name', 'RedirectOutputAndErrors': 'a_file_name', 'StructMemberAlignment': '1', 'SuppressStartupBanner': 'true', 'TargetEnvironment': '1', 'TypeLibraryName': 'a_file_name', 'UndefinePreprocessorDefinitions': 'string1;string2', 'ValidateParameters': 'true', 'WarnAsError': 'true', 'WarningLevel': '1'}, 'VCResourceCompilerTool': { 'AdditionalOptions': 'a string1', 'AdditionalIncludeDirectories': 'folder1;folder2', 'Culture': '1003', 'IgnoreStandardIncludePath': 'true', 'notgood2': 'bogus', 'PreprocessorDefinitions': 'string1;string2', 'ResourceOutputFileName': 'a string1', 'ShowProgress': 'true', 'SuppressStartupBanner': 'true', 'UndefinePreprocessorDefinitions': 'string1;string2'}, 'VCLibrarianTool': { 'AdditionalDependencies': 'file1;file2', 'AdditionalLibraryDirectories': 'folder1;folder2', 'AdditionalOptions': 'a string1', 'ExportNamedFunctions': 'string1;string2', 'ForceSymbolReferences': 'a string1', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreSpecificDefaultLibraries': 'file1;file2', 'LinkLibraryDependencies': 'true', 'ModuleDefinitionFile': 'a_file_name', 'OutputFile': 'a_file_name', 'SuppressStartupBanner': 'true', 'UseUnicodeResponseFiles': 'true'}, 'VCManifestTool': { 'AdditionalManifestFiles': 'file1;file2', 'AdditionalOptions': 'a string1', 'AssemblyIdentity': 'a string1', 'ComponentFileName': 'a_file_name', 'DependencyInformationFile': 'a_file_name', 'GenerateCatalogFiles': 'true', 'InputResourceManifests': 'a string1', 'ManifestResourceFile': 'a_file_name', 'OutputManifestFile': 'a_file_name', 'RegistrarScriptFile': 'a_file_name', 'ReplacementsFile': 'a_file_name', 'SuppressStartupBanner': 'true', 'TypeLibraryFile': 'a_file_name', 'UpdateFileHashes': 'truel', 'UpdateFileHashesSearchPath': 'a_file_name', 'UseFAT32Workaround': 'true', 'UseUnicodeResponseFiles': 'true', 'VerboseOutput': 'true'}}, self.stderr) self._ExpectedWarnings([ 'Warning: for VCCLCompilerTool/BasicRuntimeChecks, ' 'index value (5) not in expected range [0, 4)', 'Warning: for VCCLCompilerTool/BrowseInformation, ' "invalid literal for int() with base 10: 'fdkslj'", 'Warning: for VCCLCompilerTool/CallingConvention, ' 'index value (-1) not in expected range [0, 4)', 'Warning: for VCCLCompilerTool/DebugInformationFormat, ' 'converted value for 2 not specified.', 'Warning: unrecognized setting VCCLCompilerTool/Enableprefast', 'Warning: unrecognized setting VCCLCompilerTool/ZZXYZ', 'Warning: for VCLinkerTool/TargetMachine, ' 'converted value for 2 not specified.', 'Warning: unrecognized setting VCMIDLTool/notgood', 'Warning: unrecognized setting VCResourceCompilerTool/notgood2', 'Warning: for VCManifestTool/UpdateFileHashes, ' "expected bool; got 'truel'" '']) def testValidateMSBuildSettings_settings(self): """Tests that for invalid MSBuild settings.""" MSVSSettings.ValidateMSBuildSettings( {'ClCompile': { 'AdditionalIncludeDirectories': 'folder1;folder2', 'AdditionalOptions': ['string1', 'string2'], 'AdditionalUsingDirectories': 'folder1;folder2', 'AssemblerListingLocation': 'a_file_name', 'AssemblerOutput': 'NoListing', 'BasicRuntimeChecks': 'StackFrameRuntimeCheck', 'BrowseInformation': 'false', 'BrowseInformationFile': 'a_file_name', 'BufferSecurityCheck': 'true', 'BuildingInIDE': 'true', 'CallingConvention': 'Cdecl', 'CompileAs': 'CompileAsC', 'CompileAsManaged': 'true', 'CreateHotpatchableImage': 'true', 'DebugInformationFormat': 'ProgramDatabase', 'DisableLanguageExtensions': 'true', 'DisableSpecificWarnings': 'string1;string2', 'EnableEnhancedInstructionSet': 'StreamingSIMDExtensions', 'EnableFiberSafeOptimizations': 'true', 'EnablePREfast': 'true', 'Enableprefast': 'bogus', 'ErrorReporting': 'Prompt', 'ExceptionHandling': 'SyncCThrow', 'ExpandAttributedSource': 'true', 'FavorSizeOrSpeed': 'Neither', 'FloatingPointExceptions': 'true', 'FloatingPointModel': 'Precise', 'ForceConformanceInForLoopScope': 'true', 'ForcedIncludeFiles': 'file1;file2', 'ForcedUsingFiles': 'file1;file2', 'FunctionLevelLinking': 'false', 'GenerateXMLDocumentationFiles': 'true', 'IgnoreStandardIncludePath': 'true', 'InlineFunctionExpansion': 'OnlyExplicitInline', 'IntrinsicFunctions': 'false', 'MinimalRebuild': 'true', 'MultiProcessorCompilation': 'true', 'ObjectFileName': 'a_file_name', 'OmitDefaultLibName': 'true', 'OmitFramePointers': 'true', 'OpenMPSupport': 'true', 'Optimization': 'Disabled', 'PrecompiledHeader': 'NotUsing', 'PrecompiledHeaderFile': 'a_file_name', 'PrecompiledHeaderOutputFile': 'a_file_name', 'PreprocessKeepComments': 'true', 'PreprocessorDefinitions': 'string1;string2', 'PreprocessOutputPath': 'a string1', 'PreprocessSuppressLineNumbers': 'false', 'PreprocessToFile': 'false', 'ProcessorNumber': '33', 'ProgramDataBaseFileName': 'a_file_name', 'RuntimeLibrary': 'MultiThreaded', 'RuntimeTypeInfo': 'true', 'ShowIncludes': 'true', 'SmallerTypeCheck': 'true', 'StringPooling': 'true', 'StructMemberAlignment': '1Byte', 'SuppressStartupBanner': 'true', 'TrackerLogDirectory': 'a_folder', 'TreatSpecificWarningsAsErrors': 'string1;string2', 'TreatWarningAsError': 'true', 'TreatWChar_tAsBuiltInType': 'true', 'UndefineAllPreprocessorDefinitions': 'true', 'UndefinePreprocessorDefinitions': 'string1;string2', 'UseFullPaths': 'true', 'UseUnicodeForAssemblerListing': 'true', 'WarningLevel': 'TurnOffAllWarnings', 'WholeProgramOptimization': 'true', 'XMLDocumentationFileName': 'a_file_name', 'ZZXYZ': 'bogus'}, 'Link': { 'AdditionalDependencies': 'file1;file2', 'AdditionalLibraryDirectories': 'folder1;folder2', 'AdditionalManifestDependencies': 'file1;file2', 'AdditionalOptions': 'a string1', 'AddModuleNamesToAssembly': 'file1;file2', 'AllowIsolation': 'true', 'AssemblyDebug': '', 'AssemblyLinkResource': 'file1;file2', 'BaseAddress': 'a string1', 'BuildingInIDE': 'true', 'CLRImageType': 'ForceIJWImage', 'CLRSupportLastError': 'Enabled', 'CLRThreadAttribute': 'MTAThreadingAttribute', 'CLRUnmanagedCodeCheck': 'true', 'CreateHotPatchableImage': 'X86Image', 'DataExecutionPrevention': 'false', 'DelayLoadDLLs': 'file1;file2', 'DelaySign': 'true', 'Driver': 'NotSet', 'EmbedManagedResourceFile': 'file1;file2', 'EnableCOMDATFolding': 'false', 'EnableUAC': 'true', 'EntryPointSymbol': 'a string1', 'FixedBaseAddress': 'false', 'ForceFileOutput': 'Enabled', 'ForceSymbolReferences': 'file1;file2', 'FunctionOrder': 'a_file_name', 'GenerateDebugInformation': 'true', 'GenerateMapFile': 'true', 'HeapCommitSize': 'a string1', 'HeapReserveSize': 'a string1', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreEmbeddedIDL': 'true', 'IgnoreSpecificDefaultLibraries': 'a_file_list', 'ImageHasSafeExceptionHandlers': 'true', 'ImportLibrary': 'a_file_name', 'KeyContainer': 'a_file_name', 'KeyFile': 'a_file_name', 'LargeAddressAware': 'false', 'LinkDLL': 'true', 'LinkErrorReporting': 'SendErrorReport', 'LinkStatus': 'true', 'LinkTimeCodeGeneration': 'UseLinkTimeCodeGeneration', 'ManifestFile': 'a_file_name', 'MapExports': 'true', 'MapFileName': 'a_file_name', 'MergedIDLBaseFileName': 'a_file_name', 'MergeSections': 'a string1', 'MidlCommandFile': 'a_file_name', 'MinimumRequiredVersion': 'a string1', 'ModuleDefinitionFile': 'a_file_name', 'MSDOSStubFileName': 'a_file_name', 'NoEntryPoint': 'true', 'OptimizeReferences': 'false', 'OutputFile': 'a_file_name', 'PerUserRedirection': 'true', 'PreventDllBinding': 'true', 'Profile': 'true', 'ProfileGuidedDatabase': 'a_file_name', 'ProgramDatabaseFile': 'a_file_name', 'RandomizedBaseAddress': 'false', 'RegisterOutput': 'true', 'SectionAlignment': '33', 'SetChecksum': 'true', 'ShowProgress': 'LinkVerboseREF', 'SpecifySectionAttributes': 'a string1', 'StackCommitSize': 'a string1', 'StackReserveSize': 'a string1', 'StripPrivateSymbols': 'a_file_name', 'SubSystem': 'Console', 'SupportNobindOfDelayLoadedDLL': 'true', 'SupportUnloadOfDelayLoadedDLL': 'true', 'SuppressStartupBanner': 'true', 'SwapRunFromCD': 'true', 'SwapRunFromNET': 'true', 'TargetMachine': 'MachineX86', 'TerminalServerAware': 'false', 'TrackerLogDirectory': 'a_folder', 'TreatLinkerWarningAsErrors': 'true', 'TurnOffAssemblyGeneration': 'true', 'TypeLibraryFile': 'a_file_name', 'TypeLibraryResourceID': '33', 'UACExecutionLevel': 'AsInvoker', 'UACUIAccess': 'true', 'Version': 'a string1'}, 'ResourceCompile': { 'AdditionalIncludeDirectories': 'folder1;folder2', 'AdditionalOptions': 'a string1', 'Culture': '0x236', 'IgnoreStandardIncludePath': 'true', 'NullTerminateStrings': 'true', 'PreprocessorDefinitions': 'string1;string2', 'ResourceOutputFileName': 'a string1', 'ShowProgress': 'true', 'SuppressStartupBanner': 'true', 'TrackerLogDirectory': 'a_folder', 'UndefinePreprocessorDefinitions': 'string1;string2'}, 'Midl': { 'AdditionalIncludeDirectories': 'folder1;folder2', 'AdditionalOptions': 'a string1', 'ApplicationConfigurationMode': 'true', 'ClientStubFile': 'a_file_name', 'CPreprocessOptions': 'a string1', 'DefaultCharType': 'Signed', 'DllDataFileName': 'a_file_name', 'EnableErrorChecks': 'EnableCustom', 'ErrorCheckAllocations': 'true', 'ErrorCheckBounds': 'true', 'ErrorCheckEnumRange': 'true', 'ErrorCheckRefPointers': 'true', 'ErrorCheckStubData': 'true', 'GenerateClientFiles': 'Stub', 'GenerateServerFiles': 'None', 'GenerateStublessProxies': 'true', 'GenerateTypeLibrary': 'true', 'HeaderFileName': 'a_file_name', 'IgnoreStandardIncludePath': 'true', 'InterfaceIdentifierFileName': 'a_file_name', 'LocaleID': '33', 'MkTypLibCompatible': 'true', 'OutputDirectory': 'a string1', 'PreprocessorDefinitions': 'string1;string2', 'ProxyFileName': 'a_file_name', 'RedirectOutputAndErrors': 'a_file_name', 'ServerStubFile': 'a_file_name', 'StructMemberAlignment': 'NotSet', 'SuppressCompilerWarnings': 'true', 'SuppressStartupBanner': 'true', 'TargetEnvironment': 'Itanium', 'TrackerLogDirectory': 'a_folder', 'TypeLibFormat': 'NewFormat', 'TypeLibraryName': 'a_file_name', 'UndefinePreprocessorDefinitions': 'string1;string2', 'ValidateAllParameters': 'true', 'WarnAsError': 'true', 'WarningLevel': '1'}, 'Lib': { 'AdditionalDependencies': 'file1;file2', 'AdditionalLibraryDirectories': 'folder1;folder2', 'AdditionalOptions': 'a string1', 'DisplayLibrary': 'a string1', 'ErrorReporting': 'PromptImmediately', 'ExportNamedFunctions': 'string1;string2', 'ForceSymbolReferences': 'a string1', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreSpecificDefaultLibraries': 'file1;file2', 'LinkTimeCodeGeneration': 'true', 'MinimumRequiredVersion': 'a string1', 'ModuleDefinitionFile': 'a_file_name', 'Name': 'a_file_name', 'OutputFile': 'a_file_name', 'RemoveObjects': 'file1;file2', 'SubSystem': 'Console', 'SuppressStartupBanner': 'true', 'TargetMachine': 'MachineX86i', 'TrackerLogDirectory': 'a_folder', 'TreatLibWarningAsErrors': 'true', 'UseUnicodeResponseFiles': 'true', 'Verbose': 'true'}, 'Manifest': { 'AdditionalManifestFiles': 'file1;file2', 'AdditionalOptions': 'a string1', 'AssemblyIdentity': 'a string1', 'ComponentFileName': 'a_file_name', 'EnableDPIAwareness': 'fal', 'GenerateCatalogFiles': 'truel', 'GenerateCategoryTags': 'true', 'InputResourceManifests': 'a string1', 'ManifestFromManagedAssembly': 'a_file_name', 'notgood3': 'bogus', 'OutputManifestFile': 'a_file_name', 'OutputResourceManifests': 'a string1', 'RegistrarScriptFile': 'a_file_name', 'ReplacementsFile': 'a_file_name', 'SuppressDependencyElement': 'true', 'SuppressStartupBanner': 'true', 'TrackerLogDirectory': 'a_folder', 'TypeLibraryFile': 'a_file_name', 'UpdateFileHashes': 'true', 'UpdateFileHashesSearchPath': 'a_file_name', 'VerboseOutput': 'true'}, 'ProjectReference': { 'LinkLibraryDependencies': 'true', 'UseLibraryDependencyInputs': 'true'}, 'ManifestResourceCompile': { 'ResourceOutputFileName': 'a_file_name'}, '': { 'EmbedManifest': 'true', 'GenerateManifest': 'true', 'IgnoreImportLibrary': 'true', 'LinkIncremental': 'false'}}, self.stderr) self._ExpectedWarnings([ 'Warning: unrecognized setting ClCompile/Enableprefast', 'Warning: unrecognized setting ClCompile/ZZXYZ', 'Warning: unrecognized setting Manifest/notgood3', 'Warning: for Manifest/GenerateCatalogFiles, ' "expected bool; got 'truel'", 'Warning: for Lib/TargetMachine, unrecognized enumerated value ' 'MachineX86i', "Warning: for Manifest/EnableDPIAwareness, expected bool; got 'fal'"]) def testConvertToMSBuildSettings_empty(self): """Tests an empty conversion.""" msvs_settings = {} expected_msbuild_settings = {} actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( msvs_settings, self.stderr) self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) self._ExpectedWarnings([]) def testConvertToMSBuildSettings_minimal(self): """Tests a minimal conversion.""" msvs_settings = { 'VCCLCompilerTool': { 'AdditionalIncludeDirectories': 'dir1', 'AdditionalOptions': '/foo', 'BasicRuntimeChecks': '0', }, 'VCLinkerTool': { 'LinkTimeCodeGeneration': '1', 'ErrorReporting': '1', 'DataExecutionPrevention': '2', }, } expected_msbuild_settings = { 'ClCompile': { 'AdditionalIncludeDirectories': 'dir1', 'AdditionalOptions': '/foo', 'BasicRuntimeChecks': 'Default', }, 'Link': { 'LinkTimeCodeGeneration': 'UseLinkTimeCodeGeneration', 'LinkErrorReporting': 'PromptImmediately', 'DataExecutionPrevention': 'true', }, } actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( msvs_settings, self.stderr) self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) self._ExpectedWarnings([]) def testConvertToMSBuildSettings_warnings(self): """Tests conversion that generates warnings.""" msvs_settings = { 'VCCLCompilerTool': { 'AdditionalIncludeDirectories': '1', 'AdditionalOptions': '2', # These are incorrect values: 'BasicRuntimeChecks': '12', 'BrowseInformation': '21', 'UsePrecompiledHeader': '13', 'GeneratePreprocessedFile': '14'}, 'VCLinkerTool': { # These are incorrect values: 'Driver': '10', 'LinkTimeCodeGeneration': '31', 'ErrorReporting': '21', 'FixedBaseAddress': '6'}, 'VCResourceCompilerTool': { # Custom 'Culture': '1003'}} expected_msbuild_settings = { 'ClCompile': { 'AdditionalIncludeDirectories': '1', 'AdditionalOptions': '2'}, 'Link': {}, 'ResourceCompile': { # Custom 'Culture': '0x03eb'}} actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( msvs_settings, self.stderr) self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) self._ExpectedWarnings([ 'Warning: while converting VCCLCompilerTool/BasicRuntimeChecks to ' 'MSBuild, index value (12) not in expected range [0, 4)', 'Warning: while converting VCCLCompilerTool/BrowseInformation to ' 'MSBuild, index value (21) not in expected range [0, 3)', 'Warning: while converting VCCLCompilerTool/UsePrecompiledHeader to ' 'MSBuild, index value (13) not in expected range [0, 3)', 'Warning: while converting VCCLCompilerTool/GeneratePreprocessedFile to ' 'MSBuild, value must be one of [0, 1, 2]; got 14', 'Warning: while converting VCLinkerTool/Driver to ' 'MSBuild, index value (10) not in expected range [0, 4)', 'Warning: while converting VCLinkerTool/LinkTimeCodeGeneration to ' 'MSBuild, index value (31) not in expected range [0, 5)', 'Warning: while converting VCLinkerTool/ErrorReporting to ' 'MSBuild, index value (21) not in expected range [0, 3)', 'Warning: while converting VCLinkerTool/FixedBaseAddress to ' 'MSBuild, index value (6) not in expected range [0, 3)', ]) def testConvertToMSBuildSettings_full_synthetic(self): """Tests conversion of all the MSBuild settings.""" msvs_settings = { 'VCCLCompilerTool': { 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', 'AdditionalOptions': 'a_string', 'AdditionalUsingDirectories': 'folder1;folder2;folder3', 'AssemblerListingLocation': 'a_file_name', 'AssemblerOutput': '0', 'BasicRuntimeChecks': '1', 'BrowseInformation': '2', 'BrowseInformationFile': 'a_file_name', 'BufferSecurityCheck': 'true', 'CallingConvention': '0', 'CompileAs': '1', 'DebugInformationFormat': '4', 'DefaultCharIsUnsigned': 'true', 'Detect64BitPortabilityProblems': 'true', 'DisableLanguageExtensions': 'true', 'DisableSpecificWarnings': 'd1;d2;d3', 'EnableEnhancedInstructionSet': '0', 'EnableFiberSafeOptimizations': 'true', 'EnableFunctionLevelLinking': 'true', 'EnableIntrinsicFunctions': 'true', 'EnablePREfast': 'true', 'ErrorReporting': '1', 'ExceptionHandling': '2', 'ExpandAttributedSource': 'true', 'FavorSizeOrSpeed': '0', 'FloatingPointExceptions': 'true', 'FloatingPointModel': '1', 'ForceConformanceInForLoopScope': 'true', 'ForcedIncludeFiles': 'file1;file2;file3', 'ForcedUsingFiles': 'file1;file2;file3', 'GeneratePreprocessedFile': '1', 'GenerateXMLDocumentationFiles': 'true', 'IgnoreStandardIncludePath': 'true', 'InlineFunctionExpansion': '2', 'KeepComments': 'true', 'MinimalRebuild': 'true', 'ObjectFile': 'a_file_name', 'OmitDefaultLibName': 'true', 'OmitFramePointers': 'true', 'OpenMP': 'true', 'Optimization': '3', 'PrecompiledHeaderFile': 'a_file_name', 'PrecompiledHeaderThrough': 'a_file_name', 'PreprocessorDefinitions': 'd1;d2;d3', 'ProgramDataBaseFileName': 'a_file_name', 'RuntimeLibrary': '0', 'RuntimeTypeInfo': 'true', 'ShowIncludes': 'true', 'SmallerTypeCheck': 'true', 'StringPooling': 'true', 'StructMemberAlignment': '1', 'SuppressStartupBanner': 'true', 'TreatWChar_tAsBuiltInType': 'true', 'UndefineAllPreprocessorDefinitions': 'true', 'UndefinePreprocessorDefinitions': 'd1;d2;d3', 'UseFullPaths': 'true', 'UsePrecompiledHeader': '1', 'UseUnicodeResponseFiles': 'true', 'WarnAsError': 'true', 'WarningLevel': '2', 'WholeProgramOptimization': 'true', 'XMLDocumentationFileName': 'a_file_name'}, 'VCLinkerTool': { 'AdditionalDependencies': 'file1;file2;file3', 'AdditionalLibraryDirectories': 'folder1;folder2;folder3', 'AdditionalLibraryDirectories_excluded': 'folder1;folder2;folder3', 'AdditionalManifestDependencies': 'file1;file2;file3', 'AdditionalOptions': 'a_string', 'AddModuleNamesToAssembly': 'file1;file2;file3', 'AllowIsolation': 'true', 'AssemblyDebug': '0', 'AssemblyLinkResource': 'file1;file2;file3', 'BaseAddress': 'a_string', 'CLRImageType': '1', 'CLRThreadAttribute': '2', 'CLRUnmanagedCodeCheck': 'true', 'DataExecutionPrevention': '0', 'DelayLoadDLLs': 'file1;file2;file3', 'DelaySign': 'true', 'Driver': '1', 'EmbedManagedResourceFile': 'file1;file2;file3', 'EnableCOMDATFolding': '0', 'EnableUAC': 'true', 'EntryPointSymbol': 'a_string', 'ErrorReporting': '0', 'FixedBaseAddress': '1', 'ForceSymbolReferences': 'file1;file2;file3', 'FunctionOrder': 'a_file_name', 'GenerateDebugInformation': 'true', 'GenerateManifest': 'true', 'GenerateMapFile': 'true', 'HeapCommitSize': 'a_string', 'HeapReserveSize': 'a_string', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreDefaultLibraryNames': 'file1;file2;file3', 'IgnoreEmbeddedIDL': 'true', 'IgnoreImportLibrary': 'true', 'ImportLibrary': 'a_file_name', 'KeyContainer': 'a_file_name', 'KeyFile': 'a_file_name', 'LargeAddressAware': '2', 'LinkIncremental': '1', 'LinkLibraryDependencies': 'true', 'LinkTimeCodeGeneration': '2', 'ManifestFile': 'a_file_name', 'MapExports': 'true', 'MapFileName': 'a_file_name', 'MergedIDLBaseFileName': 'a_file_name', 'MergeSections': 'a_string', 'MidlCommandFile': 'a_file_name', 'ModuleDefinitionFile': 'a_file_name', 'OptimizeForWindows98': '1', 'OptimizeReferences': '0', 'OutputFile': 'a_file_name', 'PerUserRedirection': 'true', 'Profile': 'true', 'ProfileGuidedDatabase': 'a_file_name', 'ProgramDatabaseFile': 'a_file_name', 'RandomizedBaseAddress': '1', 'RegisterOutput': 'true', 'ResourceOnlyDLL': 'true', 'SetChecksum': 'true', 'ShowProgress': '0', 'StackCommitSize': 'a_string', 'StackReserveSize': 'a_string', 'StripPrivateSymbols': 'a_file_name', 'SubSystem': '2', 'SupportUnloadOfDelayLoadedDLL': 'true', 'SuppressStartupBanner': 'true', 'SwapRunFromCD': 'true', 'SwapRunFromNet': 'true', 'TargetMachine': '3', 'TerminalServerAware': '2', 'TurnOffAssemblyGeneration': 'true', 'TypeLibraryFile': 'a_file_name', 'TypeLibraryResourceID': '33', 'UACExecutionLevel': '1', 'UACUIAccess': 'true', 'UseLibraryDependencyInputs': 'false', 'UseUnicodeResponseFiles': 'true', 'Version': 'a_string'}, 'VCResourceCompilerTool': { 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', 'AdditionalOptions': 'a_string', 'Culture': '1003', 'IgnoreStandardIncludePath': 'true', 'PreprocessorDefinitions': 'd1;d2;d3', 'ResourceOutputFileName': 'a_string', 'ShowProgress': 'true', 'SuppressStartupBanner': 'true', 'UndefinePreprocessorDefinitions': 'd1;d2;d3'}, 'VCMIDLTool': { 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', 'AdditionalOptions': 'a_string', 'CPreprocessOptions': 'a_string', 'DefaultCharType': '0', 'DLLDataFileName': 'a_file_name', 'EnableErrorChecks': '2', 'ErrorCheckAllocations': 'true', 'ErrorCheckBounds': 'true', 'ErrorCheckEnumRange': 'true', 'ErrorCheckRefPointers': 'true', 'ErrorCheckStubData': 'true', 'GenerateStublessProxies': 'true', 'GenerateTypeLibrary': 'true', 'HeaderFileName': 'a_file_name', 'IgnoreStandardIncludePath': 'true', 'InterfaceIdentifierFileName': 'a_file_name', 'MkTypLibCompatible': 'true', 'OutputDirectory': 'a_string', 'PreprocessorDefinitions': 'd1;d2;d3', 'ProxyFileName': 'a_file_name', 'RedirectOutputAndErrors': 'a_file_name', 'StructMemberAlignment': '3', 'SuppressStartupBanner': 'true', 'TargetEnvironment': '1', 'TypeLibraryName': 'a_file_name', 'UndefinePreprocessorDefinitions': 'd1;d2;d3', 'ValidateParameters': 'true', 'WarnAsError': 'true', 'WarningLevel': '4'}, 'VCLibrarianTool': { 'AdditionalDependencies': 'file1;file2;file3', 'AdditionalLibraryDirectories': 'folder1;folder2;folder3', 'AdditionalLibraryDirectories_excluded': 'folder1;folder2;folder3', 'AdditionalOptions': 'a_string', 'ExportNamedFunctions': 'd1;d2;d3', 'ForceSymbolReferences': 'a_string', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreSpecificDefaultLibraries': 'file1;file2;file3', 'LinkLibraryDependencies': 'true', 'ModuleDefinitionFile': 'a_file_name', 'OutputFile': 'a_file_name', 'SuppressStartupBanner': 'true', 'UseUnicodeResponseFiles': 'true'}, 'VCManifestTool': { 'AdditionalManifestFiles': 'file1;file2;file3', 'AdditionalOptions': 'a_string', 'AssemblyIdentity': 'a_string', 'ComponentFileName': 'a_file_name', 'DependencyInformationFile': 'a_file_name', 'EmbedManifest': 'true', 'GenerateCatalogFiles': 'true', 'InputResourceManifests': 'a_string', 'ManifestResourceFile': 'my_name', 'OutputManifestFile': 'a_file_name', 'RegistrarScriptFile': 'a_file_name', 'ReplacementsFile': 'a_file_name', 'SuppressStartupBanner': 'true', 'TypeLibraryFile': 'a_file_name', 'UpdateFileHashes': 'true', 'UpdateFileHashesSearchPath': 'a_file_name', 'UseFAT32Workaround': 'true', 'UseUnicodeResponseFiles': 'true', 'VerboseOutput': 'true'}} expected_msbuild_settings = { 'ClCompile': { 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', 'AdditionalOptions': 'a_string /J', 'AdditionalUsingDirectories': 'folder1;folder2;folder3', 'AssemblerListingLocation': 'a_file_name', 'AssemblerOutput': 'NoListing', 'BasicRuntimeChecks': 'StackFrameRuntimeCheck', 'BrowseInformation': 'true', 'BrowseInformationFile': 'a_file_name', 'BufferSecurityCheck': 'true', 'CallingConvention': 'Cdecl', 'CompileAs': 'CompileAsC', 'DebugInformationFormat': 'EditAndContinue', 'DisableLanguageExtensions': 'true', 'DisableSpecificWarnings': 'd1;d2;d3', 'EnableEnhancedInstructionSet': 'NotSet', 'EnableFiberSafeOptimizations': 'true', 'EnablePREfast': 'true', 'ErrorReporting': 'Prompt', 'ExceptionHandling': 'Async', 'ExpandAttributedSource': 'true', 'FavorSizeOrSpeed': 'Neither', 'FloatingPointExceptions': 'true', 'FloatingPointModel': 'Strict', 'ForceConformanceInForLoopScope': 'true', 'ForcedIncludeFiles': 'file1;file2;file3', 'ForcedUsingFiles': 'file1;file2;file3', 'FunctionLevelLinking': 'true', 'GenerateXMLDocumentationFiles': 'true', 'IgnoreStandardIncludePath': 'true', 'InlineFunctionExpansion': 'AnySuitable', 'IntrinsicFunctions': 'true', 'MinimalRebuild': 'true', 'ObjectFileName': 'a_file_name', 'OmitDefaultLibName': 'true', 'OmitFramePointers': 'true', 'OpenMPSupport': 'true', 'Optimization': 'Full', 'PrecompiledHeader': 'Create', 'PrecompiledHeaderFile': 'a_file_name', 'PrecompiledHeaderOutputFile': 'a_file_name', 'PreprocessKeepComments': 'true', 'PreprocessorDefinitions': 'd1;d2;d3', 'PreprocessSuppressLineNumbers': 'false', 'PreprocessToFile': 'true', 'ProgramDataBaseFileName': 'a_file_name', 'RuntimeLibrary': 'MultiThreaded', 'RuntimeTypeInfo': 'true', 'ShowIncludes': 'true', 'SmallerTypeCheck': 'true', 'StringPooling': 'true', 'StructMemberAlignment': '1Byte', 'SuppressStartupBanner': 'true', 'TreatWarningAsError': 'true', 'TreatWChar_tAsBuiltInType': 'true', 'UndefineAllPreprocessorDefinitions': 'true', 'UndefinePreprocessorDefinitions': 'd1;d2;d3', 'UseFullPaths': 'true', 'WarningLevel': 'Level2', 'WholeProgramOptimization': 'true', 'XMLDocumentationFileName': 'a_file_name'}, 'Link': { 'AdditionalDependencies': 'file1;file2;file3', 'AdditionalLibraryDirectories': 'folder1;folder2;folder3', 'AdditionalManifestDependencies': 'file1;file2;file3', 'AdditionalOptions': 'a_string', 'AddModuleNamesToAssembly': 'file1;file2;file3', 'AllowIsolation': 'true', 'AssemblyDebug': '', 'AssemblyLinkResource': 'file1;file2;file3', 'BaseAddress': 'a_string', 'CLRImageType': 'ForceIJWImage', 'CLRThreadAttribute': 'STAThreadingAttribute', 'CLRUnmanagedCodeCheck': 'true', 'DataExecutionPrevention': '', 'DelayLoadDLLs': 'file1;file2;file3', 'DelaySign': 'true', 'Driver': 'Driver', 'EmbedManagedResourceFile': 'file1;file2;file3', 'EnableCOMDATFolding': '', 'EnableUAC': 'true', 'EntryPointSymbol': 'a_string', 'FixedBaseAddress': 'false', 'ForceSymbolReferences': 'file1;file2;file3', 'FunctionOrder': 'a_file_name', 'GenerateDebugInformation': 'true', 'GenerateMapFile': 'true', 'HeapCommitSize': 'a_string', 'HeapReserveSize': 'a_string', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreEmbeddedIDL': 'true', 'IgnoreSpecificDefaultLibraries': 'file1;file2;file3', 'ImportLibrary': 'a_file_name', 'KeyContainer': 'a_file_name', 'KeyFile': 'a_file_name', 'LargeAddressAware': 'true', 'LinkErrorReporting': 'NoErrorReport', 'LinkTimeCodeGeneration': 'PGInstrument', 'ManifestFile': 'a_file_name', 'MapExports': 'true', 'MapFileName': 'a_file_name', 'MergedIDLBaseFileName': 'a_file_name', 'MergeSections': 'a_string', 'MidlCommandFile': 'a_file_name', 'ModuleDefinitionFile': 'a_file_name', 'NoEntryPoint': 'true', 'OptimizeReferences': '', 'OutputFile': 'a_file_name', 'PerUserRedirection': 'true', 'Profile': 'true', 'ProfileGuidedDatabase': 'a_file_name', 'ProgramDatabaseFile': 'a_file_name', 'RandomizedBaseAddress': 'false', 'RegisterOutput': 'true', 'SetChecksum': 'true', 'ShowProgress': 'NotSet', 'StackCommitSize': 'a_string', 'StackReserveSize': 'a_string', 'StripPrivateSymbols': 'a_file_name', 'SubSystem': 'Windows', 'SupportUnloadOfDelayLoadedDLL': 'true', 'SuppressStartupBanner': 'true', 'SwapRunFromCD': 'true', 'SwapRunFromNET': 'true', 'TargetMachine': 'MachineARM', 'TerminalServerAware': 'true', 'TurnOffAssemblyGeneration': 'true', 'TypeLibraryFile': 'a_file_name', 'TypeLibraryResourceID': '33', 'UACExecutionLevel': 'HighestAvailable', 'UACUIAccess': 'true', 'Version': 'a_string'}, 'ResourceCompile': { 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', 'AdditionalOptions': 'a_string', 'Culture': '0x03eb', 'IgnoreStandardIncludePath': 'true', 'PreprocessorDefinitions': 'd1;d2;d3', 'ResourceOutputFileName': 'a_string', 'ShowProgress': 'true', 'SuppressStartupBanner': 'true', 'UndefinePreprocessorDefinitions': 'd1;d2;d3'}, 'Midl': { 'AdditionalIncludeDirectories': 'folder1;folder2;folder3', 'AdditionalOptions': 'a_string', 'CPreprocessOptions': 'a_string', 'DefaultCharType': 'Unsigned', 'DllDataFileName': 'a_file_name', 'EnableErrorChecks': 'All', 'ErrorCheckAllocations': 'true', 'ErrorCheckBounds': 'true', 'ErrorCheckEnumRange': 'true', 'ErrorCheckRefPointers': 'true', 'ErrorCheckStubData': 'true', 'GenerateStublessProxies': 'true', 'GenerateTypeLibrary': 'true', 'HeaderFileName': 'a_file_name', 'IgnoreStandardIncludePath': 'true', 'InterfaceIdentifierFileName': 'a_file_name', 'MkTypLibCompatible': 'true', 'OutputDirectory': 'a_string', 'PreprocessorDefinitions': 'd1;d2;d3', 'ProxyFileName': 'a_file_name', 'RedirectOutputAndErrors': 'a_file_name', 'StructMemberAlignment': '4', 'SuppressStartupBanner': 'true', 'TargetEnvironment': 'Win32', 'TypeLibraryName': 'a_file_name', 'UndefinePreprocessorDefinitions': 'd1;d2;d3', 'ValidateAllParameters': 'true', 'WarnAsError': 'true', 'WarningLevel': '4'}, 'Lib': { 'AdditionalDependencies': 'file1;file2;file3', 'AdditionalLibraryDirectories': 'folder1;folder2;folder3', 'AdditionalOptions': 'a_string', 'ExportNamedFunctions': 'd1;d2;d3', 'ForceSymbolReferences': 'a_string', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreSpecificDefaultLibraries': 'file1;file2;file3', 'ModuleDefinitionFile': 'a_file_name', 'OutputFile': 'a_file_name', 'SuppressStartupBanner': 'true', 'UseUnicodeResponseFiles': 'true'}, 'Manifest': { 'AdditionalManifestFiles': 'file1;file2;file3', 'AdditionalOptions': 'a_string', 'AssemblyIdentity': 'a_string', 'ComponentFileName': 'a_file_name', 'GenerateCatalogFiles': 'true', 'InputResourceManifests': 'a_string', 'OutputManifestFile': 'a_file_name', 'RegistrarScriptFile': 'a_file_name', 'ReplacementsFile': 'a_file_name', 'SuppressStartupBanner': 'true', 'TypeLibraryFile': 'a_file_name', 'UpdateFileHashes': 'true', 'UpdateFileHashesSearchPath': 'a_file_name', 'VerboseOutput': 'true'}, 'ManifestResourceCompile': { 'ResourceOutputFileName': 'my_name'}, 'ProjectReference': { 'LinkLibraryDependencies': 'true', 'UseLibraryDependencyInputs': 'false'}, '': { 'EmbedManifest': 'true', 'GenerateManifest': 'true', 'IgnoreImportLibrary': 'true', 'LinkIncremental': 'false'}} actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( msvs_settings, self.stderr) self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) self._ExpectedWarnings([]) def testConvertToMSBuildSettings_actual(self): """Tests the conversion of an actual project. A VS2008 project with most of the options defined was created through the VS2008 IDE. It was then converted to VS2010. The tool settings found in the .vcproj and .vcxproj files were converted to the two dictionaries msvs_settings and expected_msbuild_settings. Note that for many settings, the VS2010 converter adds macros like %(AdditionalIncludeDirectories) to make sure than inherited values are included. Since the Gyp projects we generate do not use inheritance, we removed these macros. They were: ClCompile: AdditionalIncludeDirectories: ';%(AdditionalIncludeDirectories)' AdditionalOptions: ' %(AdditionalOptions)' AdditionalUsingDirectories: ';%(AdditionalUsingDirectories)' DisableSpecificWarnings: ';%(DisableSpecificWarnings)', ForcedIncludeFiles: ';%(ForcedIncludeFiles)', ForcedUsingFiles: ';%(ForcedUsingFiles)', PreprocessorDefinitions: ';%(PreprocessorDefinitions)', UndefinePreprocessorDefinitions: ';%(UndefinePreprocessorDefinitions)', Link: AdditionalDependencies: ';%(AdditionalDependencies)', AdditionalLibraryDirectories: ';%(AdditionalLibraryDirectories)', AdditionalManifestDependencies: ';%(AdditionalManifestDependencies)', AdditionalOptions: ' %(AdditionalOptions)', AddModuleNamesToAssembly: ';%(AddModuleNamesToAssembly)', AssemblyLinkResource: ';%(AssemblyLinkResource)', DelayLoadDLLs: ';%(DelayLoadDLLs)', EmbedManagedResourceFile: ';%(EmbedManagedResourceFile)', ForceSymbolReferences: ';%(ForceSymbolReferences)', IgnoreSpecificDefaultLibraries: ';%(IgnoreSpecificDefaultLibraries)', ResourceCompile: AdditionalIncludeDirectories: ';%(AdditionalIncludeDirectories)', AdditionalOptions: ' %(AdditionalOptions)', PreprocessorDefinitions: ';%(PreprocessorDefinitions)', Manifest: AdditionalManifestFiles: ';%(AdditionalManifestFiles)', AdditionalOptions: ' %(AdditionalOptions)', InputResourceManifests: ';%(InputResourceManifests)', """ msvs_settings = { 'VCCLCompilerTool': { 'AdditionalIncludeDirectories': 'dir1', 'AdditionalOptions': '/more', 'AdditionalUsingDirectories': 'test', 'AssemblerListingLocation': '$(IntDir)\\a', 'AssemblerOutput': '1', 'BasicRuntimeChecks': '3', 'BrowseInformation': '1', 'BrowseInformationFile': '$(IntDir)\\e', 'BufferSecurityCheck': 'false', 'CallingConvention': '1', 'CompileAs': '1', 'DebugInformationFormat': '4', 'DefaultCharIsUnsigned': 'true', 'Detect64BitPortabilityProblems': 'true', 'DisableLanguageExtensions': 'true', 'DisableSpecificWarnings': 'abc', 'EnableEnhancedInstructionSet': '1', 'EnableFiberSafeOptimizations': 'true', 'EnableFunctionLevelLinking': 'true', 'EnableIntrinsicFunctions': 'true', 'EnablePREfast': 'true', 'ErrorReporting': '2', 'ExceptionHandling': '2', 'ExpandAttributedSource': 'true', 'FavorSizeOrSpeed': '2', 'FloatingPointExceptions': 'true', 'FloatingPointModel': '1', 'ForceConformanceInForLoopScope': 'false', 'ForcedIncludeFiles': 'def', 'ForcedUsingFiles': 'ge', 'GeneratePreprocessedFile': '2', 'GenerateXMLDocumentationFiles': 'true', 'IgnoreStandardIncludePath': 'true', 'InlineFunctionExpansion': '1', 'KeepComments': 'true', 'MinimalRebuild': 'true', 'ObjectFile': '$(IntDir)\\b', 'OmitDefaultLibName': 'true', 'OmitFramePointers': 'true', 'OpenMP': 'true', 'Optimization': '3', 'PrecompiledHeaderFile': '$(IntDir)\\$(TargetName).pche', 'PrecompiledHeaderThrough': 'StdAfx.hd', 'PreprocessorDefinitions': 'WIN32;_DEBUG;_CONSOLE', 'ProgramDataBaseFileName': '$(IntDir)\\vc90b.pdb', 'RuntimeLibrary': '3', 'RuntimeTypeInfo': 'false', 'ShowIncludes': 'true', 'SmallerTypeCheck': 'true', 'StringPooling': 'true', 'StructMemberAlignment': '3', 'SuppressStartupBanner': 'false', 'TreatWChar_tAsBuiltInType': 'false', 'UndefineAllPreprocessorDefinitions': 'true', 'UndefinePreprocessorDefinitions': 'wer', 'UseFullPaths': 'true', 'UsePrecompiledHeader': '0', 'UseUnicodeResponseFiles': 'false', 'WarnAsError': 'true', 'WarningLevel': '3', 'WholeProgramOptimization': 'true', 'XMLDocumentationFileName': '$(IntDir)\\c'}, 'VCLinkerTool': { 'AdditionalDependencies': 'zx', 'AdditionalLibraryDirectories': 'asd', 'AdditionalManifestDependencies': 's2', 'AdditionalOptions': '/mor2', 'AddModuleNamesToAssembly': 'd1', 'AllowIsolation': 'false', 'AssemblyDebug': '1', 'AssemblyLinkResource': 'd5', 'BaseAddress': '23423', 'CLRImageType': '3', 'CLRThreadAttribute': '1', 'CLRUnmanagedCodeCheck': 'true', 'DataExecutionPrevention': '0', 'DelayLoadDLLs': 'd4', 'DelaySign': 'true', 'Driver': '2', 'EmbedManagedResourceFile': 'd2', 'EnableCOMDATFolding': '1', 'EnableUAC': 'false', 'EntryPointSymbol': 'f5', 'ErrorReporting': '2', 'FixedBaseAddress': '1', 'ForceSymbolReferences': 'd3', 'FunctionOrder': 'fssdfsd', 'GenerateDebugInformation': 'true', 'GenerateManifest': 'false', 'GenerateMapFile': 'true', 'HeapCommitSize': '13', 'HeapReserveSize': '12', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreDefaultLibraryNames': 'flob;flok', 'IgnoreEmbeddedIDL': 'true', 'IgnoreImportLibrary': 'true', 'ImportLibrary': 'f4', 'KeyContainer': 'f7', 'KeyFile': 'f6', 'LargeAddressAware': '2', 'LinkIncremental': '0', 'LinkLibraryDependencies': 'false', 'LinkTimeCodeGeneration': '1', 'ManifestFile': '$(IntDir)\\$(TargetFileName).2intermediate.manifest', 'MapExports': 'true', 'MapFileName': 'd5', 'MergedIDLBaseFileName': 'f2', 'MergeSections': 'f5', 'MidlCommandFile': 'f1', 'ModuleDefinitionFile': 'sdsd', 'OptimizeForWindows98': '2', 'OptimizeReferences': '2', 'OutputFile': '$(OutDir)\\$(ProjectName)2.exe', 'PerUserRedirection': 'true', 'Profile': 'true', 'ProfileGuidedDatabase': '$(TargetDir)$(TargetName).pgdd', 'ProgramDatabaseFile': 'Flob.pdb', 'RandomizedBaseAddress': '1', 'RegisterOutput': 'true', 'ResourceOnlyDLL': 'true', 'SetChecksum': 'false', 'ShowProgress': '1', 'StackCommitSize': '15', 'StackReserveSize': '14', 'StripPrivateSymbols': 'd3', 'SubSystem': '1', 'SupportUnloadOfDelayLoadedDLL': 'true', 'SuppressStartupBanner': 'false', 'SwapRunFromCD': 'true', 'SwapRunFromNet': 'true', 'TargetMachine': '1', 'TerminalServerAware': '1', 'TurnOffAssemblyGeneration': 'true', 'TypeLibraryFile': 'f3', 'TypeLibraryResourceID': '12', 'UACExecutionLevel': '2', 'UACUIAccess': 'true', 'UseLibraryDependencyInputs': 'true', 'UseUnicodeResponseFiles': 'false', 'Version': '333'}, 'VCResourceCompilerTool': { 'AdditionalIncludeDirectories': 'f3', 'AdditionalOptions': '/more3', 'Culture': '3084', 'IgnoreStandardIncludePath': 'true', 'PreprocessorDefinitions': '_UNICODE;UNICODE2', 'ResourceOutputFileName': '$(IntDir)/$(InputName)3.res', 'ShowProgress': 'true'}, 'VCManifestTool': { 'AdditionalManifestFiles': 'sfsdfsd', 'AdditionalOptions': 'afdsdafsd', 'AssemblyIdentity': 'sddfdsadfsa', 'ComponentFileName': 'fsdfds', 'DependencyInformationFile': '$(IntDir)\\mt.depdfd', 'EmbedManifest': 'false', 'GenerateCatalogFiles': 'true', 'InputResourceManifests': 'asfsfdafs', 'ManifestResourceFile': '$(IntDir)\\$(TargetFileName).embed.manifest.resfdsf', 'OutputManifestFile': '$(TargetPath).manifestdfs', 'RegistrarScriptFile': 'sdfsfd', 'ReplacementsFile': 'sdffsd', 'SuppressStartupBanner': 'false', 'TypeLibraryFile': 'sfsd', 'UpdateFileHashes': 'true', 'UpdateFileHashesSearchPath': 'sfsd', 'UseFAT32Workaround': 'true', 'UseUnicodeResponseFiles': 'false', 'VerboseOutput': 'true'}} expected_msbuild_settings = { 'ClCompile': { 'AdditionalIncludeDirectories': 'dir1', 'AdditionalOptions': '/more /J', 'AdditionalUsingDirectories': 'test', 'AssemblerListingLocation': '$(IntDir)a', 'AssemblerOutput': 'AssemblyCode', 'BasicRuntimeChecks': 'EnableFastChecks', 'BrowseInformation': 'true', 'BrowseInformationFile': '$(IntDir)e', 'BufferSecurityCheck': 'false', 'CallingConvention': 'FastCall', 'CompileAs': 'CompileAsC', 'DebugInformationFormat': 'EditAndContinue', 'DisableLanguageExtensions': 'true', 'DisableSpecificWarnings': 'abc', 'EnableEnhancedInstructionSet': 'StreamingSIMDExtensions', 'EnableFiberSafeOptimizations': 'true', 'EnablePREfast': 'true', 'ErrorReporting': 'Queue', 'ExceptionHandling': 'Async', 'ExpandAttributedSource': 'true', 'FavorSizeOrSpeed': 'Size', 'FloatingPointExceptions': 'true', 'FloatingPointModel': 'Strict', 'ForceConformanceInForLoopScope': 'false', 'ForcedIncludeFiles': 'def', 'ForcedUsingFiles': 'ge', 'FunctionLevelLinking': 'true', 'GenerateXMLDocumentationFiles': 'true', 'IgnoreStandardIncludePath': 'true', 'InlineFunctionExpansion': 'OnlyExplicitInline', 'IntrinsicFunctions': 'true', 'MinimalRebuild': 'true', 'ObjectFileName': '$(IntDir)b', 'OmitDefaultLibName': 'true', 'OmitFramePointers': 'true', 'OpenMPSupport': 'true', 'Optimization': 'Full', 'PrecompiledHeader': 'NotUsing', # Actual conversion gives '' 'PrecompiledHeaderFile': 'StdAfx.hd', 'PrecompiledHeaderOutputFile': '$(IntDir)$(TargetName).pche', 'PreprocessKeepComments': 'true', 'PreprocessorDefinitions': 'WIN32;_DEBUG;_CONSOLE', 'PreprocessSuppressLineNumbers': 'true', 'PreprocessToFile': 'true', 'ProgramDataBaseFileName': '$(IntDir)vc90b.pdb', 'RuntimeLibrary': 'MultiThreadedDebugDLL', 'RuntimeTypeInfo': 'false', 'ShowIncludes': 'true', 'SmallerTypeCheck': 'true', 'StringPooling': 'true', 'StructMemberAlignment': '4Bytes', 'SuppressStartupBanner': 'false', 'TreatWarningAsError': 'true', 'TreatWChar_tAsBuiltInType': 'false', 'UndefineAllPreprocessorDefinitions': 'true', 'UndefinePreprocessorDefinitions': 'wer', 'UseFullPaths': 'true', 'WarningLevel': 'Level3', 'WholeProgramOptimization': 'true', 'XMLDocumentationFileName': '$(IntDir)c'}, 'Link': { 'AdditionalDependencies': 'zx', 'AdditionalLibraryDirectories': 'asd', 'AdditionalManifestDependencies': 's2', 'AdditionalOptions': '/mor2', 'AddModuleNamesToAssembly': 'd1', 'AllowIsolation': 'false', 'AssemblyDebug': 'true', 'AssemblyLinkResource': 'd5', 'BaseAddress': '23423', 'CLRImageType': 'ForceSafeILImage', 'CLRThreadAttribute': 'MTAThreadingAttribute', 'CLRUnmanagedCodeCheck': 'true', 'DataExecutionPrevention': '', 'DelayLoadDLLs': 'd4', 'DelaySign': 'true', 'Driver': 'UpOnly', 'EmbedManagedResourceFile': 'd2', 'EnableCOMDATFolding': 'false', 'EnableUAC': 'false', 'EntryPointSymbol': 'f5', 'FixedBaseAddress': 'false', 'ForceSymbolReferences': 'd3', 'FunctionOrder': 'fssdfsd', 'GenerateDebugInformation': 'true', 'GenerateMapFile': 'true', 'HeapCommitSize': '13', 'HeapReserveSize': '12', 'IgnoreAllDefaultLibraries': 'true', 'IgnoreEmbeddedIDL': 'true', 'IgnoreSpecificDefaultLibraries': 'flob;flok', 'ImportLibrary': 'f4', 'KeyContainer': 'f7', 'KeyFile': 'f6', 'LargeAddressAware': 'true', 'LinkErrorReporting': 'QueueForNextLogin', 'LinkTimeCodeGeneration': 'UseLinkTimeCodeGeneration', 'ManifestFile': '$(IntDir)$(TargetFileName).2intermediate.manifest', 'MapExports': 'true', 'MapFileName': 'd5', 'MergedIDLBaseFileName': 'f2', 'MergeSections': 'f5', 'MidlCommandFile': 'f1', 'ModuleDefinitionFile': 'sdsd', 'NoEntryPoint': 'true', 'OptimizeReferences': 'true', 'OutputFile': '$(OutDir)$(ProjectName)2.exe', 'PerUserRedirection': 'true', 'Profile': 'true', 'ProfileGuidedDatabase': '$(TargetDir)$(TargetName).pgdd', 'ProgramDatabaseFile': 'Flob.pdb', 'RandomizedBaseAddress': 'false', 'RegisterOutput': 'true', 'SetChecksum': 'false', 'ShowProgress': 'LinkVerbose', 'StackCommitSize': '15', 'StackReserveSize': '14', 'StripPrivateSymbols': 'd3', 'SubSystem': 'Console', 'SupportUnloadOfDelayLoadedDLL': 'true', 'SuppressStartupBanner': 'false', 'SwapRunFromCD': 'true', 'SwapRunFromNET': 'true', 'TargetMachine': 'MachineX86', 'TerminalServerAware': 'false', 'TurnOffAssemblyGeneration': 'true', 'TypeLibraryFile': 'f3', 'TypeLibraryResourceID': '12', 'UACExecutionLevel': 'RequireAdministrator', 'UACUIAccess': 'true', 'Version': '333'}, 'ResourceCompile': { 'AdditionalIncludeDirectories': 'f3', 'AdditionalOptions': '/more3', 'Culture': '0x0c0c', 'IgnoreStandardIncludePath': 'true', 'PreprocessorDefinitions': '_UNICODE;UNICODE2', 'ResourceOutputFileName': '$(IntDir)%(Filename)3.res', 'ShowProgress': 'true'}, 'Manifest': { 'AdditionalManifestFiles': 'sfsdfsd', 'AdditionalOptions': 'afdsdafsd', 'AssemblyIdentity': 'sddfdsadfsa', 'ComponentFileName': 'fsdfds', 'GenerateCatalogFiles': 'true', 'InputResourceManifests': 'asfsfdafs', 'OutputManifestFile': '$(TargetPath).manifestdfs', 'RegistrarScriptFile': 'sdfsfd', 'ReplacementsFile': 'sdffsd', 'SuppressStartupBanner': 'false', 'TypeLibraryFile': 'sfsd', 'UpdateFileHashes': 'true', 'UpdateFileHashesSearchPath': 'sfsd', 'VerboseOutput': 'true'}, 'ProjectReference': { 'LinkLibraryDependencies': 'false', 'UseLibraryDependencyInputs': 'true'}, '': { 'EmbedManifest': 'false', 'GenerateManifest': 'false', 'IgnoreImportLibrary': 'true', 'LinkIncremental': '' }, 'ManifestResourceCompile': { 'ResourceOutputFileName': '$(IntDir)$(TargetFileName).embed.manifest.resfdsf'} } actual_msbuild_settings = MSVSSettings.ConvertToMSBuildSettings( msvs_settings, self.stderr) self.assertEqual(expected_msbuild_settings, actual_msbuild_settings) self._ExpectedWarnings([]) if __name__ == '__main__': unittest.main()
mit
shakamunyi/ansible
test/units/plugins/test_plugins.py
137
2968
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com> # # This file is part of Ansible # # Ansible is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Ansible is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. # Make coding more python3-ish from __future__ import (absolute_import, division, print_function) __metaclass__ = type import os from ansible.compat.tests import unittest from ansible.compat.tests import BUILTINS from ansible.compat.tests.mock import mock_open, patch, MagicMock from ansible.plugins import MODULE_CACHE, PATH_CACHE, PLUGIN_PATH_CACHE, _basedirs, push_basedir, PluginLoader class TestErrors(unittest.TestCase): def setUp(self): pass def tearDown(self): pass @patch.object(PluginLoader, '_get_paths') def test_print_paths(self, mock_method): mock_method.return_value = ['/path/one', '/path/two', '/path/three'] pl = PluginLoader('foo', 'foo', '', 'test_plugins') paths = pl.print_paths() expected_paths = os.pathsep.join(['/path/one', '/path/two', '/path/three']) self.assertEqual(paths, expected_paths) def test_plugins__get_package_paths_no_package(self): pl = PluginLoader('test', '', 'test', 'test_plugin') self.assertEqual(pl._get_package_paths(), []) def test_plugins__get_package_paths_with_package(self): # the _get_package_paths() call uses __import__ to load a # python library, and then uses the __file__ attribute of # the result for that to get the library path, so we mock # that here and patch the builtin to use our mocked result m = MagicMock() m.return_value.__file__ = '/path/to/my/test.py' pl = PluginLoader('test', 'foo.bar.bam', 'test', 'test_plugin') with patch('{0}.__import__'.format(BUILTINS), m): self.assertEqual(pl._get_package_paths(), ['/path/to/my/bar/bam']) def test_plugins__get_paths(self): pl = PluginLoader('test', '', 'test', 'test_plugin') pl._paths = ['/path/one', '/path/two'] self.assertEqual(pl._get_paths(), ['/path/one', '/path/two']) # NOT YET WORKING #def fake_glob(path): # if path == 'test/*': # return ['test/foo', 'test/bar', 'test/bam'] # elif path == 'test/*/*' #m._paths = None #mock_glob = MagicMock() #mock_glob.return_value = [] #with patch('glob.glob', mock_glob): # pass
gpl-3.0
marc-sensenich/ansible
lib/ansible/module_utils/common/_collections_compat.py
115
1190
# Copyright (c), Sviatoslav Sydorenko <ssydoren@redhat.com> 2018 # Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) """Collections ABC import shim. This module is intended only for internal use. It will go away once the bundled copy of six includes equivalent functionality. Third parties should not use this. """ from __future__ import absolute_import, division, print_function __metaclass__ = type try: """Python 3.3+ branch.""" from collections.abc import ( MappingView, ItemsView, KeysView, ValuesView, Mapping, MutableMapping, Sequence, MutableSequence, Set, MutableSet, Container, Hashable, Sized, Callable, Iterable, Iterator, ) except ImportError: """Use old lib location under 2.6-3.2.""" from collections import ( MappingView, ItemsView, KeysView, ValuesView, Mapping, MutableMapping, Sequence, MutableSequence, Set, MutableSet, Container, Hashable, Sized, Callable, Iterable, Iterator, )
gpl-3.0
sstoma/CellProfiler
cellprofiler/modules/images.py
2
24811
import cellprofiler.icons from cellprofiler.gui.help import PROTIP_RECOMEND_ICON, PROTIP_AVOID_ICON, TECH_NOTE_ICON, IMAGES_FILELIST_BLANK, IMAGES_FILELIST_FILLED __doc__ = """ The <b>Images</b> module specifies the location of image files to be analyzed by your pipeline. <hr> The <b>Images</b> module allows you to specify the location of files to be analyzed by the pipeline; setting this module correctly is the first step in creating a new project in CellProfiler. These files can be located on your hard drive, on a networked computer elsewhere, or accessible with a URL. You can also provide rules to specify only those files that you want analyzed out of a larger collection (for example, from a folder containing both images for analysis and non-image files that should be disregarded). <h4>What is a "digital image"?</h4> A <i>digital image</i> is a set of numbers arranged into a two-dimensional format of rows and columns; a pixel refers to the row/column location of a particular point in the image. Pixels in grayscale or monochrome (black/white) images contain a single intensity value, whereas in color images, each pixel contains a red, green, and blue (RGB) triplet of intensity values. Additionally, the term image can be used as short-hand for an image sequence, that is, an image collection such as a time-lapse series (2-D + <i>t</i>), confocal Z-stacks (3-D), etc. <p>CellProfiler can read a wide variety of image formats by using a library called Bio-Formats; see <a href="http://www.openmicroscopy.org/site/support/bio-formats5/supported-formats.html">here</a> for the formats available. Some image formats are better than others for use in image analysis. Some are <a href="http://www.techterms.com/definition/lossy">"lossy"</a> (information is lost in the conversion to the format) like most JPG/JPEG files; others are <a href="http://www.techterms.com/definition/lossless">"lossless"</a> (no image information is lost). For image analysis purposes, a lossless format like TIF or PNG is recommended.</p> <h4>What do I need as input?</h4> The most straightforward way to provide image files to the <b>Images</b> module is to simply drag-and-drop them on the file list panel (the blank space indicated by the text "Drop files and folders here"). <table cellpadding="0" width="100%%"> <tr align="center"><td><img src="memory:%(IMAGES_FILELIST_BLANK)s"></td></tr> </table> <p>Using the file explorer tool of your choice (e.g., Explorer in Windows, Finder in Mac), you can drag-and-drop individual files and/or entire folders into this panel. You can also right-click in the File list panel to bring up a file selection window to browse for individual files; on the Mac, folders can be drag-and-dropped from this window and you can select multiple files using Ctrl-A (Windows) or Cmd-A (Mac). <table cellpadding="0" width="100%%"> <tr align="center"><td><img src="memory:%(IMAGES_FILELIST_FILLED)s"></td></tr> </table> Right-clicking on the file list panel will provide a context menu with options to modify the file list: <ul> <li><i>Show Selected Image:</i> Selecting this option (or double-clicking on the file) will open the image in a new window.</li> <li><i>Remove From List:</i> Removes the selected file or folder from the list. Note that this does not remove the file/folder from the hard drive.</li> <li><i>Remove Unavailable Files:</i> Refresh the list by checking for existence of file. Note that this does not remove the file from the hard drive.</li> <li><i>Browse For Images:</i> Use a dialog box to select an image file (though drag-and-drop is recommended).</li> <li><i>Refresh:</i> Shown only if folder is selected. Refresh the list of files from the folder. Files that were manually removed from the list for that folder are restored.</li> <li><i>Expand All Folders:</i> Expand all trees shown in the file list panel.</li> <li><i>Collapse All Folders:</i> Collapse all folder trees shown in the file list panel.</li> <li><i>Clear File List:</i> Remove all files/folders in the file list panel. You will be prompted for confirmation beforehand.</li> </ul></p> <h4>What do the settings mean?</h4> If you have a subset of files that you want to analyze from the full listing shown in the panel, you can filter the files according to a set of rules. This is useful in cases such as: <ul> <li>You have dragged a folder of images onto the file list panel, but the folder contains images you want to analyze along with non-image files that you want to disregard.</li> <li>You have dragged a folder of images onto the file list panel, but the folder contains the images from one experiment that you want to process along with images from another experiment that you want to ignore for now. </li> </ul> You may specify as many rules as necessary to define the desired list of images. <p>After you have filtered the file list, press the "Apply" button to update the view of the file list. You can also toggle the "Show file excluded by filters" box to modify the display of the files: <ul> <li>Checking this box will show all the files in the list, with the files that have been filtered out shown as grayed-out entries.</li> <li>Not checking this box will only show the files in the list that pass the filter(s).</li> </ul></p> <h4>What do I get as output?</h4> The final product of the <b>Images</b> module is a file list in which any files that are not intended for further processing have been removed, whether manually or using filtering. This list will be used when collecting metadata (if desired) and when assembling the image sets in NamesAndTypes. The list can be filtered further in NamesAndTypes to specify, for example, that a subset of these images represents a particular wavelength. """%globals() # CellProfiler is distributed under the GNU General Public License. # See the accompanying file LICENSE for details. # # Copyright (c) 2003-2009 Massachusetts Institute of Technology # Copyright (c) 2009-2015 Broad Institute # # Please see the AUTHORS file for credits. # # Website: http://www.cellprofiler.org import cellprofiler.cpmodule as cpm import cellprofiler.pipeline as cpp import cellprofiler.preferences as cpprefs import cellprofiler.settings as cps from cellprofiler.settings import YES, NO import cellprofiler.workspace as cpw import cellprofiler.utilities.walk_in_background as W import javabridge as J import os import sys import urllib import uuid from .loadimages import pathname2url, SUPPORTED_IMAGE_EXTENSIONS from .loadimages import SUPPORTED_MOVIE_EXTENSIONS from cellprofiler.utilities.hdf5_dict import HDF5FileList from cellprofiler.gui.help import FILTER_RULES_BUTTONS_HELP FILTER_CHOICE_NONE = "No filtering" FILTER_CHOICE_IMAGES = "Images only" FILTER_CHOICE_CUSTOM = "Custom" FILTER_CHOICE_ALL = [FILTER_CHOICE_NONE, FILTER_CHOICE_IMAGES, FILTER_CHOICE_CUSTOM] FILTER_DEFAULT = 'and (extension does isimage) (directory doesnot containregexp "[\\\\\\\\/]\\\\.")' class Images(cpm.CPModule): variable_revision_number = 2 module_name = "Images" category = "File Processing" MI_SHOW_IMAGE = "Show image" MI_REMOVE = cps.FileCollectionDisplay.DeleteMenuItem("Remove from list") MI_REFRESH = "Refresh" def create_settings(self): self.workspace = None module_explanation = [ "To begin creating your project, use the %s module to compile" %self.module_name, "a list of files and/or folders that you want to analyze. You can also specify a set of rules", "to include only the desired files in your selected folders."] self.set_notes([" ".join(module_explanation)]) self.path_list_display = cps.PathListDisplay() predicates = [FilePredicate(), DirectoryPredicate(), ExtensionPredicate()] self.filter_choice = cps.Choice( "Filter images?", FILTER_CHOICE_ALL, value=FILTER_CHOICE_IMAGES, doc = """ The <b>Images</b> module will pass all the files specified in the file list panel downstream to have a meaningful name assigned to it (so other modules can access it) or optionally, to define the relationships between images and associated metadata. Enabling file filtering will allow you to specify a subset of the files from the file list panel by defining rules to filter the files. This approach is useful if, for example, you drag-and-dropped a folder onto the file list panel which contains a mixture of images that you want to analyze and other files that you want to ignore. <p>Several options are available for this setting: <ul> <li><i>%(FILTER_CHOICE_NONE)s:</i> Do not enable filtering; all files in the File list panel will be passed to downstream modules for processing. This option can be selected if you are sure that only images are specified in the list.</li> <li><i>%(FILTER_CHOICE_IMAGES)s:</i> Only image files will be passed to downstream modules. The permissible image formats are provided by a library called Bio-Formats; see <a href="http://www.openmicroscopy.org/site/support/bio-formats5/supported-formats.html">here</a> for the formats available.</li> <li><i>%(FILTER_CHOICE_CUSTOM)s:</i> Specify custom rules for selecting a subset of the files from the File list panel. This approach is useful if, for example, you drag-and-dropped a folder onto the File list panel which contains a mixture of images that you want to analyze and other files that you want to ignore.</li> </ul></p>"""%globals()) self.filter = cps.Filter("Select the rule criteria", predicates, FILTER_DEFAULT, doc = """ Specify a set of rules to narrow down the files to be analyzed. <p>%(FILTER_RULES_BUTTONS_HELP)s</p>"""%globals()) self.update_button = cps.PathListRefreshButton( "", "Apply filters to the file list", doc = """ <i>(Only displayed if filtering based on rules)</i><br> Re-display the file list, removing or graying out the files that do not pass the current filter. """) @staticmethod def modpath_to_url(modpath): if modpath[0] in ("http", "https", "ftp"): if len(modpath) == 1: return modpath[0] + ":" elif len(modpath) == 2: return modpath[0] + ":" + modpath[1] else: return modpath[0] + ":" + modpath[1] + "/" + "/".join( [urllib.quote(part) for part in modpath[2:]]) path = os.path.join(*modpath) return pathname2url(path) @staticmethod def url_to_modpath(url): if not url.lower().startswith("file:"): schema, rest = HDF5FileList.split_url(url) return [schema] + rest[0:1] + [urllib.unquote(part) for part in rest[1:]] path = urllib.url2pathname(url[5:]) parts = [] while True: new_path, part = os.path.split(path) if len(new_path) == 0 or len(part) == 0: parts.insert(0, path) break parts.insert(0, part) path = new_path return parts @classmethod def make_modpath_from_path(cls, path): result = [] while True: new_path, part = os.path.split(path) if len(new_path) == 0 or len(part) == 0: return [path] + result result.insert(0, part) path = new_path def settings(self): return [self.path_list_display, self.filter_choice, self.filter] def visible_settings(self): result = [self.path_list_display, self.filter_choice] if self.filter_choice == FILTER_CHOICE_CUSTOM: result += [self.filter, self.update_button] self.path_list_display.using_filter = True elif self.filter_choice == FILTER_CHOICE_IMAGES: result += [self.update_button] self.path_list_display.using_filter = True else: self.path_list_display.using_filter = False return result def change_causes_prepare_run(self, setting): '''Return True if a change to the settings requires a call to prepare_run Images should return True if any setting changes because that will affect the image plane descriptors passed onto later modules which will change the image set produced by the pipeline. ''' return setting in self.settings() @classmethod def is_input_module(self): return True def prepare_run(self, workspace): '''Create an IPD for every url that passes the filter''' if workspace.pipeline.in_batch_mode(): return True file_list = workspace.pipeline.file_list if self.filter_choice != FILTER_CHOICE_NONE: if self.filter_choice == FILTER_CHOICE_IMAGES: expression = FILTER_DEFAULT else: expression = self.filter.value_text env = J.get_env() ifcls = J.class_for_name("org.cellprofiler.imageset.ImageFile") scls = env.find_class("java/lang/String") iffilter = J.make_instance( "org/cellprofiler/imageset/filter/Filter", "(Ljava/lang/String;Ljava/lang/Class;)V", expression, ifcls) file_array = env.make_object_array(len(file_list), scls) for i, url in enumerate(file_list): if isinstance(url, unicode): ourl = env.new_string(url) else: ourl = env.new_string_utf(url) env.set_object_array_element(file_array, i, ourl) passes_filter = J.call( iffilter, "filterURLs", "([Ljava/lang/String;)[Z", file_array) if isinstance(passes_filter, J.JB_Object): passes_filter = J.get_env().get_boolean_array_elements( passes_filter) file_list = [f for f, passes in zip(file_list, passes_filter) if passes] workspace.pipeline.set_filtered_file_list(file_list, self) return True def run(self, workspace): pass def upgrade_settings(self, setting_values, variable_revision_number, module_name, from_matlab): '''Upgrade pipeline settings from a previous revision setting_values - the text values of the module's settings variable_revision_number - revision # of module version that saved them module_name / from_matlab - ignore please Returns upgraded setting values, revision number and matlab flag ''' if variable_revision_number == 1: # Changed from yes/no for filter to a choice filter_choice = \ FILTER_CHOICE_CUSTOM if setting_values[1] == cps.YES else\ FILTER_CHOICE_NONE setting_values = \ setting_values[:1] +[filter_choice] + setting_values[2:] variable_revision_number = 2 return setting_values, variable_revision_number, from_matlab class DirectoryPredicate(cps.Filter.FilterPredicate): '''A predicate that only filters directories''' def __init__(self): subpredicates = ( cps.Filter.CONTAINS_PREDICATE, cps.Filter.CONTAINS_REGEXP_PREDICATE, cps.Filter.STARTS_WITH_PREDICATE, cps.Filter.ENDSWITH_PREDICATE, cps.Filter.EQ_PREDICATE) predicates = [cps.Filter.DoesPredicate(subpredicates), cps.Filter.DoesNotPredicate(subpredicates)] cps.Filter.FilterPredicate.__init__(self, 'directory', "Directory", self.fn_filter, predicates, doc = "Apply the rule to directories") def fn_filter(self, (node_type, modpath, module), *args): '''The DirectoryPredicate filter function The arg slot expects a tuple of node_type and modpath. The predicate returns None (= agnostic about filtering) if the node is not a directory, otherwise it composites the modpath into a file path and applies it to the rest of the args. ''' if isinstance(modpath[-1], tuple) and len(modpath[-1]) == 3: path = os.path.join(*modpath[:-2]) else: path = os.path.join(*modpath[:-1]) return args[0](path, *args[1:]) def test_valid(self, pipeline, *args): self((cps.FileCollectionDisplay.NODE_FILE, ["/imaging","image.tif"], None), *args) class FilePredicate(cps.Filter.FilterPredicate): '''A predicate that only filters files''' def __init__(self): subpredicates = ( cps.Filter.CONTAINS_PREDICATE, cps.Filter.CONTAINS_REGEXP_PREDICATE, cps.Filter.STARTS_WITH_PREDICATE, cps.Filter.ENDSWITH_PREDICATE, cps.Filter.EQ_PREDICATE) predicates = [cps.Filter.DoesPredicate(subpredicates), cps.Filter.DoesNotPredicate(subpredicates)] cps.Filter.FilterPredicate.__init__(self, 'file', "File", self.fn_filter, predicates, doc = "Apply the rule to files") def fn_filter(self, (node_type, modpath, module), *args): '''The FilePredicate filter function The arg slot expects a tuple of node_type and modpath. The predicate returns None (= agnostic about filtering) if the node is not a directory, otherwise it composites the modpath into a file path and applies it to the rest of the args ''' if node_type == cps.FileCollectionDisplay.NODE_DIRECTORY: return None elif isinstance(modpath[-1], tuple) and len(modpath[-1]) == 3: filename = modpath[-2] else: filename = modpath[-1] return args[0](filename, *args[1:]) def test_valid(self, pipeline, *args): self((cps.FileCollectionDisplay.NODE_FILE, ["/imaging", "test.tif"], None), *args) def is_image_extension(suffix): '''Return True if the extension is one of those recongized by bioformats''' extensions = J.get_collection_wrapper( J.static_call("org/cellprofiler/imageset/filter/IsImagePredicate", "getImageSuffixes", "()Ljava/util/Set;")) return extensions.contains(suffix.lower()) class ExtensionPredicate(cps.Filter.FilterPredicate): '''A predicate that operates on file extensions''' IS_TIF_PREDICATE = cps.Filter.FilterPredicate( "istif", '"tif", "tiff", "ome.tif" or "ome.tiff"', lambda x: x.lower() in ("tif", "tiff", "ome.tif", "ome.tiff"), [], doc="The extension is associated with TIFF image files") IS_JPEG_PREDICATE = cps.Filter.FilterPredicate( "isjpeg", '"jpg" or "jpeg"', lambda x: x.lower() in ("jpg", "jpeg"), [], doc = "The extension is associated with JPEG image files") IS_PNG_PREDICATE = cps.Filter.FilterPredicate( "ispng", '"png"', lambda x: x.lower() == "png", [], doc = "The extension is associated with PNG image files") IS_IMAGE_PREDICATE = cps.Filter.FilterPredicate( 'isimage', 'the extension of an image file', is_image_extension, [], 'Is an extension commonly associated with image files') IS_FLEX_PREDICATE = cps.Filter.FilterPredicate( 'isflex', '"flex"', lambda x: x.lower() == "flex", [], doc = "The extension is associated with .flex files") IS_MOVIE_PREDICATE = cps.Filter.FilterPredicate( "ismovie", '"mov" or "avi"', lambda x: x.lower() in ("mov", "avi"), [], doc = "The extension is associated with movie files") def __init__(self): subpredicates = ( self.IS_TIF_PREDICATE, self.IS_JPEG_PREDICATE, self.IS_PNG_PREDICATE, self.IS_IMAGE_PREDICATE, self.IS_FLEX_PREDICATE, self.IS_MOVIE_PREDICATE) predicates = [ cps.Filter.DoesPredicate(subpredicates, "Is"), cps.Filter.DoesNotPredicate(subpredicates, "Is not")] cps.Filter.FilterPredicate.__init__(self, 'extension', "Extension", self.fn_filter, predicates, doc="The rule applies to the file extension") def fn_filter(self, (node_type, modpath, module), *args): '''The ExtensionPredicate filter function If the element is a file, try the different predicates on all possible extension parsings. ''' if node_type == cps.FileCollectionDisplay.NODE_DIRECTORY: return None elif isinstance(modpath[-1], tuple) and len(modpath[-1]) == 3: filename = modpath[-2] else: filename = modpath[-1] exts = [] while True: filename, ext = os.path.splitext(filename) if len(filename) == 0 or len(ext) == 0: return False exts.insert(0, ext[1:]) ext = '.'.join(exts) if args[0](ext, *args[1:]): return True def test_valid(self, pipeline, *args): self((cps.FileCollectionDisplay.NODE_FILE, ["/imaging", "test.tif"], None), *args) class ImagePredicate(cps.Filter.FilterPredicate): '''A predicate that applies subpredicates to image plane details''' IS_COLOR_PREDICATE = cps.Filter.FilterPredicate( "iscolor", "Color", lambda x: ( x.metadata.has_key(cpp.ImagePlaneDetails.MD_COLOR_FORMAT) and x.metadata[cpp.ImagePlaneDetails.MD_COLOR_FORMAT] == cpp.ImagePlaneDetails.MD_RGB), [], doc = "The image is an interleaved color image (for example, a PNG image)") IS_MONOCHROME_PREDICATE = cps.Filter.FilterPredicate( "ismonochrome", "Monochrome", lambda x: ( x.metadata.has_key(cpp.ImagePlaneDetails.MD_COLOR_FORMAT) and x.metadata[cpp.ImagePlaneDetails.MD_COLOR_FORMAT] == cpp.ImagePlaneDetails.MD_MONOCHROME), [], doc = "The image is monochrome") @staticmethod def is_stack(x): if (x.metadata.has_key(cpp.ImagePlaneDetails.MD_SIZE_T) and x.metadata[cpp.ImagePlaneDetails.MD_SIZE_T] > 1): return True if (x.metadata.has_key(cpp.ImagePlaneDetails.MD_SIZE_Z) and x.metadata[cpp.ImagePlaneDetails.MD_SIZE_Z] > 1): return True return False IS_STACK_PREDICATE = cps.Filter.FilterPredicate( "isstack", "Stack", lambda x: ImagePredicate.is_stack(x), [], doc = "The image is a Z-stack or movie") IS_STACK_FRAME_PREDICATE = cps.Filter.FilterPredicate( "isstackframe", "Stack frame", lambda x: x.index is not None, [], doc = "The image is a frame of a movie or a plane of a Z-stack") def __init__(self): subpredicates = ( self.IS_COLOR_PREDICATE, self.IS_MONOCHROME_PREDICATE, self.IS_STACK_PREDICATE, self.IS_STACK_FRAME_PREDICATE) predicates = [ pred_class(subpredicates, text) for pred_class, text in ( (cps.Filter.DoesPredicate, "Is"), (cps.Filter.DoesNotPredicate, "Is not"))] cps.Filter.FilterPredicate.__init__(self, 'image', "Image", self.fn_filter, predicates, doc = "Filter based on image characteristics") def fn_filter(self, (node_type, modpath, module), *args): if node_type == cps.FileCollectionDisplay.NODE_DIRECTORY: return None ipd = module.get_image_plane_details(modpath) if ipd is None: return None return args[0](ipd, *args[1:]) class FakeModule(cpm.CPModule): '''A fake module for setting validation''' def get_image_plane_details(self, modpath): url = Images.modpath_to_url(modpath) return cpp.ImagePlaneDetails(url, None, None, None) def test_valid(self, pipeline, *args): self((cps.FileCollectionDisplay.NODE_FILE, ["/imaging", "test.tif"], self.FakeModule()), *args)
gpl-2.0
mogoweb/webkit_for_android5.1
webkit/Tools/Scripts/webkitpy/layout_tests/port/mac_unittest.py
15
6206
# Copyright (C) 2010 Google Inc. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # copyright notice, this list of conditions and the following disclaimer # in the documentation and/or other materials provided with the # distribution. # * Neither the name of Google Inc. nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import StringIO import sys import unittest from webkitpy.layout_tests.port import mac from webkitpy.layout_tests.port import port_testcase class MacTest(port_testcase.PortTestCase): def port_maker(self, platform): if platform != 'darwin': return None return mac.MacPort def assert_skipped_files_for_version(self, port_name, expected_paths): port = mac.MacPort(port_name=port_name) skipped_paths = port._skipped_file_paths() # FIXME: _skipped_file_paths should return WebKit-relative paths. # So to make it unit testable, we strip the WebKit directory from the path. relative_paths = [path[len(port.path_from_webkit_base()):] for path in skipped_paths] self.assertEqual(relative_paths, expected_paths) def test_skipped_file_paths(self): # We skip this on win32 because we use '/' as the dir separator and it's # not worth making platform-independent. if sys.platform == 'win32': return None self.assert_skipped_files_for_version('mac-snowleopard', ['/LayoutTests/platform/mac-snowleopard/Skipped', '/LayoutTests/platform/mac/Skipped']) self.assert_skipped_files_for_version('mac-leopard', ['/LayoutTests/platform/mac-leopard/Skipped', '/LayoutTests/platform/mac/Skipped']) example_skipped_file = u""" # <rdar://problem/5647952> fast/events/mouseout-on-window.html needs mac DRT to issue mouse out events fast/events/mouseout-on-window.html # <rdar://problem/5643675> window.scrollTo scrolls a window with no scrollbars fast/events/attempt-scroll-with-no-scrollbars.html # see bug <rdar://problem/5646437> REGRESSION (r28015): svg/batik/text/smallFonts fails svg/batik/text/smallFonts.svg """ example_skipped_tests = [ "fast/events/mouseout-on-window.html", "fast/events/attempt-scroll-with-no-scrollbars.html", "svg/batik/text/smallFonts.svg", ] def test_tests_from_skipped_file_contents(self): port = mac.MacPort() self.assertEqual(port._tests_from_skipped_file_contents(self.example_skipped_file), self.example_skipped_tests) def assert_name(self, port_name, os_version_string, expected): port = mac.MacPort(port_name=port_name, os_version_string=os_version_string) self.assertEquals(expected, port.name()) def test_tests_for_other_platforms(self): port = mac.MacPort(port_name='mac-snowleopard') dirs_to_skip = port._tests_for_other_platforms() self.assertTrue('platform/chromium-linux' in dirs_to_skip) self.assertTrue('platform/mac-tiger' in dirs_to_skip) self.assertFalse('platform/mac' in dirs_to_skip) self.assertFalse('platform/mac-snowleopard' in dirs_to_skip) def test_version(self): port = mac.MacPort() self.assertTrue(port.version()) def test_versions(self): port = self.make_port() if port: self.assertTrue(port.name() in ('mac-tiger', 'mac-leopard', 'mac-snowleopard', 'mac-future')) self.assert_name(None, '10.4.8', 'mac-tiger') self.assert_name('mac', '10.4.8', 'mac-tiger') self.assert_name('mac-tiger', '10.4.8', 'mac-tiger') self.assert_name('mac-tiger', '10.5.3', 'mac-tiger') self.assert_name('mac-tiger', '10.6.3', 'mac-tiger') self.assert_name(None, '10.5.3', 'mac-leopard') self.assert_name('mac', '10.5.3', 'mac-leopard') self.assert_name('mac-leopard', '10.4.8', 'mac-leopard') self.assert_name('mac-leopard', '10.5.3', 'mac-leopard') self.assert_name('mac-leopard', '10.6.3', 'mac-leopard') self.assert_name(None, '10.6.3', 'mac-snowleopard') self.assert_name('mac', '10.6.3', 'mac-snowleopard') self.assert_name('mac-snowleopard', '10.4.3', 'mac-snowleopard') self.assert_name('mac-snowleopard', '10.5.3', 'mac-snowleopard') self.assert_name('mac-snowleopard', '10.6.3', 'mac-snowleopard') self.assert_name(None, '10.7', 'mac-future') self.assert_name(None, '10.7.3', 'mac-future') self.assert_name(None, '10.8', 'mac-future') self.assert_name('mac', '10.7.3', 'mac-future') self.assert_name('mac-future', '10.4.3', 'mac-future') self.assert_name('mac-future', '10.5.3', 'mac-future') self.assert_name('mac-future', '10.6.3', 'mac-future') self.assert_name('mac-future', '10.7.3', 'mac-future') self.assertRaises(AssertionError, self.assert_name, None, '10.3.1', 'should-raise-assertion-so-this-value-does-not-matter') if __name__ == '__main__': unittest.main()
apache-2.0
andris210296/andris-projeto
backend/venv/test/lib/python2.7/site-packages/unidecode/x0fb.py
252
3838
data = ( 'ff', # 0x00 'fi', # 0x01 'fl', # 0x02 'ffi', # 0x03 'ffl', # 0x04 'st', # 0x05 'st', # 0x06 '[?]', # 0x07 '[?]', # 0x08 '[?]', # 0x09 '[?]', # 0x0a '[?]', # 0x0b '[?]', # 0x0c '[?]', # 0x0d '[?]', # 0x0e '[?]', # 0x0f '[?]', # 0x10 '[?]', # 0x11 '[?]', # 0x12 'mn', # 0x13 'me', # 0x14 'mi', # 0x15 'vn', # 0x16 'mkh', # 0x17 '[?]', # 0x18 '[?]', # 0x19 '[?]', # 0x1a '[?]', # 0x1b '[?]', # 0x1c 'yi', # 0x1d '', # 0x1e 'ay', # 0x1f '`', # 0x20 '', # 0x21 'd', # 0x22 'h', # 0x23 'k', # 0x24 'l', # 0x25 'm', # 0x26 'm', # 0x27 't', # 0x28 '+', # 0x29 'sh', # 0x2a 's', # 0x2b 'sh', # 0x2c 's', # 0x2d 'a', # 0x2e 'a', # 0x2f '', # 0x30 'b', # 0x31 'g', # 0x32 'd', # 0x33 'h', # 0x34 'v', # 0x35 'z', # 0x36 '[?]', # 0x37 't', # 0x38 'y', # 0x39 'k', # 0x3a 'k', # 0x3b 'l', # 0x3c '[?]', # 0x3d 'l', # 0x3e '[?]', # 0x3f 'n', # 0x40 'n', # 0x41 '[?]', # 0x42 'p', # 0x43 'p', # 0x44 '[?]', # 0x45 'ts', # 0x46 'ts', # 0x47 'r', # 0x48 'sh', # 0x49 't', # 0x4a 'vo', # 0x4b 'b', # 0x4c 'k', # 0x4d 'p', # 0x4e 'l', # 0x4f '', # 0x50 '', # 0x51 '', # 0x52 '', # 0x53 '', # 0x54 '', # 0x55 '', # 0x56 '', # 0x57 '', # 0x58 '', # 0x59 '', # 0x5a '', # 0x5b '', # 0x5c '', # 0x5d '', # 0x5e '', # 0x5f '', # 0x60 '', # 0x61 '', # 0x62 '', # 0x63 '', # 0x64 '', # 0x65 '', # 0x66 '', # 0x67 '', # 0x68 '', # 0x69 '', # 0x6a '', # 0x6b '', # 0x6c '', # 0x6d '', # 0x6e '', # 0x6f '', # 0x70 '', # 0x71 '', # 0x72 '', # 0x73 '', # 0x74 '', # 0x75 '', # 0x76 '', # 0x77 '', # 0x78 '', # 0x79 '', # 0x7a '', # 0x7b '', # 0x7c '', # 0x7d '', # 0x7e '', # 0x7f '', # 0x80 '', # 0x81 '', # 0x82 '', # 0x83 '', # 0x84 '', # 0x85 '', # 0x86 '', # 0x87 '', # 0x88 '', # 0x89 '', # 0x8a '', # 0x8b '', # 0x8c '', # 0x8d '', # 0x8e '', # 0x8f '', # 0x90 '', # 0x91 '', # 0x92 '', # 0x93 '', # 0x94 '', # 0x95 '', # 0x96 '', # 0x97 '', # 0x98 '', # 0x99 '', # 0x9a '', # 0x9b '', # 0x9c '', # 0x9d '', # 0x9e '', # 0x9f '', # 0xa0 '', # 0xa1 '', # 0xa2 '', # 0xa3 '', # 0xa4 '', # 0xa5 '', # 0xa6 '', # 0xa7 '', # 0xa8 '', # 0xa9 '', # 0xaa '', # 0xab '', # 0xac '', # 0xad '', # 0xae '', # 0xaf '', # 0xb0 '', # 0xb1 '[?]', # 0xb2 '[?]', # 0xb3 '[?]', # 0xb4 '[?]', # 0xb5 '[?]', # 0xb6 '[?]', # 0xb7 '[?]', # 0xb8 '[?]', # 0xb9 '[?]', # 0xba '[?]', # 0xbb '[?]', # 0xbc '[?]', # 0xbd '[?]', # 0xbe '[?]', # 0xbf '[?]', # 0xc0 '[?]', # 0xc1 '[?]', # 0xc2 '[?]', # 0xc3 '[?]', # 0xc4 '[?]', # 0xc5 '[?]', # 0xc6 '[?]', # 0xc7 '[?]', # 0xc8 '[?]', # 0xc9 '[?]', # 0xca '[?]', # 0xcb '[?]', # 0xcc '[?]', # 0xcd '[?]', # 0xce '[?]', # 0xcf '[?]', # 0xd0 '[?]', # 0xd1 '[?]', # 0xd2 '', # 0xd3 '', # 0xd4 '', # 0xd5 '', # 0xd6 '', # 0xd7 '', # 0xd8 '', # 0xd9 '', # 0xda '', # 0xdb '', # 0xdc '', # 0xdd '', # 0xde '', # 0xdf '', # 0xe0 '', # 0xe1 '', # 0xe2 '', # 0xe3 '', # 0xe4 '', # 0xe5 '', # 0xe6 '', # 0xe7 '', # 0xe8 '', # 0xe9 '', # 0xea '', # 0xeb '', # 0xec '', # 0xed '', # 0xee '', # 0xef '', # 0xf0 '', # 0xf1 '', # 0xf2 '', # 0xf3 '', # 0xf4 '', # 0xf5 '', # 0xf6 '', # 0xf7 '', # 0xf8 '', # 0xf9 '', # 0xfa '', # 0xfb '', # 0xfc '', # 0xfd '', # 0xfe '', # 0xff )
mit
Winand/pandas
pandas/io/gbq.py
13
4006
""" Google BigQuery support """ def _try_import(): # since pandas is a dependency of pandas-gbq # we need to import on first use try: import pandas_gbq except ImportError: # give a nice error message raise ImportError("Load data from Google BigQuery\n" "\n" "the pandas-gbq package is not installed\n" "see the docs: https://pandas-gbq.readthedocs.io\n" "\n" "you can install via pip or conda:\n" "pip install pandas-gbq\n" "conda install pandas-gbq -c conda-forge\n") return pandas_gbq def read_gbq(query, project_id=None, index_col=None, col_order=None, reauth=False, verbose=True, private_key=None, dialect='legacy', **kwargs): r"""Load data from Google BigQuery. The main method a user calls to execute a Query in Google BigQuery and read results into a pandas DataFrame. Google BigQuery API Client Library v2 for Python is used. Documentation is available `here <https://developers.google.com/api-client-library/python/apis/bigquery/v2>`__ Authentication to the Google BigQuery service is via OAuth 2.0. - If "private_key" is not provided: By default "application default credentials" are used. If default application credentials are not found or are restrictive, user account credentials are used. In this case, you will be asked to grant permissions for product name 'pandas GBQ'. - If "private_key" is provided: Service account credentials will be used to authenticate. Parameters ---------- query : str SQL-Like Query to return data values project_id : str Google BigQuery Account project ID. index_col : str (optional) Name of result column to use for index in results DataFrame col_order : list(str) (optional) List of BigQuery column names in the desired order for results DataFrame reauth : boolean (default False) Force Google BigQuery to reauthenticate the user. This is useful if multiple accounts are used. verbose : boolean (default True) Verbose output private_key : str (optional) Service account private key in JSON format. Can be file path or string contents. This is useful for remote server authentication (eg. jupyter iPython notebook on remote host) dialect : {'legacy', 'standard'}, default 'legacy' 'legacy' : Use BigQuery's legacy SQL dialect. 'standard' : Use BigQuery's standard SQL (beta), which is compliant with the SQL 2011 standard. For more information see `BigQuery SQL Reference <https://cloud.google.com/bigquery/sql-reference/>`__ **kwargs : Arbitrary keyword arguments configuration (dict): query config parameters for job processing. For example: configuration = {'query': {'useQueryCache': False}} For more information see `BigQuery SQL Reference <https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query>`__ Returns ------- df: DataFrame DataFrame representing results of query """ pandas_gbq = _try_import() return pandas_gbq.read_gbq( query, project_id=project_id, index_col=index_col, col_order=col_order, reauth=reauth, verbose=verbose, private_key=private_key, dialect=dialect, **kwargs) def to_gbq(dataframe, destination_table, project_id, chunksize=10000, verbose=True, reauth=False, if_exists='fail', private_key=None): pandas_gbq = _try_import() pandas_gbq.to_gbq(dataframe, destination_table, project_id, chunksize=chunksize, verbose=verbose, reauth=reauth, if_exists=if_exists, private_key=private_key)
bsd-3-clause
TakashiMatsuda/sag_svm
readdata.py
1
1985
#!/Users/takashi/.pyenv/shims/python from scipy.io import arff as sparff import numpy as np def read_data(filename): """ Extract the item specifying the features and the labels """ d = sparff.loadarff(filename) data = datacleaning(d[0]) return data def datacleaning(data): n_data = np.zeros((len(data), len(data[0]) - 1)) misslist = [] labels = np.zeros(len(data)) for i, v_x in enumerate(data): for j, vv_x in enumerate(v_x): if vv_x in {bytes(b'?'), '?'}: # REMOVE this entry misslist.append(i) break elif j in {2, 3, 4}: n_data[i][j] = float(vv_x) continue elif j in {5, 6}: n_data[i][j] = (1) if (vv_x == bytes(b'normal')) else (-1) continue elif j in {7, 8}: n_data[i][j] = (1) if (vv_x == bytes(b'present')) else (-1) continue elif j in {18, 19, 20, 22, 23}: n_data[i][j] = (1) if (vv_x == bytes(b'yes')) else (-1) continue elif j in {21}: n_data[i][j] = (1) if (vv_x == bytes(b'good')) else (-1) continue elif j in {24}: labels[i] = (1) if (vv_x == bytes(b'ckd')) else (-1) continue else: if np.isnan(vv_x): misslist.append(i) break else: n_data[i][j] = vv_x continue slc_idx = np.array([(True) if v not in misslist else (False) for v in range(len(data))]) return (n_data[slc_idx], labels[slc_idx]) def test_readdata(): fn = "./chronic_kidney_disease/Chronic_Kidney_Disease_full.arff" d = read_data(fn) x = d[0] y = d[1] print(d) assert x[0][7] == 0 assert x[1][1] == 70 assert y[0] == 1 assert y[1] == 1
mit
damdam-s/OpenUpgrade
openerp/addons/test_impex/models.py
392
5891
# -*- coding: utf-8 -*- from openerp.osv import orm, fields def selection_fn(obj, cr, uid, context=None): return list(enumerate(["Corge", "Grault", "Wheee", "Moog"])) def function_fn(model, cr, uid, ids, field_name, arg, context): return dict((id, 3) for id in ids) def function_fn_write(model, cr, uid, id, field_name, field_value, fnct_inv_arg, context): """ just so CreatorCase.export can be used """ pass models = [ ('boolean', fields.boolean()), ('integer', fields.integer()), ('float', fields.float()), ('decimal', fields.float(digits=(16, 3))), ('string.bounded', fields.char('unknown', size=16)), ('string.required', fields.char('unknown', size=None, required=True)), ('string', fields.char('unknown', size=None)), ('date', fields.date()), ('datetime', fields.datetime()), ('text', fields.text()), ('selection', fields.selection([(1, "Foo"), (2, "Bar"), (3, "Qux"), (4, '')])), # here use size=-1 to store the values as integers instead of strings ('selection.function', fields.selection(selection_fn, size=-1)), # just relate to an integer ('many2one', fields.many2one('export.integer')), ('one2many', fields.one2many('export.one2many.child', 'parent_id')), ('many2many', fields.many2many('export.many2many.other')), ('function', fields.function(function_fn, fnct_inv=function_fn_write, type="integer")), # related: specialization of fields.function, should work the same way # TODO: reference ] for name, field in models: class NewModel(orm.Model): _name = 'export.%s' % name _columns = { 'const': fields.integer(), 'value': field, } _defaults = { 'const': 4, } def name_get(self, cr, uid, ids, context=None): return [(record.id, "%s:%s" % (self._name, record.value)) for record in self.browse(cr, uid, ids, context=context)] def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100): if isinstance(name, basestring) and name.split(':')[0] == self._name: ids = self.search(cr, user, [['value', operator, int(name.split(':')[1])]]) return self.name_get(cr, user, ids, context=context) else: return [] class One2ManyChild(orm.Model): _name = 'export.one2many.child' # FIXME: orm.py:1161, fix to name_get on m2o field _rec_name = 'value' _columns = { 'parent_id': fields.many2one('export.one2many'), 'str': fields.char('unknown', size=None), 'value': fields.integer(), } def name_get(self, cr, uid, ids, context=None): return [(record.id, "%s:%s" % (self._name, record.value)) for record in self.browse(cr, uid, ids, context=context)] def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100): if isinstance(name, basestring) and name.split(':')[0] == self._name: ids = self.search(cr, user, [['value', operator, int(name.split(':')[1])]]) return self.name_get(cr, user, ids, context=context) else: return [] class One2ManyMultiple(orm.Model): _name = 'export.one2many.multiple' _columns = { 'parent_id': fields.many2one('export.one2many.recursive'), 'const': fields.integer(), 'child1': fields.one2many('export.one2many.child.1', 'parent_id'), 'child2': fields.one2many('export.one2many.child.2', 'parent_id'), } _defaults = { 'const': 36, } class One2ManyChildMultiple(orm.Model): _name = 'export.one2many.multiple.child' # FIXME: orm.py:1161, fix to name_get on m2o field _rec_name = 'value' _columns = { 'parent_id': fields.many2one('export.one2many.multiple'), 'str': fields.char('unknown', size=None), 'value': fields.integer(), } def name_get(self, cr, uid, ids, context=None): return [(record.id, "%s:%s" % (self._name, record.value)) for record in self.browse(cr, uid, ids, context=context)] class One2ManyChild1(orm.Model): _name = 'export.one2many.child.1' _inherit = 'export.one2many.multiple.child' class One2ManyChild2(orm.Model): _name = 'export.one2many.child.2' _inherit = 'export.one2many.multiple.child' class Many2ManyChild(orm.Model): _name = 'export.many2many.other' # FIXME: orm.py:1161, fix to name_get on m2o field _rec_name = 'value' _columns = { 'str': fields.char('unknown', size=None), 'value': fields.integer(), } def name_get(self, cr, uid, ids, context=None): return [(record.id, "%s:%s" % (self._name, record.value)) for record in self.browse(cr, uid, ids, context=context)] def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100): if isinstance(name, basestring) and name.split(':')[0] == self._name: ids = self.search(cr, user, [['value', operator, int(name.split(':')[1])]]) return self.name_get(cr, user, ids, context=context) else: return [] class SelectionWithDefault(orm.Model): _name = 'export.selection.withdefault' _columns = { 'const': fields.integer(), 'value': fields.selection([(1, "Foo"), (2, "Bar")]), } _defaults = { 'const': 4, 'value': 2, } class RecO2M(orm.Model): _name = 'export.one2many.recursive' _columns = { 'value': fields.integer(), 'child': fields.one2many('export.one2many.multiple', 'parent_id'), } class OnlyOne(orm.Model): _name = 'export.unique' _columns = { 'value': fields.integer(), } _sql_constraints = [ ('value_unique', 'unique (value)', "The value must be unique"), ]
agpl-3.0
bartoldeman/easybuild-easyblocks
easybuild/easyblocks/o/openssl.py
2
3053
## # Copyright 2009-2018 Ghent University # # This file is part of EasyBuild, # originally created by the HPC team of Ghent University (http://ugent.be/hpc/en), # with support of Ghent University (http://ugent.be/hpc), # the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be), # Flemish Research Foundation (FWO) (http://www.fwo.be/en) # and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en). # # https://github.com/easybuilders/easybuild # # EasyBuild is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation v2. # # EasyBuild is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with EasyBuild. If not, see <http://www.gnu.org/licenses/>. ## """ EasyBuild support for OpenSSL, implemented as an easyblock @author: Kenneth Hoste (Ghent University) @author: Jens Timmerman (Ghent University) @author: Davide Vanzo (ACCRE - Vanderbilt University) """ import os from distutils.version import LooseVersion from easybuild.easyblocks.generic.configuremake import ConfigureMake from easybuild.tools.build_log import EasyBuildError from easybuild.tools.run import run_cmd class EB_OpenSSL(ConfigureMake): """Support for building OpenSSL""" def configure_step(self, cmd_prefix=''): """ Configure step """ cmd = "%s %s./config --prefix=%s threads shared %s" % (self.cfg['preconfigopts'], cmd_prefix, self.installdir, self.cfg['configopts']) (out, _) = run_cmd(cmd, log_all=True, simple=False) return out def sanity_check_step(self): """Custom sanity check""" libdir = None for libdir_cand in ['lib', 'lib64']: if os.path.exists(os.path.join(self.installdir, libdir_cand)): libdir = libdir_cand if libdir is None: raise EasyBuildError("Failed to determine library directory.") custom_paths = { 'files': [os.path.join(libdir, x) for x in ['libcrypto.a', 'libcrypto.so', 'libssl.a', 'libssl.so']] + ['bin/openssl'], 'dirs': [], } if LooseVersion(self.version) < LooseVersion("1.1"): custom_paths['files'].extend([os.path.join(libdir, 'libcrypto.so.1.0.0'), os.path.join(libdir, 'libssl.so.1.0.0')]) custom_paths['dirs'].append(os.path.join(libdir, 'engines')) else: custom_paths['files'].extend([os.path.join(libdir, 'libcrypto.so.1.1'), os.path.join(libdir, 'libssl.so.1.1')]) custom_paths['dirs'].append(os.path.join(libdir, 'engines-1.1')) super(EB_OpenSSL, self).sanity_check_step(custom_paths=custom_paths)
gpl-2.0
alordmg/dulacdesign-wp
wp-content/themes/dulacdesign/node_modules/gulp-sass/node_modules/node-sass/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py
1283
65086
# Copyright (c) 2012 Google Inc. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """ This module contains classes that help to emulate xcodebuild behavior on top of other build systems, such as make and ninja. """ import copy import gyp.common import os import os.path import re import shlex import subprocess import sys import tempfile from gyp.common import GypError # Populated lazily by XcodeVersion, for efficiency, and to fix an issue when # "xcodebuild" is called too quickly (it has been found to return incorrect # version number). XCODE_VERSION_CACHE = None # Populated lazily by GetXcodeArchsDefault, to an |XcodeArchsDefault| instance # corresponding to the installed version of Xcode. XCODE_ARCHS_DEFAULT_CACHE = None def XcodeArchsVariableMapping(archs, archs_including_64_bit=None): """Constructs a dictionary with expansion for $(ARCHS_STANDARD) variable, and optionally for $(ARCHS_STANDARD_INCLUDING_64_BIT).""" mapping = {'$(ARCHS_STANDARD)': archs} if archs_including_64_bit: mapping['$(ARCHS_STANDARD_INCLUDING_64_BIT)'] = archs_including_64_bit return mapping class XcodeArchsDefault(object): """A class to resolve ARCHS variable from xcode_settings, resolving Xcode macros and implementing filtering by VALID_ARCHS. The expansion of macros depends on the SDKROOT used ("macosx", "iphoneos", "iphonesimulator") and on the version of Xcode. """ # Match variable like $(ARCHS_STANDARD). variable_pattern = re.compile(r'\$\([a-zA-Z_][a-zA-Z0-9_]*\)$') def __init__(self, default, mac, iphonesimulator, iphoneos): self._default = (default,) self._archs = {'mac': mac, 'ios': iphoneos, 'iossim': iphonesimulator} def _VariableMapping(self, sdkroot): """Returns the dictionary of variable mapping depending on the SDKROOT.""" sdkroot = sdkroot.lower() if 'iphoneos' in sdkroot: return self._archs['ios'] elif 'iphonesimulator' in sdkroot: return self._archs['iossim'] else: return self._archs['mac'] def _ExpandArchs(self, archs, sdkroot): """Expands variables references in ARCHS, and remove duplicates.""" variable_mapping = self._VariableMapping(sdkroot) expanded_archs = [] for arch in archs: if self.variable_pattern.match(arch): variable = arch try: variable_expansion = variable_mapping[variable] for arch in variable_expansion: if arch not in expanded_archs: expanded_archs.append(arch) except KeyError as e: print 'Warning: Ignoring unsupported variable "%s".' % variable elif arch not in expanded_archs: expanded_archs.append(arch) return expanded_archs def ActiveArchs(self, archs, valid_archs, sdkroot): """Expands variables references in ARCHS, and filter by VALID_ARCHS if it is defined (if not set, Xcode accept any value in ARCHS, otherwise, only values present in VALID_ARCHS are kept).""" expanded_archs = self._ExpandArchs(archs or self._default, sdkroot or '') if valid_archs: filtered_archs = [] for arch in expanded_archs: if arch in valid_archs: filtered_archs.append(arch) expanded_archs = filtered_archs return expanded_archs def GetXcodeArchsDefault(): """Returns the |XcodeArchsDefault| object to use to expand ARCHS for the installed version of Xcode. The default values used by Xcode for ARCHS and the expansion of the variables depends on the version of Xcode used. For all version anterior to Xcode 5.0 or posterior to Xcode 5.1 included uses $(ARCHS_STANDARD) if ARCHS is unset, while Xcode 5.0 to 5.0.2 uses $(ARCHS_STANDARD_INCLUDING_64_BIT). This variable was added to Xcode 5.0 and deprecated with Xcode 5.1. For "macosx" SDKROOT, all version starting with Xcode 5.0 includes 64-bit architecture as part of $(ARCHS_STANDARD) and default to only building it. For "iphoneos" and "iphonesimulator" SDKROOT, 64-bit architectures are part of $(ARCHS_STANDARD_INCLUDING_64_BIT) from Xcode 5.0. From Xcode 5.1, they are also part of $(ARCHS_STANDARD). All thoses rules are coded in the construction of the |XcodeArchsDefault| object to use depending on the version of Xcode detected. The object is for performance reason.""" global XCODE_ARCHS_DEFAULT_CACHE if XCODE_ARCHS_DEFAULT_CACHE: return XCODE_ARCHS_DEFAULT_CACHE xcode_version, _ = XcodeVersion() if xcode_version < '0500': XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault( '$(ARCHS_STANDARD)', XcodeArchsVariableMapping(['i386']), XcodeArchsVariableMapping(['i386']), XcodeArchsVariableMapping(['armv7'])) elif xcode_version < '0510': XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault( '$(ARCHS_STANDARD_INCLUDING_64_BIT)', XcodeArchsVariableMapping(['x86_64'], ['x86_64']), XcodeArchsVariableMapping(['i386'], ['i386', 'x86_64']), XcodeArchsVariableMapping( ['armv7', 'armv7s'], ['armv7', 'armv7s', 'arm64'])) else: XCODE_ARCHS_DEFAULT_CACHE = XcodeArchsDefault( '$(ARCHS_STANDARD)', XcodeArchsVariableMapping(['x86_64'], ['x86_64']), XcodeArchsVariableMapping(['i386', 'x86_64'], ['i386', 'x86_64']), XcodeArchsVariableMapping( ['armv7', 'armv7s', 'arm64'], ['armv7', 'armv7s', 'arm64'])) return XCODE_ARCHS_DEFAULT_CACHE class XcodeSettings(object): """A class that understands the gyp 'xcode_settings' object.""" # Populated lazily by _SdkPath(). Shared by all XcodeSettings, so cached # at class-level for efficiency. _sdk_path_cache = {} _sdk_root_cache = {} # Populated lazily by GetExtraPlistItems(). Shared by all XcodeSettings, so # cached at class-level for efficiency. _plist_cache = {} # Populated lazily by GetIOSPostbuilds. Shared by all XcodeSettings, so # cached at class-level for efficiency. _codesigning_key_cache = {} def __init__(self, spec): self.spec = spec self.isIOS = False # Per-target 'xcode_settings' are pushed down into configs earlier by gyp. # This means self.xcode_settings[config] always contains all settings # for that config -- the per-target settings as well. Settings that are # the same for all configs are implicitly per-target settings. self.xcode_settings = {} configs = spec['configurations'] for configname, config in configs.iteritems(): self.xcode_settings[configname] = config.get('xcode_settings', {}) self._ConvertConditionalKeys(configname) if self.xcode_settings[configname].get('IPHONEOS_DEPLOYMENT_TARGET', None): self.isIOS = True # This is only non-None temporarily during the execution of some methods. self.configname = None # Used by _AdjustLibrary to match .a and .dylib entries in libraries. self.library_re = re.compile(r'^lib([^/]+)\.(a|dylib)$') def _ConvertConditionalKeys(self, configname): """Converts or warns on conditional keys. Xcode supports conditional keys, such as CODE_SIGN_IDENTITY[sdk=iphoneos*]. This is a partial implementation with some keys converted while the rest force a warning.""" settings = self.xcode_settings[configname] conditional_keys = [key for key in settings if key.endswith(']')] for key in conditional_keys: # If you need more, speak up at http://crbug.com/122592 if key.endswith("[sdk=iphoneos*]"): if configname.endswith("iphoneos"): new_key = key.split("[")[0] settings[new_key] = settings[key] else: print 'Warning: Conditional keys not implemented, ignoring:', \ ' '.join(conditional_keys) del settings[key] def _Settings(self): assert self.configname return self.xcode_settings[self.configname] def _Test(self, test_key, cond_key, default): return self._Settings().get(test_key, default) == cond_key def _Appendf(self, lst, test_key, format_str, default=None): if test_key in self._Settings(): lst.append(format_str % str(self._Settings()[test_key])) elif default: lst.append(format_str % str(default)) def _WarnUnimplemented(self, test_key): if test_key in self._Settings(): print 'Warning: Ignoring not yet implemented key "%s".' % test_key def IsBinaryOutputFormat(self, configname): default = "binary" if self.isIOS else "xml" format = self.xcode_settings[configname].get('INFOPLIST_OUTPUT_FORMAT', default) return format == "binary" def _IsBundle(self): return int(self.spec.get('mac_bundle', 0)) != 0 def _IsIosAppExtension(self): return int(self.spec.get('ios_app_extension', 0)) != 0 def _IsIosWatchKitExtension(self): return int(self.spec.get('ios_watchkit_extension', 0)) != 0 def _IsIosWatchApp(self): return int(self.spec.get('ios_watch_app', 0)) != 0 def GetFrameworkVersion(self): """Returns the framework version of the current target. Only valid for bundles.""" assert self._IsBundle() return self.GetPerTargetSetting('FRAMEWORK_VERSION', default='A') def GetWrapperExtension(self): """Returns the bundle extension (.app, .framework, .plugin, etc). Only valid for bundles.""" assert self._IsBundle() if self.spec['type'] in ('loadable_module', 'shared_library'): default_wrapper_extension = { 'loadable_module': 'bundle', 'shared_library': 'framework', }[self.spec['type']] wrapper_extension = self.GetPerTargetSetting( 'WRAPPER_EXTENSION', default=default_wrapper_extension) return '.' + self.spec.get('product_extension', wrapper_extension) elif self.spec['type'] == 'executable': if self._IsIosAppExtension() or self._IsIosWatchKitExtension(): return '.' + self.spec.get('product_extension', 'appex') else: return '.' + self.spec.get('product_extension', 'app') else: assert False, "Don't know extension for '%s', target '%s'" % ( self.spec['type'], self.spec['target_name']) def GetProductName(self): """Returns PRODUCT_NAME.""" return self.spec.get('product_name', self.spec['target_name']) def GetFullProductName(self): """Returns FULL_PRODUCT_NAME.""" if self._IsBundle(): return self.GetWrapperName() else: return self._GetStandaloneBinaryPath() def GetWrapperName(self): """Returns the directory name of the bundle represented by this target. Only valid for bundles.""" assert self._IsBundle() return self.GetProductName() + self.GetWrapperExtension() def GetBundleContentsFolderPath(self): """Returns the qualified path to the bundle's contents folder. E.g. Chromium.app/Contents or Foo.bundle/Versions/A. Only valid for bundles.""" if self.isIOS: return self.GetWrapperName() assert self._IsBundle() if self.spec['type'] == 'shared_library': return os.path.join( self.GetWrapperName(), 'Versions', self.GetFrameworkVersion()) else: # loadable_modules have a 'Contents' folder like executables. return os.path.join(self.GetWrapperName(), 'Contents') def GetBundleResourceFolder(self): """Returns the qualified path to the bundle's resource folder. E.g. Chromium.app/Contents/Resources. Only valid for bundles.""" assert self._IsBundle() if self.isIOS: return self.GetBundleContentsFolderPath() return os.path.join(self.GetBundleContentsFolderPath(), 'Resources') def GetBundlePlistPath(self): """Returns the qualified path to the bundle's plist file. E.g. Chromium.app/Contents/Info.plist. Only valid for bundles.""" assert self._IsBundle() if self.spec['type'] in ('executable', 'loadable_module'): return os.path.join(self.GetBundleContentsFolderPath(), 'Info.plist') else: return os.path.join(self.GetBundleContentsFolderPath(), 'Resources', 'Info.plist') def GetProductType(self): """Returns the PRODUCT_TYPE of this target.""" if self._IsIosAppExtension(): assert self._IsBundle(), ('ios_app_extension flag requires mac_bundle ' '(target %s)' % self.spec['target_name']) return 'com.apple.product-type.app-extension' if self._IsIosWatchKitExtension(): assert self._IsBundle(), ('ios_watchkit_extension flag requires ' 'mac_bundle (target %s)' % self.spec['target_name']) return 'com.apple.product-type.watchkit-extension' if self._IsIosWatchApp(): assert self._IsBundle(), ('ios_watch_app flag requires mac_bundle ' '(target %s)' % self.spec['target_name']) return 'com.apple.product-type.application.watchapp' if self._IsBundle(): return { 'executable': 'com.apple.product-type.application', 'loadable_module': 'com.apple.product-type.bundle', 'shared_library': 'com.apple.product-type.framework', }[self.spec['type']] else: return { 'executable': 'com.apple.product-type.tool', 'loadable_module': 'com.apple.product-type.library.dynamic', 'shared_library': 'com.apple.product-type.library.dynamic', 'static_library': 'com.apple.product-type.library.static', }[self.spec['type']] def GetMachOType(self): """Returns the MACH_O_TYPE of this target.""" # Weird, but matches Xcode. if not self._IsBundle() and self.spec['type'] == 'executable': return '' return { 'executable': 'mh_execute', 'static_library': 'staticlib', 'shared_library': 'mh_dylib', 'loadable_module': 'mh_bundle', }[self.spec['type']] def _GetBundleBinaryPath(self): """Returns the name of the bundle binary of by this target. E.g. Chromium.app/Contents/MacOS/Chromium. Only valid for bundles.""" assert self._IsBundle() if self.spec['type'] in ('shared_library') or self.isIOS: path = self.GetBundleContentsFolderPath() elif self.spec['type'] in ('executable', 'loadable_module'): path = os.path.join(self.GetBundleContentsFolderPath(), 'MacOS') return os.path.join(path, self.GetExecutableName()) def _GetStandaloneExecutableSuffix(self): if 'product_extension' in self.spec: return '.' + self.spec['product_extension'] return { 'executable': '', 'static_library': '.a', 'shared_library': '.dylib', 'loadable_module': '.so', }[self.spec['type']] def _GetStandaloneExecutablePrefix(self): return self.spec.get('product_prefix', { 'executable': '', 'static_library': 'lib', 'shared_library': 'lib', # Non-bundled loadable_modules are called foo.so for some reason # (that is, .so and no prefix) with the xcode build -- match that. 'loadable_module': '', }[self.spec['type']]) def _GetStandaloneBinaryPath(self): """Returns the name of the non-bundle binary represented by this target. E.g. hello_world. Only valid for non-bundles.""" assert not self._IsBundle() assert self.spec['type'] in ( 'executable', 'shared_library', 'static_library', 'loadable_module'), ( 'Unexpected type %s' % self.spec['type']) target = self.spec['target_name'] if self.spec['type'] == 'static_library': if target[:3] == 'lib': target = target[3:] elif self.spec['type'] in ('loadable_module', 'shared_library'): if target[:3] == 'lib': target = target[3:] target_prefix = self._GetStandaloneExecutablePrefix() target = self.spec.get('product_name', target) target_ext = self._GetStandaloneExecutableSuffix() return target_prefix + target + target_ext def GetExecutableName(self): """Returns the executable name of the bundle represented by this target. E.g. Chromium.""" if self._IsBundle(): return self.spec.get('product_name', self.spec['target_name']) else: return self._GetStandaloneBinaryPath() def GetExecutablePath(self): """Returns the directory name of the bundle represented by this target. E.g. Chromium.app/Contents/MacOS/Chromium.""" if self._IsBundle(): return self._GetBundleBinaryPath() else: return self._GetStandaloneBinaryPath() def GetActiveArchs(self, configname): """Returns the architectures this target should be built for.""" config_settings = self.xcode_settings[configname] xcode_archs_default = GetXcodeArchsDefault() return xcode_archs_default.ActiveArchs( config_settings.get('ARCHS'), config_settings.get('VALID_ARCHS'), config_settings.get('SDKROOT')) def _GetSdkVersionInfoItem(self, sdk, infoitem): # xcodebuild requires Xcode and can't run on Command Line Tools-only # systems from 10.7 onward. # Since the CLT has no SDK paths anyway, returning None is the # most sensible route and should still do the right thing. try: return GetStdout(['xcodebuild', '-version', '-sdk', sdk, infoitem]) except: pass def _SdkRoot(self, configname): if configname is None: configname = self.configname return self.GetPerConfigSetting('SDKROOT', configname, default='') def _SdkPath(self, configname=None): sdk_root = self._SdkRoot(configname) if sdk_root.startswith('/'): return sdk_root return self._XcodeSdkPath(sdk_root) def _XcodeSdkPath(self, sdk_root): if sdk_root not in XcodeSettings._sdk_path_cache: sdk_path = self._GetSdkVersionInfoItem(sdk_root, 'Path') XcodeSettings._sdk_path_cache[sdk_root] = sdk_path if sdk_root: XcodeSettings._sdk_root_cache[sdk_path] = sdk_root return XcodeSettings._sdk_path_cache[sdk_root] def _AppendPlatformVersionMinFlags(self, lst): self._Appendf(lst, 'MACOSX_DEPLOYMENT_TARGET', '-mmacosx-version-min=%s') if 'IPHONEOS_DEPLOYMENT_TARGET' in self._Settings(): # TODO: Implement this better? sdk_path_basename = os.path.basename(self._SdkPath()) if sdk_path_basename.lower().startswith('iphonesimulator'): self._Appendf(lst, 'IPHONEOS_DEPLOYMENT_TARGET', '-mios-simulator-version-min=%s') else: self._Appendf(lst, 'IPHONEOS_DEPLOYMENT_TARGET', '-miphoneos-version-min=%s') def GetCflags(self, configname, arch=None): """Returns flags that need to be added to .c, .cc, .m, and .mm compilations.""" # This functions (and the similar ones below) do not offer complete # emulation of all xcode_settings keys. They're implemented on demand. self.configname = configname cflags = [] sdk_root = self._SdkPath() if 'SDKROOT' in self._Settings() and sdk_root: cflags.append('-isysroot %s' % sdk_root) if self._Test('CLANG_WARN_CONSTANT_CONVERSION', 'YES', default='NO'): cflags.append('-Wconstant-conversion') if self._Test('GCC_CHAR_IS_UNSIGNED_CHAR', 'YES', default='NO'): cflags.append('-funsigned-char') if self._Test('GCC_CW_ASM_SYNTAX', 'YES', default='YES'): cflags.append('-fasm-blocks') if 'GCC_DYNAMIC_NO_PIC' in self._Settings(): if self._Settings()['GCC_DYNAMIC_NO_PIC'] == 'YES': cflags.append('-mdynamic-no-pic') else: pass # TODO: In this case, it depends on the target. xcode passes # mdynamic-no-pic by default for executable and possibly static lib # according to mento if self._Test('GCC_ENABLE_PASCAL_STRINGS', 'YES', default='YES'): cflags.append('-mpascal-strings') self._Appendf(cflags, 'GCC_OPTIMIZATION_LEVEL', '-O%s', default='s') if self._Test('GCC_GENERATE_DEBUGGING_SYMBOLS', 'YES', default='YES'): dbg_format = self._Settings().get('DEBUG_INFORMATION_FORMAT', 'dwarf') if dbg_format == 'dwarf': cflags.append('-gdwarf-2') elif dbg_format == 'stabs': raise NotImplementedError('stabs debug format is not supported yet.') elif dbg_format == 'dwarf-with-dsym': cflags.append('-gdwarf-2') else: raise NotImplementedError('Unknown debug format %s' % dbg_format) if self._Settings().get('GCC_STRICT_ALIASING') == 'YES': cflags.append('-fstrict-aliasing') elif self._Settings().get('GCC_STRICT_ALIASING') == 'NO': cflags.append('-fno-strict-aliasing') if self._Test('GCC_SYMBOLS_PRIVATE_EXTERN', 'YES', default='NO'): cflags.append('-fvisibility=hidden') if self._Test('GCC_TREAT_WARNINGS_AS_ERRORS', 'YES', default='NO'): cflags.append('-Werror') if self._Test('GCC_WARN_ABOUT_MISSING_NEWLINE', 'YES', default='NO'): cflags.append('-Wnewline-eof') # In Xcode, this is only activated when GCC_COMPILER_VERSION is clang or # llvm-gcc. It also requires a fairly recent libtool, and # if the system clang isn't used, DYLD_LIBRARY_PATH needs to contain the # path to the libLTO.dylib that matches the used clang. if self._Test('LLVM_LTO', 'YES', default='NO'): cflags.append('-flto') self._AppendPlatformVersionMinFlags(cflags) # TODO: if self._Test('COPY_PHASE_STRIP', 'YES', default='NO'): self._WarnUnimplemented('COPY_PHASE_STRIP') self._WarnUnimplemented('GCC_DEBUGGING_SYMBOLS') self._WarnUnimplemented('GCC_ENABLE_OBJC_EXCEPTIONS') # TODO: This is exported correctly, but assigning to it is not supported. self._WarnUnimplemented('MACH_O_TYPE') self._WarnUnimplemented('PRODUCT_TYPE') if arch is not None: archs = [arch] else: assert self.configname archs = self.GetActiveArchs(self.configname) if len(archs) != 1: # TODO: Supporting fat binaries will be annoying. self._WarnUnimplemented('ARCHS') archs = ['i386'] cflags.append('-arch ' + archs[0]) if archs[0] in ('i386', 'x86_64'): if self._Test('GCC_ENABLE_SSE3_EXTENSIONS', 'YES', default='NO'): cflags.append('-msse3') if self._Test('GCC_ENABLE_SUPPLEMENTAL_SSE3_INSTRUCTIONS', 'YES', default='NO'): cflags.append('-mssse3') # Note 3rd 's'. if self._Test('GCC_ENABLE_SSE41_EXTENSIONS', 'YES', default='NO'): cflags.append('-msse4.1') if self._Test('GCC_ENABLE_SSE42_EXTENSIONS', 'YES', default='NO'): cflags.append('-msse4.2') cflags += self._Settings().get('WARNING_CFLAGS', []) if sdk_root: framework_root = sdk_root else: framework_root = '' config = self.spec['configurations'][self.configname] framework_dirs = config.get('mac_framework_dirs', []) for directory in framework_dirs: cflags.append('-F' + directory.replace('$(SDKROOT)', framework_root)) self.configname = None return cflags def GetCflagsC(self, configname): """Returns flags that need to be added to .c, and .m compilations.""" self.configname = configname cflags_c = [] if self._Settings().get('GCC_C_LANGUAGE_STANDARD', '') == 'ansi': cflags_c.append('-ansi') else: self._Appendf(cflags_c, 'GCC_C_LANGUAGE_STANDARD', '-std=%s') cflags_c += self._Settings().get('OTHER_CFLAGS', []) self.configname = None return cflags_c def GetCflagsCC(self, configname): """Returns flags that need to be added to .cc, and .mm compilations.""" self.configname = configname cflags_cc = [] clang_cxx_language_standard = self._Settings().get( 'CLANG_CXX_LANGUAGE_STANDARD') # Note: Don't make c++0x to c++11 so that c++0x can be used with older # clangs that don't understand c++11 yet (like Xcode 4.2's). if clang_cxx_language_standard: cflags_cc.append('-std=%s' % clang_cxx_language_standard) self._Appendf(cflags_cc, 'CLANG_CXX_LIBRARY', '-stdlib=%s') if self._Test('GCC_ENABLE_CPP_RTTI', 'NO', default='YES'): cflags_cc.append('-fno-rtti') if self._Test('GCC_ENABLE_CPP_EXCEPTIONS', 'NO', default='YES'): cflags_cc.append('-fno-exceptions') if self._Test('GCC_INLINES_ARE_PRIVATE_EXTERN', 'YES', default='NO'): cflags_cc.append('-fvisibility-inlines-hidden') if self._Test('GCC_THREADSAFE_STATICS', 'NO', default='YES'): cflags_cc.append('-fno-threadsafe-statics') # Note: This flag is a no-op for clang, it only has an effect for gcc. if self._Test('GCC_WARN_ABOUT_INVALID_OFFSETOF_MACRO', 'NO', default='YES'): cflags_cc.append('-Wno-invalid-offsetof') other_ccflags = [] for flag in self._Settings().get('OTHER_CPLUSPLUSFLAGS', ['$(inherited)']): # TODO: More general variable expansion. Missing in many other places too. if flag in ('$inherited', '$(inherited)', '${inherited}'): flag = '$OTHER_CFLAGS' if flag in ('$OTHER_CFLAGS', '$(OTHER_CFLAGS)', '${OTHER_CFLAGS}'): other_ccflags += self._Settings().get('OTHER_CFLAGS', []) else: other_ccflags.append(flag) cflags_cc += other_ccflags self.configname = None return cflags_cc def _AddObjectiveCGarbageCollectionFlags(self, flags): gc_policy = self._Settings().get('GCC_ENABLE_OBJC_GC', 'unsupported') if gc_policy == 'supported': flags.append('-fobjc-gc') elif gc_policy == 'required': flags.append('-fobjc-gc-only') def _AddObjectiveCARCFlags(self, flags): if self._Test('CLANG_ENABLE_OBJC_ARC', 'YES', default='NO'): flags.append('-fobjc-arc') def _AddObjectiveCMissingPropertySynthesisFlags(self, flags): if self._Test('CLANG_WARN_OBJC_MISSING_PROPERTY_SYNTHESIS', 'YES', default='NO'): flags.append('-Wobjc-missing-property-synthesis') def GetCflagsObjC(self, configname): """Returns flags that need to be added to .m compilations.""" self.configname = configname cflags_objc = [] self._AddObjectiveCGarbageCollectionFlags(cflags_objc) self._AddObjectiveCARCFlags(cflags_objc) self._AddObjectiveCMissingPropertySynthesisFlags(cflags_objc) self.configname = None return cflags_objc def GetCflagsObjCC(self, configname): """Returns flags that need to be added to .mm compilations.""" self.configname = configname cflags_objcc = [] self._AddObjectiveCGarbageCollectionFlags(cflags_objcc) self._AddObjectiveCARCFlags(cflags_objcc) self._AddObjectiveCMissingPropertySynthesisFlags(cflags_objcc) if self._Test('GCC_OBJC_CALL_CXX_CDTORS', 'YES', default='NO'): cflags_objcc.append('-fobjc-call-cxx-cdtors') self.configname = None return cflags_objcc def GetInstallNameBase(self): """Return DYLIB_INSTALL_NAME_BASE for this target.""" # Xcode sets this for shared_libraries, and for nonbundled loadable_modules. if (self.spec['type'] != 'shared_library' and (self.spec['type'] != 'loadable_module' or self._IsBundle())): return None install_base = self.GetPerTargetSetting( 'DYLIB_INSTALL_NAME_BASE', default='/Library/Frameworks' if self._IsBundle() else '/usr/local/lib') return install_base def _StandardizePath(self, path): """Do :standardizepath processing for path.""" # I'm not quite sure what :standardizepath does. Just call normpath(), # but don't let @executable_path/../foo collapse to foo. if '/' in path: prefix, rest = '', path if path.startswith('@'): prefix, rest = path.split('/', 1) rest = os.path.normpath(rest) # :standardizepath path = os.path.join(prefix, rest) return path def GetInstallName(self): """Return LD_DYLIB_INSTALL_NAME for this target.""" # Xcode sets this for shared_libraries, and for nonbundled loadable_modules. if (self.spec['type'] != 'shared_library' and (self.spec['type'] != 'loadable_module' or self._IsBundle())): return None default_install_name = \ '$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)' install_name = self.GetPerTargetSetting( 'LD_DYLIB_INSTALL_NAME', default=default_install_name) # Hardcode support for the variables used in chromium for now, to # unblock people using the make build. if '$' in install_name: assert install_name in ('$(DYLIB_INSTALL_NAME_BASE:standardizepath)/' '$(WRAPPER_NAME)/$(PRODUCT_NAME)', default_install_name), ( 'Variables in LD_DYLIB_INSTALL_NAME are not generally supported ' 'yet in target \'%s\' (got \'%s\')' % (self.spec['target_name'], install_name)) install_name = install_name.replace( '$(DYLIB_INSTALL_NAME_BASE:standardizepath)', self._StandardizePath(self.GetInstallNameBase())) if self._IsBundle(): # These are only valid for bundles, hence the |if|. install_name = install_name.replace( '$(WRAPPER_NAME)', self.GetWrapperName()) install_name = install_name.replace( '$(PRODUCT_NAME)', self.GetProductName()) else: assert '$(WRAPPER_NAME)' not in install_name assert '$(PRODUCT_NAME)' not in install_name install_name = install_name.replace( '$(EXECUTABLE_PATH)', self.GetExecutablePath()) return install_name def _MapLinkerFlagFilename(self, ldflag, gyp_to_build_path): """Checks if ldflag contains a filename and if so remaps it from gyp-directory-relative to build-directory-relative.""" # This list is expanded on demand. # They get matched as: # -exported_symbols_list file # -Wl,exported_symbols_list file # -Wl,exported_symbols_list,file LINKER_FILE = r'(\S+)' WORD = r'\S+' linker_flags = [ ['-exported_symbols_list', LINKER_FILE], # Needed for NaCl. ['-unexported_symbols_list', LINKER_FILE], ['-reexported_symbols_list', LINKER_FILE], ['-sectcreate', WORD, WORD, LINKER_FILE], # Needed for remoting. ] for flag_pattern in linker_flags: regex = re.compile('(?:-Wl,)?' + '[ ,]'.join(flag_pattern)) m = regex.match(ldflag) if m: ldflag = ldflag[:m.start(1)] + gyp_to_build_path(m.group(1)) + \ ldflag[m.end(1):] # Required for ffmpeg (no idea why they don't use LIBRARY_SEARCH_PATHS, # TODO(thakis): Update ffmpeg.gyp): if ldflag.startswith('-L'): ldflag = '-L' + gyp_to_build_path(ldflag[len('-L'):]) return ldflag def GetLdflags(self, configname, product_dir, gyp_to_build_path, arch=None): """Returns flags that need to be passed to the linker. Args: configname: The name of the configuration to get ld flags for. product_dir: The directory where products such static and dynamic libraries are placed. This is added to the library search path. gyp_to_build_path: A function that converts paths relative to the current gyp file to paths relative to the build direcotry. """ self.configname = configname ldflags = [] # The xcode build is relative to a gyp file's directory, and OTHER_LDFLAGS # can contain entries that depend on this. Explicitly absolutify these. for ldflag in self._Settings().get('OTHER_LDFLAGS', []): ldflags.append(self._MapLinkerFlagFilename(ldflag, gyp_to_build_path)) if self._Test('DEAD_CODE_STRIPPING', 'YES', default='NO'): ldflags.append('-Wl,-dead_strip') if self._Test('PREBINDING', 'YES', default='NO'): ldflags.append('-Wl,-prebind') self._Appendf( ldflags, 'DYLIB_COMPATIBILITY_VERSION', '-compatibility_version %s') self._Appendf( ldflags, 'DYLIB_CURRENT_VERSION', '-current_version %s') self._AppendPlatformVersionMinFlags(ldflags) if 'SDKROOT' in self._Settings() and self._SdkPath(): ldflags.append('-isysroot ' + self._SdkPath()) for library_path in self._Settings().get('LIBRARY_SEARCH_PATHS', []): ldflags.append('-L' + gyp_to_build_path(library_path)) if 'ORDER_FILE' in self._Settings(): ldflags.append('-Wl,-order_file ' + '-Wl,' + gyp_to_build_path( self._Settings()['ORDER_FILE'])) if arch is not None: archs = [arch] else: assert self.configname archs = self.GetActiveArchs(self.configname) if len(archs) != 1: # TODO: Supporting fat binaries will be annoying. self._WarnUnimplemented('ARCHS') archs = ['i386'] ldflags.append('-arch ' + archs[0]) # Xcode adds the product directory by default. ldflags.append('-L' + product_dir) install_name = self.GetInstallName() if install_name and self.spec['type'] != 'loadable_module': ldflags.append('-install_name ' + install_name.replace(' ', r'\ ')) for rpath in self._Settings().get('LD_RUNPATH_SEARCH_PATHS', []): ldflags.append('-Wl,-rpath,' + rpath) sdk_root = self._SdkPath() if not sdk_root: sdk_root = '' config = self.spec['configurations'][self.configname] framework_dirs = config.get('mac_framework_dirs', []) for directory in framework_dirs: ldflags.append('-F' + directory.replace('$(SDKROOT)', sdk_root)) is_extension = self._IsIosAppExtension() or self._IsIosWatchKitExtension() if sdk_root and is_extension: # Adds the link flags for extensions. These flags are common for all # extensions and provide loader and main function. # These flags reflect the compilation options used by xcode to compile # extensions. ldflags.append('-lpkstart') if XcodeVersion() < '0900': ldflags.append(sdk_root + '/System/Library/PrivateFrameworks/PlugInKit.framework/PlugInKit') ldflags.append('-fapplication-extension') ldflags.append('-Xlinker -rpath ' '-Xlinker @executable_path/../../Frameworks') self._Appendf(ldflags, 'CLANG_CXX_LIBRARY', '-stdlib=%s') self.configname = None return ldflags def GetLibtoolflags(self, configname): """Returns flags that need to be passed to the static linker. Args: configname: The name of the configuration to get ld flags for. """ self.configname = configname libtoolflags = [] for libtoolflag in self._Settings().get('OTHER_LDFLAGS', []): libtoolflags.append(libtoolflag) # TODO(thakis): ARCHS? self.configname = None return libtoolflags def GetPerTargetSettings(self): """Gets a list of all the per-target settings. This will only fetch keys whose values are the same across all configurations.""" first_pass = True result = {} for configname in sorted(self.xcode_settings.keys()): if first_pass: result = dict(self.xcode_settings[configname]) first_pass = False else: for key, value in self.xcode_settings[configname].iteritems(): if key not in result: continue elif result[key] != value: del result[key] return result def GetPerConfigSetting(self, setting, configname, default=None): if configname in self.xcode_settings: return self.xcode_settings[configname].get(setting, default) else: return self.GetPerTargetSetting(setting, default) def GetPerTargetSetting(self, setting, default=None): """Tries to get xcode_settings.setting from spec. Assumes that the setting has the same value in all configurations and throws otherwise.""" is_first_pass = True result = None for configname in sorted(self.xcode_settings.keys()): if is_first_pass: result = self.xcode_settings[configname].get(setting, None) is_first_pass = False else: assert result == self.xcode_settings[configname].get(setting, None), ( "Expected per-target setting for '%s', got per-config setting " "(target %s)" % (setting, self.spec['target_name'])) if result is None: return default return result def _GetStripPostbuilds(self, configname, output_binary, quiet): """Returns a list of shell commands that contain the shell commands neccessary to strip this target's binary. These should be run as postbuilds before the actual postbuilds run.""" self.configname = configname result = [] if (self._Test('DEPLOYMENT_POSTPROCESSING', 'YES', default='NO') and self._Test('STRIP_INSTALLED_PRODUCT', 'YES', default='NO')): default_strip_style = 'debugging' if self.spec['type'] == 'loadable_module' and self._IsBundle(): default_strip_style = 'non-global' elif self.spec['type'] == 'executable': default_strip_style = 'all' strip_style = self._Settings().get('STRIP_STYLE', default_strip_style) strip_flags = { 'all': '', 'non-global': '-x', 'debugging': '-S', }[strip_style] explicit_strip_flags = self._Settings().get('STRIPFLAGS', '') if explicit_strip_flags: strip_flags += ' ' + _NormalizeEnvVarReferences(explicit_strip_flags) if not quiet: result.append('echo STRIP\\(%s\\)' % self.spec['target_name']) result.append('strip %s %s' % (strip_flags, output_binary)) self.configname = None return result def _GetDebugInfoPostbuilds(self, configname, output, output_binary, quiet): """Returns a list of shell commands that contain the shell commands neccessary to massage this target's debug information. These should be run as postbuilds before the actual postbuilds run.""" self.configname = configname # For static libraries, no dSYMs are created. result = [] if (self._Test('GCC_GENERATE_DEBUGGING_SYMBOLS', 'YES', default='YES') and self._Test( 'DEBUG_INFORMATION_FORMAT', 'dwarf-with-dsym', default='dwarf') and self.spec['type'] != 'static_library'): if not quiet: result.append('echo DSYMUTIL\\(%s\\)' % self.spec['target_name']) result.append('dsymutil %s -o %s' % (output_binary, output + '.dSYM')) self.configname = None return result def _GetTargetPostbuilds(self, configname, output, output_binary, quiet=False): """Returns a list of shell commands that contain the shell commands to run as postbuilds for this target, before the actual postbuilds.""" # dSYMs need to build before stripping happens. return ( self._GetDebugInfoPostbuilds(configname, output, output_binary, quiet) + self._GetStripPostbuilds(configname, output_binary, quiet)) def _GetIOSPostbuilds(self, configname, output_binary): """Return a shell command to codesign the iOS output binary so it can be deployed to a device. This should be run as the very last step of the build.""" if not (self.isIOS and self.spec['type'] == 'executable'): return [] settings = self.xcode_settings[configname] key = self._GetIOSCodeSignIdentityKey(settings) if not key: return [] # Warn for any unimplemented signing xcode keys. unimpl = ['OTHER_CODE_SIGN_FLAGS'] unimpl = set(unimpl) & set(self.xcode_settings[configname].keys()) if unimpl: print 'Warning: Some codesign keys not implemented, ignoring: %s' % ( ', '.join(sorted(unimpl))) return ['%s code-sign-bundle "%s" "%s" "%s" "%s"' % ( os.path.join('${TARGET_BUILD_DIR}', 'gyp-mac-tool'), key, settings.get('CODE_SIGN_RESOURCE_RULES_PATH', ''), settings.get('CODE_SIGN_ENTITLEMENTS', ''), settings.get('PROVISIONING_PROFILE', '')) ] def _GetIOSCodeSignIdentityKey(self, settings): identity = settings.get('CODE_SIGN_IDENTITY') if not identity: return None if identity not in XcodeSettings._codesigning_key_cache: output = subprocess.check_output( ['security', 'find-identity', '-p', 'codesigning', '-v']) for line in output.splitlines(): if identity in line: fingerprint = line.split()[1] cache = XcodeSettings._codesigning_key_cache assert identity not in cache or fingerprint == cache[identity], ( "Multiple codesigning fingerprints for identity: %s" % identity) XcodeSettings._codesigning_key_cache[identity] = fingerprint return XcodeSettings._codesigning_key_cache.get(identity, '') def AddImplicitPostbuilds(self, configname, output, output_binary, postbuilds=[], quiet=False): """Returns a list of shell commands that should run before and after |postbuilds|.""" assert output_binary is not None pre = self._GetTargetPostbuilds(configname, output, output_binary, quiet) post = self._GetIOSPostbuilds(configname, output_binary) return pre + postbuilds + post def _AdjustLibrary(self, library, config_name=None): if library.endswith('.framework'): l = '-framework ' + os.path.splitext(os.path.basename(library))[0] else: m = self.library_re.match(library) if m: l = '-l' + m.group(1) else: l = library sdk_root = self._SdkPath(config_name) if not sdk_root: sdk_root = '' # Xcode 7 started shipping with ".tbd" (text based stubs) files instead of # ".dylib" without providing a real support for them. What it does, for # "/usr/lib" libraries, is do "-L/usr/lib -lname" which is dependent on the # library order and cause collision when building Chrome. # # Instead substitude ".tbd" to ".dylib" in the generated project when the # following conditions are both true: # - library is referenced in the gyp file as "$(SDKROOT)/**/*.dylib", # - the ".dylib" file does not exists but a ".tbd" file do. library = l.replace('$(SDKROOT)', sdk_root) if l.startswith('$(SDKROOT)'): basename, ext = os.path.splitext(library) if ext == '.dylib' and not os.path.exists(library): tbd_library = basename + '.tbd' if os.path.exists(tbd_library): library = tbd_library return library def AdjustLibraries(self, libraries, config_name=None): """Transforms entries like 'Cocoa.framework' in libraries into entries like '-framework Cocoa', 'libcrypto.dylib' into '-lcrypto', etc. """ libraries = [self._AdjustLibrary(library, config_name) for library in libraries] return libraries def _BuildMachineOSBuild(self): return GetStdout(['sw_vers', '-buildVersion']) def _XcodeIOSDeviceFamily(self, configname): family = self.xcode_settings[configname].get('TARGETED_DEVICE_FAMILY', '1') return [int(x) for x in family.split(',')] def GetExtraPlistItems(self, configname=None): """Returns a dictionary with extra items to insert into Info.plist.""" if configname not in XcodeSettings._plist_cache: cache = {} cache['BuildMachineOSBuild'] = self._BuildMachineOSBuild() xcode, xcode_build = XcodeVersion() cache['DTXcode'] = xcode cache['DTXcodeBuild'] = xcode_build sdk_root = self._SdkRoot(configname) if not sdk_root: sdk_root = self._DefaultSdkRoot() cache['DTSDKName'] = sdk_root if xcode >= '0430': cache['DTSDKBuild'] = self._GetSdkVersionInfoItem( sdk_root, 'ProductBuildVersion') else: cache['DTSDKBuild'] = cache['BuildMachineOSBuild'] if self.isIOS: cache['DTPlatformName'] = cache['DTSDKName'] if configname.endswith("iphoneos"): cache['DTPlatformVersion'] = self._GetSdkVersionInfoItem( sdk_root, 'ProductVersion') cache['CFBundleSupportedPlatforms'] = ['iPhoneOS'] else: cache['CFBundleSupportedPlatforms'] = ['iPhoneSimulator'] XcodeSettings._plist_cache[configname] = cache # Include extra plist items that are per-target, not per global # XcodeSettings. items = dict(XcodeSettings._plist_cache[configname]) if self.isIOS: items['UIDeviceFamily'] = self._XcodeIOSDeviceFamily(configname) return items def _DefaultSdkRoot(self): """Returns the default SDKROOT to use. Prior to version 5.0.0, if SDKROOT was not explicitly set in the Xcode project, then the environment variable was empty. Starting with this version, Xcode uses the name of the newest SDK installed. """ xcode_version, xcode_build = XcodeVersion() if xcode_version < '0500': return '' default_sdk_path = self._XcodeSdkPath('') default_sdk_root = XcodeSettings._sdk_root_cache.get(default_sdk_path) if default_sdk_root: return default_sdk_root try: all_sdks = GetStdout(['xcodebuild', '-showsdks']) except: # If xcodebuild fails, there will be no valid SDKs return '' for line in all_sdks.splitlines(): items = line.split() if len(items) >= 3 and items[-2] == '-sdk': sdk_root = items[-1] sdk_path = self._XcodeSdkPath(sdk_root) if sdk_path == default_sdk_path: return sdk_root return '' class MacPrefixHeader(object): """A class that helps with emulating Xcode's GCC_PREFIX_HEADER feature. This feature consists of several pieces: * If GCC_PREFIX_HEADER is present, all compilations in that project get an additional |-include path_to_prefix_header| cflag. * If GCC_PRECOMPILE_PREFIX_HEADER is present too, then the prefix header is instead compiled, and all other compilations in the project get an additional |-include path_to_compiled_header| instead. + Compiled prefix headers have the extension gch. There is one gch file for every language used in the project (c, cc, m, mm), since gch files for different languages aren't compatible. + gch files themselves are built with the target's normal cflags, but they obviously don't get the |-include| flag. Instead, they need a -x flag that describes their language. + All o files in the target need to depend on the gch file, to make sure it's built before any o file is built. This class helps with some of these tasks, but it needs help from the build system for writing dependencies to the gch files, for writing build commands for the gch files, and for figuring out the location of the gch files. """ def __init__(self, xcode_settings, gyp_path_to_build_path, gyp_path_to_build_output): """If xcode_settings is None, all methods on this class are no-ops. Args: gyp_path_to_build_path: A function that takes a gyp-relative path, and returns a path relative to the build directory. gyp_path_to_build_output: A function that takes a gyp-relative path and a language code ('c', 'cc', 'm', or 'mm'), and that returns a path to where the output of precompiling that path for that language should be placed (without the trailing '.gch'). """ # This doesn't support per-configuration prefix headers. Good enough # for now. self.header = None self.compile_headers = False if xcode_settings: self.header = xcode_settings.GetPerTargetSetting('GCC_PREFIX_HEADER') self.compile_headers = xcode_settings.GetPerTargetSetting( 'GCC_PRECOMPILE_PREFIX_HEADER', default='NO') != 'NO' self.compiled_headers = {} if self.header: if self.compile_headers: for lang in ['c', 'cc', 'm', 'mm']: self.compiled_headers[lang] = gyp_path_to_build_output( self.header, lang) self.header = gyp_path_to_build_path(self.header) def _CompiledHeader(self, lang, arch): assert self.compile_headers h = self.compiled_headers[lang] if arch: h += '.' + arch return h def GetInclude(self, lang, arch=None): """Gets the cflags to include the prefix header for language |lang|.""" if self.compile_headers and lang in self.compiled_headers: return '-include %s' % self._CompiledHeader(lang, arch) elif self.header: return '-include %s' % self.header else: return '' def _Gch(self, lang, arch): """Returns the actual file name of the prefix header for language |lang|.""" assert self.compile_headers return self._CompiledHeader(lang, arch) + '.gch' def GetObjDependencies(self, sources, objs, arch=None): """Given a list of source files and the corresponding object files, returns a list of (source, object, gch) tuples, where |gch| is the build-directory relative path to the gch file each object file depends on. |compilable[i]| has to be the source file belonging to |objs[i]|.""" if not self.header or not self.compile_headers: return [] result = [] for source, obj in zip(sources, objs): ext = os.path.splitext(source)[1] lang = { '.c': 'c', '.cpp': 'cc', '.cc': 'cc', '.cxx': 'cc', '.m': 'm', '.mm': 'mm', }.get(ext, None) if lang: result.append((source, obj, self._Gch(lang, arch))) return result def GetPchBuildCommands(self, arch=None): """Returns [(path_to_gch, language_flag, language, header)]. |path_to_gch| and |header| are relative to the build directory. """ if not self.header or not self.compile_headers: return [] return [ (self._Gch('c', arch), '-x c-header', 'c', self.header), (self._Gch('cc', arch), '-x c++-header', 'cc', self.header), (self._Gch('m', arch), '-x objective-c-header', 'm', self.header), (self._Gch('mm', arch), '-x objective-c++-header', 'mm', self.header), ] def XcodeVersion(): """Returns a tuple of version and build version of installed Xcode.""" # `xcodebuild -version` output looks like # Xcode 4.6.3 # Build version 4H1503 # or like # Xcode 3.2.6 # Component versions: DevToolsCore-1809.0; DevToolsSupport-1806.0 # BuildVersion: 10M2518 # Convert that to '0463', '4H1503'. global XCODE_VERSION_CACHE if XCODE_VERSION_CACHE: return XCODE_VERSION_CACHE try: version_list = GetStdout(['xcodebuild', '-version']).splitlines() # In some circumstances xcodebuild exits 0 but doesn't return # the right results; for example, a user on 10.7 or 10.8 with # a bogus path set via xcode-select # In that case this may be a CLT-only install so fall back to # checking that version. if len(version_list) < 2: raise GypError("xcodebuild returned unexpected results") except: version = CLTVersion() if version: version = re.match(r'(\d\.\d\.?\d*)', version).groups()[0] else: raise GypError("No Xcode or CLT version detected!") # The CLT has no build information, so we return an empty string. version_list = [version, ''] version = version_list[0] build = version_list[-1] # Be careful to convert "4.2" to "0420": version = version.split()[-1].replace('.', '') version = (version + '0' * (3 - len(version))).zfill(4) if build: build = build.split()[-1] XCODE_VERSION_CACHE = (version, build) return XCODE_VERSION_CACHE # This function ported from the logic in Homebrew's CLT version check def CLTVersion(): """Returns the version of command-line tools from pkgutil.""" # pkgutil output looks like # package-id: com.apple.pkg.CLTools_Executables # version: 5.0.1.0.1.1382131676 # volume: / # location: / # install-time: 1382544035 # groups: com.apple.FindSystemFiles.pkg-group com.apple.DevToolsBoth.pkg-group com.apple.DevToolsNonRelocatableShared.pkg-group STANDALONE_PKG_ID = "com.apple.pkg.DeveloperToolsCLILeo" FROM_XCODE_PKG_ID = "com.apple.pkg.DeveloperToolsCLI" MAVERICKS_PKG_ID = "com.apple.pkg.CLTools_Executables" regex = re.compile('version: (?P<version>.+)') for key in [MAVERICKS_PKG_ID, STANDALONE_PKG_ID, FROM_XCODE_PKG_ID]: try: output = GetStdout(['/usr/sbin/pkgutil', '--pkg-info', key]) return re.search(regex, output).groupdict()['version'] except: continue def GetStdout(cmdlist): """Returns the content of standard output returned by invoking |cmdlist|. Raises |GypError| if the command return with a non-zero return code.""" job = subprocess.Popen(cmdlist, stdout=subprocess.PIPE) out = job.communicate()[0] if job.returncode != 0: sys.stderr.write(out + '\n') raise GypError('Error %d running %s' % (job.returncode, cmdlist[0])) return out.rstrip('\n') def MergeGlobalXcodeSettingsToSpec(global_dict, spec): """Merges the global xcode_settings dictionary into each configuration of the target represented by spec. For keys that are both in the global and the local xcode_settings dict, the local key gets precendence. """ # The xcode generator special-cases global xcode_settings and does something # that amounts to merging in the global xcode_settings into each local # xcode_settings dict. global_xcode_settings = global_dict.get('xcode_settings', {}) for config in spec['configurations'].values(): if 'xcode_settings' in config: new_settings = global_xcode_settings.copy() new_settings.update(config['xcode_settings']) config['xcode_settings'] = new_settings def IsMacBundle(flavor, spec): """Returns if |spec| should be treated as a bundle. Bundles are directories with a certain subdirectory structure, instead of just a single file. Bundle rules do not produce a binary but also package resources into that directory.""" is_mac_bundle = (int(spec.get('mac_bundle', 0)) != 0 and flavor == 'mac') if is_mac_bundle: assert spec['type'] != 'none', ( 'mac_bundle targets cannot have type none (target "%s")' % spec['target_name']) return is_mac_bundle def GetMacBundleResources(product_dir, xcode_settings, resources): """Yields (output, resource) pairs for every resource in |resources|. Only call this for mac bundle targets. Args: product_dir: Path to the directory containing the output bundle, relative to the build directory. xcode_settings: The XcodeSettings of the current target. resources: A list of bundle resources, relative to the build directory. """ dest = os.path.join(product_dir, xcode_settings.GetBundleResourceFolder()) for res in resources: output = dest # The make generator doesn't support it, so forbid it everywhere # to keep the generators more interchangable. assert ' ' not in res, ( "Spaces in resource filenames not supported (%s)" % res) # Split into (path,file). res_parts = os.path.split(res) # Now split the path into (prefix,maybe.lproj). lproj_parts = os.path.split(res_parts[0]) # If the resource lives in a .lproj bundle, add that to the destination. if lproj_parts[1].endswith('.lproj'): output = os.path.join(output, lproj_parts[1]) output = os.path.join(output, res_parts[1]) # Compiled XIB files are referred to by .nib. if output.endswith('.xib'): output = os.path.splitext(output)[0] + '.nib' # Compiled storyboard files are referred to by .storyboardc. if output.endswith('.storyboard'): output = os.path.splitext(output)[0] + '.storyboardc' yield output, res def GetMacInfoPlist(product_dir, xcode_settings, gyp_path_to_build_path): """Returns (info_plist, dest_plist, defines, extra_env), where: * |info_plist| is the source plist path, relative to the build directory, * |dest_plist| is the destination plist path, relative to the build directory, * |defines| is a list of preprocessor defines (empty if the plist shouldn't be preprocessed, * |extra_env| is a dict of env variables that should be exported when invoking |mac_tool copy-info-plist|. Only call this for mac bundle targets. Args: product_dir: Path to the directory containing the output bundle, relative to the build directory. xcode_settings: The XcodeSettings of the current target. gyp_to_build_path: A function that converts paths relative to the current gyp file to paths relative to the build direcotry. """ info_plist = xcode_settings.GetPerTargetSetting('INFOPLIST_FILE') if not info_plist: return None, None, [], {} # The make generator doesn't support it, so forbid it everywhere # to keep the generators more interchangable. assert ' ' not in info_plist, ( "Spaces in Info.plist filenames not supported (%s)" % info_plist) info_plist = gyp_path_to_build_path(info_plist) # If explicitly set to preprocess the plist, invoke the C preprocessor and # specify any defines as -D flags. if xcode_settings.GetPerTargetSetting( 'INFOPLIST_PREPROCESS', default='NO') == 'YES': # Create an intermediate file based on the path. defines = shlex.split(xcode_settings.GetPerTargetSetting( 'INFOPLIST_PREPROCESSOR_DEFINITIONS', default='')) else: defines = [] dest_plist = os.path.join(product_dir, xcode_settings.GetBundlePlistPath()) extra_env = xcode_settings.GetPerTargetSettings() return info_plist, dest_plist, defines, extra_env def _GetXcodeEnv(xcode_settings, built_products_dir, srcroot, configuration, additional_settings=None): """Return the environment variables that Xcode would set. See http://developer.apple.com/library/mac/#documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW153 for a full list. Args: xcode_settings: An XcodeSettings object. If this is None, this function returns an empty dict. built_products_dir: Absolute path to the built products dir. srcroot: Absolute path to the source root. configuration: The build configuration name. additional_settings: An optional dict with more values to add to the result. """ if not xcode_settings: return {} # This function is considered a friend of XcodeSettings, so let it reach into # its implementation details. spec = xcode_settings.spec # These are filled in on a as-needed basis. env = { 'BUILT_FRAMEWORKS_DIR' : built_products_dir, 'BUILT_PRODUCTS_DIR' : built_products_dir, 'CONFIGURATION' : configuration, 'PRODUCT_NAME' : xcode_settings.GetProductName(), # See /Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/MacOSX\ Product\ Types.xcspec for FULL_PRODUCT_NAME 'SRCROOT' : srcroot, 'SOURCE_ROOT': '${SRCROOT}', # This is not true for static libraries, but currently the env is only # written for bundles: 'TARGET_BUILD_DIR' : built_products_dir, 'TEMP_DIR' : '${TMPDIR}', } if xcode_settings.GetPerConfigSetting('SDKROOT', configuration): env['SDKROOT'] = xcode_settings._SdkPath(configuration) else: env['SDKROOT'] = '' if spec['type'] in ( 'executable', 'static_library', 'shared_library', 'loadable_module'): env['EXECUTABLE_NAME'] = xcode_settings.GetExecutableName() env['EXECUTABLE_PATH'] = xcode_settings.GetExecutablePath() env['FULL_PRODUCT_NAME'] = xcode_settings.GetFullProductName() mach_o_type = xcode_settings.GetMachOType() if mach_o_type: env['MACH_O_TYPE'] = mach_o_type env['PRODUCT_TYPE'] = xcode_settings.GetProductType() if xcode_settings._IsBundle(): env['CONTENTS_FOLDER_PATH'] = \ xcode_settings.GetBundleContentsFolderPath() env['UNLOCALIZED_RESOURCES_FOLDER_PATH'] = \ xcode_settings.GetBundleResourceFolder() env['INFOPLIST_PATH'] = xcode_settings.GetBundlePlistPath() env['WRAPPER_NAME'] = xcode_settings.GetWrapperName() install_name = xcode_settings.GetInstallName() if install_name: env['LD_DYLIB_INSTALL_NAME'] = install_name install_name_base = xcode_settings.GetInstallNameBase() if install_name_base: env['DYLIB_INSTALL_NAME_BASE'] = install_name_base if XcodeVersion() >= '0500' and not env.get('SDKROOT'): sdk_root = xcode_settings._SdkRoot(configuration) if not sdk_root: sdk_root = xcode_settings._XcodeSdkPath('') if sdk_root is None: sdk_root = '' env['SDKROOT'] = sdk_root if not additional_settings: additional_settings = {} else: # Flatten lists to strings. for k in additional_settings: if not isinstance(additional_settings[k], str): additional_settings[k] = ' '.join(additional_settings[k]) additional_settings.update(env) for k in additional_settings: additional_settings[k] = _NormalizeEnvVarReferences(additional_settings[k]) return additional_settings def _NormalizeEnvVarReferences(str): """Takes a string containing variable references in the form ${FOO}, $(FOO), or $FOO, and returns a string with all variable references in the form ${FOO}. """ # $FOO -> ${FOO} str = re.sub(r'\$([a-zA-Z_][a-zA-Z0-9_]*)', r'${\1}', str) # $(FOO) -> ${FOO} matches = re.findall(r'(\$\(([a-zA-Z0-9\-_]+)\))', str) for match in matches: to_replace, variable = match assert '$(' not in match, '$($(FOO)) variables not supported: ' + match str = str.replace(to_replace, '${' + variable + '}') return str def ExpandEnvVars(string, expansions): """Expands ${VARIABLES}, $(VARIABLES), and $VARIABLES in string per the expansions list. If the variable expands to something that references another variable, this variable is expanded as well if it's in env -- until no variables present in env are left.""" for k, v in reversed(expansions): string = string.replace('${' + k + '}', v) string = string.replace('$(' + k + ')', v) string = string.replace('$' + k, v) return string def _TopologicallySortedEnvVarKeys(env): """Takes a dict |env| whose values are strings that can refer to other keys, for example env['foo'] = '$(bar) and $(baz)'. Returns a list L of all keys of env such that key2 is after key1 in L if env[key2] refers to env[key1]. Throws an Exception in case of dependency cycles. """ # Since environment variables can refer to other variables, the evaluation # order is important. Below is the logic to compute the dependency graph # and sort it. regex = re.compile(r'\$\{([a-zA-Z0-9\-_]+)\}') def GetEdges(node): # Use a definition of edges such that user_of_variable -> used_varible. # This happens to be easier in this case, since a variable's # definition contains all variables it references in a single string. # We can then reverse the result of the topological sort at the end. # Since: reverse(topsort(DAG)) = topsort(reverse_edges(DAG)) matches = set([v for v in regex.findall(env[node]) if v in env]) for dependee in matches: assert '${' not in dependee, 'Nested variables not supported: ' + dependee return matches try: # Topologically sort, and then reverse, because we used an edge definition # that's inverted from the expected result of this function (see comment # above). order = gyp.common.TopologicallySorted(env.keys(), GetEdges) order.reverse() return order except gyp.common.CycleError, e: raise GypError( 'Xcode environment variables are cyclically dependent: ' + str(e.nodes)) def GetSortedXcodeEnv(xcode_settings, built_products_dir, srcroot, configuration, additional_settings=None): env = _GetXcodeEnv(xcode_settings, built_products_dir, srcroot, configuration, additional_settings) return [(key, env[key]) for key in _TopologicallySortedEnvVarKeys(env)] def GetSpecPostbuildCommands(spec, quiet=False): """Returns the list of postbuilds explicitly defined on |spec|, in a form executable by a shell.""" postbuilds = [] for postbuild in spec.get('postbuilds', []): if not quiet: postbuilds.append('echo POSTBUILD\\(%s\\) %s' % ( spec['target_name'], postbuild['postbuild_name'])) postbuilds.append(gyp.common.EncodePOSIXShellList(postbuild['action'])) return postbuilds def _HasIOSTarget(targets): """Returns true if any target contains the iOS specific key IPHONEOS_DEPLOYMENT_TARGET.""" for target_dict in targets.values(): for config in target_dict['configurations'].values(): if config.get('xcode_settings', {}).get('IPHONEOS_DEPLOYMENT_TARGET'): return True return False def _AddIOSDeviceConfigurations(targets): """Clone all targets and append -iphoneos to the name. Configure these targets to build for iOS devices and use correct architectures for those builds.""" for target_dict in targets.itervalues(): toolset = target_dict['toolset'] configs = target_dict['configurations'] for config_name, config_dict in dict(configs).iteritems(): iphoneos_config_dict = copy.deepcopy(config_dict) configs[config_name + '-iphoneos'] = iphoneos_config_dict configs[config_name + '-iphonesimulator'] = config_dict if toolset == 'target': iphoneos_config_dict['xcode_settings']['SDKROOT'] = 'iphoneos' return targets def CloneConfigurationForDeviceAndEmulator(target_dicts): """If |target_dicts| contains any iOS targets, automatically create -iphoneos targets for iOS device builds.""" if _HasIOSTarget(target_dicts): return _AddIOSDeviceConfigurations(target_dicts) return target_dicts
gpl-2.0
orion1024/Sick-Beard
lib/simplejson/__init__.py
448
12463
r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data interchange format. :mod:`simplejson` exposes an API familiar to users of the standard library :mod:`marshal` and :mod:`pickle` modules. It is the externally maintained version of the :mod:`json` library contained in Python 2.6, but maintains compatibility with Python 2.4 and Python 2.5 and (currently) has significant performance advantages, even without using the optional C extension for speedups. Encoding basic Python object hierarchies:: >>> import simplejson as json >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) '["foo", {"bar": ["baz", null, 1.0, 2]}]' >>> print json.dumps("\"foo\bar") "\"foo\bar" >>> print json.dumps(u'\u1234') "\u1234" >>> print json.dumps('\\') "\\" >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) {"a": 0, "b": 0, "c": 0} >>> from StringIO import StringIO >>> io = StringIO() >>> json.dump(['streaming API'], io) >>> io.getvalue() '["streaming API"]' Compact encoding:: >>> import simplejson as json >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) '[1,2,3,{"4":5,"6":7}]' Pretty printing:: >>> import simplejson as json >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) >>> print '\n'.join([l.rstrip() for l in s.splitlines()]) { "4": 5, "6": 7 } Decoding JSON:: >>> import simplejson as json >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj True >>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar' True >>> from StringIO import StringIO >>> io = StringIO('["streaming API"]') >>> json.load(io)[0] == 'streaming API' True Specializing JSON object decoding:: >>> import simplejson as json >>> def as_complex(dct): ... if '__complex__' in dct: ... return complex(dct['real'], dct['imag']) ... return dct ... >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}', ... object_hook=as_complex) (1+2j) >>> import decimal >>> json.loads('1.1', parse_float=decimal.Decimal) == decimal.Decimal('1.1') True Specializing JSON object encoding:: >>> import simplejson as json >>> def encode_complex(obj): ... if isinstance(obj, complex): ... return [obj.real, obj.imag] ... raise TypeError(repr(o) + " is not JSON serializable") ... >>> json.dumps(2 + 1j, default=encode_complex) '[2.0, 1.0]' >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j) '[2.0, 1.0]' >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j)) '[2.0, 1.0]' Using simplejson.tool from the shell to validate and pretty-print:: $ echo '{"json":"obj"}' | python -m simplejson.tool { "json": "obj" } $ echo '{ 1.2:3.4}' | python -m simplejson.tool Expecting property name: line 1 column 2 (char 2) """ __version__ = '2.0.9' __all__ = [ 'dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONEncoder', ] __author__ = 'Bob Ippolito <bob@redivi.com>' from decoder import JSONDecoder from encoder import JSONEncoder _default_encoder = JSONEncoder( skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None, encoding='utf-8', default=None, ) def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None, **kw): """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a ``.write()``-supporting file-like object). If ``skipkeys`` is true then ``dict`` keys that are not basic types (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) will be skipped instead of raising a ``TypeError``. If ``ensure_ascii`` is false, then the some chunks written to ``fp`` may be ``unicode`` instances, subject to normal Python ``str`` to ``unicode`` coercion rules. Unless ``fp.write()`` explicitly understands ``unicode`` (as in ``codecs.getwriter()``) this is likely to cause an error. If ``check_circular`` is false, then the circular reference check for container types will be skipped and a circular reference will result in an ``OverflowError`` (or worse). If ``allow_nan`` is false, then it will be a ``ValueError`` to serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). If ``indent`` is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. ``None`` is the most compact representation. If ``separators`` is an ``(item_separator, dict_separator)`` tuple then it will be used instead of the default ``(', ', ': ')`` separators. ``(',', ':')`` is the most compact JSON representation. ``encoding`` is the character encoding for str instances, default is UTF-8. ``default(obj)`` is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with the ``cls`` kwarg. """ # cached encoder if (not skipkeys and ensure_ascii and check_circular and allow_nan and cls is None and indent is None and separators is None and encoding == 'utf-8' and default is None and not kw): iterable = _default_encoder.iterencode(obj) else: if cls is None: cls = JSONEncoder iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular, allow_nan=allow_nan, indent=indent, separators=separators, encoding=encoding, default=default, **kw).iterencode(obj) # could accelerate with writelines in some versions of Python, at # a debuggability cost for chunk in iterable: fp.write(chunk) def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None, **kw): """Serialize ``obj`` to a JSON formatted ``str``. If ``skipkeys`` is false then ``dict`` keys that are not basic types (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) will be skipped instead of raising a ``TypeError``. If ``ensure_ascii`` is false, then the return value will be a ``unicode`` instance subject to normal Python ``str`` to ``unicode`` coercion rules instead of being escaped to an ASCII ``str``. If ``check_circular`` is false, then the circular reference check for container types will be skipped and a circular reference will result in an ``OverflowError`` (or worse). If ``allow_nan`` is false, then it will be a ``ValueError`` to serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). If ``indent`` is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. ``None`` is the most compact representation. If ``separators`` is an ``(item_separator, dict_separator)`` tuple then it will be used instead of the default ``(', ', ': ')`` separators. ``(',', ':')`` is the most compact JSON representation. ``encoding`` is the character encoding for str instances, default is UTF-8. ``default(obj)`` is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with the ``cls`` kwarg. """ # cached encoder if (not skipkeys and ensure_ascii and check_circular and allow_nan and cls is None and indent is None and separators is None and encoding == 'utf-8' and default is None and not kw): return _default_encoder.encode(obj) if cls is None: cls = JSONEncoder return cls( skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular, allow_nan=allow_nan, indent=indent, separators=separators, encoding=encoding, default=default, **kw).encode(obj) _default_decoder = JSONDecoder(encoding=None, object_hook=None) def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, **kw): """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing a JSON document) to a Python object. If the contents of ``fp`` is encoded with an ASCII based encoding other than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed, and should be wrapped with ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode`` object and passed to ``loads()`` ``object_hook`` is an optional function that will be called with the result of any object literal decode (a ``dict``). The return value of ``object_hook`` will be used instead of the ``dict``. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting). To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` kwarg. """ return loads(fp.read(), encoding=encoding, cls=cls, object_hook=object_hook, parse_float=parse_float, parse_int=parse_int, parse_constant=parse_constant, **kw) def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, **kw): """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON document) to a Python object. If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded to ``unicode`` first. ``object_hook`` is an optional function that will be called with the result of any object literal decode (a ``dict``). The return value of ``object_hook`` will be used instead of the ``dict``. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting). ``parse_float``, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal). ``parse_int``, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float). ``parse_constant``, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN, null, true, false. This can be used to raise an exception if invalid JSON numbers are encountered. To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` kwarg. """ if (cls is None and encoding is None and object_hook is None and parse_int is None and parse_float is None and parse_constant is None and not kw): return _default_decoder.decode(s) if cls is None: cls = JSONDecoder if object_hook is not None: kw['object_hook'] = object_hook if parse_float is not None: kw['parse_float'] = parse_float if parse_int is not None: kw['parse_int'] = parse_int if parse_constant is not None: kw['parse_constant'] = parse_constant return cls(encoding=encoding, **kw).decode(s)
gpl-3.0
shaneknapp/spark
examples/src/main/python/ml/gradient_boosted_tree_regressor_example.py
27
2654
# # 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. # """ Gradient Boosted Tree Regressor Example. """ # $example on$ from pyspark.ml import Pipeline from pyspark.ml.regression import GBTRegressor from pyspark.ml.feature import VectorIndexer from pyspark.ml.evaluation import RegressionEvaluator # $example off$ from pyspark.sql import SparkSession if __name__ == "__main__": spark = SparkSession\ .builder\ .appName("GradientBoostedTreeRegressorExample")\ .getOrCreate() # $example on$ # Load and parse the data file, converting it to a DataFrame. data = spark.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt") # Automatically identify categorical features, and index them. # Set maxCategories so features with > 4 distinct values are treated as continuous. featureIndexer =\ VectorIndexer(inputCol="features", outputCol="indexedFeatures", maxCategories=4).fit(data) # Split the data into training and test sets (30% held out for testing) (trainingData, testData) = data.randomSplit([0.7, 0.3]) # Train a GBT model. gbt = GBTRegressor(featuresCol="indexedFeatures", maxIter=10) # Chain indexer and GBT in a Pipeline pipeline = Pipeline(stages=[featureIndexer, gbt]) # Train model. This also runs the indexer. model = pipeline.fit(trainingData) # Make predictions. predictions = model.transform(testData) # Select example rows to display. predictions.select("prediction", "label", "features").show(5) # Select (prediction, true label) and compute test error evaluator = RegressionEvaluator( labelCol="label", predictionCol="prediction", metricName="rmse") rmse = evaluator.evaluate(predictions) print("Root Mean Squared Error (RMSE) on test data = %g" % rmse) gbtModel = model.stages[1] print(gbtModel) # summary only # $example off$ spark.stop()
apache-2.0
remap/ndn-flow
framework/ndn_iot_js/.waf-1.7.9-ce9b2a6d663b32d9a89df143dad45f99/waflib/Tools/gcc.py
64
2730
#! /usr/bin/env python # encoding: utf-8 # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file import os,sys from waflib import Configure,Options,Utils from waflib.Tools import ccroot,ar from waflib.Configure import conf @conf def find_gcc(conf): cc=conf.find_program(['gcc','cc'],var='CC') cc=conf.cmd_to_list(cc) conf.get_cc_version(cc,gcc=True) conf.env.CC_NAME='gcc' conf.env.CC=cc @conf def gcc_common_flags(conf): v=conf.env v['CC_SRC_F']=[] v['CC_TGT_F']=['-c','-o'] if not v['LINK_CC']:v['LINK_CC']=v['CC'] v['CCLNK_SRC_F']=[] v['CCLNK_TGT_F']=['-o'] v['CPPPATH_ST']='-I%s' v['DEFINES_ST']='-D%s' v['LIB_ST']='-l%s' v['LIBPATH_ST']='-L%s' v['STLIB_ST']='-l%s' v['STLIBPATH_ST']='-L%s' v['RPATH_ST']='-Wl,-rpath,%s' v['SONAME_ST']='-Wl,-h,%s' v['SHLIB_MARKER']='-Wl,-Bdynamic' v['STLIB_MARKER']='-Wl,-Bstatic' v['cprogram_PATTERN']='%s' v['CFLAGS_cshlib']=['-fPIC'] v['LINKFLAGS_cshlib']=['-shared'] v['cshlib_PATTERN']='lib%s.so' v['LINKFLAGS_cstlib']=['-Wl,-Bstatic'] v['cstlib_PATTERN']='lib%s.a' v['LINKFLAGS_MACBUNDLE']=['-bundle','-undefined','dynamic_lookup'] v['CFLAGS_MACBUNDLE']=['-fPIC'] v['macbundle_PATTERN']='%s.bundle' @conf def gcc_modifier_win32(conf): v=conf.env v['cprogram_PATTERN']='%s.exe' v['cshlib_PATTERN']='%s.dll' v['implib_PATTERN']='lib%s.dll.a' v['IMPLIB_ST']='-Wl,--out-implib,%s' v['CFLAGS_cshlib']=[] v.append_value('LINKFLAGS',['-Wl,--enable-auto-import']) @conf def gcc_modifier_cygwin(conf): gcc_modifier_win32(conf) v=conf.env v['cshlib_PATTERN']='cyg%s.dll' v.append_value('LINKFLAGS_cshlib',['-Wl,--enable-auto-image-base']) v['CFLAGS_cshlib']=[] @conf def gcc_modifier_darwin(conf): v=conf.env v['CFLAGS_cshlib']=['-fPIC','-compatibility_version','1','-current_version','1'] v['LINKFLAGS_cshlib']=['-dynamiclib'] v['cshlib_PATTERN']='lib%s.dylib' v['FRAMEWORKPATH_ST']='-F%s' v['FRAMEWORK_ST']=['-framework'] v['ARCH_ST']=['-arch'] v['LINKFLAGS_cstlib']=[] v['SHLIB_MARKER']=[] v['STLIB_MARKER']=[] v['SONAME_ST']=[] @conf def gcc_modifier_aix(conf): v=conf.env v['LINKFLAGS_cprogram']=['-Wl,-brtl'] v['LINKFLAGS_cshlib']=['-shared','-Wl,-brtl,-bexpfull'] v['SHLIB_MARKER']=[] @conf def gcc_modifier_hpux(conf): v=conf.env v['SHLIB_MARKER']=[] v['STLIB_MARKER']='-Bstatic' v['CFLAGS_cshlib']=['-fPIC','-DPIC'] v['cshlib_PATTERN']='lib%s.sl' @conf def gcc_modifier_platform(conf): gcc_modifier_func=getattr(conf,'gcc_modifier_'+conf.env.DEST_OS,None) if gcc_modifier_func: gcc_modifier_func() def configure(conf): conf.find_gcc() conf.find_ar() conf.gcc_common_flags() conf.gcc_modifier_platform() conf.cc_load_tools() conf.cc_add_flags() conf.link_add_flags()
lgpl-3.0
soniyj/basement
src/Cocos2d-x/MyGame1/cocos2d/plugin/tools/toolsForGame/modifyProject.py
255
1300
import sys, string, os from xml.etree import ElementTree as ET from xml.dom import minidom projFile = sys.argv[1] targetPath = sys.argv[2] def getLinkElement(): global targetPath ret = ET.Element('link') nameEle = ET.Element('name') nameEle.text = 'plugin-x' typeEle = ET.Element('type') typeEle.text = '2' locEle = ET.Element('locationURI') locEle.text = targetPath ret.append(nameEle) ret.append(typeEle) ret.append(locEle) return ret tree = ET.parse(projFile) root = tree.getroot() nodeLinkRes = root.find('linkedResources') if nodeLinkRes != None: linkNodes = nodeLinkRes.findall('link') haveTarget = False if linkNodes != None and len(linkNodes) > 0: for node in linkNodes: locNode = node.find('locationURI') if locNode == None: continue tempText = locNode.text tempText = tempText.strip(' \n\r\t') if tempText == targetPath: haveTarget = True break if not haveTarget: nodeLinkRes.append(getLinkElement()) tree.write(projFile, 'UTF-8') else: linkResEle = ET.Element('linkedResources') linkResEle.append(getLinkElement()) root.append(linkResEle) tree.write(projFile, 'UTF-8')
gpl-2.0
vlachoudis/sl4a
python/src/Lib/test/test_strftime.py
56
6967
""" Unittest for time.strftime """ import calendar import sys import os import re from test import test_support import time import unittest # helper functions def fixasctime(s): if s[8] == ' ': s = s[:8] + '0' + s[9:] return s def escapestr(text, ampm): """ Escape text to deal with possible locale values that have regex syntax while allowing regex syntax used for comparison. """ new_text = re.escape(text) new_text = new_text.replace(re.escape(ampm), ampm) new_text = new_text.replace('\%', '%') new_text = new_text.replace('\:', ':') new_text = new_text.replace('\?', '?') return new_text class StrftimeTest(unittest.TestCase): def __init__(self, *k, **kw): unittest.TestCase.__init__(self, *k, **kw) def _update_variables(self, now): # we must update the local variables on every cycle self.gmt = time.gmtime(now) now = time.localtime(now) if now[3] < 12: self.ampm='(AM|am)' else: self.ampm='(PM|pm)' self.jan1 = time.localtime(time.mktime((now[0], 1, 1, 0, 0, 0, 0, 1, 0))) try: if now[8]: self.tz = time.tzname[1] else: self.tz = time.tzname[0] except AttributeError: self.tz = '' if now[3] > 12: self.clock12 = now[3] - 12 elif now[3] > 0: self.clock12 = now[3] else: self.clock12 = 12 self.now = now def setUp(self): try: import java java.util.Locale.setDefault(java.util.Locale.US) except ImportError: import locale locale.setlocale(locale.LC_TIME, 'C') def test_strftime(self): now = time.time() self._update_variables(now) self.strftest1(now) self.strftest2(now) if test_support.verbose: print "Strftime test, platform: %s, Python version: %s" % \ (sys.platform, sys.version.split()[0]) for j in range(-5, 5): for i in range(25): arg = now + (i+j*100)*23*3603 self._update_variables(arg) self.strftest1(arg) self.strftest2(arg) def strftest1(self, now): if test_support.verbose: print "strftime test for", time.ctime(now) now = self.now # Make sure any characters that could be taken as regex syntax is # escaped in escapestr() expectations = ( ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'), ('%A', calendar.day_name[now[6]], 'full weekday name'), ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'), ('%B', calendar.month_name[now[1]], 'full month name'), # %c see below ('%d', '%02d' % now[2], 'day of month as number (00-31)'), ('%H', '%02d' % now[3], 'hour (00-23)'), ('%I', '%02d' % self.clock12, 'hour (01-12)'), ('%j', '%03d' % now[7], 'julian day (001-366)'), ('%m', '%02d' % now[1], 'month as number (01-12)'), ('%M', '%02d' % now[4], 'minute, (00-59)'), ('%p', self.ampm, 'AM or PM as appropriate'), ('%S', '%02d' % now[5], 'seconds of current time (00-60)'), ('%U', '%02d' % ((now[7] + self.jan1[6])//7), 'week number of the year (Sun 1st)'), ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'), ('%W', '%02d' % ((now[7] + (self.jan1[6] - 1)%7)//7), 'week number of the year (Mon 1st)'), # %x see below ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'), ('%y', '%02d' % (now[0]%100), 'year without century'), ('%Y', '%d' % now[0], 'year with century'), # %Z see below ('%%', '%', 'single percent sign'), ) for e in expectations: # musn't raise a value error try: result = time.strftime(e[0], now) except ValueError, error: print "Standard '%s' format gaver error:" % (e[0], error) continue if re.match(escapestr(e[1], self.ampm), result): continue if not result or result[0] == '%': print "Does not support standard '%s' format (%s)" % \ (e[0], e[2]) else: print "Conflict for %s (%s):" % (e[0], e[2]) print " Expected %s, but got %s" % (e[1], result) def strftest2(self, now): nowsecs = str(long(now))[:-1] now = self.now nonstandard_expectations = ( # These are standard but don't have predictable output ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'), ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), '%m/%d/%y %H:%M:%S'), ('%Z', '%s' % self.tz, 'time zone name'), # These are some platform specific extensions ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'), ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'), ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'), ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'), ('%n', '\n', 'newline character'), ('%r', '%02d:%02d:%02d %s' % (self.clock12, now[4], now[5], self.ampm), '%I:%M:%S %p'), ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'), ('%s', nowsecs, 'seconds since the Epoch in UCT'), ('%t', '\t', 'tab character'), ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'), ('%3y', '%03d' % (now[0]%100), 'year without century rendered using fieldwidth'), ) for e in nonstandard_expectations: try: result = time.strftime(e[0], now) except ValueError, result: msg = "Error for nonstandard '%s' format (%s): %s" % \ (e[0], e[2], str(result)) if test_support.verbose: print msg continue if re.match(escapestr(e[1], self.ampm), result): if test_support.verbose: print "Supports nonstandard '%s' format (%s)" % (e[0], e[2]) elif not result or result[0] == '%': if test_support.verbose: print "Does not appear to support '%s' format (%s)" % \ (e[0], e[2]) else: if test_support.verbose: print "Conflict for nonstandard '%s' format (%s):" % \ (e[0], e[2]) print " Expected %s, but got %s" % (e[1], result) def test_main(): test_support.run_unittest(StrftimeTest) if __name__ == '__main__': test_main()
apache-2.0
JShadowMan/package
python/zdl/error_logger/error_logger/url_rules/report.py
2
2478
#!/usr/bin/env python # # Copyright (C) 2017 import json import time from error_logger.url_rules import _base_url_rule # from error_logger.net import sms_notification, email_notification from error_logger.utils import generic class Report(_base_url_rule.BaseUrlRule): __url__ = '/report' __methods__ = ['POST'] def __init__(self, config, *args, **kwargs): super(Report, self).__init__(config) def callback(self): adapter = self.get_adapter() source = self.get_url_parameter('source') # type: str json_data = self.get_body_dict() # type: dict for error in json_data.get('errors', []): _level = int(error.pop('level')) _time = int(error.pop('time', self.get_current_timestamp())) _module = generic.to_string(error.pop('module')) _type = generic.to_string(error.pop('type')) _msg = error.pop('msg') _other_data = json.dumps(error) _ip = generic.to_string(self.get_remote_ip()) # TODO. modify this self._notification(source, _level) if not _level or not _time or not _module or not _type or not _msg: return self.jsonify(1, 'report data format invalid, ' 'may be loss some fields') source.replace('\'', '\'\'') sql = 'INSERT INTO "{source}"'.format(source=source) with adapter.cursor() as cursor: sql = cursor.mogrify( ''' INSERT INTO "{source}" ("level", "time", "module", "type", "msg", "ip", "other_data") VALUES (%s, %s, %s, %s, %s, %s, %s) '''.format(source=source), (_level, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(_time)), _module, _type, _msg, _ip, _other_data ) ) try: adapter.execute(sql) except Exception as e: print e return self.jsonify(2, 'insert error data error occurs, may be' ' error data invalid or server error') else: return self.jsonify(0, 'success') def _notification(self, source, error_level): pass
mit
berth64/modded_modded_1257ad
source/ID_map_icons.py
1
14478
icon_player = 0 icon_player_horseman = 1 icon_gray_knight = 2 icon_vaegir_knight = 3 icon_flagbearer_a = 4 icon_flagbearer_b = 5 icon_peasant = 6 icon_khergit = 7 icon_khergit_horseman_b = 8 icon_axeman = 9 icon_woman = 10 icon_woman_b = 11 icon_town = 12 icon_town_steppe = 13 icon_town_desert = 14 icon_village_a = 15 icon_village_b = 16 icon_village_c = 17 icon_village_burnt_a = 18 icon_village_deserted_a = 19 icon_village_burnt_b = 20 icon_village_deserted_b = 21 icon_village_burnt_c = 22 icon_village_deserted_c = 23 icon_village_snow_a = 24 icon_village_snow_burnt_a = 25 icon_village_snow_deserted_a = 26 icon_camp = 27 icon_ship = 28 icon_ship_on_land = 29 icon_castle_a = 30 icon_castle_b = 31 icon_castle_c = 32 icon_castle_d = 33 icon_town_snow = 34 icon_castle_snow_a = 35 icon_castle_snow_b = 36 icon_mule = 37 icon_cattle = 38 icon_training_ground = 39 icon_bridge_a = 40 icon_bridge_b = 41 icon_bridge_snow_a = 42 icon_longship = 43 icon_longship_poland = 44 icon_longship_denmark = 45 icon_longship_teu = 46 icon_longship_lithuania = 47 icon_longship_hre = 48 icon_longship_hungary = 49 icon_longship_novgorod = 50 icon_longship_england = 51 icon_longship_france = 52 icon_longship_norway = 53 icon_longship_scotland = 54 icon_longship_ireland = 55 icon_longship_sweden = 56 icon_longship_mongol = 57 icon_longship_galicia = 58 icon_longship_portugal = 59 icon_longship_aragon = 60 icon_longship_castile = 61 icon_longship_navarra = 62 icon_longship_granada = 63 icon_longship_papal = 64 icon_longship_byzantine = 65 icon_longship_jerusalem = 66 icon_longship_sicily = 67 icon_longship_mamluke = 68 icon_longship_latin = 69 icon_longship_ilkhanate = 70 icon_longship_hafsid = 71 icon_longship_serbia = 72 icon_longship_bulgaria = 73 icon_longship_marinid = 74 icon_longship_pirate = 75 icon_constantinople = 76 icon_prague = 77 icon_andalusian = 78 icon_italy = 79 icon_russian = 80 icon_eastern = 81 icon_eastern2 = 82 icon_wooden = 83 icon_acre = 84 icon_french_town = 85 icon_dutch_town = 86 icon_dutch_castle = 87 icon_european_castle = 88 icon_french_castle = 89 icon_norman_castle = 90 icon_medi_castle = 91 icon_castle_icon_a = 92 icon_rus = 93 icon_manor_icon = 94 icon_merc_icon = 95 icon_crusaders = 96 icon_italy_new_a = 97 icon_italy_new_b = 98 icon_teutonic_town = 99 icon_italy_castle = 100 icon_village_eastern = 101 icon_village_italy = 102 icon_village_middle_europe = 103 icon_village_byzantium = 104 icon_castle_byzantium = 105 icon_bandit_marker = 106 icon_bandit_lair = 107 icon_custom_banner_01 = 108 icon_custom_banner_02 = 109 icon_custom_banner_03 = 110 icon_banner_01 = 111 icon_banner_02 = 112 icon_banner_03 = 113 icon_banner_04 = 114 icon_banner_05 = 115 icon_banner_06 = 116 icon_banner_07 = 117 icon_banner_08 = 118 icon_banner_09 = 119 icon_banner_10 = 120 icon_banner_11 = 121 icon_banner_12 = 122 icon_banner_13 = 123 icon_banner_14 = 124 icon_banner_15 = 125 icon_banner_16 = 126 icon_banner_17 = 127 icon_banner_18 = 128 icon_banner_19 = 129 icon_banner_20 = 130 icon_banner_21 = 131 icon_banner_22 = 132 icon_banner_23 = 133 icon_banner_24 = 134 icon_banner_25 = 135 icon_banner_26 = 136 icon_banner_27 = 137 icon_banner_28 = 138 icon_banner_29 = 139 icon_banner_30 = 140 icon_banner_31 = 141 icon_banner_32 = 142 icon_banner_33 = 143 icon_banner_34 = 144 icon_banner_35 = 145 icon_banner_36 = 146 icon_banner_37 = 147 icon_banner_38 = 148 icon_banner_39 = 149 icon_banner_40 = 150 icon_banner_41 = 151 icon_banner_42 = 152 icon_banner_43 = 153 icon_banner_44 = 154 icon_banner_45 = 155 icon_banner_46 = 156 icon_banner_47 = 157 icon_banner_48 = 158 icon_banner_49 = 159 icon_banner_50 = 160 icon_banner_51 = 161 icon_banner_52 = 162 icon_banner_53 = 163 icon_banner_54 = 164 icon_banner_55 = 165 icon_banner_56 = 166 icon_banner_57 = 167 icon_banner_58 = 168 icon_banner_59 = 169 icon_banner_60 = 170 icon_banner_61 = 171 icon_banner_62 = 172 icon_banner_63 = 173 icon_banner_64 = 174 icon_banner_65 = 175 icon_banner_66 = 176 icon_banner_67 = 177 icon_banner_68 = 178 icon_banner_69 = 179 icon_banner_70 = 180 icon_banner_71 = 181 icon_banner_72 = 182 icon_banner_73 = 183 icon_banner_74 = 184 icon_banner_75 = 185 icon_banner_76 = 186 icon_banner_77 = 187 icon_banner_78 = 188 icon_banner_79 = 189 icon_banner_80 = 190 icon_banner_81 = 191 icon_banner_82 = 192 icon_banner_83 = 193 icon_banner_84 = 194 icon_banner_85 = 195 icon_banner_86 = 196 icon_banner_87 = 197 icon_banner_88 = 198 icon_banner_89 = 199 icon_banner_90 = 200 icon_banner_91 = 201 icon_banner_92 = 202 icon_banner_93 = 203 icon_banner_94 = 204 icon_banner_95 = 205 icon_banner_96 = 206 icon_banner_97 = 207 icon_banner_98 = 208 icon_banner_99 = 209 icon_banner_100 = 210 icon_banner_101 = 211 icon_banner_102 = 212 icon_banner_103 = 213 icon_banner_104 = 214 icon_banner_105 = 215 icon_banner_106 = 216 icon_banner_107 = 217 icon_banner_108 = 218 icon_banner_109 = 219 icon_banner_110 = 220 icon_banner_111 = 221 icon_banner_112 = 222 icon_banner_113 = 223 icon_banner_114 = 224 icon_banner_115 = 225 icon_banner_116 = 226 icon_banner_117 = 227 icon_banner_118 = 228 icon_banner_119 = 229 icon_banner_120 = 230 icon_banner_121 = 231 icon_banner_122 = 232 icon_banner_123 = 233 icon_banner_124 = 234 icon_banner_125 = 235 icon_banner_126 = 236 icon_banner_127 = 237 icon_banner_128 = 238 icon_banner_129 = 239 icon_banner_130 = 240 icon_banner_131 = 241 icon_banner_132 = 242 icon_banner_133 = 243 icon_banner_134 = 244 icon_banner_135 = 245 icon_banner_136 = 246 icon_banner_137 = 247 icon_banner_138 = 248 icon_banner_139 = 249 icon_banner_140 = 250 icon_banner_141 = 251 icon_banner_142 = 252 icon_banner_143 = 253 icon_banner_144 = 254 icon_banner_145 = 255 icon_banner_146 = 256 icon_banner_147 = 257 icon_banner_148 = 258 icon_banner_149 = 259 icon_banner_150 = 260 icon_banner_151 = 261 icon_banner_152 = 262 icon_banner_153 = 263 icon_banner_154 = 264 icon_banner_155 = 265 icon_banner_156 = 266 icon_banner_157 = 267 icon_banner_158 = 268 icon_banner_159 = 269 icon_banner_160 = 270 icon_banner_161 = 271 icon_banner_162 = 272 icon_banner_163 = 273 icon_banner_164 = 274 icon_banner_165 = 275 icon_banner_166 = 276 icon_banner_167 = 277 icon_banner_168 = 278 icon_banner_169 = 279 icon_banner_170 = 280 icon_banner_171 = 281 icon_banner_172 = 282 icon_banner_173 = 283 icon_banner_174 = 284 icon_banner_175 = 285 icon_banner_176 = 286 icon_banner_177 = 287 icon_banner_178 = 288 icon_banner_179 = 289 icon_banner_180 = 290 icon_banner_181 = 291 icon_banner_182 = 292 icon_banner_183 = 293 icon_banner_184 = 294 icon_banner_185 = 295 icon_banner_186 = 296 icon_banner_187 = 297 icon_banner_188 = 298 icon_banner_189 = 299 icon_banner_190 = 300 icon_banner_191 = 301 icon_banner_192 = 302 icon_banner_193 = 303 icon_banner_194 = 304 icon_banner_195 = 305 icon_banner_196 = 306 icon_banner_197 = 307 icon_banner_198 = 308 icon_banner_199 = 309 icon_banner_200 = 310 icon_banner_201 = 311 icon_banner_202 = 312 icon_banner_203 = 313 icon_banner_204 = 314 icon_banner_205 = 315 icon_banner_206 = 316 icon_banner_207 = 317 icon_banner_208 = 318 icon_banner_209 = 319 icon_banner_210 = 320 icon_banner_211 = 321 icon_banner_212 = 322 icon_banner_213 = 323 icon_banner_214 = 324 icon_banner_215 = 325 icon_banner_216 = 326 icon_banner_217 = 327 icon_banner_218 = 328 icon_banner_219 = 329 icon_banner_220 = 330 icon_banner_221 = 331 icon_banner_222 = 332 icon_banner_223 = 333 icon_banner_224 = 334 icon_banner_225 = 335 icon_banner_226 = 336 icon_banner_227 = 337 icon_banner_228 = 338 icon_banner_229 = 339 icon_banner_230 = 340 icon_banner_231 = 341 icon_banner_232 = 342 icon_banner_233 = 343 icon_banner_234 = 344 icon_banner_235 = 345 icon_banner_236 = 346 icon_banner_237 = 347 icon_banner_238 = 348 icon_banner_239 = 349 icon_banner_240 = 350 icon_banner_241 = 351 icon_banner_242 = 352 icon_banner_243 = 353 icon_banner_244 = 354 icon_banner_245 = 355 icon_banner_246 = 356 icon_banner_247 = 357 icon_banner_248 = 358 icon_banner_249 = 359 icon_banner_250 = 360 icon_banner_251 = 361 icon_banner_252 = 362 icon_banner_253 = 363 icon_banner_254 = 364 icon_banner_255 = 365 icon_banner_256 = 366 icon_banner_257 = 367 icon_banner_258 = 368 icon_banner_259 = 369 icon_banner_260 = 370 icon_banner_261 = 371 icon_banner_262 = 372 icon_banner_263 = 373 icon_banner_264 = 374 icon_banner_265 = 375 icon_banner_266 = 376 icon_banner_267 = 377 icon_banner_268 = 378 icon_banner_269 = 379 icon_banner_270 = 380 icon_banner_271 = 381 icon_banner_272 = 382 icon_banner_273 = 383 icon_banner_274 = 384 icon_banner_275 = 385 icon_banner_276 = 386 icon_banner_277 = 387 icon_banner_278 = 388 icon_banner_279 = 389 icon_banner_280 = 390 icon_banner_281 = 391 icon_banner_282 = 392 icon_banner_283 = 393 icon_banner_284 = 394 icon_banner_285 = 395 icon_banner_286 = 396 icon_banner_287 = 397 icon_banner_288 = 398 icon_banner_289 = 399 icon_banner_290 = 400 icon_banner_291 = 401 icon_banner_292 = 402 icon_banner_293 = 403 icon_banner_294 = 404 icon_banner_295 = 405 icon_banner_296 = 406 icon_banner_297 = 407 icon_banner_298 = 408 icon_banner_299 = 409 icon_banner_300 = 410 icon_banner_301 = 411 icon_banner_302 = 412 icon_banner_303 = 413 icon_banner_304 = 414 icon_banner_305 = 415 icon_banner_306 = 416 icon_banner_307 = 417 icon_banner_308 = 418 icon_banner_309 = 419 icon_banner_310 = 420 icon_banner_311 = 421 icon_banner_312 = 422 icon_banner_313 = 423 icon_banner_314 = 424 icon_banner_315 = 425 icon_banner_316 = 426 icon_banner_317 = 427 icon_banner_318 = 428 icon_banner_319 = 429 icon_banner_320 = 430 icon_banner_321 = 431 icon_banner_322 = 432 icon_banner_323 = 433 icon_banner_324 = 434 icon_banner_325 = 435 icon_banner_326 = 436 icon_banner_327 = 437 icon_banner_328 = 438 icon_banner_329 = 439 icon_banner_330 = 440 icon_banner_331 = 441 icon_banner_332 = 442 icon_banner_333 = 443 icon_banner_334 = 444 icon_banner_335 = 445 icon_banner_336 = 446 icon_banner_337 = 447 icon_banner_338 = 448 icon_banner_339 = 449 icon_banner_340 = 450 icon_banner_341 = 451 icon_banner_342 = 452 icon_banner_343 = 453 icon_banner_344 = 454 icon_banner_345 = 455 icon_banner_346 = 456 icon_banner_347 = 457 icon_banner_348 = 458 icon_banner_349 = 459 icon_banner_350 = 460 icon_banner_351 = 461 icon_banner_352 = 462 icon_banner_353 = 463 icon_banner_354 = 464 icon_banner_355 = 465 icon_banner_356 = 466 icon_banner_357 = 467 icon_banner_358 = 468 icon_banner_359 = 469 icon_banner_360 = 470 icon_banner_361 = 471 icon_banner_362 = 472 icon_banner_363 = 473 icon_banner_364 = 474 icon_banner_365 = 475 icon_banner_366 = 476 icon_banner_367 = 477 icon_banner_368 = 478 icon_banner_369 = 479 icon_banner_370 = 480 icon_banner_371 = 481 icon_banner_372 = 482 icon_banner_373 = 483 icon_banner_374 = 484 icon_banner_375 = 485 icon_banner_376 = 486 icon_banner_377 = 487 icon_banner_378 = 488 icon_banner_379 = 489 icon_banner_380 = 490 icon_banner_381 = 491 icon_banner_382 = 492 icon_banner_383 = 493 icon_banner_384 = 494 icon_banner_385 = 495 icon_banner_386 = 496 icon_banner_387 = 497 icon_banner_388 = 498 icon_banner_389 = 499 icon_banner_390 = 500 icon_banner_391 = 501 icon_banner_392 = 502 icon_banner_393 = 503 icon_banner_394 = 504 icon_banner_395 = 505 icon_banner_396 = 506 icon_banner_397 = 507 icon_banner_398 = 508 icon_banner_399 = 509 icon_banner_400 = 510 icon_banner_401 = 511 icon_banner_402 = 512 icon_banner_403 = 513 icon_banner_404 = 514 icon_banner_405 = 515 icon_banner_406 = 516 icon_banner_407 = 517 icon_banner_408 = 518 icon_banner_409 = 519 icon_banner_410 = 520 icon_banner_411 = 521 icon_banner_412 = 522 icon_banner_413 = 523 icon_banner_414 = 524 icon_banner_415 = 525 icon_banner_416 = 526 icon_banner_417 = 527 icon_banner_418 = 528 icon_banner_419 = 529 icon_banner_420 = 530 icon_banner_421 = 531 icon_banner_422 = 532 icon_banner_423 = 533 icon_banner_424 = 534 icon_banner_425 = 535 icon_banner_426 = 536 icon_banner_427 = 537 icon_banner_428 = 538 icon_banner_429 = 539 icon_banner_430 = 540 icon_banner_431 = 541 icon_banner_432 = 542 icon_banner_433 = 543 icon_banner_434 = 544 icon_banner_435 = 545 icon_banner_436 = 546 icon_banner_437 = 547 icon_banner_438 = 548 icon_banner_439 = 549 icon_banner_440 = 550 icon_banner_441 = 551 icon_banner_442 = 552 icon_banner_443 = 553 icon_banner_444 = 554 icon_banner_445 = 555 icon_banner_446 = 556 icon_banner_447 = 557 icon_banner_448 = 558 icon_banner_449 = 559 icon_banner_450 = 560 icon_banner_451 = 561 icon_banner_452 = 562 icon_banner_453 = 563 icon_banner_454 = 564 icon_banner_455 = 565 icon_banner_456 = 566 icon_banner_457 = 567 icon_banner_458 = 568 icon_banner_459 = 569 icon_banner_460 = 570 icon_banner_461 = 571 icon_banner_462 = 572 icon_banner_463 = 573 icon_banner_464 = 574 icon_banner_465 = 575 icon_banner_466 = 576 icon_banner_467 = 577 icon_banner_468 = 578 icon_banner_469 = 579 icon_banner_470 = 580 icon_banner_471 = 581 icon_banner_472 = 582 icon_banner_473 = 583 icon_banner_474 = 584 icon_banner_475 = 585 icon_banner_476 = 586 icon_banner_477 = 587 icon_banner_478 = 588 icon_banner_479 = 589 icon_banner_480 = 590 icon_banner_481 = 591 icon_banner_482 = 592 icon_banner_483 = 593 icon_map_flag_kingdom_1 = 594 icon_map_flag_kingdom_2 = 595 icon_map_flag_kingdom_3 = 596 icon_map_flag_kingdom_4 = 597 icon_map_flag_kingdom_5 = 598 icon_map_flag_kingdom_6 = 599 icon_map_flag_kingdom_7 = 600 icon_map_flag_kingdom_8 = 601 icon_map_flag_kingdom_9 = 602 icon_map_flag_kingdom_10 = 603 icon_map_flag_kingdom_11 = 604 icon_map_flag_kingdom_12 = 605 icon_map_flag_kingdom_13 = 606 icon_map_flag_kingdom_14 = 607 icon_map_flag_kingdom_15 = 608 icon_map_flag_kingdom_16 = 609 icon_map_flag_kingdom_17 = 610 icon_map_flag_kingdom_18 = 611 icon_map_flag_kingdom_19 = 612 icon_map_flag_kingdom_20 = 613 icon_map_flag_kingdom_21 = 614 icon_map_flag_kingdom_22 = 615 icon_map_flag_kingdom_23 = 616 icon_map_flag_kingdom_24 = 617
agpl-3.0