Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tlvince/scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Vincent committed May 2, 2011
2 parents de1b7b4 + 9b67035 commit 37c4e74
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
6 changes: 5 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
Various multi-purpose Python scripts that are too small to warrant their own
Various multi-purpose scripts that are too small to warrant their own
repository. See individual files for usage descriptions.

This repository contains a branch per language.

This is the Python branch.
2 changes: 1 addition & 1 deletion chromium-update.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def download(remote, local):
def verifyDownload(fileSize, local):
"""Make some attempts to check the file was downloaded correctly."""
try:
assert os.stat(local).st_size == total
assert os.stat(local).st_size == fileSize
except AssertionError:
logging.warning("Downloaded file size does not match expect.")
logging.info("The file maybe corrupt or incomplete.")
Expand Down
20 changes: 3 additions & 17 deletions cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@
"""Setup cmd with some sane defaults."""

import os
import ctypes
import subprocess
import getpass
import sys
import logging

from urllib.request import getproxies_registry
from urllib.parse import urlparse

def getMyDocs():
"""Return the path to My Documents.
http://stackoverflow.com/questions/3927259/how-do-you-get-the-exact-path-to-my-documents/3927493#3927493"""
dll = ctypes.windll.shell32
buf = ctypes.create_unicode_buffer(300)
dll.SHGetSpecialFolderPathW(None, buf, 0x0005, False)
return buf.value

def setProxies(user, password, host, port):
"""Set proxy variables for the currently running shell."""
schemes = ["http", "https"]
Expand All @@ -34,12 +24,6 @@ def setProxies(user, password, host, port):
"{0}://{1}:{2}@{3}:{4}".format(base, user, password, host, port)
)

def postHook():
"""Perform some platform-specific post operations."""
if sys.platform == "win32":
cmd = ["cmd.exe", "/k", "cd", getMyDocs()]
subprocess.call(cmd)

def getProxyHost():
"""Return the proxy host and port from Windows registry."""
try:
Expand All @@ -60,7 +44,9 @@ def main():
port = input("Port: ")

setProxies(un, pw, host, port)
postHook()

# Start a child shell to inherit the new environment variables
subprocess.call(["cmd.exe", "/k", "title", "cmd"])

if __name__ == "__main__":
main()
33 changes: 24 additions & 9 deletions sanitise.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import argparse
import string
import unicodedata
import re

def removeAccents(str):
"""Remove any form of UTF-8 accents.
Expand All @@ -15,19 +16,33 @@ def removeAccents(str):
nkfd_form = unicodedata.normalize('NFKD', str)
return "".join([c for c in nkfd_form if not unicodedata.combining(c)])

def sanitise(str):
"""Perform substitutions and return the string."""
def regexSanitise(str):
"""Perform detailed sanitising substitutions using regex."""

str = removeAccents(str)
# List of (pattern, replacement) tuples
regex = [
("&", "and"), # Replace ampersand with a safe string
("( |_)", "-"), # See: http://webmasters.stackexchange.com/q/374
("(\.|-){2,}", "\\1"), # Flatten a series of two or more dots or dashes
("^-", ""), # Remove a leading dash
("(-$|\.$)", ""), # Remove a trailing dash or dot
]

for handler in regex:
pattern, replacement = handler
str = re.sub(pattern, replacement, str)

str = str.replace("&", "and")
str = str.replace("_", "-")
str = str.replace(" ", "-")
return str

valid = string.ascii_letters + string.digits + "-."
str = "".join([chr for chr in str if chr in valid])
def sanitise(str):
"""Perform substitutions and return the string."""
str = str.lower()
str = removeAccents(str)
str = regexSanitise(str)

return str.lower()
# Permit only letters, digits, dash (seperator) and dot (file extension)
valid = string.ascii_lowercase + string.digits + "-."
return "".join([chr for chr in str if chr in valid])

def parseArguments():
"""Parse the command-line arguments."""
Expand Down
13 changes: 11 additions & 2 deletions test/test-sanitise.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ def testAll():
"éèêàùçÇ": "eeeaucc",
"CRaZyCASE": "crazycase",
"Bill & Ted": "bill-and-ted",
"file.mp3": "file.mp3",
"file.MP3": "file.mp3",
".dotfile.txt": ".dotfile.txt",
"---heLLO": "hello",
"infix----dashes": "infix-dashes",
"---hello....txt.": "hello.txt",
}

i = 1
for case in cases:
assert sanitise(case) == cases[case]
expected = cases[case]
actual = sanitise(case)
print("({0}) {1}: {2} -> {3}".format(i, case, expected, actual))
assert expected == actual
i += 1

def main():
"""Start execution of test-sanitise."""
Expand Down

0 comments on commit 37c4e74

Please sign in to comment.