Skip to content
This repository was archived by the owner on Jun 30, 2018. It is now read-only.

Commit 0280cf9

Browse files
fguillotMaxime Belanger
authored andcommitted
Use standard Python command line parser (#6)
1 parent b013e96 commit 0280cf9

File tree

8 files changed

+88
-79
lines changed

8 files changed

+88
-79
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,35 @@ Requirements
2323
- MongoDB
2424
- Python 2.7
2525

26+
Command line usage
27+
------------------
28+
29+
Usage:
30+
31+
```bash
32+
usage: almanach [-h] [--logging LOGGING] {api,collector} config_file
33+
```
34+
35+
Start the API daemon:
36+
37+
```bash
38+
almanach api /path/to/almanach.cfg
39+
```
40+
41+
Start the collector:
42+
43+
```bash
44+
almanach collector /path/to/almanach.cfg
45+
```
46+
47+
Custom logging configuration:
48+
49+
```bash
50+
almanach collector /path/to/almanach.cfg --logging /path/to/logging.cfg
51+
```
52+
53+
The syntax of the logging configuration file is available in the official [Python documentation](https://docs.python.org/2/library/logging.config.html).
54+
2655
Database entities
2756
-----------------
2857

almanach/api.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import logging
16-
1715
from flask import Flask
1816
from gunicorn.app.base import Application
1917

20-
from almanach import config
2118
from almanach.adapters import api_route_v1 as api_route
22-
from almanach import log_bootstrap
2319
from almanach.adapters.database_adapter import DatabaseAdapter
2420
from almanach.core.controller import Controller
2521

@@ -30,24 +26,12 @@ def __init__(self):
3026
super(AlmanachApi, self).__init__()
3127

3228
def init(self, parser, opts, args):
33-
log_bootstrap.configure()
34-
config.read(args)
3529
self._controller = Controller(DatabaseAdapter())
3630

3731
def load(self):
38-
logging.info("starting flask worker")
3932
api_route.controller = self._controller
4033

4134
app = Flask("almanach")
4235
app.register_blueprint(api_route.api)
4336

4437
return app
45-
46-
47-
def run():
48-
almanach_api = AlmanachApi()
49-
almanach_api.run()
50-
51-
52-
if __name__ == "__main__":
53-
run()

almanach/cli.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2016 Internap.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import argparse
16+
import logging
17+
18+
from almanach import config
19+
from almanach.api import AlmanachApi
20+
from almanach.collector import AlmanachCollector
21+
22+
23+
def run():
24+
parser = argparse.ArgumentParser()
25+
parser.add_argument("service", help="Service to execute: 'api' or 'collector'", choices=["api", "collector"])
26+
parser.add_argument("config_file", help="Config file path")
27+
parser.add_argument("--logging", help="Logger configuration")
28+
args = parser.parse_args()
29+
30+
config.read(args.config_file)
31+
32+
if args.service == "api":
33+
almanach_api = AlmanachApi()
34+
almanach_api.run()
35+
else:
36+
almanach_collector = AlmanachCollector()
37+
almanach_collector.run()
38+
39+
if args.logging:
40+
logging.config.fileConfig(args.logging, disable_existing_loggers=False)
41+
42+
43+
if __name__ == "__main__":
44+
run()

almanach/collector.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
# limitations under the License.
1414

1515
import logging
16-
import sys
1716

1817
from kombu import Connection
1918

20-
from almanach import log_bootstrap
2119
from almanach import config
2220
from almanach.adapters.bus_adapter import BusAdapter
2321
from almanach.adapters.database_adapter import DatabaseAdapter
@@ -28,24 +26,11 @@
2826
class AlmanachCollector(object):
2927

3028
def __init__(self):
31-
log_bootstrap.configure()
32-
config.read(sys.argv)
3329
self._controller = Controller(DatabaseAdapter())
3430
_connection = Connection(config.rabbitmq_url(), heartbeat=540)
3531
retry_adapter = RetryAdapter(_connection)
36-
3732
self._busAdapter = BusAdapter(self._controller, _connection, retry_adapter)
3833

3934
def run(self):
40-
logging.info("starting bus adapter")
35+
logging.info("Listening for incoming events")
4136
self._busAdapter.run()
42-
logging.info("shutting down")
43-
44-
45-
def run():
46-
almanach_collector = AlmanachCollector()
47-
almanach_collector.run()
48-
49-
50-
if __name__ == "__main__":
51-
run()

almanach/config.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,18 @@
1313
# limitations under the License.
1414

1515
import ConfigParser
16-
import pkg_resources
17-
import os.path as Path
16+
import os.path as os_path
1817

1918
from almanach.common.almanach_exception import AlmanachException
2019

2120
configuration = ConfigParser.RawConfigParser()
2221

2322

24-
def read(args=[], config_file="resources/config/almanach.cfg"):
25-
filename = pkg_resources.resource_filename("almanach", config_file)
23+
def read(filename):
24+
if not os_path.isfile(filename):
25+
raise AlmanachException("Config file '{0}' not found".format(filename))
2626

27-
for param in args:
28-
if param.startswith("config_file="):
29-
filename = param.split("=")[-1]
30-
break
31-
32-
if not Path.isfile(filename):
33-
raise AlmanachException("config file '{0}' not found".format(filename))
34-
35-
print "loading configuration file {0}".format(filename)
27+
print("Loading configuration file {0}".format(filename))
3628
configuration.read(filename)
3729

3830

@@ -112,7 +104,7 @@ def rabbitmq_time_to_live():
112104

113105

114106
def _read_file(filename):
115-
file = open(filename, "r")
116-
content = file.read()
117-
file.close()
107+
f = open(filename, "r")
108+
content = f.read()
109+
f.close()
118110
return content

almanach/log_bootstrap.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

setup.cfg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ packages =
2424

2525
[entry_points]
2626
console_scripts =
27-
almanach_collector = almanach.collector:run
28-
almanach_api = almanach.api:run
27+
almanach = almanach.cli:run
2928

3029
[nosetests]
3130
no-path-adjustment = 1

tests/adapters/test_database_adapter.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import pkg_resources
1516
import unittest
16-
from datetime import datetime
17-
1817
import mongomock
18+
19+
from datetime import datetime
1920
from flexmock import flexmock, flexmock_teardown
2021
from hamcrest import assert_that, contains_inanyorder
2122
from pymongo import MongoClient
23+
2224
from almanach.adapters.database_adapter import DatabaseAdapter
2325
from almanach.common.volume_type_not_found_exception import VolumeTypeNotFoundException
2426
from almanach.common.almanach_exception import AlmanachException
@@ -29,7 +31,7 @@
2931

3032
class DatabaseAdapterTest(unittest.TestCase):
3133
def setUp(self):
32-
config.read(config_file="resources/config/test.cfg")
34+
config.read(pkg_resources.resource_filename("almanach", "resources/config/test.cfg"))
3335
mongo_connection = mongomock.Connection()
3436

3537
self.adapter = DatabaseAdapter()

0 commit comments

Comments
 (0)