Esempio n. 1
0
    UpdateDownloads,
    UpdateDownloadsHandler,
)
from pepy.application.helper import AdminPasswordChecker
from pepy.application.query import BadgeProvider, ProjectProvider, DownloadsNumberFormatter
from pepy.domain.model import HashedPassword
from pepy.infrastructure.bq_downloads_extractor import BQDownloadsExtractor
from pepy.infrastructure.db_repository import DBProjectRepository
from pepy.infrastructure.db_view import DBProjectView
from ._config import DATABASE, BQ_CREDENTIALS_FILE, ADMIN_PASSWORD, LOGGING_FILE, DATABASE_ORATOR, LOGGING_DIR

db_connection = psycopg2.connect(**DATABASE)
db_orator = DatabaseManager(DATABASE_ORATOR)
project_repository = DBProjectRepository(db_connection)
db_project_view = DBProjectView(db_orator)
command_bus = CommandBus()
command_bus.subscribe(ImportDownloadsFile,
                      ImportDownloadsFileHandler(project_repository))
downloads_formatter = DownloadsNumberFormatter()
badge_query = BadgeProvider(db_project_view, downloads_formatter)
project_provider = ProjectProvider(db_project_view)

# Directories configuration
if not os.path.exists(LOGGING_DIR):
    os.makedirs(LOGGING_DIR)

# Logger configuration
logger = logging.getLogger("pepy")
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
    "[%(asctime)s] [%(levelname)s] [%(pathname)s:%(funcName)s:%(lineno)d]: %(message)s"
Esempio n. 2
0
file_handler = logging.FileHandler(LOGGING_FILE)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
mongo_client = MongoClient(MONGODB)

if environment == Environment.test:
    project_repository = MongoProjectRepository(mongo_client.pepy_test)
else:
    project_repository = MongoProjectRepository(mongo_client.pepy)

bq_client = None
if environment == Environment.prod:
    bq_client = bigquery.Client.from_service_account_json(BQ_CREDENTIALS_FILE)

if environment == Environment.test:
    stats_viewer = MockStatsViewer()
else:
    stats_viewer = BQStatsViewer(bq_client)

admin_password_checker = AdminPasswordChecker(HashedPassword(ADMIN_PASSWORD))
command_bus = CommandBus()
command_bus.subscribe(
    UpdateVersionDownloads,
    UpdateVersionDownloadsHandler(project_repository, stats_viewer,
                                  admin_password_checker, logger),
)
command_bus.subscribe(ImportTotalDownloads,
                      ImportTotalDownloadsHandler(project_repository, logger))
downloads_formatter = DownloadsNumberFormatter()
badge_service = BadgeService(project_repository, downloads_formatter)
Esempio n. 3
0
def bus():
    return CommandBus()
Esempio n. 4
0
def main():
    """
    Main. Init database, create command bus, 
    receive commands and pass to handlers
    """

    # Init commandbus
    bus = CommandBus()

    # Init handlers for commands
    root_handler = RootHandler()
    new_handler = NewHandler()
    parent_handler = ParentHandler()
    child_handler = ChildHandler()
    read_handler = ReadHandler()
    update_handler = UpdateHandler()
    ancestor_handler = AncestorHandler()
    ancestors_handler = AncestorsHandler()
    descendants_handler = DescendantsHandler()
    remove_handler = RemoveHandler()
    open_handler = OpenHandler()

    # Route commands to coresponding handlers
    bus.subscribe(RootCommand, root_handler)
    bus.subscribe(NewCommand, new_handler)
    bus.subscribe(ParentCommand, parent_handler)
    bus.subscribe(ChildCommand, child_handler)
    bus.subscribe(ReadCommand, read_handler)
    bus.subscribe(UpdateCommand, update_handler)
    bus.subscribe(AncestorCommand, ancestor_handler)
    bus.subscribe(AncestorsCommand, ancestors_handler)
    bus.subscribe(DescendantsCommand, descendants_handler)
    bus.subscribe(RemoveCommand, remove_handler)
    bus.subscribe(OpenCommand, open_handler)

    # Get transaction mode
    mode = 'normal'
    try:
        mode = sys.argv[1][2:]
    except IndexError:
        pass


    # Read json-object-like commands from the input file
    commands = read_data(input())
    open_command = commands.pop(0)
	
    # Check connection with the specified database
    bus.publish(OpenCommand(open_command))

    # init database
    init_db(open_command, 'init', 'init.sql')

    # invoke each command
    for command in commands:
        with psycopg2.connect(host='localhost', 
                        dbname=open_command['open']['database'],
                        user=open_command['open']['user'],
                        password=open_command['open']['password']) \
                    as db_conn:

            try:
                command_name = globals()[
                        list(command.keys())[0].title() + 'Command']

                command_instance = command_name(db_conn, 
                                    list(command.values())[0], mode)

                # Handle command
                bus.publish(command_instance)

            except KeyError:
                # Command key should be valid
                print(json.JSONEncoder().encode({'status': 'ERROR'}))