Merge lp:~patrickas/ensoft-sextant/environment_variables into lp:ensoft-sextant

Proposed by Patrick Stevens
Status: Merged
Approved by: Phil Connell
Approved revision: 14
Merged at revision: 8
Proposed branch: lp:~patrickas/ensoft-sextant/environment_variables
Merge into: lp:ensoft-sextant
Diff against target: 147 lines (+62/-14)
3 files modified
src/sextant/__main__.py (+17/-11)
src/sextant/environment.py (+43/-0)
src/sextant/web/server.py (+2/-3)
To merge this branch: bzr merge lp:~patrickas/ensoft-sextant/environment_variables
Reviewer Review Type Date Requested Status
Phil Connell Approve
Review via email: mp+231525@code.launchpad.net

Commit message

Add sextant.environment

Description of the change

Add a sextant.environment file to do any munging of paths required (so that other classes don't need to know about where they are installed). This should fix the inability to run Sextant Web from source (which happened because the resources folder is in a different place relative to sextant.web.server when installed from source vs. when installed by pip).

To post a comment you must log in.
Revision history for this message
Phil Connell (pconnell) :
review: Needs Fixing
11. By Patrick Stevens <email address hidden>

Bugfix: ConnectionError was not caught, presumably because it's a subclass of IOError.

12. By Patrick Stevens <email address hidden>

Docstring for sextant.environment; rename ENVIRONMENT_VAR to ENV_CONFIG_FILE.

13. By Patrick Stevens <email address hidden>

More docstring, and DRY on sextant.environment._find_folder

Revision history for this message
Patrick Stevens (patrickas) wrote :

Changes made.

Revision history for this message
Phil Connell (pconnell) wrote :

Couple more minor markups suggested.

review: Needs Fixing
14. By Patrick Stevens <email address hidden>

Clarify IOError when Neo4J server is not found; fix sextant.environment.__all__

Revision history for this message
Patrick Stevens (patrickas) :
Revision history for this message
Phil Connell (pconnell) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/sextant/__main__.py'
2--- src/sextant/__main__.py 2014-08-19 16:01:31 +0000
3+++ src/sextant/__main__.py 2014-08-21 13:03:35 +0000
4@@ -21,6 +21,7 @@
5 from . import update_db
6 from . import query
7 from . import db_api
8+from . import environment
9
10
11 # @@@ Logging level should be configurable (with some structure to setting up
12@@ -45,12 +46,13 @@
13 def get_config_file():
14 # get the config file option for neo4j server location and web port number
15 _ROOT = os.path.abspath(os.path.dirname(__file__))
16+
17 def get_data(path, file_name):
18 return os.path.join(_ROOT, path, file_name)
19
20- home_config = os.path.expanduser(os.path.join("~", ".sextant.conf"))
21- env_config = os.environ.get("SEXTANT_CONFIG", "")
22- example_config = get_data('../etc', 'sextant.conf')
23+ home_config = environment.HOME_CONFIG_FILE
24+ env_config = os.environ.get(environment.ENV_CONFIG_FILE, "")
25+ example_config = environment.DEFAULT_CONFIG_FILE
26
27 try:
28 conffile = next(p
29@@ -58,13 +60,13 @@
30 if os.path.exists(p))
31 except StopIteration:
32 #No config files accessable
33- if "SEXTANT_CONFIG" in os.environ:
34+ if environment.ENV_CONFIG_FILE in os.environ:
35 #SEXTANT_CONFIG environment variable is set
36- log.error("SEXTANT_CONFIG file %r doesn't exist.", env_config)
37+ log.error("{} file %r doesn't exist.".format(environment.ENV_CONFIG_FILE), env_config)
38 log.error("Sextant requires a configuration file.")
39 sys.exit(1)
40
41- log.info("Sextant is using config file %s", conffile)
42+ log.info("Sextant is using config file {}".format(conffile))
43 return conffile
44
45 conffile = get_config_file()
46@@ -72,9 +74,9 @@
47 conf = ConfigParser.ConfigParser()
48 conf.read(conffile)
49
50-#remote_neo4j = 'http://localhost:7474'
51-#web_port = 2905
52-#common_def = 10 # definition of a 'common' node
53+remote_neo4j = 'http://localhost:7474'
54+web_port = 2905
55+common_def = 10 # definition of a 'common' node
56 try:
57 options = conf.options('Neo4j')
58 except ConfigParser.NoSectionError:
59@@ -179,11 +181,15 @@
60 update_db.upload_program(namespace.input_file[0],
61 namespace.remote_neo4j,
62 alternative_name, not_object_file)
63- except IOError: # in case of Python 2, where FileNotFoundError is undefined
64- logging.error('Input file {} was not found.'.format(namespace.input_file[0]))
65 except requests.exceptions.ConnectionError as e:
66 logging.error('Connection error to server {}: {}'.format(namespace.remote_neo4j,
67 e))
68+
69+ except IOError as e:
70+ # in case of Python 2, where FileNotFoundError is undefined
71+ logging.error('Input file {} was not found.'.format(namespace.input_file[0]))
72+ logging.error(e)
73+ logging.debug(e, exc_info=True)
74 except ValueError as e:
75 logging.error(e)
76
77
78=== added file 'src/sextant/environment.py'
79--- src/sextant/environment.py 1970-01-01 00:00:00 +0000
80+++ src/sextant/environment.py 2014-08-21 13:03:35 +0000
81@@ -0,0 +1,43 @@
82+# -----------------------------------------
83+# Sextant
84+# Copyright 2014, Ensoft Ltd.
85+# Author: Patrick Stevens
86+# -----------------------------------------
87+# Define 'environment' variables for 'where we are located'.
88+
89+"""
90+Defines standard Sextant locations.
91+"""
92+
93+__all__ = ("RESOURCES_DIR", "DEFAULT_CONFIG_FILE", "ENV_CONFIG_FILE",
94+ "HOME_CONFIG_FILE")
95+
96+import os
97+
98+
99+RESOURCES_DIR = "" # where the resources folder is located
100+DEFAULT_CONFIG_FILE = "" # where the bundled config file is located
101+ENV_CONFIG_FILE = "SEXTANT_CONFIG" # name of the config-file env var to seek
102+# path to user-specific configuration file to seek
103+HOME_CONFIG_FILE = os.path.expanduser(os.path.join("~", ".sextant.conf"))
104+
105+
106+def _find_folder(name):
107+ """
108+ Given a folder name, returns the absolute path to that folder.
109+ If the folder could not be found in the standard installation places,
110+ raises IOError.
111+ """
112+ path = os.path.abspath(os.path.dirname(__file__))
113+ trial = path
114+ for level in range(2): # search both .. and ../..
115+ trial = os.path.join(trial, '..')
116+ if os.path.isdir(os.path.join(trial, name)):
117+ return os.path.abspath(os.path.join(trial, name))
118+
119+ raise IOError("Could not find {} folder.".format(name))
120+
121+
122+RESOURCES_DIR = _find_folder('resources')
123+_conf_path = _find_folder('etc')
124+DEFAULT_CONFIG_FILE = os.path.join(_conf_path, 'sextant.conf')
125
126=== modified file 'src/sextant/web/server.py'
127--- src/sextant/web/server.py 2014-08-19 09:20:45 +0000
128+++ src/sextant/web/server.py 2014-08-21 13:03:35 +0000
129@@ -20,6 +20,7 @@
130
131 import sextant.db_api as db_api
132 import sextant.export as export
133+import sextant.environment as environment
134 import tempfile
135 import subprocess
136
137@@ -298,9 +299,7 @@
138 global database_url
139 database_url = input_database_url
140 # serve static directory at root
141- root = File(os.path.join(
142- os.path.dirname(os.path.abspath(__file__)),
143- "..", "..", "..", "resources", "sextant", "web"))
144+ root = File(os.path.join(environment.RESOURCES_DIR, 'sextant', 'web'))
145
146 # serve a dynamic Echoer webpage at /echoer.html
147 root.putChild("echoer.html", Echoer())

Subscribers

People subscribed via source and target branches