-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathemulator.py
219 lines (167 loc) · 5.93 KB
/
emulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os, sys
import traceback
from threading import Thread
from functools import partial
try:
from importlib import reload
except:
pass
from kivy.lang import Builder
from kivy.clock import mainthread
from kivy.resources import resource_add_path, resource_remove_path
# from kivystudio.components.emulator_area import emulator_area
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.behaviors import ToggleButtonBehavior
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import ObjectProperty, StringProperty
from kivy.clock import Clock
import os
# __all__ = ('get_emulator',)
# filepath = os.path.dirname(__file__)
# Builder.load_file(os.path.join(filepath, 'emulator.kv'))
# class EmulatorArea(BoxLayout):
# screen_display = ObjectProperty(None)
# emulation_file = StringProperty('')
#
# def __init__(self, **kwargs):
# super(EmulatorArea, self).__init__(**kwargs)
# self.screen_manager = EmulatorScreens()
# self.add_widget(self.screen_manager)
# # self.screen_display = ScreenDisplay()
# self.screen_manager.add_widget(self.screen_display)
#
# def add_widget(self, widget):
# super(EmulatorArea, self).add_widget(widget)
# def toggle_orientation(self):
# if self.screen_display.screen.orientation == 'portrait':
# self.screen_display.screen.orientation = 'landscape'
# else:
# self.screen_display.screen.orientation = 'portrait'
# def open_screen_drop(self, widget):
# ScreenDrop().open(widget, self.screen_display)
#
# instance = []
#
#
# def emulator_area():
# if instance:
# return instance[0]
# else:
# emulator_area = EmulatorArea(size_hint_x=.45)
# instance.append(emulator_area)
# return emulator_area
def emulate_file(filename, threaded=False):
root = None
if not os.path.exists(filename):
return
dirname = os.path.dirname(filename)
sys.path.append(dirname)
os.chdir(dirname)
resource_add_path(dirname)
emulator_area().screen_display.screen.clear_widgets()
if threaded:
Thread(target=partial(start_emulation, filename, threaded=threaded)).start()
else:
start_emulation(filename, threaded=threaded)
def start_emulation(filename, threaded=False):
root = None
if os.path.splitext(filename)[1] == '.kv': # load the kivy file directly
try: # cahching error with kivy files
Builder.unload_file(filename)
root = Builder.load_file(filename)
except:
traceback.print_exc()
print("You kivy file has a problem")
elif os.path.splitext(filename)[1] == '.py':
load_defualt_kv(filename)
try: # cahching error with python files
root = load_py_file(filename)
except:
traceback.print_exc()
print("You python file has a problem")
if root:
if threaded:
emulation_done(root, filename)
else:
emulator_area().screen_display.screen.add_widget(root)
dirname = os.path.dirname(filename)
sys.path.pop()
resource_remove_path(dirname)
@mainthread
def emulation_done(root, filename):
if root:
emulator_area().screen_display.screen.add_widget(root)
def load_defualt_kv(filename):
app_cls_name = get_app_cls_name(filename)
if app_cls_name is None:
return
kv_name = app_cls_name.lower()
if app_cls_name.endswith('App'):
kv_name = app_cls_name[:len(app_cls_name) - 3].lower()
if app_cls_name:
file_dir = os.path.dirname(filename)
kv_filename = os.path.join(file_dir, kv_name + '.kv')
if os.path.exists(kv_filename):
try: # cahching error with kivy files
Builder.unload_file(kv_filename)
root = Builder.load_file(kv_filename)
except:
traceback.print_exc()
print("You kivy file has a problem")
def get_app_cls_name(filename):
with open(filename) as fn:
text = fn.read()
lines = text.splitlines()
app_cls = get_import_as('from kivy.app import App', lines)
def check_app_cls(line):
line = line.strip()
return line.startswith('class') and line.endswith('(%s):' % app_cls)
found = list(filter(check_app_cls, lines))
if found:
line = found[0]
cls_name = line.split('(')[0].split(' ')[1]
return cls_name
def get_root_from_runTouch():
with open(filename) as fn:
text = fn.read()
lines = text.splitlines()
run_touch = get_import_as('from kivy.base import runTouchApp', lines)
def check_run_touch(line):
line = line.strip()
return line.startswith('%s(' % run_touch)
found = list(filter(check_run_touch, lines))
if found:
line = found[0]
root_name = line.strip().split('(')[1].split(')')[0]
root_file = import_from_dir(filename)
root = getattr(reload(root_file), root_name)
return root
def load_py_file(filename):
app_cls_name = get_app_cls_name(filename)
if app_cls_name:
root_file = import_from_dir(filename)
app_cls = getattr(reload(root_file), app_cls_name)
root = app_cls().build()
return root
run_root = get_root_from_runTouch(filename)
if run_root:
return run_root
def import_from_dir(filename):
''' force python to import this file
from the project_ dir'''
dirname, file = os.path.split(filename)
sys.path = [dirname] + sys.path
import_word = os.path.splitext(file)[0]
imported = __import__(import_word)
return imported
def get_import_as(start, lines):
line = list(filter(lambda line: line.strip().startswith(start), lines))
if line:
words = line[0].split(' ')
import_word = words[len(words) - 1]
return import_word
else:
return