changeset 2:6cb7696edc4d

add some more stuff for string loady type things and rearrange structure
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 10 May 2011 09:28:40 -0700
parents 47d9472e7754
children 65d9a7c7ac63
files pyloader/__init__.py pyloader/cast.py pyloader/invoke.py pyloader/loader.py setup.py
diffstat 5 files changed, 113 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/pyloader/__init__.py	Tue May 03 14:26:35 2011 -0700
+++ b/pyloader/__init__.py	Tue May 10 09:28:40 2011 -0700
@@ -1,49 +1,2 @@
-#!/usr/bin/env python
-
-"""
-load modules and their attributes from a string
-"""
-
-import imp
-import os
-import sys
-
-def import_dotted_path(module):
-  path = module.split('.')
-  module = __import__(module)
-  for name in path[1:]:
-    module = getattr(module, name)
-  return module
-
-def load(string):
-  """loads a string and returns a python object"""
-
-  try:
-    if ':' in string:
-      path, target = string.split(':', 1)
-      if os.path.isabs(path) and os.path.exists(path):
-        # path to file
-        sys.path.append(os.path.dirname(path))
-        module = imp.load_source(path, path)
-        sys.path.pop()
-      else:
-        module = import_dotted_path(path)
-      obj = module
-      while '.' in target:
-        attr, target = target.split('.', 1)
-        obj = getattr(obj, attr)
-      obj = getattr(obj, target)
-      return obj
-    else:
-      # module: dotted path
-      return import_dotted_path(string)
-  except:
-    print string
-    raise
-
-  # TODO: entry points
-
-if __name__ == '__main__':
-  import sys
-  for i in sys.argv[1:]:
-    print load(i)
+# import front-ends
+from loader import *
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyloader/cast.py	Tue May 10 09:28:40 2011 -0700
@@ -0,0 +1,50 @@
+class StringCastException(Exception):
+  """raised on unsucessful cast"""
+
+class StringCaster(object):
+  """cast strings to other things"""
+  # as yet unused
+
+  def __init__(self, *casts):
+    self.casts = casts
+
+  def __call__(self, string):
+    for cast in self.casts:
+      try:
+        return cast(string)
+      except:
+        continue
+    return string
+
+### casters
+
+def str2bool(string):
+  mapping = {'true': True,
+             'True': True,
+             'false': False,
+             'False': False}
+  return mapping[string]
+
+def str2list(string, separator=None):
+  string = string.rstrip(separator)
+  return [i.strip() for i in string.split(separator)]
+
+def str2args(string, separator=None):
+  args = str2list(string, separator)
+  _args = []
+  kw = {}
+  for arg in args:
+    if '=' in arg:
+      pass
+    else:
+      _args.append(arg)
+  return _args, kw
+
+# convenience functions
+casts = [int, float, str2bool, str2list, str2args]
+cast = StringCaster(*casts)
+
+if __name__ == '__main__':
+  import sys
+  for arg in sys.argv[1:]:
+    print cast(arg)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyloader/invoke.py	Tue May 10 09:28:40 2011 -0700
@@ -0,0 +1,11 @@
+import inspect
+
+def call(obj, *args, **kwargs):
+    """call a thing with string arguments"""
+    inspected = obj
+    if inspect.isclass(obj):
+        inspected = obj.__init__ # inspect the ctor
+    argspec = inspect.getargspec(inspected)
+    
+
+    
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyloader/loader.py	Tue May 10 09:28:40 2011 -0700
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+
+"""
+load modules and their attributes from a string
+"""
+
+import imp
+import os
+import sys
+
+def import_dotted_path(module):
+  path = module.split('.')
+  module = __import__(module)
+  for name in path[1:]:
+    module = getattr(module, name)
+  return module
+
+def load(string):
+  """loads a string and returns a python object"""
+
+  try:
+    if ':' in string:
+      path, target = string.split(':', 1)
+      if os.path.isabs(path) and os.path.exists(path):
+        # path to file
+        sys.path.append(os.path.dirname(path))
+        module = imp.load_source(path, path)
+        sys.path.pop()
+      else:
+        module = import_dotted_path(path)
+      obj = module
+      while '.' in target:
+        attr, target = target.split('.', 1)
+        obj = getattr(obj, attr)
+      obj = getattr(obj, target)
+      return obj
+    else:
+      # module: dotted path
+      return import_dotted_path(string)
+  except:
+    print string
+    raise
+
+  # TODO: entry points
+
+if __name__ == '__main__':
+  import sys
+  for i in sys.argv[1:]:
+    print load(i)
--- a/setup.py	Tue May 03 14:26:35 2011 -0700
+++ b/setup.py	Tue May 10 09:28:40 2011 -0700
@@ -7,7 +7,7 @@
 except IOError: 
     description = ''
 
-version = "0.1.1"
+version = "0.1.2"
 dependencies = []
 
 setup(name='pyloader',