自定义import

This commit is contained in:
Irony 2018-01-28 20:26:39 +08:00
parent f71f51db09
commit 2b217d5bb8
13 changed files with 166 additions and 6 deletions

4
.gitignore vendored
View file

@ -2,6 +2,10 @@
/tmp/*
*.pyc
.setting
.setting/
.setting/*
/.setting/
/.setting/*
tmp
.project
.pydevproject

View file

@ -10,6 +10,8 @@ encoding//PyQtChart\u7EC3\u4E60/test/ToolTip2.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u4E16\u754C\u5730\u56FE/WorldMap.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u6DFB\u52A0QWidget.py=utf-8
encoding//tmp/\u622A\u56FE\u753B\u77E9\u5F62/DrawRectangle.py=utf-8
encoding//tmp/\u6570\u636E\u8054\u52A8/ComboBox.py=utf-8
encoding//tmp/\u7B80\u5355\u63D2\u4EF6/Widget.py=utf-8
encoding//\u5168\u5C40\u70ED\u952E/HotKey.py=utf-8
encoding//\u56FE\u7247\u52A0\u8F7D/LoadImage.py=utf-8
encoding//\u56FE\u7247\u52A0\u8F7D/res_rc.py=utf-8
@ -38,4 +40,8 @@ encoding//\u81EA\u52A8\u66F4\u65B0/test.py=utf-8
encoding//\u81EA\u5B9A\u4E49QWidget\u7684QSS\u6837\u5F0F/CustomPaintWidget.py=utf-8
encoding//\u81EA\u5B9A\u4E49QWidget\u7684QSS\u6837\u5F0F/CustomWidget.py=utf-8
encoding//\u81EA\u5B9A\u4E49QWidget\u7684QSS\u6837\u5F0F/test.py=utf-8
encoding//\u81EA\u5B9A\u4E49import/IronyImporter.py=utf-8
encoding//\u81EA\u5B9A\u4E49import/build.py=utf-8
encoding//\u81EA\u5B9A\u4E49import/main.py=utf-8
encoding//\u81EA\u5B9A\u4E49import/src/test.py=utf-8
encoding//\u8868\u683C\u590D\u5236/TableView.py=utf-8

View file

@ -18,6 +18,7 @@
- [1.13 图片加载](图片加载/)
- [1.14 窗口重启](窗口重启/)
- [1.15 气泡提示](气泡提示/)
- [1.16 自定义import](自定义import/)
### [2.QGraphicsView练习](QGraphicsView练习/)
- [2.1 世界地图](QGraphicsView练习/世界地图)

View file

@ -0,0 +1,57 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2018年1月28日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: IronyImporter
@description:
'''
import base64
import os
import sys
from types import ModuleType
import xxtea # @UnresolvedImport
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
KEY = base64.b85decode("HF5^hbNbOVOKM=(SB`7h")
class IronyImporter:
@classmethod
def find_module(cls, name, path=None):
name = name + ".irony"
if not os.path.isfile(name):
return None
return cls
@classmethod
def load_module(cls, name):
if name in sys.modules:
return sys.modules[name]
mod = ModuleType(name)
mod.__loader__ = cls
mod.__name__ = name
mod.__file__ = name + ".irony"
try:
exec(xxtea.decrypt(open(mod.__file__, "rb").read(), KEY), mod.__dict__)
except Exception as e:
print(e)
return None
sys.modules[name] = mod
return mod
@classmethod
def module_repr(cls, module):
return "<module {!r} from ({!r})>".format(module.__name__, module.__file__)
sys.meta_path.insert(0, IronyImporter)

22
自定义import/README.md Normal file
View file

@ -0,0 +1,22 @@
# 自定义import
需要Python3.5.2或者自行编译xxtea
简单的了解了下import的原理
# 测试过程
- 1.在src中编写一个test.py
- 2.通过build.py 利用xxtea加密src/test.py 到当前目录的test.irony文件
- 3.运行main.py 进行测试
# 截图
test.py
![test.py](ScreenShot/1.png)
test.irony
![test.irony](ScreenShot/2.png)
main.py
![main.py](ScreenShot/3.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

25
自定义import/build.py Normal file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2018年1月28日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: build
@description:
'''
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
import base64
import xxtea # @UnresolvedImport
KEY = base64.b85decode("HF5^hbNbOVOKM=(SB`7h")
with open("src/test.py", "rb") as fi:
open("test.irony", "wb").write(xxtea.encrypt(fi.read(), KEY))
print("ok")

25
自定义import/main.py Normal file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2018年1月28日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: main
@description:
'''
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
# 首先要引入importer
import IronyImporter # @UnresolvedImport @UnusedImport
# 测试开始
import test
print(test)
print(dir(test))
print(test.test(1, 5)) # @UndefinedVariable

View file

@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2018年1月28日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: test
@description:
'''
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
def test(a, b):
return a + b

View file

@ -0,0 +1 @@
—ÌnÏ¿?_<>p(v:ˆ™,(÷!‰ýn ™öÕ¶<C395>P Vín÷µO_eÝ8·MC m9y?Gâ9{a°°ÄC‡ãt¦%d:åWk#¨¼U,£~TÂm/œ.š&äË<C3A4>)Ì8ygŸxý5ßrÉ뢷#|¯wÁ.#ú/ƒö3/^lqÃ}”™á'ÛONŸÓ+ŠŽÍÜ£E\[ŸVŒüeµ6<C2B5>$¸ëظ½Ihûi¤Çi·7¨OÁ²óÕÕÕ°&<26><><EFBFBD>åbï©SÿÁ˜raL—oƒ"¼Ü<C2BC>œ&4dc÷ä|îNä-(ôŸÚutt÷¢$Фüí\*«H¯¬æÿLÈ8ÚRtÝíºõ¼èó¼—xÕ|…pÆ-E:ÈÞç ?—Ô@í¶þÁŠÀb¬Äù¹•ì§™^h”*/>QtZZLR¦ø¯N¯ nl5ÓÖG«?‡Áµ5¼„ugštº|ÒõBºµŸ}ÎQ<C38E>ä·Áç.U àß(ÄH­žZÜ`Vž¡ŒÎ#(Aüy8ý}¤ˆAfÚv¥ÒV÷Gql$zÒËW<C38B>žM¡Ð>˜àP¢2BârH yµX¹<06>¨Ú±(o<0E>¼èÞÆCd˜jòu Óƒè׎oPne²6^éIy¶

BIN
自定义import/xxtea.pyd Normal file

Binary file not shown.