From 7b3771617773f836ef1285664ef9ab56ebd0b175 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Wed, 14 Oct 2015 13:19:52 +0200 Subject: [PATCH] Add themes as a submodule and handle both builtin themes and local themes. --- .gitmodules | 3 +++ butterfly/__init__.py | 20 +++++++++++++++++++- butterfly/routes.py | 32 +++++++++++++++++++------------- butterfly/static/ext.js | 35 +++++++++++++++++++++-------------- butterfly/themes | 1 + coffees/ext/theme.coffee | 31 ++++++++++++++++++++++--------- setup.py | 2 ++ 7 files changed, 87 insertions(+), 37 deletions(-) create mode 160000 butterfly/themes diff --git a/.gitmodules b/.gitmodules index e69de29..48b5f09 100644 --- a/.gitmodules +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "butterfly/themes"] + path = butterfly/themes + url = https://github.com/paradoxxxzero/butterfly-themes diff --git a/butterfly/__init__.py b/butterfly/__init__.py index 09188c0..5319df0 100644 --- a/butterfly/__init__.py +++ b/butterfly/__init__.py @@ -14,7 +14,7 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -__version__ = '2.0.8' +__version__ = '2.0.10' import os @@ -43,6 +43,24 @@ class Route(tornado.web.RequestHandler): def log(self): return log + @property + def builtin_themes_dir(self): + return os.path.join( + os.path.dirname(__file__), 'themes') + + @property + def themes_dir(self): + return os.path.join( + self.application.butterfly_dir, 'themes') + + def get_theme_dir(self, theme): + if theme.startswith('built-in-'): + return os.path.join( + self.builtin_themes_dir, theme[len('built-in-'):]) + return os.path.join( + self.themes_dir, theme) + + # Imported from executable if hasattr(tornado.options.options, 'debug'): application = tornado.web.Application( diff --git a/butterfly/routes.py b/butterfly/routes.py index 8037bb6..b09d3dc 100644 --- a/butterfly/routes.py +++ b/butterfly/routes.py @@ -56,10 +56,8 @@ class Theme(Route): 'You must install libsass to use sass ' '(pip install libsass)') return + base_dir = self.get_theme_dir(theme) - themes_dir = os.path.join( - self.application.butterfly_dir, 'themes') - base_dir = os.path.join(themes_dir, theme) style = None for ext in ['css', 'scss', 'sass']: probable_style = os.path.join(base_dir, 'style.%s' % ext) @@ -96,9 +94,7 @@ class ThemeStatic(Route): if '..' in name: raise tornado.web.HTTPError(403) - themes_dir = os.path.join( - self.application.butterfly_dir, 'themes') - base_dir = os.path.join(themes_dir, theme) + base_dir = self.get_theme_dir(theme) fn = os.path.normpath(os.path.join(base_dir, name)) if not fn.startswith(base_dir): @@ -326,19 +322,29 @@ class ThemesList(Route): """Get the theme list""" def get(self): - themes_dir = os.path.join( - self.application.butterfly_dir, 'themes') - self.set_header('Content-Type', 'application/json') - if os.path.exists(themes_dir): + + if os.path.exists(self.themes_dir): themes = [ theme - for theme in os.listdir(themes_dir) - if os.path.isdir(os.path.join(themes_dir, theme)) and + for theme in os.listdir(self.themes_dir) + if os.path.isdir(os.path.join(self.themes_dir, theme)) and not theme.startswith('.')] else: themes = [] + if os.path.exists(self.builtin_themes_dir): + builtin_themes = [ + 'built-in-%s' % theme + for theme in os.listdir(self.builtin_themes_dir) + if os.path.isdir(os.path.join( + self.builtin_themes_dir, theme)) and + not theme.startswith('.')] + else: + builtin_themes = [] + + self.set_header('Content-Type', 'application/json') self.write(tornado.escape.json_encode({ 'themes': sorted(themes), - 'dir': themes_dir + 'builtin_themes': sorted(builtin_themes), + 'dir': self.themes_dir })) diff --git a/butterfly/static/ext.js b/butterfly/static/ext.js index fbc3cdd..d7c9758 100644 --- a/butterfly/static/ext.js +++ b/butterfly/static/ext.js @@ -614,30 +614,37 @@ } oReq = new XMLHttpRequest(); oReq.addEventListener('load', function() { - var inner, j, len1, ref, response, theme, theme_list, themes, url; + var builtin_themes, inner, j, k, len1, len2, option, response, theme, theme_list, themes, url; response = JSON.parse(this.responseText); + builtin_themes = response.builtin_themes; themes = response.themes; - if (themes.length === 0) { - alert("No themes found in " + response.dir + ".\n Please install themes with butterfly.server.py --install-themes"); - return; - } inner = "
\n

Pick a theme:

\n \n \n
"; popup.open(inner); theme_list = document.getElementById('theme_list'); diff --git a/butterfly/themes b/butterfly/themes new file mode 160000 index 0000000..c9dd55f --- /dev/null +++ b/butterfly/themes @@ -0,0 +1 @@ +Subproject commit c9dd55f4e6bffe8b7fe737997f5b832a99baa733 diff --git a/coffees/ext/theme.coffee b/coffees/ext/theme.coffee index abd2a3b..0676f5c 100644 --- a/coffees/ext/theme.coffee +++ b/coffees/ext/theme.coffee @@ -25,23 +25,20 @@ document.addEventListener 'keydown', (e) -> oReq = new XMLHttpRequest() oReq.addEventListener 'load', -> response = JSON.parse(@responseText) + builtin_themes = response.builtin_themes themes = response.themes - if themes.length is 0 - alert("No themes found in #{response.dir}.\n - Please install themes with butterfly.server.py --install-themes") - return + # if themes.length is 0 + # alert("No themes found in #{response.dir}.\n + # Please install themes with butterfly.server.py --install-themes") + # return inner = """

Pick a theme:

diff --git a/setup.py b/setup.py index 45da483..cfa7f4f 100644 --- a/setup.py +++ b/setup.py @@ -29,6 +29,8 @@ options = dict( package_data={ 'butterfly': [ 'sass/*.sass', + 'themes/*', + 'themes/**/*', 'static/fonts/*', 'static/images/favicon.png', 'static/main.css',