# -*- coding:utf8 -*-
from base import *
# a=cli.render('''
#    my name is {{name}}.
#    {%- for i in [1,2,3,4,5,6,7] %}
#     {{i}}
#    {%- endfor %}
# ''',{'name':"jqzhang"})
#
# # print(a)
#
#
# dsn='mysql://root:root@localhost:3306/ferry'
#
# conn=cli.get_mysql_connection(dsn)
# print(cli.mysql_query(conn,'show tables'))
# print(cli.mysql_query(conn,'select * from test'))


# conn=cli.get_sqlite_connection(":memory:")
#
# cli.sqlite_query(conn,'''
# CREATE TABLE COMPANY(
#    ID INT PRIMARY KEY     NOT NULL,
#    NAME           TEXT    NOT NULL,
#    AGE            INT     NOT NULL,
#    ADDRESS        CHAR(50),
#    SALARY         REAL
# );
# ''')

# i="insert into COMPANY(NAME,ID,AGE) values('jqzhang','1',30)"
# cli.sqlite_query(conn,i)
# i="insert into COMPANY(NAME,ID,AGE) values('hello','2',35)"
# cli.sqlite_query(conn,i)

# sql='''
# INSERT INTO COMPANY (`ID`,`NAME`,`AGE`,`ADDRESS`,`SALARY`)
# VALUES
# (1,'jqzhang',30,NULL,NULL),
# (2,'hello',35,NULL,NULL);'''
# cli.sqlite_query(conn,sql)
#
# rows=cli.sqlite_query(conn,'select * from COMPANY')
#
# print(cli.dict2sql('COMPANY',rows))
#
#
# dsn = os.environ.get("DSN", "mysql://root:root@127.0.0.1:3306/pjm_db")
#
# dsn='mysql://root:root@localhost:3306/ferry'
# conn=cli.get_mysql_connection(dsn)
# cli.dump_mysql_data(conn,"/tmp/ferry")
# cli.dump_mysql_ddl(conn,"/tmp/ferry")
#
# print(cli.get_dependencies())


# tpl='''
# <dl>
#     {% for key, value in d.items() %}
#     <dt>{{ key }}</dt>
#     <dd>{{ value }}</dd>
#     {% endfor %}
# </dl>
# '''
#
# d={'key1':'value1','key2':'value2'}
# print(cli.render(tpl,globals()))


# cli.example()
#
# req=cli.requests.get('https://www.baidu.com')
# print(req.content)

# print(cli.get_dependencies(True))

# html=cli.requests.get("https://www.baidu.com").content
# for i in cli.pq(html,'a','items'):
#     print(cli.pq(i,'a','text'))


# xml='''
# <note>
# <to>Tove</to>
# <from name="xxx">Jani</from>
# <heading>Reminder</heading>
# <body>Don't forget me this weekend!</body>
# </note>
# '''
#
# for i in cli.pq(xml,'from','items'):
#     print(i.attr('name'))


# cli.example()


# cli.get_redis()
#
# import dsnparse
#
# print(cli.parse_dsn(dsn))

# dsn='redis://localhost:63790/0'
# r=cli.get_redis(dsn)
# r.set('a','b')
# print(r.get('a'))

# import kafka

#

#
# def cc():
#     dsn="kafka://127.0.0.1:9092/test?group_id=2&bootstrap_servers=127.0.0.1:9092"
#     consumer=cli.get_kafka_consumer(dsn)
#     for m in consumer:
#         print(m)
# import threading
#
# c=threading.Thread(target=cc)
# c.setDaemon(True)
# c.start()
# #
# dsn="kafka://127.0.0.1:9092/?bootstrap_servers=127.0.0.1:9092"
#
# p=cli.get_kafka_producer(dsn)
# k=cli.kafka
#
# import time
# for i in range(1,1000):
#     p.send('test','xxx')
#     time.sleep(1)


# yaml='''
# Section:
#     heading: Heading 1
#     font:
#         name: Times New Roman
#         size: 22
#         color_theme: ACCENT_2
# '''
#
# a=cli.yaml2json(yaml)
# b=cli.json2yaml(a)
# print(a)
# print(b)

# def server():
#
#     import zmq
#
#     context = zmq.Context()
#     socket = context.socket(zmq.REP)
#     socket.bind("tcp://*:5555")
#
#     while True:
#         message = socket.recv()
#         print("Received: %s" % message)
#         socket.send("I am OK!".encode('utf-8'))
#
# import threading
# svc=threading.Thread(target=server)
# svc.setDaemon(True)
# svc.start()
#
# from concurrent.futures import ThreadPoolExecutor
#
# import zmq
# context = zmq.Context()
# socket = context.socket(zmq.REQ)
# socket.connect("tcp://localhost:5555")
#
# time.sleep(3)
#
# socket.send('Are you OK?'.encode('utf-8'))
# response = socket.recv()
# print("response: %s" % response)


