Skip to content

Instantly share code, notes, and snippets.

@m-r-r
Created December 30, 2011 12:05
Show Gist options
  • Save m-r-r/1539530 to your computer and use it in GitHub Desktop.
Save m-r-r/1539530 to your computer and use it in GitHub Desktop.
mozillaprofiles
#!/usr/bin/env python
# -*- coding: utf-8 -*- #
import os
try:
from ConfigParser import ConfigParser
except:
from configparser import ConfigParser
"""
This module allow to find and parse the `profiles.ini' files of the mozilla applications:
>>> from mozillaprofiles import *
>>> find_profiles_ini('thunderbird')
'/home/skami/.thunderbird/profiles.ini'
The parser can parse an ini file:
>>> ini_file = '/home/skami/.mozilla/firefox/profiles.ini'
>>> parser = ProfilesIni(ini_file)
It can also find the ini file by itself:
>>> parser = ProfileIni(app='firefox')
Supported applications are `firefox', `thunderbird' & `seamonkey'.
You can get a list containing all the profiles of an application:
>>> parser.profiles()
[{'default': False,
'isrelative': true,
'name': 'default',
'path': '/home/skami/.mozilla/firefox/p4ebz6jk.default'}] #There is only 1 profile here
You can find the last profile use:
>>> parser.last_profile()
[{'default': False,
'isrelative': true,
'name': 'default',
'path': '/home/skami/.mozilla/firefox/p4ebz6jk.default'}]
This can be useful for accessing data of the currently used profile:
>>> bookmark_file = parser.last_profile()['path'] + '/bookmarks.html'
Disclamer:
This module *should* work on GNU/Linux, Windows & Mac OS, but i don't
have Windows nor Mac OS, so test it carefully if you use those platforms...
"""
class ProfilesFileError(Exception):
"""Raised when the profile file is not found or not readable"""
pass
_PROFILES_INI_PATHS = {
'firefox' : [
'~/.mozilla/firefox/profiles.ini',
'~/Library/Mozilla/Firefox/Profiles/profiles.ini',
'~/Library/Application Support/Firefox/Profiles/profiles.ini',
"%APPDATA%\\Mozilla\\Firefox\\Profiles\\profiles.ini"
],
'thunderbird' : [
"~/.thunderbird/profiles.ini",
"~/.mozilla-thunderbird/profiles.ini",
'~/Library/Thunderbird/Profiles/profiles.ini',
'~/Library/Application Support/Thunderbird/Profiles/profiles.ini',
"%APPDATA%\\Thunderbird\\Profiles\\profiles.ini"
],
'seamonkey' : [
"~/.mozilla/seamonkey/profiles.ini",
'~/Library/Thunderbird/Profiles/profiles.ini',
'~/Library/Application Support/Thunderbird/Profiles/profiles.ini',
"%APPDATA%\\Mozilla\\SeaMonkey\\Profiles\\profiles.ini"
]
}
def find_profiles_ini(app='firefox'):
"""Find the profiles.ini file for the given application"""
if app not in _PROFILES_INI_PATHS.keys():
raise TypeError('Argument #1 must be in ' + str(_PROFILES_INI_PATHS.keys()))
paths = list()
for path in _PROFILES_INI_PATHS[app]:
path = os.path.expanduser(path)
path = os.path.expandvars(path)
if os.path.isfile(path):
return path
class ProfilesIni(object):
def __init__(self, path=None, app=None):
"""Create a new `profiles.ini' parser"""
self._ini = ConfigParser()
if app:
path = find_profiles_ini(app)
self._ini.read([path])
if not 'General' in self._ini.sections():
raise ProfileFileError('invalid profiles file {0}'.format(path))
self._cwd = os.path.abspath(os.path.dirname(path))
def _get_profiles(self):
profiles = list()
for section in self._ini.sections():
if section in ('General', 'general'):
continue
try:
profile = dict(self._ini.items(section))
profile['isrelative'] = bool(profile.get('isrelative'))
profile['default'] = bool(profile.get('default'))
if profile['isrelative']:
profile['path'] = os.path.join(self._cwd, profile['path'])
yield profile
except:
raise ProfileFileError('invalid section {0}'.format(sectionh))
def profiles(self):
"""Return all the profiles referenced by the profiles.ini file"""
return list(self._get_profiles())
def last_profile(self):
"""Return the last used profile or None"""
profiles = list(self._get_profiles())
default = None
if len(profiles) is 1:
default = profiles[0]
else:
for profile in profiles:
if bool(profile.get('default')):
deflault = profile
return default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment