openmedialibrary/oml/api.py
2019-01-20 18:40:04 +05:30

117 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from os.path import normpath, dirname, abspath, join
import json
import os
import subprocess
import sys
import ox
from oxtornado import actions
import item.api
import user.api
import update
import logging
logger = logging.getLogger(__name__)
def win32_ui(type):
base = normpath(dirname(dirname(dirname(abspath(__file__)))))
cmd = [
join(base, 'platform_win32', 'python.exe'),
join(base, 'openmediablirary', 'oml', 'ui.py'), type
]
return cmd
def selectFolder(data):
'''
returns {
path
}
'''
cmd = ['./ctl', 'ui', 'folder']
if sys.platform == 'win32':
cmd = win32_ui('folder')
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
stdout, stderr = p.communicate()
path = stdout.decode('utf-8').strip()
return {
'path': path
}
actions.register(selectFolder, cache=False)
def selectFile(data):
'''
returns {
path
}
'''
cmd = ['./ctl', 'ui', 'file']
if sys.platform == 'win32':
cmd = win32_ui('file')
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
path = stdout.decode('utf-8').strip()
return {
'path': path
}
actions.register(selectFile, cache=False)
def autocompleteFolder(data):
'''
takes {
path
}
returns {
items
}
'''
path = data['path']
path = os.path.expanduser(path)
if os.path.isdir(path):
if path.endswith('/') and path != '/':
path = path[:-1]
folder = path
name = ''
else:
folder, name = os.path.split(path)
if os.path.exists(folder):
prefix, folders, files = next(os.walk(folder))
folders = [os.path.join(prefix, f) for f in folders if (not name or f.startswith(name)) and not f.startswith('.')]
if prefix == path:
folders = [path] + folders
else:
folders = []
return {
'items': ox.sorted_strings(folders)
}
actions.register(autocompleteFolder, cache=False)
def quit(data):
'''
takes {
}
returns {
}
'''
import state
state.main.stop()
return {}
actions.register(quit, cache=False)
def logError(data):
'''
takes {
}
returns {
}
'''
#logger.debug('frontend error %s', data)
return {}
actions.register(logError, cache=False)