-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythagoreModule.py
72 lines (65 loc) · 2.37 KB
/
PythagoreModule.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Pythagore ("2.0")
# A Python IRC Bot
#
# PythagoreModule.py : Module class for Pythagore bot
#
# Copyright © 2007 Nicolas Dandrimont <[email protected]>
#
# This file is part of Pythagore.
#
# Pythagore is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# Pythagore is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# of FITNESS FOR ANY PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pythagore; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
class PythagoreModule(object):
"""This is the class all Pythagore's Modules should subclass
In fact it is quite empty..."""
def __init__(self, pythagore):
"""Initialization takes a pythagore argument which is the current bot
instance"""
self.exports = {}
self.bot = pythagore
self.config = self.load_config()
self.name = self.__class__.__name__
def load_config(self):
"""Configuration initialization. Override this if you do not want
plaintext config"""
try:
import yaml
except ImportError:
return {}
name = self.__class__.__name__
try:
configfile = file(os.path.join("Config", name + ".yml"), 'r')
config = yaml.safe_load(configfile)
configfile.close()
except IOError:
print _("Config file for %(module)s not open !") % {'module': name}
config = {}
return config
def save_config(self):
"""Configuration file save. Should be more or less failproof"""
try:
import yaml
except ImportError:
return
name = self.__class__.__name__
try:
newconfigfile = file(os.path.join("Config", name + ".new.yml"), 'w')
yaml.dump(self.config, newconfigfile, default_flow_style=False)
os.rename(os.path.join("Config", name + ".new.yml"),
os.path.join("Config", name + ".yml"))
except IOError:
return