def makeTurtle() -> Turtle: global turtle turtle = Turtle() JsExecutor.call('makeTurtle', turtle.canvas.id, turtle.lineWidth, turtle.strokeStyle, turtle.fillStyle) turtle.home() return turtle
def parse_urls(config_start_urls): start_urls = [] for start_url in config_start_urls: if isinstance(start_url, basestring): start_url = {'url': start_url} start_url['compiled_url'] = re.compile(start_url['url']) if "page_rank" not in start_url: start_url['page_rank'] = 0 if "tags" not in start_url: start_url['tags'] = [] if "selectors_key" not in start_url: start_url['selectors_key'] = 'default' matches = ConfigLoader.get_url_variables_name(start_url['url']) start_url['url_attributes'] = {} for match in matches: if len(start_url['url']) > 2 and start_url['url'][-2:] == '?)': print('\033[0;35mWARNING: ' + start_url['url'] + ' finish by a variable.' ' The regex probably won\'t work as expected.' ' Add a \'/\' or \'$\' to make it work properly\033[0m') start_url['url_attributes'][match] = None # If there is tag(s) we need to generate all possible urls if len(matches) > 0: values = {} for match in matches: if 'variables' in start_url: if match in start_url['variables']: if isinstance(start_url['variables'][match], list): values[match] = start_url['variables'][match] else: if 'url' in start_url['variables'][match] and 'js' in start_url['variables'][match]: executor = JsExecutor() values[match] = executor.execute(start_url['variables'][match]['url'], start_url['variables'][match]['js']) else: raise Exception("Bad arguments for variables." + match + " for url " + start_url['url']) else: raise Exception("Missing " + match + " in variables" + " for url " + start_url['url']) start_urls = ConfigLoader.geturls(start_url, matches[0], matches[1:], values, start_urls) # If there is no tag just keep it like this else: start_urls.append(start_url) return start_urls
def arc(self, radius, angle, counterclockwise=True): toCenter = self.direction.rotate(90, counterclockwise) * radius fromCenter = -toCenter center = self.pos + toCenter startAngle = fromCenter.absRotation(False, Vec2D(-1, 0)) rotDirection = -1 if counterclockwise else 1 endAngle = startAngle + rotDirection * angle JsExecutor.call('arc', self.canvas.id, self.canvas.transform(center).x, self.canvas.transform( center).y, radius, math.radians(startAngle), math.radians(endAngle), counterclockwise) self.rotate(angle, counterclockwise) self.pos = center + fromCenter.rotate(angle, counterclockwise)
def fillPath(self): JsExecutor.call('fillPath', self.canvas.id)
def startPath(self): JsExecutor.call('startPath', self.canvas.id)
def fill(self, x, y): pixelPos = self.canvas.transform(Vec2D(x, y)) JsExecutor.call('floodFill', self.canvas.id, pixelPos.x, pixelPos.y, self.fillStyle, 255)
def setPenWidth(self, lineWidth): self.lineWidth = lineWidth JsExecutor.call('setPenWidth', self.canvas.id, lineWidth)
def setFillColor(self, fillStyle): self.fillStyle = fillStyle JsExecutor.call('setFillColor', self.canvas.id, fillStyle)
def setPenColor(self, strokeStyle): self.strokeStyle = strokeStyle JsExecutor.call('setPenColor', self.canvas.id, strokeStyle, self.canvasPos().x, self.canvasPos().y)
def setPos(self, x, y): self.pos = Vec2D(x, y) JsExecutor.call('moveTurtleTo', self.canvas.id, self.canvasPos().x, self.canvasPos().y)
def dot(self, diameter): JsExecutor.call('dot', self.canvas.id, self.canvasPos().x, self.canvasPos().y, diameter) time.sleep(self.delay)
def forward(self, distance): self.pos += self.direction * distance JsExecutor.call('lineTo', self.canvas.id, self.canvasPos().x, self.canvasPos().y) time.sleep(self.delay)
def home(self): self.pos = Vec2D(0, 0) self.direction = Vec2D(0, 1) JsExecutor.call('moveTurtleTo', self.canvas.id, turtle.canvasPos().x, turtle.canvasPos().y)
# Version 0.2 (in progress) from IPython.core.display import display from IPython.core.display import HTML from IPython.core.display import Javascript from js_executor import JsExecutor import time import math import random JsExecutor.configure('./javascript') class Vec2D: x: float = 0 y: float = 0 def __init__(self, xVal, yVal): self.x = xVal self.y = yVal def __neg__(self): return Vec2D(-self.x, -self.y) def __add__(self, other): if type(other) != Vec2D: raise TypeError( "unsupported operand type(s) for +: 'Vec2D' and '" + type(other).__name__ + "'") xNew = self.x + other.x yNew = self.y + other.y return Vec2D(xNew, yNew)