def edit(alias, setting, value): """Modify settings for a follow. The following settings can be edited: alias, directory. """ series = db.Series.alias_lookup(alias, unfollowed=True) alias = series.alias if value.lower() == 'none' or value.lower() == '-': value = None if setting == 'alias': series.alias = value elif setting == 'directory': series.directory = value else: setting = click.style(setting, bold=True) output.error('Invalid setting {}'.format(setting)) exit(1) if not value: value = click.style('none', dim=True) else: value = click.style(value, bold=True) try: db.session.commit() except exceptions.DatabaseIntegrityError: db.session.rollback() output.error('Illegal value {}'.format(value)) exit(1) else: output.chapter('Changed {} for {} to {}'.format(setting, alias, value))
def login(self): if not self.username: username = click.prompt('Batoto username') else: username = self.username if not self.password: password = click.prompt('Batoto password', hide_input=True) else: password = self.password r = requests.get(self.forum_url, params=self.login_query) auth_key = re.search(r"'auth_key' value='(.+)'", r.text).group(1) data = {'auth_key': auth_key, 'referer': 'http://bato.to/', 'ips_username': username, 'ips_password': password, 'rememberMe': 1} r = requests.post(self.forum_url, params=self.login_query, data=data) try: self.cookie = r.cookies['session_id'] self.member_id = r.cookies['member_id'] self.pass_hash = r.cookies['pass_hash'] except KeyError: output.error('Batoto: invalid login') exit(1) self._config.write()
def test_database(): """Runs a database sanity test.""" sanity_tester = sanity.DatabaseSanity(Base, engine) sanity_tester.test() if sanity_tester.errors: for error in sanity_tester.errors: err_target, err_msg = str(error).split(' ', 1) message = ' '.join([click.style(err_target, bold=True), err_msg]) output.warning(message) output.error('Database has failed sanity check; ' 'run `cum repair-db` to repair database') exit(1)
def alias_lookup(alias): """Returns a DB object for a series by alias name. Prints an error if an invalid alias is specified. """ try: s = (session.query(Series) .filter_by(alias=alias, following=True) .one()) except NoResultFound: output.error('Could not find alias "{}"'.format(alias)) exit(1) else: return s
def alias_lookup(alias, unfollowed=False): """Returns a DB object for a series by alias name. Prints an error if an invalid alias is specified. """ filters = {'alias': alias} if not unfollowed: filters['following'] = not unfollowed try: s = session.query(Series).filter_by(**filters).one() except NoResultFound: output.error('Could not find alias "{}"'.format(alias)) exit(1) else: return s
def download(self): auth = requests.auth.HTTPBasicAuth(*config.get().madokami.login) r = requests.get(self.url, auth=auth, stream=True) total_length = r.headers.get('content-length') if not r.headers.get('content-disposition'): output.error('Madokami: invalid login') exit(1) with open(self.filename, 'wb') as f: if total_length is None: f.write(r.content) else: total_length = int(total_length) with self.progress_bar(total_length) as bar: for chunk in r.iter_content(chunk_size=4096): if chunk: bar.update(len(chunk)) f.write(chunk) f.flush()
def config_command(mode, setting, value): """Get or set configuration options. Mode can be either "get" or "set", depending on whether you want to read or write configuration values. If mode is "get", you can specify a setting to read that particular setting or omit it to list out all the settings. If mode is "set", you must specify the setting to change and assign it a new value. """ if mode == 'get': if setting: parameters = setting.split('.') value = config.get() for parameter in parameters: try: value = getattr(value, parameter) except AttributeError: output.error('Setting not found') exit(1) output.configuration({setting: value}) else: configuration = config.get().serialize() output.configuration(configuration) elif mode == 'set': if setting is None: output.error('You must specify a setting') exit(1) if value is None: output.error('You must specify a value') exit(1) parameters = setting.split('.') preference = config.get() for parameter in parameters[0:-1]: try: preference = getattr(preference, parameter) except AttributeError: output.error('Setting not found') exit(1) try: current_value = getattr(preference, parameters[-1]) except AttributeError: output.error('Setting not found') exit(1) if current_value is not None: if isinstance(current_value, bool): if value.lower() == 'false' or value == 0: value = False else: value = True else: try: value = type(current_value)(value) except ValueError: output.error('Type mismatch: value should be {}'.format( type(current_value).__name__)) exit(1) setattr(preference, parameters[-1], value) config.get().write() else: output.error('Mode must be either get or set') exit(1)
def config_command(mode, setting, value): """Get or set configuration options. Mode can be either "get" or "set", depending on whether you want to read or write configuration values. If mode is "get", you can specify a setting to read that particular setting or omit it to list out all the settings. If mode is "set", you must specify the setting to change and assign it a new value. """ if mode == 'get': if setting: parameters = setting.split('.') value = config.get() for parameter in parameters: try: value = getattr(value, parameter) except AttributeError: output.error('Setting not found') exit(1) output.configuration({setting: value}) else: configuration = config.get().serialize() output.configuration(configuration) elif mode == 'set': if setting is None: output.error('You must specify a setting') exit(1) if value is None: output.error('You must specify a value') exit(1) parameters = setting.split('.') preference = config.get() for parameter in parameters[0:-1]: try: preference = getattr(preference, parameter) except AttributeError: output.error('Setting not found') exit(1) try: current_value = getattr(preference, parameters[-1]) except AttributeError: output.error('Setting not found') exit(1) if current_value is not None: try: value = type(current_value)(value) except ValueError: output.error('Type mismatch: value should be {}' .format(type(current_value).__name__)) exit(1) setattr(preference, parameters[-1], value) config.get().write() else: output.error('Mode must be either get or set') exit(1)