Ejemplo n.º 1
0
 def compute_net_percent_change(self):
     logger.debug("Computing the net percent change")
     net = 0
     for percent_change in self.percent_changes:
         net += percent_change.compute_percent_change()
     logger.debug("Net Δ%: {:.5f}".format(net))
     return net
Ejemplo n.º 2
0
 def __init__(self, fxn=None):
     Thread.__init__(self)
     logger.debug("Initialize %s" % self)
     self.stop_event = Event()
     StateThread.threads.append(self)
     self.daemon = True
     self.fxn = fxn
Ejemplo n.º 3
0
 def __init__(self, fxn=None):
     Thread.__init__(self)
     logger.debug("Initialize %s" % self)
     self.stop_event = Event()
     StateThread.threads.append(self)
     self.daemon=True
     self.fxn = fxn
Ejemplo n.º 4
0
def __validate_role_constraints(user):
    for role_constraint in user.role_constraints:
        result = __validate_constraint(role_constraint)
        if result is not SUCCESS:
            logger.debug('validate_role_constraints deactivate user-role:' +
                         user.uid + '.' + role_constraint.name)
            __deactivate_role(user, role_constraint)
Ejemplo n.º 5
0
def login_user(userid, password):
    logger.info("User Login")
    user_data = User.query.filter_by(userid=userid).first()

    if user_data is not None:
        if not user_data.check_password(password):
            logger.error("Authentication error: Wrong userid or password")
            return {
                'message': 'Authentication error: Wrong userid or password',
                "authenticated": False
            }, 401
        # Set JWT_ACCESS_TOKEN_EXPIRES to change token duration.
        access_token = create_access_token(identity=user_data)
        refresh_token = create_refresh_token(identity=user_data)
        logger.info("Access token created")
        logger.debug(f'access_token: {access_token}')
        resp = {
            'login': True,
            'msg': 'New login',
            'access_token': access_token,
            #'refresh_token': refresh_token
        }
        _set_cookies(access_token, refresh_token)
        return resp, 200
    else:
        logger.error("User Does Not Exist")
        return {'message': 'User Does Not Exist', "authenticated": False}, 401
Ejemplo n.º 6
0
def getFilelist(mode=None,
                data=str(),
                parseRoot=str(),
                rootAttr=dict(),
                element=str(),
                attr=dict(),
                encode='utf8'):
    # type: (Union(str, None), str, str, dict, str, dict, Union(str, None)) -> List[(str, List)]
    # todo: 함수명을 변경한다
    # todo: result로 [[title:str(), element:list()],...]의 list를 반환하도록 한다.(title을 추가 반환->folder 이름으로 삼을 수 있도록)

    logger.debug("-* length of data: {}".format(len(data)))
    if encode is not None:
        data = data.encode(encode)

    page = page_parser.parse(data)
    root1 = parseRoot
    rootAttr1 = rootAttr
    element1 = element
    attr1 = attr

    if mode is None:
        if page.page.rss is not None:
            mode = "rss"
        else:
            mode = "html"

    if mode == "rss":
        root1 = ''
        rootAttr1 = dict()
        element1 = "item"
        attr1 = dict()

    logger.debug(root1, rootAttr1, element1, attr1)
    results = page.findElementsExt(root1, rootAttr1, element1, attr1)
    logger.debug("-* length of parse: {}".format(len(results)))
    # print results

    if mode == "rss":
        # hp = HTMLParser()
        targets = results
        results = list()
        for iter in targets:
            logger.debug("-* string of description : {}".format(
                iter.description.string))
            # data = hp.unescape(iter.description.string)
            data = unescape(iter.description.string)
            result = getFilelist("item",
                                 data,
                                 parseRoot,
                                 rootAttr,
                                 element,
                                 attr,
                                 encode=None)
            results.extend(result)
        pass
    logger.debug("-* type of result : ({})[0]{}".format(
        len(results), type(results[0])))

    return results
Ejemplo n.º 7
0
 def _check_data(self, data_df):
     if data_df.empty:
         err = ValueError("get_prices return empty data")
         logger.error(err)
         raise err
     else:
         logger.debug("get number of data shape: " + str(data_df.shape))
