2010-02-09 02:45:56 +01:00
######################################################################
# Copyright 2009, 2010 ryuslash
#
# This file is part of 4grab.
#
# 4grab 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.
#
# 4grab 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 4grab. If not, see <http://www.gnu.org/licenses/>.
######################################################################
2010-01-18 20:08:38 +01:00
import os
import ConfigParser
2010-02-08 01:24:18 +01:00
import sys
homedir = os . getenv ( " HOME " )
if homedir is None :
homedir = os . path . dirname ( sys . argv [ 0 ] )
2010-01-18 20:08:38 +01:00
class _Configuration ( object ) :
def __init__ ( self ) :
2010-02-08 01:24:18 +01:00
self . filename = os . path . join ( os . path . join ( homedir , " .4grab " ) , " config.cfg " )
2010-01-18 20:08:38 +01:00
self . configparser = ConfigParser . RawConfigParser ( )
if not os . path . exists ( self . filename ) :
self . create_new ( )
else :
self . configparser . read ( self . filename )
def create_new ( self ) :
self . configparser . add_section ( " settings " )
2010-02-09 01:32:42 +01:00
self . set_category ( self . raw_input_with_default ( " w " , " Please enter which category you would like to download from: " ) )
2010-01-18 20:08:38 +01:00
self . configparser . add_section ( " locations " )
2010-02-08 01:24:18 +01:00
self . configparser . set ( " locations " , " download " , self . raw_input_with_default ( os . path . join ( homedir , " Pictures " ) , " Please enter where you would like the downloads to go: " ) )
2010-01-18 20:08:38 +01:00
2010-02-09 01:32:42 +01:00
self . save ( )
2010-01-18 20:08:38 +01:00
def raw_input_with_default ( self , default , prompt ) :
inp = raw_input ( " %s (default= %s ): " % ( prompt , default ) )
if inp == " " :
return default
return inp
def get_download_location ( self ) :
return self . configparser . get ( " locations " , " download " )
def get_category ( self ) :
return self . configparser . get ( " settings " , " category " )
2010-02-09 01:32:42 +01:00
def set_category ( self , value ) :
self . configparser . set ( " settings " , " category " , value )
def option_exists ( self , option ) :
2010-02-09 02:10:17 +01:00
sections = self . configparser . sections ( )
for section in sections :
2010-02-09 01:32:42 +01:00
if self . configparser . has_option ( section , option ) :
return True
return False
2010-02-09 02:10:17 +01:00
def set_option ( self , option , value ) :
sec = None
sections = self . configparser . sections ( )
for section in sections :
if self . configparser . has_option ( section , option ) :
sec = section
if not sec is None :
self . configparser . set ( sec , option , value )
return True
else :
return False
2010-02-09 01:32:42 +01:00
def save ( self ) :
dirname = os . path . dirname ( self . filename )
if not os . path . exists ( dirname ) :
os . mkdir ( dirname )
configfile = open ( self . filename , " w " )
self . configparser . write ( configfile )
2010-01-18 20:08:38 +01:00
_configuration = _Configuration ( )
def Configuration ( ) : return _configuration