# cli.gen_requirements()

# app=cli.get_flask_app()
# from flask import request
# app.config['SECRET_KEY']=os.urandom(24)
# @app.route("/")
# def hello():
#
#     respose= cli.get_flask_response()
#     respose.data='xxx'
#     respose.status=404
#     return respose
#
# app.run('0.0.0.0',port=8000,debug=True)

# states = ['New' , 'Ready', 'Waiting', 'Running','Terminated']
# transitions = [
#     {'trigger': 'Admitted', 'source': 'New', 'dest': 'Ready'},
#     {'trigger': 'Dispatch', 'source': 'Ready', 'dest': 'Running'},
#     {'trigger': 'Interrupt', 'source': 'Running', 'dest': 'Ready'},
#     {'trigger': 'InputOutputoreventwait', 'source': 'Running', 'dest': 'Waiting'},
#     {'trigger': 'InputOutputoreventcompletion', 'source': 'Waiting', 'dest': 'Ready'},
#     {'trigger': 'Exit', 'source': 'Running', 'dest': 'Terminated'}]
flow = '''
  1=New
2 = Ready
  3=        Waiting
4=Running
5=Terminated
Admitted:1->2
Dispatch:2 -> 4
Interrupt:4->2
InputOutputoreventwait:4->3
InputOutputoreventcompletion:3->2
Exit:4->5
'''
#
# flow = '''
#
# //格式如下：
# 动作1:状态1 -> 状态2
# 动作2:状态2 -> 状态3
# 动作3:状态3 -> 状态4
#
# //举例
# //通知member填写校招培训自评
# NotifyMemberToFillCampusAssignment:init->WaitForMemberFillCampusAssignment
# //member填写校招自评
# MemberFillCampusAssignment:WaitingFillCampusAssignment->FinishFillCampusAssignment
# //通知mentor提交校招生评价
# NotifyMentorToFillCampusAssignment:FinishFillCampusAssignment->WaitForMentorFillCampusAssignment
#
# '''
#
# print(cli.parse_fsm_flow(flow))




#
# from transitions import Transition,Machine
#
#
# data=cli.parse_fsm_flow(flow)
#
# class Matter(object):
#     pass
# model = Matter()
#
# machine = Machine(model=model, **cli.parse_fsm_flow(flow), initial='New')
#
# # machine.set_state('Exit')
# for i in range(1,10):
#     print(model.state)
#     print(model.Admitted())
#     print(model.Dispatch())


# dsn = 'mysq://root:mock@127.0.0.1:63307/mock'
# # d = '/Users/junqiang.zhang/repo/go/hr-api/mock/db'
# d='/tmp/test'
# conn = cli.get_mysql_connection(dsn)
# tables=['audit_log_tab']
# # tables.remove('audit_log_tab')
# # cli.dump_mysql_data(conn, dir=d, limit=0, overwrite=True,tables=tables)
#
# cli.dump_mysql_update_dml(conn,dir=d)


# conn=cli.get_sqlite_connection(":memory:")
#
# cli.sqlite_query(conn,'''
# CREATE TABLE COMPANY(
#    ID INT PRIMARY KEY     NOT NULL,
#    NAME           TEXT    NOT NULL,
#    AGE            INT     NOT NULL,
#    ADDRESS        CHAR(50),
#    SALARY         REAL
# );
# ''')
#
# cli.sqlite_query(conn,'''
# CREATE TABLE abc(
#    ID INT PRIMARY KEY     NOT NULL,
#    NAME           TEXT    NOT NULL,
#    AGE            INT     NOT NULL,
#    ADDRESS        CHAR(50),
#    SALARY         REAL
# );
# ''')
#
# sql='''
# INSERT INTO COMPANY (`ID`,`NAME`,`AGE`,`ADDRESS`,`SALARY`)
# VALUES
# (1,'jqzh''ang',30,NULL,NULL),
# (2,'hello',35,NULL,NULL);'''
# cli.sqlite_query(conn,sql)
# sql= cli.dump_sqlite_data(conn)['COMPANY']
#
# cli.sqlite_query(conn,'delete from COMPANY')
#
#
#
#
# cli.sqlite_query(conn,sql)
#
#
# print(cli.dump_sqlite_data(conn)['COMPANY'])


# cli.dump_sqlite_data(conn,'/tmp/test')

dsn = 'mysql://sz_sc_test2:6vJaoy5HHiPP0ms_sUnO@master.shopee_sls.mysql.cloud.test.shopee.io:6606/shopee_ssc_hr_db'
dsn='mysql://root:mock@127.0.0.1:63307/mock'

conn = cli.get_mysql_connection(dsn)

print(cli.dump_mysql_ddl(conn,dir='./'))