Ejemplo n.º 8
0
def create_device(**kwargs):
    logger.info('Register new device')
    now = datetime.utcnow()
    try:
        query = Device.query.filter_by(serial=kwargs.get('serial')).first()
        if query is not None:
            logger.error("Device already exists")
            return {'message': 'Device already exists'}, 409

        company = db.session.query(Company).filter_by(
            name=kwargs.get('company')).one()
        owner = db.session.query(User).filter_by(
            userid=kwargs.get('owner')).one()
        device = Device(model=kwargs.get('model'),
                        serial=kwargs.get('serial'),
                        company=company,
                        owner=owner,
                        ip=kwargs.get('ip'),
                        created=now,
                        created_by=kwargs.get('created_by', current_user),
                        last_edited=now,
                        edited_by=kwargs.get('edited_by', current_user),
                        is_deleted=False)
        db.session.add(device)
        db.session.commit()
        return {
            'message': f'Posted device<{kwargs.get("serial")}> to db.'
        }, 201
    except Exception as e:
        logger.error(e)
        logger.debug(traceback.format_exc())
        raise e
Ejemplo n.º 9
0
    def start(self):
        while True:
            self.output.write(self.prompt())
            self.output.flush()

            try:
                line = self.input.readline()
                if not line:  # got EOF
                    raise ShellExit

                line = line.strip()
                if not line:  # empty line
                    continue

                # Get command name.
                cmd_args = line.split(' ')
                cmd_name = cmd_args.pop(0)

                cmd_name_upper = cmd_name.upper()
                cmd = self.commands.get(cmd_name_upper)

                if not cmd:
                    self.output.write('unknown command: {}\n'.format(cmd_name))
                    continue

                self.commands[cmd_name_upper].run(*cmd_args)(self)

            except (ShellExit, EOFError):
                break
            except KeyboardInterrupt:
                pass
            except Exception as e:
                logger.debug(traceback.format_exc())
                self.output.write('Exception: {}\n'.format(repr(e)))
Ejemplo n.º 10
0
def get_twitter_data(client, node: str, field: dict):
    for k, v in dict(field).items():
        if not v:
            field.pop(k)

    encoded_field = parse.urlencode(field)
    url = base + node + '?' + encoded_field

    logger.info(f'request to "{url}"')
    logger.debug(
        f'request info : <base : "{base}"> <node: {node}> <encoded_field: {field}>'
    )

    response, data = client.request(url)  # type : dict, bytes
    logger.debug(f'response status : {response["status"]}')

    try:
        if response['status'] == '200':
            ret = json.loads(data.decode('utf-8'))
            # print(ret)
            return ret
    except Exception as e:
        print("ERR:", e)
        logger.error('cannot responsed from : "%s" ' %
                     (response['status'], url))
        logger.error('error message :', e)
        return None
Ejemplo n.º 11
0
 def compute_percent_change(self):
     if self.is_complete():
         percent_change = ((self.v2 - self.v1) * 100) / math.fabs(self.v1)
         logger.debug("v1: {:.5f}, v2: {:.5f}, Δ%: {:.5f}".format(
             self.v1, self.v2, percent_change))
         return percent_change
     return 0
Ejemplo n.º 12
0
def update_device(**kwargs):
    logger.info('Update existing device')
    now = datetime.utcnow()
    try:
        query = db.session.query(Device).filter_by(
            serial=kwargs.get('serial')).one()
        if kwargs.get('newserial'):
            query.serial = kwargs.get('newserial')
        if kwargs.get('model'):
            query.model = kwargs.get('model')
        if kwargs.get('company'):
            query.company = db.session.query(Company).filter_by(
                name=kwargs.get('company')).one()
        if kwargs.get('ip'):
            query.ip = kwargs.get('ip')
        if kwargs.get('owner'):
            query.owner = db.session.query(User).filter_by(
                userid=kwargs.get('owner')).one()
        query.edited = now
        query.edited_by = current_user
        db.session.commit()
        return {'message': f'Updated device<{query.serial}> from db.'}, 200
    except Exception as e:
        logger.error(e)
        logger.debug(traceback.format_exc())
        raise e
