# coding=utf-8 from emft.core import constant from emft.core.logging import make_logger from emft.gui.base import GridLayout, HSpacer, Label, VLayout, VSpacer from emft.gui.main_ui_tab_widget import MainUiTabChild LOGGER = make_logger(__name__) class TabChildAbout(MainUiTabChild): def tab_clicked(self): pass @property def tab_title(self) -> str: return 'About' def __init__(self, parent=None): super(TabChildAbout, self).__init__(parent) repo_label = Label( '''{link}'''.format(link=constant.LINK_REPO) ) repo_label.setOpenExternalLinks(True) changelog_label = Label( '''{link}'''.format(link=constant.LINK_CHANGELOG) ) changelog_label.setOpenExternalLinks(True) self.setLayout( VLayout( [ GridLayout( [ [Label('Github repository: '), repo_label, HSpacer()], [Label('Changelog: '), changelog_label, HSpacer()], ], [0, 0, 1] ), VSpacer(), ] ) ) # Original author: D. Eppstein, UC Irvine, August 12, 2003. # The original code at http://www.ics.uci.edu/~eppstein/PADS/ is public domain. # Copyright (C) 2004-2016 by # Aric Hagberg # Dan Schult # Pieter Swart # Tomas Gavenciak # All rights reserved. # BSD license. # # Authors: Tomas Gavenciak # Aric Hagberg """Functions for reading and writing graphs in the *graph6* format. The *graph6* file format is suitable for small graphs or large dense graphs. For large sparse graphs, use the *sparse6* format. For more information, see the `graph6`_ homepage. .. _graph6: http://users.cecs.anu.edu.au/~bdm/data/formats.html """ import networkx as nx from networkx.exception import NetworkXError from networkx.utils import open_file, not_implemented_for __all__ = ['read_graph6', 'parse_graph6', 'generate_graph6', 'write_graph6'] def parse_graph6(string): """Read a simple undirected graph in graph6 format from string. Parameters ---------- string : string Data in graph6 format Returns ------- G : Graph Raises ------ NetworkXError If the string is unable to be parsed in graph6 format Examples -------- >>> G = nx.parse_graph6('A_') >>> sorted(G.edges()) [(0, 1)] See Also -------- generate_graph6, read_graph6, write_graph6 References ---------- .. [1] Graph6 specification """ def bits(): """Return sequence of individual bits from 6-bit-per-value list of data values.""" for d in data: for i in [5,4,3,2,1,0]: yield (d>>i)&1 if string.startswith('>>graph6<<'): string = string[10:] data = graph6_to_data(string) n, data = data_to_n(data) nd = (n*(n-1)//2 + 5) // 6 if len(data) != nd: raise NetworkXError(\ 'Expected %d bits but got %d in graph6' % (n*(n-1)//2, len(data)*6)) G=nx.Graph() G.add_nodes_from(range(n)) for (i,j),b in zip([(i,j) for j in range(1,n) for i in range(j)], bits()): if b: G.add_edge(i,j) return G @open_file(0,mode='rt') def read_graph6(path): """Read simple undirected graphs in graph6 format from path. Parameters ---------- path : file or string File or filename to write. Returns ------- G : Graph or list of Graphs If the file contains multiple lines then a list of graphs is returned Raises ------ NetworkXError If the string is unable to be parsed in graph6 format Examples -------- >>> nx.write_graph6(nx.Graph([(0,1)]), 'test.g6') >>> G = nx.read_graph6('test.g6') >>> sorted(G.edges()) [(0, 1)] See Also -------- generate_graph6, parse_graph6, write_graph6 References ---------- .. [1] Graph6 specification """ glist = [] for line in path: line = line.strip() if not len(line): continue glist.append(parse_graph6(line)) if len(glist) == 1: return glist[0] else: return glist @not_implemented_for('directed','multigraph') def generate_graph6(G, nodes = None, header=True): """Generate graph6 format string from a simple undirected graph. Parameters ---------- G : Graph (undirected) nodes: list or iterable Nodes are labeled 0...n-1 in the order provided. If None the ordering given by G.nodes() is used. header: bool If True add '>>graph6<<' string to head of data Returns ------- s : string String in graph6 format Raises ------ NetworkXError If the graph is directed or has parallel edges Examples -------- >>> G = nx.Graph([(0, 1)]) >>> nx.generate_graph6(G) '>>graph6< """ if nodes is not None: G = G.subgraph(nodes) H = nx.convert_node_labels_to_integers(G) ns = sorted(H.nodes()) def bits(): for (i,j) in [(i,j) for j in range(1,n) for i in range(j)]: yield G.has_edge(ns[i],ns[j]) n = G.order() data = n_to_data(n) d = 0 flush = False for i, b in zip(range(n * n), bits()): d |= b << (5 - (i % 6)) flush = True if i % 6 == 5: data.append(d) d = 0 flush = False if flush: data.append(d) string_data = data_to_graph6(data) if header: string_data = '>>graph6<<' + string_data return string_data @open_file(1, mode='wt') def write_graph6(G, path, nodes = None, header=True): """Write a simple undirected graph to path in graph6 format. Parameters ---------- G : Graph (undirected) path : file or string File or filename to write. nodes: list or iterable Nodes are labeled 0...n-1 in the order provided. If None the ordering given by G.nodes() is used. header: bool If True add '>>graph6<<' string to head of data Raises ------ NetworkXError If the graph is directed or has parallel edges Examples -------- >>> G = nx.Graph([(0, 1)]) >>> nx.write_graph6(G, 'test.g6') See Also -------- generate_graph6, parse_graph6, read_graph6 Notes ----- The format does not support edge or node labels, parallel edges or self loops. If self loops are present they are silently ignored. References ---------- .. [1] Graph6 specification """ path.write(generate_graph6(G, nodes=nodes, header=header)) path.write('\n') # helper functions def graph6_to_data(string): """Convert graph6 character sequence to 6-bit integers.""" v = [ord(c)-63 for c in string] if len(v) > 0 and (min(v) < 0 or max(v) > 63): return None return v def data_to_graph6(data): """Convert 6-bit integer sequence to graph6 character sequence.""" if len(data) > 0 and (min(data) < 0 or max(data) > 63): raise NetworkXError("graph6 data units must be within 0..63") return ''.join([chr(d+63) for d in data]) def data_to_n(data): """Read initial one-, four- or eight-unit value from graph6 integer sequence. Return (value, rest of seq.)""" if data[0] <= 62: return data[0], data[1:] if data[1] <= 62: return (data[1]<<12) + (data[2]<<6) + data[3], data[4:] return ((data[2]<<30) + (data[3]<<24) + (data[4]<<18) + (data[5]<<12) + (data[6]<<6) + data[7], data[8:]) def n_to_data(n): """Convert an integer to one-, four- or eight-unit graph6 sequence.""" if n < 0: raise NetworkXError("Numbers in graph6 format must be non-negative.") if n <= 62: return [n] if n <= 258047: return [63, (n>>12) & 0x3f, (n>>6) & 0x3f, n & 0x3f] if n <= 68719476735: return [63, 63, (n>>30) & 0x3f, (n>>24) & 0x3f, (n>>18) & 0x3f, (n>>12) & 0x3f, (n>>6) & 0x3f, n & 0x3f] raise NetworkXError("Numbers above 68719476735 are not supported by graph6") def teardown_module(module): import os if os.path.isfile('test.g6'): os.unlink('test.g6') #!/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. """ Lexer for PPAPI IDL """ # # IDL Lexer # # The lexer is uses the PLY lex library to build a tokenizer which understands # WebIDL tokens. # # WebIDL, and WebIDL regular expressions can be found at: # http://dev.w3.org/2006/webapi/WebIDL/ # PLY can be found at: # http://www.dabeaz.com/ply/ import os.path import re import sys # # Try to load the ply module, if not, then assume it is in the third_party # directory, relative to ppapi # try: from ply import lex except: module_path, module_name = os.path.split(__file__) third_party = os.path.join(module_path, '..', '..', 'third_party') sys.path.append(third_party) from ply import lex from idl_option import GetOption, Option, ParseOptions Option('output', 'Generate output.') # # IDL Lexer # class IDLLexer(object): # 'tokens' is a value required by lex which specifies the complete list # of valid token types. tokens = [ # Symbol and keywords types 'COMMENT', 'DESCRIBE', 'ENUM', 'LABEL', 'SYMBOL', 'INLINE', 'INTERFACE', 'STRUCT', 'TYPEDEF', # Extra WebIDL keywords 'CALLBACK', 'DICTIONARY', 'OPTIONAL', 'STATIC', # Invented for apps use 'NAMESPACE', # Data types 'FLOAT', 'OCT', 'INT', 'HEX', 'STRING', # Operators 'LSHIFT', 'RSHIFT' ] # 'keywords' is a map of string to token type. All SYMBOL tokens are # matched against keywords, to determine if the token is actually a keyword. keywords = { 'describe' : 'DESCRIBE', 'enum' : 'ENUM', 'label' : 'LABEL', 'interface' : 'INTERFACE', 'readonly' : 'READONLY', 'struct' : 'STRUCT', 'typedef' : 'TYPEDEF', 'callback' : 'CALLBACK', 'dictionary' : 'DICTIONARY', 'optional' : 'OPTIONAL', 'static' : 'STATIC', 'namespace' : 'NAMESPACE', } # 'literals' is a value expected by lex which specifies a list of valid # literal tokens, meaning the token type and token value are identical. literals = '"*.(){}[],;:=+-/~|&^?' # Token definitions # # Lex assumes any value or function in the form of 't_' represents a # regular expression where a match will emit a token of type . In the # case of a function, the function is called when a match is made. These # definitions come from WebIDL. # 't_ignore' is a special match of items to ignore t_ignore = ' \t' # Constant values t_FLOAT = r'-?(\d+\.\d*|\d*\.\d+)([Ee][+-]?\d+)?|-?\d+[Ee][+-]?\d+' t_INT = r'-?[0-9]+[uU]?' t_OCT = r'-?0[0-7]+' t_HEX = r'-?0[Xx][0-9A-Fa-f]+' t_LSHIFT = r'<<' t_RSHIFT = r'>>' # A line ending '\n', we use this to increment the line number def t_LINE_END(self, t): r'\n+' self.AddLines(len(t.value)) # We do not process escapes in the IDL strings. Strings are exclusively # used for attributes, and not used as typical 'C' constants. def t_STRING(self, t): r'"[^"]*"' t.value = t.value[1:-1] self.AddLines(t.value.count('\n')) return t # A C or C++ style comment: /* xxx */ or // def t_COMMENT(self, t): r'(/\*(.|\n)*?\*/)|(//.*(\n[ \t]*//.*)*)' self.AddLines(t.value.count('\n')) return t # Return a "preprocessor" inline block def t_INLINE(self, t): r'\#inline (.|\n)*?\#endinl.*' self.AddLines(t.value.count('\n')) return t # A symbol or keyword. def t_KEYWORD_SYMBOL(self, t): r'_?[A-Za-z][A-Za-z_0-9]*' # All non-keywords are assumed to be symbols t.type = self.keywords.get(t.value, 'SYMBOL') # We strip leading underscores so that you can specify symbols with the same # value as a keywords (E.g. a dictionary named 'interface'). if t.value[0] == '_': t.value = t.value[1:] return t def t_ANY_error(self, t): msg = "Unrecognized input" line = self.lexobj.lineno # If that line has not been accounted for, then we must have hit # EoF, so compute the beginning of the line that caused the problem. if line >= len(self.index): # Find the offset in the line of the first word causing the issue word = t.value.split()[0] offs = self.lines[line - 1].find(word) # Add the computed line's starting position self.index.append(self.lexobj.lexpos - offs) msg = "Unexpected EoF reached after" pos = self.lexobj.lexpos - self.index[line] file = self.lexobj.filename out = self.ErrorMessage(file, line, pos, msg) sys.stderr.write(out + '\n') self.lex_errors += 1 def AddLines(self, count): # Set the lexer position for the beginning of the next line. In the case # of multiple lines, tokens can not exist on any of the lines except the # last one, so the recorded value for previous lines are unused. We still # fill the array however, to make sure the line count is correct. self.lexobj.lineno += count for i in range(count): self.index.append(self.lexobj.lexpos) def FileLineMsg(self, file, line, msg): if file: return "%s(%d) : %s" % (file, line + 1, msg) return " : %s" % msg def SourceLine(self, file, line, pos): caret = '\t^'.expandtabs(pos) # We decrement the line number since the array is 0 based while the # line numbers are 1 based. return "%s\n%s" % (self.lines[line - 1], caret) def ErrorMessage(self, file, line, pos, msg): return "\n%s\n%s" % ( self.FileLineMsg(file, line, msg), self.SourceLine(file, line, pos)) def SetData(self, filename, data): # Start with line 1, not zero self.lexobj.lineno = 1 self.lexobj.filename = filename self.lines = data.split('\n') self.index = [0] self.lexobj.input(data) self.lex_errors = 0 def __init__(self): self.lexobj = lex.lex(object=self, lextab=None, optimize=0) # # FilesToTokens # # From a set of source file names, generate a list of tokens. # def FilesToTokens(filenames, verbose=False): lexer = IDLLexer() outlist = [] for filename in filenames: data = open(filename).read() lexer.SetData(filename, data) if verbose: sys.stdout.write(' Loaded %s...\n' % filename) while 1: t = lexer.lexobj.token() if t is None: break outlist.append(t) return outlist def TokensFromText(text): lexer = IDLLexer() lexer.SetData('unknown', text) outlist = [] while 1: t = lexer.lexobj.token() if t is None: break outlist.append(t.value) return outlist # # TextToTokens # # From a block of text, generate a list of tokens # def TextToTokens(source): lexer = IDLLexer() outlist = [] lexer.SetData('AUTO', source) while 1: t = lexer.lexobj.token() if t is None: break outlist.append(t.value) return outlist # # TestSame # # From a set of token values, generate a new source text by joining with a # single space. The new source is then tokenized and compared against the # old set. # def TestSame(values1): # Recreate the source from the tokens. We use newline instead of whitespace # since the '//' and #inline regex are line sensitive. text = '\n'.join(values1) values2 = TextToTokens(text) count1 = len(values1) count2 = len(values2) if count1 != count2: print "Size mismatch original %d vs %d\n" % (count1, count2) if count1 > count2: count1 = count2 for i in range(count1): if values1[i] != values2[i]: print "%d >>%s<< >>%s<<" % (i, values1[i], values2[i]) if GetOption('output'): sys.stdout.write('Generating original.txt and tokenized.txt\n') open('original.txt', 'w').write(src1) open('tokenized.txt', 'w').write(src2) if values1 == values2: sys.stdout.write('Same: Pass\n') return 0 print "****************\n%s\n%s***************\n" % (src1, src2) sys.stdout.write('Same: Failed\n') return -1 # # TestExpect # # From a set of tokens pairs, verify the type field of the second matches # the value of the first, so that: # INT 123 FLOAT 1.1 # will generate a passing test, where the first token is the SYMBOL INT, # and the second token is the INT 123, third token is the SYMBOL FLOAT and # the fourth is the FLOAT 1.1, etc... def TestExpect(tokens): count = len(tokens) index = 0 errors = 0 while index < count: type = tokens[index].value token = tokens[index + 1] index += 2 if type != token.type: sys.stderr.write('Mismatch: Expected %s, but got %s = %s.\n' % (type, token.type, token.value)) errors += 1 if not errors: sys.stdout.write('Expect: Pass\n') return 0 sys.stdout.write('Expect: Failed\n') return -1 def Main(args): filenames = ParseOptions(args) try: tokens = FilesToTokens(filenames, GetOption('verbose')) values = [tok.value for tok in tokens] if GetOption('output'): sys.stdout.write(' <> '.join(values) + '\n') if GetOption('test'): if TestSame(values): return -1 if TestExpect(tokens): return -1 return 0 except lex.LexError as le: sys.stderr.write('%s\n' % str(le)) return -1 if __name__ == '__main__': sys.exit(Main(sys.argv[1:])) import gff3toembl from gt import CustomVisitor from gff3toembl.EMBLContig import EMBLContig class EMBLConverter(CustomVisitor): def __init__(self, locus_tag=None, translation_table=11): CustomVisitor.__init__(self) self.contigs = {} self.locus_tag = locus_tag self.translation_table = translation_table def visit_feature_node(self, feature_node): sequence_id = feature_node.get_seqid() contig = self.contigs.get(sequence_id) if contig: # contig already exists, just try and update it contig.add_feature(sequence_id = sequence_id, feature_type = feature_node.get_type(), start = feature_node.get_start(), end = feature_node.get_end(), strand = feature_node.get_strand(), feature_attributes = feature_node.attribs, locus_tag = self.locus_tag, translation_table = self.translation_table) else: contig = EMBLContig() successfully_added_feature = contig.add_feature(sequence_id = sequence_id, feature_type = feature_node.get_type(), start = feature_node.get_start(), end = feature_node.get_end(), strand = feature_node.get_strand(), feature_attributes = feature_node.attribs, locus_tag = self.locus_tag, translation_table = self.translation_table) if successfully_added_feature: self.contigs[sequence_id] = contig else: pass # discard the contig because we didn't add a feature so it is empty def visit_region_node(self, region_node): pass # for now def visit_comment_node(self, comment_node): pass # for now def visit_sequence_node(self, sequence_node): sequence_id = sequence_node.get_description() contig = self.contigs.setdefault(sequence_id, EMBLContig()) contig.add_sequence(sequence_node.get_sequence()) # This file is part of GridCal. # # GridCal 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. # # GridCal 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 GridCal. If not, see . import numpy as np def slice_to_range(sl: slice, n): """ Turn a slice into a range :param sl: slice object :param n: total number of items :return: range object, if the slice is not supported an exception is raised """ if sl.start is None and sl.step is None and sl.start is None: # (:) return range(n) elif sl.start is not None and sl.step is None and sl.start is None: # (a:) return range(sl.start, n) elif sl.start is not None and sl.step is not None and sl.start is None: # (?) raise Exception('Invalid slice') elif sl.start is not None and sl.step is None and sl.start is not None: # (a:b) return range(sl.start, sl.stop) elif sl.start is not None and sl.step is not None and sl.start is not None: # (a:s:b) return range(sl.start, sl.stop, sl.step) elif sl.start is None and sl.step is None and sl.start is not None: # (:b) return range(sl.stop) else: raise Exception('Invalid slice') def dense_to_str(mat: np.ndarray): """ Turn dense 2D numpy array into a string :param mat: 2D numpy array :return: string """ rows, cols = mat.shape val = "Matrix (" + ("%d" % rows) + " x " + ("%d" % cols) + ")\n" val += str(mat).replace('. ', ' ').replace('[', ' ').replace(']', '').replace('0 ', '_ ').replace('0.', '_ ') # for i in range(0, rows): # for j in range(0, cols): # x = mat[i, j] # if x is not None: # if x == 0: # val += '{:<4}'.format(0) # else: # val += '{:<4}'.format(x) # else: # val += "" # val += '\n' # for rows in M: # print(*['{:<4}'.format(each) for each in rows]) return val #!/usr/bin/python import sys, os.path, random import json from mwm import MWM if len(sys.argv) < 2: print('Dumps some MWM structures.') print('Usage: {0} '.format(sys.argv[0])) sys.exit(1) mwm = MWM(open(sys.argv[1], 'rb')) mwm.read_types(os.path.join(os.path.dirname(sys.argv[0]), '..', '..', '..', 'data', 'types.txt')) print('Tags:') tvv = sorted([(k, v[0], v[1]) for k, v in mwm.tags.items()], key=lambda x: x[1]) for tv in tvv: print(' {0:<8}: offs {1:9} len {2:8}'.format(tv[0], tv[1], tv[2])) v = mwm.read_version() print('Format: {0}, version: {1}'.format(v['fmt'], v['date'].strftime('%Y-%m-%d %H:%M'))) print('Header: {0}'.format(mwm.read_header())) print('Region Info: {0}'.format(mwm.read_region_info())) print('Metadata count: {0}'.format(len(mwm.read_metadata()))) cross = mwm.read_crossmwm() if cross: print('Outgoing points: {0}, incoming: {1}'.format(len(cross['out']), len(cross['in']))) print('Outgoing regions: {0}'.format(set(cross['neighbours']))) # Print some random features using reservoir sampling count = 5 sample = [] for i, feature in enumerate(mwm.iter_features()): if i < count: sample.append(feature) elif random.randint(0, i) < count: sample[random.randint(0, count-1)] = feature print('Feature count: {0}'.format(i)) print('Sample features:') for feature in sample: print(json.dumps(feature, ensure_ascii=False)) #!/usr/bin/env python # # Electrum - lightweight Bitcoin client # Copyright (C) 2012 thomasv@gitorious # # 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 . import threading, re, socket import webbrowser import requests from PyQt4.QtGui import * from PyQt4.QtCore import * import PyQt4.QtCore as QtCore from electrum_xvg.i18n import _ from electrum_xvg import ELECTRUM_VERSION, print_error class VersionGetter(threading.Thread): def __init__(self, label): threading.Thread.__init__(self) self.label = label self.daemon = True def run(self): try: res = requests.request("GET", "http://electrum-verge.xyz/version") except: print_error("Could not retrieve version information") return if res.status_code == 200: latest_version = res.text latest_version = latest_version.replace("\n","") if(re.match('^\d+(\.\d+)*$', latest_version)): self.label.callback(latest_version) class UpdateLabel(QLabel): def __init__(self, config, sb): QLabel.__init__(self) self.new_version = False self.sb = sb self.config = config self.current_version = ELECTRUM_VERSION self.connect(self, QtCore.SIGNAL('new_electrum_version'), self.new_electrum_version) # prevent HTTP leaks if a proxy is set if self.config.get('proxy'): return VersionGetter(self).start() def callback(self, version): self.latest_version = version if(self.compare_versions(self.latest_version, self.current_version) == 1): latest_seen = self.config.get("last_seen_version",ELECTRUM_VERSION) if(self.compare_versions(self.latest_version, latest_seen) == 1): self.new_version = True self.emit(QtCore.SIGNAL('new_electrum_version')) def new_electrum_version(self): if self.new_version: self.setText(_("New version available") + ": " + self.latest_version) self.sb.insertPermanentWidget(1, self) def compare_versions(self, version1, version2): def normalize(v): return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")] try: return cmp(normalize(version1), normalize(version2)) except: return 0 def ignore_this_version(self): self.setText("") self.config.set_key("last_seen_version", self.latest_version, True) QMessageBox.information(self, _("Preference saved"), _("Notifications about this update will not be shown again.")) self.dialog.done(0) def ignore_all_version(self): self.setText("") self.config.set_key("last_seen_version", "9.9.9", True) QMessageBox.information(self, _("Preference saved"), _("No more notifications about version updates will be shown.")) self.dialog.done(0) def open_website(self): webbrowser.open("http://electrum-verge.xyz/download.html") self.dialog.done(0) def mouseReleaseEvent(self, event): dialog = QDialog(self) dialog.setWindowTitle(_('Electrum-XVG update')) dialog.setModal(1) main_layout = QGridLayout() main_layout.addWidget(QLabel(_("A new version of Electrum-XVG is available:")+" " + self.latest_version), 0,0,1,3) ignore_version = QPushButton(_("Ignore this version")) ignore_version.clicked.connect(self.ignore_this_version) ignore_all_versions = QPushButton(_("Ignore all versions")) ignore_all_versions.clicked.connect(self.ignore_all_version) open_website = QPushButton(_("Goto download page")) open_website.clicked.connect(self.open_website) main_layout.addWidget(ignore_version, 1, 0) main_layout.addWidget(ignore_all_versions, 1, 1) main_layout.addWidget(open_website, 1, 2) dialog.setLayout(main_layout) self.dialog = dialog if not dialog.exec_(): return #!/usr/bin/python #coding: utf-8 -*- # (c) 2013, Benno Joy # # This module 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 software 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 software. If not, see . try: try: from neutronclient.neutron import client except ImportError: from quantumclient.quantum import client from keystoneclient.v2_0 import client as ksclient HAVE_DEPS = True except ImportError: HAVE_DEPS = False DOCUMENTATION = ''' --- module: quantum_router version_added: "1.2" author: "Benno Joy (@bennojoy)" deprecated: Deprecated in 2.0. Use os_router instead short_description: Create or Remove router from openstack description: - Create or Delete routers from OpenStack options: login_username: description: - login username to authenticate to keystone required: true default: admin login_password: description: - Password of login user required: true default: 'yes' login_tenant_name: description: - The tenant name of the login user required: true default: 'yes' auth_url: description: - The keystone url for authentication required: false default: 'http://127.0.0.1:35357/v2.0/' region_name: description: - Name of the region required: false default: None state: description: - Indicate desired state of the resource choices: ['present', 'absent'] default: present name: description: - Name to be give to the router required: true default: None tenant_name: description: - Name of the tenant for which the router has to be created, if none router would be created for the login tenant. required: false default: None admin_state_up: description: - desired admin state of the created router . required: false default: true requirements: - "python >= 2.6" - "python-neutronclient or python-quantumclient" - "python-keystoneclient" ''' EXAMPLES = ''' # Creates a router for tenant admin - quantum_router: state=present login_username=admin login_password=admin login_tenant_name=admin name=router1" ''' _os_keystone = None _os_tenant_id = None def _get_ksclient(module, kwargs): try: kclient = ksclient.Client(username=kwargs.get('login_username'), password=kwargs.get('login_password'), tenant_name=kwargs.get('login_tenant_name'), auth_url=kwargs.get('auth_url')) except Exception, e: module.fail_json(msg = "Error authenticating to the keystone: %s " % e.message) global _os_keystone _os_keystone = kclient return kclient def _get_endpoint(module, ksclient): try: endpoint = ksclient.service_catalog.url_for(service_type='network', endpoint_type='publicURL') except Exception, e: module.fail_json(msg = "Error getting network endpoint: %s" % e.message) return endpoint def _get_neutron_client(module, kwargs): _ksclient = _get_ksclient(module, kwargs) token = _ksclient.auth_token endpoint = _get_endpoint(module, _ksclient) kwargs = { 'token': token, 'endpoint_url': endpoint } try: neutron = client.Client('2.0', **kwargs) except Exception, e: module.fail_json(msg = "Error in connecting to neutron: %s " % e.message) return neutron def _set_tenant_id(module): global _os_tenant_id if not module.params['tenant_name']: _os_tenant_id = _os_keystone.tenant_id else: tenant_name = module.params['tenant_name'] for tenant in _os_keystone.tenants.list(): if tenant.name == tenant_name: _os_tenant_id = tenant.id break if not _os_tenant_id: module.fail_json(msg = "The tenant id cannot be found, please check the parameters") def _get_router_id(module, neutron): kwargs = { 'name': module.params['name'], 'tenant_id': _os_tenant_id, } try: routers = neutron.list_routers(**kwargs) except Exception, e: module.fail_json(msg = "Error in getting the router list: %s " % e.message) if not routers['routers']: return None return routers['routers'][0]['id'] def _create_router(module, neutron): router = { 'name': module.params['name'], 'tenant_id': _os_tenant_id, 'admin_state_up': module.params['admin_state_up'], } try: new_router = neutron.create_router(dict(router=router)) except Exception, e: module.fail_json( msg = "Error in creating router: %s" % e.message) return new_router['router']['id'] def _delete_router(module, neutron, router_id): try: neutron.delete_router(router_id) except: module.fail_json("Error in deleting the router") return True def main(): argument_spec = openstack_argument_spec() argument_spec.update(dict( name = dict(required=True), tenant_name = dict(default=None), state = dict(default='present', choices=['absent', 'present']), admin_state_up = dict(type='bool', default=True), )) module = AnsibleModule(argument_spec=argument_spec) if not HAVE_DEPS: module.fail_json(msg='python-keystoneclient and either python-neutronclient or python-quantumclient are required') neutron = _get_neutron_client(module, module.params) _set_tenant_id(module) if module.params['state'] == 'present': router_id = _get_router_id(module, neutron) if not router_id: router_id = _create_router(module, neutron) module.exit_json(changed=True, result="Created", id=router_id) else: module.exit_json(changed=False, result="success" , id=router_id) else: router_id = _get_router_id(module, neutron) if not router_id: module.exit_json(changed=False, result="success") else: _delete_router(module, neutron, router_id) module.exit_json(changed=True, result="deleted") # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * if __name__ == '__main__': main() # Copyright 2013 Regionbibliotek Halland # # This file is part of Digitala sagor. # # Digitala sagor 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. # # Digitala sagor 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 Digitala sagor. If not, see . from Tkinter import * import ttk from tooltip import ToolTip from language import lang import language as lng from playerdlg import showPlayerDialog from datamodel import tpVideo import tkFileDialog import os import os.path import shutil import spmanager as spm from edittab import EditTab _videoFileFormats = [('mp4', '*.mp4'), ('avi', '*.avi'), ('wmv', '*.wmv'), ('mpeg', '*.mpeg'), ('mov', '*.mov')] class EditTabVideo(EditTab): """A Frame for editing video based stories""" def __init__(self, parent, wdir, datamodel, psize): """Initiate Arguments parent -- parent tkinter item wdir -- working directory datamodel -- the database that is edited by the program psize -- tuple defining preview size of videos """ EditTab.__init__(self, parent, wdir, datamodel, psize) self._mediatype = tpVideo #Create variables for common data self._svVideo = StringVar() #Make the first row expandable self.rowconfigure(0, weight = 1) #Add frame from super class self._superFrame.grid(row = 0, column = 0, sticky = W + N) #Create the right column rightLf = ttk.LabelFrame(self, text = ' ' + lang[lng.txtVideo] + ' ') rightLf.grid(row = 0, column = 1, pady = 10, sticky = W + N) rightFrame = Frame(rightLf) rightFrame.grid() e = Entry(rightFrame, w = 32, textvariable = self._svVideo, state = "readonly") e.grid(row = 0, column = 0, padx = 10, pady = 5, sticky = W); tt = ToolTip(e, '', textvariable = self._svVideo, wraplength = parent.winfo_screenwidth() * 4 / 5) b = Button(rightFrame, text = lang[lng.txtSelect] + '...', command = self._ehGetVideo) b.grid(row = 0, column = 1, padx = 10, pady = 5) b = Button(rightFrame, text = lang[lng.txtWatch], command = self._ehWatch) b.grid(row = 0, column = 2, padx = 10, pady = 5) def open(self, slideshow, prepared = False): """Open a slideshow for editing Arguments slideshow -- the slideshow prepared -- if true, all media data is already copied to the working folder (i.e. the slideshow has been created automatically) """ EditTab.open(self, slideshow, prepared = False) if(not prepared): if(slideshow.video != ''): shutil.copyfile(slideshow.getPath(slideshow.video), os.path.join(self._wdir, slideshow.video)) self._svVideo.set(slideshow.video) def clear(self): """Clear the edit tab""" EditTab.clear(self) self._svVideo.set('') def _getCurrentSlideshow(self): """Create and return a slideshow representing the currently edited slideshow.""" slideshow = EditTab._getCurrentSlideshow(self) slideshow.video = self._svVideo.get() return slideshow #Event handlers def _ehGetVideo(self): """Event handler for assigning a video""" initdir = spm.spmanager.getFirstPath([spm.VideoFolder, spm.MostRecentFolder]) filenamepath = tkFileDialog.askopenfilename(initialdir = initdir, filetypes = _videoFileFormats) if(len(filenamepath) > 0): filename = os.path.basename(filenamepath) try: shutil.copyfile(filenamepath, os.path.join(self._wdir, filename)) except IOError: showerror(lang[lng.txtCopyError], lang[lng.txtCouldNotCopy] + os.path.basename(filename)) return self._svVideo.set(filename) self.setDirty(True) spm.spmanager.setPath(spm.VideoFolder, os.path.dirname(filenamepath)) def _ehWatch(self): """Event handler for preview of the video""" media = self._getCurrentSlideshow() showPlayerDialog(self._parent, self._psize, media) # Protocol Buffers - Google's data interchange format # Copyright 2008 Google Inc. All rights reserved. # http://code.google.com/p/protobuf/ # # 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. """Code for encoding protocol message primitives. Contains the logic for encoding every logical protocol field type into one of the 5 physical wire types. This code is designed to push the Python interpreter's performance to the limits. The basic idea is that at startup time, for every field (i.e. every FieldDescriptor) we construct two functions: a "sizer" and an "encoder". The sizer takes a value of this field's type and computes its byte size. The encoder takes a writer function and a value. It encodes the value into byte strings and invokes the writer function to write those strings. Typically the writer function is the write() method of a cStringIO. We try to do as much work as possible when constructing the writer and the sizer rather than when calling them. In particular: * We copy any needed global functions to local variables, so that we do not need to do costly global table lookups at runtime. * Similarly, we try to do any attribute lookups at startup time if possible. * Every field's tag is encoded to bytes at startup, since it can't change at runtime. * Whatever component of the field size we can compute at startup, we do. * We *avoid* sharing code if doing so would make the code slower and not sharing does not burden us too much. For example, encoders for repeated fields do not just call the encoders for singular fields in a loop because this would add an extra function call overhead for every loop iteration; instead, we manually inline the single-value encoder into the loop. * If a Python function lacks a return statement, Python actually generates instructions to pop the result of the last statement off the stack, push None onto the stack, and then return that. If we really don't care what value is returned, then we can save two instructions by returning the result of the last statement. It looks funny but it helps. * We assume that type and bounds checking has happened at a higher level. """ __author__ = 'kenton@google.com (Kenton Varda)' import struct from google.protobuf.internal import wire_format # This will overflow and thus become IEEE-754 "infinity". We would use # "float('inf')" but it doesn't work on Windows pre-Python-2.6. _POS_INF = 1e10000 _NEG_INF = -_POS_INF def _VarintSize(value): """Compute the size of a varint value.""" if value <= 0x7f: return 1 if value <= 0x3fff: return 2 if value <= 0x1fffff: return 3 if value <= 0xfffffff: return 4 if value <= 0x7ffffffff: return 5 if value <= 0x3ffffffffff: return 6 if value <= 0x1ffffffffffff: return 7 if value <= 0xffffffffffffff: return 8 if value <= 0x7fffffffffffffff: return 9 return 10 def _SignedVarintSize(value): """Compute the size of a signed varint value.""" if value < 0: return 10 if value <= 0x7f: return 1 if value <= 0x3fff: return 2 if value <= 0x1fffff: return 3 if value <= 0xfffffff: return 4 if value <= 0x7ffffffff: return 5 if value <= 0x3ffffffffff: return 6 if value <= 0x1ffffffffffff: return 7 if value <= 0xffffffffffffff: return 8 if value <= 0x7fffffffffffffff: return 9 return 10 def _TagSize(field_number): """Returns the number of bytes required to serialize a tag with this field number.""" # Just pass in type 0, since the type won't affect the tag+type size. return _VarintSize(wire_format.PackTag(field_number, 0)) # -------------------------------------------------------------------- # In this section we define some generic sizers. Each of these functions # takes parameters specific to a particular field type, e.g. int32 or fixed64. # It returns another function which in turn takes parameters specific to a # particular field, e.g. the field number and whether it is repeated or packed. # Look at the next section to see how these are used. def _SimpleSizer(compute_value_size): """A sizer which uses the function compute_value_size to compute the size of each value. Typically compute_value_size is _VarintSize.""" def SpecificSizer(field_number, is_repeated, is_packed): tag_size = _TagSize(field_number) if is_packed: local_VarintSize = _VarintSize def PackedFieldSize(value): result = 0 for element in value: result += compute_value_size(element) return result + local_VarintSize(result) + tag_size return PackedFieldSize elif is_repeated: def RepeatedFieldSize(value): result = tag_size * len(value) for element in value: result += compute_value_size(element) return result return RepeatedFieldSize else: def FieldSize(value): return tag_size + compute_value_size(value) return FieldSize return SpecificSizer def _ModifiedSizer(compute_value_size, modify_value): """Like SimpleSizer, but modify_value is invoked on each value before it is passed to compute_value_size. modify_value is typically ZigZagEncode.""" def SpecificSizer(field_number, is_repeated, is_packed): tag_size = _TagSize(field_number) if is_packed: local_VarintSize = _VarintSize def PackedFieldSize(value): result = 0 for element in value: result += compute_value_size(modify_value(element)) return result + local_VarintSize(result) + tag_size return PackedFieldSize elif is_repeated: def RepeatedFieldSize(value): result = tag_size * len(value) for element in value: result += compute_value_size(modify_value(element)) return result return RepeatedFieldSize else: def FieldSize(value): return tag_size + compute_value_size(modify_value(value)) return FieldSize return SpecificSizer def _FixedSizer(value_size): """Like _SimpleSizer except for a fixed-size field. The input is the size of one value.""" def SpecificSizer(field_number, is_repeated, is_packed): tag_size = _TagSize(field_number) if is_packed: local_VarintSize = _VarintSize def PackedFieldSize(value): result = len(value) * value_size return result + local_VarintSize(result) + tag_size return PackedFieldSize elif is_repeated: element_size = value_size + tag_size def RepeatedFieldSize(value): return len(value) * element_size return RepeatedFieldSize else: field_size = value_size + tag_size def FieldSize(value): return field_size return FieldSize return SpecificSizer # ==================================================================== # Here we declare a sizer constructor for each field type. Each "sizer # constructor" is a function that takes (field_number, is_repeated, is_packed) # as parameters and returns a sizer, which in turn takes a field value as # a parameter and returns its encoded size. Int32Sizer = Int64Sizer = EnumSizer = _SimpleSizer(_SignedVarintSize) UInt32Sizer = UInt64Sizer = _SimpleSizer(_VarintSize) SInt32Sizer = SInt64Sizer = _ModifiedSizer( _SignedVarintSize, wire_format.ZigZagEncode) Fixed32Sizer = SFixed32Sizer = FloatSizer = _FixedSizer(4) Fixed64Sizer = SFixed64Sizer = DoubleSizer = _FixedSizer(8) BoolSizer = _FixedSizer(1) def StringSizer(field_number, is_repeated, is_packed): """Returns a sizer for a string field.""" tag_size = _TagSize(field_number) local_VarintSize = _VarintSize local_len = len assert not is_packed if is_repeated: def RepeatedFieldSize(value): result = tag_size * len(value) for element in value: l = local_len(element.encode('utf-8')) result += local_VarintSize(l) + l return result return RepeatedFieldSize else: def FieldSize(value): l = local_len(value.encode('utf-8')) return tag_size + local_VarintSize(l) + l return FieldSize def BytesSizer(field_number, is_repeated, is_packed): """Returns a sizer for a bytes field.""" tag_size = _TagSize(field_number) local_VarintSize = _VarintSize local_len = len assert not is_packed if is_repeated: def RepeatedFieldSize(value): result = tag_size * len(value) for element in value: l = local_len(element) result += local_VarintSize(l) + l return result return RepeatedFieldSize else: def FieldSize(value): l = local_len(value) return tag_size + local_VarintSize(l) + l return FieldSize def GroupSizer(field_number, is_repeated, is_packed): """Returns a sizer for a group field.""" tag_size = _TagSize(field_number) * 2 assert not is_packed if is_repeated: def RepeatedFieldSize(value): result = tag_size * len(value) for element in value: result += element.ByteSize() return result return RepeatedFieldSize else: def FieldSize(value): return tag_size + value.ByteSize() return FieldSize def MessageSizer(field_number, is_repeated, is_packed): """Returns a sizer for a message field.""" tag_size = _TagSize(field_number) local_VarintSize = _VarintSize assert not is_packed if is_repeated: def RepeatedFieldSize(value): result = tag_size * len(value) for element in value: l = element.ByteSize() result += local_VarintSize(l) + l return result return RepeatedFieldSize else: def FieldSize(value): l = value.ByteSize() return tag_size + local_VarintSize(l) + l return FieldSize # -------------------------------------------------------------------- # MessageSet is special. def MessageSetItemSizer(field_number): """Returns a sizer for extensions of MessageSet. The message set message looks like this: message MessageSet { repeated group Item = 1 { required int32 type_id = 2; required string message = 3; } } """ static_size = (_TagSize(1) * 2 + _TagSize(2) + _VarintSize(field_number) + _TagSize(3)) local_VarintSize = _VarintSize def FieldSize(value): l = value.ByteSize() return static_size + local_VarintSize(l) + l return FieldSize # ==================================================================== # Encoders! def _VarintEncoder(): """Return an encoder for a basic varint value (does not include tag).""" local_chr = chr def EncodeVarint(write, value): bits = value & 0x7f value >>= 7 while value: write(local_chr(0x80|bits)) bits = value & 0x7f value >>= 7 return write(local_chr(bits)) return EncodeVarint def _SignedVarintEncoder(): """Return an encoder for a basic signed varint value (does not include tag).""" local_chr = chr def EncodeSignedVarint(write, value): if value < 0: value += (1 << 64) bits = value & 0x7f value >>= 7 while value: write(local_chr(0x80|bits)) bits = value & 0x7f value >>= 7 return write(local_chr(bits)) return EncodeSignedVarint _EncodeVarint = _VarintEncoder() _EncodeSignedVarint = _SignedVarintEncoder() def _VarintBytes(value): """Encode the given integer as a varint and return the bytes. This is only called at startup time so it doesn't need to be fast.""" pieces = [] _EncodeVarint(pieces.append, value) return "".join(pieces) def TagBytes(field_number, wire_type): """Encode the given tag and return the bytes. Only called at startup.""" return _VarintBytes(wire_format.PackTag(field_number, wire_type)) # -------------------------------------------------------------------- # As with sizers (see above), we have a number of common encoder # implementations. def _SimpleEncoder(wire_type, encode_value, compute_value_size): """Return a constructor for an encoder for fields of a particular type. Args: wire_type: The field's wire type, for encoding tags. encode_value: A function which encodes an individual value, e.g. _EncodeVarint(). compute_value_size: A function which computes the size of an individual value, e.g. _VarintSize(). """ def SpecificEncoder(field_number, is_repeated, is_packed): if is_packed: tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local_EncodeVarint = _EncodeVarint def EncodePackedField(write, value): write(tag_bytes) size = 0 for element in value: size += compute_value_size(element) local_EncodeVarint(write, size) for element in value: encode_value(write, element) return EncodePackedField elif is_repeated: tag_bytes = TagBytes(field_number, wire_type) def EncodeRepeatedField(write, value): for element in value: write(tag_bytes) encode_value(write, element) return EncodeRepeatedField else: tag_bytes = TagBytes(field_number, wire_type) def EncodeField(write, value): write(tag_bytes) return encode_value(write, value) return EncodeField return SpecificEncoder def _ModifiedEncoder(wire_type, encode_value, compute_value_size, modify_value): """Like SimpleEncoder but additionally invokes modify_value on every value before passing it to encode_value. Usually modify_value is ZigZagEncode.""" def SpecificEncoder(field_number, is_repeated, is_packed): if is_packed: tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local_EncodeVarint = _EncodeVarint def EncodePackedField(write, value): write(tag_bytes) size = 0 for element in value: size += compute_value_size(modify_value(element)) local_EncodeVarint(write, size) for element in value: encode_value(write, modify_value(element)) return EncodePackedField elif is_repeated: tag_bytes = TagBytes(field_number, wire_type) def EncodeRepeatedField(write, value): for element in value: write(tag_bytes) encode_value(write, modify_value(element)) return EncodeRepeatedField else: tag_bytes = TagBytes(field_number, wire_type) def EncodeField(write, value): write(tag_bytes) return encode_value(write, modify_value(value)) return EncodeField return SpecificEncoder def _StructPackEncoder(wire_type, format): """Return a constructor for an encoder for a fixed-width field. Args: wire_type: The field's wire type, for encoding tags. format: The format string to pass to struct.pack(). """ value_size = struct.calcsize(format) def SpecificEncoder(field_number, is_repeated, is_packed): local_struct_pack = struct.pack if is_packed: tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local_EncodeVarint = _EncodeVarint def EncodePackedField(write, value): write(tag_bytes) local_EncodeVarint(write, len(value) * value_size) for element in value: write(local_struct_pack(format, element)) return EncodePackedField elif is_repeated: tag_bytes = TagBytes(field_number, wire_type) def EncodeRepeatedField(write, value): for element in value: write(tag_bytes) write(local_struct_pack(format, element)) return EncodeRepeatedField else: tag_bytes = TagBytes(field_number, wire_type) def EncodeField(write, value): write(tag_bytes) return write(local_struct_pack(format, value)) return EncodeField return SpecificEncoder def _FloatingPointEncoder(wire_type, format): """Return a constructor for an encoder for float fields. This is like StructPackEncoder, but catches errors that may be due to passing non-finite floating-point values to struct.pack, and makes a second attempt to encode those values. Args: wire_type: The field's wire type, for encoding tags. format: The format string to pass to struct.pack(). """ value_size = struct.calcsize(format) if value_size == 4: def EncodeNonFiniteOrRaise(write, value): # Remember that the serialized form uses little-endian byte order. if value == _POS_INF: write('\x00\x00\x80\x7F') elif value == _NEG_INF: write('\x00\x00\x80\xFF') elif value != value: # NaN write('\x00\x00\xC0\x7F') else: raise elif value_size == 8: def EncodeNonFiniteOrRaise(write, value): if value == _POS_INF: write('\x00\x00\x00\x00\x00\x00\xF0\x7F') elif value == _NEG_INF: write('\x00\x00\x00\x00\x00\x00\xF0\xFF') elif value != value: # NaN write('\x00\x00\x00\x00\x00\x00\xF8\x7F') else: raise else: raise ValueError('Can\'t encode floating-point values that are ' '%d bytes long (only 4 or 8)' % value_size) def SpecificEncoder(field_number, is_repeated, is_packed): local_struct_pack = struct.pack if is_packed: tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local_EncodeVarint = _EncodeVarint def EncodePackedField(write, value): write(tag_bytes) local_EncodeVarint(write, len(value) * value_size) for element in value: # This try/except block is going to be faster than any code that # we could write to check whether element is finite. try: write(local_struct_pack(format, element)) except SystemError: EncodeNonFiniteOrRaise(write, element) return EncodePackedField elif is_repeated: tag_bytes = TagBytes(field_number, wire_type) def EncodeRepeatedField(write, value): for element in value: write(tag_bytes) try: write(local_struct_pack(format, element)) except SystemError: EncodeNonFiniteOrRaise(write, element) return EncodeRepeatedField else: tag_bytes = TagBytes(field_number, wire_type) def EncodeField(write, value): write(tag_bytes) try: write(local_struct_pack(format, value)) except SystemError: EncodeNonFiniteOrRaise(write, value) return EncodeField return SpecificEncoder # ==================================================================== # Here we declare an encoder constructor for each field type. These work # very similarly to sizer constructors, described earlier. Int32Encoder = Int64Encoder = EnumEncoder = _SimpleEncoder( wire_format.WIRETYPE_VARINT, _EncodeSignedVarint, _SignedVarintSize) UInt32Encoder = UInt64Encoder = _SimpleEncoder( wire_format.WIRETYPE_VARINT, _EncodeVarint, _VarintSize) SInt32Encoder = SInt64Encoder = _ModifiedEncoder( wire_format.WIRETYPE_VARINT, _EncodeVarint, _VarintSize, wire_format.ZigZagEncode) # Note that Python conveniently guarantees that when using the '<' prefix on # formats, they will also have the same size across all platforms (as opposed # to without the prefix, where their sizes depend on the C compiler's basic # type sizes). Fixed32Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED32, '= 2: print("Running pre-migrate handlers for application %s" % app_config.label) models.signals.pre_migrate.send( sender=app_config, app_config=app_config, verbosity=verbosity, interactive=interactive, using=db) def emit_post_migrate_signal(verbosity, interactive, db): # Emit the post_migrate signal for every application. for app_config in apps.get_app_configs(): if app_config.models_module is None: continue if verbosity >= 2: print("Running post-migrate handlers for application %s" % app_config.label) models.signals.post_migrate.send( sender=app_config, app_config=app_config, verbosity=verbosity, interactive=interactive, using=db) """concurrent-cpu-bound.py Operate on a raster dataset window-by-window using a ThreadPoolExecutor. Simulates a CPU-bound thread situation where multiple threads can improve performance. With -j 4, the program returns in about 1/4 the time as with -j 1. """ import concurrent.futures import multiprocessing import time import numpy import rasterio from rasterio._example import compute def main(infile, outfile, num_workers=4): with rasterio.drivers(): # Open the source dataset. with rasterio.open(infile) as src: # Create a destination dataset based on source params. # The destination will be tiled, and we'll "process" the tiles # concurrently. meta = src.meta del meta['transform'] meta.update(affine=src.affine) meta.update(blockxsize=256, blockysize=256, tiled='yes') with rasterio.open(outfile, 'w', **meta) as dst: # Define a generator for data, window pairs. # We use the new read() method here to a 3D array with all # bands, but could also use read_band(). def jobs(): for ij, window in dst.block_windows(): data = src.read(window=window) result = numpy.zeros(data.shape, dtype=data.dtype) yield data, result, window # Submit the jobs to the thread pool executor. with concurrent.futures.ThreadPoolExecutor( max_workers=num_workers) as executor: # Map the futures returned from executor.submit() # to their destination windows. # # The _example.compute function modifies no Python # objects and releases the GIL. It can execute # concurrently. future_to_window = { executor.submit(compute, data, res): (res, window) for data, res, window in jobs()} # As the processing jobs are completed, get the # results and write the data to the appropriate # destination window. for future in concurrent.futures.as_completed( future_to_window): result, window = future_to_window[future] # Since there's no multiband write() method yet in # Rasterio, we use write_band for each part of the # 3D data array. for i, arr in enumerate(result, 1): dst.write_band(i, arr, window=window) if __name__ == '__main__': import argparse parser = argparse.ArgumentParser( description="Concurrent raster processing demo") parser.add_argument( 'input', metavar='INPUT', help="Input file name") parser.add_argument( 'output', metavar='OUTPUT', help="Output file name") parser.add_argument( '-j', metavar='NUM_JOBS', type=int, default=multiprocessing.cpu_count(), help="Number of concurrent jobs") args = parser.parse_args() main(args.input, args.output, args.j) # -*- coding: utf-8 -*- from micolog_plugin import * import logging import urllib from model import * from google.appengine.api import users from google.appengine.api import memcache from base import BaseRequestHandler,request_cache from google.appengine.ext import webapp from datetime import datetime, timedelta def urlencode(value): return urllib.quote(value.encode('utf8')) class wapblog(Plugin): def __init__(self): Plugin.__init__(self,__file__) self.author="云在天边" self.authoruri="http://www.tangblog.info" self.uri="http://www.tangblog.info" self.description="Micolog WAP Blog插件,使用该插件可以方便在手机上浏览新博文,查看并发表评论。(默认仅支持Google Account用户登陆留言,点击插件名进行设置。)" self.name="Micolog Wap Blog" self.version="0.6" self.register_urlhandler('(?i)/wap',waphandler) self.register_urlhandler('/wap/(\d+)',waphandler) self.register_urlhandler('(?i)/wap/page',pagehandler) self.register_urlhandler('(?i)/wap/post_comment',postComment) self.register_urlhandler('(?i)/wap/(.*)',Error404) def get(self,page): postcount=OptionSet.getValue("posts_per_page",default="8") commentcount=OptionSet.getValue("LatestCommentCount",default="5") return '''

