kivyblocks/kivyblocks/blocksapp.py

235 lines
5.4 KiB
Python
Raw Normal View History

2019-12-19 11:13:47 +08:00
import os
import sys
import signal
2021-01-21 12:30:56 +08:00
import codecs
import json
2019-12-19 11:13:47 +08:00
from appPublic.jsonConfig import getConfig
from appPublic.folderUtils import ProgramPath
2021-01-21 12:30:56 +08:00
from appPublic.uniqueID import getID
2019-12-19 11:13:47 +08:00
2020-06-03 11:23:34 +08:00
from kivy.factory import Factory
2019-12-19 11:13:47 +08:00
from kivy.config import Config
from kivy.metrics import sp,dp,mm
from kivy.core.window import WindowBase, Window
from kivy.clock import Clock
2020-03-17 18:05:56 +08:00
from kivy.logger import Logger
2021-07-09 14:15:54 +08:00
from kivy.utils import platform
2019-12-19 11:13:47 +08:00
import kivy
2021-01-21 12:30:56 +08:00
import plyer
2019-12-19 11:13:47 +08:00
from kivy.resources import resource_add_path
resource_add_path(os.path.join(os.path.dirname(__file__),'./ttf'))
Config.set('kivy', 'default_font', [
'msgothic',
'DroidSansFallback.ttf'])
from kivy.app import App
2021-01-20 14:10:00 +08:00
from kivy.utils import platform
2019-12-19 11:13:47 +08:00
from .threadcall import HttpClient,Workers
from .utils import *
from .pagescontainer import PageContainer
from .blocks import Blocks
2020-03-07 22:11:07 +08:00
from .theming import ThemeManager
2020-02-26 17:24:58 +08:00
from appPublic.rsa import RSA
2021-01-21 12:30:56 +08:00
if platform == 'android':
from jnius import autoclass
2020-02-26 17:24:58 +08:00
2021-06-30 11:07:19 +08:00
from .android_rotation import get_rotation
2020-02-26 17:24:58 +08:00
class ServerInfo:
def __init__(self):
self.rsaEngine = RSA()
self.publickey = None
self.authCode = None
def getServerPK(self):
config = getConfig()
url = '%s%s' % (config.uihome, config.publickey_url)
hc = App.get_running_app().hc
d = hc.get(url)
self.publickey = self.rsaEngine. publickeyFromText(d)
def encode(self,uinfo):
if self.publickey is None:
self.getServerPK()
if uinfo['authmethod'] == 'password':
authinfo = '%s::%s::%s' % (uinfo['authmethod'], uinfo['userid'], uinfo['passwd'])
x = self.rsaEngine.encode(self.publickey, authinfo)
self.authCode = x
return x
return None
2019-12-19 11:13:47 +08:00
def signal_handler(signal, frame):
app = App.get_running_app()
app.workers.running = False
app.stop()
print('Singal handled .........')
signal.signal(signal.SIGINT, signal_handler)
2020-03-15 00:51:44 +08:00
def getAuthHeader():
app = App.get_running_app()
if not hasattr(app,'serverinfo'):
print('app serverinfo not found')
2020-02-26 17:24:58 +08:00
return {}
2020-03-15 00:51:44 +08:00
serverinfo = app.serverinfo
if hasattr(serverinfo,'authCode'):
return {
'authorization':serverinfo.authCode
}
print('serverinfo authCode not found')
return {}
2021-03-22 22:54:22 +08:00
kivyblocks_css_keys = [
"height_nm",
"width_nm",
"height_cm",
"width_cm",
"bgcolor",
"fgcolor",
"radius",
"spacing",
"padding",
"border"
]
2020-03-15 00:51:44 +08:00
class BlocksApp(App):
2021-06-30 11:07:19 +08:00
def get_rotation(self):
return get_rotation()
2021-03-22 22:54:22 +08:00
def load_csses(self):
config = getConfig()
if not config.css:
return
if config.css.css_filename:
with codecs.open(config.css.css_filename, 'r', 'utf-8') as f:
d = json.load(f)
self.buildCsses(d)
if config.css.css_url:
hc = HttpClient()
d = hc.get(self.realurl(config.css.css_url))
self.buildCsses(d)
2021-05-24 12:25:29 +08:00
def get_css(self, cssname):
return self.csses.get(cssname, 'default')
2021-04-24 19:13:42 +08:00
def on_rotate(self,*largs):
self.current_rotation = Window.rotation
Logger.info('BlocksApp:on_rotate(), largs=%s',
self.current_rotation)
2021-03-22 22:54:22 +08:00
def buildCsses(self, dic):
for k,v in dic.items():
if isinstance(v,dict):
self.csses[k] = \
{x:y for x,y in v.items() if x in kivyblocks_css_keys}
def get_csses(self):
return self.csses
def get_css(self, name):
return self.csses.get(name,self.csses.get('default',{}))
2020-03-15 00:51:44 +08:00
def build(self):
2021-07-09 14:15:54 +08:00
self.platform = platform
self.is_desktop = platform in ['win', 'linux', 'macosx']
2020-03-15 00:51:44 +08:00
config = getConfig()
2021-03-22 22:54:22 +08:00
self.csses = {
"default":{
"bgcolor":[0.35,0.35,0.35,1],
"fgcolor":[0.85,0.85,0.85,1]
},
"input":{
"bgcolor":[0.35,0.35,0.35,1],
"fgcolor":[0.85,0.85,0.85,1]
},
"input_focus":{
2021-05-24 12:25:29 +08:00
"bgcolor":[0.25,0.25,0.25,1],
"fgcolor":[0.8,0.8,0.8,1]
2021-03-22 22:54:22 +08:00
}
}
2021-01-20 14:10:00 +08:00
self.public_headers = {}
Window.bind(on_request_close=self.on_close)
2021-04-24 21:09:07 +08:00
Window.bind(on_rotate=self.on_rotate)
2021-01-20 14:10:00 +08:00
Window.bind(size=self.device_info)
self.workers = Workers(maxworkers=config.maxworkers or 80)
self.workers.start()
2021-03-22 22:54:22 +08:00
self.load_csses()
2021-01-20 14:10:00 +08:00
self.running = True
2021-03-16 11:11:15 +08:00
if config.root:
blocks = Blocks()
x = blocks.widgetBuild(config.root)
if x is None:
alert('buildError,Exit', title='Error')
return Label(text='error')
return x
return None
2021-01-20 14:10:00 +08:00
2021-03-05 17:26:42 +08:00
def realurl(self, url):
if url.startswith('https://') or url.startswith('http://'):
return url
config = getConfig()
return '%s%s' % (config.uihome, url)
2021-01-21 12:30:56 +08:00
def get_user_data_path(self):
if platform == 'android':
Environment = autoclass('android.os.Environment')
2021-01-21 13:56:33 +08:00
sdpath = Environment.getExternalStorageDirectory()
2021-01-21 12:30:56 +08:00
return str(sdpath)
sdpath = App.get_running_app().user_data_dir
return str(sdpath)
def get_profile_name(self):
2021-01-21 13:56:33 +08:00
fname = os.path.join(self.user_data_dir,'.profile.json')
2021-01-21 12:30:56 +08:00
print('profile_path=', fname)
return fname
def write_profile(self, dic):
fname = self.get_profile_name()
with codecs.open(fname,'w','utf-8') as f:
json.dump(dic,f)
def write_default_profile(self):
device_id = getID()
try:
device_id = plyer.uniqueid.id
except:
pass
2021-02-14 13:13:21 +08:00
if isinstance(device_id,bytes):
device_id = device_id.decode('utf-8')
2021-01-21 12:30:56 +08:00
d = {
'device_id': device_id
}
self.write_profile(d)
def read_profile(self):
fname = self.get_profile_name()
if not os.path.isfile(fname):
self.write_default_profile()
with codecs.open(fname, 'r', 'utf-8') as f:
d = json.load(f)
return d
2021-01-20 14:10:00 +08:00
def device_info(self, *args):
d = {
"platform":platform,
"width":Window.width,
"height":Window.height
}
device = {
"device_info":";".join([f'{k}={v}' for k,v in d.items()])
}
self.public_headers.update(device)
def on_close(self, *args):
self.workers.running = False
return False
2020-06-03 11:23:34 +08:00