def main(): command = Command('kindlepush', 'automatically deliver your docs to your kindle', version=__version__) command.option('-c, --count [count]', 'the count of the docs to deliver') try: with open(os.path.join(sys.path[0], 'kindlepush_config.json')) as f: config = json.load(f) except IOError: sys.exit("Check your config file in {0}".format(sys.path[0])) @command.action def read(number=config['number']): """ read the log file :param number: the count of days :option number: -n, --number [number] """ file_path = os.path.join(config['directory'], 'kindlepush.log') os.system('tail -n {0} {1}'.format(number, file_path)) @command.action def pending(): """ get pending deliveries """ login(config) titles = get_pending_deliveries() print "Pending Deliveries:" for title in titles: print '\t{0}'.format(title) command.parse() logging.basicConfig( filename=os.path.join(config['directory'], 'kindlepush.log'), level='INFO', format='%(asctime)s [%(levelname)s] %(message)s') # Disable unwanted log message from the requests library requests_log = logging.getLogger("requests") requests_log.setLevel(logging.WARNING) # Connect to the database lying the directory specified in the config database = sqlite3.connect(os.path.join(config['directory'], 'kindlepush.db')) # overwrite the value of count if user has specified it if command.count: config['count'] = command.count if not 'read' and 'pending' in sys.argv: try: login(config) deliver_all(get_contents(config), database) except KeyError: print 'KeyError, check your config file please.' except requests.exceptions.ConnectionError: print 'Check your network please.'
def test_alias(self): program = Command('list', alias=['ls']) program.option('-v, --verbose', 'show more log') program.parse( 'ls -v' ) assert program.verbose
def main(): signin_url = 'https://www.v2ex.com/signin' balance_url = 'https://www.v2ex.com/balance' mission_url = 'https://www.v2ex.com/mission/daily' # get the configuration try: with open(os.path.join(sys.path[0], 'v2ex_config.json')) as f: config = json.load(f) except IOError: sys.exit("Don't forget your config.json.\nPlease read " "https://github.com/lord63/a_bunch_of_code/tree/master/v2ex") command = Command('v2ex_daily_mission', description='complete the mission and get money', version=__version__) # subcommand @command.action def read(count=config['count']): """ read log file :param count: read the count of the recent days :option count: -c, --count [count] """ file_path = os.path.join(config['log_directory'], 'v2ex.log') for line in deque(open(file_path), int(count)): print line, @command.action def last(): """how long you have kept signing in""" login(signin_url, config) r = session.get(mission_url, verify=False) tree = html.fromstring(r.text) last = tree.xpath( '//div[@id="Main"]/div[@class="box"]/div[3]/text()')[0].strip() print last.encode('utf-8') command.parse() # set log logging.basicConfig( filename=os.path.join(config['log_directory'], 'v2ex.log'), level='INFO', format='%(asctime)s [%(levelname)s] %(message)s') # disable log message from the requests library requests_log = logging.getLogger("requests") requests_log.setLevel(logging.WARNING) if len(sys.argv) == 1: try: login(signin_url, config) get_money(mission_url, balance_url) except KeyError: print 'Keyerror, please check your config file.' except IndexError: print 'Please check your username and password.'
def test_help_footer(self): footers = [ 'Examples:', '', ' $ terminal -h' ] program = Command('footer', help_footer='\n'.join(footers)) program.print_help()
def test_subcommand(self): program = Command('subcommand') @program.subcommand def bar(): return 'bar' program.print_help()
def test_run_parse(self): program = Command('run-parse') def func(**kwargs): print('func') program._command_func = func program.parse()
def test_call(self): # for __call__ program = Command('call') @program def bar(): return 'bar' program.print_help()
def main(): signin_url = 'https://www.v2ex.com/signin' balance_url = 'https://www.v2ex.com/balance' mission_url = 'https://www.v2ex.com/mission/daily' # get the configuration try: with open(os.path.join(sys.path[0], 'v2ex_config.json')) as f: config = json.load(f) except IOError: sys.exit("Don't forget your config.json.\nPlease read ") command = Command('v2ex_daily_mission', description='complete the mission and get money', version="") # subcommand @command.action def read(count=config['count']): """ read log file :param count: read the count of the recent days :option count: -c, --count [count] """ file_path = os.path.join(config['log_directory'], 'v2ex.log') for line in deque(open(file_path), int(count)): print(line), @command.action def last(): """how long you have kept signing in""" login(signin_url, config) r = session.get(mission_url, verify=False) tree = html.fromstring(r.text) last = tree.xpath( '//div[@id="Main"]/div[@class="box"]/div[3]/text()')[0].strip() print(last.encode('utf-8')) command.parse() # set log logging.basicConfig(filename=os.path.join(config['log_directory'], 'v2ex.log'), level='INFO', format='%(asctime)s [%(levelname)s] %(message)s') # disable log message from the requests library requests_log = logging.getLogger("requests") requests_log.setLevel(logging.WARNING) if len(sys.argv) == 1: try: login(signin_url, config) get_money(mission_url, balance_url) except KeyError: print('Keyerror, please check your config file.') except IndexError: print('Please check your username and password.')
def test_print(self): program = Command('print', title='foobar', version='1.0.0') program.print_version() program.print_help() program._usage = 'print [options]' program.print_help()
def test_add_action(self): program = Command('add-action') @program.action def hello(bar): """ description of hello usage: hello <bar> """ assert bar == 'baz' program.parse('add-action hello baz') program.print_help()
def test_action(self): program = Command('foo') @program.action def lepture(bar): assert bar == 'lepture' program.parse('foo lepture --bar lepture') # subcommand itself program.action(program) program.parse('foo foo lepture --bar lepture')
def test_missing_option(self): program = Command('missing-option') program.option('-o, --output <dir>', 'output directory') program.parse('missing-option -o')
def test_func(self): def bar(): return 'bar' program = Command('func', func=bar) program.parse('func')
def test_get(self): program = Command('get') program.get('bar')
def test_parse(self): program = Command('parse', version='1.0.0') program.option('-f', 'force') program.option('-v, --verbose', 'show more log') program.option('--no-color', 'output without color') program.option('-t, --tag <tag>', 'tag name') program.option('-s [source]', 'source repo') program.option('--key <keyword>', 'keywords') program.print_version() program.print_help() program.parse('parse -f -v --verbose --no-color bar -t tag --key=what') assert program.get('-f') assert program.verbose assert program.tag == 'tag' assert program.color is False assert program.key == 'what'
def test_get_default(self): program = Command('get-default') program.option('--output [dir]', 'output dir, default: site') assert program.output == 'site'
def test_parse(self): program = Command('foo') program.option('-f', 'force') program.option('-v, --verbose', 'show more log') program.option('--no-color', 'output without color') program.option('-t, --tag <tag>', 'tag name') program.option('-s <source>', 'source repo') program.option('--key <keyword>', 'keywords') program.parse( 'foo -f -v --verbose --no-color bar -t tag --key=what'.split() ) assert program.get('-f') assert program.verbose assert program.tag == 'tag' assert program.color is False assert program.keyword == 'what'
def test_help_footer(self): footers = ['Examples:', '', ' $ terminal -h'] program = Command('footer', help_footer='\n'.join(footers)) program.print_help()
def test_attr(self): program = Command('attr') program.bar
def test_arguments(self): program = Command('arguments', arguments=['name', 'hello']) program.print_help() program.parse('arguments foo') assert program.name == 'foo'
def test_missing_required(self): program = Command('missing-required') program.option('-o, --output <dir>', 'output directory') program.parse('missing-required')
def test_as_container(self): program = Command('container', version='1.0.0') program.option('-f', 'force') program.option('-v, --verbose', 'show more log') program.option('--no-color', 'output without color') program.option('-t, --tag <tag>', 'tag name') program.option('-s [source]', 'source repo') program.option('--key <keyword>', 'keywords') program.parse( 'container -f -v --verbose --no-color bar -t tag --key=what') assert 'verbose' in dict(program)
def test_action(self): program = Command('action') @program.action def lepture(bar, color=True, force=False, msg='hello'): """ description of lepture subcommand. :param bar: description of bar :option bar: --bar [name] """ assert bar == 'lepture' program.print_version() program.print_help() program.parse('action lepture --bar lepture') program.parse('action lepture --bar lepture baz') assert 'baz' in program.args # subcommand itself program.action(program) program.parse('action action lepture --bar lepture')
def test_as_container(self): program = Command('container', version='1.0.0') program.option('-f', 'force') program.option('-v, --verbose', 'show more log') program.option('--no-color', 'output without color') program.option('-t, --tag <tag>', 'tag name') program.option('-s [source]', 'source repo') program.option('--key <keyword>', 'keywords') program.parse( 'container -f -v --verbose --no-color bar -t tag --key=what' ) assert 'verbose' in dict(program)
def test_parse(self): program = Command('parse', version='1.0.0') program.option('-f', 'force') program.option('-v, --verbose', 'show more log') program.option('--no-color', 'output without color') program.option('-t, --tag <tag>', 'tag name') program.option('-s [source]', 'source repo') program.option('--key <keyword>', 'keywords') program.print_version() program.print_help() program.parse( 'parse -f -v --verbose --no-color bar -t tag --key=what' ) assert program.get('-f') assert program.verbose assert program.tag == 'tag' assert program.color is False assert program.key == 'what'