Dynamically maintain local_constants.py from South migration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def set_constant(file, constant, value, comment = ''):
    '''
    Search through file for the constant and either adds it to the end or 
    updates the existing value. Comment is optionally passed and is placed on
    the same line as the constant along with the modification date/time.
    Comment is only updated if the value is updated.
    
    It's recommended that the file for local_constants.py be different than the
    file checked into your repo as it's locally maintained.
    
    Example: set_constant('a.txt', 'TYPE', 1, 'This is optional')
    
    Author: Ed Menendez (ed@menendez.com)
    '''
    write_file = found_constant = False
    
    if comment:
        comment += ' '
    
    line_to_write = '%s = %s  # %sLast modified %s\n' % \
                    (constant, value.__repr__(), comment, datetime.datetime.now())
    
    # The file to be written is stored here.
    new_file = ''
    
    try:
        f = open(file, 'r+')
    except IOError:
        # Nothing to search through here. Move along.
        f = None
    else:
        # Loop through the file and look for the string
        for line in f.readlines():
            if line.find(constant) > -1:
                line_splits = line.split('=')
                
                if len(line_splits) == 2:
                    # Yes, it's an assignment line.
                    old_constant, old_value = line_splits
                    old_constant = old_constant.strip()
                
                    if old_constant == constant:
                        # We've found the contant!
                        found_constant = True
                        old_value_splits = old_value.split('#')
                        
                        if len(old_value_splits) == 2:
                            old_value, trash = old_value_splits
                        else:
                            old_value = old_value_splits[0]
                        
                        old_value = old_value.strip()
                    
                        if old_value != value.__repr__():
                            # It's changed!
                            write_file = True
                            line = line_to_write
            
            new_file += line

        f.close()

    # If nothing found, then add it to the end
    if not found_constant:
        write_file = True
        # New line needs to be added in case the file doesn't end with a new
        # line. Could be made smarter and check for the newline. This will make
        # for a prettier local_constants.py
        new_file += '\n' + line_to_write
    
    if write_file:
        # Write out the new file
        f = open(file, 'w+')
        f.write(new_file)
        f.close()

More like this

  1. Transparently encrypt ORM fields using OpenSSL (via M2Crypto) by ncoghlan 2 years, 9 months ago
  2. db_dump.py - for dumpping and loading data from database by limodou 7 years, 1 month ago
  3. Proper fixtures loading in south data migrations by JustDelight 1 year, 1 month ago
  4. post_migrate handler to load initial SQL after migrating with south by stingydrone 3 years, 3 months ago
  5. "Partial Templates" - an alternative to "include" by vigrid 5 years, 2 months ago

Comments

(Forgotten your password?)