configfile.py

Sun, 16 Aug 2015 23:30:11 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 16 Aug 2015 23:30:11 +0300
changeset 154
df862cca1773
parent 146
c17b82b1f573
permissions
-rw-r--r--

Added ability to define an IRC command with a function decorator instead of a manifest entry

124
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
1 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
2 Copyright 2014-2015 Teemu Piippo
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
3 All rights reserved.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
4
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
5 Redistribution and use in source and binary forms, with or without
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
6 modification, are permitted provided that the following conditions
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
7 are met:
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
8
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
9 1. Redistributions of source code must retain the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
10 notice, this list of conditions and the following disclaimer.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
12 notice, this list of conditions and the following disclaimer in the
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
13 documentation and/or other materials provided with the distribution.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
14 3. The name of the author may not be used to endorse or promote products
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
15 derived from this software without specific prior written permission.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
16
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
27 '''
7b2cd8b1ba86 - now with extra license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 118
diff changeset
28
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 132
diff changeset
29 from __future__ import print_function
132
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
30 import json, sys
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
31 IsInitialized = False
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
32 Config = None
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
33 ConfigFileName = ""
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 class ConfigNode:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 def __init__ (self, obj, name, parent):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 self.obj = obj
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 self.name = name
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39 self.root = parent if parent == None or parent.root == None else parent.root
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 def keyname (self, key):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 if self.name == None:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 return key
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 return self.name + ':' + key
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 def get_value (self, key, default=None):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 i = 0
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 needSave = False
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 if not key in self.obj:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 if default == None:
132
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
52 raise ValueError ("""Mandatory key '%s' not found""" % self.keyname (key))
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 self.obj[key] = default
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 self.save()
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 return self.obj[key]
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 def set_value (self, key, value):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 self.obj[key] = value
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 self.save()
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
61
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
62 def append_value (self, key, value):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
63 if key not in self.obj:
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
64 self.obj[key] = []
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
65
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
66 self.obj[key].append (value)
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
67 self.save()
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 def get_node (self, key):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 return ConfigNode (obj=self.get_value (key, {}), name=self.keyname (key), parent=self)
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
72 def get_nodelist (self, key, default=None):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
73 data = self.get_value (key, default)
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 result = []
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 for entry in data:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 node = ConfigNode (obj=entry, name=self.keyname (key), parent=self)
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 result.append (node)
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 return result
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
81
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
82 def has_node (self, key):
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
83 return key in self.obj
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
85 def append_nodelist (self, key):
118
dbf49689af0d - added bridging functionality
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
86 data = self.get_value (key, [])
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
87 obj = {}
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
88 data.append (obj)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
89 return ConfigNode (obj=obj, name=self.keyname (key), parent=self)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents: 65
diff changeset
90
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 def save (self):
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 if self.root != None:
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 self.root.save()
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 return
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95
132
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
96 with open (ConfigFileName, 'w') as fp:
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 json.dump (self.obj, fp, sort_keys = True, indent = 1)
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 132
diff changeset
99 print ("""Config saved.""")
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
100
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
101 def find_developer_by_email (self, commit_email):
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 132
diff changeset
102 for developer, emails in self.get_value ('developer_emails', default={}).items():
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
103 if commit_email in emails:
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
104 return developer
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
105
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
106 return ''
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 def init():
132
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
109 filename = 'cobalt.json'
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
110
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
111 if len (sys.argv) >= 2:
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
112 filename = sys.argv[1]
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
113
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
114 global IsInitialized
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
115 global ConfigFileName
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
116 ConfigFileName = filename
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
117
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
118 if not IsInitialized:
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 132
diff changeset
119 print ("""Loading configuration...""")
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120
132
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
121 try:
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
122 with open (filename, 'r') as fp:
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
123 jsondata = json.loads (fp.read())
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
124 except IOError as e:
146
c17b82b1f573 Mercurial handling major overhaul. Also get some stuff ready for Python 3
Teemu Piippo <tsapii@utu.fi>
parents: 132
diff changeset
125 print ("""Couldn't open %s: %s""" % (filename, e))
132
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
126 quit()
65
20bd76353eb5 - modularized the configuration and made it more systematic
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127
132
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
128 global Config
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
129 Config = ConfigNode (jsondata, name=None, parent=None)
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
130 IsInitialized = True
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
131
a22c50f52a23 lots of new stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 124
diff changeset
132 init()

mercurial