Ejemplo n.º 13
0
    def _add(item, fieldname: str, field: str, parse=True, usingCDATA=True):
        logger.debug("-*|{fieldname} : ({len}){field}".format_map({
            'fieldname':
            fieldname,
            'len':
            len(field),
            'field':
            field
        }))
        if field and (len(field) > 0):
            logger.debug(
                "-*|{fieldname} not Null(None|zero length)".format_map(
                    {'fieldname': fieldname}))
            if parse:
                value = page.select(field)[0].get_text()
            else:
                value = field

            # print(fieldname,len(value),sep=':')
            if usingCDATA:
                setattr(item, fieldname, make_CDATA(value))
            else:
                setattr(item, fieldname, value)

            return True
        return False
Ejemplo n.º 14
0
def update_feed(feedid, rset, itemlist: list):
    # cleanup list of new feeds
    _itemlist = itemlist.copy()  # list of new item's links
    olditems = Item.query.filter(
        Item.feedid == feedid).all()  # list of old items in feed
    for iter in olditems:
        if str(iter.link) in _itemlist: _itemlist.remove(iter.link)

    # 새 feed가 없고, 이전에도 feed가 없었던 경우
    if (len(_itemlist) == 0) and (len(itemlist) == len(olditems)):
        return False

    # delete all old feed items
    # todo: feed별 삭제여부를 지정할 수 있도록 할 것
    logger.debug("remove all old items in feed: {}".format(feedid))
    for olditem in olditems:
        db_session.delete(olditem)

    count = 1
    for iter in itemlist:
        logger.info("processing...: {count} / {length}".format_map({
            'count':
            count,
            'length':
            len(itemlist)
        }))
        count = count + 1

        feeditem = update_feeditem(feedid, iter, rset)
        if feeditem:
            db_session.add(feeditem)
    db_session.commit()
    return True
Ejemplo n.º 15
0
    def __new__(cls, res, s = str()):
        logger.debug("* responsedString.__new__(res: {typeofres} - {idofres}),"
                     " s: {typeofs} - {idofs})".format_map({'typeofres': type(res),
                                                            'idofres':   id(res),
                                                            'typeofs':   type(s),
                                                            'idofs':     id(s)}))
        info = None

        if isinstance(res, addinfourl):
            info = res.info()
            s = res.read()

        elif isinstance(res, client.HTTPMessage):
            info = res
            s = res.__str__
            #    #client.HttpMessage와 상동인지는 알 수 없음
        elif isinstance(res, client.HTTPResponse):
            info = res
            s = res.read()
        else:
            raise TypeError

        # 지역 변수 s가 obj의 내용으로 할당됨
        ret = super(responsedString, cls).__new__(cls, s)
        logger.debug("*- return responsedString.__new__() : {} - s.__str__() == ret.__str__() : {},".format(
                type(ret), s.__str__() == ret.__str__()))

        ret.info = info
        return ret
Ejemplo n.º 16
0
 def progress(cur, all_num):
     bin_num = int(all_num / 10)
     if bin_num == 0:
         bin_num = 1
     if cur % bin_num == 0 or cur >= all_num:
         progress = 100 * (cur / all_num)
         logger.debug("  progress: {:.2f}%".format(progress))
Ejemplo n.º 17
0
 def __stream_from_inputs(self, inputs):
     storage = AmpStorageWrapper(inputs.metadata)
     stream = storage.find_stream()
     logger.debug('Found Stream: {}'.format(stream.get('name')))
     # connection_data = stream['amqp_credentials']
     # Change this in development if we have no correct data from API
     # connection_data.update({'host': self.RMQ_HOST, 'port': self.RMQ_PORT})
     return stream
Ejemplo n.º 18
0
 def run(self):
     logger.debug("Running %s" % self)
     while (not self.stop_event.isSet()):
         StateThread.pause_event.wait()
         if callable(self.fxn):
             self.fxn()
     self.stop_event.clear()
     logger.info("Exiting %s" % self)
Ejemplo n.º 19
0
 def run(self):
     logger.debug("Running %s" % self)
     while(not self.stop_event.isSet()):
         StateThread.pause_event.wait()
         if callable(self.fxn):
             self.fxn()
     self.stop_event.clear()
     logger.info("Exiting %s" % self)
Ejemplo n.º 20
0
    def nextValidId(self, orderId: int):
        super().nextValidId(orderId)

        logger.debug("setting nextValidOrderId: %d", orderId)
        self.nextValidOrderId = orderId
        logger.debug("NextValidId: %d", orderId)

        # we can start now
        self.start()
