コード例 #1
0
class AccountManager:

    DEFAULT_ACCOUNT_ID = 0

    def __init__(self):
        self.__transmitter = Transmitter()
        self.__configpath  = self.__set_configpath()
        self.__config      = self.__init_config()
        self.__default_account = AccountManager.get_default_account()
        self.__accounts    = []
        self.__load_accounts_from_config()

    def pass_transmitter(self):
        return self.__transmitter

    def reset_config(self):
        self.__accounts = []
        if os.path.exists(self.__configpath):
            os.remove(self.__configpath)
        self.__config = self.__init_config()

    def get_accounts(self):
        return [self.__default_account] + self.__accounts

    def add_new_account(self, account):
        self.__verify_account(account)
        self.__set_section_id_and_load_account(account)
        self.__add_account_to_config(account)

    def get_recent_account(self):
        id = self.get_recent_id()
        id = int(id)
        return self.get_account_by_id(id)

    def get_recent_id(self):
        id = self.__config.get('DEFAULT', 'last_profile')
        return int(id) 

    def get_account_by_id(self, id):
        self.__record_recent_account(id)
        if id == 0:
            account = self.__default_account
        else:
            account = self.__accounts[id - 1]
        self.__transmitter.set_account(account) 
        return  account
    
    def delete_account_by_id(self, id):
        offset_id = id - 1
        del self.__accounts[offset_id]
        self.__renumber_accounts()


    def __set_section_id_and_load_account(self, account):
        account.set_section_id(self.__next_id())
        self.__accounts.append(account)



    def __load_accounts_from_config(self):
        for section in self.__config.sections():
            section_id = self.__next_id()
            url = self.__config.get(section, 'url')
            username = self.__config.get(section, 'username')
            try:
                password = self.__config.get(section, 'password')
                if password == '': password = None
            except NoOptionError:
                password = None
            new_account = Account(url, username, password)
            new_account.set_section_id(section_id)
            self.__set_section_id_and_load_account(new_account)

    def __verify_account(self, account):
        self.__transmitter.set_account(account)
        self.__transmitter.check_credentials()

    def __next_id(self):
        return len(self.__accounts) + 1

    def __add_account_to_config(self, account):
        id = int(account.get_section_id()) # This will throw an exception if it's not a number
        id = str(id)
        try:
            self.__config.add_section(id)
        except DuplicateSectionError:
            # No need to add it if it's already there
            message = 'a ok'

        self.__config.set(id, 'url', account.get_url())
        self.__config.set(id, 'username', account.get_username())
        if account.get_password():
            self.__config.set(id, 'password', account.get_password())
        self.__save_config_to_file()

    def __save_config_to_file(self):
        with open(self.__configpath, 'wb') as handle: 
            self.__config.write(handle)

    def __record_recent_account(self, id):
        self.__config.set('DEFAULT', 'last_profile', str(id))
        self.__save_config_to_file()

    def __renumber_accounts(self):
        counter = 1
        renumbered_accounts = []
        for account in self.get_accounts():
            account.set_section_id(counter)
            counter += 1 
            renumbered_accounts.append(account)
        self.__accounts = renumbered_accounts
    def __init_config(self):
        config = ConfigParser.ConfigParser()
        if not os.path.exists(self.__configpath):
            # Set up initial config file
            config.set('DEFAULT', 'delay', '1.0') # wait while showing author info
            config.set('DEFAULT', 'cut_flag', '#! CUT MATERIAL') # we remove anything after this in the html
            config.set('DEFAULT', 'last_profile', '0') # use as default input option
            config.set('DEFAULT', 'next_section_n', '1') # next usable profile section number
            with open(self.__configpath, 'wb') as f: config.write(f)
        config.read(self.__configpath)
        return config


    def __set_configpath(self):
        # Determine which operating system is in use
        platform = sys.platform
        linux_relative_path = "~/.lyxblogger/config.cfg"
        try:
            relative_configpath = {'win32' : "~\\lyxblogger\\config.cfg", 
                              'darwin' : "~/.lyxblogger/config.cfg",
                              'linux2' : linux_relative_path,
                              'linux3' : linux_relative_path}[platform]
        except KeyError:
            raise SystemNotFoundError(platform)
        configpath = os.path.expanduser(relative_configpath)
        configdir = os.path.dirname(configpath)
        if not os.path.exists(configdir): os.makedirs(configdir)
        return configpath

    @classmethod
    def get_default_account(cls):
        # we are not going to set the id, because we never save this
        url = 'blogtest.letseatalready.com'
        username = '******'
        password = '******'
        default_account = Account(url, username, password)
        default_account.set_section_id(AccountManager.DEFAULT_ACCOUNT_ID)
        return default_account
