~arsenal-devel/upstreamer/trunk

« back to all changes in this revision

Viewing changes to Upstreamer/validation.py

  • Committer: Bryce Harrington
  • Date: 2011-11-30 23:17:45 UTC
  • Revision ID: bryce@canonical.com-20111130231745-lwkug9m4gz3r2y0f
Move bug upstreaming prototype code from arsenal here.

Change namespace of python code from arsenal to Upstreamer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
'''Routines for validating various data types'''
 
5
 
 
6
import re
 
7
 
 
8
def validate_number(num):
 
9
    if ( type(num) is int or
 
10
         type(num) is long or
 
11
         num.isdigit()):
 
12
        return True
 
13
    return False         
 
14
 
 
15
def validate_package_name(name):
 
16
    distro_regex = re.compile("^[\w-]+(?: \(\w+\)|)$")
 
17
    m = distro_regex.search(name)
 
18
    if m:
 
19
        return True
 
20
    return False
 
21
 
 
22
 
 
23
if __name__ == "__main__":
 
24
    valid_numbers = [
 
25
        1, 0, 100000000000000000000000000000000000000,
 
26
        '1', '0', '100000000000000000000000000000000000000',
 
27
        ]
 
28
    should_be_valid_numbers = [
 
29
        '-1', '+1', '1E23', '1.2E3.4', '-4E-321',
 
30
        ]
 
31
    invalid_numbers = [
 
32
        '', '1a', 'a', 'a1a',
 
33
        ]
 
34
    valid_package_names = [
 
35
        'xorg', 'xserver-xorg', 'xserver-xorg-video-intel (Ubuntu)',
 
36
        ]
 
37
    invalid_package_names = [
 
38
        'this is not a valid package name',
 
39
        'this isn\'t either',
 
40
        'zöe',
 
41
        '!valid',
 
42
        'no?',
 
43
        'xorg 1.2.3-4ubuntu6',
 
44
        ]
 
45
 
 
46
    print "Valid numbers:"
 
47
    for num in valid_numbers:
 
48
        print validate_number(num), num
 
49
    print
 
50
 
 
51
    print "Should be (TODO)"
 
52
    print
 
53
 
 
54
    print "Invalid numbers:"
 
55
    for num in invalid_numbers:
 
56
        print validate_number(num), num
 
57
    print
 
58
 
 
59
    print "Valid package names:"
 
60
    for name in valid_package_names:
 
61
        print validate_package_name(name), name
 
62
    print
 
63
    
 
64
    print "Invalid package names:"
 
65
    for name in invalid_package_names:
 
66
        print validate_package_name(name), name
 
67
    print
 
68