Ejemplo n.º 21
0
def get_pageitems(rule, maxpage=1, encoding='utf-8'):
    list_url = rule.listUrl
    item_list = list()
    page_count = 0

    # end loop : 1 < 1 -> False
    while page_count < maxpage:
        logger.debug("*- loop {}/{} - encoding: ({}) {}".format(
            page_count + 1, maxpage, len(rule.encoding), rule.encoding))

        # _page = openPage(listurl).decode(encoding, 'ignore')
        _page = openPage(list_url)  # bytes
        if encoding != 'utf-8':
            _t1 = _page.rfind(b"<meta", 0, _page.find(b"charset"))
            _t2 = _page.find(b'>', _t1) + 1
            page = _page[:_t1] + _page[_t2:]
        else:
            page = _page
        # item_list 가 초기화되지 않음
        # logger.info("*- list(encoding:{encoding}, listpage: {list_len}(|{listpage}|), item:{item_len})"
        #             .format_map({'encoding': encoding, 'list_len': len(_page), 'listpage': _page[:13].decode(encoding),
        #                          'item_len': len(item_list)}))
        logger.info(
            "*- list(encoding:{encoding}, listpage: {list_len}(|{listpage}|))".
            format_map({
                'encoding': encoding,
                'list_len': len(_page),
                'listpage': _page[:13].decode(encoding),
            }))

        listpage = page_parser.parse(page)
        items = listpage.select(str(rule.itemlink))
        item_list.extend([iter for iter in items if iter not in item_list])

        logger.info(
            "*- item {item_len}, (listpage loaded: {listpage}, get_itemlink: {getlink})"
            .format_map({
                'count': page_count + 1,
                'listpage': len(str(listpage.page)) > 0,
                'getlink': len(items) > 0,
                'item_len': len(item_list)
            }))

        nextpages = listpage.select(str(rule.nextpagelink))
        logger.debug("*- nextpage:{} - {}({})".format(str(rule.nextpagelink),
                                                      (len(nextpages) > 0),
                                                      nextpages))
        if len(nextpages):  # relative url -> absolute url
            _listurl = get_absolute_anchor_reference(str(rule.listUrl),
                                                     nextpages[0])
            if list_url == _listurl: break
            list_url = _listurl
        else:
            break

        page_count = page_count + 1
    return item_list, listpage
Ejemplo n.º 22
0
def __validate_constraint(constraint):
    result = SUCCESS
    for validator in validators:
        result = validator.validate(constraint, CurrentDateTime())
        if result is not SUCCESS:
            logger.debug(validator.__class__.__name__ + ' validation failed:' +
                         constraint.name)
            break
    return result
Ejemplo n.º 23
0
def delete_device(**kwargs):
    logger.info('Delete existing device')
    try:
        query = db.session.query(Device).filter_by(**kwargs).one()
        db.session.delete(query)
        db.session.commit()
        return {'message': f'Deleted device<{query.serial}> from db.'}, 200
    except Exception as e:
        logger.error(e)
        logger.debug(traceback.format_exc())
        raise e
Ejemplo n.º 24
0
def read_device(**kwargs):
    logger.info('Get device list')
    logger.info(f'Filter: {kwargs}')
    try:
        condition = {k: v for k, v in kwargs.items() if v is not None}
        query = db.session.query(Device).filter_by(**condition).all()
        return query
    except Exception as e:
        logger.error(e)
        logger.debug(traceback.format_exc())
        raise e
Ejemplo n.º 25
0
 def exit(self):
     if isinstance(self._update, StateUpdateThread):
         self._update.stop()
     if callable(self.exitfxn):
         self.exitfxn()
     
     if callable(self.update):
         logger.debug("FXN type update, deactivate")
         
     logger.debug("Exit %s" % self)
     self.active=False
     self._entry_has_run = False
Ejemplo n.º 26
0
 def __init__(self, description, updatefxn=None, enterfxn=None, exitfxn=None, data=None):
     self.enterfxn = enterfxn
     self.exitfxn = exitfxn
     self.updatefxn = updatefxn
     self.description = description
     logger.debug("Initialize %s" % self)
     self.data = data
     self.active = False
     State.states.append(self)
     self.substates = []
     self._needs_to_run = False
     self._entry_has_run = False
