示例#1
0
def demo_msg_mysql2ftp():
    msg = MSG()
    msg.origination = URL().init("mysql").check
    msg.destination = URL().init("ftp").check
    msg.treatment = URL().init("tcp")
    case = CASE()
    case.origination = msg.origination
    case.destination = msg.destination
    msg.activity = "init"
    msg.case = case
    msg.add_datum("This is a test string", path=URL().init("file"))
    msg.add_datum("中文UTF-8编码测试", path=URL().init("file"))
    path = "..\README\Babelor-设计.png"
    with open(path, "rb") as f:
        bytes_f = f.read()
        url = URL().init("file")
        url.path = path
        msg.add_datum(bytes_f, url)

    msg_string = str(msg)

    new_msg = MSG(msg_string)
    new_bytes_f = new_msg.read_datum(3)["stream"]
    new_path = "..\README\Babelor-设计-new.png"
    with open(new_path, "wb") as f:
        f.write(new_bytes_f)
示例#2
0
def demo_numpy():
    arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    msg = MSG()
    msg.add_datum(arr)
    message = str(msg)
    new_msg = MSG(message)
    print(msg)
    print(new_msg)
    print(new_msg.read_datum(0)["stream"])
示例#3
0
 def _create_mime(self, msg: MSG):
     # Connection
     sender_user = self.conn.fragment.username  # 寄件人用户名
     receiver_user = self.conn.username  # 收件人用户名
     sender_name = self.conn.fragment.fragment.path  # 寄件人名
     receiver_name = self.conn.path  # 收件人名
     sender_postfix = self.conn.fragment.fragment.hostname  # 寄件人 postfix
     receiver_postfix = self.conn.hostname  # 收件人 postfix
     # from msg
     data = {}
     for i in range(0, msg.dt_count, 1):
         datum = msg.read_datum(i)
         if datum["path"] not in data.keys():
             data[datum["path"]] = datum["stream"]
     if "subject" in data.keys():
         subject = data["subject"]
     else:
         subject = CONFIG.MAIL_SUBJECT
     if "content" in data.keys():
         content = data["content"]
     else:
         content = CONFIG.MAIL_CONTENT
     attachments = []
     for k in data.keys():
         if k not in ["subject", "content"]:
             attachments.append({
                 "stream": data[k],
                 "path": k,
             })
     # Structure MIME
     self.me = str(
         Address(
             Header(sender_name, CONFIG.Coding).encode(), sender_user,
             sender_postfix))
     self.to = str(
         Address(
             Header(receiver_name, CONFIG.Coding).encode(), receiver_user,
             receiver_postfix))
     self.subject = subject
     self.content = content
     self.mime['from'] = self.me  # 寄件人
     self.mime['to'] = self.to  # 收件人
     self.mime['subject'] = Header(self.subject, 'UTF-8').encode()  # 标题
     self.mime["Accept-Language"] = "zh-CN"  # 语言
     self.mime["Accept-Charset"] = "ISO-8859-1,utf-8"  # 字符集
     self.mime.attach(MIMEText(self.content, 'plain', "utf-8"))  # 正文
     if len(attachments) > 0:  # 附件
         for attachment in attachments:
             part = MIMEBase('application', 'octet-stream')
             part.set_payload(attachment["stream"])
             encoders.encode_base64(part)
             part.add_header('Content-Disposition',
                             'attachment',
                             filename=Header(
                                 attachment["path"].split("/")[-1],
                                 'UTF-8').encode())
             self.mime.attach(part)
示例#4
0
def func_encrypter(msg: MSG):
    # -————————————------------------------ INIT ---------
    data = {}
    for i in range(0, msg.dt_count, 1):
        datum = msg.read_datum(i)
        if datum["path"] not in data.keys():
            data[datum["path"]] = datum["stream"]
    # -————————————------------------------ PROCESS ------
    msg_out = msg
    # -————————————------------------------ END ----------
    return msg_out
示例#5
0
def func_sender(msg: MSG):
    # -—————————————------------------------ INIT ---------
    arguments = {}
    for i in range(0, msg.dt_count, 1):
        argument = msg.read_datum(i)
        if argument["path"] not in arguments.keys():
            arguments[argument["path"]] = argument["stream"]
    # -—————————————------------------------ PROCESS ------
    msg_out = msg
    # -—————————————------------------------ END ----------
    return msg_out
