OSDN Git Service

python-twoauth update for SSL support
[gwit/gwit.git] / gwit
1 #!/usr/bin/env python
2 #-*- coding: utf-8 -*-
3
4 '''gwit setup script
5 '''
6
7 ################################################################################
8 #
9 # Copyright (c) 2010 University of Tsukuba Linux User Group
10 #
11 # This file is part of "gwit".
12 #
13 # "gwit" is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation, either version 3 of the License, or
16 # (at your option) any later version.
17 #
18 # "gwit" is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with "gwit".  If not, see <http://www.gnu.org/licenses/>.
25 #
26 ################################################################################
27
28
29 import os
30 import sys
31 import optparse
32 from ConfigParser import SafeConfigParser
33
34 __version__ = '0.9.2-beta'
35 __author__ = 'University of Tsukuba Linux User Group <staff@tsukuba-linux.org>'
36
37 def print_version():
38     print """\
39 %(name)s %(version)s
40 Copyright (C) %(year)s %(copyright)s
41 %(name)s comes with ABSOLUTELY NO WARRANTY.
42 You may redistribute copies of %(name)s
43 under the terms of the GNU General Public License.
44 For more information about these matters, see the file named COPYING.""" % {
45        "name" : "gwit",
46        "version" : __version__,
47        "year" : "2011",
48        "copyright" : __author__,
49        }
50
51 if __name__ == "__main__":
52     # Option Parser
53     p = optparse.OptionParser()
54     p.add_option("-u", "--user", dest="user", 
55                  help="choose auth user", metavar="USER")
56     p.add_option("--add", dest="add", action="store_true", 
57                  help="add new user")
58     p.add_option("-v", "--version", dest="version", 
59                  action="store_true", help="print version")
60     
61     (options, args) = p.parse_args()
62     
63     if options.version:
64         print_version()
65         exit()
66         
67     # Config Parser
68     confp = SafeConfigParser()
69         
70     # Config file absolute path (~/.gwit/config)
71     conf_dir = os.path.expanduser("~/.gwit")
72     if not os.path.isdir(conf_dir):
73         os.mkdir(conf_dir)
74     conf_path = os.path.join(conf_dir, "config")
75     
76     # Set gwit library path into python path
77     script_path = os.path.realpath(__file__)
78     #lib_path = os.path.join(os.path.dirname(script_path), "../")
79     #sys.path.insert(0, os.path.realpath(lib_path))
80     
81     # config file exist?
82     fexist = os.path.isfile(conf_path)
83     
84     if fexist:
85         # readfile
86         confp.read(conf_path)
87
88     if not fexist or options.add:
89         # Run Setup Wizard
90         from gwitlib import SetupWizard
91         setup = SetupWizard()
92         setup.main()
93         
94         if setup.ok:
95             # if setup ok, set settings
96             if not fexist:
97                 confp.set("DEFAULT", "default_user", setup.screen_name)
98             confp.add_section(setup.screen_name)
99             confp.set(setup.screen_name, "ckey", setup.keys[0])
100             confp.set(setup.screen_name, "csecret", setup.keys[1])
101             confp.set(setup.screen_name, "atoken", setup.keys[2])
102             confp.set(setup.screen_name, "asecret", setup.keys[3])
103             confp.set(setup.screen_name, "footer", "")
104             user = setup.screen_name
105             del setup
106             
107             # write config
108             fp = open(conf_path, "w")
109             confp.write(fp)
110             fp.close()
111         else:
112             # or die
113             exit()
114     
115     # Settings dict
116     settings = dict()
117     settings["DEFAULT"] = dict(confp.items("DEFAULT"))
118     for i in confp.sections():
119         settings[i] = dict(confp.items(i))
120    
121     # Read settings
122     if options.user != None:
123         user = options.user
124     elif options.add:
125         pass
126     else:
127         user = settings["DEFAULT"]["default_user"]
128     
129     try:
130         keys = (settings[user]["ckey"], settings[user]["csecret"], 
131                 settings[user]["atoken"], settings[user]["asecret"])
132     except KeyError:
133         print >>sys.stderr, "[Error] User '%s' not exists in config file." % options.user
134         print >>sys.stderr, "Try `gwit --add` to add user."
135         exit()
136     
137     # Run Main()
138     from gwitlib import Main
139     gwit = Main(user, keys)
140     gwit.main()