<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -20,7 +20,7 @@ class LedgerHandler(BaseHTTPRequestHandler):
         except Exception:
             print &quot;Saw exception in POST handler&quot;
 
-def cmd_server():
+def main(*args):
     try:
         port   = 9000
         server = HTTPServer(('', port), LedgerHandler)
@@ -31,3 +31,6 @@ def cmd_server():
         print &quot;Shutting down server&quot;
         server.socket.close()
 
+print __name__
+if __name__ == '__main__':
+    main()</diff>
      <filename>python/server.py</filename>
    </modified>
    <modified>
      <diff>@@ -99,66 +99,75 @@ void python_interpreter_t::initialize()
     Py_Initialize();
     assert(Py_IsInitialized());
 
+    hack_system_paths();
+
     object main_module = python::import(&quot;__main__&quot;);
     if (! main_module)
-      throw_(std::logic_error,
+      throw_(std::runtime_error,
 	     _(&quot;Python failed to initialize (couldn't find __main__)&quot;));
 
     main_nspace = extract&lt;dict&gt;(main_module.attr(&quot;__dict__&quot;));
     if (! main_nspace)
-      throw_(std::logic_error,
+      throw_(std::runtime_error,
 	     _(&quot;Python failed to initialize (couldn't find __dict__)&quot;));
 
     python::detail::init_module(&quot;ledger&quot;, &amp;initialize_for_python);
 
     is_initialized = true;
+  }
+  catch (const error_already_set&amp;) {
+    PyErr_Print();
+    throw_(std::runtime_error, _(&quot;Python failed to initialize&quot;));
+  }
 
-    // Hack ledger.__path__ so it points to a real location
-    python::object module_sys = import(&quot;sys&quot;); 
-    python::object sys_dict   = module_sys.attr(&quot;__dict__&quot;);
-
-    python::list paths(sys_dict[&quot;path&quot;]);
-
-    bool path_initialized = false;
-    int n = python::extract&lt;int&gt;(paths.attr(&quot;__len__&quot;)());
-    for (int i = 0; i &lt; n; i++) {
-      python::extract&lt;std::string&gt; str(paths[i]);
-      path pathname(str);
-      DEBUG(&quot;python.interp&quot;, &quot;sys.path = &quot; &lt;&lt; pathname);
+  TRACE_FINISH(python_init, 1);
+}
 
-      if (exists(pathname / &quot;ledger&quot; / &quot;__init__.py&quot;)) {
-	if (python::object module_ledger = import(&quot;ledger&quot;)) {
-	  DEBUG(&quot;python.interp&quot;,
-		&quot;Setting ledger.__path__ = &quot; &lt;&lt; (pathname / &quot;ledger&quot;));
+void python_interpreter_t::hack_system_paths()
+{
+  // Hack ledger.__path__ so it points to a real location
+  python::object sys_module = python::import(&quot;sys&quot;); 
+  python::object sys_dict   = sys_module.attr(&quot;__dict__&quot;);
 
-	  python::object ledger_dict = module_ledger.attr(&quot;__dict__&quot;);
-	  python::list temp_list;
-	  temp_list.append((pathname / &quot;ledger&quot;).string());
+  python::list paths(sys_dict[&quot;path&quot;]);
 
-	  ledger_dict[&quot;__path__&quot;] = temp_list;
-	} else {
-	  throw_(std::logic_error,
-		 _(&quot;Python failed to initialize (couldn't find ledger)&quot;));
-	}
-	path_initialized = true;
-	break;
+#if defined(DEBUG_ON)
+  bool path_initialized = false;
+#endif
+  int n = python::extract&lt;int&gt;(paths.attr(&quot;__len__&quot;)());
+  for (int i = 0; i &lt; n; i++) {
+    python::extract&lt;std::string&gt; str(paths[i]);
+    path pathname(str);
+    DEBUG(&quot;python.interp&quot;, &quot;sys.path = &quot; &lt;&lt; pathname);
+
+    if (exists(pathname / &quot;ledger&quot; / &quot;__init__.py&quot;)) {
+      if (python::object module_ledger = python::import(&quot;ledger&quot;)) {
+	DEBUG(&quot;python.interp&quot;,
+	      &quot;Setting ledger.__path__ = &quot; &lt;&lt; (pathname / &quot;ledger&quot;));
+
+	python::object ledger_dict = module_ledger.attr(&quot;__dict__&quot;);
+	python::list temp_list;
+	temp_list.append((pathname / &quot;ledger&quot;).string());
+
+	ledger_dict[&quot;__path__&quot;] = temp_list;
+      } else {
+	throw_(std::runtime_error,
+	       _(&quot;Python failed to initialize (couldn't find ledger)&quot;));
       }
-    }
 #if defined(DEBUG_ON)
-    if (! path_initialized)
-      DEBUG(&quot;python.init&quot;,
-	    &quot;Ledger failed to find 'ledger/__init__.py' on the PYTHONPATH&quot;);
+      path_initialized = true;
 #endif
+      break;
+    }
   }
-  catch (const error_already_set&amp;) {
-    PyErr_Print();
-    throw_(std::logic_error, _(&quot;Python failed to initialize&quot;));
-  }
-
-  TRACE_FINISH(python_init, 1);
+#if defined(DEBUG_ON)
+  if (! path_initialized)
+    DEBUG(&quot;python.init&quot;,
+	  &quot;Ledger failed to find 'ledger/__init__.py' on the PYTHONPATH&quot;);
+#endif
 }
 
-object python_interpreter_t::import(const string&amp; str)
+object python_interpreter_t::import_into_main(const string&amp; str)
 {
   if (! is_initialized)
     initialize();
@@ -166,7 +175,8 @@ object python_interpreter_t::import(const string&amp; str)
   try {
     object mod = python::import(str.c_str());
     if (! mod)
-      throw_(std::logic_error, _(&quot;Failed to import Python module %1&quot;) &lt;&lt; str);
+      throw_(std::runtime_error,
+	     _(&quot;Failed to import Python module %1&quot;) &lt;&lt; str);
  
     // Import all top-level entries directly into the main namespace
     main_nspace.update(mod.attr(&quot;__dict__&quot;));
@@ -179,6 +189,32 @@ object python_interpreter_t::import(const string&amp; str)
   return object();
 }
 
+object python_interpreter_t::import_option(const string&amp; str)
+{
+  path file(str);
+
+  python::object sys_module = python::import(&quot;sys&quot;); 
+  python::object sys_dict   = sys_module.attr(&quot;__dict__&quot;);
+
+  python::list paths(sys_dict[&quot;path&quot;]);
+
+#if BOOST_VERSION &gt;= 103700
+  paths.insert(0, file.parent_path().string());
+  sys_dict[&quot;path&quot;] = paths;
+
+  string name = file.filename();
+  if (contains(name, &quot;.py&quot;))
+    name = file.stem();
+#else // BOOST_VERSION &gt;= 103700
+  paths.insert(0, file.branch_path().string());
+  sys_dict[&quot;path&quot;] = paths;
+
+  string name = file.leaf();
+#endif // BOOST_VERSION &gt;= 103700
+
+  return python::import(python::str(name.c_str()));
+}
+
 object python_interpreter_t::eval(std::istream&amp; in, py_eval_mode_t mode)
 {
   bool	 first = true;
@@ -212,7 +248,7 @@ object python_interpreter_t::eval(std::istream&amp; in, py_eval_mode_t mode)
   }
   catch (const error_already_set&amp;) {
     PyErr_Print();
-    throw_(std::logic_error, _(&quot;Failed to evaluate Python code&quot;));
+    throw_(std::runtime_error, _(&quot;Failed to evaluate Python code&quot;));
   }
   return object();
 }
@@ -234,7 +270,7 @@ object python_interpreter_t::eval(const string&amp; str, py_eval_mode_t mode)
   }
   catch (const error_already_set&amp;) {
     PyErr_Print();
-    throw_(std::logic_error, _(&quot;Failed to evaluate Python code&quot;));
+    throw_(std::runtime_error, _(&quot;Failed to evaluate Python code&quot;));
   }
   return object();
 }
@@ -255,7 +291,7 @@ value_t python_interpreter_t::python_command(call_scope_t&amp; args)
     std::strcpy(argv[i + 1], arg.c_str());
   }
 
-  int status;
+  int status = 1;
 
   try {
     status = Py_Main(static_cast&lt;int&gt;(args.size()) + 1, argv);
@@ -277,6 +313,44 @@ value_t python_interpreter_t::python_command(call_scope_t&amp; args)
   return NULL_VALUE;
 }
 
+value_t python_interpreter_t::server_command(call_scope_t&amp; args)
+{
+  if (! is_initialized)
+    initialize();
+
+  python::object server_module;
+
+  try {
+    server_module = python::import(&quot;ledger.server&quot;);
+    if (! server_module)
+      throw_(std::runtime_error,
+	     _(&quot;Could not import ledger.server; please check your PYTHONPATH&quot;));
+  }
+  catch (const error_already_set&amp;) {
+    PyErr_Print();
+    throw_(std::runtime_error,
+	   _(&quot;Could not import ledger.server; please check your PYTHONPATH&quot;));
+  }
+
+  if (python::object main_function = server_module.attr(&quot;main&quot;)) {
+    functor_t func(main_function, &quot;main&quot;);
+    try {
+      func(args);
+    }
+    catch (const error_already_set&amp;) {
+      PyErr_Print();
+      throw_(std::runtime_error,
+	     _(&quot;Error while invoking ledger.server's main() function&quot;));
+    }
+    return true;
+  } else {
+      throw_(std::runtime_error,
+	     _(&quot;The ledger.server module is missing its main() function!&quot;));
+  }
+
+  return false;
+}
+
 option_t&lt;python_interpreter_t&gt; *
 python_interpreter_t::lookup_option(const char * p)
 {
@@ -304,7 +378,7 @@ expr_t::ptr_op_t python_interpreter_t::lookup(const symbol_t::kind_t kind,
       DEBUG(&quot;python.interp&quot;, &quot;Python lookup: &quot; &lt;&lt; name);
 
       if (python::object obj = main_nspace.get(name.c_str()))
-	return WRAP_FUNCTOR(functor_t(name, obj));
+	return WRAP_FUNCTOR(functor_t(obj, name));
     }
     break;
 
@@ -320,6 +394,11 @@ expr_t::ptr_op_t python_interpreter_t::lookup(const symbol_t::kind_t kind,
       if (is_eq(p, &quot;python&quot;))
 	return MAKE_FUNCTOR(python_interpreter_t::python_command);
       break;
+
+    case 's':
+      if (is_eq(p, &quot;server&quot;))
+	return MAKE_FUNCTOR(python_interpreter_t::server_command);
+      break;
     }
   }
 
@@ -349,7 +428,7 @@ namespace {
 	       dynamic_cast&lt;const auto_xact_t *&gt;(scope))
 	lst.append(ptr(auto_xact));
       else
-	throw_(std::runtime_error,
+	throw_(std::logic_error,
 	       _(&quot;Cannot downcast scoped object to specific type&quot;));
     } else {
       lst.append(value);</diff>
      <filename>src/pyinterp.cc</filename>
    </modified>
    <modified>
      <diff>@@ -57,8 +57,10 @@ public:
   }
 
   void initialize();
+  void hack_system_paths();
 
-  python::object import(const string&amp; name);
+  python::object import_into_main(const string&amp; name);
+  python::object import_option(const string&amp; name);
 
   enum py_eval_mode_t {
     PY_EVAL_EXPR,
@@ -67,16 +69,17 @@ public:
   };
 
   python::object eval(std::istream&amp; in,
-			     py_eval_mode_t mode = PY_EVAL_EXPR);
+		      py_eval_mode_t mode = PY_EVAL_EXPR);
   python::object eval(const string&amp; str,
-			     py_eval_mode_t mode = PY_EVAL_EXPR);
+		      py_eval_mode_t mode = PY_EVAL_EXPR);
   python::object eval(const char * c_str,
-			     py_eval_mode_t mode = PY_EVAL_EXPR) {
+		      py_eval_mode_t mode = PY_EVAL_EXPR) {
     string str(c_str);
     return eval(str, mode);
   }
 
   value_t python_command(call_scope_t&amp; scope);
+  value_t server_command(call_scope_t&amp; args);
 
   class functor_t {
     functor_t();
@@ -87,9 +90,9 @@ public:
   public:
     string name;
 
-    functor_t(const string&amp; _name, python::object _func)
+    functor_t(python::object _func, const string&amp; _name)
       : func(_func), name(_name) {
-      TRACE_CTOR(functor_t, &quot;const string&amp;, python::object&quot;);
+      TRACE_CTOR(functor_t, &quot;python::object, const string&amp;&quot;);
     }
     functor_t(const functor_t&amp; other)
       : func(other.func), name(other.name) {
@@ -106,41 +109,10 @@ public:
   virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
 				  const string&amp; name);
 
-#if BOOST_VERSION &gt;= 103700
   OPTION_(python_interpreter_t, import_, DO_(scope) {
-      interactive_t args(scope, &quot;s&quot;);
-
-      path file(args.get&lt;string&gt;(0));
-
-      python::object module_sys = parent-&gt;import(&quot;sys&quot;); 
-      python::object sys_dict	= module_sys.attr(&quot;__dict__&quot;);
-
-      python::list paths(sys_dict[&quot;path&quot;]);
-      paths.insert(0, file.parent_path().string());
-      sys_dict[&quot;path&quot;] = paths;
-
-      string name = file.filename();
-      if (contains(name, &quot;.py&quot;))
-	parent-&gt;import(file.stem());
-      else
-	parent-&gt;import(name);
-    });
-#else // BOOST_VERSION &gt;= 103700
-  OPTION_(python_interpreter_t, import_, DO_(scope) {
-      interactive_t args(scope, &quot;s&quot;);
-
-      path file(args.get&lt;string&gt;(0));
-
-      python::object module_sys = parent-&gt;import(&quot;sys&quot;); 
-      python::object sys_dict	= module_sys.attr(&quot;__dict__&quot;);
-
-      python::list paths(sys_dict[&quot;path&quot;]);
-      paths.insert(0, file.branch_path().string());
-      sys_dict[&quot;path&quot;] = paths;
-
-      parent-&gt;import(file.leaf());
+      interactive_t args(scope, &quot;ss&quot;);
+      parent-&gt;import_option(args.get&lt;string&gt;(1));
     });
-#endif // BOOST_VERSION &gt;= 103700
 };
 
 extern shared_ptr&lt;python_interpreter_t&gt; python_session;</diff>
      <filename>src/pyinterp.h</filename>
    </modified>
    <modified>
      <diff>@@ -453,15 +453,6 @@ value_t report_t::echo_command(call_scope_t&amp; scope)
   return true;
 }
 
-bool report_t::maybe_import(const string&amp; module)
-{
-  if (lookup(symbol_t::OPTION, &quot;import_&quot;)) {
-    expr_t(string(&quot;import_(\&quot;&quot;) + module + &quot;\&quot;)&quot;).calc(*this);
-    return true;
-  }
-  return false;
-}
-
 option_t&lt;report_t&gt; * report_t::lookup_option(const char * p)
 {
   switch (*p) {
@@ -930,8 +921,6 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
 	  (reporter&lt;post_t, post_handler_ptr, &amp;report_t::commodities_report&gt;
 	   (new format_posts(*this, report_format(HANDLER(pricesdb_format_))),
 	    *this, &quot;#pricesdb&quot;));
-      else if (is_eq(p, &quot;python&quot;) &amp;&amp; maybe_import(&quot;ledger.interp&quot;))
-	return session.lookup(symbol_t::COMMAND, &quot;python&quot;);
       break;
 
     case 'r':
@@ -947,9 +936,8 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
     case 's':
       if (is_eq(p, &quot;stats&quot;) || is_eq(p, &quot;stat&quot;))
 	return WRAP_FUNCTOR(report_statistics);
-      else
-	if (is_eq(p, &quot;server&quot;) &amp;&amp; maybe_import(&quot;ledger.server&quot;))
-	  return session.lookup(symbol_t::COMMAND, &quot;server&quot;);
+      else if (is_eq(p, &quot;server&quot;))
+	return session.lookup(symbol_t::COMMAND, &quot;server&quot;);
       break;
 
     case 'x':
@@ -981,10 +969,6 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
 	  (reporter&lt;post_t, post_handler_ptr, &amp;report_t::generate_report&gt;
 	   (new format_posts(*this, report_format(HANDLER(print_format_)),
 			     false), *this, &quot;#generate&quot;));
-    case 'h':
-      if (is_eq(p, &quot;hello&quot;) &amp;&amp; maybe_import(&quot;ledger.hello&quot;))
-	return session.lookup(symbol_t::PRECOMMAND, &quot;hello&quot;);
-      break;
     case 'p':
       if (is_eq(p, &quot;parse&quot;))
 	return WRAP_FUNCTOR(parse_command);</diff>
      <filename>src/report.cc</filename>
    </modified>
    <modified>
      <diff>@@ -183,8 +183,6 @@ public:
 			  HANDLED(lots_actual));
   }
 
-  bool maybe_import(const string&amp; module);
-
   void report_options(std::ostream&amp; out)
   {
     HANDLER(abbrev_len_).report(out);</diff>
      <filename>src/report.h</filename>
    </modified>
    <modified>
      <diff>@@ -235,9 +235,7 @@ ledger_la_DEPENDENCIES = $(lib_LTLIBRARIES)
 ledger_la_LDFLAGS      = -avoid-version -module
 ledger_la_LIBADD       = $(LIBOBJS) $(lib_LTLIBRARIES) $(INTLLIBS)
 
-pkgpython_PYTHON = python/__init__.py		\
-	python/hello.py				\
-	python/server.py
+pkgpython_PYTHON = python/__init__.py python/server.py
 
 endif
 </diff>
      <filename>tools/Makefile.am</filename>
    </modified>
  </modified>
  <removed type="array">
    <removed>
      <filename>python/hello.py</filename>
    </removed>
  </removed>
  <parents type="array">
    <parent>
      <id>5ffa987daf4d97c52066e4c28733d826d3726297</id>
    </parent>
  </parents>
  <author>
    <name>John Wiegley</name>
    <email>johnw@newartisans.com</email>
  </author>
  <url>http://github.com/jwiegley/ledger/commit/91e8378f04d33137f8e6281928ae70af12be3b2b</url>
  <id>91e8378f04d33137f8e6281928ae70af12be3b2b</id>
  <committed-date>2009-11-10T11:16:40-08:00</committed-date>
  <authored-date>2009-11-10T11:16:40-08:00</authored-date>
  <message>Fixes to Python importing; removed &quot;hello&quot; precommand</message>
  <tree>1fe03ea8d7eeb21aa4f923373fbbd8c06f536d75</tree>
  <committer>
    <name>John Wiegley</name>
    <email>johnw@newartisans.com</email>
  </committer>
</commit>
