Login

Dynamically maintain local_constants.py from South migration

Author:
menendez
Posted:
March 14, 2010
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

Allows you to dynamically maintain a local_constants.py file from a migration tool like South. Example of usage:

set_constant('/home/projects/sample/local_constants.py', 'STAMP_MW_ID', 42, 'Set from sample.0007_add_constants.py')

More more information, see [Allows you to dynamically maintain a local_constants.py file from a migration tool like South. Example of usage:

set_constant('/home/projects/sample/local_constants.py', 'STAMP_MW_ID', 42, 'Set from sample.0007_add_constants.py')

More more information, see http://menendez.com/blog/maintain-contants-through-south-data-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 ([email protected])
    '''
    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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.