summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorTulio Casagrande <tcasagra@thoughtworks.com>2017-01-25 17:42:07 -0200
committerTulio Casagrande <tcasagra@thoughtworks.com>2017-01-25 17:43:00 -0200
commit772592f612d615e3a1b1486d33edca4d298f0d04 (patch)
treeaa6a2188f6b3f387e750506947d13df6bf9a8631 /service
parent012d1f0b8ec7fa2e45043e59071a87f050cf9f5d (diff)
Extract duplicated methods to a common parent
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/resources/__init__.py13
-rw-r--r--service/pixelated/resources/login_resource.py26
-rw-r--r--service/pixelated/resources/root_resource.py16
3 files changed, 21 insertions, 34 deletions
diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py
index 11611f0b..6bac2f59 100644
--- a/service/pixelated/resources/__init__.py
+++ b/service/pixelated/resources/__init__.py
@@ -15,6 +15,7 @@
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
import json
+import os
from twisted.web.http import UNAUTHORIZED
from twisted.web.resource import Resource
@@ -56,6 +57,18 @@ def handle_error_deferred(e, request):
request.finish()
+def get_startup_folder():
+ path = os.path.dirname(os.path.abspath(__file__))
+ return os.path.join(path, '..', 'assets')
+
+
+def get_static_folder():
+ static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..", "..", "web-ui", "dist"))
+ if not os.path.exists(static_folder):
+ static_folder = os.path.join('/', 'usr', 'share', 'pixelated-user-agent')
+ return static_folder
+
+
class BaseResource(Resource):
def __init__(self, services_factory):
diff --git a/service/pixelated/resources/login_resource.py b/service/pixelated/resources/login_resource.py
index 6d6bee20..a6f4f9cf 100644
--- a/service/pixelated/resources/login_resource.py
+++ b/service/pixelated/resources/login_resource.py
@@ -20,7 +20,7 @@ from xml.sax import SAXParseException
from pixelated.authentication import Authenticator
from pixelated.config.leap import BootstrapUserServices
from pixelated.resources import BaseResource, UnAuthorizedResource, IPixelatedSession
-from pixelated.resources import handle_error_deferred
+from pixelated.resources import handle_error_deferred, get_startup_folder, get_static_folder
from twisted.cred.error import UnauthorizedLogin
from twisted.internet import defer
from twisted.logger import Logger
@@ -35,22 +35,6 @@ from twisted.web.template import Element, XMLFile, renderElement, renderer
log = Logger()
-def _get_startup_folder():
- path = os.path.dirname(os.path.abspath(__file__))
- return os.path.join(path, '..', 'assets')
-
-
-def _get_static_folder():
- static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..", "web-ui", "dist"))
- # this is a workaround for packaging
- if not os.path.exists(static_folder):
- static_folder = os.path.abspath(
- os.path.join(os.path.abspath(__file__), "..", "..", "..", "..", "web-ui", "dist"))
- if not os.path.exists(static_folder):
- static_folder = os.path.join('/', 'usr', 'share', 'pixelated-user-agent')
- return static_folder
-
-
def parse_accept_language(all_headers):
accepted_languages = ['pt-BR', 'en-US']
languages = all_headers.get('accept-language', '').split(';')[0]
@@ -61,7 +45,7 @@ def parse_accept_language(all_headers):
class DisclaimerElement(Element):
- loader = XMLFile(FilePath(os.path.join(_get_startup_folder(), '_login_disclaimer_banner.html')))
+ loader = XMLFile(FilePath(os.path.join(get_startup_folder(), '_login_disclaimer_banner.html')))
def __init__(self, banner):
super(DisclaimerElement, self).__init__()
@@ -84,7 +68,7 @@ class DisclaimerElement(Element):
class LoginWebSite(Element):
- loader = XMLFile(FilePath(os.path.join(_get_startup_folder(), 'login.html')))
+ loader = XMLFile(FilePath(os.path.join(get_startup_folder(), 'login.html')))
def __init__(self, error_msg=None, disclaimer_banner_file=None):
super(LoginWebSite, self).__init__()
@@ -107,8 +91,8 @@ class LoginResource(BaseResource):
def __init__(self, services_factory, provider=None, disclaimer_banner=None, authenticator=None):
BaseResource.__init__(self, services_factory)
- self._static_folder = _get_static_folder()
- self._startup_folder = _get_startup_folder()
+ self._static_folder = get_static_folder()
+ self._startup_folder = get_startup_folder()
self._disclaimer_banner = disclaimer_banner
self._provider = provider
self._authenticator = authenticator or Authenticator(provider)
diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py
index b4eef4ec..d3e0456e 100644
--- a/service/pixelated/resources/root_resource.py
+++ b/service/pixelated/resources/root_resource.py
@@ -20,7 +20,7 @@ from string import Template
from pixelated.resources.users import UsersResource
from pixelated.resources import BaseResource, UnAuthorizedResource, UnavailableResource
-from pixelated.resources import IPixelatedSession
+from pixelated.resources import get_startup_folder, get_static_folder
from pixelated.resources.attachments_resource import AttachmentsResource
from pixelated.resources.sandbox_resource import SandboxResource
from pixelated.resources.contacts_resource import ContactsResource
@@ -50,8 +50,8 @@ MODE_RUNNING = 2
class RootResource(BaseResource):
def __init__(self, services_factory):
BaseResource.__init__(self, services_factory)
- self._startup_assets_folder = self._get_startup_folder()
- self._static_folder = self._get_static_folder()
+ self._startup_assets_folder = get_startup_folder()
+ self._static_folder = get_static_folder()
self._html_template = open(os.path.join(self._static_folder, 'index.html')).read()
self._services_factory = services_factory
self._child_resources = ChildResourcesMap()
@@ -106,16 +106,6 @@ class RootResource(BaseResource):
self._mode = MODE_RUNNING
- def _get_startup_folder(self):
- path = os.path.dirname(os.path.abspath(__file__))
- return os.path.join(path, '..', 'assets')
-
- def _get_static_folder(self):
- static_folder = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..", "..", "web-ui", "dist"))
- if not os.path.exists(static_folder):
- static_folder = os.path.join('/', 'usr', 'share', 'pixelated-user-agent')
- return static_folder
-
def _is_starting(self):
return self._mode == MODE_STARTUP