Esempio n. 1
0
class SMWinservice(win32serviceutil.ServiceFramework):

    _svc_name_ = 'airpods-service'
    _svc_display_name_ = 'Airpods Service'
    _svc_description_ = 'Airpods Service developed by ohanedan'

    @classmethod
    def parse_command_line(cls):
        if len(sys.argv) == 1 and \
                sys.argv[0].endswith('.exe') and \
                not sys.argv[0].endswith(r'win32\PythonService.exe'):

            servicemanager.Initialize()
            servicemanager.PrepareToHostSingle(cls)
            servicemanager.StartServiceCtrlDispatcher()

        else:
            if len(sys.argv) == 2 and sys.argv[1] == 'help':
                sys.argv = sys.argv[:1]

            win32serviceutil.HandleCommandLine(cls)

    def __init__(self, args):
        self.logger = Logger(self)
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        self.stopping = True
        self.stop()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        self.logger.Log("stopping")
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        self.stopping = False
        self.start()
        self.logger.Log("starting")
        self.main()

    def start(self):
        self.airpods = Airpods(self)
        self.pipe = Pipe(self)
        pass

    def stop(self):
        pass

    def main(self):
        while not self.stopping:
            try:
                data = self.airpods.GetDataJsonString()
                data = data + "\n"
                self.pipe.SendData(data.encode())
            except Exception as ex:
                self.logger.Log("error on send: " + str(ex))
                break
Esempio n. 2
0
def UploadedFiles():
    try:
        files = dbAccess.Files.GetAllUploadedFilesForUser(
            current_user.username)
        return jsonify({'success': True, 'data': files})
    except Exception as e:
        print(e)
        Logger.Log("Exception", e)
        return jsonify({'success': False, 'data': []})
Esempio n. 3
0
def GetGraphItems():
    try:
        return jsonify({
            "success": True,
            "data": dbAccess.PlotLy.GetAllPlotLyGraphs()
        })
    except Exception as e:
        print(e)
        Logger.Log(e)
        return jsonify({"success": False, "data": []})
Esempio n. 4
0
def DeleteFile():
    try:
        id = request.json['id']
        metadata = dbAccess.Files.GetFileMetaData(id)
        pathToFile = metadata["absPath"]
        success = dbAccess.Files.DeleteFile(pathToFile)
        if (metadata["convertToHtml"]):
            sucess = success and dbAccess.Files.DeleteFile(
                pathToFile.split(".")[0] + ".html")
        success = success and dbAccess.Files.DeleteFileMetaData(id)
        if (success):
            return jsonify({
                'success': success,
                'msg': "File Successfully deleted"
            })
        else:
            return jsonify({'success': False, 'msg': "Failed to delete File"})
    except Exception as e:
        print(e)
        Logger.Log("Exception", e)
        return jsonify({'success': False, 'msg': "Failed to delete File"})
