]> jfr.im git - irc/weechat/qweechat.git/blob - qweechat/config.py
b56e2839ebc28ca05be8eb63b6dc27d319a0d192
[irc/weechat/qweechat.git] / qweechat / config.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # config.py - configuration for QWeeChat (~/.qweechat/qweechat.conf)
5 #
6 # Copyright (C) 2011-2014 Sébastien Helleu <flashcode@flashtux.org>
7 #
8 # This file is part of QWeeChat, a Qt remote GUI for WeeChat.
9 #
10 # QWeeChat is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # QWeeChat is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with QWeeChat. If not, see <http://www.gnu.org/licenses/>.
22 #
23
24 import ConfigParser
25 import os
26
27 CONFIG_DIR = '%s/.qweechat' % os.getenv('HOME')
28 CONFIG_FILENAME = '%s/qweechat.conf' % CONFIG_DIR
29
30 CONFIG_DEFAULT_RELAY_LINES = 50
31
32 CONFIG_DEFAULT_SECTIONS = ('relay', 'look', 'color')
33 CONFIG_DEFAULT_OPTIONS = (('relay.server', ''),
34 ('relay.port', ''),
35 ('relay.ssl', 'off'),
36 ('relay.password', ''),
37 ('relay.autoconnect', 'off'),
38 ('relay.lines', str(CONFIG_DEFAULT_RELAY_LINES)),
39 ('look.debug', 'off'),
40 ('look.statusbar', 'off'))
41
42 # Default colors for WeeChat color options (option name, #rgb value)
43 CONFIG_DEFAULT_COLOR_OPTIONS = (
44 ('separator', '#000066'), # 0
45 ('chat', '#000000'), # 1
46 ('chat_time', '#999999'), # 2
47 ('chat_time_delimiters', '#000000'), # 3
48 ('chat_prefix_error', '#FF6633'), # 4
49 ('chat_prefix_network', '#990099'), # 5
50 ('chat_prefix_action', '#000000'), # 6
51 ('chat_prefix_join', '#00CC00'), # 7
52 ('chat_prefix_quit', '#CC0000'), # 8
53 ('chat_prefix_more', '#CC00FF'), # 9
54 ('chat_prefix_suffix', '#330099'), # 10
55 ('chat_buffer', '#000000'), # 11
56 ('chat_server', '#000000'), # 12
57 ('chat_channel', '#000000'), # 13
58 ('chat_nick', '#000000'), # 14
59 ('chat_nick_self', '*#000000'), # 15
60 ('chat_nick_other', '#000000'), # 16
61 ('', '#000000'), # 17 (nick1 -- obsolete)
62 ('', '#000000'), # 18 (nick2 -- obsolete)
63 ('', '#000000'), # 19 (nick3 -- obsolete)
64 ('', '#000000'), # 20 (nick4 -- obsolete)
65 ('', '#000000'), # 21 (nick5 -- obsolete)
66 ('', '#000000'), # 22 (nick6 -- obsolete)
67 ('', '#000000'), # 23 (nick7 -- obsolete)
68 ('', '#000000'), # 24 (nick8 -- obsolete)
69 ('', '#000000'), # 25 (nick9 -- obsolete)
70 ('', '#000000'), # 26 (nick10 -- obsolete)
71 ('chat_host', '#666666'), # 27
72 ('chat_delimiters', '#9999FF'), # 28
73 ('chat_highlight', '#3399CC'), # 29
74 ('chat_read_marker', '#000000'), # 30
75 ('chat_text_found', '#000000'), # 31
76 ('chat_value', '#000000'), # 32
77 ('chat_prefix_buffer', '#000000'), # 33
78 ('chat_tags', '#000000'), # 34
79 ('chat_inactive_window', '#000000'), # 35
80 ('chat_inactive_buffer', '#000000'), # 36
81 ('chat_prefix_buffer_inactive_buffer', '#000000'), # 37
82 ('chat_nick_offline', '#000000'), # 38
83 ('chat_nick_offline_highlight', '#000000'), # 39
84 ('chat_nick_prefix', '#000000'), # 40
85 ('chat_nick_suffix', '#000000'), # 41
86 ('emphasis', '#000000'), # 42
87 ('chat_day_change', '#000000'), # 43
88 )
89 config_color_options = []
90
91
92 def read():
93 """Read config file."""
94 global config_color_options
95 config = ConfigParser.RawConfigParser()
96 if os.path.isfile(CONFIG_FILENAME):
97 config.read(CONFIG_FILENAME)
98
99 # add missing sections/options
100 for section in CONFIG_DEFAULT_SECTIONS:
101 if not config.has_section(section):
102 config.add_section(section)
103 for option in reversed(CONFIG_DEFAULT_OPTIONS):
104 section, name = option[0].split('.', 1)
105 if not config.has_option(section, name):
106 config.set(section, name, option[1])
107 section = 'color'
108 for option in reversed(CONFIG_DEFAULT_COLOR_OPTIONS):
109 if option[0] and not config.has_option(section, option[0]):
110 config.set(section, option[0], option[1])
111
112 # build list of color options
113 config_color_options = []
114 for option in CONFIG_DEFAULT_COLOR_OPTIONS:
115 if option[0]:
116 config_color_options.append(config.get('color', option[0]))
117 else:
118 config_color_options.append('#000000')
119
120 return config
121
122
123 def write(config):
124 """Write config file."""
125 if not os.path.exists(CONFIG_DIR):
126 os.mkdir(CONFIG_DIR, 0o0755)
127 with open(CONFIG_FILENAME, 'wb') as cfg:
128 config.write(cfg)
129
130
131 def color_options():
132 global config_color_options
133 return config_color_options