Selaa lähdekoodia

Configured folders now should end with a slash (/).

Taddeus Kroes 14 vuotta sitten
vanhempi
sitoutus
c6c4738449
2 muutettua tiedostoa jossa 39 lisäystä ja 19 poistoa
  1. 23 11
      cache.py
  2. 16 8
      importer.py

+ 23 - 11
cache.py

@@ -8,7 +8,7 @@ from hashlib import md5
 from copy import copy
 
 
-ADMINISTRATION_FILE = 'administration.py'
+ADMINISTRATION_FILE = 'administration.pickle'
 
 
 def to_dir(path):
@@ -34,7 +34,7 @@ class Cache:
     a concatenation of the file list.
     """
 
-    def __init__(self, root='', files=[], cached='cached',
+    def __init__(self, root='', files=[], cached='cached/',
             expires={'days': 365}):
         """
         The constructor takes the following arguments (all are optional):
@@ -45,19 +45,24 @@ class Cache:
         4. A dictionary containing arguments to the timedelta constructor, that
            indicates how long the cache object sould live.
         """
-        self.root = to_dir(root + '/')
-        self.cached = to_dir(cached + '/')
-        self.files = map(lambda f: self.root + f, files)
+        self.root = to_dir(root)
+        self.cached = to_dir(cached)
+        self.files = []
         self.expires = expires
 
+        for f in files:
+            self.add(f)
+
+        web.debug(str(self))
+
         self.assert_files_exist()
 
     def assert_files_exist(self):
         map(assert_file_exists, self.files)
 
     def __str__(self):
-        return '<Cache filename=%s files=[%s]>' \
-               % (self.filename(), ','.join(self.files))
+        return '<Cache root=%s filename=%s files=[%s]>' \
+               % (self.root, self.filename(), ','.join(self.files))
 
     def add(self, path, absolute=False):
         """
@@ -68,6 +73,7 @@ class Cache:
         if not absolute:
             path = self.root + path
 
+        web.debug('Adding file "%s"...' % path)
         assert_file_exists(path)
         self.files.append(path)
 
@@ -169,7 +175,8 @@ class Cache:
         admin = self.load_administration()
 
         if not exists(path):
-            web.debug('Cached file "%s" does not exist yet, generating it...')
+            web.debug('Cached file "%s" does not exist yet, generating it...' \
+                      % path)
             server_modified = True
         else:
             server_modified = False
@@ -191,14 +198,19 @@ class Cache:
             f.close()
 
         try:
-            web.http.modified(seconds_to_datetime(last_modified), self.etag())
-            web.http.expires(timedelta(**self.expires))
+            # Set headers
             web.header('Cache-Control', 'private')
 
+            if not server_modified:
+                web.http.modified(seconds_to_datetime(last_modified),
+                                  self.etag())
+
+            web.http.expires(timedelta(**self.expires))
+
             if not server_modified:
                 # Concatenated content has not been loaded yet, read the cached
                 # file
-                web.debug('Cached file "%s" already exists, sending content...')
+                web.debug('Cached file "%s" already exists...' % path)
                 f = open(path, 'r')
                 content = f.read()
                 f.close()

+ 16 - 8
importer.py

@@ -5,7 +5,7 @@ import os.path
 from cache import Cache, to_dir, assert_file_exists
 
 
-ALIAS_PREFIX = '@'
+ALIAS_PREFIX = '.'
 
 
 def separate_imports(content):
@@ -37,8 +37,9 @@ def separate_imports(content):
     content = content.lstrip()
     imports = []
 
+    filename = '[a-zA-Z0-9\._-]+'
     comma = re.compile(' *, *(?:\\\\\r?\n *)?')
-    file_list = '\w+(?:%s\w+?)*' % comma.pattern
+    file_list = '%s(?:%s%s?)*' % (filename, comma.pattern, filename)
     m = re.match('^import (%s)(?:(?:\r?\n)+(.*))?$' % file_list, content, re.M)
 
     if m:
@@ -52,9 +53,9 @@ def separate_imports(content):
 
 
 class Importer(Cache):
-    def __init__(self, root, extension=None):
-        Cache.__init__(self)
-        self.root = to_dir(root + '/')
+    def __init__(self, files=[], extension=None, **kwargs):
+        Cache.__init__(self, **kwargs)
+
         self.aliases = {}
 
         if extension:
@@ -62,7 +63,14 @@ class Importer(Cache):
         else:
             self.extension = ''
 
-    def set_alias(self, name, path):
+        for f in files:
+            self.add(f)
+
+    def add(self, path):
+        path = self.create_full_paths([path])[0]
+        Cache.add(self, path, absolute=True)
+
+    def set_alias(self, alias, path):
         """
         Add an alias for the path to a file, relative to the importer's root
         directory. Aliases are used to shorten the names of imports. E.g. using
@@ -70,7 +78,8 @@ class Importer(Cache):
         """
         path = self.root + path + self.extension
         assert_file_exists(path)
-        self.aliases[name] = path
+        self.aliases[alias] = path
+        web.debug('Added alias "%s" for path "%s".' % (alias, path))
 
     def create_full_paths(self, files, relative_file=None):
         """
@@ -112,7 +121,6 @@ class Importer(Cache):
         return replaced
 
     def concatenate(self, files):
-        files = self.create_full_paths(files)
         map(assert_file_exists, files)
         self.loaded = []
         self.import_map = {}