Skip to content

Commit

Permalink
Minor refactoring of conf to simplify and modernize.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jun 18, 2024
1 parent a1336eb commit 2176b54
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions googlevoice/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import configparser
import os
import contextlib
import pathlib

from . import settings

Expand All @@ -10,22 +11,17 @@ class Config(configparser.ConfigParser):
``.gvoice`` and parses configuration data from it.
"""

def __init__(self, filename: str = os.path.expanduser('~/.gvoice')):
self.fname = filename
def __init__(self, filename: str = '~/.gvoice'):
self.fname = pathlib.Path(filename).expanduser()

if not os.path.exists(self.fname):
try:
with open(self.fname, 'w', encoding='utf-8') as f:
f.write(settings.DEFAULT_CONFIG)
except OSError:
return
if not self.fname.exists():
with contextlib.suppress(OSError):
self.fname.write_text(settings.DEFAULT_CONFIG, encoding='utf-8')

configparser.ConfigParser.__init__(self)

try:
with contextlib.suppress(OSError):
self.read([self.fname], encoding='utf-8')
except OSError:
return

def get(self, option, section='gvoice', **kwargs):
try:
Expand All @@ -47,7 +43,7 @@ def phoneType(self):
return

def save(self):
with open(self.fname, 'w', encoding='utf-8') as f:
with self.fname.open('w', encoding='utf-8') as f:
self.write(f)

forwardingNumber = property(lambda self: self.get('forwardingNumber'))
Expand Down

0 comments on commit 2176b54

Please sign in to comment.