mekk.feeds / src / mekk / feeds / greader2org /

# -*- coding: utf-8 -*-

import os
import ConfigParser
import keyring
import getpass

AUTH_SECTION = 'google-auth'
FILE_SECTION = 'files'
FILE_OPTION = 'feeds_file'

KEYRING_ENTRY = 'greader2org'

class Settings(object):

    SETTINGS_DIR = os.path.abspath(os.path.expanduser("~/.feeds")) 

    def __init__(self, dir = SETTINGS_DIR):
        self._dir = dir
        if not os.path.exists(self._dir):

        self._ini_file = os.path.join(self._dir, "feeds.ini")

        config = ConfigParser.SafeConfigParser({})
        self._config = config

        if not config.has_section(AUTH_SECTION):
        if not config.has_section(FILE_SECTION): 
        if not config.has_option(AUTH_SECTION, USERNAME_OPTION):
        if not config.has_option(FILE_SECTION, FILE_OPTION):

        self._config = config

    def google_auth(self):
        login = self.google_login()
        password = self.google_password()
        if not password:
            return self.google_auth()
        return dict(login = login,
                    password = password)

    def org_file(self):
        name = self.org_file_name()
        if not name:
            name = self.org_file_name()
        return name

    def google_login(self):
        return self._config.get(AUTH_SECTION, USERNAME_OPTION)
    def google_password(self):
        return keyring.get_password(KEYRING_ENTRY, self.google_login())

    def org_file_name(self):
        return self._config.get(FILE_SECTION, FILE_OPTION)

    def query_feeds_location(self):
        self._query_option("Feeds file location",
                           FILE_SECTION, FILE_OPTION,
                           os.path.join(self._dir, "feeds.txt"))
        self._config.write(open(self._ini_file, 'w'))

    def query_google_credentials(self):
        login = self._query_option("Your Google account name",
                                   AUTH_SECTION, USERNAME_OPTION)
        if login.find('@') < 0:
            login = login + ''
            self._config.set(AUTH_SECTION, USERNAME_OPTION, login)
        self._config.write(open(self._ini_file, 'w'))

        self._query_password("Your Google password", login)

    def reconfigure(self):

    def _query_password(self, label, login):
        old_password = self.google_password()
        while True:
            if old_password:
                new_password = getpass.getpass("%s for %s (or Enter to keep old password): " % (label, login)).strip()
                if not new_password:
                    new_password = old_password
                new_password = getpass.getpass("%s for %s: " % (label, login)).strip()
            if new_password:
        keyring.set_password(KEYRING_ENTRY, login, new_password)

    def _query_option(self, label, section, option, default = None):
        config = self._config
        if config.has_option(section, option):
            prev_value = config.get(section, option)
            prev_value = default
        while True:
            if prev_value:
                new_value = raw_input("%s (or Enter to keep %s): " % (label, prev_value)).strip()
                if not new_value:
                    new_value = prev_value
                new_value = raw_input("%s: " % label).strip()
            if new_value:
        config.set(section, option, new_value)
        return new_value
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.