Skip to content

Commit 27bd7df

Browse files
committed
updated to use plotdevice.lib.register
- register() returns an initial _ctx value (from whatever script imported it) and signs the module up to have its _ctx updated whenever a new context is bound - need to test whether submodules need to register independently or if they can safely peek at the parent module’s _ctx value (as wordnet, web, graph, and grid seem to be doing)… - (it wouldn’t surprise me if they get a stale _ctx after the initial import)
1 parent 06860e9 commit 27bd7df

File tree

16 files changed

+381
-340
lines changed

16 files changed

+381
-340
lines changed

beziereditor/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242

4343
######################################################################################################
4444

45+
from plotdevice.lib import register
46+
_ctx = register(__name__)
47+
4548
from plotdevice.gfx import MOVETO, LINETO, CURVETO, CLOSE, CENTER, Point, PathElement, BezierPath
4649
from math import sin, cos, atan, pi, degrees, radians, sqrt, pow
4750

beziereditor/svg/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
# See also the SVG DOM specification:
55
# http://www.w3.org/TR/SVG/
66

7-
import xml.dom.minidom as parser
87
import re
8+
import xml.dom.minidom as parser
99
from plotdevice.gfx import RGB, MOVETO
10+
from plotdevice.lib import register
11+
_ctx = register(__name__)
1012

1113
def parse(svg):
1214

boids/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# http://www.vergenet.net/~conrad/boids/pseudocode.html
88

99
from plotdevice.util import random
10+
from plotdevice.lib import register
11+
_ctx = register(__name__)
1012

1113
class Boid:
1214

colors/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@
4242
from math import floor, ceil
4343
from copy import deepcopy
4444
from random import random, choice
45-
from shutil import copyfile
45+
# from shutil import copyfile
4646

4747
try:
4848
# NodeBox / Cocoa specific functionality.
4949
# Our library can still do a lot of interesting stuff without these!
50-
from plotdevice.gfx import Grob, RGB, HSB, CMYK, CORNER, _restore, _save
5150
from AppKit import NSShadow, NSColor
5251
from AppKit import CIImage, CIColor, CIFilter, CIVector, NSGraphicsContext
52+
from plotdevice.gfx import Grob, RGB, HSB, CMYK, CORNER, _restore, _save
53+
from plotdevice.lib import register
54+
_ctx = register(__name__)
5355
except:
5456
class Grob: pass
5557

coreimage/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
__copyright__ = "Copyright (c) 2007 Tom De Smedt"
99
__license__ = "GPL"
1010

11+
from plotdevice.lib import register
12+
_ctx = register(__name__)
13+
1114
### NODEBOX CORE IMAGE ###############################################################################
1215

1316
# The Core Image library for NodeBox adds image manipulation to NodeBox.

cornu/__init__.py

+28-26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# You can download Raph's curve editor from http://www.levien.com/spiro/.
66

77
from math import *
8+
from plotdevice.lib import register
9+
_ctx = register(__name__)
810

911
### CORNU ############################################################################################
1012

@@ -44,7 +46,7 @@ def boundary_ths(path, ths, closed):
4446
ths[0] = first_th
4547
last_th = 2 * atan2(path[-1][1] - path[-2][1], path[-1][0] - path[-2][0]) - ths[-2]
4648
ths[-1] = last_th
47-
49+
4850
# implementation adapted from cephes
4951
def polevl(x, coef):
5052
ans = coef[-1]
@@ -271,10 +273,10 @@ def draw_cornu(path, ths, closed, flat=False):
271273
#print 'stroke'
272274

273275
def draw_cornu_flat(x0, y0, t0, t1, s0, c0, flip, cs, ss, cmd):
274-
276+
275277
""" Raph Levien's code draws fast LINETO segments.
276278
"""
277-
279+
278280
for j in range(0, 100):
279281
t = j * .01
280282
s, c = eval_cornu(t0 + t * (t1 - t0))
@@ -287,7 +289,7 @@ def draw_cornu_flat(x0, y0, t0, t1, s0, c0, flip, cs, ss, cmd):
287289
print_pt(x0 + x, y0 + y, cmd)
288290
cmd = 'lineto'
289291
return cmd
290-
292+
291293
def draw_cornu_bezier(x0, y0, t0, t1, s0, c0, flip, cs, ss, cmd, scale, rot):
292294