Ejemplo n.º 27
0
 def listWidget_casemodel_handle(self):
     try:
         self.listWidget_caseModel.clear()
         condition_List = list()
         module = self.comboBox_module.currentText()
         if module is not "":
             condition_List.clear()
             condition_List.append("module='" + module + "'")
             module_id = DBManager().query("modules", "module_id", condition_List)[0]
             condition_List.clear()
             condition_List.append("module_id='" + str(module_id) + "'")
             logger.debug("=====================================================================")
             logger.debug(condition_List)
             toplevel_element_list = DBManager().query("casemodel", "toplevel_element", condition_List)
             logger.debug(toplevel_element_list)
             sublevel_element_list = DBManager().query("casemodel", "sublevel_element", condition_List)
             thirdlevel_element_list = DBManager().query("casemodel", "thirdlevel_element", condition_List)
             casemodel_list = list()
             for j in range(len(toplevel_element_list)):
                 casemodel_list.append(
                     toplevel_element_list[j] + "-" + sublevel_element_list[j] + ":" + thirdlevel_element_list[j])
             logger.debug(casemodel_list)
             for k in casemodel_list:
                 self.listWidget_caseModel.addItem(k)
     except:
         logger.exception("发现错误:")
Ejemplo n.º 28
0
 def delete(self, table, condition_list=None):
     self.delete = QtSql.QSqlTableModel()
     self.delete.setTable(table)
     if condition_list is not None:
         condition = " and ".join(condition_list)
         self.delete.setFilter(condition)
     self.delete.select()
     count = self.delete.rowCount()
     logger.debug("删除行数:" + str(count))
     # while (counts > 0):
     #     self.delete.removeRows(0, counts)
     #     counts = counts - 1
     # logger.debug("counts=" + str(counts))
     while (count >= 0):
         self.delete.removeRow(count)
         count = count - 1
     self.delete.submitAll()
Ejemplo n.º 29
0
def main():
    # parse arguments
    parser = argparse.ArgumentParser(prog='mog_cli', description='Command line interface for CSA shogi client')
    parser.add_argument('-H', '--host', metavar='DEFAULT_HOST', default='localhost',
                        help='default host of shogi-server')
    parser.add_argument('-P', '--port', metavar='DEFAULT_PORT', default=4081, type=int,
                        help='default port of shogi-server')
    parser.add_argument('-u', dest='username', metavar='DEFAULT_USERNAME', help='default login username')
    parser.add_argument('-p', dest='password', metavar='DEFAULT_PASSWORD', help='default login password')
    parser.add_argument('--debug', dest='log_level', action='store_const', const=logging.DEBUG, default=logging.INFO,
                        help='set log level to DEBUG')
    args = parser.parse_args()

    logger.setLevel(args.log_level)
    logger.debug('Starting shell with args: {}'.format(args))

    sh = Shell(args.host, args.port, args.username, args.password)
    sh.start()
Ejemplo n.º 30
0
 def add_thirdlevel(self):
     try:
         data_dict = dict()
         condition_list = list()
         module = self.comboBox_module.currentText()
         condition_list.clear()
         condition_list.append("module='" + module + "'")
         module_id = DBManager().query("modules", "module_id", condition_list)[0]
         thirdlevel_element = self.textEdit_thirdlevel.toPlainText()
         # 由于casemodel界面展示时包含“:”和“-”,所以在处理包含这两个符号的选项时会出错,这个弹提示不允许这个的数据输入
         if ":" in thirdlevel_element or ":" in thirdlevel_element or "-" in thirdlevel_element:
             msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "错误输入提示", "不允许输入冒号或横线!")
             msg_box.exec_()
         else:
             items_sublevel = self.listWidget_sublevel.selectedItems()
             for i_sublevel in items_sublevel:
                 sublevel_element = i_sublevel.text()
                 condition_list.clear()
                 condition_list.append("sublevel_element='" + i_sublevel.text() + "'")
                 sublevel_id = DBManager().query("sublevel", "sublevel_id", condition_list)[0]
             condition_list.clear()
             condition_list.append("module_id='" + module_id + "'")
             module_id_thirdlevel = DBManager().query("thirdlevel", "thirdlevel_id", condition_list)
             module_id_thirdlevel_toint = list()
             for i in module_id_thirdlevel:
                 module_id_thirdlevel_toint.append(int(i))
             if len(module_id_thirdlevel) == 0:
                 thirdlevel_id = str(1)
             else:
                 thirdlevel_id = str(max(module_id_thirdlevel_toint) + 1)
             logger.debug(thirdlevel_id)
             condition_list.clear()
             if thirdlevel_element is not "":
                 data_dict["thirdlevel_element"] = thirdlevel_element
             data_dict["sublevel_id"] = sublevel_id
             data_dict["module_id"] = module_id
             data_dict["thirdlevel_id"] = thirdlevel_id
             DBManager().insert_data("thirdlevel", data_dict)
             self.textEdit_thirdlevel.clear()
             self.listWidget_thirdlevel_handle()
     except:
         logger.exception("发现错误:")
