diff licenser/licenses.py @ 27:7e0c931a201d

web licenser now works
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 25 Nov 2011 00:57:38 -0800
parents 0faf3e7b593a
children
line wrap: on
line diff
--- a/licenser/licenses.py	Fri Nov 25 00:08:34 2011 -0800
+++ b/licenser/licenses.py	Fri Nov 25 00:57:38 2011 -0800
@@ -56,6 +56,16 @@
     def __init__(self, filename):
         self.filename = filename
 
+    def fileobj(self):
+        """return a file object open for writing"""
+        if isinstance(self.filename, basestring):
+            return file(self.filename, 'w')
+        return self.filename
+
+    def close(self, _file):
+        if isinstance(self.filename, basestring):
+            _file.close()
+
     def isempty(self):
         return not bool(file(self.filename).read().strip)
 
@@ -71,12 +81,12 @@
 
     def __call__(self, license):
 
+        lines = self.lines()
         if self.isempty():
             return # you're done
-        lines = self.lines()
 
         # open the file for writing
-        f = file(self.filename, 'w')
+        f = self.fileobj()
 
         # print the license
         license_lines = license.splitlines()
@@ -93,7 +103,7 @@
         # print the rest of the file
         for line in lines:
             f.write(line)
-        f.close()
+        self.close(f)
 
 
 class HashCommentsFile(CommentedFile):
@@ -103,12 +113,12 @@
     def __call__(self, license):
         """interpolate the file"""
 
+        lines = self.lines()
         if self.isempty():
             return # you're done
-        lines = self.lines()
 
         # open the file for writing
-        f = file(self.filename, 'w')
+        f = self.fileobj()
 
         # print shebang if it exists
         if lines[0].startswith('#!'):
@@ -123,7 +133,7 @@
         # print the rest of the file
         for line in lines:
             f.write(line)
-        f.close()
+        self.close(f)
 
     def isempty(self):
         """
@@ -171,7 +181,11 @@
     def has_license(self, filename):
         """does the file already have a license?"""
         token = self.token()
-        for line in file(filename).readlines():
+        if isinstance(filename, basestring):
+            f = file(filename)
+        else:
+            f = filename
+        for line in f.readlines():
             if token in line:
                 return True
         return False
@@ -183,6 +197,7 @@
     def obtain_variables(self, **kw):
         for var in self.variables:
             if var not in kw:
+                import pdb; pdb.set_trace()
                 print 'Enter %s: ' % var,
                 kw[var] = raw_input()
         self.pre(kw)
@@ -210,13 +225,16 @@
             return # you're done
 
         # get the license
-        # XXX should be cached
-        license = self.license()
-        license = Template(license).substitute(**variables)
+        license = self.interpolate_license(**variables)
 
         # add the license to the file
         filetype(license)
 
+
+    def interpolate_license(self, **variables):
+        license = self.license()
+        return Template(license).substitute(**variables)
+
     def interpolate(self, directory, variables):
         for _file in self.files(directory):
             self.interpolate_file(_file, variables)