Jump to content

User:ClemRutter/Toolbox/python

From Wikipedia, the free encyclopedia

DetectOS

[edit]

A custom written filter to take gridrefs from the List of mills in .... pages, check that they haven't been processed and then to insert the Sat Nav code after them.

#!python
#DetectOS.py a test framwork to deteCt and replace patternms in a file
#using the cat filemame |python DetectOS >filename.out
#the source file:filename is a copy of the WP edit box (Ctrl-A, Ctrl C, open file on gedit Ctrl-V)
#fileout.out is gedited- Ctrl-A, Ctrl C into the WP edit box


import sys
import re
import geo_helper #See http://gagravarr.org/code/ for updates and information
import osgb #This must be in the directory path: imported from www.agapow.net/programming/python/osgb.py a later version is available from http://pypi.python.org/pypi/osgb
#Declaring variables used in testing
mill_name = "Aqueduct Mill"
mill_lat = "53.4066"
mill_long = "-2.0675"

#Defining regex patterns  
pattern = "[A-Z]{2}[ ]?\d\d\d[ ]?\d\d\d"
done_pattern = "coord"
name_pattern = "{{TMtr[|]([A-Za-z' ]+)"

#Compiling regex patterns
regexp = re.compile(pattern)
done_regexp = re.compile(done_pattern)
name_regexp = re.compile(name_pattern)

def convert_function(match):
    OSgrid = match.group(0)
    os36_height = 0
    os36_long, os36_lat=osgb.osgb_to_lonlat(OSgrid) #from osgb.py
    wgs_list=geo_helper.turn_osie36_into_wgs84( os36_lat, os36_long, os36_height)
    wgs_lat=wgs_list[0]
    wgs_long=wgs_list[1]
    GEOtag = "{{Coord|" +str("%.04f" % wgs_lat)+ "|" + str("%.04f" % wgs_long) + "|display=inline|type:landmark_region:GB|format=dms|name="+ mill_name + "}}"
    return OSgrid + " "+ GEOtag

#main loop
for line in sys.stdin:
    result = regexp.search(line) and not done_regexp.search(line)
    if result:
        mill_name_result = name_regexp.search(line)
        mill_name =  mill_name_result.group(1)
        # print mill_name debugging command
        line = regexp.sub(convert_function, line)
        print line
    else:
        print line,
#line matched
    #sys.stdout.write(line) //job done by print which calls it