Ejemplo n.º 31
0
 def delete_selection(self):
     try:
         condition_list = list()
         module = self.comboBox_module.currentText()
         condition_list.clear()
         condition_list.append("module='" + module + "'")
         module_id = DBManager().query("modules", "module_id", condition_list)[0]
         condition_list.clear()
         try:
             delete_data = self.listWidget_caseModel.currentItem().text()
         except:
             delete_data = ""
         if delete_data is not "":
             thirdlevel_data = "thirdlevel_element='" + delete_data.split(":")[1] + "'"
             condition_list.append(thirdlevel_data)
             condition_list.append("module_id='" + str(module_id) + "'")
             logger.debug(condition_list)
             DBManager().delete("casemodel", condition_list)
             self.listWidget_casemodel_handle()
     except:
         logger.exception("发现错误:")
Ejemplo n.º 32
0
 def delete_thirdlevel(self):
     try:
         condition_list = list()
         module = self.comboBox_module.currentText()
         condition_list.clear()
         condition_list.append("module='" + module + "'")
         module_id = DBManager().query("modules", "module_id", condition_list)[0]
         condition_list.clear()
         try:
             # 获取当前选中的对象list,currentItems是当前点过的选项而不是选中的
             delete_data_object = self.listWidget_thirdlevel.selectedItems()
             delete_data_list = list()
             for i in delete_data_object:
                 delete_data_list.append(i.text())
         except:
             delete_data_object = None
         if delete_data_object is not None:
             for i in delete_data_list:
                 logger.debug(i)
                 thirdlevel_data = "thirdlevel_element='" + i + "'"
                 logger.debug(thirdlevel_data)
                 condition_list.clear()
                 condition_list.append(thirdlevel_data)
                 condition_list.append("module_id='" + str(module_id) + "'")
                 logger.debug(condition_list)
                 DBManager().delete("thirdlevel", condition_list)
                 self.listWidget_thirdlevel_handle()
     except:
         logger.exception("发现错误:")
Ejemplo n.º 33
0
 def update(self, no_enter=False):
     
     if no_enter and not self.active:
         logger.debug("%s Not active, nothing to update" % self)
         return
     
     if isinstance(self._update, StateUpdateThread):
         if not self._update.isAlive():
             self._update.start()
             logger.debug("Start Updating %s" % self)
         else:
             logger.debug("%s Already Active" % self)
     elif callable(self._update):
         self._update()
     else:
         logger.debug("%s Nothing to update" % self)
Ejemplo n.º 34
0
 def enter(self):
     if self._entry_has_run:
         logger.debug("No Need to Run Enter %s" % self)
         return
     elif callable(self.enterfxn):
         self.enterfxn()
         self._entry_has_run = True
         logger.debug("Enter %s" % self)
     else:
         logger.debug("Nothing to Call on Enter %s" % self)
     
     self.active = True
