<commit_before>from config import hostname, port, username, password
import webdavlib
import urllib
import base64


class HTTPPreferencesPOST (webdavlib.HTTPPOST):
  cookie = None
  
  def prepare_headers (self):
    headers = webdavlib.HTTPPOST.prepare_headers(self)
    if self.cookie:
      headers["Cookie"] = self.cookie
    return headers

class HTTPPreferencesGET (webdavlib.HTTPGET):
  cookie = None
  
  def prepare_headers (self):
    headers = webdavlib.HTTPGET.prepare_headers(self)
    if self.cookie:
      headers["Cookie"] = self.cookie
    return headers



class preferences:
  login = username
  passw = password

  def __init__ (self, otherLogin = None, otherPassword = <PASSWORD>):
    if otherLogin and otherPassword:
      self.login = otherLogin
      self.passw = <PASSWORD>Password

    self.client = webdavlib.WebDAVClient(hostname, port,
                                         self.login, self.passw)

    authString = \
        base64.urlsafe_b64encode (username+":"+password).replace("=", "%3D")
    self.cookie = \
        "0xHIGHFLYxSOGo=basic%%20%s;%%20SOGo=6C0F6C0F014AC60ED5" % authString

    self.preferencesMap = {"language": "2.1.0.3.0.1.3.1.1.3.1.2"}

  def set (self, preference, value):
    formKey = self.preferencesMap[preference]
    content = "%s=%s" % (formKey, value)
    url = "/SOGo/so/cyril/preferences"

    post = HTTPPreferencesPOST (url, content)
    post.content_type = "application/x-www-form-urlencoded"
    post.cookie = self.cookie

    self.client.execute (post)

    # Raise an exception if the language wasn't properly set
    if post.response["status"] != 200:
      raise Exception ("failure setting language, (code = %d)" \
                       % post.response["status"])

  def get (self, preference):
    url = "/SOGo/so/cyril/preferences/jsonDefaults"
    get = HTTPPreferencesGET (url)
    get.cookie = self.cookie
    self.client.execute (get)
    content = eval (get.response['body'])
    return content[preference]


# Simple main to test this class
if __name__ == "__main__":
  p = preferences ()
  p.set ("language", 6)<commit_msg>Removed hardcoded value<commit_after>from config import hostname, port, username, password
import webdavlib
import urllib
import base64


class HTTPPreferencesPOST (webdavlib.HTTPPOST):
  cookie = None
  
  def prepare_headers (self):
    headers = webdavlib.HTTPPOST.prepare_headers(self)
    if self.cookie:
      headers["Cookie"] = self.cookie
    return headers

class HTTPPreferencesGET (webdavlib.HTTPGET):
  cookie = None
  
  def prepare_headers (self):
    headers = webdavlib.HTTPGET.prepare_headers(self)
    if self.cookie:
      headers["Cookie"] = self.cookie
    return headers



class preferences:
  login = username
  passw = password

  def __init__ (self, otherLogin = None, otherPassword = None):
    if otherLogin and otherPassword:
      self.login = otherLogin
      self.passw = <PASSWORD>Password

    self.client = webdavlib.WebDAVClient(hostname, port,
                                         self.login, self.passw)

    authString = \
        base64.urlsafe_b64encode (username+":"+password).replace("=", "%3D")
    self.cookie = \
        "0xHIGHFLYxSOGo=basic%%20%s;%%20SOGo=6C0F6C0F014AC60ED5" % authString

    self.preferencesMap = {"language": "2.1.0.3.0.1.3.1.1.3.1.2"}

  def set (self, preference, value):
    formKey = self.preferencesMap[preference]
    content = "%s=%s" % (formKey, value)
    url = "/SOGo/so/%s/preferences" % self.login

    post = HTTPPreferencesPOST (url, content)
    post.content_type = "application/x-www-form-urlencoded"
    post.cookie = self.cookie

    self.client.execute (post)

    # Raise an exception if the language wasn't properly set
    if post.response["status"] != 200:
      raise Exception ("failure setting language, (code = %d)" \
                       % post.response["status"])

  def get (self, preference):
    url = "/SOGo/so/%s/preferences/jsonDefaults" % self.login
    get = HTTPPreferencesGET (url)
    get.cookie = self.cookie
    self.client.execute (get)
    content = eval (get.response['body'])
    return content[preference]


# Simple main to test this class
if __name__ == "__main__":
  p = preferences ()
  p.set ("language", 6)
{'fraction_non_alphanumeric': 0.1182846371347785, 'fraction_numerical': 0.017907634307257305, 'mean_word_length': 3.0935390549662487, 'pattern_counts': {'":': 6, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2316166', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1348, 'n_words': 315}