Skip to content

Commit

Permalink
Merge pull request #32 from Hamuko/master
Browse files Browse the repository at this point in the history
CLI option to specify config file location
  • Loading branch information
Nandaka committed Apr 24, 2014
2 parents 3858b10 + 7d4899b commit 81f5d13
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
27 changes: 18 additions & 9 deletions PixivConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ class PixivConfig:
skipDumpFilter = ""
dumpMediumPage = False

def loadConfig(self):
configFile = script_path + os.sep + 'config.ini'
def loadConfig(self, path=None):
if path != None:
configFile = path
else:
configFile = script_path + os.sep + 'config.ini'

print 'Reading', configFile, '...'
oldSetting = False
haveError = False
Expand Down Expand Up @@ -374,7 +378,7 @@ def loadConfig(self):


#-UI01B------write config
def writeConfig(self, error=False):
def writeConfig(self, error=False, path=None):
'''Backup old config if exist and write updated config.ini'''
print 'Writing config file...',
config = ConfigParser.RawConfigParser()
Expand Down Expand Up @@ -428,19 +432,24 @@ def writeConfig(self, error=False):
config.set('Pixiv', 'numberOfPage', self.numberOfPage)
config.set('Pixiv', 'R18Mode', self.r18mode)

if path != None:
configlocation = path
else:
configlocation = 'config.ini'

try:
##with codecs.open('config.ini.bak', encoding = 'utf-8', mode = 'wb') as configfile:
with open('config.ini.tmp', 'w') as configfile:
with open(configlocation + '.tmp', 'w') as configfile:
config.write(configfile)
if os.path.exists('config.ini'):
if os.path.exists(configlocation):
if error:
backupName = 'config.ini.error-' + str(int(time.time()))
backupName = configlocation + '.error-' + str(int(time.time()))
print "Backing up old config (error exist!) to " + backupName
shutil.move('config.ini', backupName)
shutil.move(configlocation, backupName)
else:
print "Backing up old config to config.ini.bak"
shutil.move('config.ini', 'config.ini.bak')
os.rename('config.ini.tmp', 'config.ini')
shutil.move(configlocation, configlocation + '.bak')
os.rename(configlocation + '.tmp', configlocation)
except:
self.__logger.exception('Error at writeConfig()')
raise
Expand Down
15 changes: 10 additions & 5 deletions PixivUtil2.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def pixiv_process_login(response):
if cookie.name == 'PHPSESSID':
print 'new cookie value:', cookie.value
__config__.cookie = cookie.value
__config__.writeConfig()
__config__.writeConfig(path=configfile)
break
return True
else:
Expand Down Expand Up @@ -388,7 +388,7 @@ def process_member(mode, member_id, user_dir='', page=1, end_page=0, bookmark=Fa
elif __config__.numberOfPage != 0:
PixivHelper.printAndLog('info', 'End Page from config: ' + str(__config__.numberOfPage))

__config__.loadConfig()
__config__.loadConfig(path=configfile)
list_page = None
try:
no_of_images = 1
Expand Down Expand Up @@ -824,7 +824,7 @@ def process_tags(mode, tags, page=1, end_page=0, wild_card=True, title_caption=F
start_date=None, end_date=None, use_tags_as_dir=False, member_id=None,
bookmark_count=None):
try:
__config__.loadConfig() # Reset the config for root directory
__config__.loadConfig(path=configfile) # Reset the config for root directory

try:
if tags.startswith("%"):
Expand Down Expand Up @@ -1567,7 +1567,7 @@ def menu_export_online_bookmark(mode, opisvalid, args):

def menu_reload_config():
__log__.info('Manual Reload Config.')
__config__.loadConfig()
__config__.loadConfig(path=configfile)


def menu_print_config():
Expand Down Expand Up @@ -1606,6 +1606,9 @@ def setup_option_parser():
action='store_true', default=False)
parser.add_option('-n', '--numberofpages', dest='numberofpages',
help='temporarily overwrites numberOfPage set in config.ini')
parser.add_option('-c', '--config', dest='configlocation',
help='load the config file from a custom location',
default=None)
return parser


Expand Down Expand Up @@ -1690,6 +1693,7 @@ def main():
global start_iv # used in download_image
global op
global __br__
global configfile

parser = setup_option_parser()
(options, args) = parser.parse_args()
Expand All @@ -1705,6 +1709,7 @@ def main():
# Yavos: use print option instead when program should be running even with this error

ewd = options.exitwhendone
configfile = options.configlocation

try:
if options.numberofpages is not None:
Expand All @@ -1721,7 +1726,7 @@ def main():
__log__.info('###############################################################')
__log__.info('Starting...')
try:
__config__.loadConfig()
__config__.loadConfig(path=configfile)
PixivHelper.setConfig(__config__)
except:
print 'Failed to read configuration.'
Expand Down

0 comments on commit 81f5d13

Please sign in to comment.