Ejemplo n.º 35
0
    def add_to_casemodel(self):
        try:
            module = self.comboBox_module.currentText()
            condition_List = list()
            condition_List.clear()
            condition_List.append("module='" + module + "'")
            module_id = DBManager().query("modules", "module_id", condition_List)[0]
            toplevel_element = self.comboBox_toplevel.currentText()
            items_sublevel = self.listWidget_sublevel.selectedItems()
            items_thirdlevel = self.listWidget_thirdlevel.selectedItems()
            for i_sublevel in items_sublevel:
                sublevel_element = i_sublevel.text()
                condition_List.clear()
                condition_List.append("sublevel_element='" + i_sublevel.text() + "'")
                sublevel_id = DBManager().query("sublevel", "sublevel_id", condition_List)[0]

            # 使用字典记录即将添加的值,并调用公用方法添加数据
            for i_thirdlevel in items_thirdlevel:
                data_dict = dict()
                condition_List.clear()
                condition_List.append("sublevel_id='" + sublevel_id + "'")
                logger.debug(sublevel_id)
                condition_List.append("module_id='" + str(module_id) + "'")
                condition_List.append("thirdlevel_element='" + i_thirdlevel.text() + "'")
                logger.debug(condition_List)
                thirdlevel_id = DBManager().query("thirdlevel", "thirdlevel_id", condition_List)[0]
                condition_List.clear()
                condition_List.append("module_id='" + str(module_id) + "'")
                condition_List.append("thirdlevel_element='" + i_thirdlevel.text() + "'")
                casemodel_thirdlevel_id = DBManager().query("casemodel", "thirdlevel_id", condition_List)
                if thirdlevel_id not in casemodel_thirdlevel_id:
                    data_dict["thirdlevel_id"] = thirdlevel_id
                    thirdlevel_element = i_thirdlevel.text()
                    data_dict["thirdlevel_element"] = thirdlevel_element
                    data_dict["sublevel_id"] = sublevel_id
                    data_dict["sublevel_element"] = sublevel_element
                    condition_List.clear()
                    condition_List.append("toplevel_element='" + toplevel_element + "'")
                    toplevel_id = DBManager().query("toplevel", "toplevel_id", condition_List)[0]
                    data_dict["toplevel_id"] = toplevel_id
                    data_dict["toplevel_element"] = toplevel_element
                    data_dict["module_id"] = module_id
                    logger.debug(data_dict)
                    DBManager().insert_data("casemodel", data_dict)
            self.listWidget_casemodel_handle()
        except:
            logger.exception("发现错误:")
Ejemplo n.º 36
0
 def to_excel(self):
     try:
         # toplevel_id在sublevel表中个数索引从1开始
         toplevel_count_list = list()
         condition_list = list()
         module = self.comboBox_module.currentText()
         condition_list.clear()
         condition_list.append("module='" + module + "'")
         module_id = DBManager().query("modules", "module_id", condition_list)[0]
         logger.debug(module_id)
         logger.debug(condition_list)
         for i in range(4):
             i = i + 1
             condition_list.clear()
             condition_list.append("toplevel_id='" + str(i) + "'")
             toplevel_count_list.append(len(DBManager().query("sublevel", "sublevel_elemnt", condition_list)))
         TEMPLATE_FILE = "./templates/测试建模模板.xlsx"
         SHEET_COUNT = 4
         real_index_list = list()
         condition_list.clear()
         condition_list.append("module_id='" + str(module_id) + "'")
         toplevel_id = DBManager().query("casemodel", "toplevel_id", condition_list)
         # sublevel_id数据索引从1开始
         sublevel_id = DBManager().query("casemodel", "sublevel_id", condition_list)
         for j in sublevel_id:
             real_index = int(j)
             toplevel_count_index = 0
             # 由于sublevel_id数据索引和sublevel表中toplevel_id个数索引均从1开始
             # 故两者之差大于0时认为当前sublevel_element为下一个顶级元素的次级元素,需要做差取真实表格索引
             while ((real_index - toplevel_count_list[toplevel_count_index]) > 0):
                 real_index = real_index - toplevel_count_list[toplevel_count_index]
                 toplevel_count_index = toplevel_count_index + 1
             real_index_list.append(real_index)
         thirdlevel_element = DBManager().query("casemodel", "thirdlevel_element", condition_list)
         data_list = list()
         data_list.append(toplevel_id)
         data_list.append(real_index_list)
         data_list.append(thirdlevel_element)
         logger.debug(data_list)
         OutputWithTemplate().output_with_excel(TEMPLATE_FILE, SHEET_COUNT, data_list)
     except:
         logger.exception("发现错误:")
