openmedialibrary/oml/api.py

131 lines
2.8 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-02 22:32:44 +00:00
2014-05-04 17:26:43 +00:00
2019-01-20 13:10:04 +00:00
from os.path import normpath, dirname, abspath, join
2014-05-18 23:24:04 +00:00
import json
import os
2019-01-20 13:10:04 +00:00
import subprocess
import sys
2014-05-18 23:24:04 +00:00
import ox
2014-05-19 20:14:24 +00:00
from oxtornado import actions
2014-05-04 17:26:43 +00:00
import item.api
import user.api
2014-08-22 17:46:45 +00:00
import update
2014-05-18 23:24:04 +00:00
2016-02-23 08:15:37 +00:00
import logging
logger = logging.getLogger(__name__)
2014-05-19 20:14:24 +00:00
2019-01-20 13:10:04 +00:00
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
2014-05-19 20:14:24 +00:00
def selectFolder(data):
2014-05-18 23:24:04 +00:00
'''
returns {
path
}
'''
cmd = ['./ctl', 'ui', 'folder']
2019-01-20 13:10:04 +00:00
if sys.platform == 'win32':
cmd = win32_ui('folder')
2019-01-23 08:07:03 +00:00
if 'base' in data:
cmd += [data['base']]
2014-08-22 16:49:11 +00:00
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
2014-05-18 23:24:04 +00:00
stdout, stderr = p.communicate()
path = stdout.decode('utf-8').strip()
2019-01-24 07:17:00 +00:00
if path == 'None':
path = None
2014-05-18 23:24:04 +00:00
return {
'path': path
}
actions.register(selectFolder, cache=False)
2014-05-19 20:14:24 +00:00
def selectFile(data):
2014-05-18 23:24:04 +00:00
'''
returns {
path
}
'''
cmd = ['./ctl', 'ui', 'file']
2019-01-20 13:10:04 +00:00
if sys.platform == 'win32':
cmd = win32_ui('file')
2019-01-23 08:07:03 +00:00
if 'base' in data:
cmd += [data['base']]
2014-05-18 23:24:04 +00:00
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
path = stdout.decode('utf-8').strip()
2019-01-24 07:17:00 +00:00
if path == 'None':
path = None
2014-05-18 23:24:04 +00:00
return {
'path': path
}
actions.register(selectFile, cache=False)
2019-01-24 07:17:00 +00:00
def short_home(path):
home = os.path.expanduser('~')
if path and path.startswith(home):
path = path.replace(home, '~')
return path
2014-05-19 20:14:24 +00:00
def autocompleteFolder(data):
2014-05-18 23:24:04 +00:00
'''
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):
2014-09-02 22:32:44 +00:00
prefix, folders, files = next(os.walk(folder))
2014-05-19 18:12:02 +00:00
folders = [os.path.join(prefix, f) for f in folders if (not name or f.startswith(name)) and not f.startswith('.')]
2014-05-18 23:24:04 +00:00
if prefix == path:
folders = [path] + folders
else:
folders = []
2019-01-24 07:17:00 +00:00
folders = [short_home(f) for f in folders]
2014-05-18 23:24:04 +00:00
return {
'items': ox.sorted_strings(folders)
}
actions.register(autocompleteFolder, cache=False)
def quit(data):
'''
takes {
}
returns {
}
'''
2016-01-31 16:46:28 +00:00
import state
state.main.stop()
return {}
actions.register(quit, cache=False)
2016-02-23 08:15:37 +00:00
def logError(data):
'''
takes {
}
returns {
}
'''
#logger.debug('frontend error %s', data)
return {}
actions.register(logError, cache=False)