2015-06-30 00:51:55 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2017-01-04 07:33:32 -06:00
|
|
|
# Copyright (C) 2013 - 2017, The pgAdmin Development Team
|
2015-06-30 00:51:55 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
from collections import defaultdict
|
|
|
|
from operator import attrgetter
|
2016-06-21 08:12:14 -05:00
|
|
|
|
|
|
|
from flask import Blueprint
|
|
|
|
|
2016-05-12 13:34:28 -05:00
|
|
|
from .paths import get_storage_directory
|
2016-06-21 08:12:14 -05:00
|
|
|
from .preferences import Preferences
|
2015-06-29 01:58:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
class PgAdminModule(Blueprint):
|
|
|
|
"""
|
|
|
|
Base class for every PgAdmin Module.
|
|
|
|
|
|
|
|
This class defines a set of method and attributes that
|
|
|
|
every module should implement.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, import_name, **kwargs):
|
|
|
|
kwargs.setdefault('url_prefix', '/' + name)
|
|
|
|
kwargs.setdefault('template_folder', 'templates')
|
|
|
|
kwargs.setdefault('static_folder', 'static')
|
|
|
|
self.submodules = []
|
2017-07-07 01:25:55 -05:00
|
|
|
self.parentmodules = []
|
2016-03-07 05:48:24 -06:00
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
super(PgAdminModule, self).__init__(name, import_name, **kwargs)
|
|
|
|
|
2016-03-07 05:48:24 -06:00
|
|
|
def create_module_preference():
|
|
|
|
# Create preference for each module by default
|
|
|
|
if hasattr(self, 'LABEL'):
|
|
|
|
self.preference = Preferences(self.name, self.LABEL)
|
|
|
|
else:
|
|
|
|
self.preference = Preferences(self.name, None)
|
|
|
|
|
|
|
|
self.register_preferences()
|
|
|
|
|
|
|
|
# Create and register the module preference object and preferences for
|
|
|
|
# it just before the first request
|
|
|
|
self.before_app_first_request(create_module_preference)
|
|
|
|
|
|
|
|
def register_preferences(self):
|
|
|
|
pass
|
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
def register(self, app, options, first_registration=False):
|
|
|
|
"""
|
|
|
|
Override the default register function to automagically register
|
|
|
|
sub-modules at once.
|
|
|
|
"""
|
|
|
|
if first_registration:
|
|
|
|
self.submodules = list(app.find_submodules(self.import_name))
|
2016-03-07 05:48:24 -06:00
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
super(PgAdminModule, self).register(app, options, first_registration)
|
2016-03-07 05:48:24 -06:00
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
for module in self.submodules:
|
2017-07-07 01:25:55 -05:00
|
|
|
if first_registration:
|
|
|
|
module.parentmodules.append(self)
|
2015-06-29 01:58:41 -05:00
|
|
|
app.register_blueprint(module)
|
|
|
|
|
|
|
|
def get_own_stylesheets(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
list: the stylesheets used by this module, not including any
|
|
|
|
stylesheet needed by the submodules.
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
2016-05-10 05:37:45 -05:00
|
|
|
def get_own_messages(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
dict: the i18n messages used by this module, not including any
|
|
|
|
messages needed by the submodules.
|
|
|
|
"""
|
|
|
|
return dict()
|
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
def get_own_javascripts(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
list: the javascripts used by this module, not including
|
|
|
|
any script needed by the submodules.
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_own_menuitems(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
dict: the menuitems for this module, not including
|
|
|
|
any needed from the submodules.
|
|
|
|
"""
|
|
|
|
return defaultdict(list)
|
|
|
|
|
2015-06-29 02:54:05 -05:00
|
|
|
def get_panels(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
list: a list of panel objects to add
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
2017-06-12 01:31:22 -05:00
|
|
|
def get_exposed_url_endpoints(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
list: a list of url endpoints exposed to the client.
|
|
|
|
"""
|
|
|
|
return []
|
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
@property
|
|
|
|
def stylesheets(self):
|
|
|
|
stylesheets = self.get_own_stylesheets()
|
|
|
|
for module in self.submodules:
|
|
|
|
stylesheets.extend(module.stylesheets)
|
|
|
|
return stylesheets
|
|
|
|
|
2016-05-10 05:37:45 -05:00
|
|
|
@property
|
|
|
|
def messages(self):
|
|
|
|
res = self.get_own_messages()
|
|
|
|
|
|
|
|
for module in self.submodules:
|
|
|
|
res.update(module.messages)
|
|
|
|
return res
|
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
@property
|
|
|
|
def javascripts(self):
|
|
|
|
javascripts = self.get_own_javascripts()
|
|
|
|
for module in self.submodules:
|
|
|
|
javascripts.extend(module.javascripts)
|
|
|
|
return javascripts
|
|
|
|
|
|
|
|
@property
|
|
|
|
def menu_items(self):
|
|
|
|
menu_items = self.get_own_menuitems()
|
|
|
|
for module in self.submodules:
|
|
|
|
for key, value in module.menu_items.items():
|
|
|
|
menu_items[key].extend(value)
|
2016-01-27 08:59:54 -06:00
|
|
|
menu_items = dict((key, sorted(value, key=attrgetter('priority')))
|
2016-06-21 08:21:06 -05:00
|
|
|
for key, value in menu_items.items())
|
2015-06-29 01:58:41 -05:00
|
|
|
return menu_items
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
|
2017-06-12 01:31:22 -05:00
|
|
|
@property
|
|
|
|
def exposed_endpoints(self):
|
|
|
|
res = self.get_exposed_url_endpoints()
|
|
|
|
|
|
|
|
for module in self.submodules:
|
|
|
|
res += module.exposed_endpoints
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
Resolved quite a few file-system encoding/decoding related cases.
In order to resolve the non-ascii characters in path (in user directory,
storage path, etc) on windows, we have converted the path into the
short-path, so that - we don't need to deal with the encoding issues
(specially with Python 2).
We've resolved majority of the issues with this patch.
We still need couple issues to resolve after this in the same area.
TODO
* Add better support for non-ascii characters in the database name on
windows with Python 3
* Improve the messages created after the background processes by
different modules (such as Backup, Restore, Import/Export, etc.),
which does not show short-paths, and xml representable characters for
non-ascii characters, when found in the database objects, and the file
PATH.
Fixes #2174, #1797, #2166, #1940
Initial patch by: Surinder Kumar
Reviewed by: Murtuza Zabuawala
2017-03-07 04:00:57 -06:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
IS_PY2 = (sys.version_info[0] == 2)
|
|
|
|
IS_WIN = (os.name == 'nt')
|
|
|
|
|
|
|
|
sys_encoding = sys.getdefaultencoding()
|
|
|
|
if not sys_encoding or sys_encoding == 'ascii':
|
|
|
|
# Fall back to 'utf-8', if we couldn't determine the default encoding,
|
|
|
|
# or 'ascii'.
|
|
|
|
sys_encoding = 'utf-8'
|
|
|
|
|
|
|
|
fs_encoding = sys.getfilesystemencoding()
|
|
|
|
if not fs_encoding or fs_encoding == 'ascii':
|
|
|
|
# Fall back to 'utf-8', if we couldn't determine the file-system encoding,
|
|
|
|
# or 'ascii'.
|
|
|
|
fs_encoding = 'utf-8'
|
|
|
|
|
|
|
|
|
|
|
|
def u(_s, _encoding=sys_encoding):
|
|
|
|
if IS_PY2:
|
|
|
|
if isinstance(_s, str):
|
|
|
|
return unicode(_s, _encoding)
|
|
|
|
return _s
|
|
|
|
|
|
|
|
|
|
|
|
def file_quote(_p):
|
|
|
|
if IS_PY2:
|
|
|
|
if isinstance(_p, unicode):
|
|
|
|
return _p.encode(fs_encoding)
|
|
|
|
return _p
|
|
|
|
|
|
|
|
|
|
|
|
if IS_WIN:
|
|
|
|
import ctypes
|
|
|
|
from ctypes import wintypes
|
|
|
|
|
|
|
|
if IS_PY2:
|
|
|
|
def env(name):
|
|
|
|
if IS_PY2:
|
|
|
|
# Make sure string argument is unicode
|
|
|
|
name = unicode(name)
|
|
|
|
n = ctypes.windll.kernel32.GetEnvironmentVariableW(name, None, 0)
|
|
|
|
|
|
|
|
if n == 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
buf= ctypes.create_unicode_buffer(u'\0'*n)
|
|
|
|
ctypes.windll.kernel32.GetEnvironmentVariableW(name, buf, n)
|
|
|
|
|
|
|
|
return buf.value
|
|
|
|
else:
|
|
|
|
def env(name):
|
|
|
|
if name in os.environ:
|
|
|
|
return os.environ[name]
|
|
|
|
return None
|
|
|
|
|
|
|
|
_GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW
|
|
|
|
_GetShortPathNameW.argtypes = [
|
|
|
|
wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD
|
|
|
|
]
|
|
|
|
_GetShortPathNameW.restype = wintypes.DWORD
|
|
|
|
|
|
|
|
def fs_short_path(_path):
|
|
|
|
"""
|
|
|
|
Gets the short path name of a given long path.
|
|
|
|
http://stackoverflow.com/a/23598461/200291
|
|
|
|
"""
|
|
|
|
buf_size = len(_path)
|
|
|
|
while True:
|
|
|
|
res = ctypes.create_unicode_buffer(buf_size)
|
|
|
|
needed = _GetShortPathNameW(_path, res, buf_size)
|
|
|
|
|
|
|
|
if buf_size >= needed:
|
|
|
|
return res.value
|
|
|
|
else:
|
|
|
|
buf_size += needed
|
|
|
|
|
|
|
|
def document_dir():
|
|
|
|
CSIDL_PERSONAL = 5 # My Documents
|
|
|
|
SHGFP_TYPE_CURRENT = 0 # Get current, not default value
|
|
|
|
|
|
|
|
buf = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
|
|
|
|
ctypes.windll.shell32.SHGetFolderPathW(
|
|
|
|
None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf
|
|
|
|
)
|
|
|
|
|
|
|
|
return buf.value
|
|
|
|
|
|
|
|
else:
|
|
|
|
def env(name):
|
|
|
|
if name in os.environ:
|
|
|
|
return os.environ[name]
|
|
|
|
return None
|
|
|
|
|
|
|
|
def fs_short_path(_path):
|
|
|
|
return _path
|
|
|
|
|
|
|
|
def document_dir():
|
|
|
|
return os.path.realpath(os.path.expanduser(u'~/'))
|