示例#6
0
 def write(self, msg: MSG):
     # logging.debug("FILE::{0}::WRITE msg:{1}".format(self.conn, msg))
     if self.url_is_dir:
         if not os.path.exists(self.conn.path):
             os.mkdir(self.conn.path)
     # -------------------------------
     rm_idx = []
     for i in range(0, msg.dt_count, 1):
         dt = msg.read_datum(i)
         if self.url_is_dir:
             path = os.path.join(self.conn.path, dt["path"])
         else:
             path = self.conn.path
         suffix = os.path.splitext(path)[-1]
         # -------------------------------
         if os.path.exists(path):
             logging.warning("FILE::{0}::WRITE failed.".format(path))
         elif os.path.isfile(os.path.split(path)[0]):
             logging.warning("FILE::{0}::WRITE failed.".format(path))
         else:
             if not os.path.isdir(os.path.split(path)[0]):
                 mkdir(os.path.split(path)[0])
             # -------------------------------
             if suffix in [".xls", ".xlsx"]:
                 if isinstance(dt["stream"], pd.DataFrame):
                     dt["stream"].to_excel(path, index=False)
                     logging.info(
                         "FILE::EXCEL::{0}::WRITE successfully.".format(
                             path))
                 else:
                     logging.warning(
                         "FILE::EXCEL::{0}::WRITE failed.".format(path))
             elif suffix in [".npy"]:
                 if isinstance(dt["stream"], np.ndarray):
                     np.save(path, dt["stream"])
                     logging.info(
                         "FILE::NUMPY::{0}::WRITE successfully.".format(
                             path))
                 else:
                     logging.warning(
                         "FILE::NUMPY::{0}::WRITE failed.".format(path))
             elif suffix in [""]:
                 logging.warning("FILE::{0}::WRITE None.".format(path))
             else:
                 with open(path, "wb") as file:
                     file.write(dt["stream"])
                 logging.info("FILE::{0}::WRITE successfully.".format(path))
         rm_idx = [i] + rm_idx
     # -------------------------------
     if CONFIG.IS_DATA_WRITE_END:
         for i in rm_idx:
             msg.remove_datum(i)
示例#7
0
 def write(self, msg: MSG):
     # logging.debug("FTP::{0}::WRITE msg:{1}".format(self.conn, msg))
     ftp = self.open()
     # -------------------------------------------------
     rm_idx = []
     for i in range(0, msg.dt_count, 1):
         dt = msg.read_datum(i)
         if self.url_is_dir:
             path = os.path.join(self.conn.path, dt["path"])
         else:
             path = self.conn.path
         # ----------------------------
         suffix = os.path.splitext(path)[-1]
         temp_path = "temp/temp" + suffix
         mkdir(os.path.split(temp_path)[0])
         # ----------------------------
         if suffix in [".xls", ".xlsx"]:
             if isinstance(dt["stream"], pd.DataFrame):
                 dt["stream"].to_excel(temp_path, index=False)
                 with open(temp_path, "rb") as temp_file:
                     stream = temp_file.read()
                 logging_info = "::EXCEL"
             else:
                 stream = None
                 logging_info = "::EXCEL"
         elif suffix in [".npy"]:
             if isinstance(dt["stream"], np.ndarray):
                 np.save(temp_path, dt["stream"])
                 with open(temp_path, "rb") as temp_file:
                     stream = temp_file.read()
                 logging_info = "::NUMPY"
             else:
                 stream = None
                 logging_info = "::NUMPY"
         else:
             stream = dt["stream"]
             logging_info = ""
         # ----------------------------
         ftp.storbinary('STOR ' + path, stream, CONFIG.FTP_BUFFER)
         rm_idx = [i] + rm_idx
         logging.info("FTP{0}::{1}::WRITE successfully.".format(
             logging_info, self.conn))
     # -------------------------------------------------
     if CONFIG.IS_DATA_WRITE_END:
         for i in rm_idx:
             msg.remove_datum(i)
     ftp.close()
示例#8
0
 def write(self, msg: MSG):
     # logging.debug("SQL::{0} write:{1}".format(self.conn, msg))
     # ----------------------------------
     rm_idx = []
     for i in range(0, msg.dt_count, 1):
         rt = msg.read_datum(i)
         df = rt["stream"]
         path = os.path.splitext(rt["path"])[0]
         if isinstance(df, pd.DataFrame):
             df.to_sql(path,
                       con=self.engine,
                       if_exists='replace',
                       index=False,
                       index_label=False)
             logging.info("SQL::{0}::WRITE successfully.".format(self.conn))
         else:
             logging.warning("SQL::{0}::WRITE failed.".format(self.conn))
         rm_idx = [i] + rm_idx
     # ----------------------------------
     if CONFIG.IS_DATA_WRITE_END:
         for i in rm_idx:
             msg.remove_datum(i)