#!/usr/bin/env python
#-*- coding: utf-8 -*-
'''gwit setup script
'''
################################################################################
#
# Copyright (c) 2010 University of Tsukuba Linux User Group
#
# This file is part of "gwit".
#
# "gwit" is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# "gwit" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A 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 "gwit". If not, see .
#
################################################################################
import os
import sys
import optparse
from ConfigParser import SafeConfigParser
__version__ = '0.9.2-beta'
__author__ = 'University of Tsukuba Linux User Group '
def print_version():
print """\
%(name)s %(version)s
Copyright (C) %(year)s %(copyright)s
%(name)s comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of %(name)s
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.""" % {
"name" : "gwit",
"version" : __version__,
"year" : "2011",
"copyright" : __author__,
}
if __name__ == "__main__":
# Option Parser
p = optparse.OptionParser()
p.add_option("-u", "--user", dest="user",
help="choose auth user", metavar="USER")
p.add_option("--add", dest="add", action="store_true",
help="add new user")
p.add_option("-v", "--version", dest="version",
action="store_true", help="print version")
(options, args) = p.parse_args()
if options.version:
print_version()
exit()
# Config Parser
confp = SafeConfigParser()
# Config file absolute path (~/.gwit/config)
conf_dir = os.path.expanduser("~/.gwit")
if not os.path.isdir(conf_dir):
os.mkdir(conf_dir)
conf_path = os.path.join(conf_dir, "config")
# Set gwit library path into python path
script_path = os.path.realpath(__file__)
#lib_path = os.path.join(os.path.dirname(script_path), "../")
#sys.path.insert(0, os.path.realpath(lib_path))
# config file exist?
fexist = os.path.isfile(conf_path)
if fexist:
# readfile
confp.read(conf_path)
if not fexist or options.add:
# Run Setup Wizard
from gwitlib import SetupWizard
setup = SetupWizard()
setup.main()
if setup.ok:
# if setup ok, set settings
if not fexist:
confp.set("DEFAULT", "default_user", setup.screen_name)
confp.add_section(setup.screen_name)
confp.set(setup.screen_name, "ckey", setup.keys[0])
confp.set(setup.screen_name, "csecret", setup.keys[1])
confp.set(setup.screen_name, "atoken", setup.keys[2])
confp.set(setup.screen_name, "asecret", setup.keys[3])
confp.set(setup.screen_name, "footer", "")
user = setup.screen_name
del setup
# write config
fp = open(conf_path, "w")
confp.write(fp)
fp.close()
else:
# or die
exit()
# Settings dict
settings = dict()
settings["DEFAULT"] = dict(confp.items("DEFAULT"))
for i in confp.sections():
settings[i] = dict(confp.items(i))
# Read settings
if options.user != None:
user = options.user
elif options.add:
pass
else:
user = settings["DEFAULT"]["default_user"]
try:
keys = (settings[user]["ckey"], settings[user]["csecret"],
settings[user]["atoken"], settings[user]["asecret"])
except KeyError:
print >>sys.stderr, "[Error] User '%s' not exists in config file." % options.user
print >>sys.stderr, "Try `gwit --add` to add user."
exit()
# Run Main()
from gwitlib import Main
gwit = Main(user, keys)
gwit.main()