Permalink
Browse files

Move filetypes onto configuration file

The filetypes can be extended using a configuration files, where
is possible to associate file type and its corresponden regular
expression.

The code includes a script to test the regex without running
gitdm.

Signed-off-by: Germán Póo-Caamaño <gpoo@gnome.org>
  • Loading branch information...
gpoo committed Jun 24, 2011
1 parent 1a85ace commit b2fd0c693997b1aea57800870d13fdf52474610a
Showing with 445 additions and 408 deletions.
  1. +40 −0 ConfigFile.py
  2. +18 −0 README
  3. +19 −0 database.py
  4. +0 −406 file_types.py
  5. +1 −2 gitdm
  6. +5 −0 gitdm.config
  7. +362 −0 sample-config/filetypes.txt
View
@@ -122,6 +122,43 @@ def ReadVirtual (file, name):
#
croak ('Missing "end" line for virtual employer %s' % (name))
+#
+# Read file type patterns for more fine graned reports
+#
+def ReadFileType (filename):
+ try:
+ file = open (filename, 'r')
+ except IOError:
+ croak ('Unable to open file type mapping file %s' % (filename))
+ patterns = {}
+ order = []
+ regex_order = re.compile ('^order\s+(.*)$')
+ regex_file_type = re.compile ('^filetype\s+(\S+)\s+(.+)$')
+ line = ReadConfigLine (file)
+ while line:
+ o = regex_order.match (line)
+ if o:
+ # Consider only the first definition in the config file
+ elements = o.group(1).replace (' ', '')
+ order = order or elements.split(',')
+ line = ReadConfigLine (file)
+ continue
+
+ m = regex_file_type.match (line)
+ if not m or len (m.groups ()) != 2:
+ ConfigFile.croak ('Funky file type line "%s"' % (line))
+ if not patterns.has_key (m.group (1)):
+ patterns[m.group (1)] = []
+ if m.group (1) not in order:
+ print '%s not found, appended to the last order' % m.group (1)
+ order.append (m.group (1))
+
+ patterns[m.group (1)].append (re.compile (m.group (2), re.IGNORECASE))
+
+ line = ReadConfigLine (file)
+ file.close ()
+ return patterns, order
+
#
# Read an overall config file.
#
@@ -146,6 +183,9 @@ def ConfigFile (name, confdir):
ReadGroupMap (os.path.join (confdir, sline[1]), sline[2])
elif sline[0] == 'VirtualEmployer':
ReadVirtual (file, ' '.join (sline[1:]))
+ elif sline[0] == 'FileTypeMap':
+ patterns, order = ReadFileType (os.path.join (confdir, sline[1]))
+ database.FileTypes = database.FileType (patterns, order)
else:
croak ('Unrecognized config line: "%s"' % (line))
line = ReadConfigLine (file)
View
18 README
@@ -145,6 +145,24 @@ end
for example, no check to ensure that the percentages add up to
something rational.
+FileTypeMap file
+
+ Map file names/extensions onto file types. These files contain lines
+ like:
+
+ order <type1>,<type2>,...,<typeN>
+
+ filetype <type> <regex>
+ ...
+
+ This construct allows fine graned reports by type of contribution
+ (build, code, image, multimedia, documentation, etc.)
+
+ Order is important because it is possible to have overlapping between
+ filenames. For instance, ltmain.sh fits better as 'build' instead of
+ 'code' (the filename instead of '\.sh$'). The first element in order
+ has precedence over the next ones.
+
OTHER TOOLS
View
@@ -188,6 +188,25 @@ def store (self):
# Should check that they add up too, but I'm lazy
Employers[self.name] = self
+class FileType:
+ def __init__ (self, patterns={}, order=[]):
+ self.patterns = patterns
+ self.order = order
+
+ def guess_file_type (self, filename, patterns=None, order=None):
+ patterns = patterns or self.patterns
+ order = order or self.order
+
+ for file_type in order:
+ if patterns.has_key (file_type):
+ for patt in patterns[file_type]:
+ if patt.search (filename):
+ return file_type
+
+ return 'unknown'
+
+FileTypes = None
+
#
# Mix all the virtual employers into their real destinations.
#
Oops, something went wrong.

0 comments on commit b2fd0c6

Please sign in to comment.