Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils.py
blob: 7a3216b9c41a03dbf9aed26f05b93070c86e2a6a (plain)
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
import gi
import base64
import re
import os
import time
import constants

from time import strftime
from gi.repository import GdkPixbuf
from gi.repository import Rsvg

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 = GdkPixbuf.PixbufLoader()
    data = base64.b64decode( str )
    pbl.write(data)
    pbl.close()
    
    return pbl.get_pixbuf()

def load_colored_svg(filename, stroke, fill):
    
    path = os.path.join(constants.GFX_PATH, filename)
    data = open(path, 'r').read()

    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)

    Rsvg.Handle.new_from_data(data.encode('utf-8')).get_pixbuf()

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 generate_thumbnail(pixbuf):
    
    return pixbuf.scale_simple(108, 81, GdkPixbuf.InterpType.BILINEAR)

def getDateString( when ):
    
    return strftime( "%c", time.localtime(when) )