Skip to content

Commit

Permalink
[#20]add clear all history option (-d)
Browse files Browse the repository at this point in the history
  • Loading branch information
sk1418 committed Dec 1, 2015
1 parent 5e62389 commit 5b0517f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
24 changes: 24 additions & 0 deletions zhuaxia/hist_handler.py
Expand Up @@ -25,6 +25,22 @@ def __getConnection():
conn = sqlite.connect(config.HIST_DB)
return conn

def get_history_count():
conn = __getConnection()
dao = HistDao(conn)
count = dao.get_history_count()
conn.close()
return count

def empty_hists():
"""empty history table"""
conn = __getConnection()
dao = HistDao(conn)
hists = dao.delete_all()
print log.hl(msg.history_cleared,'cyan')
conn.commit()
conn.close()

def export_hists():
"""
export all history data
Expand Down Expand Up @@ -106,6 +122,14 @@ def get_history(self,song):
cur.close()
return hist

def get_history_count(self):
sql = "select count(*) from History"
cur = self.conn.cursor()
cur.execute(sql)
count = cur.fetchone()[0]
cur.close()
return count

def get_all_histories(self):
sql = SELECT_PART
cur = self.conn.cursor()
Expand Down
3 changes: 3 additions & 0 deletions zhuaxia/i18n/msg_cn.py
Expand Up @@ -13,6 +13,9 @@
summary_prompt_err = u" 无效输入\n"
summary_saved = u" 下载报告保存于: %s"

history_clear_confirm = u" 找到 %d 条下载记录, 确认要清空所有下载历史记录? [y/n]"
history_clearing = u" 忽略其它选项,清空zhuaxia下载历史记录..."
history_cleared = u" zhuaxia所有下载记录已清空"
history_exporting = u" 忽略其它选项, 正在导出下载历史记录..."
history_exported = u" 下载历史记录导出到: %s"

Expand Down
3 changes: 3 additions & 0 deletions zhuaxia/i18n/msg_en.py
Expand Up @@ -13,6 +13,9 @@
summary_prompt_err = u"Invalid input.\n"
summary_saved = u" summary was saved at: %s"

history_clear_confirm = u" %d downloading histories found. Are you sure to remove all these histories? [y/n]"
history_clearing = u" Clear zhuaxia downloading hisotory (other options will be ignored)"
history_cleared = u" All zhuaxia download history has been cleared."
history_exporting = u" Exporting download history (other options will be ignored)..."
history_exported = u" Zhuaxia download-history was exported to: %s"

Expand Down
35 changes: 30 additions & 5 deletions zx
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import zhuaxia.config as config
import sys

#load config at very beginning
config.load_config()
Expand Down Expand Up @@ -36,6 +37,23 @@ def version():
"""print version"""
print msg.ver_text + zxver.version

def export_or_clear_hist(export_hist, empty_hist):
if export_hist and empty_hist : raise getopt.GetoptError("option -e and -d cannot be used together!")
if export_hist:
hist_handler.export_hists()
sys.exit(0)
elif empty_hist:
print log.hl(msg.history_clearing ,'cyan')
while True:
count = hist_handler.get_history_count()
sys.stdout.write(msg.history_clear_confirm % count)
choice = raw_input().lower()
if choice == 'n':
sys.exit(0)
elif choice == 'y':
hist_handler.empty_hists()
sys.exit(0)

if __name__ == '__main__':
log.setup_log(logger_name, config.LOG_LVL_CONSOLE, config.LOG_LVL_FILE)
LOG = log.get_logger(logger_name)
Expand All @@ -47,10 +65,12 @@ if __name__ == '__main__':
needProxyPool = False
dl_lyric = False
incremental_dl = False
export_hist = False
empty_hist = False

if len(sys.argv)<2: raise getopt.GetoptError("parameters are missing...")

opts, args = getopt.getopt(sys.argv[1:],":f:Hhvplie")
opts, args = getopt.getopt(sys.argv[1:],":f:Hhvplied")
for o, a in opts:
if o == '-h':
usage()
Expand All @@ -70,10 +90,14 @@ if __name__ == '__main__':
elif o == '-i':
incremental_dl = True
elif o == '-e': #export history data
hist_handler.export_hists()
sys.exit(0)

export_hist = True
elif o == '-d': #empty history
empty_hist = True

#handle the export and empty
export_or_clear_hist(export_hist, empty_hist)

## -f check file
if not inFile:
if not len(args):
raise getopt.GetoptError("input file or xiami url is needed")
Expand All @@ -92,9 +116,10 @@ if __name__ == '__main__':
#print debug
option.debug_me()

# ok, let's go!
commander.shall_I_begin(option)

except getopt.GetoptError as e:
LOG.error(str(e))
usage()
print log.hl("\nTo know how to use zhuaxia, run:\n\tzx -h\n",'cyan')
sys.exit(2)

0 comments on commit 5b0517f

Please sign in to comment.