wvdial_importer.py :  » Network » Chestnut-Dialer » chestnut-dialer-0.3.3 » chestnut_dialer » importers » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Chestnut Dialer 
Chestnut Dialer » chestnut dialer 0.3.3 » chestnut_dialer » importers » wvdial_importer.py
# Copyright (C) 2003 Konstantin Korikov

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

importer_class = "WvdialImporter"
    
import ConfigParser
import re
import locale
import importer
import chestnut_dialer.account_set
from chestnut_dialer import _

try: default_encoding = locale.getpreferredencoding()
except: default_encoding = 'ASCII'

class Account(chestnut_dialer.account_set.Account):
  _acc_conf = None
  _sections = None
  _id = None
  _acc_name = None
  _wvdial_attrs = {
    "speed": "Baud",    
    "dial_cmd": "Dial Command",
    "chat_login_prompt": "Login Prompt",
    "passwd": "Password",
    "ask_passwd": "Ask Password",
    "chat_passwd_prompt": "Password Prompt",
    "ip": "Force Address",
    "remotename": "Remote Name",
    "redial_auto": "Auto Reconnect",
    "redial_attempts": "Dial Attempts",
    "dial_prefix": "Dial Prefix"}
  _encoding = None
  def __init__(self, acc_conf, sections, acc_id, acc_name, encoding):
    chestnut_dialer.account_set.Account.__init__(self)
    self._acc_conf = acc_conf
    self._id = acc_id
    self._sections = sections[:]
    self._sections.reverse()
    self._acc_name = acc_name
    self._encoding = encoding
  def _get_acc_attr(self, name):
    acc_conf = self._acc_conf
    sections = self._sections
    wvdial_attrs = self._wvdial_attrs
    def get_value(option):
      for s in sections:
        sec = (u"Dialer " + s).encode(self._encoding)
        op = option.encode(self._encoding)
        if acc_conf.has_option(sec, op):
    return unicode(acc_conf.get(sec, op), self._encoding)
      return None
    def get_bool_value(option):
      v = get_value(option)
      if v == None: return None
      return (v.lower() in ("on", "yes", "true")) and 1 or 0
    def get_int_value(option):
      v = get_value(option)
      if v == None: return None
      return int(v)
    def get_str_value(option):
      v = get_value(option)
      return v
    if name == "id": return self._id
    if name == "name": return self._acc_name or sections[0]
    if name == "auth_type": return u"pap/chap"
    if name == "use_script":
      return (get_bool_value("Stupid Mode") and
          "predef-noterm" or "predef-auto")
    if name == "user":
      v = get_value("Username")
      if v == None: v = get_value("Login")
      if v == None: v = u""
      return v 
    if name == "phone_numbers":
      l = []
      for i in ("Phone", "Phone1", "Phone2", "Phone3", "Phone4"):
  v = get_value(i)
  if v: l.append(v)
      return l
    if name == "dns_servers":
      if get_bool_value("Auto DNS"): return []
      l = []
      for i in ("DNS Test1", "DNS Test2"):
  v = get_value(i)
  if v: l.append(v)
      return l or None
    if name == "init_cmd":
      for i in ("Init", "Init1"):
        v = get_value(i)
  if v != None: return v
      return None
    if name == "init2_cmd":
      for i in range(2, 9):
        v = get_value("Init%d" % i)
  if v != None: return v
      return None
    if wvdial_attrs.has_key(name):
      if name in ("redial_auto", "ask_passwd"):
        return get_bool_value(wvdial_attrs[name])
      if name == "redial_attempts":
        v = get_int_value(wvdial_attrs[name])
        return v == 0 and 99 or v      
      return get_str_value(wvdial_attrs[name])
    return None

class WvdialImporter(chestnut_dialer.account_set.AccountSet):
  __doc__ = _("Import accounts from wvdial")
  author = _("Konstantin Korikov")
  constructor_args = (
    { "name": "file_name",
      "type": "filename",
      "display": _("Source File"),
      "default":  "/etc/wvdial.conf"},
    { "name": "sections",
      "type": "string",
      "display": _("Join sections"),
      "default":  "",
      "description": _("""List of white space separated sections
for join to one account. If empty all sections will be displayed.""")},
    { "name": "encoding",
      "type": "string",
      "display": _("Encoding"),
      "default": default_encoding})
  program = "wvdial"
  acc_conf = None
  sections = None
  acc_sec_list = None
  encoding = None
  def __init__(self, file_name, sections, encoding):
    self.acc_conf = ConfigParser.ConfigParser()
    try: self.acc_conf.read(file_name)
    except: pass
    if not self.acc_conf.sections():
      raise importer.ConstructImporterException(
          _("The source file is empty or broken"))
    self.sections = []
    for s in sections.split(" "):
      if s == "": continue
      if not self.acc_conf.has_section(
          (u"Dialer " + s).encode(encoding)):
        raise importer.ConstructImporterException(
          _("No such section: %s") % s)
      self.sections.append(s)
    if not self.sections:
      self.acc_sec_list = []
      regexp = re.compile(r'^Dialer (\w+)$')
      for section in self.acc_conf.sections():
        sec = unicode(section, encoding)
  m = regexp.match(sec)
  if m:
    self.acc_sec_list.append(m.group(1))
    self.encoding = encoding
  def ls_accounts(self):
    if not self.sections:
      accounts = []
      i = 0
      for s in self.acc_sec_list:
        accounts.append((s, i))
  i += 1
      return accounts
    return [(_("wvdial Account"), 0)]
  def get_account(self, account_id):
    if not self.sections:
      return Account(self.acc_conf, 
        [u"Defaults", self.acc_sec_list[account_id]], account_id,
        self.acc_sec_list[account_id], self.encoding)
    if account_id != 0: return None
    return Account(self.acc_conf, [u"Defaults"] + self.sections, 
      0, _("wvdial Account"), self.encoding)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.