##// END OF EJS Templates
Translation updates from RhodeCode v2.2.5...
Translation updates from RhodeCode v2.2.5 Includes a new German translation. These are licensed under GPLv3, for various reasons, including since they include textual copies of strings from the GPLv3'd Python and HTML files.

File last commit:

r4116:ffd45b18 rhodecode-2.2.5-gpl
r4137:6c485794 rhodecode-2.2.5-gpl
Show More
rhodecode_api.py
124 lines | 4.1 KiB | text/x-python | PythonLexer
created rhodecode-api binary script for working with api via cli...
r2379 # -*- coding: utf-8 -*-
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
Bradley M. Kuhn
Imported some of the GPLv3'd changes from RhodeCode v2.2.5....
r4116 """
rhodecode.bin.api
~~~~~~~~~~~~~~~~~
Api CLI client for RhodeCode
:created_on: Jun 3, 2012
:author: marcink
:copyright: (c) 2013 RhodeCode GmbH.
:license: GPLv3, see LICENSE for more details.
"""
created rhodecode-api binary script for working with api via cli...
r2379
from __future__ import with_statement
import sys
import argparse
Added --format into gist CLI...
r3875 from rhodecode.bin.base import json, api_call, RcConf, FORMAT_JSON, FORMAT_PRETTY
created rhodecode-api binary script for working with api via cli...
r2379
def argparser(argv):
added --config option into rhodecode-api script for optional path for...
r2571 usage = (
api: decouple some parts from api CLI script...
r3833 "rhodecode-api [-h] [--format=FORMAT] [--apikey=APIKEY] [--apihost=APIHOST] "
"[--config=CONFIG] [--save-config] "
"METHOD <key:val> <key2:val> ...\n"
Bradley M. Kuhn
Imported some of the GPLv3'd changes from RhodeCode v2.2.5....
r4116 "Create config file: rhodecode-api --apikey=<key> --apihost=http://rhodecode.server --save-config"
added --config option into rhodecode-api script for optional path for...
r2571 )
created rhodecode-api binary script for working with api via cli...
r2379
parser = argparse.ArgumentParser(description='RhodeCode API cli',
usage=usage)
## config
group = parser.add_argument_group('config')
group.add_argument('--apikey', help='api access key')
group.add_argument('--apihost', help='api host')
added --config option into rhodecode-api script for optional path for...
r2571 group.add_argument('--config', help='config file')
api: decouple some parts from api CLI script...
r3833 group.add_argument('--save-config', action='store_true', help='save the given config into a file')
created rhodecode-api binary script for working with api via cli...
r2379
group = parser.add_argument_group('API')
api: decouple some parts from api CLI script...
r3833 group.add_argument('method', metavar='METHOD', nargs='?', type=str, default=None,
created rhodecode-api binary script for working with api via cli...
r2379 help='API method name to call followed by key:value attributes',
)
Added optional --format=json into api cli. That will return pure JSON data from server
r2381 group.add_argument('--format', dest='format', type=str,
Added --format into gist CLI...
r3875 help='output format default: `%s` can '
'be also `%s`' % (FORMAT_PRETTY, FORMAT_JSON),
Added optional --format=json into api cli. That will return pure JSON data from server
r2381 default=FORMAT_PRETTY
)
created rhodecode-api binary script for working with api via cli...
r2379 args, other = parser.parse_known_args()
return parser, args, other
def main(argv=None):
"""
Main execution function for cli
:param argv:
"""
if argv is None:
argv = sys.argv
conf = None
parser, args, other = argparser(argv)
api_credentials_given = (args.apikey and args.apihost)
api: decouple some parts from api CLI script...
r3833 if args.save_config:
created rhodecode-api binary script for working with api via cli...
r2379 if not api_credentials_given:
api: decouple some parts from api CLI script...
r3833 raise parser.error('--save-config requires --apikey and --apihost')
added --config option into rhodecode-api script for optional path for...
r2571 conf = RcConf(config_location=args.config,
autocreate=True, config={'apikey': args.apikey,
created rhodecode-api binary script for working with api via cli...
r2379 'apihost': args.apihost})
api: decouple some parts from api CLI script...
r3833 sys.exit()
created rhodecode-api binary script for working with api via cli...
r2379
if not conf:
added --config option into rhodecode-api script for optional path for...
r2571 conf = RcConf(config_location=args.config, autoload=True)
created rhodecode-api binary script for working with api via cli...
r2379 if not conf:
if not api_credentials_given:
parser.error('Could not find config file and missing '
'--apikey or --apihost in params')
apikey = args.apikey or conf['apikey']
Added --format into gist CLI...
r3875 apihost = args.apihost or conf['apihost']
created rhodecode-api binary script for working with api via cli...
r2379 method = args.method
Show proper error on argument parse when using api-cli
r2509
Added --format into gist CLI...
r3875 # if we don't have method here it's an error
if not method:
parser.error('Please specify method name')
Show proper error on argument parse when using api-cli
r2509 try:
margs = dict(map(lambda s: s.split(':', 1), other))
Don't catch all exceptions
r3631 except Exception:
Show proper error on argument parse when using api-cli
r2509 sys.stderr.write('Error parsing arguments \n')
sys.exit()
Added --format into gist CLI...
r3875 if args.format == FORMAT_PRETTY:
print 'Calling method %s => %s' % (method, apihost)
created rhodecode-api binary script for working with api via cli...
r2379
API cli should prefer to display errors instead of responses
r3896 json_resp = api_call(apikey, apihost, method, **margs)
Bradley M. Kuhn
Imported some of the GPLv3'd changes from RhodeCode v2.2.5....
r4116 error_prefix = ''
API cli should prefer to display errors instead of responses
r3896 if json_resp['error']:
Bradley M. Kuhn
Imported some of the GPLv3'd changes from RhodeCode v2.2.5....
r4116 error_prefix = 'ERROR:'
API cli should prefer to display errors instead of responses
r3896 json_data = json_resp['error']
else:
json_data = json_resp['result']
Added --format into gist CLI...
r3875 if args.format == FORMAT_JSON:
print json.dumps(json_data)
elif args.format == FORMAT_PRETTY:
Bradley M. Kuhn
Imported some of the GPLv3'd changes from RhodeCode v2.2.5....
r4116 print 'Server response \n%s%s' % (
error_prefix, json.dumps(json_data, indent=4, sort_keys=True)
)
created rhodecode-api binary script for working with api via cli...
r2379 return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))