~ubuntu-branches/ubuntu/hardy/emesene/hardy-proposed

« back to all changes in this revision

Viewing changes to emesenecommon.py

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2008-02-06 21:57:05 UTC
  • Revision ID: james.westby@ubuntu.com-20080206215705-d1abf07rdwcaju3p
Tags: upstream-1.0~r1013
ImportĀ upstreamĀ versionĀ 1.0~r1013

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#   This file is part of emesene.
 
2
#
 
3
#    Emesene is free software; you can redistribute it and/or modify
 
4
#    it under the terms of the GNU General Public License as published by
 
5
#    the Free Software Foundation; either version 2 of the License, or
 
6
#    (at your option) any later version.
 
7
#
 
8
#    emesene is distributed in the hope that it will be useful,
 
9
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
#    GNU General Public License for more details.
 
12
#
 
13
#    You should have received a copy of the GNU General Public License
 
14
#    along with emesene; if not, write to the Free Software
 
15
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 
 
17
import os
 
18
import re
 
19
import sys
 
20
 
 
21
from htmlentitydefs import codepoint2name
 
22
 
 
23
#
 
24
# Global variables for use throughout the program
 
25
#
 
26
APP_NAME = 'emesene'
 
27
APP_VERSION = '1.0'
 
28
OS_NAME = os.name
 
29
DIR_SEP = os.sep
 
30
 
 
31
class PATH(object):
 
32
    '''a namespace for paths'''
 
33
 
 
34
    DIR_SEP = os.sep
 
35
 
 
36
    if hasattr(sys, "frozen"):
 
37
        APP_PATH = os.path.dirname(sys.executable)
 
38
    else:
 
39
        APP_PATH = os.path.abspath(os.path.dirname(__file__))
 
40
    
 
41
    HOME_DIR = os.path.expanduser('~')
 
42
    CONF_DIR_NAME = '.config' + DIR_SEP + 'emesene1.0'
 
43
    CONFIG_DIR = HOME_DIR + DIR_SEP + CONF_DIR_NAME
 
44
        
 
45
    THEME_HOME_PATH = CONFIG_DIR + DIR_SEP + 'themes'
 
46
    THEME_SYSTEM_WIDE_PATH = APP_PATH + DIR_SEP + 'themes'
 
47
    DEFAULT_THEME_PATH = THEME_SYSTEM_WIDE_PATH + DIR_SEP + 'default' + DIR_SEP
 
48
 
 
49
    PLUGINS_HOME = 'pluginsEmesene'
 
50
    PLUGINS_SYSTEM_WIDE = 'plugins_base'
 
51
    PLUGIN_SYSTEM_WIDE_PATH = APP_PATH + DIR_SEP + PLUGINS_SYSTEM_WIDE
 
52
    PLUGIN_HOME_PATH = CONFIG_DIR + DIR_SEP + PLUGINS_HOME
 
53
    
 
54
    SMILIES_SYSTEM_WIDE_PATH = APP_PATH + DIR_SEP + 'smilies'
 
55
    SMILIES_HOME_PATH = CONFIG_DIR + DIR_SEP + 'smilies'
 
56
    DEFAULT_SMILIES_PATH = SMILIES_SYSTEM_WIDE_PATH + DIR_SEP + 'default' + DIR_SEP
 
57
    
 
58
    CONVTHEMES_SYSTEM_WIDE_PATH = APP_PATH + DIR_SEP + 'conversation_themes'
 
59
    CONVTHEMES_HOME_PATH = CONFIG_DIR + DIR_SEP + 'conversation_themes'
 
60
    DEFAULT_CONVTHEMES_PATH = CONVTHEMES_SYSTEM_WIDE_PATH + DIR_SEP + 'default' + DIR_SEP
 
61
    
 
62
    LANG_PATH = APP_PATH + DIR_SEP + 'po'
 
63
    SOUNDS_PATH = APP_PATH + DIR_SEP + 'sound_themes'
 
64
 
 
65
# workaround for pluginsEmesene/__init__.py
 
66
PLUGINS_HOME = PATH.PLUGINS_HOME
 
67
PLUGIN_SYSTEM_WIDE_PATH = PATH.PLUGIN_SYSTEM_WIDE_PATH
 
68
 
 
69
# Other constants
 
70
RESPONSE_DELETE_ALIAS = 50
 
71
RESPONSE_NO_AVATAR = -50
 
72
MAX_MESSAGE_LENGTH = 1100
 
73
 
 
74
#Regex
 
75
urlRe = re.compile("(^|[^A-Za-z0-9])(https?\:\/\/|ftp\:\/\/|www\.)([A-Za-z0-9\$_\.\+!\*':\(\|\)@,;\/\?&%=~#^`\\\-]+)")
 
76
reUnencode = re.compile( '=\\?([^\\?]+)\\?Q\\?([^\\?]+)\\?=' )
 
77
reChar = re.compile( '=([A-z0-9]{2})' )
 
78
 
 
79
#
 
80
# Global functions
 
81
#
 
82
 
 
83
def htmlEncode(string):
 
84
    '''this method return a string whit html tags encoded'''
 
85
    for i,j in codepoint2name.iteritems():
 
86
        if i <= 256 and str(chr(i)) in string:
 
87
            string = string.replace(chr(i), j)
 
88
    return string
 
89
 
 
90
def rgbToHexa( color ):
 
91
    '''take a gtk.gdk.Color end returns a string with html way color. Eg.: #FFCC00'''
 
92
    
 
93
    red = color.red >> 8
 
94
    green = color.green >> 8
 
95
    blue = color.blue >> 8
 
96
    
 
97
    return '#'+'%02X%02X%02X' % ( red , green , blue )
 
98
 
 
99
def sort( list ):
 
100
    '''Sorts a list in alphabetical order without regard of the capitalization'''
 
101
    
 
102
    list = list[:]
 
103
    list.sort( key= lambda x: str.lower( x ) )
 
104
    return list
 
105
    
 
106
def getLinks( text ):
 
107
    links = ()
 
108
    text = re.sub( urlRe, _searchAnchors, text.replace('%','%%') )
 
109
    return text, links
 
110
 
 
111
def _searchAnchors( data ):
 
112
    s, w, url = data.groups()
 
113
    href = w+url
 
114
    if w == 'www.':
 
115
        href  = 'http://'+href
 
116
    return '%s'
 
117