Skip to content

Commit

Permalink
define server command #48 and improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Fekete committed Oct 14, 2019
1 parent d2a719e commit e109fb1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
12 changes: 12 additions & 0 deletions abcd/backends/atoms_pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
from ase import Atoms
from ase.io import iread

import abcd.errors
from abcd.model import AbstractModel
from abcd.database import AbstractABCD
from abcd.queryset import AbstractQuerySet
from abcd.parsers import extras

import pymongo.errors
from pymongo import MongoClient
from bson import ObjectId

Expand Down Expand Up @@ -155,6 +157,16 @@ def __init__(self, host='localhost', port=27017,
host=host, port=port, username=username, password=password,
authSource=authSource)

try:
info = self.client.server_info() # Forces a call.
logger.info('DB info: {}'.format(info))

except pymongo.errors.OperationFailure:
raise abcd.errors.AuthenticationError()

except pymongo.errors.ServerSelectionTimeoutError:
raise abcd.errors.TimeoutError()

self.db = self.client[db_name]
self.collection = self.db[collection_name]

Expand Down
4 changes: 4 additions & 0 deletions abcd/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ class AuthenticationError(ABCDError):

class PropertyNotImplementedError(NotImplementedError):
"""Raised if a calculator does not implement the requested property."""


class TimeoutError(ABCDError):
pass
35 changes: 16 additions & 19 deletions abcd/frontends/commandline/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,19 @@
def login(*, config, name, url, **kwargs):
logger.info('login args: \nconfig:{}, name:{}, url:{}, kwargs:{}'.format(config, name, url, kwargs))
from abcd import ABCD
from abcd.errors import URLError, AuthenticationError

try:
db = ABCD.from_url(url=url)
info = db.info()
db = ABCD.from_url(url=url)
info = db.info()

config['url'] = url
config.save()
config['url'] = url
config.save()

print('Successfully connected to the database!')
print(" type: {type}\n"
" hostname: {host}\n"
" port: {port}\n"
" database: {db}\n"
" # of confs: {number of confs}".format(**info))

except URLError:
print('Wrong connection: Please check the parameters of th url!')
exit(1)
except AuthenticationError:
print('Authentication failed!')
exit(1)
print('Successfully connected to the database!')
print(" type: {type}\n"
" hostname: {host}\n"
" port: {port}\n"
" database: {db}\n"
" # of confs: {number of confs}".format(**info))


@init_config
Expand Down Expand Up @@ -254,6 +245,12 @@ def execute(*, db, query, yes, python_code, **kwargs):
db.exec(python_code, query)


@check_readonly
def server(*, abcd_url, url, api_only, **kwargs):
logger.info("SERVER - abcd: {}, url: {}, api_only:{}".format(abcd_url, url, api_only))
exit(0)


class Formater(object):
partialBlocks = ["▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"] # char=pb

Expand Down
25 changes: 21 additions & 4 deletions abcd/frontends/commandline/parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from argparse import ArgumentParser, REMAINDER
from argparse import ArgumentParser
from abcd.frontends.commandline import commands
from abcd.errors import URLError, AuthenticationError, TimeoutError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -85,6 +86,12 @@
exec_parser.add_argument('-y', '--yes', action='store_true', help='Do the actual execution.')
exec_parser.add_argument('python_code', help='Selecting properties for detailed description')

server = subparsers.add_parser('server', help='Running custom python code')
server.set_defaults(callback_func=commands.server)
server.add_argument('abcd_url', help='Url for abcd database.')
server.add_argument('--api-only', action='store_true', help='Running only the API.')
server.add_argument('-u', '--url', help='Url to run the server.', default='http://localhost:27017')


def main(args=None):
kwargs = parser.parse_args(args).__dict__
Expand All @@ -102,9 +109,19 @@ def main(args=None):
print(parser.format_help())
return

# return namespace.callback_func(namespace)
callback_func = kwargs.pop('callback_func')
return callback_func(**kwargs)
try:
callback_func = kwargs.pop('callback_func')
callback_func(**kwargs)

except URLError:
print('Wrong connection: Please check the parameters of the url!')
exit(1)
except AuthenticationError:
print('Authentication failed: Please check the parameters of the connection!')
exit(1)
except TimeoutError:
print("Timeout: Please check the parameters of the connection!")
exit(1)


if __name__ == '__main__':
Expand Down

0 comments on commit e109fb1

Please sign in to comment.