{ // 获取包含Hugging Face文本的span元素 const spans = link.querySelectorAll('span.whitespace-nowrap, span.hidden.whitespace-nowrap'); spans.forEach(span => { if (span.textContent && span.textContent.trim().match(/Hugging\s*Face/i)) { span.textContent = 'AI快站'; } }); }); // 替换logo图片的alt属性 document.querySelectorAll('img[alt*="Hugging"], img[alt*="Face"]').forEach(img => { if (img.alt.match(/Hugging\s*Face/i)) { img.alt = 'AI快站 logo'; } }); } // 替换导航栏中的链接 function replaceNavigationLinks() { // 已替换标记,防止重复运行 if (window._navLinksReplaced) { return; } // 已经替换过的链接集合,防止重复替换 const replacedLinks = new Set(); // 只在导航栏区域查找和替换链接 const headerArea = document.querySelector('header') || document.querySelector('nav'); if (!headerArea) { return; } // 在导航区域内查找链接 const navLinks = headerArea.querySelectorAll('a'); navLinks.forEach(link => { // 如果已经替换过,跳过 if (replacedLinks.has(link)) return; const linkText = link.textContent.trim(); const linkHref = link.getAttribute('href') || ''; // 替换Spaces链接 - 仅替换一次 if ( (linkHref.includes('/spaces') || linkHref === '/spaces' || linkText === 'Spaces' || linkText.match(/^s*Spacess*$/i)) && linkText !== 'PDF TO Markdown' && linkText !== 'PDF TO Markdown' ) { link.textContent = 'PDF TO Markdown'; link.href = 'https://fast360.xyz'; link.setAttribute('target', '_blank'); link.setAttribute('rel', 'noopener noreferrer'); replacedLinks.add(link); } // 删除Posts链接 else if ( (linkHref.includes('/posts') || linkHref === '/posts' || linkText === 'Posts' || linkText.match(/^s*Postss*$/i)) ) { if (link.parentNode) { link.parentNode.removeChild(link); } replacedLinks.add(link); } // 替换Docs链接 - 仅替换一次 else if ( (linkHref.includes('/docs') || linkHref === '/docs' || linkText === 'Docs' || linkText.match(/^s*Docss*$/i)) && linkText !== 'Voice Cloning' ) { link.textContent = 'Voice Cloning'; link.href = 'https://vibevoice.info/'; replacedLinks.add(link); } // 删除Enterprise链接 else if ( (linkHref.includes('/enterprise') || linkHref === '/enterprise' || linkText === 'Enterprise' || linkText.match(/^s*Enterprises*$/i)) ) { if (link.parentNode) { link.parentNode.removeChild(link); } replacedLinks.add(link); } }); // 查找可能嵌套的Spaces和Posts文本 const textNodes = []; function findTextNodes(element) { if (element.nodeType === Node.TEXT_NODE) { const text = element.textContent.trim(); if (text === 'Spaces' || text === 'Posts' || text === 'Enterprise') { textNodes.push(element); } } else { for (const child of element.childNodes) { findTextNodes(child); } } } // 只在导航区域内查找文本节点 findTextNodes(headerArea); // 替换找到的文本节点 textNodes.forEach(node => { const text = node.textContent.trim(); if (text === 'Spaces') { node.textContent = node.textContent.replace(/Spaces/g, 'PDF TO Markdown'); } else if (text === 'Posts') { // 删除Posts文本节点 if (node.parentNode) { node.parentNode.removeChild(node); } } else if (text === 'Enterprise') { // 删除Enterprise文本节点 if (node.parentNode) { node.parentNode.removeChild(node); } } }); // 标记已替换完成 window._navLinksReplaced = true; } // 替换代码区域中的域名 function replaceCodeDomains() { // 特别处理span.hljs-string和span.njs-string元素 document.querySelectorAll('span.hljs-string, span.njs-string, span[class*="hljs-string"], span[class*="njs-string"]').forEach(span => { if (span.textContent && span.textContent.includes('huggingface.co')) { span.textContent = span.textContent.replace(/huggingface.co/g, 'aifasthub.com'); } }); // 替换hljs-string类的span中的域名(移除多余的转义符号) document.querySelectorAll('span.hljs-string, span[class*="hljs-string"]').forEach(span => { if (span.textContent && span.textContent.includes('huggingface.co')) { span.textContent = span.textContent.replace(/huggingface.co/g, 'aifasthub.com'); } }); // 替换pre和code标签中包含git clone命令的域名 document.querySelectorAll('pre, code').forEach(element => { if (element.textContent && element.textContent.includes('git clone')) { const text = element.innerHTML; if (text.includes('huggingface.co')) { element.innerHTML = text.replace(/huggingface.co/g, 'aifasthub.com'); } } }); // 处理特定的命令行示例 document.querySelectorAll('pre, code').forEach(element => { const text = element.innerHTML; if (text.includes('huggingface.co')) { // 针对git clone命令的专门处理 if (text.includes('git clone') || text.includes('GIT_LFS_SKIP_SMUDGE=1')) { element.innerHTML = text.replace(/huggingface.co/g, 'aifasthub.com'); } } }); // 特别处理模型下载页面上的代码片段 document.querySelectorAll('.flex.border-t, .svelte_hydrator, .inline-block').forEach(container => { const content = container.innerHTML; if (content && content.includes('huggingface.co')) { container.innerHTML = content.replace(/huggingface.co/g, 'aifasthub.com'); } }); // 特别处理模型仓库克隆对话框中的代码片段 try { // 查找包含"Clone this model repository"标题的对话框 const cloneDialog = document.querySelector('.svelte_hydration_boundary, [data-target="MainHeader"]'); if (cloneDialog) { // 查找对话框中所有的代码片段和命令示例 const codeElements = cloneDialog.querySelectorAll('pre, code, span'); codeElements.forEach(element => { if (element.textContent && element.textContent.includes('huggingface.co')) { if (element.innerHTML.includes('huggingface.co')) { element.innerHTML = element.innerHTML.replace(/huggingface.co/g, 'aifasthub.com'); } else { element.textContent = element.textContent.replace(/huggingface.co/g, 'aifasthub.com'); } } }); } // 更精确地定位克隆命令中的域名 document.querySelectorAll('[data-target]').forEach(container => { const codeBlocks = container.querySelectorAll('pre, code, span.hljs-string'); codeBlocks.forEach(block => { if (block.textContent && block.textContent.includes('huggingface.co')) { if (block.innerHTML.includes('huggingface.co')) { block.innerHTML = block.innerHTML.replace(/huggingface.co/g, 'aifasthub.com'); } else { block.textContent = block.textContent.replace(/huggingface.co/g, 'aifasthub.com'); } } }); }); } catch (e) { // 错误处理但不打印日志 } } // 当DOM加载完成后执行替换 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => { replaceHeaderBranding(); replaceNavigationLinks(); replaceCodeDomains(); // 只在必要时执行替换 - 3秒后再次检查 setTimeout(() => { if (!window._navLinksReplaced) { console.log('[Client] 3秒后重新检查导航链接'); replaceNavigationLinks(); } }, 3000); }); } else { replaceHeaderBranding(); replaceNavigationLinks(); replaceCodeDomains(); // 只在必要时执行替换 - 3秒后再次检查 setTimeout(() => { if (!window._navLinksReplaced) { console.log('[Client] 3秒后重新检查导航链接'); replaceNavigationLinks(); } }, 3000); } // 增加一个MutationObserver来处理可能的动态元素加载 const observer = new MutationObserver(mutations => { // 检查是否导航区域有变化 const hasNavChanges = mutations.some(mutation => { // 检查是否存在header或nav元素变化 return Array.from(mutation.addedNodes).some(node => { if (node.nodeType === Node.ELEMENT_NODE) { // 检查是否是导航元素或其子元素 if (node.tagName === 'HEADER' || node.tagName === 'NAV' || node.querySelector('header, nav')) { return true; } // 检查是否在导航元素内部 let parent = node.parentElement; while (parent) { if (parent.tagName === 'HEADER' || parent.tagName === 'NAV') { return true; } parent = parent.parentElement; } } return false; }); }); // 只在导航区域有变化时执行替换 if (hasNavChanges) { // 重置替换状态,允许再次替换 window._navLinksReplaced = false; replaceHeaderBranding(); replaceNavigationLinks(); } }); // 开始观察document.body的变化,包括子节点 if (document.body) { observer.observe(document.body, { childList: true, subtree: true }); } else { document.addEventListener('DOMContentLoaded', () => { observer.observe(document.body, { childList: true, subtree: true }); }); } })(); \n\n'''\n\ndef make_eval_exception(app, global_conf, xmlhttp_key=None):\n \"\"\"\n Wraps the application in an interactive debugger.\n\n This debugger is a major security hole, and should only be\n used during development.\n\n xmlhttp_key is a string that, if present in QUERY_STRING,\n indicates that the request is an XMLHttp request, and the\n Javascript/interactive debugger should not be returned. (If you\n try to put the debugger somewhere with innerHTML, you will often\n crash the browser)\n \"\"\"\n if xmlhttp_key is None:\n xmlhttp_key = global_conf.get('xmlhttp_key', '_')\n return EvalException(app, xmlhttp_key=xmlhttp_key)\n"},"license":{"kind":"string","value":"bsd-3-clause"}}}],"truncated":false,"partial":false},"paginationData":{"pageIndex":2033,"numItemsPerPage":100,"numTotalItems":203850,"offset":203300,"length":100}},"jwt":"eyJhbGciOiJFZERTQSJ9.eyJyZWFkIjp0cnVlLCJwZXJtaXNzaW9ucyI6eyJyZXBvLmNvbnRlbnQucmVhZCI6dHJ1ZX0sImlhdCI6MTc1ODA3OTA4MCwic3ViIjoiL2RhdGFzZXRzL3Rob213b2xmL2dpdGh1Yi1kYXRhc2V0IiwiZXhwIjoxNzU4MDgyNjgwLCJpc3MiOiJodHRwczovL2h1Z2dpbmdmYWNlLmNvIn0.y41olYp-6mfPRFXESokBAOIyZK_WTAhGjrDp7VhxTFdudAV_iJi-O6lbM4ulmcr3XrZe1ofYDh212Z98qClJBw","displayUrls":true},"discussionsStats":{"closed":0,"open":1,"total":1},"fullWidth":true,"hasGatedAccess":true,"hasFullAccess":true,"isEmbedded":false,"savedQueries":{"community":[],"user":[]}}">
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
DmitriyKirakosyan/online-go.com
i18n/gen-country-lists.py
4
1467
#!/usr/bin/env python2 import json import os import re import sys from xml.dom.minidom import parse import xml.dom.minidom cldr_main = "cldr/common/main"; def getText(nodelist): rc = [] for node in nodelist: if node.nodeType == node.TEXT_NODE: rc.append(node.data) return ''.join(rc) def read_and_generate(base_language_file): countries = {} dom = parse(base_language_file) lds = dom.getElementsByTagName("localeDisplayNames")[0] try: territories = lds.getElementsByTagName("territories")[0] for territory in territories.getElementsByTagName("territory"): alt = territory.getAttribute("alt") if alt == "variant": continue if alt == "short": continue cc = territory.getAttribute("type").strip().lower() name = getText(territory.childNodes).strip() countries[cc] = name except Exception as e: print(e) countries['un'] = "Unspecified"; return countries result = {} for filename in sorted(os.listdir(cldr_main)): m = re.search('^([a-z]+)[.]xml$', filename) if m: base_language = m.group(1) print("Loading " + base_language) result[base_language] = read_and_generate(cldr_main + "/" + filename) print("Writing build/countries.json") with open('build/countries.json', 'w') as output: output.write(json.dumps(result)) print('Done.');
agpl-3.0
nishnik/networkx
networkx/algorithms/components/tests/test_biconnected.py
5
5596
#!/usr/bin/env python from nose.tools import * import networkx as nx from networkx.algorithms.components import biconnected from networkx import NetworkXNotImplemented def assert_components_edges_equal(x, y): sx = {frozenset([frozenset(e) for e in c]) for c in x} sy = {frozenset([frozenset(e) for e in c]) for c in y} assert_equal(sx, sy) def assert_components_equal(x, y): sx = {frozenset(c) for c in x} sy = {frozenset(c) for c in y} assert_equal(sx, sy) def test_barbell(): G = nx.barbell_graph(8, 4) nx.add_path(G, [7, 20, 21, 22]) nx.add_cycle(G, [22, 23, 24, 25]) pts = set(nx.articulation_points(G)) assert_equal(pts, {7, 8, 9, 10, 11, 12, 20, 21, 22}) answer = [ {12, 13, 14, 15, 16, 17, 18, 19}, {0, 1, 2, 3, 4, 5, 6, 7}, {22, 23, 24, 25}, {11, 12}, {10, 11}, {9, 10}, {8, 9}, {7, 8}, {21, 22}, {20, 21}, {7, 20}, ] assert_components_equal(list(nx.biconnected_components(G)), answer) G.add_edge(2,17) pts = set(nx.articulation_points(G)) assert_equal(pts, {7, 20, 21, 22}) def test_articulation_points_cycle(): G=nx.cycle_graph(3) nx.add_cycle(G, [1, 3, 4]) pts=set(nx.articulation_points(G)) assert_equal(pts, {1}) def test_is_biconnected(): G=nx.cycle_graph(3) assert_true(nx.is_biconnected(G)) nx.add_cycle(G, [1, 3, 4]) assert_false(nx.is_biconnected(G)) def test_empty_is_biconnected(): G=nx.empty_graph(5) assert_false(nx.is_biconnected(G)) G.add_edge(0, 1) assert_false(nx.is_biconnected(G)) def test_biconnected_components_cycle(): G=nx.cycle_graph(3) nx.add_cycle(G, [1, 3, 4]) answer = [{0, 1, 2}, {1, 3, 4}] assert_components_equal(list(nx.biconnected_components(G)), answer) def test_biconnected_component_subgraphs_cycle(): G=nx.cycle_graph(3) nx.add_cycle(G, [1, 3, 4, 5]) Gc = set(nx.biconnected_component_subgraphs(G)) assert_equal(len(Gc), 2) g1, g2=Gc if 0 in g1: assert_true(nx.is_isomorphic(g1, nx.Graph([(0,1),(0,2),(1,2)]))) assert_true(nx.is_isomorphic(g2, nx.Graph([(1,3),(1,5),(3,4),(4,5)]))) else: assert_true(nx.is_isomorphic(g1, nx.Graph([(1,3),(1,5),(3,4),(4,5)]))) assert_true(nx.is_isomorphic(g2, nx.Graph([(0,1),(0,2),(1,2)]))) def test_biconnected_components1(): # graph example from # http://www.ibluemojo.com/school/articul_algorithm.html edges=[ (0, 1), (0, 5), (0, 6), (0, 14), (1, 5), (1, 6), (1, 14), (2, 4), (2, 10), (3, 4), (3, 15), (4, 6), (4, 7), (4, 10), (5, 14), (6, 14), (7, 9), (8, 9), (8, 12), (8, 13), (10, 15), (11, 12), (11, 13), (12, 13) ] G=nx.Graph(edges) pts = set(nx.articulation_points(G)) assert_equal(pts, {4, 6, 7, 8, 9}) comps = list(nx.biconnected_component_edges(G)) answer = [ [(3, 4), (15, 3), (10, 15), (10, 4), (2, 10), (4, 2)], [(13, 12), (13, 8), (11, 13), (12, 11), (8, 12)], [(9, 8)], [(7, 9)], [(4, 7)], [(6, 4)], [(14, 0), (5, 1), (5, 0), (14, 5), (14, 1), (6, 14), (6, 0), (1, 6), (0, 1)], ] assert_components_edges_equal(comps, answer) def test_biconnected_components2(): G=nx.Graph() nx.add_cycle(G, 'ABC') nx.add_cycle(G, 'CDE') nx.add_cycle(G, 'FIJHG') nx.add_cycle(G, 'GIJ') G.add_edge('E','G') comps = list(nx.biconnected_component_edges(G)) answer = [ [tuple('GF'), tuple('FI'), tuple('IG'), tuple('IJ'), tuple('JG'), tuple('JH'), tuple('HG')], [tuple('EG')], [tuple('CD'), tuple('DE'), tuple('CE')], [tuple('AB'), tuple('BC'), tuple('AC')] ] assert_components_edges_equal(comps, answer) def test_biconnected_davis(): D = nx.davis_southern_women_graph() bcc = list(nx.biconnected_components(D))[0] assert_true(set(D) == bcc) # All nodes in a giant bicomponent # So no articulation points assert_equal(len(list(nx.articulation_points(D))), 0) def test_biconnected_karate(): K = nx.karate_club_graph() answer = [{0, 1, 2, 3, 7, 8, 9, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}, {0, 4, 5, 6, 10, 16}, {0, 11}] bcc = list(nx.biconnected_components(K)) assert_components_equal(bcc, answer) assert_equal(set(nx.articulation_points(K)), {0}) def test_biconnected_eppstein(): # tests from http://www.ics.uci.edu/~eppstein/PADS/Biconnectivity.py G1 = nx.Graph({ 0: [1, 2, 5], 1: [0, 5], 2: [0, 3, 4], 3: [2, 4, 5, 6], 4: [2, 3, 5, 6], 5: [0, 1, 3, 4], 6: [3, 4], }) G2 = nx.Graph({ 0: [2, 5], 1: [3, 8], 2: [0, 3, 5], 3: [1, 2, 6, 8], 4: [7], 5: [0, 2], 6: [3, 8], 7: [4], 8: [1, 3, 6], }) assert_true(nx.is_biconnected(G1)) assert_false(nx.is_biconnected(G2)) answer_G2 = [{1, 3, 6, 8}, {0, 2, 5}, {2, 3}, {4, 7}] bcc = list(nx.biconnected_components(G2)) assert_components_equal(bcc, answer_G2) def test_connected_raise(): DG = nx.DiGraph() assert_raises(NetworkXNotImplemented, nx.biconnected_components, DG) assert_raises(NetworkXNotImplemented, nx.biconnected_component_subgraphs, DG) assert_raises(NetworkXNotImplemented, nx.biconnected_component_edges, DG) assert_raises(NetworkXNotImplemented, nx.articulation_points, DG) assert_raises(NetworkXNotImplemented, nx.is_biconnected, DG)
bsd-3-clause
fxia22/ASM_xf
PythonD/lib/python2.4/opcode.py
14
5144
""" opcode module - potentially shared between dis and other modules which operate on bytecodes (e.g. peephole optimizers). """ __all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs", "haslocal", "hascompare", "hasfree", "opname", "opmap", "HAVE_ARGUMENT", "EXTENDED_ARG"] cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', 'is not', 'exception match', 'BAD') hasconst = [] hasname = [] hasjrel = [] hasjabs = [] haslocal = [] hascompare = [] hasfree = [] opmap = {} opname = [''] * 256 for op in range(256): opname[op] = '<%r>' % (op,) del op def def_op(name, op): opname[op] = name opmap[name] = op def name_op(name, op): def_op(name, op) hasname.append(op) def jrel_op(name, op): def_op(name, op) hasjrel.append(op) def jabs_op(name, op): def_op(name, op) hasjabs.append(op) # Instruction opcodes for compiled code def_op('STOP_CODE', 0) def_op('POP_TOP', 1) def_op('ROT_TWO', 2) def_op('ROT_THREE', 3) def_op('DUP_TOP', 4) def_op('ROT_FOUR', 5) def_op('NOP', 9) def_op('UNARY_POSITIVE', 10) def_op('UNARY_NEGATIVE', 11) def_op('UNARY_NOT', 12) def_op('UNARY_CONVERT', 13) def_op('UNARY_INVERT', 15) def_op('LIST_APPEND', 18) def_op('BINARY_POWER', 19) def_op('BINARY_MULTIPLY', 20) def_op('BINARY_DIVIDE', 21) def_op('BINARY_MODULO', 22) def_op('BINARY_ADD', 23) def_op('BINARY_SUBTRACT', 24) def_op('BINARY_SUBSCR', 25) def_op('BINARY_FLOOR_DIVIDE', 26) def_op('BINARY_TRUE_DIVIDE', 27) def_op('INPLACE_FLOOR_DIVIDE', 28) def_op('INPLACE_TRUE_DIVIDE', 29) def_op('SLICE+0', 30) def_op('SLICE+1', 31) def_op('SLICE+2', 32) def_op('SLICE+3', 33) def_op('STORE_SLICE+0', 40) def_op('STORE_SLICE+1', 41) def_op('STORE_SLICE+2', 42) def_op('STORE_SLICE+3', 43) def_op('DELETE_SLICE+0', 50) def_op('DELETE_SLICE+1', 51) def_op('DELETE_SLICE+2', 52) def_op('DELETE_SLICE+3', 53) def_op('INPLACE_ADD', 55) def_op('INPLACE_SUBTRACT', 56) def_op('INPLACE_MULTIPLY', 57) def_op('INPLACE_DIVIDE', 58) def_op('INPLACE_MODULO', 59) def_op('STORE_SUBSCR', 60) def_op('DELETE_SUBSCR', 61) def_op('BINARY_LSHIFT', 62) def_op('BINARY_RSHIFT', 63) def_op('BINARY_AND', 64) def_op('BINARY_XOR', 65) def_op('BINARY_OR', 66) def_op('INPLACE_POWER', 67) def_op('GET_ITER', 68) def_op('PRINT_EXPR', 70) def_op('PRINT_ITEM', 71) def_op('PRINT_NEWLINE', 72) def_op('PRINT_ITEM_TO', 73) def_op('PRINT_NEWLINE_TO', 74) def_op('INPLACE_LSHIFT', 75) def_op('INPLACE_RSHIFT', 76) def_op('INPLACE_AND', 77) def_op('INPLACE_XOR', 78) def_op('INPLACE_OR', 79) def_op('BREAK_LOOP', 80) def_op('LOAD_LOCALS', 82) def_op('RETURN_VALUE', 83) def_op('IMPORT_STAR', 84) def_op('EXEC_STMT', 85) def_op('YIELD_VALUE', 86) def_op('POP_BLOCK', 87) def_op('END_FINALLY', 88) def_op('BUILD_CLASS', 89) HAVE_ARGUMENT = 90 # Opcodes from here have an argument: name_op('STORE_NAME', 90) # Index in name list name_op('DELETE_NAME', 91) # "" def_op('UNPACK_SEQUENCE', 92) # Number of tuple items jrel_op('FOR_ITER', 93) name_op('STORE_ATTR', 95) # Index in name list name_op('DELETE_ATTR', 96) # "" name_op('STORE_GLOBAL', 97) # "" name_op('DELETE_GLOBAL', 98) # "" def_op('DUP_TOPX', 99) # number of items to duplicate def_op('LOAD_CONST', 100) # Index in const list hasconst.append(100) name_op('LOAD_NAME', 101) # Index in name list def_op('BUILD_TUPLE', 102) # Number of tuple items def_op('BUILD_LIST', 103) # Number of list items def_op('BUILD_MAP', 104) # Always zero for now name_op('LOAD_ATTR', 105) # Index in name list def_op('COMPARE_OP', 106) # Comparison operator hascompare.append(106) name_op('IMPORT_NAME', 107) # Index in name list name_op('IMPORT_FROM', 108) # Index in name list jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip jrel_op('JUMP_IF_FALSE', 111) # "" jrel_op('JUMP_IF_TRUE', 112) # "" jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code name_op('LOAD_GLOBAL', 116) # Index in name list jabs_op('CONTINUE_LOOP', 119) # Target address jrel_op('SETUP_LOOP', 120) # Distance to target address jrel_op('SETUP_EXCEPT', 121) # "" jrel_op('SETUP_FINALLY', 122) # "" def_op('LOAD_FAST', 124) # Local variable number haslocal.append(124) def_op('STORE_FAST', 125) # Local variable number haslocal.append(125) def_op('DELETE_FAST', 126) # Local variable number haslocal.append(126) def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3) def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8) def_op('MAKE_FUNCTION', 132) # Number of args with default values def_op('BUILD_SLICE', 133) # Number of items def_op('MAKE_CLOSURE', 134) def_op('LOAD_CLOSURE', 135) hasfree.append(135) def_op('LOAD_DEREF', 136) hasfree.append(136) def_op('STORE_DEREF', 137) hasfree.append(137) def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8) def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8) def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8) def_op('EXTENDED_ARG', 143) EXTENDED_ARG = 143 del def_op, name_op, jrel_op, jabs_op
gpl-2.0
google/rekall
rekall-agent/rekall_agent/serializer_test.py
4
5583
from rekall import testlib from rekall_agent import serializer class TestObject1(serializer.SerializedObject): schema = [ dict(name="C1", type="int"), dict(name="R1", type="unicode", repeated=True), dict(name="C2", type="str"), ] # Note that forward declarations are not currently implemented so declaration # order matters. class TestObject2(serializer.SerializedObject): schema = [ # Nested field. dict(name="N1", type="TestObject1"), ] class ExtendedTestObject1(TestObject1): """An inherited object. This will inherit all the fields from TestObject1 automatically. """ schema = [ dict(name="extra") ] class TestSerializer(testlib.RekallBaseUnitTestCase): """Test the serializer framework.""" def testFieldValidation(self): test_obj = TestObject1(session=self.session) # Should not be allowed to set a string. with self.assertRaises(ValueError): test_obj.C1 = "Hello" test_obj.C1 = 10 self.assertEqual(test_obj.C1, 10) # Setting a float will convert to an int. test_obj.C1 = 20.5 self.assertEqual(test_obj.C1, 20) def testNestedFields(self): test_obj = TestObject2(session=self.session) test_obj.N1.C1 = 10 self.assertEqual(test_obj.N1.C1, 10) self.check_serialization( test_obj, {'N1': {'C1': 10L, '__type__': 'TestObject1'}}) def testUnknownFields(self): """Test handling of unknown fields. When parsing from JSON it should be ok to include unknown fields, since they could represent a new version of the object. Any unknown fields should be stored though and later emitted upon serialization. """ json_blob = """ {"C1": 5, "U1": "foobar"} """ # This should parse properly. obj = TestObject1.from_json(json_blob, session=self.session) self.assertEqual(obj._unknowns["U1"], "foobar") # Make sure there is no such attribute. with self.assertRaises(AttributeError): _ = obj.U1 # Ensure unknowns get re-serialized. self.assertEqual(obj.to_primitive(), dict(C1=5, U1="foobar", __type__="TestObject1")) def check_serialization(self, test_obj, primitive): self.assertEqual(test_obj.to_primitive(with_type=False), primitive) self.assertEqual( test_obj.__class__.from_primitive( primitive, session=self.session), test_obj) self.assertEqual(test_obj.__class__.from_primitive( primitive, session=self.session).to_primitive(with_type=False), primitive) def testRepeatedField(self): test_obj = TestObject1(session=self.session) with self.assertRaises(ValueError): test_obj.R1.append(10) test_obj.R1.append("hello") test_obj.R1.append("world") self.check_serialization(test_obj, {'R1': [u'hello', u'world']}) def testStringSerialization(self): """Makes sure that serializing a string field base64 encodes it.""" test_obj = TestObject1(session=self.session) test_obj.C2 = "hello" with self.assertRaises(ValueError): test_obj.C2 = 10 TestObject1.from_primitive({'C2': 'aGVsbG8=\n'}, session=self.session) self.check_serialization(test_obj, {'C2': 'aGVsbG8=\n'}) def testInheritance(self): """We support a natural form of object inheritance. This means that we can derive a data type and assign to a field of the type of the base class with a derived data type. Upon de-serialization the derived class will be instantiated. This is achieved by including an explicit __type__ field in the raw JSON output. In this test we define container.N1 to be of type TestObject1. We extend TestObject1 with a new type (ExtendedTestObject1). We are allowed to assign to N1 this extended type, and when we serialize it the system will note the proper name of the class in the __type__ attribute. De-serialization will use the __type__ attribute to automatically restore the correct type in the N1 field. """ # A derived object inherits all the base object's fields. test_obj = ExtendedTestObject1.from_keywords(extra="foo", C1=5, session=self.session) container = TestObject2(session=self.session) # Can not assign an int to this field - it is still strongly typed. with self.assertRaises(ValueError): container.N1 = 10 # container.N1 is defined to be of type TestObject1 which is a base # class of ExtendedTestObject1. container.N1 = test_obj self.assertTrue(test_obj, ExtendedTestObject1) self.assertTrue(type(container.N1), ExtendedTestObject1) # Now convert to primitive type. primitive_data = container.to_primitive() # There will be a __type__ field which declares the proper nested type. self.assertEqual(primitive_data["N1"]["__type__"], "ExtendedTestObject1") json_data = container.to_json() # When decoding we receive the correct type in this field. decoded = TestObject2.from_json(json_data, session=self.session) self.assertTrue(type(decoded.N1), ExtendedTestObject1) if __name__ == "__main__": testlib.main()
gpl-2.0
mancoast/CPythonPyc_test
cpython/254_mapping_tests.py
37
22279
# tests common to dict and UserDict import unittest import UserDict class BasicTestMappingProtocol(unittest.TestCase): # This base class can be used to check that an object conforms to the # mapping protocol # Functions that can be useful to override to adapt to dictionary # semantics type2test = None # which class is being tested (overwrite in subclasses) def _reference(self): """Return a dictionary of values which are invariant by storage in the object under test.""" return {1:2, "key1":"value1", "key2":(1,2,3)} def _empty_mapping(self): """Return an empty mapping object""" return self.type2test() def _full_mapping(self, data): """Return a mapping object with the value contained in data dictionary""" x = self._empty_mapping() for key, value in data.items(): x[key] = value return x def __init__(self, *args, **kw): unittest.TestCase.__init__(self, *args, **kw) self.reference = self._reference().copy() # A (key, value) pair not in the mapping key, value = self.reference.popitem() self.other = {key:value} # A (key, value) pair in the mapping key, value = self.reference.popitem() self.inmapping = {key:value} self.reference[key] = value def test_read(self): # Test for read only operations on mapping p = self._empty_mapping() p1 = dict(p) #workaround for singleton objects d = self._full_mapping(self.reference) if d is p: p = p1 #Indexing for key, value in self.reference.items(): self.assertEqual(d[key], value) knownkey = self.other.keys()[0] self.failUnlessRaises(KeyError, lambda:d[knownkey]) #len self.assertEqual(len(p), 0) self.assertEqual(len(d), len(self.reference)) #has_key for k in self.reference: self.assert_(d.has_key(k)) self.assert_(k in d) for k in self.other: self.failIf(d.has_key(k)) self.failIf(k in d) #cmp self.assertEqual(cmp(p,p), 0) self.assertEqual(cmp(d,d), 0) self.assertEqual(cmp(p,d), -1) self.assertEqual(cmp(d,p), 1) #__non__zero__ if p: self.fail("Empty mapping must compare to False") if not d: self.fail("Full mapping must compare to True") # keys(), items(), iterkeys() ... def check_iterandlist(iter, lst, ref): self.assert_(hasattr(iter, 'next')) self.assert_(hasattr(iter, '__iter__')) x = list(iter) self.assert_(set(x)==set(lst)==set(ref)) check_iterandlist(d.iterkeys(), d.keys(), self.reference.keys()) check_iterandlist(iter(d), d.keys(), self.reference.keys()) check_iterandlist(d.itervalues(), d.values(), self.reference.values()) check_iterandlist(d.iteritems(), d.items(), self.reference.items()) #get key, value = d.iteritems().next() knownkey, knownvalue = self.other.iteritems().next() self.assertEqual(d.get(key, knownvalue), value) self.assertEqual(d.get(knownkey, knownvalue), knownvalue) self.failIf(knownkey in d) def test_write(self): # Test for write operations on mapping p = self._empty_mapping() #Indexing for key, value in self.reference.items(): p[key] = value self.assertEqual(p[key], value) for key in self.reference.keys(): del p[key] self.failUnlessRaises(KeyError, lambda:p[key]) p = self._empty_mapping() #update p.update(self.reference) self.assertEqual(dict(p), self.reference) items = p.items() p = self._empty_mapping() p.update(items) self.assertEqual(dict(p), self.reference) d = self._full_mapping(self.reference) #setdefault key, value = d.iteritems().next() knownkey, knownvalue = self.other.iteritems().next() self.assertEqual(d.setdefault(key, knownvalue), value) self.assertEqual(d[key], value) self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue) self.assertEqual(d[knownkey], knownvalue) #pop self.assertEqual(d.pop(knownkey), knownvalue) self.failIf(knownkey in d) self.assertRaises(KeyError, d.pop, knownkey) default = 909 d[knownkey] = knownvalue self.assertEqual(d.pop(knownkey, default), knownvalue) self.failIf(knownkey in d) self.assertEqual(d.pop(knownkey, default), default) #popitem key, value = d.popitem() self.failIf(key in d) self.assertEqual(value, self.reference[key]) p=self._empty_mapping() self.assertRaises(KeyError, p.popitem) def test_constructor(self): self.assertEqual(self._empty_mapping(), self._empty_mapping()) def test_bool(self): self.assert_(not self._empty_mapping()) self.assert_(self.reference) self.assert_(bool(self._empty_mapping()) is False) self.assert_(bool(self.reference) is True) def test_keys(self): d = self._empty_mapping() self.assertEqual(d.keys(), []) d = self.reference self.assert_(self.inmapping.keys()[0] in d.keys()) self.assert_(self.other.keys()[0] not in d.keys()) self.assertRaises(TypeError, d.keys, None) def test_values(self): d = self._empty_mapping() self.assertEqual(d.values(), []) self.assertRaises(TypeError, d.values, None) def test_items(self): d = self._empty_mapping() self.assertEqual(d.items(), []) self.assertRaises(TypeError, d.items, None) def test_len(self): d = self._empty_mapping() self.assertEqual(len(d), 0) def test_getitem(self): d = self.reference self.assertEqual(d[self.inmapping.keys()[0]], self.inmapping.values()[0]) self.assertRaises(TypeError, d.__getitem__) def test_update(self): # mapping argument d = self._empty_mapping() d.update(self.other) self.assertEqual(d.items(), self.other.items()) # No argument d = self._empty_mapping() d.update() self.assertEqual(d, self._empty_mapping()) # item sequence d = self._empty_mapping() d.update(self.other.items()) self.assertEqual(d.items(), self.other.items()) # Iterator d = self._empty_mapping() d.update(self.other.iteritems()) self.assertEqual(d.items(), self.other.items()) # FIXME: Doesn't work with UserDict # self.assertRaises((TypeError, AttributeError), d.update, None) self.assertRaises((TypeError, AttributeError), d.update, 42) outerself = self class SimpleUserDict: def __init__(self): self.d = outerself.reference def keys(self): return self.d.keys() def __getitem__(self, i): return self.d[i] d.clear() d.update(SimpleUserDict()) i1 = d.items() i2 = self.reference.items() i1.sort() i2.sort() self.assertEqual(i1, i2) class Exc(Exception): pass d = self._empty_mapping() class FailingUserDict: def keys(self): raise Exc self.assertRaises(Exc, d.update, FailingUserDict()) d.clear() class FailingUserDict: def keys(self): class BogonIter: def __init__(self): self.i = 1 def __iter__(self): return self def next(self): if self.i: self.i = 0 return 'a' raise Exc return BogonIter() def __getitem__(self, key): return key self.assertRaises(Exc, d.update, FailingUserDict()) class FailingUserDict: def keys(self): class BogonIter: def __init__(self): self.i = ord('a') def __iter__(self): return self def next(self): if self.i <= ord('z'): rtn = chr(self.i) self.i += 1 return rtn raise StopIteration return BogonIter() def __getitem__(self, key): raise Exc self.assertRaises(Exc, d.update, FailingUserDict()) d = self._empty_mapping() class badseq(object): def __iter__(self): return self def next(self): raise Exc() self.assertRaises(Exc, d.update, badseq()) self.assertRaises(ValueError, d.update, [(1, 2, 3)]) # no test_fromkeys or test_copy as both os.environ and selves don't support it def test_get(self): d = self._empty_mapping() self.assert_(d.get(self.other.keys()[0]) is None) self.assertEqual(d.get(self.other.keys()[0], 3), 3) d = self.reference self.assert_(d.get(self.other.keys()[0]) is None) self.assertEqual(d.get(self.other.keys()[0], 3), 3) self.assertEqual(d.get(self.inmapping.keys()[0]), self.inmapping.values()[0]) self.assertEqual(d.get(self.inmapping.keys()[0], 3), self.inmapping.values()[0]) self.assertRaises(TypeError, d.get) self.assertRaises(TypeError, d.get, None, None, None) def test_setdefault(self): d = self._empty_mapping() self.assertRaises(TypeError, d.setdefault) def test_popitem(self): d = self._empty_mapping() self.assertRaises(KeyError, d.popitem) self.assertRaises(TypeError, d.popitem, 42) def test_pop(self): d = self._empty_mapping() k, v = self.inmapping.items()[0] d[k] = v self.assertRaises(KeyError, d.pop, self.other.keys()[0]) self.assertEqual(d.pop(k), v) self.assertEqual(len(d), 0) self.assertRaises(KeyError, d.pop, k) class TestMappingProtocol(BasicTestMappingProtocol): def test_constructor(self): BasicTestMappingProtocol.test_constructor(self) self.assert_(self._empty_mapping() is not self._empty_mapping()) self.assertEqual(self.type2test(x=1, y=2), {"x": 1, "y": 2}) def test_bool(self): BasicTestMappingProtocol.test_bool(self) self.assert_(not self._empty_mapping()) self.assert_(self._full_mapping({"x": "y"})) self.assert_(bool(self._empty_mapping()) is False) self.assert_(bool(self._full_mapping({"x": "y"})) is True) def test_keys(self): BasicTestMappingProtocol.test_keys(self) d = self._empty_mapping() self.assertEqual(d.keys(), []) d = self._full_mapping({'a': 1, 'b': 2}) k = d.keys() self.assert_('a' in k) self.assert_('b' in k) self.assert_('c' not in k) def test_values(self): BasicTestMappingProtocol.test_values(self) d = self._full_mapping({1:2}) self.assertEqual(d.values(), [2]) def test_items(self): BasicTestMappingProtocol.test_items(self) d = self._full_mapping({1:2}) self.assertEqual(d.items(), [(1, 2)]) def test_has_key(self): d = self._empty_mapping() self.assert_(not d.has_key('a')) d = self._full_mapping({'a': 1, 'b': 2}) k = d.keys() k.sort() self.assertEqual(k, ['a', 'b']) self.assertRaises(TypeError, d.has_key) def test_contains(self): d = self._empty_mapping() self.assert_(not ('a' in d)) self.assert_('a' not in d) d = self._full_mapping({'a': 1, 'b': 2}) self.assert_('a' in d) self.assert_('b' in d) self.assert_('c' not in d) self.assertRaises(TypeError, d.__contains__) def test_len(self): BasicTestMappingProtocol.test_len(self) d = self._full_mapping({'a': 1, 'b': 2}) self.assertEqual(len(d), 2) def test_getitem(self): BasicTestMappingProtocol.test_getitem(self) d = self._full_mapping({'a': 1, 'b': 2}) self.assertEqual(d['a'], 1) self.assertEqual(d['b'], 2) d['c'] = 3 d['a'] = 4 self.assertEqual(d['c'], 3) self.assertEqual(d['a'], 4) del d['b'] self.assertEqual(d, {'a': 4, 'c': 3}) self.assertRaises(TypeError, d.__getitem__) def test_clear(self): d = self._full_mapping({1:1, 2:2, 3:3}) d.clear() self.assertEqual(d, {}) self.assertRaises(TypeError, d.clear, None) def test_update(self): BasicTestMappingProtocol.test_update(self) # mapping argument d = self._empty_mapping() d.update({1:100}) d.update({2:20}) d.update({1:1, 2:2, 3:3}) self.assertEqual(d, {1:1, 2:2, 3:3}) # no argument d.update() self.assertEqual(d, {1:1, 2:2, 3:3}) # keyword arguments d = self._empty_mapping() d.update(x=100) d.update(y=20) d.update(x=1, y=2, z=3) self.assertEqual(d, {"x":1, "y":2, "z":3}) # item sequence d = self._empty_mapping() d.update([("x", 100), ("y", 20)]) self.assertEqual(d, {"x":100, "y":20}) # Both item sequence and keyword arguments d = self._empty_mapping() d.update([("x", 100), ("y", 20)], x=1, y=2) self.assertEqual(d, {"x":1, "y":2}) # iterator d = self._full_mapping({1:3, 2:4}) d.update(self._full_mapping({1:2, 3:4, 5:6}).iteritems()) self.assertEqual(d, {1:2, 2:4, 3:4, 5:6}) class SimpleUserDict: def __init__(self): self.d = {1:1, 2:2, 3:3} def keys(self): return self.d.keys() def __getitem__(self, i): return self.d[i] d.clear() d.update(SimpleUserDict()) self.assertEqual(d, {1:1, 2:2, 3:3}) def test_fromkeys(self): self.assertEqual(self.type2test.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) d = self._empty_mapping() self.assert_(not(d.fromkeys('abc') is d)) self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0}) self.assertEqual(d.fromkeys([]), {}) def g(): yield 1 self.assertEqual(d.fromkeys(g()), {1:None}) self.assertRaises(TypeError, {}.fromkeys, 3) class dictlike(self.type2test): pass self.assertEqual(dictlike.fromkeys('a'), {'a':None}) self.assertEqual(dictlike().fromkeys('a'), {'a':None}) self.assert_(dictlike.fromkeys('a').__class__ is dictlike) self.assert_(dictlike().fromkeys('a').__class__ is dictlike) # FIXME: the following won't work with UserDict, because it's an old style class # self.assert_(type(dictlike.fromkeys('a')) is dictlike) class mydict(self.type2test): def __new__(cls): return UserDict.UserDict() ud = mydict.fromkeys('ab') self.assertEqual(ud, {'a':None, 'b':None}) # FIXME: the following won't work with UserDict, because it's an old style class # self.assert_(isinstance(ud, UserDict.UserDict)) self.assertRaises(TypeError, dict.fromkeys) class Exc(Exception): pass class baddict1(self.type2test): def __init__(self): raise Exc() self.assertRaises(Exc, baddict1.fromkeys, [1]) class BadSeq(object): def __iter__(self): return self def next(self): raise Exc() self.assertRaises(Exc, self.type2test.fromkeys, BadSeq()) class baddict2(self.type2test): def __setitem__(self, key, value): raise Exc() self.assertRaises(Exc, baddict2.fromkeys, [1]) def test_copy(self): d = self._full_mapping({1:1, 2:2, 3:3}) self.assertEqual(d.copy(), {1:1, 2:2, 3:3}) d = self._empty_mapping() self.assertEqual(d.copy(), d) self.assert_(isinstance(d.copy(), d.__class__)) self.assertRaises(TypeError, d.copy, None) def test_get(self): BasicTestMappingProtocol.test_get(self) d = self._empty_mapping() self.assert_(d.get('c') is None) self.assertEqual(d.get('c', 3), 3) d = self._full_mapping({'a' : 1, 'b' : 2}) self.assert_(d.get('c') is None) self.assertEqual(d.get('c', 3), 3) self.assertEqual(d.get('a'), 1) self.assertEqual(d.get('a', 3), 1) def test_setdefault(self): BasicTestMappingProtocol.test_setdefault(self) d = self._empty_mapping() self.assert_(d.setdefault('key0') is None) d.setdefault('key0', []) self.assert_(d.setdefault('key0') is None) d.setdefault('key', []).append(3) self.assertEqual(d['key'][0], 3) d.setdefault('key', []).append(4) self.assertEqual(len(d['key']), 2) def test_popitem(self): BasicTestMappingProtocol.test_popitem(self) for copymode in -1, +1: # -1: b has same structure as a # +1: b is a.copy() for log2size in range(12): size = 2**log2size a = self._empty_mapping() b = self._empty_mapping() for i in range(size): a[repr(i)] = i if copymode < 0: b[repr(i)] = i if copymode > 0: b = a.copy() for i in range(size): ka, va = ta = a.popitem() self.assertEqual(va, int(ka)) kb, vb = tb = b.popitem() self.assertEqual(vb, int(kb)) self.assert_(not(copymode < 0 and ta != tb)) self.assert_(not a) self.assert_(not b) def test_pop(self): BasicTestMappingProtocol.test_pop(self) # Tests for pop with specified key d = self._empty_mapping() k, v = 'abc', 'def' # verify longs/ints get same value when key > 32 bits (for 64-bit archs) # see SF bug #689659 x = 4503599627370496L y = 4503599627370496 h = self._full_mapping({x: 'anything', y: 'something else'}) self.assertEqual(h[x], h[y]) self.assertEqual(d.pop(k, v), v) d[k] = v self.assertEqual(d.pop(k, 1), v) class TestHashMappingProtocol(TestMappingProtocol): def test_getitem(self): TestMappingProtocol.test_getitem(self) class Exc(Exception): pass class BadEq(object): def __eq__(self, other): raise Exc() d = self._empty_mapping() d[BadEq()] = 42 self.assertRaises(KeyError, d.__getitem__, 23) class BadHash(object): fail = False def __hash__(self): if self.fail: raise Exc() else: return 42 d = self._empty_mapping() x = BadHash() d[x] = 42 x.fail = True self.assertRaises(Exc, d.__getitem__, x) def test_fromkeys(self): TestMappingProtocol.test_fromkeys(self) class mydict(self.type2test): def __new__(cls): return UserDict.UserDict() ud = mydict.fromkeys('ab') self.assertEqual(ud, {'a':None, 'b':None}) self.assert_(isinstance(ud, UserDict.UserDict)) def test_pop(self): TestMappingProtocol.test_pop(self) class Exc(Exception): pass class BadHash(object): fail = False def __hash__(self): if self.fail: raise Exc() else: return 42 d = self._empty_mapping() x = BadHash() d[x] = 42 x.fail = True self.assertRaises(Exc, d.pop, x) def test_mutatingiteration(self): d = self._empty_mapping() d[1] = 1 try: for i in d: d[i+1] = 1 except RuntimeError: pass else: self.fail("changing dict size during iteration doesn't raise Error") def test_repr(self): d = self._empty_mapping() self.assertEqual(repr(d), '{}') d[1] = 2 self.assertEqual(repr(d), '{1: 2}') d = self._empty_mapping() d[1] = d self.assertEqual(repr(d), '{1: {...}}') class Exc(Exception): pass class BadRepr(object): def __repr__(self): raise Exc() d = self._full_mapping({1: BadRepr()}) self.assertRaises(Exc, repr, d) def test_le(self): self.assert_(not (self._empty_mapping() < self._empty_mapping())) self.assert_(not (self._full_mapping({1: 2}) < self._full_mapping({1L: 2L}))) class Exc(Exception): pass class BadCmp(object): def __eq__(self, other): raise Exc() d1 = self._full_mapping({BadCmp(): 1}) d2 = self._full_mapping({1: 1}) try: d1 < d2 except Exc: pass else: self.fail("< didn't raise Exc") def test_setdefault(self): TestMappingProtocol.test_setdefault(self) class Exc(Exception): pass class BadHash(object): fail = False def __hash__(self): if self.fail: raise Exc() else: return 42 d = self._empty_mapping() x = BadHash() d[x] = 42 x.fail = True self.assertRaises(Exc, d.setdefault, x, [])
gpl-3.0
kkirsche/trigger
trigger/packages/peewee.py
13
81973
""" peewee - a small, expressive ORM Source: https://raw.github.com/coleifer/peewee/2.1.4/peewee.py License: BSD Integrated into Trigger 2013-09-12 """ # (\ # ( \ /(o)\ caw! # ( \/ ()/ /) # ( `;.))'".) # `(/////.-' # =====))=))===() # ///' # // # ' from __future__ import with_statement import datetime import decimal import logging import operator import re import sys import threading from collections import deque from collections import namedtuple from copy import deepcopy from inspect import isclass __all__ = [ 'BigIntegerField', 'BlobField', 'BooleanField', 'CharField', 'Clause', 'CompositeKey', 'DateField', 'DateTimeField', 'DecimalField', 'DoesNotExist', 'DoubleField', 'DQ', 'Entity', 'Field', 'FloatField', 'fn', 'ForeignKeyField', 'ImproperlyConfigured', 'IntegerField', 'JOIN_FULL', 'JOIN_INNER', 'JOIN_LEFT_OUTER', 'Model', 'MySQLDatabase', 'PostgresqlDatabase', 'prefetch', 'PrimaryKeyField', 'R', 'SqliteDatabase', 'TextField', 'TimeField', ] # Python 2/3 compat def with_metaclass(meta, base=object): return meta("NewBase", (base,), {}) PY3 = sys.version_info[0] == 3 if PY3: import builtins from collections import Callable from functools import reduce callable = lambda c: isinstance(c, Callable) unicode_type = str string_type = bytes basestring = str print_ = getattr(builtins, 'print') binary_construct = lambda s: bytes(s.encode('raw_unicode_escape')) else: unicode_type = unicode string_type = basestring binary_construct = buffer def print_(s): sys.stdout.write(s) sys.stdout.write('\n') # DB libraries try: import sqlite3 except ImportError: sqlite3 = None try: import psycopg2 except ImportError: psycopg2 = None try: import MySQLdb as mysql except ImportError: try: import pymysql as mysql except ImportError: mysql = None class ImproperlyConfigured(Exception): pass if sqlite3 is None and psycopg2 is None and mysql is None: raise ImproperlyConfigured('Either sqlite3, psycopg2 or MySQLdb must be ' 'installed') if sqlite3: sqlite3.register_adapter(decimal.Decimal, str) sqlite3.register_adapter(datetime.date, str) sqlite3.register_adapter(datetime.time, str) SQLITE_DT_FORMATS = ( '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d', '%H:%M:%S', '%H:%M:%S.%f', '%H:%M') DT_PARTS = ['year', 'month', 'day', 'hour', 'minute', 'second'] DT_LOOKUPS = set(DT_PARTS) def _sqlite_date_part(lookup_type, datetime_string): assert lookup_type in DT_LOOKUPS dt = format_date_time(datetime_string, SQLITE_DT_FORMATS) return getattr(dt, lookup_type) if psycopg2: import psycopg2.extensions psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) # Peewee logger = logging.getLogger('peewee') OP_AND = 'and' OP_OR = 'or' OP_ADD = '+' OP_SUB = '-' OP_MUL = '*' OP_DIV = '/' OP_BIN_AND = '&' OP_BIN_OR = '|' OP_XOR = '^' OP_MOD = '%' OP_EQ = '=' OP_LT = '<' OP_LTE = '<=' OP_GT = '>' OP_GTE = '>=' OP_NE = '!=' OP_IN = 'in' OP_IS = 'is' OP_LIKE = 'like' OP_ILIKE = 'ilike' OP_BETWEEN = 'between' DJANGO_MAP = { 'eq': OP_EQ, 'lt': OP_LT, 'lte': OP_LTE, 'gt': OP_GT, 'gte': OP_GTE, 'ne': OP_NE, 'in': OP_IN, 'is': OP_IS, 'like': OP_LIKE, 'ilike': OP_ILIKE, } JOIN_INNER = 'inner' JOIN_LEFT_OUTER = 'left outer' JOIN_FULL = 'full' def dict_update(orig, extra): new = {} new.update(orig) new.update(extra) return new def returns_clone(func): def inner(self, *args, **kwargs): clone = self.clone() func(clone, *args, **kwargs) return clone inner.call_local = func return inner def not_allowed(fn): def inner(self, *args, **kwargs): raise NotImplementedError('%s is not allowed on %s instances' % ( fn, type(self).__name__)) return inner class Node(object): def __init__(self): self._negated = False self._alias = None self._ordering = None def clone_base(self): return type(self)() def clone(self): inst = self.clone_base() inst._negated = self._negated inst._alias = self._alias return inst @returns_clone def __invert__(self): self._negated = not self._negated @returns_clone def alias(self, a=None): self._alias = a @returns_clone def asc(self): self._ordering = 'ASC' @returns_clone def desc(self): self._ordering = 'DESC' def _e(op, inv=False): def inner(self, rhs): if inv: return Expression(rhs, op, self) return Expression(self, op, rhs) return inner __and__ = _e(OP_AND) __or__ = _e(OP_OR) __add__ = _e(OP_ADD) __sub__ = _e(OP_SUB) __mul__ = _e(OP_MUL) __div__ = _e(OP_DIV) __xor__ = _e(OP_XOR) __radd__ = _e(OP_ADD, inv=True) __rsub__ = _e(OP_SUB, inv=True) __rmul__ = _e(OP_MUL, inv=True) __rdiv__ = _e(OP_DIV, inv=True) __rand__ = _e(OP_AND, inv=True) __ror__ = _e(OP_OR, inv=True) __rxor__ = _e(OP_XOR, inv=True) __eq__ = _e(OP_EQ) __lt__ = _e(OP_LT) __le__ = _e(OP_LTE) __gt__ = _e(OP_GT) __ge__ = _e(OP_GTE) __ne__ = _e(OP_NE) __lshift__ = _e(OP_IN) __rshift__ = _e(OP_IS) __mod__ = _e(OP_LIKE) __pow__ = _e(OP_ILIKE) bin_and = _e(OP_BIN_AND) bin_or = _e(OP_BIN_OR) def between(self, low, high): return Expression(self, OP_BETWEEN, Clause(low, R('AND'), high)) class Expression(Node): def __init__(self, lhs, op, rhs): super(Expression, self).__init__() self.lhs = lhs self.op = op self.rhs = rhs def clone_base(self): return Expression(self.lhs, self.op, self.rhs) class DQ(Node): def __init__(self, **query): super(DQ, self).__init__() self.query = query def clone_base(self): return DQ(**self.query) class Param(Node): def __init__(self, value): self.value = value super(Param, self).__init__() def clone_base(self): return Param(self.value) class R(Node): def __init__(self, value): self.value = value super(R, self).__init__() def clone_base(self): return R(self.value) class Func(Node): def __init__(self, name, *nodes): self.name = name self.nodes = nodes super(Func, self).__init__() def clone_base(self): return Func(self.name, *self.nodes) def __getattr__(self, attr): def dec(*args, **kwargs): return Func(attr, *args, **kwargs) return dec fn = Func(None) class Clause(Node): def __init__(self, *nodes): super(Clause, self).__init__() self.nodes = nodes def clone_base(self): return Clause(*self.nodes) class Entity(Node): def __init__(self, *path): super(Entity, self).__init__() self.path = path def clone_base(self): return Entity(*self.path) def __getattr__(self, attr): return Entity(*self.path + (attr,)) Join = namedtuple('Join', ('model_class', 'join_type', 'on')) class FieldDescriptor(object): def __init__(self, field): self.field = field self.att_name = self.field.name def __get__(self, instance, instance_type=None): if instance is not None: return instance._data.get(self.att_name) return self.field def __set__(self, instance, value): instance._data[self.att_name] = value class Field(Node): _field_counter = 0 _order = 0 db_field = 'unknown' template = '%(column_type)s' template_extra = '' def __init__(self, null=False, index=False, unique=False, verbose_name=None, help_text=None, db_column=None, default=None, choices=None, primary_key=False, sequence=None, *args, **kwargs): self.null = null self.index = index self.unique = unique self.verbose_name = verbose_name self.help_text = help_text self.db_column = db_column self.default = default self.choices = choices self.primary_key = primary_key self.sequence = sequence self.attributes = self.field_attributes() self.attributes.update(kwargs) Field._field_counter += 1 self._order = Field._field_counter self._is_bound = False super(Field, self).__init__() def clone_base(self, **kwargs): inst = type(self)( null=self.null, index=self.index, unique=self.unique, verbose_name=self.verbose_name, help_text=self.help_text, db_column=self.db_column, default=self.default, choices=self.choices, primary_key=self.primary_key, sequence=self.sequence, **kwargs ) inst.attributes = dict(self.attributes) if self._is_bound: inst.name = self.name inst.model_class = self.model_class return inst def add_to_class(self, model_class, name): self.name = name self.model_class = model_class self.db_column = self.db_column or self.name if not self.verbose_name: self.verbose_name = re.sub('_+', ' ', name).title() model_class._meta.fields[self.name] = self model_class._meta.columns[self.db_column] = self setattr(model_class, name, FieldDescriptor(self)) self._is_bound = True def get_database(self): return self.model_class._meta.database def field_attributes(self): return {} def get_db_field(self): return self.db_field def get_template(self): return self.template def coerce(self, value): return value def db_value(self, value): return value if value is None else self.coerce(value) def python_value(self, value): return value if value is None else self.coerce(value) def __hash__(self): return hash(self.name + '.' + self.model_class.__name__) class IntegerField(Field): db_field = 'int' coerce = int class BigIntegerField(IntegerField): db_field = 'bigint' class PrimaryKeyField(IntegerField): db_field = 'primary_key' def __init__(self, *args, **kwargs): kwargs['primary_key'] = True super(PrimaryKeyField, self).__init__(*args, **kwargs) class FloatField(Field): db_field = 'float' coerce = float class DoubleField(FloatField): db_field = 'double' class DecimalField(Field): db_field = 'decimal' template = '%(column_type)s(%(max_digits)d, %(decimal_places)d)' def field_attributes(self): return { 'max_digits': 10, 'decimal_places': 5, 'auto_round': False, 'rounding': decimal.DefaultContext.rounding, } def db_value(self, value): D = decimal.Decimal if not value: return value if value is None else D(0) if self.attributes['auto_round']: exp = D(10) ** (-self.attributes['decimal_places']) rounding = self.attributes['rounding'] return D(str(value)).quantize(exp, rounding=rounding) return value def python_value(self, value): if value is not None: if isinstance(value, decimal.Decimal): return value return decimal.Decimal(str(value)) def format_unicode(s, encoding='utf-8'): if isinstance(s, unicode_type): return s elif isinstance(s, string_type): return s.decode(encoding) return unicode_type(s) class CharField(Field): db_field = 'string' template = '%(column_type)s(%(max_length)s)' def field_attributes(self): return {'max_length': 255} def coerce(self, value): return format_unicode(value or '') class TextField(Field): db_field = 'text' def coerce(self, value): return format_unicode(value or '') class BlobField(Field): db_field = 'blob' def db_value(self, value): if isinstance(value, basestring): return binary_construct(value) return value def format_date_time(value, formats, post_process=None): post_process = post_process or (lambda x: x) for fmt in formats: try: return post_process(datetime.datetime.strptime(value, fmt)) except ValueError: pass return value def _date_part(date_part): def dec(self): return self.model_class._meta.database.extract_date(date_part, self) return dec class DateTimeField(Field): db_field = 'datetime' def field_attributes(self): return { 'formats': [ '%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d', ] } def python_value(self, value): if value and isinstance(value, basestring): return format_date_time(value, self.attributes['formats']) return value year = property(_date_part('year')) month = property(_date_part('month')) day = property(_date_part('day')) hour = property(_date_part('hour')) minute = property(_date_part('minute')) second = property(_date_part('second')) class DateField(Field): db_field = 'date' def field_attributes(self): return { 'formats': [ '%Y-%m-%d', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M:%S.%f', ] } def python_value(self, value): if value and isinstance(value, basestring): pp = lambda x: x.date() return format_date_time(value, self.attributes['formats'], pp) elif value and isinstance(value, datetime.datetime): return value.date() return value year = property(_date_part('year')) month = property(_date_part('month')) day = property(_date_part('day')) class TimeField(Field): db_field = 'time' def field_attributes(self): return { 'formats': [ '%H:%M:%S.%f', '%H:%M:%S', '%H:%M', '%Y-%m-%d %H:%M:%S.%f', '%Y-%m-%d %H:%M:%S', ] } def python_value(self, value): if value and isinstance(value, basestring): pp = lambda x: x.time() return format_date_time(value, self.attributes['formats'], pp) elif value and isinstance(value, datetime.datetime): return value.time() return value hour = property(_date_part('hour')) minute = property(_date_part('minute')) second = property(_date_part('second')) class BooleanField(Field): db_field = 'bool' coerce = bool class RelationDescriptor(FieldDescriptor): def __init__(self, field, rel_model): self.rel_model = rel_model super(RelationDescriptor, self).__init__(field) def get_object_or_id(self, instance): rel_id = instance._data.get(self.att_name) if rel_id is not None or self.att_name in instance._obj_cache: if self.att_name not in instance._obj_cache: obj = self.rel_model.get( self.rel_model._meta.primary_key == rel_id) instance._obj_cache[self.att_name] = obj return instance._obj_cache[self.att_name] elif not self.field.null: raise self.rel_model.DoesNotExist return rel_id def __get__(self, instance, instance_type=None): if instance is not None: return self.get_object_or_id(instance) return self.field def __set__(self, instance, value): if isinstance(value, self.rel_model): instance._data[self.att_name] = value.get_id() instance._obj_cache[self.att_name] = value else: instance._data[self.att_name] = value class ReverseRelationDescriptor(object): def __init__(self, field): self.field = field self.rel_model = field.model_class def __get__(self, instance, instance_type=None): if instance is not None: return self.rel_model.select().where(self.field==instance.get_id()) return self class ForeignKeyField(IntegerField): def __init__(self, rel_model, null=False, related_name=None, cascade=False, extra=None, *args, **kwargs): self.rel_model = rel_model self._related_name = related_name self.cascade = cascade self.extra = extra kwargs.update(dict( cascade='ON DELETE CASCADE' if self.cascade else '', extra=extra or '')) super(ForeignKeyField, self).__init__(null=null, *args, **kwargs) def clone_base(self): return super(ForeignKeyField, self).clone_base( rel_model=self.rel_model, related_name=self.related_name, cascade=self.cascade, extra=self.extra) def add_to_class(self, model_class, name): self.name = name self.model_class = model_class self.db_column = self.db_column or '%s_id' % self.name if not self.verbose_name: self.verbose_name = re.sub('_+', ' ', name).title() model_class._meta.fields[self.name] = self model_class._meta.columns[self.db_column] = self model_name = model_class._meta.name self.related_name = self._related_name or '%s_set' % (model_name) if self.rel_model == 'self': self.rel_model = self.model_class if self.related_name in self.rel_model._meta.fields: error = ('Foreign key: %s.%s related name "%s" collision with ' 'field of the same name.') params = self.model_class._meta.name, self.name, self.related_name raise AttributeError(error % params) if self.related_name in self.rel_model._meta.reverse_rel: error = ('Foreign key: %s.%s related name "%s" collision with ' 'foreign key using same related_name.') params = self.model_class._meta.name, self.name, self.related_name raise AttributeError(error % params) fk_descriptor = RelationDescriptor(self, self.rel_model) backref_descriptor = ReverseRelationDescriptor(self) setattr(model_class, name, fk_descriptor) setattr(self.rel_model, self.related_name, backref_descriptor) self._is_bound = True model_class._meta.rel[self.name] = self self.rel_model._meta.reverse_rel[self.related_name] = self def get_db_field(self): to_pk = self.rel_model._meta.primary_key if not isinstance(to_pk, PrimaryKeyField): return to_pk.get_db_field() return super(ForeignKeyField, self).get_db_field() def coerce(self, value): return self.rel_model._meta.primary_key.coerce(value) def db_value(self, value): if isinstance(value, self.rel_model): value = value.get_id() return self.rel_model._meta.primary_key.db_value(value) class CompositeKey(object): sequence = None def __init__(self, *fields): self.fields = fields def add_to_class(self, model_class, name): self.name = name setattr(model_class, name, self) def __get__(self, instance, instance_type=None): if instance is not None: return [getattr(instance, field) for field in self.fields] return self def __set__(self, instance, value): pass class QueryCompiler(object): field_map = { 'bigint': 'BIGINT', 'blob': 'BLOB', 'bool': 'SMALLINT', 'date': 'DATE', 'datetime': 'DATETIME', 'decimal': 'DECIMAL', 'double': 'REAL', 'float': 'REAL', 'int': 'INTEGER', 'primary_key': 'INTEGER', 'string': 'VARCHAR', 'text': 'TEXT', 'time': 'TIME', } op_map = { OP_EQ: '=', OP_LT: '<', OP_LTE: '<=', OP_GT: '>', OP_GTE: '>=', OP_NE: '!=', OP_IN: 'IN', OP_IS: 'IS', OP_BIN_AND: '&', OP_BIN_OR: '|', OP_LIKE: 'LIKE', OP_ILIKE: 'ILIKE', OP_BETWEEN: 'BETWEEN', OP_ADD: '+', OP_SUB: '-', OP_MUL: '*', OP_DIV: '/', OP_XOR: '#', OP_AND: 'AND', OP_OR: 'OR', OP_MOD: '%', } join_map = { JOIN_INNER: 'INNER', JOIN_LEFT_OUTER: 'LEFT OUTER', JOIN_FULL: 'FULL', } def __init__(self, quote_char='"', interpolation='?', field_overrides=None, op_overrides=None): self.quote_char = quote_char self.interpolation = interpolation self._field_map = dict_update(self.field_map, field_overrides or {}) self._op_map = dict_update(self.op_map, op_overrides or {}) def quote(self, s): return s.join((self.quote_char, self.quote_char)) def get_field(self, f): return self._field_map[f] def get_op(self, q): return self._op_map[q] def _max_alias(self, am): max_alias = 0 if am: for a in am.values(): i = int(a.lstrip('t')) if i > max_alias: max_alias = i return max_alias + 1 def _parse(self, node, alias_map, conv): # By default treat the incoming node as a raw value that should be # parameterized. sql = self.interpolation params = [node] unknown = False if isinstance(node, Expression): if isinstance(node.lhs, Field): conv = node.lhs lhs, lparams = self.parse_node(node.lhs, alias_map, conv) rhs, rparams = self.parse_node(node.rhs, alias_map, conv) sql = '(%s %s %s)' % (lhs, self.get_op(node.op), rhs) params = lparams + rparams elif isinstance(node, Field): sql = self.quote(node.db_column) if alias_map and node.model_class in alias_map: sql = '.'.join((alias_map[node.model_class], sql)) params = [] elif isinstance(node, Func): sql, params = self.parse_node_list(node.nodes, alias_map, conv) sql = '%s(%s)' % (node.name, sql) elif isinstance(node, Clause): sql, params = self.parse_node_list( node.nodes, alias_map, conv, ' ') elif isinstance(node, Param): params = [node.value] elif isinstance(node, R): sql = node.value params = [] elif isinstance(node, SelectQuery): max_alias = self._max_alias(alias_map) alias_copy = alias_map and alias_map.copy() or None clone = node.clone() if not node._explicit_selection: clone._select = (clone.model_class._meta.primary_key,) sub, params = self.generate_select(clone, max_alias, alias_copy) sql = '(%s)' % sub elif isinstance(node, (list, tuple)): sql, params = self.parse_node_list(node, alias_map, conv) sql = '(%s)' % sql elif isinstance(node, Model): sql = self.interpolation params = [node.get_id()] elif isinstance(node, Entity): sql = '.'.join(map(self.quote, node.path)) params = [] elif isclass(node) and issubclass(node, Model): sql = self.quote(node._meta.db_table) params = [] else: unknown = True return sql, params, unknown def parse_node(self, node, alias_map=None, conv=None): sql, params, unknown = self._parse(node, alias_map, conv) if unknown and conv and params: params = [conv.db_value(i) for i in params] if isinstance(node, Node): if node._negated: sql = 'NOT %s' % sql if node._alias: sql = ' '.join((sql, 'AS', node._alias)) if node._ordering: sql = ' '.join((sql, node._ordering)) return sql, params def parse_node_list(self, nodes, alias_map, conv=None, glue=', '): sql = [] params = [] for node in nodes: node_sql, node_params = self.parse_node(node, alias_map, conv) sql.append(node_sql) params.extend(node_params) return glue.join(sql), params def parse_field_dict(self, d): sets, params = [], [] for field, value in d.items(): field_sql, _ = self.parse_node(field) # because we don't know whether to call db_value or parse_node # first, we'd prefer to call parse_node since its more general, but # it does special things with lists -- it treats them as if it were # buliding up an IN query. for some things we don't want that, so # here, if the node is *not* a special object, we'll pass thru # parse_node and let db_value handle it if not isinstance(value, (Node, Model, Query)): value = Param(value) # passthru to the field's db_value func val_sql, val_params = self.parse_node(value) val_params = [field.db_value(vp) for vp in val_params] sets.append((field_sql, val_sql)) params.extend(val_params) return sets, params def parse_query_node(self, node, alias_map): if node is not None: return self.parse_node(node, alias_map) return '', [] def calculate_alias_map(self, query, start=1): alias_map = {query.model_class: 't%s' % start} for model, joins in query._joins.items(): if model not in alias_map: start += 1 alias_map[model] = 't%s' % start for join in joins: if join.model_class not in alias_map: start += 1 alias_map[join.model_class] = 't%s' % start return alias_map def generate_joins(self, joins, model_class, alias_map): sql = [] params = [] seen = set() q = [model_class] while q: curr = q.pop() if curr not in joins or curr in seen: continue seen.add(curr) for join in joins[curr]: src = curr dest = join.model_class if isinstance(join.on, Expression): # Clear any alias on the join expression. join_node = join.on.clone().alias() else: field = src._meta.rel_for_model(dest, join.on) if field: left_field = field right_field = dest._meta.primary_key else: field = dest._meta.rel_for_model(src, join.on) left_field = src._meta.primary_key right_field = field join_node = (left_field == right_field) join_type = join.join_type or JOIN_INNER join_sql, join_params = self.parse_node(join_node, alias_map) sql.append('%s JOIN %s AS %s ON %s' % ( self.join_map[join_type], self.quote(dest._meta.db_table), alias_map[dest], join_sql)) params.extend(join_params) q.append(dest) return sql, params def generate_select(self, query, start=1, alias_map=None): model = query.model_class db = model._meta.database alias_map = alias_map or {} alias_map.update(self.calculate_alias_map(query, start)) parts = ['SELECT'] params = [] if query._distinct: parts.append('DISTINCT') select, s_params = self.parse_node_list(query._select, alias_map) parts.append(select) params.extend(s_params) parts.append('FROM %s AS %s' % ( self.quote(model._meta.db_table), alias_map[model])) joins, j_params = self.generate_joins(query._joins, model, alias_map) if joins: parts.append(' '.join(joins)) params.extend(j_params) where, w_params = self.parse_query_node(query._where, alias_map) if where: parts.append('WHERE %s' % where) params.extend(w_params) if query._group_by: group, g_params = self.parse_node_list(query._group_by, alias_map) parts.append('GROUP BY %s' % group) params.extend(g_params) if query._having: having, h_params = self.parse_query_node(query._having, alias_map) parts.append('HAVING %s' % having) params.extend(h_params) if query._order_by: order, o_params = self.parse_node_list(query._order_by, alias_map) parts.append('ORDER BY %s' % order) params.extend(o_params) if query._limit or (query._offset and db.limit_max): limit = query._limit or db.limit_max parts.append('LIMIT %s' % limit) if query._offset: parts.append('OFFSET %s' % query._offset) if query._for_update: parts.append('FOR UPDATE') return ' '.join(parts), params def generate_update(self, query): model = query.model_class parts = ['UPDATE %s SET' % self.quote(model._meta.db_table)] sets, params = self.parse_field_dict(query._update) parts.append(', '.join('%s=%s' % (f, v) for f, v in sets)) where, w_params = self.parse_query_node(query._where, None) if where: parts.append('WHERE %s' % where) params.extend(w_params) return ' '.join(parts), params def generate_insert(self, query): model = query.model_class parts = ['INSERT INTO %s' % self.quote(model._meta.db_table)] sets, params = self.parse_field_dict(query._insert) if sets: parts.append('(%s)' % ', '.join(s[0] for s in sets)) parts.append('VALUES (%s)' % ', '.join(s[1] for s in sets)) return ' '.join(parts), params def generate_delete(self, query): model = query.model_class parts = ['DELETE FROM %s' % self.quote(model._meta.db_table)] params = [] where, w_params = self.parse_query_node(query._where, None) if where: parts.append('WHERE %s' % where) params.extend(w_params) return ' '.join(parts), params def field_sql(self, field): attrs = field.attributes attrs['column_type'] = self.get_field(field.get_db_field()) template = field.get_template() if isinstance(field, ForeignKeyField): to_pk = field.rel_model._meta.primary_key if not isinstance(to_pk, PrimaryKeyField): template = to_pk.get_template() attrs.update(to_pk.attributes) parts = [self.quote(field.db_column), template] if not field.null: parts.append('NOT NULL') if field.primary_key: parts.append('PRIMARY KEY') if field.template_extra: parts.append(field.template_extra) if isinstance(field, ForeignKeyField): ref_mc = ( self.quote(field.rel_model._meta.db_table), self.quote(field.rel_model._meta.primary_key.db_column)) parts.append('REFERENCES %s (%s)' % ref_mc) parts.append('%(cascade)s%(extra)s') elif field.sequence: parts.append("DEFAULT NEXTVAL('%s')" % self.quote(field.sequence)) return ' '.join(p % attrs for p in parts) def create_table_sql(self, model_class, safe=False): parts = ['CREATE TABLE'] if safe: parts.append('IF NOT EXISTS') meta = model_class._meta parts.append(self.quote(meta.db_table)) columns = map(self.field_sql, meta.get_fields()) if isinstance(meta.primary_key, CompositeKey): pk_cols = map(self.quote, ( meta.fields[f].db_column for f in meta.primary_key.fields)) columns.append('PRIMARY KEY (%s)' % ', '.join(pk_cols)) parts.append('(%s)' % ', '.join(columns)) return parts def create_table(self, model_class, safe=False): return ' '.join(self.create_table_sql(model_class, safe)) def drop_table(self, model_class, fail_silently=False, cascade=False): parts = ['DROP TABLE'] if fail_silently: parts.append('IF EXISTS') parts.append(self.quote(model_class._meta.db_table)) if cascade: parts.append('CASCADE') return ' '.join(parts) def create_index_sql(self, model_class, fields, unique): tbl_name = model_class._meta.db_table colnames = [f.db_column for f in fields] parts = ['CREATE %s' % ('UNIQUE INDEX' if unique else 'INDEX')] parts.append(self.quote('%s_%s' % (tbl_name, '_'.join(colnames)))) parts.append('ON %s' % self.quote(tbl_name)) parts.append('(%s)' % ', '.join(map(self.quote, colnames))) return parts def create_index(self, model_class, fields, unique): return ' '.join(self.create_index_sql(model_class, fields, unique)) def create_sequence(self, sequence_name): return 'CREATE SEQUENCE %s;' % self.quote(sequence_name) def drop_sequence(self, sequence_name): return 'DROP SEQUENCE %s;' % self.quote(sequence_name) class QueryResultWrapper(object): """ Provides an iterator over the results of a raw Query, additionally doing two things: - converts rows from the database into python representations - ensures that multiple iterations do not result in multiple queries """ def __init__(self, model, cursor, meta=None): self.model = model self.cursor = cursor self.__ct = 0 self.__idx = 0 self._result_cache = [] self._populated = False self._initialized = False if meta is not None: self.column_meta, self.join_meta = meta else: self.column_meta = self.join_meta = None def __iter__(self): self.__idx = 0 if not self._populated: return self else: return iter(self._result_cache) def process_row(self, row): return row def iterate(self): row = self.cursor.fetchone() if not row: self._populated = True raise StopIteration elif not self._initialized: self.initialize(self.cursor.description) self._initialized = True return self.process_row(row) def iterator(self): while True: yield self.iterate() def next(self): if self.__idx < self.__ct: inst = self._result_cache[self.__idx] self.__idx += 1 return inst obj = self.iterate() self._result_cache.append(obj) self.__ct += 1 self.__idx += 1 return obj __next__ = next def fill_cache(self, n=None): n = n or float('Inf') if n < 0: raise ValueError('Negative values are not supported.') self.__idx = self.__ct while not self._populated and (n > self.__ct): try: self.next() except StopIteration: break class ExtQueryResultWrapper(QueryResultWrapper): def initialize(self, description): model = self.model conv = [] identity = lambda x: x for i in range(len(description)): column = description[i][0] func = identity if column in model._meta.columns: field_obj = model._meta.columns[column] column = field_obj.name func = field_obj.python_value elif self.column_meta is not None: select_column = self.column_meta[i] # Special-case handling aggregations. if (isinstance(select_column, Func) and isinstance(select_column.nodes[0], Field)): func = select_column.nodes[0].python_value conv.append((i, column, func)) self.conv = conv class TuplesQueryResultWrapper(ExtQueryResultWrapper): def process_row(self, row): return tuple([self.conv[i][2](col) for i, col in enumerate(row)]) class NaiveQueryResultWrapper(ExtQueryResultWrapper): def process_row(self, row): instance = self.model() for i, column, func in self.conv: setattr(instance, column, func(row[i])) instance.prepared() return instance class DictQueryResultWrapper(ExtQueryResultWrapper): def process_row(self, row): res = {} for i, column, func in self.conv: res[column] = func(row[i]) return res class ModelQueryResultWrapper(QueryResultWrapper): def initialize(self, description): column_map = [] join_map = [] models = set([self.model]) for i, node in enumerate(self.column_meta): attr = conv = None if isinstance(node, Field): if isinstance(node, FieldProxy): key = node._model_alias constructor = node.model else: key = constructor = node.model_class attr = node.name conv = node.python_value else: key = constructor = self.model if isinstance(node, Expression) and node._alias: attr = node._alias column_map.append((key, constructor, attr, conv)) models.add(key) joins = self.join_meta stack = [self.model] while stack: current = stack.pop() if current not in joins: continue for join in joins[current]: join_model = join.model_class if join_model in models: fk_field = current._meta.rel_for_model(join_model) if not fk_field: if isinstance(join.on, Expression): fk_name = join.on._alias or join.on.lhs.name else: # Patch the joined model using the name of the # database table. fk_name = join_model._meta.db_table else: fk_name = fk_field.name stack.append(join_model) join_map.append((current, fk_name, join_model)) self.column_map, self.join_map = column_map, join_map def process_row(self, row): collected = self.construct_instance(row) instances = self.follow_joins(collected) for i in instances: i.prepared() return instances[0] def construct_instance(self, row): collected_models = {} for i, (key, constructor, attr, conv) in enumerate(self.column_map): value = row[i] if key not in collected_models: collected_models[key] = constructor() instance = collected_models[key] if attr is None: attr = self.cursor.description[i][0] if conv is not None: value = conv(value) setattr(instance, attr, value) return collected_models def follow_joins(self, collected): prepared = [collected[self.model]] for (lhs, attr, rhs) in self.join_map: inst = collected[lhs] joined_inst = collected[rhs] if joined_inst.get_id() is None and attr in inst._data: joined_inst.set_id(inst._data[attr]) setattr(inst, attr, joined_inst) prepared.append(joined_inst) return prepared class Query(Node): require_commit = True def __init__(self, model_class): super(Query, self).__init__() self.model_class = model_class self.database = model_class._meta.database self._dirty = True self._query_ctx = model_class self._joins = {self.model_class: []} # adjacency graph self._where = None def __repr__(self): sql, params = self.sql() return '%s %s %s' % (self.model_class, sql, params) def clone(self): query = type(self)(self.model_class) return self._clone_attributes(query) def _clone_attributes(self, query): if self._where is not None: query._where = self._where.clone() query._joins = self._clone_joins() query._query_ctx = self._query_ctx return query def _clone_joins(self): return dict( (mc, list(j)) for mc, j in self._joins.items() ) def _build_tree(self, initial, expressions): reduced = reduce(operator.and_, expressions) if initial is None: return reduced return initial & reduced @returns_clone def where(self, *expressions): self._where = self._build_tree(self._where, expressions) @returns_clone def join(self, model_class, join_type=None, on=None): if not self._query_ctx._meta.rel_exists(model_class) and on is None: raise ValueError('No foreign key between %s and %s' % ( self._query_ctx, model_class, )) if on and isinstance(on, basestring): on = self._query_ctx._meta.fields[on] self._joins.setdefault(self._query_ctx, []) self._joins[self._query_ctx].append(Join(model_class, join_type, on)) self._query_ctx = model_class @returns_clone def switch(self, model_class=None): self._query_ctx = model_class or self.model_class def ensure_join(self, lm, rm, on=None): ctx = self._query_ctx for join in self._joins.get(lm, []): if join.model_class == rm: return self query = self.switch(lm).join(rm, on=on).switch(ctx) return query def convert_dict_to_node(self, qdict): accum = [] joins = [] relationship = (ForeignKeyField, ReverseRelationDescriptor) for key, value in sorted(qdict.items()): curr = self.model_class if '__' in key and key.rsplit('__', 1)[1] in DJANGO_MAP: key, op = key.rsplit('__', 1) op = DJANGO_MAP[op] else: op = OP_EQ for piece in key.split('__'): model_attr = getattr(curr, piece) if isinstance(model_attr, relationship): curr = model_attr.rel_model joins.append(model_attr) accum.append(Expression(model_attr, op, value)) return accum, joins def filter(self, *args, **kwargs): # normalize args and kwargs into a new expression dq_node = Node() if args: dq_node &= reduce(operator.and_, [a.clone() for a in args]) if kwargs: dq_node &= DQ(**kwargs) # dq_node should now be an Expression, lhs = Node(), rhs = ... q = deque([dq_node]) dq_joins = set() while q: curr = q.popleft() if not isinstance(curr, Expression): continue for side, piece in (('lhs', curr.lhs), ('rhs', curr.rhs)): if isinstance(piece, DQ): query, joins = self.convert_dict_to_node(piece.query) dq_joins.update(joins) expression = reduce(operator.and_, query) # Apply values from the DQ object. expression._negated = piece._negated expression._alias = piece._alias setattr(curr, side, expression) else: q.append(piece) dq_node = dq_node.rhs query = self.clone() for field in dq_joins: if isinstance(field, ForeignKeyField): lm, rm = field.model_class, field.rel_model field_obj = field elif isinstance(field, ReverseRelationDescriptor): lm, rm = field.field.rel_model, field.rel_model field_obj = field.field query = query.ensure_join(lm, rm, field_obj) return query.where(dq_node) def compiler(self): return self.database.compiler() def sql(self): raise NotImplementedError def _execute(self): sql, params = self.sql() return self.database.execute_sql(sql, params, self.require_commit) def execute(self): raise NotImplementedError def scalar(self, as_tuple=False, convert=False): if convert: row = self.tuples().first() else: row = self._execute().fetchone() if row and not as_tuple: return row[0] else: return row class RawQuery(Query): def __init__(self, model, query, *params): self._sql = query self._params = list(params) self._qr = None self._tuples = False self._dicts = False super(RawQuery, self).__init__(model) def clone(self): query = RawQuery(self.model_class, self._sql, *self._params) query._tuples = self._tuples query._dicts = self._dicts return query join = not_allowed('joining') where = not_allowed('where') switch = not_allowed('switch') @returns_clone def tuples(self, tuples=True): self._tuples = tuples @returns_clone def dicts(self, dicts=True): self._dicts = dicts def sql(self): return self._sql, self._params def execute(self): if self._qr is None: if self._tuples: ResultWrapper = TuplesQueryResultWrapper elif self._dicts: ResultWrapper = DictQueryResultWrapper else: ResultWrapper = NaiveQueryResultWrapper self._qr = ResultWrapper(self.model_class, self._execute(), None) return self._qr def __iter__(self): return iter(self.execute()) class SelectQuery(Query): def __init__(self, model_class, *selection): super(SelectQuery, self).__init__(model_class) self.require_commit = self.database.commit_select self._explicit_selection = len(selection) > 0 selection = selection or model_class._meta.get_fields() self._select = self._model_shorthand(selection) self._group_by = None self._having = None self._order_by = None self._limit = None self._offset = None self._distinct = False self._for_update = False self._naive = False self._tuples = False self._dicts = False self._alias = None self._qr = None def _clone_attributes(self, query): query = super(SelectQuery, self)._clone_attributes(query) query._explicit_selection = self._explicit_selection query._select = list(self._select) if self._group_by is not None: query._group_by = list(self._group_by) if self._having: query._having = self._having.clone() if self._order_by is not None: query._order_by = list(self._order_by) query._limit = self._limit query._offset = self._offset query._distinct = self._distinct query._for_update = self._for_update query._naive = self._naive query._tuples = self._tuples query._dicts = self._dicts query._alias = self._alias return query def _model_shorthand(self, args): accum = [] for arg in args: if isinstance(arg, Node): accum.append(arg) elif isinstance(arg, Query): accum.append(arg) elif isinstance(arg, ModelAlias): accum.extend(arg.get_proxy_fields()) elif isclass(arg) and issubclass(arg, Model): accum.extend(arg._meta.get_fields()) return accum @returns_clone def group_by(self, *args): self._group_by = self._model_shorthand(args) @returns_clone def having(self, *expressions): self._having = self._build_tree(self._having, expressions) @returns_clone def order_by(self, *args): self._order_by = list(args) @returns_clone def limit(self, lim): self._limit = lim @returns_clone def offset(self, off): self._offset = off @returns_clone def paginate(self, page, paginate_by=20): if page > 0: page -= 1 self._limit = paginate_by self._offset = page * paginate_by @returns_clone def distinct(self, is_distinct=True): self._distinct = is_distinct @returns_clone def for_update(self, for_update=True): self._for_update = for_update @returns_clone def naive(self, naive=True): self._naive = naive @returns_clone def tuples(self, tuples=True): self._tuples = tuples @returns_clone def dicts(self, dicts=True): self._dicts = dicts @returns_clone def alias(self, alias=None): self._alias = alias def annotate(self, rel_model, annotation=None): if annotation is None: annotation = fn.Count(rel_model._meta.primary_key).alias('count') query = self.clone() query = query.ensure_join(query._query_ctx, rel_model) if not query._group_by: query._group_by = [x.alias() for x in query._select] query._select = tuple(query._select) + (annotation,) return query def _aggregate(self, aggregation=None): if aggregation is None: aggregation = fn.Count(self.model_class._meta.primary_key) query = self.order_by() query._select = [aggregation] return query def aggregate(self, aggregation=None, convert=True): return self._aggregate(aggregation).scalar(convert=convert) def count(self): if self._distinct or self._group_by: return self.wrapped_count() # defaults to a count() of the primary key return self.aggregate(convert=False) or 0 def wrapped_count(self): clone = self.order_by() clone._limit = clone._offset = None sql, params = clone.sql() wrapped = 'SELECT COUNT(1) FROM (%s) AS wrapped_select' % sql rq = RawQuery(self.model_class, wrapped, *params) return rq.scalar() or 0 def exists(self): clone = self.paginate(1, 1) clone._select = [self.model_class._meta.primary_key] return bool(clone.scalar()) def get(self): clone = self.paginate(1, 1) try: return clone.execute().next() except StopIteration: raise self.model_class.DoesNotExist( 'Instance matching query does not exist:\nSQL: %s\nPARAMS: %s' % self.sql()) def first(self): res = self.execute() res.fill_cache(1) try: return res._result_cache[0] except IndexError: pass def sql(self): return self.compiler().generate_select(self) def verify_naive(self): model_class = self.model_class for node in self._select: if isinstance(node, Field) and node.model_class != model_class: return False return True def execute(self): if self._dirty or not self._qr: model_class = self.model_class query_meta = [self._select, self._joins] if self._tuples: ResultWrapper = TuplesQueryResultWrapper elif self._dicts: ResultWrapper = DictQueryResultWrapper elif self._naive or not self._joins or self.verify_naive(): ResultWrapper = NaiveQueryResultWrapper else: ResultWrapper = ModelQueryResultWrapper self._qr = ResultWrapper(model_class, self._execute(), query_meta) self._dirty = False return self._qr else: return self._qr def __iter__(self): return iter(self.execute()) def iterator(self): return iter(self.execute().iterator()) def __getitem__(self, value): start = end = None res = self.execute() if isinstance(value, slice): res.fill_cache(value.stop) else: res.fill_cache(value) return res._result_cache[value] class UpdateQuery(Query): def __init__(self, model_class, update=None): self._update = update super(UpdateQuery, self).__init__(model_class) def _clone_attributes(self, query): query._update = dict(self._update) return query join = not_allowed('joining') def sql(self): return self.compiler().generate_update(self) def execute(self): return self.database.rows_affected(self._execute()) class InsertQuery(Query): def __init__(self, model_class, insert=None): mm = model_class._meta defaults = mm.get_default_dict() query = dict((mm.fields[f], v) for f, v in defaults.items()) query.update(insert) self._insert = query super(InsertQuery, self).__init__(model_class) def _clone_attributes(self, query): query._insert = dict(self._insert) return query join = not_allowed('joining') where = not_allowed('where clause') def sql(self): return self.compiler().generate_insert(self) def execute(self): return self.database.last_insert_id(self._execute(), self.model_class) class DeleteQuery(Query): join = not_allowed('joining') def sql(self): return self.compiler().generate_delete(self) def execute(self): return self.database.rows_affected(self._execute()) class Database(object): commit_select = False compiler_class = QueryCompiler field_overrides = {} for_update = False interpolation = '?' limit_max = None op_overrides = {} quote_char = '"' reserved_tables = [] sequences = False subquery_delete_same_table = True def __init__(self, database, threadlocals=False, autocommit=True, fields=None, ops=None, **connect_kwargs): self.init(database, **connect_kwargs) if threadlocals: self.__local = threading.local() else: self.__local = type('DummyLocal', (object,), {}) self._conn_lock = threading.Lock() self.autocommit = autocommit self.field_overrides = dict_update(self.field_overrides, fields or {}) self.op_overrides = dict_update(self.op_overrides, ops or {}) def init(self, database, **connect_kwargs): self.deferred = database is None self.database = database self.connect_kwargs = connect_kwargs def connect(self): with self._conn_lock: if self.deferred: raise Exception('Error, database not properly initialized ' 'before opening connection') self.__local.conn = self._connect( self.database, **self.connect_kwargs) self.__local.closed = False def close(self): with self._conn_lock: if self.deferred: raise Exception('Error, database not properly initialized ' 'before closing connection') self._close(self.__local.conn) self.__local.closed = True def get_conn(self): if not hasattr(self.__local, 'closed') or self.__local.closed: self.connect() return self.__local.conn def is_closed(self): return getattr(self.__local, 'closed', True) def get_cursor(self): return self.get_conn().cursor() def _close(self, conn): conn.close() def _connect(self, database, **kwargs): raise NotImplementedError @classmethod def register_fields(cls, fields): cls.field_overrides = dict_update(cls.field_overrides, fields) @classmethod def register_ops(cls, ops): cls.op_overrides = dict_update(cls.op_overrides, ops) def last_insert_id(self, cursor, model): if model._meta.auto_increment: return cursor.lastrowid def rows_affected(self, cursor): return cursor.rowcount def sql_error_handler(self, exception, sql, params, require_commit): raise exception def compiler(self): return self.compiler_class( self.quote_char, self.interpolation, self.field_overrides, self.op_overrides) def execute_sql(self, sql, params=None, require_commit=True): logger.debug((sql, params)) cursor = self.get_cursor() try: res = cursor.execute(sql, params or ()) except Exception as exc: logger.error('Error executing query %s (%s)' % (sql, params)) return self.sql_error_handler(exc, sql, params, require_commit) if require_commit and self.get_autocommit(): self.commit() return cursor def begin(self): pass def commit(self): self.get_conn().commit() def rollback(self): self.get_conn().rollback() def set_autocommit(self, autocommit): self.__local.autocommit = autocommit def get_autocommit(self): if not hasattr(self.__local, 'autocommit'): self.set_autocommit(self.autocommit) return self.__local.autocommit def transaction(self): return transaction(self) def commit_on_success(self, func): def inner(*args, **kwargs): orig = self.get_autocommit() self.set_autocommit(False) self.begin() try: res = func(*args, **kwargs) self.commit() except: self.rollback() raise else: return res finally: self.set_autocommit(orig) return inner def get_tables(self): raise NotImplementedError def get_indexes_for_table(self, table): raise NotImplementedError def sequence_exists(self, seq): raise NotImplementedError def create_table(self, model_class, safe=False): qc = self.compiler() return self.execute_sql(qc.create_table(model_class, safe)) def create_index(self, model_class, fields, unique=False): qc = self.compiler() if not isinstance(fields, (list, tuple)): raise ValueError('Fields passed to "create_index" must be a list ' 'or tuple: "%s"' % fields) fobjs = [ model_class._meta.fields[f] if isinstance(f, basestring) else f for f in fields] return self.execute_sql(qc.create_index(model_class, fobjs, unique)) def create_foreign_key(self, model_class, field): if not field.primary_key: return self.create_index(model_class, [field], field.unique) def create_sequence(self, seq): if self.sequences: qc = self.compiler() return self.execute_sql(qc.create_sequence(seq)) def drop_table(self, model_class, fail_silently=False): qc = self.compiler() return self.execute_sql(qc.drop_table(model_class, fail_silently)) def drop_sequence(self, seq): if self.sequences: qc = self.compiler() return self.execute_sql(qc.drop_sequence(seq)) def extract_date(self, date_part, date_field): return fn.EXTRACT(Clause(date_part, R('FROM'), date_field)) class SqliteDatabase(Database): limit_max = -1 op_overrides = { OP_LIKE: 'GLOB', OP_ILIKE: 'LIKE', } if sqlite3: ConnectionError = sqlite3.OperationalError def _connect(self, database, **kwargs): if not sqlite3: raise ImproperlyConfigured('sqlite3 must be installed on the system') conn = sqlite3.connect(database, **kwargs) conn.create_function('date_part', 2, _sqlite_date_part) return conn def get_indexes_for_table(self, table): res = self.execute_sql('PRAGMA index_list(%s);' % self.quote(table)) rows = sorted([(r[1], r[2] == 1) for r in res.fetchall()]) return rows def get_tables(self): res = self.execute_sql('select name from sqlite_master where ' 'type="table" order by name;') return [r[0] for r in res.fetchall()] def extract_date(self, date_part, date_field): return fn.date_part(date_part, date_field) class PostgresqlDatabase(Database): commit_select = True field_overrides = { 'blob': 'BYTEA', 'bool': 'BOOLEAN', 'datetime': 'TIMESTAMP', 'decimal': 'NUMERIC', 'double': 'DOUBLE PRECISION', 'primary_key': 'SERIAL', } for_update = True interpolation = '%s' reserved_tables = ['user'] sequences = True def _connect(self, database, **kwargs): if not psycopg2: raise ImproperlyConfigured('psycopg2 must be installed.') return psycopg2.connect(database=database, **kwargs) def last_insert_id(self, cursor, model): seq = model._meta.primary_key.sequence if seq: cursor.execute("SELECT CURRVAL('\"%s\"')" % (seq)) return cursor.fetchone()[0] elif model._meta.auto_increment: cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % ( model._meta.db_table, model._meta.primary_key.db_column)) return cursor.fetchone()[0] def get_indexes_for_table(self, table): res = self.execute_sql(""" SELECT c2.relname, i.indisprimary, i.indisunique FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i WHERE c.relname = %s AND c.oid = i.indrelid AND i.indexrelid = c2.oid ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname""", (table,)) return sorted([(r[0], r[1]) for r in res.fetchall()]) def get_tables(self): res = self.execute_sql(""" SELECT c.relname FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r', 'v', '') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY c.relname""") return [row[0] for row in res.fetchall()] def sequence_exists(self, sequence): res = self.execute_sql(""" SELECT COUNT(*) FROM pg_class, pg_namespace WHERE relkind='S' AND pg_class.relnamespace = pg_namespace.oid AND relname=%s""", (sequence,)) return bool(res.fetchone()[0]) def set_search_path(self, *search_path): path_params = ','.join(['%s'] * len(search_path)) self.execute_sql('SET search_path TO %s' % path_params, search_path) class MySQLDatabase(Database): commit_select = True field_overrides = { 'bool': 'BOOL', 'decimal': 'NUMERIC', 'double': 'DOUBLE PRECISION', 'float': 'FLOAT', 'primary_key': 'INTEGER AUTO_INCREMENT', 'text': 'LONGTEXT', } for_update = True interpolation = '%s' limit_max = 2 ** 64 - 1 # MySQL quirk op_overrides = { OP_LIKE: 'LIKE BINARY', OP_ILIKE: 'LIKE', OP_XOR: 'XOR', } quote_char = '`' subquery_delete_same_table = False def _connect(self, database, **kwargs): if not mysql: raise ImproperlyConfigured('MySQLdb must be installed.') conn_kwargs = { 'charset': 'utf8', 'use_unicode': True, } conn_kwargs.update(kwargs) return mysql.connect(db=database, **conn_kwargs) def create_foreign_key(self, model_class, field): compiler = self.compiler() framing = """ ALTER TABLE %(table)s ADD CONSTRAINT %(constraint)s FOREIGN KEY (%(field)s) REFERENCES %(to)s(%(to_field)s)%(cascade)s; """ db_table = model_class._meta.db_table constraint = 'fk_%s_%s_%s' % ( db_table, field.rel_model._meta.db_table, field.db_column, ) quote = compiler.quote query = framing % { 'table': quote(db_table), 'constraint': quote(constraint), 'field': quote(field.db_column), 'to': quote(field.rel_model._meta.db_table), 'to_field': quote(field.rel_model._meta.primary_key.db_column), 'cascade': ' ON DELETE CASCADE' if field.cascade else ''} self.execute_sql(query) return super(MySQLDatabase, self).create_foreign_key( model_class, field) def get_indexes_for_table(self, table): res = self.execute_sql('SHOW INDEXES IN `%s`;' % table) rows = sorted([(r[2], r[1] == 0) for r in res.fetchall()]) return rows def get_tables(self): res = self.execute_sql('SHOW TABLES;') return [r[0] for r in res.fetchall()] def extract_date(self, date_part, date_field): assert date_part.lower() in DT_LOOKUPS return fn.EXTRACT(Clause(R(date_part), R('FROM'), date_field)) class transaction(object): def __init__(self, db): self.db = db def __enter__(self): self._orig = self.db.get_autocommit() self.db.set_autocommit(False) self.db.begin() def __exit__(self, exc_type, exc_val, exc_tb): success = True if exc_type: self.db.rollback() success = False else: self.db.commit() self.db.set_autocommit(self._orig) return success class FieldProxy(Field): def __init__(self, alias, field_instance): self._model_alias = alias self.model = self._model_alias.model_class self.field_instance = field_instance def clone_base(self): return FieldProxy(self._model_alias, self.field_instance) def __getattr__(self, attr): if attr == 'model_class': return self._model_alias return getattr(self.field_instance, attr) class ModelAlias(object): def __init__(self, model_class): self.__dict__['model_class'] = model_class def __getattr__(self, attr): model_attr = getattr(self.model_class, attr) if isinstance(model_attr, Field): return FieldProxy(self, model_attr) return model_attr def __setattr__(self, attr, value): raise AttributeError('Cannot set attributes on ModelAlias instances') def get_proxy_fields(self): return [ FieldProxy(self, f) for f in self.model_class._meta.get_fields()] class DoesNotExist(Exception): pass default_database = SqliteDatabase('peewee.db') class ModelOptions(object): def __init__(self, cls, database=None, db_table=None, indexes=None, order_by=None, primary_key=None, **kwargs): self.model_class = cls self.name = cls.__name__.lower() self.fields = {} self.columns = {} self.defaults = {} self.database = database or default_database self.db_table = db_table self.indexes = list(indexes or []) self.order_by = order_by self.primary_key = primary_key self.auto_increment = None self.rel = {} self.reverse_rel = {} for key, value in kwargs.items(): setattr(self, key, value) self._additional_keys = set(kwargs.keys()) def prepared(self): for field in self.fields.values(): if field.default is not None: self.defaults[field] = field.default if self.order_by: norm_order_by = [] for clause in self.order_by: field = self.fields[clause.lstrip('-')] if clause.startswith('-'): norm_order_by.append(field.desc()) else: norm_order_by.append(field.asc()) self.order_by = norm_order_by def get_default_dict(self): dd = {} for field, default in self.defaults.items(): if callable(default): dd[field.name] = default() else: dd[field.name] = default return dd def get_sorted_fields(self): key = lambda i: (i[1] is self.primary_key and 1 or 2, i[1]._order) return sorted(self.fields.items(), key=key) def get_field_names(self): return [f[0] for f in self.get_sorted_fields()] def get_fields(self): return [f[1] for f in self.get_sorted_fields()] def rel_for_model(self, model, field_obj=None): for field in self.get_fields(): if isinstance(field, ForeignKeyField) and field.rel_model == model: if field_obj is None or field_obj.name == field.name: return field def reverse_rel_for_model(self, model): return model._meta.rel_for_model(self.model_class) def rel_exists(self, model): return self.rel_for_model(model) or self.reverse_rel_for_model(model) class BaseModel(type): inheritable = set(['database', 'indexes', 'order_by', 'primary_key']) def __new__(cls, name, bases, attrs): if not bases: return super(BaseModel, cls).__new__(cls, name, bases, attrs) meta_options = {} meta = attrs.pop('Meta', None) if meta: for k, v in meta.__dict__.items(): if not k.startswith('_'): meta_options[k] = v model_pk = getattr(meta, 'primary_key', None) parent_pk = None # inherit any field descriptors by deep copying the underlying field # into the attrs of the new model, additionally see if the bases define # inheritable model options and swipe them for b in bases: if not hasattr(b, '_meta'): continue base_meta = getattr(b, '_meta') if parent_pk is None: parent_pk = deepcopy(base_meta.primary_key) all_inheritable = cls.inheritable | base_meta._additional_keys for (k, v) in base_meta.__dict__.items(): if k in all_inheritable and k not in meta_options: meta_options[k] = v for (k, v) in b.__dict__.items(): if isinstance(v, FieldDescriptor) and k not in attrs: if not v.field.primary_key: attrs[k] = deepcopy(v.field) # initialize the new class and set the magic attributes cls = super(BaseModel, cls).__new__(cls, name, bases, attrs) cls._meta = ModelOptions(cls, **meta_options) cls._data = None cls._meta.indexes = list(cls._meta.indexes) # replace fields with field descriptors, calling the add_to_class hook for name, attr in list(cls.__dict__.items()): if isinstance(attr, Field): attr.add_to_class(cls, name) if attr.primary_key and model_pk: raise ValueError('primary key is overdetermined.') elif attr.primary_key: model_pk = attr if model_pk is None: if parent_pk: model_pk, name = parent_pk, parent_pk.name else: model_pk, name = PrimaryKeyField(primary_key=True), 'id' model_pk.add_to_class(cls, name) elif isinstance(model_pk, CompositeKey): model_pk.add_to_class(cls, '_composite_key') cls._meta.primary_key = model_pk cls._meta.auto_increment = ( isinstance(model_pk, PrimaryKeyField) or bool(model_pk.sequence)) if not cls._meta.db_table: cls._meta.db_table = re.sub('[^\w]+', '_', cls.__name__.lower()) # create a repr and error class before finalizing if hasattr(cls, '__unicode__'): setattr(cls, '__repr__', lambda self: '<%s: %r>' % ( cls.__name__, self.__unicode__())) exc_name = '%sDoesNotExist' % cls.__name__ exception_class = type(exc_name, (DoesNotExist,), {}) cls.DoesNotExist = exception_class cls._meta.prepared() return cls class Model(with_metaclass(BaseModel)): def __init__(self, *args, **kwargs): self._data = self._meta.get_default_dict() self._obj_cache = {} # cache of related objects for k, v in kwargs.items(): setattr(self, k, v) @classmethod def alias(cls): return ModelAlias(cls) @classmethod def select(cls, *selection): query = SelectQuery(cls, *selection) if cls._meta.order_by: query = query.order_by(*cls._meta.order_by) return query @classmethod def update(cls, **update): fdict = dict((cls._meta.fields[f], v) for f, v in update.items()) return UpdateQuery(cls, fdict) @classmethod def insert(cls, **insert): fdict = dict((cls._meta.fields[f], v) for f, v in insert.items()) return InsertQuery(cls, fdict) @classmethod def delete(cls): return DeleteQuery(cls) @classmethod def raw(cls, sql, *params): return RawQuery(cls, sql, *params) @classmethod def create(cls, **query): inst = cls(**query) inst.save(force_insert=True) return inst @classmethod def get(cls, *query, **kwargs): sq = cls.select().naive() if query: sq = sq.where(*query) if kwargs: sq = sq.filter(**kwargs) return sq.get() @classmethod def get_or_create(cls, **kwargs): sq = cls.select().filter(**kwargs) try: return sq.get() except cls.DoesNotExist: return cls.create(**kwargs) @classmethod def filter(cls, *dq, **query): return cls.select().filter(*dq, **query) @classmethod def table_exists(cls): return cls._meta.db_table in cls._meta.database.get_tables() @classmethod def create_table(cls, fail_silently=False): if fail_silently and cls.table_exists(): return db = cls._meta.database pk = cls._meta.primary_key if db.sequences and pk.sequence: if not db.sequence_exists(pk.sequence): db.create_sequence(pk.sequence) db.create_table(cls) cls._create_indexes() @classmethod def _create_indexes(cls): db = cls._meta.database for field_name, field_obj in cls._meta.fields.items(): if isinstance(field_obj, ForeignKeyField): db.create_foreign_key(cls, field_obj) elif field_obj.index or field_obj.unique: db.create_index(cls, [field_obj], field_obj.unique) if cls._meta.indexes: for fields, unique in cls._meta.indexes: db.create_index(cls, fields, unique) @classmethod def drop_table(cls, fail_silently=False): cls._meta.database.drop_table(cls, fail_silently) def get_id(self): return getattr(self, self._meta.primary_key.name) def set_id(self, id): setattr(self, self._meta.primary_key.name, id) def pk_expr(self): return self._meta.primary_key == self.get_id() def prepared(self): pass def _prune_fields(self, field_dict, only): new_data = {} for field in only: if field.name in field_dict: new_data[field.name] = field_dict[field.name] return new_data def save(self, force_insert=False, only=None): field_dict = dict(self._data) pk = self._meta.primary_key if only: field_dict = self._prune_fields(field_dict, only) if self.get_id() is not None and not force_insert: field_dict.pop(pk.name, None) self.update(**field_dict).where(self.pk_expr()).execute() else: pk = self.get_id() ret_pk = self.insert(**field_dict).execute() if ret_pk is not None: pk = ret_pk self.set_id(pk) def dependencies(self, search_nullable=False): query = self.select().where(self.pk_expr()) stack = [(type(self), query)] seen = set() while stack: klass, query = stack.pop() if klass in seen: continue seen.add(klass) for rel_name, fk in klass._meta.reverse_rel.items(): rel_model = fk.model_class node = fk << query if not fk.null or search_nullable: stack.append((rel_model, rel_model.select().where(node))) yield (node, fk) def delete_instance(self, recursive=False, delete_nullable=False): if recursive: dependencies = self.dependencies(delete_nullable) for query, fk in reversed(list(dependencies)): model = fk.model_class if fk.null and not delete_nullable: model.update(**{fk.name: None}).where(query).execute() else: model.delete().where(query).execute() return self.delete().where(self.pk_expr()).execute() def __eq__(self, other): return ( other.__class__ == self.__class__ and self.get_id() is not None and other.get_id() == self.get_id()) def __ne__(self, other): return not self == other def prefetch_add_subquery(sq, subqueries): fixed_queries = [(sq, None)] for i, subquery in enumerate(subqueries): if not isinstance(subquery, Query) and issubclass(subquery, Model): subquery = subquery.select() subquery_model = subquery.model_class fkf = None for j in reversed(range(i + 1)): last_query = fixed_queries[j][0] fkf = subquery_model._meta.rel_for_model(last_query.model_class) if fkf: break if not fkf: raise AttributeError('Error: unable to find foreign key for ' 'query: %s' % subquery) fixed_queries.append((subquery.where(fkf << last_query), fkf)) return fixed_queries def prefetch(sq, *subqueries): if not subqueries: return sq fixed_queries = prefetch_add_subquery(sq, subqueries) deps = {} rel_map = {} for query, foreign_key_field in reversed(fixed_queries): query_model = query.model_class deps[query_model] = {} id_map = deps[query_model] has_relations = bool(rel_map.get(query_model)) for result in query: if foreign_key_field: fk_val = result._data[foreign_key_field.name] id_map.setdefault(fk_val, []) id_map[fk_val].append(result) if has_relations: for rel_model, rel_fk in rel_map[query_model]: rel_name = '%s_prefetch' % rel_fk.related_name rel_instances = deps[rel_model].get(result.get_id(), []) for inst in rel_instances: setattr(inst, rel_fk.name, result) setattr(result, rel_name, rel_instances) if foreign_key_field: rel_model = foreign_key_field.rel_model rel_map.setdefault(rel_model, []) rel_map[rel_model].append((query_model, foreign_key_field)) return query def create_model_tables(models, **create_table_kwargs): """Create tables for all given models (in the right order).""" for m in sort_models_topologically(models): m.create_table(**create_table_kwargs) def drop_model_tables(models, **drop_table_kwargs): """Drop tables for all given models (in the right order).""" for m in reversed(sort_models_topologically(models)): m.drop_table(**drop_table_kwargs) def sort_models_topologically(models): """Sort models topologically so that parents will precede children.""" models = set(models) seen = set() ordering = [] def dfs(model): if model in models and model not in seen: seen.add(model) for foreign_key in model._meta.reverse_rel.values(): dfs(foreign_key.model_class) ordering.append(model) # parent will follow descendants # order models by name and table initially to guarantee a total ordering names = lambda m: (m._meta.name, m._meta.db_table) for m in sorted(models, key=names, reverse=True): dfs(m) return list(reversed(ordering)) # want parents first in output ordering
bsd-3-clause
atty303/pyfilesystem
fs/path.py
1
15148
""" fs.path ======= Useful functions for FS path manipulation. This is broadly similar to the standard ``os.path`` module but works with paths in the canonical format expected by all FS objects (forwardslash-separated, optional leading slash). """ import re _requires_normalization = re.compile(r'/\.\.|\./|//|\\').search def normpath(path): """Normalizes a path to be in the format expected by FS objects. This function remove any leading or trailing slashes, collapses duplicate slashes, replaces backward with forward slashes, and generally tries very hard to return a new path string the canonical FS format. If the path is invalid, ValueError will be raised. :param path: path to normalize :returns: a valid FS path >>> normpath(r"foo\\bar\\baz") 'foo/bar/baz' >>> normpath("/foo//bar/frob/../baz") '/foo/bar/baz' >>> normpath("foo/../../bar") Traceback (most recent call last) ... ValueError: too many backrefs in path 'foo/../../bar' """ if path in ('', '/'): return path # An early out if there is no need to normalize this paath if not _requires_normalization(path): return path.rstrip('/') components = [] append = components.append for comp in [c for c in path.replace('\\','/').split("/") if c not in ('', '.')]: if comp == "..": try: components.pop() except IndexError: raise ValueError("too many backrefs in path '%s'" % path) else: append(comp) if path[0] in '\\/': if not components: append("") components.insert(0, "") return "/".join(components) def iteratepath(path, numsplits=None): """Iterate over the individual components of a path. :param path: Path to iterate over :numsplits: Maximum number of splits """ path = relpath(normpath(path)) if not path: return [] if numsplits == None: return path.split('/') else: return path.split('/', numsplits) def recursepath(path, reverse=False): """Returns intermediate paths from the root to the given path :param reverse: reverses the order of the paths >>> recursepath('a/b/c') ['/', u'/a', u'/a/b', u'/a/b/c'] """ if path in ('', '/'): return [u'/'] path = abspath(normpath(path)) + '/' paths = [u'/'] find = path.find append = paths.append pos = 1 len_path = len(path) while pos < len_path: pos = find('/', pos) append(path[:pos]) pos += 1 if reverse: return paths[::-1] return paths def abspath(path): """Convert the given path to an absolute path. Since FS objects have no concept of a 'current directory' this simply adds a leading '/' character if the path doesn't already have one. """ if not path.startswith('/'): return u'/' + path return path def relpath(path): """Convert the given path to a relative path. This is the inverse of abspath(), stripping a leading '/' from the path if it is present. :param path: Path to adjust >>> relpath('/a/b') 'a/b' """ return path.lstrip('/') def pathjoin(*paths): """Joins any number of paths together, returning a new path string. :param paths: Paths to join are given in positional arguments >>> pathjoin('foo', 'bar', 'baz') 'foo/bar/baz' >>> pathjoin('foo/bar', '../baz') 'foo/baz' >>> pathjoin('foo/bar', '/baz') '/baz' """ absolute = False relpaths = [] for p in paths: if p: if p[0] in '\\/': del relpaths[:] absolute = True relpaths.append(p) path = normpath(u"/".join(relpaths)) if absolute: path = abspath(path) return path def join(*paths): """Joins any number of paths together, returning a new path string. This is a simple alias for the ``pathjoin`` function, allowing it to be used as ``fs.path.join`` in direct correspondance with ``os.path.join``. :param paths: Paths to join are given in positional arguments """ return pathjoin(*paths) def pathsplit(path): """Splits a path into (head, tail) pair. This function splits a path into a pair (head, tail) where 'tail' is the last pathname component and 'head' is all preceeding components. :param path: Path to split >>> pathsplit("foo/bar") ('foo', 'bar') >>> pathsplit("foo/bar/baz") ('foo/bar', 'baz') >>> pathsplit("/foo/bar/baz") ('/foo/bar', 'baz') """ if '/' not in path: return ('', path) split = path.rsplit('/', 1) return (split[0] or '/', split[1]) def split(path): """Splits a path into (head, tail) pair. This is a simple alias for the ``pathsplit`` function, allowing it to be used as ``fs.path.split`` in direct correspondance with ``os.path.split``. :param path: Path to split """ return pathsplit(path) def splitext(path): """Splits the extension from the path, and returns the path (up to the last '.' and the extension :param path: A path to split >>> splitext('baz.txt') ('baz', 'txt') >>> splitext('foo/bar/baz.txt') ('foo/bar/baz', 'txt') """ parent_path, pathname = pathsplit(path) if '.' not in pathname: return path, '' pathname, ext = pathname.rsplit('.', 1) path = pathjoin(parent_path, pathname) return path, '.' + ext def isdotfile(path): """Detects if a path references a dot file, i.e. a resource who's name starts with a '.' :param path: Path to check >>> isdotfile('.baz') True >>> isdotfile('foo/bar/baz') True >>> isdotfile('foo/bar.baz'). False """ return basename(path).startswith('.') def dirname(path): """Returns the parent directory of a path. This is always equivalent to the 'head' component of the value returned by pathsplit(path). :param path: A FS path >>> dirname('foo/bar/baz') 'foo/bar' """ if '/' not in path: return '' return path.rsplit('/', 1)[0] def basename(path): """Returns the basename of the resource referenced by a path. This is always equivalent to the 'head' component of the value returned by pathsplit(path). :param path: A FS path >>> basename('foo/bar/baz') 'baz' """ if '/' not in path: return path return path.rsplit('/', 1)[-1] def issamedir(path1, path2): """Return true if two paths reference a resource in the same directory. :param path1: An FS path :param path2: An FS path >>> issamedir("foo/bar/baz.txt", "foo/bar/spam.txt") True >>> issamedir("foo/bar/baz/txt", "spam/eggs/spam.txt") False """ return dirname(normpath(path1)) == dirname(normpath(path2)) def isbase(path1, path2): p1 = forcedir(abspath(path1)) p2 = forcedir(abspath(path2)) return p1 == p2 or p1.startswith(p2) def isprefix(path1, path2): """Return true is path1 is a prefix of path2. :param path1: An FS path :param path2: An FS path >>> isprefix("foo/bar", "foo/bar/spam.txt") True >>> isprefix("foo/bar/", "foo/bar") True >>> isprefix("foo/barry", "foo/baz/bar") False >>> isprefix("foo/bar/baz/", "foo/baz/bar") False """ bits1 = path1.split("/") bits2 = path2.split("/") while bits1 and bits1[-1] == "": bits1.pop() if len(bits1) > len(bits2): return False for (bit1,bit2) in zip(bits1,bits2): if bit1 != bit2: return False return True def forcedir(path): """Ensure the path ends with a trailing / :param path: An FS path >>> forcedir("foo/bar") 'foo/bar/' >>> forcedir("foo/bar/") 'foo/bar/' """ if not path.endswith('/'): return path + '/' return path def frombase(path1, path2): if not isprefix(path1, path2): raise ValueError("path1 must be a prefix of path2") return path2[len(path1):] class PathMap(object): """Dict-like object with paths for keys. A PathMap is like a dictionary where the keys are all FS paths. It has two main advantages over a standard dictionary. First, keys are normalised automatically:: >>> pm = PathMap() >>> pm["hello/world"] = 42 >>> print pm["/hello/there/../world"] 42 Second, various dictionary operations (e.g. listing or clearing values) can be efficiently performed on a subset of keys sharing some common prefix:: # list all values in the map pm.values() # list all values for paths starting with "/foo/bar" pm.values("/foo/bar") Under the hood, a PathMap is a trie-like structure where each level is indexed by path name component. This allows lookups to be performed in O(number of path components) while permitting efficient prefix-based operations. """ def __init__(self): self._map = {} def __getitem__(self,path): """Get the value stored under the given path.""" m = self._map for name in iteratepath(path): try: m = m[name] except KeyError: raise KeyError(path) try: return m[""] except KeyError: raise KeyError(path) def __contains__(self,path): """Check whether the given path has a value stored in the map.""" try: self[path] except KeyError: return False else: return True def __setitem__(self,path,value): """Set the value stored under the given path.""" m = self._map for name in iteratepath(path): try: m = m[name] except KeyError: m = m.setdefault(name,{}) m[""] = value def __delitem__(self,path): """Delete the value stored under the given path.""" ms = [[self._map,None]] for name in iteratepath(path): try: ms.append([ms[-1][0][name],None]) except KeyError: raise KeyError(path) else: ms[-2][1] = name try: del ms[-1][0][""] except KeyError: raise KeyError(path) else: while len(ms) > 1 and not ms[-1][0]: del ms[-1] del ms[-1][0][ms[-1][1]] def get(self,path,default=None): """Get the value stored under the given path, or the given default.""" try: return self[path] except KeyError: return default def pop(self,path,default=None): """Pop the value stored under the given path, or the given default.""" ms = [[self._map,None]] for name in iteratepath(path): try: ms.append([ms[-1][0][name],None]) except KeyError: return default else: ms[-2][1] = name try: val = ms[-1][0].pop("") except KeyError: val = default else: while len(ms) > 1 and not ms[-1][0]: del ms[-1] del ms[-1][0][ms[-1][1]] return val def setdefault(self,path,value): m = self._map for name in iteratepath(path): try: m = m[name] except KeyError: m = m.setdefault(name,{}) return m.setdefault("",value) def clear(self,root="/"): """Clear all entries beginning with the given root path.""" m = self._map for name in iteratepath(root): try: m = m[name] except KeyError: return m.clear() def iterkeys(self,root="/",m=None): """Iterate over all keys beginning with the given root path.""" if m is None: m = self._map for name in iteratepath(root): try: m = m[name] except KeyError: return for (nm,subm) in m.iteritems(): if not nm: yield abspath(normpath(root)) else: k = pathjoin(root,nm) for subk in self.iterkeys(k,subm): yield subk def keys(self,root="/"): return list(self.iterkeys(root)) def itervalues(self,root="/",m=None): """Iterate over all values whose keys begin with the given root path.""" if m is None: m = self._map for name in iteratepath(root): try: m = m[name] except KeyError: return for (nm,subm) in m.iteritems(): if not nm: yield subm else: k = pathjoin(root,nm) for subv in self.itervalues(k,subm): yield subv def values(self,root="/"): return list(self.itervalues(root)) def iteritems(self,root="/",m=None): """Iterate over all (key,value) pairs beginning with the given root.""" if m is None: m = self._map for name in iteratepath(root): try: m = m[name] except KeyError: return for (nm,subm) in m.iteritems(): if not nm: yield (abspath(normpath(root)),subm) else: k = pathjoin(root,nm) for (subk,subv) in self.iteritems(k,subm): yield (subk,subv) def items(self,root="/"): return list(self.iteritems(root)) def iternames(self,root="/"): """Iterate over all names beneath the given root path. This is basically the equivalent of listdir() for a PathMap - it yields the next level of name components beneath the given path. """ m = self._map for name in iteratepath(root): try: m = m[name] except KeyError: return for (nm,subm) in m.iteritems(): if nm and subm: yield nm def names(self,root="/"): return list(self.iternames(root)) _wild_chars = frozenset('*?[]!{}') def iswildcard(path): """Check if a path ends with a wildcard >>> is_wildcard('foo/bar/baz.*') True >>> is_wildcard('foo/bar') False """ assert path is not None base_chars = frozenset(basename(path)) return not base_chars.isdisjoint(_wild_chars) if __name__ == "__main__": print recursepath('a/b/c')
bsd-3-clause
MattsFleaMarket/python-for-android
python-build/python-libs/gdata/build/lib/gdata/blogger/__init__.py
140
6426
#!/usr/bin/python # # Copyright (C) 2007, 2008 Google 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. """Contains extensions to Atom objects used with Blogger.""" __author__ = 'api.jscudder (Jeffrey Scudder)' import atom import gdata import re LABEL_SCHEME = 'http://www.blogger.com/atom/ns#' THR_NAMESPACE = 'http://purl.org/syndication/thread/1.0' class BloggerEntry(gdata.GDataEntry): """Adds convenience methods inherited by all Blogger entries.""" blog_name_pattern = re.compile('(http://)(\w*)') blog_id_pattern = re.compile('(tag:blogger.com,1999:blog-)(\w*)') blog_id2_pattern = re.compile('tag:blogger.com,1999:user-(\d+)\.blog-(\d+)') def GetBlogId(self): """Extracts the Blogger id of this blog. This method is useful when contructing URLs by hand. The blog id is often used in blogger operation URLs. This should not be confused with the id member of a BloggerBlog. The id element is the Atom id XML element. The blog id which this method returns is a part of the Atom id. Returns: The blog's unique id as a string. """ if self.id.text: match = self.blog_id_pattern.match(self.id.text) if match: return match.group(2) else: return self.blog_id2_pattern.match(self.id.text).group(2) return None def GetBlogName(self): """Finds the name of this blog as used in the 'alternate' URL. An alternate URL is in the form 'http://blogName.blogspot.com/'. For an entry representing the above example, this method would return 'blogName'. Returns: The blog's URL name component as a string. """ for link in self.link: if link.rel == 'alternate': return self.blog_name_pattern.match(link.href).group(2) return None class BlogEntry(BloggerEntry): """Describes a blog entry in the feed listing a user's blogs.""" def BlogEntryFromString(xml_string): return atom.CreateClassFromXMLString(BlogEntry, xml_string) class BlogFeed(gdata.GDataFeed): """Describes a feed of a user's blogs.""" _children = gdata.GDataFeed._children.copy() _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [BlogEntry]) def BlogFeedFromString(xml_string): return atom.CreateClassFromXMLString(BlogFeed, xml_string) class BlogPostEntry(BloggerEntry): """Describes a blog post entry in the feed of a blog's posts.""" post_id_pattern = re.compile('(tag:blogger.com,1999:blog-)(\w*)(.post-)(\w*)') def AddLabel(self, label): """Adds a label to the blog post. The label is represented by an Atom category element, so this method is shorthand for appending a new atom.Category object. Args: label: str """ self.category.append(atom.Category(scheme=LABEL_SCHEME, term=label)) def GetPostId(self): """Extracts the postID string from the entry's Atom id. Returns: A string of digits which identify this post within the blog. """ if self.id.text: return self.post_id_pattern.match(self.id.text).group(4) return None def BlogPostEntryFromString(xml_string): return atom.CreateClassFromXMLString(BlogPostEntry, xml_string) class BlogPostFeed(gdata.GDataFeed): """Describes a feed of a blog's posts.""" _children = gdata.GDataFeed._children.copy() _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [BlogPostEntry]) def BlogPostFeedFromString(xml_string): return atom.CreateClassFromXMLString(BlogPostFeed, xml_string) class InReplyTo(atom.AtomBase): _tag = 'in-reply-to' _namespace = THR_NAMESPACE _attributes = atom.AtomBase._attributes.copy() _attributes['href'] = 'href' _attributes['ref'] = 'ref' _attributes['source'] = 'source' _attributes['type'] = 'type' def __init__(self, href=None, ref=None, source=None, type=None, extension_elements=None, extension_attributes=None, text=None): self.href = href self.ref = ref self.source = source self.type = type self.extension_elements = extension_elements or [] self.extension_attributes = extension_attributes or {} self.text = text def InReplyToFromString(xml_string): return atom.CreateClassFromXMLString(InReplyTo, xml_string) class CommentEntry(BloggerEntry): """Describes a blog post comment entry in the feed of a blog post's comments.""" _children = BloggerEntry._children.copy() _children['{%s}in-reply-to' % THR_NAMESPACE] = ('in_reply_to', InReplyTo) comment_id_pattern = re.compile('.*-(\w*)$') def __init__(self, author=None, category=None, content=None, contributor=None, atom_id=None, link=None, published=None, rights=None, source=None, summary=None, control=None, title=None, updated=None, in_reply_to=None, extension_elements=None, extension_attributes=None, text=None): BloggerEntry.__init__(self, author=author, category=category, content=content, contributor=contributor, atom_id=atom_id, link=link, published=published, rights=rights, source=source, summary=summary, control=control, title=title, updated=updated, extension_elements=extension_elements, extension_attributes=extension_attributes, text=text) self.in_reply_to = in_reply_to def GetCommentId(self): """Extracts the commentID string from the entry's Atom id. Returns: A string of digits which identify this post within the blog. """ if self.id.text: return self.comment_id_pattern.match(self.id.text).group(1) return None def CommentEntryFromString(xml_string): return atom.CreateClassFromXMLString(CommentEntry, xml_string) class CommentFeed(gdata.GDataFeed): """Describes a feed of a blog post's comments.""" _children = gdata.GDataFeed._children.copy() _children['{%s}entry' % atom.ATOM_NAMESPACE] = ('entry', [CommentEntry]) def CommentFeedFromString(xml_string): return atom.CreateClassFromXMLString(CommentFeed, xml_string)
apache-2.0
dontnod/weblate
weblate/checks/chars.py
1
13311
# -*- coding: utf-8 -*- # # Copyright © 2012 - 2019 Michal Čihař <[email protected]> # # This file is part of Weblate <https://weblate.org/> # # 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 <https://www.gnu.org/licenses/>. # from __future__ import unicode_literals from django.utils.translation import ugettext_lazy as _ from weblate.checks.base import CountingCheck, TargetCheck, TargetCheckParametrized KASHIDA_CHARS = ( '\u0640', '\uFCF2', '\uFCF3', '\uFCF4', '\uFE71', '\uFE77', '\uFE79', '\uFE7B', '\uFE7D', '\uFE7F' ) FRENCH_PUNCTUATION = {';', ':', '?', '!'} class BeginNewlineCheck(TargetCheck): """Check for newlines at beginning.""" check_id = 'begin_newline' name = _('Starting newline') description = _('Source and translation do not both start with a newline') severity = 'warning' def check_single(self, source, target, unit): return self.check_chars(source, target, 0, ['\n']) class EndNewlineCheck(TargetCheck): """Check for newlines at end.""" check_id = 'end_newline' name = _('Trailing newline') description = _('Source and translation do not both end with a newline') severity = 'warning' def check_single(self, source, target, unit): return self.check_chars(source, target, -1, ['\n']) class BeginSpaceCheck(TargetCheck): """Whitespace check, starting whitespace usually is important for UI.""" check_id = 'begin_space' name = _('Starting spaces') description = _( 'Source and translation do not both start with same number of spaces' ) severity = 'warning' def check_single(self, source, target, unit): # One letter things are usually decimal/thousand separators if len(source) <= 1 and len(target) <= 1: return False stripped_target = target.lstrip(' ') stripped_source = source.lstrip(' ') # String translated to spaces only if not stripped_target: return False # Count space chars in source and target source_space = len(source) - len(stripped_source) target_space = len(target) - len(stripped_target) # Compare numbers return source_space != target_space def get_fixup(self, unit): source = unit.get_source_plurals()[0] stripped_source = source.lstrip(' ') spaces = len(source) - len(stripped_source) if spaces: replacement = source[:spaces] else: replacement = '' return [(' *$', replacement)] class EndSpaceCheck(TargetCheck): """Whitespace check.""" check_id = 'end_space' name = _('Trailing space') description = _('Source and translation do not both end with a space') severity = 'warning' def check_single(self, source, target, unit): # One letter things are usually decimal/thousand separators if len(source) <= 1 and len(target) <= 1: return False if not source or not target: return False stripped_target = target.rstrip(' ') stripped_source = source.rstrip(' ') # String translated to spaces only if not stripped_target: return False # Count space chars in source and target source_space = len(source) - len(stripped_source) target_space = len(target) - len(stripped_target) # Compare numbers return source_space != target_space def get_fixup(self, unit): source = unit.get_source_plurals()[0] stripped_source = source.rstrip(' ') spaces = len(source) - len(stripped_source) if spaces: replacement = source[-spaces:] else: replacement = '' return [(' *$', replacement)] class EndStopCheck(TargetCheck): """Check for final stop.""" check_id = 'end_stop' name = _('Trailing stop') description = _('Source and translation do not both end with a full stop') severity = 'warning' def check_single(self, source, target, unit): if len(source) <= 4: # Might need to use shortcut in translation return False if not target: return False # Thai and Lojban does not have a full stop if self.is_language(unit, ('th', 'jbo')): return False # Allow ... to be translated into ellipsis if source.endswith('...') and target[-1] == '…': return False if self.is_language(unit, ('ja', )) and source[-1] in (':', ';'): # Japanese sentence might need to end with full stop # in case it's used before list. return self.check_chars( source, target, -1, (';', ':', ':', '.', '。') ) if self.is_language(unit, ('hy', )): return self.check_chars( source, target, -1, ( '.', '。', '।', '۔', '։', '·', '෴', '។', ':', '՝', '?', '!', '`', ) ) if self.is_language(unit, ('hi', 'bn', 'or')): # Using | instead of । is not typographically correct, but # seems to be quite usual return self.check_chars( source, target, -1, ('.', '।', '|') ) return self.check_chars( source, target, -1, ('.', '。', '।', '۔', '։', '·', '෴', '។') ) class EndColonCheck(TargetCheck): """Check for final colon.""" check_id = 'end_colon' name = _('Trailing colon') description = _( 'Source and translation do not both end with a colon' ) severity = 'warning' def _check_hy(self, source, target): if source[-1] == ':': return self.check_chars( source, target, -1, (':', '՝', '`') ) return False def _check_ja(self, source, target): # Japanese sentence might need to end with full stop # in case it's used before list. if source[-1] in (':', ';'): return self.check_chars( source, target, -1, (';', ':', ':', '.', '。') ) return False def check_single(self, source, target, unit): if not source or not target: return False if self.is_language(unit, ('jbo', )): return False if self.is_language(unit, ('hy', )): return self._check_hy(source, target) if self.is_language(unit, ('ja', )): return self._check_ja(source, target) return self.check_chars(source, target, -1, (':', ':', '៖')) class EndQuestionCheck(TargetCheck): """Check for final question mark.""" check_id = 'end_question' name = _('Trailing question') description = _( 'Source and translation do not both end with a question mark' ) question_el = ('?', ';', ';') severity = 'warning' def _check_hy(self, source, target): if source[-1] == '?': return self.check_chars( source, target, -1, ('?', '՞', '։') ) return False def _check_el(self, source, target): if source[-1] != '?': return False return target[-1] not in self.question_el def check_single(self, source, target, unit): if not source or not target: return False if self.is_language(unit, ('jbo', )): return False if self.is_language(unit, ('hy', )): return self._check_hy(source, target) if self.is_language(unit, ('el', )): return self._check_el(source, target) return self.check_chars( source, target, -1, ('?', '՞', '؟', '⸮', '?', '፧', '꘏', '⳺') ) class EndExclamationCheck(TargetCheck): """Check for final exclamation mark.""" check_id = 'end_exclamation' name = _('Trailing exclamation') description = _( 'Source and translation do not both end with an exclamation mark' ) def check_single(self, source, target, unit): if not source or not target: return False if (self.is_language(unit, ('eu', )) and source[-1] == '!' and '¡' in target and '!' in target): return False if self.is_language(unit, ('hy', 'jbo')): return False if source.endswith('Texy!') or target.endswith('Texy!'): return False return self.check_chars( source, target, -1, ('!', '!', '՜', '᥄', '႟', '߹') ) class EndEllipsisCheck(TargetCheck): """Check for ellipsis at the end of string.""" check_id = 'end_ellipsis' name = _('Trailing ellipsis') description = _('Source and translation do not both end with an ellipsis') severity = 'warning' def check_single(self, source, target, unit): if not target: return False if self.is_language(unit, ('jbo', )): return False # Allow ... to be translated into ellipsis if source.endswith('...') and target[-1] == '…': return False return self.check_chars(source, target, -1, ('…', )) class NewlineCountingCheck(CountingCheck): r"""Check whether there is same amount of \n strings.""" string = '\\n' check_id = 'escaped_newline' name = _('Mismatched \\n') description = _('Number of \\n in translation does not match source') severity = 'warning' class ZeroWidthSpaceCheck(TargetCheck): """Check for zero width space char (<U+200B>).""" check_id = 'zero-width-space' name = _('Zero-width space') description = _('Translation contains extra zero-width space character') severity = 'warning' def check_single(self, source, target, unit): if self.is_language(unit, ('km', )): return False return ('\u200b' in target) != ('\u200b' in source) def get_fixup(self, unit): return [('\u200b', '')] class MaxLengthCheck(TargetCheckParametrized): """Check for maximum length of translation.""" check_id = 'max-length' name = _('Maximum length of translation') description = _('Translation should not exceed given length') severity = 'danger' default_disabled = True param_type = int def check_target_params(self, sources, targets, unit, value): return any((len(target) > value for target in targets)) class EndSemicolonCheck(TargetCheck): """Check for semicolon at end.""" check_id = 'end_semicolon' name = _('Trailing semicolon') description = _('Source and translation do not both end with a semicolon') severity = 'warning' def check_single(self, source, target, unit): if self.is_language(unit, ('el', )) and source and source[-1] == '?': # Complement to question mark check return False return self.check_chars(source, target, -1, [';']) class KashidaCheck(TargetCheck): check_id = 'kashida' name = _('Kashida used') description = _('The decorative kashida letters should not be used') severity = 'warning' def check_single(self, source, target, unit): return any((x in target for x in KASHIDA_CHARS)) def get_fixup(self, unit): return [('[{}]'.format(''.join(KASHIDA_CHARS)), '')] class PuctuationSpacingCheck(TargetCheck): check_id = 'punctuation_spacing' name = _('Punctuation spacing') description = _('Missing non breakable space before double punctuation sign') severity = 'warning' def check_single(self, source, target, unit): if not self.is_language(unit, ('fr', 'br')): return False # Replace HTML markup to simplify parsing target = target.replace('&nbsp;', '\u00A0') whitespace = {' ', '\u00A0', '\u202F', '\u2009'} total = len(target) for i, char in enumerate(target): if char in FRENCH_PUNCTUATION: if i + 1 < total and not target[i + 1].isspace(): continue if i == 0 or target[i - 1] not in whitespace: return True return False def get_fixup(self, unit): return [ # First fix possibly wrong whitespace ('([ \u00A0\u2009])([{}])'.format(''.join(FRENCH_PUNCTUATION)), '\u202F$2'), # Then add missing ones ('([^\u202F])([{}])'.format(''.join(FRENCH_PUNCTUATION)), '$1\u202F$2'), ]
gpl-3.0
WhiteMagic/JoystickGremlin
gremlin/ui/profile_settings.py
1
11998
# -*- coding: utf-8; -*- # Copyright (C) 2015 - 2019 Lionel Ott # # 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/>. from PyQt5 import QtCore, QtWidgets import gremlin.joystick_handling import gremlin.ui.common class ProfileSettingsWidget(QtWidgets.QWidget): """Widget allowing changing profile specific settings.""" # Signal emitted when a change occurs changed = QtCore.pyqtSignal() def __init__(self, profile_settings, parent=None): """Creates a new UI widget. :param profile_settings the settings of the profile :param parent the parent widget """ super().__init__(parent) self.profile_settings = profile_settings self.main_layout = QtWidgets.QVBoxLayout(self) # Create required scroll UI elements self.scroll_area = QtWidgets.QScrollArea() self.scroll_widget = QtWidgets.QWidget() self.scroll_layout = QtWidgets.QVBoxLayout() # Configure the widget holding the layout with all the buttons self.scroll_widget.setLayout(self.scroll_layout) self.scroll_widget.setSizePolicy( QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding ) self.scroll_area.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) # Configure the scroll area self.scroll_area.setWidgetResizable(True) self.scroll_area.setWidget(self.scroll_widget) # Add the scroll area to the main layout self.main_layout.addWidget(self.scroll_area) self._create_ui() def refresh_ui(self, emit_change=False): """Refreshes the entire UI.""" gremlin.ui.common.clear_layout(self.scroll_layout) self._create_ui() if emit_change: self.changed.emit() def _create_ui(self): """Creates the UI elements of this widget.""" # Default start mode selection self.scroll_layout.addWidget(DefaultModeSelector(self.profile_settings)) # Default macro delay self.scroll_layout.addWidget(DefaultDelay(self.profile_settings)) # vJoy devices as inputs vjoy_as_input_widget = VJoyAsInputWidget(self.profile_settings) self.scroll_layout.addWidget(vjoy_as_input_widget) vjoy_as_input_widget.changed.connect(lambda: self.refresh_ui(True)) # vJoy axis initialization value setup for dev in sorted( gremlin.joystick_handling.vjoy_devices(), key=lambda x: x.vjoy_id ): # Only show devices that are not treated as inputs if self.profile_settings.vjoy_as_input.get(dev.vjoy_id) is True: continue widget = QtWidgets.QGroupBox("{} #{}".format(dev.name, dev.vjoy_id)) box_layout = QtWidgets.QVBoxLayout() widget.setLayout(box_layout) box_layout.addWidget(VJoyAxisDefaultsWidget( dev, self.profile_settings )) self.scroll_layout.addWidget(widget) self.scroll_layout.addStretch(1) # Information label label = QtWidgets.QLabel( "This tab allows setting default initialization of vJoy axis " "values. These values will be used when activating Gremlin." ) label.setStyleSheet("QLabel { background-color : '#FFF4B0'; }") label.setWordWrap(True) label.setFrameShape(QtWidgets.QFrame.Box) label.setMargin(10) self.scroll_layout.addWidget(label) class DefaultDelay(QtWidgets.QGroupBox): """Configures the default delay used with macro executions.""" def __init__(self, profile_data, parent=None): """Creates a new instance. Parameters ========== profile_data : profile.Settings Profile settings data storing information parent : QtObject Parent of this widget """ super().__init__(parent) self.profile_data = profile_data self.main_layout = QtWidgets.QHBoxLayout(self) self._create_ui() def _create_ui(self): """Creates the UI of this widget.""" self.setTitle("Default Macro Action Delay") self.delay_value = gremlin.ui.common.DynamicDoubleSpinBox() self.delay_value.setRange(0.0, 10.0) self.delay_value.setSingleStep(0.05) self.delay_value.setValue(self.profile_data.default_delay) self.delay_value.valueChanged.connect(self._update_delay) self.main_layout.addWidget(self.delay_value) self.main_layout.addStretch() def _update_delay(self, value): """Updates the value of the delay with the user input. Parameters ========== value : float New delay value to use between macro actions """ self.profile_data.default_delay = value class DefaultModeSelector(QtWidgets.QGroupBox): """Allows selecting the mode in which Gremlin starts.""" def __init__(self, profile_data, parent=None): """Creates a new instance. :param profile_data profile settings managed by the widget :param parent the parent of this widget """ super().__init__(parent) self.profile_data = profile_data self.main_layout = QtWidgets.QHBoxLayout(self) self._create_ui() def _create_ui(self): """Creates the UI used to configure the startup mode.""" self.setTitle("Startup Mode") self.dropdown = QtWidgets.QComboBox() self.dropdown.addItem("Use Heuristic") for mode in gremlin.profile.mode_list(self.profile_data): self.dropdown.addItem(mode) if self.profile_data.startup_mode: self.dropdown.setCurrentText(self.profile_data.startup_mode) self.dropdown.currentIndexChanged.connect(self._update_cb) self.main_layout.addWidget(self.dropdown) self.main_layout.addStretch() def _update_cb(self, index): """Handles changes in the mode selection drop down. :param index the index of the entry selected """ if index == 0: self.profile_data.startup_mode = None else: self.profile_data.startup_mode = self.dropdown.currentText() class VJoyAxisDefaultsWidget(QtWidgets.QWidget): """UI widget allowing modification of axis initialization values.""" def __init__(self, joy_data, profile_data, parent=None): """Creates a new UI widget. :param joy_data JoystickDeviceData object containing device information :param profile_data profile settings managed by the widget :param parent the parent of this widget """ super().__init__(parent) self.joy_data = joy_data self.profile_data = profile_data self.main_layout = QtWidgets.QGridLayout(self) self.main_layout.setColumnMinimumWidth(0, 100) self.main_layout.setColumnStretch(2, 1) self._spin_boxes = [] self._create_ui() def _create_ui(self): """Creates the UI elements.""" vjoy_proxy = gremlin.joystick_handling.VJoyProxy() for i in range(self.joy_data.axis_count): # FIXME: This is a workaround to not being able to read a vJoy # device's axes names when it is grabbed by another process # and the inability of SDL to provide canonical axis names axis_name = "Axis {:d}".format(i+1) try: axis_name = vjoy_proxy[self.joy_data.vjoy_id]\ .axis_name(linear_index=i+1) except gremlin.error.VJoyError: pass self.main_layout.addWidget( QtWidgets.QLabel(axis_name), i, 0 ) box = gremlin.ui.common.DynamicDoubleSpinBox() box.setRange(-1, 1) box.setSingleStep(0.05) box.setValue(self.profile_data.get_initial_vjoy_axis_value( self.joy_data.vjoy_id, i+1 )) box.valueChanged.connect(self._create_value_cb(i+1)) self.main_layout.addWidget(box, i, 1) vjoy_proxy.reset() def _create_value_cb(self, axis_id): """Creates a callback function which updates axis values. :param axis_id id of the axis to change the value of :return callback customized for the given axis_id """ return lambda x: self._update_axis_value(axis_id, x) def _update_axis_value(self, axis_id, value): """Updates an axis' default value. :param axis_id id of the axis to update :param value the value to update the axis to """ self.profile_data.set_initial_vjoy_axis_value( self.joy_data.vjoy_id, axis_id, value ) class VJoyAsInputWidget(QtWidgets.QGroupBox): """Configures which vJoy devices are treated as physical inputs.""" # Signal emitted when a change occurs changed = QtCore.pyqtSignal() def __init__(self, profile_data, parent=None): """Creates a new instance. :param profile_data profile information read and modified by the widget :param parent the paren of this widget """ super().__init__(parent) self.profile_data = profile_data self.setTitle("vJoy as Input") self.main_layout = QtWidgets. QHBoxLayout(self) self.vjoy_layout = QtWidgets.QVBoxLayout() self._create_ui() def _create_ui(self): """Creates the UI to set physical input state.""" for dev in sorted( gremlin.joystick_handling.vjoy_devices(), key=lambda x: x.vjoy_id ): check_box = QtWidgets.QCheckBox("vJoy {:d}".format(dev.vjoy_id)) if self.profile_data.vjoy_as_input.get(dev.vjoy_id, False): check_box.setChecked(True) check_box.stateChanged.connect( self._create_update_state_cb(dev.vjoy_id) ) self.vjoy_layout.addWidget(check_box) # Information label label = QtWidgets.QLabel( "Declaring a vJoy device as an input device will allow it to be" "used like a physical device, i.e. it can be forwarded to other" "vJoy devices. However, this also means that it won't be available" "as a virtual device." ) label.setStyleSheet("QLabel { background-color : '#FFF4B0'; }") label.setWordWrap(True) label.setFrameShape(QtWidgets.QFrame.Box) label.setMargin(10) self.main_layout.addLayout(self.vjoy_layout) self.main_layout.addWidget(label) def _update_state_cb(self, vid, state): """Callback executed when an entry is modified. :param vid the id of the vJoy device being modified :param state the state of the checkbox """ self.profile_data.vjoy_as_input[vid] = state == QtCore.Qt.Checked self.changed.emit() def _create_update_state_cb(self, vid): """Creates the callback allowing handling of state changes. :param vid the id of the vJoy device being modified """ return lambda x: self._update_state_cb(vid, x)
gpl-3.0
xhlyuestc/SmartDesk
tools/vs.py
19
3717
import os import sys import string import building import xml.etree.ElementTree as etree from xml.etree.ElementTree import SubElement from utils import _make_path_relative from utils import xml_indent fs_encoding = sys.getfilesystemencoding() def VS_AddGroup(ProjectFiles, parent, name, files, project_path): Filter = SubElement(parent, 'Filter') Filter.set('Name', name) #set group name to group for f in files: fn = f.rfile() name = fn.name path = os.path.dirname(fn.abspath) path = _make_path_relative(project_path, path) path = os.path.join(path, name) File = SubElement(Filter, 'File') File.set('RelativePath', path.decode(fs_encoding)) def VS_AddHeadFilesGroup(program, elem, project_path): building.source_ext = [] building.source_ext = ["h"] for item in program: building.walk_children(item) building.source_list.sort() # print building.source_list for f in building.source_list: path = _make_path_relative(project_path, f) File = SubElement(elem, 'File') File.set('RelativePath', path.decode(fs_encoding)) def VSProject(target, script, program): project_path = os.path.dirname(os.path.abspath(target)) tree = etree.parse('template_vs2005.vcproj') root = tree.getroot() out = file(target, 'wb') out.write('<?xml version="1.0" encoding="UTF-8"?>\r\n') ProjectFiles = [] # add "*.c" files group for elem in tree.iter(tag='Filter'): if elem.attrib['Name'] == 'Source Files': #print elem.tag, elem.attrib break for group in script: group_xml = VS_AddGroup(ProjectFiles, elem, group['name'], group['src'], project_path) # add "*.h" files group for elem in tree.iter(tag='Filter'): if elem.attrib['Name'] == 'Header Files': break VS_AddHeadFilesGroup(program, elem, project_path) # write head include path if building.Env.has_key('CPPPATH'): cpp_path = building.Env['CPPPATH'] paths = set() for path in cpp_path: inc = _make_path_relative(project_path, os.path.normpath(path)) paths.add(inc) #.replace('\\', '/') paths = [i for i in paths] paths.sort() cpp_path = ';'.join(paths) # write include path, definitions for elem in tree.iter(tag='Tool'): if elem.attrib['Name'] == 'VCCLCompilerTool': #print elem.tag, elem.attrib break elem.set('AdditionalIncludeDirectories', cpp_path) # write cppdefinitons flags if building.Env.has_key('CPPDEFINES'): definitions = ';'.join(building.Env['CPPDEFINES']) elem.set('PreprocessorDefinitions', definitions) # write link flags # write lib dependence if building.Env.has_key('LIBS'): for elem in tree.iter(tag='Tool'): if elem.attrib['Name'] == 'VCLinkerTool': break libs_with_extention = [i+'.lib' for i in building.Env['LIBS']] libs = ' '.join(libs_with_extention) elem.set('AdditionalDependencies', libs) # write lib include path if building.Env.has_key('LIBPATH'): lib_path = building.Env['LIBPATH'] paths = set() for path in lib_path: inc = _make_path_relative(project_path, os.path.normpath(path)) paths.add(inc) #.replace('\\', '/') paths = [i for i in paths] paths.sort() lib_paths = ';'.join(paths) elem.set('AdditionalLibraryDirectories', lib_paths) xml_indent(root) out.write(etree.tostring(root, encoding='utf-8')) out.close()
gpl-2.0
jacroe/spynot
cherrypy/lib/reprconf.py
83
14551
"""Generic configuration system using unrepr. Configuration data may be supplied as a Python dictionary, as a filename, or as an open file object. When you supply a filename or file, Python's builtin ConfigParser is used (with some extensions). Namespaces ---------- Configuration keys are separated into namespaces by the first "." in the key. The only key that cannot exist in a namespace is the "environment" entry. This special entry 'imports' other config entries from a template stored in the Config.environments dict. You can define your own namespaces to be called when new config is merged by adding a named handler to Config.namespaces. The name can be any string, and the handler must be either a callable or a context manager. """ try: # Python 3.0+ from configparser import ConfigParser except ImportError: from ConfigParser import ConfigParser try: set except NameError: from sets import Set as set try: basestring except NameError: basestring = str try: # Python 3 import builtins except ImportError: # Python 2 import __builtin__ as builtins import operator as _operator import sys def as_dict(config): """Return a dict from 'config' whether it is a dict, file, or filename.""" if isinstance(config, basestring): config = Parser().dict_from_file(config) elif hasattr(config, 'read'): config = Parser().dict_from_file(config) return config class NamespaceSet(dict): """A dict of config namespace names and handlers. Each config entry should begin with a namespace name; the corresponding namespace handler will be called once for each config entry in that namespace, and will be passed two arguments: the config key (with the namespace removed) and the config value. Namespace handlers may be any Python callable; they may also be Python 2.5-style 'context managers', in which case their __enter__ method should return a callable to be used as the handler. See cherrypy.tools (the Toolbox class) for an example. """ def __call__(self, config): """Iterate through config and pass it to each namespace handler. config A flat dict, where keys use dots to separate namespaces, and values are arbitrary. The first name in each config key is used to look up the corresponding namespace handler. For example, a config entry of {'tools.gzip.on': v} will call the 'tools' namespace handler with the args: ('gzip.on', v) """ # Separate the given config into namespaces ns_confs = {} for k in config: if "." in k: ns, name = k.split(".", 1) bucket = ns_confs.setdefault(ns, {}) bucket[name] = config[k] # I chose __enter__ and __exit__ so someday this could be # rewritten using Python 2.5's 'with' statement: # for ns, handler in self.iteritems(): # with handler as callable: # for k, v in ns_confs.get(ns, {}).iteritems(): # callable(k, v) for ns, handler in self.items(): exit = getattr(handler, "__exit__", None) if exit: callable = handler.__enter__() no_exc = True try: try: for k, v in ns_confs.get(ns, {}).items(): callable(k, v) except: # The exceptional case is handled here no_exc = False if exit is None: raise if not exit(*sys.exc_info()): raise # The exception is swallowed if exit() returns true finally: # The normal and non-local-goto cases are handled here if no_exc and exit: exit(None, None, None) else: for k, v in ns_confs.get(ns, {}).items(): handler(k, v) def __repr__(self): return "%s.%s(%s)" % (self.__module__, self.__class__.__name__, dict.__repr__(self)) def __copy__(self): newobj = self.__class__() newobj.update(self) return newobj copy = __copy__ class Config(dict): """A dict-like set of configuration data, with defaults and namespaces. May take a file, filename, or dict. """ defaults = {} environments = {} namespaces = NamespaceSet() def __init__(self, file=None, **kwargs): self.reset() if file is not None: self.update(file) if kwargs: self.update(kwargs) def reset(self): """Reset self to default values.""" self.clear() dict.update(self, self.defaults) def update(self, config): """Update self from a dict, file or filename.""" if isinstance(config, basestring): # Filename config = Parser().dict_from_file(config) elif hasattr(config, 'read'): # Open file object config = Parser().dict_from_file(config) else: config = config.copy() self._apply(config) def _apply(self, config): """Update self from a dict.""" which_env = config.get('environment') if which_env: env = self.environments[which_env] for k in env: if k not in config: config[k] = env[k] dict.update(self, config) self.namespaces(config) def __setitem__(self, k, v): dict.__setitem__(self, k, v) self.namespaces({k: v}) class Parser(ConfigParser): """Sub-class of ConfigParser that keeps the case of options and that raises an exception if the file cannot be read. """ def optionxform(self, optionstr): return optionstr def read(self, filenames): if isinstance(filenames, basestring): filenames = [filenames] for filename in filenames: # try: # fp = open(filename) # except IOError: # continue fp = open(filename) try: self._read(fp, filename) finally: fp.close() def as_dict(self, raw=False, vars=None): """Convert an INI file to a dictionary""" # Load INI file into a dict result = {} for section in self.sections(): if section not in result: result[section] = {} for option in self.options(section): value = self.get(section, option, raw=raw, vars=vars) try: value = unrepr(value) except Exception: x = sys.exc_info()[1] msg = ("Config error in section: %r, option: %r, " "value: %r. Config values must be valid Python." % (section, option, value)) raise ValueError(msg, x.__class__.__name__, x.args) result[section][option] = value return result def dict_from_file(self, file): if hasattr(file, 'read'): self.readfp(file) else: self.read(file) return self.as_dict() # public domain "unrepr" implementation, found on the web and then improved. class _Builder2: def build(self, o): m = getattr(self, 'build_' + o.__class__.__name__, None) if m is None: raise TypeError("unrepr does not recognize %s" % repr(o.__class__.__name__)) return m(o) def astnode(self, s): """Return a Python2 ast Node compiled from a string.""" try: import compiler except ImportError: # Fallback to eval when compiler package is not available, # e.g. IronPython 1.0. return eval(s) p = compiler.parse("__tempvalue__ = " + s) return p.getChildren()[1].getChildren()[0].getChildren()[1] def build_Subscript(self, o): expr, flags, subs = o.getChildren() expr = self.build(expr) subs = self.build(subs) return expr[subs] def build_CallFunc(self, o): children = map(self.build, o.getChildren()) callee = children.pop(0) kwargs = children.pop() or {} starargs = children.pop() or () args = tuple(children) + tuple(starargs) return callee(*args, **kwargs) def build_List(self, o): return map(self.build, o.getChildren()) def build_Const(self, o): return o.value def build_Dict(self, o): d = {} i = iter(map(self.build, o.getChildren())) for el in i: d[el] = i.next() return d def build_Tuple(self, o): return tuple(self.build_List(o)) def build_Name(self, o): name = o.name if name == 'None': return None if name == 'True': return True if name == 'False': return False # See if the Name is a package or module. If it is, import it. try: return modules(name) except ImportError: pass # See if the Name is in builtins. try: return getattr(builtins, name) except AttributeError: pass raise TypeError("unrepr could not resolve the name %s" % repr(name)) def build_Add(self, o): left, right = map(self.build, o.getChildren()) return left + right def build_Mul(self, o): left, right = map(self.build, o.getChildren()) return left * right def build_Getattr(self, o): parent = self.build(o.expr) return getattr(parent, o.attrname) def build_NoneType(self, o): return None def build_UnarySub(self, o): return -self.build(o.getChildren()[0]) def build_UnaryAdd(self, o): return self.build(o.getChildren()[0]) class _Builder3: def build(self, o): m = getattr(self, 'build_' + o.__class__.__name__, None) if m is None: raise TypeError("unrepr does not recognize %s" % repr(o.__class__.__name__)) return m(o) def astnode(self, s): """Return a Python3 ast Node compiled from a string.""" try: import ast except ImportError: # Fallback to eval when ast package is not available, # e.g. IronPython 1.0. return eval(s) p = ast.parse("__tempvalue__ = " + s) return p.body[0].value def build_Subscript(self, o): return self.build(o.value)[self.build(o.slice)] def build_Index(self, o): return self.build(o.value) def build_Call(self, o): callee = self.build(o.func) if o.args is None: args = () else: args = tuple([self.build(a) for a in o.args]) if o.starargs is None: starargs = () else: starargs = self.build(o.starargs) if o.kwargs is None: kwargs = {} else: kwargs = self.build(o.kwargs) return callee(*(args + starargs), **kwargs) def build_List(self, o): return list(map(self.build, o.elts)) def build_Str(self, o): return o.s def build_Num(self, o): return o.n def build_Dict(self, o): return dict([(self.build(k), self.build(v)) for k, v in zip(o.keys, o.values)]) def build_Tuple(self, o): return tuple(self.build_List(o)) def build_Name(self, o): name = o.id if name == 'None': return None if name == 'True': return True if name == 'False': return False # See if the Name is a package or module. If it is, import it. try: return modules(name) except ImportError: pass # See if the Name is in builtins. try: import builtins return getattr(builtins, name) except AttributeError: pass raise TypeError("unrepr could not resolve the name %s" % repr(name)) def build_UnaryOp(self, o): op, operand = map(self.build, [o.op, o.operand]) return op(operand) def build_BinOp(self, o): left, op, right = map(self.build, [o.left, o.op, o.right]) return op(left, right) def build_Add(self, o): return _operator.add def build_Mult(self, o): return _operator.mul def build_USub(self, o): return _operator.neg def build_Attribute(self, o): parent = self.build(o.value) return getattr(parent, o.attr) def build_NoneType(self, o): return None def unrepr(s): """Return a Python object compiled from a string.""" if not s: return s if sys.version_info < (3, 0): b = _Builder2() else: b = _Builder3() obj = b.astnode(s) return b.build(obj) def modules(modulePath): """Load a module and retrieve a reference to that module.""" try: mod = sys.modules[modulePath] if mod is None: raise KeyError() except KeyError: # The last [''] is important. mod = __import__(modulePath, globals(), locals(), ['']) return mod def attributes(full_attribute_name): """Load a module and retrieve an attribute of that module.""" # Parse out the path, module, and attribute last_dot = full_attribute_name.rfind(".") attr_name = full_attribute_name[last_dot + 1:] mod_path = full_attribute_name[:last_dot] mod = modules(mod_path) # Let an AttributeError propagate outward. try: attr = getattr(mod, attr_name) except AttributeError: raise AttributeError("'%s' object has no attribute '%s'" % (mod_path, attr_name)) # Return a reference to the attribute. return attr
lgpl-3.0
ychen820/microblog
flask/lib/python2.7/site-packages/wtforms/utils.py
194
1504
class UnsetValue(object): """ An unset value. This is used in situations where a blank value like `None` is acceptable usually as the default value of a class variable or function parameter (iow, usually when `None` is a valid value.) """ def __str__(self): return '<unset value>' def __repr__(self): return '<unset value>' def __bool__(self): return False def __nonzero__(self): return False unset_value = UnsetValue() class WebobInputWrapper(object): """ Wrap a webob MultiDict for use as passing as `formdata` to Field. Since for consistency, we have decided in WTForms to support as input a small subset of the API provided in common between cgi.FieldStorage, Django's QueryDict, and Werkzeug's MultiDict, we need to wrap Webob, the only supported framework whose multidict does not fit this API, but is nevertheless used by a lot of frameworks. While we could write a full wrapper to support all the methods, this will undoubtedly result in bugs due to some subtle differences between the various wrappers. So we will keep it simple. """ def __init__(self, multidict): self._wrapped = multidict def __iter__(self): return iter(self._wrapped) def __len__(self): return len(self._wrapped) def __contains__(self, name): return (name in self._wrapped) def getlist(self, name): return self._wrapped.getall(name)
bsd-3-clause
Rodolfoarv/Mars-Explorer-AI-with-Python-
env/lib/python2.7/site-packages/pip/req/req_install.py
21
46670
from __future__ import absolute_import import logging import os import re import shutil import sys import tempfile import traceback import warnings import zipfile from distutils import sysconfig from distutils.util import change_root from email.parser import FeedParser from pip._vendor import pkg_resources, six from pip._vendor.distlib.markers import interpret as markers_interpret from pip._vendor.packaging import specifiers from pip._vendor.six.moves import configparser import pip.wheel from pip.compat import native_str, get_stdlib, WINDOWS from pip.download import is_url, url_to_path, path_to_url, is_archive_file from pip.exceptions import ( InstallationError, UninstallationError, UnsupportedWheel, ) from pip.locations import ( bin_py, running_under_virtualenv, PIP_DELETE_MARKER_FILENAME, bin_user, ) from pip.utils import ( display_path, rmtree, ask_path_exists, backup_dir, is_installable_dir, dist_in_usersite, dist_in_site_packages, egg_link_path, call_subprocess, read_text_file, FakeFile, _make_build_dir, ensure_dir, get_installed_version, canonicalize_name, normalize_path, dist_is_local, ) from pip.utils.hashes import Hashes from pip.utils.deprecation import RemovedInPip10Warning from pip.utils.logging import indent_log from pip.utils.setuptools_build import SETUPTOOLS_SHIM from pip.utils.ui import open_spinner from pip.req.req_uninstall import UninstallPathSet from pip.vcs import vcs from pip.wheel import move_wheel_files, Wheel from pip._vendor.packaging.version import Version logger = logging.getLogger(__name__) operators = specifiers.Specifier._operators.keys() def _strip_extras(path): m = re.match(r'^(.+)(\[[^\]]+\])$', path) extras = None if m: path_no_extras = m.group(1) extras = m.group(2) else: path_no_extras = path return path_no_extras, extras class InstallRequirement(object): def __init__(self, req, comes_from, source_dir=None, editable=False, link=None, as_egg=False, update=True, editable_options=None, pycompile=True, markers=None, isolated=False, options=None, wheel_cache=None, constraint=False): self.extras = () if isinstance(req, six.string_types): try: req = pkg_resources.Requirement.parse(req) except pkg_resources.RequirementParseError: if os.path.sep in req: add_msg = "It looks like a path. Does it exist ?" elif '=' in req and not any(op in req for op in operators): add_msg = "= is not a valid operator. Did you mean == ?" else: add_msg = traceback.format_exc() raise InstallationError( "Invalid requirement: '%s'\n%s" % (req, add_msg)) self.extras = req.extras self.req = req self.comes_from = comes_from self.constraint = constraint self.source_dir = source_dir self.editable = editable if editable_options is None: editable_options = {} self.editable_options = editable_options self._wheel_cache = wheel_cache self.link = self.original_link = link self.as_egg = as_egg self.markers = markers self._egg_info_path = None # This holds the pkg_resources.Distribution object if this requirement # is already available: self.satisfied_by = None # This hold the pkg_resources.Distribution object if this requirement # conflicts with another installed distribution: self.conflicts_with = None # Temporary build location self._temp_build_dir = None # Used to store the global directory where the _temp_build_dir should # have been created. Cf _correct_build_location method. self._ideal_build_dir = None # True if the editable should be updated: self.update = update # Set to True after successful installation self.install_succeeded = None # UninstallPathSet of uninstalled distribution (for possible rollback) self.uninstalled = None # Set True if a legitimate do-nothing-on-uninstall has happened - e.g. # system site packages, stdlib packages. self.nothing_to_uninstall = False self.use_user_site = False self.target_dir = None self.options = options if options else {} self.pycompile = pycompile # Set to True after successful preparation of this requirement self.prepared = False self.isolated = isolated @classmethod def from_editable(cls, editable_req, comes_from=None, default_vcs=None, isolated=False, options=None, wheel_cache=None, constraint=False): from pip.index import Link name, url, extras_override, editable_options = parse_editable( editable_req, default_vcs) if url.startswith('file:'): source_dir = url_to_path(url) else: source_dir = None res = cls(name, comes_from, source_dir=source_dir, editable=True, link=Link(url), constraint=constraint, editable_options=editable_options, isolated=isolated, options=options if options else {}, wheel_cache=wheel_cache) if extras_override is not None: res.extras = extras_override return res @classmethod def from_line( cls, name, comes_from=None, isolated=False, options=None, wheel_cache=None, constraint=False): """Creates an InstallRequirement from a name, which might be a requirement, directory containing 'setup.py', filename, or URL. """ from pip.index import Link if is_url(name): marker_sep = '; ' else: marker_sep = ';' if marker_sep in name: name, markers = name.split(marker_sep, 1) markers = markers.strip() if not markers: markers = None else: markers = None name = name.strip() req = None path = os.path.normpath(os.path.abspath(name)) link = None extras = None if is_url(name): link = Link(name) else: p, extras = _strip_extras(path) if (os.path.isdir(p) and (os.path.sep in name or name.startswith('.'))): if not is_installable_dir(p): raise InstallationError( "Directory %r is not installable. File 'setup.py' " "not found." % name ) link = Link(path_to_url(p)) elif is_archive_file(p): if not os.path.isfile(p): logger.warning( 'Requirement %r looks like a filename, but the ' 'file does not exist', name ) link = Link(path_to_url(p)) # it's a local file, dir, or url if link: # Handle relative file URLs if link.scheme == 'file' and re.search(r'\.\./', link.url): link = Link( path_to_url(os.path.normpath(os.path.abspath(link.path)))) # wheel file if link.is_wheel: wheel = Wheel(link.filename) # can raise InvalidWheelFilename if not wheel.supported(): raise UnsupportedWheel( "%s is not a supported wheel on this platform." % wheel.filename ) req = "%s==%s" % (wheel.name, wheel.version) else: # set the req to the egg fragment. when it's not there, this # will become an 'unnamed' requirement req = link.egg_fragment # a requirement specifier else: req = name options = options if options else {} res = cls(req, comes_from, link=link, markers=markers, isolated=isolated, options=options, wheel_cache=wheel_cache, constraint=constraint) if extras: res.extras = pkg_resources.Requirement.parse('__placeholder__' + extras).extras return res def __str__(self): if self.req: s = str(self.req) if self.link: s += ' from %s' % self.link.url else: s = self.link.url if self.link else None if self.satisfied_by is not None: s += ' in %s' % display_path(self.satisfied_by.location) if self.comes_from: if isinstance(self.comes_from, six.string_types): comes_from = self.comes_from else: comes_from = self.comes_from.from_path() if comes_from: s += ' (from %s)' % comes_from return s def __repr__(self): return '<%s object: %s editable=%r>' % ( self.__class__.__name__, str(self), self.editable) def populate_link(self, finder, upgrade, require_hashes): """Ensure that if a link can be found for this, that it is found. Note that self.link may still be None - if Upgrade is False and the requirement is already installed. If require_hashes is True, don't use the wheel cache, because cached wheels, always built locally, have different hashes than the files downloaded from the index server and thus throw false hash mismatches. Furthermore, cached wheels at present have undeterministic contents due to file modification times. """ if self.link is None: self.link = finder.find_requirement(self, upgrade) if self._wheel_cache is not None and not require_hashes: old_link = self.link self.link = self._wheel_cache.cached_wheel(self.link, self.name) if old_link != self.link: logger.debug('Using cached wheel link: %s', self.link) @property def specifier(self): return self.req.specifier @property def is_pinned(self): """Return whether I am pinned to an exact version. For example, some-package==1.2 is pinned; some-package>1.2 is not. """ specifiers = self.specifier return (len(specifiers) == 1 and next(iter(specifiers)).operator in ('==', '===')) def from_path(self): if self.req is None: return None s = str(self.req) if self.comes_from: if isinstance(self.comes_from, six.string_types): comes_from = self.comes_from else: comes_from = self.comes_from.from_path() if comes_from: s += '->' + comes_from return s def build_location(self, build_dir): if self._temp_build_dir is not None: return self._temp_build_dir if self.req is None: # for requirement via a path to a directory: the name of the # package is not available yet so we create a temp directory # Once run_egg_info will have run, we'll be able # to fix it via _correct_build_location self._temp_build_dir = tempfile.mkdtemp('-build', 'pip-') self._ideal_build_dir = build_dir return self._temp_build_dir if self.editable: name = self.name.lower() else: name = self.name # FIXME: Is there a better place to create the build_dir? (hg and bzr # need this) if not os.path.exists(build_dir): logger.debug('Creating directory %s', build_dir) _make_build_dir(build_dir) return os.path.join(build_dir, name) def _correct_build_location(self): """Move self._temp_build_dir to self._ideal_build_dir/self.req.name For some requirements (e.g. a path to a directory), the name of the package is not available until we run egg_info, so the build_location will return a temporary directory and store the _ideal_build_dir. This is only called by self.egg_info_path to fix the temporary build directory. """ if self.source_dir is not None: return assert self.req is not None assert self._temp_build_dir assert self._ideal_build_dir old_location = self._temp_build_dir self._temp_build_dir = None new_location = self.build_location(self._ideal_build_dir) if os.path.exists(new_location): raise InstallationError( 'A package already exists in %s; please remove it to continue' % display_path(new_location)) logger.debug( 'Moving package %s from %s to new location %s', self, display_path(old_location), display_path(new_location), ) shutil.move(old_location, new_location) self._temp_build_dir = new_location self._ideal_build_dir = None self.source_dir = new_location self._egg_info_path = None @property def name(self): if self.req is None: return None return native_str(self.req.project_name) @property def setup_py(self): assert self.source_dir, "No source dir for %s" % self try: import setuptools # noqa except ImportError: if get_installed_version('setuptools') is None: add_msg = "Please install setuptools." else: add_msg = traceback.format_exc() # Setuptools is not available raise InstallationError( "Could not import setuptools which is required to " "install from a source distribution.\n%s" % add_msg ) setup_file = 'setup.py' if self.editable_options and 'subdirectory' in self.editable_options: setup_py = os.path.join(self.source_dir, self.editable_options['subdirectory'], setup_file) else: setup_py = os.path.join(self.source_dir, setup_file) # Python2 __file__ should not be unicode if six.PY2 and isinstance(setup_py, six.text_type): setup_py = setup_py.encode(sys.getfilesystemencoding()) return setup_py def run_egg_info(self): assert self.source_dir if self.name: logger.debug( 'Running setup.py (path:%s) egg_info for package %s', self.setup_py, self.name, ) else: logger.debug( 'Running setup.py (path:%s) egg_info for package from %s', self.setup_py, self.link, ) with indent_log(): script = SETUPTOOLS_SHIM % self.setup_py base_cmd = [sys.executable, '-c', script] if self.isolated: base_cmd += ["--no-user-cfg"] egg_info_cmd = base_cmd + ['egg_info'] # We can't put the .egg-info files at the root, because then the # source code will be mistaken for an installed egg, causing # problems if self.editable: egg_base_option = [] else: egg_info_dir = os.path.join(self.source_dir, 'pip-egg-info') ensure_dir(egg_info_dir) egg_base_option = ['--egg-base', 'pip-egg-info'] cwd = self.source_dir if self.editable_options and \ 'subdirectory' in self.editable_options: cwd = os.path.join(cwd, self.editable_options['subdirectory']) call_subprocess( egg_info_cmd + egg_base_option, cwd=cwd, show_stdout=False, command_level=logging.DEBUG, command_desc='python setup.py egg_info') if not self.req: if isinstance( pkg_resources.parse_version(self.pkg_info()["Version"]), Version): op = "==" else: op = "===" self.req = pkg_resources.Requirement.parse( "".join([ self.pkg_info()["Name"], op, self.pkg_info()["Version"], ])) self._correct_build_location() else: metadata_name = canonicalize_name(self.pkg_info()["Name"]) if canonicalize_name(self.req.project_name) != metadata_name: logger.warning( 'Running setup.py (path:%s) egg_info for package %s ' 'produced metadata for project name %s. Fix your ' '#egg=%s fragments.', self.setup_py, self.name, metadata_name, self.name ) self.req = pkg_resources.Requirement.parse(metadata_name) def egg_info_data(self, filename): if self.satisfied_by is not None: if not self.satisfied_by.has_metadata(filename): return None return self.satisfied_by.get_metadata(filename) assert self.source_dir filename = self.egg_info_path(filename) if not os.path.exists(filename): return None data = read_text_file(filename) return data def egg_info_path(self, filename): if self._egg_info_path is None: if self.editable: base = self.source_dir else: base = os.path.join(self.source_dir, 'pip-egg-info') filenames = os.listdir(base) if self.editable: filenames = [] for root, dirs, files in os.walk(base): for dir in vcs.dirnames: if dir in dirs: dirs.remove(dir) # Iterate over a copy of ``dirs``, since mutating # a list while iterating over it can cause trouble. # (See https://github.com/pypa/pip/pull/462.) for dir in list(dirs): # Don't search in anything that looks like a virtualenv # environment if ( os.path.exists( os.path.join(root, dir, 'bin', 'python') ) or os.path.exists( os.path.join( root, dir, 'Scripts', 'Python.exe' ) )): dirs.remove(dir) # Also don't search through tests elif dir == 'test' or dir == 'tests': dirs.remove(dir) filenames.extend([os.path.join(root, dir) for dir in dirs]) filenames = [f for f in filenames if f.endswith('.egg-info')] if not filenames: raise InstallationError( 'No files/directories in %s (from %s)' % (base, filename) ) assert filenames, \ "No files/directories in %s (from %s)" % (base, filename) # if we have more than one match, we pick the toplevel one. This # can easily be the case if there is a dist folder which contains # an extracted tarball for testing purposes. if len(filenames) > 1: filenames.sort( key=lambda x: x.count(os.path.sep) + (os.path.altsep and x.count(os.path.altsep) or 0) ) self._egg_info_path = os.path.join(base, filenames[0]) return os.path.join(self._egg_info_path, filename) def pkg_info(self): p = FeedParser() data = self.egg_info_data('PKG-INFO') if not data: logger.warning( 'No PKG-INFO file found in %s', display_path(self.egg_info_path('PKG-INFO')), ) p.feed(data or '') return p.close() _requirements_section_re = re.compile(r'\[(.*?)\]') @property def installed_version(self): return get_installed_version(self.name) def assert_source_matches_version(self): assert self.source_dir version = self.pkg_info()['version'] if version not in self.req: logger.warning( 'Requested %s, but installing version %s', self, self.installed_version, ) else: logger.debug( 'Source in %s has version %s, which satisfies requirement %s', display_path(self.source_dir), version, self, ) def update_editable(self, obtain=True): if not self.link: logger.debug( "Cannot update repository at %s; repository location is " "unknown", self.source_dir, ) return assert self.editable assert self.source_dir if self.link.scheme == 'file': # Static paths don't get updated return assert '+' in self.link.url, "bad url: %r" % self.link.url if not self.update: return vc_type, url = self.link.url.split('+', 1) backend = vcs.get_backend(vc_type) if backend: vcs_backend = backend(self.link.url) if obtain: vcs_backend.obtain(self.source_dir) else: vcs_backend.export(self.source_dir) else: assert 0, ( 'Unexpected version control type (in %s): %s' % (self.link, vc_type)) def uninstall(self, auto_confirm=False): """ Uninstall the distribution currently satisfying this requirement. Prompts before removing or modifying files unless ``auto_confirm`` is True. Refuses to delete or modify files outside of ``sys.prefix`` - thus uninstallation within a virtual environment can only modify that virtual environment, even if the virtualenv is linked to global site-packages. """ if not self.check_if_exists(): raise UninstallationError( "Cannot uninstall requirement %s, not installed" % (self.name,) ) dist = self.satisfied_by or self.conflicts_with dist_path = normalize_path(dist.location) if not dist_is_local(dist): logger.info( "Not uninstalling %s at %s, outside environment %s", dist.key, dist_path, sys.prefix, ) self.nothing_to_uninstall = True return if dist_path in get_stdlib(): logger.info( "Not uninstalling %s at %s, as it is in the standard library.", dist.key, dist_path, ) self.nothing_to_uninstall = True return paths_to_remove = UninstallPathSet(dist) develop_egg_link = egg_link_path(dist) develop_egg_link_egg_info = '{0}.egg-info'.format( pkg_resources.to_filename(dist.project_name)) egg_info_exists = dist.egg_info and os.path.exists(dist.egg_info) # Special case for distutils installed package distutils_egg_info = getattr(dist._provider, 'path', None) # Uninstall cases order do matter as in the case of 2 installs of the # same package, pip needs to uninstall the currently detected version if (egg_info_exists and dist.egg_info.endswith('.egg-info') and not dist.egg_info.endswith(develop_egg_link_egg_info)): # if dist.egg_info.endswith(develop_egg_link_egg_info), we # are in fact in the develop_egg_link case paths_to_remove.add(dist.egg_info) if dist.has_metadata('installed-files.txt'): for installed_file in dist.get_metadata( 'installed-files.txt').splitlines(): path = os.path.normpath( os.path.join(dist.egg_info, installed_file) ) paths_to_remove.add(path) # FIXME: need a test for this elif block # occurs with --single-version-externally-managed/--record outside # of pip elif dist.has_metadata('top_level.txt'): if dist.has_metadata('namespace_packages.txt'): namespaces = dist.get_metadata('namespace_packages.txt') else: namespaces = [] for top_level_pkg in [ p for p in dist.get_metadata('top_level.txt').splitlines() if p and p not in namespaces]: path = os.path.join(dist.location, top_level_pkg) paths_to_remove.add(path) paths_to_remove.add(path + '.py') paths_to_remove.add(path + '.pyc') paths_to_remove.add(path + '.pyo') elif distutils_egg_info: warnings.warn( "Uninstalling a distutils installed project ({0}) has been " "deprecated and will be removed in a future version. This is " "due to the fact that uninstalling a distutils project will " "only partially uninstall the project.".format(self.name), RemovedInPip10Warning, ) paths_to_remove.add(distutils_egg_info) elif dist.location.endswith('.egg'): # package installed by easy_install # We cannot match on dist.egg_name because it can slightly vary # i.e. setuptools-0.6c11-py2.6.egg vs setuptools-0.6rc11-py2.6.egg paths_to_remove.add(dist.location) easy_install_egg = os.path.split(dist.location)[1] easy_install_pth = os.path.join(os.path.dirname(dist.location), 'easy-install.pth') paths_to_remove.add_pth(easy_install_pth, './' + easy_install_egg) elif develop_egg_link: # develop egg with open(develop_egg_link, 'r') as fh: link_pointer = os.path.normcase(fh.readline().strip()) assert (link_pointer == dist.location), ( 'Egg-link %s does not match installed location of %s ' '(at %s)' % (link_pointer, self.name, dist.location) ) paths_to_remove.add(develop_egg_link) easy_install_pth = os.path.join(os.path.dirname(develop_egg_link), 'easy-install.pth') paths_to_remove.add_pth(easy_install_pth, dist.location) elif egg_info_exists and dist.egg_info.endswith('.dist-info'): for path in pip.wheel.uninstallation_paths(dist): paths_to_remove.add(path) else: logger.debug( 'Not sure how to uninstall: %s - Check: %s', dist, dist.location) # find distutils scripts= scripts if dist.has_metadata('scripts') and dist.metadata_isdir('scripts'): for script in dist.metadata_listdir('scripts'): if dist_in_usersite(dist): bin_dir = bin_user else: bin_dir = bin_py paths_to_remove.add(os.path.join(bin_dir, script)) if WINDOWS: paths_to_remove.add(os.path.join(bin_dir, script) + '.bat') # find console_scripts if dist.has_metadata('entry_points.txt'): config = configparser.SafeConfigParser() config.readfp( FakeFile(dist.get_metadata_lines('entry_points.txt')) ) if config.has_section('console_scripts'): for name, value in config.items('console_scripts'): if dist_in_usersite(dist): bin_dir = bin_user else: bin_dir = bin_py paths_to_remove.add(os.path.join(bin_dir, name)) if WINDOWS: paths_to_remove.add( os.path.join(bin_dir, name) + '.exe' ) paths_to_remove.add( os.path.join(bin_dir, name) + '.exe.manifest' ) paths_to_remove.add( os.path.join(bin_dir, name) + '-script.py' ) paths_to_remove.remove(auto_confirm) self.uninstalled = paths_to_remove def rollback_uninstall(self): if self.uninstalled: self.uninstalled.rollback() else: logger.error( "Can't rollback %s, nothing uninstalled.", self.name, ) def commit_uninstall(self): if self.uninstalled: self.uninstalled.commit() elif not self.nothing_to_uninstall: logger.error( "Can't commit %s, nothing uninstalled.", self.name, ) def archive(self, build_dir): assert self.source_dir create_archive = True archive_name = '%s-%s.zip' % (self.name, self.pkg_info()["version"]) archive_path = os.path.join(build_dir, archive_name) if os.path.exists(archive_path): response = ask_path_exists( 'The file %s exists. (i)gnore, (w)ipe, (b)ackup ' % display_path(archive_path), ('i', 'w', 'b')) if response == 'i': create_archive = False elif response == 'w': logger.warning('Deleting %s', display_path(archive_path)) os.remove(archive_path) elif response == 'b': dest_file = backup_dir(archive_path) logger.warning( 'Backing up %s to %s', display_path(archive_path), display_path(dest_file), ) shutil.move(archive_path, dest_file) if create_archive: zip = zipfile.ZipFile( archive_path, 'w', zipfile.ZIP_DEFLATED, allowZip64=True ) dir = os.path.normcase(os.path.abspath(self.source_dir)) for dirpath, dirnames, filenames in os.walk(dir): if 'pip-egg-info' in dirnames: dirnames.remove('pip-egg-info') for dirname in dirnames: dirname = os.path.join(dirpath, dirname) name = self._clean_zip_name(dirname, dir) zipdir = zipfile.ZipInfo(self.name + '/' + name + '/') zipdir.external_attr = 0x1ED << 16 # 0o755 zip.writestr(zipdir, '') for filename in filenames: if filename == PIP_DELETE_MARKER_FILENAME: continue filename = os.path.join(dirpath, filename) name = self._clean_zip_name(filename, dir) zip.write(filename, self.name + '/' + name) zip.close() logger.info('Saved %s', display_path(archive_path)) def _clean_zip_name(self, name, prefix): assert name.startswith(prefix + os.path.sep), ( "name %r doesn't start with prefix %r" % (name, prefix) ) name = name[len(prefix) + 1:] name = name.replace(os.path.sep, '/') return name def match_markers(self): if self.markers is not None: return markers_interpret(self.markers) else: return True def install(self, install_options, global_options=[], root=None, prefix=None): if self.editable: self.install_editable( install_options, global_options, prefix=prefix) return if self.is_wheel: version = pip.wheel.wheel_version(self.source_dir) pip.wheel.check_compatibility(version, self.name) self.move_wheel_files(self.source_dir, root=root, prefix=prefix) self.install_succeeded = True return # Extend the list of global and install options passed on to # the setup.py call with the ones from the requirements file. # Options specified in requirements file override those # specified on the command line, since the last option given # to setup.py is the one that is used. global_options += self.options.get('global_options', []) install_options += self.options.get('install_options', []) if self.isolated: global_options = list(global_options) + ["--no-user-cfg"] temp_location = tempfile.mkdtemp('-record', 'pip-') record_filename = os.path.join(temp_location, 'install-record.txt') try: install_args = [sys.executable, "-u"] install_args.append('-c') install_args.append(SETUPTOOLS_SHIM % self.setup_py) install_args += list(global_options) + \ ['install', '--record', record_filename] if not self.as_egg: install_args += ['--single-version-externally-managed'] if root is not None: install_args += ['--root', root] if prefix is not None: install_args += ['--prefix', prefix] if self.pycompile: install_args += ["--compile"] else: install_args += ["--no-compile"] if running_under_virtualenv(): py_ver_str = 'python' + sysconfig.get_python_version() install_args += ['--install-headers', os.path.join(sys.prefix, 'include', 'site', py_ver_str, self.name)] msg = 'Running setup.py install for %s' % (self.name,) with open_spinner(msg) as spinner: with indent_log(): call_subprocess( install_args + install_options, cwd=self.source_dir, show_stdout=False, spinner=spinner, ) if not os.path.exists(record_filename): logger.debug('Record file %s not found', record_filename) return self.install_succeeded = True if self.as_egg: # there's no --always-unzip option we can pass to install # command so we unable to save the installed-files.txt return def prepend_root(path): if root is None or not os.path.isabs(path): return path else: return change_root(root, path) with open(record_filename) as f: for line in f: directory = os.path.dirname(line) if directory.endswith('.egg-info'): egg_info_dir = prepend_root(directory) break else: logger.warning( 'Could not find .egg-info directory in install record' ' for %s', self, ) # FIXME: put the record somewhere # FIXME: should this be an error? return new_lines = [] with open(record_filename) as f: for line in f: filename = line.strip() if os.path.isdir(filename): filename += os.path.sep new_lines.append( os.path.relpath( prepend_root(filename), egg_info_dir) ) inst_files_path = os.path.join(egg_info_dir, 'installed-files.txt') with open(inst_files_path, 'w') as f: f.write('\n'.join(new_lines) + '\n') finally: if os.path.exists(record_filename): os.remove(record_filename) rmtree(temp_location) def ensure_has_source_dir(self, parent_dir): """Ensure that a source_dir is set. This will create a temporary build dir if the name of the requirement isn't known yet. :param parent_dir: The ideal pip parent_dir for the source_dir. Generally src_dir for editables and build_dir for sdists. :return: self.source_dir """ if self.source_dir is None: self.source_dir = self.build_location(parent_dir) return self.source_dir def remove_temporary_source(self): """Remove the source files from this requirement, if they are marked for deletion""" if self.source_dir and os.path.exists( os.path.join(self.source_dir, PIP_DELETE_MARKER_FILENAME)): logger.debug('Removing source in %s', self.source_dir) rmtree(self.source_dir) self.source_dir = None if self._temp_build_dir and os.path.exists(self._temp_build_dir): rmtree(self._temp_build_dir) self._temp_build_dir = None def install_editable(self, install_options, global_options=(), prefix=None): logger.info('Running setup.py develop for %s', self.name) if self.isolated: global_options = list(global_options) + ["--no-user-cfg"] if prefix: prefix_param = ['--prefix={0}'.format(prefix)] install_options = list(install_options) + prefix_param with indent_log(): # FIXME: should we do --install-headers here too? cwd = self.source_dir if self.editable_options and \ 'subdirectory' in self.editable_options: cwd = os.path.join(cwd, self.editable_options['subdirectory']) call_subprocess( [ sys.executable, '-c', SETUPTOOLS_SHIM % self.setup_py ] + list(global_options) + ['develop', '--no-deps'] + list(install_options), cwd=cwd, show_stdout=False) self.install_succeeded = True def check_if_exists(self): """Find an installed distribution that satisfies or conflicts with this requirement, and set self.satisfied_by or self.conflicts_with appropriately. """ if self.req is None: return False try: self.satisfied_by = pkg_resources.get_distribution(self.req) except pkg_resources.DistributionNotFound: return False except pkg_resources.VersionConflict: existing_dist = pkg_resources.get_distribution( self.req.project_name ) if self.use_user_site: if dist_in_usersite(existing_dist): self.conflicts_with = existing_dist elif (running_under_virtualenv() and dist_in_site_packages(existing_dist)): raise InstallationError( "Will not install to the user site because it will " "lack sys.path precedence to %s in %s" % (existing_dist.project_name, existing_dist.location) ) else: self.conflicts_with = existing_dist return True @property def is_wheel(self): return self.link and self.link.is_wheel def move_wheel_files(self, wheeldir, root=None, prefix=None): move_wheel_files( self.name, self.req, wheeldir, user=self.use_user_site, home=self.target_dir, root=root, prefix=prefix, pycompile=self.pycompile, isolated=self.isolated, ) def get_dist(self): """Return a pkg_resources.Distribution built from self.egg_info_path""" egg_info = self.egg_info_path('').rstrip('/') base_dir = os.path.dirname(egg_info) metadata = pkg_resources.PathMetadata(base_dir, egg_info) dist_name = os.path.splitext(os.path.basename(egg_info))[0] return pkg_resources.Distribution( os.path.dirname(egg_info), project_name=dist_name, metadata=metadata) @property def has_hash_options(self): """Return whether any known-good hashes are specified as options. These activate --require-hashes mode; hashes specified as part of a URL do not. """ return bool(self.options.get('hashes', {})) def hashes(self, trust_internet=True): """Return a hash-comparer that considers my option- and URL-based hashes to be known-good. Hashes in URLs--ones embedded in the requirements file, not ones downloaded from an index server--are almost peers with ones from flags. They satisfy --require-hashes (whether it was implicitly or explicitly activated) but do not activate it. md5 and sha224 are not allowed in flags, which should nudge people toward good algos. We always OR all hashes together, even ones from URLs. :param trust_internet: Whether to trust URL-based (#md5=...) hashes downloaded from the internet, as by populate_link() """ good_hashes = self.options.get('hashes', {}).copy() link = self.link if trust_internet else self.original_link if link and link.hash: good_hashes.setdefault(link.hash_name, []).append(link.hash) return Hashes(good_hashes) def _strip_postfix(req): """ Strip req postfix ( -dev, 0.2, etc ) """ # FIXME: use package_to_requirement? match = re.search(r'^(.*?)(?:-dev|-\d.*)$', req) if match: # Strip off -dev, -0.2, etc. req = match.group(1) return req def _build_req_from_url(url): parts = [p for p in url.split('#', 1)[0].split('/') if p] req = None if parts[-2] in ('tags', 'branches', 'tag', 'branch'): req = parts[-3] elif parts[-1] == 'trunk': req = parts[-2] return req def _build_editable_options(req): """ This method generates a dictionary of the query string parameters contained in a given editable URL. """ regexp = re.compile(r"[\?#&](?P<name>[^&=]+)=(?P<value>[^&=]+)") matched = regexp.findall(req) if matched: ret = dict() for option in matched: (name, value) = option if name in ret: raise Exception("%s option already defined" % name) ret[name] = value return ret return None def parse_editable(editable_req, default_vcs=None): """Parses an editable requirement into: - a requirement name - an URL - extras - editable options Accepted requirements: svn+http://blahblah@rev#egg=Foobar[baz]&subdirectory=version_subdir .[some_extra] """ from pip.index import Link url = editable_req extras = None # If a file path is specified with extras, strip off the extras. m = re.match(r'^(.+)(\[[^\]]+\])$', url) if m: url_no_extras = m.group(1) extras = m.group(2) else: url_no_extras = url if os.path.isdir(url_no_extras): if not os.path.exists(os.path.join(url_no_extras, 'setup.py')): raise InstallationError( "Directory %r is not installable. File 'setup.py' not found." % url_no_extras ) # Treating it as code that has already been checked out url_no_extras = path_to_url(url_no_extras) if url_no_extras.lower().startswith('file:'): package_name = Link(url_no_extras).egg_fragment if extras: return ( package_name, url_no_extras, pkg_resources.Requirement.parse( '__placeholder__' + extras ).extras, {}, ) else: return package_name, url_no_extras, None, {} for version_control in vcs: if url.lower().startswith('%s:' % version_control): url = '%s+%s' % (version_control, url) break if '+' not in url: if default_vcs: url = default_vcs + '+' + url else: raise InstallationError( '%s should either be a path to a local project or a VCS url ' 'beginning with svn+, git+, hg+, or bzr+' % editable_req ) vc_type = url.split('+', 1)[0].lower() if not vcs.get_backend(vc_type): error_message = 'For --editable=%s only ' % editable_req + \ ', '.join([backend.name + '+URL' for backend in vcs.backends]) + \ ' is currently supported' raise InstallationError(error_message) try: options = _build_editable_options(editable_req) except Exception as exc: raise InstallationError( '--editable=%s error in editable options:%s' % (editable_req, exc) ) if not options or 'egg' not in options: req = _build_req_from_url(editable_req) if not req: raise InstallationError( '--editable=%s is not the right format; it must have ' '#egg=Package' % editable_req ) else: req = options['egg'] package = _strip_postfix(req) return package, url, None, options
mit
endlessm/chromium-browser
third_party/chromite/third_party/requests/packages/chardet/euctwfreq.py
3133
34872
######################## BEGIN LICENSE BLOCK ######################## # The Original Code is Mozilla Communicator client code. # # The Initial Developer of the Original Code is # Netscape Communications Corporation. # Portions created by the Initial Developer are Copyright (C) 1998 # the Initial Developer. All Rights Reserved. # # Contributor(s): # Mark Pilgrim - port to Python # # This library 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 2.1 of the License, or (at your option) any later version. # # 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., 51 Franklin St, Fifth Floor, Boston, MA # 02110-1301 USA ######################### END LICENSE BLOCK ######################### # EUCTW frequency table # Converted from big5 work # by Taiwan's Mandarin Promotion Council # <http:#www.edu.tw:81/mandr/> # 128 --> 0.42261 # 256 --> 0.57851 # 512 --> 0.74851 # 1024 --> 0.89384 # 2048 --> 0.97583 # # Idea Distribution Ratio = 0.74851/(1-0.74851) =2.98 # Random Distribution Ration = 512/(5401-512)=0.105 # # Typical Distribution Ratio about 25% of Ideal one, still much higher than RDR EUCTW_TYPICAL_DISTRIBUTION_RATIO = 0.75 # Char to FreqOrder table , EUCTW_TABLE_SIZE = 8102 EUCTWCharToFreqOrder = ( 1,1800,1506, 255,1431, 198, 9, 82, 6,7310, 177, 202,3615,1256,2808, 110, # 2742 3735, 33,3241, 261, 76, 44,2113, 16,2931,2184,1176, 659,3868, 26,3404,2643, # 2758 1198,3869,3313,4060, 410,2211, 302, 590, 361,1963, 8, 204, 58,4296,7311,1931, # 2774 63,7312,7313, 317,1614, 75, 222, 159,4061,2412,1480,7314,3500,3068, 224,2809, # 2790 3616, 3, 10,3870,1471, 29,2774,1135,2852,1939, 873, 130,3242,1123, 312,7315, # 2806 4297,2051, 507, 252, 682,7316, 142,1914, 124, 206,2932, 34,3501,3173, 64, 604, # 2822 7317,2494,1976,1977, 155,1990, 645, 641,1606,7318,3405, 337, 72, 406,7319, 80, # 2838 630, 238,3174,1509, 263, 939,1092,2644, 756,1440,1094,3406, 449, 69,2969, 591, # 2854 179,2095, 471, 115,2034,1843, 60, 50,2970, 134, 806,1868, 734,2035,3407, 180, # 2870 995,1607, 156, 537,2893, 688,7320, 319,1305, 779,2144, 514,2374, 298,4298, 359, # 2886 2495, 90,2707,1338, 663, 11, 906,1099,2545, 20,2436, 182, 532,1716,7321, 732, # 2902 1376,4062,1311,1420,3175, 25,2312,1056, 113, 399, 382,1949, 242,3408,2467, 529, # 2918 3243, 475,1447,3617,7322, 117, 21, 656, 810,1297,2295,2329,3502,7323, 126,4063, # 2934 706, 456, 150, 613,4299, 71,1118,2036,4064, 145,3069, 85, 835, 486,2114,1246, # 2950 1426, 428, 727,1285,1015, 800, 106, 623, 303,1281,7324,2127,2354, 347,3736, 221, # 2966 3503,3110,7325,1955,1153,4065, 83, 296,1199,3070, 192, 624, 93,7326, 822,1897, # 2982 2810,3111, 795,2064, 991,1554,1542,1592, 27, 43,2853, 859, 139,1456, 860,4300, # 2998 437, 712,3871, 164,2392,3112, 695, 211,3017,2096, 195,3872,1608,3504,3505,3618, # 3014 3873, 234, 811,2971,2097,3874,2229,1441,3506,1615,2375, 668,2076,1638, 305, 228, # 3030 1664,4301, 467, 415,7327, 262,2098,1593, 239, 108, 300, 200,1033, 512,1247,2077, # 3046 7328,7329,2173,3176,3619,2673, 593, 845,1062,3244, 88,1723,2037,3875,1950, 212, # 3062 266, 152, 149, 468,1898,4066,4302, 77, 187,7330,3018, 37, 5,2972,7331,3876, # 3078 7332,7333, 39,2517,4303,2894,3177,2078, 55, 148, 74,4304, 545, 483,1474,1029, # 3094 1665, 217,1869,1531,3113,1104,2645,4067, 24, 172,3507, 900,3877,3508,3509,4305, # 3110 32,1408,2811,1312, 329, 487,2355,2247,2708, 784,2674, 4,3019,3314,1427,1788, # 3126 188, 109, 499,7334,3620,1717,1789, 888,1217,3020,4306,7335,3510,7336,3315,1520, # 3142 3621,3878, 196,1034, 775,7337,7338, 929,1815, 249, 439, 38,7339,1063,7340, 794, # 3158 3879,1435,2296, 46, 178,3245,2065,7341,2376,7342, 214,1709,4307, 804, 35, 707, # 3174 324,3622,1601,2546, 140, 459,4068,7343,7344,1365, 839, 272, 978,2257,2572,3409, # 3190 2128,1363,3623,1423, 697, 100,3071, 48, 70,1231, 495,3114,2193,7345,1294,7346, # 3206 2079, 462, 586,1042,3246, 853, 256, 988, 185,2377,3410,1698, 434,1084,7347,3411, # 3222 314,2615,2775,4308,2330,2331, 569,2280, 637,1816,2518, 757,1162,1878,1616,3412, # 3238 287,1577,2115, 768,4309,1671,2854,3511,2519,1321,3737, 909,2413,7348,4069, 933, # 3254 3738,7349,2052,2356,1222,4310, 765,2414,1322, 786,4311,7350,1919,1462,1677,2895, # 3270 1699,7351,4312,1424,2437,3115,3624,2590,3316,1774,1940,3413,3880,4070, 309,1369, # 3286 1130,2812, 364,2230,1653,1299,3881,3512,3882,3883,2646, 525,1085,3021, 902,2000, # 3302 1475, 964,4313, 421,1844,1415,1057,2281, 940,1364,3116, 376,4314,4315,1381, 7, # 3318 2520, 983,2378, 336,1710,2675,1845, 321,3414, 559,1131,3022,2742,1808,1132,1313, # 3334 265,1481,1857,7352, 352,1203,2813,3247, 167,1089, 420,2814, 776, 792,1724,3513, # 3350 4071,2438,3248,7353,4072,7354, 446, 229, 333,2743, 901,3739,1200,1557,4316,2647, # 3366 1920, 395,2744,2676,3740,4073,1835, 125, 916,3178,2616,4317,7355,7356,3741,7357, # 3382 7358,7359,4318,3117,3625,1133,2547,1757,3415,1510,2313,1409,3514,7360,2145, 438, # 3398 2591,2896,2379,3317,1068, 958,3023, 461, 311,2855,2677,4074,1915,3179,4075,1978, # 3414 383, 750,2745,2617,4076, 274, 539, 385,1278,1442,7361,1154,1964, 384, 561, 210, # 3430 98,1295,2548,3515,7362,1711,2415,1482,3416,3884,2897,1257, 129,7363,3742, 642, # 3446 523,2776,2777,2648,7364, 141,2231,1333, 68, 176, 441, 876, 907,4077, 603,2592, # 3462 710, 171,3417, 404, 549, 18,3118,2393,1410,3626,1666,7365,3516,4319,2898,4320, # 3478 7366,2973, 368,7367, 146, 366, 99, 871,3627,1543, 748, 807,1586,1185, 22,2258, # 3494 379,3743,3180,7368,3181, 505,1941,2618,1991,1382,2314,7369, 380,2357, 218, 702, # 3510 1817,1248,3418,3024,3517,3318,3249,7370,2974,3628, 930,3250,3744,7371, 59,7372, # 3526 585, 601,4078, 497,3419,1112,1314,4321,1801,7373,1223,1472,2174,7374, 749,1836, # 3542 690,1899,3745,1772,3885,1476, 429,1043,1790,2232,2116, 917,4079, 447,1086,1629, # 3558 7375, 556,7376,7377,2020,1654, 844,1090, 105, 550, 966,1758,2815,1008,1782, 686, # 3574 1095,7378,2282, 793,1602,7379,3518,2593,4322,4080,2933,2297,4323,3746, 980,2496, # 3590 544, 353, 527,4324, 908,2678,2899,7380, 381,2619,1942,1348,7381,1341,1252, 560, # 3606 3072,7382,3420,2856,7383,2053, 973, 886,2080, 143,4325,7384,7385, 157,3886, 496, # 3622 4081, 57, 840, 540,2038,4326,4327,3421,2117,1445, 970,2259,1748,1965,2081,4082, # 3638 3119,1234,1775,3251,2816,3629, 773,1206,2129,1066,2039,1326,3887,1738,1725,4083, # 3654 279,3120, 51,1544,2594, 423,1578,2130,2066, 173,4328,1879,7386,7387,1583, 264, # 3670 610,3630,4329,2439, 280, 154,7388,7389,7390,1739, 338,1282,3073, 693,2857,1411, # 3686 1074,3747,2440,7391,4330,7392,7393,1240, 952,2394,7394,2900,1538,2679, 685,1483, # 3702 4084,2468,1436, 953,4085,2054,4331, 671,2395, 79,4086,2441,3252, 608, 567,2680, # 3718 3422,4087,4088,1691, 393,1261,1791,2396,7395,4332,7396,7397,7398,7399,1383,1672, # 3734 3748,3182,1464, 522,1119, 661,1150, 216, 675,4333,3888,1432,3519, 609,4334,2681, # 3750 2397,7400,7401,7402,4089,3025, 0,7403,2469, 315, 231,2442, 301,3319,4335,2380, # 3766 7404, 233,4090,3631,1818,4336,4337,7405, 96,1776,1315,2082,7406, 257,7407,1809, # 3782 3632,2709,1139,1819,4091,2021,1124,2163,2778,1777,2649,7408,3074, 363,1655,3183, # 3798 7409,2975,7410,7411,7412,3889,1567,3890, 718, 103,3184, 849,1443, 341,3320,2934, # 3814 1484,7413,1712, 127, 67, 339,4092,2398, 679,1412, 821,7414,7415, 834, 738, 351, # 3830 2976,2146, 846, 235,1497,1880, 418,1992,3749,2710, 186,1100,2147,2746,3520,1545, # 3846 1355,2935,2858,1377, 583,3891,4093,2573,2977,7416,1298,3633,1078,2549,3634,2358, # 3862 78,3750,3751, 267,1289,2099,2001,1594,4094, 348, 369,1274,2194,2175,1837,4338, # 3878 1820,2817,3635,2747,2283,2002,4339,2936,2748, 144,3321, 882,4340,3892,2749,3423, # 3894 4341,2901,7417,4095,1726, 320,7418,3893,3026, 788,2978,7419,2818,1773,1327,2859, # 3910 3894,2819,7420,1306,4342,2003,1700,3752,3521,2359,2650, 787,2022, 506, 824,3636, # 3926 534, 323,4343,1044,3322,2023,1900, 946,3424,7421,1778,1500,1678,7422,1881,4344, # 3942 165, 243,4345,3637,2521, 123, 683,4096, 764,4346, 36,3895,1792, 589,2902, 816, # 3958 626,1667,3027,2233,1639,1555,1622,3753,3896,7423,3897,2860,1370,1228,1932, 891, # 3974 2083,2903, 304,4097,7424, 292,2979,2711,3522, 691,2100,4098,1115,4347, 118, 662, # 3990 7425, 611,1156, 854,2381,1316,2861, 2, 386, 515,2904,7426,7427,3253, 868,2234, # 4006 1486, 855,2651, 785,2212,3028,7428,1040,3185,3523,7429,3121, 448,7430,1525,7431, # 4022 2164,4348,7432,3754,7433,4099,2820,3524,3122, 503, 818,3898,3123,1568, 814, 676, # 4038 1444, 306,1749,7434,3755,1416,1030, 197,1428, 805,2821,1501,4349,7435,7436,7437, # 4054 1993,7438,4350,7439,7440,2195, 13,2779,3638,2980,3124,1229,1916,7441,3756,2131, # 4070 7442,4100,4351,2399,3525,7443,2213,1511,1727,1120,7444,7445, 646,3757,2443, 307, # 4086 7446,7447,1595,3186,7448,7449,7450,3639,1113,1356,3899,1465,2522,2523,7451, 519, # 4102 7452, 128,2132, 92,2284,1979,7453,3900,1512, 342,3125,2196,7454,2780,2214,1980, # 4118 3323,7455, 290,1656,1317, 789, 827,2360,7456,3758,4352, 562, 581,3901,7457, 401, # 4134 4353,2248, 94,4354,1399,2781,7458,1463,2024,4355,3187,1943,7459, 828,1105,4101, # 4150 1262,1394,7460,4102, 605,4356,7461,1783,2862,7462,2822, 819,2101, 578,2197,2937, # 4166 7463,1502, 436,3254,4103,3255,2823,3902,2905,3425,3426,7464,2712,2315,7465,7466, # 4182 2332,2067, 23,4357, 193, 826,3759,2102, 699,1630,4104,3075, 390,1793,1064,3526, # 4198 7467,1579,3076,3077,1400,7468,4105,1838,1640,2863,7469,4358,4359, 137,4106, 598, # 4214 3078,1966, 780, 104, 974,2938,7470, 278, 899, 253, 402, 572, 504, 493,1339,7471, # 4230 3903,1275,4360,2574,2550,7472,3640,3029,3079,2249, 565,1334,2713, 863, 41,7473, # 4246 7474,4361,7475,1657,2333, 19, 463,2750,4107, 606,7476,2981,3256,1087,2084,1323, # 4262 2652,2982,7477,1631,1623,1750,4108,2682,7478,2864, 791,2714,2653,2334, 232,2416, # 4278 7479,2983,1498,7480,2654,2620, 755,1366,3641,3257,3126,2025,1609, 119,1917,3427, # 4294 862,1026,4109,7481,3904,3760,4362,3905,4363,2260,1951,2470,7482,1125, 817,4110, # 4310 4111,3906,1513,1766,2040,1487,4112,3030,3258,2824,3761,3127,7483,7484,1507,7485, # 4326 2683, 733, 40,1632,1106,2865, 345,4113, 841,2524, 230,4364,2984,1846,3259,3428, # 4342 7486,1263, 986,3429,7487, 735, 879, 254,1137, 857, 622,1300,1180,1388,1562,3907, # 4358 3908,2939, 967,2751,2655,1349, 592,2133,1692,3324,2985,1994,4114,1679,3909,1901, # 4374 2185,7488, 739,3642,2715,1296,1290,7489,4115,2198,2199,1921,1563,2595,2551,1870, # 4390 2752,2986,7490, 435,7491, 343,1108, 596, 17,1751,4365,2235,3430,3643,7492,4366, # 4406 294,3527,2940,1693, 477, 979, 281,2041,3528, 643,2042,3644,2621,2782,2261,1031, # 4422 2335,2134,2298,3529,4367, 367,1249,2552,7493,3530,7494,4368,1283,3325,2004, 240, # 4438 1762,3326,4369,4370, 836,1069,3128, 474,7495,2148,2525, 268,3531,7496,3188,1521, # 4454 1284,7497,1658,1546,4116,7498,3532,3533,7499,4117,3327,2684,1685,4118, 961,1673, # 4470 2622, 190,2005,2200,3762,4371,4372,7500, 570,2497,3645,1490,7501,4373,2623,3260, # 4486 1956,4374, 584,1514, 396,1045,1944,7502,4375,1967,2444,7503,7504,4376,3910, 619, # 4502 7505,3129,3261, 215,2006,2783,2553,3189,4377,3190,4378, 763,4119,3763,4379,7506, # 4518 7507,1957,1767,2941,3328,3646,1174, 452,1477,4380,3329,3130,7508,2825,1253,2382, # 4534 2186,1091,2285,4120, 492,7509, 638,1169,1824,2135,1752,3911, 648, 926,1021,1324, # 4550 4381, 520,4382, 997, 847,1007, 892,4383,3764,2262,1871,3647,7510,2400,1784,4384, # 4566 1952,2942,3080,3191,1728,4121,2043,3648,4385,2007,1701,3131,1551, 30,2263,4122, # 4582 7511,2026,4386,3534,7512, 501,7513,4123, 594,3431,2165,1821,3535,3432,3536,3192, # 4598 829,2826,4124,7514,1680,3132,1225,4125,7515,3262,4387,4126,3133,2336,7516,4388, # 4614 4127,7517,3912,3913,7518,1847,2383,2596,3330,7519,4389, 374,3914, 652,4128,4129, # 4630 375,1140, 798,7520,7521,7522,2361,4390,2264, 546,1659, 138,3031,2445,4391,7523, # 4646 2250, 612,1848, 910, 796,3765,1740,1371, 825,3766,3767,7524,2906,2554,7525, 692, # 4662 444,3032,2624, 801,4392,4130,7526,1491, 244,1053,3033,4131,4132, 340,7527,3915, # 4678 1041,2987, 293,1168, 87,1357,7528,1539, 959,7529,2236, 721, 694,4133,3768, 219, # 4694 1478, 644,1417,3331,2656,1413,1401,1335,1389,3916,7530,7531,2988,2362,3134,1825, # 4710 730,1515, 184,2827, 66,4393,7532,1660,2943, 246,3332, 378,1457, 226,3433, 975, # 4726 3917,2944,1264,3537, 674, 696,7533, 163,7534,1141,2417,2166, 713,3538,3333,4394, # 4742 3918,7535,7536,1186, 15,7537,1079,1070,7538,1522,3193,3539, 276,1050,2716, 758, # 4758 1126, 653,2945,3263,7539,2337, 889,3540,3919,3081,2989, 903,1250,4395,3920,3434, # 4774 3541,1342,1681,1718, 766,3264, 286, 89,2946,3649,7540,1713,7541,2597,3334,2990, # 4790 7542,2947,2215,3194,2866,7543,4396,2498,2526, 181, 387,1075,3921, 731,2187,3335, # 4806 7544,3265, 310, 313,3435,2299, 770,4134, 54,3034, 189,4397,3082,3769,3922,7545, # 4822 1230,1617,1849, 355,3542,4135,4398,3336, 111,4136,3650,1350,3135,3436,3035,4137, # 4838 2149,3266,3543,7546,2784,3923,3924,2991, 722,2008,7547,1071, 247,1207,2338,2471, # 4854 1378,4399,2009, 864,1437,1214,4400, 373,3770,1142,2216, 667,4401, 442,2753,2555, # 4870 3771,3925,1968,4138,3267,1839, 837, 170,1107, 934,1336,1882,7548,7549,2118,4139, # 4886 2828, 743,1569,7550,4402,4140, 582,2384,1418,3437,7551,1802,7552, 357,1395,1729, # 4902 3651,3268,2418,1564,2237,7553,3083,3772,1633,4403,1114,2085,4141,1532,7554, 482, # 4918 2446,4404,7555,7556,1492, 833,1466,7557,2717,3544,1641,2829,7558,1526,1272,3652, # 4934 4142,1686,1794, 416,2556,1902,1953,1803,7559,3773,2785,3774,1159,2316,7560,2867, # 4950 4405,1610,1584,3036,2419,2754, 443,3269,1163,3136,7561,7562,3926,7563,4143,2499, # 4966 3037,4406,3927,3137,2103,1647,3545,2010,1872,4144,7564,4145, 431,3438,7565, 250, # 4982 97, 81,4146,7566,1648,1850,1558, 160, 848,7567, 866, 740,1694,7568,2201,2830, # 4998 3195,4147,4407,3653,1687, 950,2472, 426, 469,3196,3654,3655,3928,7569,7570,1188, # 5014 424,1995, 861,3546,4148,3775,2202,2685, 168,1235,3547,4149,7571,2086,1674,4408, # 5030 3337,3270, 220,2557,1009,7572,3776, 670,2992, 332,1208, 717,7573,7574,3548,2447, # 5046 3929,3338,7575, 513,7576,1209,2868,3339,3138,4409,1080,7577,7578,7579,7580,2527, # 5062 3656,3549, 815,1587,3930,3931,7581,3550,3439,3777,1254,4410,1328,3038,1390,3932, # 5078 1741,3933,3778,3934,7582, 236,3779,2448,3271,7583,7584,3657,3780,1273,3781,4411, # 5094 7585, 308,7586,4412, 245,4413,1851,2473,1307,2575, 430, 715,2136,2449,7587, 270, # 5110 199,2869,3935,7588,3551,2718,1753, 761,1754, 725,1661,1840,4414,3440,3658,7589, # 5126 7590, 587, 14,3272, 227,2598, 326, 480,2265, 943,2755,3552, 291, 650,1883,7591, # 5142 1702,1226, 102,1547, 62,3441, 904,4415,3442,1164,4150,7592,7593,1224,1548,2756, # 5158 391, 498,1493,7594,1386,1419,7595,2055,1177,4416, 813, 880,1081,2363, 566,1145, # 5174 4417,2286,1001,1035,2558,2599,2238, 394,1286,7596,7597,2068,7598, 86,1494,1730, # 5190 3936, 491,1588, 745, 897,2948, 843,3340,3937,2757,2870,3273,1768, 998,2217,2069, # 5206 397,1826,1195,1969,3659,2993,3341, 284,7599,3782,2500,2137,2119,1903,7600,3938, # 5222 2150,3939,4151,1036,3443,1904, 114,2559,4152, 209,1527,7601,7602,2949,2831,2625, # 5238 2385,2719,3139, 812,2560,7603,3274,7604,1559, 737,1884,3660,1210, 885, 28,2686, # 5254 3553,3783,7605,4153,1004,1779,4418,7606, 346,1981,2218,2687,4419,3784,1742, 797, # 5270 1642,3940,1933,1072,1384,2151, 896,3941,3275,3661,3197,2871,3554,7607,2561,1958, # 5286 4420,2450,1785,7608,7609,7610,3942,4154,1005,1308,3662,4155,2720,4421,4422,1528, # 5302 2600, 161,1178,4156,1982, 987,4423,1101,4157, 631,3943,1157,3198,2420,1343,1241, # 5318 1016,2239,2562, 372, 877,2339,2501,1160, 555,1934, 911,3944,7611, 466,1170, 169, # 5334 1051,2907,2688,3663,2474,2994,1182,2011,2563,1251,2626,7612, 992,2340,3444,1540, # 5350 2721,1201,2070,2401,1996,2475,7613,4424, 528,1922,2188,1503,1873,1570,2364,3342, # 5366 3276,7614, 557,1073,7615,1827,3445,2087,2266,3140,3039,3084, 767,3085,2786,4425, # 5382 1006,4158,4426,2341,1267,2176,3664,3199, 778,3945,3200,2722,1597,2657,7616,4427, # 5398 7617,3446,7618,7619,7620,3277,2689,1433,3278, 131, 95,1504,3946, 723,4159,3141, # 5414 1841,3555,2758,2189,3947,2027,2104,3665,7621,2995,3948,1218,7622,3343,3201,3949, # 5430 4160,2576, 248,1634,3785, 912,7623,2832,3666,3040,3786, 654, 53,7624,2996,7625, # 5446 1688,4428, 777,3447,1032,3950,1425,7626, 191, 820,2120,2833, 971,4429, 931,3202, # 5462 135, 664, 783,3787,1997, 772,2908,1935,3951,3788,4430,2909,3203, 282,2723, 640, # 5478 1372,3448,1127, 922, 325,3344,7627,7628, 711,2044,7629,7630,3952,2219,2787,1936, # 5494 3953,3345,2220,2251,3789,2300,7631,4431,3790,1258,3279,3954,3204,2138,2950,3955, # 5510 3956,7632,2221, 258,3205,4432, 101,1227,7633,3280,1755,7634,1391,3281,7635,2910, # 5526 2056, 893,7636,7637,7638,1402,4161,2342,7639,7640,3206,3556,7641,7642, 878,1325, # 5542 1780,2788,4433, 259,1385,2577, 744,1183,2267,4434,7643,3957,2502,7644, 684,1024, # 5558 4162,7645, 472,3557,3449,1165,3282,3958,3959, 322,2152, 881, 455,1695,1152,1340, # 5574 660, 554,2153,4435,1058,4436,4163, 830,1065,3346,3960,4437,1923,7646,1703,1918, # 5590 7647, 932,2268, 122,7648,4438, 947, 677,7649,3791,2627, 297,1905,1924,2269,4439, # 5606 2317,3283,7650,7651,4164,7652,4165, 84,4166, 112, 989,7653, 547,1059,3961, 701, # 5622 3558,1019,7654,4167,7655,3450, 942, 639, 457,2301,2451, 993,2951, 407, 851, 494, # 5638 4440,3347, 927,7656,1237,7657,2421,3348, 573,4168, 680, 921,2911,1279,1874, 285, # 5654 790,1448,1983, 719,2167,7658,7659,4441,3962,3963,1649,7660,1541, 563,7661,1077, # 5670 7662,3349,3041,3451, 511,2997,3964,3965,3667,3966,1268,2564,3350,3207,4442,4443, # 5686 7663, 535,1048,1276,1189,2912,2028,3142,1438,1373,2834,2952,1134,2012,7664,4169, # 5702 1238,2578,3086,1259,7665, 700,7666,2953,3143,3668,4170,7667,4171,1146,1875,1906, # 5718 4444,2601,3967, 781,2422, 132,1589, 203, 147, 273,2789,2402, 898,1786,2154,3968, # 5734 3969,7668,3792,2790,7669,7670,4445,4446,7671,3208,7672,1635,3793, 965,7673,1804, # 5750 2690,1516,3559,1121,1082,1329,3284,3970,1449,3794, 65,1128,2835,2913,2759,1590, # 5766 3795,7674,7675, 12,2658, 45, 976,2579,3144,4447, 517,2528,1013,1037,3209,7676, # 5782 3796,2836,7677,3797,7678,3452,7679,2602, 614,1998,2318,3798,3087,2724,2628,7680, # 5798 2580,4172, 599,1269,7681,1810,3669,7682,2691,3088, 759,1060, 489,1805,3351,3285, # 5814 1358,7683,7684,2386,1387,1215,2629,2252, 490,7685,7686,4173,1759,2387,2343,7687, # 5830 4448,3799,1907,3971,2630,1806,3210,4449,3453,3286,2760,2344, 874,7688,7689,3454, # 5846 3670,1858, 91,2914,3671,3042,3800,4450,7690,3145,3972,2659,7691,3455,1202,1403, # 5862 3801,2954,2529,1517,2503,4451,3456,2504,7692,4452,7693,2692,1885,1495,1731,3973, # 5878 2365,4453,7694,2029,7695,7696,3974,2693,1216, 237,2581,4174,2319,3975,3802,4454, # 5894 4455,2694,3560,3457, 445,4456,7697,7698,7699,7700,2761, 61,3976,3672,1822,3977, # 5910 7701, 687,2045, 935, 925, 405,2660, 703,1096,1859,2725,4457,3978,1876,1367,2695, # 5926 3352, 918,2105,1781,2476, 334,3287,1611,1093,4458, 564,3146,3458,3673,3353, 945, # 5942 2631,2057,4459,7702,1925, 872,4175,7703,3459,2696,3089, 349,4176,3674,3979,4460, # 5958 3803,4177,3675,2155,3980,4461,4462,4178,4463,2403,2046, 782,3981, 400, 251,4179, # 5974 1624,7704,7705, 277,3676, 299,1265, 476,1191,3804,2121,4180,4181,1109, 205,7706, # 5990 2582,1000,2156,3561,1860,7707,7708,7709,4464,7710,4465,2565, 107,2477,2157,3982, # 6006 3460,3147,7711,1533, 541,1301, 158, 753,4182,2872,3562,7712,1696, 370,1088,4183, # 6022 4466,3563, 579, 327, 440, 162,2240, 269,1937,1374,3461, 968,3043, 56,1396,3090, # 6038 2106,3288,3354,7713,1926,2158,4467,2998,7714,3564,7715,7716,3677,4468,2478,7717, # 6054 2791,7718,1650,4469,7719,2603,7720,7721,3983,2661,3355,1149,3356,3984,3805,3985, # 6070 7722,1076, 49,7723, 951,3211,3289,3290, 450,2837, 920,7724,1811,2792,2366,4184, # 6086 1908,1138,2367,3806,3462,7725,3212,4470,1909,1147,1518,2423,4471,3807,7726,4472, # 6102 2388,2604, 260,1795,3213,7727,7728,3808,3291, 708,7729,3565,1704,7730,3566,1351, # 6118 1618,3357,2999,1886, 944,4185,3358,4186,3044,3359,4187,7731,3678, 422, 413,1714, # 6134 3292, 500,2058,2345,4188,2479,7732,1344,1910, 954,7733,1668,7734,7735,3986,2404, # 6150 4189,3567,3809,4190,7736,2302,1318,2505,3091, 133,3092,2873,4473, 629, 31,2838, # 6166 2697,3810,4474, 850, 949,4475,3987,2955,1732,2088,4191,1496,1852,7737,3988, 620, # 6182 3214, 981,1242,3679,3360,1619,3680,1643,3293,2139,2452,1970,1719,3463,2168,7738, # 6198 3215,7739,7740,3361,1828,7741,1277,4476,1565,2047,7742,1636,3568,3093,7743, 869, # 6214 2839, 655,3811,3812,3094,3989,3000,3813,1310,3569,4477,7744,7745,7746,1733, 558, # 6230 4478,3681, 335,1549,3045,1756,4192,3682,1945,3464,1829,1291,1192, 470,2726,2107, # 6246 2793, 913,1054,3990,7747,1027,7748,3046,3991,4479, 982,2662,3362,3148,3465,3216, # 6262 3217,1946,2794,7749, 571,4480,7750,1830,7751,3570,2583,1523,2424,7752,2089, 984, # 6278 4481,3683,1959,7753,3684, 852, 923,2795,3466,3685, 969,1519, 999,2048,2320,1705, # 6294 7754,3095, 615,1662, 151, 597,3992,2405,2321,1049, 275,4482,3686,4193, 568,3687, # 6310 3571,2480,4194,3688,7755,2425,2270, 409,3218,7756,1566,2874,3467,1002, 769,2840, # 6326 194,2090,3149,3689,2222,3294,4195, 628,1505,7757,7758,1763,2177,3001,3993, 521, # 6342 1161,2584,1787,2203,2406,4483,3994,1625,4196,4197, 412, 42,3096, 464,7759,2632, # 6358 4484,3363,1760,1571,2875,3468,2530,1219,2204,3814,2633,2140,2368,4485,4486,3295, # 6374 1651,3364,3572,7760,7761,3573,2481,3469,7762,3690,7763,7764,2271,2091, 460,7765, # 6390 4487,7766,3002, 962, 588,3574, 289,3219,2634,1116, 52,7767,3047,1796,7768,7769, # 6406 7770,1467,7771,1598,1143,3691,4198,1984,1734,1067,4488,1280,3365, 465,4489,1572, # 6422 510,7772,1927,2241,1812,1644,3575,7773,4490,3692,7774,7775,2663,1573,1534,7776, # 6438 7777,4199, 536,1807,1761,3470,3815,3150,2635,7778,7779,7780,4491,3471,2915,1911, # 6454 2796,7781,3296,1122, 377,3220,7782, 360,7783,7784,4200,1529, 551,7785,2059,3693, # 6470 1769,2426,7786,2916,4201,3297,3097,2322,2108,2030,4492,1404, 136,1468,1479, 672, # 6486 1171,3221,2303, 271,3151,7787,2762,7788,2049, 678,2727, 865,1947,4493,7789,2013, # 6502 3995,2956,7790,2728,2223,1397,3048,3694,4494,4495,1735,2917,3366,3576,7791,3816, # 6518 509,2841,2453,2876,3817,7792,7793,3152,3153,4496,4202,2531,4497,2304,1166,1010, # 6534 552, 681,1887,7794,7795,2957,2958,3996,1287,1596,1861,3154, 358, 453, 736, 175, # 6550 478,1117, 905,1167,1097,7796,1853,1530,7797,1706,7798,2178,3472,2287,3695,3473, # 6566 3577,4203,2092,4204,7799,3367,1193,2482,4205,1458,2190,2205,1862,1888,1421,3298, # 6582 2918,3049,2179,3474, 595,2122,7800,3997,7801,7802,4206,1707,2636, 223,3696,1359, # 6598 751,3098, 183,3475,7803,2797,3003, 419,2369, 633, 704,3818,2389, 241,7804,7805, # 6614 7806, 838,3004,3697,2272,2763,2454,3819,1938,2050,3998,1309,3099,2242,1181,7807, # 6630 1136,2206,3820,2370,1446,4207,2305,4498,7808,7809,4208,1055,2605, 484,3698,7810, # 6646 3999, 625,4209,2273,3368,1499,4210,4000,7811,4001,4211,3222,2274,2275,3476,7812, # 6662 7813,2764, 808,2606,3699,3369,4002,4212,3100,2532, 526,3370,3821,4213, 955,7814, # 6678 1620,4214,2637,2427,7815,1429,3700,1669,1831, 994, 928,7816,3578,1260,7817,7818, # 6694 7819,1948,2288, 741,2919,1626,4215,2729,2455, 867,1184, 362,3371,1392,7820,7821, # 6710 4003,4216,1770,1736,3223,2920,4499,4500,1928,2698,1459,1158,7822,3050,3372,2877, # 6726 1292,1929,2506,2842,3701,1985,1187,2071,2014,2607,4217,7823,2566,2507,2169,3702, # 6742 2483,3299,7824,3703,4501,7825,7826, 666,1003,3005,1022,3579,4218,7827,4502,1813, # 6758 2253, 574,3822,1603, 295,1535, 705,3823,4219, 283, 858, 417,7828,7829,3224,4503, # 6774 4504,3051,1220,1889,1046,2276,2456,4004,1393,1599, 689,2567, 388,4220,7830,2484, # 6790 802,7831,2798,3824,2060,1405,2254,7832,4505,3825,2109,1052,1345,3225,1585,7833, # 6806 809,7834,7835,7836, 575,2730,3477, 956,1552,1469,1144,2323,7837,2324,1560,2457, # 6822 3580,3226,4005, 616,2207,3155,2180,2289,7838,1832,7839,3478,4506,7840,1319,3704, # 6838 3705,1211,3581,1023,3227,1293,2799,7841,7842,7843,3826, 607,2306,3827, 762,2878, # 6854 1439,4221,1360,7844,1485,3052,7845,4507,1038,4222,1450,2061,2638,4223,1379,4508, # 6870 2585,7846,7847,4224,1352,1414,2325,2921,1172,7848,7849,3828,3829,7850,1797,1451, # 6886 7851,7852,7853,7854,2922,4006,4007,2485,2346, 411,4008,4009,3582,3300,3101,4509, # 6902 1561,2664,1452,4010,1375,7855,7856, 47,2959, 316,7857,1406,1591,2923,3156,7858, # 6918 1025,2141,3102,3157, 354,2731, 884,2224,4225,2407, 508,3706, 726,3583, 996,2428, # 6934 3584, 729,7859, 392,2191,1453,4011,4510,3707,7860,7861,2458,3585,2608,1675,2800, # 6950 919,2347,2960,2348,1270,4511,4012, 73,7862,7863, 647,7864,3228,2843,2255,1550, # 6966 1346,3006,7865,1332, 883,3479,7866,7867,7868,7869,3301,2765,7870,1212, 831,1347, # 6982 4226,4512,2326,3830,1863,3053, 720,3831,4513,4514,3832,7871,4227,7872,7873,4515, # 6998 7874,7875,1798,4516,3708,2609,4517,3586,1645,2371,7876,7877,2924, 669,2208,2665, # 7014 2429,7878,2879,7879,7880,1028,3229,7881,4228,2408,7882,2256,1353,7883,7884,4518, # 7030 3158, 518,7885,4013,7886,4229,1960,7887,2142,4230,7888,7889,3007,2349,2350,3833, # 7046 516,1833,1454,4014,2699,4231,4519,2225,2610,1971,1129,3587,7890,2766,7891,2961, # 7062 1422, 577,1470,3008,1524,3373,7892,7893, 432,4232,3054,3480,7894,2586,1455,2508, # 7078 2226,1972,1175,7895,1020,2732,4015,3481,4520,7896,2733,7897,1743,1361,3055,3482, # 7094 2639,4016,4233,4521,2290, 895, 924,4234,2170, 331,2243,3056, 166,1627,3057,1098, # 7110 7898,1232,2880,2227,3374,4522, 657, 403,1196,2372, 542,3709,3375,1600,4235,3483, # 7126 7899,4523,2767,3230, 576, 530,1362,7900,4524,2533,2666,3710,4017,7901, 842,3834, # 7142 7902,2801,2031,1014,4018, 213,2700,3376, 665, 621,4236,7903,3711,2925,2430,7904, # 7158 2431,3302,3588,3377,7905,4237,2534,4238,4525,3589,1682,4239,3484,1380,7906, 724, # 7174 2277, 600,1670,7907,1337,1233,4526,3103,2244,7908,1621,4527,7909, 651,4240,7910, # 7190 1612,4241,2611,7911,2844,7912,2734,2307,3058,7913, 716,2459,3059, 174,1255,2701, # 7206 4019,3590, 548,1320,1398, 728,4020,1574,7914,1890,1197,3060,4021,7915,3061,3062, # 7222 3712,3591,3713, 747,7916, 635,4242,4528,7917,7918,7919,4243,7920,7921,4529,7922, # 7238 3378,4530,2432, 451,7923,3714,2535,2072,4244,2735,4245,4022,7924,1764,4531,7925, # 7254 4246, 350,7926,2278,2390,2486,7927,4247,4023,2245,1434,4024, 488,4532, 458,4248, # 7270 4025,3715, 771,1330,2391,3835,2568,3159,2159,2409,1553,2667,3160,4249,7928,2487, # 7286 2881,2612,1720,2702,4250,3379,4533,7929,2536,4251,7930,3231,4252,2768,7931,2015, # 7302 2736,7932,1155,1017,3716,3836,7933,3303,2308, 201,1864,4253,1430,7934,4026,7935, # 7318 7936,7937,7938,7939,4254,1604,7940, 414,1865, 371,2587,4534,4535,3485,2016,3104, # 7334 4536,1708, 960,4255, 887, 389,2171,1536,1663,1721,7941,2228,4027,2351,2926,1580, # 7350 7942,7943,7944,1744,7945,2537,4537,4538,7946,4539,7947,2073,7948,7949,3592,3380, # 7366 2882,4256,7950,4257,2640,3381,2802, 673,2703,2460, 709,3486,4028,3593,4258,7951, # 7382 1148, 502, 634,7952,7953,1204,4540,3594,1575,4541,2613,3717,7954,3718,3105, 948, # 7398 3232, 121,1745,3837,1110,7955,4259,3063,2509,3009,4029,3719,1151,1771,3838,1488, # 7414 4030,1986,7956,2433,3487,7957,7958,2093,7959,4260,3839,1213,1407,2803, 531,2737, # 7430 2538,3233,1011,1537,7960,2769,4261,3106,1061,7961,3720,3721,1866,2883,7962,2017, # 7446 120,4262,4263,2062,3595,3234,2309,3840,2668,3382,1954,4542,7963,7964,3488,1047, # 7462 2704,1266,7965,1368,4543,2845, 649,3383,3841,2539,2738,1102,2846,2669,7966,7967, # 7478 1999,7968,1111,3596,2962,7969,2488,3842,3597,2804,1854,3384,3722,7970,7971,3385, # 7494 2410,2884,3304,3235,3598,7972,2569,7973,3599,2805,4031,1460, 856,7974,3600,7975, # 7510 2885,2963,7976,2886,3843,7977,4264, 632,2510, 875,3844,1697,3845,2291,7978,7979, # 7526 4544,3010,1239, 580,4545,4265,7980, 914, 936,2074,1190,4032,1039,2123,7981,7982, # 7542 7983,3386,1473,7984,1354,4266,3846,7985,2172,3064,4033, 915,3305,4267,4268,3306, # 7558 1605,1834,7986,2739, 398,3601,4269,3847,4034, 328,1912,2847,4035,3848,1331,4270, # 7574 3011, 937,4271,7987,3602,4036,4037,3387,2160,4546,3388, 524, 742, 538,3065,1012, # 7590 7988,7989,3849,2461,7990, 658,1103, 225,3850,7991,7992,4547,7993,4548,7994,3236, # 7606 1243,7995,4038, 963,2246,4549,7996,2705,3603,3161,7997,7998,2588,2327,7999,4550, # 7622 8000,8001,8002,3489,3307, 957,3389,2540,2032,1930,2927,2462, 870,2018,3604,1746, # 7638 2770,2771,2434,2463,8003,3851,8004,3723,3107,3724,3490,3390,3725,8005,1179,3066, # 7654 8006,3162,2373,4272,3726,2541,3163,3108,2740,4039,8007,3391,1556,2542,2292, 977, # 7670 2887,2033,4040,1205,3392,8008,1765,3393,3164,2124,1271,1689, 714,4551,3491,8009, # 7686 2328,3852, 533,4273,3605,2181, 617,8010,2464,3308,3492,2310,8011,8012,3165,8013, # 7702 8014,3853,1987, 618, 427,2641,3493,3394,8015,8016,1244,1690,8017,2806,4274,4552, # 7718 8018,3494,8019,8020,2279,1576, 473,3606,4275,3395, 972,8021,3607,8022,3067,8023, # 7734 8024,4553,4554,8025,3727,4041,4042,8026, 153,4555, 356,8027,1891,2888,4276,2143, # 7750 408, 803,2352,8028,3854,8029,4277,1646,2570,2511,4556,4557,3855,8030,3856,4278, # 7766 8031,2411,3396, 752,8032,8033,1961,2964,8034, 746,3012,2465,8035,4279,3728, 698, # 7782 4558,1892,4280,3608,2543,4559,3609,3857,8036,3166,3397,8037,1823,1302,4043,2706, # 7798 3858,1973,4281,8038,4282,3167, 823,1303,1288,1236,2848,3495,4044,3398, 774,3859, # 7814 8039,1581,4560,1304,2849,3860,4561,8040,2435,2161,1083,3237,4283,4045,4284, 344, # 7830 1173, 288,2311, 454,1683,8041,8042,1461,4562,4046,2589,8043,8044,4563, 985, 894, # 7846 8045,3399,3168,8046,1913,2928,3729,1988,8047,2110,1974,8048,4047,8049,2571,1194, # 7862 425,8050,4564,3169,1245,3730,4285,8051,8052,2850,8053, 636,4565,1855,3861, 760, # 7878 1799,8054,4286,2209,1508,4566,4048,1893,1684,2293,8055,8056,8057,4287,4288,2210, # 7894 479,8058,8059, 832,8060,4049,2489,8061,2965,2490,3731, 990,3109, 627,1814,2642, # 7910 4289,1582,4290,2125,2111,3496,4567,8062, 799,4291,3170,8063,4568,2112,1737,3013, # 7926 1018, 543, 754,4292,3309,1676,4569,4570,4050,8064,1489,8065,3497,8066,2614,2889, # 7942 4051,8067,8068,2966,8069,8070,8071,8072,3171,4571,4572,2182,1722,8073,3238,3239, # 7958 1842,3610,1715, 481, 365,1975,1856,8074,8075,1962,2491,4573,8076,2126,3611,3240, # 7974 433,1894,2063,2075,8077, 602,2741,8078,8079,8080,8081,8082,3014,1628,3400,8083, # 7990 3172,4574,4052,2890,4575,2512,8084,2544,2772,8085,8086,8087,3310,4576,2891,8088, # 8006 4577,8089,2851,4578,4579,1221,2967,4053,2513,8090,8091,8092,1867,1989,8093,8094, # 8022 8095,1895,8096,8097,4580,1896,4054, 318,8098,2094,4055,4293,8099,8100, 485,8101, # 8038 938,3862, 553,2670, 116,8102,3863,3612,8103,3498,2671,2773,3401,3311,2807,8104, # 8054 3613,2929,4056,1747,2930,2968,8105,8106, 207,8107,8108,2672,4581,2514,8109,3015, # 8070 890,3614,3864,8110,1877,3732,3402,8111,2183,2353,3403,1652,8112,8113,8114, 941, # 8086 2294, 208,3499,4057,2019, 330,4294,3865,2892,2492,3733,4295,8115,8116,8117,8118, # 8102 #Everything below is of no interest for detection purpose 2515,1613,4582,8119,3312,3866,2516,8120,4058,8121,1637,4059,2466,4583,3867,8122, # 8118 2493,3016,3734,8123,8124,2192,8125,8126,2162,8127,8128,8129,8130,8131,8132,8133, # 8134 8134,8135,8136,8137,8138,8139,8140,8141,8142,8143,8144,8145,8146,8147,8148,8149, # 8150 8150,8151,8152,8153,8154,8155,8156,8157,8158,8159,8160,8161,8162,8163,8164,8165, # 8166 8166,8167,8168,8169,8170,8171,8172,8173,8174,8175,8176,8177,8178,8179,8180,8181, # 8182 8182,8183,8184,8185,8186,8187,8188,8189,8190,8191,8192,8193,8194,8195,8196,8197, # 8198 8198,8199,8200,8201,8202,8203,8204,8205,8206,8207,8208,8209,8210,8211,8212,8213, # 8214 8214,8215,8216,8217,8218,8219,8220,8221,8222,8223,8224,8225,8226,8227,8228,8229, # 8230 8230,8231,8232,8233,8234,8235,8236,8237,8238,8239,8240,8241,8242,8243,8244,8245, # 8246 8246,8247,8248,8249,8250,8251,8252,8253,8254,8255,8256,8257,8258,8259,8260,8261, # 8262 8262,8263,8264,8265,8266,8267,8268,8269,8270,8271,8272,8273,8274,8275,8276,8277, # 8278 8278,8279,8280,8281,8282,8283,8284,8285,8286,8287,8288,8289,8290,8291,8292,8293, # 8294 8294,8295,8296,8297,8298,8299,8300,8301,8302,8303,8304,8305,8306,8307,8308,8309, # 8310 8310,8311,8312,8313,8314,8315,8316,8317,8318,8319,8320,8321,8322,8323,8324,8325, # 8326 8326,8327,8328,8329,8330,8331,8332,8333,8334,8335,8336,8337,8338,8339,8340,8341, # 8342 8342,8343,8344,8345,8346,8347,8348,8349,8350,8351,8352,8353,8354,8355,8356,8357, # 8358 8358,8359,8360,8361,8362,8363,8364,8365,8366,8367,8368,8369,8370,8371,8372,8373, # 8374 8374,8375,8376,8377,8378,8379,8380,8381,8382,8383,8384,8385,8386,8387,8388,8389, # 8390 8390,8391,8392,8393,8394,8395,8396,8397,8398,8399,8400,8401,8402,8403,8404,8405, # 8406 8406,8407,8408,8409,8410,8411,8412,8413,8414,8415,8416,8417,8418,8419,8420,8421, # 8422 8422,8423,8424,8425,8426,8427,8428,8429,8430,8431,8432,8433,8434,8435,8436,8437, # 8438 8438,8439,8440,8441,8442,8443,8444,8445,8446,8447,8448,8449,8450,8451,8452,8453, # 8454 8454,8455,8456,8457,8458,8459,8460,8461,8462,8463,8464,8465,8466,8467,8468,8469, # 8470 8470,8471,8472,8473,8474,8475,8476,8477,8478,8479,8480,8481,8482,8483,8484,8485, # 8486 8486,8487,8488,8489,8490,8491,8492,8493,8494,8495,8496,8497,8498,8499,8500,8501, # 8502 8502,8503,8504,8505,8506,8507,8508,8509,8510,8511,8512,8513,8514,8515,8516,8517, # 8518 8518,8519,8520,8521,8522,8523,8524,8525,8526,8527,8528,8529,8530,8531,8532,8533, # 8534 8534,8535,8536,8537,8538,8539,8540,8541,8542,8543,8544,8545,8546,8547,8548,8549, # 8550 8550,8551,8552,8553,8554,8555,8556,8557,8558,8559,8560,8561,8562,8563,8564,8565, # 8566 8566,8567,8568,8569,8570,8571,8572,8573,8574,8575,8576,8577,8578,8579,8580,8581, # 8582 8582,8583,8584,8585,8586,8587,8588,8589,8590,8591,8592,8593,8594,8595,8596,8597, # 8598 8598,8599,8600,8601,8602,8603,8604,8605,8606,8607,8608,8609,8610,8611,8612,8613, # 8614 8614,8615,8616,8617,8618,8619,8620,8621,8622,8623,8624,8625,8626,8627,8628,8629, # 8630 8630,8631,8632,8633,8634,8635,8636,8637,8638,8639,8640,8641,8642,8643,8644,8645, # 8646 8646,8647,8648,8649,8650,8651,8652,8653,8654,8655,8656,8657,8658,8659,8660,8661, # 8662 8662,8663,8664,8665,8666,8667,8668,8669,8670,8671,8672,8673,8674,8675,8676,8677, # 8678 8678,8679,8680,8681,8682,8683,8684,8685,8686,8687,8688,8689,8690,8691,8692,8693, # 8694 8694,8695,8696,8697,8698,8699,8700,8701,8702,8703,8704,8705,8706,8707,8708,8709, # 8710 8710,8711,8712,8713,8714,8715,8716,8717,8718,8719,8720,8721,8722,8723,8724,8725, # 8726 8726,8727,8728,8729,8730,8731,8732,8733,8734,8735,8736,8737,8738,8739,8740,8741) # 8742 # flake8: noqa
bsd-3-clause
cloudera/hue
desktop/core/ext-py/boto-2.46.1/boto/ec2/volumestatus.py
181
6329
# Copyright (c) 2012 Mitch Garnaat http://garnaat.org/ # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. # All Rights Reserved # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, dis- # tribute, sublicense, and/or sell copies of the Software, and to permit # persons to whom the Software is furnished to do so, subject to the fol- # lowing conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. from boto.ec2.instancestatus import Status, Details class Event(object): """ A status event for an instance. :ivar type: The type of the event. :ivar id: The ID of the event. :ivar description: A string describing the reason for the event. :ivar not_before: A datestring describing the earliest time for the event. :ivar not_after: A datestring describing the latest time for the event. """ def __init__(self, type=None, id=None, description=None, not_before=None, not_after=None): self.type = type self.id = id self.description = description self.not_before = not_before self.not_after = not_after def __repr__(self): return 'Event:%s' % self.type def startElement(self, name, attrs, connection): return None def endElement(self, name, value, connection): if name == 'eventType': self.type = value elif name == 'eventId': self.id = value elif name == 'description': self.description = value elif name == 'notBefore': self.not_before = value elif name == 'notAfter': self.not_after = value else: setattr(self, name, value) class EventSet(list): def startElement(self, name, attrs, connection): if name == 'item': event = Event() self.append(event) return event else: return None def endElement(self, name, value, connection): setattr(self, name, value) class Action(object): """ An action for an instance. :ivar code: The code for the type of the action. :ivar id: The ID of the event. :ivar type: The type of the event. :ivar description: A description of the action. """ def __init__(self, code=None, id=None, description=None, type=None): self.code = code self.id = id self.type = type self.description = description def __repr__(self): return 'Action:%s' % self.code def startElement(self, name, attrs, connection): return None def endElement(self, name, value, connection): if name == 'eventType': self.type = value elif name == 'eventId': self.id = value elif name == 'description': self.description = value elif name == 'code': self.code = value else: setattr(self, name, value) class ActionSet(list): def startElement(self, name, attrs, connection): if name == 'item': action = Action() self.append(action) return action else: return None def endElement(self, name, value, connection): setattr(self, name, value) class VolumeStatus(object): """ Represents an EC2 Volume status as reported by DescribeVolumeStatus request. :ivar id: The volume identifier. :ivar zone: The availability zone of the volume :ivar volume_status: A Status object that reports impaired functionality that arises from problems internal to the instance. :ivar events: A list of events relevant to the instance. :ivar actions: A list of events relevant to the instance. """ def __init__(self, id=None, zone=None): self.id = id self.zone = zone self.volume_status = Status() self.events = None self.actions = None def __repr__(self): return 'VolumeStatus:%s' % self.id def startElement(self, name, attrs, connection): if name == 'eventsSet': self.events = EventSet() return self.events elif name == 'actionsSet': self.actions = ActionSet() return self.actions elif name == 'volumeStatus': return self.volume_status else: return None def endElement(self, name, value, connection): if name == 'volumeId': self.id = value elif name == 'availabilityZone': self.zone = value else: setattr(self, name, value) class VolumeStatusSet(list): """ A list object that contains the results of a call to DescribeVolumeStatus request. Each element of the list will be an VolumeStatus object. :ivar next_token: If the response was truncated by the EC2 service, the next_token attribute of the object will contain the string that needs to be passed in to the next request to retrieve the next set of results. """ def __init__(self, connection=None): list.__init__(self) self.connection = connection self.next_token = None def startElement(self, name, attrs, connection): if name == 'item': status = VolumeStatus() self.append(status) return status else: return None def endElement(self, name, value, connection): if name == 'NextToken': self.next_token = value setattr(self, name, value)
apache-2.0
jaggu303619/asylum
openerp/addons/portal/mail_message.py
34
3555
# -*- coding: utf-8 -*- ############################################################################## # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2011 OpenERP S.A (<http://www.openerp.com>). # # 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/>. # ############################################################################## from openerp import SUPERUSER_ID from openerp.osv import osv, orm from openerp.tools.translate import _ class mail_message(osv.Model): """ Update of mail_message class, to restrict mail access. """ _inherit = 'mail.message' def _search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False, access_rights_uid=None): """ Override that adds specific access rights of mail.message, to remove all internal notes if uid is a non-employee """ if uid == SUPERUSER_ID: return super(mail_message, self)._search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count, access_rights_uid=access_rights_uid) group_ids = self.pool.get('res.users').browse(cr, uid, uid, context=context).groups_id group_user_id = self.pool.get("ir.model.data").get_object_reference(cr, uid, 'base', 'group_user')[1] if group_user_id not in [group.id for group in group_ids]: args = ['&', '|', ('type', '!=', 'comment'), ('subtype_id', '!=', False)] + list(args) return super(mail_message, self)._search(cr, uid, args, offset=offset, limit=limit, order=order, context=context, count=count, access_rights_uid=access_rights_uid) def check_access_rule(self, cr, uid, ids, operation, context=None): """ Add Access rules of mail.message for non-employee user: - read: - raise if the type is comment and subtype NULL (internal note) """ if uid == SUPERUSER_ID: return super(mail_message, self).check_access_rule(cr, uid, ids=ids, operation=operation, context=context) group_ids = self.pool.get('res.users').browse(cr, uid, uid, context=context).groups_id group_user_id = self.pool.get("ir.model.data").get_object_reference(cr, uid, 'base', 'group_user')[1] if group_user_id not in [group.id for group in group_ids]: cr.execute('SELECT DISTINCT id FROM "%s" WHERE type = %%s AND subtype_id IS NULL AND id = ANY (%%s)' % (self._table), ('comment', ids,)) if cr.fetchall(): raise orm.except_orm(_('Access Denied'), _('The requested operation cannot be completed due to security restrictions. Please contact your system administrator.\n\n(Document type: %s, Operation: %s)') % \ (self._description, operation)) return super(mail_message, self).check_access_rule(cr, uid, ids=ids, operation=operation, context=context)
agpl-3.0
sanyaade-mediadev/Transana
FormatDialog.py
1
28539
# Copyright (C) 2003 - 2015 The Board of Regents of the University of Wisconsin System # # This program is free software; you can redistribute it and/or modify # it under the terms of version 2 of the GNU General Public License as # published by the Free Software Foundation. # # 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # """This file implements the FormatDialog class, which defines the Font and Paragraph formatting Dialog Box. """ __author__ = 'David Woods <[email protected]>' # import wxPython import wx # import the Font Formatting Panel import FormatFontPanel # import the Paragraph Formatting Panel import FormatParagraphPanel # import the Tabs Panel import FormatTabsPanel # Import Transana's Globals import TransanaGlobal # Define Formatting Definition Constants fd_OFF = 0 # The "unselected" state for font Weight, Style, and Underline fd_BOLD = 1 # The "selected" state for Weight fd_ITALIC = 1 # The "selected" state for Style fd_UNDERLINE = 1 # The "selected" state for Underline fd_AMBIGUOUS = 2 # The "ambiguous" (mixed unselected and selected) state for font Weight, Style, and Underline class FormatDef(object): """ The Transana Font Dialog uses its own custom font definition object so that it is able to handle ambiguous font information. For example, if some of the text to be set by this font specification is Bold and some is not, the Bold setting should be unset. The traditional wx.formatData object can't do this. """ def __init__(self): """ Initialize the FormatDef object """ self._fontFace = None self._fontSize = None self._fontWeight = fd_OFF # Options are fd_OFF, fd_BOLD, fd_AMBIGUOUS self._fontStyle = fd_OFF # Options are fd_OFF, fd_ITALIC, fd_AMBIGUOUS self._fontUnderline = fd_OFF # Options are fd_OFF, fd_UNDERLINE, fd_AMBIGUOUS self._fontColorName = None self._fontColorDef = None self._fontBackgroundColorName = None self._fontBackgroundColorDef = None self._paragraphAlignment = None self._paragraphLeftIndent = None self._paragraphLeftSubIndent = None self._paragraphRightIndent = None self._paragraphLineSpacing = None self._paragraphSpaceBefore = None self._paragraphSpaceAfter = None self._tabs = [] def __repr__(self): """ String Representation of the contents of the FormatDef object """ st = 'FormatDialog.FormatDef object:\n' st += 'fontFace: %s\n' % self.fontFace st += 'fontSize: %s\n' % self.fontSize st += 'fontWeight: ' if self.fontWeight == fd_OFF: st += 'fd_OFF' elif self.fontWeight == fd_BOLD: st += 'fd_BOLD' elif self.fontWeight == fd_AMBIGUOUS: st += 'fd_AMBIGUOUS' else: st += 'ILLEGAL SETTING "%s"' % self.fontWeight st += '\n' st += 'fontStyle: ' if self.fontStyle == fd_OFF: st += 'fd_OFF' elif self.fontStyle == fd_ITALIC: st += 'fd_ITALIC' elif self.fontStyle == fd_AMBIGUOUS: st += 'fd_AMBIGUOUS' else: st += 'ILLEGAL SETTING "%s"' % self.fontStyle st += '\n' st += 'fontUnderline: ' if self.fontUnderline == fd_OFF: st += 'fd_OFF' elif self.fontUnderline == fd_UNDERLINE: st += 'fd_UNDERLINE' elif self.fontUnderline == fd_AMBIGUOUS: st += 'fd_AMBIGUOUS' else: st += 'ILLEGAL SETTING "%s"' % self.fontUnderline st += '\n' st += 'fontColorName: %s\n' % self.fontColorName st += 'fontColorDef: %s\n' % (self.fontColorDef,) st += 'fontBackgroundColorName: %s\n' % self.fontBackgroundColorName st += 'fontBackgroundColorDef: %s\n\n' % (self.fontBackgroundColorDef,) st += "Alignment: %s (Left = %s, Center = %s, Right = %s\n" % (self.paragraphAlignment, wx.TEXT_ALIGNMENT_LEFT, wx.TEXT_ALIGNMENT_CENTRE, wx.TEXT_ALIGNMENT_RIGHT) st += "Left Indent: %s\n" % self.paragraphLeftIndent st += "Left SubIndent: %s\n" % self.paragraphLeftSubIndent st += " ... producing Left: %d, First Line: %d\n" % (self.paragraphLeftIndent + self.paragraphLeftSubIndent, 0 - self.paragraphLeftSubIndent) st += "Right Indent: %s\n" % self.paragraphRightIndent st += "Line Spacing: %s\n" % self.paragraphLineSpacing st += "Space Before: %s\n" % self.paragraphSpaceBefore st += "Space After: %s\n" % self.paragraphSpaceAfter st += "Tabs: %s\n\n" % self.tabs return st def copy(self): """ Create a copy of a FormatDef object """ # Create a new FormatDef object fdCopy = FormatDef() # Copy the existing data values to the new Object fdCopy.fontFace = self.fontFace fdCopy.fontSize = self.fontSize fdCopy.fontWeight = self.fontWeight fdCopy.fontStyle = self.fontStyle fdCopy.fontUnderline = self.fontUnderline # We don't need to copy fontColorName. Copying fontColorDef will take care of it. fdCopy.fontColorDef = self.fontColorDef fdCopy.fontBackgroundColorDef = self.fontBackgroundColorDef fdCopy.paragraphAlignment = self.paragraphAlignment fdCopy.paragraphLeftIndent = self.paragraphLeftIndent fdCopy.paragraphLeftSubIndent = self.paragraphLeftSubIndent fdCopy.paragraphRightIndent = self.paragraphRightIndent fdCopy.paragraphLineSpacing = self.paragraphLineSpacing fdCopy.paragraphSpaceBefore = self.paragraphSpaceBefore fdCopy.paragraphSpaceAfter = self.paragraphSpaceAfter fdCopy.tabs = self.tabs # Return the new Object return fdCopy # Property Getter and Setter functions def _getFontFace(self): return self._fontFace def _setFontFace(self, fontFace): if fontFace != None: self._fontFace = fontFace.strip() else: self._fontFace = fontFace def _delFontFace(self): self._fontFace = None def _getFontSize(self): return self._fontSize def _setFontSize(self, fontSize): # If the parameter cannot be converted to an integer, don't change the value. try: if fontSize == None: self._fontSize = None else: self._fontSize = int(fontSize) except: pass def _delFontSize(self): self._fontSize = None def _getFontWeight(self): return self._fontWeight def _setFontWeight(self, fontWeight): if fontWeight in [fd_OFF, fd_BOLD, fd_AMBIGUOUS]: self._fontWeight = fontWeight def _delFontWeight(self): self._fontWeight = fd_OFF def _getFontStyle(self): return self._fontStyle def _setFontStyle(self, fontStyle): if fontStyle in [fd_OFF, fd_ITALIC, fd_AMBIGUOUS]: self._fontStyle = fontStyle def _delFontStyle(self): self._fontStyle = fd_OFF def _getFontUnderline(self): return self._fontUnderline def _setFontUnderline(self, fontUnderline): if fontUnderline in [fd_OFF, fd_UNDERLINE, fd_AMBIGUOUS]: self._fontUnderline = fontUnderline def _delFontUnderline(self): self._fontUnderline = fd_OFF def _getFontColorName(self): return self._fontColorName def _setFontColorName(self, fontColorName): if fontColorName in TransanaGlobal.transana_colorNameList: self._fontColorName = fontColorName # Set fontColorDef to match fontColorName for (colorName, colorDef) in TransanaGlobal.transana_textColorList: if colorName == fontColorName: self._fontColorDef = wx.Colour(colorDef[0], colorDef[1], colorDef[2]) break def _delFontColorName(self): self._fontColorName = None self._fontColorDef = None def _getFontColorDef(self): return self._fontColorDef def _setFontColorDef(self, fontColorDef): self._fontColorDef = fontColorDef # Set fontColorName to match fontColorDef for (colorName, colorDef) in TransanaGlobal.transana_textColorList: if colorDef == fontColorDef: self._fontColorName = colorName break def _delFontColorDef(self): self._fontColorDef = None self._fontColorName = None def _getFontBackgroundColorName(self): return self._fontBackgroundColorName def _setFontBackgroundColorName(self, fontBackgroundColorName): if fontBackgroundColorName in TransanaGlobal.transana_colorNameList: self._fontBackgroundColorName = fontColorName # Set fontBackgroundColorDef to match fontColorName for (colorName, colorDef) in TransanaGlobal.transana_textColorList: if colorName == fontColorName: self._fontBackgroundColorDef = wx.Colour(colorDef[0], colorDef[1], colorDef[2]) break def _delFontBackgroundColorName(self): self._fontBackgroundColorName = None self._fontBackgroundColorDef = None def _getFontBackgroundColorDef(self): return self._fontBackgroundColorDef def _setFontBackgroundColorDef(self, fontBackgroundColorDef): self._fontBackgroundColorDef = fontBackgroundColorDef # Set fontBackgroundColorName to match fontBackgroundColorDef for (colorName, colorDef) in TransanaGlobal.transana_textColorList: if colorDef == fontBackgroundColorDef: self._fontBackgroundColorName = colorName break def _delFontBackgroundColorDef(self): self._fontBackgroundColorDef = None self._fontBackgroundColorName = None def _getParagraphAlignment(self): return self._paragraphAlignment def _setParagraphAlignment(self, paragraphAlignment): self._paragraphAlignment = paragraphAlignment def _delParagraphAlignment(self): self._paragraphAlignment = None def _getParagraphLeftIndent(self): return self._paragraphLeftIndent def _setParagraphLeftIndent(self, paragraphLeftIndent): self._paragraphLeftIndent = paragraphLeftIndent def _delParagraphLeftIndent(self): self._paragraphLeftIndent = None def _getParagraphLeftSubIndent(self): return self._paragraphLeftSubIndent def _setParagraphLeftSubIndent(self, paragraphLeftSubIndent): self._paragraphLeftSubIndent = paragraphLeftSubIndent def _delParagraphLeftSubIndent(self): self._paragraphLeftSubIndent = None def _getParagraphRightIndent(self): return self._paragraphRightIndent def _setParagraphRightIndent(self, paragraphRightIndent): self._paragraphRightIndent = paragraphRightIndent def _delParagraphRightIndent(self): self._paragraphRightIndent = None def _getParagraphLineSpacing(self): return self._paragraphLineSpacing def _setParagraphLineSpacing(self, paragraphLineSpacing): self._paragraphLineSpacing = paragraphLineSpacing def _delParagraphLineSpacing(self): self._paragraphLineSpacing = None def _getParagraphSpaceBefore(self): return self._paragraphSpaceBefore def _setParagraphSpaceBefore(self, paragraphSpaceBefore): self._paragraphSpaceBefore = paragraphSpaceBefore def _delParagraphSpaceBefore(self): self._paragraphSpaceBefore = None def _getParagraphSpaceAfter(self): return self._paragraphSpaceAfter def _setParagraphSpaceAfter(self, paragraphSpaceAfter): self._paragraphSpaceAfter = paragraphSpaceAfter def _delParagraphSpaceAfter(self): self._paragraphSpaceAfter = None def _getTabs(self): return self._tabs def _setTabs(self, tabs): self._tabs = tabs def _delTabs(self): self._tabs = [] # Public properties fontFace = property(_getFontFace, _setFontFace, _delFontFace, """ Font Face """) fontSize = property(_getFontSize, _setFontSize, _delFontSize, """ Font Size """) fontWeight = property(_getFontWeight, _setFontWeight, _delFontWeight, """ Font Weight [fd_OFF (0), fd_BOLD (1), fd_AMBIGUOUS (2)] """) fontStyle = property(_getFontStyle, _setFontStyle, _delFontStyle, """ Font Style [fd_OFF (0), fd_ITALIC (1), fd_AMBIGUOUS (2)] """) fontUnderline = property(_getFontUnderline, _setFontUnderline, _delFontUnderline, """ Font Underline [fd_OFF (0), fd_UNDERLINE (1), fd_AMBIGUOUS (2)] """) fontColorName = property(_getFontColorName, _setFontColorName, _delFontColorName, """ Font Color Name """) fontColorDef = property(_getFontColorDef, _setFontColorDef, _delFontColorDef, """ Font Color Definition """) fontBackgroundColorName = property(_getFontBackgroundColorName, _setFontBackgroundColorName, _delFontBackgroundColorName, """ Background Color Name """) fontBackgroundColorDef = property(_getFontBackgroundColorDef, _setFontBackgroundColorDef, _delFontBackgroundColorDef, """ Background Color Definition """) paragraphAlignment = property(_getParagraphAlignment, _setParagraphAlignment, _delParagraphAlignment, "Paragraph Alignment") paragraphLeftIndent = property(_getParagraphLeftIndent, _setParagraphLeftIndent, _delParagraphLeftIndent, "Paragraph Left Indent") paragraphLeftSubIndent = property(_getParagraphLeftSubIndent, _setParagraphLeftSubIndent, _delParagraphLeftSubIndent, "Paragraph Left SubIndent") paragraphRightIndent = property(_getParagraphRightIndent, _setParagraphRightIndent, _delParagraphRightIndent, "Paragraph Right Indent") paragraphLineSpacing = property(_getParagraphLineSpacing, _setParagraphLineSpacing, _delParagraphLineSpacing, "Paragraph Line Spacing") paragraphSpaceBefore = property(_getParagraphSpaceBefore, _setParagraphSpaceBefore, _delParagraphSpaceBefore, "Paragraph Space Before") paragraphSpaceAfter = property(_getParagraphSpaceAfter, _setParagraphSpaceAfter, _delParagraphSpaceAfter, "Paragraph Space After") tabs = property(_getTabs, _setTabs, _delTabs, "Tabs") class FormatDialog(wx.Dialog): """ Format Font and Paragraph properties """ def __init__(self, parent, formatData, tabToShow=0): self.parent = parent wx.Dialog.__init__(self, parent, -1, _('Format'), style=wx.CAPTION | wx.SYSTEM_MENU | wx.THICK_FRAME) # To look right, the Mac needs the Small Window Variant. if "__WXMAC__" in wx.PlatformInfo: self.SetWindowVariant(wx.WINDOW_VARIANT_SMALL) self.formatData = formatData # Create the main Sizer, which will hold the boxTop, boxMiddle, and boxButton sizers box = wx.BoxSizer(wx.VERTICAL) notebook = wx.Notebook(self, -1) # The Notebook Background Color prevents a visual anomoly in Arabic! notebook.SetBackgroundColour(wx.WHITE) box.Add(notebook, 1, wx.EXPAND, 2) self.panelFont = FormatFontPanel.FormatFontPanel(notebook, formatData) self.panelFont.SetAutoLayout(True) self.panelFont.Layout() notebook.AddPage(self.panelFont, _("Font"), True) self.panelParagraph = FormatParagraphPanel.FormatParagraphPanel(notebook, formatData) self.panelParagraph.SetAutoLayout(True) self.panelParagraph.Layout() notebook.AddPage(self.panelParagraph, _("Paragraph"), True) self.panelTabs = FormatTabsPanel.FormatTabsPanel(notebook, formatData) self.panelTabs.SetAutoLayout(True) self.panelTabs.Layout() notebook.AddPage(self.panelTabs, _("Tabs"), True) if tabToShow != notebook.GetSelection(): notebook.SetSelection(tabToShow) # Create the boxButtons sizer, which will hold the dialog box's buttons boxButtons = wx.BoxSizer(wx.HORIZONTAL) # Create an OK button btnOK = wx.Button(self, wx.ID_OK, _("OK")) btnOK.SetDefault() btnOK.Bind(wx.EVT_BUTTON, self.OnOK) boxButtons.Add(btnOK, 0, wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM | wx.RIGHT, 20) # Create a Cancel button btnCancel = wx.Button(self, wx.ID_CANCEL, _("Cancel")) btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel) boxButtons.Add(btnCancel, 0, wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM) # Add the boxButtons sizer to the main box sizer box.Add(boxButtons, 0, wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM | wx.ALL, 10) # Define box as the form's main sizer self.SetSizer(box) # Fit the form to the widgets created self.Fit() # Set this as the minimum size for the form. self.SetSizeHints(minW = self.GetSize()[0], minH = self.GetSize()[1]) # Tell the form to maintain the layout and have it set the intitial Layout self.SetAutoLayout(True) self.Layout() # Position the form in the center of the screen self.CentreOnScreen() # Update the Sample Font to match the initial settings wx.FutureCall(50, self.panelFont.SetSampleFont) def OnOK(self, event): """ OK Button Press """ # When the OK button is pressed, we take the local data (currentFont and currentColor) and # put that information into the formatData structure. self.formatData.fontFace = self.panelFont.txtFont.GetValue() self.formatData.fontSize = self.panelFont.txtSize.GetValue() self.formatData.fontWeight = self.panelFont.font.fontWeight self.formatData.fontStyle = self.panelFont.font.fontStyle self.formatData.fontUnderline = self.panelFont.font.fontUnderline self.formatData.fontColorDef = self.panelFont.font.fontColorDef self.formatData.fontBackgroundColorDef = self.panelFont.font.fontBackgroundColorDef self.formatData.paragraphAlignment = self.panelParagraph.formatData.paragraphAlignment # Dealing with Margins is tricky. try: # If AMBIGUOUS ... if self.panelParagraph.txtLeftIndent.GetValue() == '': # ... set the value to NONE leftVal = None # Otherwise ... else: # ... get the value from the panel control leftVal = float(self.panelParagraph.txtLeftIndent.GetValue()) # If an exception is raised in the float() conversion ... except: # ... assume illegal values equal 0 leftVal = 0.0 try: # If AMBIGUOUS ... if self.panelParagraph.txtFirstLineIndent.GetValue() == '': # ... set the value to NONE firstLineVal = None # Otherwise ... else: # ... get the value from the panel control firstLineVal = float(self.panelParagraph.txtFirstLineIndent.GetValue()) # If an exception is raised in the float() conversion ... except: # ... assume illegal values equal 0 firstLineVal = 0.0 try: # If AMBIGUOUS ... if self.panelParagraph.txtRightIndent.GetValue() == '': # ... set the value to NONE rightVal = None # Otherwise ... else: # ... get the value from the panel control rightVal = float(self.panelParagraph.txtRightIndent.GetValue()) # If an exception is raised in the float() conversion ... except: # ... assume illegal values equal 0 rightVal = 0.0 # The calculations are straight-forward, but the possibility of ambiguous values make that much more complicated. # leftSubIndent = 0.0 - firstLineVal # leftIndent = leftVal + firstLineVal # rightIndent = rightVal # print # print "FormatDialog:" # If the First Line Value is ambiguous ... if firstLineVal == None: # ... pass the ambiguity on to the calling routine self.formatData.paragraphLeftSubIndent = None # If the First Line Value is known ... else: # ... translate the units into tenths of a milimeter, which is what the RTC uses. # So if we are using INCHES ... if self.panelParagraph.rbUnitsInches.GetValue(): # ... convert to Left Sub and convert to 10ths of a milimeter self.formatData.paragraphLeftSubIndent = (0.0 - firstLineVal) * 254.0 # If we are using CENTIMETERS ... else: # ... convert to Left Sub and convert to 10ths of a milimeter self.formatData.paragraphLeftSubIndent = (0.0 - firstLineVal) * 100.0 # print " Converted First Line Value:", self.formatData.paragraphLeftSubIndent # If the Left Value is ambiguous ... if leftVal == None: # ... pass the ambiguity on to the calling routine self.formatData.paragraphLeftIndent = None self.formatData.paragraphLeftValue = None # If the Left Value is known ... else: # ... but we don't know the first line values ... if firstLineVal == None: # ... then the Left Indent is STILL ambiguous! self.formatData.paragraphLeftIndent = None # ... but we DO know HALF of the value of the calculation and need to take that into account. # Thus, let's add it to the object. # ... translate the units into tenths of a milimeter, which is what the RTC uses. # So if we are using INCHES ... if self.panelParagraph.rbUnitsInches.GetValue(): # ... convert to 10ths of a milimeter self.formatData.paragraphLeftValue = leftVal * 254.0 # If we are using CENTIMETERS ... else: # ... convert to 10ths of a milimeter self.formatData.paragraphLeftValue = leftVal * 100.0 # ... and the First Line Value IS known ... else: # ... then the Left Indent value is also known # ... translate the units into tenths of a milimeter, which is what the RTC uses. # So if we are using INCHES ... if self.panelParagraph.rbUnitsInches.GetValue(): # ... convert to 10ths of a milimeter self.formatData.paragraphLeftIndent = (leftVal + firstLineVal) * 254.0 # If we are using CENTIMETERS ... else: # ... convert to 10ths of a milimeter self.formatData.paragraphLeftIndent = (leftVal + firstLineVal) * 100.0 # print " Converted Left Indent:", self.formatData.paragraphLeftIndent, leftVal, firstLineVal, leftVal + firstLineVal # print "Line Spacing:", self.formatData.paragraphLineSpacing # print # If the Right Value is ambiguous ... if rightVal == None: # ... pass the ambiguity on to the calling routine self.formatData.paragraphRightIndent = None # If the Right Value is known ... else: # ... translate the units into tenths of a milimeter, which is what the RTC uses. # So if we are using INCHES ... if self.panelParagraph.rbUnitsInches.GetValue(): # ... convert to 10ths of a milimeter self.formatData.paragraphRightIndent = rightVal * 254.0 # If we are using CENTIMETERS ... else: # ... convert to 10ths of a milimeter self.formatData.paragraphRightIndent = rightVal * 100.0 self.formatData.paragraphLineSpacing = self.panelParagraph.formatData.paragraphLineSpacing # try: # self.formatData.paragraphSpaceBefore = int(self.panelParagraph.txtSpacingBefore.GetValue()) # except: # self.formatData.paragraphSpaceBefore = None # If the Space Before is ambiguous ... if self.formatData.paragraphSpaceBefore == None: # ... pass the ambiguity on to the calling routine self.formatData.paragraphSpaceBefore = None # If the Space Before is known ... else: # ... translate the units into tenths of a milimeter, which is what the RTC uses. # So if we are using INCHES ... if self.panelParagraph.rbUnitsInches.GetValue(): # ... convert to 10ths of a milimeter self.formatData.paragraphSpaceBefore = float(self.panelParagraph.txtSpacingBefore.GetValue()) * 254.0 # If we are using CENTIMETERS ... else: # ... convert to 10ths of a milimeter self.formatData.paragraphSpaceBefore = float(self.panelParagraph.txtSpacingBefore.GetValue()) * 100.0 # try: # self.formatData.paragraphSpaceAfter = int(self.panelParagraph.txtSpacingAfter.GetValue()) # except: # self.formatData.paragraphSpaceAfter = None # If the Space After is ambiguous ... if self.formatData.paragraphSpaceAfter == None: # ... pass the ambiguity on to the calling routine self.formatData.paragraphSpaceAfter = None # If the Space After is known ... else: # ... translate the units into tenths of a milimeter, which is what the RTC uses. # So if we are using INCHES ... if self.panelParagraph.rbUnitsInches.GetValue(): # ... convert to 10ths of a milimeter self.formatData.paragraphSpaceAfter = float(self.panelParagraph.txtSpacingAfter.GetValue()) * 254.0 # If we are using CENTIMETERS ... else: # ... convert to 10ths of a milimeter self.formatData.paragraphSpaceAfter = float(self.panelParagraph.txtSpacingAfter.GetValue()) * 100.0 tmpTabs = self.panelTabs.lbTabStops.GetItems() try: # See if there's a value in the "To Add" entry box that didn't actually get entered. # First, get the value. val = float(self.panelTabs.txtAdd.GetValue()) # IF the value is > 0 and not already in the list ... if (val > 0) and (not "%4.2f" % val in tmpTabs): # ... add it to the list ... tmpTabs.append("%4.2f" % val) # ... and sort the list. tmpTabs.sort() except: pass newTabs = [] for tab in tmpTabs: if self.panelTabs.rbUnitsInches.GetValue(): newTabs.append(int(round(float(tab) * 254.0))) else: newTabs.append(int(round(float(tab) * 100.0))) # If we sent tabs in OR we got tabs out ... if (self.formatData.tabs != None) or (len(newTabs) > 0): # ... update the tab stops self.formatData.tabs = newTabs # indicate that we are closing the form self.closing = True # Allow the form's OK event to fire to close the form event.Skip() def OnCancel(self, event): """ Cancel Button Press """ # If you hit Cancel on the Mac, you get a Segment Fault!! This is an attempt to fix that. # indicate that we are closing the form self.closing = True # Allow the form's Cancel event to fire to close the form event.Skip() def GetFormatDef(self): """ This method allows the calling routine to access the FormatDialog.FormatDef. This includes background color. """ # If the user pressed "OK", the originalFont has been updated to reflect the changes. # Otherwise, it is the unchanged original data. return self.formatData
gpl-2.0
YYWen0o0/python-frame-django
tests/model_options/test_default_related_name.py
41
1645
from django.test import TestCase from .models.default_related_name import Author, Editor, Book class DefaultRelatedNameTests(TestCase): def setUp(self): self.author = Author.objects.create(first_name="Dave", last_name="Loper") self.editor = Editor.objects.create(name="Test Editions", bestselling_author=self.author) self.book = Book.objects.create(title="Test Book", editor=self.editor) self.book.authors.add(self.author) self.book.save() def test_no_default_related_name(self): try: self.author.editor_set except AttributeError: self.fail("Author should have an editor_set relation.") def test_default_related_name(self): try: self.author.books except AttributeError: self.fail("Author should have a books relation.") def test_related_name_overrides_default_related_name(self): try: self.editor.edited_books except AttributeError: self.fail("Editor should have a edited_books relation.") def test_inheritance(self): try: # Here model_options corresponds to the name of the application used # in this test self.book.model_options_bookstores except AttributeError: self.fail("Book should have a model_options_bookstores relation.") def test_inheritance_with_overrided_default_related_name(self): try: self.book.editor_stores except AttributeError: self.fail("Book should have a editor_stores relation.")
bsd-3-clause
Qalthos/ansible
lib/ansible/modules/database/mysql/mysql_user.py
12
26349
#!/usr/bin/python # -*- coding: utf-8 -*- # Copyright: (c) 2012, Mark Theunissen <[email protected]> # Sponsored by Four Kitchens http://fourkitchens.com. # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} DOCUMENTATION = r''' --- module: mysql_user short_description: Adds or removes a user from a MySQL database description: - Adds or removes a user from a MySQL database. version_added: "0.6" options: name: description: - Name of the user (role) to add or remove. type: str required: true password: description: - Set the user's password.. type: str encrypted: description: - Indicate that the 'password' field is a `mysql_native_password` hash. type: bool default: no version_added: "2.0" host: description: - The 'host' part of the MySQL username. type: str default: localhost host_all: description: - Override the host option, making ansible apply changes to all hostnames for a given user. - This option cannot be used when creating users. type: bool default: no version_added: "2.1" priv: description: - "MySQL privileges string in the format: C(db.table:priv1,priv2)." - "Multiple privileges can be specified by separating each one using a forward slash: C(db.table:priv/db.table:priv)." - The format is based on MySQL C(GRANT) statement. - Database and table names can be quoted, MySQL-style. - If column privileges are used, the C(priv1,priv2) part must be exactly as returned by a C(SHOW GRANT) statement. If not followed, the module will always report changes. It includes grouping columns by permission (C(SELECT(col1,col2)) instead of C(SELECT(col1),SELECT(col2))). type: str append_privs: description: - Append the privileges defined by priv to the existing ones for this user instead of overwriting existing ones. type: bool default: no version_added: "1.4" sql_log_bin: description: - Whether binary logging should be enabled or disabled for the connection. type: bool default: yes version_added: "2.1" state: description: - Whether the user should exist. - When C(absent), removes the user. type: str choices: [ absent, present ] default: present check_implicit_admin: description: - Check if mysql allows login as root/nopassword before trying supplied credentials. type: bool default: no version_added: "1.3" update_password: description: - C(always) will update passwords if they differ. - C(on_create) will only set the password for newly created users. type: str choices: [ always, on_create ] default: always version_added: "2.0" notes: - "MySQL server installs with default login_user of 'root' and no password. To secure this user as part of an idempotent playbook, you must create at least two tasks: the first must change the root user's password, without providing any login_user/login_password details. The second must drop a ~/.my.cnf file containing the new root credentials. Subsequent runs of the playbook will then succeed by reading the new credentials from the file." - Currently, there is only support for the `mysql_native_password` encrypted password hash module. author: - Jonathan Mainguy (@Jmainguy) - Benjamin Malynovytch (@bmalynovytch) extends_documentation_fragment: mysql ''' EXAMPLES = r''' - name: Removes anonymous user account for localhost mysql_user: name: '' host: localhost state: absent - name: Removes all anonymous user accounts mysql_user: name: '' host_all: yes state: absent - name: Create database user with name 'bob' and password '12345' with all database privileges mysql_user: name: bob password: 12345 priv: '*.*:ALL' state: present - name: Create database user using hashed password with all database privileges mysql_user: name: bob password: '*EE0D72C1085C46C5278932678FBE2C6A782821B4' encrypted: yes priv: '*.*:ALL' state: present - name: Create database user with password and all database privileges and 'WITH GRANT OPTION' mysql_user: name: bob password: 12345 priv: '*.*:ALL,GRANT' state: present # Note that REQUIRESSL is a special privilege that should only apply to *.* by itself. - name: Modify user to require SSL connections. mysql_user: name: bob append_privs: yes priv: '*.*:REQUIRESSL' state: present - name: Ensure no user named 'sally'@'localhost' exists, also passing in the auth credentials. mysql_user: login_user: root login_password: 123456 name: sally state: absent - name: Ensure no user named 'sally' exists at all mysql_user: name: sally host_all: yes state: absent - name: Specify grants composed of more than one word mysql_user: name: replication password: 12345 priv: "*.*:REPLICATION CLIENT" state: present - name: Revoke all privileges for user 'bob' and password '12345' mysql_user: name: bob password: 12345 priv: "*.*:USAGE" state: present # Example privileges string format # mydb.*:INSERT,UPDATE/anotherdb.*:SELECT/yetanotherdb.*:ALL - name: Example using login_unix_socket to connect to server mysql_user: name: root password: abc123 login_unix_socket: /var/run/mysqld/mysqld.sock - name: Example of skipping binary logging while adding user 'bob' mysql_user: name: bob password: 12345 priv: "*.*:USAGE" state: present sql_log_bin: no # Example .my.cnf file for setting the root password # [client] # user=root # password=n<_665{vS43y ''' import re import string from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.database import SQLParseError from ansible.module_utils.mysql import mysql_connect, mysql_driver, mysql_driver_fail_msg from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native VALID_PRIVS = frozenset(('CREATE', 'DROP', 'GRANT', 'GRANT OPTION', 'LOCK TABLES', 'REFERENCES', 'EVENT', 'ALTER', 'DELETE', 'INDEX', 'INSERT', 'SELECT', 'UPDATE', 'CREATE TEMPORARY TABLES', 'TRIGGER', 'CREATE VIEW', 'SHOW VIEW', 'ALTER ROUTINE', 'CREATE ROUTINE', 'EXECUTE', 'FILE', 'CREATE TABLESPACE', 'CREATE USER', 'PROCESS', 'PROXY', 'RELOAD', 'REPLICATION CLIENT', 'REPLICATION SLAVE', 'SHOW DATABASES', 'SHUTDOWN', 'SUPER', 'ALL', 'ALL PRIVILEGES', 'USAGE', 'REQUIRESSL', 'CREATE ROLE', 'DROP ROLE', 'APPLICATION PASSWORD ADMIN', 'AUDIT ADMIN', 'BACKUP ADMIN', 'BINLOG ADMIN', 'BINLOG ENCRYPTION ADMIN', 'CONNECTION ADMIN', 'ENCRYPTION KEY ADMIN', 'FIREWALL ADMIN', 'FIREWALL USER', 'GROUP REPLICATION ADMIN', 'PERSIST RO VARIABLES ADMIN', 'REPLICATION SLAVE ADMIN', 'RESOURCE GROUP ADMIN', 'RESOURCE GROUP USER', 'ROLE ADMIN', 'SET USER ID', 'SESSION VARIABLES ADMIN', 'SYSTEM VARIABLES ADMIN', 'VERSION TOKEN ADMIN', 'XA RECOVER ADMIN')) class InvalidPrivsError(Exception): pass # =========================================== # MySQL module specific support methods. # # User Authentication Management changed in MySQL 5.7 and MariaDB 10.2.0 def use_old_user_mgmt(cursor): cursor.execute("SELECT VERSION()") result = cursor.fetchone() version_str = result[0] version = version_str.split('.') if 'mariadb' in version_str.lower(): # Prior to MariaDB 10.2 if int(version[0]) * 1000 + int(version[1]) < 10002: return True else: return False else: # Prior to MySQL 5.7 if int(version[0]) * 1000 + int(version[1]) < 5007: return True else: return False def get_mode(cursor): cursor.execute('SELECT @@GLOBAL.sql_mode') result = cursor.fetchone() mode_str = result[0] if 'ANSI' in mode_str: mode = 'ANSI' else: mode = 'NOTANSI' return mode def user_exists(cursor, user, host, host_all): if host_all: cursor.execute("SELECT count(*) FROM mysql.user WHERE user = %s", ([user])) else: cursor.execute("SELECT count(*) FROM mysql.user WHERE user = %s AND host = %s", (user, host)) count = cursor.fetchone() return count[0] > 0 def user_add(cursor, user, host, host_all, password, encrypted, new_priv, check_mode): # we cannot create users without a proper hostname if host_all: return False if check_mode: return True if password and encrypted: cursor.execute("CREATE USER %s@%s IDENTIFIED BY PASSWORD %s", (user, host, password)) elif password and not encrypted: cursor.execute("CREATE USER %s@%s IDENTIFIED BY %s", (user, host, password)) else: cursor.execute("CREATE USER %s@%s", (user, host)) if new_priv is not None: for db_table, priv in iteritems(new_priv): privileges_grant(cursor, user, host, db_table, priv) return True def is_hash(password): ishash = False if len(password) == 41 and password[0] == '*': if frozenset(password[1:]).issubset(string.hexdigits): ishash = True return ishash def user_mod(cursor, user, host, host_all, password, encrypted, new_priv, append_privs, module): changed = False msg = "User unchanged" grant_option = False if host_all: hostnames = user_get_hostnames(cursor, [user]) else: hostnames = [host] for host in hostnames: # Handle clear text and hashed passwords. if bool(password): # Determine what user management method server uses old_user_mgmt = use_old_user_mgmt(cursor) # Get a list of valid columns in mysql.user table to check if Password and/or authentication_string exist cursor.execute(""" SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'mysql' AND TABLE_NAME = 'user' AND COLUMN_NAME IN ('Password', 'authentication_string') ORDER BY COLUMN_NAME DESC LIMIT 1 """) colA = cursor.fetchone() cursor.execute(""" SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'mysql' AND TABLE_NAME = 'user' AND COLUMN_NAME IN ('Password', 'authentication_string') ORDER BY COLUMN_NAME ASC LIMIT 1 """) colB = cursor.fetchone() # Select hash from either Password or authentication_string, depending which one exists and/or is filled cursor.execute(""" SELECT COALESCE( CASE WHEN %s = '' THEN NULL ELSE %s END, CASE WHEN %s = '' THEN NULL ELSE %s END ) FROM mysql.user WHERE user = %%s AND host = %%s """ % (colA[0], colA[0], colB[0], colB[0]), (user, host)) current_pass_hash = cursor.fetchone()[0] if encrypted: encrypted_password = password if not is_hash(encrypted_password): module.fail_json(msg="encrypted was specified however it does not appear to be a valid hash expecting: *SHA1(SHA1(your_password))") else: if old_user_mgmt: cursor.execute("SELECT PASSWORD(%s)", (password,)) else: cursor.execute("SELECT CONCAT('*', UCASE(SHA1(UNHEX(SHA1(%s)))))", (password,)) encrypted_password = cursor.fetchone()[0] if current_pass_hash != encrypted_password: msg = "Password updated" if module.check_mode: return (True, msg) if old_user_mgmt: cursor.execute("SET PASSWORD FOR %s@%s = %s", (user, host, encrypted_password)) msg = "Password updated (old style)" else: try: cursor.execute("ALTER USER %s@%s IDENTIFIED WITH mysql_native_password AS %s", (user, host, encrypted_password)) msg = "Password updated (new style)" except (mysql_driver.Error) as e: # https://stackoverflow.com/questions/51600000/authentication-string-of-root-user-on-mysql # Replacing empty root password with new authentication mechanisms fails with error 1396 if e.args[0] == 1396: cursor.execute( "UPDATE user SET plugin = %s, authentication_string = %s, Password = '' WHERE User = %s AND Host = %s", ('mysql_native_password', encrypted_password, user, host) ) cursor.execute("FLUSH PRIVILEGES") msg = "Password forced update" else: raise e changed = True # Handle privileges if new_priv is not None: curr_priv = privileges_get(cursor, user, host) # If the user has privileges on a db.table that doesn't appear at all in # the new specification, then revoke all privileges on it. for db_table, priv in iteritems(curr_priv): # If the user has the GRANT OPTION on a db.table, revoke it first. if "GRANT" in priv: grant_option = True if db_table not in new_priv: if user != "root" and "PROXY" not in priv and not append_privs: msg = "Privileges updated" if module.check_mode: return (True, msg) privileges_revoke(cursor, user, host, db_table, priv, grant_option) changed = True # If the user doesn't currently have any privileges on a db.table, then # we can perform a straight grant operation. for db_table, priv in iteritems(new_priv): if db_table not in curr_priv: msg = "New privileges granted" if module.check_mode: return (True, msg) privileges_grant(cursor, user, host, db_table, priv) changed = True # If the db.table specification exists in both the user's current privileges # and in the new privileges, then we need to see if there's a difference. db_table_intersect = set(new_priv.keys()) & set(curr_priv.keys()) for db_table in db_table_intersect: priv_diff = set(new_priv[db_table]) ^ set(curr_priv[db_table]) if len(priv_diff) > 0: msg = "Privileges updated" if module.check_mode: return (True, msg) if not append_privs: privileges_revoke(cursor, user, host, db_table, curr_priv[db_table], grant_option) privileges_grant(cursor, user, host, db_table, new_priv[db_table]) changed = True return (changed, msg) def user_delete(cursor, user, host, host_all, check_mode): if check_mode: return True if host_all: hostnames = user_get_hostnames(cursor, [user]) for hostname in hostnames: cursor.execute("DROP USER %s@%s", (user, hostname)) else: cursor.execute("DROP USER %s@%s", (user, host)) return True def user_get_hostnames(cursor, user): cursor.execute("SELECT Host FROM mysql.user WHERE user = %s", user) hostnames_raw = cursor.fetchall() hostnames = [] for hostname_raw in hostnames_raw: hostnames.append(hostname_raw[0]) return hostnames def privileges_get(cursor, user, host): """ MySQL doesn't have a better method of getting privileges aside from the SHOW GRANTS query syntax, which requires us to then parse the returned string. Here's an example of the string that is returned from MySQL: GRANT USAGE ON *.* TO 'user'@'localhost' IDENTIFIED BY 'pass'; This function makes the query and returns a dictionary containing the results. The dictionary format is the same as that returned by privileges_unpack() below. """ output = {} cursor.execute("SHOW GRANTS FOR %s@%s", (user, host)) grants = cursor.fetchall() def pick(x): if x == 'ALL PRIVILEGES': return 'ALL' else: return x for grant in grants: res = re.match("""GRANT (.+) ON (.+) TO (['`"]).*\\3@(['`"]).*\\4( IDENTIFIED BY PASSWORD (['`"]).+\\6)? ?(.*)""", grant[0]) if res is None: raise InvalidPrivsError('unable to parse the MySQL grant string: %s' % grant[0]) privileges = res.group(1).split(", ") privileges = [pick(x) for x in privileges] if "WITH GRANT OPTION" in res.group(7): privileges.append('GRANT') if "REQUIRE SSL" in res.group(7): privileges.append('REQUIRESSL') db = res.group(2) output[db] = privileges return output def privileges_unpack(priv, mode): """ Take a privileges string, typically passed as a parameter, and unserialize it into a dictionary, the same format as privileges_get() above. We have this custom format to avoid using YAML/JSON strings inside YAML playbooks. Example of a privileges string: mydb.*:INSERT,UPDATE/anotherdb.*:SELECT/yetanother.*:ALL The privilege USAGE stands for no privileges, so we add that in on *.* if it's not specified in the string, as MySQL will always provide this by default. """ if mode == 'ANSI': quote = '"' else: quote = '`' output = {} privs = [] for item in priv.strip().split('/'): pieces = item.strip().rsplit(':', 1) dbpriv = pieces[0].rsplit(".", 1) # Check for FUNCTION or PROCEDURE object types parts = dbpriv[0].split(" ", 1) object_type = '' if len(parts) > 1 and (parts[0] == 'FUNCTION' or parts[0] == 'PROCEDURE'): object_type = parts[0] + ' ' dbpriv[0] = parts[1] # Do not escape if privilege is for database or table, i.e. # neither quote *. nor .* for i, side in enumerate(dbpriv): if side.strip('`') != '*': dbpriv[i] = '%s%s%s' % (quote, side.strip('`'), quote) pieces[0] = object_type + '.'.join(dbpriv) if '(' in pieces[1]: output[pieces[0]] = re.split(r',\s*(?=[^)]*(?:\(|$))', pieces[1].upper()) for i in output[pieces[0]]: privs.append(re.sub(r'\s*\(.*\)', '', i)) else: output[pieces[0]] = pieces[1].upper().split(',') privs = output[pieces[0]] new_privs = frozenset(privs) if not new_privs.issubset(VALID_PRIVS): raise InvalidPrivsError('Invalid privileges specified: %s' % new_privs.difference(VALID_PRIVS)) if '*.*' not in output: output['*.*'] = ['USAGE'] # if we are only specifying something like REQUIRESSL and/or GRANT (=WITH GRANT OPTION) in *.* # we still need to add USAGE as a privilege to avoid syntax errors if 'REQUIRESSL' in priv and not set(output['*.*']).difference(set(['GRANT', 'REQUIRESSL'])): output['*.*'].append('USAGE') return output def privileges_revoke(cursor, user, host, db_table, priv, grant_option): # Escape '%' since mysql db.execute() uses a format string db_table = db_table.replace('%', '%%') if grant_option: query = ["REVOKE GRANT OPTION ON %s" % db_table] query.append("FROM %s@%s") query = ' '.join(query) cursor.execute(query, (user, host)) priv_string = ",".join([p for p in priv if p not in ('GRANT', 'REQUIRESSL')]) query = ["REVOKE %s ON %s" % (priv_string, db_table)] query.append("FROM %s@%s") query = ' '.join(query) cursor.execute(query, (user, host)) def privileges_grant(cursor, user, host, db_table, priv): # Escape '%' since mysql db.execute uses a format string and the # specification of db and table often use a % (SQL wildcard) db_table = db_table.replace('%', '%%') priv_string = ",".join([p for p in priv if p not in ('GRANT', 'REQUIRESSL')]) query = ["GRANT %s ON %s" % (priv_string, db_table)] query.append("TO %s@%s") if 'REQUIRESSL' in priv: query.append("REQUIRE SSL") if 'GRANT' in priv: query.append("WITH GRANT OPTION") query = ' '.join(query) cursor.execute(query, (user, host)) # =========================================== # Module execution. # def main(): module = AnsibleModule( argument_spec=dict( login_user=dict(type='str'), login_password=dict(type='str', no_log=True), login_host=dict(type='str', default='localhost'), login_port=dict(type='int', default=3306), login_unix_socket=dict(type='str'), user=dict(type='str', required=True, aliases=['name']), password=dict(type='str', no_log=True), encrypted=dict(type='bool', default=False), host=dict(type='str', default='localhost'), host_all=dict(type="bool", default=False), state=dict(type='str', default='present', choices=['absent', 'present']), priv=dict(type='str'), append_privs=dict(type='bool', default=False), check_implicit_admin=dict(type='bool', default=False), update_password=dict(type='str', default='always', choices=['always', 'on_create']), connect_timeout=dict(type='int', default=30), config_file=dict(type='path', default='~/.my.cnf'), sql_log_bin=dict(type='bool', default=True), client_cert=dict(type='path', aliases=['ssl_cert']), client_key=dict(type='path', aliases=['ssl_key']), ca_cert=dict(type='path', aliases=['ssl_ca']), ), supports_check_mode=True, ) login_user = module.params["login_user"] login_password = module.params["login_password"] user = module.params["user"] password = module.params["password"] encrypted = module.boolean(module.params["encrypted"]) host = module.params["host"].lower() host_all = module.params["host_all"] state = module.params["state"] priv = module.params["priv"] check_implicit_admin = module.params['check_implicit_admin'] connect_timeout = module.params['connect_timeout'] config_file = module.params['config_file'] append_privs = module.boolean(module.params["append_privs"]) update_password = module.params['update_password'] ssl_cert = module.params["client_cert"] ssl_key = module.params["client_key"] ssl_ca = module.params["ca_cert"] db = '' sql_log_bin = module.params["sql_log_bin"] if mysql_driver is None: module.fail_json(msg=mysql_driver_fail_msg) cursor = None try: if check_implicit_admin: try: cursor = mysql_connect(module, 'root', '', config_file, ssl_cert, ssl_key, ssl_ca, db, connect_timeout=connect_timeout) except Exception: pass if not cursor: cursor = mysql_connect(module, login_user, login_password, config_file, ssl_cert, ssl_key, ssl_ca, db, connect_timeout=connect_timeout) except Exception as e: module.fail_json(msg="unable to connect to database, check login_user and login_password are correct or %s has the credentials. " "Exception message: %s" % (config_file, to_native(e))) if not sql_log_bin: cursor.execute("SET SQL_LOG_BIN=0;") if priv is not None: try: mode = get_mode(cursor) except Exception as e: module.fail_json(msg=to_native(e)) try: priv = privileges_unpack(priv, mode) except Exception as e: module.fail_json(msg="invalid privileges string: %s" % to_native(e)) if state == "present": if user_exists(cursor, user, host, host_all): try: if update_password == 'always': changed, msg = user_mod(cursor, user, host, host_all, password, encrypted, priv, append_privs, module) else: changed, msg = user_mod(cursor, user, host, host_all, None, encrypted, priv, append_privs, module) except (SQLParseError, InvalidPrivsError, mysql_driver.Error) as e: module.fail_json(msg=to_native(e)) else: if host_all: module.fail_json(msg="host_all parameter cannot be used when adding a user") try: changed = user_add(cursor, user, host, host_all, password, encrypted, priv, module.check_mode) if changed: msg = "User added" except (SQLParseError, InvalidPrivsError, mysql_driver.Error) as e: module.fail_json(msg=to_native(e)) elif state == "absent": if user_exists(cursor, user, host, host_all): changed = user_delete(cursor, user, host, host_all, module.check_mode) msg = "User deleted" else: changed = False msg = "User doesn't exist" module.exit_json(changed=changed, user=user, msg=msg) if __name__ == '__main__': main()
gpl-3.0
benjyw/pants
src/python/pants/base/exception_sink.py
4
17975
# Copyright 2018 Pants project contributors (see CONTRIBUTORS.md). # Licensed under the Apache License, Version 2.0 (see LICENSE). import datetime import faulthandler import logging import os import signal import sys import threading import traceback from contextlib import contextmanager from typing import Callable, Dict, Iterator import psutil import setproctitle from pants.util.dirutil import safe_mkdir, safe_open from pants.util.osutil import Pid logger = logging.getLogger(__name__) class SignalHandler: """A specification for how to handle a fixed set of nonfatal signals. This is subclassed and registered with ExceptionSink.reset_signal_handler() whenever the signal handling behavior is modified for different pants processes, for example in the remote client when pantsd is enabled. The default behavior is to exit "gracefully" by leaving a detailed log of which signal was received, then exiting with failure. Note that the terminal will convert a ctrl-c from the user into a SIGINT. """ @property def signal_handler_mapping(self) -> Dict[signal.Signals, Callable]: """A dict mapping (signal number) -> (a method handling the signal).""" # Could use an enum here, but we never end up doing any matching on the specific signal value, # instead just iterating over the registered signals to set handlers, so a dict is probably # better. return { signal.SIGINT: self._handle_sigint_if_enabled, signal.SIGQUIT: self.handle_sigquit, signal.SIGTERM: self.handle_sigterm, } def __init__(self, *, pantsd_instance: bool): self._ignore_sigint_lock = threading.Lock() self._ignoring_sigint = False self._pantsd_instance = pantsd_instance def _handle_sigint_if_enabled(self, signum: int, _frame): with self._ignore_sigint_lock: if not self._ignoring_sigint: self.handle_sigint(signum, _frame) def _toggle_ignoring_sigint(self, toggle: bool) -> None: if not self._pantsd_instance: with self._ignore_sigint_lock: self._ignoring_sigint = toggle def _send_signal_to_children(self, received_signal: int, signame: str) -> None: """Send a signal to any children of this process in order. Pants may have spawned multiple subprocesses via Python or Rust. Upon receiving a signal, this method is invoked to propagate the signal to all children, regardless of how they were spawned. """ self_process = psutil.Process() children = self_process.children() logger.debug(f"Sending signal {signame} ({received_signal}) to child processes: {children}") for child_process in children: child_process.send_signal(received_signal) def handle_sigint(self, signum: int, _frame): self._send_signal_to_children(signum, "SIGINT") raise KeyboardInterrupt("User interrupted execution with control-c!") # TODO(#7406): figure out how to let sys.exit work in a signal handler instead of having to raise # this exception! class SignalHandledNonLocalExit(Exception): """Raised in handlers for non-fatal signals to overcome Python limitations. When waiting on a subprocess and in a signal handler, sys.exit appears to be ignored, and causes the signal handler to return. We want to (eventually) exit after these signals, not ignore them, so we raise this exception instead and check it in our sys.excepthook override. """ def __init__(self, signum, signame): self.signum = signum self.signame = signame self.traceback_lines = traceback.format_stack() super(SignalHandler.SignalHandledNonLocalExit, self).__init__() if "I/O operation on closed file" in self.traceback_lines: logger.debug( "SignalHandledNonLocalExit: unexpected appearance of " "'I/O operation on closed file' in traceback" ) def handle_sigquit(self, signum, _frame): self._send_signal_to_children(signum, "SIGQUIT") raise self.SignalHandledNonLocalExit(signum, "SIGQUIT") def handle_sigterm(self, signum, _frame): self._send_signal_to_children(signum, "SIGTERM") raise self.SignalHandledNonLocalExit(signum, "SIGTERM") class ExceptionSink: """A mutable singleton object representing where exceptions should be logged to. The ExceptionSink should be installed in any process that is running Pants @rules via the engine. Notably, this does _not_ include the pantsd client, which does its own signal handling directly in order to forward information to the pantsd server. """ # NB: see the bottom of this file where we call reset_log_location() and other mutators in order # to properly setup global state. _log_dir = None # Where to log stacktraces to in a SIGUSR2 handler. _interactive_output_stream = None # An instance of `SignalHandler` which is invoked to handle a static set of specific nonfatal # signals (these signal handlers are allowed to make pants exit, but unlike SIGSEGV they don't # need to exit immediately). _signal_handler: SignalHandler = SignalHandler(pantsd_instance=False) # These persistent open file descriptors are kept so the signal handler can do almost no work # (and lets faulthandler figure out signal safety). _pid_specific_error_fileobj = None _shared_error_fileobj = None def __new__(cls, *args, **kwargs): raise TypeError( "Instances of {} are not allowed to be constructed! Call install() instead.".format( cls.__name__ ) ) class ExceptionSinkError(Exception): pass @classmethod def install(cls, log_location: str, pantsd_instance: bool) -> None: """Setup global state for this process, such as signal handlers and sys.excepthook.""" # Set the log location for writing logs before bootstrap options are parsed. cls.reset_log_location(log_location) # NB: Mutate process-global state! sys.excepthook = ExceptionSink.log_exception # Setup a default signal handler. cls.reset_signal_handler(SignalHandler(pantsd_instance=pantsd_instance)) # All reset_* methods are ~idempotent! @classmethod def reset_log_location(cls, new_log_location: str) -> None: """Re-acquire file handles to error logs based in the new location. Class state: - Overwrites `cls._log_dir`, `cls._pid_specific_error_fileobj`, and `cls._shared_error_fileobj`. OS state: - May create a new directory. - Overwrites signal handlers for many fatal and non-fatal signals (but not SIGUSR2). :raises: :class:`ExceptionSink.ExceptionSinkError` if the directory does not exist or is not writable. """ # We could no-op here if the log locations are the same, but there's no reason not to have the # additional safety of re-acquiring file descriptors each time (and erroring out early if the # location is no longer writable). try: safe_mkdir(new_log_location) except Exception as e: raise cls.ExceptionSinkError( "The provided log location path at '{}' is not writable or could not be created: {}.".format( new_log_location, str(e) ), e, ) pid = os.getpid() pid_specific_log_path = cls.exceptions_log_path(for_pid=pid, in_dir=new_log_location) shared_log_path = cls.exceptions_log_path(in_dir=new_log_location) assert pid_specific_log_path != shared_log_path try: pid_specific_error_stream = safe_open(pid_specific_log_path, mode="w") shared_error_stream = safe_open(shared_log_path, mode="a") except Exception as e: raise cls.ExceptionSinkError( "Error opening fatal error log streams for log location '{}': {}".format( new_log_location, str(e) ) ) # NB: mutate process-global state! if faulthandler.is_enabled(): logger.debug("re-enabling faulthandler") # Call Py_CLEAR() on the previous error stream: # https://github.com/vstinner/faulthandler/blob/master/faulthandler.c faulthandler.disable() # Send a stacktrace to this file if interrupted by a fatal error. faulthandler.enable(file=pid_specific_error_stream, all_threads=True) # NB: mutate the class variables! cls._log_dir = new_log_location cls._pid_specific_error_fileobj = pid_specific_error_stream cls._shared_error_fileobj = shared_error_stream @classmethod def exceptions_log_path(cls, for_pid=None, in_dir=None): """Get the path to either the shared or pid-specific fatal errors log file.""" if for_pid is None: intermediate_filename_component = "" else: assert isinstance(for_pid, Pid) intermediate_filename_component = ".{}".format(for_pid) in_dir = in_dir or cls._log_dir return os.path.join( in_dir, ".pids", "exceptions{}.log".format(intermediate_filename_component) ) @classmethod def _log_exception(cls, msg): """Try to log an error message to this process's error log and the shared error log. NB: Doesn't raise (logs an error instead). """ pid = os.getpid() fatal_error_log_entry = cls._format_exception_message(msg, pid) # We care more about this log than the shared log, so write to it first. try: cls._try_write_with_flush(cls._pid_specific_error_fileobj, fatal_error_log_entry) except Exception as e: logger.error( "Error logging the message '{}' to the pid-specific file handle for {} at pid {}:\n{}".format( msg, cls._log_dir, pid, e ) ) # Write to the shared log. try: # TODO: we should probably guard this against concurrent modification by other pants # subprocesses somehow. cls._try_write_with_flush(cls._shared_error_fileobj, fatal_error_log_entry) except Exception as e: logger.error( "Error logging the message '{}' to the shared file handle for {} at pid {}:\n{}".format( msg, cls._log_dir, pid, e ) ) @classmethod def _try_write_with_flush(cls, fileobj, payload): """This method is here so that it can be patched to simulate write errors. This is because mock can't patch primitive objects like file objects. """ fileobj.write(payload) fileobj.flush() @classmethod def reset_signal_handler(cls, signal_handler: SignalHandler) -> SignalHandler: """Given a SignalHandler, uses the `signal` std library functionality to set the pants process's signal handlers to those specified in the object. Note that since this calls `signal.signal()`, it will crash if not the main thread. Returns the previously-registered signal handler. """ for signum, handler in signal_handler.signal_handler_mapping.items(): signal.signal(signum, handler) # Retry any system calls interrupted by any of the signals we just installed handlers for # (instead of having them raise EINTR). siginterrupt(3) says this is the default behavior on # Linux and OSX. signal.siginterrupt(signum, False) previous_signal_handler = cls._signal_handler cls._signal_handler = signal_handler return previous_signal_handler @classmethod @contextmanager def trapped_signals(cls, new_signal_handler: SignalHandler) -> Iterator[None]: """A contextmanager which temporarily overrides signal handling. NB: This method calls signal.signal(), which will crash if not called from the main thread! """ previous_signal_handler = cls.reset_signal_handler(new_signal_handler) try: yield finally: cls.reset_signal_handler(previous_signal_handler) @classmethod @contextmanager def ignoring_sigint(cls) -> Iterator[None]: """This method provides a context that temporarily disables responding to the SIGINT signal sent by a Ctrl-C in the terminal. We currently only use this to implement disabling catching SIGINT while an InteractiveProcess is running (where we want that process to catch it), and only when pantsd is not enabled (if pantsd is enabled, the client will actually catch SIGINT and forward it to the server, so we don't want the server process to ignore it. """ try: cls._signal_handler._toggle_ignoring_sigint(True) yield finally: cls._signal_handler._toggle_ignoring_sigint(False) @classmethod def _iso_timestamp_for_now(cls): return datetime.datetime.now().isoformat() # NB: This includes a trailing newline, but no leading newline. _EXCEPTION_LOG_FORMAT = """\ timestamp: {timestamp} process title: {process_title} sys.argv: {args} pid: {pid} {message} """ @classmethod def _format_exception_message(cls, msg, pid): return cls._EXCEPTION_LOG_FORMAT.format( timestamp=cls._iso_timestamp_for_now(), process_title=setproctitle.getproctitle(), args=sys.argv, pid=pid, message=msg, ) _traceback_omitted_default_text = "(backtrace omitted)" @classmethod def _format_traceback(cls, traceback_lines, should_print_backtrace): if should_print_backtrace: traceback_string = "\n{}".format("".join(traceback_lines)) else: traceback_string = " {}".format(cls._traceback_omitted_default_text) return traceback_string _UNHANDLED_EXCEPTION_LOG_FORMAT = """\ Exception caught: ({exception_type}){backtrace} Exception message: {exception_message}{maybe_newline} """ @classmethod def _format_unhandled_exception_log(cls, exc, tb, add_newline, should_print_backtrace): exc_type = type(exc) exception_full_name = "{}.{}".format(exc_type.__module__, exc_type.__name__) exception_message = str(exc) if exc else "(no message)" maybe_newline = "\n" if add_newline else "" return cls._UNHANDLED_EXCEPTION_LOG_FORMAT.format( exception_type=exception_full_name, backtrace=cls._format_traceback( traceback_lines=traceback.format_tb(tb), should_print_backtrace=should_print_backtrace, ), exception_message=exception_message, maybe_newline=maybe_newline, ) @classmethod def log_exception(cls, exc_class=None, exc=None, tb=None, add_newline=False): """Logs an unhandled exception to a variety of locations.""" exc_class = exc_class or sys.exc_info()[0] exc = exc or sys.exc_info()[1] tb = tb or sys.exc_info()[2] # This exception was raised by a signal handler with the intent to exit the program. if exc_class == SignalHandler.SignalHandledNonLocalExit: return cls._handle_signal_gracefully(exc.signum, exc.signame, exc.traceback_lines) extra_err_msg = None try: # Always output the unhandled exception details into a log file, including the # traceback. exception_log_entry = cls._format_unhandled_exception_log( exc, tb, add_newline, should_print_backtrace=True ) cls._log_exception(exception_log_entry) except Exception as e: extra_err_msg = "Additional error logging unhandled exception {}: {}".format(exc, e) logger.error(extra_err_msg) # The rust logger implementation will have its own stacktrace, but at import time, we want # to be able to see any stacktrace to know where the error is being raised, so we reproduce # it here. exception_log_entry = cls._format_unhandled_exception_log( exc, tb, add_newline, should_print_backtrace=True ) logger.exception(exception_log_entry) @classmethod def _handle_signal_gracefully(cls, signum, signame, traceback_lines): """Signal handler for non-fatal signals which raises or logs an error.""" def gen_formatted(formatted_traceback: str) -> str: return f"Signal {signum} ({signame}) was raised. Exiting with failure.{formatted_traceback}" # Extract the stack, and format an entry to be written to the exception log. formatted_traceback = cls._format_traceback( traceback_lines=traceback_lines, should_print_backtrace=True ) signal_error_log_entry = gen_formatted(formatted_traceback) # TODO: determine the appropriate signal-safe behavior here (to avoid writing to our file # descriptors reentrantly, which raises an IOError). # This method catches any exceptions raised within it. cls._log_exception(signal_error_log_entry) # Create a potentially-abbreviated traceback for the terminal or other interactive stream. formatted_traceback_for_terminal = cls._format_traceback( traceback_lines=traceback_lines, should_print_backtrace=True, ) terminal_log_entry = gen_formatted(formatted_traceback_for_terminal) # Print the output via standard logging. logger.error(terminal_log_entry)
apache-2.0
mxOBS/deb-pkg_trusty_chromium-browser
tools/gyp/pylib/gyp/generator/eclipse.py
2
16953
# 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. """GYP backend that generates Eclipse CDT settings files. This backend DOES NOT generate Eclipse CDT projects. Instead, it generates XML files that can be imported into an Eclipse CDT project. The XML file contains a list of include paths and symbols (i.e. defines). Because a full .cproject definition is not created by this generator, it's not possible to properly define the include dirs and symbols for each file individually. Instead, one set of includes/symbols is generated for the entire project. This works fairly well (and is a vast improvement in general), but may still result in a few indexer issues here and there. This generator has no automated tests, so expect it to be broken. """ from xml.sax.saxutils import escape import os.path import subprocess import gyp import gyp.common import gyp.msvs_emulation import shlex import xml.etree.cElementTree as ET generator_wants_static_library_dependencies_adjusted = False generator_default_variables = { } for dirname in ['INTERMEDIATE_DIR', 'PRODUCT_DIR', 'LIB_DIR', 'SHARED_LIB_DIR']: # Some gyp steps fail if these are empty(!), so we convert them to variables generator_default_variables[dirname] = '$' + dirname for unused in ['RULE_INPUT_PATH', 'RULE_INPUT_ROOT', 'RULE_INPUT_NAME', 'RULE_INPUT_DIRNAME', 'RULE_INPUT_EXT', 'EXECUTABLE_PREFIX', 'EXECUTABLE_SUFFIX', 'STATIC_LIB_PREFIX', 'STATIC_LIB_SUFFIX', 'SHARED_LIB_PREFIX', 'SHARED_LIB_SUFFIX', 'CONFIGURATION_NAME']: generator_default_variables[unused] = '' # Include dirs will occasionally use the SHARED_INTERMEDIATE_DIR variable as # part of the path when dealing with generated headers. This value will be # replaced dynamically for each configuration. generator_default_variables['SHARED_INTERMEDIATE_DIR'] = \ '$SHARED_INTERMEDIATE_DIR' def CalculateVariables(default_variables, params): generator_flags = params.get('generator_flags', {}) for key, val in generator_flags.items(): default_variables.setdefault(key, val) flavor = gyp.common.GetFlavor(params) default_variables.setdefault('OS', flavor) if flavor == 'win': # Copy additional generator configuration data from VS, which is shared # by the Eclipse generator. import gyp.generator.msvs as msvs_generator generator_additional_non_configuration_keys = getattr(msvs_generator, 'generator_additional_non_configuration_keys', []) generator_additional_path_sections = getattr(msvs_generator, 'generator_additional_path_sections', []) gyp.msvs_emulation.CalculateCommonVariables(default_variables, params) def CalculateGeneratorInputInfo(params): """Calculate the generator specific info that gets fed to input (called by gyp).""" generator_flags = params.get('generator_flags', {}) if generator_flags.get('adjust_static_libraries', False): global generator_wants_static_library_dependencies_adjusted generator_wants_static_library_dependencies_adjusted = True def GetAllIncludeDirectories(target_list, target_dicts, shared_intermediate_dirs, config_name, params, compiler_path): """Calculate the set of include directories to be used. Returns: A list including all the include_dir's specified for every target followed by any include directories that were added as cflag compiler options. """ gyp_includes_set = set() compiler_includes_list = [] # Find compiler's default include dirs. if compiler_path: command = shlex.split(compiler_path) command.extend(['-E', '-xc++', '-v', '-']) proc = subprocess.Popen(args=command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = proc.communicate()[1] # Extract the list of include dirs from the output, which has this format: # ... # #include "..." search starts here: # #include <...> search starts here: # /usr/include/c++/4.6 # /usr/local/include # End of search list. # ... in_include_list = False for line in output.splitlines(): if line.startswith('#include'): in_include_list = True continue if line.startswith('End of search list.'): break if in_include_list: include_dir = line.strip() if include_dir not in compiler_includes_list: compiler_includes_list.append(include_dir) flavor = gyp.common.GetFlavor(params) if flavor == 'win': generator_flags = params.get('generator_flags', {}) for target_name in target_list: target = target_dicts[target_name] if config_name in target['configurations']: config = target['configurations'][config_name] # Look for any include dirs that were explicitly added via cflags. This # may be done in gyp files to force certain includes to come at the end. # TODO(jgreenwald): Change the gyp files to not abuse cflags for this, and # remove this. if flavor == 'win': msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags) cflags = msvs_settings.GetCflags(config_name) else: cflags = config['cflags'] for cflag in cflags: if cflag.startswith('-I'): include_dir = cflag[2:] if include_dir not in compiler_includes_list: compiler_includes_list.append(include_dir) # Find standard gyp include dirs. if config.has_key('include_dirs'): include_dirs = config['include_dirs'] for shared_intermediate_dir in shared_intermediate_dirs: for include_dir in include_dirs: include_dir = include_dir.replace('$SHARED_INTERMEDIATE_DIR', shared_intermediate_dir) if not os.path.isabs(include_dir): base_dir = os.path.dirname(target_name) include_dir = base_dir + '/' + include_dir include_dir = os.path.abspath(include_dir) gyp_includes_set.add(include_dir) # Generate a list that has all the include dirs. all_includes_list = list(gyp_includes_set) all_includes_list.sort() for compiler_include in compiler_includes_list: if not compiler_include in gyp_includes_set: all_includes_list.append(compiler_include) # All done. return all_includes_list def GetCompilerPath(target_list, data, options): """Determine a command that can be used to invoke the compiler. Returns: If this is a gyp project that has explicit make settings, try to determine the compiler from that. Otherwise, see if a compiler was specified via the CC_target environment variable. """ # First, see if the compiler is configured in make's settings. build_file, _, _ = gyp.common.ParseQualifiedTarget(target_list[0]) make_global_settings_dict = data[build_file].get('make_global_settings', {}) for key, value in make_global_settings_dict: if key in ['CC', 'CXX']: return os.path.join(options.toplevel_dir, value) # Check to see if the compiler was specified as an environment variable. for key in ['CC_target', 'CC', 'CXX']: compiler = os.environ.get(key) if compiler: return compiler return 'gcc' def GetAllDefines(target_list, target_dicts, data, config_name, params, compiler_path): """Calculate the defines for a project. Returns: A dict that includes explict defines declared in gyp files along with all of the default defines that the compiler uses. """ # Get defines declared in the gyp files. all_defines = {} flavor = gyp.common.GetFlavor(params) if flavor == 'win': generator_flags = params.get('generator_flags', {}) for target_name in target_list: target = target_dicts[target_name] if flavor == 'win': msvs_settings = gyp.msvs_emulation.MsvsSettings(target, generator_flags) extra_defines = msvs_settings.GetComputedDefines(config_name) else: extra_defines = [] if config_name in target['configurations']: config = target['configurations'][config_name] target_defines = config['defines'] else: target_defines = [] for define in target_defines + extra_defines: split_define = define.split('=', 1) if len(split_define) == 1: split_define.append('1') if split_define[0].strip() in all_defines: # Already defined continue all_defines[split_define[0].strip()] = split_define[1].strip() # Get default compiler defines (if possible). if flavor == 'win': return all_defines # Default defines already processed in the loop above. if compiler_path: command = shlex.split(compiler_path) command.extend(['-E', '-dM', '-']) cpp_proc = subprocess.Popen(args=command, cwd='.', stdin=subprocess.PIPE, stdout=subprocess.PIPE) cpp_output = cpp_proc.communicate()[0] cpp_lines = cpp_output.split('\n') for cpp_line in cpp_lines: if not cpp_line.strip(): continue cpp_line_parts = cpp_line.split(' ', 2) key = cpp_line_parts[1] if len(cpp_line_parts) >= 3: val = cpp_line_parts[2] else: val = '1' all_defines[key] = val return all_defines def WriteIncludePaths(out, eclipse_langs, include_dirs): """Write the includes section of a CDT settings export file.""" out.write(' <section name="org.eclipse.cdt.internal.ui.wizards.' \ 'settingswizards.IncludePaths">\n') out.write(' <language name="holder for library settings"></language>\n') for lang in eclipse_langs: out.write(' <language name="%s">\n' % lang) for include_dir in include_dirs: out.write(' <includepath workspace_path="false">%s</includepath>\n' % include_dir) out.write(' </language>\n') out.write(' </section>\n') def WriteMacros(out, eclipse_langs, defines): """Write the macros section of a CDT settings export file.""" out.write(' <section name="org.eclipse.cdt.internal.ui.wizards.' \ 'settingswizards.Macros">\n') out.write(' <language name="holder for library settings"></language>\n') for lang in eclipse_langs: out.write(' <language name="%s">\n' % lang) for key in sorted(defines.iterkeys()): out.write(' <macro><name>%s</name><value>%s</value></macro>\n' % (escape(key), escape(defines[key]))) out.write(' </language>\n') out.write(' </section>\n') def GenerateOutputForConfig(target_list, target_dicts, data, params, config_name): options = params['options'] generator_flags = params.get('generator_flags', {}) # build_dir: relative path from source root to our output files. # e.g. "out/Debug" build_dir = os.path.join(generator_flags.get('output_dir', 'out'), config_name) toplevel_build = os.path.join(options.toplevel_dir, build_dir) # Ninja uses out/Debug/gen while make uses out/Debug/obj/gen as the # SHARED_INTERMEDIATE_DIR. Include both possible locations. shared_intermediate_dirs = [os.path.join(toplevel_build, 'obj', 'gen'), os.path.join(toplevel_build, 'gen')] GenerateCdtSettingsFile(target_list, target_dicts, data, params, config_name, os.path.join(toplevel_build, 'eclipse-cdt-settings.xml'), options, shared_intermediate_dirs) GenerateClasspathFile(target_list, target_dicts, options.toplevel_dir, toplevel_build, os.path.join(toplevel_build, 'eclipse-classpath.xml')) def GenerateCdtSettingsFile(target_list, target_dicts, data, params, config_name, out_name, options, shared_intermediate_dirs): gyp.common.EnsureDirExists(out_name) with open(out_name, 'w') as out: out.write('<?xml version="1.0" encoding="UTF-8"?>\n') out.write('<cdtprojectproperties>\n') eclipse_langs = ['C++ Source File', 'C Source File', 'Assembly Source File', 'GNU C++', 'GNU C', 'Assembly'] compiler_path = GetCompilerPath(target_list, data, options) include_dirs = GetAllIncludeDirectories(target_list, target_dicts, shared_intermediate_dirs, config_name, params, compiler_path) WriteIncludePaths(out, eclipse_langs, include_dirs) defines = GetAllDefines(target_list, target_dicts, data, config_name, params, compiler_path) WriteMacros(out, eclipse_langs, defines) out.write('</cdtprojectproperties>\n') def GenerateClasspathFile(target_list, target_dicts, toplevel_dir, toplevel_build, out_name): '''Generates a classpath file suitable for symbol navigation and code completion of Java code (such as in Android projects) by finding all .java and .jar files used as action inputs.''' gyp.common.EnsureDirExists(out_name) result = ET.Element('classpath') def AddElements(kind, paths): # First, we need to normalize the paths so they are all relative to the # toplevel dir. rel_paths = set() for path in paths: if os.path.isabs(path): rel_paths.add(os.path.relpath(path, toplevel_dir)) else: rel_paths.add(path) for path in sorted(rel_paths): entry_element = ET.SubElement(result, 'classpathentry') entry_element.set('kind', kind) entry_element.set('path', path) AddElements('lib', GetJavaJars(target_list, target_dicts, toplevel_dir)) AddElements('src', GetJavaSourceDirs(target_list, target_dicts)) # Include the standard JRE container and a dummy out folder AddElements('con', ['org.eclipse.jdt.launching.JRE_CONTAINER']) # Include a dummy out folder so that Eclipse doesn't use the default /bin # folder in the root of the project. AddElements('output', [os.path.join(toplevel_build, '.eclipse-java-build')]) ET.ElementTree(result).write(out_name) def GetJavaJars(target_list, target_dicts, toplevel_dir): '''Generates a sequence of all .jars used as inputs.''' for target_name in target_list: target = target_dicts[target_name] for action in target.get('actions', []): for input_ in action['inputs']: if os.path.splitext(input_)[1] == '.jar' and not input_.startswith('$'): if os.path.isabs(input_): yield input_ else: yield os.path.join(os.path.dirname(target_name), input_) def GetJavaSourceDirs(target_list, target_dicts): '''Generates a sequence of all likely java package root directories.''' for target_name in target_list: target = target_dicts[target_name] for action in target.get('actions', []): for input_ in action['inputs']: if (os.path.splitext(input_)[1] == '.java' and not input_.startswith('$')): dir_ = os.path.dirname(os.path.join(os.path.dirname(target_name), input_)) # If there is a parent 'src' or 'java' folder, navigate up to it - # these are canonical package root names in Chromium. This will # break if 'src' or 'java' exists in the package structure. This # could be further improved by inspecting the java file for the # package name if this proves to be too fragile in practice. parent_search = dir_ while os.path.basename(parent_search) not in ['src', 'java']: parent_search, _ = os.path.split(parent_search) if not parent_search: # Didn't find a known root, just return the original path yield dir_ break else: yield parent_search def GenerateOutput(target_list, target_dicts, data, params): """Generate an XML settings file that can be imported into a CDT project.""" if params['options'].generator_output: raise NotImplementedError("--generator_output not implemented for eclipse") user_config = params.get('generator_flags', {}).get('config', None) if user_config: GenerateOutputForConfig(target_list, target_dicts, data, params, user_config) else: config_names = target_dicts[target_list[0]]['configurations'].keys() for config_name in config_names: GenerateOutputForConfig(target_list, target_dicts, data, params, config_name)
bsd-3-clause
Just-D/panda3d
direct/src/extensions_native/extension_native_helpers.py
9
4505
### Tools __all__ = ["Dtool_ObjectToDict", "Dtool_funcToMethod", "Dtool_PreloadDLL"] import imp, sys, os # The following code exists to work around a problem that exists # with Python 2.5 or greater. # Specifically, Python 2.5 is designed to import files named *.pyd # only; it will not import files named *.dll (or *.so). We work # around this problem by explicitly preloading all of the dll's we # expect to need. dll_suffix = '' if sys.platform == "win32": # On Windows, dynamic libraries end in ".dll". dll_ext = '.dll' module_ext = '.pyd' # We allow the caller to preload dll_suffix into the sys module. dll_suffix = getattr(sys, 'dll_suffix', None) if dll_suffix is None: # Otherwise, we try to determine it from the executable name: # python_d.exe implies _d across the board. dll_suffix = '' if sys.executable.endswith('_d.exe'): dll_suffix = '_d' elif sys.platform == "darwin": # On OSX, the dynamic libraries usually end in .dylib, but # sometimes we need .so. try: from direct.extensions_native.extensions_darwin import dll_ext except ImportError: dll_ext = '.dylib' module_ext = '.so' else: # On most other UNIX systems (including linux), .so is used. dll_ext = '.so' module_ext = '.so' if sys.platform == "win32": # On Windows, we must furthermore ensure that the PATH is modified # to locate all of the DLL files. # First, search for the directory that contains all of our compiled # modules. target = None filename = "libpandaexpress%s%s" % (dll_suffix, dll_ext) for dir in sys.path + [sys.prefix]: lib = os.path.join(dir, filename) if (os.path.exists(lib)): target = dir if target == None: message = "Cannot find %s" % (filename) raise ImportError(message) # And add that directory to the system path. path = os.environ["PATH"] if not path.startswith(target + ";"): os.environ["PATH"] = target + ";" + path def Dtool_FindModule(module): # Finds a .pyd module on the Python path. filename = module.replace('.', os.path.sep) + module_ext for dir in sys.path: lib = os.path.join(dir, filename) if (os.path.exists(lib)): return lib return None def Dtool_PreloadDLL(module): if module in sys.modules: return # First find it as a .pyd module on the Python path. if Dtool_FindModule(module): # OK, we should have no problem importing it as is. return # Nope, we'll need to search for a dynamic lib and preload it. # Search for the appropriate directory. target = None filename = module.replace('.', os.path.sep) + dll_suffix + dll_ext for dir in sys.path + [sys.prefix]: lib = os.path.join(dir, filename) if (os.path.exists(lib)): target = dir break if target is None: message = "DLL loader cannot find %s." % (module) raise ImportError(message) # Now import the file explicitly. pathname = os.path.join(target, filename) imp.load_dynamic(module, pathname) # Nowadays, we can compile libpandaexpress with libpanda into a # .pyd file called panda3d/core.pyd which can be imported without # any difficulty. Let's see if this is the case. # In order to support things like py2exe that play games with the # physical python files on disk, we can't entirely rely on # Dtool_FindModule to find our panda3d.core module. However, we # should be able to import it. To differentiate the old-style Panda # build (with .dll's) from the new-style Panda build (with .pyd's), we # first try to import panda3d.core directly; if it succeeds we're in a # new-style build, and if it fails we must be in an old-style build. try: from panda3d.core import * except ImportError: Dtool_PreloadDLL("libpandaexpress") from libpandaexpress import * def Dtool_ObjectToDict(cls, name, obj): cls.DtoolClassDict[name] = obj; def Dtool_funcToMethod(func, cls, method_name=None): """Adds func to class so it is an accessible method; use method_name to specify the name to be used for calling the method. The new method is accessible to any instance immediately.""" if sys.version_info < (3, 0): func.im_class = cls func.im_func = func func.im_self = None if not method_name: method_name = func.__name__ cls.DtoolClassDict[method_name] = func;
bsd-3-clause
hug33k/PyTalk-R2D2
r2d2.py
1
1042
#!/usr/local/bin/python3.5 import wave import time import pyaudio import sys import os if __name__ == '__main__': baseLocation = os.path.dirname(__file__) if len(sys.argv) == 2: word = sys.argv[-1].lower() elif len(sys.argv) == 1: word = ["a","b","c","c1","d","e","f","g","g1","h","i","j","k","l","m","n","o","o1","p","q","r","s","s1","t","u","u1","v","w","x","y","z"] else: print("Usage : ./r2d2.py (string)") sys.exit(-1) root = baseLocation + "/sounds/{0}.wav" p = pyaudio.PyAudio() stream = p.open(format = p.get_format_from_width(2), channels = 1, rate = 22050, output = True) data = b"" chunk = 1024 for letter in word: if not letter.isalpha(): continue try: with wave.open(root.format(letter), "rb") as f: data += f.readframes(f.getnframes()) except Exception as e: print(e) stream.write(data) wf = wave.open(baseLocation + "/out.wav", 'wb') wf.setnchannels(1) wf.setsampwidth(p.get_sample_size(pyaudio.paInt16)) wf.setframerate(22050) wf.writeframes(data) wf.close() p.terminate()
mit
fwenzel/stapler
staplelib/tests.py
1
1812
#!/usr/bin/env python import os.path import shutil from subprocess import check_call import tempfile import unittest from PyPDF2 import PdfFileReader HERE = os.path.abspath(os.path.dirname(__file__)) TESTFILE_DIR = os.path.join(HERE, 'testfiles') STAPLER = os.path.join(HERE, '..', 'stapler') ONEPAGE_PDF = os.path.join(TESTFILE_DIR, '1page.pdf') FIVEPAGE_PDF = os.path.join(TESTFILE_DIR, '5page.pdf') class TestStapler(unittest.TestCase): """Some unit tests for the stapler tool.""" def setUp(self): self.tmpdir = tempfile.mkdtemp() self.outputfile = os.path.join(self.tmpdir, 'output.pdf') os.chdir(self.tmpdir) def tearDown(self): shutil.rmtree(self.tmpdir) os.chdir(HERE) def test_cat(self): """Make sure files are properly concatenated.""" check_call([STAPLER, 'cat', ONEPAGE_PDF, FIVEPAGE_PDF, self.outputfile]) self.assert_(os.path.isfile(self.outputfile)) pdf = PdfFileReader(file(self.outputfile, 'rb')) self.assertEqual(pdf.getNumPages(), 6) def test_cat_glob(self): """Make sure wildcard inputs work.""" check_call([STAPLER, 'cat', os.path.join(TESTFILE_DIR, '*.pdf'), self.outputfile]) pdf = PdfFileReader(file(self.outputfile, 'rb')) self.assertEqual(pdf.getNumPages(), 6) def test_split(self): """Make sure a file is properly split into pages.""" check_call([STAPLER, 'split', FIVEPAGE_PDF]) filelist = os.listdir(self.tmpdir) self.assertEqual(len(filelist), 5) for f in os.listdir(self.tmpdir): pdf = PdfFileReader(file(os.path.join(self.tmpdir, f), 'rb')) self.assertEqual(pdf.getNumPages(), 1) if __name__ == '__main__': unittest.main()
bsd-3-clause
jhd/spunout
flask/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/exceptions.py
374
3274
# urllib3/exceptions.py # Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt) # # This module is part of urllib3 and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php ## Base Exceptions class HTTPError(Exception): "Base exception used by this module." pass class PoolError(HTTPError): "Base exception for errors caused within a pool." def __init__(self, pool, message): self.pool = pool HTTPError.__init__(self, "%s: %s" % (pool, message)) def __reduce__(self): # For pickling purposes. return self.__class__, (None, None) class RequestError(PoolError): "Base exception for PoolErrors that have associated URLs." def __init__(self, pool, url, message): self.url = url PoolError.__init__(self, pool, message) def __reduce__(self): # For pickling purposes. return self.__class__, (None, self.url, None) class SSLError(HTTPError): "Raised when SSL certificate fails in an HTTPS connection." pass class ProxyError(HTTPError): "Raised when the connection to a proxy fails." pass class DecodeError(HTTPError): "Raised when automatic decoding based on Content-Type fails." pass ## Leaf Exceptions class MaxRetryError(RequestError): "Raised when the maximum number of retries is exceeded." def __init__(self, pool, url, reason=None): self.reason = reason message = "Max retries exceeded with url: %s" % url if reason: message += " (Caused by %s: %s)" % (type(reason), reason) else: message += " (Caused by redirect)" RequestError.__init__(self, pool, url, message) class HostChangedError(RequestError): "Raised when an existing pool gets a request for a foreign host." def __init__(self, pool, url, retries=3): message = "Tried to open a foreign host with url: %s" % url RequestError.__init__(self, pool, url, message) self.retries = retries class TimeoutStateError(HTTPError): """ Raised when passing an invalid state to a timeout """ pass class TimeoutError(HTTPError): """ Raised when a socket timeout error occurs. Catching this error will catch both :exc:`ReadTimeoutErrors <ReadTimeoutError>` and :exc:`ConnectTimeoutErrors <ConnectTimeoutError>`. """ pass class ReadTimeoutError(TimeoutError, RequestError): "Raised when a socket timeout occurs while receiving data from a server" pass # This timeout error does not have a URL attached and needs to inherit from the # base HTTPError class ConnectTimeoutError(TimeoutError): "Raised when a socket timeout occurs while connecting to a server" pass class EmptyPoolError(PoolError): "Raised when a pool runs out of connections and no more are allowed." pass class ClosedPoolError(PoolError): "Raised when a request enters a pool after the pool has been closed." pass class LocationParseError(ValueError, HTTPError): "Raised when get_host or similar fails to parse the URL input." def __init__(self, location): message = "Failed to parse: %s" % location HTTPError.__init__(self, message) self.location = location
gpl-3.0
fbossy/SickRage
lib/feedparser/feedparsertest.py
25
39905
#!/usr/bin/env python __author__ = "Mark Pilgrim <http://diveintomark.org/>" __license__ = """ Copyright (c) 2010-2013 Kurt McKee <[email protected]> Copyright (c) 2004-2008 Mark Pilgrim 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. 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 codecs import datetime import glob import operator import os import posixpath import pprint import re import struct import sys import threading import time import unittest import urllib import warnings import zlib import BaseHTTPServer import SimpleHTTPServer import feedparser if not feedparser._XML_AVAILABLE: sys.stderr.write('No XML parsers available, unit testing can not proceed\n') sys.exit(1) try: # the utf_32 codec was introduced in Python 2.6; it's necessary to # check this as long as feedparser supports Python 2.4 and 2.5 codecs.lookup('utf_32') except LookupError: _UTF32_AVAILABLE = False else: _UTF32_AVAILABLE = True _s2bytes = feedparser._s2bytes _l2bytes = feedparser._l2bytes #---------- custom HTTP server (used to serve test feeds) ---------- _PORT = 8097 # not really configurable, must match hardcoded port in tests _HOST = '127.0.0.1' # also not really configurable class FeedParserTestRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): headers_re = re.compile(_s2bytes(r"^Header:\s+([^:]+):(.+)$"), re.MULTILINE) def send_head(self): """Send custom headers defined in test case Example: <!-- Header: Content-type: application/atom+xml Header: X-Foo: bar --> """ # Short-circuit the HTTP status test `test_redirect_to_304()` if self.path == '/-/return-304.xml': self.send_response(304) self.send_header('Content-type', 'text/xml') self.end_headers() return feedparser._StringIO(u''.encode('utf-8')) path = self.translate_path(self.path) # the compression tests' filenames determine the header sent if self.path.startswith('/tests/compression'): if self.path.endswith('gz'): headers = {'Content-Encoding': 'gzip'} else: headers = {'Content-Encoding': 'deflate'} headers['Content-type'] = 'application/xml' else: headers = dict([(k.decode('utf-8'), v.decode('utf-8').strip()) for k, v in self.headers_re.findall(open(path, 'rb').read())]) f = open(path, 'rb') if (self.headers.get('if-modified-since') == headers.get('Last-Modified', 'nom')) \ or (self.headers.get('if-none-match') == headers.get('ETag', 'nomatch')): status = 304 else: status = 200 headers.setdefault('Status', status) self.send_response(int(headers['Status'])) headers.setdefault('Content-type', self.guess_type(path)) self.send_header("Content-type", headers['Content-type']) self.send_header("Content-Length", str(os.stat(f.name)[6])) for k, v in headers.items(): if k not in ('Status', 'Content-type'): self.send_header(k, v) self.end_headers() return f def log_request(self, *args): pass class FeedParserTestServer(threading.Thread): """HTTP Server that runs in a thread and handles a predetermined number of requests""" def __init__(self, requests): threading.Thread.__init__(self) self.requests = requests self.ready = threading.Event() def run(self): self.httpd = BaseHTTPServer.HTTPServer((_HOST, _PORT), FeedParserTestRequestHandler) self.ready.set() while self.requests: self.httpd.handle_request() self.requests -= 1 self.ready.clear() #---------- dummy test case class (test methods are added dynamically) ---------- unicode1_re = re.compile(_s2bytes(" u'")) unicode2_re = re.compile(_s2bytes(' u"')) # _bytes is only used in everythingIsUnicode(). # In Python 2 it's str, and in Python 3 it's bytes. _bytes = type(_s2bytes('')) def everythingIsUnicode(d): """Takes a dictionary, recursively verifies that every value is unicode""" for k, v in d.iteritems(): if isinstance(v, dict) and k != 'headers': if not everythingIsUnicode(v): return False elif isinstance(v, list): for i in v: if isinstance(i, dict) and not everythingIsUnicode(i): return False elif isinstance(i, _bytes): return False elif isinstance(v, _bytes): return False return True def failUnlessEval(self, xmlfile, evalString, msg=None): """Fail unless eval(evalString, env)""" env = feedparser.parse(xmlfile) try: if not eval(evalString, globals(), env): failure=(msg or 'not eval(%s) \nWITH env(%s)' % (evalString, pprint.pformat(env))) raise self.failureException, failure if not everythingIsUnicode(env): raise self.failureException, "not everything is unicode \nWITH env(%s)" % (pprint.pformat(env), ) except SyntaxError: # Python 3 doesn't have the `u""` syntax, so evalString needs to be modified, # which will require the failure message to be updated evalString = re.sub(unicode1_re, _s2bytes(" '"), evalString) evalString = re.sub(unicode2_re, _s2bytes(' "'), evalString) if not eval(evalString, globals(), env): failure=(msg or 'not eval(%s) \nWITH env(%s)' % (evalString, pprint.pformat(env))) raise self.failureException, failure class BaseTestCase(unittest.TestCase): failUnlessEval = failUnlessEval class TestCase(BaseTestCase): pass class TestTemporaryFallbackBehavior(unittest.TestCase): "These tests are temporarily here because of issues 310 and 328" def test_issue_328_fallback_behavior(self): warnings.filterwarnings('error') d = feedparser.FeedParserDict() d['published'] = u'pub string' d['published_parsed'] = u'pub tuple' d['updated'] = u'upd string' d['updated_parsed'] = u'upd tuple' # Ensure that `updated` doesn't map to `published` when it exists self.assertTrue('published' in d) self.assertTrue('published_parsed' in d) self.assertTrue('updated' in d) self.assertTrue('updated_parsed' in d) self.assertEqual(d['published'], 'pub string') self.assertEqual(d['published_parsed'], 'pub tuple') self.assertEqual(d['updated'], 'upd string') self.assertEqual(d['updated_parsed'], 'upd tuple') d = feedparser.FeedParserDict() d['published'] = u'pub string' d['published_parsed'] = u'pub tuple' # Ensure that `updated` doesn't actually exist self.assertTrue('updated' not in d) self.assertTrue('updated_parsed' not in d) # Ensure that accessing `updated` throws a DeprecationWarning try: d['updated'] except DeprecationWarning: # Expected behavior pass else: # Wrong behavior self.assertEqual(True, False) try: d['updated_parsed'] except DeprecationWarning: # Expected behavior pass else: # Wrong behavior self.assertEqual(True, False) # Ensure that `updated` maps to `published` warnings.filterwarnings('ignore') self.assertEqual(d['updated'], u'pub string') self.assertEqual(d['updated_parsed'], u'pub tuple') warnings.resetwarnings() class TestEverythingIsUnicode(unittest.TestCase): "Ensure that `everythingIsUnicode()` is working appropriately" def test_everything_is_unicode(self): self.assertTrue(everythingIsUnicode( {'a': u'a', 'b': [u'b', {'c': u'c'}], 'd': {'e': u'e'}} )) def test_not_everything_is_unicode(self): self.assertFalse(everythingIsUnicode({'a': _s2bytes('a')})) self.assertFalse(everythingIsUnicode({'a': [_s2bytes('a')]})) self.assertFalse(everythingIsUnicode({'a': {'b': _s2bytes('b')}})) self.assertFalse(everythingIsUnicode({'a': [{'b': _s2bytes('b')}]})) class TestLooseParser(BaseTestCase): "Test the sgmllib-based parser by manipulating feedparser " \ "into believing no XML parsers are installed" def __init__(self, arg): unittest.TestCase.__init__(self, arg) self._xml_available = feedparser._XML_AVAILABLE def setUp(self): feedparser._XML_AVAILABLE = 0 def tearDown(self): feedparser._XML_AVAILABLE = self._xml_available class TestStrictParser(BaseTestCase): pass class TestMicroformats(BaseTestCase): pass class TestEncodings(BaseTestCase): def test_doctype_replacement(self): "Ensure that non-ASCII-compatible encodings don't hide " \ "disallowed ENTITY declarations" doc = """<?xml version="1.0" encoding="utf-16be"?> <!DOCTYPE feed [ <!ENTITY exponential1 "bogus "> <!ENTITY exponential2 "&exponential1;&exponential1;"> <!ENTITY exponential3 "&exponential2;&exponential2;"> ]> <feed><title type="html">&exponential3;</title></feed>""" doc = codecs.BOM_UTF16_BE + doc.encode('utf-16be') result = feedparser.parse(doc) self.assertEqual(result['feed']['title'], u'&amp;exponential3') def test_gb2312_converted_to_gb18030_in_xml_encoding(self): # \u55de was chosen because it exists in gb18030 but not gb2312 feed = u'''<?xml version="1.0" encoding="gb2312"?> <feed><title>\u55de</title></feed>''' result = feedparser.parse(feed.encode('gb18030'), response_headers={ 'Content-Type': 'text/xml' }) self.assertEqual(result.encoding, 'gb18030') class TestFeedParserDict(unittest.TestCase): "Ensure that FeedParserDict returns values as expected and won't crash" def setUp(self): self.d = feedparser.FeedParserDict() def _check_key(self, k): self.assertTrue(k in self.d) self.assertTrue(hasattr(self.d, k)) self.assertEqual(self.d[k], 1) self.assertEqual(getattr(self.d, k), 1) def _check_no_key(self, k): self.assertTrue(k not in self.d) self.assertTrue(not hasattr(self.d, k)) def test_empty(self): keys = ( 'a','entries', 'id', 'guid', 'summary', 'subtitle', 'description', 'category', 'enclosures', 'license', 'categories', ) for k in keys: self._check_no_key(k) self.assertTrue('items' not in self.d) self.assertTrue(hasattr(self.d, 'items')) # dict.items() exists def test_neutral(self): self.d['a'] = 1 self._check_key('a') def test_single_mapping_target_1(self): self.d['id'] = 1 self._check_key('id') self._check_key('guid') def test_single_mapping_target_2(self): self.d['guid'] = 1 self._check_key('id') self._check_key('guid') def test_multiple_mapping_target_1(self): self.d['summary'] = 1 self._check_key('summary') self._check_key('description') def test_multiple_mapping_target_2(self): self.d['subtitle'] = 1 self._check_key('subtitle') self._check_key('description') def test_multiple_mapping_mapped_key(self): self.d['description'] = 1 self._check_key('summary') self._check_key('description') def test_license(self): self.d['links'] = [] try: self.d['license'] self.assertTrue(False) except KeyError: pass self.d['links'].append({'rel': 'license'}) try: self.d['license'] self.assertTrue(False) except KeyError: pass self.d['links'].append({'rel': 'license', 'href': 'http://dom.test/'}) self.assertEqual(self.d['license'], 'http://dom.test/') def test_category(self): self.d['tags'] = [] try: self.d['category'] self.assertTrue(False) except KeyError: pass self.d['tags'] = [{}] try: self.d['category'] self.assertTrue(False) except KeyError: pass self.d['tags'] = [{'term': 'cat'}] self.assertEqual(self.d['category'], 'cat') self.d['tags'].append({'term': 'dog'}) self.assertEqual(self.d['category'], 'cat') class TestOpenResource(unittest.TestCase): "Ensure that `_open_resource()` interprets its arguments as URIs, " \ "file-like objects, or in-memory feeds as expected" def test_fileobj(self): r = feedparser._open_resource(sys.stdin, '', '', '', '', [], {}) self.assertTrue(r is sys.stdin) def test_feed(self): f = feedparser.parse(u'feed://localhost:8097/tests/http/target.xml') self.assertEqual(f.href, u'http://localhost:8097/tests/http/target.xml') def test_feed_http(self): f = feedparser.parse(u'feed:http://localhost:8097/tests/http/target.xml') self.assertEqual(f.href, u'http://localhost:8097/tests/http/target.xml') def test_bytes(self): s = '<feed><item><title>text</title></item></feed>'.encode('utf-8') r = feedparser._open_resource(s, '', '', '', '', [], {}) self.assertEqual(s, r.read()) def test_string(self): s = '<feed><item><title>text</title></item></feed>' r = feedparser._open_resource(s, '', '', '', '', [], {}) self.assertEqual(s.encode('utf-8'), r.read()) def test_unicode_1(self): s = u'<feed><item><title>text</title></item></feed>' r = feedparser._open_resource(s, '', '', '', '', [], {}) self.assertEqual(s.encode('utf-8'), r.read()) def test_unicode_2(self): s = u'<feed><item><title>t\u00e9xt</title></item></feed>' r = feedparser._open_resource(s, '', '', '', '', [], {}) self.assertEqual(s.encode('utf-8'), r.read()) class TestMakeSafeAbsoluteURI(unittest.TestCase): "Exercise the URI joining and sanitization code" base = u'http://d.test/d/f.ext' def _mktest(rel, expect, doc): def fn(self): value = feedparser._makeSafeAbsoluteURI(self.base, rel) self.assertEqual(value, expect) fn.__doc__ = doc return fn # make the test cases; the call signature is: # (relative_url, expected_return_value, test_doc_string) test_abs = _mktest(u'https://s.test/', u'https://s.test/', 'absolute uri') test_rel = _mktest(u'/new', u'http://d.test/new', 'relative uri') test_bad = _mktest(u'x://bad.test/', u'', 'unacceptable uri protocol') test_mag = _mktest(u'magnet:?xt=a', u'magnet:?xt=a', 'magnet uri') def test_catch_ValueError(self): 'catch ValueError in Python 2.7 and up' uri = u'http://bad]test/' value1 = feedparser._makeSafeAbsoluteURI(uri) value2 = feedparser._makeSafeAbsoluteURI(self.base, uri) swap = feedparser.ACCEPTABLE_URI_SCHEMES feedparser.ACCEPTABLE_URI_SCHEMES = () value3 = feedparser._makeSafeAbsoluteURI(self.base, uri) feedparser.ACCEPTABLE_URI_SCHEMES = swap # Only Python 2.7 and up throw a ValueError, otherwise uri is returned self.assertTrue(value1 in (uri, u'')) self.assertTrue(value2 in (uri, u'')) self.assertTrue(value3 in (uri, u'')) class TestConvertToIdn(unittest.TestCase): "Test IDN support (unavailable in Jython as of Jython 2.5.2)" # this is the greek test domain hostname = u'\u03c0\u03b1\u03c1\u03ac\u03b4\u03b5\u03b9\u03b3\u03bc\u03b1' hostname += u'.\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae' def test_control(self): r = feedparser._convert_to_idn(u'http://example.test/') self.assertEqual(r, u'http://example.test/') def test_idn(self): r = feedparser._convert_to_idn(u'http://%s/' % (self.hostname,)) self.assertEqual(r, u'http://xn--hxajbheg2az3al.xn--jxalpdlp/') def test_port(self): r = feedparser._convert_to_idn(u'http://%s:8080/' % (self.hostname,)) self.assertEqual(r, u'http://xn--hxajbheg2az3al.xn--jxalpdlp:8080/') class TestCompression(unittest.TestCase): "Test the gzip and deflate support in the HTTP code" def test_gzip_good(self): f = feedparser.parse('http://localhost:8097/tests/compression/gzip.gz') self.assertEqual(f.version, 'atom10') def test_gzip_not_compressed(self): f = feedparser.parse('http://localhost:8097/tests/compression/gzip-not-compressed.gz') self.assertEqual(f.bozo, 1) self.assertTrue(isinstance(f.bozo_exception, IOError)) self.assertEqual(f['feed']['title'], 'gzip') def test_gzip_struct_error(self): f = feedparser.parse('http://localhost:8097/tests/compression/gzip-struct-error.gz') self.assertEqual(f.bozo, 1) self.assertTrue(isinstance(f.bozo_exception, struct.error)) def test_zlib_good(self): f = feedparser.parse('http://localhost:8097/tests/compression/deflate.z') self.assertEqual(f.version, 'atom10') def test_zlib_no_headers(self): f = feedparser.parse('http://localhost:8097/tests/compression/deflate-no-headers.z') self.assertEqual(f.version, 'atom10') def test_zlib_not_compressed(self): f = feedparser.parse('http://localhost:8097/tests/compression/deflate-not-compressed.z') self.assertEqual(f.bozo, 1) self.assertTrue(isinstance(f.bozo_exception, zlib.error)) self.assertEqual(f['feed']['title'], 'deflate') class TestHTTPStatus(unittest.TestCase): "Test HTTP redirection and other status codes" def test_301(self): f = feedparser.parse('http://localhost:8097/tests/http/http_status_301.xml') self.assertEqual(f.status, 301) self.assertEqual(f.href, 'http://localhost:8097/tests/http/target.xml') self.assertEqual(f.entries[0].title, 'target') def test_302(self): f = feedparser.parse('http://localhost:8097/tests/http/http_status_302.xml') self.assertEqual(f.status, 302) self.assertEqual(f.href, 'http://localhost:8097/tests/http/target.xml') self.assertEqual(f.entries[0].title, 'target') def test_303(self): f = feedparser.parse('http://localhost:8097/tests/http/http_status_303.xml') self.assertEqual(f.status, 303) self.assertEqual(f.href, 'http://localhost:8097/tests/http/target.xml') self.assertEqual(f.entries[0].title, 'target') def test_307(self): f = feedparser.parse('http://localhost:8097/tests/http/http_status_307.xml') self.assertEqual(f.status, 307) self.assertEqual(f.href, 'http://localhost:8097/tests/http/target.xml') self.assertEqual(f.entries[0].title, 'target') def test_304(self): # first retrieve the url u = 'http://localhost:8097/tests/http/http_status_304.xml' f = feedparser.parse(u) self.assertEqual(f.status, 200) self.assertEqual(f.entries[0].title, 'title 304') # extract the etag and last-modified headers e = [v for k, v in f.headers.items() if k.lower() == 'etag'][0] mh = [v for k, v in f.headers.items() if k.lower() == 'last-modified'][0] ms = f.updated mt = f.updated_parsed md = datetime.datetime(*mt[0:7]) self.assertTrue(isinstance(mh, basestring)) self.assertTrue(isinstance(ms, basestring)) self.assertTrue(isinstance(mt, time.struct_time)) self.assertTrue(isinstance(md, datetime.datetime)) # test that sending back the etag results in a 304 f = feedparser.parse(u, etag=e) self.assertEqual(f.status, 304) # test that sending back last-modified (string) results in a 304 f = feedparser.parse(u, modified=ms) self.assertEqual(f.status, 304) # test that sending back last-modified (9-tuple) results in a 304 f = feedparser.parse(u, modified=mt) self.assertEqual(f.status, 304) # test that sending back last-modified (datetime) results in a 304 f = feedparser.parse(u, modified=md) self.assertEqual(f.status, 304) def test_404(self): f = feedparser.parse('http://localhost:8097/tests/http/http_status_404.xml') self.assertEqual(f.status, 404) def test_redirect_to_304(self): # ensure that an http redirect to an http 304 doesn't # trigger a bozo_exception u = 'http://localhost:8097/tests/http/http_redirect_to_304.xml' f = feedparser.parse(u) self.assertTrue(f.bozo == 0) self.assertTrue(f.status == 302) class TestDateParsers(unittest.TestCase): "Test the various date parsers; most of the test cases are constructed " \ "dynamically based on the contents of the `date_tests` dict, below" def test_None(self): self.assertTrue(feedparser._parse_date(None) is None) def _check_date(self, func, dtstring, expected_value): try: parsed_value = func(dtstring) except (OverflowError, ValueError): parsed_value = None self.assertEqual(parsed_value, expected_value) # self.assertEqual(parsed_value, feedparser._parse_date(dtstring)) def test_year_10000_date(self): # On some systems this date string will trigger an OverflowError. # On Jython and x64 systems, however, it's interpreted just fine. try: date = feedparser._parse_date_rfc822(u'Sun, 31 Dec 9999 23:59:59 -9999') except OverflowError: date = None self.assertTrue(date in (None, (10000, 1, 5, 4, 38, 59, 2, 5, 0))) date_tests = { feedparser._parse_date_greek: ( (u'', None), # empty string (u'\u039a\u03c5\u03c1, 11 \u0399\u03bf\u03cd\u03bb 2004 12:00:00 EST', (2004, 7, 11, 17, 0, 0, 6, 193, 0)), ), feedparser._parse_date_hungarian: ( (u'', None), # empty string (u'2004-j\u00falius-13T9:15-05:00', (2004, 7, 13, 14, 15, 0, 1, 195, 0)), ), feedparser._parse_date_iso8601: ( (u'', None), # empty string (u'-0312', (2003, 12, 1, 0, 0, 0, 0, 335, 0)), # 2-digit year/month only variant (u'031231', (2003, 12, 31, 0, 0, 0, 2, 365, 0)), # 2-digit year/month/day only, no hyphens (u'03-12-31', (2003, 12, 31, 0, 0, 0, 2, 365, 0)), # 2-digit year/month/day only (u'-03-12', (2003, 12, 1, 0, 0, 0, 0, 335, 0)), # 2-digit year/month only (u'03335', (2003, 12, 1, 0, 0, 0, 0, 335, 0)), # 2-digit year/ordinal, no hyphens (u'2003-12-31T10:14:55.1234Z', (2003, 12, 31, 10, 14, 55, 2, 365, 0)), # fractional seconds # Special case for Google's extra zero in the month (u'2003-012-31T10:14:55+00:00', (2003, 12, 31, 10, 14, 55, 2, 365, 0)), ), feedparser._parse_date_nate: ( (u'', None), # empty string (u'2004-05-25 \uc624\ud6c4 11:23:17', (2004, 5, 25, 14, 23, 17, 1, 146, 0)), ), feedparser._parse_date_onblog: ( (u'', None), # empty string (u'2004\ub144 05\uc6d4 28\uc77c 01:31:15', (2004, 5, 27, 16, 31, 15, 3, 148, 0)), ), feedparser._parse_date_perforce: ( (u'', None), # empty string (u'Fri, 2006/09/15 08:19:53 EDT', (2006, 9, 15, 12, 19, 53, 4, 258, 0)), ), feedparser._parse_date_rfc822: ( (u'', None), # empty string (u'Thu, 01 Jan 0100 00:00:01 +0100', (99, 12, 31, 23, 0, 1, 3, 365, 0)), # ancient date (u'Thu, 01 Jan 04 19:48:21 GMT', (2004, 1, 1, 19, 48, 21, 3, 1, 0)), # 2-digit year (u'Thu, 01 Jan 2004 19:48:21 GMT', (2004, 1, 1, 19, 48, 21, 3, 1, 0)), # 4-digit year (u'Thu, 5 Apr 2012 10:00:00 GMT', (2012, 4, 5, 10, 0, 0, 3, 96, 0)), # 1-digit day (u'Wed, 19 Aug 2009 18:28:00 Etc/GMT', (2009, 8, 19, 18, 28, 0, 2, 231, 0)), # etc/gmt timezone (u'Wed, 19 Feb 2012 22:40:00 GMT-01:01', (2012, 2, 19, 23, 41, 0, 6, 50, 0)), # gmt+hh:mm timezone (u'Mon, 13 Feb, 2012 06:28:00 UTC', (2012, 2, 13, 6, 28, 0, 0, 44, 0)), # extraneous comma (u'Thu, 01 Jan 2004 00:00 GMT', (2004, 1, 1, 0, 0, 0, 3, 1, 0)), # no seconds (u'Thu, 01 Jan 2004', (2004, 1, 1, 0, 0, 0, 3, 1, 0)), # no time # Additional tests to handle Disney's long month names and invalid timezones (u'Mon, 26 January 2004 16:31:00 AT', (2004, 1, 26, 20, 31, 0, 0, 26, 0)), (u'Mon, 26 January 2004 16:31:00 ET', (2004, 1, 26, 21, 31, 0, 0, 26, 0)), (u'Mon, 26 January 2004 16:31:00 CT', (2004, 1, 26, 22, 31, 0, 0, 26, 0)), (u'Mon, 26 January 2004 16:31:00 MT', (2004, 1, 26, 23, 31, 0, 0, 26, 0)), (u'Mon, 26 January 2004 16:31:00 PT', (2004, 1, 27, 0, 31, 0, 1, 27, 0)), # Swapped month and day (u'Thu Aug 30 2012 17:26:16 +0200', (2012, 8, 30, 15, 26, 16, 3, 243, 0)), (u'Sun, 16 Dec 2012 1:2:3:4 GMT', None), # invalid time (u'Sun, 16 zzz 2012 11:47:32 GMT', None), # invalid month (u'Sun, Dec x 2012 11:47:32 GMT', None), # invalid day (swapped day/month) ('Sun, 16 Dec zz 11:47:32 GMT', None), # invalid year ('Sun, 16 Dec 2012 11:47:32 +zz:00', None), # invalid timezone hour ('Sun, 16 Dec 2012 11:47:32 +00:zz', None), # invalid timezone minute ('Sun, 99 Jun 2009 12:00:00 GMT', None), # out-of-range day ), feedparser._parse_date_asctime: ( (u'Sun Jan 4 16:29:06 2004', (2004, 1, 4, 16, 29, 6, 6, 4, 0)), ), feedparser._parse_date_w3dtf: ( (u'', None), # empty string (u'2003-12-31T10:14:55Z', (2003, 12, 31, 10, 14, 55, 2, 365, 0)), # UTC (u'2003-12-31T10:14:55-08:00', (2003, 12, 31, 18, 14, 55, 2, 365, 0)), # San Francisco timezone (u'2003-12-31T18:14:55+08:00', (2003, 12, 31, 10, 14, 55, 2, 365, 0)), # Tokyo timezone (u'2007-04-23T23:25:47.538+10:00', (2007, 4, 23, 13, 25, 47, 0, 113, 0)), # fractional seconds (u'2003-12-31', (2003, 12, 31, 0, 0, 0, 2, 365, 0)), # year/month/day only (u'2003-12', (2003, 12, 1, 0, 0, 0, 0, 335, 0)), # year/month only (u'2003', (2003, 1, 1, 0, 0, 0, 2, 1, 0)), # year only # Special cases for rollovers in leap years (u'2004-02-28T18:14:55-08:00', (2004, 2, 29, 2, 14, 55, 6, 60, 0)), # feb 28 in leap year (u'2003-02-28T18:14:55-08:00', (2003, 3, 1, 2, 14, 55, 5, 60, 0)), # feb 28 in non-leap year (u'2000-02-28T18:14:55-08:00', (2000, 2, 29, 2, 14, 55, 1, 60, 0)), # feb 28 in leap year on century divisible by 400 # Out-of-range times (u'9999-12-31T23:59:59-99:99', None), # Date is out-of-range (u'2003-12-31T25:14:55Z', None), # invalid (25 hours) (u'2003-12-31T10:61:55Z', None), # invalid (61 minutes) (u'2003-12-31T10:14:61Z', None), # invalid (61 seconds) # Invalid formats (u'22013', None), # Year is too long (u'013', None), # Year is too short (u'2013-01-27-01', None), # Date has to many parts (u'2013-01-28T11:30:00-06:00Textra', None), # Too many 't's # Non-integer values (u'2013-xx-27', None), # Date (u'2013-01-28T09:xx:00Z', None), # Time (u'2013-01-28T09:00:00+00:xx', None), # Timezone # MSSQL-style dates (u'2004-07-08 23:56:58 -00:20', (2004, 7, 9, 0, 16, 58, 4, 191, 0)), # with timezone (u'2004-07-08 23:56:58', (2004, 7, 8, 23, 56, 58, 3, 190, 0)), # without timezone (u'2004-07-08 23:56:58.0', (2004, 7, 8, 23, 56, 58, 3, 190, 0)), # with fractional second ) } def make_date_test(f, s, t): return lambda self: self._check_date(f, s, t) for func, items in date_tests.iteritems(): for i, (dtstring, dttuple) in enumerate(items): uniqfunc = make_date_test(func, dtstring, dttuple) setattr(TestDateParsers, 'test_%s_%02i' % (func.__name__, i), uniqfunc) class TestHTMLGuessing(unittest.TestCase): "Exercise the HTML sniffing code" def _mktest(text, expect, doc): def fn(self): value = bool(feedparser._FeedParserMixin.lookslikehtml(text)) self.assertEqual(value, expect) fn.__doc__ = doc return fn test_text_1 = _mktest(u'plain text', False, u'plain text') test_text_2 = _mktest(u'2 < 3', False, u'plain text with angle bracket') test_html_1 = _mktest(u'<a href="">a</a>', True, u'anchor tag') test_html_2 = _mktest(u'<i>i</i>', True, u'italics tag') test_html_3 = _mktest(u'<b>b</b>', True, u'bold tag') test_html_4 = _mktest(u'<code>', False, u'allowed tag, no end tag') test_html_5 = _mktest(u'<rss> .. </rss>', False, u'disallowed tag') test_entity_1 = _mktest(u'AT&T', False, u'corporation name') test_entity_2 = _mktest(u'&copy;', True, u'named entity reference') test_entity_3 = _mktest(u'&#169;', True, u'numeric entity reference') test_entity_4 = _mktest(u'&#xA9;', True, u'hex numeric entity reference') #---------- additional api unit tests, not backed by files class TestBuildRequest(unittest.TestCase): "Test that HTTP request objects are created as expected" def test_extra_headers(self): """You can pass in extra headers and they go into the request object.""" request = feedparser._build_urllib2_request( 'http://example.com/feed', 'agent-name', None, None, None, None, {'Cache-Control': 'max-age=0'}) # nb, urllib2 folds the case of the headers self.assertEqual( request.get_header('Cache-control'), 'max-age=0') class TestLxmlBug(unittest.TestCase): def test_lxml_etree_bug(self): try: import lxml.etree except ImportError: pass else: doc = u"<feed>&illformed_charref</feed>".encode('utf8') # Importing lxml.etree currently causes libxml2 to # throw SAXException instead of SAXParseException. feedparser.parse(feedparser._StringIO(doc)) self.assertTrue(True) #---------- parse test files and create test methods ---------- def convert_to_utf8(data): "Identify data's encoding using its byte order mark" \ "and convert it to its utf-8 equivalent" if data[:4] == _l2bytes([0x4c, 0x6f, 0xa7, 0x94]): return data.decode('cp037').encode('utf-8') elif data[:4] == _l2bytes([0x00, 0x00, 0xfe, 0xff]): if not _UTF32_AVAILABLE: return None return data.decode('utf-32be').encode('utf-8') elif data[:4] == _l2bytes([0xff, 0xfe, 0x00, 0x00]): if not _UTF32_AVAILABLE: return None return data.decode('utf-32le').encode('utf-8') elif data[:4] == _l2bytes([0x00, 0x00, 0x00, 0x3c]): if not _UTF32_AVAILABLE: return None return data.decode('utf-32be').encode('utf-8') elif data[:4] == _l2bytes([0x3c, 0x00, 0x00, 0x00]): if not _UTF32_AVAILABLE: return None return data.decode('utf-32le').encode('utf-8') elif data[:4] == _l2bytes([0x00, 0x3c, 0x00, 0x3f]): return data.decode('utf-16be').encode('utf-8') elif data[:4] == _l2bytes([0x3c, 0x00, 0x3f, 0x00]): return data.decode('utf-16le').encode('utf-8') elif (data[:2] == _l2bytes([0xfe, 0xff])) and (data[2:4] != _l2bytes([0x00, 0x00])): return data[2:].decode('utf-16be').encode('utf-8') elif (data[:2] == _l2bytes([0xff, 0xfe])) and (data[2:4] != _l2bytes([0x00, 0x00])): return data[2:].decode('utf-16le').encode('utf-8') elif data[:3] == _l2bytes([0xef, 0xbb, 0xbf]): return data[3:] # no byte order mark was found return data skip_re = re.compile(_s2bytes("SkipUnless:\s*(.*?)\n")) desc_re = re.compile(_s2bytes("Description:\s*(.*?)\s*Expect:\s*(.*)\s*-->")) def getDescription(xmlfile, data): """Extract test data Each test case is an XML file which contains not only a test feed but also the description of the test and the condition that we would expect the parser to create when it parses the feed. Example: <!-- Description: feed title Expect: feed['title'] == u'Example feed' --> """ skip_results = skip_re.search(data) if skip_results: skipUnless = skip_results.group(1).strip() else: skipUnless = '1' search_results = desc_re.search(data) if not search_results: raise RuntimeError, "can't parse %s" % xmlfile description, evalString = map(lambda s: s.strip(), list(search_results.groups())) description = xmlfile + ": " + unicode(description, 'utf8') return description, evalString, skipUnless def buildTestCase(xmlfile, description, evalString): func = lambda self, xmlfile=xmlfile, evalString=evalString: \ self.failUnlessEval(xmlfile, evalString) func.__doc__ = description return func def runtests(): "Read the files in the tests/ directory, dynamically add tests to the " \ "TestCases above, spawn the HTTP server, and run the test suite" if sys.argv[1:]: allfiles = filter(lambda s: s.endswith('.xml'), reduce(operator.add, map(glob.glob, sys.argv[1:]), [])) wellformedfiles = illformedfiles = encodingfiles = entitiesfiles = microformatfiles = [] sys.argv = [sys.argv[0]] #+ sys.argv[2:] else: allfiles = glob.glob(os.path.join('.', 'tests', '**', '**', '*.xml')) wellformedfiles = glob.glob(os.path.join('.', 'tests', 'wellformed', '**', '*.xml')) illformedfiles = glob.glob(os.path.join('.', 'tests', 'illformed', '*.xml')) encodingfiles = glob.glob(os.path.join('.', 'tests', 'encoding', '*.xml')) entitiesfiles = glob.glob(os.path.join('.', 'tests', 'entities', '*.xml')) microformatfiles = glob.glob(os.path.join('.', 'tests', 'microformats', '**', '*.xml')) httpd = None # there are several compression test cases that must be accounted for # as well as a number of http status tests that redirect to a target # and a few `_open_resource`-related tests httpcount = 6 + 16 + 2 httpcount += len([f for f in allfiles if 'http' in f]) httpcount += len([f for f in wellformedfiles if 'http' in f]) httpcount += len([f for f in illformedfiles if 'http' in f]) httpcount += len([f for f in encodingfiles if 'http' in f]) try: for c, xmlfile in enumerate(allfiles + encodingfiles + illformedfiles + entitiesfiles): addTo = TestCase if xmlfile in encodingfiles: addTo = TestEncodings elif xmlfile in entitiesfiles: addTo = (TestStrictParser, TestLooseParser) elif xmlfile in microformatfiles: addTo = TestMicroformats elif xmlfile in wellformedfiles: addTo = (TestStrictParser, TestLooseParser) f = open(xmlfile, 'rb') data = f.read() f.close() if 'encoding' in xmlfile: data = convert_to_utf8(data) if data is None: # convert_to_utf8 found a byte order mark for utf_32 # but it's not supported in this installation of Python if 'http' in xmlfile: httpcount -= 1 + (xmlfile in wellformedfiles) continue description, evalString, skipUnless = getDescription(xmlfile, data) testName = 'test_%06d' % c ishttp = 'http' in xmlfile try: if not eval(skipUnless): raise NotImplementedError except (ImportError, LookupError, NotImplementedError, AttributeError): if ishttp: httpcount -= 1 + (xmlfile in wellformedfiles) continue if ishttp: xmlfile = 'http://%s:%s/%s' % (_HOST, _PORT, posixpath.normpath(xmlfile.replace('\\', '/'))) testFunc = buildTestCase(xmlfile, description, evalString) if isinstance(addTo, tuple): setattr(addTo[0], testName, testFunc) setattr(addTo[1], testName, testFunc) else: setattr(addTo, testName, testFunc) if httpcount: httpd = FeedParserTestServer(httpcount) httpd.daemon = True httpd.start() httpd.ready.wait() testsuite = unittest.TestSuite() testloader = unittest.TestLoader() testsuite.addTest(testloader.loadTestsFromTestCase(TestCase)) testsuite.addTest(testloader.loadTestsFromTestCase(TestStrictParser)) testsuite.addTest(testloader.loadTestsFromTestCase(TestLooseParser)) testsuite.addTest(testloader.loadTestsFromTestCase(TestEncodings)) testsuite.addTest(testloader.loadTestsFromTestCase(TestDateParsers)) testsuite.addTest(testloader.loadTestsFromTestCase(TestHTMLGuessing)) testsuite.addTest(testloader.loadTestsFromTestCase(TestHTTPStatus)) testsuite.addTest(testloader.loadTestsFromTestCase(TestCompression)) testsuite.addTest(testloader.loadTestsFromTestCase(TestConvertToIdn)) testsuite.addTest(testloader.loadTestsFromTestCase(TestMicroformats)) testsuite.addTest(testloader.loadTestsFromTestCase(TestOpenResource)) testsuite.addTest(testloader.loadTestsFromTestCase(TestFeedParserDict)) testsuite.addTest(testloader.loadTestsFromTestCase(TestMakeSafeAbsoluteURI)) testsuite.addTest(testloader.loadTestsFromTestCase(TestEverythingIsUnicode)) testsuite.addTest(testloader.loadTestsFromTestCase(TestTemporaryFallbackBehavior)) testsuite.addTest(testloader.loadTestsFromTestCase(TestLxmlBug)) testresults = unittest.TextTestRunner(verbosity=1).run(testsuite) # Return 0 if successful, 1 if there was a failure sys.exit(not testresults.wasSuccessful()) finally: if httpd: if httpd.requests: # Should never get here unless something went horribly wrong, like the # user hitting Ctrl-C. Tell our HTTP server that it's done, then do # one more request to flush it. This rarely works; the combination of # threading, self-terminating HTTP servers, and unittest is really # quite flaky. Just what you want in a testing framework, no? httpd.requests = 0 if httpd.ready: urllib.urlopen('http://127.0.0.1:8097/tests/wellformed/rss/aaa_wellformed.xml').read() httpd.join(0) if __name__ == "__main__": runtests()
gpl-3.0
temasek/android_external_chromium_org_third_party_WebKit
Tools/Scripts/webkitpy/layout_tests/port/server_process_mock.py
61
3134
# Copyright (C) 2012 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. class MockServerProcess(object): def __init__(self, port_obj=None, name=None, cmd=None, env=None, universal_newlines=False, treat_no_data_as_crash=False, logging=False, lines=None, crashed=False): self.timed_out = False self.lines = lines or ['#READY'] self.crashed = crashed self.writes = [] self.cmd = cmd self.env = env self.treat_no_data_as_crash = treat_no_data_as_crash self.logging = logging self.started = False self.stopped = False def write(self, bytes): self.writes.append(bytes) def has_crashed(self): return self.crashed def read_stdout_line(self, deadline): return self.lines.pop(0) + "\n" def read_stdout(self, deadline, size): first_line = self.lines[0] if size > len(first_line): self.lines.pop(0) remaining_size = size - len(first_line) - 1 if not remaining_size: return first_line + "\n" return first_line + "\n" + self.read_stdout(deadline, remaining_size) result = self.lines[0][:size] self.lines[0] = self.lines[0][size:] return result def pop_all_buffered_stderr(self): return '' def read_either_stdout_or_stderr_line(self, deadline): # FIXME: We should have tests which intermix stderr and stdout lines. return self.read_stdout_line(deadline), None def start(self): self.started = True def stop(self, timeout_sec=0.0): self.stopped = True return def kill(self): return
bsd-3-clause
ardi69/pyload-0.4.10
pyload/plugin/hoster/MegaCoNz.py
1
8133
# -*- coding: utf-8 -*- import array import base64 import os # import pycurl import random import re import Crypto from pyload.utils import json_loads, json_dumps from pyload.plugin.Hoster import Hoster from pyload.utils import decode, fs_decode, fs_encode ############################ General errors ################################### # EINTERNAL (-1): An internal error has occurred. Please submit a bug report, detailing the exact circumstances in which this error occurred # EARGS (-2): You have passed invalid arguments to this command # EAGAIN (-3): (always at the request level) A temporary congestion or server malfunction prevented your request from being processed. No data was altered. Retry. Retries must be spaced with exponential backoff # ERATELIMIT (-4): You have exceeded your command weight per time quota. Please wait a few seconds, then try again (this should never happen in sane real-life applications) # ############################ Upload errors #################################### # EFAILED (-5): The upload failed. Please restart it from scratch # ETOOMANY (-6): Too many concurrent IP addresses are accessing this upload target URL # ERANGE (-7): The upload file packet is out of range or not starting and ending on a chunk boundary # EEXPIRED (-8): The upload target URL you are trying to access has expired. Please request a fresh one # ############################ Stream/System errors ############################# # ENOENT (-9): Object (typically, node or user) not found # ECIRCULAR (-10): Circular linkage attempted # EACCESS (-11): Access violation (e.g., trying to write to a read-only share) # EEXIST (-12): Trying to create an object that already exists # EINCOMPLETE (-13): Trying to access an incomplete resource # EKEY (-14): A decryption operation failed (never returned by the API) # ESID (-15): Invalid or expired user session, please relogin # EBLOCKED (-16): User blocked # EOVERQUOTA (-17): Request over quota # ETEMPUNAVAIL (-18): Resource temporarily not available, please try again later # ETOOMANYCONNECTIONS (-19): Too many connections on this resource # EWRITE (-20): Write failed # EREAD (-21): Read failed # EAPPKEY (-22): Invalid application key; request not processed class MegaCoNz(Hoster): __name = "MegaCoNz" __type = "hoster" __version = "0.26" __pattern = r'(?:https?://(?:www\.)?mega\.co\.nz/|mega:|chrome:.+?)#(?P<TYPE>N|)!(?P<ID>[\w^_]+)!(?P<KEY>[\w,-]+)' __description = """Mega.co.nz hoster plugin""" __license = "GPLv3" __authors = [("RaNaN", "[email protected]"), ("Walter Purcaro", "[email protected]")] API_URL = "https://eu.api.mega.co.nz/cs" FILE_SUFFIX = ".crypted" def b64_decode(self, data): data = data.replace("-", "+").replace("_", "/") return base64.standard_b64decode(data + '=' * (-len(data) % 4)) def getCipherKey(self, key): """Construct the cipher key from the given data""" a = array.array("I", self.b64_decode(key)) k = array.array("I", (a[0] ^ a[4], a[1] ^ a[5], a[2] ^ a[6], a[3] ^ a[7])) iv = a[4:6] + array.array("I", (0, 0)) meta_mac = a[6:8] return k, iv, meta_mac def api_response(self, **kwargs): """Dispatch a call to the api, see https://mega.co.nz/#developers""" # generate a session id, no idea where to obtain elsewhere uid = random.random.randint(10 << 9, 10 ** 10) res = self.load(self.API_URL, get={'id': uid}, post=json_dumps([kwargs])) self.logDebug("Api Response: " + res) return json_loads(res) def decryptAttr(self, data, key): k, iv, meta_mac = self.getCipherKey(key) cbc = Crypto.Cipher.AES.new(k, Crypto.Cipher.AES.MODE_CBC, "\0" * 16) attr = decode(cbc.decrypt(self.b64_decode(data))) self.logDebug("Decrypted Attr: %s" % attr) if not attr.startswith("MEGA"): self.fail(_("Decryption failed")) # Data is padded, 0-bytes must be stripped return json_loads(re.search(r'{.+?}', attr).group(0)) def decryptFile(self, key): """Decrypts the file at lastDownload`""" # upper 64 bit of counter start n = self.b64_decode(key)[16:24] # convert counter to long and shift bytes k, iv, meta_mac = self.getCipherKey(key) ctr = Crypto.Util.Counter.new(128, initial_value=long(n.encode("hex"), 16) << 64) cipher = Crypto.Cipher.AES.new(k, Crypto.Cipher.AES.MODE_CTR, counter=ctr) self.pyfile.setStatus("decrypting") self.pyfile.setProgress(0) file_crypted = fs_encode(self.lastDownload) file_decrypted = file_crypted.rsplit(self.FILE_SUFFIX)[0] try: f = open(file_crypted, "rb") df = open(file_decrypted, "wb") except IOError, e: self.fail(e) chunk_size = 2 ** 15 #: buffer size, 32k # file_mac = [0, 0, 0, 0] #: calculate CBC-MAC for checksum chunks = os.path.getsize(file_crypted) / chunk_size + 1 for i in xrange(chunks): buf = f.read(chunk_size) if not buf: break chunk = cipher.decrypt(buf) df.write(chunk) self.pyfile.setProgress(int((100.0 / chunks) * i)) # chunk_mac = [iv[0], iv[1], iv[0], iv[1]] # for i in xrange(0, chunk_size, 16): # block = chunk[i:i+16] # if len(block) % 16: # block += '=' * (16 - (len(block) % 16)) # block = array.array("I", block) # chunk_mac = [chunk_mac[0] ^ a_[0], chunk_mac[1] ^ block[1], chunk_mac[2] ^ block[2], chunk_mac[3] ^ block[3]] # chunk_mac = aes_cbc_encrypt_a32(chunk_mac, k) # file_mac = [file_mac[0] ^ chunk_mac[0], file_mac[1] ^ chunk_mac[1], file_mac[2] ^ chunk_mac[2], file_mac[3] ^ chunk_mac[3]] # file_mac = aes_cbc_encrypt_a32(file_mac, k) self.pyfile.setProgress(100) f.close() df.close() # if file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3] != meta_mac: # os.remove(file_decrypted) # self.fail(_("Checksum mismatch")) os.remove(file_crypted) self.lastDownload = fs_decode(file_decrypted) def checkError(self, code): ecode = abs(code) if ecode in (9, 16, 21): self.offline() elif ecode in (3, 13, 17, 18, 19): self.tempOffline() elif ecode in (1, 4, 6, 10, 15, 21): self.retry(5, 30, _("Error code: [%s]") % -ecode) else: self.fail(_("Error code: [%s]") % -ecode) def process(self, pyfile): pattern = re.match(self.__pattern, pyfile.url).groupdict() id = pattern['ID'] key = pattern['KEY'] public = pattern['TYPE'] == '' self.logDebug("ID: %s" % id, "Key: %s" % key, "Type: %s" % ("public" if public else "node")) # g is for requesting a download url # this is similar to the calls in the mega js app, documentation is very bad if public: mega = self.api_response(a="g", g=1, p=id, ssl=1)[0] else: mega = self.api_response(a="g", g=1, n=id, ssl=1)[0] if isinstance(mega, int): self.checkError(mega) elif "e" in mega: self.checkError(mega['e']) attr = self.decryptAttr(mega['at'], key) pyfile.name = attr['n'] + self.FILE_SUFFIX pyfile.size = mega['s'] # self.req.http.c.setopt(pycurl.SSL_CIPHER_LIST, "RC4-MD5:DEFAULT") self.download(mega['g']) self.decryptFile(key) # Everything is finished and final name can be set pyfile.name = attr['n']
gpl-3.0
krez13/scikit-learn
examples/plot_multilabel.py
236
4157
# Authors: Vlad Niculae, Mathieu Blondel # License: BSD 3 clause """ ========================= Multilabel classification ========================= This example simulates a multi-label document classification problem. The dataset is generated randomly based on the following process: - pick the number of labels: n ~ Poisson(n_labels) - n times, choose a class c: c ~ Multinomial(theta) - pick the document length: k ~ Poisson(length) - k times, choose a word: w ~ Multinomial(theta_c) In the above process, rejection sampling is used to make sure that n is more than 2, and that the document length is never zero. Likewise, we reject classes which have already been chosen. The documents that are assigned to both classes are plotted surrounded by two colored circles. The classification is performed by projecting to the first two principal components found by PCA and CCA for visualisation purposes, followed by using the :class:`sklearn.multiclass.OneVsRestClassifier` metaclassifier using two SVCs with linear kernels to learn a discriminative model for each class. Note that PCA is used to perform an unsupervised dimensionality reduction, while CCA is used to perform a supervised one. Note: in the plot, "unlabeled samples" does not mean that we don't know the labels (as in semi-supervised learning) but that the samples simply do *not* have a label. """ print(__doc__) import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_multilabel_classification from sklearn.multiclass import OneVsRestClassifier from sklearn.svm import SVC from sklearn.preprocessing import LabelBinarizer from sklearn.decomposition import PCA from sklearn.cross_decomposition import CCA def plot_hyperplane(clf, min_x, max_x, linestyle, label): # get the separating hyperplane w = clf.coef_[0] a = -w[0] / w[1] xx = np.linspace(min_x - 5, max_x + 5) # make sure the line is long enough yy = a * xx - (clf.intercept_[0]) / w[1] plt.plot(xx, yy, linestyle, label=label) def plot_subfigure(X, Y, subplot, title, transform): if transform == "pca": X = PCA(n_components=2).fit_transform(X) elif transform == "cca": X = CCA(n_components=2).fit(X, Y).transform(X) else: raise ValueError min_x = np.min(X[:, 0]) max_x = np.max(X[:, 0]) min_y = np.min(X[:, 1]) max_y = np.max(X[:, 1]) classif = OneVsRestClassifier(SVC(kernel='linear')) classif.fit(X, Y) plt.subplot(2, 2, subplot) plt.title(title) zero_class = np.where(Y[:, 0]) one_class = np.where(Y[:, 1]) plt.scatter(X[:, 0], X[:, 1], s=40, c='gray') plt.scatter(X[zero_class, 0], X[zero_class, 1], s=160, edgecolors='b', facecolors='none', linewidths=2, label='Class 1') plt.scatter(X[one_class, 0], X[one_class, 1], s=80, edgecolors='orange', facecolors='none', linewidths=2, label='Class 2') plot_hyperplane(classif.estimators_[0], min_x, max_x, 'k--', 'Boundary\nfor class 1') plot_hyperplane(classif.estimators_[1], min_x, max_x, 'k-.', 'Boundary\nfor class 2') plt.xticks(()) plt.yticks(()) plt.xlim(min_x - .5 * max_x, max_x + .5 * max_x) plt.ylim(min_y - .5 * max_y, max_y + .5 * max_y) if subplot == 2: plt.xlabel('First principal component') plt.ylabel('Second principal component') plt.legend(loc="upper left") plt.figure(figsize=(8, 6)) X, Y = make_multilabel_classification(n_classes=2, n_labels=1, allow_unlabeled=True, random_state=1) plot_subfigure(X, Y, 1, "With unlabeled samples + CCA", "cca") plot_subfigure(X, Y, 2, "With unlabeled samples + PCA", "pca") X, Y = make_multilabel_classification(n_classes=2, n_labels=1, allow_unlabeled=False, random_state=1) plot_subfigure(X, Y, 3, "Without unlabeled samples + CCA", "cca") plot_subfigure(X, Y, 4, "Without unlabeled samples + PCA", "pca") plt.subplots_adjust(.04, .02, .97, .94, .09, .2) plt.show()
bsd-3-clause
rentongzhang/servo
tests/wpt/css-tests/tools/pywebsocket/src/test/test_handshake_hybi00.py
466
17345
#!/usr/bin/env python # # Copyright 2011, 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. """Tests for handshake.hybi00 module.""" import unittest import set_sys_path # Update sys.path to locate mod_pywebsocket module. from mod_pywebsocket.handshake._base import HandshakeException from mod_pywebsocket.handshake.hybi00 import Handshaker from mod_pywebsocket.handshake.hybi00 import _validate_subprotocol from test import mock _TEST_KEY1 = '4 @1 46546xW%0l 1 5' _TEST_KEY2 = '12998 5 Y3 1 .P00' _TEST_KEY3 = '^n:ds[4U' _TEST_CHALLENGE_RESPONSE = '8jKS\'y:G*Co,Wxa-' _GOOD_REQUEST = ( 80, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'sample', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3) _GOOD_REQUEST_CAPITALIZED_HEADER_VALUES = ( 80, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'UPGRADE', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'sample', 'Upgrade': 'WEBSOCKET', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3) _GOOD_REQUEST_CASE_MIXED_HEADER_NAMES = ( 80, 'GET', '/demo', { 'hOsT': 'example.com', 'cOnNeCtIoN': 'Upgrade', 'sEc-wEbsOcKeT-kEy2': _TEST_KEY2, 'sEc-wEbsOcKeT-pRoToCoL': 'sample', 'uPgRaDe': 'WebSocket', 'sEc-wEbsOcKeT-kEy1': _TEST_KEY1, 'oRiGiN': 'http://example.com', }, _TEST_KEY3) _GOOD_RESPONSE_DEFAULT_PORT = ( 'HTTP/1.1 101 WebSocket Protocol Handshake\r\n' 'Upgrade: WebSocket\r\n' 'Connection: Upgrade\r\n' 'Sec-WebSocket-Location: ws://example.com/demo\r\n' 'Sec-WebSocket-Origin: http://example.com\r\n' 'Sec-WebSocket-Protocol: sample\r\n' '\r\n' + _TEST_CHALLENGE_RESPONSE) _GOOD_RESPONSE_SECURE = ( 'HTTP/1.1 101 WebSocket Protocol Handshake\r\n' 'Upgrade: WebSocket\r\n' 'Connection: Upgrade\r\n' 'Sec-WebSocket-Location: wss://example.com/demo\r\n' 'Sec-WebSocket-Origin: http://example.com\r\n' 'Sec-WebSocket-Protocol: sample\r\n' '\r\n' + _TEST_CHALLENGE_RESPONSE) _GOOD_REQUEST_NONDEFAULT_PORT = ( 8081, 'GET', '/demo', { 'Host': 'example.com:8081', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'sample', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3) _GOOD_RESPONSE_NONDEFAULT_PORT = ( 'HTTP/1.1 101 WebSocket Protocol Handshake\r\n' 'Upgrade: WebSocket\r\n' 'Connection: Upgrade\r\n' 'Sec-WebSocket-Location: ws://example.com:8081/demo\r\n' 'Sec-WebSocket-Origin: http://example.com\r\n' 'Sec-WebSocket-Protocol: sample\r\n' '\r\n' + _TEST_CHALLENGE_RESPONSE) _GOOD_RESPONSE_SECURE_NONDEF = ( 'HTTP/1.1 101 WebSocket Protocol Handshake\r\n' 'Upgrade: WebSocket\r\n' 'Connection: Upgrade\r\n' 'Sec-WebSocket-Location: wss://example.com:8081/demo\r\n' 'Sec-WebSocket-Origin: http://example.com\r\n' 'Sec-WebSocket-Protocol: sample\r\n' '\r\n' + _TEST_CHALLENGE_RESPONSE) _GOOD_REQUEST_NO_PROTOCOL = ( 80, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3) _GOOD_RESPONSE_NO_PROTOCOL = ( 'HTTP/1.1 101 WebSocket Protocol Handshake\r\n' 'Upgrade: WebSocket\r\n' 'Connection: Upgrade\r\n' 'Sec-WebSocket-Location: ws://example.com/demo\r\n' 'Sec-WebSocket-Origin: http://example.com\r\n' '\r\n' + _TEST_CHALLENGE_RESPONSE) _GOOD_REQUEST_WITH_OPTIONAL_HEADERS = ( 80, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'EmptyValue': '', 'Sec-WebSocket-Protocol': 'sample', 'AKey': 'AValue', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3) # TODO(tyoshino): Include \r \n in key3, challenge response. _GOOD_REQUEST_WITH_NONPRINTABLE_KEY = ( 80, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': 'y R2 48 Q1O4 e|BV3 i5 1 u- 65', 'Sec-WebSocket-Protocol': 'sample', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': '36 7 74 i 92 2\'m 9 0G', 'Origin': 'http://example.com', }, ''.join(map(chr, [0x01, 0xd1, 0xdd, 0x3b, 0xd1, 0x56, 0x63, 0xff]))) _GOOD_RESPONSE_WITH_NONPRINTABLE_KEY = ( 'HTTP/1.1 101 WebSocket Protocol Handshake\r\n' 'Upgrade: WebSocket\r\n' 'Connection: Upgrade\r\n' 'Sec-WebSocket-Location: ws://example.com/demo\r\n' 'Sec-WebSocket-Origin: http://example.com\r\n' 'Sec-WebSocket-Protocol: sample\r\n' '\r\n' + ''.join(map(chr, [0x0b, 0x99, 0xfa, 0x55, 0xbd, 0x01, 0x23, 0x7b, 0x45, 0xa2, 0xf1, 0xd0, 0x87, 0x8a, 0xee, 0xeb]))) _GOOD_REQUEST_WITH_QUERY_PART = ( 80, 'GET', '/demo?e=mc2', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'sample', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3) _GOOD_RESPONSE_WITH_QUERY_PART = ( 'HTTP/1.1 101 WebSocket Protocol Handshake\r\n' 'Upgrade: WebSocket\r\n' 'Connection: Upgrade\r\n' 'Sec-WebSocket-Location: ws://example.com/demo?e=mc2\r\n' 'Sec-WebSocket-Origin: http://example.com\r\n' 'Sec-WebSocket-Protocol: sample\r\n' '\r\n' + _TEST_CHALLENGE_RESPONSE) _BAD_REQUESTS = ( ( # HTTP request 80, 'GET', '/demo', { 'Host': 'www.google.com', 'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5;' ' en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3' ' GTB6 GTBA', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,' '*/*;q=0.8', 'Accept-Language': 'en-us,en;q=0.5', 'Accept-Encoding': 'gzip,deflate', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'Keep-Alive': '300', 'Connection': 'keep-alive', }), ( # Wrong method 80, 'POST', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'sample', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3), ( # Missing Upgrade 80, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'sample', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3), ( # Wrong Upgrade 80, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'sample', 'Upgrade': 'NonWebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3), ( # Empty WebSocket-Protocol 80, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': '', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3), ( # Wrong port number format 80, 'GET', '/demo', { 'Host': 'example.com:0x50', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'sample', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3), ( # Header/connection port mismatch 8080, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'sample', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3), ( # Illegal WebSocket-Protocol 80, 'GET', '/demo', { 'Host': 'example.com', 'Connection': 'Upgrade', 'Sec-WebSocket-Key2': _TEST_KEY2, 'Sec-WebSocket-Protocol': 'illegal\x09protocol', 'Upgrade': 'WebSocket', 'Sec-WebSocket-Key1': _TEST_KEY1, 'Origin': 'http://example.com', }, _TEST_KEY3), ) def _create_request(request_def): data = '' if len(request_def) > 4: data = request_def[4] conn = mock.MockConn(data) conn.local_addr = ('0.0.0.0', request_def[0]) return mock.MockRequest( method=request_def[1], uri=request_def[2], headers_in=request_def[3], connection=conn) def _create_get_memorized_lines(lines): """Creates a function that returns the given string.""" def get_memorized_lines(): return lines return get_memorized_lines def _create_requests_with_lines(request_lines_set): requests = [] for lines in request_lines_set: request = _create_request(_GOOD_REQUEST) request.connection.get_memorized_lines = _create_get_memorized_lines( lines) requests.append(request) return requests class HyBi00HandshakerTest(unittest.TestCase): def test_good_request_default_port(self): request = _create_request(_GOOD_REQUEST) handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual(_GOOD_RESPONSE_DEFAULT_PORT, request.connection.written_data()) self.assertEqual('/demo', request.ws_resource) self.assertEqual('http://example.com', request.ws_origin) self.assertEqual('ws://example.com/demo', request.ws_location) self.assertEqual('sample', request.ws_protocol) def test_good_request_capitalized_header_values(self): request = _create_request(_GOOD_REQUEST_CAPITALIZED_HEADER_VALUES) handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual(_GOOD_RESPONSE_DEFAULT_PORT, request.connection.written_data()) def test_good_request_case_mixed_header_names(self): request = _create_request(_GOOD_REQUEST_CASE_MIXED_HEADER_NAMES) handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual(_GOOD_RESPONSE_DEFAULT_PORT, request.connection.written_data()) def test_good_request_secure_default_port(self): request = _create_request(_GOOD_REQUEST) request.connection.local_addr = ('0.0.0.0', 443) request.is_https_ = True handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual(_GOOD_RESPONSE_SECURE, request.connection.written_data()) self.assertEqual('sample', request.ws_protocol) def test_good_request_nondefault_port(self): request = _create_request(_GOOD_REQUEST_NONDEFAULT_PORT) handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual(_GOOD_RESPONSE_NONDEFAULT_PORT, request.connection.written_data()) self.assertEqual('sample', request.ws_protocol) def test_good_request_secure_non_default_port(self): request = _create_request(_GOOD_REQUEST_NONDEFAULT_PORT) request.is_https_ = True handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual(_GOOD_RESPONSE_SECURE_NONDEF, request.connection.written_data()) self.assertEqual('sample', request.ws_protocol) def test_good_request_default_no_protocol(self): request = _create_request(_GOOD_REQUEST_NO_PROTOCOL) handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual(_GOOD_RESPONSE_NO_PROTOCOL, request.connection.written_data()) self.assertEqual(None, request.ws_protocol) def test_good_request_optional_headers(self): request = _create_request(_GOOD_REQUEST_WITH_OPTIONAL_HEADERS) handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual('AValue', request.headers_in['AKey']) self.assertEqual('', request.headers_in['EmptyValue']) def test_good_request_with_nonprintable_key(self): request = _create_request(_GOOD_REQUEST_WITH_NONPRINTABLE_KEY) handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual(_GOOD_RESPONSE_WITH_NONPRINTABLE_KEY, request.connection.written_data()) self.assertEqual('sample', request.ws_protocol) def test_good_request_with_query_part(self): request = _create_request(_GOOD_REQUEST_WITH_QUERY_PART) handshaker = Handshaker(request, mock.MockDispatcher()) handshaker.do_handshake() self.assertEqual(_GOOD_RESPONSE_WITH_QUERY_PART, request.connection.written_data()) self.assertEqual('ws://example.com/demo?e=mc2', request.ws_location) def test_bad_requests(self): for request in map(_create_request, _BAD_REQUESTS): handshaker = Handshaker(request, mock.MockDispatcher()) self.assertRaises(HandshakeException, handshaker.do_handshake) class HyBi00ValidateSubprotocolTest(unittest.TestCase): def test_validate_subprotocol(self): # should succeed. _validate_subprotocol('sample') _validate_subprotocol('Sample') _validate_subprotocol('sample\x7eprotocol') _validate_subprotocol('sample\x20protocol') # should fail. self.assertRaises(HandshakeException, _validate_subprotocol, '') self.assertRaises(HandshakeException, _validate_subprotocol, 'sample\x19protocol') self.assertRaises(HandshakeException, _validate_subprotocol, 'sample\x7fprotocol') self.assertRaises(HandshakeException, _validate_subprotocol, # "Japan" in Japanese u'\u65e5\u672c') if __name__ == '__main__': unittest.main() # vi:sts=4 sw=4 et
mpl-2.0
mollstam/UnrealPy
UnrealPyEmbed/Development/Python/2015.08.07-Python2710-x64-Source-vs2015/Python27/Source/django-1.8.2/tests/gis_tests/inspectapp/tests.py
22
6691
from __future__ import unicode_literals import os import re from unittest import skipUnless from django.contrib.gis.gdal import HAS_GDAL from django.core.management import call_command from django.db import connection, connections from django.test import TestCase, skipUnlessDBFeature from django.utils.six import StringIO from ..test_data import TEST_DATA if HAS_GDAL: from django.contrib.gis.gdal import Driver, GDALException from django.contrib.gis.utils.ogrinspect import ogrinspect from .models import AllOGRFields @skipUnless(HAS_GDAL, "InspectDbTests needs GDAL support") @skipUnlessDBFeature("gis_enabled") class InspectDbTests(TestCase): def test_geom_columns(self): """ Test the geo-enabled inspectdb command. """ out = StringIO() call_command('inspectdb', table_name_filter=lambda tn: tn.startswith('inspectapp_'), stdout=out) output = out.getvalue() if connection.features.supports_geometry_field_introspection: self.assertIn('geom = models.PolygonField()', output) self.assertIn('point = models.PointField()', output) else: self.assertIn('geom = models.GeometryField(', output) self.assertIn('point = models.GeometryField(', output) self.assertIn('objects = models.GeoManager()', output) @skipUnless(HAS_GDAL, "OGRInspectTest needs GDAL support") @skipUnlessDBFeature("gis_enabled") class OGRInspectTest(TestCase): maxDiff = 1024 def test_poly(self): shp_file = os.path.join(TEST_DATA, 'test_poly', 'test_poly.shp') model_def = ogrinspect(shp_file, 'MyModel') expected = [ '# This is an auto-generated Django model module created by ogrinspect.', 'from django.contrib.gis.db import models', '', 'class MyModel(models.Model):', ' float = models.FloatField()', ' int = models.FloatField()', ' str = models.CharField(max_length=80)', ' geom = models.PolygonField(srid=-1)', ' objects = models.GeoManager()', ] self.assertEqual(model_def, '\n'.join(expected)) def test_date_field(self): shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp') model_def = ogrinspect(shp_file, 'City') expected = [ '# This is an auto-generated Django model module created by ogrinspect.', 'from django.contrib.gis.db import models', '', 'class City(models.Model):', ' name = models.CharField(max_length=80)', ' population = models.FloatField()', ' density = models.FloatField()', ' created = models.DateField()', ' geom = models.PointField(srid=-1)', ' objects = models.GeoManager()', ] self.assertEqual(model_def, '\n'.join(expected)) def test_time_field(self): # Getting the database identifier used by OGR, if None returned # GDAL does not have the support compiled in. ogr_db = get_ogr_db_string() if not ogr_db: self.skipTest("Unable to setup an OGR connection to your database") try: # Writing shapefiles via GDAL currently does not support writing OGRTime # fields, so we need to actually use a database model_def = ogrinspect(ogr_db, 'Measurement', layer_key=AllOGRFields._meta.db_table, decimal=['f_decimal']) except GDALException: self.skipTest("Unable to setup an OGR connection to your database") self.assertTrue(model_def.startswith( '# This is an auto-generated Django model module created by ogrinspect.\n' 'from django.contrib.gis.db import models\n' '\n' 'class Measurement(models.Model):\n' )) # The ordering of model fields might vary depending on several factors (version of GDAL, etc.) self.assertIn(' f_decimal = models.DecimalField(max_digits=0, decimal_places=0)', model_def) self.assertIn(' f_int = models.IntegerField()', model_def) self.assertIn(' f_datetime = models.DateTimeField()', model_def) self.assertIn(' f_time = models.TimeField()', model_def) self.assertIn(' f_float = models.FloatField()', model_def) self.assertIn(' f_char = models.CharField(max_length=10)', model_def) self.assertIn(' f_date = models.DateField()', model_def) self.assertIsNotNone(re.search( r' geom = models.PolygonField\(([^\)])*\)\n' # Some backends may have srid=-1 r' objects = models.GeoManager\(\)', model_def)) def test_management_command(self): shp_file = os.path.join(TEST_DATA, 'cities', 'cities.shp') out = StringIO() call_command('ogrinspect', shp_file, 'City', stdout=out) output = out.getvalue() self.assertIn('class City(models.Model):', output) def get_ogr_db_string(): """ Construct the DB string that GDAL will use to inspect the database. GDAL will create its own connection to the database, so we re-use the connection settings from the Django test. """ db = connections.databases['default'] # Map from the django backend into the OGR driver name and database identifier # http://www.gdal.org/ogr/ogr_formats.html # # TODO: Support Oracle (OCI). drivers = { 'django.contrib.gis.db.backends.postgis': ('PostgreSQL', "PG:dbname='%(db_name)s'", ' '), 'django.contrib.gis.db.backends.mysql': ('MySQL', 'MYSQL:"%(db_name)s"', ','), 'django.contrib.gis.db.backends.spatialite': ('SQLite', '%(db_name)s', '') } db_engine = db['ENGINE'] if db_engine not in drivers: return None drv_name, db_str, param_sep = drivers[db_engine] # Ensure that GDAL library has driver support for the database. try: Driver(drv_name) except: return None # SQLite/Spatialite in-memory databases if db['NAME'] == ":memory:": return None # Build the params of the OGR database connection string params = [db_str % {'db_name': db['NAME']}] def add(key, template): value = db.get(key, None) # Don't add the parameter if it is not in django's settings if value: params.append(template % value) add('HOST', "host='%s'") add('PORT', "port='%s'") add('USER', "user='%s'") add('PASSWORD', "password='%s'") return param_sep.join(params)
mit
revolutionaryG/phantomjs
src/qt/qtbase/src/3rdparty/freetype/src/tools/docmaker/sources.py
367
10766
# Sources (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009 # David Turner <[email protected]> # # # this file contains definitions of classes needed to decompose # C sources files into a series of multi-line "blocks". There are # two kinds of blocks: # # - normal blocks, which contain source code or ordinary comments # # - documentation blocks, which have restricted formatting, and # whose text always start with a documentation markup tag like # "<Function>", "<Type>", etc.. # # the routines used to process the content of documentation blocks # are not contained here, but in "content.py" # # the classes and methods found here only deal with text parsing # and basic documentation block extraction # import fileinput, re, sys, os, string ################################################################ ## ## BLOCK FORMAT PATTERN ## ## A simple class containing compiled regular expressions used ## to detect potential documentation format block comments within ## C source code ## ## note that the 'column' pattern must contain a group that will ## be used to "unbox" the content of documentation comment blocks ## class SourceBlockFormat: def __init__( self, id, start, column, end ): """create a block pattern, used to recognize special documentation blocks""" self.id = id self.start = re.compile( start, re.VERBOSE ) self.column = re.compile( column, re.VERBOSE ) self.end = re.compile( end, re.VERBOSE ) # # format 1 documentation comment blocks look like the following: # # /************************************/ # /* */ # /* */ # /* */ # /************************************/ # # we define a few regular expressions here to detect them # start = r''' \s* # any number of whitespace /\*{2,}/ # followed by '/' and at least two asterisks then '/' \s*$ # probably followed by whitespace ''' column = r''' \s* # any number of whitespace /\*{1} # followed by '/' and precisely one asterisk ([^*].*) # followed by anything (group 1) \*{1}/ # followed by one asterisk and a '/' \s*$ # probably followed by whitespace ''' re_source_block_format1 = SourceBlockFormat( 1, start, column, start ) # # format 2 documentation comment blocks look like the following: # # /************************************ (at least 2 asterisks) # * # * # * # * # **/ (1 or more asterisks at the end) # # we define a few regular expressions here to detect them # start = r''' \s* # any number of whitespace /\*{2,} # followed by '/' and at least two asterisks \s*$ # probably followed by whitespace ''' column = r''' \s* # any number of whitespace \*{1}(?!/) # followed by precisely one asterisk not followed by `/' (.*) # then anything (group1) ''' end = r''' \s* # any number of whitespace \*+/ # followed by at least one asterisk, then '/' ''' re_source_block_format2 = SourceBlockFormat( 2, start, column, end ) # # the list of supported documentation block formats, we could add new ones # relatively easily # re_source_block_formats = [re_source_block_format1, re_source_block_format2] # # the following regular expressions corresponds to markup tags # within the documentation comment blocks. they're equivalent # despite their different syntax # # notice how each markup tag _must_ begin a new line # re_markup_tag1 = re.compile( r'''\s*<(\w*)>''' ) # <xxxx> format re_markup_tag2 = re.compile( r'''\s*@(\w*):''' ) # @xxxx: format # # the list of supported markup tags, we could add new ones relatively # easily # re_markup_tags = [re_markup_tag1, re_markup_tag2] # # used to detect a cross-reference, after markup tags have been stripped # re_crossref = re.compile( r'@(\w*)(.*)' ) # # used to detect italic and bold styles in paragraph text # re_italic = re.compile( r"_(\w(\w|')*)_(.*)" ) # _italic_ re_bold = re.compile( r"\*(\w(\w|')*)\*(.*)" ) # *bold* # # used to detect the end of commented source lines # re_source_sep = re.compile( r'\s*/\*\s*\*/' ) # # used to perform cross-reference within source output # re_source_crossref = re.compile( r'(\W*)(\w*)' ) # # a list of reserved source keywords # re_source_keywords = re.compile( '''\\b ( typedef | struct | enum | union | const | char | int | short | long | void | signed | unsigned | \#include | \#define | \#undef | \#if | \#ifdef | \#ifndef | \#else | \#endif ) \\b''', re.VERBOSE ) ################################################################ ## ## SOURCE BLOCK CLASS ## ## A SourceProcessor is in charge of reading a C source file ## and decomposing it into a series of different "SourceBlocks". ## each one of these blocks can be made of the following data: ## ## - A documentation comment block that starts with "/**" and ## whose exact format will be discussed later ## ## - normal sources lines, including comments ## ## the important fields in a text block are the following ones: ## ## self.lines : a list of text lines for the corresponding block ## ## self.content : for documentation comment blocks only, this is the ## block content that has been "unboxed" from its ## decoration. This is None for all other blocks ## (i.e. sources or ordinary comments with no starting ## markup tag) ## class SourceBlock: def __init__( self, processor, filename, lineno, lines ): self.processor = processor self.filename = filename self.lineno = lineno self.lines = lines[:] self.format = processor.format self.content = [] if self.format == None: return words = [] # extract comment lines lines = [] for line0 in self.lines: m = self.format.column.match( line0 ) if m: lines.append( m.group( 1 ) ) # now, look for a markup tag for l in lines: l = string.strip( l ) if len( l ) > 0: for tag in re_markup_tags: if tag.match( l ): self.content = lines return def location( self ): return "(" + self.filename + ":" + repr( self.lineno ) + ")" # debugging only - not used in normal operations def dump( self ): if self.content: print "{{{content start---" for l in self.content: print l print "---content end}}}" return fmt = "" if self.format: fmt = repr( self.format.id ) + " " for line in self.lines: print line ################################################################ ## ## SOURCE PROCESSOR CLASS ## ## The SourceProcessor is in charge of reading a C source file ## and decomposing it into a series of different "SourceBlock" ## objects. ## ## each one of these blocks can be made of the following data: ## ## - A documentation comment block that starts with "/**" and ## whose exact format will be discussed later ## ## - normal sources lines, include comments ## ## class SourceProcessor: def __init__( self ): """initialize a source processor""" self.blocks = [] self.filename = None self.format = None self.lines = [] def reset( self ): """reset a block processor, clean all its blocks""" self.blocks = [] self.format = None def parse_file( self, filename ): """parse a C source file, and add its blocks to the processor's list""" self.reset() self.filename = filename fileinput.close() self.format = None self.lineno = 0 self.lines = [] for line in fileinput.input( filename ): # strip trailing newlines, important on Windows machines! if line[-1] == '\012': line = line[0:-1] if self.format == None: self.process_normal_line( line ) else: if self.format.end.match( line ): # that's a normal block end, add it to 'lines' and # create a new block self.lines.append( line ) self.add_block_lines() elif self.format.column.match( line ): # that's a normal column line, add it to 'lines' self.lines.append( line ) else: # humm.. this is an unexpected block end, # create a new block, but don't process the line self.add_block_lines() # we need to process the line again self.process_normal_line( line ) # record the last lines self.add_block_lines() def process_normal_line( self, line ): """process a normal line and check whether it is the start of a new block""" for f in re_source_block_formats: if f.start.match( line ): self.add_block_lines() self.format = f self.lineno = fileinput.filelineno() self.lines.append( line ) def add_block_lines( self ): """add the current accumulated lines and create a new block""" if self.lines != []: block = SourceBlock( self, self.filename, self.lineno, self.lines ) self.blocks.append( block ) self.format = None self.lines = [] # debugging only, not used in normal operations def dump( self ): """print all blocks in a processor""" for b in self.blocks: b.dump() # eof
bsd-3-clause
tempbottle/Firefly
firefly/utils/services.py
8
4997
#coding:utf8 ''' Created on 2011-1-3 服务类 @author: sean_lan ''' import threading from twisted.internet import defer,threads from twisted.python import log class Service(object): """A remoting service attributes: ============ * name - string, service name. * runstyle """ SINGLE_STYLE = 1 PARALLEL_STYLE = 2 def __init__(self, name,runstyle = SINGLE_STYLE): self._name = name self._runstyle = runstyle self.unDisplay = set() self._lock = threading.RLock() self._targets = {} # Keeps track of targets internally def __iter__(self): return self._targets.itervalues() def addUnDisplayTarget(self,command): '''Add a target unDisplay when client call it.''' self.unDisplay.add(command) def mapTarget(self, target): """Add a target to the service.""" self._lock.acquire() try: key = target.__name__ if self._targets.has_key(key): exist_target = self._targets.get(key) raise "target [%d] Already exists,\ Conflict between the %s and %s"%(key,exist_target.__name__,target.__name__) self._targets[key] = target finally: self._lock.release() def unMapTarget(self, target): """Remove a target from the service.""" self._lock.acquire() try: key = target.__name__ if key in self._targets: del self._targets[key] finally: self._lock.release() def unMapTargetByKey(self,targetKey): """Remove a target from the service.""" self._lock.acquire() try: del self._targets[targetKey] finally: self._lock.release() def getTarget(self, targetKey): """Get a target from the service by name.""" self._lock.acquire() try: target = self._targets.get(targetKey, None) finally: self._lock.release() return target def callTarget(self,targetKey,*args,**kw): '''call Target @param conn: client connection @param targetKey: target ID @param data: client data ''' if self._runstyle == self.SINGLE_STYLE: result = self.callTargetSingle(targetKey,*args,**kw) else: result = self.callTargetParallel(targetKey,*args,**kw) return result def callTargetSingle(self,targetKey,*args,**kw): '''call Target by Single @param conn: client connection @param targetKey: target ID @param data: client data ''' target = self.getTarget(targetKey) self._lock.acquire() try: if not target: log.err('the command '+str(targetKey)+' not Found on service') return None if targetKey not in self.unDisplay: log.msg("call method %s on service[single]"%target.__name__) defer_data = target(*args,**kw) if not defer_data: return None if isinstance(defer_data,defer.Deferred): return defer_data d = defer.Deferred() d.callback(defer_data) finally: self._lock.release() return d def callTargetParallel(self,targetKey,*args,**kw): '''call Target by Single @param conn: client connection @param targetKey: target ID @param data: client data ''' self._lock.acquire() try: target = self.getTarget(targetKey) if not target: log.err('the command '+str(targetKey)+' not Found on service') return None log.msg("call method %s on service[parallel]"%target.__name__) d = threads.deferToThread(target,*args,**kw) finally: self._lock.release() return d class CommandService(Service): """A remoting service According to Command ID search target """ def mapTarget(self, target): """Add a target to the service.""" self._lock.acquire() try: key = int((target.__name__).split('_')[-1]) if self._targets.has_key(key): exist_target = self._targets.get(key) raise "target [%d] Already exists,\ Conflict between the %s and %s"%(key,exist_target.__name__,target.__name__) self._targets[key] = target finally: self._lock.release() def unMapTarget(self, target): """Remove a target from the service.""" self._lock.acquire() try: key = int((target.__name__).split('_')[-1]) if key in self._targets: del self._targets[key] finally: self._lock.release()
mit
cl4u2/chirp
chirp/chirp_common.py
1
45611
# Copyright 2008 Dan Smith <[email protected]> # # 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/>. SEPCHAR = "," #print "Using separation character of '%s'" % SEPCHAR import math from chirp import errors, memmap # 50 Tones TONES = [ 67.0, 69.3, 71.9, 74.4, 77.0, 79.7, 82.5, 85.4, 88.5, 91.5, 94.8, 97.4, 100.0, 103.5, 107.2, 110.9, 114.8, 118.8, 123.0, 127.3, 131.8, 136.5, 141.3, 146.2, 151.4, 156.7, 159.8, 162.2, 165.5, 167.9, 171.3, 173.8, 177.3, 179.9, 183.5, 186.2, 189.9, 192.8, 196.6, 199.5, 203.5, 206.5, 210.7, 218.1, 225.7, 229.1, 233.6, 241.8, 250.3, 254.1, ] TONES_EXTRA = [62.5] OLD_TONES = list(TONES) [OLD_TONES.remove(x) for x in [159.8, 165.5, 171.3, 177.3, 183.5, 189.9, 196.6, 199.5, 206.5, 229.1, 254.1]] # 104 DTCS Codes DTCS_CODES = [ 23, 25, 26, 31, 32, 36, 43, 47, 51, 53, 54, 65, 71, 72, 73, 74, 114, 115, 116, 122, 125, 131, 132, 134, 143, 145, 152, 155, 156, 162, 165, 172, 174, 205, 212, 223, 225, 226, 243, 244, 245, 246, 251, 252, 255, 261, 263, 265, 266, 271, 274, 306, 311, 315, 325, 331, 332, 343, 346, 351, 356, 364, 365, 371, 411, 412, 413, 423, 431, 432, 445, 446, 452, 454, 455, 462, 464, 465, 466, 503, 506, 516, 523, 526, 532, 546, 565, 606, 612, 624, 627, 631, 632, 654, 662, 664, 703, 712, 723, 731, 732, 734, 743, 754, ] # 512 Possible DTCS Codes ALL_DTCS_CODES = [] for a in range(0, 8): for b in range(0, 8): for c in range(0, 8): ALL_DTCS_CODES.append((a * 100) + (b * 10) + c) CROSS_MODES = [ "Tone->Tone", "DTCS->", "->DTCS", "Tone->DTCS", "DTCS->Tone", "->Tone", "DTCS->DTCS", ] MODES = ["WFM", "FM", "NFM", "AM", "NAM", "DV", "USB", "LSB", "CW", "RTTY", "DIG", "PKT", "NCW", "NCWR", "CWR", "P25", "Auto"] TONE_MODES = [ "", "Tone", "TSQL", "DTCS", "DTCS-R", "TSQL-R", "Cross", ] TUNING_STEPS = [ 5.0, 6.25, 10.0, 12.5, 15.0, 20.0, 25.0, 30.0, 50.0, 100.0, 125.0, 200.0, # Need to fix drivers using this list as an index! 9.0, 1.0, 2.5, ] SKIP_VALUES = [ "", "S", "P" ] CHARSET_UPPER_NUMERIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890" CHARSET_ALPHANUMERIC = \ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890" CHARSET_ASCII = "".join([chr(x) for x in range(ord(" "), ord("~")+1)]) # http://aprs.org/aprs11/SSIDs.txt APRS_SSID = ( "0 Your primary station usually fixed and message capable", "1 generic additional station, digi, mobile, wx, etc", "2 generic additional station, digi, mobile, wx, etc", "3 generic additional station, digi, mobile, wx, etc", "4 generic additional station, digi, mobile, wx, etc", "5 Other networks (Dstar, Iphones, Androids, Blackberry's etc)", "6 Special activity, Satellite ops, camping or 6 meters, etc", "7 walkie talkies, HT's or other human portable", "8 boats, sailboats, RV's or second main mobile", "9 Primary Mobile (usually message capable)", "10 internet, Igates, echolink, winlink, AVRS, APRN, etc", "11 balloons, aircraft, spacecraft, etc", "12 APRStt, DTMF, RFID, devices, one-way trackers*, etc", "13 Weather stations", "14 Truckers or generally full time drivers", "15 generic additional station, digi, mobile, wx, etc") APRS_POSITION_COMMENT = ( "off duty", "en route", "in service", "returning", "committed", "special", "priority", "custom 0", "custom 1", "custom 2", "custom 3", "custom 4", "custom 5", "custom 6", "EMERGENCY") # http://aprs.org/symbols/symbolsX.txt APRS_SYMBOLS = ( "Police/Sheriff", "[reserved]", "Digi", "Phone", "DX Cluster", "HF Gateway", "Small Aircraft", "Mobile Satellite Groundstation", "Wheelchair", "Snowmobile", "Red Cross", "Boy Scouts", "House QTH (VHF)", "X", "Red Dot", "0 in Circle", "1 in Circle", "2 in Circle", "3 in Circle", "4 in Circle", "5 in Circle", "6 in Circle", "7 in Circle", "8 in Circle", "9 in Circle", "Fire", "Campground", "Motorcycle", "Railroad Engine", "Car", "File Server", "Hurricane Future Prediction", "Aid Station", "BBS or PBBS", "Canoe", "[reserved]", "Eyeball", "Tractor/Farm Vehicle", "Grid Square", "Hotel", "TCP/IP", "[reserved]", "School", "PC User", "MacAPRS", "NTS Station", "Balloon", "Police", "TBD", "Recreational Vehicle", "Space Shuttle", "SSTV", "Bus", "ATV", "National WX Service Site", "Helicopter", "Yacht/Sail Boat", "WinAPRS", "Human/Person", "Triangle", "Mail/Postoffice", "Large Aircraft", "WX Station", "Dish Antenna", "Ambulance", "Bicycle", "Incident Command Post", "Dual Garage/Fire Dept", "Horse/Equestrian", "Fire Truck", "Glider", "Hospital", "IOTA", "Jeep", "Truck", "Laptop", "Mic-Repeater", "Node", "Emergency Operations Center", "Rover (dog)", "Grid Square above 128m", "Repeater", "Ship/Power Boat", "Truck Stop", "Truck (18 wheeler)", "Van", "Water Station", "X-APRS", "Yagi at QTH", "TDB", "[reserved]" ) def watts_to_dBm(watts): """Converts @watts in watts to dBm""" return int(10 * math.log10(int(watts * 1000))) def dBm_to_watts(dBm): """Converts @dBm from dBm to watts""" return int(math.pow(10, (dBm - 30) / 10)) class PowerLevel: """Represents a power level supported by a radio""" def __init__(self, label, watts=0, dBm=0): if watts: dBm = watts_to_dBm(watts) self._power = int(dBm) self._label = label def __str__(self): return str(self._label) def __int__(self): return self._power def __sub__(self, val): return int(self) - int(val) def __add__(self, val): return int(self) + int(val) def __eq__(self, val): if val is not None: return int(self) == int(val) return False def __lt__(self, val): return int(self) < int(val) def __gt__(self, val): return int(self) > int(val) def __nonzero__(self): return int(self) != 0 def __repr__(self): return "%s (%i dBm)" % (self._label, self._power) def parse_freq(freqstr): """Parse a frequency string and return the value in integral Hz""" freqstr = freqstr.strip() if freqstr == "": return 0 elif freqstr.endswith(" MHz"): return parse_freq(freqstr.split(" ")[0]) elif freqstr.endswith(" kHz"): return int(freqstr.split(" ")[0]) * 1000 if "." in freqstr: mhz, khz = freqstr.split(".") if mhz == "": mhz = 0 khz = khz.ljust(6, "0") if len(khz) > 6: raise ValueError("Invalid kHz value: %s", khz) mhz = int(mhz) * 1000000 khz = int(khz) else: mhz = int(freqstr) * 1000000 khz = 0 return mhz + khz def format_freq(freq): """Format a frequency given in Hz as a string""" return "%i.%06i" % (freq / 1000000, freq % 1000000) class ImmutableValueError(ValueError): pass class Memory: """Base class for a single radio memory""" freq = 0 number = 0 extd_number = "" name = "" vfo = 0 rtone = 88.5 ctone = 88.5 dtcs = 23 rx_dtcs = 23 tmode = "" cross_mode = "Tone->Tone" dtcs_polarity = "NN" skip = "" power = None duplex = "" offset = 600000 mode = "FM" tuning_step = 5.0 comment = "" empty = False immutable = [] # A RadioSettingsGroup of additional settings supported by the radio, # or an empty list if none extra = [] def __init__(self): self.freq = 0 self.number = 0 self.extd_number = "" self.name = "" self.vfo = 0 self.rtone = 88.5 self.ctone = 88.5 self.dtcs = 23 self.rx_dtcs = 23 self.tmode = "" self.cross_mode = "Tone->Tone" self.dtcs_polarity = "NN" self.skip = "" self.power = None self.duplex = "" self.offset = 600000 self.mode = "FM" self.tuning_step = 5.0 self.comment = "" self.empty = False self.immutable = [] _valid_map = { "rtone" : TONES + TONES_EXTRA, "ctone" : TONES + TONES_EXTRA, "dtcs" : ALL_DTCS_CODES, "rx_dtcs" : ALL_DTCS_CODES, "tmode" : TONE_MODES, "dtcs_polarity" : ["NN", "NR", "RN", "RR"], "cross_mode" : CROSS_MODES, "mode" : MODES, "duplex" : ["", "+", "-", "split", "off"], "skip" : SKIP_VALUES, "empty" : [True, False], "dv_code" : [x for x in range(0, 100)], } def __repr__(self): return "Memory[%i]" % self.number def dupe(self): """Return a deep copy of @self""" mem = self.__class__() for k, v in self.__dict__.items(): mem.__dict__[k] = v return mem def clone(self, source): """Absorb all of the properties of @source""" for k, v in source.__dict__.items(): self.__dict__[k] = v CSV_FORMAT = ["Location", "Name", "Frequency", "Duplex", "Offset", "Tone", "rToneFreq", "cToneFreq", "DtcsCode", "DtcsPolarity", "Mode", "TStep", "Skip", "Comment", "URCALL", "RPT1CALL", "RPT2CALL"] def __setattr__(self, name, val): if not hasattr(self, name): raise ValueError("No such attribute `%s'" % name) if name in self.immutable: raise ImmutableValueError("Field %s is not " % name + "mutable on this memory") if self._valid_map.has_key(name) and val not in self._valid_map[name]: raise ValueError("`%s' is not in valid list: %s" % (\ val, self._valid_map[name])) self.__dict__[name] = val def format_freq(self): """Return a properly-formatted string of this memory's frequency""" return format_freq(self.freq) def parse_freq(self, freqstr): """Set the frequency from a string""" self.freq = parse_freq(freqstr) return self.freq def __str__(self): if self.tmode == "Tone": tenc = "*" else: tenc = " " if self.tmode == "TSQL": tsql = "*" else: tsql = " " if self.tmode == "DTCS": dtcs = "*" else: dtcs = " " if self.duplex == "": dup = "/" else: dup = self.duplex return "Memory %i: %s%s%s %s (%s) r%.1f%s c%.1f%s d%03i%s%s [%.2f]"% \ (self.number, format_freq(self.freq), dup, format_freq(self.offset), self.mode, self.name, self.rtone, tenc, self.ctone, tsql, self.dtcs, dtcs, self.dtcs_polarity, self.tuning_step) def to_csv(self): """Return a CSV representation of this memory""" return [ "%i" % self.number, "%s" % self.name, format_freq(self.freq), "%s" % self.duplex, format_freq(self.offset), "%s" % self.tmode, "%.1f" % self.rtone, "%.1f" % self.ctone, "%03i" % self.dtcs, "%s" % self.dtcs_polarity, "%s" % self.mode, "%.2f" % self.tuning_step, "%s" % self.skip, "%s" % self.comment, "", "", "", ""] @classmethod def _from_csv(cls, _line): line = _line.strip() if line.startswith("Location"): raise errors.InvalidMemoryLocation("Non-CSV line") vals = line.split(SEPCHAR) if len(vals) < 11: raise errors.InvalidDataError("CSV format error " + "(14 columns expected)") if vals[10] == "DV": mem = DVMemory() else: mem = Memory() mem.really_from_csv(vals) return mem def really_from_csv(self, vals): """Careful parsing of split-out @vals""" try: self.number = int(vals[0]) except: print "Loc: %s" % vals[0] raise errors.InvalidDataError("Location is not a valid integer") self.name = vals[1] try: self.freq = float(vals[2]) except: raise errors.InvalidDataError("Frequency is not a valid number") if vals[3].strip() in ["+", "-", ""]: self.duplex = vals[3].strip() else: raise errors.InvalidDataError("Duplex is not +,-, or empty") try: self.offset = float(vals[4]) except: raise errors.InvalidDataError("Offset is not a valid number") self.tmode = vals[5] if self.tmode not in TONE_MODES: raise errors.InvalidDataError("Invalid tone mode `%s'" % self.tmode) try: self.rtone = float(vals[6]) except: raise errors.InvalidDataError("rTone is not a valid number") if self.rtone not in TONES: raise errors.InvalidDataError("rTone is not valid") try: self.ctone = float(vals[7]) except: raise errors.InvalidDataError("cTone is not a valid number") if self.ctone not in TONES: raise errors.InvalidDataError("cTone is not valid") try: self.dtcs = int(vals[8], 10) except: raise errors.InvalidDataError("DTCS code is not a valid number") if self.dtcs not in DTCS_CODES: raise errors.InvalidDataError("DTCS code is not valid") try: self.rx_dtcs = int(vals[8], 10) except: raise errors.InvalidDataError("DTCS Rx code is not a valid number") if self.rx_dtcs not in DTCS_CODES: raise errors.InvalidDataError("DTCS Rx code is not valid") if vals[9] in ["NN", "NR", "RN", "RR"]: self.dtcs_polarity = vals[9] else: raise errors.InvalidDataError("DtcsPolarity is not valid") if vals[10] in MODES: self.mode = vals[10] else: raise errors.InvalidDataError("Mode is not valid") try: self.tuning_step = float(vals[11]) except: raise errors.InvalidDataError("Tuning step is invalid") try: self.skip = vals[12] except: raise errors.InvalidDataError("Skip value is not valid") return True class DVMemory(Memory): """A Memory with D-STAR attributes""" dv_urcall = "CQCQCQ" dv_rpt1call = "" dv_rpt2call = "" dv_code = 0 def __str__(self): string = Memory.__str__(self) string += " <%s,%s,%s>" % (self.dv_urcall, self.dv_rpt1call, self.dv_rpt2call) return string def to_csv(self): return [ "%i" % self.number, "%s" % self.name, format_freq(self.freq), "%s" % self.duplex, format_freq(self.offset), "%s" % self.tmode, "%.1f" % self.rtone, "%.1f" % self.ctone, "%03i" % self.dtcs, "%s" % self.dtcs_polarity, "%s" % self.mode, "%.2f" % self.tuning_step, "%s" % self.skip, "%s" % self.comment, "%s" % self.dv_urcall, "%s" % self.dv_rpt1call, "%s" % self.dv_rpt2call, "%i" % self.dv_code] def really_from_csv(self, vals): Memory.really_from_csv(self, vals) self.dv_urcall = vals[15].rstrip()[:8] self.dv_rpt1call = vals[16].rstrip()[:8] self.dv_rpt2call = vals[17].rstrip()[:8] try: self.dv_code = int(vals[18].strip()) except Exception: self.dv_code = 0 class MemoryMapping(object): """Base class for a memory mapping""" def __init__(self, model, index, name): self._model = model self._index = index self._name = name def __str__(self): return self.get_name() def __repr__(self): return "%s-%s" % (self.__class__.__name__, self._index) def get_name(self): """Returns the mapping name""" return self._name def get_index(self): """Returns the immutable index (string or int)""" return self._index def __eq__(self, other): return self.get_index() == other.get_index() class MappingModel(object): """Base class for a memory mapping model""" def __init__(self, radio, name): self._radio = radio self._name = name def get_name(self): return self._name def get_num_mappings(self): """Returns the number of mappings in the model (should be callable without consulting the radio""" raise NotImplementedError() def get_mappings(self): """Return a list of mappings""" raise NotImplementedError() def add_memory_to_mapping(self, memory, mapping): """Add @memory to @mapping.""" raise NotImplementedError() def remove_memory_from_mapping(self, memory, mapping): """Remove @memory from @mapping. Shall raise exception if @memory is not in @bank""" raise NotImplementedError() def get_mapping_memories(self, mapping): """Return a list of memories in @mapping""" raise NotImplementedError() def get_memory_mappings(self, memory): """Return a list of mappings that @memory is in""" raise NotImplementedError() class Bank(MemoryMapping): """Base class for a radio's Bank""" class NamedBank(Bank): """A bank that can have a name""" def set_name(self, name): """Changes the user-adjustable bank name""" self._name = name class BankModel(MappingModel): """A bank model where one memory is in zero or one banks at any point""" def __init__(self, radio, name='Banks'): super(BankModel, self).__init__(radio, name) class MappingModelIndexInterface: """Interface for mappings with index capabilities""" def get_index_bounds(self): """Returns a tuple (lo,hi) of the min and max mapping indices""" raise NotImplementedError() def get_memory_index(self, memory, mapping): """Returns the index of @memory in @mapping""" raise NotImplementedError() def set_memory_index(self, memory, mapping, index): """Sets the index of @memory in @mapping to @index""" raise NotImplementedError() def get_next_mapping_index(self, mapping): """Returns the next available mapping index in @mapping, or raises Exception if full""" raise NotImplementedError() class MTOBankModel(BankModel): """A bank model where one memory can be in multiple banks at once """ pass def console_status(status): """Write a status object to the console""" import sys sys.stderr.write("\r%s" % status) class RadioPrompts: """Radio prompt strings""" experimental = None pre_download = None pre_upload = None BOOLEAN = [True, False] class RadioFeatures: """Radio Feature Flags""" _valid_map = { # General "has_bank_index" : BOOLEAN, "has_dtcs" : BOOLEAN, "has_rx_dtcs" : BOOLEAN, "has_dtcs_polarity" : BOOLEAN, "has_mode" : BOOLEAN, "has_offset" : BOOLEAN, "has_name" : BOOLEAN, "has_bank" : BOOLEAN, "has_bank_names" : BOOLEAN, "has_tuning_step" : BOOLEAN, "has_ctone" : BOOLEAN, "has_cross" : BOOLEAN, "has_infinite_number" : BOOLEAN, "has_nostep_tuning" : BOOLEAN, "has_comment" : BOOLEAN, "has_settings" : BOOLEAN, # Attributes "valid_modes" : [], "valid_tmodes" : [], "valid_duplexes" : [], "valid_tuning_steps" : [], "valid_bands" : [], "valid_skips" : [], "valid_power_levels" : [], "valid_characters" : "", "valid_name_length" : 0, "valid_cross_modes" : [], "valid_dtcs_pols" : [], "valid_dtcs_codes" : [], "valid_special_chans" : [], "has_sub_devices" : BOOLEAN, "memory_bounds" : (0, 0), "can_odd_split" : BOOLEAN, # D-STAR "requires_call_lists" : BOOLEAN, "has_implicit_calls" : BOOLEAN, } def __setattr__(self, name, val): if name.startswith("_"): self.__dict__[name] = val return elif not name in self._valid_map.keys(): raise ValueError("No such attribute `%s'" % name) if type(self._valid_map[name]) == tuple: # Tuple, cardinality must match if type(val) != tuple or len(val) != len(self._valid_map[name]): raise ValueError("Invalid value `%s' for attribute `%s'" % \ (val, name)) elif type(self._valid_map[name]) == list and not self._valid_map[name]: # Empty list, must be another list if type(val) != list: raise ValueError("Invalid value `%s' for attribute `%s'" % \ (val, name)) elif type(self._valid_map[name]) == str: if type(val) != str: raise ValueError("Invalid value `%s' for attribute `%s'" % \ (val, name)) elif type(self._valid_map[name]) == int: if type(val) != int: raise ValueError("Invalid value `%s' for attribute `%s'" % \ (val, name)) elif val not in self._valid_map[name]: # Value not in the list of valid values raise ValueError("Invalid value `%s' for attribute `%s'" % (val, name)) self.__dict__[name] = val def __getattr__(self, name): raise AttributeError("pylint is confused by RadioFeatures") def init(self, attribute, default, doc=None): """Initialize a feature flag @attribute with default value @default, and documentation string @doc""" self.__setattr__(attribute, default) self.__docs[attribute] = doc def get_doc(self, attribute): """Return the description of @attribute""" return self.__docs[attribute] def __init__(self): self.__docs = {} self.init("has_bank_index", False, "Indicates that memories in a bank can be stored in " + "an order other than in main memory") self.init("has_dtcs", True, "Indicates that DTCS tone mode is available") self.init("has_rx_dtcs", False, "Indicates that radio can use two different DTCS codes for rx and tx") self.init("has_dtcs_polarity", True, "Indicates that the DTCS polarity can be changed") self.init("has_mode", True, "Indicates that multiple emission modes are supported") self.init("has_offset", True, "Indicates that the TX offset memory property is supported") self.init("has_name", True, "Indicates that an alphanumeric memory name is supported") self.init("has_bank", True, "Indicates that memories may be placed into banks") self.init("has_bank_names", False, "Indicates that banks may be named") self.init("has_tuning_step", True, "Indicates that memories store their tuning step") self.init("has_ctone", True, "Indicates that the radio keeps separate tone frequencies " + "for repeater and CTCSS operation") self.init("has_cross", False, "Indicates that the radios supports different tone modes " + "on transmit and receive") self.init("has_infinite_number", False, "Indicates that the radio is not constrained in the " + "number of memories that it can store") self.init("has_nostep_tuning", False, "Indicates that the radio does not require a valid " + "tuning step to store a frequency") self.init("has_comment", False, "Indicates that the radio supports storing a comment " + "with each memory") self.init("has_settings", False, "Indicates that the radio supports general settings") self.init("valid_modes", list(MODES), "Supported emission (or receive) modes") self.init("valid_tmodes", [], "Supported tone squelch modes") self.init("valid_duplexes", ["", "+", "-"], "Supported duplex modes") self.init("valid_tuning_steps", list(TUNING_STEPS), "Supported tuning steps") self.init("valid_bands", [], "Supported frequency ranges") self.init("valid_skips", ["", "S"], "Supported memory scan skip settings") self.init("valid_power_levels", [], "Supported power levels") self.init("valid_characters", CHARSET_UPPER_NUMERIC, "Supported characters for a memory's alphanumeric tag") self.init("valid_name_length", 6, "The maximum number of characters in a memory's " + "alphanumeric tag") self.init("valid_cross_modes", list(CROSS_MODES), "Supported tone cross modes") self.init("valid_dtcs_pols", ["NN", "RN", "NR", "RR"], "Supported DTCS polarities") self.init("valid_dtcs_codes", list(DTCS_CODES), "Supported DTCS codes") self.init("valid_special_chans", [], "Supported special channel names") self.init("has_sub_devices", False, "Indicates that the radio behaves as two semi-independent " + "devices") self.init("memory_bounds", (0, 1), "The minimum and maximum channel numbers") self.init("can_odd_split", False, "Indicates that the radio can store an independent " + "transmit frequency") self.init("requires_call_lists", True, "[D-STAR] Indicates that the radio requires all callsigns " + "to be in the master list and cannot be stored " + "arbitrarily in each memory channel") self.init("has_implicit_calls", False, "[D-STAR] Indicates that the radio has an implied " + "callsign at the beginning of the master URCALL list") def is_a_feature(self, name): """Returns True if @name is a valid feature flag name""" return name in self._valid_map.keys() def __getitem__(self, name): return self.__dict__[name] def validate_memory(self, mem): """Return a list of warnings and errors that will be encoundered if trying to set @mem on the current radio""" msgs = [] lo, hi = self.memory_bounds if not self.has_infinite_number and \ (mem.number < lo or mem.number > hi) and \ mem.extd_number not in self.valid_special_chans: msg = ValidationWarning("Location %i is out of range" % mem.number) msgs.append(msg) if (self.valid_modes and mem.mode not in self.valid_modes and mem.mode != "Auto"): msg = ValidationError("Mode %s not supported" % mem.mode) msgs.append(msg) if self.valid_tmodes and mem.tmode not in self.valid_tmodes: msg = ValidationError("Tone mode %s not supported" % mem.tmode) msgs.append(msg) else: if mem.tmode == "Cross": if self.valid_cross_modes and \ mem.cross_mode not in self.valid_cross_modes: msg = ValidationError("Cross tone mode %s not supported" % \ mem.cross_mode) msgs.append(msg) if self.has_dtcs_polarity and \ mem.dtcs_polarity not in self.valid_dtcs_pols: msg = ValidationError("DTCS Polarity %s not supported" % \ mem.dtcs_polarity) msgs.append(msg) if self.valid_dtcs_codes and \ mem.dtcs not in self.valid_dtcs_codes: msg = ValidationError("DTCS Code %03i not supported" % mem.dtcs) if self.valid_dtcs_codes and \ mem.rx_dtcs not in self.valid_dtcs_codes: msg = ValidationError("DTCS Code %03i not supported" % mem.rx_dtcs) if self.valid_duplexes and mem.duplex not in self.valid_duplexes: msg = ValidationError("Duplex %s not supported" % mem.duplex) msgs.append(msg) ts = mem.tuning_step if self.valid_tuning_steps and ts not in self.valid_tuning_steps and \ not self.has_nostep_tuning: msg = ValidationError("Tuning step %.2f not supported" % ts) msgs.append(msg) if self.valid_bands: valid = False for lo, hi in self.valid_bands: if lo <= mem.freq < hi: valid = True break if not valid: msg = ValidationError( ("Frequency {freq} is out " "of supported range").format(freq=format_freq(mem.freq))) msgs.append(msg) if mem.power and \ self.valid_power_levels and \ mem.power not in self.valid_power_levels: msg = ValidationWarning("Power level %s not supported" % mem.power) msgs.append(msg) if self.valid_tuning_steps and not self.has_nostep_tuning: try: step = required_step(mem.freq) if step not in self.valid_tuning_steps: msg = ValidationError("Frequency requires %.2fkHz step" %\ required_step(mem.freq)) msgs.append(msg) except errors.InvalidDataError, e: msgs.append(str(e)) if self.valid_characters: for char in mem.name: if char not in self.valid_characters: msgs.append(ValidationWarning("Name character " + "`%s'" % char + " not supported")) break return msgs class ValidationMessage(str): """Base class for Validation Errors and Warnings""" pass class ValidationWarning(ValidationMessage): """A non-fatal warning during memory validation""" pass class ValidationError(ValidationMessage): """A fatal error during memory validation""" pass class Radio(object): """Base class for all Radio drivers""" BAUD_RATE = 9600 HARDWARE_FLOW = False VENDOR = "Unknown" MODEL = "Unknown" VARIANT = "" def status_fn(self, status): """Deliver @status to the UI""" console_status(status) def __init__(self, pipe): self.errors = [] self.pipe = pipe def get_features(self): """Return a RadioFeatures object for this radio""" return RadioFeatures() @classmethod def get_name(cls): """Return a printable name for this radio""" return "%s %s" % (cls.VENDOR, cls.MODEL) @classmethod def get_prompts(cls): """Return a set of strings for use in prompts""" return RadioPrompts() def set_pipe(self, pipe): """Set the serial object to be used for communications""" self.pipe = pipe def get_memory(self, number): """Return a Memory object for the memory at location @number""" pass def erase_memory(self, number): """Erase memory at location @number""" mem = Memory() mem.number = number mem.empty = True self.set_memory(mem) def get_memories(self, lo=None, hi=None): """Get all the memories between @lo and @hi""" pass def set_memory(self, memory): """Set the memory object @memory""" pass def get_mapping_models(self): """Returns a list of MappingModel objects (or an empty list)""" if hasattr(self, "get_bank_model"): # FIXME: Backwards compatibility for old bank models bank_model = self.get_bank_model() if bank_model: return [bank_model] return [] def get_raw_memory(self, number): """Return a raw string describing the memory at @number""" pass def filter_name(self, name): """Filter @name to just the length and characters supported""" rf = self.get_features() if rf.valid_characters == rf.valid_characters.upper(): # Radio only supports uppercase, so help out here name = name.upper() return "".join([x for x in name[:rf.valid_name_length] if x in rf.valid_characters]) def get_sub_devices(self): """Return a list of sub-device Radio objects, if RadioFeatures.has_sub_devices is True""" return [] def validate_memory(self, mem): """Return a list of warnings and errors that will be encoundered if trying to set @mem on the current radio""" rf = self.get_features() return rf.validate_memory(mem) def get_settings(self): """Returns a RadioSettingGroup containing one or more RadioSettingGroup or RadioSetting objects. These represent general setting knobs and dials that can be adjusted on the radio. If this function is implemented, the has_settings RadioFeatures flag should be True and set_settings() must be implemented as well.""" pass def set_settings(self, settings): """Accepts the top-level RadioSettingGroup returned from get_settings() and adjusts the values in the radio accordingly. This function expects the entire RadioSettingGroup hierarchy returned from get_settings(). If this function is implemented, the has_settings RadioFeatures flag should be True and get_settings() must be implemented as well.""" pass class FileBackedRadio(Radio): """A file-backed radio stores its data in a file""" FILE_EXTENSION = "img" def __init__(self, *args, **kwargs): Radio.__init__(self, *args, **kwargs) self._memobj = None def save(self, filename): """Save the radio's memory map to @filename""" self.save_mmap(filename) def load(self, filename): """Load the radio's memory map object from @filename""" self.load_mmap(filename) def process_mmap(self): """Process a newly-loaded or downloaded memory map""" pass def load_mmap(self, filename): """Load the radio's memory map from @filename""" mapfile = file(filename, "rb") self._mmap = memmap.MemoryMap(mapfile.read()) mapfile.close() self.process_mmap() def save_mmap(self, filename): """ try to open a file and write to it If IOError raise a File Access Error Exception """ try: mapfile = file(filename, "wb") mapfile.write(self._mmap.get_packed()) mapfile.close() except IOError: raise Exception("File Access Error") def get_mmap(self): """Return the radio's memory map object""" return self._mmap class CloneModeRadio(FileBackedRadio): """A clone-mode radio does a full memory dump in and out and we store an image of the radio into an image file""" _memsize = 0 def __init__(self, pipe): self.errors = [] self._mmap = None if isinstance(pipe, str): self.pipe = None self.load_mmap(pipe) elif isinstance(pipe, memmap.MemoryMap): self.pipe = None self._mmap = pipe self.process_mmap() else: FileBackedRadio.__init__(self, pipe) def get_memsize(self): """Return the radio's memory size""" return self._memsize @classmethod def match_model(cls, filedata, filename): """Given contents of a stored file (@filedata), return True if this radio driver handles the represented model""" # Unless the radio driver does something smarter, claim # support if the data is the same size as our memory. # Ideally, each radio would perform an intelligent analysis to # make this determination to avoid model conflicts with # memories of the same size. return len(filedata) == cls._memsize def sync_in(self): "Initiate a radio-to-PC clone operation" pass def sync_out(self): "Initiate a PC-to-radio clone operation" pass class LiveRadio(Radio): """Base class for all Live-Mode radios""" pass class NetworkSourceRadio(Radio): """Base class for all radios based on a network source""" def do_fetch(self): """Fetch the source data from the network""" pass class IcomDstarSupport: """Base interface for radios supporting Icom's D-STAR technology""" MYCALL_LIMIT = (1, 1) URCALL_LIMIT = (1, 1) RPTCALL_LIMIT = (1, 1) def get_urcall_list(self): """Return a list of URCALL callsigns""" return [] def get_repeater_call_list(self): """Return a list of RPTCALL callsigns""" return [] def get_mycall_list(self): """Return a list of MYCALL callsigns""" return [] def set_urcall_list(self, calls): """Set the URCALL callsign list""" pass def set_repeater_call_list(self, calls): """Set the RPTCALL callsign list""" pass def set_mycall_list(self, calls): """Set the MYCALL callsign list""" pass class ExperimentalRadio: """Interface for experimental radios""" @classmethod def get_experimental_warning(cls): return ("This radio's driver is marked as experimental and may " + "be unstable or unsafe to use.") class Status: """Clone status object for conveying clone progress to the UI""" name = "Job" msg = "Unknown" max = 100 cur = 0 def __str__(self): try: pct = (self.cur / float(self.max)) * 100 nticks = int(pct) / 10 ticks = "=" * nticks except ValueError: pct = 0.0 ticks = "?" * 10 return "|%-10s| %2.1f%% %s" % (ticks, pct, self.msg) def is_fractional_step(freq): """Returns True if @freq requires a 12.5kHz or 6.25kHz step""" return not is_5_0(freq) and (is_12_5(freq) or is_6_25(freq)) def is_5_0(freq): """Returns True if @freq is reachable by a 5kHz step""" return (freq % 5000) == 0 def is_12_5(freq): """Returns True if @freq is reachable by a 12.5kHz step""" return (freq % 12500) == 0 def is_6_25(freq): """Returns True if @freq is reachable by a 6.25kHz step""" return (freq % 6250) == 0 def is_2_5(freq): """Returns True if @freq is reachable by a 2.5kHz step""" return (freq % 2500) == 0 def required_step(freq): """Returns the simplest tuning step that is required to reach @freq""" if is_5_0(freq): return 5.0 elif is_12_5(freq): return 12.5 elif is_6_25(freq): return 6.25 elif is_2_5(freq): return 2.5 else: raise errors.InvalidDataError("Unable to calculate the required " + "tuning step for %i.%5i" % \ (freq / 1000000, freq % 1000000)) def fix_rounded_step(freq): """Some radios imply the last bit of 12.5kHz and 6.25kHz step frequencies. Take the base @freq and return the corrected one""" try: required_step(freq) return freq except errors.InvalidDataError: pass try: required_step(freq + 500) return freq + 500 except errors.InvalidDataError: pass try: required_step(freq + 250) return freq + 250 except errors.InvalidDataError: pass try: required_step(freq + 750) return float(freq + 750) except errors.InvalidDataError: pass raise errors.InvalidDataError("Unable to correct rounded frequency " + \ format_freq(freq)) def _name(name, len, just_upper): """Justify @name to @len, optionally converting to all uppercase""" if just_upper: name = name.upper() return name.ljust(len)[:len] def name6(name, just_upper=True): """6-char name""" return _name(name, 6, just_upper) def name8(name, just_upper=False): """8-char name""" return _name(name, 8, just_upper) def name16(name, just_upper=False): """16-char name""" return _name(name, 16, just_upper) def to_GHz(val): """Convert @val in GHz to Hz""" return val * 1000000000 def to_MHz(val): """Convert @val in MHz to Hz""" return val * 1000000 def to_kHz(val): """Convert @val in kHz to Hz""" return val * 1000 def from_GHz(val): """Convert @val in Hz to GHz""" return val / 100000000 def from_MHz(val): """Convert @val in Hz to MHz""" return val / 100000 def from_kHz(val): """Convert @val in Hz to kHz""" return val / 100 def split_tone_decode(mem, txtone, rxtone): """ Set tone mode and values on @mem based on txtone and rxtone specs like: None, None, None "Tone", 123.0, None "DTCS", 23, "N" """ txmode, txval, txpol = txtone rxmode, rxval, rxpol = rxtone mem.dtcs_polarity = "%s%s" % (txpol or "N", rxpol or "N") if not txmode and not rxmode: # No tone return if txmode == "Tone" and not rxmode: mem.tmode = "Tone" mem.rtone = txval return if txmode == rxmode == "Tone" and txval == rxval: # TX and RX same tone -> TSQL mem.tmode = "TSQL" mem.ctone = txval return if txmode == rxmode == "DTCS" and txval == rxval: mem.tmode = "DTCS" mem.dtcs = txval return mem.tmode = "Cross" mem.cross_mode = "%s->%s" % (txmode or "", rxmode or "") if txmode == "Tone": mem.rtone = txval elif txmode == "DTCS": mem.dtcs = txval if rxmode == "Tone": mem.ctone = rxval elif rxmode == "DTCS": mem.rx_dtcs = rxval def split_tone_encode(mem): """ Returns TX, RX tone specs based on @mem like: None, None, None "Tone", 123.0, None "DTCS", 23, "N" """ txmode = '' rxmode = '' txval = None rxval = None if mem.tmode == "Tone": txmode = "Tone" txval = mem.rtone elif mem.tmode == "TSQL": txmode = rxmode = "Tone" txval = rxval = mem.ctone elif mem.tmode == "DTCS": txmode = rxmode = "DTCS" txval = rxval = mem.dtcs elif mem.tmode == "Cross": txmode, rxmode = mem.cross_mode.split("->", 1) if txmode == "Tone": txval = mem.rtone elif txmode == "DTCS": txval = mem.dtcs if rxmode == "Tone": rxval = mem.ctone elif rxmode == "DTCS": rxval = mem.rx_dtcs if txmode == "DTCS": txpol = mem.dtcs_polarity[0] else: txpol = None if rxmode == "DTCS": rxpol = mem.dtcs_polarity[1] else: rxpol = None return ((txmode, txval, txpol), (rxmode, rxval, rxpol))
gpl-3.0
dyninc/dyn-python
dyn/mm/accounts.py
2
22757
# -*- coding: utf-8 -*- """This module contains Dyn Message Management accounts features. It's important to note that any/all timestamps are expected as `datetime.datetime` instances and will be returned as such. """ from datetime import datetime from ..core import cleared_class_dict from .utils import str_to_date, date_to_str, APIDict from .errors import NoSuchAccountError from .session import MMSession __author__ = 'jnappi' def get_all_accounts(): """Return a list of all :class:`~dyn.mm.accounts.Account`'s accessible to the currently authenticated user """ uri = '/accounts' response = MMSession.get_session().execute(uri, 'GET') accounts = [] for account in response['accounts']: username = account.pop('username') cap = response['emailcap'] accounts.append(Account(username, api=False, emailcap=cap, **account)) return accounts def get_all_senders(start_index=0): """Return a list of all :class:`~dyn.mm.accounts.ApprovedSenders`'s accessible to the currently authenticated user """ uri = '/senders' args = {'start_index': start_index} response = MMSession.get_session().execute(uri, 'GET', args) senders = [] for sender in response['senders']: email = sender.pop('emailaddress') senders.append(ApprovedSender(email, api=False, **sender)) return senders def get_all_suppressions(startdate=None, enddate=None, startindex=0): """Return a list of all :class:`~dyn.mm.accounts.Suppression`'s""" uri = '/suppressions' args = {'start_index': startindex} if startdate: args['startdate'] = date_to_str(startdate) enddate = enddate or datetime.now() args['enddate'] = date_to_str(enddate) response = MMSession.get_session().execute(uri, 'GET', args) suppressions = [] for suppression in response['suppressions']: email = suppression.pop('emailaddress') suppress_time = suppression.pop('suppresstime') reason_type = suppression.pop('reasontype') suppressions.append(Suppression(email, api=False, reasontype=reason_type, suppresstime=suppress_time)) return suppressions class Account(object): """A Message Management account instance. password, companyname, and phone are required for creating a new account. To access an existing Account, simply provide the username of the account you wish to access. """ uri = '/accounts' def __init__(self, username, *args, **kwargs): """Create a new :class:`~dyn.mm.accounts.Account` object :param username: The username for this :class:`~dyn.mm.accounts.Account` - must be a valid email address, and must be unique among all other sub-accounts. :param password: :class:`~dyn.mm.accounts.Account` password to be assigned. May be passed as clear text or MD5-encrypted with "md5-" as a prefix :param companyname: Name of the company assigned to this :class:`~dyn.mm.accounts.Account` :param phone: Contact Phone number for this :class:`~dyn.mm.accounts.Account` :param address: The primary address associated with this :class:`~dyn.mm.accounts.Account` :param city: The City associated with this :class:`~dyn.mm.accounts.Account` :param state: The State associated with this :class:`~dyn.mm.accounts.Account` :param zipcode: The Zipcode associated with this :class:`~dyn.mm.accounts.Account` :param country: Two-letter English ISO 3166 country code :param timezone: The timezone of the account, in [+/-]h.mm format :param bounceurl: Bounce postback URL :param spamurl: Spam postback URL :param unsubscribeurl: Unsubscribe postback URL :param trackopens: Toggle open tracking (1 or 0). :param tracklinks: Toggle click tracking (1 or 0). :param trackunsubscribes: Toggle automatic list-unsubscribe support (1 or 0). :param generatenewapikey: Used to create a new API key for an existing account (1 or 0). """ super(Account, self).__init__() self._username = username self._accountname = self._address = self._apikey = self._city = None self._companyname = self._contactname = self._country = None self._created = self._emailsent = self._max_sample_count = None self._phone = self._state = self._timezone = self._tracklinks = None self._trackopens = self._trackunsubscribes = self._usertype = None self._zipcode = self._password = self._emailcap = None if 'api' in kwargs: del kwargs['api'] self._update(kwargs) elif len(args) + len(kwargs) == 0: self._get() else: self._post(*args, **kwargs) self._xheaders = None def _post(self, password, companyname, phone, address=None, city=None, state=None, zipcode=None, country=None, timezone=None, bounceurl=None, spamurl=None, unsubscribeurl=None, trackopens=0, tracklinks=0, trackunsubscribes=0, generatenewapikey=0): """Create a new :class:`~dyn.mm.accounts.Account` on the Dyn Email System """ self._password = password self._companyname = companyname self._phone = phone self._address = address self._city = city self._state = state self._zipcode = zipcode self._country = country self._timezone = timezone self._bounceurl = bounceurl self._spamurl = spamurl self._unsubscribeurl = unsubscribeurl self._trackopens = trackopens self._tracklinks = tracklinks self._trackunsubscribes = trackunsubscribes self._generatenewapikey = generatenewapikey valid = ('username', 'password', 'companyname', 'phone', 'address', 'city', 'state', 'zipcode', 'country', 'timezone', 'bounceurl', 'spamurl', 'unsubscribeurl', 'trackopens', 'tracklinks', 'trackunsubscribes', 'generatenewapikey') d = cleared_class_dict(self.__dict__) api_args = {x[1:]: d[x] for x in d if d[x] is not None and x[1:] in valid} response = MMSession.get_session().execute(self.uri, 'POST', api_args) for key, val in response.items(): setattr(self, '_' + key, val) def _get(self): """Retrieve an existing :class:`~dyn.mm.accounts.Account` from the Dyn Email System """ accounts = get_all_accounts() found = False for account in accounts: if account.username == self._username: self._update(cleared_class_dict(account.__dict__)) found = True if not found: raise NoSuchAccountError('No such Account') def _update(self, data): """Update the fields in this object with the provided data dict""" resp = MMSession.get_session().execute(self.uri, 'POST', data) for key, val in resp.items(): setattr(self, '_' + key, val) @property def xheaders(self): """A list of the configured custom x-header field names associated with this :class:`~dyn.mm.accounts.Account`. """ if self._xheaders is None: self._get_xheaders() return self._xheaders @xheaders.setter def xheaders(self, value): if isinstance(value, dict) and not isinstance(value, APIDict): new_xheaders = APIDict(MMSession.get_session) for key, val in value.items(): new_xheaders[key] = val new_xheaders.uri = '/accounts/xheaders' self._xheaders = new_xheaders elif isinstance(value, APIDict): self._xheaders = value @property def username(self): """A list of the configured custom x-header field names associated with this :class:`~dyn.mm.accounts.Account`. """ return self._username @username.setter def username(self, value): pass @property def account_name(self): return self._accountname @account_name.setter def account_name(self, value): pass @property def address(self): """The primary address associated with this :class:`~dyn.mm.accounts.Account` """ return self._address @address.setter def address(self, value): pass @property def apikey(self): """The apikey for this account""" return self._apikey @apikey.setter def apikey(self, value): pass @property def city(self): """The City associated with this :class:`~dyn.mm.accounts.Account`""" return self._city @city.setter def city(self, value): pass @property def company_name(self): """The name of the company this :class:`~dyn.mm.accounts.Account` is registered under """ return self._companyname @company_name.setter def company_name(self, value): pass @property def contact_name(self): """The name of the contact associated with this :class:`~dyn.mm.accounts.Account` """ return self._contactname @contact_name.setter def contact_name(self, value): pass @property def country(self): """The Two letter country code associated with this :class:`~dyn.mm.accounts.Account` """ return self._country @country.setter def country(self, value): pass @property def created(self): return self._created @created.setter def created(self, value): pass @property def email_sent(self): return self._emailsent @email_sent.setter def email_sent(self, value): pass @property def max_sample_count(self): return self._max_sample_count @max_sample_count.setter def max_sample_count(self, value): pass @property def phone(self): """The primary telephone number of the contact associated with this :class:`~dyn.mm.accounts.Account`""" return self._phone @phone.setter def phone(self, value): pass @property def state(self): """The state associated with this :class:`~dyn.mm.accounts.Account`""" return self._state @state.setter def state(self, value): pass @property def timezone(self): """The current timezone of the primary user of this :class:`~dyn.mm.accounts.Account` """ return self._timezone @timezone.setter def timezone(self, value): pass @property def track_links(self): """A settings flag determining whether or not emails sent from this :class:`~dyn.mm.accounts.Account` will be monitored for followed links """ return self._tracklinks == 1 @track_links.setter def track_links(self, value): pass @property def track_opens(self): """A settings flag determining whether or not emails sent from this :class:`~dyn.mm.accounts.Account` will be monitored for opens """ return self._trackopens == 1 @track_opens.setter def track_opens(self, value): pass @property def track_unsubscribes(self): """A settings flag determining whether or not emails sent from this :class:`~dyn.mm.accounts.Account` will be monitored for unsubscribes """ return self._trackunsubscribes == 1 @track_unsubscribes.setter def track_unsubscribes(self, value): pass @property def user_type(self): return self._usertype @user_type.setter def user_type(self, value): pass @property def zipcode(self): """The zipcode of this :class:`~dyn.mm.accounts.Account` """ return self._zipcode @zipcode.setter def zipcode(self, value): pass @property def password(self): """The password for this :class:`~dyn.mm.accounts.Account`. Note: Unless you've just created this :class:`~dyn.mm.accounts.Account`, this field will be *None*. """ return self._password @password.setter def password(self, value): pass @property def emailcap(self): return self._emailcap @emailcap.setter def emailcap(self, value): pass def _get_xheaders(self): """Build the list of the configured custom x-header field names associated with this :class:`~dyn.mm.accounts.Account`. """ uri = '/accounts/xheaders' api_args = {} response = MMSession.get_session().execute(uri, 'GET', api_args) xheaders = {} for key, val in response.items(): xheaders[key] = val self._xheaders = APIDict(MMSession.get_session, '/accounts/xheaders', xheaders) def delete(self): """Delete this :class:`~dyn.mm.accounts.Account` from the Dyn Email System """ uri = '/accounts/delete' api_args = {'username': self._username} MMSession.get_session().execute(uri, 'POST', api_args) def __str__(self): """str override""" return '<MM Account>: {}'.format(self._username) __repr__ = __unicode__ = __str__ class ApprovedSender(object): """An email address that is able to be used in the "from" field of messages """ uri = '/senders' def __init__(self, emailaddress, *args, **kwargs): """Create an :class:`~dyn.mm.accounts.ApprovedSender` object :param emailaddress: The email address of this :class:`~dyn.mm.accounts.ApprovedSender` :param seeding: 1 to opt this approved sender in for seeding; 0 (default)to opt them out. Seeding is used to provide insight into inbox placement. See the `Approved Senders <https://help.dynect.net/email/control-panel/senders/>`_. page for more information. """ self._emailaddress = emailaddress self._seeding = self._status = self._dkim = self._spf = None self._dkimval = None if 'api' in kwargs: del kwargs['api'] for key, val in kwargs.items(): setattr(self, '_' + key, val) elif len(args) + len(kwargs) > 0: self._post(*args, **kwargs) else: self._get() def _post(self, seeding=0): """Create or update a :class:`~dyn.mm.accounts.ApprovedSender` on the Dyn Message Management System. :param seeding: """ self._seeding = seeding api_args = {'emailaddress': self._emailaddress, 'seeding': self._seeding} response = MMSession.get_session().execute(self.uri, 'POST', api_args) for key, val in response.items(): setattr(self, '_' + key, val) def _get(self): """Get an existing :class:`~dyn.mm.accounts.ApprovedSender` from the Dyn Message Management System. """ uri = '/senders/details' api_args = {'emailaddress': self._emailaddress} response = MMSession.get_session().execute(uri, 'GET', api_args) for key, val in response.items(): setattr(self, '_' + key, val) def _update(self, api_args): """Update this :class:`~dyn.mm.accounts.ApprovedSender` object.""" if 'emailaddress' not in api_args: api_args['emailaddress'] = self._emailaddress response = MMSession.get_session().execute(self.uri, 'POST', api_args) for key, val in response.items(): setattr(self, '_' + key, val) @property def seeding(self): """1 to opt this approved sender in for seeding; 0 to opt them out (default). Seeding is used to provide insight into inbox placement. See the `Approved Senders <https://help.dynect.net/email/control-panel/senders/>`_. page for more information. """ if self._seeding is None: self._seeding = self.status return self._seeding @seeding.setter def seeding(self, value): if value in range(0, 2): self._update({'seeding': value}) @property def status(self): """Retrieves the status of an approved sender -- whether or not it is ready for use in sending. This is most useful when you create a new approved sender and need to know for sure whether it is ready for use. """ uri = '/senders/status' args = {'emailaddress': self._emailaddress} response = MMSession.get_session().execute(uri, 'GET', args) for key in response: self._status = response[key] return self._status @status.setter def status(self, value): pass @property def dkim(self): """DKIM identifier for this approved sender - identifier may contain only aplanumeric characters, dashes, or underscores. """ return self._dkim @dkim.setter def dkim(self, value): uri = '/senders/dkim' api_args = {'emailaddress': self._emailaddress, 'dkim': value} response = MMSession.get_session().execute(uri, 'POST', api_args) for key, val in response.items(): setattr(self, '_' + key, val) @property def spf(self): """SPF for this :class:`~dyn.mm.accounts.ApprovedSender`""" return self._spf @spf.setter def spf(self, value): pass @property def dkimval(self): """DKIM val for this :class:`~dyn.mm.accounts.ApprovedSender`""" return self._dkimval @dkimval.setter def dkimval(self, value): pass def delete(self): """Delete this :class:`~dyn.mm.accounts.ApprovedSender`""" uri = '/senders/delete' api_args = {'emailaddress': self._emailaddress} MMSession.get_session().execute(uri, 'POST', api_args) def __str__(self): """str override""" return '<MM ApprovedSender>: {}'.format(self._emailaddress) __repr__ = __unicode__ = __str__ class Recipient(object): """A :class:`~dyn.mm.accounts.Recipient` is an email address that is capable of recieving email. """ def __init__(self, emailaddress, method='GET'): """Create a :class:`~dyn.mm.accounts.Recipient` object :param emailaddress: This :class:`~dyn.mm.accounts.Recipient`'s email address. :param method: A Flag specifying whether you're looking for an existing :class:`~dyn.mm.accounts.Recipient` or if you want to create a new one. Because both GET and POST calls accept the same requirements there's no way to automatically deduce what the user is trying to do so you must specify either GET or POST in the constructor """ self.emailaddress = emailaddress self.status = self.unsuppressed = self.pending_addition = None self.suppressed = self.pending_removal = None if method == 'GET': self._get() else: self._post() def _get(self): """Private getter method""" uri = '/recipients/status' api_args = {'emailaddress': self.emailaddress} response = MMSession.get_session().execute(uri, 'GET', api_args) for key, val in response.items(): setattr(self, key, val) def _post(self): """Activate a new recipient""" uri = '/recipients/activate' api_args = {'emailaddress': self.emailaddress} # Note: this api call returns nothing, so we won't parse it for data MMSession.get_session().execute(uri, 'POST', api_args) def activate(self): """Updates the status of this recipient to active which allows them to receive email. """ uri = '/recipients/activate' api_args = {'emailaddress': self.emailaddress} MMSession.get_session().execute(uri, 'POST', api_args) class Suppression(object): """A :class:`~dyn.mm.accounts.Supression` representing a suppressed email """ uri = '/suppressions' def __init__(self, emailaddress, *args, **kwargs): """Create a :class:`~dyn.mm.accounts.Suppression` object. :param emailaddress: This email address of for the :class:`~dyn.mm.accounts.Suppression`'s to apply to. """ self.emailaddress = emailaddress self._count = self._suppresstime = None if 'api' in kwargs: del kwargs['api'] for key, val in kwargs.items(): if key == 'suppresstime': self._suppresstime = str_to_date(val) else: setattr(self, '_' + key, val) elif len(args) + len(kwargs) == 0: self._post() def _post(self): """Activate a new recipient""" api_args = {'emailaddress': self.emailaddress} # Note: this api call returns nothing, so we won't parse it for data MMSession.get_session().execute(self.uri, 'POST', api_args) def get_count(self, startdate=None, enddate=None): """Get the count attribute of this suppression for the provided range """ if startdate: startdate = date_to_str(startdate) enddate = enddate or datetime.now() enddate = date_to_str(enddate) api_args = {'startdate': startdate, 'enddate': enddate} else: api_args = None uri = self.uri + '/count' response = MMSession.get_session().execute(uri, 'GET', api_args) self._count = int(response['count']) return self._count @property def count(self): """the total number of email addresses in the suppression list for the specified account, filtered by date range. """ return self._count @count.setter def count(self, value): pass def activate(self): """Removes a :class:`~dyn.mm.accounts.Recipient` from the user's suppression list. This will not unbounce/uncomplain the :class:`~dyn.mm.accounts.Recipient`, but you will be permitted to send to them again. """ uri = self.uri + '/activate' api_args = {'emailaddress': self.emailaddress} MMSession.get_session().execute(uri, 'POST', api_args)
bsd-3-clause
suto/infernal-twin
build/reportlab/build/lib.linux-i686-2.7/reportlab/lib/attrmap.py
32
5508
#Copyright ReportLab Europe Ltd. 2000-2012 #see license.txt for license details #history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/lib/attrmap.py __version__=''' $Id$ ''' __doc__='''Framework for objects whose assignments are checked. Used by graphics. We developed reportlab/graphics prior to Python 2 and metaclasses. For the graphics, we wanted to be able to declare the attributes of a class, check them on assignment, and convert from string arguments. Examples of attrmap-based objects can be found in reportlab/graphics/shapes. It lets us defined structures like the one below, which are seen more modern form in Django models and other frameworks. We'll probably replace this one day soon, hopefully with no impact on client code. class Rect(SolidShape): """Rectangle, possibly with rounded corners.""" _attrMap = AttrMap(BASE=SolidShape, x = AttrMapValue(isNumber), y = AttrMapValue(isNumber), width = AttrMapValue(isNumber), height = AttrMapValue(isNumber), rx = AttrMapValue(isNumber), ry = AttrMapValue(isNumber), ) ''' from reportlab.lib.validators import isAnything, DerivedValue from reportlab.lib.utils import isSeq from reportlab import rl_config class CallableValue: '''a class to allow callable initial values''' def __init__(self,func,*args,**kw): #assert iscallable(func) self.func = func self.args = args self.kw = kw def __call__(self): return self.func(*self.args,**self.kw) class AttrMapValue: '''Simple multi-value holder for attribute maps''' def __init__(self,validate=None,desc=None,initial=None, advancedUsage=0, **kw): self.validate = validate or isAnything self.desc = desc self._initial = initial self._advancedUsage = advancedUsage for k,v in kw.items(): setattr(self,k,v) def __getattr__(self,name): #hack to allow callable initial values if name=='initial': if isinstance(self._initial,CallableValue): return self._initial() return self._initial elif name=='hidden': return 0 raise AttributeError(name) def __repr__(self): return 'AttrMapValue(%s)' % ', '.join(['%s=%r' % i for i in self.__dict__.items()]) class AttrMap(dict): def __init__(self,BASE=None,UNWANTED=[],**kw): data = {} if BASE: if isinstance(BASE,AttrMap): data = BASE else: if not isSeq(BASE): BASE = (BASE,) for B in BASE: am = getattr(B,'_attrMap',self) if am is not self: if am: data.update(am) else: raise ValueError('BASE=%s has wrong kind of value' % ascii(B)) dict.__init__(self,data) self.remove(UNWANTED) self.update(kw) def remove(self,unwanted): for k in unwanted: try: del self[k] except KeyError: pass def clone(self,UNWANTED=[],**kw): c = AttrMap(BASE=self,UNWANTED=UNWANTED) c.update(kw) return c def validateSetattr(obj,name,value): '''validate setattr(obj,name,value)''' if rl_config.shapeChecking: map = obj._attrMap if map and name[0]!= '_': #we always allow the inherited values; they cannot #be checked until draw time. if isinstance(value, DerivedValue): #let it through pass else: try: validate = map[name].validate if not validate(value): raise AttributeError("Illegal assignment of '%s' to '%s' in class %s" % (value, name, obj.__class__.__name__)) except KeyError: raise AttributeError("Illegal attribute '%s' in class %s" % (name, obj.__class__.__name__)) obj.__dict__[name] = value def _privateAttrMap(obj,ret=0): '''clone obj._attrMap if required''' A = obj._attrMap oA = getattr(obj.__class__,'_attrMap',None) if ret: if oA is A: return A.clone(), oA else: return A, None else: if oA is A: obj._attrMap = A.clone() def _findObjectAndAttr(src, P): '''Locate the object src.P for P a string, return parent and name of attribute ''' P = P.split('.') if len(P) == 0: return None, None else: for p in P[0:-1]: src = getattr(src, p) return src, P[-1] def hook__setattr__(obj): if not hasattr(obj,'__attrproxy__'): C = obj.__class__ import new obj.__class__=new.classobj(C.__name__,(C,)+C.__bases__, {'__attrproxy__':[], '__setattr__':lambda self,k,v,osa=getattr(obj,'__setattr__',None),hook=hook: hook(self,k,v,osa)}) def addProxyAttribute(src,name,validate=None,desc=None,initial=None,dst=None): ''' Add a proxy attribute 'name' to src with targets dst ''' #sanity assert hasattr(src,'_attrMap'), 'src object has no _attrMap' A, oA = _privateAttrMap(src,1) if not isSeq(dst): dst = dst, D = [] DV = [] for d in dst: if isSeq(d): d, e = d[0], d[1:] obj, attr = _findObjectAndAttr(src,d) if obj: dA = getattr(obj,'_attrMap',None)
gpl-3.0
psexton/house.local
weather/announcer.py
1
1038
#!/usr/bin/env python3 import datetime import json import requests import Adafruit_IO print("Ran at", datetime.datetime.now()) # Read in the json config # Found at /etc/net.psexton.house-local.json with open('/etc/net.psexton.house-local.json') as config_file: config_data = json.load(config_file) # Read in the weather forecast url = ("https://api.darksky.net/forecast" + "/" + config_data["darkSkySecretKey"] + "/" + config_data["latitude"] + "," + config_data["longitude"] + "?exclude=minutely,hourly,daily,alerts,flags") response = requests.get(url).json() # We want to pull out two values from the response json: # currently.temperature and currently.apparentTemperature temperature_f = response["currently"]["temperature"] apparent_temperature_f = response["currently"]["apparentTemperature"] # Send them up to adafruit.io aio = Adafruit_IO.Client(config_data["adafruitIoKey"]) aio.send("weather-temperature-f", temperature_f) aio.send("weather-apparent-temperature-f", apparent_temperature_f)
lgpl-3.0
ramanajee/phantomjs
src/qt/qtwebkit/Tools/Scripts/webkitpy/style/checkers/cmake.py
123
7236
# Copyright (C) 2012 Intel 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: # 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. # # 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. """Supports checking WebKit style in cmake files.(.cmake, CMakeLists.txt)""" import re from common import TabChecker class CMakeChecker(object): """Processes CMake lines for checking style.""" # NO_SPACE_CMDS list are based on commands section of CMake document. # Now it is generated from # http://www.cmake.org/cmake/help/v2.8.10/cmake.html#section_Commands. # Some commands are from default CMake modules such as pkg_check_modules. # Please keep list in alphabet order. # # For commands in this list, spaces should not be added it and its # parentheses. For eg, message("testing"), not message ("testing") # # The conditional commands like if, else, endif, foreach, endforeach, # while, endwhile and break are listed in ONE_SPACE_CMDS NO_SPACE_CMDS = [ 'add_custom_command', 'add_custom_target', 'add_definitions', 'add_dependencies', 'add_executable', 'add_library', 'add_subdirectory', 'add_test', 'aux_source_directory', 'build_command', 'cmake_minimum_required', 'cmake_policy', 'configure_file', 'create_test_sourcelist', 'define_property', 'enable_language', 'enable_testing', 'endfunction', 'endmacro', 'execute_process', 'export', 'file', 'find_file', 'find_library', 'find_package', 'find_path', 'find_program', 'fltk_wrap_ui', 'function', 'get_cmake_property', 'get_directory_property', 'get_filename_component', 'get_property', 'get_source_file_property', 'get_target_property', 'get_test_property', 'include', 'include_directories', 'include_external_msproject', 'include_regular_expression', 'install', 'link_directories', 'list', 'load_cache', 'load_command', 'macro', 'mark_as_advanced', 'math', 'message', 'option', #From FindPkgConfig.cmake 'pkg_check_modules', 'project', 'qt_wrap_cpp', 'qt_wrap_ui', 'remove_definitions', 'return', 'separate_arguments', 'set', 'set_directory_properties', 'set_property', 'set_source_files_properties', 'set_target_properties', 'set_tests_properties', 'site_name', 'source_group', 'string', 'target_link_libraries', 'try_compile', 'try_run', 'unset', 'variable_watch', ] # CMake conditional commands, require one space between command and # its parentheses, such as "if (", "foreach (", etc. ONE_SPACE_CMDS = [ 'if', 'else', 'elseif', 'endif', 'foreach', 'endforeach', 'while', 'endwhile', 'break', ] def __init__(self, file_path, handle_style_error): self._handle_style_error = handle_style_error self._tab_checker = TabChecker(file_path, handle_style_error) def check(self, lines): self._tab_checker.check(lines) self._num_lines = len(lines) for l in xrange(self._num_lines): self._process_line(l + 1, lines[l]) def _process_line(self, line_number, line_content): if re.match('(^|\ +)#', line_content): # ignore comment line return l = line_content.expandtabs(4) # check command like message( "testing") if re.search('\(\ +', l): self._handle_style_error(line_number, 'whitespace/parentheses', 5, 'No space after "("') # check command like message("testing" ) if re.search('\ +\)', l) and not re.search('^\ +\)$', l): self._handle_style_error(line_number, 'whitespace/parentheses', 5, 'No space before ")"') self._check_trailing_whitespace(line_number, l) self._check_no_space_cmds(line_number, l) self._check_one_space_cmds(line_number, l) self._check_indent(line_number, line_content) def _check_trailing_whitespace(self, line_number, line_content): line_content = line_content.rstrip('\n') # chr(10), newline line_content = line_content.rstrip('\r') # chr(13), carriage return line_content = line_content.rstrip('\x0c') # chr(12), form feed, ^L stripped = line_content.rstrip() if line_content != stripped: self._handle_style_error(line_number, 'whitespace/trailing', 5, 'No trailing spaces') def _check_no_space_cmds(self, line_number, line_content): # check command like "SET (" or "Set(" for t in self.NO_SPACE_CMDS: self._check_non_lowercase_cmd(line_number, line_content, t) if re.search('(^|\ +)' + t.lower() + '\ +\(', line_content): msg = 'No space between command "' + t.lower() + '" and its parentheses, should be "' + t + '("' self._handle_style_error(line_number, 'whitespace/parentheses', 5, msg) def _check_one_space_cmds(self, line_number, line_content): # check command like "IF (" or "if(" or "if (" or "If ()" for t in self.ONE_SPACE_CMDS: self._check_non_lowercase_cmd(line_number, line_content, t) if re.search('(^|\ +)' + t.lower() + '(\(|\ \ +\()', line_content): msg = 'One space between command "' + t.lower() + '" and its parentheses, should be "' + t + ' ("' self._handle_style_error(line_number, 'whitespace/parentheses', 5, msg) def _check_non_lowercase_cmd(self, line_number, line_content, cmd): if re.search('(^|\ +)' + cmd + '\ *\(', line_content, flags=re.IGNORECASE) and \ (not re.search('(^|\ +)' + cmd.lower() + '\ *\(', line_content)): msg = 'Use lowercase command "' + cmd.lower() + '"' self._handle_style_error(line_number, 'command/lowercase', 5, msg) def _check_indent(self, line_number, line_content): #TODO (halton): add indent checking pass
bsd-3-clause
JDReutt/libgdx
extensions/gdx-freetype/jni/freetype-2.5.5/src/tools/docmaker/docbeauty.py
877
2642
#!/usr/bin/env python # # DocBeauty (c) 2003, 2004, 2008 David Turner <[email protected]> # # This program is used to beautify the documentation comments used # in the FreeType 2 public headers. # from sources import * from content import * from utils import * import utils import sys, os, time, string, getopt content_processor = ContentProcessor() def beautify_block( block ): if block.content: content_processor.reset() markups = content_processor.process_content( block.content ) text = [] first = 1 for markup in markups: text.extend( markup.beautify( first ) ) first = 0 # now beautify the documentation "borders" themselves lines = [" /*************************************************************************"] for l in text: lines.append( " *" + l ) lines.append( " */" ) block.lines = lines def usage(): print "\nDocBeauty 0.1 Usage information\n" print " docbeauty [options] file1 [file2 ...]\n" print "using the following options:\n" print " -h : print this page" print " -b : backup original files with the 'orig' extension" print "" print " --backup : same as -b" def main( argv ): """main program loop""" global output_dir try: opts, args = getopt.getopt( sys.argv[1:], \ "hb", \ ["help", "backup"] ) except getopt.GetoptError: usage() sys.exit( 2 ) if args == []: usage() sys.exit( 1 ) # process options # output_dir = None do_backup = None for opt in opts: if opt[0] in ( "-h", "--help" ): usage() sys.exit( 0 ) if opt[0] in ( "-b", "--backup" ): do_backup = 1 # create context and processor source_processor = SourceProcessor() # retrieve the list of files to process file_list = make_file_list( args ) for filename in file_list: source_processor.parse_file( filename ) for block in source_processor.blocks: beautify_block( block ) new_name = filename + ".new" ok = None try: file = open( new_name, "wt" ) for block in source_processor.blocks: for line in block.lines: file.write( line ) file.write( "\n" ) file.close() except: ok = 0 # if called from the command line # if __name__ == '__main__': main( sys.argv ) # eof
apache-2.0
mancoast/CPythonPyc_test
fail/330_test_doctest2.py
194
2359
"""A module to test whether doctest recognizes some 2.2 features, like static and class methods. >>> print('yup') # 1 yup We include some (random) encoded (utf-8) text in the text surrounding the example. It should be ignored: ЉЊЈЁЂ """ import sys import unittest from test import support if sys.flags.optimize >= 2: raise unittest.SkipTest("Cannot test docstrings with -O2") class C(object): """Class C. >>> print(C()) # 2 42 We include some (random) encoded (utf-8) text in the text surrounding the example. It should be ignored: ЉЊЈЁЂ """ def __init__(self): """C.__init__. >>> print(C()) # 3 42 """ def __str__(self): """ >>> print(C()) # 4 42 """ return "42" class D(object): """A nested D class. >>> print("In D!") # 5 In D! """ def nested(self): """ >>> print(3) # 6 3 """ def getx(self): """ >>> c = C() # 7 >>> c.x = 12 # 8 >>> print(c.x) # 9 -12 """ return -self._x def setx(self, value): """ >>> c = C() # 10 >>> c.x = 12 # 11 >>> print(c.x) # 12 -12 """ self._x = value x = property(getx, setx, doc="""\ >>> c = C() # 13 >>> c.x = 12 # 14 >>> print(c.x) # 15 -12 """) @staticmethod def statm(): """ A static method. >>> print(C.statm()) # 16 666 >>> print(C().statm()) # 17 666 """ return 666 @classmethod def clsm(cls, val): """ A class method. >>> print(C.clsm(22)) # 18 22 >>> print(C().clsm(23)) # 19 23 """ return val def test_main(): from test import test_doctest2 EXPECTED = 19 f, t = support.run_doctest(test_doctest2) if t != EXPECTED: raise support.TestFailed("expected %d tests to run, not %d" % (EXPECTED, t)) # Pollute the namespace with a bunch of imported functions and classes, # to make sure they don't get tested. from doctest import * if __name__ == '__main__': test_main()
gpl-3.0
TamiaLab/carnetdumaker
apps/dbmutex/tests/test_models.py
1
1835
""" Tests suite for the database mutex app. """ from unittest import mock from datetime import timedelta from django.test import TestCase from django.utils import timezone from ..settings import MUTEX_LOCK_EXPIRATION_DELAY_SEC from ..models import DbMutexLock class DatabaseMutexTestCase(TestCase): """ Tests suite for the database mutex app. """ def test_str_method(self): """ Test the ``__str__`` method of the ``DbMutexLock`` class. """ mutex = DbMutexLock.objects.create(mutex_name='test') self.assertEqual(mutex.mutex_name, str(mutex)) def test_expired_method(self): """ Test the ``expired()`` method of the ``DbMutexLock`` class. """ now = timezone.now() with mock.patch('django.utils.timezone.now') as mock_now: mock_now.return_value = now mutex = DbMutexLock.objects.create(mutex_name='test') self.assertFalse(mutex.expired()) future_now = now + timedelta(seconds=MUTEX_LOCK_EXPIRATION_DELAY_SEC - 1) with mock.patch('django.utils.timezone.now') as mock_now: mock_now.return_value = future_now self.assertFalse(mutex.expired()) future_now = now + timedelta(seconds=MUTEX_LOCK_EXPIRATION_DELAY_SEC) with mock.patch('django.utils.timezone.now') as mock_now: mock_now.return_value = future_now self.assertTrue(mutex.expired()) def test_ordering(self): """ Test the default ordering of the lock. """ DbMutexLock.objects.create(mutex_name='test1') DbMutexLock.objects.create(mutex_name='test2') # Test the ordering queryset = DbMutexLock.objects.all() self.assertQuerysetEqual(queryset, ['<DbMutexLock: test2>', '<DbMutexLock: test1>'])
agpl-3.0
scripnichenko/nova
nova/virt/libvirt/compat.py
67
1411
# 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 oslo_log import log as logging from nova.i18n import _LW LOG = logging.getLogger(__name__) def get_domain_info(libvirt, host, virt_dom): """Method virDomain.info (libvirt version < 1.2.11) is affected by a race condition. See bug #1372670 for more details. This method detects it to perform a retry. """ def is_race(e): code = e.get_error_code() message = e.get_error_message() return (code == libvirt.VIR_ERR_OPERATION_FAILED and 'cannot read cputime for domain' in message) try: return virt_dom.info() except libvirt.libvirtError as e: if not host.has_min_version((1, 2, 11)) and is_race(e): LOG.warn(_LW('Race detected in libvirt.virDomain.info, ' 'trying one more time')) return virt_dom.info() raise
apache-2.0
hzwjava/mongo-connector
mongo_connector/config.py
21
5216
# Copyright 2013-2014 MongoDB, 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. import json import logging import optparse import sys from mongo_connector import compat, errors from mongo_connector.compat import reraise def default_apply_function(option, cli_values): first_value = list(cli_values.values())[0] if first_value is not None: option.value = first_value class Option(object): """A config file option which can be overwritten on the command line. config_key is the corresponding field in the JSON config file. apply_function has the following signature: def apply_function(option, cli_values): # modify option.value ... When apply_function is invoked, option.value will be set to the value given in the config file (or the default value). apply_function reads the cli_values and modifies option.value accordingly """ def __init__(self, config_key=None, default=None, type=None, apply_function=default_apply_function): self.config_key = config_key self.value = default self.type = type self.apply_function = apply_function self.cli_names = [] self.cli_options = [] def validate_type(self): if self.type == str: return compat.is_string(self.value) else: return isinstance(self.value, self.type) def add_cli(self, *args, **kwargs): """Add a command line argument. All of the given arguments will be passed directly to optparse.OptionParser().add_option """ self.cli_options.append((args, kwargs)) class Config(object): """Manages command line application configuration. conf = Config(options) conf.parse_args() value = conf['key'] value2 = conf['key1.key2'] # same as conf['key1']['key2'] """ def __init__(self, options): self.options = options self.config_key_to_option = dict( [(option.config_key, option) for option in self.options]) def parse_args(self, argv=None): """Parses command line arguments from stdin (or given argv). Does the following: 1. Parses command line arguments 2. Loads config file into options (if config file specified) 3. calls option.apply_function with the parsed cli_values """ # parse the command line options parser = optparse.OptionParser() for option in self.options: for args, kwargs in option.cli_options: cli_option = parser.add_option(*args, **kwargs) option.cli_names.append(cli_option.dest) parsed_options, args = parser.parse_args(argv) if args: raise errors.InvalidConfiguration( 'The following command line arguments are not recognized: ' + ', '.join(args)) # load the config file if parsed_options.config_file: try: with open(parsed_options.config_file) as f: self.load_json(f.read()) except (OSError, IOError, ValueError): reraise(errors.InvalidConfiguration, *sys.exc_info()[1:]) # apply the command line arguments values = parsed_options.__dict__ for option in self.options: option.apply_function( option, dict((k, values.get(k)) for k in option.cli_names)) def __getitem__(self, key): keys = key.split('.') cur = self.config_key_to_option[keys[0]].value for k in keys[1:]: if cur is not None: if isinstance(cur, dict): cur = cur.get(k) else: cur = None return cur def load_json(self, text): parsed_config = json.loads(text) for k in parsed_config: option = self.config_key_to_option.get(k) if option: # load into option.value if isinstance(parsed_config[k], dict): for k2 in parsed_config[k]: option.value[k2] = parsed_config[k][k2] else: option.value = parsed_config[k] # type check if not option.validate_type(): raise errors.InvalidConfiguration( "%s should be a %r, %r was given!" % (option.config_key, option.type.__name__, type(option.value).__name__)) else: if not k.startswith("__"): logging.warning("Unrecognized option: %s" % k)
apache-2.0
pozdnyakov/chromium-crosswalk
third_party/jinja2/visitor.py
1401
3316
# -*- coding: utf-8 -*- """ jinja2.visitor ~~~~~~~~~~~~~~ This module implements a visitor for the nodes. :copyright: (c) 2010 by the Jinja Team. :license: BSD. """ from jinja2.nodes import Node class NodeVisitor(object): """Walks the abstract syntax tree and call visitor functions for every node found. The visitor functions may return values which will be forwarded by the `visit` method. Per default the visitor functions for the nodes are ``'visit_'`` + class name of the node. So a `TryFinally` node visit function would be `visit_TryFinally`. This behavior can be changed by overriding the `get_visitor` function. If no visitor function exists for a node (return value `None`) the `generic_visit` visitor is used instead. """ def get_visitor(self, node): """Return the visitor function for this node or `None` if no visitor exists for this node. In that case the generic visit function is used instead. """ method = 'visit_' + node.__class__.__name__ return getattr(self, method, None) def visit(self, node, *args, **kwargs): """Visit a node.""" f = self.get_visitor(node) if f is not None: return f(node, *args, **kwargs) return self.generic_visit(node, *args, **kwargs) def generic_visit(self, node, *args, **kwargs): """Called if no explicit visitor function exists for a node.""" for node in node.iter_child_nodes(): self.visit(node, *args, **kwargs) class NodeTransformer(NodeVisitor): """Walks the abstract syntax tree and allows modifications of nodes. The `NodeTransformer` will walk the AST and use the return value of the visitor functions to replace or remove the old node. If the return value of the visitor function is `None` the node will be removed from the previous location otherwise it's replaced with the return value. The return value may be the original node in which case no replacement takes place. """ def generic_visit(self, node, *args, **kwargs): for field, old_value in node.iter_fields(): if isinstance(old_value, list): new_values = [] for value in old_value: if isinstance(value, Node): value = self.visit(value, *args, **kwargs) if value is None: continue elif not isinstance(value, Node): new_values.extend(value) continue new_values.append(value) old_value[:] = new_values elif isinstance(old_value, Node): new_node = self.visit(old_value, *args, **kwargs) if new_node is None: delattr(node, field) else: setattr(node, field, new_node) return node def visit_list(self, node, *args, **kwargs): """As transformers may return lists in some places this method can be used to enforce a list as return value. """ rv = self.visit(node, *args, **kwargs) if not isinstance(rv, list): rv = [rv] return rv
bsd-3-clause
cloudera/impyla
impala/tests/test_hive_dict_cursor.py
2
1848
# Copyright 2015 Cloudera 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 pytest import yield_fixture @yield_fixture(scope='session') def cur2(con): cur = con.cursor(dictify=True) yield cur cur.close() def test_dict_cursor(cur2): cur = cur2 cur.execute('CREATE TABLE tmp_hive (a STRING, b INT, c DOUBLE)') cur.execute('SHOW TABLES') tables = cur.fetchall() assert any(t['name'] == 'tmp_hive' for t in tables) cur.execute("INSERT INTO tmp_hive " "VALUES ('foo', 1, 0.5), ('bar', 2, NULL), ('baz', 3, 6.2)") cur.execute('SELECT b FROM tmp_hive LIMIT 2') assert len(cur.description) == 1 assert cur.description[0][0] == 'b' results = cur.fetchall() assert len(results) == 2 cur.execute('SELECT * FROM tmp_hive WHERE c IS NOT NULL') results = cur.fetchall() assert len(results) == 2 cur.execute("SELECT c from tmp_hive WHERE a = 'foo'") results = cur.fetchall() assert len(results) == 1 assert results[0]['c'] == 0.5 cur.execute("SELECT c from tmp_hive WHERE a = 'bar'") results = cur.fetchall() assert len(results) == 1 assert results[0]['c'] is None cur.execute('DROP TABLE tmp_hive') cur.execute('SHOW TABLES') tables = cur.fetchall() assert not any(t['tableName'] == 'tmp_hive' for t in tables)
apache-2.0
kirillzhuravlev/numpy
numpy/f2py/tests/test_return_character.py
130
3967
from __future__ import division, absolute_import, print_function from numpy import array from numpy.compat import asbytes from numpy.testing import run_module_suite, assert_, dec import util class TestReturnCharacter(util.F2PyTest): def check_function(self, t): tname = t.__doc__.split()[0] if tname in ['t0', 't1', 's0', 's1']: assert_(t(23) == asbytes('2')) r = t('ab') assert_(r == asbytes('a'), repr(r)) r = t(array('ab')) assert_(r == asbytes('a'), repr(r)) r = t(array(77, 'u1')) assert_(r == asbytes('M'), repr(r)) #assert_(_raises(ValueError, t, array([77,87]))) #assert_(_raises(ValueError, t, array(77))) elif tname in ['ts', 'ss']: assert_(t(23) == asbytes('23 '), repr(t(23))) assert_(t('123456789abcdef') == asbytes('123456789a')) elif tname in ['t5', 's5']: assert_(t(23) == asbytes('23 '), repr(t(23))) assert_(t('ab') == asbytes('ab '), repr(t('ab'))) assert_(t('123456789abcdef') == asbytes('12345')) else: raise NotImplementedError class TestF77ReturnCharacter(TestReturnCharacter): code = """ function t0(value) character value character t0 t0 = value end function t1(value) character*1 value character*1 t1 t1 = value end function t5(value) character*5 value character*5 t5 t5 = value end function ts(value) character*(*) value character*(*) ts ts = value end subroutine s0(t0,value) character value character t0 cf2py intent(out) t0 t0 = value end subroutine s1(t1,value) character*1 value character*1 t1 cf2py intent(out) t1 t1 = value end subroutine s5(t5,value) character*5 value character*5 t5 cf2py intent(out) t5 t5 = value end subroutine ss(ts,value) character*(*) value character*10 ts cf2py intent(out) ts ts = value end """ @dec.slow def test_all(self): for name in "t0,t1,t5,s0,s1,s5,ss".split(","): self.check_function(getattr(self.module, name)) class TestF90ReturnCharacter(TestReturnCharacter): suffix = ".f90" code = """ module f90_return_char contains function t0(value) character :: value character :: t0 t0 = value end function t0 function t1(value) character(len=1) :: value character(len=1) :: t1 t1 = value end function t1 function t5(value) character(len=5) :: value character(len=5) :: t5 t5 = value end function t5 function ts(value) character(len=*) :: value character(len=10) :: ts ts = value end function ts subroutine s0(t0,value) character :: value character :: t0 !f2py intent(out) t0 t0 = value end subroutine s0 subroutine s1(t1,value) character(len=1) :: value character(len=1) :: t1 !f2py intent(out) t1 t1 = value end subroutine s1 subroutine s5(t5,value) character(len=5) :: value character(len=5) :: t5 !f2py intent(out) t5 t5 = value end subroutine s5 subroutine ss(ts,value) character(len=*) :: value character(len=10) :: ts !f2py intent(out) ts ts = value end subroutine ss end module f90_return_char """ @dec.slow def test_all(self): for name in "t0,t1,t5,ts,s0,s1,s5,ss".split(","): self.check_function(getattr(self.module.f90_return_char, name)) if __name__ == "__main__": run_module_suite()
bsd-3-clause
Netflix/repokid
repokid/lib/__init__.py
1
9989
# Copyright 2020 Netflix, 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. """ This module contains wrapper functions for the functions contained in the child modules so developers don't have to worry about passing configs, hooks, and dynamo clients. """ from typing import List from repokid import CONFIG from repokid import get_hooks from repokid.commands.repo import _repo_all_roles from repokid.commands.repo import _repo_role from repokid.commands.repo import _repo_stats from repokid.commands.repo import _rollback_role from repokid.commands.role import _display_role from repokid.commands.role import _display_roles from repokid.commands.role import _find_roles_with_permissions from repokid.commands.role import _remove_permissions_from_roles from repokid.commands.role_cache import _update_role_cache from repokid.commands.schedule import _cancel_scheduled_repo from repokid.commands.schedule import _schedule_repo from repokid.commands.schedule import _show_scheduled_roles hooks = get_hooks(CONFIG.get("hooks", ["repokid.hooks.loggers"])) def update_role_cache(account_number: str) -> None: """ Library wrapper to update data about all roles in a given account. Ref: :func:`~repokid.commands.role_cache._update_role_cache` Args: account_number (string): The current account number Repokid is being run against Returns: None """ return _update_role_cache(account_number, CONFIG, hooks) def display_role_cache(account_number: str, inactive: bool = False) -> None: """ Library wrapper to display a table with data about all roles in an account and write a csv file with the data. Ref: :func:`~repokid.commands.role_cache._display_roles` Args: account_number (string): The current account number Repokid is being run against inactive (bool): show roles that have historically (but not currently) existed in the account if True Returns: None """ return _display_roles(account_number, inactive=inactive) def find_roles_with_permissions(permissions: List[str], output_file: str = "") -> None: """ Library wrapper to search roles in all accounts for a policy with any of the provided permissions, log the ARN of each role. Ref: :func:`~repokid.commands.role._find_roles_with_permissions` Args: permissions (list[string]): The name of the permissions to find output_file (string): filename to write the output Returns: None """ return _find_roles_with_permissions(permissions, output_file) def remove_permissions_from_roles( permissions: List[str], role_filename: str, commit: bool = False ) -> None: """ Library wrapper to loads role specified in file and call _remove_permissions_from_role() for each one. Ref: :func:`~repokid.commands.role._remove_permissions_from_roles` Args: permissions (list<string>) role_filename (string) commit (bool) Returns: None """ return _remove_permissions_from_roles( permissions, role_filename, CONFIG, hooks, commit=commit ) def display_role(account_number: str, role_name: str) -> None: """ Library wrapper to display data about a role in a given account Ref: :func:`~repokid.commands.role._display_role` Args: account_number (string): The current account number Repokid is being run against role_name (string) Returns: None """ return _display_role(account_number, role_name, CONFIG) def repo_role( account_number: str, role_name: str, commit: bool = False, update: bool = True ) -> List[str]: """ Library wrapper to calculate what repoing can be done for a role and then actually do it if commit is set. Ref: :func:`~repokid.commands.repo._repo_role` Args: account_number (string): The current account number Repokid is being run against role_name (string) commit (bool) update (bool) Returns: errors (list): if any """ return _repo_role(account_number, role_name, CONFIG, hooks, commit=commit) def rollback_role( account_number: str, role_name: str, selection: int = 0, commit: bool = False ) -> List[str]: """ Library wrapper to display the historical policy versions for a roll as a numbered list. Restore to a specific version if selected. Indicate changes that will be made and then actually make them if commit is selected. Ref: :func:`~repokid.commands.repo._rollback_role` Args: account_number (string): The current account number Repokid is being run against role_name (string) selection (int): which policy version in the list to rollback to commit (bool): actually make the change Returns: errors (list): if any """ return _rollback_role( account_number, role_name, CONFIG, hooks, selection=selection, commit=commit ) def schedule_repo(account_number: str) -> None: """ Library wrapper to schedule a repo for a given account. Schedule repo for a time in the future (default 7 days) for any roles in the account with repoable permissions. Ref: :func:`~repokid.commands.repo._repo_all_roles` Args: account_number (string): The current account number Repokid is being run against Returns: None """ _update_role_cache(account_number, CONFIG, hooks) return _schedule_repo(account_number, CONFIG, hooks) def repo_all_roles( account_number: str, commit: bool = False, update: bool = True, limit: int = -1 ) -> None: """ Convenience wrapper for repo_roles() with scheduled=False. Ref: :func:`~repokid.commands.repo_roles` Args: account_number (string): The current account number Repokid is being run against commit (bool): actually make the changes update (bool): if True run update_role_cache before repoing limit (int): limit number of roles to be repoed per run (< 0 is unlimited) Returns: None """ return repo_roles( account_number, commit=commit, scheduled=False, update=update, limit=limit ) def repo_scheduled_roles( account_number: str, commit: bool = False, update: bool = True, limit: int = -1 ) -> None: """ Convenience wrapper for repo_roles() with scheduled=True. Ref: :func:`~repokid.commands.repo_roles` Args: account_number (string): The current account number Repokid is being run against commit (bool): actually make the changes update (bool): if True run update_role_cache before repoing limit (int): limit number of roles to be repoed per run (< 0 is unlimited) Returns: None """ return repo_roles( account_number, commit=commit, scheduled=True, update=update, limit=limit ) def repo_roles( account_number: str, commit: bool = False, scheduled: bool = False, update: bool = True, limit: int = -1, ) -> None: """ Library wrapper to repo all scheduled or eligible roles in an account. Collect any errors and display them at the end. Ref: :func:`~repokid.commands.repo._repo_all_roles` Args: account_number (string): The current account number Repokid is being run against commit (bool): actually make the changes scheduled (bool): if True only repo the scheduled roles, if False repo all the (eligible) roles update (bool): if True run update_role_cache before repoing limit (int): limit number of roles to be repoed per run (< 0 is unlimited) Returns: None """ if update: _update_role_cache(account_number, CONFIG, hooks) return _repo_all_roles( account_number, CONFIG, hooks, commit=commit, scheduled=scheduled, limit=limit ) def show_scheduled_roles(account_number: str) -> None: """ Library wrapper to show scheduled repos for a given account. For each scheduled show whether scheduled time is elapsed or not. Ref: :func:`~repokid.commands.schedule._show_scheduled_roles` Args: account_number (string): The current account number Repokid is being run against Returns: None """ return _show_scheduled_roles(account_number) def cancel_scheduled_repo( account_number: str, role_name: str = "", is_all: bool = False ) -> None: """ Library wrapper to cancel scheduled repo for a role in an account. Ref: :func:`~repokid.commands.schedule._cancel_scheduled_repo` Args: account_number (string): The current account number Repokid is being run against role_name (string): Role name to cancel scheduled repo for is_all (bool): Cancel schedule repos on all roles if True Returns: None """ return _cancel_scheduled_repo(account_number, role_name=role_name, is_all=is_all) def repo_stats(output_filename: str = "", account_number: str = "") -> None: """ Library wrapper to create a csv file with stats about roles, total permissions, and applicable filters over time. Ref: :func:`~repokid.commands.repo._repo_stats` Args: output_filename (string): the name of the csv file to write account_number (string): if specified only display roles from selected account, otherwise display all Returns: None """ return _repo_stats(output_filename, account_number=account_number)
apache-2.0
szeged/servo
tests/power/PowerMeasure.py
15
7012
#!/usr/bin/env python # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at https://mozilla.org/MPL/2.0/. # ---------Power measurement ------------------------------# # This script will run the servo with the given benchmark and # get the power usage using Powermetrics. Results will be put # in sperate files with that name. # Do not forget to run the script in servo/tests/power folder # --------------------------------------------------------# from __future__ import print_function, unicode_literals import os from os import path import time import argparse # ------------------------PowerCollector----------------------------# # Collecting all the power data and put them into files TOP_DIR = path.join("..", "..") def PowerCollector(OutputDir, Benchmarks, LayoutThreads, Renderer): print("Running the power collector") power_dir = path.join(OutputDir, "power") time_dir = path.join(OutputDir, "time") etc_dir = path.join(OutputDir, "etc") for d in [power_dir, time_dir, etc_dir]: os.mkdir(d) SleepTime = 20 GuardTime = 0.5 powerTiming = 1 ExperimentNum = 21 for ExpNum in range(1, ExperimentNum): for layoutT in range(1, LayoutThreads + 1): print(" layoutT=%d ExpNum=%d" % (layoutT, ExpNum)) PowerFiles = path.join( power_dir, "power-Layout%d-set%d.csv" % (layoutT, ExpNum)) TimeFiles = path.join( time_dir, "time-Layout%d-set%d.csv" % (layoutT, ExpNum)) ServoCmd = "(time ../../target/release/servo -x -y %d %s %s) 2> %s" % \ (layoutT, Renderer, Benchmarks, TimeFiles) Metrics = path.join( etc_dir, "metrics-Layout%d-set%d-css.csv" % (layoutT, ExpNum)) cmd = "(sudo powermetrics -i %d | " \ "grep \"energy\\|elapsed\\|servo\" > %s &_) 2> %s" % \ (powerTiming, PowerFiles, Metrics) time.sleep(SleepTime) os.system(cmd) time.sleep(GuardTime) os.system(ServoCmd) time.sleep(GuardTime) os.system('sudo pkill -9 powermetrics') time.sleep(SleepTime) # -------------------PowerParser ---------------------------------# # Parsing collected power by PowerCollector fucntion def PowerParser(OutputDir, LayoutThreads): print("Running the PowerParser") ExperimentNum = 21 ResultTable = OutputDir + "ResultTable.csv" ResultFile = open(ResultTable, "w") ResultFile.write("LayoutThreads, MeanPower, MaxPower , MinPower, MeanTime , MaxTime, " "MinTime \n") for layoutT in range(1, LayoutThreads + 1): MaxTime = 0 MinTime = 1000000 MaxPower = 0 MinPower = 1000000 TotalPower = 0 TotalTime = 0 TimeGen = 0 PowerGen = 0 for ExpNum in range(1, ExperimentNum): print(" layoutT=%d ExpNum=%d" % (layoutT, ExpNum)) Files = path.join( OutputDir, "power", "power-Layout%d-set%d.csv" % (layoutT, ExpNum)) NewFile = path.join(OutputDir, "power", "Servo-Layout%d-set%d.csv" % (layoutT, ExpNum)) File = open(Files, 'r') PowerFile = open(NewFile, 'w') TimeFiles = path.join(OutputDir, "time", "time-Layout%d-set%d.csv" % (layoutT, ExpNum)) # ----Putting the power the power and its time into a table---- # for line in File: words = line.split() if words[0] == "***": insertingWord = words[10][1:-2] + " " elif words[0] == "Intel": insertingWord += words[7][:-1] insertingWord += "\n" PowerFile.write(insertingWord) File.close() PowerFile.close() # ---------------geting the total power of experiments-------- # TempFile = open(NewFile, 'r') Power = 0 for line in TempFile: words2 = line.split() Power += float(words2[0]) * float(words2[1]) TotalPower = float(Power / 1000.0) if TotalPower > MaxPower: MaxPower = TotalPower if TotalPower < MinPower: MinPower = TotalPower # -------------getting the total time of execution---------- # TempFile2 = open(TimeFiles, "r") for line in TempFile2: words3 = line.split() if line != "\n" and words3[0] == "real": TotalTime = (float(words3[1][0]) * 60) + \ float(words3[1][2:-1]) if TotalTime > MaxTime: MaxTime = TotalTime if TotalTime < MinTime: MinTime = TotalTime TimeGen = TimeGen + TotalTime PowerGen = PowerGen + TotalPower TotalPower = PowerGen / float(ExperimentNum - 1) TotalTime = TimeGen / float(ExperimentNum - 1) ResultFile.write(str(layoutT) + " , " + str(TotalPower) + " , " + str(MaxPower) + " , " + str(MinPower) + " , " + str(TotalTime) + " , " + str(MaxTime) + " , " + str(MinTime) + "\n") ResultFile.close() Opener = ResultFile = open(ResultTable, "r") for line in Opener: print(line) print("Also you can find all the numbers for Power " "and Performance in : ", ResultTable) # ----------------------------------------------------# def main(): LayoutThreads = 8 # Maximum number of threads considered for Layout Benchmarks = path.join(TOP_DIR, "tests", "html", "perf-rainbow.html") OutputDir = "Experiments" os.mkdir(OutputDir) Renderer = "" # Parsing the input of the script parser = argparse.ArgumentParser(description="Measuring \ power and performance of your Servo runs") parser.add_argument("-b", "--benchmark", help="Gets the \ benchmark, for example \"-B perf-rainbow.html\"") parser.add_argument("-c", "--CPU", help="Rendering with \ CPU instead of GPU, for example -C") parser.add_argument("-l", "--LayoutThreads", help="Specify \ the maximum number of threads for layout, for example \" -L 5\"") parser.add_argument("-o", "--Output", help="Specify \ the output directory") args = parser.parse_args() if args.benchmark: Benchmarks = args.benchmark if args.CPU: Renderer = "-c" if args.LayoutThreads: LayoutThreads = int(args.LayoutThreads) if args.Output: OutputDir = args.Output PowerCollector(OutputDir, Benchmarks, LayoutThreads, Renderer) PowerParser(OutputDir, LayoutThreads) if __name__ == "__main__": main()
mpl-2.0
MDAnalysis/pmda
pmda/rms/rmsf.py
1
8614
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*- # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 # # PMDA # Copyright (c) 2019 The MDAnalysis Development Team and contributors # (see the file AUTHORS for the full list of names) # # Released under the GNU Public Licence, v2 or any higher version """ Calculating Root-Mean-Square Fluctuations (RMSF) --- :mod:`pmda.rmsf` ===================================================================== This module contains parallel versions of analysis tasks in :mod:`MDAnalysis.analysis.rms`. .. autoclass:: RMSF :members: :inherited-members: See Also -------- MDAnalysis.analysis.rms.RMSF """ from __future__ import absolute_import, division import numpy as np from pmda.parallel import ParallelAnalysisBase from pmda.util import fold_second_order_moments class RMSF(ParallelAnalysisBase): r"""Parallel RMSF analysis. Calculates RMSF of given atoms across a trajectory. Attributes ---------- rmsf : array ``N``-length :class:`numpy.ndarray` array of RMSF values, where ``N`` is the number of atoms in the `atomgroup` of interest. Returned values have units of ångströms. Parameters ---------- atomgroup : AtomGroup Atoms for which RMSF is calculated Raises ------ ValueError raised if negative values are calculated, which indicates that a numerical overflow or underflow occured See Also -------- MDAnalysis.analysis.rms.RMSF Notes ----- No RMSD-superposition is performed; it is assumed that the user is providing a trajectory where the protein of interest has been structurally aligned to a reference structure (see the Examples section below). The protein also has be whole because periodic boundaries are not taken into account. Run the analysis with :meth:`RMSF.run`, which stores the results in the array :attr:`RMSF.rmsf`. The root mean square fluctuation of an atom :math:`i` is computed as the time average: .. math:: \sigma_{i} = \sqrt{\left\langle (\mathbf{x}_{i} - \langle\mathbf{x}_{i}\rangle)^2 \right\rangle} No mass weighting is performed. This method implements an algorithm for computing sums of squares while avoiding overflows and underflows [Welford1962]_, as well as an algorithm for combining the sum of squares and means of separate partitions of a given trajectory to calculate the RMSF of the entire trajectory [CGL1979]_. For more details about RMSF calculations, refer to [Awtrey2019]_. References ---------- .. [Welford1962] B. P. Welford (1962). "Note on a Method for Calculating Corrected Sums of Squares and Products." Technometrics 4(3):419-420. Examples -------- In this example we calculate the residue RMSF fluctuations by analyzing the :math:`\text{C}_\alpha` atoms. First we need to fit the trajectory to the average structure as a reference. That requires calculating the average structure first. Because we need to analyze and manipulate the same trajectory multiple times, we are going to load it into memory using the :mod:`~MDAnalysis.coordinates.MemoryReader`. (If your trajectory does not fit into memory, you will need to :ref:`write out intermediate trajectories <writing-trajectories>` to disk or :ref:`generate an in-memory universe <creating-in-memory-trajectory-label>` that only contains, say, the protein):: import MDAnalysis as mda from MDAnalysis.analysis import align from MDAnalysis.tests.datafiles import TPR, XTC u = mda.Universe(TPR, XTC, in_memory=True) protein = u.select_atoms("protein") # TODO: Need to center and make whole (this test trajectory # contains the protein being split across periodic boundaries # and the results will be WRONG!) # Fit to the initial frame to get a better average structure # (the trajectory is changed in memory) prealigner = align.AlignTraj(u, u, select="protein and name CA", in_memory=True).run() # ref = average structure ref_coordinates = u.trajectory.timeseries(asel=protein).mean(axis=1) # Make a reference structure (need to reshape into a # 1-frame "trajectory"). ref = mda.Merge(protein).load_new(ref_coordinates[:, None, :], order="afc") We created a new universe ``reference`` that contains a single frame with the averaged coordinates of the protein. Now we need to fit the whole trajectory to the reference by minimizing the RMSD. We use :class:`MDAnalysis.analysis.align.AlignTraj`:: aligner = align.AlignTraj(u, ref, select="protein and name CA", in_memory=True).run() # need to write the trajectory to disk for PMDA 0.3.0 (see issue #15) with mda.Writer("rmsfit.xtc", n_atoms=u.atoms.n_atoms) as W: for ts in u.trajectory: W.write(u.atoms) (For use with PMDA we cannot currently use a in-memory trajectory (see `Issue #15`_) so we must write out the RMS-superimposed trajectory to the file "rmsfit.xtc".) The trajectory is now fitted to the reference (the RMSD is stored as `aligner.rmsd` for further inspection). Now we can calculate the RMSF:: from pmda.rms import RMSF u = mda.Universe(TPR, "rmsfit.xtc") calphas = protein.select_atoms("protein and name CA") rmsfer = RMSF(calphas).run() and plot:: import matplotlib.pyplot as plt plt.plot(calphas.resnums, rmsfer.rmsf) .. versionadded:: 0.3.0 .. _`Issue #15`: https://github.com/MDAnalysis/pmda/issues/15 """ def __init__(self, atomgroup): u = atomgroup.universe super(RMSF, self).__init__(u, (atomgroup, )) self._atomgroup = atomgroup self._top = u.filename self._traj = u.trajectory.filename def _single_frame(self, ts, atomgroups): # mean and sum of squares calculations done in _reduce() return atomgroups[0] def _conclude(self): """ self._results : Array (n_blocks x 2 x N x 3) array """ n_blocks = len(self._results) # serial case if n_blocks == 1: # get length of trajectory slice self.mean = self._results[0, 0] self.sumsquares = self._results[0, 1] self.rmsf = np.sqrt(self.sumsquares.sum(axis=1) / self.n_frames) # parallel case else: mean = self._results[:, 0] sos = self._results[:, 1] # create list of (timesteps, mean, sumsq tuples for each block vals = [] for i in range(n_blocks): vals.append((len(self._blocks[i]), mean[i], sos[i])) # combine block results using fold method results = fold_second_order_moments(vals) self.mean = results[1] self.sumsquares = results[2] self.rmsf = np.sqrt(self.sumsquares.sum(axis=1) / self.n_frames) self._negative_rmsf(self.rmsf) @staticmethod def _reduce(res, result_single_frame): """ 'sum' action for time series """ atoms = result_single_frame positions = atoms.positions.astype(np.float64) # initial time step case if isinstance(res, list) and len(res) == 0: # initial mean position = initial position mean = positions # create new zero-array for sum of squares to prevent blocks from # using data from previous blocks sumsq = np.zeros((atoms.n_atoms, 3)) # set initial time step for each block to zero k = 0 # assign initial (sum of squares and mean) zero-arrays to res res = [mean, sumsq, k] else: # update time step k = res[2] + 1 # update sum of squares res[1] += (k / (k + 1)) * (positions - res[0]) ** 2 # update mean res[0] = (k * res[0] + positions) / (k + 1) # update time step in res res[2] = k return res @staticmethod def _negative_rmsf(rmsf): if not (rmsf >= 0).all(): raise ValueError("Some RMSF values negative; overflow " + "or underflow occurred")
gpl-2.0
idea4bsd/idea4bsd
python/helpers/profiler/ply/ctokens.py
197
3177
# ---------------------------------------------------------------------- # ctokens.py # # Token specifications for symbols in ANSI C and C++. This file is # meant to be used as a library in other tokenizers. # ---------------------------------------------------------------------- # Reserved words tokens = [ # Literals (identifier, integer constant, float constant, string constant, char const) 'ID', 'TYPEID', 'INTEGER', 'FLOAT', 'STRING', 'CHARACTER', # Operators (+,-,*,/,%,|,&,~,^,<<,>>, ||, &&, !, <, <=, >, >=, ==, !=) 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'MODULO', 'OR', 'AND', 'NOT', 'XOR', 'LSHIFT', 'RSHIFT', 'LOR', 'LAND', 'LNOT', 'LT', 'LE', 'GT', 'GE', 'EQ', 'NE', # Assignment (=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=) 'EQUALS', 'TIMESEQUAL', 'DIVEQUAL', 'MODEQUAL', 'PLUSEQUAL', 'MINUSEQUAL', 'LSHIFTEQUAL','RSHIFTEQUAL', 'ANDEQUAL', 'XOREQUAL', 'OREQUAL', # Increment/decrement (++,--) 'INCREMENT', 'DECREMENT', # Structure dereference (->) 'ARROW', # Ternary operator (?) 'TERNARY', # Delimeters ( ) [ ] { } , . ; : 'LPAREN', 'RPAREN', 'LBRACKET', 'RBRACKET', 'LBRACE', 'RBRACE', 'COMMA', 'PERIOD', 'SEMI', 'COLON', # Ellipsis (...) 'ELLIPSIS', ] # Operators t_PLUS = r'\+' t_MINUS = r'-' t_TIMES = r'\*' t_DIVIDE = r'/' t_MODULO = r'%' t_OR = r'\|' t_AND = r'&' t_NOT = r'~' t_XOR = r'\^' t_LSHIFT = r'<<' t_RSHIFT = r'>>' t_LOR = r'\|\|' t_LAND = r'&&' t_LNOT = r'!' t_LT = r'<' t_GT = r'>' t_LE = r'<=' t_GE = r'>=' t_EQ = r'==' t_NE = r'!=' # Assignment operators t_EQUALS = r'=' t_TIMESEQUAL = r'\*=' t_DIVEQUAL = r'/=' t_MODEQUAL = r'%=' t_PLUSEQUAL = r'\+=' t_MINUSEQUAL = r'-=' t_LSHIFTEQUAL = r'<<=' t_RSHIFTEQUAL = r'>>=' t_ANDEQUAL = r'&=' t_OREQUAL = r'\|=' t_XOREQUAL = r'\^=' # Increment/decrement t_INCREMENT = r'\+\+' t_DECREMENT = r'--' # -> t_ARROW = r'->' # ? t_TERNARY = r'\?' # Delimeters t_LPAREN = r'\(' t_RPAREN = r'\)' t_LBRACKET = r'\[' t_RBRACKET = r'\]' t_LBRACE = r'\{' t_RBRACE = r'\}' t_COMMA = r',' t_PERIOD = r'\.' t_SEMI = r';' t_COLON = r':' t_ELLIPSIS = r'\.\.\.' # Identifiers t_ID = r'[A-Za-z_][A-Za-z0-9_]*' # Integer literal t_INTEGER = r'\d+([uU]|[lL]|[uU][lL]|[lL][uU])?' # Floating literal t_FLOAT = r'((\d+)(\.\d+)(e(\+|-)?(\d+))? | (\d+)e(\+|-)?(\d+))([lL]|[fF])?' # String literal t_STRING = r'\"([^\\\n]|(\\.))*?\"' # Character constant 'c' or L'c' t_CHARACTER = r'(L)?\'([^\\\n]|(\\.))*?\'' # Comment (C-Style) def t_COMMENT(t): r'/\*(.|\n)*?\*/' t.lexer.lineno += t.value.count('\n') return t # Comment (C++-Style) def t_CPPCOMMENT(t): r'//.*\n' t.lexer.lineno += 1 return t
apache-2.0
hiroakis/ansible
lib/ansible/plugins/test/math.py
33
1056
# (c) 2014, Brian Coca <[email protected]> # # 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/>. from __future__ import absolute_import import math from ansible import errors def isnotanumber(x): try: return math.isnan(x) except TypeError: return False class TestModule(object): ''' Ansible math jinja2 tests ''' def tests(self): return { # general math 'isnan': isnotanumber, }
gpl-3.0
aESeguridad/GERE
venv/local/lib/python2.7/abc.py
488
7145
# Copyright 2007 Google, Inc. All Rights Reserved. # Licensed to PSF under a Contributor Agreement. """Abstract Base Classes (ABCs) according to PEP 3119.""" import types from _weakrefset import WeakSet # Instance of old-style class class _C: pass _InstanceType = type(_C()) def abstractmethod(funcobj): """A decorator indicating abstract methods. Requires that the metaclass is ABCMeta or derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods are overridden. The abstract methods can be called using any of the normal 'super' call mechanisms. Usage: class C: __metaclass__ = ABCMeta @abstractmethod def my_abstract_method(self, ...): ... """ funcobj.__isabstractmethod__ = True return funcobj class abstractproperty(property): """A decorator indicating abstract properties. Requires that the metaclass is ABCMeta or derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract properties are overridden. The abstract properties can be called using any of the normal 'super' call mechanisms. Usage: class C: __metaclass__ = ABCMeta @abstractproperty def my_abstract_property(self): ... This defines a read-only property; you can also define a read-write abstract property using the 'long' form of property declaration: class C: __metaclass__ = ABCMeta def getx(self): ... def setx(self, value): ... x = abstractproperty(getx, setx) """ __isabstractmethod__ = True class ABCMeta(type): """Metaclass for defining Abstract Base Classes (ABCs). Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class. You can also register unrelated concrete classes (even built-in classes) and unrelated ABCs as 'virtual subclasses' -- these and their descendants will be considered subclasses of the registering ABC by the built-in issubclass() function, but the registering ABC won't show up in their MRO (Method Resolution Order) nor will method implementations defined by the registering ABC be callable (not even via super()). """ # A global counter that is incremented each time a class is # registered as a virtual subclass of anything. It forces the # negative cache to be cleared before its next use. _abc_invalidation_counter = 0 def __new__(mcls, name, bases, namespace): cls = super(ABCMeta, mcls).__new__(mcls, name, bases, namespace) # Compute set of abstract method names abstracts = set(name for name, value in namespace.items() if getattr(value, "__isabstractmethod__", False)) for base in bases: for name in getattr(base, "__abstractmethods__", set()): value = getattr(cls, name, None) if getattr(value, "__isabstractmethod__", False): abstracts.add(name) cls.__abstractmethods__ = frozenset(abstracts) # Set up inheritance registry cls._abc_registry = WeakSet() cls._abc_cache = WeakSet() cls._abc_negative_cache = WeakSet() cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter return cls def register(cls, subclass): """Register a virtual subclass of an ABC.""" if not isinstance(subclass, (type, types.ClassType)): raise TypeError("Can only register classes") if issubclass(subclass, cls): return # Already a subclass # Subtle: test for cycles *after* testing for "already a subclass"; # this means we allow X.register(X) and interpret it as a no-op. if issubclass(cls, subclass): # This would create a cycle, which is bad for the algorithm below raise RuntimeError("Refusing to create an inheritance cycle") cls._abc_registry.add(subclass) ABCMeta._abc_invalidation_counter += 1 # Invalidate negative cache def _dump_registry(cls, file=None): """Debug helper to print the ABC registry.""" print >> file, "Class: %s.%s" % (cls.__module__, cls.__name__) print >> file, "Inv.counter: %s" % ABCMeta._abc_invalidation_counter for name in sorted(cls.__dict__.keys()): if name.startswith("_abc_"): value = getattr(cls, name) print >> file, "%s: %r" % (name, value) def __instancecheck__(cls, instance): """Override for isinstance(instance, cls).""" # Inline the cache checking when it's simple. subclass = getattr(instance, '__class__', None) if subclass is not None and subclass in cls._abc_cache: return True subtype = type(instance) # Old-style instances if subtype is _InstanceType: subtype = subclass if subtype is subclass or subclass is None: if (cls._abc_negative_cache_version == ABCMeta._abc_invalidation_counter and subtype in cls._abc_negative_cache): return False # Fall back to the subclass check. return cls.__subclasscheck__(subtype) return (cls.__subclasscheck__(subclass) or cls.__subclasscheck__(subtype)) def __subclasscheck__(cls, subclass): """Override for issubclass(subclass, cls).""" # Check cache if subclass in cls._abc_cache: return True # Check negative cache; may have to invalidate if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter: # Invalidate the negative cache cls._abc_negative_cache = WeakSet() cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter elif subclass in cls._abc_negative_cache: return False # Check the subclass hook ok = cls.__subclasshook__(subclass) if ok is not NotImplemented: assert isinstance(ok, bool) if ok: cls._abc_cache.add(subclass) else: cls._abc_negative_cache.add(subclass) return ok # Check if it's a direct subclass if cls in getattr(subclass, '__mro__', ()): cls._abc_cache.add(subclass) return True # Check if it's a subclass of a registered class (recursive) for rcls in cls._abc_registry: if issubclass(subclass, rcls): cls._abc_cache.add(subclass) return True # Check if it's a subclass of a subclass (recursive) for scls in cls.__subclasses__(): if issubclass(subclass, scls): cls._abc_cache.add(subclass) return True # No dice; update negative cache cls._abc_negative_cache.add(subclass) return False
gpl-3.0
rmoorman/sqlalchemy-utils
sqlalchemy_utils/relationships/__init__.py
6
3422
import sqlalchemy as sa from sqlalchemy.sql.util import ClauseAdapter from .chained_join import chained_join # noqa def path_to_relationships(path, cls): relationships = [] for path_name in path.split('.'): rel = getattr(cls, path_name) relationships.append(rel) cls = rel.mapper.class_ return relationships def adapt_expr(expr, *selectables): for selectable in selectables: expr = ClauseAdapter(selectable).traverse(expr) return expr def inverse_join(selectable, left_alias, right_alias, relationship): if relationship.property.secondary is not None: secondary_alias = sa.alias(relationship.property.secondary) return selectable.join( secondary_alias, adapt_expr( relationship.property.secondaryjoin, sa.inspect(left_alias).selectable, secondary_alias ) ).join( right_alias, adapt_expr( relationship.property.primaryjoin, sa.inspect(right_alias).selectable, secondary_alias ) ) else: join = sa.orm.join(right_alias, left_alias, relationship) onclause = join.onclause return selectable.join(right_alias, onclause) def relationship_to_correlation(relationship, alias): if relationship.property.secondary is not None: return adapt_expr( relationship.property.primaryjoin, alias, ) else: return sa.orm.join( relationship.parent, alias, relationship ).onclause def chained_inverse_join(relationships, leaf_model): selectable = sa.inspect(leaf_model).selectable aliases = [leaf_model] for index, relationship in enumerate(relationships[1:]): aliases.append(sa.orm.aliased(relationship.mapper.class_)) selectable = inverse_join( selectable, aliases[index], aliases[index + 1], relationships[index] ) if relationships[-1].property.secondary is not None: secondary_alias = sa.alias(relationships[-1].property.secondary) selectable = selectable.join( secondary_alias, adapt_expr( relationships[-1].property.secondaryjoin, secondary_alias, sa.inspect(aliases[-1]).selectable ) ) aliases.append(secondary_alias) return selectable, aliases def select_correlated_expression( root_model, expr, path, leaf_model, from_obj=None, order_by=None, correlate=True ): relationships = list(reversed(path_to_relationships(path, root_model))) query = sa.select([expr]) selectable = sa.inspect(leaf_model).selectable if order_by: query = query.order_by( *[adapt_expr(o, selectable) for o in order_by] ) join_expr, aliases = chained_inverse_join(relationships, leaf_model) condition = relationship_to_correlation( relationships[-1], aliases[-1] ) if from_obj is not None: condition = adapt_expr(condition, from_obj) query = query.select_from(join_expr.selectable) if correlate: query = query.correlate( from_obj if from_obj is not None else root_model ) return query.where(condition)
bsd-3-clause
ctxis/canape
CANAPE.Scripting/Lib/encodings/mac_iceland.py
593
13754
""" Python Character Mapping Codec mac_iceland generated from 'MAPPINGS/VENDORS/APPLE/ICELAND.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-iceland', 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'\xdd' # 0xA0 -> LATIN CAPITAL LETTER Y WITH ACUTE 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'\xd0' # 0xDC -> LATIN CAPITAL LETTER ETH u'\xf0' # 0xDD -> LATIN SMALL LETTER ETH u'\xde' # 0xDE -> LATIN CAPITAL LETTER THORN u'\xfe' # 0xDF -> LATIN SMALL LETTER THORN u'\xfd' # 0xE0 -> LATIN SMALL LETTER Y WITH ACUTE 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)
gpl-3.0
wengole/pybbm
pybb/south_migrations/0006_autoone.py
11
8702
# encoding: utf-8 from south.v2 import SchemaMigration from pybb.compat import get_user_model_path, get_user_frozen_models AUTH_USER = get_user_model_path() class Migration(SchemaMigration): def forwards(self, orm): pass def backwards(self, orm): pass models = { 'auth.group': { 'Meta': {'object_name': 'Group'}, 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) }, 'auth.permission': { 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'}, 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) }, 'contenttypes.contenttype': { 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) }, 'pybb.attachment': { 'Meta': {'object_name': 'Attachment'}, 'content_type': ('django.db.models.fields.CharField', [], {'max_length': '255'}), 'hash': ('django.db.models.fields.CharField', [], {'db_index': 'True', 'max_length': '40', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.TextField', [], {}), 'path': ('django.db.models.fields.CharField', [], {'max_length': '255'}), 'post': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'attachments'", 'to': "orm['pybb.Post']"}), 'size': ('django.db.models.fields.IntegerField', [], {}) }, 'pybb.category': { 'Meta': {'ordering': "['position']", 'object_name': 'Category'}, 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '80'}), 'position': ('django.db.models.fields.IntegerField', [], {'default': '0', 'blank': 'True'}) }, 'pybb.forum': { 'Meta': {'ordering': "['position']", 'object_name': 'Forum'}, 'category': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'forums'", 'to': "orm['pybb.Category']"}), 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'last_post': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'last_post_in_forum'", 'null': 'True', 'to': "orm['pybb.Post']"}), 'moderators': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['%s']"% AUTH_USER, 'null': 'True', 'blank': 'True'}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '80'}), 'position': ('django.db.models.fields.IntegerField', [], {'default': '0', 'blank': 'True'}), 'post_count': ('django.db.models.fields.IntegerField', [], {'default': '0', 'blank': 'True'}), 'topic_count': ('django.db.models.fields.IntegerField', [], {'default': '0', 'blank': 'True'}), 'updated': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}) }, 'pybb.post': { 'Meta': {'ordering': "['created']", 'object_name': 'Post'}, 'body': ('django.db.models.fields.TextField', [], {}), 'body_html': ('django.db.models.fields.TextField', [], {}), 'body_text': ('django.db.models.fields.TextField', [], {}), 'created': ('django.db.models.fields.DateTimeField', [], {'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'markup': ('django.db.models.fields.CharField', [], {'default': "'bbcode'", 'max_length': '15'}), 'topic': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'posts'", 'to': "orm['pybb.Topic']"}), 'updated': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}), 'user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'posts'", 'to': "orm['%s']"% AUTH_USER}), 'user_ip': ('django.db.models.fields.IPAddressField', [], {'default': "'0.0.0.0'", 'max_length': '15', 'blank': 'True'}) }, 'pybb.profile': { 'Meta': {'object_name': 'Profile'}, 'ban_status': ('django.db.models.fields.SmallIntegerField', [], {'default': '0'}), 'ban_till': ('django.db.models.fields.DateTimeField', [], {'default': 'None', 'null': 'True', 'blank': 'True'}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'language': ('django.db.models.fields.CharField', [], {'max_length': '10', 'blank': 'True'}), 'markup': ('django.db.models.fields.CharField', [], {'default': "'bbcode'", 'max_length': '15'}), 'post_count': ('django.db.models.fields.IntegerField', [], {'default': '0', 'blank': 'True'}), 'show_signatures': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), 'signature': ('django.db.models.fields.TextField', [], {'max_length': '1024', 'blank': 'True'}), 'signature_html': ('django.db.models.fields.TextField', [], {'max_length': '1054', 'blank': 'True'}), 'time_zone': ('django.db.models.fields.FloatField', [], {'default': '3.0'}), 'user': ('annoying.fields.AutoOneToOneField', [], {'related_name': "'pybb_profile'", 'unique': 'True', 'to': "orm['%s']"% AUTH_USER}) }, 'pybb.readtracking': { 'Meta': {'object_name': 'ReadTracking'}, 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'last_read': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), 'topics': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), 'user': ('annoying.fields.AutoOneToOneField', [], {'to': "orm['%s']"% AUTH_USER, 'unique': 'True'}) }, 'pybb.topic': { 'Meta': {'ordering': "['-created']", 'object_name': 'Topic'}, 'closed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), 'forum': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'topics'", 'to': "orm['pybb.Forum']"}), 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), 'last_post': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'last_post_in_topic'", 'null': 'True', 'to': "orm['pybb.Post']"}), 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}), 'post_count': ('django.db.models.fields.IntegerField', [], {'default': '0', 'blank': 'True'}), 'sticky': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), 'subscribers': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'subscriptions'", 'blank': 'True', 'to': "orm['%s']"% AUTH_USER}), 'updated': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}), 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['%s']"% AUTH_USER}), 'views': ('django.db.models.fields.IntegerField', [], {'default': '0', 'blank': 'True'}) } } models.update(get_user_frozen_models(AUTH_USER)) complete_apps = ['pybb']
bsd-2-clause
wangyou/XX-Net
code/default/python27/1.0/lib/noarch/hyper/http11/response.py
9
12225
# -*- coding: utf-8 -*- """ hyper/http11/response ~~~~~~~~~~~~~~~~~~~~~ Contains the HTTP/1.1 equivalent of the HTTPResponse object defined in httplib/http.client. """ import logging import weakref import zlib from ..common.decoder import DeflateDecoder from ..common.exceptions import ChunkedDecodeError, InvalidResponseError from ..common.exceptions import ConnectionResetError log = logging.getLogger(__name__) class HTTP11Response(object): """ An ``HTTP11Response`` wraps the HTTP/1.1 response from the server. It provides access to the response headers and the entity body. The response is an iterable object and can be used in a with statement. """ def __init__(self, code, reason, headers, sock, connection=None): #: The reason phrase returned by the server. self.reason = reason #: The status code returned by the server. self.status = code #: The response headers. These are determined upon creation, assigned #: once, and never assigned again. self.headers = headers #: The response trailers. These are always intially ``None``. self.trailers = None # The socket this response is being sent over. self._sock = sock # Whether we expect the connection to be closed. If we do, we don't # bother checking for content-length, we just keep reading until # we no longer can. self._expect_close = False if b'close' in self.headers.get(b'connection', []): self._expect_close = True # The expected length of the body. try: self._length = int(self.headers[b'content-length'][0]) except KeyError: self._length = None # Whether we expect a chunked response. self._chunked = b'chunked' in self.headers.get(b'transfer-encoding', []) # One of the following must be true: we must expect that the connection # will be closed following the body, or that a content-length was sent, # or that we're getting a chunked response. # FIXME: Remove naked assert, replace with something better. assert self._expect_close or self._length is not None or self._chunked # This object is used for decompressing gzipped request bodies. Right # now we only support gzip because that's all the RFC mandates of us. # Later we'll add support for more encodings. # This 16 + MAX_WBITS nonsense is to force gzip. See this # Stack Overflow answer for more: # http://stackoverflow.com/a/2695466/1401686 if b'gzip' in self.headers.get(b'content-encoding', []): self._decompressobj = zlib.decompressobj(16 + zlib.MAX_WBITS) elif b'deflate' in self.headers.get(b'content-encoding', []): self._decompressobj = DeflateDecoder() else: self._decompressobj = None # This is a reference that allows for the Response class to tell the # parent connection object to throw away its socket object. This is to # be used when the connection is genuinely closed, so that the user # can keep using the Connection object. # Strictly, we take a weakreference to this so that we don't set up a # reference cycle. if connection is not None: self._parent = weakref.ref(connection) else: self._parent = None self._buffered_data = b'' self._chunker = None def read(self, amt=None, decode_content=True): """ Reads the response body, or up to the next ``amt`` bytes. :param amt: (optional) The amount of data to read. If not provided, all the data will be read from the response. :param decode_content: (optional) If ``True``, will transparently decode the response data. :returns: The read data. Note that if ``decode_content`` is set to ``True``, the actual amount of data returned may be different to the amount requested. """ # Return early if we've lost our connection. if self._sock is None: return b'' if self._chunked: return self._normal_read_chunked(amt, decode_content) # If we're asked to do a read without a length, we need to read # everything. That means either the entire content length, or until the # socket is closed, depending. if amt is None: if self._length is not None: amt = self._length elif self._expect_close: return self._read_expect_closed(decode_content) else: # pragma: no cover raise InvalidResponseError( "Response must either have length or Connection: close" ) # Otherwise, we've been asked to do a bounded read. We should read no # more than the remaining length, obviously. # FIXME: Handle cases without _length if self._length is not None: amt = min(amt, self._length) # If we are now going to read nothing, exit early. We still need to # close the socket. if not amt: self.close(socket_close=self._expect_close) return b'' # Now, issue reads until we read that length. This is to account for # the fact that it's possible that we'll be asked to read more than # 65kB in one shot. to_read = amt chunks = [] # Ideally I'd like this to read 'while to_read', but I want to be # defensive against the admittedly unlikely case that the socket # returns *more* data than I want. while to_read > 0: chunk = self._sock.recv(amt).tobytes() # If we got an empty read, but were expecting more, the remote end # has hung up. Raise an exception if we were expecting more data, # but if we were expecting the remote end to close then it's ok. if not chunk: if self._length is not None or not self._expect_close: self.close(socket_close=True) raise ConnectionResetError("Remote end hung up!") break to_read -= len(chunk) chunks.append(chunk) data = b''.join(chunks) if self._length is not None: self._length -= len(data) # If we're at the end of the request, we have some cleaning up to do. # Close the stream, and if necessary flush the buffer. Checking that # we're at the end is actually obscenely complex: either we've read the # full content-length or, if we were expecting a closed connection, # we've had a read shorter than the requested amount. We also have to # do this before we try to decompress the body. end_of_request = (self._length == 0 or (self._expect_close and len(data) < amt)) # We may need to decode the body. if decode_content and self._decompressobj and data: data = self._decompressobj.decompress(data) if decode_content and self._decompressobj and end_of_request: data += self._decompressobj.flush() # We're at the end. Close the connection. Explicit check for zero here # because self._length might be None. if end_of_request: self.close(socket_close=self._expect_close) return data def read_chunked(self, decode_content=True): """ Reads chunked transfer encoded bodies. This method returns a generator: each iteration of which yields one chunk *unless* the chunks are compressed, in which case it yields whatever the decompressor provides for each chunk. .. warning:: This may yield the empty string, without that being the end of the body! """ if not self._chunked: raise ChunkedDecodeError( "Attempted chunked read of non-chunked body." ) # Return early if possible. if self._sock is None: return while True: # Read to the newline to get the chunk length. This is a # hexadecimal integer. chunk_length = int(self._sock.readline().tobytes().strip(), 16) data = b'' # If the chunk length is zero, consume the newline and then we're # done. If we were decompressing data, return the remaining data. if not chunk_length: self._sock.readline() if decode_content and self._decompressobj: yield self._decompressobj.flush() self.close(socket_close=self._expect_close) break # Then read that many bytes. while chunk_length > 0: chunk = self._sock.recv(chunk_length).tobytes() data += chunk chunk_length -= len(chunk) assert chunk_length == 0 # Now, consume the newline. self._sock.readline() # We may need to decode the body. if decode_content and self._decompressobj and data: data = self._decompressobj.decompress(data) yield data return def close(self, socket_close=False): """ Close the response. This causes the Response to lose access to the backing socket. In some cases, it can also cause the backing connection to be torn down. :param socket_close: Whether to close the backing socket. :returns: Nothing. """ if socket_close and self._parent is not None: # The double call is necessary because we need to dereference the # weakref. If the weakref is no longer valid, that's fine, there's # no connection object to tell. parent = self._parent() if parent is not None: parent.close() self._sock = None def _read_expect_closed(self, decode_content): """ Implements the logic for an unbounded read on a socket that we expect to be closed by the remote end. """ # In this case, just read until we cannot read anymore. Then, close the # socket, becuase we know we have to. chunks = [] while True: try: chunk = self._sock.recv(65535).tobytes() if not chunk: break except ConnectionResetError: break else: chunks.append(chunk) self.close(socket_close=True) # We may need to decompress the data. data = b''.join(chunks) if decode_content and self._decompressobj: data = self._decompressobj.decompress(data) data += self._decompressobj.flush() return data def _normal_read_chunked(self, amt, decode_content): """ Implements the logic for calling ``read()`` on a chunked response. """ # If we're doing a full read, read it as chunked and then just join # the chunks together! if amt is None: return self._buffered_data + b''.join(self.read_chunked()) if self._chunker is None: self._chunker = self.read_chunked() # Otherwise, we have a certain amount of data we want to read. current_amount = len(self._buffered_data) extra_data = [self._buffered_data] while current_amount < amt: try: chunk = next(self._chunker) except StopIteration: self.close(socket_close=self._expect_close) break current_amount += len(chunk) extra_data.append(chunk) data = b''.join(extra_data) self._buffered_data = data[amt:] return data[:amt] # The following methods implement the context manager protocol. def __enter__(self): return self def __exit__(self, *args): self.close() return False # Never swallow exceptions.
bsd-2-clause
WillisXChen/django-oscar
oscar/lib/python2.7/site-packages/django/conf/locale/mk/formats.py
112
1742
# -*- encoding: utf-8 -*- # This file is distributed under the same license as the Django package. # from __future__ import unicode_literals # The *_FORMAT strings use the Django date format syntax, # see http://docs.djangoproject.com/en/dev/ref/templates/builtins/#date DATE_FORMAT = 'd F Y' TIME_FORMAT = 'H:i' DATETIME_FORMAT = 'j. F Y H:i' YEAR_MONTH_FORMAT = 'F Y' MONTH_DAY_FORMAT = 'j. F' SHORT_DATE_FORMAT = 'j.m.Y' SHORT_DATETIME_FORMAT = 'j.m.Y H:i' FIRST_DAY_OF_WEEK = 1 # The *_INPUT_FORMATS strings use the Python strftime format syntax, # see http://docs.python.org/library/datetime.html#strftime-strptime-behavior DATE_INPUT_FORMATS = ( '%d.%m.%Y', '%d.%m.%y', # '25.10.2006', '25.10.06' '%d. %m. %Y', '%d. %m. %y', # '25. 10. 2006', '25. 10. 06' ) DATETIME_INPUT_FORMATS = ( '%d.%m.%Y %H:%M:%S', # '25.10.2006 14:30:59' '%d.%m.%Y %H:%M:%S.%f', # '25.10.2006 14:30:59.000200' '%d.%m.%Y %H:%M', # '25.10.2006 14:30' '%d.%m.%Y', # '25.10.2006' '%d.%m.%y %H:%M:%S', # '25.10.06 14:30:59' '%d.%m.%y %H:%M:%S.%f', # '25.10.06 14:30:59.000200' '%d.%m.%y %H:%M', # '25.10.06 14:30' '%d.%m.%y', # '25.10.06' '%d. %m. %Y %H:%M:%S', # '25. 10. 2006 14:30:59' '%d. %m. %Y %H:%M:%S.%f', # '25. 10. 2006 14:30:59.000200' '%d. %m. %Y %H:%M', # '25. 10. 2006 14:30' '%d. %m. %Y', # '25. 10. 2006' '%d. %m. %y %H:%M:%S', # '25. 10. 06 14:30:59' '%d. %m. %y %H:%M:%S.%f', # '25. 10. 06 14:30:59.000200' '%d. %m. %y %H:%M', # '25. 10. 06 14:30' '%d. %m. %y', # '25. 10. 06' ) DECIMAL_SEPARATOR = ',' THOUSAND_SEPARATOR = '.' NUMBER_GROUPING = 3
bsd-3-clause
agaldona/odoomrp-wip-1
mrp_configurable_timing/models/res_company.py
25
1082
# -*- encoding: utf-8 -*- ############################################################################## # # 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 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/. # ############################################################################## from openerp import models, fields class ResCompany(models.Model): _inherit = 'res.company' complete_cycle = fields.Boolean(string='Force complete cycles', default=True)
agpl-3.0
andybalaam/pepper
old/pepper1/src/libpepper/vals/types/pepimplementsfunction.py
1
1733
from libpepper.vals.functions.pepfunction import PepFunction from libpepper.values import PepBool from libpepper.values import PepType from libpepper.values import all_known class PepImplementsFunction( PepFunction ): """ A function automatically present in every class called "implements" that returns true if this class implements the supplied interface. """ def __init__( self, clazz ): PepFunction.__init__( self ) self.clazz = clazz def call( self, args, env ): if all_known( ( self.clazz,) + args, env ): evaldarg = args[0].evaluate( env ) # TODO: check evaldarg is an interface (or at least has can_match) return PepBool( evaldarg.can_match( self.clazz, env ) ) else: raise Exception( "Can't (currently) support checking whether classes " + "implement interfaces at runtime." ) def return_type( self, args, env ): return PepType( PepBool ) def args_match( self, args, env ): if PepDefInit.INIT_IMPL_NAME not in self.user_class.namespace: # If there is no __init__, we will supply an empty constructor return ( len( args ) == 0 ) # Make an object that looks like an instance so it passes the # call to matches() on the PepUserClass, and put it on the beginning # of the args array before we match against the user-defined init # method. self_plus_args = [ PepKnownInstance( self.user_class ) ] + args return self.user_class.namespace[PepDefInit.INIT_IMPL_NAME].args_match( self_plus_args, env ) def construction_args( self ): return ( self.clazz, )
mit
abhitopia/tensorflow
tensorflow/python/saved_model/signature_def_utils_impl.py
58
5229
# Copyright 2016 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. # ============================================================================== """SignatureDef utility functions implementation.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from tensorflow.core.protobuf import meta_graph_pb2 from tensorflow.python.saved_model import signature_constants from tensorflow.python.saved_model import utils def build_signature_def(inputs=None, outputs=None, method_name=None): """Utility function to build a SignatureDef protocol buffer. Args: inputs: Inputs of the SignatureDef defined as a proto map of string to tensor info. outputs: Outputs of the SignatureDef defined as a proto map of string to tensor info. method_name: Method name of the SignatureDef as a string. Returns: A SignatureDef protocol buffer constructed based on the supplied arguments. """ signature_def = meta_graph_pb2.SignatureDef() if inputs is not None: for item in inputs: signature_def.inputs[item].CopyFrom(inputs[item]) if outputs is not None: for item in outputs: signature_def.outputs[item].CopyFrom(outputs[item]) if method_name is not None: signature_def.method_name = method_name return signature_def def regression_signature_def(examples, predictions): """Creates regression signature from given examples and predictions. Args: examples: `Tensor`. predictions: `Tensor`. Returns: A regression-flavored signature_def. Raises: ValueError: If examples is `None`. """ if examples is None: raise ValueError('examples cannot be None for regression.') if predictions is None: raise ValueError('predictions cannot be None for regression.') input_tensor_info = utils.build_tensor_info(examples) signature_inputs = {signature_constants.REGRESS_INPUTS: input_tensor_info} output_tensor_info = utils.build_tensor_info(predictions) signature_outputs = {signature_constants.REGRESS_OUTPUTS: output_tensor_info} signature_def = build_signature_def( signature_inputs, signature_outputs, signature_constants.REGRESS_METHOD_NAME) return signature_def def classification_signature_def(examples, classes, scores): """Creates classification signature from given examples and predictions. Args: examples: `Tensor`. classes: `Tensor`. scores: `Tensor`. Returns: A classification-flavored signature_def. Raises: ValueError: If examples is `None`. """ if examples is None: raise ValueError('examples cannot be None for classification.') if classes is None and scores is None: raise ValueError('classes and scores cannot both be None for ' 'classification.') input_tensor_info = utils.build_tensor_info(examples) signature_inputs = {signature_constants.CLASSIFY_INPUTS: input_tensor_info} signature_outputs = {} if classes is not None: classes_tensor_info = utils.build_tensor_info(classes) signature_outputs[signature_constants.CLASSIFY_OUTPUT_CLASSES] = ( classes_tensor_info) if scores is not None: scores_tensor_info = utils.build_tensor_info(scores) signature_outputs[signature_constants.CLASSIFY_OUTPUT_SCORES] = ( scores_tensor_info) signature_def = build_signature_def( signature_inputs, signature_outputs, signature_constants.CLASSIFY_METHOD_NAME) return signature_def def predict_signature_def(inputs, outputs): """Creates prediction signature from given inputs and outputs. Args: inputs: dict of string to `Tensor`. outputs: dict of string to `Tensor`. Returns: A prediction-flavored signature_def. Raises: ValueError: If inputs or outputs is `None`. """ if inputs is None or not inputs: raise ValueError('inputs cannot be None or empty for prediction.') if outputs is None: raise ValueError('outputs cannot be None or empty for prediction.') # If there's only one input or output, we can standardize keys if len(inputs) == 1: (_, value), = inputs.items() inputs = {signature_constants.PREDICT_INPUTS: value} if len(outputs) == 1: (_, value), = outputs.items() outputs = {signature_constants.PREDICT_OUTPUTS: value} signature_inputs = {key: utils.build_tensor_info(tensor) for key, tensor in inputs.items()} signature_outputs = {key: utils.build_tensor_info(tensor) for key, tensor in outputs.items()} signature_def = build_signature_def( signature_inputs, signature_outputs, signature_constants.PREDICT_METHOD_NAME) return signature_def
apache-2.0
edisonlz/fruit
web_project/base/site-packages/django/contrib/gis/db/backends/postgis/creation.py
117
4498
from django.conf import settings from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation from django.utils.functional import cached_property class PostGISCreation(DatabaseCreation): geom_index_type = 'GIST' geom_index_ops = 'GIST_GEOMETRY_OPS' geom_index_ops_nd = 'GIST_GEOMETRY_OPS_ND' @cached_property def template_postgis(self): template_postgis = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis') cursor = self.connection.cursor() cursor.execute('SELECT 1 FROM pg_database WHERE datname = %s LIMIT 1;', (template_postgis,)) if cursor.fetchone(): return template_postgis return None def sql_indexes_for_field(self, model, f, style): "Return any spatial index creation SQL for the field." from django.contrib.gis.db.models.fields import GeometryField output = super(PostGISCreation, self).sql_indexes_for_field(model, f, style) if isinstance(f, GeometryField): gqn = self.connection.ops.geo_quote_name qn = self.connection.ops.quote_name db_table = model._meta.db_table if f.geography or self.connection.ops.geometry: # Geography and Geometry (PostGIS 2.0+) columns are # created normally. pass else: # Geometry columns are created by `AddGeometryColumn` # stored procedure. output.append(style.SQL_KEYWORD('SELECT ') + style.SQL_TABLE('AddGeometryColumn') + '(' + style.SQL_TABLE(gqn(db_table)) + ', ' + style.SQL_FIELD(gqn(f.column)) + ', ' + style.SQL_FIELD(str(f.srid)) + ', ' + style.SQL_COLTYPE(gqn(f.geom_type)) + ', ' + style.SQL_KEYWORD(str(f.dim)) + ');') if not f.null: # Add a NOT NULL constraint to the field output.append(style.SQL_KEYWORD('ALTER TABLE ') + style.SQL_TABLE(qn(db_table)) + style.SQL_KEYWORD(' ALTER ') + style.SQL_FIELD(qn(f.column)) + style.SQL_KEYWORD(' SET NOT NULL') + ';') if f.spatial_index: # Spatial indexes created the same way for both Geometry and # Geography columns. # PostGIS 2.0 does not support GIST_GEOMETRY_OPS. So, on 1.5 # we use GIST_GEOMETRY_OPS, on 2.0 we use either "nd" ops # which are fast on multidimensional cases, or just plain # gist index for the 2d case. if f.geography: index_ops = '' elif self.connection.ops.geometry: if f.dim > 2: index_ops = ' ' + style.SQL_KEYWORD(self.geom_index_ops_nd) else: index_ops = '' else: index_ops = ' ' + style.SQL_KEYWORD(self.geom_index_ops) output.append(style.SQL_KEYWORD('CREATE INDEX ') + style.SQL_TABLE(qn('%s_%s_id' % (db_table, f.column))) + style.SQL_KEYWORD(' ON ') + style.SQL_TABLE(qn(db_table)) + style.SQL_KEYWORD(' USING ') + style.SQL_COLTYPE(self.geom_index_type) + ' ( ' + style.SQL_FIELD(qn(f.column)) + index_ops + ' );') return output def sql_table_creation_suffix(self): if self.template_postgis is not None: return ' TEMPLATE %s' % ( self.connection.ops.quote_name(self.template_postgis),) return '' def _create_test_db(self, verbosity, autoclobber): test_database_name = super(PostGISCreation, self)._create_test_db(verbosity, autoclobber) if self.template_postgis is None: # Connect to the test database in order to create the postgis extension self.connection.close() self.connection.settings_dict["NAME"] = test_database_name cursor = self.connection.cursor() cursor.execute("CREATE EXTENSION postgis") cursor.connection.commit() return test_database_name
apache-2.0
SanchayanMaity/gem5
util/cpt_upgraders/arm-gem5-gic-ext.py
26
3402
# Copyright (c) 2015 ARM Limited # All rights reserved # # The license below extends only to copyright in the software and shall # not be construed as granting a license to any other intellectual # property including but not limited to intellectual property relating # to a hardware implementation of the functionality of the software # licensed hereunder. You may use the software subject to the license # terms below provided that you ensure that this notice is replicated # unmodified and in its entirety in all distributions of the software, # modified or unmodified, in source code or in binary form. # # 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 the copyright holders 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. # # Authors: Andreas Sandberg # def upgrader(cpt): """The gem5 GIC extensions change the size of many GIC data structures. Resize them to match the new GIC.""" import re if cpt.get('root','isa') != 'arm': return old_cpu_max = 8 new_cpu_max = 256 sgi_max = 16 ppi_max = 16 per_cpu_regs = ( ("iccrpr", [ "0xff", ]), ("cpuEnabled", [ "false", ]), ("cpuPriority", [ "0xff", ]), ("cpuBpr", [ "0", ]), ("cpuHighestInt", [ "1023", ]), ("cpuPpiPending", [ "0", ]), ("cpuPpiActive", [ "0", ] ), ("interrupt_time", [ "0", ]), ("*bankedIntPriority", ["0", ] * (sgi_max + ppi_max)), ) new_per_cpu_regs = ( ("cpuSgiPendingExt", "0"), ("cpuSgiActiveExt", "0"), ) for sec in cpt.sections(): if re.search('.*\.gic$', sec): for reg, default in per_cpu_regs: value = cpt.get(sec, reg).split(" ") assert len(value) / len(default) == old_cpu_max, \ "GIC register size mismatch" value += [ " ".join(default), ] * (new_cpu_max - old_cpu_max) cpt.set(sec, reg, " ".join(value)) for reg, default in new_per_cpu_regs: cpt.set(sec, reg, " ".join([ default, ] * new_cpu_max))
bsd-3-clause
caiowilson/brackets-shell
gyp/pylib/gyp/MSVSVersion.py
122
13527
# 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. """Handle version information related to Visual Stuio.""" import errno import os import re import subprocess import sys import gyp class VisualStudioVersion(object): """Information regarding a version of Visual Studio.""" def __init__(self, short_name, description, solution_version, project_version, flat_sln, uses_vcxproj, path, sdk_based, default_toolset=None): self.short_name = short_name self.description = description self.solution_version = solution_version self.project_version = project_version self.flat_sln = flat_sln self.uses_vcxproj = uses_vcxproj self.path = path self.sdk_based = sdk_based self.default_toolset = default_toolset def ShortName(self): return self.short_name def Description(self): """Get the full description of the version.""" return self.description def SolutionVersion(self): """Get the version number of the sln files.""" return self.solution_version def ProjectVersion(self): """Get the version number of the vcproj or vcxproj files.""" return self.project_version def FlatSolution(self): return self.flat_sln def UsesVcxproj(self): """Returns true if this version uses a vcxproj file.""" return self.uses_vcxproj def ProjectExtension(self): """Returns the file extension for the project.""" return self.uses_vcxproj and '.vcxproj' or '.vcproj' def Path(self): """Returns the path to Visual Studio installation.""" return self.path def ToolPath(self, tool): """Returns the path to a given compiler tool. """ return os.path.normpath(os.path.join(self.path, "VC/bin", tool)) def DefaultToolset(self): """Returns the msbuild toolset version that will be used in the absence of a user override.""" return self.default_toolset def SetupScript(self, target_arch): """Returns a command (with arguments) to be used to set up the environment.""" # Check if we are running in the SDK command line environment and use # the setup script from the SDK if so. |target_arch| should be either # 'x86' or 'x64'. assert target_arch in ('x86', 'x64') sdk_dir = os.environ.get('WindowsSDKDir') if self.sdk_based and sdk_dir: return [os.path.normpath(os.path.join(sdk_dir, 'Bin/SetEnv.Cmd')), '/' + target_arch] else: # We don't use VC/vcvarsall.bat for x86 because vcvarsall calls # vcvars32, which it can only find if VS??COMNTOOLS is set, which it # isn't always. if target_arch == 'x86': return [os.path.normpath( os.path.join(self.path, 'Common7/Tools/vsvars32.bat'))] else: assert target_arch == 'x64' arg = 'x86_amd64' if (os.environ.get('PROCESSOR_ARCHITECTURE') == 'AMD64' or os.environ.get('PROCESSOR_ARCHITEW6432') == 'AMD64'): # Use the 64-on-64 compiler if we can. arg = 'amd64' return [os.path.normpath( os.path.join(self.path, 'VC/vcvarsall.bat')), arg] def _RegistryQueryBase(sysdir, key, value): """Use reg.exe to read a particular key. While ideally we might use the win32 module, we would like gyp to be python neutral, so for instance cygwin python lacks this module. Arguments: sysdir: The system subdirectory to attempt to launch reg.exe from. key: The registry key to read from. value: The particular value to read. Return: stdout from reg.exe, or None for failure. """ # Skip if not on Windows or Python Win32 setup issue if sys.platform not in ('win32', 'cygwin'): return None # Setup params to pass to and attempt to launch reg.exe cmd = [os.path.join(os.environ.get('WINDIR', ''), sysdir, 'reg.exe'), 'query', key] if value: cmd.extend(['/v', value]) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Obtain the stdout from reg.exe, reading to the end so p.returncode is valid # Note that the error text may be in [1] in some cases text = p.communicate()[0] # Check return code from reg.exe; officially 0==success and 1==error if p.returncode: return None return text def _RegistryQuery(key, value=None): """Use reg.exe to read a particular key through _RegistryQueryBase. First tries to launch from %WinDir%\Sysnative to avoid WoW64 redirection. If that fails, it falls back to System32. Sysnative is available on Vista and up and available on Windows Server 2003 and XP through KB patch 942589. Note that Sysnative will always fail if using 64-bit python due to it being a virtual directory and System32 will work correctly in the first place. KB 942589 - http://support.microsoft.com/kb/942589/en-us. Arguments: key: The registry key. value: The particular registry value to read (optional). Return: stdout from reg.exe, or None for failure. """ text = None try: text = _RegistryQueryBase('Sysnative', key, value) except OSError, e: if e.errno == errno.ENOENT: text = _RegistryQueryBase('System32', key, value) else: raise return text def _RegistryGetValue(key, value): """Use reg.exe to obtain the value of a registry key. Args: key: The registry key. value: The particular registry value to read. Return: contents of the registry key's value, or None on failure. """ text = _RegistryQuery(key, value) if not text: return None # Extract value. match = re.search(r'REG_\w+\s+([^\r]+)\r\n', text) if not match: return None return match.group(1) def _RegistryKeyExists(key): """Use reg.exe to see if a key exists. Args: key: The registry key to check. Return: True if the key exists """ if not _RegistryQuery(key): return False return True def _CreateVersion(name, path, sdk_based=False): """Sets up MSVS project generation. Setup is based off the GYP_MSVS_VERSION environment variable or whatever is autodetected if GYP_MSVS_VERSION is not explicitly specified. If a version is passed in that doesn't match a value in versions python will throw a error. """ if path: path = os.path.normpath(path) versions = { '2012': VisualStudioVersion('2012', 'Visual Studio 2012', solution_version='12.00', project_version='4.0', flat_sln=False, uses_vcxproj=True, path=path, sdk_based=sdk_based, default_toolset='v110'), '2012e': VisualStudioVersion('2012e', 'Visual Studio 2012', solution_version='12.00', project_version='4.0', flat_sln=True, uses_vcxproj=True, path=path, sdk_based=sdk_based, default_toolset='v110'), '2010': VisualStudioVersion('2010', 'Visual Studio 2010', solution_version='11.00', project_version='4.0', flat_sln=False, uses_vcxproj=True, path=path, sdk_based=sdk_based), '2010e': VisualStudioVersion('2010e', 'Visual Studio 2010', solution_version='11.00', project_version='4.0', flat_sln=True, uses_vcxproj=True, path=path, sdk_based=sdk_based), '2008': VisualStudioVersion('2008', 'Visual Studio 2008', solution_version='10.00', project_version='9.00', flat_sln=False, uses_vcxproj=False, path=path, sdk_based=sdk_based), '2008e': VisualStudioVersion('2008e', 'Visual Studio 2008', solution_version='10.00', project_version='9.00', flat_sln=True, uses_vcxproj=False, path=path, sdk_based=sdk_based), '2005': VisualStudioVersion('2005', 'Visual Studio 2005', solution_version='9.00', project_version='8.00', flat_sln=False, uses_vcxproj=False, path=path, sdk_based=sdk_based), '2005e': VisualStudioVersion('2005e', 'Visual Studio 2005', solution_version='9.00', project_version='8.00', flat_sln=True, uses_vcxproj=False, path=path, sdk_based=sdk_based), } return versions[str(name)] def _ConvertToCygpath(path): """Convert to cygwin path if we are using cygwin.""" if sys.platform == 'cygwin': p = subprocess.Popen(['cygpath', path], stdout=subprocess.PIPE) path = p.communicate()[0].strip() return path def _DetectVisualStudioVersions(versions_to_check, force_express): """Collect the list of installed visual studio versions. Returns: A list of visual studio versions installed in descending order of usage preference. Base this on the registry and a quick check if devenv.exe exists. Only versions 8-10 are considered. Possibilities are: 2005(e) - Visual Studio 2005 (8) 2008(e) - Visual Studio 2008 (9) 2010(e) - Visual Studio 2010 (10) 2012(e) - Visual Studio 2012 (11) Where (e) is e for express editions of MSVS and blank otherwise. """ version_to_year = { '8.0': '2005', '9.0': '2008', '10.0': '2010', '11.0': '2012'} versions = [] for version in versions_to_check: # Old method of searching for which VS version is installed # We don't use the 2010-encouraged-way because we also want to get the # path to the binaries, which it doesn't offer. keys = [r'HKLM\Software\Microsoft\VisualStudio\%s' % version, r'HKLM\Software\Wow6432Node\Microsoft\VisualStudio\%s' % version, r'HKLM\Software\Microsoft\VCExpress\%s' % version, r'HKLM\Software\Wow6432Node\Microsoft\VCExpress\%s' % version] for index in range(len(keys)): path = _RegistryGetValue(keys[index], 'InstallDir') if not path: continue path = _ConvertToCygpath(path) # Check for full. full_path = os.path.join(path, 'devenv.exe') express_path = os.path.join(path, 'vcexpress.exe') if not force_express and os.path.exists(full_path): # Add this one. versions.append(_CreateVersion(version_to_year[version], os.path.join(path, '..', '..'))) # Check for express. elif os.path.exists(express_path): # Add this one. versions.append(_CreateVersion(version_to_year[version] + 'e', os.path.join(path, '..', '..'))) # The old method above does not work when only SDK is installed. keys = [r'HKLM\Software\Microsoft\VisualStudio\SxS\VC7', r'HKLM\Software\Wow6432Node\Microsoft\VisualStudio\SxS\VC7'] for index in range(len(keys)): path = _RegistryGetValue(keys[index], version) if not path: continue path = _ConvertToCygpath(path) versions.append(_CreateVersion(version_to_year[version] + 'e', os.path.join(path, '..'), sdk_based=True)) return versions def SelectVisualStudioVersion(version='auto'): """Select which version of Visual Studio projects to generate. Arguments: version: Hook to allow caller to force a particular version (vs auto). Returns: An object representing a visual studio project format version. """ # In auto mode, check environment variable for override. if version == 'auto': version = os.environ.get('GYP_MSVS_VERSION', 'auto') version_map = { 'auto': ('10.0', '9.0', '8.0', '11.0'), '2005': ('8.0',), '2005e': ('8.0',), '2008': ('9.0',), '2008e': ('9.0',), '2010': ('10.0',), '2010e': ('10.0',), '2012': ('11.0',), '2012e': ('11.0',), } version = str(version) versions = _DetectVisualStudioVersions(version_map[version], 'e' in version) if not versions: if version == 'auto': # Default to 2005 if we couldn't find anything return _CreateVersion('2005', None) else: return _CreateVersion(version, None) return versions[0]
mit
SebastianFM/SebastianFM-kernel
tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/SchedGui.py
12980
5411
# SchedGui.py - Python extension for perf script, basic GUI code for # traces drawing and overview. # # Copyright (C) 2010 by Frederic Weisbecker <[email protected]> # # This software is distributed under the terms of the GNU General # Public License ("GPL") version 2 as published by the Free Software # Foundation. try: import wx except ImportError: raise ImportError, "You need to install the wxpython lib for this script" class RootFrame(wx.Frame): Y_OFFSET = 100 RECT_HEIGHT = 100 RECT_SPACE = 50 EVENT_MARKING_WIDTH = 5 def __init__(self, sched_tracer, title, parent = None, id = -1): wx.Frame.__init__(self, parent, id, title) (self.screen_width, self.screen_height) = wx.GetDisplaySize() self.screen_width -= 10 self.screen_height -= 10 self.zoom = 0.5 self.scroll_scale = 20 self.sched_tracer = sched_tracer self.sched_tracer.set_root_win(self) (self.ts_start, self.ts_end) = sched_tracer.interval() self.update_width_virtual() self.nr_rects = sched_tracer.nr_rectangles() + 1 self.height_virtual = RootFrame.Y_OFFSET + (self.nr_rects * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)) # whole window panel self.panel = wx.Panel(self, size=(self.screen_width, self.screen_height)) # scrollable container self.scroll = wx.ScrolledWindow(self.panel) self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, self.height_virtual / self.scroll_scale) self.scroll.EnableScrolling(True, True) self.scroll.SetFocus() # scrollable drawing area self.scroll_panel = wx.Panel(self.scroll, size=(self.screen_width - 15, self.screen_height / 2)) self.scroll_panel.Bind(wx.EVT_PAINT, self.on_paint) self.scroll_panel.Bind(wx.EVT_KEY_DOWN, self.on_key_press) self.scroll_panel.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down) self.scroll.Bind(wx.EVT_PAINT, self.on_paint) self.scroll.Bind(wx.EVT_KEY_DOWN, self.on_key_press) self.scroll.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_down) self.scroll.Fit() self.Fit() self.scroll_panel.SetDimensions(-1, -1, self.width_virtual, self.height_virtual, wx.SIZE_USE_EXISTING) self.txt = None self.Show(True) def us_to_px(self, val): return val / (10 ** 3) * self.zoom def px_to_us(self, val): return (val / self.zoom) * (10 ** 3) def scroll_start(self): (x, y) = self.scroll.GetViewStart() return (x * self.scroll_scale, y * self.scroll_scale) def scroll_start_us(self): (x, y) = self.scroll_start() return self.px_to_us(x) def paint_rectangle_zone(self, nr, color, top_color, start, end): offset_px = self.us_to_px(start - self.ts_start) width_px = self.us_to_px(end - self.ts_start) offset_py = RootFrame.Y_OFFSET + (nr * (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE)) width_py = RootFrame.RECT_HEIGHT dc = self.dc if top_color is not None: (r, g, b) = top_color top_color = wx.Colour(r, g, b) brush = wx.Brush(top_color, wx.SOLID) dc.SetBrush(brush) dc.DrawRectangle(offset_px, offset_py, width_px, RootFrame.EVENT_MARKING_WIDTH) width_py -= RootFrame.EVENT_MARKING_WIDTH offset_py += RootFrame.EVENT_MARKING_WIDTH (r ,g, b) = color color = wx.Colour(r, g, b) brush = wx.Brush(color, wx.SOLID) dc.SetBrush(brush) dc.DrawRectangle(offset_px, offset_py, width_px, width_py) def update_rectangles(self, dc, start, end): start += self.ts_start end += self.ts_start self.sched_tracer.fill_zone(start, end) def on_paint(self, event): dc = wx.PaintDC(self.scroll_panel) self.dc = dc width = min(self.width_virtual, self.screen_width) (x, y) = self.scroll_start() start = self.px_to_us(x) end = self.px_to_us(x + width) self.update_rectangles(dc, start, end) def rect_from_ypixel(self, y): y -= RootFrame.Y_OFFSET rect = y / (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE) height = y % (RootFrame.RECT_HEIGHT + RootFrame.RECT_SPACE) if rect < 0 or rect > self.nr_rects - 1 or height > RootFrame.RECT_HEIGHT: return -1 return rect def update_summary(self, txt): if self.txt: self.txt.Destroy() self.txt = wx.StaticText(self.panel, -1, txt, (0, (self.screen_height / 2) + 50)) def on_mouse_down(self, event): (x, y) = event.GetPositionTuple() rect = self.rect_from_ypixel(y) if rect == -1: return t = self.px_to_us(x) + self.ts_start self.sched_tracer.mouse_down(rect, t) def update_width_virtual(self): self.width_virtual = self.us_to_px(self.ts_end - self.ts_start) def __zoom(self, x): self.update_width_virtual() (xpos, ypos) = self.scroll.GetViewStart() xpos = self.us_to_px(x) / self.scroll_scale self.scroll.SetScrollbars(self.scroll_scale, self.scroll_scale, self.width_virtual / self.scroll_scale, self.height_virtual / self.scroll_scale, xpos, ypos) self.Refresh() def zoom_in(self): x = self.scroll_start_us() self.zoom *= 2 self.__zoom(x) def zoom_out(self): x = self.scroll_start_us() self.zoom /= 2 self.__zoom(x) def on_key_press(self, event): key = event.GetRawKeyCode() if key == ord("+"): self.zoom_in() return if key == ord("-"): self.zoom_out() return key = event.GetKeyCode() (x, y) = self.scroll.GetViewStart() if key == wx.WXK_RIGHT: self.scroll.Scroll(x + 1, y) elif key == wx.WXK_LEFT: self.scroll.Scroll(x - 1, y) elif key == wx.WXK_DOWN: self.scroll.Scroll(x, y + 1) elif key == wx.WXK_UP: self.scroll.Scroll(x, y - 1)
gpl-2.0
ojii/sandlib
lib/lib-python/2.7/sqlite3/test/regression.py
3
11367
#-*- coding: ISO-8859-1 -*- # pysqlite2/test/regression.py: pysqlite regression tests # # Copyright (C) 2006-2007 Gerhard Häring <[email protected]> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import datetime import unittest import sqlite3 as sqlite class RegressionTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") def tearDown(self): self.con.close() def CheckPragmaUserVersion(self): # This used to crash pysqlite because this pragma command returns NULL for the column name cur = self.con.cursor() cur.execute("pragma user_version") def CheckPragmaSchemaVersion(self): # This still crashed pysqlite <= 2.2.1 con = sqlite.connect(":memory:", detect_types=sqlite.PARSE_COLNAMES) try: cur = self.con.cursor() cur.execute("pragma schema_version") finally: cur.close() con.close() def CheckStatementReset(self): # pysqlite 2.1.0 to 2.2.0 have the problem that not all statements are # reset before a rollback, but only those that are still in the # statement cache. The others are not accessible from the connection object. con = sqlite.connect(":memory:", cached_statements=5) cursors = [con.cursor() for x in xrange(5)] cursors[0].execute("create table test(x)") for i in range(10): cursors[0].executemany("insert into test(x) values (?)", [(x,) for x in xrange(10)]) for i in range(5): cursors[i].execute(" " * i + "select x from test") con.rollback() def CheckColumnNameWithSpaces(self): cur = self.con.cursor() cur.execute('select 1 as "foo bar [datetime]"') self.assertEqual(cur.description[0][0], "foo bar") cur.execute('select 1 as "foo baz"') self.assertEqual(cur.description[0][0], "foo baz") def CheckStatementFinalizationOnCloseDb(self): # pysqlite versions <= 2.3.3 only finalized statements in the statement # cache when closing the database. statements that were still # referenced in cursors weren't closed an could provoke " # "OperationalError: Unable to close due to unfinalised statements". con = sqlite.connect(":memory:") cursors = [] # default statement cache size is 100 for i in range(105): cur = con.cursor() cursors.append(cur) cur.execute("select 1 x union select " + str(i)) con.close() def CheckOnConflictRollback(self): if sqlite.sqlite_version_info < (3, 2, 2): return con = sqlite.connect(":memory:") con.execute("create table foo(x, unique(x) on conflict rollback)") con.execute("insert into foo(x) values (1)") try: con.execute("insert into foo(x) values (1)") except sqlite.DatabaseError: pass con.execute("insert into foo(x) values (2)") try: con.commit() except sqlite.OperationalError: self.fail("pysqlite knew nothing about the implicit ROLLBACK") def CheckWorkaroundForBuggySqliteTransferBindings(self): """ pysqlite would crash with older SQLite versions unless a workaround is implemented. """ self.con.execute("create table foo(bar)") self.con.execute("drop table foo") self.con.execute("create table foo(bar)") def CheckEmptyStatement(self): """ pysqlite used to segfault with SQLite versions 3.5.x. These return NULL for "no-operation" statements """ self.con.execute("") def CheckUnicodeConnect(self): """ With pysqlite 2.4.0 you needed to use a string or a APSW connection object for opening database connections. Formerly, both bytestrings and unicode strings used to work. Let's make sure unicode strings work in the future. """ con = sqlite.connect(u":memory:") con.close() def CheckTypeMapUsage(self): """ pysqlite until 2.4.1 did not rebuild the row_cast_map when recompiling a statement. This test exhibits the problem. """ SELECT = "select * from foo" con = sqlite.connect(":memory:",detect_types=sqlite.PARSE_DECLTYPES) con.execute("create table foo(bar timestamp)") con.execute("insert into foo(bar) values (?)", (datetime.datetime.now(),)) con.execute(SELECT) con.execute("drop table foo") con.execute("create table foo(bar integer)") con.execute("insert into foo(bar) values (5)") con.execute(SELECT) def CheckRegisterAdapter(self): """ See issue 3312. """ self.assertRaises(TypeError, sqlite.register_adapter, {}, None) def CheckSetIsolationLevel(self): """ See issue 3312. """ con = sqlite.connect(":memory:") self.assertRaises(UnicodeEncodeError, setattr, con, "isolation_level", u"\xe9") def CheckCursorConstructorCallCheck(self): """ Verifies that cursor methods check wether base class __init__ was called. """ class Cursor(sqlite.Cursor): def __init__(self, con): pass con = sqlite.connect(":memory:") cur = Cursor(con) try: cur.execute("select 4+5").fetchall() self.fail("should have raised ProgrammingError") except sqlite.ProgrammingError: pass except: self.fail("should have raised ProgrammingError") def CheckConnectionConstructorCallCheck(self): """ Verifies that connection methods check wether base class __init__ was called. """ class Connection(sqlite.Connection): def __init__(self, name): pass con = Connection(":memory:") try: cur = con.cursor() self.fail("should have raised ProgrammingError") except sqlite.ProgrammingError: pass except: self.fail("should have raised ProgrammingError") def CheckCursorRegistration(self): """ Verifies that subclassed cursor classes are correctly registered with the connection object, too. (fetch-across-rollback problem) """ class Connection(sqlite.Connection): def cursor(self): return Cursor(self) class Cursor(sqlite.Cursor): def __init__(self, con): sqlite.Cursor.__init__(self, con) con = Connection(":memory:") cur = con.cursor() cur.execute("create table foo(x)") cur.executemany("insert into foo(x) values (?)", [(3,), (4,), (5,)]) cur.execute("select x from foo") con.rollback() try: cur.fetchall() self.fail("should have raised InterfaceError") except sqlite.InterfaceError: pass except: self.fail("should have raised InterfaceError") def CheckAutoCommit(self): """ Verifies that creating a connection in autocommit mode works. 2.5.3 introduced a regression so that these could no longer be created. """ con = sqlite.connect(":memory:", isolation_level=None) def CheckPragmaAutocommit(self): """ Verifies that running a PRAGMA statement that does an autocommit does work. This did not work in 2.5.3/2.5.4. """ cur = self.con.cursor() cur.execute("create table foo(bar)") cur.execute("insert into foo(bar) values (5)") cur.execute("pragma page_size") row = cur.fetchone() def CheckSetDict(self): """ See http://bugs.python.org/issue7478 It was possible to successfully register callbacks that could not be hashed. Return codes of PyDict_SetItem were not checked properly. """ class NotHashable: def __call__(self, *args, **kw): pass def __hash__(self): raise TypeError() var = NotHashable() self.assertRaises(TypeError, self.con.create_function, var) self.assertRaises(TypeError, self.con.create_aggregate, var) self.assertRaises(TypeError, self.con.set_authorizer, var) self.assertRaises(TypeError, self.con.set_progress_handler, var) def CheckConnectionCall(self): """ Call a connection with a non-string SQL request: check error handling of the statement constructor. """ self.assertRaises(sqlite.Warning, self.con, 1) def CheckUpdateDescriptionNone(self): """ Call Cursor.update with an UPDATE query and check that it sets the cursor's description to be None. """ cur = self.con.cursor() cur.execute("CREATE TABLE foo (id INTEGER)") cur.execute("UPDATE foo SET id = 3 WHERE id = 1") self.assertEqual(cur.description, None) def CheckStatementCache(self): cur = self.con.cursor() cur.execute("CREATE TABLE foo (id INTEGER)") values = [(i,) for i in xrange(5)] cur.executemany("INSERT INTO foo (id) VALUES (?)", values) cur.execute("SELECT id FROM foo") self.assertEqual(list(cur), values) self.con.commit() cur.execute("SELECT id FROM foo") self.assertEqual(list(cur), values) def CheckRecursiveCursorUse(self): """ http://bugs.python.org/issue10811 Recursively using a cursor, such as when reusing it from a generator led to segfaults. Now we catch recursive cursor usage and raise a ProgrammingError. """ con = sqlite.connect(":memory:") cur = con.cursor() cur.execute("create table a (bar)") cur.execute("create table b (baz)") def foo(): cur.execute("insert into a (bar) values (?)", (1,)) yield 1 with self.assertRaises(sqlite.ProgrammingError): cur.executemany("insert into b (baz) values (?)", ((i,) for i in foo())) def suite(): regression_suite = unittest.makeSuite(RegressionTests, "Check") return unittest.TestSuite((regression_suite,)) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
bsd-3-clause
abhishek-ch/hue
desktop/core/src/desktop/management/commands/config_upgrade.py
37
3292
#!/usr/bin/env python # Licensed to Cloudera, Inc. under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. Cloudera, Inc. 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. """ Upgrades a configuration from a mapping file. Mapping files have a series of search/replace instructions in the form s/<old_value>/<new_value>/ This will rewrite the configurations if any changes are required. """ import logging import os, glob, string import desktop.conf import desktop.log from optparse import make_option from desktop.lib.paths import get_desktop_root from django.core.management.base import BaseCommand, CommandError from django.utils.translation import ugettext as _ LOG = logging.getLogger(__name__) class Command(BaseCommand): args = '' help = _('Upgrades the Hue configuration with a mapping file.') option_list = BaseCommand.option_list + ( make_option('--mapping_file', help=_('Location of the mapping file.')), ) """Upgrades a configuration.""" def handle(self, *args, **options): required = ("mapping_file",) for r in required: if not options.get(r): raise CommandError(_("--%(param)s is required.") % {'param': r}) # Pull out all the mappings mapping_file = options["mapping_file"] mapping_handle = open(mapping_file, 'r') mappings = [] for mapping in mapping_handle: map_parts = mapping.strip().lstrip('s/') map_parts = map_parts.rstrip('/') map_parts = map_parts.split('/') if len(map_parts) != 2: raise CommandError(_("Invalid mapping %(mapping)s in %(file)s.") % {'mapping': mapping.strip(), 'file': mapping_file}) mappings.append(map_parts) config_dir = os.getenv("HUE_CONF_DIR", get_desktop_root("conf")) for conf_file in glob.glob(os.path.join(config_dir, '*.ini')): LOG.info("Upgrading %s" % conf_file) conf_handle = open(conf_file, 'r') data = [] for line in conf_handle: # Pull apart any variables so we don't overwrite config settings data.append(line.split('=', 1)) # Iterate over mappings to perform for line in data: for mapping in mappings: old_value = mapping[0] new_value = mapping[1] if old_value in line[0]: LOG.info("Replacing %s with %s in line %s" % (old_value, new_value, '='.join(line),)) line[0] = line[0].replace(old_value, new_value) # Rewrite file with replacements made conf_handle.close() conf_handle = open(conf_file, 'w') data_to_write = ''.join([ '='.join(split) for split in data ]) conf_handle.write(data_to_write)
apache-2.0
DARKPOP/external_chromium_org_third_party_WebKit
Tools/Scripts/webkitpy/style/checkers/png.py
170
3914
# Copyright (C) 2012 Balazs Ankes ([email protected]) University of Szeged # # 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. # # 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. """Supports checking WebKit style in png files.""" import os import re from webkitpy.common import checksvnconfigfile from webkitpy.common import read_checksum_from_png from webkitpy.common.system.systemhost import SystemHost from webkitpy.common.checkout.scm.detection import SCMDetector class PNGChecker(object): """Check svn:mime-type for checking style""" categories = set(['image/png']) def __init__(self, file_path, handle_style_error, scm=None, host=None): self._file_path = file_path self._handle_style_error = handle_style_error self._host = host or SystemHost() self._fs = self._host.filesystem self._detector = scm or SCMDetector(self._fs, self._host.executive).detect_scm_system(self._fs.getcwd()) def check(self, inline=None): errorstr = "" config_file_path = "" detection = self._detector.display_name() if self._fs.exists(self._file_path) and self._file_path.endswith("-expected.png"): with self._fs.open_binary_file_for_reading(self._file_path) as filehandle: if not read_checksum_from_png.read_checksum(filehandle): self._handle_style_error(0, 'image/png', 5, "Image lacks a checksum. Generate pngs using run-webkit-tests to ensure they have a checksum.") if detection == "git": (file_missing, autoprop_missing, png_missing) = checksvnconfigfile.check(self._host, self._fs) config_file_path = checksvnconfigfile.config_file_path(self._host, self._fs) if file_missing: self._handle_style_error(0, 'image/png', 5, "There is no SVN config file. (%s)" % config_file_path) elif autoprop_missing and png_missing: self._handle_style_error(0, 'image/png', 5, checksvnconfigfile.errorstr_autoprop(config_file_path) + checksvnconfigfile.errorstr_png(config_file_path)) elif autoprop_missing: self._handle_style_error(0, 'image/png', 5, checksvnconfigfile.errorstr_autoprop(config_file_path)) elif png_missing: self._handle_style_error(0, 'image/png', 5, checksvnconfigfile.errorstr_png(config_file_path)) elif detection == "svn": prop_get = self._detector.propget("svn:mime-type", self._file_path) if prop_get != "image/png": errorstr = "Set the svn:mime-type property (svn propset svn:mime-type image/png %s)." % self._file_path self._handle_style_error(0, 'image/png', 5, errorstr)
bsd-3-clause
leiferikb/bitpop-private
chrome/test/functional/wifi_downloads.py
51
7800
#!/usr/bin/env python # Copyright (c) 2012 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. import hashlib import logging import os import time import urllib2 import pyauto_functional # Must be imported before pyauto import pyauto import pyauto_utils import chromeos_network MAX_WAIT_TIME_IN_MSEC = 15 * 60 * 1000 class WifiDownloadsTest(chromeos_network.PyNetworkUITest): """TestCase for ChromeOS Wifi Downloads This test makes a few assumptions. It needs to have access to the power strip used in pyautolib/chromeos/wifi_downloads.py. It also assumes access to the server 172.22.12.98:8080. If the server is passed a filname in the format <integer>.lf, it will generate a file of size <integer> in KB. In addition the name of the file returned is the md5 checksum of the file. In addition the download times are written to a file in /tmp/wifi_download_time.csv. All times are appended to the file if it already exists. """ def setUp(self): chromeos_network.PyNetworkUITest.setUp(self) self.InitWifiPowerStrip() # The power strip is a shared resource and if we every crash in the middle # of a test we will be in an unknown state. This returns us to 'all off'. self.TurnOffAllRouters() # Downloading files of a large size, can take a while, bump the timeout self.changer = pyauto.PyUITest.ActionTimeoutChanger(self, 4 * 1000 * 60) self.log_file_path = '/tmp/wifi_download_time.csv' def _WriteTimeToFile(self, output_file, router_name, file_size, dl_time): """Write or append a time into a csv file. This method will create or append the amount of time a download took for a given filesize and router to a file at the given path. The format of the output file is as follows: <router name A>,<file size A>,time,time,time,time,time,time,time,time <router name A>,<file size B>,time,time,time,time,time,time,time,time <router name B>,<file size C>,time,time,time,time,time,time,time,time Args: output_file: the complete path of the file to write to file_size: the size of the file, this is the row header dl_time: the amount of time in seconds """ file_data = [] if os.path.exists(output_file): file_handle = open(output_file) lines = file_handle.readlines() file_handle.close() # Convert the file to a full data structure. for line in lines: values = line.strip().split(',') file_data.append(values) for values in file_data: found_existing_time = False if values[0] == router_name and values[1] == file_size: values.append('%2.2f' % dl_time) found_existing_time = True break if not found_existing_time: new_line = [router_name, file_size, ('%2.2f' % dl_time)] file_data.append(new_line) else: file_data = [[router_name, file_size, ('%2.2f' % dl_time)]] # Write the data back out file_handle = open(output_file, 'w') for line in file_data: if len(line) > 2: file_handle.write(','.join(line)) file_handle.write('\n') file_handle.close() def _Md5Checksum(self, file_path): """Returns the md5 checksum of a file at a given path. Args: file_path: The complete path of the file to generate the md5 checksum for. """ file_handle = open(file_path, 'rb') m = hashlib.md5() while True: data = file_handle.read(8192) if not data: break m.update(data) file_handle.close() return m.hexdigest() def _ConnectToRouterAndVerify(self, router_name): """Generic routine for connecting to a router. Args: router_name: The name of the router to connect to. """ router = self.GetRouterConfig(router_name) self.RouterPower(router_name, True) self.assertTrue(self.WaitUntilWifiNetworkAvailable(router['ssid']), 'Wifi network %s never showed up.' % router['ssid']) # Verify connect did not have any errors. error = self.ConnectToWifiRouter(router_name) self.assertFalse(error, 'Failed to connect to wifi network %s. ' 'Reason: %s.' % (router['ssid'], error)) # Verify the network we connected to. ssid = self.GetConnectedWifi() self.assertEqual(ssid, router['ssid'], 'Did not successfully connect to wifi network %s.' % ssid) def _DownloadAndVerifyFile(self, download_url): """Downloads a file at a given URL and validates it This method downloads a file from a server whose filename matches the md5 checksum. Then we manually generate the md5 and check it against the filename. Args: download_url: URL of the file to download. Returns: The download time in seconds. """ start = time.time() # Make a copy of the download directory now to work around segfault downloads_dir = self.GetDownloadDirectory().value() try: self.DownloadAndWaitForStart(download_url) except AssertionError: # We need to redo this since the external server may not respond the # first time. logging.info('Could not start download. Retrying ...') self.DownloadAndWaitForStart(download_url) # Maximum wait time is set as 15 mins as an 100MB file may take somewhere # between 8-12 mins to download. self.WaitForAllDownloadsToComplete(timeout=MAX_WAIT_TIME_IN_MSEC) end = time.time() logging.info('Download took %2.2f seconds to complete' % (end - start)) downloaded_files = os.listdir(downloads_dir) self.assertEquals(len(downloaded_files), 1, msg='Expected only one file in the Downloads folder. ' 'but got this instead: %s' % ', '.join(downloaded_files)) filename = os.path.splitext(downloaded_files[0])[0] file_path = os.path.join(self.GetDownloadDirectory().value(), downloaded_files[0]) md5_sum = self._Md5Checksum(file_path) md5_url = download_url[:-4] + '.md5' # replacing .slf with .md5 md5_file = urllib2.urlopen(md5_url).readlines()[0] self.assertTrue(md5_file.rstrip().endswith(md5_sum.encode()), msg='Unexpected checksum. The download is incomplete.') return end - start def testDownload1MBFile(self): """Test downloading a 1MB file from a wireless router.""" download_url = 'http://172.22.12.98:80/downloads/1M.slf' router_name = 'Nfiniti' self._ConnectToRouterAndVerify(router_name) download_time = self._DownloadAndVerifyFile(download_url) self._WriteTimeToFile(self.log_file_path, router_name, '1MB', download_time) self.DisconnectFromWifiNetwork() def testDownload10MBFile(self): """Test downloading a 10MB file from a wireless router.""" download_url = 'http://172.22.12.98:80/downloads/10M.slf' router_name = 'Linksys_WRT54G2' self._ConnectToRouterAndVerify(router_name) download_time = self._DownloadAndVerifyFile(download_url) self._WriteTimeToFile(self.log_file_path, router_name, '10MB', download_time) self.DisconnectFromWifiNetwork() def testDownload100MBFile(self): """Test downloading a 100MB file from a wireless router.""" download_url = 'http://172.22.12.98:80/downloads/100M.slf' router_name = 'Trendnet_639gr_4' self._ConnectToRouterAndVerify(router_name) download_time = self._DownloadAndVerifyFile(download_url) self._WriteTimeToFile(self.log_file_path, router_name, '100MB', download_time) self.DisconnectFromWifiNetwork() if __name__ == '__main__': pyauto_functional.Main()
bsd-3-clause
smalls257/VRvisu
Library/External.LCA_RESTRICTED/Languages/CPython/27/Lib/test/test_frozen.py
133
1236
# Test the frozen module defined in frozen.c. from test.test_support import captured_stdout, run_unittest import unittest import sys class FrozenTests(unittest.TestCase): def test_frozen(self): with captured_stdout() as stdout: try: import __hello__ except ImportError, x: self.fail("import __hello__ failed:" + str(x)) try: import __phello__ except ImportError, x: self.fail("import __phello__ failed:" + str(x)) try: import __phello__.spam except ImportError, x: self.fail("import __phello__.spam failed:" + str(x)) try: import __phello__.foo except ImportError: pass else: self.fail("import __phello__.foo should have failed") self.assertEqual(stdout.getvalue(), 'Hello world...\nHello world...\nHello world...\n') del sys.modules['__hello__'] del sys.modules['__phello__'] del sys.modules['__phello__.spam'] def test_main(): run_unittest(FrozenTests) if __name__ == '__main__': test_main()
gpl-3.0
hantek/fuel
fuel/converters/__init__.py
6
1213
"""Data conversion modules for built-in datasets. Conversion submodules generate an HDF5 file that is compatible with their corresponding built-in dataset. Conversion functions accept a single argument, `subparser`, which is an `argparse.ArgumentParser` instance that it needs to fill with its own specific arguments. They should set a `func` default argument for the subparser with a function that will get called and given the parsed command-line arguments, and is expected to download the required files. """ from fuel.converters import adult from fuel.converters import binarized_mnist from fuel.converters import caltech101_silhouettes from fuel.converters import cifar10 from fuel.converters import cifar100 from fuel.converters import iris from fuel.converters import mnist from fuel.converters import svhn __version__ = '0.2' all_converters = ( ('adult', adult.fill_subparser), ('binarized_mnist', binarized_mnist.fill_subparser), ('caltech101_silhouettes', caltech101_silhouettes.fill_subparser), ('cifar10', cifar10.fill_subparser), ('cifar100', cifar100.fill_subparser), ('iris', iris.fill_subparser), ('mnist', mnist.fill_subparser), ('svhn', svhn.fill_subparser))
mit
Thraxis/pymedusa
lib/adba/aniDBtvDBmaper.py
50
2174
#!/usr/bin/env python # # This file is part of aDBa. # # aDBa 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. # # aDBa 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 aDBa. If not, see <http://www.gnu.org/licenses/>. import os try: import xml.etree.cElementTree as etree except ImportError: import xml.etree.ElementTree as etree import aniDBfileInfo as fileInfo class TvDBMap(): def __init__(self,filePath=None): self.xmlMap = fileInfo.read_tvdb_map_xml(filePath) def get_tvdb_for_anidb(self,anidb_id): return self._get_x_for_y(anidb_id,"anidbid","tvdbid") def get_anidb_for_tvdb(self,tvdb_id): return self._get_x_for_y(tvdb_id,"tvdbid","anidbid") def _get_x_for_y(self,xValue,x,y): #print("searching "+x+" with the value "+str(xValue)+" and want to give back "+y) xValue = str(xValue) for anime in self.xmlMap.findall("anime"): try: if anime.get(x,False) == xValue: return int(anime.get(y,0)) except ValueError, e: continue return 0 def get_season_episode_for_anidb_absoluteNumber(self,anidb_id,absoluteNumber): # NOTE: this cant be done without the length of each season from thetvdb #TODO: implement season = 0 episode = 0 for anime in self.xmlMap.findall("anime"): if int(anime.get("anidbid",False)) == anidb_id: defaultSeason = int(anime.get("defaulttvdbseason",1)) return (season,episode) def get_season_episode_for_tvdb_absoluteNumber(self,anidb_id,absoluteNumber): #TODO: implement season = 0 episode = 0 return (season,episode)
gpl-3.0
hustlzp/eve
eve/__init__.py
9
1992
# -*- coding: utf-8 -*- """ Eve ~~~ An out-of-the-box REST Web API that's as dangerous as you want it to be. :copyright: (c) 2015 by Nicola Iarocci. :license: BSD, see LICENSE for more details. .. versionchanged:: 0.5 'SERVER_NAME' removed. 'QUERY_WHERE' added. 'QUERY_SORT' added. 'QUERY_PAGE' added. 'QUERY_MAX_RESULTS' added. 'QUERY_PROJECTION' added. 'QUERY_EMBEDDED' added. 'RFC1123_DATE_FORMAT' added. .. versionchanged:: 0.4 'META' defaults to '_meta'. 'ERROR' defaults to '_error'. Remove unnecessary commented code. .. versionchanged:: 0.2 'LINKS' defaults to '_links'. 'ITEMS' defaults to '_items'. 'STATUS' defaults to 'status'. 'ISSUES' defaults to 'issues'. .. versionchanged:: 0.1.1 'SERVER_NAME' defaults to None. .. versionchagned:: 0.0.9 'DATE_FORMAT now using GMT instead of UTC. """ __version__ = '0.6-dev0' # RFC 1123 (ex RFC 822) DATE_FORMAT = '%a, %d %b %Y %H:%M:%S GMT' RFC1123_DATE_FORMAT = '%a, %d %b %Y %H:%M:%S GMT' URL_PREFIX = '' API_VERSION = '' PAGINATION = True PAGINATION_LIMIT = 50 PAGINATION_DEFAULT = 25 ID_FIELD = '_id' CACHE_CONTROL = 'max-age=10,must-revalidate' # TODO confirm this value CACHE_EXPIRES = 10 RESOURCE_METHODS = ['GET'] ITEM_METHODS = ['GET'] ITEM_LOOKUP = True ITEM_LOOKUP_FIELD = ID_FIELD ITEM_URL = '[a-f0-9]{24}' STATUS_OK = "OK" STATUS_ERR = "ERR" LAST_UPDATED = '_updated' DATE_CREATED = '_created' ISSUES = '_issues' STATUS = '_status' ERROR = '_error' ITEMS = '_items' LINKS = '_links' ETAG = '_etag' VERSION = '_version' META = '_meta' QUERY_WHERE = 'where' QUERY_SORT = 'sort' QUERY_PAGE = 'page' QUERY_MAX_RESULTS = 'max_results' QUERY_EMBEDDED = 'embedded' QUERY_PROJECTION = 'projection' VALIDATION_ERROR_STATUS = 422 VALIDATION_ERROR_AS_LIST = False # must be the last line (will raise W402 on pyflakes) from eve.flaskapp import Eve # noqa
bsd-3-clause
berth64/modded_modded_1257ad
source/header_animations.py
6
5723
arf_blend_in_0 = 0x00000001 arf_blend_in_1 = 0x00000002 arf_blend_in_2 = 0x00000003 arf_blend_in_3 = 0x00000004 arf_blend_in_4 = 0x00000005 arf_blend_in_5 = 0x00000006 arf_blend_in_6 = 0x00000007 arf_blend_in_7 = 0x00000008 arf_blend_in_8 = 0x00000009 arf_blend_in_9 = 0x0000000a arf_blend_in_10 = 0x0000000b arf_blend_in_11 = 0x0000000c arf_blend_in_12 = 0x0000000d arf_blend_in_13 = 0x0000000e arf_blend_in_14 = 0x0000000f arf_blend_in_15 = 0x00000010 arf_blend_in_16 = 0x00000011 arf_blend_in_17 = 0x00000012 arf_blend_in_18 = 0x00000013 arf_blend_in_19 = 0x00000014 arf_blend_in_20 = 0x00000015 arf_blend_in_21 = 0x00000016 arf_blend_in_22 = 0x00000017 arf_blend_in_23 = 0x00000018 arf_blend_in_24 = 0x00000019 arf_blend_in_25 = 0x0000001a arf_blend_in_26 = 0x0000001b arf_blend_in_27 = 0x0000001c arf_blend_in_28 = 0x0000001d arf_blend_in_29 = 0x0000001e arf_blend_in_30 = 0x0000001f arf_blend_in_31 = 0x00000020 arf_blend_in_32 = 0x00000021 arf_blend_in_48 = 0x00000031 arf_blend_in_64 = 0x00000041 arf_blend_in_128 = 0x00000081 arf_blend_in_254 = 0x000000ff arf_make_walk_sound = 0x00000100 arf_make_custom_sound = 0x00000200 ##arf_start_pos_0 = 0x00000100 ##arf_end_pos_0 = 0x00000200 ##arf_start_pos_0_25 = 0x00000400 ##arf_end_pos_0_25 = 0x00000800 ##arf_start_pos_0_5 = 0x00001000 ##arf_end_pos_0_5 = 0x00002000 ##arf_start_pos_0_75 = 0x00004000 ##arf_end_pos_0_75 = 0x00008000 ##arf_loop_pos_0 = arf_start_pos_0 | arf_end_pos_0 ##arf_loop_pos_0_25 = arf_start_pos_0_25 | arf_end_pos_0_25 ##arf_loop_pos_0_5 = arf_start_pos_0_5 | arf_end_pos_0_5 ##arf_loop_pos_0_75 = arf_start_pos_0_75 | arf_end_pos_0_75 ##arf_phase_even = 0x00010000 ##arf_phase_odd = 0x00030000 ##arf_phase_inverse_even = 0x00050000 ##arf_phase_inverse_odd = 0x00070000 arf_two_handed_blade = 0x01000000 arf_lancer = 0x02000000 arf_stick_item_to_left_hand = 0x04000000 arf_cyclic = 0x10000000 arf_use_walk_progress = 0x20000000 arf_use_stand_progress = 0x40000000 arf_use_inv_walk_progress = 0x80000000 ##arf_walk = arf_phase_even | arf_cyclic #----------------------------------------- amf_priority_mask = 0x00000fff amf_rider_rot_bow = 0x00001000 amf_rider_rot_throw = 0x00002000 amf_rider_rot_crossbow = 0x00003000 amf_rider_rot_pistol = 0x00004000 amf_rider_rot_overswing = 0x00005000 amf_rider_rot_thrust = 0x00006000 amf_rider_rot_swing_right = 0x00007000 amf_rider_rot_swing_left = 0x00008000 amf_rider_rot_couched_lance = 0x00009000 amf_rider_rot_shield = 0x0000a000 amf_rider_rot_defend = 0x0000b000 amf_start_instantly = 0x00010000 amf_use_cycle_period = 0x00100000 amf_use_weapon_speed = 0x00200000 amf_use_defend_speed = 0x00400000 amf_accurate_body = 0x00800000 amf_client_prediction = 0x01000000 amf_play = 0x02000000 amf_keep = 0x04000000 amf_restart = 0x08000000 # restart animation even if it is the current animation amf_hide_weapon = 0x10000000 amf_client_owner_prediction = 0x20000000 amf_use_inertia = 0x40000000 amf_continue_to_next = 0x80000000 #----------------------------------------- acf_synch_with_horse = 0x00000001 acf_align_with_ground = 0x00000002 acf_enforce_lowerbody = 0x00000100 acf_enforce_rightside = 0x00000200 acf_enforce_all = 0x00000400 acf_parallels_for_look_slope = 0x00001000 acf_lock_camera = 0x00002000 acf_displace_position = 0x00004000 acf_ignore_slope = 0x00008000 acf_thrust = 0x00010000 acf_right_cut = 0x00020000 acf_left_cut = 0x00040000 acf_overswing = 0x00080000 acf_rot_vertical_mask = 0x00300000 acf_rot_vertical_bow = 0x00100000 acf_rot_vertical_sword = 0x00200000 acf_anim_length_mask = 0xff000000 acf_anim_length_bits = 24 def acf_anim_length(x): return (x << acf_anim_length_bits) & acf_anim_length_mask #------------------------------------------ #### Do not edit these lines def get_byte(f): if f == 0.0: return 0 i = int(f * 255.0) if (i< 1): i=1 elif (i > 255): i = 255 return i def pack2f(a,b): ai = get_byte(a) bi = get_byte(b) return ((bi << 8) | ai) def pack4f(a,b,c,d): ai = get_byte(a) bi = get_byte(b) ci = get_byte(c) di = get_byte(d) return ((di << 24) | (ci << 16) | (bi << 8) | ai)
agpl-3.0
wimnat/ansible
lib/ansible/plugins/connection/ssh.py
8
56883
# Copyright (c) 2012, Michael DeHaan <[email protected]> # Copyright 2015 Abhijit Menon-Sen <[email protected]> # Copyright 2017 Toshio Kuratomi <[email protected]> # Copyright (c) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) __metaclass__ = type DOCUMENTATION = ''' name: ssh short_description: connect via ssh client binary description: - This connection plugin allows ansible to communicate to the target machines via normal ssh command line. - Ansible does not expose a channel to allow communication between the user and the ssh process to accept a password manually to decrypt an ssh key when using this connection plugin (which is the default). The use of ``ssh-agent`` is highly recommended. author: ansible (@core) version_added: historical options: host: description: Hostname/ip to connect to. default: inventory_hostname vars: - name: ansible_host - name: ansible_ssh_host host_key_checking: description: Determines if ssh should check host keys type: boolean ini: - section: defaults key: 'host_key_checking' - section: ssh_connection key: 'host_key_checking' version_added: '2.5' env: - name: ANSIBLE_HOST_KEY_CHECKING - name: ANSIBLE_SSH_HOST_KEY_CHECKING version_added: '2.5' vars: - name: ansible_host_key_checking version_added: '2.5' - name: ansible_ssh_host_key_checking version_added: '2.5' password: description: Authentication password for the C(remote_user). Can be supplied as CLI option. vars: - name: ansible_password - name: ansible_ssh_pass - name: ansible_ssh_password sshpass_prompt: description: Password prompt that sshpass should search for. Supported by sshpass 1.06 and up. default: '' ini: - section: 'ssh_connection' key: 'sshpass_prompt' env: - name: ANSIBLE_SSHPASS_PROMPT vars: - name: ansible_sshpass_prompt version_added: '2.10' ssh_args: description: Arguments to pass to all ssh cli tools default: '-C -o ControlMaster=auto -o ControlPersist=60s' ini: - section: 'ssh_connection' key: 'ssh_args' env: - name: ANSIBLE_SSH_ARGS vars: - name: ansible_ssh_args version_added: '2.7' ssh_common_args: description: Common extra args for all ssh CLI tools ini: - section: 'ssh_connection' key: 'ssh_common_args' version_added: '2.7' env: - name: ANSIBLE_SSH_COMMON_ARGS version_added: '2.7' vars: - name: ansible_ssh_common_args ssh_executable: default: ssh description: - This defines the location of the ssh binary. It defaults to ``ssh`` which will use the first ssh binary available in $PATH. - This option is usually not required, it might be useful when access to system ssh is restricted, or when using ssh wrappers to connect to remote hosts. env: [{name: ANSIBLE_SSH_EXECUTABLE}] ini: - {key: ssh_executable, section: ssh_connection} #const: ANSIBLE_SSH_EXECUTABLE version_added: "2.2" vars: - name: ansible_ssh_executable version_added: '2.7' sftp_executable: default: sftp description: - This defines the location of the sftp binary. It defaults to ``sftp`` which will use the first binary available in $PATH. env: [{name: ANSIBLE_SFTP_EXECUTABLE}] ini: - {key: sftp_executable, section: ssh_connection} version_added: "2.6" vars: - name: ansible_sftp_executable version_added: '2.7' scp_executable: default: scp description: - This defines the location of the scp binary. It defaults to `scp` which will use the first binary available in $PATH. env: [{name: ANSIBLE_SCP_EXECUTABLE}] ini: - {key: scp_executable, section: ssh_connection} version_added: "2.6" vars: - name: ansible_scp_executable version_added: '2.7' scp_extra_args: description: Extra exclusive to the ``scp`` CLI vars: - name: ansible_scp_extra_args env: - name: ANSIBLE_SCP_EXTRA_ARGS version_added: '2.7' ini: - key: scp_extra_args section: ssh_connection version_added: '2.7' sftp_extra_args: description: Extra exclusive to the ``sftp`` CLI vars: - name: ansible_sftp_extra_args env: - name: ANSIBLE_SFTP_EXTRA_ARGS version_added: '2.7' ini: - key: sftp_extra_args section: ssh_connection version_added: '2.7' ssh_extra_args: description: Extra exclusive to the 'ssh' CLI vars: - name: ansible_ssh_extra_args env: - name: ANSIBLE_SSH_EXTRA_ARGS version_added: '2.7' ini: - key: ssh_extra_args section: ssh_connection version_added: '2.7' retries: # constant: ANSIBLE_SSH_RETRIES description: Number of attempts to connect. default: 3 type: integer env: - name: ANSIBLE_SSH_RETRIES ini: - section: connection key: retries - section: ssh_connection key: retries vars: - name: ansible_ssh_retries version_added: '2.7' port: description: Remote port to connect to. type: int default: 22 ini: - section: defaults key: remote_port env: - name: ANSIBLE_REMOTE_PORT vars: - name: ansible_port - name: ansible_ssh_port remote_user: description: - User name with which to login to the remote server, normally set by the remote_user keyword. - If no user is supplied, Ansible will let the ssh client binary choose the user as it normally ini: - section: defaults key: remote_user env: - name: ANSIBLE_REMOTE_USER vars: - name: ansible_user - name: ansible_ssh_user pipelining: default: ANSIBLE_PIPELINING description: - Pipelining reduces the number of SSH operations required to execute a module on the remote server, by executing many Ansible modules without actual file transfer. - This can result in a very significant performance improvement when enabled. - However this conflicts with privilege escalation (become). For example, when using sudo operations you must first disable 'requiretty' in the sudoers file for the target hosts, which is why this feature is disabled by default. env: - name: ANSIBLE_PIPELINING - name: ANSIBLE_SSH_PIPELINING ini: - section: defaults key: pipelining - section: ssh_connection key: pipelining type: boolean vars: - name: ansible_pipelining - name: ansible_ssh_pipelining private_key_file: description: - Path to private key file to use for authentication ini: - section: defaults key: private_key_file env: - name: ANSIBLE_PRIVATE_KEY_FILE vars: - name: ansible_private_key_file - name: ansible_ssh_private_key_file control_path: description: - This is the location to save ssh's ControlPath sockets, it uses ssh's variable substitution. - Since 2.3, if null, ansible will generate a unique hash. Use `%(directory)s` to indicate where to use the control dir path setting. env: - name: ANSIBLE_SSH_CONTROL_PATH ini: - key: control_path section: ssh_connection vars: - name: ansible_control_path version_added: '2.7' control_path_dir: default: ~/.ansible/cp description: - This sets the directory to use for ssh control path if the control path setting is null. - Also, provides the `%(directory)s` variable for the control path setting. env: - name: ANSIBLE_SSH_CONTROL_PATH_DIR ini: - section: ssh_connection key: control_path_dir vars: - name: ansible_control_path_dir version_added: '2.7' sftp_batch_mode: default: 'yes' description: 'TODO: write it' env: [{name: ANSIBLE_SFTP_BATCH_MODE}] ini: - {key: sftp_batch_mode, section: ssh_connection} type: bool vars: - name: ansible_sftp_batch_mode version_added: '2.7' scp_if_ssh: default: smart description: - "Preferred method to use when transfering files over ssh" - When set to smart, Ansible will try them until one succeeds or they all fail - If set to True, it will force 'scp', if False it will use 'sftp' env: [{name: ANSIBLE_SCP_IF_SSH}] ini: - {key: scp_if_ssh, section: ssh_connection} vars: - name: ansible_scp_if_ssh version_added: '2.7' use_tty: version_added: '2.5' default: 'yes' description: add -tt to ssh commands to force tty allocation env: [{name: ANSIBLE_SSH_USETTY}] ini: - {key: usetty, section: ssh_connection} type: bool vars: - name: ansible_ssh_use_tty version_added: '2.7' ''' import errno import fcntl import hashlib import os import pty import re import subprocess import time from functools import wraps from ansible import constants as C from ansible.errors import ( AnsibleAuthenticationFailure, AnsibleConnectionFailure, AnsibleError, AnsibleFileNotFound, ) from ansible.errors import AnsibleOptionsError from ansible.module_utils.compat import selectors from ansible.module_utils.six import PY3, text_type, binary_type from ansible.module_utils.six.moves import shlex_quote from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils.parsing.convert_bool import BOOLEANS, boolean from ansible.plugins.connection import ConnectionBase, BUFSIZE from ansible.plugins.shell.powershell import _parse_clixml from ansible.utils.display import Display from ansible.utils.path import unfrackpath, makedirs_safe display = Display() b_NOT_SSH_ERRORS = (b'Traceback (most recent call last):', # Python-2.6 when there's an exception # while invoking a script via -m b'PHP Parse error:', # Php always returns error 255 ) SSHPASS_AVAILABLE = None class AnsibleControlPersistBrokenPipeError(AnsibleError): ''' ControlPersist broken pipe ''' pass def _handle_error(remaining_retries, command, return_tuple, no_log, host, display=display): # sshpass errors if command == b'sshpass': # Error 5 is invalid/incorrect password. Raise an exception to prevent retries from locking the account. if return_tuple[0] == 5: msg = 'Invalid/incorrect username/password. Skipping remaining {0} retries to prevent account lockout:'.format(remaining_retries) if remaining_retries <= 0: msg = 'Invalid/incorrect password:' if no_log: msg = '{0} <error censored due to no log>'.format(msg) else: msg = '{0} {1}'.format(msg, to_native(return_tuple[2]).rstrip()) raise AnsibleAuthenticationFailure(msg) # sshpass returns codes are 1-6. We handle 5 previously, so this catches other scenarios. # No exception is raised, so the connection is retried - except when attempting to use # sshpass_prompt with an sshpass that won't let us pass -P, in which case we fail loudly. elif return_tuple[0] in [1, 2, 3, 4, 6]: msg = 'sshpass error:' if no_log: msg = '{0} <error censored due to no log>'.format(msg) else: details = to_native(return_tuple[2]).rstrip() if "sshpass: invalid option -- 'P'" in details: details = 'Installed sshpass version does not support customized password prompts. ' \ 'Upgrade sshpass to use sshpass_prompt, or otherwise switch to ssh keys.' raise AnsibleError('{0} {1}'.format(msg, details)) msg = '{0} {1}'.format(msg, details) if return_tuple[0] == 255: SSH_ERROR = True for signature in b_NOT_SSH_ERRORS: if signature in return_tuple[1]: SSH_ERROR = False break if SSH_ERROR: msg = "Failed to connect to the host via ssh:" if no_log: msg = '{0} <error censored due to no log>'.format(msg) else: msg = '{0} {1}'.format(msg, to_native(return_tuple[2]).rstrip()) raise AnsibleConnectionFailure(msg) # For other errors, no exception is raised so the connection is retried and we only log the messages if 1 <= return_tuple[0] <= 254: msg = u"Failed to connect to the host via ssh:" if no_log: msg = u'{0} <error censored due to no log>'.format(msg) else: msg = u'{0} {1}'.format(msg, to_text(return_tuple[2]).rstrip()) display.vvv(msg, host=host) def _ssh_retry(func): """ Decorator to retry ssh/scp/sftp in the case of a connection failure Will retry if: * an exception is caught * ssh returns 255 Will not retry if * sshpass returns 5 (invalid password, to prevent account lockouts) * remaining_tries is < 2 * retries limit reached """ @wraps(func) def wrapped(self, *args, **kwargs): remaining_tries = int(C.ANSIBLE_SSH_RETRIES) + 1 cmd_summary = u"%s..." % to_text(args[0]) conn_password = self.get_option('password') or self._play_context.password for attempt in range(remaining_tries): cmd = args[0] if attempt != 0 and conn_password and isinstance(cmd, list): # If this is a retry, the fd/pipe for sshpass is closed, and we need a new one self.sshpass_pipe = os.pipe() cmd[1] = b'-d' + to_bytes(self.sshpass_pipe[0], nonstring='simplerepr', errors='surrogate_or_strict') try: try: return_tuple = func(self, *args, **kwargs) if self._play_context.no_log: display.vvv(u'rc=%s, stdout and stderr censored due to no log' % return_tuple[0], host=self.host) else: display.vvv(return_tuple, host=self.host) # 0 = success # 1-254 = remote command return code # 255 could be a failure from the ssh command itself except (AnsibleControlPersistBrokenPipeError): # Retry one more time because of the ControlPersist broken pipe (see #16731) cmd = args[0] if conn_password and isinstance(cmd, list): # This is a retry, so the fd/pipe for sshpass is closed, and we need a new one self.sshpass_pipe = os.pipe() cmd[1] = b'-d' + to_bytes(self.sshpass_pipe[0], nonstring='simplerepr', errors='surrogate_or_strict') display.vvv(u"RETRYING BECAUSE OF CONTROLPERSIST BROKEN PIPE") return_tuple = func(self, *args, **kwargs) remaining_retries = remaining_tries - attempt - 1 _handle_error(remaining_retries, cmd[0], return_tuple, self._play_context.no_log, self.host) break # 5 = Invalid/incorrect password from sshpass except AnsibleAuthenticationFailure: # Raising this exception, which is subclassed from AnsibleConnectionFailure, prevents further retries raise except (AnsibleConnectionFailure, Exception) as e: if attempt == remaining_tries - 1: raise else: pause = 2 ** attempt - 1 if pause > 30: pause = 30 if isinstance(e, AnsibleConnectionFailure): msg = u"ssh_retry: attempt: %d, ssh return code is 255. cmd (%s), pausing for %d seconds" % (attempt + 1, cmd_summary, pause) else: msg = (u"ssh_retry: attempt: %d, caught exception(%s) from cmd (%s), " u"pausing for %d seconds" % (attempt + 1, to_text(e), cmd_summary, pause)) display.vv(msg, host=self.host) time.sleep(pause) continue return return_tuple return wrapped class Connection(ConnectionBase): ''' ssh based connections ''' transport = 'ssh' has_pipelining = True def __init__(self, *args, **kwargs): super(Connection, self).__init__(*args, **kwargs) self.host = self._play_context.remote_addr self.port = self._play_context.port self.user = self._play_context.remote_user self.control_path = C.ANSIBLE_SSH_CONTROL_PATH self.control_path_dir = C.ANSIBLE_SSH_CONTROL_PATH_DIR # Windows operates differently from a POSIX connection/shell plugin, # we need to set various properties to ensure SSH on Windows continues # to work if getattr(self._shell, "_IS_WINDOWS", False): self.has_native_async = True self.always_pipeline_modules = True self.module_implementation_preferences = ('.ps1', '.exe', '') self.allow_executable = False # The connection is created by running ssh/scp/sftp from the exec_command, # put_file, and fetch_file methods, so we don't need to do any connection # management here. def _connect(self): return self @staticmethod def _create_control_path(host, port, user, connection=None, pid=None): '''Make a hash for the controlpath based on con attributes''' pstring = '%s-%s-%s' % (host, port, user) if connection: pstring += '-%s' % connection if pid: pstring += '-%s' % to_text(pid) m = hashlib.sha1() m.update(to_bytes(pstring)) digest = m.hexdigest() cpath = '%(directory)s/' + digest[:10] return cpath @staticmethod def _sshpass_available(): global SSHPASS_AVAILABLE # We test once if sshpass is available, and remember the result. It # would be nice to use distutils.spawn.find_executable for this, but # distutils isn't always available; shutils.which() is Python3-only. if SSHPASS_AVAILABLE is None: try: p = subprocess.Popen(["sshpass"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() SSHPASS_AVAILABLE = True except OSError: SSHPASS_AVAILABLE = False return SSHPASS_AVAILABLE @staticmethod def _persistence_controls(b_command): ''' Takes a command array and scans it for ControlPersist and ControlPath settings and returns two booleans indicating whether either was found. This could be smarter, e.g. returning false if ControlPersist is 'no', but for now we do it simple way. ''' controlpersist = False controlpath = False for b_arg in (a.lower() for a in b_command): if b'controlpersist' in b_arg: controlpersist = True elif b'controlpath' in b_arg: controlpath = True return controlpersist, controlpath def _add_args(self, b_command, b_args, explanation): """ Adds arguments to the ssh command and displays a caller-supplied explanation of why. :arg b_command: A list containing the command to add the new arguments to. This list will be modified by this method. :arg b_args: An iterable of new arguments to add. This iterable is used more than once so it must be persistent (ie: a list is okay but a StringIO would not) :arg explanation: A text string containing explaining why the arguments were added. It will be displayed with a high enough verbosity. .. note:: This function does its work via side-effect. The b_command list has the new arguments appended. """ display.vvvvv(u'SSH: %s: (%s)' % (explanation, ')('.join(to_text(a) for a in b_args)), host=self._play_context.remote_addr) b_command += b_args def _build_command(self, binary, *other_args): ''' Takes a binary (ssh, scp, sftp) and optional extra arguments and returns a command line as an array that can be passed to subprocess.Popen. ''' b_command = [] conn_password = self.get_option('password') or self._play_context.password # # First, the command to invoke # # If we want to use password authentication, we have to set up a pipe to # write the password to sshpass. if conn_password: if not self._sshpass_available(): raise AnsibleError("to use the 'ssh' connection type with passwords, you must install the sshpass program") self.sshpass_pipe = os.pipe() b_command += [b'sshpass', b'-d' + to_bytes(self.sshpass_pipe[0], nonstring='simplerepr', errors='surrogate_or_strict')] password_prompt = self.get_option('sshpass_prompt') if password_prompt: b_command += [b'-P', to_bytes(password_prompt, errors='surrogate_or_strict')] if binary == 'ssh': b_command += [to_bytes(self._play_context.ssh_executable, errors='surrogate_or_strict')] else: b_command += [to_bytes(binary, errors='surrogate_or_strict')] # # Next, additional arguments based on the configuration. # # sftp batch mode allows us to correctly catch failed transfers, but can # be disabled if the client side doesn't support the option. However, # sftp batch mode does not prompt for passwords so it must be disabled # if not using controlpersist and using sshpass if binary == 'sftp' and C.DEFAULT_SFTP_BATCH_MODE: if conn_password: b_args = [b'-o', b'BatchMode=no'] self._add_args(b_command, b_args, u'disable batch mode for sshpass') b_command += [b'-b', b'-'] if self._play_context.verbosity > 3: b_command.append(b'-vvv') # # Next, we add [ssh_connection]ssh_args from ansible.cfg. # ssh_args = self.get_option('ssh_args') if ssh_args: b_args = [to_bytes(a, errors='surrogate_or_strict') for a in self._split_ssh_args(ssh_args)] self._add_args(b_command, b_args, u"ansible.cfg set ssh_args") # Now we add various arguments controlled by configuration file settings # (e.g. host_key_checking) or inventory variables (ansible_ssh_port) or # a combination thereof. if not C.HOST_KEY_CHECKING: b_args = (b"-o", b"StrictHostKeyChecking=no") self._add_args(b_command, b_args, u"ANSIBLE_HOST_KEY_CHECKING/host_key_checking disabled") if self._play_context.port is not None: b_args = (b"-o", b"Port=" + to_bytes(self._play_context.port, nonstring='simplerepr', errors='surrogate_or_strict')) self._add_args(b_command, b_args, u"ANSIBLE_REMOTE_PORT/remote_port/ansible_port set") key = self._play_context.private_key_file if key: b_args = (b"-o", b'IdentityFile="' + to_bytes(os.path.expanduser(key), errors='surrogate_or_strict') + b'"') self._add_args(b_command, b_args, u"ANSIBLE_PRIVATE_KEY_FILE/private_key_file/ansible_ssh_private_key_file set") if not conn_password: self._add_args( b_command, ( b"-o", b"KbdInteractiveAuthentication=no", b"-o", b"PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey", b"-o", b"PasswordAuthentication=no" ), u"ansible_password/ansible_ssh_password not set" ) user = self._play_context.remote_user if user: self._add_args( b_command, (b"-o", b'User="%s"' % to_bytes(self._play_context.remote_user, errors='surrogate_or_strict')), u"ANSIBLE_REMOTE_USER/remote_user/ansible_user/user/-u set" ) self._add_args( b_command, (b"-o", b"ConnectTimeout=" + to_bytes(self._play_context.timeout, errors='surrogate_or_strict', nonstring='simplerepr')), u"ANSIBLE_TIMEOUT/timeout set" ) # Add in any common or binary-specific arguments from the PlayContext # (i.e. inventory or task settings or overrides on the command line). for opt in (u'ssh_common_args', u'{0}_extra_args'.format(binary)): attr = getattr(self._play_context, opt, None) if attr is not None: b_args = [to_bytes(a, errors='surrogate_or_strict') for a in self._split_ssh_args(attr)] self._add_args(b_command, b_args, u"PlayContext set %s" % opt) # Check if ControlPersist is enabled and add a ControlPath if one hasn't # already been set. controlpersist, controlpath = self._persistence_controls(b_command) if controlpersist: self._persistent = True if not controlpath: cpdir = unfrackpath(self.control_path_dir) b_cpdir = to_bytes(cpdir, errors='surrogate_or_strict') # The directory must exist and be writable. makedirs_safe(b_cpdir, 0o700) if not os.access(b_cpdir, os.W_OK): raise AnsibleError("Cannot write to ControlPath %s" % to_native(cpdir)) if not self.control_path: self.control_path = self._create_control_path( self.host, self.port, self.user ) b_args = (b"-o", b"ControlPath=" + to_bytes(self.control_path % dict(directory=cpdir), errors='surrogate_or_strict')) self._add_args(b_command, b_args, u"found only ControlPersist; added ControlPath") # Finally, we add any caller-supplied extras. if other_args: b_command += [to_bytes(a) for a in other_args] return b_command def _send_initial_data(self, fh, in_data, ssh_process): ''' Writes initial data to the stdin filehandle of the subprocess and closes it. (The handle must be closed; otherwise, for example, "sftp -b -" will just hang forever waiting for more commands.) ''' display.debug(u'Sending initial data') try: fh.write(to_bytes(in_data)) fh.close() except (OSError, IOError) as e: # The ssh connection may have already terminated at this point, with a more useful error # Only raise AnsibleConnectionFailure if the ssh process is still alive time.sleep(0.001) ssh_process.poll() if getattr(ssh_process, 'returncode', None) is None: raise AnsibleConnectionFailure( 'Data could not be sent to remote host "%s". Make sure this host can be reached ' 'over ssh: %s' % (self.host, to_native(e)), orig_exc=e ) display.debug(u'Sent initial data (%d bytes)' % len(in_data)) # Used by _run() to kill processes on failures @staticmethod def _terminate_process(p): """ Terminate a process, ignoring errors """ try: p.terminate() except (OSError, IOError): pass # This is separate from _run() because we need to do the same thing for stdout # and stderr. def _examine_output(self, source, state, b_chunk, sudoable): ''' Takes a string, extracts complete lines from it, tests to see if they are a prompt, error message, etc., and sets appropriate flags in self. Prompt and success lines are removed. Returns the processed (i.e. possibly-edited) output and the unprocessed remainder (to be processed with the next chunk) as strings. ''' output = [] for b_line in b_chunk.splitlines(True): display_line = to_text(b_line).rstrip('\r\n') suppress_output = False # display.debug("Examining line (source=%s, state=%s): '%s'" % (source, state, display_line)) if self.become.expect_prompt() and self.become.check_password_prompt(b_line): display.debug(u"become_prompt: (source=%s, state=%s): '%s'" % (source, state, display_line)) self._flags['become_prompt'] = True suppress_output = True elif self.become.success and self.become.check_success(b_line): display.debug(u"become_success: (source=%s, state=%s): '%s'" % (source, state, display_line)) self._flags['become_success'] = True suppress_output = True elif sudoable and self.become.check_incorrect_password(b_line): display.debug(u"become_error: (source=%s, state=%s): '%s'" % (source, state, display_line)) self._flags['become_error'] = True elif sudoable and self.become.check_missing_password(b_line): display.debug(u"become_nopasswd_error: (source=%s, state=%s): '%s'" % (source, state, display_line)) self._flags['become_nopasswd_error'] = True if not suppress_output: output.append(b_line) # The chunk we read was most likely a series of complete lines, but just # in case the last line was incomplete (and not a prompt, which we would # have removed from the output), we retain it to be processed with the # next chunk. remainder = b'' if output and not output[-1].endswith(b'\n'): remainder = output[-1] output = output[:-1] return b''.join(output), remainder def _bare_run(self, cmd, in_data, sudoable=True, checkrc=True): ''' Starts the command and communicates with it until it ends. ''' # We don't use _shell.quote as this is run on the controller and independent from the shell plugin chosen display_cmd = u' '.join(shlex_quote(to_text(c)) for c in cmd) display.vvv(u'SSH: EXEC {0}'.format(display_cmd), host=self.host) # Start the given command. If we don't need to pipeline data, we can try # to use a pseudo-tty (ssh will have been invoked with -tt). If we are # pipelining data, or can't create a pty, we fall back to using plain # old pipes. p = None if isinstance(cmd, (text_type, binary_type)): cmd = to_bytes(cmd) else: cmd = list(map(to_bytes, cmd)) conn_password = self.get_option('password') or self._play_context.password if not in_data: try: # Make sure stdin is a proper pty to avoid tcgetattr errors master, slave = pty.openpty() if PY3 and conn_password: # pylint: disable=unexpected-keyword-arg p = subprocess.Popen(cmd, stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE, pass_fds=self.sshpass_pipe) else: p = subprocess.Popen(cmd, stdin=slave, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdin = os.fdopen(master, 'wb', 0) os.close(slave) except (OSError, IOError): p = None if not p: try: if PY3 and conn_password: # pylint: disable=unexpected-keyword-arg p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, pass_fds=self.sshpass_pipe) else: p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdin = p.stdin except (OSError, IOError) as e: raise AnsibleError('Unable to execute ssh command line on a controller due to: %s' % to_native(e)) # If we are using SSH password authentication, write the password into # the pipe we opened in _build_command. if conn_password: os.close(self.sshpass_pipe[0]) try: os.write(self.sshpass_pipe[1], to_bytes(conn_password) + b'\n') except OSError as e: # Ignore broken pipe errors if the sshpass process has exited. if e.errno != errno.EPIPE or p.poll() is None: raise os.close(self.sshpass_pipe[1]) # # SSH state machine # # Now we read and accumulate output from the running process until it # exits. Depending on the circumstances, we may also need to write an # escalation password and/or pipelined input to the process. states = [ 'awaiting_prompt', 'awaiting_escalation', 'ready_to_send', 'awaiting_exit' ] # Are we requesting privilege escalation? Right now, we may be invoked # to execute sftp/scp with sudoable=True, but we can request escalation # only when using ssh. Otherwise we can send initial data straightaway. state = states.index('ready_to_send') if to_bytes(self.get_option('ssh_executable')) in cmd and sudoable: prompt = getattr(self.become, 'prompt', None) if prompt: # We're requesting escalation with a password, so we have to # wait for a password prompt. state = states.index('awaiting_prompt') display.debug(u'Initial state: %s: %s' % (states[state], to_text(prompt))) elif self.become and self.become.success: # We're requesting escalation without a password, so we have to # detect success/failure before sending any initial data. state = states.index('awaiting_escalation') display.debug(u'Initial state: %s: %s' % (states[state], to_text(self.become.success))) # We store accumulated stdout and stderr output from the process here, # but strip any privilege escalation prompt/confirmation lines first. # Output is accumulated into tmp_*, complete lines are extracted into # an array, then checked and removed or copied to stdout or stderr. We # set any flags based on examining the output in self._flags. b_stdout = b_stderr = b'' b_tmp_stdout = b_tmp_stderr = b'' self._flags = dict( become_prompt=False, become_success=False, become_error=False, become_nopasswd_error=False ) # select timeout should be longer than the connect timeout, otherwise # they will race each other when we can't connect, and the connect # timeout usually fails timeout = 2 + self._play_context.timeout for fd in (p.stdout, p.stderr): fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK) # TODO: bcoca would like to use SelectSelector() when open # filehandles is low, then switch to more efficient ones when higher. # select is faster when filehandles is low. selector = selectors.DefaultSelector() selector.register(p.stdout, selectors.EVENT_READ) selector.register(p.stderr, selectors.EVENT_READ) # If we can send initial data without waiting for anything, we do so # before we start polling if states[state] == 'ready_to_send' and in_data: self._send_initial_data(stdin, in_data, p) state += 1 try: while True: poll = p.poll() events = selector.select(timeout) # We pay attention to timeouts only while negotiating a prompt. if not events: # We timed out if state <= states.index('awaiting_escalation'): # If the process has already exited, then it's not really a # timeout; we'll let the normal error handling deal with it. if poll is not None: break self._terminate_process(p) raise AnsibleError('Timeout (%ds) waiting for privilege escalation prompt: %s' % (timeout, to_native(b_stdout))) # Read whatever output is available on stdout and stderr, and stop # listening to the pipe if it's been closed. for key, event in events: if key.fileobj == p.stdout: b_chunk = p.stdout.read() if b_chunk == b'': # stdout has been closed, stop watching it selector.unregister(p.stdout) # When ssh has ControlMaster (+ControlPath/Persist) enabled, the # first connection goes into the background and we never see EOF # on stderr. If we see EOF on stdout, lower the select timeout # to reduce the time wasted selecting on stderr if we observe # that the process has not yet existed after this EOF. Otherwise # we may spend a long timeout period waiting for an EOF that is # not going to arrive until the persisted connection closes. timeout = 1 b_tmp_stdout += b_chunk display.debug(u"stdout chunk (state=%s):\n>>>%s<<<\n" % (state, to_text(b_chunk))) elif key.fileobj == p.stderr: b_chunk = p.stderr.read() if b_chunk == b'': # stderr has been closed, stop watching it selector.unregister(p.stderr) b_tmp_stderr += b_chunk display.debug("stderr chunk (state=%s):\n>>>%s<<<\n" % (state, to_text(b_chunk))) # We examine the output line-by-line until we have negotiated any # privilege escalation prompt and subsequent success/error message. # Afterwards, we can accumulate output without looking at it. if state < states.index('ready_to_send'): if b_tmp_stdout: b_output, b_unprocessed = self._examine_output('stdout', states[state], b_tmp_stdout, sudoable) b_stdout += b_output b_tmp_stdout = b_unprocessed if b_tmp_stderr: b_output, b_unprocessed = self._examine_output('stderr', states[state], b_tmp_stderr, sudoable) b_stderr += b_output b_tmp_stderr = b_unprocessed else: b_stdout += b_tmp_stdout b_stderr += b_tmp_stderr b_tmp_stdout = b_tmp_stderr = b'' # If we see a privilege escalation prompt, we send the password. # (If we're expecting a prompt but the escalation succeeds, we # didn't need the password and can carry on regardless.) if states[state] == 'awaiting_prompt': if self._flags['become_prompt']: display.debug(u'Sending become_password in response to prompt') become_pass = self.become.get_option('become_pass', playcontext=self._play_context) stdin.write(to_bytes(become_pass, errors='surrogate_or_strict') + b'\n') # On python3 stdin is a BufferedWriter, and we don't have a guarantee # that the write will happen without a flush stdin.flush() self._flags['become_prompt'] = False state += 1 elif self._flags['become_success']: state += 1 # We've requested escalation (with or without a password), now we # wait for an error message or a successful escalation. if states[state] == 'awaiting_escalation': if self._flags['become_success']: display.vvv(u'Escalation succeeded') self._flags['become_success'] = False state += 1 elif self._flags['become_error']: display.vvv(u'Escalation failed') self._terminate_process(p) self._flags['become_error'] = False raise AnsibleError('Incorrect %s password' % self.become.name) elif self._flags['become_nopasswd_error']: display.vvv(u'Escalation requires password') self._terminate_process(p) self._flags['become_nopasswd_error'] = False raise AnsibleError('Missing %s password' % self.become.name) elif self._flags['become_prompt']: # This shouldn't happen, because we should see the "Sorry, # try again" message first. display.vvv(u'Escalation prompt repeated') self._terminate_process(p) self._flags['become_prompt'] = False raise AnsibleError('Incorrect %s password' % self.become.name) # Once we're sure that the privilege escalation prompt, if any, has # been dealt with, we can send any initial data and start waiting # for output. if states[state] == 'ready_to_send': if in_data: self._send_initial_data(stdin, in_data, p) state += 1 # Now we're awaiting_exit: has the child process exited? If it has, # and we've read all available output from it, we're done. if poll is not None: if not selector.get_map() or not events: break # We should not see further writes to the stdout/stderr file # descriptors after the process has closed, set the select # timeout to gather any last writes we may have missed. timeout = 0 continue # If the process has not yet exited, but we've already read EOF from # its stdout and stderr (and thus no longer watching any file # descriptors), we can just wait for it to exit. elif not selector.get_map(): p.wait() break # Otherwise there may still be outstanding data to read. finally: selector.close() # close stdin, stdout, and stderr after process is terminated and # stdout/stderr are read completely (see also issues #848, #64768). stdin.close() p.stdout.close() p.stderr.close() if C.HOST_KEY_CHECKING: if cmd[0] == b"sshpass" and p.returncode == 6: raise AnsibleError('Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support ' 'this. Please add this host\'s fingerprint to your known_hosts file to manage this host.') controlpersisterror = b'Bad configuration option: ControlPersist' in b_stderr or b'unknown configuration option: ControlPersist' in b_stderr if p.returncode != 0 and controlpersisterror: raise AnsibleError('using -c ssh on certain older ssh versions may not support ControlPersist, set ANSIBLE_SSH_ARGS="" ' '(or ssh_args in [ssh_connection] section of the config file) before running again') # If we find a broken pipe because of ControlPersist timeout expiring (see #16731), # we raise a special exception so that we can retry a connection. controlpersist_broken_pipe = b'mux_client_hello_exchange: write packet: Broken pipe' in b_stderr if p.returncode == 255: additional = to_native(b_stderr) if controlpersist_broken_pipe: raise AnsibleControlPersistBrokenPipeError('Data could not be sent because of ControlPersist broken pipe: %s' % additional) elif in_data and checkrc: raise AnsibleConnectionFailure('Data could not be sent to remote host "%s". Make sure this host can be reached over ssh: %s' % (self.host, additional)) return (p.returncode, b_stdout, b_stderr) @_ssh_retry def _run(self, cmd, in_data, sudoable=True, checkrc=True): """Wrapper around _bare_run that retries the connection """ return self._bare_run(cmd, in_data, sudoable=sudoable, checkrc=checkrc) @_ssh_retry def _file_transport_command(self, in_path, out_path, sftp_action): # scp and sftp require square brackets for IPv6 addresses, but # accept them for hostnames and IPv4 addresses too. host = '[%s]' % self.host smart_methods = ['sftp', 'scp', 'piped'] # Windows does not support dd so we cannot use the piped method if getattr(self._shell, "_IS_WINDOWS", False): smart_methods.remove('piped') # Transfer methods to try methods = [] # Use the transfer_method option if set, otherwise use scp_if_ssh ssh_transfer_method = self._play_context.ssh_transfer_method if ssh_transfer_method is not None: if not (ssh_transfer_method in ('smart', 'sftp', 'scp', 'piped')): raise AnsibleOptionsError('transfer_method needs to be one of [smart|sftp|scp|piped]') if ssh_transfer_method == 'smart': methods = smart_methods else: methods = [ssh_transfer_method] else: # since this can be a non-bool now, we need to handle it correctly scp_if_ssh = C.DEFAULT_SCP_IF_SSH if not isinstance(scp_if_ssh, bool): scp_if_ssh = scp_if_ssh.lower() if scp_if_ssh in BOOLEANS: scp_if_ssh = boolean(scp_if_ssh, strict=False) elif scp_if_ssh != 'smart': raise AnsibleOptionsError('scp_if_ssh needs to be one of [smart|True|False]') if scp_if_ssh == 'smart': methods = smart_methods elif scp_if_ssh is True: methods = ['scp'] else: methods = ['sftp'] for method in methods: returncode = stdout = stderr = None if method == 'sftp': cmd = self._build_command(self.get_option('sftp_executable'), to_bytes(host)) in_data = u"{0} {1} {2}\n".format(sftp_action, shlex_quote(in_path), shlex_quote(out_path)) in_data = to_bytes(in_data, nonstring='passthru') (returncode, stdout, stderr) = self._bare_run(cmd, in_data, checkrc=False) elif method == 'scp': scp = self.get_option('scp_executable') if sftp_action == 'get': cmd = self._build_command(scp, u'{0}:{1}'.format(host, self._shell.quote(in_path)), out_path) else: cmd = self._build_command(scp, in_path, u'{0}:{1}'.format(host, self._shell.quote(out_path))) in_data = None (returncode, stdout, stderr) = self._bare_run(cmd, in_data, checkrc=False) elif method == 'piped': if sftp_action == 'get': # we pass sudoable=False to disable pty allocation, which # would end up mixing stdout/stderr and screwing with newlines (returncode, stdout, stderr) = self.exec_command('dd if=%s bs=%s' % (in_path, BUFSIZE), sudoable=False) with open(to_bytes(out_path, errors='surrogate_or_strict'), 'wb+') as out_file: out_file.write(stdout) else: with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as f: in_data = to_bytes(f.read(), nonstring='passthru') if not in_data: count = ' count=0' else: count = '' (returncode, stdout, stderr) = self.exec_command('dd of=%s bs=%s%s' % (out_path, BUFSIZE, count), in_data=in_data, sudoable=False) # Check the return code and rollover to next method if failed if returncode == 0: return (returncode, stdout, stderr) else: # If not in smart mode, the data will be printed by the raise below if len(methods) > 1: display.warning(u'%s transfer mechanism failed on %s. Use ANSIBLE_DEBUG=1 to see detailed information' % (method, host)) display.debug(u'%s' % to_text(stdout)) display.debug(u'%s' % to_text(stderr)) if returncode == 255: raise AnsibleConnectionFailure("Failed to connect to the host via %s: %s" % (method, to_native(stderr))) else: raise AnsibleError("failed to transfer file to %s %s:\n%s\n%s" % (to_native(in_path), to_native(out_path), to_native(stdout), to_native(stderr))) def _escape_win_path(self, path): """ converts a Windows path to one that's supported by SFTP and SCP """ # If using a root path then we need to start with / prefix = "" if re.match(r'^\w{1}:', path): prefix = "/" # Convert all '\' to '/' return "%s%s" % (prefix, path.replace("\\", "/")) # # Main public methods # def exec_command(self, cmd, in_data=None, sudoable=True): ''' run a command on the remote host ''' super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable) display.vvv(u"ESTABLISH SSH CONNECTION FOR USER: {0}".format(self._play_context.remote_user), host=self._play_context.remote_addr) if getattr(self._shell, "_IS_WINDOWS", False): # Become method 'runas' is done in the wrapper that is executed, # need to disable sudoable so the bare_run is not waiting for a # prompt that will not occur sudoable = False # Make sure our first command is to set the console encoding to # utf-8, this must be done via chcp to get utf-8 (65001) cmd_parts = ["chcp.com", "65001", self._shell._SHELL_REDIRECT_ALLNULL, self._shell._SHELL_AND] cmd_parts.extend(self._shell._encode_script(cmd, as_list=True, strict_mode=False, preserve_rc=False)) cmd = ' '.join(cmd_parts) # we can only use tty when we are not pipelining the modules. piping # data into /usr/bin/python inside a tty automatically invokes the # python interactive-mode but the modules are not compatible with the # interactive-mode ("unexpected indent" mainly because of empty lines) ssh_executable = self._play_context.ssh_executable # -tt can cause various issues in some environments so allow the user # to disable it as a troubleshooting method. use_tty = self.get_option('use_tty') if not in_data and sudoable and use_tty: args = (ssh_executable, '-tt', self.host, cmd) else: args = (ssh_executable, self.host, cmd) cmd = self._build_command(*args) (returncode, stdout, stderr) = self._run(cmd, in_data, sudoable=sudoable) # When running on Windows, stderr may contain CLIXML encoded output if getattr(self._shell, "_IS_WINDOWS", False) and stderr.startswith(b"#< CLIXML"): stderr = _parse_clixml(stderr) return (returncode, stdout, stderr) def put_file(self, in_path, out_path): ''' transfer a file from local to remote ''' super(Connection, self).put_file(in_path, out_path) display.vvv(u"PUT {0} TO {1}".format(in_path, out_path), host=self.host) if not os.path.exists(to_bytes(in_path, errors='surrogate_or_strict')): raise AnsibleFileNotFound("file or module does not exist: {0}".format(to_native(in_path))) if getattr(self._shell, "_IS_WINDOWS", False): out_path = self._escape_win_path(out_path) return self._file_transport_command(in_path, out_path, 'put') def fetch_file(self, in_path, out_path): ''' fetch a file from remote to local ''' super(Connection, self).fetch_file(in_path, out_path) display.vvv(u"FETCH {0} TO {1}".format(in_path, out_path), host=self.host) # need to add / if path is rooted if getattr(self._shell, "_IS_WINDOWS", False): in_path = self._escape_win_path(in_path) return self._file_transport_command(in_path, out_path, 'get') def reset(self): # If we have a persistent ssh connection (ControlPersist), we can ask it to stop listening. cmd = self._build_command(self._play_context.ssh_executable, '-O', 'stop', self.host) controlpersist, controlpath = self._persistence_controls(cmd) cp_arg = [a for a in cmd if a.startswith(b"ControlPath=")] # only run the reset if the ControlPath already exists or if it isn't # configured and ControlPersist is set run_reset = False if controlpersist and len(cp_arg) > 0: cp_path = cp_arg[0].split(b"=", 1)[-1] if os.path.exists(cp_path): run_reset = True elif controlpersist: run_reset = True if run_reset: display.vvv(u'sending stop: %s' % to_text(cmd)) p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() status_code = p.wait() if status_code != 0: display.warning(u"Failed to reset connection:%s" % to_text(stderr)) self.close() def close(self): self._connected = False
gpl-3.0
nvoron23/scikit-learn
sklearn/preprocessing/label.py
137
27165
# Authors: Alexandre Gramfort <[email protected]> # Mathieu Blondel <[email protected]> # Olivier Grisel <[email protected]> # Andreas Mueller <[email protected]> # Joel Nothman <[email protected]> # Hamzeh Alsalhi <[email protected]> # License: BSD 3 clause from collections import defaultdict import itertools import array import numpy as np import scipy.sparse as sp from ..base import BaseEstimator, TransformerMixin from ..utils.fixes import np_version from ..utils.fixes import sparse_min_max from ..utils.fixes import astype from ..utils.fixes import in1d from ..utils import column_or_1d from ..utils.validation import check_array from ..utils.validation import check_is_fitted from ..utils.validation import _num_samples from ..utils.multiclass import unique_labels from ..utils.multiclass import type_of_target from ..externals import six zip = six.moves.zip map = six.moves.map __all__ = [ 'label_binarize', 'LabelBinarizer', 'LabelEncoder', 'MultiLabelBinarizer', ] def _check_numpy_unicode_bug(labels): """Check that user is not subject to an old numpy bug Fixed in master before 1.7.0: https://github.com/numpy/numpy/pull/243 """ if np_version[:3] < (1, 7, 0) and labels.dtype.kind == 'U': raise RuntimeError("NumPy < 1.7.0 does not implement searchsorted" " on unicode data correctly. Please upgrade" " NumPy to use LabelEncoder with unicode inputs.") class LabelEncoder(BaseEstimator, TransformerMixin): """Encode labels with value between 0 and n_classes-1. Read more in the :ref:`User Guide <preprocessing_targets>`. Attributes ---------- classes_ : array of shape (n_class,) Holds the label for each class. Examples -------- `LabelEncoder` can be used to normalize labels. >>> from sklearn import preprocessing >>> le = preprocessing.LabelEncoder() >>> le.fit([1, 2, 2, 6]) LabelEncoder() >>> le.classes_ array([1, 2, 6]) >>> le.transform([1, 1, 2, 6]) #doctest: +ELLIPSIS array([0, 0, 1, 2]...) >>> le.inverse_transform([0, 0, 1, 2]) array([1, 1, 2, 6]) It can also be used to transform non-numerical labels (as long as they are hashable and comparable) to numerical labels. >>> le = preprocessing.LabelEncoder() >>> le.fit(["paris", "paris", "tokyo", "amsterdam"]) LabelEncoder() >>> list(le.classes_) ['amsterdam', 'paris', 'tokyo'] >>> le.transform(["tokyo", "tokyo", "paris"]) #doctest: +ELLIPSIS array([2, 2, 1]...) >>> list(le.inverse_transform([2, 2, 1])) ['tokyo', 'tokyo', 'paris'] """ def fit(self, y): """Fit label encoder Parameters ---------- y : array-like of shape (n_samples,) Target values. Returns ------- self : returns an instance of self. """ y = column_or_1d(y, warn=True) _check_numpy_unicode_bug(y) self.classes_ = np.unique(y) return self def fit_transform(self, y): """Fit label encoder and return encoded labels Parameters ---------- y : array-like of shape [n_samples] Target values. Returns ------- y : array-like of shape [n_samples] """ y = column_or_1d(y, warn=True) _check_numpy_unicode_bug(y) self.classes_, y = np.unique(y, return_inverse=True) return y def transform(self, y): """Transform labels to normalized encoding. Parameters ---------- y : array-like of shape [n_samples] Target values. Returns ------- y : array-like of shape [n_samples] """ check_is_fitted(self, 'classes_') classes = np.unique(y) _check_numpy_unicode_bug(classes) if len(np.intersect1d(classes, self.classes_)) < len(classes): diff = np.setdiff1d(classes, self.classes_) raise ValueError("y contains new labels: %s" % str(diff)) return np.searchsorted(self.classes_, y) def inverse_transform(self, y): """Transform labels back to original encoding. Parameters ---------- y : numpy array of shape [n_samples] Target values. Returns ------- y : numpy array of shape [n_samples] """ check_is_fitted(self, 'classes_') diff = np.setdiff1d(y, np.arange(len(self.classes_))) if diff: raise ValueError("y contains new labels: %s" % str(diff)) y = np.asarray(y) return self.classes_[y] class LabelBinarizer(BaseEstimator, TransformerMixin): """Binarize labels in a one-vs-all fashion Several regression and binary classification algorithms are available in the scikit. A simple way to extend these algorithms to the multi-class classification case is to use the so-called one-vs-all scheme. At learning time, this simply consists in learning one regressor or binary classifier per class. In doing so, one needs to convert multi-class labels to binary labels (belong or does not belong to the class). LabelBinarizer makes this process easy with the transform method. At prediction time, one assigns the class for which the corresponding model gave the greatest confidence. LabelBinarizer makes this easy with the inverse_transform method. Read more in the :ref:`User Guide <preprocessing_targets>`. Parameters ---------- neg_label : int (default: 0) Value with which negative labels must be encoded. pos_label : int (default: 1) Value with which positive labels must be encoded. sparse_output : boolean (default: False) True if the returned array from transform is desired to be in sparse CSR format. Attributes ---------- classes_ : array of shape [n_class] Holds the label for each class. y_type_ : str, Represents the type of the target data as evaluated by utils.multiclass.type_of_target. Possible type are 'continuous', 'continuous-multioutput', 'binary', 'multiclass', 'mutliclass-multioutput', 'multilabel-indicator', and 'unknown'. multilabel_ : boolean True if the transformer was fitted on a multilabel rather than a multiclass set of labels. The ``multilabel_`` attribute is deprecated and will be removed in 0.18 sparse_input_ : boolean, True if the input data to transform is given as a sparse matrix, False otherwise. indicator_matrix_ : str 'sparse' when the input data to tansform is a multilable-indicator and is sparse, None otherwise. The ``indicator_matrix_`` attribute is deprecated as of version 0.16 and will be removed in 0.18 Examples -------- >>> from sklearn import preprocessing >>> lb = preprocessing.LabelBinarizer() >>> lb.fit([1, 2, 6, 4, 2]) LabelBinarizer(neg_label=0, pos_label=1, sparse_output=False) >>> lb.classes_ array([1, 2, 4, 6]) >>> lb.transform([1, 6]) array([[1, 0, 0, 0], [0, 0, 0, 1]]) Binary targets transform to a column vector >>> lb = preprocessing.LabelBinarizer() >>> lb.fit_transform(['yes', 'no', 'no', 'yes']) array([[1], [0], [0], [1]]) Passing a 2D matrix for multilabel classification >>> import numpy as np >>> lb.fit(np.array([[0, 1, 1], [1, 0, 0]])) LabelBinarizer(neg_label=0, pos_label=1, sparse_output=False) >>> lb.classes_ array([0, 1, 2]) >>> lb.transform([0, 1, 2, 1]) array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0]]) See also -------- label_binarize : function to perform the transform operation of LabelBinarizer with fixed classes. """ def __init__(self, neg_label=0, pos_label=1, sparse_output=False): if neg_label >= pos_label: raise ValueError("neg_label={0} must be strictly less than " "pos_label={1}.".format(neg_label, pos_label)) if sparse_output and (pos_label == 0 or neg_label != 0): raise ValueError("Sparse binarization is only supported with non " "zero pos_label and zero neg_label, got " "pos_label={0} and neg_label={1}" "".format(pos_label, neg_label)) self.neg_label = neg_label self.pos_label = pos_label self.sparse_output = sparse_output def fit(self, y): """Fit label binarizer Parameters ---------- y : numpy array of shape (n_samples,) or (n_samples, n_classes) Target values. The 2-d matrix should only contain 0 and 1, represents multilabel classification. Returns ------- self : returns an instance of self. """ self.y_type_ = type_of_target(y) if 'multioutput' in self.y_type_: raise ValueError("Multioutput target data is not supported with " "label binarization") if _num_samples(y) == 0: raise ValueError('y has 0 samples: %r' % y) self.sparse_input_ = sp.issparse(y) self.classes_ = unique_labels(y) return self def transform(self, y): """Transform multi-class labels to binary labels The output of transform is sometimes referred to by some authors as the 1-of-K coding scheme. Parameters ---------- y : numpy array or sparse matrix of shape (n_samples,) or (n_samples, n_classes) Target values. The 2-d matrix should only contain 0 and 1, represents multilabel classification. Sparse matrix can be CSR, CSC, COO, DOK, or LIL. Returns ------- Y : numpy array or CSR matrix of shape [n_samples, n_classes] Shape will be [n_samples, 1] for binary problems. """ check_is_fitted(self, 'classes_') y_is_multilabel = type_of_target(y).startswith('multilabel') if y_is_multilabel and not self.y_type_.startswith('multilabel'): raise ValueError("The object was not fitted with multilabel" " input.") return label_binarize(y, self.classes_, pos_label=self.pos_label, neg_label=self.neg_label, sparse_output=self.sparse_output) def inverse_transform(self, Y, threshold=None): """Transform binary labels back to multi-class labels Parameters ---------- Y : numpy array or sparse matrix with shape [n_samples, n_classes] Target values. All sparse matrices are converted to CSR before inverse transformation. threshold : float or None Threshold used in the binary and multi-label cases. Use 0 when: - Y contains the output of decision_function (classifier) Use 0.5 when: - Y contains the output of predict_proba If None, the threshold is assumed to be half way between neg_label and pos_label. Returns ------- y : numpy array or CSR matrix of shape [n_samples] Target values. Notes ----- In the case when the binary labels are fractional (probabilistic), inverse_transform chooses the class with the greatest value. Typically, this allows to use the output of a linear model's decision_function method directly as the input of inverse_transform. """ check_is_fitted(self, 'classes_') if threshold is None: threshold = (self.pos_label + self.neg_label) / 2. if self.y_type_ == "multiclass": y_inv = _inverse_binarize_multiclass(Y, self.classes_) else: y_inv = _inverse_binarize_thresholding(Y, self.y_type_, self.classes_, threshold) if self.sparse_input_: y_inv = sp.csr_matrix(y_inv) elif sp.issparse(y_inv): y_inv = y_inv.toarray() return y_inv def label_binarize(y, classes, neg_label=0, pos_label=1, sparse_output=False): """Binarize labels in a one-vs-all fashion Several regression and binary classification algorithms are available in the scikit. A simple way to extend these algorithms to the multi-class classification case is to use the so-called one-vs-all scheme. This function makes it possible to compute this transformation for a fixed set of class labels known ahead of time. Parameters ---------- y : array-like Sequence of integer labels or multilabel data to encode. classes : array-like of shape [n_classes] Uniquely holds the label for each class. neg_label : int (default: 0) Value with which negative labels must be encoded. pos_label : int (default: 1) Value with which positive labels must be encoded. sparse_output : boolean (default: False), Set to true if output binary array is desired in CSR sparse format Returns ------- Y : numpy array or CSR matrix of shape [n_samples, n_classes] Shape will be [n_samples, 1] for binary problems. Examples -------- >>> from sklearn.preprocessing import label_binarize >>> label_binarize([1, 6], classes=[1, 2, 4, 6]) array([[1, 0, 0, 0], [0, 0, 0, 1]]) The class ordering is preserved: >>> label_binarize([1, 6], classes=[1, 6, 4, 2]) array([[1, 0, 0, 0], [0, 1, 0, 0]]) Binary targets transform to a column vector >>> label_binarize(['yes', 'no', 'no', 'yes'], classes=['no', 'yes']) array([[1], [0], [0], [1]]) See also -------- LabelBinarizer : class used to wrap the functionality of label_binarize and allow for fitting to classes independently of the transform operation """ if not isinstance(y, list): # XXX Workaround that will be removed when list of list format is # dropped y = check_array(y, accept_sparse='csr', ensure_2d=False, dtype=None) else: if _num_samples(y) == 0: raise ValueError('y has 0 samples: %r' % y) if neg_label >= pos_label: raise ValueError("neg_label={0} must be strictly less than " "pos_label={1}.".format(neg_label, pos_label)) if (sparse_output and (pos_label == 0 or neg_label != 0)): raise ValueError("Sparse binarization is only supported with non " "zero pos_label and zero neg_label, got " "pos_label={0} and neg_label={1}" "".format(pos_label, neg_label)) # To account for pos_label == 0 in the dense case pos_switch = pos_label == 0 if pos_switch: pos_label = -neg_label y_type = type_of_target(y) if 'multioutput' in y_type: raise ValueError("Multioutput target data is not supported with label " "binarization") if y_type == 'unknown': raise ValueError("The type of target data is not known") n_samples = y.shape[0] if sp.issparse(y) else len(y) n_classes = len(classes) classes = np.asarray(classes) if y_type == "binary": if len(classes) == 1: Y = np.zeros((len(y), 1), dtype=np.int) Y += neg_label return Y elif len(classes) >= 3: y_type = "multiclass" sorted_class = np.sort(classes) if (y_type == "multilabel-indicator" and classes.size != y.shape[1]): raise ValueError("classes {0} missmatch with the labels {1}" "found in the data".format(classes, unique_labels(y))) if y_type in ("binary", "multiclass"): y = column_or_1d(y) # pick out the known labels from y y_in_classes = in1d(y, classes) y_seen = y[y_in_classes] indices = np.searchsorted(sorted_class, y_seen) indptr = np.hstack((0, np.cumsum(y_in_classes))) data = np.empty_like(indices) data.fill(pos_label) Y = sp.csr_matrix((data, indices, indptr), shape=(n_samples, n_classes)) elif y_type == "multilabel-indicator": Y = sp.csr_matrix(y) if pos_label != 1: data = np.empty_like(Y.data) data.fill(pos_label) Y.data = data else: raise ValueError("%s target data is not supported with label " "binarization" % y_type) if not sparse_output: Y = Y.toarray() Y = astype(Y, int, copy=False) if neg_label != 0: Y[Y == 0] = neg_label if pos_switch: Y[Y == pos_label] = 0 else: Y.data = astype(Y.data, int, copy=False) # preserve label ordering if np.any(classes != sorted_class): indices = np.searchsorted(sorted_class, classes) Y = Y[:, indices] if y_type == "binary": if sparse_output: Y = Y.getcol(-1) else: Y = Y[:, -1].reshape((-1, 1)) return Y def _inverse_binarize_multiclass(y, classes): """Inverse label binarization transformation for multiclass. Multiclass uses the maximal score instead of a threshold. """ classes = np.asarray(classes) if sp.issparse(y): # Find the argmax for each row in y where y is a CSR matrix y = y.tocsr() n_samples, n_outputs = y.shape outputs = np.arange(n_outputs) row_max = sparse_min_max(y, 1)[1] row_nnz = np.diff(y.indptr) y_data_repeated_max = np.repeat(row_max, row_nnz) # picks out all indices obtaining the maximum per row y_i_all_argmax = np.flatnonzero(y_data_repeated_max == y.data) # For corner case where last row has a max of 0 if row_max[-1] == 0: y_i_all_argmax = np.append(y_i_all_argmax, [len(y.data)]) # Gets the index of the first argmax in each row from y_i_all_argmax index_first_argmax = np.searchsorted(y_i_all_argmax, y.indptr[:-1]) # first argmax of each row y_ind_ext = np.append(y.indices, [0]) y_i_argmax = y_ind_ext[y_i_all_argmax[index_first_argmax]] # Handle rows of all 0 y_i_argmax[np.where(row_nnz == 0)[0]] = 0 # Handles rows with max of 0 that contain negative numbers samples = np.arange(n_samples)[(row_nnz > 0) & (row_max.ravel() == 0)] for i in samples: ind = y.indices[y.indptr[i]:y.indptr[i + 1]] y_i_argmax[i] = classes[np.setdiff1d(outputs, ind)][0] return classes[y_i_argmax] else: return classes.take(y.argmax(axis=1), mode="clip") def _inverse_binarize_thresholding(y, output_type, classes, threshold): """Inverse label binarization transformation using thresholding.""" if output_type == "binary" and y.ndim == 2 and y.shape[1] > 2: raise ValueError("output_type='binary', but y.shape = {0}". format(y.shape)) if output_type != "binary" and y.shape[1] != len(classes): raise ValueError("The number of class is not equal to the number of " "dimension of y.") classes = np.asarray(classes) # Perform thresholding if sp.issparse(y): if threshold > 0: if y.format not in ('csr', 'csc'): y = y.tocsr() y.data = np.array(y.data > threshold, dtype=np.int) y.eliminate_zeros() else: y = np.array(y.toarray() > threshold, dtype=np.int) else: y = np.array(y > threshold, dtype=np.int) # Inverse transform data if output_type == "binary": if sp.issparse(y): y = y.toarray() if y.ndim == 2 and y.shape[1] == 2: return classes[y[:, 1]] else: if len(classes) == 1: y = np.empty(len(y), dtype=classes.dtype) y.fill(classes[0]) return y else: return classes[y.ravel()] elif output_type == "multilabel-indicator": return y else: raise ValueError("{0} format is not supported".format(output_type)) class MultiLabelBinarizer(BaseEstimator, TransformerMixin): """Transform between iterable of iterables and a multilabel format Although a list of sets or tuples is a very intuitive format for multilabel data, it is unwieldy to process. This transformer converts between this intuitive format and the supported multilabel format: a (samples x classes) binary matrix indicating the presence of a class label. Parameters ---------- classes : array-like of shape [n_classes] (optional) Indicates an ordering for the class labels sparse_output : boolean (default: False), Set to true if output binary array is desired in CSR sparse format Attributes ---------- classes_ : array of labels A copy of the `classes` parameter where provided, or otherwise, the sorted set of classes found when fitting. Examples -------- >>> mlb = MultiLabelBinarizer() >>> mlb.fit_transform([(1, 2), (3,)]) array([[1, 1, 0], [0, 0, 1]]) >>> mlb.classes_ array([1, 2, 3]) >>> mlb.fit_transform([set(['sci-fi', 'thriller']), set(['comedy'])]) array([[0, 1, 1], [1, 0, 0]]) >>> list(mlb.classes_) ['comedy', 'sci-fi', 'thriller'] """ def __init__(self, classes=None, sparse_output=False): self.classes = classes self.sparse_output = sparse_output def fit(self, y): """Fit the label sets binarizer, storing `classes_` Parameters ---------- y : iterable of iterables A set of labels (any orderable and hashable object) for each sample. If the `classes` parameter is set, `y` will not be iterated. Returns ------- self : returns this MultiLabelBinarizer instance """ if self.classes is None: classes = sorted(set(itertools.chain.from_iterable(y))) else: classes = self.classes dtype = np.int if all(isinstance(c, int) for c in classes) else object self.classes_ = np.empty(len(classes), dtype=dtype) self.classes_[:] = classes return self def fit_transform(self, y): """Fit the label sets binarizer and transform the given label sets Parameters ---------- y : iterable of iterables A set of labels (any orderable and hashable object) for each sample. If the `classes` parameter is set, `y` will not be iterated. Returns ------- y_indicator : array or CSR matrix, shape (n_samples, n_classes) A matrix such that `y_indicator[i, j] = 1` iff `classes_[j]` is in `y[i]`, and 0 otherwise. """ if self.classes is not None: return self.fit(y).transform(y) # Automatically increment on new class class_mapping = defaultdict(int) class_mapping.default_factory = class_mapping.__len__ yt = self._transform(y, class_mapping) # sort classes and reorder columns tmp = sorted(class_mapping, key=class_mapping.get) # (make safe for tuples) dtype = np.int if all(isinstance(c, int) for c in tmp) else object class_mapping = np.empty(len(tmp), dtype=dtype) class_mapping[:] = tmp self.classes_, inverse = np.unique(class_mapping, return_inverse=True) yt.indices = np.take(inverse, yt.indices) if not self.sparse_output: yt = yt.toarray() return yt def transform(self, y): """Transform the given label sets Parameters ---------- y : iterable of iterables A set of labels (any orderable and hashable object) for each sample. If the `classes` parameter is set, `y` will not be iterated. Returns ------- y_indicator : array or CSR matrix, shape (n_samples, n_classes) A matrix such that `y_indicator[i, j] = 1` iff `classes_[j]` is in `y[i]`, and 0 otherwise. """ class_to_index = dict(zip(self.classes_, range(len(self.classes_)))) yt = self._transform(y, class_to_index) if not self.sparse_output: yt = yt.toarray() return yt def _transform(self, y, class_mapping): """Transforms the label sets with a given mapping Parameters ---------- y : iterable of iterables class_mapping : Mapping Maps from label to column index in label indicator matrix Returns ------- y_indicator : sparse CSR matrix, shape (n_samples, n_classes) Label indicator matrix """ indices = array.array('i') indptr = array.array('i', [0]) for labels in y: indices.extend(set(class_mapping[label] for label in labels)) indptr.append(len(indices)) data = np.ones(len(indices), dtype=int) return sp.csr_matrix((data, indices, indptr), shape=(len(indptr) - 1, len(class_mapping))) def inverse_transform(self, yt): """Transform the given indicator matrix into label sets Parameters ---------- yt : array or sparse matrix of shape (n_samples, n_classes) A matrix containing only 1s ands 0s. Returns ------- y : list of tuples The set of labels for each sample such that `y[i]` consists of `classes_[j]` for each `yt[i, j] == 1`. """ if yt.shape[1] != len(self.classes_): raise ValueError('Expected indicator for {0} classes, but got {1}' .format(len(self.classes_), yt.shape[1])) if sp.issparse(yt): yt = yt.tocsr() if len(yt.data) != 0 and len(np.setdiff1d(yt.data, [0, 1])) > 0: raise ValueError('Expected only 0s and 1s in label indicator.') return [tuple(self.classes_.take(yt.indices[start:end])) for start, end in zip(yt.indptr[:-1], yt.indptr[1:])] else: unexpected = np.setdiff1d(yt, [0, 1]) if len(unexpected) > 0: raise ValueError('Expected only 0s and 1s in label indicator. ' 'Also got {0}'.format(unexpected)) return [tuple(self.classes_.compress(indicators)) for indicators in yt]
bsd-3-clause
jokey2k/ShockGsite
djangobb_forum/tests/test_forum.py
8
1366
# -*- coding: utf-8 -*- from django.test import TestCase, Client from django.contrib.auth.models import User from djangobb_forum.models import Category, Forum, Topic, Post class TestForum(TestCase): fixtures = ['test_forum.json'] def setUp(self): self.category = Category.objects.get(pk=1) self.forum = Forum.objects.get(pk=1) self.topic = Topic.objects.get(pk=1) self.post = Post.objects.get(pk=1) self.user = User.objects.get(pk=1) self.client = Client() self.ip = '127.0.0.1' def test_login(self): self.assertTrue(self.client.login(username='djangobb', password='djangobb')) def test_create_topic(self): topic = Topic.objects.create(forum=self.forum, user=self.user, name="Test Title") self.assert_(topic) post = Post.objects.create( topic=topic, user=self.user, user_ip=self.ip, markup='bbcode', body='Test Body' ) self.assert_(post) def test_create_post(self): post = Post.objects.create( topic=self.topic, user=self.user, user_ip=self.ip, markup='bbcode', body='Test Body' ) self.assert_(post) def test_edit_post(self): self.post.body = 'Test Edit Body' self.assertEqual(self.post.body, 'Test Edit Body')
bsd-3-clause
metagenomics/bioPipeline
scripts/create_overview.py
2
5401
#!/usr/bin/env python """ Usage: create_overview.py -u <HMM-Hits.unique> -faa <faa_folder> -o <output_folder> [--search=<search_config.yaml>] -c <coverage_files>... -h --help Please enter a HMM.unique file, faa folder, output folder and as many coverage files as you want to. """ from sys import argv from docopt import docopt import functools import csv from Bio import SeqIO import os import shutil import util import yaml def writeHeader(coverages, file, insertGroup): header = [util.GENE_ID, util.HMM, util.SCORE_HMM, util.EVAL_HMM, util.BEST_BLASTP_HIT, util.EVALUE_BEST_BLASTP, util.IDENTITY, util.SUBJECT_ACCESSION, util.SUBJECT_TITLES, util.SUBJECT_TAXIDS, util.SUBJECT_IDS, util.LINKS, util.GENE_SEQUENCE] if insertGroup: header.insert(2, util.GROUP) file.write(("\t".join(coverages + header)) + '\n') def move_txt_faa_files(output, file_txt, file_faa): if os.path.exists(file_txt): shutil.move(file_txt, output) if os.path.exists(file_faa): shutil.move(file_faa, output) def move_html_files(output, file_html): if os.path.exists(file_html): shutil.move(file_html, output) def load_search_config(file): return yaml.load(file) def determine_config_values(config, hmm): """ Returns group of the HMM protein. :param config: column patterns :param hmm: HMM :return: tuple of hmm and key """ for group in config: for key in group: if hmm in group[key]: return (hmm, key) return (hmm, "N/A") def get_contig_txt_information(contig): """ Extracts contig information. :param contig: contig file :return: various information """ BLASTP = util.NOT_AVAILABLE EVALUE = util.NOT_AVAILABLE IDENTITY = util.NOT_AVAILABLE SUBACCES = util.NOT_AVAILABLE SUBTIT = util.NOT_AVAILABLE SUBTAXID = util.NOT_AVAILABLE SUBID = util.NOT_AVAILABLE if os.path.isfile(contig): with open(contig, 'rb') as f: reader = csv.reader(f, delimiter='\t') for row in reader: BLASTP = row[1] EVALUE = row[10] IDENTITY = row[2] SUBACCES = row[12] SUBTIT = row[13] SUBTAXID = row[14] SUBID = row[15] break; return [BLASTP, EVALUE, IDENTITY, SUBACCES, SUBTIT, SUBTAXID, SUBID] def get_coverage_information(coverage_path, id): """ Extracts coverage information :param coverage_path: :param id: id of the contig :return: coverage value """ with open(coverage_path, 'r') as coverage_file: cov = 0 reader = csv.DictReader(coverage_file, delimiter='\t') for row in reader: contig = row[util.CONTIG_NAME] + "_" if (id.startswith(contig)): cov = row[util.AVG_COVERAGE] return cov def get_sequence(contig_faa): """ get sequence from a faa file with one entry :param contig_faa: faa sequence :return: faa sequence """ seq = util.NOT_AVAILABLE if os.path.isfile(contig_faa) and not os.stat(contig_faa).st_size == 0: record = SeqIO.read(open(contig_faa), "fasta") seq = record.seq return seq def main(): args = docopt(__doc__, argv[1:]) unique_file_path = args["<HMM-Hits.unique>"] faa_folder = args['<faa_folder>'] output = args['<output_folder>'] coverage_files = args['<coverage_files>'] search_config = args['--search'] config = [] if search_config: with open(search_config, "r") as config_file: config = load_search_config(config_file) faa_txt_folder = os.path.join(output, util.FAA_TXT_OUTPUT_FOLDER) html_folder = os.path.join(output, util.HTML_OUTPUT_FOLDER) if not os.path.exists(faa_txt_folder): os.makedirs(faa_txt_folder) if not os.path.exists(output): os.makedirs(html_folder) with open(unique_file_path, 'r') as unique, open(os.path.join(output, util.OVERVIEW_TXT), 'w') as output_file: writeHeader(coverage_files, output_file, bool(search_config)) reader = unique.readlines() for line in reader: row = line.split() LINK = util.NO_LINK ID = row[3] HMM = row[0] SCORE = row[7] EVALHMM = row[6] txt_path = os.path.join(faa_folder, ID + ".txt") faa_path = os.path.join(faa_folder, ID + ".faa") coverages = map(functools.partial(get_coverage_information, id=ID), coverage_files) contig_txt_info = get_contig_txt_information(txt_path) SEQ = get_sequence(faa_path) BASE_COLUMNS = [] if search_config: additional_column = determine_config_values(config, HMM) BASE_COLUMNS = [ID, HMM, additional_column[1], SCORE, EVALHMM] else: BASE_COLUMNS = [ID, HMM, SCORE, EVALHMM] coverages.extend(BASE_COLUMNS + contig_txt_info + [LINK, SEQ]) output_file.write(('\t'.join(str(x) for x in coverages)) + '\n') move_html_files(html_folder, os.path.join(faa_txt_folder, ID + ".html")) move_txt_faa_files(faa_txt_folder, txt_path, faa_path) if __name__ == '__main__': main()
mit
ryfeus/lambda-packs
H2O/ArchiveH2O/setuptools/_vendor/six.py
2715
30098
"""Utilities for writing code that runs on Python 2 and 3""" # Copyright (c) 2010-2015 Benjamin Peterson # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. from __future__ import absolute_import import functools import itertools import operator import sys import types __author__ = "Benjamin Peterson <[email protected]>" __version__ = "1.10.0" # Useful for very coarse version differentiation. PY2 = sys.version_info[0] == 2 PY3 = sys.version_info[0] == 3 PY34 = sys.version_info[0:2] >= (3, 4) if PY3: string_types = str, integer_types = int, class_types = type, text_type = str binary_type = bytes MAXSIZE = sys.maxsize else: string_types = basestring, integer_types = (int, long) class_types = (type, types.ClassType) text_type = unicode binary_type = str if sys.platform.startswith("java"): # Jython always uses 32 bits. MAXSIZE = int((1 << 31) - 1) else: # It's possible to have sizeof(long) != sizeof(Py_ssize_t). class X(object): def __len__(self): return 1 << 31 try: len(X()) except OverflowError: # 32-bit MAXSIZE = int((1 << 31) - 1) else: # 64-bit MAXSIZE = int((1 << 63) - 1) del X def _add_doc(func, doc): """Add documentation to a function.""" func.__doc__ = doc def _import_module(name): """Import module, returning the module after the last dot.""" __import__(name) return sys.modules[name] class _LazyDescr(object): def __init__(self, name): self.name = name def __get__(self, obj, tp): result = self._resolve() setattr(obj, self.name, result) # Invokes __set__. try: # This is a bit ugly, but it avoids running this again by # removing this descriptor. delattr(obj.__class__, self.name) except AttributeError: pass return result class MovedModule(_LazyDescr): def __init__(self, name, old, new=None): super(MovedModule, self).__init__(name) if PY3: if new is None: new = name self.mod = new else: self.mod = old def _resolve(self): return _import_module(self.mod) def __getattr__(self, attr): _module = self._resolve() value = getattr(_module, attr) setattr(self, attr, value) return value class _LazyModule(types.ModuleType): def __init__(self, name): super(_LazyModule, self).__init__(name) self.__doc__ = self.__class__.__doc__ def __dir__(self): attrs = ["__doc__", "__name__"] attrs += [attr.name for attr in self._moved_attributes] return attrs # Subclasses should override this _moved_attributes = [] class MovedAttribute(_LazyDescr): def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None): super(MovedAttribute, self).__init__(name) if PY3: if new_mod is None: new_mod = name self.mod = new_mod if new_attr is None: if old_attr is None: new_attr = name else: new_attr = old_attr self.attr = new_attr else: self.mod = old_mod if old_attr is None: old_attr = name self.attr = old_attr def _resolve(self): module = _import_module(self.mod) return getattr(module, self.attr) class _SixMetaPathImporter(object): """ A meta path importer to import six.moves and its submodules. This class implements a PEP302 finder and loader. It should be compatible with Python 2.5 and all existing versions of Python3 """ def __init__(self, six_module_name): self.name = six_module_name self.known_modules = {} def _add_module(self, mod, *fullnames): for fullname in fullnames: self.known_modules[self.name + "." + fullname] = mod def _get_module(self, fullname): return self.known_modules[self.name + "." + fullname] def find_module(self, fullname, path=None): if fullname in self.known_modules: return self return None def __get_module(self, fullname): try: return self.known_modules[fullname] except KeyError: raise ImportError("This loader does not know module " + fullname) def load_module(self, fullname): try: # in case of a reload return sys.modules[fullname] except KeyError: pass mod = self.__get_module(fullname) if isinstance(mod, MovedModule): mod = mod._resolve() else: mod.__loader__ = self sys.modules[fullname] = mod return mod def is_package(self, fullname): """ Return true, if the named module is a package. We need this method to get correct spec objects with Python 3.4 (see PEP451) """ return hasattr(self.__get_module(fullname), "__path__") def get_code(self, fullname): """Return None Required, if is_package is implemented""" self.__get_module(fullname) # eventually raises ImportError return None get_source = get_code # same as get_code _importer = _SixMetaPathImporter(__name__) class _MovedItems(_LazyModule): """Lazy loading of moved objects""" __path__ = [] # mark as package _moved_attributes = [ MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"), MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"), MovedAttribute("filterfalse", "itertools", "itertools", "ifilterfalse", "filterfalse"), MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"), MovedAttribute("intern", "__builtin__", "sys"), MovedAttribute("map", "itertools", "builtins", "imap", "map"), MovedAttribute("getcwd", "os", "os", "getcwdu", "getcwd"), MovedAttribute("getcwdb", "os", "os", "getcwd", "getcwdb"), MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"), MovedAttribute("reload_module", "__builtin__", "importlib" if PY34 else "imp", "reload"), MovedAttribute("reduce", "__builtin__", "functools"), MovedAttribute("shlex_quote", "pipes", "shlex", "quote"), MovedAttribute("StringIO", "StringIO", "io"), MovedAttribute("UserDict", "UserDict", "collections"), MovedAttribute("UserList", "UserList", "collections"), MovedAttribute("UserString", "UserString", "collections"), MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"), MovedAttribute("zip", "itertools", "builtins", "izip", "zip"), MovedAttribute("zip_longest", "itertools", "itertools", "izip_longest", "zip_longest"), MovedModule("builtins", "__builtin__"), MovedModule("configparser", "ConfigParser"), MovedModule("copyreg", "copy_reg"), MovedModule("dbm_gnu", "gdbm", "dbm.gnu"), MovedModule("_dummy_thread", "dummy_thread", "_dummy_thread"), MovedModule("http_cookiejar", "cookielib", "http.cookiejar"), MovedModule("http_cookies", "Cookie", "http.cookies"), MovedModule("html_entities", "htmlentitydefs", "html.entities"), MovedModule("html_parser", "HTMLParser", "html.parser"), MovedModule("http_client", "httplib", "http.client"), MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"), MovedModule("email_mime_nonmultipart", "email.MIMENonMultipart", "email.mime.nonmultipart"), MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"), MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"), MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"), MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"), MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"), MovedModule("cPickle", "cPickle", "pickle"), MovedModule("queue", "Queue"), MovedModule("reprlib", "repr"), MovedModule("socketserver", "SocketServer"), MovedModule("_thread", "thread", "_thread"), MovedModule("tkinter", "Tkinter"), MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"), MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"), MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"), MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"), MovedModule("tkinter_tix", "Tix", "tkinter.tix"), MovedModule("tkinter_ttk", "ttk", "tkinter.ttk"), MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"), MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"), MovedModule("tkinter_colorchooser", "tkColorChooser", "tkinter.colorchooser"), MovedModule("tkinter_commondialog", "tkCommonDialog", "tkinter.commondialog"), MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"), MovedModule("tkinter_font", "tkFont", "tkinter.font"), MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"), MovedModule("tkinter_tksimpledialog", "tkSimpleDialog", "tkinter.simpledialog"), MovedModule("urllib_parse", __name__ + ".moves.urllib_parse", "urllib.parse"), MovedModule("urllib_error", __name__ + ".moves.urllib_error", "urllib.error"), MovedModule("urllib", __name__ + ".moves.urllib", __name__ + ".moves.urllib"), MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"), MovedModule("xmlrpc_client", "xmlrpclib", "xmlrpc.client"), MovedModule("xmlrpc_server", "SimpleXMLRPCServer", "xmlrpc.server"), ] # Add windows specific modules. if sys.platform == "win32": _moved_attributes += [ MovedModule("winreg", "_winreg"), ] for attr in _moved_attributes: setattr(_MovedItems, attr.name, attr) if isinstance(attr, MovedModule): _importer._add_module(attr, "moves." + attr.name) del attr _MovedItems._moved_attributes = _moved_attributes moves = _MovedItems(__name__ + ".moves") _importer._add_module(moves, "moves") class Module_six_moves_urllib_parse(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_parse""" _urllib_parse_moved_attributes = [ MovedAttribute("ParseResult", "urlparse", "urllib.parse"), MovedAttribute("SplitResult", "urlparse", "urllib.parse"), MovedAttribute("parse_qs", "urlparse", "urllib.parse"), MovedAttribute("parse_qsl", "urlparse", "urllib.parse"), MovedAttribute("urldefrag", "urlparse", "urllib.parse"), MovedAttribute("urljoin", "urlparse", "urllib.parse"), MovedAttribute("urlparse", "urlparse", "urllib.parse"), MovedAttribute("urlsplit", "urlparse", "urllib.parse"), MovedAttribute("urlunparse", "urlparse", "urllib.parse"), MovedAttribute("urlunsplit", "urlparse", "urllib.parse"), MovedAttribute("quote", "urllib", "urllib.parse"), MovedAttribute("quote_plus", "urllib", "urllib.parse"), MovedAttribute("unquote", "urllib", "urllib.parse"), MovedAttribute("unquote_plus", "urllib", "urllib.parse"), MovedAttribute("urlencode", "urllib", "urllib.parse"), MovedAttribute("splitquery", "urllib", "urllib.parse"), MovedAttribute("splittag", "urllib", "urllib.parse"), MovedAttribute("splituser", "urllib", "urllib.parse"), MovedAttribute("uses_fragment", "urlparse", "urllib.parse"), MovedAttribute("uses_netloc", "urlparse", "urllib.parse"), MovedAttribute("uses_params", "urlparse", "urllib.parse"), MovedAttribute("uses_query", "urlparse", "urllib.parse"), MovedAttribute("uses_relative", "urlparse", "urllib.parse"), ] for attr in _urllib_parse_moved_attributes: setattr(Module_six_moves_urllib_parse, attr.name, attr) del attr Module_six_moves_urllib_parse._moved_attributes = _urllib_parse_moved_attributes _importer._add_module(Module_six_moves_urllib_parse(__name__ + ".moves.urllib_parse"), "moves.urllib_parse", "moves.urllib.parse") class Module_six_moves_urllib_error(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_error""" _urllib_error_moved_attributes = [ MovedAttribute("URLError", "urllib2", "urllib.error"), MovedAttribute("HTTPError", "urllib2", "urllib.error"), MovedAttribute("ContentTooShortError", "urllib", "urllib.error"), ] for attr in _urllib_error_moved_attributes: setattr(Module_six_moves_urllib_error, attr.name, attr) del attr Module_six_moves_urllib_error._moved_attributes = _urllib_error_moved_attributes _importer._add_module(Module_six_moves_urllib_error(__name__ + ".moves.urllib.error"), "moves.urllib_error", "moves.urllib.error") class Module_six_moves_urllib_request(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_request""" _urllib_request_moved_attributes = [ MovedAttribute("urlopen", "urllib2", "urllib.request"), MovedAttribute("install_opener", "urllib2", "urllib.request"), MovedAttribute("build_opener", "urllib2", "urllib.request"), MovedAttribute("pathname2url", "urllib", "urllib.request"), MovedAttribute("url2pathname", "urllib", "urllib.request"), MovedAttribute("getproxies", "urllib", "urllib.request"), MovedAttribute("Request", "urllib2", "urllib.request"), MovedAttribute("OpenerDirector", "urllib2", "urllib.request"), MovedAttribute("HTTPDefaultErrorHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPRedirectHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPCookieProcessor", "urllib2", "urllib.request"), MovedAttribute("ProxyHandler", "urllib2", "urllib.request"), MovedAttribute("BaseHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPPasswordMgr", "urllib2", "urllib.request"), MovedAttribute("HTTPPasswordMgrWithDefaultRealm", "urllib2", "urllib.request"), MovedAttribute("AbstractBasicAuthHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPBasicAuthHandler", "urllib2", "urllib.request"), MovedAttribute("ProxyBasicAuthHandler", "urllib2", "urllib.request"), MovedAttribute("AbstractDigestAuthHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPDigestAuthHandler", "urllib2", "urllib.request"), MovedAttribute("ProxyDigestAuthHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPSHandler", "urllib2", "urllib.request"), MovedAttribute("FileHandler", "urllib2", "urllib.request"), MovedAttribute("FTPHandler", "urllib2", "urllib.request"), MovedAttribute("CacheFTPHandler", "urllib2", "urllib.request"), MovedAttribute("UnknownHandler", "urllib2", "urllib.request"), MovedAttribute("HTTPErrorProcessor", "urllib2", "urllib.request"), MovedAttribute("urlretrieve", "urllib", "urllib.request"), MovedAttribute("urlcleanup", "urllib", "urllib.request"), MovedAttribute("URLopener", "urllib", "urllib.request"), MovedAttribute("FancyURLopener", "urllib", "urllib.request"), MovedAttribute("proxy_bypass", "urllib", "urllib.request"), ] for attr in _urllib_request_moved_attributes: setattr(Module_six_moves_urllib_request, attr.name, attr) del attr Module_six_moves_urllib_request._moved_attributes = _urllib_request_moved_attributes _importer._add_module(Module_six_moves_urllib_request(__name__ + ".moves.urllib.request"), "moves.urllib_request", "moves.urllib.request") class Module_six_moves_urllib_response(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_response""" _urllib_response_moved_attributes = [ MovedAttribute("addbase", "urllib", "urllib.response"), MovedAttribute("addclosehook", "urllib", "urllib.response"), MovedAttribute("addinfo", "urllib", "urllib.response"), MovedAttribute("addinfourl", "urllib", "urllib.response"), ] for attr in _urllib_response_moved_attributes: setattr(Module_six_moves_urllib_response, attr.name, attr) del attr Module_six_moves_urllib_response._moved_attributes = _urllib_response_moved_attributes _importer._add_module(Module_six_moves_urllib_response(__name__ + ".moves.urllib.response"), "moves.urllib_response", "moves.urllib.response") class Module_six_moves_urllib_robotparser(_LazyModule): """Lazy loading of moved objects in six.moves.urllib_robotparser""" _urllib_robotparser_moved_attributes = [ MovedAttribute("RobotFileParser", "robotparser", "urllib.robotparser"), ] for attr in _urllib_robotparser_moved_attributes: setattr(Module_six_moves_urllib_robotparser, attr.name, attr) del attr Module_six_moves_urllib_robotparser._moved_attributes = _urllib_robotparser_moved_attributes _importer._add_module(Module_six_moves_urllib_robotparser(__name__ + ".moves.urllib.robotparser"), "moves.urllib_robotparser", "moves.urllib.robotparser") class Module_six_moves_urllib(types.ModuleType): """Create a six.moves.urllib namespace that resembles the Python 3 namespace""" __path__ = [] # mark as package parse = _importer._get_module("moves.urllib_parse") error = _importer._get_module("moves.urllib_error") request = _importer._get_module("moves.urllib_request") response = _importer._get_module("moves.urllib_response") robotparser = _importer._get_module("moves.urllib_robotparser") def __dir__(self): return ['parse', 'error', 'request', 'response', 'robotparser'] _importer._add_module(Module_six_moves_urllib(__name__ + ".moves.urllib"), "moves.urllib") def add_move(move): """Add an item to six.moves.""" setattr(_MovedItems, move.name, move) def remove_move(name): """Remove item from six.moves.""" try: delattr(_MovedItems, name) except AttributeError: try: del moves.__dict__[name] except KeyError: raise AttributeError("no such move, %r" % (name,)) if PY3: _meth_func = "__func__" _meth_self = "__self__" _func_closure = "__closure__" _func_code = "__code__" _func_defaults = "__defaults__" _func_globals = "__globals__" else: _meth_func = "im_func" _meth_self = "im_self" _func_closure = "func_closure" _func_code = "func_code" _func_defaults = "func_defaults" _func_globals = "func_globals" try: advance_iterator = next except NameError: def advance_iterator(it): return it.next() next = advance_iterator try: callable = callable except NameError: def callable(obj): return any("__call__" in klass.__dict__ for klass in type(obj).__mro__) if PY3: def get_unbound_function(unbound): return unbound create_bound_method = types.MethodType def create_unbound_method(func, cls): return func Iterator = object else: def get_unbound_function(unbound): return unbound.im_func def create_bound_method(func, obj): return types.MethodType(func, obj, obj.__class__) def create_unbound_method(func, cls): return types.MethodType(func, None, cls) class Iterator(object): def next(self): return type(self).__next__(self) callable = callable _add_doc(get_unbound_function, """Get the function out of a possibly unbound function""") get_method_function = operator.attrgetter(_meth_func) get_method_self = operator.attrgetter(_meth_self) get_function_closure = operator.attrgetter(_func_closure) get_function_code = operator.attrgetter(_func_code) get_function_defaults = operator.attrgetter(_func_defaults) get_function_globals = operator.attrgetter(_func_globals) if PY3: def iterkeys(d, **kw): return iter(d.keys(**kw)) def itervalues(d, **kw): return iter(d.values(**kw)) def iteritems(d, **kw): return iter(d.items(**kw)) def iterlists(d, **kw): return iter(d.lists(**kw)) viewkeys = operator.methodcaller("keys") viewvalues = operator.methodcaller("values") viewitems = operator.methodcaller("items") else: def iterkeys(d, **kw): return d.iterkeys(**kw) def itervalues(d, **kw): return d.itervalues(**kw) def iteritems(d, **kw): return d.iteritems(**kw) def iterlists(d, **kw): return d.iterlists(**kw) viewkeys = operator.methodcaller("viewkeys") viewvalues = operator.methodcaller("viewvalues") viewitems = operator.methodcaller("viewitems") _add_doc(iterkeys, "Return an iterator over the keys of a dictionary.") _add_doc(itervalues, "Return an iterator over the values of a dictionary.") _add_doc(iteritems, "Return an iterator over the (key, value) pairs of a dictionary.") _add_doc(iterlists, "Return an iterator over the (key, [values]) pairs of a dictionary.") if PY3: def b(s): return s.encode("latin-1") def u(s): return s unichr = chr import struct int2byte = struct.Struct(">B").pack del struct byte2int = operator.itemgetter(0) indexbytes = operator.getitem iterbytes = iter import io StringIO = io.StringIO BytesIO = io.BytesIO _assertCountEqual = "assertCountEqual" if sys.version_info[1] <= 1: _assertRaisesRegex = "assertRaisesRegexp" _assertRegex = "assertRegexpMatches" else: _assertRaisesRegex = "assertRaisesRegex" _assertRegex = "assertRegex" else: def b(s): return s # Workaround for standalone backslash def u(s): return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape") unichr = unichr int2byte = chr def byte2int(bs): return ord(bs[0]) def indexbytes(buf, i): return ord(buf[i]) iterbytes = functools.partial(itertools.imap, ord) import StringIO StringIO = BytesIO = StringIO.StringIO _assertCountEqual = "assertItemsEqual" _assertRaisesRegex = "assertRaisesRegexp" _assertRegex = "assertRegexpMatches" _add_doc(b, """Byte literal""") _add_doc(u, """Text literal""") def assertCountEqual(self, *args, **kwargs): return getattr(self, _assertCountEqual)(*args, **kwargs) def assertRaisesRegex(self, *args, **kwargs): return getattr(self, _assertRaisesRegex)(*args, **kwargs) def assertRegex(self, *args, **kwargs): return getattr(self, _assertRegex)(*args, **kwargs) if PY3: exec_ = getattr(moves.builtins, "exec") def reraise(tp, value, tb=None): if value is None: value = tp() if value.__traceback__ is not tb: raise value.with_traceback(tb) raise value else: def exec_(_code_, _globs_=None, _locs_=None): """Execute code in a namespace.""" if _globs_ is None: frame = sys._getframe(1) _globs_ = frame.f_globals if _locs_ is None: _locs_ = frame.f_locals del frame elif _locs_ is None: _locs_ = _globs_ exec("""exec _code_ in _globs_, _locs_""") exec_("""def reraise(tp, value, tb=None): raise tp, value, tb """) if sys.version_info[:2] == (3, 2): exec_("""def raise_from(value, from_value): if from_value is None: raise value raise value from from_value """) elif sys.version_info[:2] > (3, 2): exec_("""def raise_from(value, from_value): raise value from from_value """) else: def raise_from(value, from_value): raise value print_ = getattr(moves.builtins, "print", None) if print_ is None: def print_(*args, **kwargs): """The new-style print function for Python 2.4 and 2.5.""" fp = kwargs.pop("file", sys.stdout) if fp is None: return def write(data): if not isinstance(data, basestring): data = str(data) # If the file has an encoding, encode unicode with it. if (isinstance(fp, file) and isinstance(data, unicode) and fp.encoding is not None): errors = getattr(fp, "errors", None) if errors is None: errors = "strict" data = data.encode(fp.encoding, errors) fp.write(data) want_unicode = False sep = kwargs.pop("sep", None) if sep is not None: if isinstance(sep, unicode): want_unicode = True elif not isinstance(sep, str): raise TypeError("sep must be None or a string") end = kwargs.pop("end", None) if end is not None: if isinstance(end, unicode): want_unicode = True elif not isinstance(end, str): raise TypeError("end must be None or a string") if kwargs: raise TypeError("invalid keyword arguments to print()") if not want_unicode: for arg in args: if isinstance(arg, unicode): want_unicode = True break if want_unicode: newline = unicode("\n") space = unicode(" ") else: newline = "\n" space = " " if sep is None: sep = space if end is None: end = newline for i, arg in enumerate(args): if i: write(sep) write(arg) write(end) if sys.version_info[:2] < (3, 3): _print = print_ def print_(*args, **kwargs): fp = kwargs.get("file", sys.stdout) flush = kwargs.pop("flush", False) _print(*args, **kwargs) if flush and fp is not None: fp.flush() _add_doc(reraise, """Reraise an exception.""") if sys.version_info[0:2] < (3, 4): def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES): def wrapper(f): f = functools.wraps(wrapped, assigned, updated)(f) f.__wrapped__ = wrapped return f return wrapper else: wraps = functools.wraps def with_metaclass(meta, *bases): """Create a base class with a metaclass.""" # This requires a bit of explanation: the basic idea is to make a dummy # metaclass for one level of class instantiation that replaces itself with # the actual metaclass. class metaclass(meta): def __new__(cls, name, this_bases, d): return meta(name, bases, d) return type.__new__(metaclass, 'temporary_class', (), {}) def add_metaclass(metaclass): """Class decorator for creating a class with a metaclass.""" def wrapper(cls): orig_vars = cls.__dict__.copy() slots = orig_vars.get('__slots__') if slots is not None: if isinstance(slots, str): slots = [slots] for slots_var in slots: orig_vars.pop(slots_var) orig_vars.pop('__dict__', None) orig_vars.pop('__weakref__', None) return metaclass(cls.__name__, cls.__bases__, orig_vars) return wrapper def python_2_unicode_compatible(klass): """ A decorator that defines __unicode__ and __str__ methods under Python 2. Under Python 3 it does nothing. To support Python 2 and 3 with a single code base, define a __str__ method returning text and apply this decorator to the class. """ if PY2: if '__str__' not in klass.__dict__: raise ValueError("@python_2_unicode_compatible cannot be applied " "to %s because it doesn't define __str__()." % klass.__name__) klass.__unicode__ = klass.__str__ klass.__str__ = lambda self: self.__unicode__().encode('utf-8') return klass # Complete the moves implementation. # This code is at the end of this module to speed up module loading. # Turn this module into a package. __path__ = [] # required for PEP 302 and PEP 451 __package__ = __name__ # see PEP 366 @ReservedAssignment if globals().get("__spec__") is not None: __spec__.submodule_search_locations = [] # PEP 451 @UndefinedVariable # Remove other six meta path importers, since they cause problems. This can # happen if six is removed from sys.modules and then reloaded. (Setuptools does # this for some reason.) if sys.meta_path: for i, importer in enumerate(sys.meta_path): # Here's some real nastiness: Another "instance" of the six module might # be floating around. Therefore, we can't use isinstance() to check for # the six meta path importer, since the other six instance will have # inserted an importer with different class. if (type(importer).__name__ == "_SixMetaPathImporter" and importer.name == __name__): del sys.meta_path[i] break del i, importer # Finally, add the importer to the meta path import hook. sys.meta_path.append(_importer)
mit
metashell/metashell
3rd/templight/clang/utils/clangdiag.py
28
7621
#!/usr/bin/python #---------------------------------------------------------------------- # Be sure to add the python path that points to the LLDB shared library. # # # To use this in the embedded python interpreter using "lldb" just # import it with the full path using the "command script import" # command # (lldb) command script import /path/to/clandiag.py #---------------------------------------------------------------------- from __future__ import absolute_import, division, print_function import lldb import argparse import shlex import os import re import subprocess class MyParser(argparse.ArgumentParser): def format_help(self): return ''' Commands for managing clang diagnostic breakpoints Syntax: clangdiag enable [<warning>|<diag-name>] clangdiag disable clangdiag diagtool [<path>|reset] The following subcommands are supported: enable -- Enable clang diagnostic breakpoints. disable -- Disable all clang diagnostic breakpoints. diagtool -- Return, set, or reset diagtool path. This command sets breakpoints in clang, and clang based tools, that emit diagnostics. When a diagnostic is emitted, and clangdiag is enabled, it will use the appropriate diagtool application to determine the name of the DiagID, and set breakpoints in all locations that 'diag::name' appears in the source. Since the new breakpoints are set after they are encountered, users will need to launch the executable a second time in order to hit the new breakpoints. For in-tree builds, the diagtool application, used to map DiagID's to names, is found automatically in the same directory as the target executable. However, out-or-tree builds must use the 'diagtool' subcommand to set the appropriate path for diagtool in the clang debug bin directory. Since this mapping is created at build-time, it's important for users to use the same version that was generated when clang was compiled, or else the id's won't match. Notes: - Substrings can be passed for both <warning> and <diag-name>. - If <warning> is passed, only enable the DiagID(s) for that warning. - If <diag-name> is passed, only enable that DiagID. - Rerunning enable clears existing breakpoints. - diagtool is used in breakpoint callbacks, so it can be changed without the need to rerun enable. - Adding this to your ~.lldbinit file makes clangdiag available at startup: "command script import /path/to/clangdiag.py" ''' def create_diag_options(): parser = MyParser(prog='clangdiag') subparsers = parser.add_subparsers( title='subcommands', dest='subcommands', metavar='') disable_parser = subparsers.add_parser('disable') enable_parser = subparsers.add_parser('enable') enable_parser.add_argument('id', nargs='?') diagtool_parser = subparsers.add_parser('diagtool') diagtool_parser.add_argument('path', nargs='?') return parser def getDiagtool(target, diagtool = None): id = target.GetProcess().GetProcessID() if 'diagtool' not in getDiagtool.__dict__: getDiagtool.diagtool = {} if diagtool: if diagtool == 'reset': getDiagtool.diagtool[id] = None elif os.path.exists(diagtool): getDiagtool.diagtool[id] = diagtool else: print('clangdiag: %s not found.' % diagtool) if not id in getDiagtool.diagtool or not getDiagtool.diagtool[id]: getDiagtool.diagtool[id] = None exe = target.GetExecutable() if not exe.Exists(): print('clangdiag: Target (%s) not set.' % exe.GetFilename()) else: diagtool = os.path.join(exe.GetDirectory(), 'diagtool') if os.path.exists(diagtool): getDiagtool.diagtool[id] = diagtool else: print('clangdiag: diagtool not found along side %s' % exe) return getDiagtool.diagtool[id] def setDiagBreakpoint(frame, bp_loc, dict): id = frame.FindVariable("DiagID").GetValue() if id is None: print('clangdiag: id is None') return False # Don't need to test this time, since we did that in enable. target = frame.GetThread().GetProcess().GetTarget() diagtool = getDiagtool(target) name = subprocess.check_output([diagtool, "find-diagnostic-id", id]).rstrip(); # Make sure we only consider errors, warnings, and extensions. # FIXME: Make this configurable? prefixes = ['err_', 'warn_', 'exp_'] if len([prefix for prefix in prefixes+[''] if name.startswith(prefix)][0]): bp = target.BreakpointCreateBySourceRegex(name, lldb.SBFileSpec()) bp.AddName("clang::Diagnostic") return False def enable(exe_ctx, args): # Always disable existing breakpoints disable(exe_ctx) target = exe_ctx.GetTarget() numOfBreakpoints = target.GetNumBreakpoints() if args.id: # Make sure we only consider errors, warnings, and extensions. # FIXME: Make this configurable? prefixes = ['err_', 'warn_', 'exp_'] if len([prefix for prefix in prefixes+[''] if args.id.startswith(prefix)][0]): bp = target.BreakpointCreateBySourceRegex(args.id, lldb.SBFileSpec()) bp.AddName("clang::Diagnostic") else: diagtool = getDiagtool(target) list = subprocess.check_output([diagtool, "list-warnings"]).rstrip(); for line in list.splitlines(True): m = re.search(r' *(.*) .*\[\-W' + re.escape(args.id) + r'.*].*', line) # Make sure we only consider warnings. if m and m.group(1).startswith('warn_'): bp = target.BreakpointCreateBySourceRegex(m.group(1), lldb.SBFileSpec()) bp.AddName("clang::Diagnostic") else: print('Adding callbacks.') bp = target.BreakpointCreateByName('DiagnosticsEngine::Report') bp.SetScriptCallbackFunction('clangdiag.setDiagBreakpoint') bp.AddName("clang::Diagnostic") count = target.GetNumBreakpoints() - numOfBreakpoints print('%i breakpoint%s added.' % (count, "s"[count==1:])) return def disable(exe_ctx): target = exe_ctx.GetTarget() # Remove all diag breakpoints. bkpts = lldb.SBBreakpointList(target) target.FindBreakpointsByName("clang::Diagnostic", bkpts) for i in range(bkpts.GetSize()): target.BreakpointDelete(bkpts.GetBreakpointAtIndex(i).GetID()) return def the_diag_command(debugger, command, exe_ctx, result, dict): # Use the Shell Lexer to properly parse up command options just like a # shell would command_args = shlex.split(command) parser = create_diag_options() try: args = parser.parse_args(command_args) except: return if args.subcommands == 'enable': enable(exe_ctx, args) elif args.subcommands == 'disable': disable(exe_ctx) else: diagtool = getDiagtool(exe_ctx.GetTarget(), args.path) print('diagtool = %s' % diagtool) return def __lldb_init_module(debugger, dict): # This initializer is being run from LLDB in the embedded command interpreter # Make the options so we can generate the help text for the new LLDB # command line command prior to registering it with LLDB below parser = create_diag_options() the_diag_command.__doc__ = parser.format_help() # Add any commands contained in this module to LLDB debugger.HandleCommand( 'command script add -f clangdiag.the_diag_command clangdiag') print('The "clangdiag" command has been installed, type "help clangdiag" or "clangdiag --help" for detailed help.')
gpl-3.0
noslenfa/tdjangorest
uw/lib/python2.7/site-packages/django/dispatch/saferef.py
218
10623
""" "Safe weakrefs", originally from pyDispatcher. Provides a way to safely weakref any function, including bound methods (which aren't handled by the core weakref module). """ import traceback import weakref def safeRef(target, onDelete = None): """Return a *safe* weak reference to a callable target target -- the object to be weakly referenced, if it's a bound method reference, will create a BoundMethodWeakref, otherwise creates a simple weakref. onDelete -- if provided, will have a hard reference stored to the callable to be called after the safe reference goes out of scope with the reference object, (either a weakref or a BoundMethodWeakref) as argument. """ if hasattr(target, '__self__'): if target.__self__ is not None: # Turn a bound method into a BoundMethodWeakref instance. # Keep track of these instances for lookup by disconnect(). assert hasattr(target, '__func__'), """safeRef target %r has __self__, but no __func__, don't know how to create reference"""%( target,) reference = get_bound_method_weakref( target=target, onDelete=onDelete ) return reference if callable(onDelete): return weakref.ref(target, onDelete) else: return weakref.ref( target ) class BoundMethodWeakref(object): """'Safe' and reusable weak references to instance methods BoundMethodWeakref objects provide a mechanism for referencing a bound method without requiring that the method object itself (which is normally a transient object) is kept alive. Instead, the BoundMethodWeakref object keeps weak references to both the object and the function which together define the instance method. Attributes: key -- the identity key for the reference, calculated by the class's calculateKey method applied to the target instance method deletionMethods -- sequence of callable objects taking single argument, a reference to this object which will be called when *either* the target object or target function is garbage collected (i.e. when this object becomes invalid). These are specified as the onDelete parameters of safeRef calls. weakSelf -- weak reference to the target object weakFunc -- weak reference to the target function Class Attributes: _allInstances -- class attribute pointing to all live BoundMethodWeakref objects indexed by the class's calculateKey(target) method applied to the target objects. This weak value dictionary is used to short-circuit creation so that multiple references to the same (object, function) pair produce the same BoundMethodWeakref instance. """ _allInstances = weakref.WeakValueDictionary() def __new__( cls, target, onDelete=None, *arguments,**named ): """Create new instance or return current instance Basically this method of construction allows us to short-circuit creation of references to already- referenced instance methods. The key corresponding to the target is calculated, and if there is already an existing reference, that is returned, with its deletionMethods attribute updated. Otherwise the new instance is created and registered in the table of already-referenced methods. """ key = cls.calculateKey(target) current =cls._allInstances.get(key) if current is not None: current.deletionMethods.append( onDelete) return current else: base = super( BoundMethodWeakref, cls).__new__( cls ) cls._allInstances[key] = base base.__init__( target, onDelete, *arguments,**named) return base def __init__(self, target, onDelete=None): """Return a weak-reference-like instance for a bound method target -- the instance-method target for the weak reference, must have __self__ and __func__ attributes and be reconstructable via: target.__func__.__get__( target.__self__ ) which is true of built-in instance methods. onDelete -- optional callback which will be called when this weak reference ceases to be valid (i.e. either the object or the function is garbage collected). Should take a single argument, which will be passed a pointer to this object. """ def remove(weak, self=self): """Set self.isDead to true when method or instance is destroyed""" methods = self.deletionMethods[:] del self.deletionMethods[:] try: del self.__class__._allInstances[ self.key ] except KeyError: pass for function in methods: try: if callable( function ): function( self ) except Exception as e: try: traceback.print_exc() except AttributeError: print('Exception during saferef %s cleanup function %s: %s' % ( self, function, e) ) self.deletionMethods = [onDelete] self.key = self.calculateKey( target ) self.weakSelf = weakref.ref(target.__self__, remove) self.weakFunc = weakref.ref(target.__func__, remove) self.selfName = str(target.__self__) self.funcName = str(target.__func__.__name__) def calculateKey( cls, target ): """Calculate the reference key for this reference Currently this is a two-tuple of the id()'s of the target object and the target function respectively. """ return (id(target.__self__),id(target.__func__)) calculateKey = classmethod( calculateKey ) def __str__(self): """Give a friendly representation of the object""" return """%s( %s.%s )"""%( self.__class__.__name__, self.selfName, self.funcName, ) __repr__ = __str__ def __hash__(self): return hash(self.key) def __bool__( self ): """Whether we are still a valid reference""" return self() is not None def __nonzero__(self): # Python 2 compatibility return type(self).__bool__(self) def __eq__(self, other): """Compare with another reference""" if not isinstance(other, self.__class__): return self.__class__ == type(other) return self.key == other.key def __call__(self): """Return a strong reference to the bound method If the target cannot be retrieved, then will return None, otherwise returns a bound instance method for our object and function. Note: You may call this method any number of times, as it does not invalidate the reference. """ target = self.weakSelf() if target is not None: function = self.weakFunc() if function is not None: return function.__get__(target) return None class BoundNonDescriptorMethodWeakref(BoundMethodWeakref): """A specialized BoundMethodWeakref, for platforms where instance methods are not descriptors. It assumes that the function name and the target attribute name are the same, instead of assuming that the function is a descriptor. This approach is equally fast, but not 100% reliable because functions can be stored on an attribute named differenty than the function's name such as in: class A: pass def foo(self): return "foo" A.bar = foo But this shouldn't be a common use case. So, on platforms where methods aren't descriptors (such as Jython) this implementation has the advantage of working in the most cases. """ def __init__(self, target, onDelete=None): """Return a weak-reference-like instance for a bound method target -- the instance-method target for the weak reference, must have __self__ and __func__ attributes and be reconstructable via: target.__func__.__get__( target.__self__ ) which is true of built-in instance methods. onDelete -- optional callback which will be called when this weak reference ceases to be valid (i.e. either the object or the function is garbage collected). Should take a single argument, which will be passed a pointer to this object. """ assert getattr(target.__self__, target.__name__) == target, \ ("method %s isn't available as the attribute %s of %s" % (target, target.__name__, target.__self__)) super(BoundNonDescriptorMethodWeakref, self).__init__(target, onDelete) def __call__(self): """Return a strong reference to the bound method If the target cannot be retrieved, then will return None, otherwise returns a bound instance method for our object and function. Note: You may call this method any number of times, as it does not invalidate the reference. """ target = self.weakSelf() if target is not None: function = self.weakFunc() if function is not None: # Using partial() would be another option, but it erases the # "signature" of the function. That is, after a function is # curried, the inspect module can't be used to determine how # many arguments the function expects, nor what keyword # arguments it supports, and pydispatcher needs this # information. return getattr(target, function.__name__) return None def get_bound_method_weakref(target, onDelete): """Instantiates the appropiate BoundMethodWeakRef, depending on the details of the underlying class method implementation""" if hasattr(target, '__get__'): # target method is a descriptor, so the default implementation works: return BoundMethodWeakref(target=target, onDelete=onDelete) else: # no luck, use the alternative implementation: return BoundNonDescriptorMethodWeakref(target=target, onDelete=onDelete)
apache-2.0
JizhouZhang/SDR
docs/doxygen/other/doxypy.py
76
13882
#!/usr/bin/env python __applicationName__ = "doxypy" __blurb__ = """ doxypy is an input filter for Doxygen. It preprocesses python files so that docstrings of classes and functions are reformatted into Doxygen-conform documentation blocks. """ __doc__ = __blurb__ + \ """ In order to make Doxygen preprocess files through doxypy, simply add the following lines to your Doxyfile: FILTER_SOURCE_FILES = YES INPUT_FILTER = "python /path/to/doxypy.py" """ __version__ = "0.4.1" __date__ = "5th December 2008" __website__ = "http://code.foosel.org/doxypy" __author__ = ( "Philippe 'demod' Neumann (doxypy at demod dot org)", "Gina 'foosel' Haeussge (gina at foosel dot net)" ) __licenseName__ = "GPL v2" __license__ = """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, see <http://www.gnu.org/licenses/>. """ import sys import re from optparse import OptionParser, OptionGroup class FSM(object): """Implements a finite state machine. Transitions are given as 4-tuples, consisting of an origin state, a target state, a condition for the transition (given as a reference to a function which gets called with a given piece of input) and a pointer to a function to be called upon the execution of the given transition. """ """ @var transitions holds the transitions @var current_state holds the current state @var current_input holds the current input @var current_transition hold the currently active transition """ def __init__(self, start_state=None, transitions=[]): self.transitions = transitions self.current_state = start_state self.current_input = None self.current_transition = None def setStartState(self, state): self.current_state = state def addTransition(self, from_state, to_state, condition, callback): self.transitions.append([from_state, to_state, condition, callback]) def makeTransition(self, input): """ Makes a transition based on the given input. @param input input to parse by the FSM """ for transition in self.transitions: [from_state, to_state, condition, callback] = transition if from_state == self.current_state: match = condition(input) if match: self.current_state = to_state self.current_input = input self.current_transition = transition if options.debug: print >>sys.stderr, "# FSM: executing (%s -> %s) for line '%s'" % (from_state, to_state, input) callback(match) return class Doxypy(object): def __init__(self): string_prefixes = "[uU]?[rR]?" self.start_single_comment_re = re.compile("^\s*%s(''')" % string_prefixes) self.end_single_comment_re = re.compile("(''')\s*$") self.start_double_comment_re = re.compile("^\s*%s(\"\"\")" % string_prefixes) self.end_double_comment_re = re.compile("(\"\"\")\s*$") self.single_comment_re = re.compile("^\s*%s(''').*(''')\s*$" % string_prefixes) self.double_comment_re = re.compile("^\s*%s(\"\"\").*(\"\"\")\s*$" % string_prefixes) self.defclass_re = re.compile("^(\s*)(def .+:|class .+:)") self.empty_re = re.compile("^\s*$") self.hashline_re = re.compile("^\s*#.*$") self.importline_re = re.compile("^\s*(import |from .+ import)") self.multiline_defclass_start_re = re.compile("^(\s*)(def|class)(\s.*)?$") self.multiline_defclass_end_re = re.compile(":\s*$") ## Transition list format # ["FROM", "TO", condition, action] transitions = [ ### FILEHEAD # single line comments ["FILEHEAD", "FILEHEAD", self.single_comment_re.search, self.appendCommentLine], ["FILEHEAD", "FILEHEAD", self.double_comment_re.search, self.appendCommentLine], # multiline comments ["FILEHEAD", "FILEHEAD_COMMENT_SINGLE", self.start_single_comment_re.search, self.appendCommentLine], ["FILEHEAD_COMMENT_SINGLE", "FILEHEAD", self.end_single_comment_re.search, self.appendCommentLine], ["FILEHEAD_COMMENT_SINGLE", "FILEHEAD_COMMENT_SINGLE", self.catchall, self.appendCommentLine], ["FILEHEAD", "FILEHEAD_COMMENT_DOUBLE", self.start_double_comment_re.search, self.appendCommentLine], ["FILEHEAD_COMMENT_DOUBLE", "FILEHEAD", self.end_double_comment_re.search, self.appendCommentLine], ["FILEHEAD_COMMENT_DOUBLE", "FILEHEAD_COMMENT_DOUBLE", self.catchall, self.appendCommentLine], # other lines ["FILEHEAD", "FILEHEAD", self.empty_re.search, self.appendFileheadLine], ["FILEHEAD", "FILEHEAD", self.hashline_re.search, self.appendFileheadLine], ["FILEHEAD", "FILEHEAD", self.importline_re.search, self.appendFileheadLine], ["FILEHEAD", "DEFCLASS", self.defclass_re.search, self.resetCommentSearch], ["FILEHEAD", "DEFCLASS_MULTI", self.multiline_defclass_start_re.search, self.resetCommentSearch], ["FILEHEAD", "DEFCLASS_BODY", self.catchall, self.appendFileheadLine], ### DEFCLASS # single line comments ["DEFCLASS", "DEFCLASS_BODY", self.single_comment_re.search, self.appendCommentLine], ["DEFCLASS", "DEFCLASS_BODY", self.double_comment_re.search, self.appendCommentLine], # multiline comments ["DEFCLASS", "COMMENT_SINGLE", self.start_single_comment_re.search, self.appendCommentLine], ["COMMENT_SINGLE", "DEFCLASS_BODY", self.end_single_comment_re.search, self.appendCommentLine], ["COMMENT_SINGLE", "COMMENT_SINGLE", self.catchall, self.appendCommentLine], ["DEFCLASS", "COMMENT_DOUBLE", self.start_double_comment_re.search, self.appendCommentLine], ["COMMENT_DOUBLE", "DEFCLASS_BODY", self.end_double_comment_re.search, self.appendCommentLine], ["COMMENT_DOUBLE", "COMMENT_DOUBLE", self.catchall, self.appendCommentLine], # other lines ["DEFCLASS", "DEFCLASS", self.empty_re.search, self.appendDefclassLine], ["DEFCLASS", "DEFCLASS", self.defclass_re.search, self.resetCommentSearch], ["DEFCLASS", "DEFCLASS_MULTI", self.multiline_defclass_start_re.search, self.resetCommentSearch], ["DEFCLASS", "DEFCLASS_BODY", self.catchall, self.stopCommentSearch], ### DEFCLASS_BODY ["DEFCLASS_BODY", "DEFCLASS", self.defclass_re.search, self.startCommentSearch], ["DEFCLASS_BODY", "DEFCLASS_MULTI", self.multiline_defclass_start_re.search, self.startCommentSearch], ["DEFCLASS_BODY", "DEFCLASS_BODY", self.catchall, self.appendNormalLine], ### DEFCLASS_MULTI ["DEFCLASS_MULTI", "DEFCLASS", self.multiline_defclass_end_re.search, self.appendDefclassLine], ["DEFCLASS_MULTI", "DEFCLASS_MULTI", self.catchall, self.appendDefclassLine], ] self.fsm = FSM("FILEHEAD", transitions) self.outstream = sys.stdout self.output = [] self.comment = [] self.filehead = [] self.defclass = [] self.indent = "" def __closeComment(self): """Appends any open comment block and triggering block to the output.""" if options.autobrief: if len(self.comment) == 1 \ or (len(self.comment) > 2 and self.comment[1].strip() == ''): self.comment[0] = self.__docstringSummaryToBrief(self.comment[0]) if self.comment: block = self.makeCommentBlock() self.output.extend(block) if self.defclass: self.output.extend(self.defclass) def __docstringSummaryToBrief(self, line): """Adds \\brief to the docstrings summary line. A \\brief is prepended, provided no other doxygen command is at the start of the line. """ stripped = line.strip() if stripped and not stripped[0] in ('@', '\\'): return "\\brief " + line else: return line def __flushBuffer(self): """Flushes the current outputbuffer to the outstream.""" if self.output: try: if options.debug: print >>sys.stderr, "# OUTPUT: ", self.output print >>self.outstream, "\n".join(self.output) self.outstream.flush() except IOError: # Fix for FS#33. Catches "broken pipe" when doxygen closes # stdout prematurely upon usage of INPUT_FILTER, INLINE_SOURCES # and FILTER_SOURCE_FILES. pass self.output = [] def catchall(self, input): """The catchall-condition, always returns true.""" return True def resetCommentSearch(self, match): """Restarts a new comment search for a different triggering line. Closes the current commentblock and starts a new comment search. """ if options.debug: print >>sys.stderr, "# CALLBACK: resetCommentSearch" self.__closeComment() self.startCommentSearch(match) def startCommentSearch(self, match): """Starts a new comment search. Saves the triggering line, resets the current comment and saves the current indentation. """ if options.debug: print >>sys.stderr, "# CALLBACK: startCommentSearch" self.defclass = [self.fsm.current_input] self.comment = [] self.indent = match.group(1) def stopCommentSearch(self, match): """Stops a comment search. Closes the current commentblock, resets the triggering line and appends the current line to the output. """ if options.debug: print >>sys.stderr, "# CALLBACK: stopCommentSearch" self.__closeComment() self.defclass = [] self.output.append(self.fsm.current_input) def appendFileheadLine(self, match): """Appends a line in the FILEHEAD state. Closes the open comment block, resets it and appends the current line. """ if options.debug: print >>sys.stderr, "# CALLBACK: appendFileheadLine" self.__closeComment() self.comment = [] self.output.append(self.fsm.current_input) def appendCommentLine(self, match): """Appends a comment line. The comment delimiter is removed from multiline start and ends as well as singleline comments. """ if options.debug: print >>sys.stderr, "# CALLBACK: appendCommentLine" (from_state, to_state, condition, callback) = self.fsm.current_transition # single line comment if (from_state == "DEFCLASS" and to_state == "DEFCLASS_BODY") \ or (from_state == "FILEHEAD" and to_state == "FILEHEAD"): # remove comment delimiter from begin and end of the line activeCommentDelim = match.group(1) line = self.fsm.current_input self.comment.append(line[line.find(activeCommentDelim)+len(activeCommentDelim):line.rfind(activeCommentDelim)]) if (to_state == "DEFCLASS_BODY"): self.__closeComment() self.defclass = [] # multiline start elif from_state == "DEFCLASS" or from_state == "FILEHEAD": # remove comment delimiter from begin of the line activeCommentDelim = match.group(1) line = self.fsm.current_input self.comment.append(line[line.find(activeCommentDelim)+len(activeCommentDelim):]) # multiline end elif to_state == "DEFCLASS_BODY" or to_state == "FILEHEAD": # remove comment delimiter from end of the line activeCommentDelim = match.group(1) line = self.fsm.current_input self.comment.append(line[0:line.rfind(activeCommentDelim)]) if (to_state == "DEFCLASS_BODY"): self.__closeComment() self.defclass = [] # in multiline comment else: # just append the comment line self.comment.append(self.fsm.current_input) def appendNormalLine(self, match): """Appends a line to the output.""" if options.debug: print >>sys.stderr, "# CALLBACK: appendNormalLine" self.output.append(self.fsm.current_input) def appendDefclassLine(self, match): """Appends a line to the triggering block.""" if options.debug: print >>sys.stderr, "# CALLBACK: appendDefclassLine" self.defclass.append(self.fsm.current_input) def makeCommentBlock(self): """Indents the current comment block with respect to the current indentation level. @returns a list of indented comment lines """ doxyStart = "##" commentLines = self.comment commentLines = map(lambda x: "%s# %s" % (self.indent, x), commentLines) l = [self.indent + doxyStart] l.extend(commentLines) return l def parse(self, input): """Parses a python file given as input string and returns the doxygen- compatible representation. @param input the python code to parse @returns the modified python code """ lines = input.split("\n") for line in lines: self.fsm.makeTransition(line) if self.fsm.current_state == "DEFCLASS": self.__closeComment() return "\n".join(self.output) def parseFile(self, filename): """Parses a python file given as input string and returns the doxygen- compatible representation. @param input the python code to parse @returns the modified python code """ f = open(filename, 'r') for line in f: self.parseLine(line.rstrip('\r\n')) if self.fsm.current_state == "DEFCLASS": self.__closeComment() self.__flushBuffer() f.close() def parseLine(self, line): """Parse one line of python and flush the resulting output to the outstream. @param line the python code line to parse """ self.fsm.makeTransition(line) self.__flushBuffer() def optParse(): """Parses commandline options.""" parser = OptionParser(prog=__applicationName__, version="%prog " + __version__) parser.set_usage("%prog [options] filename") parser.add_option("--autobrief", action="store_true", dest="autobrief", help="use the docstring summary line as \\brief description" ) parser.add_option("--debug", action="store_true", dest="debug", help="enable debug output on stderr" ) ## parse options global options (options, filename) = parser.parse_args() if not filename: print >>sys.stderr, "No filename given." sys.exit(-1) return filename[0] def main(): """Starts the parser on the file given by the filename as the first argument on the commandline. """ filename = optParse() fsm = Doxypy() fsm.parseFile(filename) if __name__ == "__main__": main()
gpl-3.0
flavioamieiro/pypln.backend
tests/test_worker_palavras_semantic_tagger.py
2
3630
# coding: utf-8 # # Copyright 2012 NAMD-EMAP-FGV # # This file is part of PyPLN. You can get more information at: http://pypln.org/. # # PyPLN 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. # # PyPLN 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 PyPLN. If not, see <http://www.gnu.org/licenses/>. from textwrap import dedent from pypln.backend.workers import SemanticTagger from utils import TaskTest class TestSemanticTaggerWorker(TaskTest): def test_basic_semantic_tags(self): palavras_output = dedent(''' Eu\t[eu] <*> PERS M/F 1S NOM @SUBJ> #1->2 sei\t[saber] <fmc> <mv> V PR 1S IND VFIN @FS-STA #2->0 que\t[que] <clb> <clb-fs> KS @SUB #3->7 em\t[em] <sam-> PRP @PIV> #4->7 este\t[este] <-sam> <dem> DET M S @>N #5->6 momento\t[momento] <dur> <f-q> N M S @P< #6->4 falo\t[falar] <vH> <mv> V PR 1S IND VFIN @FS-<ACC #7->2 para\t[para] PRP @<ADVL #8->7 todo=o\t[todo=o] <quant> DET M S @>N #9->10 povo\t[povo] <HH> N M S @P< #10->8 de\t[de] <sam-> <np-close> PRP @N< #11->10 o\t[o] <-sam> <artd> DET M S @>N #12->13 Brasil\t[Brasil] <civ> <*> PROP M S @P< #13->11 $. #14->0 </s> ''').strip() + '\n\n' expected_tags = { 'Non_Tagged': ['Eu', 'sei', 'que', 'em', 'este', 'para', 'todo=o', 'de', 'o'], 'Collective': ['povo'], 'Time_Event': ['momento'], 'Place': ['Brasil'], 'Human': ['povo'], 'Verbs_related_human_things': ['falo'] } doc_id = self.collection.insert({'palavras_raw': palavras_output, 'palavras_raw_ran': True}, w=1) SemanticTagger().delay(doc_id) refreshed_document = self.collection.find_one({'_id': doc_id}) self.assertEqual(refreshed_document['semantic_tags'], expected_tags) def test_ambiguous_tags(self): palavras_output = dedent(''' Eu [eu] <*> PERS M/F 1S NOM @SUBJ> #1->2 canto [cantar] <vH> <fmc> <mv> V PR 1S IND VFIN @FS-STA #2->0 bem [bem] <quant> ADV @<ADVL #3->2 enquanto [enquanto] <clb> <clb-fs> <rel> <ks> ADV @ADVL> #4->6 ele [ele] PERS M 3S NOM @SUBJ> #5->6 está [estar] <mv> V PR 3S IND VFIN @FS-<ADVL #6->2 em [em] <sam-> PRP @<SA #7->6 o [o] <-sam> <artd> DET M S @>N #8->9 canto [canto] <Labs> <act-d> <sem-l> <sem-r> N M S @P< #9->7 $. #10->0 </s> ''').strip() + '\n\n' expected_tags = { 'Non_Tagged': [u'Eu', u'bem', u'enquanto', u'ele', u'está', u'em', u'o'], 'Place and spatial': [u'canto'], 'Verbs_related_human_things': [u'canto'] } doc_id = self.collection.insert({'palavras_raw': palavras_output, 'palavras_raw_ran': True}, w=1) SemanticTagger().delay(doc_id) refreshed_document = self.collection.find_one({'_id': doc_id}) self.assertEqual(refreshed_document['semantic_tags'], expected_tags)
gpl-3.0
abramhindle/UnnaturalCodeFork
python/testdata/launchpad/lib/lp/services/fields/__init__.py
1
30406
# Copyright 2009-2012 Canonical Ltd. This software is licensed under the # GNU Affero General Public License version 3 (see the file LICENSE). __metaclass__ = type __all__ = [ 'AnnouncementDate', 'FormattableDate', 'BaseImageUpload', 'BlacklistableContentNameField', 'BugField', 'ContentNameField', 'Description', 'Datetime', 'DuplicateBug', 'FieldNotBoundError', 'IAnnouncementDate', 'IBaseImageUpload', 'IBugField', 'IDescription', 'ILocationField', 'INoneableTextLine', 'IPersonChoice', 'IStrippedTextLine', 'ISummary', 'ITag', 'ITimeInterval', 'ITitle', 'IURIField', 'IWhiteboard', 'IconImageUpload', 'KEEP_SAME_IMAGE', 'LocationField', 'LogoImageUpload', 'MugshotImageUpload', 'NoneableDescription', 'NoneableTextLine', 'PersonChoice', 'PillarAliases', 'PillarNameField', 'PrivateMembershipTeamNotAllowed', 'PrivateTeamNotAllowed', 'ProductBugTracker', 'ProductNameField', 'PublicPersonChoice', 'SearchTag', 'StrippedTextLine', 'Summary', 'Tag', 'TimeInterval', 'Title', 'URIField', 'UniqueField', 'Whiteboard', 'WorkItemsText', 'is_public_person_or_closed_team', 'is_public_person', ] import re from StringIO import StringIO from textwrap import dedent from lazr.restful.fields import Reference from lazr.restful.interfaces import IReferenceChoice from lazr.uri import ( InvalidURIError, URI, ) from zope.component import getUtility from zope.interface import implements from zope.schema import ( Bool, Bytes, Choice, Date, Datetime, Field, Float, Int, Text, TextLine, Tuple, ) from zope.schema.interfaces import ( ConstraintNotSatisfied, IBytes, IDate, IDatetime, IField, Interface, IObject, IText, ITextLine, ) from zope.security.interfaces import ForbiddenAttribute from lp import _ from lp.app.validators import LaunchpadValidationError from lp.app.validators.name import ( name_validator, valid_name, ) from lp.blueprints.enums import SpecificationWorkItemStatus from lp.bugs.errors import InvalidDuplicateValue from lp.registry.enums import ( EXCLUSIVE_TEAM_POLICY, PersonVisibility, ) from lp.registry.interfaces.pillar import IPillarNameSet from lp.services.webapp.interfaces import ILaunchBag # Marker object to tell BaseImageUpload to keep the existing image. KEEP_SAME_IMAGE = object() # Regexp for detecting milestone headers in work items text. MILESTONE_RE = re.compile('^work items(.*)\s*:\s*$', re.I) # Regexp for work items. WORKITEM_RE = re.compile( '^(\[(?P<assignee>.*?)\])?\s*(?P<title>.*)\s*:\s*(?P<status>.*)\s*$', re.I) # Field Interfaces class IStrippedTextLine(ITextLine): """A field with leading and trailing whitespaces stripped.""" class ITitle(IStrippedTextLine): """A Field that implements a launchpad Title""" class INoneableTextLine(IStrippedTextLine): """A field that is None if it's value is empty or whitespace.""" class ISummary(IText): """A Field that implements a Summary""" class IDescription(IText): """A Field that implements a Description""" class INoneableDescription(IDescription): """A field that is None if it's value is empty or whitespace.""" class IWhiteboard(IText): """A Field that implements a Whiteboard""" class ITimeInterval(ITextLine): """A field that captures a time interval in days, hours, minutes.""" class IBugField(IObject): """A field that allows entry of a Bug number or nickname""" class IAnnouncementDate(IDatetime): """Marker interface for AnnouncementDate fields. This is used in cases where we either want to publish something immediately, or come back in future to publish it, or set a date for publication in advance. Essentially this amounts to a Datetime that can be None. """ class ILocationField(IField): """A location, consisting of geographic coordinates and a time zone.""" latitude = Float(title=_('Latitude')) longitude = Float(title=_('Longitude')) time_zone = Choice(title=_('Time zone'), vocabulary='TimezoneName') class ITag(ITextLine): """A tag. A text line which can be used as a simple text tag. """ class IURIField(ITextLine): """A URI. A text line that holds a URI. """ trailing_slash = Bool( title=_('Whether a trailing slash is required for this field'), required=False, description=_('If set to True, then the path component of the URI ' 'will be automatically normalized to end in a slash. ' 'If set to False, any trailing slash will be ' 'automatically removed. If set to None, URIs will ' 'not be normalized.')) def normalize(input): """Normalize a URI. * whitespace is stripped from the input value * if the field requires (or forbids) a trailing slash on the URI, ensures that the widget ends in a slash (or doesn't end in a slash). * the URI is canonicalized. """ class IBaseImageUpload(IBytes): """Marker interface for ImageUpload fields.""" dimensions = Tuple( title=_('Maximum dimensions'), description=_('A two-tuple with the maximum width and height (in ' 'pixels) of this image.')) max_size = Int( title=_('Maximum size'), description=_('The maximum size (in bytes) of this image.')) default_image_resource = TextLine( title=_('The default image'), description=_( 'The URL of the zope3 resource of the default image that should ' 'be used. Something of the form /@@/team-mugshot')) def getCurrentImage(): """Return the value of the field for the object bound to it. Raise FieldNotBoundError if the field is not bound to any object. """ class StrippedTextLine(TextLine): implements(IStrippedTextLine) def set(self, object, value): """Strip the value and pass up.""" if value is not None: value = value.strip() super(StrippedTextLine, self).set(object, value) class NoneableTextLine(StrippedTextLine): implements(INoneableTextLine) # Title # A field to capture a launchpad object title class Title(StrippedTextLine): implements(ITitle) class StrippableText(Text): """A text that can be configured to strip when setting.""" def __init__(self, strip_text=False, trailing_only=False, **kwargs): super(StrippableText, self).__init__(**kwargs) self.strip_text = strip_text self.trailing_only = trailing_only def normalize(self, value): """Strip the leading and trailing whitespace.""" if self.strip_text and value is not None: if self.trailing_only: value = value.rstrip() else: value = value.strip() return value def set(self, object, value): """Strip the value and pass up.""" value = self.normalize(value) super(StrippableText, self).set(object, value) def validate(self, value): """See `IField`.""" value = self.normalize(value) return super(StrippableText, self).validate(value) # Summary # A field capture a Launchpad object summary class Summary(StrippableText): implements(ISummary) # Description # A field capture a Launchpad object description class Description(StrippableText): implements(IDescription) class NoneableDescription(Description): implements(INoneableDescription) # Whiteboard # A field capture a Launchpad object whiteboard class Whiteboard(StrippableText): implements(IWhiteboard) class FormattableDate(Date): """A datetime field that checks for compatibility with Python's strformat. From the user's perspective this is a date entry field; it converts to and from datetime because that's what the db is expecting. """ implements(IDate) def _validate(self, value): error_msg = ("Date could not be formatted. Provide a date formatted " "like YYYY-MM-DD format. The year must be after 1900.") super(FormattableDate, self)._validate(value) # The only thing of interest here is whether or the input can be # formatted properly, not whether it makes sense otherwise. # As a minimal sanity check, just raise an error if it fails. try: value.strftime('%Y') except ValueError: raise LaunchpadValidationError(error_msg) class AnnouncementDate(Datetime): implements(IDatetime) # TimeInterval # A field to capture an interval in time, such as X days, Y hours, Z # minutes. class TimeInterval(TextLine): implements(ITimeInterval) def _validate(self, value): if 'mon' in value: return 0 return 1 class BugField(Reference): implements(IBugField) def __init__(self, *args, **kwargs): """The schema will always be `IBug`.""" super(BugField, self).__init__(Interface, *args, **kwargs) def _get_schema(self): """Get the schema here to avoid circular imports.""" from lp.bugs.interfaces.bug import IBug return IBug def _set_schema(self, schema): """Ignore attempts to set the schema by the superclass.""" schema = property(_get_schema, _set_schema) # XXX: Tim Penhey 2011-01-21 bug 706099 # Should have bug specific fields in lp.services.fields class DuplicateBug(BugField): """A bug that the context is a duplicate of.""" def _validate(self, value): """Prevent dups of dups. Returns True if the dup target is not a duplicate /and/ if the current bug doesn't have any duplicates referencing it /and/ if the bug isn't a duplicate of itself, otherwise return False. """ current_bug = self.context dup_target = value if current_bug == dup_target: raise InvalidDuplicateValue(_(dedent(""" You can't mark a bug as a duplicate of itself."""))) elif dup_target.duplicateof is not None: raise InvalidDuplicateValue(_(dedent(""" Bug ${dup} is already a duplicate of bug ${orig}. You can only mark a bug report as duplicate of one that isn't a duplicate itself. """), mapping={'dup': dup_target.id, 'orig': dup_target.duplicateof.id})) else: return True class Tag(TextLine): implements(ITag) def constraint(self, value): """Make sure that the value is a valid name.""" super_constraint = TextLine.constraint(self, value) return super_constraint and valid_name(value) class SearchTag(Tag): def constraint(self, value): """Make sure the value is a valid search tag. A valid search tag is a valid name or a valid name prepended with a minus, denoting "not this tag". A simple wildcard - an asterisk - is also valid, with or without a leading minus. """ if value in ('*', '-*'): return True elif value.startswith('-'): return super(SearchTag, self).constraint(value[1:]) else: return super(SearchTag, self).constraint(value) class UniqueField(TextLine): """Base class for fields that are used for unique attributes.""" errormessage = _("%s is already taken") attribute = None @property def _content_iface(self): """Return the content interface. Override this in subclasses. """ return None def _getByAttribute(self, input): """Return the content object with the given attribute. Override this in subclasses. """ raise NotImplementedError def _isValueTaken(self, value): """Returns true if and only if the specified value is already taken. """ return self._getByAttribute(value) is not None def unchanged(self, input): """Return True if the attribute on the object is unchanged.""" _marker = object() if (self._content_iface.providedBy(self.context) and input == getattr(self.context, self.attribute, _marker)): return True return False def _validate(self, input): """Raise a LaunchpadValidationError if the attribute is not available. A attribute is not available if it's already in use by another object of this same context. The 'input' should be valid as per TextLine. """ super(UniqueField, self)._validate(input) assert self._content_iface is not None if self.unchanged(input): # The value is not being changed, thus it already existed, so we # know it is unique. return # Now we know we are dealing with either a new object, or an # object whose attribute is going to be updated. We need to # ensure the new value is unique. if self._isValueTaken(input): raise LaunchpadValidationError(self.errormessage % input) class ContentNameField(UniqueField): """Base class for fields that are used by unique 'name' attributes.""" attribute = 'name' def _getByAttribute(self, input): """Return the content object with the given attribute.""" return self._getByName(input) def _getByName(self, input): """Return the content object with the given name. Override this in subclasses. """ raise NotImplementedError def _validate(self, name): """Check that the given name is valid (and by delegation, unique).""" name_validator(name) UniqueField._validate(self, name) class BlacklistableContentNameField(ContentNameField): """ContentNameField that also checks that a name is not blacklisted""" blacklistmessage = _("The name '%s' has been blocked by the Launchpad " "administrators. Contact Launchpad Support if you " "want to use this name.") def _validate(self, input): """Check that the given name is valid, unique and not blacklisted.""" super(BlacklistableContentNameField, self)._validate(input) # Although this check is performed in UniqueField._validate(), we need # to do it here again to avoid checking whether or not the name is # blacklisted when it hasn't been changed. if self.unchanged(input): # The attribute wasn't changed. return # Need a local import because of circular dependencies. from lp.registry.interfaces.person import IPersonSet user = getUtility(ILaunchBag).user if getUtility(IPersonSet).isNameBlacklisted(input, user): raise LaunchpadValidationError(self.blacklistmessage % input) class PillarAliases(TextLine): """A field which takes a list of space-separated aliases for a pillar.""" def _split_input(self, input): if input is None: return [] return re.sub(r'\s+', ' ', input).split() def _validate(self, input): """Make sure all the aliases are valid for the field's pillar. An alias is valid if it can be used as the name of a pillar and is not identical to the pillar's existing name. """ context = self.context from lp.registry.interfaces.product import IProduct from lp.registry.interfaces.projectgroup import IProjectGroup from lp.registry.interfaces.distribution import IDistribution if IProduct.providedBy(context): name_field = IProduct['name'] elif IProjectGroup.providedBy(context): name_field = IProjectGroup['name'] elif IDistribution.providedBy(context): name_field = IDistribution['name'] else: raise AssertionError("Unexpected context type.") name_field.bind(context) existing_aliases = context.aliases for name in self._split_input(input): if name == context.name: raise LaunchpadValidationError('This is your name: %s' % name) elif name in existing_aliases: # This is already an alias to this pillar, so there's no need # to validate it. pass else: name_field._validate(name) def set(self, object, value): object.setAliases(self._split_input(value)) def get(self, object): return " ".join(object.aliases) class ProductBugTracker(Choice): """A bug tracker used by a Product. It accepts all the values in the vocabulary, as well as a special marker object, which represents the Malone bug tracker. This field uses two attributes on the Product to model its state: 'official_malone' and 'bugtracker' """ implements(IReferenceChoice) malone_marker = object() @property def schema(self): # The IBugTracker needs to be imported here to avoid an import loop. from lp.bugs.interfaces.bugtracker import IBugTracker return IBugTracker def get(self, ob): if ob.official_malone: return self.malone_marker else: return getattr(ob, self.__name__) def set(self, ob, value): if self.readonly: raise TypeError("Can't set values on read-only fields.") if value is self.malone_marker: ob.official_malone = True setattr(ob, self.__name__, None) else: ob.official_malone = False setattr(ob, self.__name__, value) class URIField(TextLine): implements(IURIField) def __init__(self, allowed_schemes=(), allow_userinfo=True, allow_port=True, allow_query=True, allow_fragment=True, trailing_slash=None, **kwargs): super(URIField, self).__init__(**kwargs) self.allowed_schemes = set(allowed_schemes) self.allow_userinfo = allow_userinfo self.allow_port = allow_port self.allow_query = allow_query self.allow_fragment = allow_fragment self.trailing_slash = trailing_slash def set(self, object, value): """Canonicalize a URL and set it as a field value.""" value = self.normalize(value) super(URIField, self).set(object, value) def normalize(self, input): """See `IURIField`.""" if input is None: return input try: uri = URI(input) except InvalidURIError as exc: raise LaunchpadValidationError(str(exc)) # If there is a policy for whether trailing slashes are # allowed at the end of the path segment, ensure that the # URI conforms. if self.trailing_slash is not None: if self.trailing_slash: uri = uri.ensureSlash() else: uri = uri.ensureNoSlash() input = unicode(uri) return input def _validate(self, value): """Ensure the value is a valid URI.""" uri = URI(self.normalize(value)) if self.allowed_schemes and uri.scheme not in self.allowed_schemes: raise LaunchpadValidationError( 'The URI scheme "%s" is not allowed. Only URIs with ' 'the following schemes may be used: %s' % (uri.scheme, ', '.join(sorted(self.allowed_schemes)))) if not self.allow_userinfo and uri.userinfo is not None: raise LaunchpadValidationError( 'A username may not be specified in the URI.') if not self.allow_port and uri.port is not None: raise LaunchpadValidationError( 'Non-default ports are not allowed.') if not self.allow_query and uri.query is not None: raise LaunchpadValidationError( 'URIs with query strings are not allowed.') if not self.allow_fragment and uri.fragment is not None: raise LaunchpadValidationError( 'URIs with fragment identifiers are not allowed.') super(URIField, self)._validate(value) class FieldNotBoundError(Exception): """The field is not bound to any object.""" class BaseImageUpload(Bytes): """Base class for ImageUpload fields. Any subclass of this one must be used in conjunction with ImageUploadWidget and must define the following attributes: - dimensions: the exact dimensions of the image; a tuple of the form (width, height). - max_size: the maximum size of the image, in bytes. """ implements(IBaseImageUpload) exact_dimensions = True dimensions = () max_size = 0 def __init__(self, default_image_resource=None, **kw): # 'default_image_resource' is a keyword argument so that the # class constructor can be used in the same way as other # Interface attribute specifiers. if default_image_resource is None: raise AssertionError( "You must specify a default image resource.") self.default_image_resource = default_image_resource Bytes.__init__(self, **kw) def getCurrentImage(self): if self.context is None: raise FieldNotBoundError("This field must be bound to an object.") else: try: current = getattr(self.context, self.__name__) except ForbiddenAttribute: # When this field is used in add forms it gets bound to # I*Set objects, which don't have the attribute represented # by the field, so we need this hack here. current = None return current def _valid_image(self, image): """Check that the given image is under the given constraints.""" # No global import to avoid hard dependency on PIL being installed import PIL.Image if len(image) > self.max_size: raise LaunchpadValidationError(_(dedent(""" This image exceeds the maximum allowed size in bytes."""))) try: pil_image = PIL.Image.open(StringIO(image)) except (IOError, ValueError): raise LaunchpadValidationError(_(dedent(""" The file uploaded was not recognized as an image; please check it and retry."""))) width, height = pil_image.size required_width, required_height = self.dimensions if self.exact_dimensions: if width != required_width or height != required_height: raise LaunchpadValidationError(_(dedent(""" This image is not exactly ${width}x${height} pixels in size."""), mapping={'width': required_width, 'height': required_height})) else: if width > required_width or height > required_height: raise LaunchpadValidationError(_(dedent(""" This image is larger than ${width}x${height} pixels in size."""), mapping={'width': required_width, 'height': required_height})) return True def _validate(self, value): if hasattr(value, 'seek'): value.seek(0) content = value.read() else: content = value super(BaseImageUpload, self)._validate(content) self._valid_image(content) def set(self, object, value): if value is not KEEP_SAME_IMAGE: Bytes.set(self, object, value) class IconImageUpload(BaseImageUpload): dimensions = (14, 14) max_size = 5 * 1024 class LogoImageUpload(BaseImageUpload): dimensions = (64, 64) max_size = 50 * 1024 class MugshotImageUpload(BaseImageUpload): dimensions = (192, 192) max_size = 100 * 1024 class LocationField(Field): """A Location field.""" implements(ILocationField) @property def latitude(self): return self.value.latitude @property def longitude(self): return self.value.longitude @property def time_zone(self): return self.value.time_zone class PillarNameField(BlacklistableContentNameField): """Base field used for names of distros/projects/products.""" errormessage = _("%s is already used by another project") def _getByName(self, name): return getUtility(IPillarNameSet).getByName(name) class ProductNameField(PillarNameField): """Field used by IProduct.name.""" @property def _content_iface(self): # Local import to avoid circular dependencies. from lp.registry.interfaces.product import IProduct return IProduct def is_public_person(person): """Return True if the person is public.""" from lp.registry.interfaces.person import IPerson if not IPerson.providedBy(person): return False return person.visibility == PersonVisibility.PUBLIC def is_public_person_or_closed_team(person): """Return True if person is a Person or not an open or delegated team.""" from lp.registry.interfaces.person import IPerson if not IPerson.providedBy(person): return False if not person.is_team: return person.visibility == PersonVisibility.PUBLIC return person.membership_policy in EXCLUSIVE_TEAM_POLICY class PrivateTeamNotAllowed(ConstraintNotSatisfied): __doc__ = _("A private team is not allowed.") class PrivateMembershipTeamNotAllowed(ConstraintNotSatisfied): __doc__ = _("A private-membership team is not allowed.") class IPersonChoice(IReferenceChoice): """A marker for a choice among people.""" class PersonChoice(Choice): """A person or team. This is useful as a superclass and provides a clearer error message than "Constraint not satisfied". """ implements(IPersonChoice) schema = IObject # Will be set to IPerson once IPerson is defined. class PublicPersonChoice(PersonChoice): """A person or team who is public.""" def constraint(self, value): if is_public_person(value): return True else: # The vocabulary prevents the revealing of private team names. raise PrivateTeamNotAllowed(value) class WorkItemsText(Text): def parseLine(self, line): workitem_match = WORKITEM_RE.search(line) if workitem_match: assignee = workitem_match.group('assignee') title = workitem_match.group('title') status = workitem_match.group('status') else: raise LaunchpadValidationError( 'Invalid work item format: "%s"' % line) if title == '': raise LaunchpadValidationError( 'No work item title found on "%s"' % line) if title.startswith('['): raise LaunchpadValidationError( 'Missing closing "]" for assignee on "%s".' % line) return {'title': title, 'status': status.strip().upper(), 'assignee': assignee} def parse(self, text): sequence = 0 milestone = None work_items = [] if text is not None: for line in text.splitlines(): if line.strip() == '': continue milestone_match = MILESTONE_RE.search(line) if milestone_match: milestone_part = milestone_match.group(1).strip() if milestone_part == '': milestone = None else: milestone = milestone_part.split()[-1] else: new_work_item = self.parseLine(line) new_work_item['milestone'] = milestone new_work_item['sequence'] = sequence sequence += 1 work_items.append(new_work_item) return work_items def validate(self, value): self.parseAndValidate(value) def parseAndValidate(self, text): work_items = self.parse(text) for work_item in work_items: work_item['status'] = self.getStatus(work_item['status']) work_item['assignee'] = self.getAssignee(work_item['assignee']) work_item['milestone'] = self.getMilestone(work_item['milestone']) return work_items def getStatus(self, text): valid_statuses = SpecificationWorkItemStatus.items if text.lower() not in [item.name.lower() for item in valid_statuses]: raise LaunchpadValidationError('Unknown status: %s' % text) return valid_statuses[text.upper()] def getAssignee(self, assignee_name): if assignee_name is None: return None from lp.registry.interfaces.person import IPersonSet assignee = getUtility(IPersonSet).getByName(assignee_name) if assignee is None: raise LaunchpadValidationError( "Unknown person name: %s" % assignee_name) return assignee def getMilestone(self, milestone_name): if milestone_name is None: return None target = self.context.target milestone = None from lp.registry.interfaces.distribution import IDistribution from lp.registry.interfaces.milestone import IMilestoneSet from lp.registry.interfaces.product import IProduct if IProduct.providedBy(target): milestone = getUtility(IMilestoneSet).getByNameAndProduct( milestone_name, target) elif IDistribution.providedBy(target): milestone = getUtility(IMilestoneSet).getByNameAndDistribution( milestone_name, target) else: raise AssertionError("Unexpected target type.") if milestone is None: raise LaunchpadValidationError("The milestone '%s' is not valid " "for the target '%s'." % \ (milestone_name, target.name)) return milestone
agpl-3.0
scivey/goosepp
external/gmock-1.7.0/gtest/xcode/Scripts/versiongenerate.py
3088
4536
#!/usr/bin/env python # # Copyright 2008, 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. """A script to prepare version informtion for use the gtest Info.plist file. This script extracts the version information from the configure.ac file and uses it to generate a header file containing the same information. The #defines in this header file will be included in during the generation of the Info.plist of the framework, giving the correct value to the version shown in the Finder. This script makes the following assumptions (these are faults of the script, not problems with the Autoconf): 1. The AC_INIT macro will be contained within the first 1024 characters of configure.ac 2. The version string will be 3 integers separated by periods and will be surrounded by squre brackets, "[" and "]" (e.g. [1.0.1]). The first segment represents the major version, the second represents the minor version and the third represents the fix version. 3. No ")" character exists between the opening "(" and closing ")" of AC_INIT, including in comments and character strings. """ import sys import re # Read the command line argument (the output directory for Version.h) if (len(sys.argv) < 3): print "Usage: versiongenerate.py input_dir output_dir" sys.exit(1) else: input_dir = sys.argv[1] output_dir = sys.argv[2] # Read the first 1024 characters of the configure.ac file config_file = open("%s/configure.ac" % input_dir, 'r') buffer_size = 1024 opening_string = config_file.read(buffer_size) config_file.close() # Extract the version string from the AC_INIT macro # The following init_expression means: # Extract three integers separated by periods and surrounded by squre # brackets(e.g. "[1.0.1]") between "AC_INIT(" and ")". Do not be greedy # (*? is the non-greedy flag) since that would pull in everything between # the first "(" and the last ")" in the file. version_expression = re.compile(r"AC_INIT\(.*?\[(\d+)\.(\d+)\.(\d+)\].*?\)", re.DOTALL) version_values = version_expression.search(opening_string) major_version = version_values.group(1) minor_version = version_values.group(2) fix_version = version_values.group(3) # Write the version information to a header file to be included in the # Info.plist file. file_data = """// // DO NOT MODIFY THIS FILE (but you can delete it) // // This file is autogenerated by the versiongenerate.py script. This script // is executed in a "Run Script" build phase when creating gtest.framework. This // header file is not used during compilation of C-source. Rather, it simply // defines some version strings for substitution in the Info.plist. Because of // this, we are not not restricted to C-syntax nor are we using include guards. // #define GTEST_VERSIONINFO_SHORT %s.%s #define GTEST_VERSIONINFO_LONG %s.%s.%s """ % (major_version, minor_version, major_version, minor_version, fix_version) version_file = open("%s/Version.h" % output_dir, 'w') version_file.write(file_data) version_file.close()
mit
btheosam/TizenRT
external/gmock/test/gmock_output_test.py
986
5999
#!/usr/bin/env python # # Copyright 2008, 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. """Tests the text output of Google C++ Mocking Framework. SYNOPSIS gmock_output_test.py --build_dir=BUILD/DIR --gengolden # where BUILD/DIR contains the built gmock_output_test_ file. gmock_output_test.py --gengolden gmock_output_test.py """ __author__ = '[email protected] (Zhanyong Wan)' import os import re import sys import gmock_test_utils # The flag for generating the golden file GENGOLDEN_FLAG = '--gengolden' PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_output_test_') COMMAND = [PROGRAM_PATH, '--gtest_stack_trace_depth=0', '--gtest_print_time=0'] GOLDEN_NAME = 'gmock_output_test_golden.txt' GOLDEN_PATH = os.path.join(gmock_test_utils.GetSourceDir(), GOLDEN_NAME) def ToUnixLineEnding(s): """Changes all Windows/Mac line endings in s to UNIX line endings.""" return s.replace('\r\n', '\n').replace('\r', '\n') def RemoveReportHeaderAndFooter(output): """Removes Google Test result report's header and footer from the output.""" output = re.sub(r'.*gtest_main.*\n', '', output) output = re.sub(r'\[.*\d+ tests.*\n', '', output) output = re.sub(r'\[.* test environment .*\n', '', output) output = re.sub(r'\[=+\] \d+ tests .* ran.*', '', output) output = re.sub(r'.* FAILED TESTS\n', '', output) return output def RemoveLocations(output): """Removes all file location info from a Google Test program's output. Args: output: the output of a Google Test program. Returns: output with all file location info (in the form of 'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or 'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by 'FILE:#: '. """ return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\:', 'FILE:#:', output) def NormalizeErrorMarker(output): """Normalizes the error marker, which is different on Windows vs on Linux.""" return re.sub(r' error: ', ' Failure\n', output) def RemoveMemoryAddresses(output): """Removes memory addresses from the test output.""" return re.sub(r'@\w+', '@0x#', output) def RemoveTestNamesOfLeakedMocks(output): """Removes the test names of leaked mock objects from the test output.""" return re.sub(r'\(used in test .+\) ', '', output) def GetLeakyTests(output): """Returns a list of test names that leak mock objects.""" # findall() returns a list of all matches of the regex in output. # For example, if '(used in test FooTest.Bar)' is in output, the # list will contain 'FooTest.Bar'. return re.findall(r'\(used in test (.+)\)', output) def GetNormalizedOutputAndLeakyTests(output): """Normalizes the output of gmock_output_test_. Args: output: The test output. Returns: A tuple (the normalized test output, the list of test names that have leaked mocks). """ output = ToUnixLineEnding(output) output = RemoveReportHeaderAndFooter(output) output = NormalizeErrorMarker(output) output = RemoveLocations(output) output = RemoveMemoryAddresses(output) return (RemoveTestNamesOfLeakedMocks(output), GetLeakyTests(output)) def GetShellCommandOutput(cmd): """Runs a command in a sub-process, and returns its STDOUT in a string.""" return gmock_test_utils.Subprocess(cmd, capture_stderr=False).output def GetNormalizedCommandOutputAndLeakyTests(cmd): """Runs a command and returns its normalized output and a list of leaky tests. Args: cmd: the shell command. """ # Disables exception pop-ups on Windows. os.environ['GTEST_CATCH_EXCEPTIONS'] = '1' return GetNormalizedOutputAndLeakyTests(GetShellCommandOutput(cmd)) class GMockOutputTest(gmock_test_utils.TestCase): def testOutput(self): (output, leaky_tests) = GetNormalizedCommandOutputAndLeakyTests(COMMAND) golden_file = open(GOLDEN_PATH, 'rb') golden = golden_file.read() golden_file.close() # The normalized output should match the golden file. self.assertEquals(golden, output) # The raw output should contain 2 leaked mock object errors for # test GMockOutputTest.CatchesLeakedMocks. self.assertEquals(['GMockOutputTest.CatchesLeakedMocks', 'GMockOutputTest.CatchesLeakedMocks'], leaky_tests) if __name__ == '__main__': if sys.argv[1:] == [GENGOLDEN_FLAG]: (output, _) = GetNormalizedCommandOutputAndLeakyTests(COMMAND) golden_file = open(GOLDEN_PATH, 'wb') golden_file.write(output) golden_file.close() else: gmock_test_utils.Main()
apache-2.0
jmr2704/betakore-alien
src/scons-local-2.0.1/SCons/Platform/win32.py
50
13779
"""SCons.Platform.win32 Platform-specific initialization for Win32 systems. There normally shouldn't be any need to import this module directly. It will usually be imported through the generic SCons.Platform.Platform() selection method. """ # # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # __revision__ = "src/engine/SCons/Platform/win32.py 5134 2010/08/16 23:02:40 bdeegan" import os import os.path import sys import tempfile from SCons.Platform.posix import exitvalmap from SCons.Platform import TempFileMunge import SCons.Util try: import msvcrt import win32api import win32con msvcrt.get_osfhandle win32api.SetHandleInformation win32con.HANDLE_FLAG_INHERIT except ImportError: parallel_msg = \ "you do not seem to have the pywin32 extensions installed;\n" + \ "\tparallel (-j) builds may not work reliably with open Python files." except AttributeError: parallel_msg = \ "your pywin32 extensions do not support file handle operations;\n" + \ "\tparallel (-j) builds may not work reliably with open Python files." else: parallel_msg = None import builtins _builtin_file = builtins.file _builtin_open = builtins.open def _scons_file(*args, **kw): fp = _builtin_file(*args, **kw) win32api.SetHandleInformation(msvcrt.get_osfhandle(fp.fileno()), win32con.HANDLE_FLAG_INHERIT, 0) return fp def _scons_open(*args, **kw): fp = _builtin_open(*args, **kw) win32api.SetHandleInformation(msvcrt.get_osfhandle(fp.fileno()), win32con.HANDLE_FLAG_INHERIT, 0) return fp builtins.file = _scons_file builtins.open = _scons_open # The upshot of all this is that, if you are using Python 1.5.2, # you had better have cmd or command.com in your PATH when you run # scons. def piped_spawn(sh, escape, cmd, args, env, stdout, stderr): # There is no direct way to do that in python. What we do # here should work for most cases: # In case stdout (stderr) is not redirected to a file, # we redirect it into a temporary file tmpFileStdout # (tmpFileStderr) and copy the contents of this file # to stdout (stderr) given in the argument if not sh: sys.stderr.write("scons: Could not find command interpreter, is it in your PATH?\n") return 127 else: # one temporary file for stdout and stderr tmpFileStdout = os.path.normpath(tempfile.mktemp()) tmpFileStderr = os.path.normpath(tempfile.mktemp()) # check if output is redirected stdoutRedirected = 0 stderrRedirected = 0 for arg in args: # are there more possibilities to redirect stdout ? if (arg.find( ">", 0, 1 ) != -1 or arg.find( "1>", 0, 2 ) != -1): stdoutRedirected = 1 # are there more possibilities to redirect stderr ? if arg.find( "2>", 0, 2 ) != -1: stderrRedirected = 1 # redirect output of non-redirected streams to our tempfiles if stdoutRedirected == 0: args.append(">" + str(tmpFileStdout)) if stderrRedirected == 0: args.append("2>" + str(tmpFileStderr)) # actually do the spawn try: args = [sh, '/C', escape(' '.join(args)) ] ret = os.spawnve(os.P_WAIT, sh, args, env) except OSError, e: # catch any error try: ret = exitvalmap[e[0]] except KeyError: sys.stderr.write("scons: unknown OSError exception code %d - %s: %s\n" % (e[0], cmd, e[1])) if stderr is not None: stderr.write("scons: %s: %s\n" % (cmd, e[1])) # copy child output from tempfiles to our streams # and do clean up stuff if stdout is not None and stdoutRedirected == 0: try: stdout.write(open( tmpFileStdout, "r" ).read()) os.remove( tmpFileStdout ) except (IOError, OSError): pass if stderr is not None and stderrRedirected == 0: try: stderr.write(open( tmpFileStderr, "r" ).read()) os.remove( tmpFileStderr ) except (IOError, OSError): pass return ret def exec_spawn(l, env): try: result = os.spawnve(os.P_WAIT, l[0], l, env) except OSError, e: try: result = exitvalmap[e[0]] sys.stderr.write("scons: %s: %s\n" % (l[0], e[1])) except KeyError: result = 127 if len(l) > 2: if len(l[2]) < 1000: command = ' '.join(l[0:3]) else: command = l[0] else: command = l[0] sys.stderr.write("scons: unknown OSError exception code %d - '%s': %s\n" % (e[0], command, e[1])) return result def spawn(sh, escape, cmd, args, env): if not sh: sys.stderr.write("scons: Could not find command interpreter, is it in your PATH?\n") return 127 return exec_spawn([sh, '/C', escape(' '.join(args))], env) # Windows does not allow special characters in file names anyway, so no # need for a complex escape function, we will just quote the arg, except # that "cmd /c" requires that if an argument ends with a backslash it # needs to be escaped so as not to interfere with closing double quote # that we add. def escape(x): if x[-1] == '\\': x = x + '\\' return '"' + x + '"' # Get the windows system directory name _system_root = None def get_system_root(): global _system_root if _system_root is not None: return _system_root # A resonable default if we can't read the registry val = os.environ.get('SystemRoot', "C:\\WINDOWS") if SCons.Util.can_read_reg: try: # Look for Windows NT system root k=SCons.Util.RegOpenKeyEx(SCons.Util.hkey_mod.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Windows NT\\CurrentVersion') val, tok = SCons.Util.RegQueryValueEx(k, 'SystemRoot') except SCons.Util.RegError: try: # Okay, try the Windows 9x system root k=SCons.Util.RegOpenKeyEx(SCons.Util.hkey_mod.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Windows\\CurrentVersion') val, tok = SCons.Util.RegQueryValueEx(k, 'SystemRoot') except KeyboardInterrupt: raise except: pass _system_root = val return val # Get the location of the program files directory def get_program_files_dir(): # Now see if we can look in the registry... val = '' if SCons.Util.can_read_reg: try: # Look for Windows Program Files directory k=SCons.Util.RegOpenKeyEx(SCons.Util.hkey_mod.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Windows\\CurrentVersion') val, tok = SCons.Util.RegQueryValueEx(k, 'ProgramFilesDir') except SCons.Util.RegError: val = '' pass if val == '': # A reasonable default if we can't read the registry # (Actually, it's pretty reasonable even if we can :-) val = os.path.join(os.path.dirname(get_system_root()),"Program Files") return val # Determine which windows CPU were running on. class ArchDefinition(object): """ A class for defining architecture-specific settings and logic. """ def __init__(self, arch, synonyms=[]): self.arch = arch self.synonyms = synonyms SupportedArchitectureList = [ ArchDefinition( 'x86', ['i386', 'i486', 'i586', 'i686'], ), ArchDefinition( 'x86_64', ['AMD64', 'amd64', 'em64t', 'EM64T', 'x86_64'], ), ArchDefinition( 'ia64', ['IA64'], ), ] SupportedArchitectureMap = {} for a in SupportedArchitectureList: SupportedArchitectureMap[a.arch] = a for s in a.synonyms: SupportedArchitectureMap[s] = a def get_architecture(arch=None): """Returns the definition for the specified architecture string. If no string is specified, the system default is returned (as defined by the PROCESSOR_ARCHITEW6432 or PROCESSOR_ARCHITECTURE environment variables). """ if arch is None: arch = os.environ.get('PROCESSOR_ARCHITEW6432') if not arch: arch = os.environ.get('PROCESSOR_ARCHITECTURE') return SupportedArchitectureMap.get(arch, ArchDefinition('', [''])) def generate(env): # Attempt to find cmd.exe (for WinNT/2k/XP) or # command.com for Win9x cmd_interp = '' # First see if we can look in the registry... if SCons.Util.can_read_reg: try: # Look for Windows NT system root k=SCons.Util.RegOpenKeyEx(SCons.Util.hkey_mod.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Windows NT\\CurrentVersion') val, tok = SCons.Util.RegQueryValueEx(k, 'SystemRoot') cmd_interp = os.path.join(val, 'System32\\cmd.exe') except SCons.Util.RegError: try: # Okay, try the Windows 9x system root k=SCons.Util.RegOpenKeyEx(SCons.Util.hkey_mod.HKEY_LOCAL_MACHINE, 'Software\\Microsoft\\Windows\\CurrentVersion') val, tok = SCons.Util.RegQueryValueEx(k, 'SystemRoot') cmd_interp = os.path.join(val, 'command.com') except KeyboardInterrupt: raise except: pass # For the special case of not having access to the registry, we # use a temporary path and pathext to attempt to find the command # interpreter. If we fail, we try to find the interpreter through # the env's PATH. The problem with that is that it might not # contain an ENV and a PATH. if not cmd_interp: systemroot = get_system_root() tmp_path = systemroot + os.pathsep + \ os.path.join(systemroot,'System32') tmp_pathext = '.com;.exe;.bat;.cmd' if 'PATHEXT' in os.environ: tmp_pathext = os.environ['PATHEXT'] cmd_interp = SCons.Util.WhereIs('cmd', tmp_path, tmp_pathext) if not cmd_interp: cmd_interp = SCons.Util.WhereIs('command', tmp_path, tmp_pathext) if not cmd_interp: cmd_interp = env.Detect('cmd') if not cmd_interp: cmd_interp = env.Detect('command') if 'ENV' not in env: env['ENV'] = {} # Import things from the external environment to the construction # environment's ENV. This is a potential slippery slope, because we # *don't* want to make builds dependent on the user's environment by # default. We're doing this for SystemRoot, though, because it's # needed for anything that uses sockets, and seldom changes, and # for SystemDrive because it's related. # # Weigh the impact carefully before adding other variables to this list. import_env = [ 'SystemDrive', 'SystemRoot', 'TEMP', 'TMP' ] for var in import_env: v = os.environ.get(var) if v: env['ENV'][var] = v if 'COMSPEC' not in env['ENV']: v = os.environ.get("COMSPEC") if v: env['ENV']['COMSPEC'] = v env.AppendENVPath('PATH', get_system_root() + '\System32') env['ENV']['PATHEXT'] = '.COM;.EXE;.BAT;.CMD' env['OBJPREFIX'] = '' env['OBJSUFFIX'] = '.obj' env['SHOBJPREFIX'] = '$OBJPREFIX' env['SHOBJSUFFIX'] = '$OBJSUFFIX' env['PROGPREFIX'] = '' env['PROGSUFFIX'] = '.exe' env['LIBPREFIX'] = '' env['LIBSUFFIX'] = '.lib' env['SHLIBPREFIX'] = '' env['SHLIBSUFFIX'] = '.dll' env['LIBPREFIXES'] = [ '$LIBPREFIX' ] env['LIBSUFFIXES'] = [ '$LIBSUFFIX' ] env['PSPAWN'] = piped_spawn env['SPAWN'] = spawn env['SHELL'] = cmd_interp env['TEMPFILE'] = TempFileMunge env['TEMPFILEPREFIX'] = '@' env['MAXLINELENGTH'] = 2048 env['ESCAPE'] = escape env['HOST_OS'] = 'win32' env['HOST_ARCH'] = get_architecture().arch # Local Variables: # tab-width:4 # indent-tabs-mode:nil # End: # vim: set expandtab tabstop=4 shiftwidth=4:
gpl-2.0
PennartLoettring/Poettrix
rootfs/usr/lib/python3.4/encodings/shift_jis.py
816
1039
# # shift_jis.py: Python Unicode Codec for SHIFT_JIS # # Written by Hye-Shik Chang <[email protected]> # import _codecs_jp, codecs import _multibytecodec as mbc codec = _codecs_jp.getcodec('shift_jis') class Codec(codecs.Codec): encode = codec.encode decode = codec.decode class IncrementalEncoder(mbc.MultibyteIncrementalEncoder, codecs.IncrementalEncoder): codec = codec class IncrementalDecoder(mbc.MultibyteIncrementalDecoder, codecs.IncrementalDecoder): codec = codec class StreamReader(Codec, mbc.MultibyteStreamReader, codecs.StreamReader): codec = codec class StreamWriter(Codec, mbc.MultibyteStreamWriter, codecs.StreamWriter): codec = codec def getregentry(): return codecs.CodecInfo( name='shift_jis', encode=Codec().encode, decode=Codec().decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, )
gpl-2.0
kumarkrishna/sympy
sympy/matrices/tests/test_interactions.py
58
1881
""" We have a few different kind of Matrices Matrix, ImmutableMatrix, MatrixExpr Here we test the extent to which they cooperate """ from sympy import symbols from sympy.matrices import (Matrix, MatrixSymbol, eye, Identity, ImmutableMatrix) from sympy.core.compatibility import range from sympy.matrices.expressions import MatrixExpr, MatAdd from sympy.matrices.matrices import classof from sympy.utilities.pytest import raises SM = MatrixSymbol('X', 3, 3) MM = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) IM = ImmutableMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) meye = eye(3) imeye = ImmutableMatrix(eye(3)) ideye = Identity(3) a, b, c = symbols('a,b,c') def test_IM_MM(): assert isinstance(MM + IM, ImmutableMatrix) assert isinstance(IM + MM, ImmutableMatrix) assert isinstance(2*IM + MM, ImmutableMatrix) assert MM.equals(IM) def test_ME_MM(): assert isinstance(Identity(3) + MM, MatrixExpr) assert isinstance(SM + MM, MatAdd) assert isinstance(MM + SM, MatAdd) assert (Identity(3) + MM)[1, 1] == 6 def test_equality(): a, b, c = Identity(3), eye(3), ImmutableMatrix(eye(3)) for x in [a, b, c]: for y in [a, b, c]: assert x.equals(y) def test_matrix_symbol_MM(): X = MatrixSymbol('X', 3, 3) Y = eye(3) + X assert Y[1, 1] == 1 + X[1, 1] def test_indexing_interactions(): assert (a * IM)[1, 1] == 5*a assert (SM + IM)[1, 1] == SM[1, 1] + IM[1, 1] assert (SM * IM)[1, 1] == SM[1, 0]*IM[0, 1] + SM[1, 1]*IM[1, 1] + \ SM[1, 2]*IM[2, 1] def test_classof(): A = Matrix(3, 3, range(9)) B = ImmutableMatrix(3, 3, range(9)) C = MatrixSymbol('C', 3, 3) assert classof(A, A) == Matrix assert classof(B, B) == ImmutableMatrix assert classof(A, B) == ImmutableMatrix assert classof(B, A) == ImmutableMatrix raises(TypeError, lambda: classof(A, C))
bsd-3-clause
Stanford-Online/edx-platform
cms/djangoapps/contentstore/views/tests/test_container_page.py
13
10800
""" Unit tests for the container page. """ import datetime import re from django.http import Http404 from django.test.client import RequestFactory from django.utils import http from mock import Mock, patch from pytz import UTC import contentstore.views.component as views from contentstore.tests.test_libraries import LibraryTestCase from contentstore.views.tests.utils import StudioPageTestCase from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.django import modulestore from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory class ContainerPageTestCase(StudioPageTestCase, LibraryTestCase): """ Unit tests for the container page. """ container_view = 'container_preview' reorderable_child_view = 'reorderable_container_child_preview' def setUp(self): super(ContainerPageTestCase, self).setUp() self.vertical = self._create_item(self.sequential.location, 'vertical', 'Unit') self.html = self._create_item(self.vertical.location, "html", "HTML") self.child_container = self._create_item(self.vertical.location, 'split_test', 'Split Test') self.child_vertical = self._create_item(self.child_container.location, 'vertical', 'Child Vertical') self.video = self._create_item(self.child_vertical.location, "video", "My Video") self.store = modulestore() past = datetime.datetime(1970, 1, 1, tzinfo=UTC) future = datetime.datetime.now(UTC) + datetime.timedelta(days=1) self.released_private_vertical = self._create_item( parent_location=self.sequential.location, category='vertical', display_name='Released Private Unit', start=past) self.unreleased_private_vertical = self._create_item( parent_location=self.sequential.location, category='vertical', display_name='Unreleased Private Unit', start=future) self.released_public_vertical = self._create_item( parent_location=self.sequential.location, category='vertical', display_name='Released Public Unit', start=past) self.unreleased_public_vertical = self._create_item( parent_location=self.sequential.location, category='vertical', display_name='Unreleased Public Unit', start=future) self.store.publish(self.unreleased_public_vertical.location, self.user.id) self.store.publish(self.released_public_vertical.location, self.user.id) def test_container_html(self): self._test_html_content( self.child_container, expected_section_tag=( '<section class="wrapper-xblock level-page is-hidden studio-xblock-wrapper" ' 'data-locator="{0}" data-course-key="{0.course_key}">'.format(self.child_container.location) ), expected_breadcrumbs=( r'<a href="/course/{course}{section_parameters}" class="{classes}">\s*Week 1\s*</a>\s*' r'<a href="/course/{course}{subsection_parameters}" class="{classes}">\s*Lesson 1\s*</a>\s*' r'<a href="/container/{unit}" class="{classes}">\s*Unit\s*</a>' ).format( course=re.escape(unicode(self.course.id)), unit=re.escape(unicode(self.vertical.location)), classes='navigation-item navigation-link navigation-parent', section_parameters=re.escape(u'?show={}'.format(http.urlquote(self.chapter.location))), subsection_parameters=re.escape(u'?show={}'.format(http.urlquote(self.sequential.location))), ), ) def test_container_on_container_html(self): """ Create the scenario of an xblock with children (non-vertical) on the container page. This should create a container page that is a child of another container page. """ draft_container = self._create_item(self.child_container.location, "wrapper", "Wrapper") self._create_item(draft_container.location, "html", "Child HTML") def test_container_html(xblock): self._test_html_content( xblock, expected_section_tag=( '<section class="wrapper-xblock level-page is-hidden studio-xblock-wrapper" ' 'data-locator="{0}" data-course-key="{0.course_key}">'.format(draft_container.location) ), expected_breadcrumbs=( r'<a href="/course/{course}{section_parameters}" class="{classes}">\s*Week 1\s*</a>\s*' r'<a href="/course/{course}{subsection_parameters}" class="{classes}">\s*Lesson 1\s*</a>\s*' r'<a href="/container/{unit}" class="{classes}">\s*Unit\s*</a>\s*' r'<a href="/container/{split_test}" class="{classes}">\s*Split Test\s*</a>' ).format( course=re.escape(unicode(self.course.id)), unit=re.escape(unicode(self.vertical.location)), split_test=re.escape(unicode(self.child_container.location)), classes='navigation-item navigation-link navigation-parent', section_parameters=re.escape(u'?show={}'.format(http.urlquote(self.chapter.location))), subsection_parameters=re.escape(u'?show={}'.format(http.urlquote(self.sequential.location))), ), ) # Test the draft version of the container test_container_html(draft_container) # Now publish the unit and validate again self.store.publish(self.vertical.location, self.user.id) draft_container = self.store.get_item(draft_container.location) test_container_html(draft_container) def _test_html_content(self, xblock, expected_section_tag, expected_breadcrumbs): """ Get the HTML for a container page and verify the section tag is correct and the breadcrumbs trail is correct. """ html = self.get_page_html(xblock) self.assertIn(expected_section_tag, html) self.assertRegexpMatches(html, expected_breadcrumbs) def test_public_container_preview_html(self): """ Verify that a public xblock's container preview returns the expected HTML. """ published_unit = self.store.publish(self.vertical.location, self.user.id) published_child_container = self.store.get_item(self.child_container.location) published_child_vertical = self.store.get_item(self.child_vertical.location) self.validate_preview_html(published_unit, self.container_view) self.validate_preview_html(published_child_container, self.container_view) self.validate_preview_html(published_child_vertical, self.reorderable_child_view) def test_library_page_preview_html(self): """ Verify that a library xblock's container (library page) preview returns the expected HTML. """ # Add some content to library. self._add_simple_content_block() self.validate_preview_html(self.library, self.container_view, can_reorder=False, can_move=False) def test_library_content_preview_html(self): """ Verify that a library content block container page preview returns the expected HTML. """ # Library content block is only supported in split courses. with modulestore().default_store(ModuleStoreEnum.Type.split): course = CourseFactory.create() # Add some content to library self._add_simple_content_block() # Create a library content block lc_block = self._add_library_content_block(course, self.lib_key) self.assertEqual(len(lc_block.children), 0) # Refresh children to be reflected in lc_block lc_block = self._refresh_children(lc_block) self.assertEqual(len(lc_block.children), 1) self.validate_preview_html( lc_block, self.container_view, can_add=False, can_reorder=False, can_move=False, can_edit=True, can_duplicate=False, can_delete=False ) def test_draft_container_preview_html(self): """ Verify that a draft xblock's container preview returns the expected HTML. """ self.validate_preview_html(self.vertical, self.container_view) self.validate_preview_html(self.child_container, self.container_view) self.validate_preview_html(self.child_vertical, self.reorderable_child_view) def _create_item(self, parent_location, category, display_name, **kwargs): """ creates an item in the module store, without publishing it. """ return ItemFactory.create( parent_location=parent_location, category=category, display_name=display_name, publish_item=False, user_id=self.user.id, **kwargs ) def test_public_child_container_preview_html(self): """ Verify that a public container rendered as a child of the container page returns the expected HTML. """ empty_child_container = self._create_item(self.vertical.location, 'split_test', 'Split Test') published_empty_child_container = self.store.publish(empty_child_container.location, self.user.id) self.validate_preview_html(published_empty_child_container, self.reorderable_child_view, can_add=False) def test_draft_child_container_preview_html(self): """ Verify that a draft container rendered as a child of the container page returns the expected HTML. """ empty_child_container = self._create_item(self.vertical.location, 'split_test', 'Split Test') self.validate_preview_html(empty_child_container, self.reorderable_child_view, can_add=False) @patch('contentstore.views.component.render_to_response', Mock(return_value=Mock(status_code=200, content=''))) def test_container_page_with_valid_and_invalid_usage_key_string(self): """ Check that invalid 'usage_key_string' raises Http404. """ request = RequestFactory().get('foo') request.user = self.user request.LANGUAGE_CODE = 'en' # Check for invalid 'usage_key_strings' self.assertRaises( Http404, views.container_handler, request, usage_key_string='i4x://InvalidOrg/InvalidCourse/vertical/static/InvalidContent', ) # Check 200 response if 'usage_key_string' is correct response = views.container_handler( request=request, usage_key_string=unicode(self.vertical.location) ) self.assertEqual(response.status_code, 200)
agpl-3.0
voxlol/scikit-learn
sklearn/feature_extraction/tests/test_feature_hasher.py
258
2861
from __future__ import unicode_literals import numpy as np from sklearn.feature_extraction import FeatureHasher from nose.tools import assert_raises, assert_true from numpy.testing import assert_array_equal, assert_equal def test_feature_hasher_dicts(): h = FeatureHasher(n_features=16) assert_equal("dict", h.input_type) raw_X = [{"dada": 42, "tzara": 37}, {"gaga": 17}] X1 = FeatureHasher(n_features=16).transform(raw_X) gen = (iter(d.items()) for d in raw_X) X2 = FeatureHasher(n_features=16, input_type="pair").transform(gen) assert_array_equal(X1.toarray(), X2.toarray()) def test_feature_hasher_strings(): # mix byte and Unicode strings; note that "foo" is a duplicate in row 0 raw_X = [["foo", "bar", "baz", "foo".encode("ascii")], ["bar".encode("ascii"), "baz", "quux"]] for lg_n_features in (7, 9, 11, 16, 22): n_features = 2 ** lg_n_features it = (x for x in raw_X) # iterable h = FeatureHasher(n_features, non_negative=True, input_type="string") X = h.transform(it) assert_equal(X.shape[0], len(raw_X)) assert_equal(X.shape[1], n_features) assert_true(np.all(X.data > 0)) assert_equal(X[0].sum(), 4) assert_equal(X[1].sum(), 3) assert_equal(X.nnz, 6) def test_feature_hasher_pairs(): raw_X = (iter(d.items()) for d in [{"foo": 1, "bar": 2}, {"baz": 3, "quux": 4, "foo": -1}]) h = FeatureHasher(n_features=16, input_type="pair") x1, x2 = h.transform(raw_X).toarray() x1_nz = sorted(np.abs(x1[x1 != 0])) x2_nz = sorted(np.abs(x2[x2 != 0])) assert_equal([1, 2], x1_nz) assert_equal([1, 3, 4], x2_nz) def test_hash_empty_input(): n_features = 16 raw_X = [[], (), iter(range(0))] h = FeatureHasher(n_features=n_features, input_type="string") X = h.transform(raw_X) assert_array_equal(X.A, np.zeros((len(raw_X), n_features))) def test_hasher_invalid_input(): assert_raises(ValueError, FeatureHasher, input_type="gobbledygook") assert_raises(ValueError, FeatureHasher, n_features=-1) assert_raises(ValueError, FeatureHasher, n_features=0) assert_raises(TypeError, FeatureHasher, n_features='ham') h = FeatureHasher(n_features=np.uint16(2 ** 6)) assert_raises(ValueError, h.transform, []) assert_raises(Exception, h.transform, [[5.5]]) assert_raises(Exception, h.transform, [[None]]) def test_hasher_set_params(): # Test delayed input validation in fit (useful for grid search). hasher = FeatureHasher() hasher.set_params(n_features=np.inf) assert_raises(TypeError, hasher.fit) def test_hasher_zeros(): # Assert that no zeros are materialized in the output. X = FeatureHasher().transform([{'foo': 0}]) assert_equal(X.data.shape, (0,))
bsd-3-clause
JshWright/home-assistant
tests/components/switch/test_rest.py
4
7488
"""The tests for the REST switch platform.""" import asyncio import aiohttp import homeassistant.components.switch.rest as rest from homeassistant.setup import setup_component from homeassistant.util.async import run_coroutine_threadsafe from homeassistant.helpers.template import Template from tests.common import get_test_home_assistant, assert_setup_component class TestRestSwitchSetup: """Tests for setting up the REST switch platform.""" def setup_method(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() def teardown_method(self): """Stop everything that was started.""" self.hass.stop() def test_setup_missing_config(self): """Test setup with configuration missing required entries.""" assert not run_coroutine_threadsafe( rest.async_setup_platform(self.hass, { 'platform': 'rest' }, None), self.hass.loop ).result() def test_setup_missing_schema(self): """Test setup with resource missing schema.""" assert not run_coroutine_threadsafe( rest.async_setup_platform(self.hass, { 'platform': 'rest', 'resource': 'localhost' }, None), self.hass.loop ).result() def test_setup_failed_connect(self, aioclient_mock): """Test setup when connection error occurs.""" aioclient_mock.get('http://localhost', exc=aiohttp.ClientError) assert not run_coroutine_threadsafe( rest.async_setup_platform(self.hass, { 'platform': 'rest', 'resource': 'http://localhost', }, None), self.hass.loop ).result() def test_setup_timeout(self, aioclient_mock): """Test setup when connection timeout occurs.""" aioclient_mock.get('http://localhost', exc=asyncio.TimeoutError()) assert not run_coroutine_threadsafe( rest.async_setup_platform(self.hass, { 'platform': 'rest', 'resource': 'http://localhost', }, None), self.hass.loop ).result() def test_setup_minimum(self, aioclient_mock): """Test setup with minimum configuration.""" aioclient_mock.get('http://localhost', status=200) with assert_setup_component(1, 'switch'): assert setup_component(self.hass, 'switch', { 'switch': { 'platform': 'rest', 'resource': 'http://localhost' } }) assert aioclient_mock.call_count == 1 def test_setup(self, aioclient_mock): """Test setup with valid configuration.""" aioclient_mock.get('http://localhost', status=200) assert setup_component(self.hass, 'switch', { 'switch': { 'platform': 'rest', 'name': 'foo', 'resource': 'http://localhost', 'body_on': 'custom on text', 'body_off': 'custom off text', } }) assert aioclient_mock.call_count == 1 assert_setup_component(1, 'switch') class TestRestSwitch: """Tests for REST switch platform.""" def setup_method(self): """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() self.name = 'foo' self.resource = 'http://localhost/' self.body_on = Template('on', self.hass) self.body_off = Template('off', self.hass) self.switch = rest.RestSwitch(self.hass, self.name, self.resource, self.body_on, self.body_off, None, 10) def teardown_method(self): """Stop everything that was started.""" self.hass.stop() def test_name(self): """Test the name.""" assert self.name == self.switch.name def test_is_on_before_update(self): """Test is_on in initial state.""" assert self.switch.is_on is None def test_turn_on_success(self, aioclient_mock): """Test turn_on.""" aioclient_mock.post(self.resource, status=200) run_coroutine_threadsafe( self.switch.async_turn_on(), self.hass.loop).result() assert self.body_on.template == \ aioclient_mock.mock_calls[-1][2].decode() assert self.switch.is_on def test_turn_on_status_not_ok(self, aioclient_mock): """Test turn_on when error status returned.""" aioclient_mock.post(self.resource, status=500) run_coroutine_threadsafe( self.switch.async_turn_on(), self.hass.loop).result() assert self.body_on.template == \ aioclient_mock.mock_calls[-1][2].decode() assert self.switch.is_on is None def test_turn_on_timeout(self, aioclient_mock): """Test turn_on when timeout occurs.""" aioclient_mock.post(self.resource, status=500) run_coroutine_threadsafe( self.switch.async_turn_on(), self.hass.loop).result() assert self.switch.is_on is None def test_turn_off_success(self, aioclient_mock): """Test turn_off.""" aioclient_mock.post(self.resource, status=200) run_coroutine_threadsafe( self.switch.async_turn_off(), self.hass.loop).result() assert self.body_off.template == \ aioclient_mock.mock_calls[-1][2].decode() assert not self.switch.is_on def test_turn_off_status_not_ok(self, aioclient_mock): """Test turn_off when error status returned.""" aioclient_mock.post(self.resource, status=500) run_coroutine_threadsafe( self.switch.async_turn_off(), self.hass.loop).result() assert self.body_off.template == \ aioclient_mock.mock_calls[-1][2].decode() assert self.switch.is_on is None def test_turn_off_timeout(self, aioclient_mock): """Test turn_off when timeout occurs.""" aioclient_mock.post(self.resource, exc=asyncio.TimeoutError()) run_coroutine_threadsafe( self.switch.async_turn_on(), self.hass.loop).result() assert self.switch.is_on is None def test_update_when_on(self, aioclient_mock): """Test update when switch is on.""" aioclient_mock.get(self.resource, text=self.body_on.template) run_coroutine_threadsafe( self.switch.async_update(), self.hass.loop).result() assert self.switch.is_on def test_update_when_off(self, aioclient_mock): """Test update when switch is off.""" aioclient_mock.get(self.resource, text=self.body_off.template) run_coroutine_threadsafe( self.switch.async_update(), self.hass.loop).result() assert not self.switch.is_on def test_update_when_unknown(self, aioclient_mock): """Test update when unknown status returned.""" aioclient_mock.get(self.resource, text='unknown status') run_coroutine_threadsafe( self.switch.async_update(), self.hass.loop).result() assert self.switch.is_on is None def test_update_timeout(self, aioclient_mock): """Test update when timeout occurs.""" aioclient_mock.get(self.resource, exc=asyncio.TimeoutError()) run_coroutine_threadsafe( self.switch.async_update(), self.hass.loop).result() assert self.switch.is_on is None
apache-2.0
cloudbase/neutron-virtualbox
neutron/plugins/cisco/common/cisco_exceptions.py
43
8481
# Copyright 2011 Cisco Systems, 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. """Exceptions used by the Cisco plugin.""" from neutron.common import exceptions class NetworkSegmentIDNotFound(exceptions.NeutronException): """Segmentation ID for network is not found.""" message = _("Segmentation ID for network %(net_id)s is not found.") class NoMoreNics(exceptions.NeutronException): """No more dynamic NICs are available in the system.""" message = _("Unable to complete operation. No more dynamic NICs are " "available in the system.") class NetworkVlanBindingAlreadyExists(exceptions.NeutronException): """Binding cannot be created, since it already exists.""" message = _("NetworkVlanBinding for %(vlan_id)s and network " "%(network_id)s already exists.") class VlanIDNotFound(exceptions.NeutronException): """VLAN ID cannot be found.""" message = _("Vlan ID %(vlan_id)s not found.") class VlanIDOutsidePool(exceptions.NeutronException): """VLAN ID cannot be allocated, since it is outside the configured pool.""" message = _("Unable to complete operation. VLAN ID exists outside of the " "configured network segment range.") class VlanIDNotAvailable(exceptions.NeutronException): """No VLAN ID available.""" message = _("No Vlan ID available.") class QosNotFound(exceptions.NeutronException): """QoS level with this ID cannot be found.""" message = _("QoS level %(qos_id)s could not be found " "for tenant %(tenant_id)s.") class QosNameAlreadyExists(exceptions.NeutronException): """QoS Name already exists.""" message = _("QoS level with name %(qos_name)s already exists " "for tenant %(tenant_id)s.") class CredentialNotFound(exceptions.NeutronException): """Credential with this ID cannot be found.""" message = _("Credential %(credential_id)s could not be found.") class CredentialNameNotFound(exceptions.NeutronException): """Credential Name could not be found.""" message = _("Credential %(credential_name)s could not be found.") class CredentialAlreadyExists(exceptions.NeutronException): """Credential already exists.""" message = _("Credential %(credential_name)s already exists.") class ProviderNetworkExists(exceptions.NeutronException): """Provider network already exists.""" message = _("Provider network %s already exists") class NexusComputeHostNotConfigured(exceptions.NeutronException): """Connection to compute host is not configured.""" message = _("Connection to %(host)s is not configured.") class NexusConnectFailed(exceptions.NeutronException): """Failed to connect to Nexus switch.""" message = _("Unable to connect to Nexus %(nexus_host)s. Reason: %(exc)s.") class NexusConfigFailed(exceptions.NeutronException): """Failed to configure Nexus switch.""" message = _("Failed to configure Nexus: %(config)s. Reason: %(exc)s.") class NexusPortBindingNotFound(exceptions.NeutronException): """NexusPort Binding is not present.""" message = _("Nexus Port Binding (%(filters)s) is not present.") def __init__(self, **kwargs): filters = ','.join('%s=%s' % i for i in kwargs.items()) super(NexusPortBindingNotFound, self).__init__(filters=filters) class NoNexusSviSwitch(exceptions.NeutronException): """No usable nexus switch found.""" message = _("No usable Nexus switch found to create SVI interface.") class PortVnicBindingAlreadyExists(exceptions.NeutronException): """PortVnic Binding already exists.""" message = _("PortVnic Binding %(port_id)s already exists.") class PortVnicNotFound(exceptions.NeutronException): """PortVnic Binding is not present.""" message = _("PortVnic Binding %(port_id)s is not present.") class SubnetNotSpecified(exceptions.NeutronException): """Subnet id not specified.""" message = _("No subnet_id specified for router gateway.") class SubnetInterfacePresent(exceptions.NeutronException): """Subnet SVI interface already exists.""" message = _("Subnet %(subnet_id)s has an interface on %(router_id)s.") class PortIdForNexusSvi(exceptions.NeutronException): """Port Id specified for Nexus SVI.""" message = _('Nexus hardware router gateway only uses Subnet Ids.') class InvalidDetach(exceptions.NeutronException): message = _("Unable to unplug the attachment %(att_id)s from port " "%(port_id)s for network %(net_id)s. The attachment " "%(att_id)s does not exist.") class PolicyProfileAlreadyExists(exceptions.NeutronException): """Policy Profile cannot be created since it already exists.""" message = _("Policy Profile %(profile_id)s " "already exists.") class PolicyProfileIdNotFound(exceptions.NotFound): """Policy Profile with the given UUID cannot be found.""" message = _("Policy Profile %(profile_id)s could not be found.") class PolicyProfileNameNotFound(exceptions.NotFound): """Policy Profile with the given name cannot be found.""" message = _("Policy Profile %(profile_name)s could not be found.") class NetworkProfileAlreadyExists(exceptions.NeutronException): """Network Profile cannot be created since it already exists.""" message = _("Network Profile %(profile_id)s " "already exists.") class NetworkProfileNotFound(exceptions.NotFound): """Network Profile with the given UUID/name cannot be found.""" message = _("Network Profile %(profile)s could not be found.") class NetworkProfileInUse(exceptions.InUse): """Network Profile with the given UUID is in use.""" message = _("One or more network segments belonging to network " "profile %(profile)s is in use.") class NoMoreNetworkSegments(exceptions.NoNetworkAvailable): """Network segments exhausted for the given network profile.""" message = _("No more segments available in network segment pool " "%(network_profile_name)s.") class VMNetworkNotFound(exceptions.NotFound): """VM Network with the given name cannot be found.""" message = _("VM Network %(name)s could not be found.") class VxlanIDInUse(exceptions.InUse): """VXLAN ID is in use.""" message = _("Unable to create the network. " "The VXLAN ID %(vxlan_id)s is in use.") class VxlanIDNotFound(exceptions.NotFound): """VXLAN ID cannot be found.""" message = _("Vxlan ID %(vxlan_id)s not found.") class VxlanIDOutsidePool(exceptions.NeutronException): """VXLAN ID cannot be allocated, as it is outside the configured pool.""" message = _("Unable to complete operation. VXLAN ID exists outside of the " "configured network segment range.") class VSMConnectionFailed(exceptions.ServiceUnavailable): """Connection to VSM failed.""" message = _("Connection to VSM failed: %(reason)s.") class VSMError(exceptions.NeutronException): """Error has occurred on the VSM.""" message = _("Internal VSM Error: %(reason)s.") class NetworkBindingNotFound(exceptions.NotFound): """Network Binding for network cannot be found.""" message = _("Network Binding for network %(network_id)s could " "not be found.") class PortBindingNotFound(exceptions.NotFound): """Port Binding for port cannot be found.""" message = _("Port Binding for port %(port_id)s could " "not be found.") class ProfileTenantBindingNotFound(exceptions.NotFound): """Profile to Tenant binding for given profile ID cannot be found.""" message = _("Profile-Tenant binding for profile %(profile_id)s could " "not be found.") class NoClusterFound(exceptions.NotFound): """No service cluster found to perform multi-segment bridging.""" message = _("No service cluster found to perform multi-segment bridging.")
apache-2.0
dmitriy0611/django
django/core/servers/basehttp.py
94
7318
""" HTTP server that implements the Python WSGI protocol (PEP 333, rev 1.21). Based on wsgiref.simple_server which is part of the standard library since 2.5. This is a simple server for use in testing or debugging Django apps. It hasn't been reviewed for security issues. DON'T USE IT FOR PRODUCTION USE! """ from __future__ import unicode_literals import socket import sys from wsgiref import simple_server from django.core.exceptions import ImproperlyConfigured from django.core.handlers.wsgi import ISO_8859_1, UTF_8 from django.core.management.color import color_style from django.core.wsgi import get_wsgi_application from django.utils import six from django.utils.encoding import uri_to_iri from django.utils.module_loading import import_string from django.utils.six.moves import socketserver __all__ = ('WSGIServer', 'WSGIRequestHandler') def get_internal_wsgi_application(): """ Loads and returns the WSGI application as configured by the user in ``settings.WSGI_APPLICATION``. With the default ``startproject`` layout, this will be the ``application`` object in ``projectname/wsgi.py``. This function, and the ``WSGI_APPLICATION`` setting itself, are only useful for Django's internal server (runserver); external WSGI servers should just be configured to point to the correct application object directly. If settings.WSGI_APPLICATION is not set (is ``None``), we just return whatever ``django.core.wsgi.get_wsgi_application`` returns. """ from django.conf import settings app_path = getattr(settings, 'WSGI_APPLICATION') if app_path is None: return get_wsgi_application() try: return import_string(app_path) except ImportError as e: msg = ( "WSGI application '%(app_path)s' could not be loaded; " "Error importing module: '%(exception)s'" % ({ 'app_path': app_path, 'exception': e, }) ) six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg), sys.exc_info()[2]) def is_broken_pipe_error(): exc_type, exc_value = sys.exc_info()[:2] return issubclass(exc_type, socket.error) and exc_value.args[0] == 32 class WSGIServer(simple_server.WSGIServer, object): """BaseHTTPServer that implements the Python WSGI protocol""" request_queue_size = 10 def __init__(self, *args, **kwargs): if kwargs.pop('ipv6', False): self.address_family = socket.AF_INET6 super(WSGIServer, self).__init__(*args, **kwargs) def server_bind(self): """Override server_bind to store the server name.""" super(WSGIServer, self).server_bind() self.setup_environ() def handle_error(self, request, client_address): if is_broken_pipe_error(): sys.stderr.write("- Broken pipe from %s\n" % (client_address,)) else: super(WSGIServer, self).handle_error(request, client_address) # Inheriting from object required on Python 2. class ServerHandler(simple_server.ServerHandler, object): def handle_error(self): # Ignore broken pipe errors, otherwise pass on if not is_broken_pipe_error(): super(ServerHandler, self).handle_error() class WSGIRequestHandler(simple_server.WSGIRequestHandler, object): def __init__(self, *args, **kwargs): self.style = color_style() super(WSGIRequestHandler, self).__init__(*args, **kwargs) def address_string(self): # Short-circuit parent method to not call socket.getfqdn return self.client_address[0] def log_message(self, format, *args): msg = "[%s]" % self.log_date_time_string() try: msg += "%s\n" % (format % args) except UnicodeDecodeError: # e.g. accessing the server via SSL on Python 2 msg += "\n" # Utilize terminal colors, if available if args[1][0] == '2': # Put 2XX first, since it should be the common case msg = self.style.HTTP_SUCCESS(msg) elif args[1][0] == '1': msg = self.style.HTTP_INFO(msg) elif args[1] == '304': msg = self.style.HTTP_NOT_MODIFIED(msg) elif args[1][0] == '3': msg = self.style.HTTP_REDIRECT(msg) elif args[1] == '404': msg = self.style.HTTP_NOT_FOUND(msg) elif args[1][0] == '4': # 0x16 = Handshake, 0x03 = SSL 3.0 or TLS 1.x if args[0].startswith(str('\x16\x03')): msg = ("You're accessing the development server over HTTPS, " "but it only supports HTTP.\n") msg = self.style.HTTP_BAD_REQUEST(msg) else: # Any 5XX, or any other response msg = self.style.HTTP_SERVER_ERROR(msg) sys.stderr.write(msg) def get_environ(self): # Strip all headers with underscores in the name before constructing # the WSGI environ. This prevents header-spoofing based on ambiguity # between underscores and dashes both normalized to underscores in WSGI # env vars. Nginx and Apache 2.4+ both do this as well. for k, v in self.headers.items(): if '_' in k: del self.headers[k] env = super(WSGIRequestHandler, self).get_environ() path = self.path if '?' in path: path = path.partition('?')[0] path = uri_to_iri(path).encode(UTF_8) # Under Python 3, non-ASCII values in the WSGI environ are arbitrarily # decoded with ISO-8859-1. We replicate this behavior here. # Refs comment in `get_bytes_from_wsgi()`. env['PATH_INFO'] = path.decode(ISO_8859_1) if six.PY3 else path return env def handle(self): """Copy of WSGIRequestHandler, but with different ServerHandler""" self.raw_requestline = self.rfile.readline(65537) if len(self.raw_requestline) > 65536: self.requestline = '' self.request_version = '' self.command = '' self.send_error(414) return if not self.parse_request(): # An error code has been sent, just exit return handler = ServerHandler( self.rfile, self.wfile, self.get_stderr(), self.get_environ() ) handler.request_handler = self # backpointer for logging handler.run(self.server.get_app()) def run(addr, port, wsgi_handler, ipv6=False, threading=False): server_address = (addr, port) if threading: httpd_cls = type(str('WSGIServer'), (socketserver.ThreadingMixIn, WSGIServer), {}) else: httpd_cls = WSGIServer httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6) if threading: # ThreadingMixIn.daemon_threads indicates how threads will behave on an # abrupt shutdown; like quitting the server by the user or restarting # by the auto-reloader. True means the server will not wait for thread # termination before it quits. This will make auto-reloader faster # and will prevent the need to kill the server manually if a thread # isn't terminating correctly. httpd.daemon_threads = True httpd.set_app(wsgi_handler) httpd.serve_forever()
bsd-3-clause
corruptnova/namebench
nb_third_party/graphy/backends/google_chart_api/util.py
233
6335
#!/usr/bin/python2.4 # # Copyright 2008 Google 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. """Utility functions for working with the Google Chart API. Not intended for end users, use the methods in __init__ instead.""" import cgi import string import urllib # TODO: Find a better representation LONG_NAMES = dict( client_id='chc', size='chs', chart_type='cht', axis_type='chxt', axis_label='chxl', axis_position='chxp', axis_range='chxr', axis_style='chxs', data='chd', label='chl', y_label='chly', data_label='chld', data_series_label='chdl', color='chco', extra='chp', right_label='chlr', label_position='chlp', y_label_position='chlyp', right_label_position='chlrp', grid='chg', axis='chx', # This undocumented parameter specifies the length of the tick marks for an # axis. Negative values will extend tick marks into the main graph area. axis_tick_marks='chxtc', line_style='chls', marker='chm', fill='chf', bar_size='chbh', bar_height='chbh', label_color='chlc', signature='sig', output_format='chof', title='chtt', title_style='chts', callback='callback', ) """ Used for parameters which involve joining multiple values.""" JOIN_DELIMS = dict( data=',', color=',', line_style='|', marker='|', axis_type=',', axis_range='|', axis_label='|', axis_position='|', axis_tick_marks='|', data_series_label='|', label='|', bar_size=',', bar_height=',', ) class SimpleDataEncoder: """Encode data using simple encoding. Out-of-range data will be dropped (encoded as '_'). """ def __init__(self): self.prefix = 's:' self.code = string.ascii_uppercase + string.ascii_lowercase + string.digits self.min = 0 self.max = len(self.code) - 1 def Encode(self, data): return ''.join(self._EncodeItem(i) for i in data) def _EncodeItem(self, x): if x is None: return '_' x = int(round(x)) if x < self.min or x > self.max: return '_' return self.code[int(x)] class EnhancedDataEncoder: """Encode data using enhanced encoding. Out-of-range data will be dropped (encoded as '_'). """ def __init__(self): self.prefix = 'e:' chars = string.ascii_uppercase + string.ascii_lowercase + string.digits \ + '-.' self.code = [x + y for x in chars for y in chars] self.min = 0 self.max = len(self.code) - 1 def Encode(self, data): return ''.join(self._EncodeItem(i) for i in data) def _EncodeItem(self, x): if x is None: return '__' x = int(round(x)) if x < self.min or x > self.max: return '__' return self.code[int(x)] def EncodeUrl(base, params, escape_url, use_html_entities): """Escape params, combine and append them to base to generate a full URL.""" real_params = [] for key, value in params.iteritems(): if escape_url: value = urllib.quote(value) if value: real_params.append('%s=%s' % (key, value)) if real_params: url = '%s?%s' % (base, '&'.join(real_params)) else: url = base if use_html_entities: url = cgi.escape(url, quote=True) return url def ShortenParameterNames(params): """Shorten long parameter names (like size) to short names (like chs).""" out = {} for name, value in params.iteritems(): short_name = LONG_NAMES.get(name, name) if short_name in out: # params can't have duplicate keys, so the caller must have specified # a parameter using both long & short names, like # {'size': '300x400', 'chs': '800x900'}. We don't know which to use. raise KeyError('Both long and short version of parameter %s (%s) ' 'found. It is unclear which one to use.' % (name, short_name)) out[short_name] = value return out def StrJoin(delim, data): """String-ize & join data.""" return delim.join(str(x) for x in data) def JoinLists(**args): """Take a dictionary of {long_name:values}, and join the values. For each long_name, join the values into a string according to JOIN_DELIMS. If values is empty or None, replace with an empty string. Returns: A dictionary {long_name:joined_value} entries. """ out = {} for key, val in args.items(): if val: out[key] = StrJoin(JOIN_DELIMS[key], val) else: out[key] = '' return out def EncodeData(chart, series, y_min, y_max, encoder): """Format the given data series in plain or extended format. Use the chart's encoder to determine the format. The formatted data will be scaled to fit within the range of values supported by the chosen encoding. Args: chart: The chart. series: A list of the the data series to format; each list element is a list of data points. y_min: Minimum data value. May be None if y_max is also None y_max: Maximum data value. May be None if y_min is also None Returns: A dictionary with one key, 'data', whose value is the fully encoded series. """ assert (y_min is None) == (y_max is None) if y_min is not None: def _ScaleAndEncode(series): series = ScaleData(series, y_min, y_max, encoder.min, encoder.max) return encoder.Encode(series) encoded_series = [_ScaleAndEncode(s) for s in series] else: encoded_series = [encoder.Encode(s) for s in series] result = JoinLists(**{'data': encoded_series}) result['data'] = encoder.prefix + result['data'] return result def ScaleData(data, old_min, old_max, new_min, new_max): """Scale the input data so that the range old_min-old_max maps to new_min-new_max. """ def ScalePoint(x): if x is None: return None return scale * x + translate if old_min == old_max: scale = 1 else: scale = (new_max - new_min) / float(old_max - old_min) translate = new_min - scale * old_min return map(ScalePoint, data)
apache-2.0
171121130/SWI
app/models.py
1
13784
from datetime import datetime import hashlib from werkzeug.security import generate_password_hash, check_password_hash from itsdangerous import TimedJSONWebSignatureSerializer as Serializer from markdown import markdown import bleach from flask import current_app, request from flask_login import UserMixin, AnonymousUserMixin from . import db, login_manager class Permission: FOLLOW = 0x01 COMMENT = 0x02 WRITE_ARTICLES = 0x04 MODERATE_COMMENTS = 0x08 ADMINISTER = 0x80 class Quest(db.Model): __tablename__ = 'quests' id = db.Column(db.Integer, primary_key=True) question = db.Column(db.Text) optionA = db.Column(db.Text) optionB = db.Column(db.Text) optionC = db.Column(db.Text) optionD = db.Column(db.Text) optionE = db.Column(db.Text) station_id = db.Column(db.Integer, db.ForeignKey('stations.id')) level_id = db.Column(db.Integer, db.ForeignKey('levels.id')) answer = db.Column(db.Integer) image = db.Column(db.LargeBinary(length=2048)) imageA = db.Column(db.LargeBinary(length=2048)) imageB = db.Column(db.LargeBinary(length=2048)) imageC = db.Column(db.LargeBinary(length=2048)) imageD = db.Column(db.LargeBinary(length=2048)) imageE = db.Column(db.LargeBinary(length=2048)) required = db.Column(db.Integer) # @staticmethod # def generate_fake(count=100): # from random import seed, randint # import forgery_py # # seed() # station_count = Station.query.count() # level_count = Level.query.count() # for i in range(count): # s = Station.query.offset(randint(0, station_count - 1)).first() # s2 = Level.query.offset(randint(0, level_count - 1)).first() # p = Quest(question=forgery_py.lorem_ipsum.sentences(randint(1, 5)), # optionA=forgery_py.lorem_ipsum.sentences(randint(1, 5)), # optionB=forgery_py.lorem_ipsum.sentences(randint(1, 5)), # optionC=forgery_py.lorem_ipsum.sentences(randint(0, 1)), # optionD=forgery_py.lorem_ipsum.sentences(randint(0, 1)), # optionE=forgery_py.lorem_ipsum.sentences(randint(0, 1)), # station=s, # level=s2, # answer=randint(1, 4), # quest_type = randint(1, 3)) # db.session.add(p) # db.session.commit() def __repr__(self): return '<Question %r>' % self.id class Station(db.Model): __tablename__ = 'stations' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) quests = db.relationship('Quest', backref='station', lazy='dynamic') checklists = db.relationship('Checklist', backref='station', lazy='dynamic') certs = db.relationship('Cert', backref='station', lazy='dynamic') @staticmethod def insert_stations(): stations = [ '车床', '焊接', '激光', '加工中心', '锯床', '铣床', '质检', '装配' '维修' ] for r in stations: station = Station.query.filter_by(name=r).first() if station is None: station = Station(name=r) db.session.add(station) db.session.commit() def __repr__(self): return '工位: %r' % self.name class Level(db.Model): __tablename__ = 'levels' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) quests = db.relationship('Quest', backref='level', lazy='dynamic') certs = db.relationship('Cert', backref='level', lazy='dynamic') @staticmethod def insert_levels(): levels = ['初级', '中级', '高级'] for r in levels: level = Level.query.filter_by(name=r).first() if level is None: level = Level(name=r) db.session.add(level) db.session.commit() def __repr__(self): return '等级: %r' % self.name class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) users = db.relationship('User', backref='role', lazy='dynamic') @staticmethod def insert_roles(): roles =[ ('Manager', (Permission.FOLLOW | Permission.COMMENT | Permission.WRITE_ARTICLES | Permission.MODERATE_COMMENTS, False)), ('User', (Permission.FOLLOW | Permission.COMMENT | Permission.WRITE_ARTICLES, True)), ('Administrator', (0xff, False)) ] for x, y in roles: role = Role.query.filter_by(name=x).first() if role is None: role = Role(name=x) role.permissions = y[0] role.default = y[1] db.session.add(role) db.session.commit() # roles ={ # 'User': (Permission.FOLLOW | # Permission.COMMENT | # Permission.WRITE_ARTICLES, True), # 'Moderator': (Permission.FOLLOW | # Permission.COMMENT | # Permission.WRITE_ARTICLES | # Permission.MODERATE_COMMENTS, False), # 'Administrator': (0xff, False) # } # for r in roles: # role = Role.query.filter_by(name=r).first() # if role is None: # role = Role(name=r) # role.permissions = roles[r][0] # role.default = roles[r][1] # db.session.add(role) # db.session.commit() def __repr__(self): return '<Role %r>' % self.name class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) password_hash = db.Column(db.String(128)) confirmed = db.Column(db.Boolean, default=False) name = db.Column(db.String(64)) location = db.Column(db.String(64)) about_me = db.Column(db.Text()) member_since = db.Column(db.DateTime(), default=datetime.utcnow) last_seen = db.Column(db.DateTime(), default=datetime.utcnow) avatar_hash = db.Column(db.String(32)) posts = db.relationship('Post', backref='author', lazy='dynamic') supervisor = db.Column(db.Text()) @staticmethod def generate_fake(count=100): from sqlalchemy.exc import IntegrityError from random import seed import forgery_py seed() for i in range(count): u = User(email=forgery_py.internet.email_address(), username=forgery_py.internet.user_name(True), password=forgery_py.lorem_ipsum.word(), confirmed=True, name=forgery_py.name.full_name(), location=forgery_py.address.city(), about_me=forgery_py.lorem_ipsum.sentence(), member_since=forgery_py.date.date(True)) db.session.add(u) try: db.session.commit() except IntegrityError: db.session.rollback() def __init__(self, **kwargs): super(User, self).__init__(**kwargs) if self.role is None: if self.email == current_app.config['FLASKY_ADMIN']: self.role = Role.query.filter_by(permissions=0xff).first() # if self.role is None: # self.role = Role.query.filter_by(default=True).first() if self.email is not None and self.avatar_hash is None: self.avatar_hash = hashlib.md5( self.email.encode('utf-8')).hexdigest() @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def generate_confirmation_token(self, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'confirm': self.id}) def confirm(self, token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except: return False if data.get('confirm') != self.id: return False self.confirmed = True db.session.add(self) return True def generate_reset_token(self, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'reset': self.id}) def reset_password(self, token, new_password): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except: return False if data.get('reset') != self.id: return False self.password = new_password db.session.add(self) return True def generate_email_change_token(self, new_email, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'change_email': self.id, 'new_email': new_email}) def change_email(self, token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except: return False if data.get('change_email') != self.id: return False new_email = data.get('new_email') if new_email is None: return False if self.query.filter_by(email=new_email).first() is not None: return False self.email = new_email self.avatar_hash = hashlib.md5( self.email.encode('utf-8')).hexdigest() db.session.add(self) return True def can(self, permissions): return self.role is not None and \ (self.role.permissions & permissions) == permissions def is_administrator(self): return self.can(Permission.ADMINISTER) def ping(self): self.last_seen = datetime.utcnow() db.session.add(self) def gravatar(self, size=100, default='identicon', rating='g'): if request.is_secure: url = 'https://secure.gravatar.com/avatar' else: url = 'http://www.gravatar.com/avatar' hash = self.avatar_hash or hashlib.md5( self.email.encode('utf-8')).hexdigest() return '{url}/{hash}?s={size}&d={default}&r={rating}'.format( url=url, hash=hash, size=size, default=default, rating=rating) def __repr__(self): return '用户: %r' % self.username class AnonymousUser(AnonymousUserMixin): def can(self, permissions): return False def is_administrator(self): return False login_manager.anonymous_user = AnonymousUser @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text) body_html = db.Column(db.Text) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) author_id = db.Column(db.Integer, db.ForeignKey('users.id')) @staticmethod def generate_fake(count=100): from random import seed, randint import forgery_py seed() user_count = User.query.count() for i in range(count): u = User.query.offset(randint(0, user_count - 1)).first() p = Post(body=forgery_py.lorem_ipsum.sentences(randint(1, 5)), timestamp=forgery_py.date.date(True), author=u) db.session.add(p) db.session.commit() @staticmethod def on_changed_body(target, value, oldvalue, initiator): allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul', 'h1', 'h2', 'h3', 'p'] target.body_html = bleach.linkify(bleach.clean( markdown(value, output_format='html'), tags=allowed_tags, strip=True)) db.event.listen(Post.body, 'set', Post.on_changed_body) class Checklist(db.Model): __tablename__ = 'checklists' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) station_id = db.Column(db.Integer, db.ForeignKey('stations.id')) level_id = db.Column(db.Integer) def __repr__(self): return '<Checklist %r>' % self.name class Cert(db.Model): __tablename__ = 'certs' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) level_id = db.Column(db.Integer, db.ForeignKey('levels.id')) station_id = db.Column(db.Integer, db.ForeignKey('stations.id')) apply_validation = db.Column(db.Boolean, default=False) cert_date = db.Column(db.DateTime()) get_cert = db.Column(db.Boolean, default=False) supervisor = db.Column(db.String(64)) comment = db.Column(db.Text) outdate = db.Column(db.Boolean, default=False) username = db.Column(db.String(64)) def __repr__(self): return '<cert %r>' % self.id
mit
kurli/blink-crosswalk
Source/devtools/scripts/jsdoc-validator/run_tests.py
54
2142
#!/usr/bin/python import hashlib import operator import os import shutil import stat import subprocess import sys import tempfile def rel_to_abs(rel_path): return os.path.join(script_path, rel_path) java_exec = 'java -Xms1024m -server -XX:+TieredCompilation' tests_dir = 'tests' jar_name = 'jsdoc-validator.jar' script_path = os.path.dirname(os.path.abspath(__file__)) tests_path = rel_to_abs(tests_dir) validator_jar_file = rel_to_abs(jar_name) golden_file = os.path.join(tests_path, 'golden.dat') test_files = [os.path.join(tests_path, f) for f in os.listdir(tests_path) if f.endswith('.js') and os.path.isfile(os.path.join(tests_path, f))] validator_command = "%s -jar %s %s" % (java_exec, validator_jar_file, " ".join(sorted(test_files))) def run_and_communicate(command, error_template): proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) (out, _) = proc.communicate() if proc.returncode: print >> sys.stderr, error_template % proc.returncode sys.exit(proc.returncode) return out def help(): print 'usage: %s [option]' % os.path.basename(__file__) print 'Options:' print '--generate-golden: Re-generate golden file' print '--dump: Dump the test results to stdout' def main(): need_golden = False need_dump = False if len(sys.argv) > 1: if sys.argv[1] == '--generate-golden': need_golden = True elif sys.argv[1] == '--dump': need_dump = True else: help() return result = run_and_communicate(validator_command, "Error running validator: %d") result = result.replace(script_path, "") # pylint: disable=E1103 if need_dump: print result return if need_golden: with open(golden_file, 'wt') as golden: golden.write(result) else: with open(golden_file, 'rt') as golden: golden_text = golden.read() if golden_text == result: print 'OK' else: print 'ERROR: Golden output mismatch' if __name__ == '__main__': main()
bsd-3-clause
home-assistant/home-assistant
homeassistant/components/system_bridge/config_flow.py
2
6605
"""Config flow for System Bridge integration.""" from __future__ import annotations import logging from typing import Any import async_timeout from systembridge import Bridge from systembridge.client import BridgeClient from systembridge.exceptions import BridgeAuthenticationException from systembridge.objects.os import Os from systembridge.objects.system import System import voluptuous as vol from homeassistant import config_entries, exceptions from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResult from homeassistant.helpers import aiohttp_client, config_validation as cv from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from .const import BRIDGE_CONNECTION_ERRORS, DOMAIN _LOGGER = logging.getLogger(__name__) STEP_AUTHENTICATE_DATA_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): cv.string}) STEP_USER_DATA_SCHEMA = vol.Schema( { vol.Required(CONF_HOST): cv.string, vol.Required(CONF_PORT, default=9170): cv.string, vol.Required(CONF_API_KEY): cv.string, } ) async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]: """Validate the user input allows us to connect. Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. """ bridge = Bridge( BridgeClient(aiohttp_client.async_get_clientsession(hass)), f"http://{data[CONF_HOST]}:{data[CONF_PORT]}", data[CONF_API_KEY], ) hostname = data[CONF_HOST] try: async with async_timeout.timeout(30): bridge_os: Os = await bridge.async_get_os() if bridge_os.hostname is not None: hostname = bridge_os.hostname bridge_system: System = await bridge.async_get_system() except BridgeAuthenticationException as exception: _LOGGER.info(exception) raise InvalidAuth from exception except BRIDGE_CONNECTION_ERRORS as exception: _LOGGER.info(exception) raise CannotConnect from exception return {"hostname": hostname, "uuid": bridge_system.uuid.os} class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for System Bridge.""" VERSION = 1 CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL def __init__(self): """Initialize flow.""" self._name: str | None = None self._input: dict[str, Any] = {} self._reauth = False async def _async_get_info( self, user_input: dict[str, Any] ) -> tuple[dict[str, str], dict[str, str] | None]: errors = {} try: info = await validate_input(self.hass, user_input) except CannotConnect: errors["base"] = "cannot_connect" except InvalidAuth: errors["base"] = "invalid_auth" except Exception: # pylint: disable=broad-except _LOGGER.exception("Unexpected exception") errors["base"] = "unknown" else: return errors, info return errors, None async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle the initial step.""" if user_input is None: return self.async_show_form( step_id="user", data_schema=STEP_USER_DATA_SCHEMA ) errors, info = await self._async_get_info(user_input) if not errors and info is not None: # Check if already configured await self.async_set_unique_id(info["uuid"], raise_on_progress=False) self._abort_if_unique_id_configured(updates={CONF_HOST: info["hostname"]}) return self.async_create_entry(title=info["hostname"], data=user_input) return self.async_show_form( step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors ) async def async_step_authenticate( self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle getting the api-key for authentication.""" errors: dict[str, str] = {} if user_input is not None: user_input = {**self._input, **user_input} errors, info = await self._async_get_info(user_input) if not errors and info is not None: # Check if already configured existing_entry = await self.async_set_unique_id(info["uuid"]) if self._reauth and existing_entry: self.hass.config_entries.async_update_entry( existing_entry, data=user_input ) await self.hass.config_entries.async_reload(existing_entry.entry_id) return self.async_abort(reason="reauth_successful") self._abort_if_unique_id_configured( updates={CONF_HOST: info["hostname"]} ) return self.async_create_entry(title=info["hostname"], data=user_input) return self.async_show_form( step_id="authenticate", data_schema=STEP_AUTHENTICATE_DATA_SCHEMA, description_placeholders={"name": self._name}, errors=errors, ) async def async_step_zeroconf( self, discovery_info: DiscoveryInfoType ) -> FlowResult: """Handle zeroconf discovery.""" host = discovery_info["properties"].get("ip") uuid = discovery_info["properties"].get("uuid") if host is None or uuid is None: return self.async_abort(reason="unknown") # Check if already configured await self.async_set_unique_id(uuid) self._abort_if_unique_id_configured(updates={CONF_HOST: host}) self._name = host self._input = { CONF_HOST: host, CONF_PORT: discovery_info["properties"].get("port"), } return await self.async_step_authenticate() async def async_step_reauth(self, entry_data: ConfigType) -> FlowResult: """Perform reauth upon an API authentication error.""" self._name = entry_data[CONF_HOST] self._input = { CONF_HOST: entry_data[CONF_HOST], CONF_PORT: entry_data[CONF_PORT], } self._reauth = True return await self.async_step_authenticate() class CannotConnect(exceptions.HomeAssistantError): """Error to indicate we cannot connect.""" class InvalidAuth(exceptions.HomeAssistantError): """Error to indicate there is invalid auth."""
apache-2.0
Anaconda-Platform/anaconda-client
binstar_client/commands/authorizations.py
1
8772
""" Manage Authentication tokens See also: * [Using Anaconda Cloud Tokens](http://docs.anaconda.org/using.html#Tokens) """ from __future__ import print_function import argparse import getpass import logging import socket import sys from argparse import FileType from datetime import datetime import pytz from dateutil.parser import parse as parse_date from six.moves import input from binstar_client import errors from binstar_client.utils import get_server_api logger = logging.getLogger('binstar.auth') SCOPE_EXAMPLES = """ Examples To allow access to only conda downloads from your account you can run: anaconda auth --create --scopes 'repos conda:download' To allow full access to your account: anaconda auth --create --scopes 'all' """ def utcnow(): now = datetime.utcnow() return now.replace(tzinfo=pytz.utc) def format_timedelta(date, expired=True): if not date: return 'Never' now = utcnow() if date < now: if expired: return 'expired' else: tmp = date date = now now = tmp delta = date - now if delta.days: days = (delta.days + (delta.seconds / (60. * 60. * 24.0))) if days > 3: days = int(days) return '%i days' % days else: return '%.1f days' % days elif delta.seconds > 60 * 60: return '%.1f hours' % (delta.seconds / (60. * 60)) elif delta.seconds > 60: return '%i minutes' % (delta.seconds // 60) else: return '%i seconds' % delta.seconds def show_auths(authentications): header = {'id': 'ID', 'application': 'Application', 'remote_addr':'Remote Addr', 'hostname':'Host', 'expires':'Expires In', 'scopes':'Scopes'} template = '%(id)-25s | %(application)-35s | %(remote_addr)-20s | %(hostname)-25s | %(expires)-15s | %(scopes)-25s' logger.info('') logger.info(template % header) logger.info('%s-+-%s-+-%s-+-%s-+-%s-+-%s' % ('-' * 25, '-' * 35, '-' * 20, '-' * 25, '-' * 15, '-' * 25)) for auth in authentications: if auth['expires']: expires = parse_date(auth['expires']) else: expires = None auth['expires'] = format_timedelta(expires) first_time = True scope_items = auth['scopes'] if scope_items: for scope in scope_items: if first_time: auth['scopes'] = scope logger.info(template % auth) first_time = False else: auth['id'] = '' auth['application'] = '' auth['remote_addr'] = '' auth['hostname'] = '' auth['expires'] = '' auth['scopes'] = scope logger.info(template % auth) else: auth['scopes'] = 'NO_SCOPE' logger.info(template % auth) def main(args): aserver_api = get_server_api(args.token, args.site) if args.info: data = aserver_api.authentication() logger.info('Name: %s' % data['application']) logger.info('Id: %s' % data['id']) if args.list: show_auths(aserver_api.authentications()) return elif args.remove: for auth_name in args.remove: aserver_api.remove_authentication(auth_name, args.organization) logger.info("Removed token %s" % auth_name) return elif args.list_scopes: scopes = aserver_api.list_scopes() for key in sorted(scopes): logger.info(key) logger.info(' ' + scopes[key]) logger.info('') logger.info(SCOPE_EXAMPLES) elif args.create: auth_type = aserver_api.authentication_type() try: current_user = aserver_api.user() username = current_user['login'] except: if auth_type == 'kerberos': logger.error("Kerberos authentication needed, please use 'anaconda login' to authenticate") return current_user = None sys.stderr.write('Username: ') sys.stderr.flush() username = input('') scopes = [scope for scopes in args.scopes for scope in scopes.split()] if not scopes: logger.warning("You have not specified the scope of this token with the '--scopes' argument.") logger.warning("This token will grant full access to %s's account" % (args.organization or username)) logger.warning("Use the --list-scopes option to see a listing of your options") for _ in range(3): try: if auth_type == 'kerberos': token = aserver_api._authenticate( None, args.name, application_url=args.url, scopes=scopes, for_user=args.organization, max_age=args.max_age, created_with=' '.join(sys.argv), strength=args.strength, fail_if_already_exists=True ) else: sys.stderr.write("Please re-enter %s's " % username) password = getpass.getpass() token = aserver_api.authenticate( username, password, args.name, application_url=args.url, scopes=scopes, for_user=args.organization, max_age=args.max_age, created_with=' '.join(sys.argv), strength=args.strength, fail_if_already_exists=True ) args.out.write(token) break except errors.Unauthorized: logger.error('Invalid Username password combination, please try again') continue def add_parser(subparsers): description = 'Manage Authorization Tokens' parser = subparsers.add_parser('auth', help=description, description=description, epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('-n', '--name', default='binstar_token:%s' % (socket.gethostname()), help='A unique name so you can identify this token later. View your tokens at anaconda.org/settings/access') parser.add_argument('-o', '--org', '--organization', help='Set the token owner (must be an organization)', dest='organization') g = parser.add_argument_group('token creation arguments', 'These arguments are only valid with the `--create` action') g.add_argument('--strength', choices=['strong', 'weak'], default='strong', dest='strength') g.add_argument('--strong', action='store_const', const='strong', dest='strength' , help='Create a longer token (default)') g.add_argument('-w', '--weak', action='store_const', const='weak', dest='strength', help='Create a shorter token') g.add_argument('--url', default='http://anaconda.org', help='The url of the application that will use this token') g.add_argument('--max-age', type=int, help='The maximum age in seconds that this token will be valid for') g.add_argument('-s', '--scopes', action='append', help=('Scopes for token. ' 'For example if you want to limit this token to conda downloads only you would use ' '--scopes "repo conda:download"'), default=[]) g.add_argument('--out', default=sys.stdout, type=FileType('w')) group = parser.add_argument_group("actions") group = group.add_mutually_exclusive_group(required=True) group.add_argument('-x', '--list-scopes', action='store_true', help='list all authentication scopes') group.add_argument('-l', '--list', action='store_true', help='list all user authentication tokens') group.add_argument('-r', '--remove', metavar='NAME', nargs='+', help='remove authentication tokens') group.add_argument('-c', '--create', action='store_true', help='Create an authentication token') group.add_argument('-i', '--info', '--current-info', dest='info', action='store_true', help='Show information about the current authentication token') parser.set_defaults(main=main)
bsd-3-clause
0x90sled/catapult
third_party/Paste/paste/evalexception/middleware.py
50
22313
# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org) # Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php """ Exception-catching middleware that allows interactive debugging. This middleware catches all unexpected exceptions. A normal traceback, like produced by ``paste.exceptions.errormiddleware.ErrorMiddleware`` is given, plus controls to see local variables and evaluate expressions in a local context. This can only be used in single-process environments, because subsequent requests must go back to the same process that the exception originally occurred in. Threaded or non-concurrent environments both work. This shouldn't be used in production in any way. That would just be silly. If calling from an XMLHttpRequest call, if the GET variable ``_`` is given then it will make the response more compact (and less Javascripty), since if you use innerHTML it'll kill your browser. You can look for the header X-Debug-URL in your 500 responses if you want to see the full debuggable traceback. Also, this URL is printed to ``wsgi.errors``, so you can open it up in another browser window. """ from __future__ import print_function import sys import os import cgi import traceback import six from six.moves import cStringIO as StringIO import pprint import itertools import time import re from paste.exceptions import errormiddleware, formatter, collector from paste import wsgilib from paste import urlparser from paste import httpexceptions from paste import registry from paste import request from paste import response from paste.evalexception import evalcontext limit = 200 def html_quote(v): """ Escape HTML characters, plus translate None to '' """ if v is None: return '' return cgi.escape(str(v), 1) def preserve_whitespace(v, quote=True): """ Quote a value for HTML, preserving whitespace (translating newlines to ``<br>`` and multiple spaces to use ``&nbsp;``). If ``quote`` is true, then the value will be HTML quoted first. """ if quote: v = html_quote(v) v = v.replace('\n', '<br>\n') v = re.sub(r'()( +)', _repl_nbsp, v) v = re.sub(r'(\n)( +)', _repl_nbsp, v) v = re.sub(r'^()( +)', _repl_nbsp, v) return '<code>%s</code>' % v def _repl_nbsp(match): if len(match.group(2)) == 1: return '&nbsp;' return match.group(1) + '&nbsp;' * (len(match.group(2))-1) + ' ' def simplecatcher(application): """ A simple middleware that catches errors and turns them into simple tracebacks. """ def simplecatcher_app(environ, start_response): try: return application(environ, start_response) except: out = StringIO() traceback.print_exc(file=out) start_response('500 Server Error', [('content-type', 'text/html')], sys.exc_info()) res = out.getvalue() return ['<h3>Error</h3><pre>%s</pre>' % html_quote(res)] return simplecatcher_app def wsgiapp(): """ Turns a function or method into a WSGI application. """ def decorator(func): def wsgiapp_wrapper(*args): # we get 3 args when this is a method, two when it is # a function :( if len(args) == 3: environ = args[1] start_response = args[2] args = [args[0]] else: environ, start_response = args args = [] def application(environ, start_response): form = wsgilib.parse_formvars(environ, include_get_vars=True) headers = response.HeaderDict( {'content-type': 'text/html', 'status': '200 OK'}) form['environ'] = environ form['headers'] = headers res = func(*args, **form.mixed()) status = headers.pop('status') start_response(status, headers.headeritems()) return [res] app = httpexceptions.make_middleware(application) app = simplecatcher(app) return app(environ, start_response) wsgiapp_wrapper.exposed = True return wsgiapp_wrapper return decorator def get_debug_info(func): """ A decorator (meant to be used under ``wsgiapp()``) that resolves the ``debugcount`` variable to a ``DebugInfo`` object (or gives an error if it can't be found). """ def debug_info_replacement(self, **form): try: if 'debugcount' not in form: raise ValueError('You must provide a debugcount parameter') debugcount = form.pop('debugcount') try: debugcount = int(debugcount) except ValueError: raise ValueError('Bad value for debugcount') if debugcount not in self.debug_infos: raise ValueError( 'Debug %s no longer found (maybe it has expired?)' % debugcount) debug_info = self.debug_infos[debugcount] return func(self, debug_info=debug_info, **form) except ValueError as e: form['headers']['status'] = '500 Server Error' return '<html>There was an error: %s</html>' % html_quote(e) return debug_info_replacement debug_counter = itertools.count(int(time.time())) def get_debug_count(environ): """ Return the unique debug count for the current request """ if 'paste.evalexception.debug_count' in environ: return environ['paste.evalexception.debug_count'] else: environ['paste.evalexception.debug_count'] = next = six.next(debug_counter) return next class EvalException(object): def __init__(self, application, global_conf=None, xmlhttp_key=None): self.application = application self.debug_infos = {} if xmlhttp_key is None: if global_conf is None: xmlhttp_key = '_' else: xmlhttp_key = global_conf.get('xmlhttp_key', '_') self.xmlhttp_key = xmlhttp_key def __call__(self, environ, start_response): assert not environ['wsgi.multiprocess'], ( "The EvalException middleware is not usable in a " "multi-process environment") environ['paste.evalexception'] = self if environ.get('PATH_INFO', '').startswith('/_debug/'): return self.debug(environ, start_response) else: return self.respond(environ, start_response) def debug(self, environ, start_response): assert request.path_info_pop(environ) == '_debug' next_part = request.path_info_pop(environ) method = getattr(self, next_part, None) if not method: exc = httpexceptions.HTTPNotFound( '%r not found when parsing %r' % (next_part, wsgilib.construct_url(environ))) return exc.wsgi_application(environ, start_response) if not getattr(method, 'exposed', False): exc = httpexceptions.HTTPForbidden( '%r not allowed' % next_part) return exc.wsgi_application(environ, start_response) return method(environ, start_response) def media(self, environ, start_response): """ Static path where images and other files live """ app = urlparser.StaticURLParser( os.path.join(os.path.dirname(__file__), 'media')) return app(environ, start_response) media.exposed = True def mochikit(self, environ, start_response): """ Static path where MochiKit lives """ app = urlparser.StaticURLParser( os.path.join(os.path.dirname(__file__), 'mochikit')) return app(environ, start_response) mochikit.exposed = True def summary(self, environ, start_response): """ Returns a JSON-format summary of all the cached exception reports """ start_response('200 OK', [('Content-type', 'text/x-json')]) data = []; items = self.debug_infos.values() items.sort(lambda a, b: cmp(a.created, b.created)) data = [item.json() for item in items] return [repr(data)] summary.exposed = True def view(self, environ, start_response): """ View old exception reports """ id = int(request.path_info_pop(environ)) if id not in self.debug_infos: start_response( '500 Server Error', [('Content-type', 'text/html')]) return [ "Traceback by id %s does not exist (maybe " "the server has been restarted?)" % id] debug_info = self.debug_infos[id] return debug_info.wsgi_application(environ, start_response) view.exposed = True def make_view_url(self, environ, base_path, count): return base_path + '/_debug/view/%s' % count #@wsgiapp() #@get_debug_info def show_frame(self, tbid, debug_info, **kw): frame = debug_info.frame(int(tbid)) vars = frame.tb_frame.f_locals if vars: registry.restorer.restoration_begin(debug_info.counter) local_vars = make_table(vars) registry.restorer.restoration_end() else: local_vars = 'No local vars' return input_form(tbid, debug_info) + local_vars show_frame = wsgiapp()(get_debug_info(show_frame)) #@wsgiapp() #@get_debug_info def exec_input(self, tbid, debug_info, input, **kw): if not input.strip(): return '' input = input.rstrip() + '\n' frame = debug_info.frame(int(tbid)) vars = frame.tb_frame.f_locals glob_vars = frame.tb_frame.f_globals context = evalcontext.EvalContext(vars, glob_vars) registry.restorer.restoration_begin(debug_info.counter) output = context.exec_expr(input) registry.restorer.restoration_end() input_html = formatter.str2html(input) return ('<code style="color: #060">&gt;&gt;&gt;</code> ' '<code>%s</code><br>\n%s' % (preserve_whitespace(input_html, quote=False), preserve_whitespace(output))) exec_input = wsgiapp()(get_debug_info(exec_input)) def respond(self, environ, start_response): if environ.get('paste.throw_errors'): return self.application(environ, start_response) base_path = request.construct_url(environ, with_path_info=False, with_query_string=False) environ['paste.throw_errors'] = True started = [] def detect_start_response(status, headers, exc_info=None): try: return start_response(status, headers, exc_info) except: raise else: started.append(True) try: __traceback_supplement__ = errormiddleware.Supplement, self, environ app_iter = self.application(environ, detect_start_response) try: return_iter = list(app_iter) return return_iter finally: if hasattr(app_iter, 'close'): app_iter.close() except: exc_info = sys.exc_info() for expected in environ.get('paste.expected_exceptions', []): if isinstance(exc_info[1], expected): raise # Tell the Registry to save its StackedObjectProxies current state # for later restoration registry.restorer.save_registry_state(environ) count = get_debug_count(environ) view_uri = self.make_view_url(environ, base_path, count) if not started: headers = [('content-type', 'text/html')] headers.append(('X-Debug-URL', view_uri)) start_response('500 Internal Server Error', headers, exc_info) msg = 'Debug at: %s\n' % view_uri if six.PY3: msg = msg.encode('utf8') environ['wsgi.errors'].write(msg) exc_data = collector.collect_exception(*exc_info) debug_info = DebugInfo(count, exc_info, exc_data, base_path, environ, view_uri) assert count not in self.debug_infos self.debug_infos[count] = debug_info if self.xmlhttp_key: get_vars = request.parse_querystring(environ) if dict(get_vars).get(self.xmlhttp_key): exc_data = collector.collect_exception(*exc_info) html = formatter.format_html( exc_data, include_hidden_frames=False, include_reusable=False, show_extra_data=False) return [html] # @@: it would be nice to deal with bad content types here return debug_info.content() def exception_handler(self, exc_info, environ): simple_html_error = False if self.xmlhttp_key: get_vars = request.parse_querystring(environ) if dict(get_vars).get(self.xmlhttp_key): simple_html_error = True return errormiddleware.handle_exception( exc_info, environ['wsgi.errors'], html=True, debug_mode=True, simple_html_error=simple_html_error) class DebugInfo(object): def __init__(self, counter, exc_info, exc_data, base_path, environ, view_uri): self.counter = counter self.exc_data = exc_data self.base_path = base_path self.environ = environ self.view_uri = view_uri self.created = time.time() self.exc_type, self.exc_value, self.tb = exc_info __exception_formatter__ = 1 self.frames = [] n = 0 tb = self.tb while tb is not None and (limit is None or n < limit): if tb.tb_frame.f_locals.get('__exception_formatter__'): # Stop recursion. @@: should make a fake ExceptionFrame break self.frames.append(tb) tb = tb.tb_next n += 1 def json(self): """Return the JSON-able representation of this object""" return { 'uri': self.view_uri, 'created': time.strftime('%c', time.gmtime(self.created)), 'created_timestamp': self.created, 'exception_type': str(self.exc_type), 'exception': str(self.exc_value), } def frame(self, tbid): for frame in self.frames: if id(frame) == tbid: return frame else: raise ValueError("No frame by id %s found from %r" % (tbid, self.frames)) def wsgi_application(self, environ, start_response): start_response('200 OK', [('content-type', 'text/html')]) return self.content() def content(self): html = format_eval_html(self.exc_data, self.base_path, self.counter) head_html = (formatter.error_css + formatter.hide_display_js) head_html += self.eval_javascript() repost_button = make_repost_button(self.environ) page = error_template % { 'repost_button': repost_button or '', 'head_html': head_html, 'body': html} if six.PY3: page = page.encode('utf8') return [page] def eval_javascript(self): base_path = self.base_path + '/_debug' return ( '<script type="text/javascript" src="%s/media/MochiKit.packed.js">' '</script>\n' '<script type="text/javascript" src="%s/media/debug.js">' '</script>\n' '<script type="text/javascript">\n' 'debug_base = %r;\n' 'debug_count = %r;\n' '</script>\n' % (base_path, base_path, base_path, self.counter)) class EvalHTMLFormatter(formatter.HTMLFormatter): def __init__(self, base_path, counter, **kw): super(EvalHTMLFormatter, self).__init__(**kw) self.base_path = base_path self.counter = counter def format_source_line(self, filename, frame): line = formatter.HTMLFormatter.format_source_line( self, filename, frame) return (line + ' <a href="#" class="switch_source" ' 'tbid="%s" onClick="return showFrame(this)">&nbsp; &nbsp; ' '<img src="%s/_debug/media/plus.jpg" border=0 width=9 ' 'height=9> &nbsp; &nbsp;</a>' % (frame.tbid, self.base_path)) def make_table(items): if isinstance(items, dict): items = items.items() items.sort() rows = [] i = 0 for name, value in items: i += 1 out = StringIO() try: pprint.pprint(value, out) except Exception as e: print('Error: %s' % e, file=out) value = html_quote(out.getvalue()) if len(value) > 100: # @@: This can actually break the HTML :( # should I truncate before quoting? orig_value = value value = value[:100] value += '<a class="switch_source" style="background-color: #999" href="#" onclick="return expandLong(this)">...</a>' value += '<span style="display: none">%s</span>' % orig_value[100:] value = formatter.make_wrappable(value) if i % 2: attr = ' class="even"' else: attr = ' class="odd"' rows.append('<tr%s style="vertical-align: top;"><td>' '<b>%s</b></td><td style="overflow: auto">%s<td></tr>' % (attr, html_quote(name), preserve_whitespace(value, quote=False))) return '<table>%s</table>' % ( '\n'.join(rows)) def format_eval_html(exc_data, base_path, counter): short_formatter = EvalHTMLFormatter( base_path=base_path, counter=counter, include_reusable=False) short_er = short_formatter.format_collected_data(exc_data) long_formatter = EvalHTMLFormatter( base_path=base_path, counter=counter, show_hidden_frames=True, show_extra_data=False, include_reusable=False) long_er = long_formatter.format_collected_data(exc_data) text_er = formatter.format_text(exc_data, show_hidden_frames=True) if short_formatter.filter_frames(exc_data.frames) != \ long_formatter.filter_frames(exc_data.frames): # Only display the full traceback when it differs from the # short version full_traceback_html = """ <br> <script type="text/javascript"> show_button('full_traceback', 'full traceback') </script> <div id="full_traceback" class="hidden-data"> %s </div> """ % long_er else: full_traceback_html = '' return """ %s %s <br> <script type="text/javascript"> show_button('text_version', 'text version') </script> <div id="text_version" class="hidden-data"> <textarea style="width: 100%%" rows=10 cols=60>%s</textarea> </div> """ % (short_er, full_traceback_html, cgi.escape(text_er)) def make_repost_button(environ): url = request.construct_url(environ) if environ['REQUEST_METHOD'] == 'GET': return ('<button onclick="window.location.href=%r">' 'Re-GET Page</button><br>' % url) else: # @@: I'd like to reconstruct this, but I can't because # the POST body is probably lost at this point, and # I can't get it back :( return None # @@: Use or lose the following code block """ fields = [] for name, value in wsgilib.parse_formvars( environ, include_get_vars=False).items(): if hasattr(value, 'filename'): # @@: Arg, we'll just submit the body, and leave out # the filename :( value = value.value fields.append( '<input type="hidden" name="%s" value="%s">' % (html_quote(name), html_quote(value))) return ''' <form action="%s" method="POST"> %s <input type="submit" value="Re-POST Page"> </form>''' % (url, '\n'.join(fields)) """ def input_form(tbid, debug_info): return ''' <form action="#" method="POST" onsubmit="return submitInput($(\'submit_%(tbid)s\'), %(tbid)s)"> <div id="exec-output-%(tbid)s" style="width: 95%%; padding: 5px; margin: 5px; border: 2px solid #000; display: none"></div> <input type="text" name="input" id="debug_input_%(tbid)s" style="width: 100%%" autocomplete="off" onkeypress="upArrow(this, event)"><br> <input type="submit" value="Execute" name="submitbutton" onclick="return submitInput(this, %(tbid)s)" id="submit_%(tbid)s" input-from="debug_input_%(tbid)s" output-to="exec-output-%(tbid)s"> <input type="submit" value="Expand" onclick="return expandInput(this)"> </form> ''' % {'tbid': tbid} error_template = ''' <html> <head> <title>Server Error</title> %(head_html)s </head> <body> <div id="error-area" style="display: none; background-color: #600; color: #fff; border: 2px solid black"> <div id="error-container"></div> <button onclick="return clearError()">clear this</button> </div> %(repost_button)s %(body)s </body> </html> ''' def make_eval_exception(app, global_conf, xmlhttp_key=None): """ Wraps the application in an interactive debugger. This debugger is a major security hole, and should only be used during development. xmlhttp_key is a string that, if present in QUERY_STRING, indicates that the request is an XMLHttp request, and the Javascript/interactive debugger should not be returned. (If you try to put the debugger somewhere with innerHTML, you will often crash the browser) """ if xmlhttp_key is None: xmlhttp_key = global_conf.get('xmlhttp_key', '_') return EvalException(app, xmlhttp_key=xmlhttp_key)
bsd-3-clause