License New BSD license
Lines 74
Keywords
example (2) import (3) proof-of-concept (1) siafoo (2)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More

Import Python straight off Siafoo Atom Feed 1

In Brief This is the core of a package to import Python snippets straight off Siafoo. Once imported a module is cached. For example, if the module is called 'siafoo_importer' you can do:... more
# 's
 1# Copyright 2009 David Isaacson
2# Modified BSD License
3
4import imp
5import os
6import re
7import sys
8from urllib2 import urlopen
9
10import cache
11cache_loc = os.path.dirname(os.path.abspath(cache.__file__))
12
13def import_module(name):
14 module = __import__(name)
15 if '.' in name:
16 for segment in name.split('.')[1:]:
17 module = getattr(module, segment)
18 return module
19
20class SiafooImporter(object):
21
22 #Importer
23 def find_module(self, fullname, path=None):
24 #print 'find module', fullname, path
25 if path != __path__:
26 return
27
28 start_path = '.'.join(path) + '.'
29 if not re.search('^%ss\d+(r\d+)?$' % start_path, fullname):
30 return
31
32 return self # return the obj with load_module
33
34 # Loader
35 def load_module(self, fullname):
36 #print 'load module', fullname
37 # TODO: remove from sys.modules if errors
38 # TODO: throw import error on errs
39
40 mod_name = fullname.split('.')[-1]
41 print mod_name
42 match = re.search(r's(\d+)(r(\d+))?', mod_name)
43 id = int(match.group(1))
44 rev = match.group(3) and int(match.group(3)) or None
45
46 try:
47 # See if we have it cached
48 return __import__(self._mod_name(id, rev), cache.__dict__) # This means essentially, import cache.s###r###
49 except ImportError:
50 pass
51
52 code = self._fetch_module(id, rev)
53 # From PEP 302, be careful changing the rest of this function
54 mod = sys.modules.setdefault(fullname, imp.new_module(fullname)) #__name__ set automatically
55 mod.__file__ = "<%s>" % self.__class__.__name__
56 mod.__loader__ = self
57 #if ispkg:
58 # mod.__path__ = []
59 exec code in mod.__dict__
60 return mod
61
62 def _fetch_module(self, id, rev=None):
63 #TODO: throw importerror if errors
64 if rev is None:
65 url = 'http://www.siafoo.net/snippet/%i/download' % id
66 else:
67 url = 'http://www.siafoo.net/snippet/%i/rev/%i/download' % (id, rev)
68
69 code = urlopen(url).read()
70 file = open(os.path.join(cache_loc, self._mod_name(id,rev)), 'wb')
71 file.write(code)
72 file.close()
73 return code
74
75 def _mod_name(self, id, rev=None):
76 if rev is None:
77 return 's%i' % id
78 return 's%ir%i' % (id, rev)
79
80sys.meta_path.append(SiafooImporter())

This is the core of a package to import Python snippets straight off Siafoo. Once imported a module is cached. For example, if the module is called 'siafoo_importer' you can do:

# 's
1>>> from siafoo_importer import s1
2Hello World!

As you can see, you import the virtual module 's' and then the snippet number. You can also require a specific version of a snippet with an additional 'r' and the snippet revision. For example, from siafoo_importer import s1r1.

This is just a proof of concept and is uber-alpha. There isn't even a setup.py to globally install it, you have to be in the packages parent directory for it to work! But please, improve on it. I can set up an SVN if anyone wants, it might be easier than trading around a tarball.

But for now, you can download the whole package or build it. You want a folder hierarchy like

  • siafoo_importer
    • __init__.py (this file)

    • cache
      • __init__.py (blank)

Enjoy!