/
args.py
124 lines (105 loc) · 4.42 KB
/
args.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
#Canto - ncurses RSS reader
# Copyright (C) 2008 Jack Miller <jack@codezen.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# This is probably the most straightforward file in the code base. It handles
# all of the argument parsing and interprets all of the common arguments between
# canto and canto-fetch.
# The one thing to note is that every option that is parsed with an actual
# argument must have that argument converted to unicode right off the bat.
from const import *
import getopt
import sys
import os
def print_canto_usage():
print "USAGE: canto [-hvulaniortDCLF]"
print "--help -h This help."
print "--version -v Print version info."
print "--update -u Fetch updates before running."
print "--list -l List configured feeds."
print "--checkall -a Prints number of new items."
print "--checknew -n [feed] Prints number of items that are new in feed."
print ""
print "--opml -o Convert conf to OPML and print to stdout."
print "--import -i [path] Add feeds from OPML file to conf."
print "--url -r [url] Add feed at URL to conf."
print "--tag -t [tag] Set tag (for -r)"
print ""
print_common_usage()
def print_fetch_usage():
print "USAGE: canto-fetch [-hvVfdbDCLF]"
print "--help -h This help."
print "--version -v Print version info."
print "--verbose -V Print extra info while running."
print "--force -f Force update, regardless of timeestamps."
print "--daemon -d Run as a daemon."
print "--background -b Background (implies -d)"
print "--interval -i Update interval when run as a daemon"
print "--sysfp -s Use system feedparser instead of builtin."
print ""
print_common_usage()
def print_common_usage():
print "--dir -D [path] Set configuration directory. (~/.canto/)"
print "--conf -C [path] Set configuration file. (~/.canto/conf)"
print "--log -L [path] Set client log file. (~/.canto/log)"
print "--fdir -F [path] Set feed directory. (~/.canto/feeds/)"
print "--sdir -S [path] Set script directory (~/.canto/scripts/)"
def parse_common_args(enc, extra_short, extra_long, iam="canto"):
shortopts = 'D:C:L:F:S:' + extra_short
longopts = ["dir=","conf=","log=","fdir=","sdir="] + extra_long
try :
optlist = getopt.getopt(sys.argv[1:],shortopts,longopts)[0]
except getopt.GetoptError, e:
print "Error: %s" % e.msg
sys.exit(-1)
for opt, arg in optlist:
if opt in ["-D", "--dir"]:
conf_dir = unicode(arg, enc, "ignore")
break
else:
conf_dir = os.getenv("HOME") + "/.canto/"
if conf_dir[-1] != '/' :
conf_dir += '/'
if not os.path.exists(conf_dir):
os.mkdir(conf_dir)
if iam == "canto":
log_file = conf_dir + "log"
else:
log_file = conf_dir + "fetchlog"
conf_file = conf_dir + "conf.py"
feed_dir = conf_dir + "feeds/"
script_dir = conf_dir + "scripts/"
# Make sure that the {feed,script}_dir does, indeed, exist and is
# actually a directory.
for dir in [feed_dir, script_dir]:
if not os.path.exists(dir):
os.mkdir(dir)
elif not os.path.isdir(dir):
os.unlink(dir)
os.mkdir(dir)
for opt, arg in optlist :
if opt in ["-C", "--conf"] :
conf_file = unicode(arg, enc, "ignore")
elif opt in ["-L","--log"] :
log_file = unicode(arg, enc, "ignore")
elif opt in ["-F","--fdir"] :
feed_dir = unicode(arg, enc, "ignore")
if feed_dir[-1] != '/' :
feed_dir += '/'
elif opt in ["-S","--sdir"] :
script_dir = unicode(arg, enc, "ignore")
if script_dir[-1] != '/' :
script_dir += '/'
elif opt in ["-h","--help"] :
if iam == "canto":
print_canto_usage()
else:
print_fetch_usage()
sys.exit(0)
elif opt in ["-v","--version"] :
print "Canto v %s (%s)" % ("%d.%d.%d" % VERSION_TUPLE, GIT_SHA)
sys.exit(0)
return (conf_dir, log_file, conf_file, feed_dir, script_dir, optlist)