<commit_before>

from urllib2 import Request, urlopen, HTTPError
from urllib import urlencode

import logging, time, base64

from urlparse import urlunparse

from elementtree import ElementTree


def http_get(url, username='', password=''):
  "Do an HTTP Get with optional Basic authorization"
  request = Request(url)
  if username:
    auth_string = base64.encodestring(username + ':' + password).replace('\n', '')
    request.add_header('Authorization', 'Basic ' + auth_string)
  response = urlopen(request)
  return response

def http_post(url, username='', password='', data={}):
  request = Request(url, urlencode(data))
  if username:
    auth_string = base64.encodestring(username + ':' + password).replace('\n', '')
    request.add_header('Authorization', 'Basic ' + auth_string)
  response = urlopen(request)
  return response
  

class V1Error(Exception): pass

class V1AssetNotFoundError(V1Error): pass


class V1Server(object):
  "Accesses a V1 HTTP server as a client of the XML API protocol"
  
  def __init__(self, address='localhost', instance='VersionOne.Web', username='', password=''):
    self.address = address
    self.instance = instance    
    self.username = username
    self.password = password
    
  def build_url(self, path, query='', fragment='', params='', port=80):
    path = self.instance + path
    if isinstance(query, dict):
      query = urlencode(query)
    url = urlunparse( ('http', self.address, path, params, query, fragment) )
    return url
    
  def get(self, path, query='', postdata=None):
    url = self.build_url(path, query=query)
    try:
      if postdata is not None:
        response = http_post(url, self.username, self.password, postdata)
      else:
        response = http_get(url, self.username, self.password)
      body = response.read()
      return (None, body)
    except HTTPError, e:
      body = e.fp.read()
      return (e, body)
      
  def get_xml(self, path, query='', postdata=None):
    exception, body = self.get(path, query=query, postdata=postdata)
    document = ElementTree.fromstring(body)
    if exception:
      exception.xmldoc = document
      if exception.code == '404':
        raise V1AssetNotFoundError(exception)
      else:
        ElementTree.dump(exception.xmldoc)
        raise V1Error(exception)
    return document
   
  def get_asset_xml(self, asset_type_name, oid):
    path = '/rest-1.v1/Data/{0}/{1}'.format(asset_type_name, oid)
    return self.get_xml(path)
    
  def get_query_xml(self, asset_type_name, where):
    path = '/rest-1.v1/Data/{0}'.format(asset_type_name)
    whereclause = urlencode({"Where": where})
    path = path + '?' + whereclause
    return self.get_xml(path)
    
  def get_meta_xml(self, asset_type_name):
    path = '/meta.v1/{0}'.format(asset_type_name)
    return self.get_xml(path)
    
  def execute_operation(self, asset_type_name, oid, opname):
    path = '/rest-1.v1/Data/{0}/{1}'.format(asset_type_name, oid)
    query = {'op': opname}
    return self.get_xml(path, query=query, postdata={})        
    

      
      
      
  
      
    <commit_msg>Canonicalize query string for queries<commit_after>

from urllib2 import Request, urlopen, HTTPError
from urllib import urlencode

import logging, time, base64

from urlparse import urlunparse

from elementtree import ElementTree


def http_get(url, username='', password=''):
  "Do an HTTP Get with optional Basic authorization"
  print url
  request = Request(url)
  if username:
    auth_string = base64.encodestring(username + ':' + password).replace('\n', '')
    request.add_header('Authorization', 'Basic ' + auth_string)
  response = urlopen(request)
  return response

def http_post(url, username='', password='', data={}):
  request = Request(url, urlencode(data))
  if username:
    auth_string = base64.encodestring(username + ':' + password).replace('\n', '')
    request.add_header('Authorization', 'Basic ' + auth_string)
  response = urlopen(request)
  return response
  

class V1Error(Exception): pass

class V1AssetNotFoundError(V1Error): pass


class V1Server(object):
  "Accesses a V1 HTTP server as a client of the XML API protocol"
  
  def __init__(self, address='localhost', instance='VersionOne.Web', username='', password=''):
    self.address = address
    self.instance = instance    
    self.username = username
    self.password = password
    
  def build_url(self, path, query='', fragment='', params='', port=80):
    path = self.instance + path
    if isinstance(query, dict):
      query = urlencode(query)
    url = urlunparse( ('http', self.address, path, params, query, fragment) )
    return url
    
  def get(self, path, query='', postdata=None):
    url = self.build_url(path, query=query)
    try:
      if postdata is not None:
        response = http_post(url, self.username, self.password, postdata)
      else:
        response = http_get(url, self.username, self.password)
      body = response.read()
      return (None, body)
    except HTTPError, e:
      body = e.fp.read()
      return (e, body)
      
  def get_xml(self, path, query='', postdata=None):
    exception, body = self.get(path, query=query, postdata=postdata)
    document = ElementTree.fromstring(body)
    if exception:
      exception.xmldoc = document
      if exception.code == '404':
        raise V1AssetNotFoundError(exception)
      else:
        ElementTree.dump(exception.xmldoc)
        raise V1Error(exception)
    return document
   
  def get_asset_xml(self, asset_type_name, oid):
    path = '/rest-1.v1/Data/{0}/{1}'.format(asset_type_name, oid)
    return self.get_xml(path)
    
  def get_query_xml(self, asset_type_name, where):
    path = '/rest-1.v1/Data/{0}'.format(asset_type_name)
    whereclause = urlencode({"Where": where})
    return self.get_xml(path, query=whereclause)
    
  def get_meta_xml(self, asset_type_name):
    path = '/meta.v1/{0}'.format(asset_type_name)
    return self.get_xml(path)
    
  def execute_operation(self, asset_type_name, oid, opname):
    path = '/rest-1.v1/Data/{0}/{1}'.format(asset_type_name, oid)
    query = {'op': opname}
    return self.get_xml(path, query=query, postdata={})        
    

      
      
      
  
      
    
{'fraction_non_alphanumeric': 0.12634971796937952, 'fraction_numerical': 0.010314262691377921, 'mean_word_length': 3.417081850533808, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1730518', 'n_tokens_mistral': 2005, 'n_tokens_neox': 2000, 'n_words': 480}