“WAP Blog”插件已经工作!

请完善如下设置

每页显示文章数目:
文章最近评论数目:(若该值设为0,将显示所有留言)

恭喜你! 你的"Micolog WAP Blog" 插件已经工作!
访问Wap页面的URL是: http://www.yourdomain.com/wap
作者:云在天边

您的支持是创作者继续发展的动力,感谢您以实际行动来帮助作者!

如果在使用过程中遇到任何问题,请到作者的留言板(云在天边 www.tangblog.info/contact)提交报告!

'''%(postcount,commentcount) def post(self,page): postcount=int(page.param("PostCount")) commentcount=int(page.param("CommentCount")) OptionSet.setValue("posts_per_page",postcount) OptionSet.setValue("LatestCommentCount",commentcount) return self.get(page) class waphandler(BaseRequestHandler): def get(self,page=1): self.doget(page) #TODO: update this @request_cache() def doget(self,page): try: from model import g_blog except: pass page=int(page) time=datetime.now() entrycount=g_blog.postscount() posts_per_page = OptionSet.getValue("posts_per_page",default="8") if posts_per_page: posts_per_page = 8 max_page = entrycount / posts_per_page + ( entrycount % posts_per_page and 1 or 0 ) comments=memcache.get("wap_comments"+self.request.path_qs) if comments is None: comments=Comment.all().order('-date').fetch(5) memcache.set("wap_comments"+self.request.path_qs,comments) if page < 1 or page > max_page: return self.error(404) entries=memcache.get("wap_entries"+self.request.path_qs) if entries is None : entries = Entry.all().filter('entrytype =','post').\ filter("published =", True).order('-date').\ fetch(posts_per_page, offset = (page-1) * posts_per_page) memcache.set("wap_entries"+self.request.path_qs,entries) show_prev =entries and (not (page == 1)) show_next =entries and (not (page == max_page)) self.render2("plugins/wapblog/index.html",{'entries':entries, 'show_prev' : show_prev, 'show_next' : show_next, 'pageindex':page, 'time':time, 'ishome':True, 'pagecount':max_page, 'postscount':entrycount, 'comments':comments }) class pagehandler(BaseRequestHandler): #TODO: update this @request_cache() def get(self,*arg1): try: id=int(self.param("id") or self.param("p") ) except: return self.redirect('/wap') time=datetime.now() commentcount = OptionSet.getValue("LatestCommentCount",default="5") if commentcount: commentcount = 5 entries = Entry.all().filter("published =", True).filter('post_id =',id).fetch(1) entry=entries[0] comments=memcache.get("wap_comments"+self.request.path_qs) if comments is None: if commentcount==0: comments=Comment.all().filter("entry =",entry).order('-date') memcache.set("wap_comments"+self.request.path_qs,comments) else: comments=Comment.all().filter("entry =",entry).order('-date').fetch(commentcount) memcache.set("wap_comments"+self.request.path_qs,comments) Comments=memcache.get("wap_Comments"+self.request.path_qs) if Comments is None: Comments=Comment.all().filter("entry =",entry).order('-date') user = users.get_current_user() if user: greeting = ("Welcome, %s! (sign out)" % (user.nickname(), users.create_logout_url(self.request.uri))) email = user.email() try: query = Comment.all().filter('email =',email).order('-date').fetch(1) name = query[0].author weburl = query[0].weburl except: name=user.nickname() weburl=None self.render2("plugins/wapblog/page.html",{'entry':entry,'id':id,'comments':comments,'Comments':Comments,'user_name':name,'user_email':email,'user':user,'user_url':weburl,'greeting':greeting,'time':time}) else: greeting = ("Sign in with your Google Account." % users.create_login_url(self.request.uri)) self.render2("plugins/wapblog/page.html",{'entry':entry,'id':id,'comments':comments,'Comments':Comments,'greeting':greeting,'user':user,'time':time}) class postComment(BaseRequestHandler): def get(self,*arg1): self.response.set_status(405) self.write('

405 Method Not Allowed

\nBack To Home') def post(self): name=self.param('author') #email=self.param('email') url=self.param('url') key=self.param('key') content=self.param('comment') parent_id=self.paramint('parentid',0) reply_notify_mail=True user = users.get_current_user() try: email=user.email() except: email=None if not (name and email and content): self.response.out.write('Please input name and comment content .\n Back') else: comment=Comment(author=name, content=content+"
from wap blog", email=email, reply_notify_mail=reply_notify_mail, entry=Entry.get(key)) starturl='http://' if url: try: if not url.lower().startswith(('http://','https://')): url = starturl + url comment.weburl=url except: comment.weburl=None info_str='#@#'.join([urlencode(name),urlencode(email),urlencode(url)]) logging.info("info:"+name+"#@#"+info_str + "Comment Form Wap Site") cookiestr='comment_user=%s;expires=%s;domain=%s;path=/'%( info_str, (datetime.now()+timedelta(days=100)).strftime("%a, %d-%b-%Y %H:%M:%S GMT"), '' ) comment.ip=self.request.remote_addr if parent_id: comment.parent=Comment.get_by_id(parent_id) comment.no=comment.entry.commentcount+1 try: comment.save() memcache.delete("/"+comment.entry.link) self.response.headers.add_header( 'Set-Cookie', cookiestr) self.redirect(self.referer+"#comment-"+str(comment.key().id())) memcache.delete("/feed/comments") except: self.response.out.write('Sorry,Comment not allowed .\n Back') class Error404(BaseRequestHandler): def get(self,*arg1): self.response.clear() self.response.set_status(404) self.response.out.write('

404 Not Found

\nBack To Main Page ! ') # -*- coding: utf-8 -*- ## ## This file is part of Invenio. ## Copyright (C) 2013 CERN. ## ## Invenio 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. ## ## Invenio 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 Invenio; if not, write to the Free Software Foundation, Inc., ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. """WebTag Forms""" from invenio.webtag_config import \ CFG_WEBTAG_LAST_MYSQL_CHARACTER from invenio.webtag_config import \ CFG_WEBTAG_NAME_MAX_LENGTH from invenio.webinterface_handler_flask_utils import _ from invenio.wtforms_utils import InvenioBaseForm from invenio.webuser_flask import current_user from wtforms import \ IntegerField, \ HiddenField, \ TextField, \ SelectMultipleField, \ validators # Models from invenio.sqlalchemyutils import db from invenio.webtag_model import \ WtgTAG, \ WtgTAGRecord, \ wash_tag_silent, \ wash_tag_blocking from invenio.bibedit_model import Bibrec from invenio.search_engine import check_user_can_view_record def validate_tag_name(dummy_form, field): """ Check validity of tag name """ if field.data: suggested_silent = wash_tag_silent(field.data) suggested = wash_tag_blocking(suggested_silent) field.data = suggested_silent if suggested != suggested_silent: raise validators.ValidationError( _('Forbidden characters. Try ') + suggested + '.') if len(suggested) <= 0: raise validators.ValidationError( _('The name must contain valid characters.')) if len(suggested_silent) > CFG_WEBTAG_NAME_MAX_LENGTH: raise validators.ValidationError( _('The name cannot exeed ') \ + str(CFG_WEBTAG_NAME_MAX_LENGTH) + _(' characters.')) if max(ord(letter) for letter in suggested_silent) \ > CFG_WEBTAG_LAST_MYSQL_CHARACTER: raise validators.ValidationError( _('Forbidden character.')) def validate_name_available(dummy_form, field): """ Check if the user already has tag named this way """ if field.data: uid = current_user.get_id() copy_count = db.session.query(WtgTAG).\ filter_by(id_user=uid, name=field.data).count() if copy_count > 0: raise validators.ValidationError( _('Tag with that name already exists.')) def validate_tag_exists(dummy_form, field): """ Check if id_tag matches a tag in database """ if field.data: try: field.data = int(field.data) except ValueError: raise validators.ValidationError(_('Tag ID must be an integer.')) if not db.session.query(WtgTAG).get(field.data): raise validators.ValidationError(_('Tag does not exist.')) def validate_user_owns_tag(dummy_form, field): """ Check if id_tag matches a tag in database """ if field.data: tag = db.session.query(WtgTAG).get(field.data) if tag and tag.id_user != current_user.get_id(): raise validators.ValidationError( _('You are not the owner of this tag.')) def validate_bibrec_exists(dummy_form, field): """ Check if id_bibrec matches a bibrec in database """ if field.data: try: field.data = int(field.data) except ValueError: raise validators.ValidationError(_('Bibrec ID must be an integer.')) record = db.session.query(Bibrec).get(field.data) if (not record): raise validators.ValidationError(_('Bibrec does not exist.')) # Switch to merged record if present merged_id = record.merged_recid_final if merged_id != record.id: record = db.session.query(Bibrec).get(merged_id) field.data = merged_id if record.deleted: raise validators.ValidationError(_('Bibrec has been deleted.')) def validate_user_can_see_bibrec(dummy_form, field): """ Check if user has rights to view bibrec """ if field.data: (auth_code, msg) = check_user_can_view_record(current_user, field.data) if auth_code > 0: raise validators.ValidationError( _('Unauthorized to view record: ')+msg) def validate_not_already_attached(form, dummy_field): """ Check if the pair (tag, bibrec) is already connected """ if form: if ('id_tag' in form.data) and ('id_bibrec' in form.data): tag_record = db.session.query(WtgTAGRecord)\ .get((form.data['id_tag'], form.data['id_bibrec'])) if tag_record is not None: raise validators.ValidationError(_('Tag already attached.')) def validate_already_attached(form, dummy_field): """ Check if the pair (tag, bibrec) is already connected """ if form: if ('id_tag' in form.data) and ('id_bibrec' in form.data): tag_record = db.session.query(WtgTAGRecord)\ .get((form.data['id_tag'], form.data['id_bibrec'])) if tag_record is None: raise validators.ValidationError(_('Tag not attached.')) class CreateTagForm(InvenioBaseForm): """Defines form for creating a new tag.""" name = TextField(_('Name'), [validators.Required(), validate_tag_name, validate_name_available]) # Ajax requests only: # Send a record ID if the tag should be attached to the record # right after creation id_bibrec = HiddenField('Tagged record', [validate_bibrec_exists, validate_user_can_see_bibrec]) class DeleteTagForm(InvenioBaseForm): """Defines form for deleting a tag.""" id_tag = SelectMultipleField('Tag ID', [validators.Required(), validate_tag_exists, validate_user_owns_tag]) class AttachTagForm(InvenioBaseForm): """Defines a form validating attaching a tag to record""" # Ajax requests only: id_tag = IntegerField('Tag ID', [validators.Required(), validate_tag_exists, validate_not_already_attached, validate_user_owns_tag]) # validate user rights on tag id_bibrec = IntegerField('Record ID', [validate_bibrec_exists, validate_user_can_see_bibrec]) class DetachTagForm(InvenioBaseForm): """Defines a form validating detaching a tag from record""" # Ajax requests only: id_tag = IntegerField('Tag ID', [validators.Required(), validate_tag_exists, validate_already_attached, validate_user_owns_tag]) # validate user rights on tag id_bibrec = IntegerField('Record ID', [validators.Required(), validate_bibrec_exists, validate_user_can_see_bibrec]) # -*- coding: utf-8 -*- import logging import simplejson import os import openerp import time import random import werkzeug.utils from openerp import http from openerp.http import request from openerp.addons.web.controllers.main import module_boot, login_redirect _logger = logging.getLogger(__name__) class PosController(http.Controller): @http.route('/pos/web', type='http', auth='user') def a(self, debug=False, **k): cr, uid, context, session = request.cr, request.uid, request.context, request.session if not session.uid: return login_redirect() PosSession = request.registry['pos.session'] pos_session_ids = PosSession.search(cr, uid, [('state','=','opened'),('user_id','=',session.uid)], context=context) if not pos_session_ids: return werkzeug.utils.redirect('/web#action=point_of_sale.action_pos_session_opening') PosSession.login(cr,uid,pos_session_ids,context=context) modules = simplejson.dumps(module_boot(request.db)) init = """ var wc = new s.web.WebClient(); wc._title_changed = function() {} wc.show_application = function(){ wc.action_manager.do_action("pos.ui"); }; wc.setElement($(document.body)); wc.start(); """ html = request.registry.get('ir.ui.view').render(cr, session.uid,'point_of_sale.index',{ 'modules': modules, 'init': init, }) return html # -*- coding: utf-8 -*- """Floyd-Warshall algorithm for shortest paths. """ # Copyright (C) 2004-2015 by # Aric Hagberg # Dan Schult # Pieter Swart # All rights reserved. # BSD license. import networkx as nx __author__ = """Aric Hagberg """ __all__ = ['floyd_warshall', 'floyd_warshall_predecessor_and_distance', 'floyd_warshall_numpy'] def floyd_warshall_numpy(G, nodelist=None, weight='weight'): """Find all-pairs shortest path lengths using Floyd's algorithm. Parameters ---------- G : NetworkX graph nodelist : list, optional The rows and columns are ordered by the nodes in nodelist. If nodelist is None then the ordering is produced by G.nodes(). weight: string, optional (default= 'weight') Edge data key corresponding to the edge weight. Returns ------- distance : NumPy matrix A matrix of shortest path distances between nodes. If there is no path between to nodes the corresponding matrix entry will be Inf. Notes ------ Floyd's algorithm is appropriate for finding shortest paths in dense graphs or graphs with negative weights when Dijkstra's algorithm fails. This algorithm can still fail if there are negative cycles. It has running time O(n^3) with running space of O(n^2). """ try: import numpy as np except ImportError: raise ImportError(\ "to_numpy_matrix() requires numpy: http://scipy.org/ ") # To handle cases when an edge has weight=0, we must make sure that # nonedges are not given the value 0 as well. A = nx.to_numpy_matrix(G, nodelist=nodelist, multigraph_weight=min, weight=weight, nonedge=np.inf) n,m = A.shape I = np.identity(n) A[I==1] = 0 # diagonal elements should be zero for i in range(n): A = np.minimum(A, A[i,:] + A[:,i]) return A def floyd_warshall_predecessor_and_distance(G, weight='weight'): """Find all-pairs shortest path lengths using Floyd's algorithm. Parameters ---------- G : NetworkX graph weight: string, optional (default= 'weight') Edge data key corresponding to the edge weight. Returns ------- predecessor,distance : dictionaries Dictionaries, keyed by source and target, of predecessors and distances in the shortest path. Notes ------ Floyd's algorithm is appropriate for finding shortest paths in dense graphs or graphs with negative weights when Dijkstra's algorithm fails. This algorithm can still fail if there are negative cycles. It has running time O(n^3) with running space of O(n^2). See Also -------- floyd_warshall floyd_warshall_numpy all_pairs_shortest_path all_pairs_shortest_path_length """ from collections import defaultdict # dictionary-of-dictionaries representation for dist and pred # use some defaultdict magick here # for dist the default is the floating point inf value dist = defaultdict(lambda : defaultdict(lambda: float('inf'))) for u in G: dist[u][u] = 0 pred = defaultdict(dict) # initialize path distance dictionary to be the adjacency matrix # also set the distance to self to 0 (zero diagonal) undirected = not G.is_directed() for u,v,d in G.edges(data=True): e_weight = d.get(weight, 1.0) dist[u][v] = min(e_weight, dist[u][v]) pred[u][v] = u if undirected: dist[v][u] = min(e_weight, dist[v][u]) pred[v][u] = v for w in G: for u in G: for v in G: if dist[u][v] > dist[u][w] + dist[w][v]: dist[u][v] = dist[u][w] + dist[w][v] pred[u][v] = pred[w][v] return dict(pred),dict(dist) def floyd_warshall(G, weight='weight'): """Find all-pairs shortest path lengths using Floyd's algorithm. Parameters ---------- G : NetworkX graph weight: string, optional (default= 'weight') Edge data key corresponding to the edge weight. Returns ------- distance : dict A dictionary, keyed by source and target, of shortest paths distances between nodes. Notes ------ Floyd's algorithm is appropriate for finding shortest paths in dense graphs or graphs with negative weights when Dijkstra's algorithm fails. This algorithm can still fail if there are negative cycles. It has running time O(n^3) with running space of O(n^2). See Also -------- floyd_warshall_predecessor_and_distance floyd_warshall_numpy all_pairs_shortest_path all_pairs_shortest_path_length """ # could make this its own function to reduce memory costs return floyd_warshall_predecessor_and_distance(G, weight=weight)[1] # fixture for nose tests def setup_module(module): from nose import SkipTest try: import numpy except: raise SkipTest("NumPy not available") #!/usr/bin/python # -*- coding: utf-8 -*- # # Copyright 2013-2015 clowwindy # # 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 ssloop # https://github.com/clowwindy/ssloop from __future__ import absolute_import, division, print_function, \ with_statement import os import socket import select import errno import logging from collections import defaultdict from shadowsocks import shell __all__ = ['EventLoop', 'POLL_NULL', 'POLL_IN', 'POLL_OUT', 'POLL_ERR', 'POLL_HUP', 'POLL_NVAL', 'EVENT_NAMES'] POLL_NULL = 0x00 POLL_IN = 0x01 POLL_OUT = 0x04 POLL_ERR = 0x08 POLL_HUP = 0x10 POLL_NVAL = 0x20 EVENT_NAMES = { POLL_NULL: 'POLL_NULL', POLL_IN: 'POLL_IN', POLL_OUT: 'POLL_OUT', POLL_ERR: 'POLL_ERR', POLL_HUP: 'POLL_HUP', POLL_NVAL: 'POLL_NVAL', } class EpollLoop(object): def __init__(self): self._epoll = select.epoll() def poll(self, timeout): return self._epoll.poll(timeout) def add_fd(self, fd, mode): self._epoll.register(fd, mode) def remove_fd(self, fd): self._epoll.unregister(fd) def modify_fd(self, fd, mode): self._epoll.modify(fd, mode) class KqueueLoop(object): MAX_EVENTS = 1024 def __init__(self): self._kqueue = select.kqueue() self._fds = {} def _control(self, fd, mode, flags): events = [] if mode & POLL_IN: events.append(select.kevent(fd, select.KQ_FILTER_READ, flags)) if mode & POLL_OUT: events.append(select.kevent(fd, select.KQ_FILTER_WRITE, flags)) for e in events: self._kqueue.control([e], 0) def poll(self, timeout): if timeout < 0: timeout = None # kqueue behaviour events = self._kqueue.control(None, KqueueLoop.MAX_EVENTS, timeout) results = defaultdict(lambda: POLL_NULL) for e in events: fd = e.ident if e.filter == select.KQ_FILTER_READ: results[fd] |= POLL_IN elif e.filter == select.KQ_FILTER_WRITE: results[fd] |= POLL_OUT return results.items() def add_fd(self, fd, mode): self._fds[fd] = mode self._control(fd, mode, select.KQ_EV_ADD) def remove_fd(self, fd): self._control(fd, self._fds[fd], select.KQ_EV_DELETE) del self._fds[fd] def modify_fd(self, fd, mode): self.remove_fd(fd) self.add_fd(fd, mode) class SelectLoop(object): def __init__(self): self._r_list = set() self._w_list = set() self._x_list = set() def poll(self, timeout): r, w, x = select.select(self._r_list, self._w_list, self._x_list, timeout) results = defaultdict(lambda: POLL_NULL) for p in [(r, POLL_IN), (w, POLL_OUT), (x, POLL_ERR)]: for fd in p[0]: results[fd] |= p[1] return results.items() def add_fd(self, fd, mode): if mode & POLL_IN: self._r_list.add(fd) if mode & POLL_OUT: self._w_list.add(fd) if mode & POLL_ERR: self._x_list.add(fd) def remove_fd(self, fd): if fd in self._r_list: self._r_list.remove(fd) if fd in self._w_list: self._w_list.remove(fd) if fd in self._x_list: self._x_list.remove(fd) def modify_fd(self, fd, mode): self.remove_fd(fd) self.add_fd(fd, mode) class EventLoop(object): def __init__(self): self._iterating = False if hasattr(select, 'epoll'): self._impl = EpollLoop() model = 'epoll' elif hasattr(select, 'kqueue'): self._impl = KqueueLoop() model = 'kqueue' elif hasattr(select, 'select'): self._impl = SelectLoop() model = 'select' else: raise Exception('can not find any available functions in select ' 'package') self._fd_to_f = {} self._handlers = [] self._ref_handlers = [] self._handlers_to_remove = [] logging.debug('using event model: %s', model) def poll(self, timeout=None): events = self._impl.poll(timeout) return [(self._fd_to_f[fd], fd, event) for fd, event in events] def add(self, f, mode): fd = f.fileno() self._fd_to_f[fd] = f self._impl.add_fd(fd, mode) def remove(self, f): fd = f.fileno() del self._fd_to_f[fd] self._impl.remove_fd(fd) def modify(self, f, mode): fd = f.fileno() self._impl.modify_fd(fd, mode) def add_handler(self, handler, ref=True): self._handlers.append(handler) if ref: # when all ref handlers are removed, loop stops self._ref_handlers.append(handler) def remove_handler(self, handler): if handler in self._ref_handlers: self._ref_handlers.remove(handler) if self._iterating: self._handlers_to_remove.append(handler) else: self._handlers.remove(handler) def run(self): events = [] while self._ref_handlers: try: events = self.poll(1) except (OSError, IOError) as e: if errno_from_exception(e) in (errno.EPIPE, errno.EINTR): # EPIPE: Happens when the client closes the connection # EINTR: Happens when received a signal # handles them as soon as possible logging.debug('poll:%s', e) else: logging.error('poll:%s', e) import traceback traceback.print_exc() continue self._iterating = True for handler in self._handlers: # TODO when there are a lot of handlers try: handler(events) except (OSError, IOError) as e: shell.print_exception(e) if self._handlers_to_remove: for handler in self._handlers_to_remove: self._handlers.remove(handler) self._handlers_to_remove = [] self._iterating = False # from tornado def errno_from_exception(e): """Provides the errno from an Exception object. There are cases that the errno attribute was not set so we pull the errno out of the args but if someone instatiates an Exception without any args you will get a tuple error. So this function abstracts all that behavior to give you a safe way to get the errno. """ if hasattr(e, 'errno'): return e.errno elif e.args: return e.args[0] else: return None # from tornado def get_sock_error(sock): error_number = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) return socket.error(error_number, os.strerror(error_number)) # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. """Random projection layers in MXNet as custom python ops. Currently slow and memory-inefficient, but functional. """ import os import numpy as np import mxnet as mx # ref: http://mxnet.io/faq/new_op.html class RandomBagOfWordsProjection(mx.operator.CustomOp): """Random projection layer for sparse bag-of-words (n-hot) inputs. In the sparse input, only the indices are supplied, because all the values are understood to be exactly 1.0. See also RandomProjection for values other than 1.0. """ def __init__(self, vocab_size, output_dim, random_seed=54321): # need_top_grad=True means this is not a loss layer super(RandomBagOfWordsProjection, self).__init__() self._vocab = vocab_size self._proj_dim = output_dim #NOTE: This naive implementation is slow and uses lots of memory. # Should use something smarter to not instantiate this matrix. rs = np.random.RandomState(seed=random_seed) self.W = self.random_unit_vecs(self._vocab, self._proj_dim, rs) def random_unit_vecs(self, num_vecs, num_dims, rs): W = rs.normal(size=(num_vecs, num_dims)) Wlen = np.linalg.norm(W, axis=1) W_unit = W / Wlen[:,None] return W_unit def _get_mask(self, idx, in_data): """Returns the mask by which to multiply the parts of the embedding layer. In this version, we have no weights to apply. """ mask = idx >= 0 # bool False for -1 values that should be removed. shape=(b,mnz) mask = np.expand_dims(mask,2) # shape = (b,mnz,1) mask = np.repeat(mask, self._proj_dim, axis=2) # shape = (b,mnz,d) return mask def forward(self, is_train, req, in_data, out_data, aux): #Note: see this run in notebooks/howto-numpy-random-proj.ipynb # Notation for shapes: b = batch_size, mnz = max_nonzero, d = proj_dim idx = in_data[0].asnumpy().astype('int32') # shape=(b,mnz) wd = self.W[idx] # shape= (b,mnz,d) mask = self._get_mask(idx, in_data) wd = np.multiply(wd,mask) # shape=(b,mnz,d), but zero'd out non-masked y = np.sum(wd,axis=1) # shape=(b,d) mxy = mx.nd.array(y) #NOTE: this hangs if the environment variables aren't set correctly # See https://github.com/dmlc/mxnet/issues/3813 self.assign(out_data[0], req[0], mxy) @mx.operator.register("SparseBOWProj") class RandomBagOfWordsProjectionProp(mx.operator.CustomOpProp): def __init__(self, vocab_size, output_dim): # need_top_grad=True means this is not a loss layer super(RandomBagOfWordsProjectionProp, self).__init__(need_top_grad=True) self._kwargs = { 'vocab_size': int(vocab_size), 'output_dim': int(output_dim), } def list_arguments(self): return ['indexes'] def list_outputs(self): return ['output'] def create_operator(self, ctx, shapes, dtypes, **kwargs): return RandomBagOfWordsProjection(**self._kwargs) def infer_shape(self, in_shape): batch_size = in_shape[0][0] output_shape = (batch_size, self._kwargs['output_dim']) return in_shape, [output_shape], [] class SparseRandomProjection(RandomBagOfWordsProjection): """Random projection of sparse input vector. Takes an sparse input layer, effectively in coordinate (COO) format, where the row number is implicit, because it's the minibatch record. See the simpler version RandomBagOfWordsProjection if all values are 1.0. """ def _get_mask(self, idx, in_data): """Returns the mask by which to multiply the parts of the embedding layer. In this version, we apply the weights. """ val = in_data[1].asnumpy() # shape=(b,mnz) mask = idx >= 0 # bool False for -1 values that should be removed. shape=(b,mnz) mask = np.multiply(mask,val) # All (b,mnz) mask = np.expand_dims(mask,2) # shape = (b,mnz,1) mask = np.repeat(mask, self._proj_dim, axis=2) # shape = (b,mnz,d) return mask @mx.operator.register("SparseRandomProjection") class SparseRandomProjectionProp(RandomBagOfWordsProjectionProp): def list_arguments(self): return ['indexes', 'values'] def create_operator(self, ctx, shapes, dtypes, **kwargs): return SparseRandomProjection(**self._kwargs) def infer_shape(self, in_shape): # check that indexes and values are the same shape. if in_shape[0] != in_shape[1]: raise ValueError("Input shapes differ. indexes:%s. values:%s. must be same" % (str(in_shape[0]),str(in_shape[1]))) return super(SparseRandomProjectionProp,self).infer_shape(in_shape) if __name__ == "__main__": print("Simple test of proj layer") data = mx.symbol.Variable('data') vals = mx.symbol.Variable('vals') net = mx.symbol.Custom(indexes=data, values=vals, name='rproj', op_type='SparseRandomProjection', vocab_size=999, output_dim=29) d = mx.nd.zeros(shape=(3,100)) v = mx.nd.ones(shape=(3,100)) e = net.bind(ctx=mx.cpu(), args={'data':d, 'vals':v}) e.forward() print(e.outputs[0].asnumpy()) print("Done with proj layer test") ''' OrderedSet implementation, from http://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set/1653978#1653978 The index() method and a few unit tests have been added. @author: Nathan Schneider (nschneid) @since: 2010-08-11 ''' # Strive towards Python 3 compatibility from __future__ import print_function, unicode_literals, division, absolute_import from future_builtins import map, filter import collections class OrderedSet(collections.OrderedDict, collections.MutableSet): ''' A set that preserves the ordering of its entries. >>> {3,2,9,2}=={9,2,3} True >>> x = OrderedSet([3,2,9,2]) >>> x == OrderedSet([2,9,3]) False >>> x == OrderedSet([3,2,3,9,2]) True >>> [y for y in x] [3, 2, 9] >>> x.index(2) 1 >>> x.index(0) Traceback (most recent call last): ... ValueError: 0 is not in set >>> [y for y in {3,2,9}] [9, 2, 3] ''' def update(self, *args, **kwargs): if kwargs: raise TypeError("update() takes no keyword arguments") for s in args: for e in s: self.add(e) def add(self, elem): self[elem] = None def discard(self, elem): self.pop(elem, None) def index(self, elem): try: return self.keys().index(elem) except ValueError: raise ValueError('{} is not in set'.format(elem)) def __le__(self, other): return all(e in other for e in self) def __lt__(self, other): return self <= other and self != other def __ge__(self, other): return all(e in self for e in other) def __gt__(self, other): return self >= other and self != other def __repr__(self): return 'OrderedSet([%s])' % (', '.join(map(repr, self.keys()))) def __str__(self): return '{%s}' % (', '.join(map(repr, self.keys()))) difference = property(lambda self: self.__sub__) difference_update = property(lambda self: self.__isub__) intersection = property(lambda self: self.__and__) intersection_update = property(lambda self: self.__iand__) issubset = property(lambda self: self.__le__) issuperset = property(lambda self: self.__ge__) symmetric_difference = property(lambda self: self.__xor__) symmetric_difference_update = property(lambda self: self.__ixor__) union = property(lambda self: self.__or__) def test(): import doctest doctest.testmod() if __name__=='__main__': test() from __future__ import unicode_literals, division, absolute_import, print_function import sys PY3 = sys.version_info[0] == 3 if PY3: text_type = str binary_type = bytes unicode = str else: range = xrange text_type = unicode binary_type = str __all__ = [ 'LXMLTreeBuilderForXML', 'LXMLTreeBuilder', ] from io import BytesIO if PY3: from io import StringIO else: from StringIO import StringIO import collections from lxml import etree from sigil_bs4.element import ( Comment, Doctype, NamespacedAttribute, ProcessingInstruction, ) from sigil_bs4.builder import ( FAST, HTML, HTMLTreeBuilder, PERMISSIVE, ParserRejectedMarkup, TreeBuilder, XML) from sigil_bs4.dammit import EncodingDetector LXML = 'lxml' class LXMLTreeBuilderForXML(TreeBuilder): DEFAULT_PARSER_CLASS = etree.XMLParser is_xml = True NAME = "lxml-xml" ALTERNATE_NAMES = ["xml"] # Well, it's permissive by XML parser standards. features = [NAME, LXML, XML, FAST, PERMISSIVE] CHUNK_SIZE = 512 # This namespace mapping is specified in the XML Namespace # standard. DEFAULT_NSMAPS = {'http://www.w3.org/XML/1998/namespace' : "xml"} def default_parser(self, encoding): # This can either return a parser object or a class, which # will be instantiated with default arguments. if self._default_parser is not None: return self._default_parser return etree.XMLParser( target=self, strip_cdata=False, recover=True, encoding=encoding) def parser_for(self, encoding): # Use the default parser. parser = self.default_parser(encoding) if isinstance(parser, collections.Callable): # Instantiate the parser with default arguments parser = parser(target=self, strip_cdata=False, encoding=encoding) return parser def __init__(self, parser=None, empty_element_tags=None): # TODO: Issue a warning if parser is present but not a # callable, since that means there's no way to create new # parsers for different encodings. self._default_parser = parser if empty_element_tags is not None: self.empty_element_tags = set(empty_element_tags) self.soup = None self.nsmaps = [self.DEFAULT_NSMAPS] def _getNsTag(self, tag): # Split the namespace URL out of a fully-qualified lxml tag # name. Copied from lxml's src/lxml/sax.py. if tag[0] == '{': return tuple(tag[1:].split('}', 1)) else: return (None, tag) def prepare_markup(self, markup, user_specified_encoding=None, exclude_encodings=None, document_declared_encoding=None): """ :yield: A series of 4-tuples. (markup, encoding, declared encoding, has undergone character replacement) Each 4-tuple represents a strategy for parsing the document. """ if isinstance(markup, unicode): # We were given Unicode. Maybe lxml can parse Unicode on # this system? yield markup, None, document_declared_encoding, False if isinstance(markup, unicode): # No, apparently not. Convert the Unicode to UTF-8 and # tell lxml to parse it as UTF-8. yield (markup.encode("utf8"), "utf8", document_declared_encoding, False) # Instead of using UnicodeDammit to convert the bytestring to # Unicode using different encodings, use EncodingDetector to # iterate over the encodings, and tell lxml to try to parse # the document as each one in turn. is_html = not self.is_xml try_encodings = [user_specified_encoding, document_declared_encoding] detector = EncodingDetector( markup, try_encodings, is_html, exclude_encodings) for encoding in detector.encodings: yield (detector.markup, encoding, document_declared_encoding, False) def feed(self, markup): if isinstance(markup, bytes): markup = BytesIO(markup) elif isinstance(markup, unicode): markup = StringIO(markup) # Call feed() at least once, even if the markup is empty, # or the parser won't be initialized. data = markup.read(self.CHUNK_SIZE) try: self.parser = self.parser_for(self.soup.original_encoding) self.parser.feed(data) while len(data) != 0: # Now call feed() on the rest of the data, chunk by chunk. data = markup.read(self.CHUNK_SIZE) if len(data) != 0: self.parser.feed(data) self.parser.close() except (UnicodeDecodeError, LookupError, etree.ParserError) as e: raise ParserRejectedMarkup(str(e)) def close(self): self.nsmaps = [self.DEFAULT_NSMAPS] def start(self, name, attrs, nsmap={}): # Make sure attrs is a mutable dict--lxml may send an immutable dictproxy. attrs = dict(attrs) nsprefix = None # Fix bug in bs4 _lxml.py that ignores attributes that specify namespaces on this tag # Invert each namespace map as it comes in. if len(nsmap) > 0: # A new namespace mapping has come into play. inverted_nsmap = dict((value, key) for key, value in list(nsmap.items())) self.nsmaps.append(inverted_nsmap) # Also treat the namespace mapping as a set of attributes on the # tag, so we can properly recreate it later. attrs = attrs.copy() for prefix, namespace in list(nsmap.items()): attribute = NamespacedAttribute( "xmlns", prefix, "http://www.w3.org/2000/xmlns/") attrs[attribute] = namespace elif len(self.nsmaps) > 1: # There are no new namespaces for this tag, but # non-default namespaces are in play, so we need a # separate tag stack to know when they end. self.nsmaps.append(None) # Namespaces are in play. Find any attributes that came in # from lxml with namespaces attached to their names, and # turn then into NamespacedAttribute objects. new_attrs = {} for attr, value in list(attrs.items()): namespace, attr = self._getNsTag(attr) if namespace is None: new_attrs[attr] = value else: nsprefix = self._prefix_for_attr_namespace(namespace) attr = NamespacedAttribute(nsprefix, attr, namespace) new_attrs[attr] = value attrs = new_attrs namespace, name = self._getNsTag(name) nsprefix = self._prefix_for_tag_namespace(namespace) self.soup.handle_starttag(name, namespace, nsprefix, attrs) def _prefix_for_attr_namespace(self, namespace): """Find the currently active prefix for the given namespace.""" if namespace is None: return None for inverted_nsmap in reversed(self.nsmaps): if inverted_nsmap is not None and namespace in inverted_nsmap: return inverted_nsmap[namespace] return None # To keep the tag prefixes as clean/simple as possible if there is # more than one possible prefix allowed and it includes None use it instead # This happens when a namespace prefix is added for an attribute that duplicates # an earlier namespace meant for tags that had set that namespace prefix to None def _prefix_for_tag_namespace(self, namespace): """Find the currently active prefix for the given namespace for a tag.""" if namespace is None: return None prefixes = [] for inverted_nsmap in self.nsmaps: if inverted_nsmap is not None and namespace in inverted_nsmap: prefixes.append(inverted_nsmap[namespace]) if len(prefixes) == 0 or None in prefixes: return None # ow return the last (most recent) viable prefix return prefixes[-1] def end(self, name): self.soup.endData() completed_tag = self.soup.tagStack[-1] namespace, name = self._getNsTag(name) nsprefix = self._prefix_for_tag_namespace(namespace) self.soup.handle_endtag(name, nsprefix) if len(self.nsmaps) > 1: # This tag, or one of its parents, introduced a namespace # mapping, so pop it off the stack. self.nsmaps.pop() def pi(self, target, data): self.soup.endData() self.soup.handle_data(target + ' ' + data) self.soup.endData(ProcessingInstruction) def data(self, content): self.soup.handle_data(content) def doctype(self, name, pubid, system): self.soup.endData() doctype = Doctype.for_name_and_ids(name, pubid, system) self.soup.object_was_parsed(doctype) def comment(self, content): "Handle comments as Comment objects." self.soup.endData() self.soup.handle_data(content) self.soup.endData(Comment) def test_fragment_to_document(self, fragment): """See `TreeBuilder`.""" return '\n%s' % fragment class LXMLTreeBuilder(HTMLTreeBuilder, LXMLTreeBuilderForXML): NAME = LXML ALTERNATE_NAMES = ["lxml-html"] features = ALTERNATE_NAMES + [NAME, HTML, FAST, PERMISSIVE] is_xml = False def default_parser(self, encoding): return etree.HTMLParser def feed(self, markup): encoding = self.soup.original_encoding try: self.parser = self.parser_for(encoding) self.parser.feed(markup) self.parser.close() except (UnicodeDecodeError, LookupError, etree.ParserError) as e: raise ParserRejectedMarkup(str(e)) def test_fragment_to_document(self, fragment): """See `TreeBuilder`.""" return '%s' % fragment import binascii import struct from django.forms import ValidationError from .const import ( GDAL_TO_POSTGIS, GDAL_TO_STRUCT, POSTGIS_HEADER_STRUCTURE, POSTGIS_TO_GDAL, STRUCT_SIZE, ) def pack(structure, data): """ Pack data into hex string with little endian format. """ return binascii.hexlify(struct.pack('<' + structure, *data)).upper() def unpack(structure, data): """ Unpack little endian hexlified binary string into a list. """ return struct.unpack('<' + structure, binascii.unhexlify(data)) def chunk(data, index): """ Split a string into two parts at the input index. """ return data[:index], data[index:] def get_pgraster_srid(data): """ Extract the SRID from a PostGIS raster string. """ if data is None: return # The positional arguments here extract the hex-encoded srid from the # header of the PostGIS raster string. This can be understood through # the POSTGIS_HEADER_STRUCTURE constant definition in the const module. return unpack('i', data[106:114])[0] def from_pgraster(data): """ Convert a PostGIS HEX String into a dictionary. """ if data is None: return # Split raster header from data header, data = chunk(data, 122) header = unpack(POSTGIS_HEADER_STRUCTURE, header) # Parse band data bands = [] pixeltypes = [] while data: # Get pixel type for this band pixeltype, data = chunk(data, 2) pixeltype = unpack('B', pixeltype)[0] # Subtract nodata byte from band nodata value if it exists has_nodata = pixeltype >= 64 if has_nodata: pixeltype -= 64 # Convert datatype from PostGIS to GDAL & get pack type and size pixeltype = POSTGIS_TO_GDAL[pixeltype] pack_type = GDAL_TO_STRUCT[pixeltype] pack_size = 2 * STRUCT_SIZE[pack_type] # Parse band nodata value. The nodata value is part of the # PGRaster string even if the nodata flag is True, so it always # has to be chunked off the data string. nodata, data = chunk(data, pack_size) nodata = unpack(pack_type, nodata)[0] # Chunk and unpack band data (pack size times nr of pixels) band, data = chunk(data, pack_size * header[10] * header[11]) band_result = {'data': binascii.unhexlify(band)} # If the nodata flag is True, set the nodata value. if has_nodata: band_result['nodata_value'] = nodata # Append band data to band list bands.append(band_result) # Store pixeltype of this band in pixeltypes array pixeltypes.append(pixeltype) # Check that all bands have the same pixeltype. # This is required by GDAL. PostGIS rasters could have different pixeltypes # for bands of the same raster. if len(set(pixeltypes)) != 1: raise ValidationError("Band pixeltypes are not all equal.") return { 'srid': int(header[9]), 'width': header[10], 'height': header[11], 'datatype': pixeltypes[0], 'origin': (header[5], header[6]), 'scale': (header[3], header[4]), 'skew': (header[7], header[8]), 'bands': bands, } def to_pgraster(rast): """ Convert a GDALRaster into PostGIS Raster format. """ # Return if the raster is null if rast is None or rast == '': return # Prepare the raster header data as a tuple. The first two numbers are # the endianness and the PostGIS Raster Version, both are fixed by # PostGIS at the moment. rasterheader = ( 1, 0, len(rast.bands), rast.scale.x, rast.scale.y, rast.origin.x, rast.origin.y, rast.skew.x, rast.skew.y, rast.srs.srid, rast.width, rast.height, ) # Hexlify raster header result = pack(POSTGIS_HEADER_STRUCTURE, rasterheader) for band in rast.bands: # The PostGIS raster band header has exactly two elements, a 8BUI byte # and the nodata value. # # The 8BUI stores both the PostGIS pixel data type and a nodata flag. # It is composed as the datatype integer plus 64 as a flag for existing # nodata values: # 8BUI_VALUE = PG_PIXEL_TYPE (0-11) + FLAG (0 or 64) # # For example, if the byte value is 71, then the datatype is # 71-64 = 7 (32BSI) and the nodata value is True. structure = 'B' + GDAL_TO_STRUCT[band.datatype()] # Get band pixel type in PostGIS notation pixeltype = GDAL_TO_POSTGIS[band.datatype()] # Set the nodata flag if band.nodata_value is not None: pixeltype += 64 # Pack band header bandheader = pack(structure, (pixeltype, band.nodata_value or 0)) # Hexlify band data band_data_hex = binascii.hexlify(band.data(as_memoryview=True)).upper() # Add packed header and band data to result result += bandheader + band_data_hex # Cast raster to string before passing it to the DB return result.decode() # -*- coding: utf-8 -*- """ magrathea.cli.commands.version ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :copyright: Copyright 2014 by the RootForum.org team, see AUTHORS. :license: MIT License, see LICENSE for details. """ import magrathea from ..base import BaseCommand class VersionCommand(BaseCommand): """ Command class implementing the version command. """ name = 'version' aliases = ('--version', '-v') help = 'Show version and copyright information' arguments = ( (('-s', '--short'), {'help': 'only print the version string', 'action': 'store_true'}), ) def handle(self): """Command handler for the version command""" if 'short' in self.args and self.args.short: self.log_notice(magrathea.get_version()) else: self.log_notice("""Magrathea version {version} Copyright (C) {year} by {author} This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. """.format( version=magrathea.get_version(), year=magrathea.COPYRIGHT[0], author=magrathea.COPYRIGHT[1]) ) self._status = 0 """\ A library of useful helper classes to the SAX classes, for the convenience of application and driver writers. """ import os, urllib.parse, urllib.request import io from . import handler from . import xmlreader def __dict_replace(s, d): """Replace substrings of a string using a dictionary.""" for key, value in d.items(): s = s.replace(key, value) return s def escape(data, entities={}): """Escape &, <, and > in a string of data. You can escape other strings of data by passing a dictionary as the optional entities parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. """ # must do ampersand first data = data.replace("&", "&") data = data.replace(">", ">") data = data.replace("<", "<") if entities: data = __dict_replace(data, entities) return data def unescape(data, entities={}): """Unescape &, <, and > in a string of data. You can unescape other strings of data by passing a dictionary as the optional entities parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. """ data = data.replace("<", "<") data = data.replace(">", ">") if entities: data = __dict_replace(data, entities) # must do ampersand last return data.replace("&", "&") def quoteattr(data, entities={}): """Escape and quote an attribute value. Escape &, <, and > in a string of data, then quote it for use as an attribute value. The \" character will be escaped as well, if necessary. You can escape other strings of data by passing a dictionary as the optional entities parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. """ entities = entities.copy() entities.update({'\n': ' ', '\r': ' ', '\t':' '}) data = escape(data, entities) if '"' in data: if "'" in data: data = '"%s"' % data.replace('"', """) else: data = "'%s'" % data else: data = '"%s"' % data return data def _gettextwriter(out, encoding): if out is None: import sys return sys.stdout if isinstance(out, io.TextIOBase): # use a text writer as is return out # wrap a binary writer with TextIOWrapper if isinstance(out, io.RawIOBase): # Keep the original file open when the TextIOWrapper is # destroyed class _wrapper: __class__ = out.__class__ def __getattr__(self, name): return getattr(out, name) buffer = _wrapper() buffer.close = lambda: None else: # This is to handle passed objects that aren't in the # IOBase hierarchy, but just have a write method buffer = io.BufferedIOBase() buffer.writable = lambda: True buffer.write = out.write try: # TextIOWrapper uses this methods to determine # if BOM (for UTF-16, etc) should be added buffer.seekable = out.seekable buffer.tell = out.tell except AttributeError: pass return io.TextIOWrapper(buffer, encoding=encoding, errors='xmlcharrefreplace', newline='\n', write_through=True) class XMLGenerator(handler.ContentHandler): def __init__(self, out=None, encoding="iso-8859-1", short_empty_elements=False): handler.ContentHandler.__init__(self) out = _gettextwriter(out, encoding) self._write = out.write self._flush = out.flush self._ns_contexts = [{}] # contains uri -> prefix dicts self._current_context = self._ns_contexts[-1] self._undeclared_ns_maps = [] self._encoding = encoding self._short_empty_elements = short_empty_elements self._pending_start_element = False def _qname(self, name): """Builds a qualified name from a (ns_url, localname) pair""" if name[0]: # Per http://www.w3.org/XML/1998/namespace, The 'xml' prefix is # bound by definition to http://www.w3.org/XML/1998/namespace. It # does not need to be declared and will not usually be found in # self._current_context. if 'http://www.w3.org/XML/1998/namespace' == name[0]: return 'xml:' + name[1] # The name is in a non-empty namespace prefix = self._current_context[name[0]] if prefix: # If it is not the default namespace, prepend the prefix return prefix + ":" + name[1] # Return the unqualified name return name[1] def _finish_pending_start_element(self,endElement=False): if self._pending_start_element: self._write('>') self._pending_start_element = False # ContentHandler methods def startDocument(self): self._write('\n' % self._encoding) def endDocument(self): self._flush() def startPrefixMapping(self, prefix, uri): self._ns_contexts.append(self._current_context.copy()) self._current_context[uri] = prefix self._undeclared_ns_maps.append((prefix, uri)) def endPrefixMapping(self, prefix): self._current_context = self._ns_contexts[-1] del self._ns_contexts[-1] def startElement(self, name, attrs): self._finish_pending_start_element() self._write('<' + name) for (name, value) in attrs.items(): self._write(' %s=%s' % (name, quoteattr(value))) if self._short_empty_elements: self._pending_start_element = True else: self._write(">") def endElement(self, name): if self._pending_start_element: self._write('/>') self._pending_start_element = False else: self._write('' % name) def startElementNS(self, name, qname, attrs): self._finish_pending_start_element() self._write('<' + self._qname(name)) for prefix, uri in self._undeclared_ns_maps: if prefix: self._write(' xmlns:%s="%s"' % (prefix, uri)) else: self._write(' xmlns="%s"' % uri) self._undeclared_ns_maps = [] for (name, value) in attrs.items(): self._write(' %s=%s' % (self._qname(name), quoteattr(value))) if self._short_empty_elements: self._pending_start_element = True else: self._write(">") def endElementNS(self, name, qname): if self._pending_start_element: self._write('/>') self._pending_start_element = False else: self._write('' % self._qname(name)) def characters(self, content): if content: self._finish_pending_start_element() self._write(escape(content)) def ignorableWhitespace(self, content): if content: self._finish_pending_start_element() self._write(content) def processingInstruction(self, target, data): self._finish_pending_start_element() self._write('' % (target, data)) class XMLFilterBase(xmlreader.XMLReader): """This class is designed to sit between an XMLReader and the client application's event handlers. By default, it does nothing but pass requests up to the reader and events on to the handlers unmodified, but subclasses can override specific methods to modify the event stream or the configuration requests as they pass through.""" def __init__(self, parent = None): xmlreader.XMLReader.__init__(self) self._parent = parent # ErrorHandler methods def error(self, exception): self._err_handler.error(exception) def fatalError(self, exception): self._err_handler.fatalError(exception) def warning(self, exception): self._err_handler.warning(exception) # ContentHandler methods def setDocumentLocator(self, locator): self._cont_handler.setDocumentLocator(locator) def startDocument(self): self._cont_handler.startDocument() def endDocument(self): self._cont_handler.endDocument() def startPrefixMapping(self, prefix, uri): self._cont_handler.startPrefixMapping(prefix, uri) def endPrefixMapping(self, prefix): self._cont_handler.endPrefixMapping(prefix) def startElement(self, name, attrs): self._cont_handler.startElement(name, attrs) def endElement(self, name): self._cont_handler.endElement(name) def startElementNS(self, name, qname, attrs): self._cont_handler.startElementNS(name, qname, attrs) def endElementNS(self, name, qname): self._cont_handler.endElementNS(name, qname) def characters(self, content): self._cont_handler.characters(content) def ignorableWhitespace(self, chars): self._cont_handler.ignorableWhitespace(chars) def processingInstruction(self, target, data): self._cont_handler.processingInstruction(target, data) def skippedEntity(self, name): self._cont_handler.skippedEntity(name) # DTDHandler methods def notationDecl(self, name, publicId, systemId): self._dtd_handler.notationDecl(name, publicId, systemId) def unparsedEntityDecl(self, name, publicId, systemId, ndata): self._dtd_handler.unparsedEntityDecl(name, publicId, systemId, ndata) # EntityResolver methods def resolveEntity(self, publicId, systemId): return self._ent_handler.resolveEntity(publicId, systemId) # XMLReader methods def parse(self, source): self._parent.setContentHandler(self) self._parent.setErrorHandler(self) self._parent.setEntityResolver(self) self._parent.setDTDHandler(self) self._parent.parse(source) def setLocale(self, locale): self._parent.setLocale(locale) def getFeature(self, name): return self._parent.getFeature(name) def setFeature(self, name, state): self._parent.setFeature(name, state) def getProperty(self, name): return self._parent.getProperty(name) def setProperty(self, name, value): self._parent.setProperty(name, value) # XMLFilter methods def getParent(self): return self._parent def setParent(self, parent): self._parent = parent # --- Utility functions def prepare_input_source(source, base=""): """This function takes an InputSource and an optional base URL and returns a fully resolved InputSource object ready for reading.""" if isinstance(source, str): source = xmlreader.InputSource(source) elif hasattr(source, "read"): f = source source = xmlreader.InputSource() source.setByteStream(f) if hasattr(f, "name"): source.setSystemId(f.name) if source.getByteStream() is None: sysid = source.getSystemId() basehead = os.path.dirname(os.path.normpath(base)) sysidfilename = os.path.join(basehead, sysid) if os.path.isfile(sysidfilename): source.setSystemId(sysidfilename) f = open(sysidfilename, "rb") else: source.setSystemId(urllib.parse.urljoin(base, sysid)) f = urllib.request.urlopen(source.getSystemId()) source.setByteStream(f) return source #! /usr/bin/python # ... try: from matplotlib import pyplot as plt PLOT=True except ImportError: PLOT=False # ... import numpy as np from pigasus.gallery.poisson import * import sys import inspect filename = inspect.getfile(inspect.currentframe()) # script filename (usually with path) # ... sin = np.sin ; cos = np.cos ; pi = np.pi ; exp = np.exp # ... #----------------------------------- try: nx = int(sys.argv[1]) except: nx = 31 try: ny = int(sys.argv[2]) except: ny = 31 try: px = int(sys.argv[3]) except: px = 2 try: py = int(sys.argv[4]) except: py = 2 from igakit.cad_geometry import quart_circle as domain geo = domain(n=[nx,ny],p=[px,py]) #----------------------------------- # ... # exact solution # ... R = 1. r = 0.5 c = 1. # for neumann #c = pi / (R**2-r**2) # for all dirichlet bc u = lambda x,y : [ x * y * sin ( c * (R**2 - x**2 - y**2 )) ] # ... # ... # rhs # ... f = lambda x,y : [4*c**2*x**3*y*sin(c*(R**2 - x**2 - y**2)) \ + 4*c**2*x*y**3*sin(c*(R**2 - x**2 - y**2)) \ + 12*c*x*y*cos(c*(R**2 - x**2 - y**2)) ] # ... # ... # values of gradu.n at the boundary # ... gradu = lambda x,y : [-2*c*x**2*y*cos(c*(R**2 - x**2 - y**2)) + y*sin(c*(R**2 - x**2 - y**2)) \ ,-2*c*x*y**2*cos(c*(R**2 - x**2 - y**2)) + x*sin(c*(R**2 - x**2 - y**2)) ] def func_g (x,y) : du = gradu (x, y) return [ du[0] , du[1] ] # ... # ... # values of u at the boundary # ... bc_neumann={} bc_neumann [0,0] = func_g Dirichlet = [[1,2,3]] #AllDirichlet = True # ... # ... try: bc_dirichlet except NameError: bc_dirichlet = None else: pass try: bc_neumann except NameError: bc_neumann = None else: pass try: AllDirichlet except NameError: AllDirichlet = None else: pass try: Dirichlet except NameError: Dirichlet = None else: pass try: Metric except NameError: Metric = None else: pass # ... # ... PDE = poisson(geometry=geo, bc_dirichlet=bc_dirichlet, bc_neumann=bc_neumann, AllDirichlet=AllDirichlet, Dirichlet=Dirichlet,metric=Metric) # ... # ... PDE.assembly(f=f) PDE.solve() # ... # ... normU = PDE.norm(exact=u) print "norm U = ", normU # ... # ... if PLOT: PDE.plot() ; plt.colorbar(); plt.title('$u_h$') plt.savefig(filename.split('.py')[0]+'.png', format='png') plt.clf() # ... PDE.free() #Constants, that don't rely on anything else in the module # Copyright (C) 2012- Sebastian Spaeth & contributors # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA try: from cStringIO import StringIO except ImportError: #python3 from io import StringIO default_conf=StringIO("""[general] #will be set automatically metadata = accounts = test ui = quiet [Account test] localrepository = Maildir remoterepository = IMAP [Repository Maildir] Type = Maildir # will be set automatically during tests localfolders = [Repository IMAP] type=IMAP # Don't hammer the server with too many connection attempts: maxconnections=1 folderfilter= lambda f: f.startswith('INBOX.OLItest') """) """ Django settings for fdp project. For more information on this file, see https://docs.djangoproject.com/en/1.7/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.7/ref/settings/ """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'j7y4q=&c=n0o9hdoc(ebkfj41k%wyhe&^zq!dqrwnwxgxbz&z+' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True TEMPLATE_DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'pontos', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) ROOT_URLCONF = 'fdp.urls' WSGI_APPLICATION = 'fdp.wsgi.application' # Database # https://docs.djangoproject.com/en/1.7/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Internationalization # https://docs.djangoproject.com/en/1.7/topics/i18n/ # LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'pt-br' # TIME_ZONE = 'UTC' TIME_ZONE = 'America/Sao_Paulo' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.7/howto/static-files/ STATIC_URL = '/static/' UPLOAD_DIR = os.path.join(BASE_DIR, 'static', 'uploads') STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static_root') STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static', 'static_dirs'), ) TEMPLATE_DIRS = ( os.path.join(BASE_DIR, 'templates'), ) """ Python Character Mapping Codec cp1253 generated from 'MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1253.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='cp1253', encode=Codec().encode, decode=Codec().decode, incrementalencoder=IncrementalEncoder, incrementaldecoder=IncrementalDecoder, streamreader=StreamReader, streamwriter=StreamWriter, ) ### Decoding Table decoding_table = ( u'\x00' # 0x00 -> NULL u'\x01' # 0x01 -> START OF HEADING u'\x02' # 0x02 -> START OF TEXT u'\x03' # 0x03 -> END OF TEXT u'\x04' # 0x04 -> END OF TRANSMISSION u'\x05' # 0x05 -> ENQUIRY u'\x06' # 0x06 -> ACKNOWLEDGE u'\x07' # 0x07 -> BELL u'\x08' # 0x08 -> BACKSPACE u'\t' # 0x09 -> HORIZONTAL TABULATION u'\n' # 0x0A -> LINE FEED u'\x0b' # 0x0B -> VERTICAL TABULATION u'\x0c' # 0x0C -> FORM FEED u'\r' # 0x0D -> CARRIAGE RETURN u'\x0e' # 0x0E -> SHIFT OUT u'\x0f' # 0x0F -> SHIFT IN u'\x10' # 0x10 -> DATA LINK ESCAPE u'\x11' # 0x11 -> DEVICE CONTROL ONE u'\x12' # 0x12 -> DEVICE CONTROL TWO u'\x13' # 0x13 -> DEVICE CONTROL THREE u'\x14' # 0x14 -> DEVICE CONTROL FOUR u'\x15' # 0x15 -> NEGATIVE ACKNOWLEDGE u'\x16' # 0x16 -> SYNCHRONOUS IDLE u'\x17' # 0x17 -> END OF TRANSMISSION BLOCK u'\x18' # 0x18 -> CANCEL u'\x19' # 0x19 -> END OF MEDIUM u'\x1a' # 0x1A -> SUBSTITUTE u'\x1b' # 0x1B -> ESCAPE u'\x1c' # 0x1C -> FILE SEPARATOR u'\x1d' # 0x1D -> GROUP SEPARATOR u'\x1e' # 0x1E -> RECORD SEPARATOR u'\x1f' # 0x1F -> UNIT SEPARATOR 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 -> DELETE u'\u20ac' # 0x80 -> EURO SIGN u'\ufffe' # 0x81 -> UNDEFINED u'\u201a' # 0x82 -> SINGLE LOW-9 QUOTATION MARK u'\u0192' # 0x83 -> LATIN SMALL LETTER F WITH HOOK u'\u201e' # 0x84 -> DOUBLE LOW-9 QUOTATION MARK u'\u2026' # 0x85 -> HORIZONTAL ELLIPSIS u'\u2020' # 0x86 -> DAGGER u'\u2021' # 0x87 -> DOUBLE DAGGER u'\ufffe' # 0x88 -> UNDEFINED u'\u2030' # 0x89 -> PER MILLE SIGN u'\ufffe' # 0x8A -> UNDEFINED u'\u2039' # 0x8B -> SINGLE LEFT-POINTING ANGLE QUOTATION MARK u'\ufffe' # 0x8C -> UNDEFINED u'\ufffe' # 0x8D -> UNDEFINED u'\ufffe' # 0x8E -> UNDEFINED u'\ufffe' # 0x8F -> UNDEFINED u'\ufffe' # 0x90 -> UNDEFINED u'\u2018' # 0x91 -> LEFT SINGLE QUOTATION MARK u'\u2019' # 0x92 -> RIGHT SINGLE QUOTATION MARK u'\u201c' # 0x93 -> LEFT DOUBLE QUOTATION MARK u'\u201d' # 0x94 -> RIGHT DOUBLE QUOTATION MARK u'\u2022' # 0x95 -> BULLET u'\u2013' # 0x96 -> EN DASH u'\u2014' # 0x97 -> EM DASH u'\ufffe' # 0x98 -> UNDEFINED u'\u2122' # 0x99 -> TRADE MARK SIGN u'\ufffe' # 0x9A -> UNDEFINED u'\u203a' # 0x9B -> SINGLE RIGHT-POINTING ANGLE QUOTATION MARK u'\ufffe' # 0x9C -> UNDEFINED u'\ufffe' # 0x9D -> UNDEFINED u'\ufffe' # 0x9E -> UNDEFINED u'\ufffe' # 0x9F -> UNDEFINED u'\xa0' # 0xA0 -> NO-BREAK SPACE u'\u0385' # 0xA1 -> GREEK DIALYTIKA TONOS u'\u0386' # 0xA2 -> GREEK CAPITAL LETTER ALPHA WITH TONOS u'\xa3' # 0xA3 -> POUND SIGN u'\xa4' # 0xA4 -> CURRENCY SIGN u'\xa5' # 0xA5 -> YEN SIGN u'\xa6' # 0xA6 -> BROKEN BAR u'\xa7' # 0xA7 -> SECTION SIGN u'\xa8' # 0xA8 -> DIAERESIS u'\xa9' # 0xA9 -> COPYRIGHT SIGN u'\ufffe' # 0xAA -> UNDEFINED u'\xab' # 0xAB -> LEFT-POINTING DOUBLE ANGLE QUOTATION MARK u'\xac' # 0xAC -> NOT SIGN u'\xad' # 0xAD -> SOFT HYPHEN u'\xae' # 0xAE -> REGISTERED SIGN u'\u2015' # 0xAF -> HORIZONTAL BAR u'\xb0' # 0xB0 -> DEGREE SIGN u'\xb1' # 0xB1 -> PLUS-MINUS SIGN u'\xb2' # 0xB2 -> SUPERSCRIPT TWO u'\xb3' # 0xB3 -> SUPERSCRIPT THREE u'\u0384' # 0xB4 -> GREEK TONOS u'\xb5' # 0xB5 -> MICRO SIGN u'\xb6' # 0xB6 -> PILCROW SIGN u'\xb7' # 0xB7 -> MIDDLE DOT u'\u0388' # 0xB8 -> GREEK CAPITAL LETTER EPSILON WITH TONOS u'\u0389' # 0xB9 -> GREEK CAPITAL LETTER ETA WITH TONOS u'\u038a' # 0xBA -> GREEK CAPITAL LETTER IOTA WITH TONOS u'\xbb' # 0xBB -> RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK u'\u038c' # 0xBC -> GREEK CAPITAL LETTER OMICRON WITH TONOS u'\xbd' # 0xBD -> VULGAR FRACTION ONE HALF u'\u038e' # 0xBE -> GREEK CAPITAL LETTER UPSILON WITH TONOS u'\u038f' # 0xBF -> GREEK CAPITAL LETTER OMEGA WITH TONOS u'\u0390' # 0xC0 -> GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS u'\u0391' # 0xC1 -> GREEK CAPITAL LETTER ALPHA u'\u0392' # 0xC2 -> GREEK CAPITAL LETTER BETA u'\u0393' # 0xC3 -> GREEK CAPITAL LETTER GAMMA u'\u0394' # 0xC4 -> GREEK CAPITAL LETTER DELTA u'\u0395' # 0xC5 -> GREEK CAPITAL LETTER EPSILON u'\u0396' # 0xC6 -> GREEK CAPITAL LETTER ZETA u'\u0397' # 0xC7 -> GREEK CAPITAL LETTER ETA u'\u0398' # 0xC8 -> GREEK CAPITAL LETTER THETA u'\u0399' # 0xC9 -> GREEK CAPITAL LETTER IOTA u'\u039a' # 0xCA -> GREEK CAPITAL LETTER KAPPA u'\u039b' # 0xCB -> GREEK CAPITAL LETTER LAMDA u'\u039c' # 0xCC -> GREEK CAPITAL LETTER MU u'\u039d' # 0xCD -> GREEK CAPITAL LETTER NU u'\u039e' # 0xCE -> GREEK CAPITAL LETTER XI u'\u039f' # 0xCF -> GREEK CAPITAL LETTER OMICRON u'\u03a0' # 0xD0 -> GREEK CAPITAL LETTER PI u'\u03a1' # 0xD1 -> GREEK CAPITAL LETTER RHO u'\ufffe' # 0xD2 -> UNDEFINED u'\u03a3' # 0xD3 -> GREEK CAPITAL LETTER SIGMA u'\u03a4' # 0xD4 -> GREEK CAPITAL LETTER TAU u'\u03a5' # 0xD5 -> GREEK CAPITAL LETTER UPSILON u'\u03a6' # 0xD6 -> GREEK CAPITAL LETTER PHI u'\u03a7' # 0xD7 -> GREEK CAPITAL LETTER CHI u'\u03a8' # 0xD8 -> GREEK CAPITAL LETTER PSI u'\u03a9' # 0xD9 -> GREEK CAPITAL LETTER OMEGA u'\u03aa' # 0xDA -> GREEK CAPITAL LETTER IOTA WITH DIALYTIKA u'\u03ab' # 0xDB -> GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA u'\u03ac' # 0xDC -> GREEK SMALL LETTER ALPHA WITH TONOS u'\u03ad' # 0xDD -> GREEK SMALL LETTER EPSILON WITH TONOS u'\u03ae' # 0xDE -> GREEK SMALL LETTER ETA WITH TONOS u'\u03af' # 0xDF -> GREEK SMALL LETTER IOTA WITH TONOS u'\u03b0' # 0xE0 -> GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS u'\u03b1' # 0xE1 -> GREEK SMALL LETTER ALPHA u'\u03b2' # 0xE2 -> GREEK SMALL LETTER BETA u'\u03b3' # 0xE3 -> GREEK SMALL LETTER GAMMA u'\u03b4' # 0xE4 -> GREEK SMALL LETTER DELTA u'\u03b5' # 0xE5 -> GREEK SMALL LETTER EPSILON u'\u03b6' # 0xE6 -> GREEK SMALL LETTER ZETA u'\u03b7' # 0xE7 -> GREEK SMALL LETTER ETA u'\u03b8' # 0xE8 -> GREEK SMALL LETTER THETA u'\u03b9' # 0xE9 -> GREEK SMALL LETTER IOTA u'\u03ba' # 0xEA -> GREEK SMALL LETTER KAPPA u'\u03bb' # 0xEB -> GREEK SMALL LETTER LAMDA u'\u03bc' # 0xEC -> GREEK SMALL LETTER MU u'\u03bd' # 0xED -> GREEK SMALL LETTER NU u'\u03be' # 0xEE -> GREEK SMALL LETTER XI u'\u03bf' # 0xEF -> GREEK SMALL LETTER OMICRON u'\u03c0' # 0xF0 -> GREEK SMALL LETTER PI u'\u03c1' # 0xF1 -> GREEK SMALL LETTER RHO u'\u03c2' # 0xF2 -> GREEK SMALL LETTER FINAL SIGMA u'\u03c3' # 0xF3 -> GREEK SMALL LETTER SIGMA u'\u03c4' # 0xF4 -> GREEK SMALL LETTER TAU u'\u03c5' # 0xF5 -> GREEK SMALL LETTER UPSILON u'\u03c6' # 0xF6 -> GREEK SMALL LETTER PHI u'\u03c7' # 0xF7 -> GREEK SMALL LETTER CHI u'\u03c8' # 0xF8 -> GREEK SMALL LETTER PSI u'\u03c9' # 0xF9 -> GREEK SMALL LETTER OMEGA u'\u03ca' # 0xFA -> GREEK SMALL LETTER IOTA WITH DIALYTIKA u'\u03cb' # 0xFB -> GREEK SMALL LETTER UPSILON WITH DIALYTIKA u'\u03cc' # 0xFC -> GREEK SMALL LETTER OMICRON WITH TONOS u'\u03cd' # 0xFD -> GREEK SMALL LETTER UPSILON WITH TONOS u'\u03ce' # 0xFE -> GREEK SMALL LETTER OMEGA WITH TONOS u'\ufffe' # 0xFF -> UNDEFINED ) ### Encoding table encoding_table=codecs.charmap_build(decoding_table) # -*- coding: utf-8 -*- from web import form import re import db # Expresiones regulares necesarias: #formatoVisa=re.compile(r'[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}') # Funciones necesarias para las validaciones def fooFunction(): pass """ EJEMPLO DE FORMULARIO PARA DARSE DE ALTA formularioInscripcion = form.Form( form.Textbox( "nombre", form.notnull, class_="form-control", id="nombreId", description="Nombre: " ), form.Textbox( "apellidos", form.notnull, class_="form-control", id="apellidosId", description="Apellidos: " ), form.Textbox( "dni", form.notnull, class_="form-control", id="dniId", description="DNI: " ), form.Textbox( "email", form.notnull, form.regexp(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}', 'Formato de email incorrecto'), class_="form-control", id="emailId", description=u"Correo electrónico: " ), form.Dropdown( "dia", [(d, d) for d in range(1,32)], id="diaID", description=u"Día de nacimiento: ", ), form.Dropdown( "mes", [(1,'Enero'),(2,'Febrero'),(3,'Marzo'),(4,'Abril'),(5,'Mayo'),(6,'Junio'), (7,'Julio'),(8,'Agosto'),(9,'Septiembre'),(10,'Octubre'),(11,'Noviembre'),(12,'Diciembre')], id="mesID", description="Mes de nacimiento: " ), form.Dropdown( "anio", [d for d in range(1930,2006)], id="anioID", description=u"Año de nacimiento: " ), form.Textarea( "direccion", form.notnull, class_="form-control", id="direccionId", description=u"Dirección: " ), form.Textbox( "username", form.notnull, class_="form-control", id="usernameId", description="Nombre de usuario: " ), form.Password( "password1", form.notnull, class_="form-control", id="password1Id", description=u"Contraseña: " ), form.Password( "password2", form.notnull, class_="form-control", id="password2Id", description=u"Repita la contraseña: " ), form.Radio( 'formaPago', [["VISA","VISA "],["contraReembolso","Contra reembolso"]], form.notnull, id="formaPagoId", description="Forma de pago: " ), form.Textbox( "visa", class_="form-control", id="visaId", description="Número de tarjeta VISA: ", ), form.Checkbox( "acepto", description="Acepto las condiciones de uso ", id="aceptoId", value="si" ), validators = [ form.Validator(u"Fecha incorrecta", lambda x: ((int(x.mes)==2 and int(x.dia)<=28)) or (int(x.mes) in [4,6,9,11] and int(x.dia)<31) or (int(x.mes) in [1,3,5,7,8,10,12]) or (int(x.mes)==2 and int(x.dia)==29 and esBisiesto(x.anio))), form.Validator(u"La contraseña debe tener al menos 7 caracteres",lambda x: len(x.password1)>6), form.Validator(u"Las contraseñas no coinciden", lambda x: x.password1 == x.password2), form.Validator(u"Debe introducir un número de tarjeta válido",lambda x: (x.formaPago=="contraReembolso") or (x.formaPago=="VISA" and formatoVisa.match(x.visa))), form.Validator(u"Debe aceptar los términos y condiciones",lambda x: x.acepto=="si") ] ) """ # Copyright 2018 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. # ============================================================================= """Loader for the custom inc_op.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function import platform if platform.system() != "Windows": # pylint: disable=g-import-not-at-top from tensorflow.contrib.util import loader from tensorflow.python.platform import resource_loader # pylint: enable=g-import-not-at-top _inc_op = loader.load_op_library( resource_loader.get_path_to_datafile("_inc_op.so")) else: raise RuntimeError("Windows not supported") # -*- coding: utf-8 -*- """ Created on Fri Mar 25 09:42:22 2016 @author: bing """ import numpy as np #import scipy import numba import sys import math bohr_angstrom = 0.52917721092 hartree_wavenumber = 219474.63 #hartree_wavenumber = scipy.constants.value(u'hartree-inverse meter relationship') / 1e2 def M1mat(a, Nb): M1 = np.zeros((Nb,Nb)) for m in range(Nb-1): M1[m,m+1] = np.sqrt(float(m+1)/2.0/a) M1 = Sym(M1) return M1 def M2mat(a, Nb): M2 = np.zeros((Nb,Nb)) for m in range(Nb): M2[m,m] = (float(m) + 0.5)/a if Nb > 1: for m in range(Nb-2): M2[m,m+2] = np.sqrt(float((m+1)*(m+2)))/2.0/a M2 = Sym(M2) return M2 def M3mat(a, Nb): M3 = np.zeros((Nb,Nb)) for m in range(Nb-1): M3[m,m+1] = 3.0 * (float(m+1)/2./a)**1.5 if Nb > 2: for m in range(Nb-3): M3[m,m+3] = np.sqrt(float((m+1)*(m+2)*(m+3))) / (2.0*a)**1.5 M3 = Sym(M3) return M3 def M4mat(a, Nb): M4 = np.zeros((Nb,Nb)) for m in range(Nb): M4[m,m] = float(3.0 * m**2 + 3.0 * (m+1)**2) / (2.*a)**2 if Nb > 1: for m in range(Nb-2): M4[m,m+2] = (4.0*m + 6.0) * np.sqrt(float((m+1)*(m+2))) / (2.*a)**2 if Nb > 3: for m in range(Nb-4): M4[m,m+4] = np.sqrt(float((m+1)*(m+2)*(m+3)*(m+4))) / (2.0*a)**2 M4 = Sym(M4) if Nb > 1: if not M4[0,1] == M4[1,0]: print(M4) print('\n ERROR: Not symmetric matrix M4.\n') sys.exit() return M4 def Hermite(x): cons = np.array([1. / np.sqrt(float(2**n) * float(math.factorial(n))) for n in range(Nb)]) H = [] H.append(1.0) H.append( x * 2.0 ) if Nb > 2: for n in range(2,Nb): Hn = 2.0 * x * H[n-1] - 2.0*(n-1) * H[n-2] H.append(Hn) for n in range(Nb): H[n] = H[n]*cons[n] return H # if n == 0: # H.append(1.) # elif n == 1: # return 2. * x * cons # elif n == 2: # return (4. * x**2 - 2.) * cons # elif n == 3: # return (8.0 * x**3 - 12.0 * x) * cons # elif n == 4: # return (16.0 * x**4 - 48.0 * x**2 + 12.0) * cons # elif n == 5: # return (32.0*x**5 - 160.0*x**3 + 120.0*x) * cons # elif n == 6: # return () #def Vx(x): # g = 0.1 # return x**2/2.0 + g * x**4 / 4.0 def Kmat(alpha,pAve, Nb): K = np.zeros((Nb,Nb),dtype=complex) ar = alpha.real for j in range(Nb): K[j,j] = np.abs(alpha)**2 / ar * (2. * j + 1.)/2. + pAve**2 for j in range(1,Nb): K[j-1,j] = -1j*np.conj(alpha) * pAve * np.sqrt(2. * j / ar) K[j,j-1] = np.conj(K[j-1,j]) if Nb > 2: for j in range(2,Nb): K[j-2,j] = - np.sqrt(float((j-1)*j)) * np.conj(alpha)**2 / 2. / ar K[j,j-2] = np.conj(K[j-2,j]) #K[0,0] = np.abs(alpha)**2/alpha.real / 2. + pAve**2 #K[1,1] = np.abs(alpha)**2/alpha.real * 3.0 / 2. + pAve**2 #K[0,1] = -1j*np.conj(alpha) * pAve * np.sqrt(2.*j/alpha.real) #K[1,0] = np.conj(K[0,1]) K = K / (2.*amx) return K def Sym(V): n = V.shape[-1] for i in range(n): for j in range(i): V[i,j] = V[j,i] return V @numba.autojit def Vint(x,y): """ interaction potential between x and y """ PES = 'HO' if PES == 'Morse': a, x0 = 1.02, 1.4 De = 0.176 / 100.0 d = (1.0-np.exp(-a*x)) v0 = De*d**2 dv = 2. * De * d * a * np.exp(-a*x) elif PES == 'HO': v0 = x**2/2.0 + y**2/2.0 elif PES == 'AHO': eps = 0.4 v0 = x**2/2.0 + eps * x**4/4.0 dv = x + eps * x**3 #ddv = 2.0 * De * (-d*np.exp(-a*((x-x0)))*a**2 + (np.exp(-a*(x-x0)))**2*a**2) # elif PES == 'pH2': # # dx = 1e-4 # # v0 = np.zeros(Ntraj) # dv = np.zeros(Ntraj) # # for i in range(Ntraj): # v0[i] = vpot(x[i]) # dv[i] = ( vpot(x[i] + dx) - v0[i])/dx return v0 def Vy(y): v0 = y**2/2.0 dv = y return v0,dv def LQF(x,w): xAve = np.dot(x,w) xSqdAve = np.dot(x*x,w) var = (xSqdAve - xAve**2) a = 1. / 2. / var r = - a * (x-xAve) dr = - a uAve = (np.dot(r**2,w))/2./amy du = -1./amy * (r*dr) return r, du, uAve @numba.autojit def qpot(x,p,r,w): """ Linear Quantum Force : direct polynomial fitting of derivative-log density (amplitude) curve_fit : randomly choose M points and do a nonlinear least-square fitting to a predefined functional form """ #tau = (max(xdata) - min(xdata))/(max(x) - min(x)) #if tau > 0.6: # pass #else: # print('Data points are not sampled well.' am= amy Nb = 2 S = np.zeros((Nb,Nb)) for j in range(Nb): for k in range(Nb): S[j,k] = np.dot(x**(j+k), w) bp = np.zeros(Nb) br = np.zeros(Nb) for n in range(Nb): bp[n] = np.dot(x**n * p, w) br[n] = np.dot(x**n * r, w) cp = np.linalg.solve(S,bp) cr = np.linalg.solve(S,br) #unit = np.identity(Nb) #r_approx = cr[0] * unit + cr[1] * x + cr[2] * x**2 + cr[3] * x**3 #p_approx = cp[0] * unit + cp[1] * x + cp[2] * x**2 + cp[3] * x**3 N = len(x) dr = np.zeros(N) dp = np.zeros(N) ddr = np.zeros(N) ddp = np.zeros(N) for k in range(1,Nb): dr += float(k) * cr[k] * x**(k-1) dp += float(k) * cp[k] * x**(k-1) for k in range(2,Nb-1): ddr += float(k * (k-1)) * cr[k] * x**(k-2) ddp += float(k * (k-1)) * cp[k] * x**(k-2) fr = -1./2./am * (2. * r * dp + ddp) fq = 1./2./am * (2. * r * dr + ddr) Eu = -1./2./am * np.dot(r**2 + dr,w) return Eu,fq,fr # initialization # for DOF y : an ensemble of trajectories # for DOF x : for each trajectory associate a complex vector c of dimension M Ntraj = 1024 M = 16 nfit = 2 ax = 1.0 # width of the GH basis ay0 = 4.0 y0 = 0.0 print('polynomial fitting of c, order = {} \n'.format(nfit)) # initial conditions for c c = np.zeros((Ntraj,M),dtype=np.complex128) # mixture of ground and first excited state #c[:,0] = 1.0/np.sqrt(2.0)+0j #c[:,1] = 1.0/np.sqrt(2.0)+0j #for i in range(2,M): # c[:,i] = 0.0+0.0j # coherent state z = 1.0/np.sqrt(2.0) for i in range(M): c[:,i] = np.exp(-0.5 * np.abs(z)**2) * z**i / np.sqrt(math.factorial(i)) print('initial occupation \n',c[0,:]) print('trace of density matrix',np.vdot(c[0,:], c[0,:])) # --------------------------------- # initial conditions for QTs y = np.random.randn(Ntraj) y = y / np.sqrt(2.0 * ay0) + y0 print('trajectory range {}, {}'.format(min(y),max(y))) py = np.zeros(Ntraj) ry = - ay0 * (y-y0) w = np.array([1./Ntraj]*Ntraj) # ------------------------------- amx = 1.0 amy = 10.0 f_MSE = open('rMSE.out','w') nout = 20 # number of trajectories to print fmt = ' {}' * (nout+1) + '\n' Eu = 0. Ndim = 1 # dimensionality of the bath fric_cons = 0.0 # friction constant Nt = 2**14 dt = 1.0/2.0**10 dt2 = dt/2.0 t = 0.0 print('time range for propagation is [0,{}]'.format(Nt*dt)) print('timestep = {}'.format(dt)) # construct the Hamiltonian matrix for anharmonic oscilator g = 0.4 V = 0.5 * M2mat(ax,M) + g/4.0 * M4mat(ax,M) K = Kmat(ax,0.0,M) H = K+V print('Hamiltonian matrix in DOF x = \n') print(H) print('\n') @numba.autojit def norm(c,w): anm = 0.0 for k in range(Ntraj): anm += np.vdot(c[k,:], c[k,:]).real * w[k] return anm @numba.autojit def fit_c(c,y): """ global approximation of c vs y to obtain the derivative c'',c' """ dc = np.zeros((Ntraj,M),dtype=np.complex128) ddc = np.zeros((Ntraj,M),dtype=np.complex128) for j in range(M): z = c[:,j] pars = np.polyfit(y,z,nfit) p0 = np.poly1d(pars) p1 = np.polyder(p0) p2 = np.polyder(p1) #for k in range(Ntraj): dc[:,j] = p1(y) ddc[:,j] = p2(y) return dc, ddc @numba.autojit def prop_c(H,c,y,ry,py): dc, ddc = fit_c(c,y) dcdt = np.zeros([Ntraj,M],dtype=np.complex128) eps = 0.50 # bilinear coupling Vint = eps*x*y X1 = M1mat(ax,M) for k in range(Ntraj): Vp = eps * y[k] * X1 tmp = (H + Vp).dot(c[k,:]) - ddc[k,:]/2.0/amy - dc[k,:] * ry[k]/amy dcdt[k,:] = -1j * tmp return dcdt @numba.autojit def xAve(c,y,w): """ compute expectation value of x """ Xmat = M1mat(ax,M) x_ave = 0.0+0.0j for k in range(Ntraj): for m in range(M): for n in range(M): x_ave += Xmat[m,n] * np.conjugate(c[k,m]) * c[k,n] * w[k] return x_ave.real # propagate the QTs for y # update the coeffcients for each trajectory fmt_c = ' {} '* (M+1) f = open('traj.dat','w') fe = open('en.out','w') fc = open('c.dat','w') fx = open('xAve.dat','w') fnorm = open('norm.dat', 'w') v0, dv = Vy(y) ry, du, Eu = LQF(y,w) cold = c dcdt = prop_c(H,c,y,ry,py) c = c + dcdt * dt for k in range(Nt): t = t + dt py += (- dv - du) * dt2 - fric_cons * py * dt2 y += py*dt/amy # force field ry, du, Eu = LQF(y,w) v0, dv = Vy(y) py += (- dv - du) * dt2 - fric_cons * py * dt2 # renormalization anm = norm(c,w) c /= np.sqrt(anm) # update c dcdt = prop_c(H,c,y,ry,py) cnew = cold + dcdt * dt * 2.0 cold = c c = cnew # output data for each timestep # d = c # for k in range(Ntraj): # for i in range(M): # d[k,i] = np.exp(-1j*t*H[i,i])*c[k,i] x_ave = xAve(c,y,w) fx.write('{} {} \n'.format(t,x_ave)) f.write(fmt.format(t,*y[0:nout])) fnorm.write(' {} {} \n'.format(t,anm)) Ek = np.dot(py*py,w)/2./amy Ev = np.dot(v0,w) Eu = Eu Etot = Ek + Ev + Eu fe.write('{} {} {} {} {} \n'.format(t,Ek,Ev,Eu,Etot)) print('The total energy = {} Hartree. \n'.format(Etot)) # print trajectory and coefficients for k in range(Ntraj): fc.write( '{} {} {} {} \n'.format(y[k], c[k,0],c[k,-2],c[k,-1])) fe.close() f.close() fc.close() fx.close() #a, x0, De = 1.02, 1.4, 0.176/100 #print('The well depth = {} cm-1. \n'.format(De * hartree_wavenumber)) # #omega = a * np.sqrt(2. * De / am ) #E0 = omega/2. - omega**2/16./De #dE = (Etot-E0) * hartree_wavenumber #print('Exact ground-state energy = {} Hartree. \nEnergy deviation = {} cm-1. \n'.format(E0,dE)) # # -*- coding: utf-8 -*- """ 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 . """ from time import mktime, strptime from module.plugins.Account import Account from module.common.json_layer import json_loads class FileserveCom(Account): __name__ = "FileserveCom" __version__ = "0.2" __type__ = "account" __description__ = """Fileserve.com account plugin""" __author_name__ = "mkaay" __author_mail__ = "mkaay@mkaay.de" def loadAccountInfo(self, user, req): data = self.getAccountData(user) page = req.load("http://app.fileserve.com/api/login/", post={"username": user, "password": data['password'], "submit": "Submit+Query"}) res = json_loads(page) if res['type'] == "premium": validuntil = mktime(strptime(res['expireTime'], "%Y-%m-%d %H:%M:%S")) return {"trafficleft": res['traffic'], "validuntil": validuntil} else: return {"premium": False, "trafficleft": None, "validuntil": None} def login(self, user, data, req): page = req.load("http://app.fileserve.com/api/login/", post={"username": user, "password": data['password'], "submit": "Submit+Query"}) res = json_loads(page) if not res['type']: self.wrongPassword() #login at fileserv page req.load("http://www.fileserve.com/login.php", post={"loginUserName": user, "loginUserPassword": data['password'], "autoLogin": "checked", "loginFormSubmit": "Login"}) import os from django.template import Context, Engine, TemplateDoesNotExist from django.template.loader_tags import ExtendsError from django.template.loaders.base import Loader from django.test import SimpleTestCase, ignore_warnings from django.utils.deprecation import RemovedInDjango20Warning from .utils import ROOT RECURSIVE = os.path.join(ROOT, 'recursive_templates') class ExtendsBehaviorTests(SimpleTestCase): def test_normal_extend(self): engine = Engine(dirs=[os.path.join(RECURSIVE, 'fs')]) template = engine.get_template('one.html') output = template.render(Context({})) self.assertEqual(output.strip(), 'three two one') def test_extend_recursive(self): engine = Engine(dirs=[ os.path.join(RECURSIVE, 'fs'), os.path.join(RECURSIVE, 'fs2'), os.path.join(RECURSIVE, 'fs3'), ]) template = engine.get_template('recursive.html') output = template.render(Context({})) self.assertEqual(output.strip(), 'fs3/recursive fs2/recursive fs/recursive') def test_extend_missing(self): engine = Engine(dirs=[os.path.join(RECURSIVE, 'fs')]) template = engine.get_template('extend-missing.html') with self.assertRaises(TemplateDoesNotExist) as e: template.render(Context({})) tried = e.exception.tried self.assertEqual(len(tried), 1) self.assertEqual(tried[0][0].template_name, 'missing.html') def test_recursive_multiple_loaders(self): engine = Engine( dirs=[os.path.join(RECURSIVE, 'fs')], loaders=[ ('django.template.loaders.locmem.Loader', { 'one.html': '{% extends "one.html" %}{% block content %}{{ block.super }} locmem-one{% endblock %}', 'two.html': '{% extends "two.html" %}{% block content %}{{ block.super }} locmem-two{% endblock %}', 'three.html': ( '{% extends "three.html" %}{% block content %}{{ block.super }} locmem-three{% endblock %}' ), }), 'django.template.loaders.filesystem.Loader', ], ) template = engine.get_template('one.html') output = template.render(Context({})) self.assertEqual(output.strip(), 'three locmem-three two locmem-two one locmem-one') def test_extend_self_error(self): """ Catch if a template extends itself and no other matching templates are found. """ engine = Engine(dirs=[os.path.join(RECURSIVE, 'fs')]) template = engine.get_template('self.html') with self.assertRaises(TemplateDoesNotExist): template.render(Context({})) def test_extend_cached(self): engine = Engine( dirs=[ os.path.join(RECURSIVE, 'fs'), os.path.join(RECURSIVE, 'fs2'), os.path.join(RECURSIVE, 'fs3'), ], loaders=[ ('django.template.loaders.cached.Loader', [ 'django.template.loaders.filesystem.Loader', ]), ], ) template = engine.get_template('recursive.html') output = template.render(Context({})) self.assertEqual(output.strip(), 'fs3/recursive fs2/recursive fs/recursive') cache = engine.template_loaders[0].get_template_cache self.assertEqual(len(cache), 3) expected_path = os.path.join('fs', 'recursive.html') self.assertTrue(cache['recursive.html'].origin.name.endswith(expected_path)) # Render another path that uses the same templates from the cache template = engine.get_template('other-recursive.html') output = template.render(Context({})) self.assertEqual(output.strip(), 'fs3/recursive fs2/recursive fs/recursive') # Template objects should not be duplicated. self.assertEqual(len(cache), 4) expected_path = os.path.join('fs', 'other-recursive.html') self.assertTrue(cache['other-recursive.html'].origin.name.endswith(expected_path)) def test_unique_history_per_loader(self): """ Extending should continue even if two loaders return the same name for a template. """ engine = Engine( loaders=[ ['django.template.loaders.locmem.Loader', { 'base.html': '{% extends "base.html" %}{% block content %}{{ block.super }} loader1{% endblock %}', }], ['django.template.loaders.locmem.Loader', { 'base.html': '{% block content %}loader2{% endblock %}', }], ] ) template = engine.get_template('base.html') output = template.render(Context({})) self.assertEqual(output.strip(), 'loader2 loader1') class NonRecursiveLoader(Loader): def __init__(self, engine, templates_dict): self.templates_dict = templates_dict super(NonRecursiveLoader, self).__init__(engine) def load_template_source(self, template_name, template_dirs=None): try: return self.templates_dict[template_name], template_name except KeyError: raise TemplateDoesNotExist(template_name) @ignore_warnings(category=RemovedInDjango20Warning) class NonRecursiveLoaderExtendsTests(SimpleTestCase): loaders = [ ('template_tests.test_extends.NonRecursiveLoader', { 'base.html': 'base', 'index.html': '{% extends "base.html" %}', 'recursive.html': '{% extends "recursive.html" %}', 'other-recursive.html': '{% extends "recursive.html" %}', 'a.html': '{% extends "b.html" %}', 'b.html': '{% extends "a.html" %}', }), ] def test_extend(self): engine = Engine(loaders=self.loaders) output = engine.render_to_string('index.html') self.assertEqual(output, 'base') def test_extend_cached(self): engine = Engine(loaders=[ ('django.template.loaders.cached.Loader', self.loaders), ]) output = engine.render_to_string('index.html') self.assertEqual(output, 'base') cache = engine.template_loaders[0].template_cache self.assertTrue('base.html' in cache) self.assertTrue('index.html' in cache) # Render a second time from cache output = engine.render_to_string('index.html') self.assertEqual(output, 'base') def test_extend_error(self): engine = Engine(loaders=self.loaders) msg = 'Cannot extend templates recursively when using non-recursive template loaders' with self.assertRaisesMessage(ExtendsError, msg): engine.render_to_string('recursive.html') with self.assertRaisesMessage(ExtendsError, msg): engine.render_to_string('other-recursive.html') with self.assertRaisesMessage(ExtendsError, msg): engine.render_to_string('a.html') # Copyright 2013 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. class Patcher(object): def GetPatchedFiles(self, version=None): '''Returns patched files as(added_files, deleted_files, modified_files) from the patchset specified by |version|. ''' raise NotImplementedError(self.__class__) def GetVersion(self): '''Returns patch version. Returns None when nothing is patched by the patcher. ''' raise NotImplementedError(self.__class__) def Apply(self, paths, file_system, version=None): '''Apply the patch to added/modified files. Returns Future with patched data. Throws FileNotFoundError if |paths| contains deleted files. ''' raise NotImplementedError(self.__class__) def GetIdentity(self): '''Returns a string that identifies this patch. Typically it would be the codereview server's ID for this patch. ''' raise NotImplementedError(self.__class__) # Copyright 2019 Sergio Teruel # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import fields, models class WizStockBarcodesNewLot(models.TransientModel): _inherit = 'barcodes.barcode_events_mixin' _name = 'wiz.stock.barcodes.new.lot' _description = 'Wizard to create new lot from barcode scanner' product_id = fields.Many2one( comodel_name='product.product', required=True, ) lot_name = fields.Char( string='Lot name', required=True, ) def on_barcode_scanned(self, barcode): product = self.env['product.product'].search([ ('barcode', '=', barcode), ])[:1] if product and not self.product_id: self.product_id = product return self.lot_name = barcode def _prepare_lot_values(self): return { 'product_id': self.product_id.id, 'name': self.lot_name, } def confirm(self): lot = self.env['stock.production.lot'].create( self._prepare_lot_values()) # Assign lot created to wizard scanning barcode lot_id field wiz = self.env[self.env.context['active_model']].browse( self.env.context['active_id'] ) if wiz: wiz.lot_id = lot return lot # -*- coding: utf-8 -*- # # This file is part of Invenio. # Copyright (C) 2014, 2015 CERN. # # Invenio 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. # # Invenio 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 Invenio; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. """Cataloguer search unit.""" from intbitset import intbitset def search_unit(query, f, m, wl=None): """Return hitset of recIDs that were modified by the given cataloguer.""" from invenio.ext.sqlalchemy import db from invenio.modules.editor.models import HstRECORD if query: try: cataloguer_name, modification_date = query.split(";") except ValueError: cataloguer_name = query modification_date = "" where = [HstRECORD.job_person == cataloguer_name] if modification_date: where += HstRECORD.filter_time_interval(modification_date) return intbitset( db.session.query(HstRECORD.id_bibrec).filter(*where).all() ) else: return intbitset([]) # Under MIT License, see LICENSE.txt from RULEngine.Game.Player import Player from RULEngine.Util.constant import PLAYER_PER_TEAM from RULEngine.Util.team_color_service import TeamColor class Team(): def __init__(self, team_color, type="friend"): self.players = {} for player_id in range(PLAYER_PER_TEAM): self.players[player_id] = Player(self, player_id, True, type) self.team_color = team_color self.score = 0 def has_player(self, player): has_player = False for team_player in self.players.values(): if team_player is player: has_player = True return has_player def is_team_yellow(self): return self.team_color == TeamColor.YELLOW_TEAM def update_player(self, player_id, pose, delta=0): try: self.players[player_id].update(pose, delta) except KeyError as err: raise err def kalman_update(self, player_id, pose_list, delta=0): try: self.players[player_id].kalman_update(pose_list, delta) except KeyError as err: raise err def update_player_command(self, player_id, cmd): try: self.players[player_id].set_command(cmd) except KeyError as err: raise err #!/usr/bin/python # (c) 2016, NetApp, Inc # 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 = """ --- module: netapp_e_amg_role short_description: Update the role of a storage array within an Asynchronous Mirror Group (AMG). description: - Update a storage array to become the primary or secondary instance in an asynchronous mirror group version_added: '2.2' author: Kevin Hulquest (@hulquest) options: api_username: required: true description: - The username to authenticate with the SANtricity WebServices Proxy or embedded REST API. api_password: required: true description: - The password to authenticate with the SANtricity WebServices Proxy or embedded REST API. api_url: required: true description: - The url to the SANtricity WebServices Proxy or embedded REST API. validate_certs: required: false default: true description: - Should https certificates be validated? ssid: description: - The ID of the primary storage array for the async mirror action required: yes role: description: - Whether the array should be the primary or secondary array for the AMG required: yes choices: ['primary', 'secondary'] noSync: description: - Whether to avoid synchronization prior to role reversal required: no default: no type: bool force: description: - Whether to force the role reversal regardless of the online-state of the primary required: no default: no """ EXAMPLES = """ - name: Update the role of a storage array netapp_e_amg_role: name: updating amg role role: primary ssid: "{{ ssid }}" api_url: "{{ netapp_api_url }}" api_username: "{{ netapp_api_username }}" api_password: "{{ netapp_api_password }}" validate_certs: "{{ netapp_api_validate_certs }}" """ RETURN = """ msg: description: Failure message returned: failure type: string sample: "No Async Mirror Group with the name." """ import json import traceback from ansible.module_utils.api import basic_auth_argument_spec from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six.moves.urllib.error import HTTPError from ansible.module_utils._text import to_native from ansible.module_utils.urls import open_url HEADERS = { "Content-Type": "application/json", "Accept": "application/json", } def request(url, data=None, headers=None, method='GET', use_proxy=True, force=False, last_mod_time=None, timeout=10, validate_certs=True, url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False): try: r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, url_username=url_username, url_password=url_password, http_agent=http_agent, force_basic_auth=force_basic_auth) except HTTPError as e: r = e.fp try: raw_data = r.read() if raw_data: data = json.loads(raw_data) else: raw_data = None except: if ignore_errors: pass else: raise Exception(raw_data) resp_code = r.getcode() if resp_code >= 400 and not ignore_errors: raise Exception(resp_code, data) else: return resp_code, data def has_match(module, ssid, api_url, api_pwd, api_usr, body, name): amg_exists = False has_desired_role = False amg_id = None amg_data = None get_amgs = 'storage-systems/%s/async-mirrors' % ssid url = api_url + get_amgs try: amg_rc, amgs = request(url, url_username=api_usr, url_password=api_pwd, headers=HEADERS) except: module.fail_json(msg="Failed to find AMGs on storage array. Id [%s]" % (ssid)) for amg in amgs: if amg['label'] == name: amg_exists = True amg_id = amg['id'] amg_data = amg if amg['localRole'] == body.get('role'): has_desired_role = True return amg_exists, has_desired_role, amg_id, amg_data def update_amg(module, ssid, api_url, api_usr, api_pwd, body, amg_id): endpoint = 'storage-systems/%s/async-mirrors/%s/role' % (ssid, amg_id) url = api_url + endpoint post_data = json.dumps(body) try: request(url, data=post_data, method='POST', url_username=api_usr, url_password=api_pwd, headers=HEADERS) except Exception as e: module.fail_json( msg="Failed to change role of AMG. Id [%s]. AMG Id [%s]. Error [%s]" % (ssid, amg_id, to_native(e)), exception=traceback.format_exc()) status_endpoint = 'storage-systems/%s/async-mirrors/%s' % (ssid, amg_id) status_url = api_url + status_endpoint try: rc, status = request(status_url, method='GET', url_username=api_usr, url_password=api_pwd, headers=HEADERS) except Exception as e: module.fail_json( msg="Failed to check status of AMG after role reversal. " "Id [%s]. AMG Id [%s]. Error [%s]" % (ssid, amg_id, to_native(e)), exception=traceback.format_exc()) # Here we wait for the role reversal to complete if 'roleChangeProgress' in status: while status['roleChangeProgress'] != "none": try: rc, status = request(status_url, method='GET', url_username=api_usr, url_password=api_pwd, headers=HEADERS) except Exception as e: module.fail_json( msg="Failed to check status of AMG after role reversal. " "Id [%s]. AMG Id [%s]. Error [%s]" % (ssid, amg_id, to_native(e)), exception=traceback.format_exc()) return status def main(): argument_spec = basic_auth_argument_spec() argument_spec.update(dict( name=dict(required=True, type='str'), role=dict(required=True, choices=['primary', 'secondary']), noSync=dict(required=False, type='bool', default=False), force=dict(required=False, type='bool', default=False), ssid=dict(required=True, type='str'), api_url=dict(required=True), api_username=dict(required=False), api_password=dict(required=False, no_log=True), )) module = AnsibleModule(argument_spec=argument_spec) p = module.params ssid = p.pop('ssid') api_url = p.pop('api_url') user = p.pop('api_username') pwd = p.pop('api_password') name = p.pop('name') if not api_url.endswith('/'): api_url += '/' agm_exists, has_desired_role, async_id, amg_data = has_match(module, ssid, api_url, pwd, user, p, name) if not agm_exists: module.fail_json(msg="No Async Mirror Group with the name: '%s' was found" % name) elif has_desired_role: module.exit_json(changed=False, **amg_data) else: amg_data = update_amg(module, ssid, api_url, user, pwd, p, async_id) if amg_data: module.exit_json(changed=True, **amg_data) else: module.exit_json(changed=True, msg="AMG role changed.") if __name__ == '__main__': main() import os # toolchains options ARCH='arm' CPU='cortex-m3' CROSS_TOOL='gcc' # bsp lib config BSP_LIBRARY_TYPE = None if os.getenv('RTT_CC'): CROSS_TOOL = os.getenv('RTT_CC') if os.getenv('RTT_ROOT'): RTT_ROOT = os.getenv('RTT_ROOT') # cross_tool provides the cross compiler # EXEC_PATH is the compiler execute path, for example, CodeSourcery, Keil MDK, IAR if CROSS_TOOL == 'gcc': PLATFORM = 'gcc' EXEC_PATH = r'C:\Users\XXYYZZ' elif CROSS_TOOL == 'keil': PLATFORM = 'armcc' EXEC_PATH = r'C:/Keil_v5' elif CROSS_TOOL == 'iar': PLATFORM = 'iar' EXEC_PATH = r'C:/Program Files (x86)/IAR Systems/Embedded Workbench 8.0' if os.getenv('RTT_EXEC_PATH'): EXEC_PATH = os.getenv('RTT_EXEC_PATH') BUILD = 'debug' if PLATFORM == 'gcc': # toolchains PREFIX = 'arm-none-eabi-' CC = PREFIX + 'gcc' AS = PREFIX + 'gcc' AR = PREFIX + 'ar' CXX = PREFIX + 'g++' LINK = PREFIX + 'gcc' TARGET_EXT = 'elf' SIZE = PREFIX + 'size' OBJDUMP = PREFIX + 'objdump' OBJCPY = PREFIX + 'objcopy' DEVICE = ' -mcpu=cortex-m3 -mthumb -ffunction-sections -fdata-sections' CFLAGS = DEVICE + ' -Dgcc' AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -Wa,-mimplicit-it=thumb ' LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rt-thread.map,-cref,-u,Reset_Handler -T board/linker_scripts/link.lds' CPATH = '' LPATH = '' if BUILD == 'debug': CFLAGS += ' -O0 -gdwarf-2 -g' AFLAGS += ' -gdwarf-2' else: CFLAGS += ' -O2' CXXFLAGS = CFLAGS POST_ACTION = OBJCPY + ' -O binary $TARGET rtthread.bin\n' + SIZE + ' $TARGET \n' elif PLATFORM == 'armcc': # toolchains CC = 'armcc' CXX = 'armcc' AS = 'armasm' AR = 'armar' LINK = 'armlink' TARGET_EXT = 'axf' DEVICE = ' --cpu Cortex-M3 ' CFLAGS = '-c ' + DEVICE + ' --apcs=interwork --c99' AFLAGS = DEVICE + ' --apcs=interwork ' LFLAGS = DEVICE + ' --scatter "board\linker_scripts\link.sct" --info sizes --info totals --info unused --info veneers --list rt-thread.map --strict' CFLAGS += ' -I' + EXEC_PATH + '/ARM/ARMCC/include' LFLAGS += ' --libpath=' + EXEC_PATH + '/ARM/ARMCC/lib' CFLAGS += ' -D__MICROLIB ' AFLAGS += ' --pd "__MICROLIB SETA 1" ' LFLAGS += ' --library_type=microlib ' EXEC_PATH += '/ARM/ARMCC/bin/' if BUILD == 'debug': CFLAGS += ' -g -O0' AFLAGS += ' -g' else: CFLAGS += ' -O2' CXXFLAGS = CFLAGS CFLAGS += ' -std=c99' POST_ACTION = 'fromelf --bin $TARGET --output rtthread.bin \nfromelf -z $TARGET' elif PLATFORM == 'iar': # toolchains CC = 'iccarm' CXX = 'iccarm' AS = 'iasmarm' AR = 'iarchive' LINK = 'ilinkarm' TARGET_EXT = 'out' DEVICE = '-Dewarm' CFLAGS = DEVICE CFLAGS += ' --diag_suppress Pa050' CFLAGS += ' --no_cse' CFLAGS += ' --no_unroll' CFLAGS += ' --no_inline' CFLAGS += ' --no_code_motion' CFLAGS += ' --no_tbaa' CFLAGS += ' --no_clustering' CFLAGS += ' --no_scheduling' CFLAGS += ' --endian=little' CFLAGS += ' --cpu=Cortex-M3' CFLAGS += ' -e' CFLAGS += ' --fpu=None' CFLAGS += ' --dlib_config "' + EXEC_PATH + '/arm/INC/c/DLib_Config_Normal.h"' CFLAGS += ' --silent' AFLAGS = DEVICE AFLAGS += ' -s+' AFLAGS += ' -w+' AFLAGS += ' -r' AFLAGS += ' --cpu Cortex-M3' AFLAGS += ' --fpu None' AFLAGS += ' -S' if BUILD == 'debug': CFLAGS += ' --debug' CFLAGS += ' -On' else: CFLAGS += ' -Oh' LFLAGS = ' --config "board/linker_scripts/link.icf"' LFLAGS += ' --entry __iar_program_start' CXXFLAGS = CFLAGS EXEC_PATH = EXEC_PATH + '/arm/bin/' POST_ACTION = 'ielftool --bin $TARGET rtthread.bin' def dist_handle(BSP_ROOT, dist_dir): import sys cwd_path = os.getcwd() sys.path.append(os.path.join(os.path.dirname(BSP_ROOT), 'tools')) from sdk_dist import dist_do_building dist_do_building(BSP_ROOT, dist_dir) # -*- coding: utf-8 -*- import atexit import json import os import re import six from collections import namedtuple from datetime import date from math import ceil from tempfile import NamedTemporaryFile from wait_for import wait_for from cached_property import cached_property from jsmin import jsmin from lxml.html import document_fromstring from selenium.common.exceptions import WebDriverException from widgetastic.exceptions import NoSuchElementException from widgetastic.log import logged from widgetastic.utils import ParametrizedLocator, Parameter, ParametrizedString, attributize_string from widgetastic.utils import VersionPick, Version from widgetastic.widget import ( Table as VanillaTable, TableColumn as VanillaTableColumn, TableRow as VanillaTableRow, Widget, View, Select, TextInput, Text, Checkbox, Image, ParametrizedView, FileInput as BaseFileInput, ClickableMixin, ConditionalSwitchableView, do_not_read_this_widget) from widgetastic.xpath import quote from widgetastic_patternfly import ( Accordion as PFAccordion, CandidateNotFound, BootstrapSwitch, BootstrapTreeview, Button, Input, BootstrapSelect, CheckableBootstrapTreeview, FlashMessages) from cfme.exceptions import ItemNotFound, ManyEntitiesFound class DynamicTableAddError(Exception): """Raised when an attempt to add or save a row to a `widgetastic_manageiq.DynamicTable` fails""" pass class DynaTree(Widget): """ A class directed at CFME Tree elements """ XPATH = """\ function xpath(root, xpath) { if(root == null) root = document; var nt = XPathResult.ANY_UNORDERED_NODE_TYPE; return document.evaluate(xpath, root, null, nt, null).singleNodeValue; } """ # This function retrieves the root of the tree. Can wait for the tree to get initialized TREE_GET_ROOT = """\ function get_root(loc) { var start_time = new Date(); var root = null; while(root === null && ((new Date()) - start_time) < 10000) { try { root = $(loc).dynatree("getRoot"); } catch(err) { // Nothing ... } } return root; } """ # This function is used to DRY the decision on which text to match GET_LEVEL_NAME = XPATH + """\ function get_level_name(level, by_id) { if(by_id){ return level.li.getAttribute("id"); } else { var e = xpath(level.li, "./span/a"); if(e === null) return null; else return e.textContent; } } """ # needs xpath to work, provided by dependencies of the other functions EXPANDABLE = """\ function expandable(el) { return xpath(el.li, "./span/span[contains(@class, 'dynatree-expander')]") !== null; } """ # This function reads whole tree. If it faces an ajax load, it returns false. # If it does not return false, the result is complete. READ_TREE = jsmin(TREE_GET_ROOT + GET_LEVEL_NAME + EXPANDABLE + """\ function read_tree(root, read_id, _root_tree) { if(read_id === undefined) read_id = false; if(_root_tree === undefined) _root_tree = true; if(_root_tree) { root = get_root(root); if(root === null) return null; if(expandable(root) && (!root.bExpanded)) { root.expand(); if(root.childList === null && root.data.isLazy){ return false; } } var result = new Array(); var need_wait = false; var children = (root.childList === null) ? [] : root.childList; for(var i = 0; i < children.length; i++) { var child = children[i]; var sub = read_tree(child, read_id, false); if(sub === false) need_wait = true; else result.push(sub); } if(need_wait) return false; else if(children.length == 0) return null; else return result; } else { if(expandable(root) && (!root.bExpanded)) { root.expand(); if(root.childList === null && root.data.isLazy){ return false; } } var name = get_level_name(root, read_id); var result = new Array(); var need_wait = false; var children = (root.childList === null) ? [] : root.childList; for(var i = 0; i < children.length; i++) { var child = children[i]; var sub = read_tree(child, read_id, false); if(sub === false) need_wait = true; else result.push(sub); } if(need_wait) return false; else if(children.length == 0) return name; else return [name, result] } } """) def __init__(self, parent, tree_id=None, logger=None): Widget.__init__(self, parent, logger=logger) self._tree_id = tree_id @property def tree_id(self): if self._tree_id is not None: return self._tree_id else: try: return self.parent.tree_id except AttributeError: raise NameError( 'You have to specify tree_id to BootstrapTreeview if the parent object does ' 'not implement .tree_id!') def __locator__(self): return '#{}'.format(self.tree_id) def read(self): return self.currently_selected def fill(self, value): if self.currently_selected == value: return False self.click_path(*value) return True @property def currently_selected(self): items = self.browser.elements( './/li[.//span[contains(@class, "dynatree-active")]]/span/a', parent=self, check_visibility=True) return map(self.browser.text, items) def root_el(self): return self.browser.element(self) def _get_tag(self): if getattr(self, 'tag', None) is None: self.tag = self.browser.tag(self) return self.tag def read_contents(self, by_id=False): result = False while result is False: self.browser.plugin.ensure_page_safe() result = self.browser.execute_script( "{} return read_tree(arguments[0], arguments[1]);".format(self.READ_TREE), self.__locator__(), by_id) return result @staticmethod def _construct_xpath(path, by_id=False): items = [] for item in path: if by_id: items.append('ul/li[@id={}]'.format(quote(item))) else: items.append('ul/li[./span/a[normalize-space(.)={}]]'.format(quote(item))) return './' + '/'.join(items) def _item_expanded(self, id): span = self.browser.element('.//li[@id={}]/span'.format(quote(id)), parent=self) return 'dynatree-expanded' in self.browser.get_attribute('class', span) def _item_expandable(self, id): return bool( self.browser.elements( './/li[@id={}]/span/span[contains(@class, "dynatree-expander")]'.format(quote(id)), parent=self)) def _click_expander(self, id): expander = self.browser.element( './/li[@id={}]/span/span[contains(@class, "dynatree-expander")]'.format(quote(id)), parent=self) return self.browser.click(expander) def expand_id(self, id): self.browser.plugin.ensure_page_safe() if not self._item_expanded(id) and self._item_expandable(id): self.logger.debug('expanding node %r', id) self._click_expander(id) wait_for(lambda: self._item_expanded(id), num_sec=15, delay=0.5) def child_items(self, id, ids=False): self.expand_id(id) items = self.browser.elements('.//li[@id={}]/ul/li'.format(quote(id)), parent=self) result = [] for item in items: if ids: result.append(self.browser.get_attribute('id', item)) else: text_item = self.browser.element('./span/a', parent=item) result.append(self.browser.text(text_item)) return result def expand_path(self, *path, **kwargs): """ Exposes a path. Args: *path: The path as multiple positional string arguments denoting the course to take. Keywords: by_id: Whether to match ids instead of text. Returns: The leaf web element. """ by_id = kwargs.pop("by_id", False) current_path = [] last_id = None node = None for item in path: if last_id is None: last_id = self.browser.get_attribute( 'id', self.browser.element('./ul/li', parent=self)) self.expand_id(last_id) if isinstance(item, re._pattern_type): self.logger.debug('Looking for regexp %r in path %r', item.pattern, current_path) for child_item in self.child_items(last_id, ids=by_id): if item.match(child_item) is not None: # found item = child_item break else: raise CandidateNotFound( {'message': "r{!r}: could not be found in the tree.".format(item.pattern), 'path': current_path, 'cause': None}) current_path.append(item) xpath = self._construct_xpath(current_path, by_id=by_id) try: node = self.browser.element(xpath, parent=self) except NoSuchElementException: raise CandidateNotFound( {'message': "{}: could not be found in the tree.".format(item), 'path': current_path, 'cause': None}) last_id = self.browser.get_attribute('id', node) if node is not None: self.expand_id(last_id) return self.browser.element('./span', parent=node) def click_path(self, *path, **kwargs): """ Exposes a path and then clicks it. Args: *path: The path as multiple positional string arguments denoting the course to take. Keywords: by_id: Whether to match ids instead of text. Returns: The leaf web element. """ leaf = self.expand_path(*path, **kwargs) title = self.browser.element('./a', parent=leaf) self.logger.info("Path %r yielded menuitem %r", path, self.browser.text(title)) if title is not None: self.browser.plugin.ensure_page_safe() self.browser.click(title) checkbox_locator = './span[contains(@class, "dynatree-checkbox")]' if self.browser.is_displayed(checkbox_locator, parent=leaf): checkbox = self.browser.element(checkbox_locator, parent=leaf) self.browser.click(checkbox) return leaf class CheckableDynaTree(DynaTree): """ Checkable variation of CFME Tree. This widget not only expands a tree for a provided path, but also checks a checkbox. """ IS_CHECKABLE = './span[contains(@class, "dynatree-checkbox")]' IS_CHECKED = './../span[contains(@class, "dynatree-selected")]' def is_checkable(self, item): return bool(self.browser.elements(self.IS_CHECKABLE, parent=item)) def is_checked(self, item): return bool(self.browser.elements(self.IS_CHECKED, parent=item)) def check_uncheck_node(self, check, *path, **kwargs): leaf = self.expand_path(*path, **kwargs) if not self.is_checkable(leaf): raise TypeError('Item is not checkable') checked = self.is_checked(leaf) if checked != check: self.logger.info('%s %r', 'Checking' if check else 'Unchecking', path[-1]) self.browser.click(self.IS_CHECKABLE, parent=leaf) def check_node(self, *path, **kwargs): """Expands the passed path and checks a checkbox that is located at the node.""" return self.check_uncheck_node(True, *path, **kwargs) def uncheck_node(self, *path, **kwargs): """Expands the passed path and unchecks a checkbox that is located at the node.""" return self.check_uncheck_node(False, *path, **kwargs) def node_checked(self, *path, **kwargs): """Check if a checkbox is checked on the node in that path.""" leaf = self.expand_path(*path, **kwargs) if not self.is_checkable(leaf): return False return self.is_checked(leaf) def fill(self, path): if self.node_checked(*path): return False else: self.check_node(*path) return True def read(self): do_not_read_this_widget() def CheckableManageIQTree(tree_id=None): # noqa return VersionPick({ Version.lowest(): CheckableDynaTree(tree_id), '5.7.0.1': CheckableBootstrapTreeview(tree_id), }) def ManageIQTree(tree_id=None): # noqa return VersionPick({ Version.lowest(): DynaTree(tree_id), '5.7.0.1': BootstrapTreeview(tree_id), }) class SummaryFormItem(Widget): """The UI item that shows the values for objects that are NOT VMs, Providers and such ones.""" LOCATOR = ( './/h3[normalize-space(.)={}]/following-sibling::div/div' '//label[normalize-space(.)={}]/following-sibling::div') def __init__(self, parent, group_title, item_name, text_filter=None, logger=None): Widget.__init__(self, parent, logger=logger) self.group_title = group_title self.item_name = item_name if text_filter is not None and not callable(text_filter): raise TypeError('text_filter= must be a callable') self.text_filter = text_filter def __locator__(self): return self.LOCATOR.format(quote(self.group_title), quote(self.item_name)) @property def text(self): if not self.is_displayed: return None ui_text = self.browser.text(self) if self.text_filter is not None: # Process it ui_text = self.text_filter(ui_text) return ui_text def read(self): text = self.text if text is None: do_not_read_this_widget() return text class MultiBoxSelect(View): """This view combines two `` id for available items CHOSEN_ITEMS_ID (str): default value of `` id for available items chosen_items (str): provided value of `