Esempio n. 5
0
def main(_):
  config=parameters.load_parameters()
  eval_config= copy.deepcopy(config)
  eval_config.batch_size=1
  config_json = json.dumps(vars(config), indent=4, sort_keys=True)

  if not os.path.exists(config.log_path):
    os.makedirs(config.log_path)

  if config.test:
    logpath = "{}/{}".format(config.log_path, config.save_path) + "_test.log"
  else:
    logpath = "{}/{}".format(config.log_path, config.save_path) + ".log"
	
  logger = Logger(logpath)

  ckpt_file = os.path.join(config.log_path, config.save_path) + "/model"


  logger.Log ('config: %s'%(config_json,)) 

  Train,Dev,Test,vocab = reader.file2seqid(config)
  pretrain_embedding = generate_embeddings(config.glove_path,vocab, config.glove_dir) 
  tf.set_random_seed(config.seed)
  with tf.Graph().as_default():
    #initializer = tf.contrib.layers.xavier_initializer(uniform=True,seed=None,dtype=tf.float32)
    initializer = tf.random_uniform_initializer(-config.init_scale,config.init_scale)
    with tf.name_scope("Train"):
      with tf.variable_scope("Model", reuse=None, initializer=initializer):
        m = MyModel(is_training=True, config=config,pretrain_embedding=pretrain_embedding)
    
    with tf.name_scope("Valid"):
      with tf.variable_scope("Model", reuse=True, initializer=initializer):
        mvalid = MyModel(is_training=False,config=eval_config,pretrain_embedding=pretrain_embedding)

    with tf.name_scope("Test"):
      with tf.variable_scope("Model", reuse=True, initializer=initializer):
        mtest = MyModel(is_training=False, config=eval_config,pretrain_embedding=pretrain_embedding)
    
    #sv = tf.train.Supervisor(logdir=config.save_path)
    sv = tf.train.Supervisor()
    with sv.managed_session() as session:
      logger.Log("\n\nmodel params:%s"%(np.sum([np.product([xi.value for xi in x.get_shape()]) for x in tf.trainable_variables()])))
      t0=time.time()
 
      best_accuracy = config.best_accuracy
      best_val_epoch = config.best_val_epoch
      last_change_epoch = 0

      best_test_acc=0
      best_test_epoch=0
      
      #global train_acc
      for i in range(config.MAXITER):
        start_time=time.time()
        train_acc,train_loss,train_global_step,learning_rate,train_pred_total,train_true_total= run_epoch(session,data=Train, model=m,config=config, eval_op=m.optim, verbose=True)
        logger.Log("Epoch: %d train_acc: %.3f train_loss %.3f train_global_step:%s" % (i,train_acc,train_loss,train_global_step))

        dev_acc,dev_loss,_,_,dev_pred_total,dev_true_total= run_epoch(session,data=Dev,model=mvalid,config=eval_config)
        logger.Log("Epoch: %d dev_acc: %.3f dev_loss %.3f" % (i, dev_acc,dev_loss))

      
        sys.stdout.flush()
        # if <= then update 
        if best_accuracy <= dev_acc:
          best_accuracy = dev_acc
          best_val_epoch = i
          if config.save_path:
            logger.Log("Saving model %d to %s." % (i,ckpt_file))
            
            sv.saver.save(session,ckpt_file, global_step=train_global_step)

        if (i - best_val_epoch > config.update_learning)and(i-last_change_epoch>config.change_epoch):
          if learning_rate>config.min_lr:
            lr_decay = config.lr_decay ** max(i - config.max_epoch, 0.0)
            new_learning_rate = config.learning_rate * lr_decay
            last_change_epoch= i
            logger.Log("learning_rate-->change!Dang!Dang!Dang!-->%.10f"%(new_learning_rate))
            m.assign_lr(session,new_learning_rate)

          logger.Log (time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))

        
        end_time=time.time()
        logger.Log("-------- all_training time: %s one_epoch time: %s\n " % ((end_time-t0)//60, (end_time-start_time)//60))
        if i - best_val_epoch > config.early_stopping:
          logger.Log ("best_val_epoch:%d  best_val_accuracy:%.3f"%(best_val_epoch,best_accuracy))
          logging.info("Normal Early stop")
          logger.Log (time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
          break        
        elif i == config.MAXITER-1:
          logger.Log ("best_val_epoch:%d  best_val_accuracy:%.3f"%(best_val_epoch,best_accuracy))
          logging.info("Finishe Training")

      logger.Log("\n\n-----------------------testing!!!-----------------------" )
      #### evaluate on the test set
      #1. restore the best parameters
      #2. test on the test set and logger.Log confusion matrix 
      #ckpt = tf.train.get_checkpoint_state(config.save_path)
      ckpt = tf.train.get_checkpoint_state(ckpt_file)
      if ckpt and ckpt.model_checkpoint_path: 
        #init_step = int(ckpt.model_checkpoint_path.rsplit('-',1)[1])
        logger.Log ("restore best model:%s for testing:"%(ckpt.model_checkpoint_path))
        sv.saver.restore(session, ckpt.model_checkpoint_path)

      test_acc,test_loss,_,_,test_pred_total,test_true_total = run_epoch(session, data=Test,model=mtest,config=eval_config)
      logger.Log ("best_test_accuracy:%.3f test_loss %.3f "%(test_acc,test_loss))
      logger.Log("\nthe confuse_matrix of test:\n")
      logger.Log(confuse_matrix(true_label_total=test_true_total,pred_label_total=test_pred_total,config=config))
      logger.Log (time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
Esempio n. 6
0
class ChoseFile(wx.Frame):
    def __init__(self):
        super().__init__(parent=None,
                         title='ChoseFile',
                         size=(640, 535),
                         style=wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX
                         | wx.CLIP_CHILDREN)

        # ui
        self.init_ui()

        # config
        self.setting = Setting()

        # default
        self.init_default(self.setting)

        # logger
        self.logger = Logger(self.console_text)

        # parser
        self.parser = Parser(self.logger)

        # processor
        self.processor = Processor(self.logger, self.setting)

    def init_ui(self):
        # panel
        self.make_panel()

        # create a menu bar
        self.make_menu_bar()

        # status bar
        self.make_status_bar()

    def init_default(self, setting):
        excel_path = setting.get_excel_path()
        if excel_path is not None:
            self.file_name_text.SetValue(excel_path)

        source_dir = setting.get_source_dir()
        if source_dir is not None:
            self.source_dir_text.SetValue(source_dir)

        target_dir = setting.get_target_dir()
        if target_dir is not None:
            self.target_dir_text.SetValue(target_dir)

    def make_panel(self):
        self.panel = wx.Panel(self, size=(640, 505))

        # 源地址
        self.source_btn = wx.Button(self.panel,
                                    label='图库文件夹',
                                    pos=(10, 10),
                                    size=(85, 25))
        self.source_btn.Bind(wx.EVT_BUTTON, self.on_source)

        # 已选择的源地址
        self.source_dir_text = wx.TextCtrl(self.panel,
                                           pos=(105, 10),
                                           size=(400, 25),
                                           style=wx.TE_READONLY)

        # 清单文件
        self.select_btn = wx.Button(self.panel,
                                    label='清单文件',
                                    pos=(10, 40),
                                    size=(85, 25))
        self.select_btn.Bind(wx.EVT_BUTTON, self.on_select)

        # 已选择的清单文件
        self.file_name_text = wx.TextCtrl(self.panel,
                                          pos=(105, 40),
                                          size=(400, 25),
                                          style=wx.TE_READONLY)

        # 目标地址
        self.target_btn = wx.Button(self.panel,
                                    label='目标文件夹',
                                    pos=(10, 70),
                                    size=(85, 25))
        self.target_btn.Bind(wx.EVT_BUTTON, self.on_target)

        # 已选择的目标地址
        self.target_dir_text = wx.TextCtrl(self.panel,
                                           pos=(105, 70),
                                           size=(400, 25),
                                           style=wx.TE_READONLY)

        # 处理文件
        self.process_btn = wx.Button(self.panel,
                                     label='批量复制',
                                     pos=(10, 100),
                                     size=(85, 25))
        self.process_btn.Bind(wx.EVT_BUTTON, self.on_process)

        # 清空控制台日志
        self.clear_btn = wx.Button(self.panel,
                                   label='清空日志',
                                   pos=(105, 100),
                                   size=(85, 25))
        self.clear_btn.Bind(wx.EVT_BUTTON, self.on_clear_console_content)

        # 控制台
        self.console_text = wx.TextCtrl(self.panel,
                                        pos=(10, 130),
                                        size=(615, 325),
                                        style=wx.TE_MULTILINE | wx.TE_READONLY)

    # 打开文件
    def on_select(self, event):
        wildcard = 'Microsoft Excel 97/2000/XP/2003 Workbook(*.xls)|*.xls|Microsoft Excel 2007/2010 Workbook(*.xlsx)|*.xlsx'
        dialog = wx.FileDialog(None, "请选择要处理的Excel文件", os.getcwd(), '',
                               wildcard)

        if dialog.ShowModal() == wx.ID_OK:
            self.file_name_text.SetValue(dialog.GetPath())
            dialog.Destroy

    # 源文件夹
    def on_source(self, event):
        dialog = wx.DirDialog(self,
                              message="请选择图库文件夹",
                              defaultPath=self.setting.get_source_dir(),
                              style=wx.DD_DEFAULT_STYLE)
        if dialog.ShowModal() == wx.ID_OK:
            self.source_dir_text.SetValue(dialog.GetPath())
        dialog.Destroy()

    # 目标文件夹
    def on_target(self, event):
        dialog = wx.DirDialog(self,
                              message="请选择目标文件夹",
                              defaultPath=self.setting.get_target_dir(),
                              style=wx.DD_DEFAULT_STYLE)
        if dialog.ShowModal() == wx.ID_OK:
            self.target_dir_text.SetValue(dialog.GetPath())
        dialog.Destroy()

    def pre_check(self):
        source_path = self.source_dir_text.GetValue()

        # 检查索引是否存在,若不存在则构建
        pre_check_result = self.processor.pre_check(source_path)
        if not pre_check_result:
            wx.MessageBox("构建索引失败,请检查日志提示信息", "处理结果", wx.OK | wx.ICON_WARNING)
            return False

        file_name = self.file_name_text.GetValue()
        if file_name is None or file_name == '':
            wx.MessageBox("请先选择清单文件", "未选择清单文件", wx.OK | wx.ICON_WARNING)
            return False

        if not os.path.exists(file_name):
            wx.MessageBox("清单文件:" + file_name, "清单文件不存在",
                          wx.OK | wx.ICON_WARNING)
            return False

        target_path = self.target_dir_text.GetValue()
        if target_path is None or target_path == '':
            wx.MessageBox("请先选择目标地址", "未选择目标地址", wx.OK | wx.ICON_WARNING)
            return False

        if not os.path.isdir(target_path):
            wx.MessageBox("目标文件夹:" + target_path, "目标文件夹不存在",
                          wx.OK | wx.ICON_WARNING)
            return False

        return True

    # 复制文件
    def on_process(self, event):
        # 检查条件
        if not self.pre_check():
            return

        file_name = self.file_name_text.GetValue()
        target_path = self.target_dir_text.GetValue()
        self.logger.Log("[清单文件]\t" + file_name)
        self.logger.Log("[目标文件夹]\t" + target_path)

        # 解析清单文件
        name_arr = self.parser.parse_excel(file_name,
                                           self.setting.get_column_title())
        if name_arr is None:
            wx.MessageBox("解析结果为空", "处理结果", wx.OK | wx.ICON_WARNING)
            return

        # 执行处理流程
        process_result = self.processor.process(target_path, name_arr)

        # 结果提示
        message = "共处理" + str(process_result["total"]) + "个文件,处理成功" + str(
            process_result["success_num"]) + "个"
        wx.MessageBox(message, "处理结果", wx.OK | wx.ICON_INFORMATION)

        return

    def on_clear_console_content(self, event):
        self.console_text.SetValue("")
        # wx.MessageBox("已清空", "处理结果", wx.OK | wx.ICON_INFORMATION)

    # 退出菜单
    def on_exit(self, event):
        """Close the frame, terminating the application."""
        self.logger.close()
        self.Close(True)

    # 生成菜单
    def make_menu_bar(self):
        # Make the menu bar and add the two menus to it. The '&' defines
        # that the next letter is the "mnemonic" for the menu item. On the
        # platforms that support it those letters are underlined and can be
        # triggered from the keyboard.
        menu_bar = wx.MenuBar()

        menu_bar.Append(self.make_file_menu(), "&文件")
        menu_bar.Append(self.make_setting_menu(), "&设置")
        menu_bar.Append(self.make_help_menu(), "&帮助")

        # Give the menu bar to the frame
        self.SetMenuBar(menu_bar)

    # 文件菜单
    def make_file_menu(self):
        file_menu = wx.Menu()

        # The "\t..." syntax defines an accelerator key that also triggers
        # the same event
        open_item = file_menu.Append(-1, "&选择清单\tCtrl-O", "选择Excel清单文件")
        self.Bind(wx.EVT_MENU, self.on_select, open_item)

        export_item = file_menu.Append(-1, "&导出模板\tCtrl-E", "导出模板文件")
        self.Bind(wx.EVT_MENU, self.on_export_template, export_item)

        # 分隔符
        file_menu.AppendSeparator()

        exit_item = file_menu.Append(-1, "&退出\tCtrl-Q", "退出")
        self.Bind(wx.EVT_MENU, self.on_exit, exit_item)

        return file_menu

    # 模板菜单
    def make_setting_menu(self):
        setting_menu = wx.Menu()

        build_index_item = setting_menu.Append(
            -1, "&重建索引\tCtrl-B", "修改源文件路径[sourceDir]后,需要重新构建文件索引")
        self.Bind(wx.EVT_MENU, self.on_build_index, build_index_item)

        return setting_menu

    # 重建索引设置
    def on_build_index(self, event):
        source_path = self.source_dir_text.GetValue()

        result = self.processor.build_index(source_path)
        if result:
            wx.MessageBox("重建索引成功", "提示", wx.OK | wx.ICON_INFORMATION)
        else:
            wx.MessageBox("重建索引失败", "提示", wx.OK | wx.ICON_WARNING)

    # 帮助菜单
    def make_help_menu(self):
        help_menu = wx.Menu()

        usage_item = help_menu.Append(-1, "&说明\tCtrl-H", "查看使用说明")
        self.Bind(wx.EVT_MENU, self.on_usage, usage_item)

        about_item = help_menu.Append(-1, "&关于", "关于")
        self.Bind(wx.EVT_MENU, self.on_about, about_item)

        return help_menu

    # 导出模板文件
    def on_export_template(self, event):
        fd = wx.FileDialog(
            self,
            message='导出模板文件',
            defaultDir='',
            defaultFile='图号清单模板',
            wildcard=
            'Microsoft Excel 97/2000/XP/2003 Workbook(*.xls)|*.xls|Microsoft Excel 2007/2010 Workbook(*.xlsx)|*.xlsx',
            style=wx.FD_SAVE)
        if fd.ShowModal() == wx.ID_OK:
            try:
                file_name = fd.GetFilename()
                dir_name = fd.GetDirectory()
                column_title = self.setting.get_column_title()
                self.processor.export_template(
                    os.path.join(dir_name, file_name), column_title)
                save_msg = wx.MessageDialog(self, '保存成功!', '提示')
            except FileNotFoundError:
                save_msg = wx.MessageDialog(self, '保存失败,无效的保存路径', '提示')

            save_msg.ShowModal()
            save_msg.Destroy()

    # 使用说明
    def on_usage(self, event):
        message = "1.打开[config.ini],配置[sourceDir]等参数,需重启应用生效\n" \
                  + "2.点击[清单文件]按钮,选择要处理的清单文件\n" \
                  + "3.点击[目标文件夹]按钮,选择要保存的路径\n" \
                  + "4.点击[批量复制]按钮,将清单中指定的文件复制到目标文件夹\n" \
                  + "4.执行完成后,查看日志,确认是否执行成功\n" \
                  + "注意:若修改了[sourceDir]参数,请点击[设置]->[重建索引]菜单,重新构建文件索引\n"
        wx.MessageBox(message, "使用说明", wx.OK | wx.ICON_INFORMATION)

    # 关于菜单
    def on_about(self, event):
        """Display an About Dialog"""
        wx.MessageBox("批量复制文件", self.get_version(),
                      wx.OK | wx.ICON_INFORMATION)

    # 状态栏
    def make_status_bar(self):
        self.CreateStatusBar()
        self.SetStatusText("欢迎使用 " + self.get_version())

    # 版本号
    def get_version(self):
        return "ChoseFile V0.0.2"
Esempio n. 7
0
 print("* how are you today?")
 print("* 42")
 print("* hum, not that good huh?")
 logger = Logger(1)
 sources = [Engineering(logger), EDJ(logger)]
 while True:
     name = input("name: ")
     name = name.strip()
     if name == "exit":
         break
     structured = {}
     source_mapping = {}
     for source in sources:
         already_inside = len(structured)
         tmp = source.GetProfile(name)
         logger.Log("INFO", "__main__", f"profiles: {tmp}")
         tmpdic = {}
         for key in tmp:
             tmpdic[key + already_inside] = tmp[key]
             source_mapping[key + already_inside] = source
         tmp = tmpdic.copy()
         del tmpdic
         structured = {**structured, **tmp}
     if len(structured) == 0:
         print("* no profile found, sorry buddy :/")
         sys.exit(1)
     for struct in structured:
         print(f"({struct})")
         print(structured[struct])
     while True:
         print("* choose the number you want to display the full profile.")