コード例 #2
0
class AccountManager:

    DEFAULT_ACCOUNT_ID = 0

    def __init__(self):
        self.__transmitter = Transmitter()
        self.__configpath = self.__set_configpath()
        self.__config = self.__init_config()
        self.__default_account = AccountManager.get_default_account()
        self.__accounts = []
        self.__load_accounts_from_config()

    def pass_transmitter(self):
        return self.__transmitter

    def reset_config(self):
        self.__accounts = []
        if os.path.exists(self.__configpath):
            os.remove(self.__configpath)
        self.__config = self.__init_config()

    def get_accounts(self):
        return [self.__default_account] + self.__accounts

    def add_new_account(self, account):
        self.__verify_account(account)
        self.__set_section_id_and_load_account(account)
        self.__add_account_to_config(account)

    def get_recent_account(self):
        id = self.get_recent_id()
        id = int(id)
        return self.get_account_by_id(id)

    def get_recent_id(self):
        id = self.__config.get('DEFAULT', 'last_profile')
        return int(id)

    def get_account_by_id(self, id):
        self.__record_recent_account(id)
        if id == 0:
            account = self.__default_account
        else:
            account = self.__accounts[id - 1]
        self.__transmitter.set_account(account)
        return account

    def delete_account_by_id(self, id):
        offset_id = id - 1
        del self.__accounts[offset_id]
        self.__renumber_accounts()

    def __set_section_id_and_load_account(self, account):
        account.set_section_id(self.__next_id())
        self.__accounts.append(account)

    def __load_accounts_from_config(self):
        for section in self.__config.sections():
            section_id = self.__next_id()
            url = self.__config.get(section, 'url')
            username = self.__config.get(section, 'username')
            try:
                password = self.__config.get(section, 'password')
                if password == '': password = None
            except NoOptionError:
                password = None
            new_account = Account(url, username, password)
            new_account.set_section_id(section_id)
            self.__set_section_id_and_load_account(new_account)

    def __verify_account(self, account):
        self.__transmitter.set_account(account)
        self.__transmitter.check_credentials()

    def __next_id(self):
        return len(self.__accounts) + 1

    def __add_account_to_config(self, account):
        id = int(account.get_section_id()
                 )  # This will throw an exception if it's not a number
        id = str(id)
        try:
            self.__config.add_section(id)
        except DuplicateSectionError:
            # No need to add it if it's already there
            message = 'a ok'

        self.__config.set(id, 'url', account.get_url())
        self.__config.set(id, 'username', account.get_username())
        if account.get_password():
            self.__config.set(id, 'password', account.get_password())
        self.__save_config_to_file()

    def __save_config_to_file(self):
        with open(self.__configpath, 'wb') as handle:
            self.__config.write(handle)

    def __record_recent_account(self, id):
        self.__config.set('DEFAULT', 'last_profile', str(id))
        self.__save_config_to_file()

    def __renumber_accounts(self):
        counter = 1
        renumbered_accounts = []
        for account in self.get_accounts():
            account.set_section_id(counter)
            counter += 1
            renumbered_accounts.append(account)
        self.__accounts = renumbered_accounts

    def __init_config(self):
        config = ConfigParser.ConfigParser()
        if not os.path.exists(self.__configpath):
            # Set up initial config file
            config.set('DEFAULT', 'delay',
                       '1.0')  # wait while showing author info
            config.set(
                'DEFAULT', 'cut_flag',
                '#! CUT MATERIAL')  # we remove anything after this in the html
            config.set('DEFAULT', 'last_profile',
                       '0')  # use as default input option
            config.set('DEFAULT', 'next_section_n',
                       '1')  # next usable profile section number
            with open(self.__configpath, 'wb') as f:
                config.write(f)
        config.read(self.__configpath)
        return config

    def __set_configpath(self):
        # Determine which operating system is in use
        platform = sys.platform
        linux_relative_path = "~/.lyxblogger/config.cfg"
        try:
            relative_configpath = {
                'win32': "~\\lyxblogger\\config.cfg",
                'darwin': "~/.lyxblogger/config.cfg",
                'linux2': linux_relative_path,
                'linux3': linux_relative_path
            }[platform]
        except KeyError:
            raise SystemNotFoundError(platform)
        configpath = os.path.expanduser(relative_configpath)
        configdir = os.path.dirname(configpath)
        if not os.path.exists(configdir): os.makedirs(configdir)
        return configpath

    @classmethod
    def get_default_account(cls):
        # we are not going to set the id, because we never save this
        url = 'blogtest.letseatalready.com'
        username = '******'
        password = '******'
        default_account = Account(url, username, password)
        default_account.set_section_id(AccountManager.DEFAULT_ACCOUNT_ID)
        return default_account
コード例 #3
0
 def test_check_credentials_bad_password(self):
     tt = Transmitter()
     tt.set_account(self.account_with_bad_password)
     self.assertEquals(tt.check_credentials(), 'username/password error') 
コード例 #4
0
 def test_check_credentials_no_password(self):
     tt = Transmitter()
     tt.set_account(self.account_with_no_password)
     self.assertEquals(tt.check_credentials(), True) 
コード例 #5
0
 def test_check_credentials_bogus(self):
     tt = Transmitter()
     tt.set_account(self.bogus_account)
     self.assertEquals(tt.check_credentials(), 'host not found')