293295
""" Mark Meyer's code draws elegant CURVETO segments.
@@ -301,11 +303,11 @@ def draw_cornu_bezier(x0, y0, t0, t1, s0, c0, flip, cs, ss, cmd, scale, rot):
301303
# becomes the new first point
302304
t = j * .2
303305
t2 = t+ .2
304-
306+
305307
curvetime = t0 + t * (t1 - t0)
306308
curvetime2 = t0 + t2 * (t1 - t0)
307309
Dt = (curvetime2 - curvetime) * scale
308-
310+
309311
if not s:
310312
# get first point
311313
# avoid calling this again: the next time though x,y will equal x3, y3
@@ -315,40 +317,40 @@ def draw_cornu_bezier(x0, y0, t0, t1, s0, c0, flip, cs, ss, cmd, scale, rot):
315317
c -= c0
316318
# calculate derivative of fresnel function at point to get tangent slope
317319
# just take the integrand of the fresnel function
318-
dx1 = cos(pow(curvetime, 2) + (flip * rot))
320+
dx1 = cos(pow(curvetime, 2) + (flip * rot))
319321
dy1 = flip * sin(pow(curvetime, 2) + (flip *rot))
320322
# x,y = first point on function
321323
x = ((c * cs - s * ss) +x0)
322324
y = ((s * cs + c * ss) + y0)
323325

324326
#evaluate the fresnel further along the function to look ahead to the next point
325-
s2,c2 = eval_cornu(curvetime2)
327+
s2,c2 = eval_cornu(curvetime2)
326328
s2 *= flip
327329
s2 -= s0
328330
c2 -= c0
329331

330-
dx2 = cos(pow(curvetime2, 2) + (flip * rot))
332+
dx2 = cos(pow(curvetime2, 2) + (flip * rot))
331333
dy2 = flip * sin(pow(curvetime2, 2) + (flip * rot))
332334
# x3, y3 = second point on function
333335
x3 = ((c2 * cs - s2 * ss)+x0)
334336
y3 = ((s2 * cs + c2 * ss)+y0)
335337

336338
# calculate control points
337339
x1 = (x + ((Dt/3.0) * dx1))
338-
y1 = (y + ((Dt/3.0) * dy1))
340+
y1 = (y + ((Dt/3.0) * dy1))
339341
x2 = (x3 - ((Dt/3.0) * dx2))
340342
y2 = (y3 - ((Dt/3.0) * dy2))
341343

342344
if cmd == 'moveto':
343345
print_pt(x, y, cmd)
344346
cmd = 'curveto'
345347
print_crv(x1, y1, x2, y2, x3, y3)
346-
348+
347349
dx1, dy1 = dx2, dy2
348350
x,y = x3, y3
349-
351+
350352
return cmd
351-
353+
352354
# update thetas based on cornu splines
353355
def tweak_ths(path, ths, closed):
354356
dks = []
@@ -430,7 +432,7 @@ def draw_cspline(path, ths):
430432
def print_pt(x, y, cmd):
431433
x = 100 * x
432434
y = 100 * y
433-
if (cmd == 'moveto'):
435+
if (cmd == 'moveto'):
434436
_ctx.moveto(x, y)
435437
elif (cmd == 'lineto'):
436438
_ctx.lineto(x, y)
@@ -447,34 +449,34 @@ def print_crv(x1, y1, x2, y2, x3, y3):
447449
_ctx.curveto(x1, y1, x2, y2, x3, y3)
448450

449451
def dot_pt(x, y):
450-
_ctx.oval(x-3,y-3, 6, 6)
452+
_ctx.oval(x-3,y-3, 6, 6)
451453

452454
def relativise(path):
453455

454456
for i in range(len(path)):
455457
x, y = path[i]
456458
x *= 0.01 * _ctx.WIDTH
457459
y *= 0.01 * _ctx.HEIGHT
458-
460+
459461
#Points on the path that have identical coordinates
460462
#generate a ZeroDivisionError
461463
for point in path:
462464
if (x,y) == point:
463465
x += 0.0000000001
464466
y += 0.0000000001
465-
467+
466468
path[i] = (x,y)
467-
469+
468470
return path
469-
471+
470472
def path(points, close=False, tweaks=20, flat=False, draw=False, helper=False):
471473

472-
# To make sure we don't change the original, make a copy of the
474+
# To make sure we don't change the original, make a copy of the
473475
# given points
474476
points = list(points)
475477

476478
points = relativise(points)
477-
479+
478480
ths = local_ths(points, close)
479481
for i in range(tweaks):
480482
boundary_ths(points, ths, close)
@@ -498,20 +500,20 @@ def path(points, close=False, tweaks=20, flat=False, draw=False, helper=False):
498500
_ctx.endpath()
499501
for x,y in points:
500502
dot_pt(x*100,y*100)
501-
503+
502504
return p
503-
505+
504506
def drawpath(p, close=False, tweaks=20, points=False, flat=False):
505-
507+
506508
path(p, close, tweaks, flat, draw=True, helper=points)
507509

508510
### PSYCO SPECIALIZATION #############################################################################
509511

510512
try:
511513
import psyco
512514
for f in [
513-
fit_arc, local_ths, boundary_ths, polevl, fresnel, eval_cornu,
514-
mod_2pi, fit_cornu_half, fit_cornu, draw_tan, draw_cornu,
515+
fit_arc, local_ths, boundary_ths, polevl, fresnel, eval_cornu,
516+
mod_2pi, fit_cornu_half, fit_cornu, draw_tan, draw_cornu,
515517
draw_cornu_flat, draw_cornu_bezier,
516518
tweak_ths, csinterp, draw_cspline, print_pt, relativise
517519
]:

flowerewolf/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
from os.path import dirname
99
from plotdevice.util import random, choice
10+
from plotdevice.lib import register
11+
_ctx = register(__name__)
12+
1013
import en
1114
dictionary = open("%s/vocabulary.txt"%dirname(__file__)).readlines()
1215

graph/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
__copyright__ = "Copyright (c) 2008 Tom De Smedt"
1717
__license__ = "GPL"
1818

19+
from plotdevice.lib import register
20+
_ctx = register(__name__)
21+
1922
######################################################################################################
2023

2124
import cluster

0 commit comments

Comments
 (0)