Ejemplo n.º 37
0
    def output_with_excel(self, template_file, sheet_count, data_list):
        '''
        :param template_file: 模板文件路径
        :param sheet_count: 模板文件容sheet的个数,比如5个sheet页通过这个参数可以指定就用3个
        :param data_list: 数据list,其中包含sheet页list,表头list,数据list
        本方法中均使用list进行数据操作,由于模板中第一行为标题,故插入数据的行数索引从1开始
        '''
        try:
            data = xlrd.open_workbook(template_file)
            sheet_name = data.sheet_names()
            sheet_list = list()
            # 将模板文件sheet对象存入sheet_list列表
            for i in range(sheet_count):
                sheet_list.append(data.sheets()[i])
            template_data_list = list()
            # 将模板文件每个sheet的数据list存入tempLate_data_list列表
            for i_sheet in sheet_list:
                template_data_list.append(i_sheet.row_values(0))
            output_file = xlwt.Workbook()
            # 字体设定
            font = xlwt.Font()
            font.bold = True
            # 边框设定
            borders = xlwt.Borders()
            borders.left = 1
            borders.right = 1
            borders.top = 1
            borders.bottom = 1
            # 背景色设定
            backcolor = xlwt.Pattern()
            backcolor.pattern = xlwt.Pattern.SOLID_PATTERN
            backcolor.pattern_fore_colour = 5
            style = xlwt.XFStyle()
            style.font = font
            style.borders = borders
            style.pattern = backcolor
            output_file_sheet_list = list()
            # 输出文件添加sheet并给予标题,对象存入output_file_sheet_list列表
            for j in range(sheet_count):
                output_file_sheet_list.append(output_file.add_sheet(sheet_name[j], cell_overwrite_ok=True))
            # j_sheet_num为sheet页的索引
            for j_sheet_num in range(len(sheet_list)):
                # j_sheet_row_index为模板文件template_dataList的索引
                for j_sheet_row_index in range(len(template_data_list[j_sheet_num])):
                    output_file_sheet_list[j_sheet_num].write(0, j_sheet_row_index,
                                                              template_data_list[j_sheet_num][j_sheet_row_index], style)

            # 数据格式为{"sheet页数":{"列数":[当前添加的数据1,当前添加的数据2]}}
            filter_dict = dict()
            logger.debug("++++++++++++++++++++++++++++++")
            logger.debug(data_list)
            # 以追加方式添加数据
            for data_num in range(len(data_list[0])):
                logger.debug(data_num)
                logger.debug(data_list)
                # 当前sheet的索引
                data_sheet_num = int(data_list[0][data_num]) - 1
                data_row_num = 1
                data_col_num = int(data_list[1][data_num]) - 1
                logger.debug("===================================")
                logger.debug(data_sheet_num)
                logger.debug(data_col_num)
                # 判断当前sheet页数是否在filter_dict中,即当前sheet是否曾经出现过
                if str(data_sheet_num) not in filter_dict:
                    # 当前sheet页数不存在filter_dict,添加对应data_sheet_num字典
                    filter_dict[str(data_sheet_num)] = {}
                    # 增加对应列数的数据list
                    filter_dict[str(data_sheet_num)][str(data_col_num)] = list()
                    # 由于sheet页数第一次出现,故数据也一定第一次出现,所以之列列表中添加值
                    filter_dict[str(data_sheet_num)][str(data_col_num)].append(data_col_num)
                else:
                    # 当前sheet页数存在filter_dict中,判断当前列数是否在列数字典中,即当前列数是否出现过
                    if str(data_col_num) not in filter_dict[str(data_sheet_num)]:
                        # 当前列数未出现过,添加列表
                        filter_dict[str(data_sheet_num)][str(data_col_num)] = list()
                        # 向列数字典列表插数据,但插入数据的当前行数为初始值,即1
                        filter_dict[str(data_sheet_num)][str(data_col_num)].append(data_col_num)
                    else:
                        # 当前列数出现过。向列数字典列表插数据,插入数据的当前行数为总出现的次数
                        filter_dict[str(data_sheet_num)][str(data_col_num)].append(data_col_num)
                        data_row_num = len(filter_dict[str(data_sheet_num)][str(data_col_num)])
                logger.debug(filter_dict)
                data_preview_add = data_list[2][data_num]
                # 向excel中插入数据
                output_file_sheet_list[data_sheet_num].write(data_row_num, data_col_num, data_preview_add)

            output_file.save(str(TimeStamp.time_stamp()) + ".xls")


        except:
            logger.exception("发现错误")
Ejemplo n.º 38
0
 def stop(self):
     logger.debug("Stop %s" % self)
     self.stop_event.set()
     self.join()
     self.stop_event.clear()
     Thread.__init__(self)