-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
108 lines (80 loc) · 2.57 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import base64
import rsvg
import re
import os
import statvfs
import cairo
import gc
import gtk
import time
from time import strftime
import hippo
from sugar import util
def getStringFromPixbuf(pixbuf):
data = [""]
pixbuf.save_to_callback(_saveDataToBufferCb, "png", {}, data)
return base64.b64encode(str(data[0]))
def _saveDataToBufferCb(buf, data):
data[0] += buf
return True
def getPixbufFromString(str):
pbl = gtk.gdk.PixbufLoader()
data = base64.b64decode( str )
pbl.write(data)
pbl.close()
return pbl.get_pixbuf()
def loadSvg( data, stroke, fill ):
if ((stroke == None) or (fill == None)):
return rsvg.Handle( data=data )
entity = '<!ENTITY fill_color "%s">' % fill
data = re.sub('<!ENTITY fill_color .*>', entity, data)
entity = '<!ENTITY stroke_color "%s">' % stroke
data = re.sub('<!ENTITY stroke_color .*>', entity, data)
return rsvg.Handle( data=data )
def getUniqueFilepath( path, i ):
pathOb = os.path.abspath( path )
newPath = os.path.join( os.path.dirname(pathOb), str( str(i) + os.path.basename(pathOb) ) )
if (os.path.exists(newPath)):
i = i + 1
return getUniqueFilepath( pathOb, i )
else:
return os.path.abspath( newPath )
def generateThumbnail( pixbuf, scale, thumbw, thumbh ):
#need to generate thumbnail version here
thumbImg = cairo.ImageSurface(cairo.FORMAT_ARGB32, thumbw, thumbh)
tctx = cairo.Context(thumbImg)
img = hippo.cairo_surface_from_gdk_pixbuf(pixbuf)
tctx.scale(scale, scale)
tctx.set_source_surface(img, 0, 0)
tctx.paint()
gc.collect()
return thumbImg
def scaleSvgToDim( handle, dim ):
#todo...
scale = 1.0
svgDim = handle.get_dimension_data()
if (svgDim[0] > dim[0]):
pass
return scale
def getDateString( when ):
return strftime( "%c", time.localtime(when) )
def grayScalePixBuf2( pb, copy ):
arr = pb.get_pixels_array()
if (copy):
arr = arr.copy()
for row in arr:
for pxl in row:
y = 0.3*pxl[0][0]+0.59*pxl[1][0]+0.11*pxl[2][0]
pxl[0][0] = y
pxl[1][0] = y
pxl[2][0] = y
return gtk.gdk.pixbuf_new_from_array(arr, pb.get_colorspace(), pb.get_bits_per_sample())
def grayScalePixBuf( pb, copy ):
pb2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, pb.get_width(), pb.get_height())
pb.saturate_and_pixelate(pb2, 0, 0)
return pb2
def getFreespaceKb( ):
stat = os.statvfs("/home")
freebytes = stat[statvfs.F_BSIZE] * stat[statvfs.F_BAVAIL]
freekb = freebytes / 1024
return freekb