def recent_item_activated(self): uri = self.get_current_uri() try: urlopen(unquote(uri)).close() newGameDialog.LoadFileExtension.run(self.get_current_uri()) except (IOError, OSError): #shomething wrong whit the uri recentManager.remove_item(uri)
def recent_item_activated (self): uri = self.get_current_uri() try: urlopen(unquote(uri)).close() newGameDialog.LoadFileExtension.run(self.get_current_uri()) except (IOError, OSError): #shomething wrong whit the uri recentManager.remove_item(uri)
def recent_item_activated(self): uri = self.get_current_uri() try: urlopen(unquote(uri)).close() perspective = perspective_manager.get_perspective("database") perspective.open_chessfile(self.get_current_uri()) except (IOError, OSError): # shomething wrong whit the uri recentManager.remove_item(uri)
def onlineanswer (message): data = urlopen("http://www.pandorabots.com/pandora/talk?botid=8d034368fe360895", urlencode({"message":message, "botcust2":"x"}).encode("utf-8")).read().decode('utf-8') ss = "<b>DMPGirl:</b>" es = "<br>" answer = data[data.find(ss)+len(ss) : data.find(es,data.find(ss))] chatManager.tellPlayer(name, answer)
def paste(gamemodel): output = StringIO() text = pgn.save(output, gamemodel) values = {'apikey': APIKEY, 'pgn': text, "name": "PyChess", 'sandbox': 'false'} data = urlencode(values).encode('utf-8') req = Request(URL, data) try: response = urlopen(req, timeout=10) except URLError as err: if hasattr(err, 'reason'): print('We failed to reach the server.') print('Reason: ', err.reason) elif hasattr(err, 'code'): print('The server couldn\'t fulfill the request.') print('Error code: ', err.code) else: ID = response.read() link = "http://www.chesspastebin.com/?p=%s" % int(ID) clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) clipboard.set_text(link, -1) #print(text) #print(clipboard.wait_for_text()) msg_dialog = Gtk.MessageDialog(type=Gtk.MessageType.INFO, buttons=Gtk.ButtonsType.OK) msg = _( "Game shared at ") + '<a href="%s">chesspastebin.com</a>' % link msg_dialog.set_markup(msg) msg_dialog.format_secondary_text(_("(Link is available on clipboard.)")) msg_dialog.connect("response", lambda msg_dialog, a: msg_dialog.hide()) msg_dialog.show()
def findContributors(lang): site = "https://translations.launchpad.net/pychess/trunk/+pots/pychess/%s/+translate" % lang data = urlopen(site).read() language = re.findall("<h1>Browsing (.*?) translation</h1>", data)[0] start = data.find('Contributors to this translation') pers = re.findall('class="sprite person">(.*?)</a>', data[start:]) print("Did", language) return [language, pers]
def onlineanswer(message): data = urlopen( "http://www.pandorabots.com/pandora/talk?botid=8d034368fe360895", urlencode({"message": message, "botcust2": "x"}).encode("utf-8")).read( ).decode('utf-8') bold_ss = "<b>DMPGirl:</b>" break_es = "<br>" answer = data[data.find(bold_ss) + len(bold_ss):data.find( break_es, data.find(bold_ss))] chatManager.tellPlayer(name, answer)
def answer (message): try: data = urlopen("http://www.pandorabots.com/pandora/talk?botid=8d034368fe360895", urlencode({"message":message, "botcust2":"x"}).encode("utf-8")).read().decode('utf-8') except IOError as e: log.warning("Couldn't answer message from online bot: '%s'" % e, extra={"task":self.defname}) return ss = "<b>DMPGirl:</b>" es = "<br>" answer = data[data.find(ss)+len(ss) : data.find(es,data.find(ss))] self.emit("offer", Offer(CHAT_ACTION, answer))
def worker(): new_version = None req = Request(URL) try: if hasattr(ssl, '_create_unverified_context'): context = ssl._create_unverified_context() response = urlopen(req, context=context, timeout=1) else: response = urlopen(req, timeout=1) except URLError as err: if hasattr(err, 'reason'): print('We failed to reach the server.') print('Reason: ', err.reason) elif hasattr(err, 'code'): print('The server couldn\'t fulfill the request.') print('Error code: ', err.code) else: reader = codecs.getreader("utf-8") new_version = json.load(reader(response))["name"] if new_version is not None and VERSION.split(".") < new_version.split("."): GLib.idle_add(notify, new_version)
def protoopen (uri): """ Function for opening many things """ try: return open(uri, "rU", encoding=PGN_ENCODING) except (IOError, OSError): pass try: return urlopen(uri) except (IOError, OSError): pass raise IOError("Protocol isn't supported by pychess")
def protoopen(uri): """ Function for opening many things """ try: return open(uri, "rU", encoding=PGN_ENCODING) except (IOError, OSError): pass try: return urlopen(uri) except (IOError, OSError): pass raise IOError("Protocol isn't supported by pychess")
def protoopen(uri): """ Function for opening many things """ if uri.startswith("file:///"): uri = uri[7:] try: return open(uri, "rU", encoding=PGN_ENCODING, newline="") except (IOError, OSError): pass try: return urlopen(uri) except (IOError, OSError): pass raise IOError("Protocol isn't supported by pychess")
def download_file(url, progressbar=None): temp_file = None try: if progressbar is not None: GLib.idle_add(progressbar.set_text, "Downloading %s ..." % url) else: print("Downloading %s ..." % url) f = urlopen(url) temp_file = os.path.join(tempfile.gettempdir(), os.path.basename(url)) with open(temp_file, "wb") as local_file: local_file.write(f.read()) except HTTPError as e: print("HTTP Error:", e.code, url) except URLError as e: print("URL Error:", e.reason, url) return temp_file
def scoreAllMoves (self, board, probeSoft=False): global URL, expression, PROMOTION_FLAGS fen = board.asFen().split()[0] + " w - - 0 1" if (fen,board.color) in self.table: return self.table[(fen,board.color)] if probeSoft or not conf.get("online_egtb_check", True): return [] # Request the page url = (URL + fen).replace(" ", "%20") try: f = urlopen(url) except IOError as e: log.warning("Unable to read endgame tablebase from the Internet: %s" % repr(e)) return [] data = f.read() # Parse for color, move_data in enumerate(data.split("\nNEXTCOLOR\n")): try: moves = [] for fcord, tcord, promotion, result in expression.findall(move_data): fcord = int(fcord) tcord = int(tcord) if promotion: flag = PROMOTION_FLAGS[int(promotion)] elif RANK(fcord) != RANK(tcord) and FILE(fcord) != FILE(tcord) and \ board.arBoard[fcord] == PAWN and board.arBoard[tcord] == EMPTY: flag = ENPASSANT else: flag = NORMAL_MOVE move = newMove(fcord, tcord, flag) if result == "Draw": state = DRAW steps = 0 else: s, steps = result.split(" in ") steps = int(steps) if result.startswith("Win"): if color == WHITE: state = WHITEWON else: state = BLACKWON elif result.startswith("Lose"): if color == WHITE: state = BLACKWON else: state = WHITEWON moves.append( (move,state,steps) ) if moves: self.table[(fen,color)] = moves elif color == board.color and board.opIsChecked(): log.warning("Asked endgametable for a won position: %s" % fen) elif color == board.color: log.warning("Couldn't get %s data for position %s.\nData was: %s" % (reprColor[color], fen, repr(data))) except (KeyError, ValueError): log.warning("Couldn't parse %s data for position %s.\nData was: %s" % (reprColor[color], fen, repr(data))) self.table[(fen, color)] = [] # Don't try again. if (fen,board.color) in self.table: return self.table[(fen,board.color)] return []
############################# FILENAME = 'TRANSLATORS' POOLSIZE = 7 ########################### # Configuration ends here # ########################### from multiprocessing import Pool import re from pychess.compat import urlopen print("Getting data from Rosetta Launchpad...") data = urlopen('http://translations.launchpad.net/pychess/trunk/+translations').read() langs = sorted(re.findall('/pychess/trunk/\+pots/pychess/(.*?)/\+translate', data)) def findContributors(lang): site = "https://translations.launchpad.net/pychess/trunk/+pots/pychess/%s/+translate" % lang data = urlopen(site).read() language = re.findall("<h1>Browsing (.*?) translation</h1>", data)[0] start = data.find('Contributors to this translation') pers = re.findall('class="sprite person">(.*?)</a>', data[start:]) print("Did", language) return [language, pers] with open(FILENAME,'w') as file: pool = Pool(POOLSIZE) contributors = pool.map(findContributors, langs) for lang, (language, pers) in zip(langs, contributors):
############################# FILENAME = 'TRANSLATORS' POOLSIZE = 7 ########################### # Configuration ends here # ########################### from multiprocessing import Pool import re from pychess.compat import urlopen print("Getting data from Rosetta Launchpad...") data = urlopen( 'http://translations.launchpad.net/pychess/trunk/+translations').read() langs = sorted( re.findall('/pychess/trunk/\+pots/pychess/(.*?)/\+translate', data)) def findContributors(lang): site = "https://translations.launchpad.net/pychess/trunk/+pots/pychess/%s/+translate" % lang data = urlopen(site).read() language = re.findall("<h1>Browsing (.*?) translation</h1>", data)[0] start = data.find('Contributors to this translation') pers = re.findall('class="sprite person">(.*?)</a>', data[start:]) print("Did", language) return [language, pers] with open(FILENAME, 'w') as file:
def scoreAllMoves(self, board, probeSoft=False): global URL, expression, PROMOTION_FLAGS fen = board.asFen().split()[0] + " w - - 0 1" if (fen, board.color) in self.table: return self.table[(fen, board.color)] if probeSoft or not conf.get("online_egtb_check", True): return [] # Request the page url = (URL + fen).replace(" ", "%20") try: f = urlopen(url) except IOError as e: log.warning( "Unable to read endgame tablebase from the Internet: %s" % repr(e)) return [] data = f.read() # Parse for color, move_data in enumerate(data.split("\nNEXTCOLOR\n")): try: moves = [] for fcord, tcord, promotion, result in expression.findall( move_data): fcord = int(fcord) tcord = int(tcord) if promotion: flag = PROMOTION_FLAGS[int(promotion)] elif RANK(fcord) != RANK(tcord) and FILE(fcord) != FILE(tcord) and \ board.arBoard[fcord] == PAWN and board.arBoard[tcord] == EMPTY: flag = ENPASSANT else: flag = NORMAL_MOVE move = newMove(fcord, tcord, flag) if result == "Draw": state = DRAW steps = 0 else: s, steps = result.split(" in ") steps = int(steps) if result.startswith("Win"): if color == WHITE: state = WHITEWON else: state = BLACKWON elif result.startswith("Lose"): if color == WHITE: state = BLACKWON else: state = WHITEWON moves.append((move, state, steps)) if moves: self.table[(fen, color)] = moves elif color == board.color and board.opIsChecked(): log.warning("Asked endgametable for a won position: %s" % fen) elif color == board.color: log.warning( "Couldn't get %s data for position %s.\nData was: %s" % (reprColor[color], fen, repr(data))) except (KeyError, ValueError): log.warning( "Couldn't parse %s data for position %s.\nData was: %s" % (reprColor[color], fen, repr(data))) self.table[(fen, color)] = [] # Don't try again. if (fen, board.color) in self.table: return self.table[(fen, board.color)] return []