def __init__(self, endpoint, db_name): endpoint = endpoint if endpoint.find( "mongodb://") else "mongodb://" + endpoint self.connection = pymongo.MongoClient(endpoint) self.db_name = db_name def get_db_connection(self): """Return mongodb connect instance.""" return self.connection def get_database(self): """Return database instance.""" return self.connection[self.db_name] db_host = config.fetch("database", "host") db_name = config.fetch("database", "database") DB = MongoDB(db_host, db_name).get_database() yinggu_col = DB.yinggu def save_yinggu(yinggu_task_info): yinggu_task_info["status"] = "waiting" yinggu_task_info["study_id"] = yinggu_task_info.get("studyInfo").get( "studyInstanceUID") yinggu_col.insert(yinggu_task_info) def find_yinggu(query_info): res = yinggu_col.find_one(query_info) return res
def get_logger(dirname, basename, level="INFO"): """日志""" _logger = logging.getLogger(basename) del _logger.handlers[:] filename = os.path.join(dirname, basename + ".log" if basename[-4:] != ".log" else basename) if not os.path.exists(dirname): os.makedirs(dirname) formats = logging.Formatter("%(asctime)s %(levelname)-8s[%(filename)s:%(lineno)04d] %(message)s") handler = RotatingFileHandler(filename=filename, mode="a", maxBytes=1024 * 1024 * 10, backupCount=5) handler.setFormatter(formats) _logger.addHandler(handler) _level = level.upper() if _level == "DEBUG": _logger.setLevel(logging.DEBUG) elif _level == "INFO": _logger.setLevel(logging.INFO) elif _level == "WARNING": _logger.setLevel(logging.WARNING) elif _level == "ERROR": _logger.setLevel(logging.ERROR) elif _level == "CRITICAL": _logger.setLevel(logging.CRITICAL) else: _logger.setLevel(logging.ERROR) return _logger log_path = config.fetch("logpath") logger = get_logger(log_path, "sigmathird")
def main(): sigmathird_ip = config.fetch("sigmathird", "ip") sigmathird_port = config.fetch("sigmathird", "port") app.run(host=sigmathird_ip, port=sigmathird_port)
# -*- coding=utf-8 -*- from __future__ import absolute_import import os import time import shutil import requests import json import config from logger import logger from db import find_yinggu, update_yinggu from utils import get_file_md5_digest, path_split, get_temp_store_dir, SigmaAuth server_addr = config.fetch("server_addr") yinggu_key = config.fetch("auth", "yinggu", "key") yinggu_secret = config.fetch("auth", "yinggu", "secret") conn = requests.session() def yinggu_download_one(local, remote): if os.path.exists(local): os.remove(local) for i in range(5): try: response = conn.get(remote, timeout=5) with open(local, "wb") as w: w.write(response.content) except Exception as e: if i >= 4:
#!/usr/bin/env python # -*- coding:utf-8 -*- import hashlib import logging import time import hmac import base64 import requests import uuid import os import config SELF_DEFINE_HEADER_PREFIX = "x-sigma-" SELF_DEFINE_AUTH_PREFIX = "12Sigma" work_path = config.fetch("work_path") def get_temp_store_dir(): workspace = work_path uid = uuid.uuid1().hex upload_dir = os.path.join(os.path.join(workspace, "upload"), uid) return upload_dir def get_file_md5_digest(pathname, block=64 * 1024): """Calculate md5 hexdigest of content.""" with open(pathname, "rb") as stream: md5 = hashlib.md5() while True: data = stream.read(block)