zoukankan      html  css  js  c++  java
  • 微信小程序 原生代码 转wepy 字符串处理

    import glob
    import os

    cwd = os.getcwd()
    sep = os.sep
    target = cwd + sep + 'pages' + sep + '*' + sep + '*'
    filelist = glob.glob(target)
    pageNameSet = set([i.split('\')[1] for i in glob.glob('./pages/*/*')])
    pageStrDicList = {}
    for i in pageNameSet:
    pageStrDicList[i] = {'wxml': '', 'wxss': '', 'json': '', 'js': ''}

    '''
    Page页面字符串结构
    <style lang="less">.wxss全部复制写入</style>
    <template>.wxml全部复制写入</template>
    <script>
    import wepy from 'wepy'
    .js Page({之前部分全部复制写入
    export default class Index extends wepy.page {
    config = {
    .json全部复制写入
    }
    data = {.js data:的对象}
    methods = { .js Page({ })中除去data:{}外的全部 }
    }
    </script>


    '''

    for i in filelist:
    for pageName in pageNameSet:
    try:
    if pageName in i:
    if i.endswith('wxml'):
    with open(i, 'r', encoding='utf-8') as fr:
    pageStrDicList[pageName]['wxml'] = fr.read()
    elif i.endswith('wxss'):
    with open(i, 'r', encoding='utf-8') as fr:
    pageStrDicList[pageName]['wxss'] = fr.read()
    elif i.endswith('json'):
    with open(i, 'r', encoding='utf-8') as fr:
    pageStrDicList[pageName]['json'] = fr.read()
    elif i.endswith('js'):
    with open(i, 'r', encoding='utf-8') as fr:
    pageStrDicList[pageName]['js'] = fr.read()
    break
    except Exception as e:
    print(i)
    print(e)

    for pageName in pageStrDicList:
    try:
    r = cwd + sep + 'res' + sep + pageName + '.wpy'
    i = pageStrDicList[pageName]
    s = '<style lang="less"> ' + i['wxss'] + '</style> '
    s += '<template> ' + i['wxml'] + '</template> '
    s += '''
    <script>
    import wepy from 'wepy'
    '''
    l = i['js'].split('Page({')
    s += l[0]
    s += '''
    export default class Index extends wepy.page {
    config =
    '''
    s += i['json']
    s += ' '
    if 'data:' in l[1]:
    # js代码必须被微信ide格式化,这样保证data:存在且第一个满足要求
    index_0 = l[1].index('data:')

    index_1 = l[1][index_0:].index(' },')
    s += ' data = ' + l[1][index_0:][5:index_1] + '} '

    s += ' methods = {'
    s += l[1][index_0 + 5 + (index_1 - 5) + len(' },'):].replace(': function', '').rstrip(' ').rstrip(
    ' ').rstrip('})')
    s += '} }'
    else:
    s += ' methods = {'
    s += l[1][len('Page({') + 1:].replace(': function', '').rstrip(' ').rstrip(
    ' ').rstrip('})')
    s += '} }'
    with open(r, 'w', encoding='utf-8') as fw:
    fw.write(s)
    except Exception as e:
    print(e)
    print(pageName)




  • 相关阅读:
    异常处理
    组合,封装
    自我介绍
    27python更多实例
    28python类代码编写细节
    29python运算符重载
    30python 类的设计
    31python类的高级主题
    32python异常基础
    33python异常编码细节
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9571565.html
Copyright © 2011-2022 走看看