def loadCfg(self): cfg = RawConfigParser() cfg.read('./projects/' + self.name + '/config.ini', encoding='utf-8') self.StopWords = eval( cfg.get('Project', 'StopWords', fallback='tuple()')) self.jobs = [] if cfg.getboolean('Tasks', 'LikeHashtagFeed', fallback=False): self.jobs += [self.like_hashtag_feed_job] if cfg.getboolean('Tasks', 'LikeGeotagFeed', fallback=False): self.jobs += [self.like_geotag_feed_job] if cfg.getboolean('Tasks', 'SaveStats', fallback=False): self.jobs += [self.save_stats_job] self.like_hashtag_amount = int( eval(cfg.get('Tasks', 'LikeHashtagAmount', fallback='700 / 24'))) self.like_hashtag_period = int( eval(cfg.get('Tasks', 'LikeHashtagPeriod', fallback='60'))) self.like_geotag_amount = int( eval(cfg.get('Tasks', 'LikeGeotagAmount', fallback='700 / 24'))) self.like_geotag_period = int( eval(cfg.get('Tasks', 'LikeGeotagPeriod', fallback='60'))) self.save_stats_period = int( eval(cfg.get('Tasks', 'SaveStatsPeriod', fallback='120'))) self.uname = cfg.get('Main', 'uname') self.upass = cfg.get('Main', 'upass') self.like_random_tag = False
def read_config(self, configpath): parser = RawConfigParser({ 'query': self.query_path, 'quote_plus': 'on' if self.quote_fn is quote_plus else 'off', 'site': self.site, 'tlds': ' '.join(self.tld_list), 'hash_prefix_numbers': 'on' if self.hash_num else 'off'}) parser.read(configpath) section = self.site if parser.has_section(section): # we allow a single level of indirection to support limited # aliases. Inheritance is not currently supported if parser.has_option(section, 'alias'): self.site = parser.get(section, 'alias') return self.read_config(configpath) self.query_path = parser.get(section, 'query') self.quote_fn = (quote_plus if parser.getboolean(section, 'quote_plus') else quote) tlds = parser.get(section, 'tlds') self.tld_list = tlds.replace(',', ' ').split() self.hash_num = parser.getboolean(section, 'hash_prefix_numbers') self.site = parser.get(section, 'site')
class ReadConfig: """读取配置文件""" def __init__(self): self.config = RawConfigParser() self.config.read(contants.global_conf_file, encoding='utf-8') #先读取global.conf文件 switch = self.config.getboolean( 'switch', 'on') #global.conf文件中的switch如果是true就去读取online.conf,相反就去读取test.conf if switch: self.config.read(contants.online_conf_file, encoding='utf-8') else: self.config.read(contants.test_conf_file, encoding='utf-8') def get_str(self, section, option): return self.config.get(section, option) def get_int(self, section, option): return self.config.getint(section, option) def get_float(self, section, option): return self.config.getfloat(section, option) def get_bool(self, section, option): return self.config.getboolean(section, option) def write(self, section, option, value): self.config.set(section, option, value) with open(contants.test_conf_file, 'w') as f: self.config.write(f)
def _read_configuration_file(self, path): """Try to read and parse `path` as a configuration file. If the configurations were illegal (checked with `self._validate_options`), raises `IllegalConfiguration`. Returns (options, should_inherit). """ parser = RawConfigParser(inline_comment_prefixes=('#', ';')) options = None should_inherit = True if parser.read(path) and self._get_section_name(parser): all_options = self._parser.option_list[:] for group in self._parser.option_groups: all_options.extend(group.option_list) option_list = dict([(o.dest, o.type or o.action) for o in all_options]) # First, read the default values new_options, _ = self._parse_args([]) # Second, parse the configuration section_name = self._get_section_name(parser) for opt in parser.options(section_name): if opt == 'inherit': should_inherit = parser.getboolean(section_name, opt) continue if opt.replace('_', '-') not in self.CONFIG_FILE_OPTIONS: log.warning("Unknown option '{}' ignored".format(opt)) continue normalized_opt = opt.replace('-', '_') opt_type = option_list[normalized_opt] if opt_type in ('int', 'count'): value = parser.getint(section_name, opt) elif opt_type == 'string': value = parser.get(section_name, opt) else: assert opt_type in ('store_true', 'store_false') value = parser.getboolean(section_name, opt) setattr(new_options, normalized_opt, value) # Third, fix the set-options options = self._fix_set_options(new_options) if options is not None: if not self._validate_options(options): raise IllegalConfiguration('in file: {}'.format(path)) return options, should_inherit
def download_data(data: Data, config: RawConfigParser): """Downloads information about subject and documents and stores them into database. :param data: data object :type data: Data :param config: config fro :param config: RawConfigParser :return: """ base_url = config.get('downloader', 'base_url') search_url = config.get('downloader', 'search_url') download_extract = config.getboolean('downloader', 'download_extract') download_documents = config.getboolean('downloader', 'download_documents') documents_type = config.get('downloader', 'documents_type') documents_dir = config.get('downloader', 'documents_dir') limit = config.getint('downloader', 'limit_day') for i in range(0, limit): subject = data.get_not_downloaded() if subject == None: break ico = subject['ico'] url = urljoin(base_url, search_url + ico) bs = _open_url(url) extract_link = bs.find('a', text='Výpis platných') # find extract link if extract_link is None: data.update_failed(ico) continue extract_url = urljoin(base_url, extract_link.get('href')) documents_url = urljoin( base_url, bs.find('a', text='Sbírka listin').get('href')) # find documents link if download_extract: capital_base, insolvency = _parse_extract(extract_url) else: capital_base = None insolvency = None if download_documents: documents = _download_documents(base_url, documents_url, documents_type, documents_dir, ico) else: documents = None data.update_downloaded(ico, capital_base, insolvency, documents)
def __init__(self): self.__read_from_course_id('./courseid') cf = RawConfigParser() cf.read('config') self.username = cf.get('info', 'username') self.password = cf.get('info', 'password') self.runtime = cf.getint('info', 'runtime') self.debug = cf.getboolean('action', 'debug') self.enroll = cf.getboolean('action', 'enroll') self.evaluate = cf.getboolean('action', 'evaluate') self.select_bat = cf.getboolean('action', 'select_bat') self.watch_logo = cf.getboolean('action', 'watch_logo') self.loginPage = 'http://sep.ucas.ac.cn' self.loginUrl = self.loginPage + '/slogin' self.courseSystem = self.loginPage + '/portal/site/226/821' self.courseBase = 'http://jwxk.ucas.ac.cn' self.courseLogin = self.courseBase + '/login' self.courseIdentify = self.courseBase + '/login?Identity=' self.courseSelected = self.courseBase + '/courseManage/selectedCourse' self.courseSelectionBase = self.courseBase + '/courseManage/main' self.courseCategory = self.courseBase + '/courseManage/selectCourse?s=' self.courseSave = self.courseBase + '/courseManage/saveCourse?s=' # deptIds=913&sids=9D5ACABA58C8DF02 # deptIds=913&sids=2585B359205108D6&did_2585B359205108D6=2585B359205108D6 self.studentCourseEvaluateUrl = 'http://jwjz.ucas.ac.cn/Student/DeskTopModules/' self.selectCourseUrl = 'http://jwjz.ucas.ac.cn/Student/DesktopModules/Course/SelectCourse.aspx' self.enrollCount = {} self.headers = { #'Host': 'jwxk.ucas.ac.cn', 'Host': 'sep.ucas.ac.cn', 'Connection': 'keep-alive', # 'Pragma': 'no-cache', # 'Cache-Control': 'no-cache', #'Cache-Control': 'max-age=0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Upgrade-Insecure-Requests': '1', #'User-Agent': header_store[-5], 'User-Agent': header_store[-3], 'Accept-Encoding': 'gzip, deflate', #'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7', 'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6' } # self.headers = None self.s = requests.Session() self.s.get(self.loginPage, headers=self.headers)
def __init__(self): self.__readCoursesId() cf = RawConfigParser() cf.read('config', encoding='utf-8') self.username = cf.get('info', 'username') self.password = cf.get('info', 'password') self.enroll = cf.getboolean('action', 'enroll') self.evaluate = cf.getboolean('action', 'evaluate') self.idle = cf.get('idle', 'time') print('Hi, username: '******'Password: '******'*' * len(self.password)) self.loginPage = 'http://sep.ucas.ac.cn' self.loginUrl = self.loginPage + '/slogin' self.courseSystem = self.loginPage + '/portal/site/226/821' self.courseBase = 'http://jwxk.ucas.ac.cn' self.courseIdentify = self.courseBase + '/login?Identity=' self.courseSelected = self.courseBase + '/courseManage/selectedCourse' # self.courseSelectionBase = self.courseBase + '/courseManage/main' self.courseSelectionBaseFirst = 'http://sep.ucas.ac.cn/portal/site/226/821' self.courseSelectionBase = self.courseBase + '/notice/view/1' self.courseCategory = self.courseBase + '/courseManage/selectCourse?s=' self.courseSave = self.courseBase + '/courseManage/saveCourse?s=' self.studentCourseEvaluateUrl = 'http://jwjz.ucas.ac.cn/Student/DeskTopModules/' self.selectCourseUrl = 'http://jwjz.ucas.ac.cn/Student/DesktopModules/Course/SelectCourse.aspx' self.evaluateIndex = 'http://jwxk.ucas.ac.cn/evaluate/52576' self.evaluateBase = 'http://jwxk.ucas.ac.cn/evaluate/evaluate/' self.evaluateSave = 'http://jwxk.ucas.ac.cn/evaluate/save/{}?s={}' self.merit = cf.get('comment', 'merit') self.flaw = cf.get('comment', 'flaw') self.suggest = cf.get('comment', 'suggest') self.enrollCount = {} self.evaluateCount = {} self.headers = { 'Host': 'sep.ucas.ac.cn', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6', } self.s = requests.Session() loginPage = self.s.get(self.loginPage, headers=self.headers) self.cookies = loginPage.cookies
def _read_configuration_file(self, path): """Try to read and parse `path` as a configuration file. If the configurations were illegal (checked with `self._validate_convention`), raises `IllegalConfiguration`. Returns (arguments, should_inherit). """ parser = RawConfigParser(inline_comment_prefixes=("#", ";")) arguments = None should_inherit = True if parser.read(path) and parser.has_section( ConfigurationParser.SECTION_NAME): all_arguments = self._parser._get_optional_actions() argument_list = {arg.dest: arg.type for arg in all_arguments} # First, read the default values new_arguments = self._parse_args([]) # Second, parse the configuration section_name = ConfigurationParser.SECTION_NAME for arg in parser.options(section_name): if arg == "inherit": should_inherit = parser.getboolean(section_name, arg) continue if arg.replace("_", "-") not in self.CONFIG_FILE_ARGUMENTS: log.warning("Unknown option '{}' ignored".format(arg)) continue normalized_arg = arg.replace("-", "_") arg_type = argument_list[normalized_arg] if arg_type is int: value = parser.getint(section_name, arg) elif arg_type == str: value = parser.get(section_name, arg) else: assert arg_type is bool value = parser.getboolean(section_name, arg) setattr(new_arguments, normalized_arg, value) # Third, fix the set-arguments arguments = self._fix_set_arguments(new_arguments) if arguments is not None: if not self._validate_convention(arguments): raise IllegalConfiguration("in file: {}".format(path)) return arguments, should_inherit
def load(paths=(), extra_config=None, ignore_missing_paths=True): config = ConfigParser() for section, values in INITIAL_CONFIG.items(): config.add_section(section) for key, data in values.items(): config.set(section, key, data["value"]) if extra_config: for section, values in extra_config.items(): for key, value in values.items(): config.set(section, key, value) for path in paths: if path or not ignore_missing_paths: try: if not config.read(path) and not ignore_missing_paths: raise RuntimeError("No such file: %r" % path) except Exception as e: raise RuntimeError("Failed to load config file %r: %s" % (path, e)) from e # Check the configuration for section in config.sections(): if section == "headers": continue if section not in INITIAL_CONFIG: raise RuntimeError("Invalid section %r in config" % section) allow_extra_options = ("type" in INITIAL_CONFIG[section] and config.get(section, "type") not in INITIAL_CONFIG[section]["type"].get( "internal", ())) for option in config[section]: if option not in INITIAL_CONFIG[section]: if allow_extra_options: continue raise RuntimeError("Invalid option %r in section %r in " "config" % (option, section)) type_ = INITIAL_CONFIG[section][option]["type"] try: if type_ == bool: config.getboolean(section, option) else: type_(config.get(section, option)) except Exception as e: raise RuntimeError( "Invalid %s value for option %r in section %r in config: " "%r" % (type_.__name__, option, section, config.get(section, option))) from e # Add internal configuration config.add_section("internal") for key, data in INTERNAL_CONFIG.items(): config.set("internal", key, data["value"]) return config
class BCMConfig(): """ Reads configuration information from bcm.ini. Args: username (str): bricklink username password (str): bricklink password wantedfilename (str): path of the wanted list pricefilename (str): path of the previously scrubbed price list reloadpricesfromweb (bool): if true, download and parse all of the price data again and save it to pricefilename. _parser (SafeConfigParser): parser that reads the config file _configfile (str): relative path of the config file """ def __init__(self): # _parser = SafeConfigParser() self._configfile = '../bcm.ini' self._parser = RawConfigParser() self._parser.read(self._configfile) self.username = self._parser.get('bricklink', 'username') self.password = self._parser.get('bricklink', 'password') self.wantedfilename = self._parser.get('filenames', 'wanted') self.pricefilename = self._parser.get('filenames', 'prices') self.reloadpricesfromweb = self._parser.getboolean('options', 'reloadpricesfromweb')
def parse_options(): parser = argparse.ArgumentParser(description='Crypt4GH filesystem') parser.add_argument('mountpoint', help='mountpoint for the Crypt4GH filesystem') parser.add_argument('--conf', help='configuration file', default='~/.c4gh/fs.conf') parser.add_argument('-f', '--foreground', action='store_true', help='do not deamonize and keep in the foreground', default=False) args = parser.parse_args() # Mountpoint mountpoint = os.path.expanduser(args.mountpoint) if not os.path.exists(mountpoint): raise ValueError(f'Mountpoint {mountpoint} does not exist') # Load configuration file conf_file = os.path.expanduser(args.conf) check_perms_ok(conf_file) conf = RawConfigParser(converters={ 'barlist': lambda value: set(value.split('|')), # bar-separated 'set': lambda value: set(value.split(',')), }) conf.read([conf_file], encoding='utf-8') # Logging log_level = conf.get('DEFAULT', 'log_level', fallback=None) include_crypt4gh_log = conf.getboolean('DEFAULT', 'include_crypt4gh_log', fallback=False) if log_level is not None: load_logger(log_level, include_crypt4gh=include_crypt4gh_log) return (mountpoint, conf, args.foreground)
class BaseConfig(object, metaclass=Singleton): CONFIG_FILE = "" ALLOW_NO_VALUE = True def __init__(self): if self.__class__ is __class__: raise NotImplementedError file = os.path.normpath(os.path.abspath(self.__class__.CONFIG_FILE)) if not os.path.exists(file): raise FileNotFoundError("config file was not found: %s" % file) self._config = RawConfigParser( allow_no_value=self.__class__.ALLOW_NO_VALUE) self._config.read(file, encoding="utf-8-sig") # 必须显示指明 encoding def get(self, section, key): return self._config.get(section, key) def getint(self, section, key): return self._config.getint(section, key) def getfloat(self, section, key): return self._config.getfloat(section, key) def getboolean(self, section, key): return self._config.getboolean(section, key)
def test_default_realbool(self): """ Test if default boolean variable is returned if given as bool Note: In the configuration file, a boolean has to be given as string, so the string should be converted to a boolean. For the wrapper a boolean value has to work as well. """ config = RawConfigParser() configtext = """ [test] """ config.read_string(configtext) with self.assertRaises(NoOptionError): _ = config.getboolean("test", "var1") wrap = ConfigWrapper( config, { "var1": { "default": False, "description": "this is just a test for integer var1" } }) var1 = wrap.getboolean("test", "var1") print(f"From ConfigWrapper -> var1: {var1}") self.assertIsInstance(var1, bool) self.assertEqual(var1, False)
def test_existing_bool(self): """Test if float variable given in config file is correctly returned""" config = RawConfigParser() configtext = """ [test] var1 = true """ config.read_string(configtext) var1 = config.getboolean("test", "var1") print(f"From RawConfigParser -> var1: {var1}") self.assertEqual(var1, True) wrap = ConfigWrapper( config, { "var1": { "default": "false", "description": "this is just a test for float var1" } }) var1 = wrap.getboolean("test", "var1") print(f"From ConfigWrapper -> var1: {var1}") self.assertIsInstance(var1, bool) self.assertEqual(var1, True)
def build_rss_feed(comic_info: RawConfigParser, comic_data_dicts: List[Dict]): global cdata_dict if not comic_info.getboolean("RSS Feed", "Build RSS feed"): return if "GITHUB_REPOSITORY" not in os.environ: raise ValueError( "Set GITHUB_REPOSITORY in your environment variables before building your RSS feed locally" ) register_namespace("atom", "http://www.w3.org/2005/Atom") register_namespace("dc", "http://purl.org/dc/elements/1.1/") root = ElementTree.Element("rss") root.set("version", "2.0") channel = ElementTree.SubElement(root, "channel") # Build comic URL repo_author, repo_name = os.environ["GITHUB_REPOSITORY"].split("/") comic_url = "https://{}.github.io/{}/".format(repo_author, repo_name) add_base_tags_to_channel(channel, comic_url, comic_info) add_image_tag(channel, comic_url, comic_info) for comic_data in comic_data_dicts: add_item(channel, comic_data, comic_url, comic_info) pretty_string = pretty_xml(root) # Replace CDATA manually, because XML is stupid and I can't figure out how to insert raw text pretty_string = pretty_string.format(**cdata_dict) with open("feed.xml", 'wb') as f: f.write(bytes(pretty_string, "utf-8"))
def build_rss_feed(comic_info: RawConfigParser, comic_data_dicts: List[Dict]): global cdata_dict if not comic_info.getboolean("RSS Feed", "Build RSS feed"): return register_namespace("atom", "http://www.w3.org/2005/Atom") register_namespace("dc", "http://purl.org/dc/elements/1.1/") root = ElementTree.Element("rss") root.set("version", "2.0") channel = ElementTree.SubElement(root, "channel") # Build comic URL comic_url, _ = get_comic_url(comic_info) add_base_tags_to_channel(channel, comic_url, comic_info) add_image_tag(channel, comic_url, comic_info) for comic_data in comic_data_dicts: add_item(channel, comic_data, comic_url, comic_info) pretty_string = pretty_xml(root) # Replace CDATA manually, because XML is stupid and I can't figure out how to insert raw text pretty_string = pretty_string.format(**cdata_dict) with open("feed.xml", 'wb') as f: f.write(bytes(pretty_string, "utf-8"))
def getbool(self, s, v, default=None): if self.has_option(s, v): return RawConfigParser.getboolean(self, s, v) elif default is None: logging.error('Section: %s - Values: %s: required (expected bool).' % (s, v)) else: return default
def read_file(self): here = "config.read_file" config_read = RawConfigParser() config_read.read(self.config_filename) section = "Scan" self.scan_delay = float(config_read.get(section, 'scan_delay')) self.max_scans = float(config_read.get(section, 'max_scans')) section = "Log" self.log_directory = config_read.get(section, 'log_directory') self.local_dir_www = config_read.get(section, 'local_dir_www') self.log_buffer_flag = config_read.getboolean(section, 'log_buffer_flag') self.text_buffer_length = int( config_read.get(section, 'text_buffer_length')) section = "Ftp" self.ftp_creds_filename = config_read.get(section, 'ftp_creds_filename') self.ftp_log_max_count = float( config_read.get(section, 'ftp_log_max_count')) section = "Sauna" self.max_temp = float(config_read.get(section, 'max_temp')) self.min_temp = float(config_read.get(section, 'min_temp')) self.min_speed = float(config_read.get(section, 'min_speed')) self.max_speed = float(config_read.get(section, 'max_speed')) self.min_freq = float(config_read.get(section, 'min_freq')) self.max_freq = float(config_read.get(section, 'max_freq')) self.sauna_GPIO_port = int(config_read.get(section, 'sauna_GPIO_port')) self.sensor4readings = str(config_read.get(section, 'sensor4readings')) section = "mqtt" self.broker_address = str(config_read.get(section, 'broker_address')) self.broker_port = int(config_read.get(section, 'broker_port')) self.topic = str(config_read.get(section, 'topic')) return
def get_comic_url(comic_info: RawConfigParser): comic_domain, base_directory = None, "" if os.path.isfile("CNAME"): with open("CNAME") as f: comic_domain = f.read().strip('/') elif "GITHUB_REPOSITORY" in os.environ: repo_author, base_directory = os.environ["GITHUB_REPOSITORY"].split("/") comic_domain = f"{repo_author}.github.io" else: if comic_info.has_option("Comic Settings", "Comic domain"): comic_domain = comic_info.get("Comic Settings", "Comic domain").strip("/") else: raise ValueError( 'Set "Comic domain" in the [Comic Settings] section of your comic_info.ini file ' 'before building your site locally. Please see the comic_git wiki for more information.' ) if comic_info.has_option("Comic Settings", "Comic subdirectory"): base_directory = comic_info.get("Comic Settings", "Comic subdirectory").strip("/") if not comic_domain.startswith("http"): if (comic_info.has_option("Comic Settings", "Use https when building comic URL") and comic_info.getboolean("Comic Settings", "Use https when building comic URL")): comic_domain = "https://" + comic_domain else: comic_domain = "http://" + comic_domain if base_directory: base_directory = "/" + base_directory comic_url = comic_domain + base_directory print(f"Base URL: {comic_url}, base subdirectory: {base_directory}") return comic_url, base_directory
def __init__(self, config_filename): portal_config = RawConfigParser() portal_config.read(config_filename) token = portal_config.get('dragos portal', 'access_token') key = portal_config.get('dragos portal', 'access_key') if not token: raise 'Config is Missing access_token' if not key: raise 'Config is Missing access_key' try: self.url = portal_config.get( 'dragos portal', 'url', fallback='https://portal.dragos.com/api/v1/') self.debug = portal_config.getboolean('dragos portal', 'debug', fallback=False) self.headers = {'Api-Token': token, 'Api-Secret': key} self.ssl_verify = '-local' not in self.url # relax SSL verification for local development if not self.ssl_verify: urllib3.disable_warnings( urllib3.exceptions.InsecureRequestWarning) except: raise ('error reading Dragos config')
def get_page_info_list(comic_info: RawConfigParser) -> Tuple[List[Dict], int]: date_format = comic_info.get("Comic Settings", "Date format") tzinfo = timezone(comic_info.get("Comic Settings", "Timezone")) local_time = datetime.now(tz=tzinfo) print(f"Local time is {local_time}") page_info_list = [] scheduled_post_count = 0 for page_path in glob("your_content/comics/*/"): page_info = read_info(f"{page_path}info.ini", to_dict=True) post_date = tzinfo.localize(datetime.strptime(page_info["Post date"], date_format)) if post_date > local_time: scheduled_post_count += 1 # Post date is in the future, so delete the folder with the resources if comic_info.getboolean("Comic Settings", "Delete scheduled posts"): shutil.rmtree(page_path) else: page_info["page_name"] = os.path.basename(os.path.normpath(page_path)) page_info["Storyline"] = page_info.get("Storyline", "") page_info["Characters"] = str_to_list(page_info.get("Characters", "")) page_info["Tags"] = str_to_list(page_info.get("Tags", "")) page_info_list.append(page_info) page_info_list = sorted( page_info_list, key=lambda x: (strptime(x["Post date"], date_format), x["page_name"]) ) return page_info_list, scheduled_post_count
class Settings(metaclass=SettingsMeta): def __init__(self): self.conf = RawConfigParser() root_dir = os.path.dirname(os.path.abspath(__file__)) self.conf.read(os.path.join(root_dir, 'settings.ini'), encoding='utf-8') url_from_env = os.getenv('url', default=None) if url_from_env is not None: self.__url = url_from_env else: self.__url = self.conf.get('data', 'url') self.__login = self.conf.get('data', 'login') self.__password = self.conf.get('data', 'password') self.__use_api_auth = self.conf.getboolean('data', 'use_api_auth') self.__driver_path = self.conf.get('data', 'driver_path') def get_url(self) -> str: return self.__url def get_login(self) -> str: return self.__login def get_password(self) -> str: return self.__password def get_driver_path(self) -> str: return self.__driver_path def get_use_api_auth(self) -> bool: return self.__use_api_auth
def generatehieradataskel(config_file, hieradata_base_dir='', create_skel_auth_strings=[]): global debug config = RawConfigParser() config.read(config_file) try: debug = config.getboolean('hieragen', 'debug') except: debug = False try: unauth_common_area = config.get('hieragen', 'unauth-common-area') except: unauth_common_area = True if unauth_common_area: mkdir_gitkeep(hieradata_base_dir + '/common') for project_id in create_skel_auth_strings: if debug: eprint("SKEL for " + project_id + ": " + hieradata_base_dir + '/' + project_id) for dir_name in [ '/env', '/hierarchy', '/type', '/servergroup', '/node', '/config-catalog' ]: mkdir_gitkeep(hieradata_base_dir + '/' + project_id + '/' + dir_name)
def read_file(self): here = "config.read_file" config_read = RawConfigParser() config_read.read(self.config_filename) section = "Scan" self.scan_delay = float(config_read.get(section, 'scan_delay')) self.max_scans = float(config_read.get(section, 'max_scans')) section = "Log" self.log_directory = config_read.get(section, 'log_directory') self.local_dir_www = config_read.get(section, 'local_dir_www') self.log_buffer_flag = config_read.getboolean(section, 'log_buffer_flag') self.text_buffer_length = int( config_read.get(section, 'text_buffer_length')) section = "Ftp" self.ftp_creds_filename = config_read.get(section, 'ftp_creds_filename') self.ftp_log_max_count = float( config_read.get(section, 'ftp_log_max_count')) section = "Fan" self.max_temp = float(config_read.get(section, 'max_temp')) self.min_temp = float(config_read.get(section, 'min_temp')) self.min_speed = float(config_read.get(section, 'min_speed')) self.max_speed = float(config_read.get(section, 'max_speed')) self.min_freq = float(config_read.get(section, 'min_freq')) self.max_freq = float(config_read.get(section, 'max_freq')) self.brightness = float(config_read.get(section, 'brightness')) return
class EnvConfig: def __init__(self, config_path): self._webapp_config = RawConfigParser() self._webapp_config.read(config_path) def _get_key(self, type, section, key, default): """ Read key in a configuration file, avoiding key reading errors """ try: if type == 'string': return self._webapp_config.get(section, key).strip("'") elif type == 'int': return self._webapp_config.getint(section, key) elif type == 'bool': return self._webapp_config.getboolean(section, key) else: raise Exception('Unknown type %s' % type) except (NoOptionError, NoSectionError): return default def get_str_key(self, section, key, default=""): return self._get_key('string', section, key, default) def get_int_key(self, section, key, default=0): return self._get_key('int', section, key, default) def get_bool_key(self, section, key, default=False): return self._get_key('bool', section, key, default)
def settingsFromFile(infile, defaults): """Given a path string :attr:`infile`, load settings and return them as dictionary. Args: infile (str): a path to a file defaults (dict): a dictionary containing fallback values """ config = RawConfigParser() config.optionxform = lambda option: option try: with open(infile) as f: try: config.read_file(f) except MissingSectionHeaderError: config['General'] = defaults except OSError: config['General'] = defaults for key in defaults: if key not in config['General']: config['General'][key] = defaults[key] try: if int(config['General']['UpdateEvery']) <= 0: config['General']['UpdateEvery'] = defaults['UpdateEvery'] except ValueError: # can't convert to integer config['General']['UpdateEvery'] = defaults['UpdateEvery'] for value in ('SyncNewCourses', ): try: booleanvalue = config.getboolean('General', value) config['General'][value] = str(booleanvalue) except ValueError: # can't convert to boolean config['General'][value] = defaults[value] return dict(config['General'])
def getboolean(self, section, option, default=None): try: return RawConfigParser.getboolean(self, section, option) except (NoOptionError, NoSectionError) as err: if not default is None: return boolean(default) else: raise err
def has_all_config_enabled(file_path): """Return the presence of `all=true` in flowconfig.""" flowconfig_path = find_in_parent_folders(".flowconfig", file_path) configParser = RawConfigParser() configParser.read(os.path.join(flowconfig_path, ".flowconfig")) return configParser.getboolean("options", "all")
def __init__(self): self.__readCoursesId('./courseid') cf = RawConfigParser() cf.read('config') self.username = cf.get('info', 'username') self.password = cf.get('info', 'password') self.runtime = cf.getint('info', 'runtime') self.debug = cf.getboolean('action', 'debug') self.enroll = cf.getboolean('action', 'enroll') self.evaluate = cf.getboolean('action', 'evaluate') self.select_bat = cf.getboolean('action', 'select_bat') self.watch_logo = cf.getboolean('action', 'watch_logo') self.loginPage = 'http://sep.ucas.ac.cn' self.loginUrl = self.loginPage + '/slogin' self.courseSystem = self.loginPage + '/portal/site/226/821' self.courseBase = 'http://jwxk.ucas.ac.cn' self.courseIdentify = self.courseBase + '/login?Identity=' self.courseSelected = self.courseBase + '/courseManage/selectedCourse' self.courseSelectionBase = self.courseBase + '/courseManage/main' self.courseCategory = self.courseBase + '/courseManage/selectCourse?s=' self.courseSave = self.courseBase + '/courseManage/saveCourse?s=' self.studentCourseEvaluateUrl = 'http://jwjz.ucas.ac.cn/Student/DeskTopModules/' self.selectCourseUrl = 'http://jwjz.ucas.ac.cn/Student/DesktopModules/Course/SelectCourse.aspx' self.enrollCount = {} self.headers = { 'Host': 'sep.ucas.ac.cn', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6', } self.s = requests.Session() loginPage = self.s.get(self.loginPage, headers=self.headers) self.cookies = loginPage.cookies
def load(paths=(), extra_config=None, ignore_missing_paths=True): config = ConfigParser() for section, values in INITIAL_CONFIG.items(): config.add_section(section) for key, data in values.items(): config.set(section, key, data["value"]) if extra_config: for section, values in extra_config.items(): for key, value in values.items(): config.set(section, key, value) for path in paths: if path or not ignore_missing_paths: try: if not config.read(path) and not ignore_missing_paths: raise RuntimeError("No such file: %r" % path) except Exception as e: raise RuntimeError( "Failed to load config file %r: %s" % (path, e)) from e # Check the configuration for section in config.sections(): if section == "headers": continue if section not in INITIAL_CONFIG: raise RuntimeError("Invalid section %r in config" % section) allow_extra_options = ("type" in INITIAL_CONFIG[section] and config.get(section, "type") not in INITIAL_CONFIG[section]["type"].get("internal", ())) for option in config[section]: if option not in INITIAL_CONFIG[section]: if allow_extra_options: continue raise RuntimeError("Invalid option %r in section %r in " "config" % (option, section)) type_ = INITIAL_CONFIG[section][option]["type"] try: if type_ == bool: config.getboolean(section, option) else: type_(config.get(section, option)) except Exception as e: raise RuntimeError( "Invalid %s value for option %r in section %r in config: " "%r" % (type_.__name__, option, section, config.get(section, option))) from e return config
def __init__(self, config_file, *args, **kwargs): """ Класс настроек ЕСИА на основе конфигурационного файла :param str config_file: путь к конфигурационному ini-файлу :raises ConfigFileError: если указан неверный путь или файл недоступен для чтения :raises ConfigParser.*: при ошибках в формате файла или параметра """ if os.path.isfile(config_file) and os.access(config_file, os.R_OK): conf = RawConfigParser() conf.read(config_file) base_dir = os.path.dirname(config_file) kwargs = { 'esia_client_id': conf.get('esia', 'CLIENT_ID'), 'redirect_uri': conf.get('esia', 'REDIRECT_URI'), 'esia_service_url': conf.get('esia', 'SERVICE_URL'), 'esia_scope': conf.get('esia', 'SCOPE'), 'crypto_backend': conf.get('esia', 'CRYPTO_BACKEND'), 'certificate_file': None, 'private_key_file': None, 'csp_cert_thumbprint': None, 'csp_container_pwd': None, 'ssl_verify': True } # Openssl, M2Crypto params if conf.has_option('esia', 'CERT_FILE') and \ conf.has_option('esia', 'PRIV_KEY_FILE'): cert_f = conf.get('esia', 'CERT_FILE') pkey_f = conf.get('esia', 'PRIV_KEY_FILE') kwargs['certificate_file'] = base_dir + '/' + cert_f kwargs['private_key_file'] = base_dir + '/' + pkey_f # CryptoPro CSP params if conf.has_option('esia', 'CSP_CERT_THUMBPRINT'): kwargs['csp_cert_thumbprint'] = conf.get( 'esia', 'CSP_CERT_THUMBPRINT') kwargs['csp_container_pwd'] = conf.get('esia', 'CSP_CONTAINER_PWD') if conf.has_option('esia', 'JWT_CHECK_KEY'): token_check_key = conf.get('esia', 'JWT_CHECK_KEY') kwargs['esia_token_check_key'] = \ base_dir + '/' + token_check_key if conf.has_option('esia', 'LOGOUT_REDIRECT_URI'): redir = conf.get('esia', 'LOGOUT_REDIRECT_URI') kwargs['logout_redirect_uri'] = redir if conf.has_option('esia', 'SSL_VERIFY'): ssl_verify = conf.getboolean('esia', 'SSL_VERIFY') kwargs['ssl_verify'] = ssl_verify super(EsiaConfig, self).__init__(*args, **kwargs) else: raise ConfigFileError("Config file not exists or not readable!")
def __init__(self): self.__readCoursesId('./courseid') cf = RawConfigParser() cf.read('config') self.username = cf.get('info', 'username') self.password = cf.get('info', 'password') self.runtime = cf.getint('info', 'runtime') self.debug = cf.getboolean('action', 'debug')
def __init__(self): self.__readCoursesId() cf = RawConfigParser() cf.read('config') self.username = cf.get('info', 'username') self.password = cf.get('info', 'password') self.enroll = cf.getboolean('action', 'enroll') self.evaluate = cf.getboolean('action', 'evaluate') self.loginPage = 'http://sep.ucas.ac.cn' self.loginUrl = self.loginPage + '/slogin' self.courseSystem = self.loginPage + '/portal/site/226/821' self.courseBase = 'http://jwxk.ucas.ac.cn' self.courseIdentify = self.courseBase + '/login?Identity=' self.courseSelected = self.courseBase + '/courseManage/selectedCourse' self.courseSelectionBase = self.courseBase + '/courseManage/main' self.courseCategory = self.courseBase + '/courseManage/selectCourse?s=' self.courseSave = self.courseBase + '/courseManage/saveCourse?s=' self.studentCourseEvaluateUrl = 'http://jwjz.ucas.ac.cn/Student/DeskTopModules/' self.selectCourseUrl = 'http://jwjz.ucas.ac.cn/Student/DesktopModules/Course/SelectCourse.aspx' self.enrollCount = {} self.headers = { 'Host': 'sep.ucas.ac.cn', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6', } self.s = requests.Session() loginPage = self.s.get(self.loginPage, headers=self.headers) self.cookies = loginPage.cookies
def getboolean(self, section, option): try: return RawConfigParser.getboolean(self, section, option) except AttributeError: # XXX: # For some reason, getboolean likes to die sometimes. # Until I figure it out, this will act as a band-aid # to prevent the error from causing Lookit to not work value = self.get(section, option) if type(value) == bool: return value elif type(value) == str: return value == 'True' else: return bool(value)
def load(self): """Load configurations from file.""" parser = RawConfigParser() parser.optionxform = str parser.read(os.path.join(app_config_dir, 'config')) if parser.has_option('Options', 'autostart'): self.autostart = parser.getboolean('Options', 'autostart') if parser.has_option('Options', 'refreshtime'): self.refreshtime = parser.getint('Options', 'refreshtime') if parser.has_option('Options', 'stoptimer'): self.stoptimer = parser.getboolean('Options', 'stoptimer') if parser.has_option('Options', 'items_per_feed'): self.items_per_feed = parser.getint('Options', 'items_per_feed') if parser.has_option('Options', 'show_notifications'): self.show_notifications = parser.getboolean('Options', 'show_notifications') if parser.has_option('Options', 'show_update_notifications'): self.show_update_notifications = parser. \ getboolean('Options', 'show_update_notifications') if parser.has_option('Options', 'feeds_at_top'): self.feeds_at_top = parser.getboolean('Options', 'feeds_at_top') if parser.has_option('Options', 'show_unread_feeds'): self.show_unread_feeds = parser.getboolean('Options', 'show_unread_feeds')
def run(arg): # The WSGI environ dict should always be there, sans any special objects # like io streams. environ = arg['environ'] plugin_config = arg['config'] config = RawConfigParser(defaults=plugin_config) config.add_section('sample_log') config._sections['sample_log'] = plugin_config l = getLogger('plugin_log') l.addHandler(logHandler) if config.getboolean('sample_log', 'debug'): l.setLevel(DEBUG) l.debug('debug logging enabled') log_url = '{proto}://{server}:{port}{request}'.format( proto=environ.get('wsgi.url_scheme'), server=environ.get('SERVER_NAME'), port=environ.get('SERVER_PORT'), request=environ.get('PATH_INFO') ) log_client = '{client_ip}'.format( client_ip=environ.get( 'HTTP_X_FORWARDED_FOR', environ.get('REMOTE_ADDR') ) ) # Log a msg l.info('{log_client} - {method} - {log_url}'.format( log_client=log_client, log_url=log_url, method=environ.get('REQUEST_METHOD') ))
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) config = RawConfigParser() config.read(BASE_DIR + '/finance/settings.ini') # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = config.get('secrets', 'SECRET_KEY') ALPHA_VANTAGE_KEY = config.get('secrets', 'ALPHA_VANTAGE_KEY') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config.getboolean('debug', 'DEBUG_FLAG') ALLOWED_HOSTS = [config.get('hosts', 'ALLOWED_HOSTS')] SECURE_SSL_REDIRECT = config.getboolean('hosts', 'SECURE_SSL_REDIRECT') # Application definition INSTALLED_APPS = ( 'returns.apps.ReturnsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
def main(original_args=None): positionals, args = split_args_in_optional_and_positional( sys.argv[1:] if original_args is None else original_args ) parser1 = argparse.ArgumentParser(add_help=False, prog='bumpversion') parser1.add_argument( '--config-file', default='.bumpversion.cfg', metavar='FILE', help='Config file to read most of the variables from', required=False) parser1.add_argument('-v', '--version', action='version', version="%s %s" % (parser1.prog, __VERSION__)) known_args, remaining_argv = parser1.parse_known_args(args) defaults = {} vcs_info = {} for vcs in VCS: if vcs.is_usable(): vcs_info.update(vcs.latest_tag_info()) if 'current_version' in vcs_info: defaults['current_version'] = vcs_info['current_version'] config = None if os.path.exists(known_args.config_file): config = RawConfigParser() config.readfp(io.open(known_args.config_file, 'rt', encoding='utf-8')) defaults.update(dict(config.items("bumpversion"))) for boolvaluename in ("commit", "tag", "dry_run"): try: defaults[boolvaluename] = config.getboolean( "bumpversion", boolvaluename) except NoOptionError: pass # no default value then ;) elif known_args.config_file != parser1.get_default('config_file'): raise argparse.ArgumentTypeError("Could not read config file at {}".format( known_args.config_file)) parser2 = argparse.ArgumentParser(add_help=False, parents=[parser1]) parser2.set_defaults(**defaults) parser2.add_argument('--current-version', metavar='VERSION', help='Version that needs to be updated', required=False) parser2.add_argument('--parse', metavar='REGEX', help='Regex parsing the version string', default=defaults.get("parse", '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)')) parser2.add_argument('--serialize', metavar='FORMAT', help='How to format what is parsed back to a version', default=defaults.get("serialize", str('{major}.{minor}.{patch}'))) known_args, remaining_argv = parser2.parse_known_args(args) defaults.update(vars(known_args)) time_context = { 'now': datetime.now(), 'utcnow': datetime.utcnow(), } v = Version( known_args.parse, known_args.serialize, context=dict(list(time_context.items()) + list(prefixed_environ().items()) + list(vcs_info.items())) ) if not 'new_version' in defaults and known_args.current_version: v.parse(known_args.current_version) if len(positionals) > 0: v.bump(positionals[0]) defaults['new_version'] = v.serialize() parser3 = argparse.ArgumentParser( description=DESCRIPTION, formatter_class=argparse.ArgumentDefaultsHelpFormatter, conflict_handler='resolve', parents=[parser2], ) parser3.set_defaults(**defaults) parser3.add_argument('--current-version', metavar='VERSION', help='Version that needs to be updated', required=not 'current_version' in defaults) parser3.add_argument('--dry-run', '-n', action='store_true', default=False, help="Don't write any files, just pretend.") parser3.add_argument('--new-version', metavar='VERSION', help='New version that should be in the files', required=not 'new_version' in defaults) commitgroup = parser3.add_mutually_exclusive_group() commitgroup.add_argument('--commit', action='store_true', dest="commit", help='Commit to version control', default=defaults.get("commit", False)) commitgroup.add_argument('--no-commit', action='store_false', dest="commit", help='Do not commit to version control', default=argparse.SUPPRESS) taggroup = parser3.add_mutually_exclusive_group() taggroup.add_argument('--tag', action='store_true', dest="tag", default=defaults.get("tag", False), help='Create a tag in version control') taggroup.add_argument('--no-tag', action='store_false', dest="tag", help='Do not create a tag in version control', default=argparse.SUPPRESS) parser3.add_argument('--tag-name', metavar='TAG_NAME', help='Tag name (only works with --tag)', default=defaults.get('tag_name', 'v{new_version}')) parser3.add_argument('--message', '-m', metavar='COMMIT_MSG', help='Commit message', default=defaults.get('message', 'Bump version: {current_version} → {new_version}')) files = [] if 'files' in defaults: assert defaults['files'] != None files = defaults['files'].split(' ') parser3.add_argument('part', help='Part of the version to be bumped.') parser3.add_argument('files', metavar='file', nargs='*', help='Files to change', default=files) args = parser3.parse_args(remaining_argv + positionals) files = files or positionals[1:] for vcs in VCS: if vcs.is_usable(): vcs.assert_nondirty() break # make sure files exist and contain version string for path in files: with io.open(path, 'rb') as f: before = f.read().decode('utf-8') assert args.current_version in before, 'Did not find string {} in file {}'.format( args.current_version, path) # change version string in files for path in files: with io.open(path, 'rb') as f: before = f.read().decode('utf-8') # assert type(args.current_version) == bytes # assert type(args.new_version) == bytes after = before.replace(args.current_version, args.new_version) if not args.dry_run: with io.open(path, 'wt', encoding='utf-8') as f: f.write(after) commit_files = files if config: config.remove_option('bumpversion', 'new_version') config.set('bumpversion', 'current_version', args.new_version) if not args.dry_run: s = StringIO() try: config.write(s) with io.open(known_args.config_file, 'wb') as f: f.write(s.getvalue().encode('utf-8')) except UnicodeEncodeError: warnings.warn( "Unable to write UTF-8 to config file, because of an old configparser version. " "Update with `pip install --upgrade configparser`." ) commit_files.append(known_args.config_file) if args.commit: assert vcs.is_usable(), "Did find '{}' unusable, unable to commit.".format(vcs.__name__) if not args.dry_run: for path in commit_files: vcs.add_path(path) vcs_context = { "current_version": args.current_version, "new_version": args.new_version, } vcs_context.update(time_context) vcs_context.update(prefixed_environ()) vcs.commit(message=args.message.format(**vcs_context)) if args.tag: vcs.tag(args.tag_name.format(**vcs_context))
def eval_command(opts, args): conf = RawConfigParser(defaults={}) conf.read([os.path.expanduser('~/.config/mutag/mutag.conf')]) ui.set_debug(opts.debug) ui.use_color(conf.getboolean("mutag", 'color')) # If the output is not a terminal, remove the colors if not sys.stdout.isatty(): ui.use_color(False) prof = get_profile(conf, opts) mutag = Mutag(prof = prof) # escape '\' in query so xapian understands us. if opts.query: opts.query = opts.query.replace('\\', '\\\\') if opts.cmd == 'autotag': mutag.autotag(query=opts.query, path=opts.path, modified_only=opts.modified, related=True, dryrun=opts.dryrun, silent=opts.silent) elif opts.cmd == 'expire': mutag.expire(dryrun=opts.dryrun, silent=opts.silent) elif opts.cmd in set(['autotag', 'expire']) and opts.index: mutag.index(dryrun=opts.dryrun, silent=opts.silent) elif opts.cmd == 'count': num = mutag.count(opts.query, modified_only=opts.modified) print(num) elif opts.cmd == 'dedup': # TODO print("dedup not implemented") sys.exit() elif opts.cmd == 'tag': L = mutag.query(opts.query, path = opts.path, modified_only=opts.modified, related=False) mutag.change_tags(L, args, dryrun=opts.dryrun, silent=opts.silent) if opts.index: mutag.index(dryrun=opts.dryrun, silent=opts.silent) elif opts.cmd == 'flag': L = mutag.query(opts.query, path = opts.path, modified_only=opts.modified, related=False) mutag.change_flags(L, args, dryrun=opts.dryrun, silent=opts.silent) if opts.index: mutag.index(dryrun=opts.dryrun, silent=opts.silent) elif opts.cmd == 'list': L = mutag.query(opts.query, path = opts.path, modified_only=opts.modified, related=False) for msg in L: ui.print_color(msg.tostring(fmt=opts.format)) elif opts.cmd == 'queue': L = mutag.queue() for msg in L: ui.print_color(msg.tostring(fmt=opts.format, outbound=True)) elif opts.cmd == 'print': L = mutag.query(opts.query, path = opts.path, modified_only=opts.modified, related=False) for msg in L: print(msg.raw()) elif opts.cmd == 'filename': L = mutag.query(opts.query, path = opts.path, modified_only=opts.modified, related=False) for msg in L: print(msg['path']) elif opts.cmd == 'rebuild': mutag.rebuild(dryrun=opts.dryrun, silent=opts.silent) elif opts.cmd == 'trash': mutag.empty_trash(dryrun=opts.dryrun, silent=opts.silent) # Index if asked to and not done in a specific command if opts.index and not opts.cmd in ['autotag', 'tag', 'rebuild']: mutag.index(dryrun=opts.dryrun, silent=opts.silent) # Update mtime if opts.update: mutag.update_mtime(dryrun=opts.dryrun, silent=opts.silent) # commit mail if opts.commit: mutag.commit(dryrun=opts.dryrun, silent=opts.silent)
PROJECT_NAME = 'jet_demo' production_config = os.path.join('/usr/local/etc', PROJECT_NAME, '{0}.conf'.format(PROJECT_NAME)) development_config = os.path.join(BASE_DIR, 'conf', '{0}.conf'.format(PROJECT_NAME)) config_path = production_config if os.path.exists(production_config) else development_config config.read(config_path) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = config.get('common', 'secret_key') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config.getboolean('common', 'debug') TEMPLATE_DEBUG = config.getboolean('common', 'debug') ALLOWED_HOSTS = ['*'] # Application definition INSTALLED_APPS = ( 'jet.dashboard', 'jet', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions',
def main(original_args=None): positionals, args = split_args_in_optional_and_positional(original_args) parser1 = argparse.ArgumentParser(add_help=False) parser1.add_argument( "--config-file", default=".bumpversion.cfg", metavar="FILE", help="Config file to read most of the variables from", required=False, ) known_args, remaining_argv = parser1.parse_known_args(args) defaults = {} vcs_info = {} for vcs in VCS: if vcs.is_usable(): vcs_info.update(vcs.latest_tag_info()) if "current_version" in vcs_info: defaults["current_version"] = vcs_info["current_version"] config = None if os.path.exists(known_args.config_file): config = RawConfigParser() config.readfp(io.open(known_args.config_file, "rt", encoding="utf-8")) defaults.update(dict(config.items("bumpversion"))) for boolvaluename in ("commit", "tag", "dry_run"): try: defaults[boolvaluename] = config.getboolean("bumpversion", boolvaluename) except NoOptionError: pass # no default value then ;) elif known_args.config_file != parser1.get_default("config_file"): raise argparse.ArgumentTypeError("Could not read config file at {}".format(known_args.config_file)) parser2 = argparse.ArgumentParser(add_help=False, parents=[parser1]) parser2.set_defaults(**defaults) parser2.add_argument( "--current-version", metavar="VERSION", help="Version that needs to be updated", required=False ) parser2.add_argument( "--parse", metavar="REGEX", help="Regex parsing the version string", default="(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)", ) parser2.add_argument( "--serialize", metavar="FORMAT", help="How to format what is parsed back to a version", default="{major}.{minor}.{patch}", ) known_args, remaining_argv = parser2.parse_known_args(args) defaults.update(vars(known_args)) time_context = {"now": datetime.now(), "utcnow": datetime.utcnow()} v = Version( known_args.parse, known_args.serialize, context=dict(list(time_context.items()) + list(prefixed_environ().items()) + list(vcs_info.items())), ) if not "new_version" in defaults and known_args.current_version: v.parse(known_args.current_version) if len(positionals) > 0: v.bump(positionals[0]) defaults["new_version"] = v.serialize() parser3 = argparse.ArgumentParser( description=DESCRIPTION, formatter_class=argparse.ArgumentDefaultsHelpFormatter, conflict_handler="resolve", parents=[parser2], ) parser3.set_defaults(**defaults) parser3.add_argument( "--current-version", metavar="VERSION", help="Version that needs to be updated", required=not "current_version" in defaults, ) parser3.add_argument( "--dry-run", "-n", action="store_true", default=False, help="Don't write any files, just pretend." ) parser3.add_argument( "--new-version", metavar="VERSION", help="New version that should be in the files", required=not "new_version" in defaults, ) commitgroup = parser3.add_mutually_exclusive_group() commitgroup.add_argument( "--commit", action="store_true", dest="commit", help="Commit to version control", default=defaults.get("commit", False), ) commitgroup.add_argument( "--no-commit", action="store_false", dest="commit", help="Do not commit to version control", default=argparse.SUPPRESS, ) taggroup = parser3.add_mutually_exclusive_group() taggroup.add_argument( "--tag", action="store_true", dest="tag", default=defaults.get("tag", False), help="Create a tag in version control", ) taggroup.add_argument( "--no-tag", action="store_false", dest="tag", help="Do not create a tag in version control", default=argparse.SUPPRESS, ) parser3.add_argument( "--tag-name", metavar="TAG_NAME", help="Tag name (only works with --tag)", default=defaults.get("tag_name", "v{new_version}"), ) parser3.add_argument( "--message", "-m", metavar="COMMIT_MSG", help="Commit message", default=defaults.get("message", "Bump version: {current_version} → {new_version}"), ) files = [] if "files" in defaults: assert defaults["files"] != None files = defaults["files"].split(" ") parser3.add_argument("part", help="Part of the version to be bumped.") parser3.add_argument("files", metavar="file", nargs="*", help="Files to change", default=files) args = parser3.parse_args(remaining_argv + positionals) files = files or positionals[1:] for vcs in VCS: if vcs.is_usable(): vcs.assert_nondirty() break # make sure files exist and contain version string for path in files: with io.open(path, "rb") as f: before = f.read().decode("utf-8") assert args.current_version in before, "Did not find string {} in file {}".format(args.current_version, path) # change version string in files for path in files: with io.open(path, "rb") as f: before = f.read().decode("utf-8") # assert type(args.current_version) == bytes # assert type(args.new_version) == bytes after = before.replace(args.current_version, args.new_version) if not args.dry_run: with io.open(path, "wt", encoding="utf-8") as f: f.write(after) commit_files = files if config: config.remove_option("bumpversion", "new_version") config.set("bumpversion", "current_version", args.new_version) if not args.dry_run: s = StringIO() try: config.write(s) with io.open(known_args.config_file, "wb") as f: f.write(s.getvalue().encode("utf-8")) except UnicodeEncodeError: warnings.warn( "Unable to write UTF-8 to config file, because of an old configparser version. " "Update with `pip install --upgrade configparser`." ) commit_files.append(known_args.config_file) if args.commit: if not args.dry_run: for path in commit_files: vcs.add_path(path) vcs_context = {"current_version": args.current_version, "new_version": args.new_version} vcs_context.update(time_context) vcs_context.update(prefixed_environ()) vcs.commit(message=args.message.format(**vcs_context)) if args.tag: vcs.tag(args.tag_name.format(**vcs_context))
class Xhtml2PdfReport(Report): def __init__(self, options): self.options = RawConfigParser(allow_no_value=True) try: self.options.read_string(options) except AttributeError: self.options.readfp(BytesIO(options.encode("UTF-8"))) def get_default_options(self): return DEFAULT_OPTS def render(self, request, context): model = context["bmfmodule"]["model"]._meta template_name = "%s/%s_htmlreport.html" % (model.app_label, model.model_name) pages_file = None try: bg_pk = self.options.getint("pages", "pdf_background_pk") file = Document.objects.get(pk=bg_pk) pages_file = codecs.encode(file.file.read(), "base64").decode().replace("\n", "") except (Document.DoesNotExist, ValueError): pass letter_file = None try: bg_pk = self.options.getint("letter_page", "pdf_background_pk") file = Document.objects.get(pk=bg_pk) letter_file = codecs.encode(file.file.read(), "base64").decode().replace("\n", "") except (Document.DoesNotExist, ValueError): pass options = { "template_name": template_name, "size": self.options.get("layout", "size"), "form": self.options.get("layout", "form"), "letter": self.options.getboolean("layout", "letter"), "template_letter": letter_file, "template_pages": pages_file, "letter_margin_right": self.options.get("letter_page", "margin_right"), "letter_margin_bottom": self.options.get("letter_page", "margin_bottom"), "letter_extra": self.options.getboolean("letter_page", "extra"), "letter_extra_right": self.options.get("letter_page", "extra_right"), "letter_extra_top": self.options.get("letter_page", "extra_top"), "page_margin_top": self.options.get("pages", "margin_top"), "page_margin_right": self.options.get("pages", "margin_right"), "page_margin_bottom": self.options.get("pages", "margin_bottom"), "footer_height": self.options.get("footer", "height"), "footer_right": self.options.get("footer", "right"), } context["options"] = options template = select_template([template_name, "djangobmf/report_html_base.html"]) # pdf won't be in UTF-8 html = template.render(Context(context)).encode("ISO-8859-1") if settings.REPORTING_SERVER: response = requests.post(settings.REPORTING_SERVER, data=html, timeout=5.0) return "pdf", "application/pdf", response.content, True elif XHTML2PDF: buffer = BytesIO() pdf = pisa.pisaDocument(BytesIO(html), buffer) pdf = buffer.getvalue() buffer.close() return "pdf", "application/pdf", pdf, True else: return "html", "text/html", html, False
import os from configparser import RawConfigParser BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) config = RawConfigParser() config.optionxform = str config.read(BASE_DIR + '/TIMA/settings.ini') # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = config.get('secrets','SECRET_KEY') SESSION_COOKIE_SECURE = config.getboolean('secrets','SESSION_COOKIE_SECURE') CSRF_COOKIE_SECURE = config.getboolean('secrets','CSRF_COOKIE_SECURE') SESSION_EXPIRE_AT_BROWSER_CLOSE = config.getboolean('secrets','SESSION_EXPIRE_AT_BROWSER_CLOSE') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config.getboolean('debug','DEBUG') TEMPLATE_DEBUG = config.getboolean('debug','TEMPLATE_DEBUG') ALLOWED_HOSTS = config.get('host','ALLOWED_HOSTS').split() ADMINS = tuple(config.items('admins')) # Email settings EMAIL_USE_TLS = config.getboolean('host_email','EMAIL_USE_TLS')
} return json.dumps(job_data) @app.route('/approve', method='POST') def approve_client(): response.content_type = 'application/json' try: jobs = dispatch_plugins() except Exception as e: response.status = 500 jobs = { 'result': { 'error': str(e) } } return json.dumps(jobs) if __name__ == '__main__': app.run( host=config.get('portal', 'listen_host'), port=config.getint('portal', 'listen_port') ) debug(config.getboolean('portal', 'debug')) else: application = app
) args = parser.parse_args() config.readfp(args.configuration) # Only list object class types and exit if args.list_object_types: print(object_classes) exit(0) st = siptracklib.connect( config.get('siptrack', 'hostname'), config.get('siptrack', 'username'), config.get('siptrack', 'password'), config.get('siptrack', 'port'), use_ssl=config.getboolean('siptrack', 'ssl') ) # Use the base view defined in the configuration st_view = st.view_tree.getChildByName( config.get('siptrack', 'base_view'), include=['view'] ) # Siptrack device tree st_dt = st_view.listChildren(include=['device tree'])[0] if args.device_path: st_root = get_category_by_path(st_dt, args.device_path) else: st_root = st_view
USE_L10N = True USE_TZ = True # FIXME: Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # SECURITY WARNING: don't run with debug turned on in production! TEMPLATE_DEBUG = True # FIXME # ALLOWED_HOSTS = config.get("django", "allowed_hosts").replace(" ", "").split( # ",") # Pimostat settings. PIMOSTAT_TESTING_WITHOUT_HARDWARE = config.getboolean( "pimostat", "testing_without_hardware") PIMOSTAT_SENSOR_UPDATE_FREQUENCY = config.getint( "pimostat", "sensor_update_frequency") # Celery settings. # Hack to make celery run in debug mode. import sys if "/usr/bin/celery" in sys.argv: DEBUG = False else: DEBUG = True CELERYBEAT_SCHEDULE = { "UpdateEnabledSensors": {
config.readfp(open(argv[1])) except: try: config.readfp(open('regexbot.ini')) except Exception: print "Syntax:" print " %s [config]" % argv[0] print "" print "If no configuration file is specified or there was an error, it will default to `regexbot.ini'." print "If there was a failure reading the configuration, it will display this message." exit(1) # read config SERVER = config.get('regexbot', 'server') PORT = config.getint('regexbot', 'port') IPV6 = config.getboolean('regexbot', 'ipv6') NICK = str(config.get('regexbot', 'nick')) CHANNELS = str(config.get('regexbot', 'channels')).split() VERSION = str(config.get('regexbot', 'version')) + '; %s' try: VERSION = VERSION % Popen(["git","branch","-v","--contains"], stdout=PIPE).communicate()[0].strip() except: VERSION = VERSION % 'unknown' del Popen, PIPE TRANSLATE_ENABLED = config.getboolean('regexbot','translate_enabled') RECONNECT_TO_SERVER = config.getboolean('regexbot', 'reconnect_to_server') FORCE_ENDING_SLASH = config.getboolean('regexbot', 'force_ending_slash') CHANNEL_FLOOD_COOLDOWN = timedelta(seconds=config.getint('regexbot', 'channel_flood_cooldown')) GLOBAL_FLOOD_COOLDOWN = timedelta(seconds=config.getint('regexbot', 'global_flood_cooldown')) MAX_MESSAGES = config.getint('regexbot', 'max_messages') MAX_MESSAGE_SIZE = config.getint('regexbot', 'max_message_size') try: NICKSERV_PASS = str(config.get('regexbot', 'nickserv_pass'))
def read_yarrharr_conf(files, namespace): """ Read the given configuration files, mutating the given dictionary to contain Django settings. :raises UnreadableConfError: if any of the given files are not read """ conf = RawConfigParser() conf.read_file(StringIO(DEFAULT_CONF), '<defaults>') files_read = conf.read(files) files_unread = set(files) - set(files_read) if files_unread: raise UnreadableConfError(files_unread) namespace['DEBUG'] = conf.getboolean('yarrharr', 'debug') namespace['HOT'] = conf.getboolean('yarrharr', 'hot') namespace['DATABASES'] = { 'default': { 'ENGINE': conf.get('db', 'engine'), 'NAME': conf.get('db', 'name'), 'USER': conf.get('db', 'user'), 'PASSWORD': conf.get('db', 'password'), 'HOST': conf.get('db', 'host'), 'PORT': conf.get('db', 'port'), }, } namespace['ATOMIC_REQUESTS'] = True external_url = urlparse(conf.get('yarrharr', 'external_url')) if external_url.path != '': # Ensure that the URL doesn't contain a path, as some day we will # probably want to add the ability to add a prefix to the path. msg = "external_url must not include path: remove {!r}".format(external_url.path) raise ValueError(msg) namespace['ALLOWED_HOSTS'] = [external_url.hostname] # The proxied config is an enumeration to ensure it can be extended to # support the Forwarded header (RFC 7239) in the future. We require expicit # configuration rather than auto-detecting these headers because the # frontend proxy *must* be configured to strip whatever header is in use, # lest clients be able to forge it. proxied = conf.get('yarrharr', 'proxied') if proxied not in {'no', 'x-forwarded'}: msg = "proxied must be 'no' or 'x-forwarded', not {!r}".format(proxied) raise ValueError(msg) namespace['USE_X_FORWARDED_HOST'] = proxied == 'x-forwarded' # Config for the Twisted production server. namespace['SERVER_ENDPOINT'] = conf.get('yarrharr', 'server_endpoint') namespace['ROOT_URLCONF'] = 'yarrharr.urls' namespace['LOGIN_URL'] = 'login' namespace['LOGIN_REDIRECT_URL'] = 'home' namespace['LOGOUT_URL'] = 'logout' namespace['LANGUAGE_CODE'] = 'en-us' namespace['USE_I18N'] = True namespace['USE_L10N'] = True namespace['USE_TZ'] = True namespace['TIME_ZONE'] = 'UTC' namespace['STATIC_ROOT'] = conf.get('yarrharr', 'static_root') namespace['STATIC_URL'] = conf.get('yarrharr', 'static_url') namespace['STATICFILES_FINDERS'] = ( 'django.contrib.staticfiles.finders.AppDirectoriesFinder',) # Template context processors. This list is missing most of the processors # in the default list as Yarrharr's templates don't use them. context_processors = [ 'django.contrib.auth.context_processors.auth', 'yarrharr.context_processors.hot', ] if namespace['DEBUG']: # When in debug mode, display SQL queries for requests coming from the # loopback interface. context_processors.append('django.template.context_processors.debug') namespace['INTERNAL_IPS'] = ['127.0.0.1'] namespace['TEMPLATES'] = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': {'context_processors': context_processors}, }] namespace['SECRET_KEY'] = conf.get('secrets', 'secret_key') namespace['X_FRAME_OPTIONS'] = 'DENY' namespace['MIDDLEWARE'] = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) namespace['SESSION_ENGINE'] = 'django.contrib.sessions.backends.signed_cookies' namespace['SESSION_COOKIE_HTTPONLY'] = True namespace['SESSION_COOKIE_SECURE'] = external_url.scheme == 'https' namespace['CSRF_TOKEN_SECURE'] = external_url.scheme == 'https' namespace['WSGI_APPLICATION'] = 'yarrharr.wsgi.application' namespace['INSTALLED_APPS'] = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.staticfiles', 'yarrharr', ) # Disable Django's logging configuration stuff (except when running under # the dev server). if 'runserver' not in sys.argv: namespace['LOGGING_CONFIG'] = None return conf
def getboolean(self, option, section=DEFSECTION): """ get a value and returns it as a boolean """ return RawConfigParser.getboolean(self, section, option)
def main(original_args=None): positionals, args = split_args_in_optional_and_positional( sys.argv[1:] if original_args is None else original_args ) if len(positionals[1:]) > 2: warnings.warn("Giving multiple files on the command line will be deprecated, please use [bumpversion:file:...] in a config file.", PendingDeprecationWarning) parser1 = argparse.ArgumentParser(add_help=False) parser1.add_argument( '--config-file', metavar='FILE', default=argparse.SUPPRESS, required=False, help='Config file to read most of the variables from (default: .bumpversion.cfg)') parser1.add_argument( '--verbose', action='count', default=0, help='Print verbose logging to stderr', required=False) parser1.add_argument( '--list', action='store_true', default=False, help='List machine readable information', required=False) parser1.add_argument( '--allow-dirty', action='store_true', default=False, help="Don't abort if working directory is dirty", required=False) known_args, remaining_argv = parser1.parse_known_args(args) logformatter = logging.Formatter('%(message)s') if len(logger.handlers) == 0: ch = logging.StreamHandler(sys.stderr) ch.setFormatter(logformatter) logger.addHandler(ch) if len(logger_list.handlers) == 0: ch2 = logging.StreamHandler(sys.stdout) ch2.setFormatter(logformatter) logger_list.addHandler(ch2) if known_args.list: logger_list.setLevel(1) log_level = { 0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG, }.get(known_args.verbose, logging.DEBUG) logger.setLevel(log_level) logger.debug("Starting {}".format(DESCRIPTION)) defaults = {} vcs_info = {} for vcs in VCS: if vcs.is_usable(): vcs_info.update(vcs.latest_tag_info()) if 'current_version' in vcs_info: defaults['current_version'] = vcs_info['current_version'] config = RawConfigParser('') # don't transform keys to lowercase (which would be the default) config.optionxform = lambda option: option config.add_section('bumpversion') explicit_config = hasattr(known_args, 'config_file') if explicit_config: config_file = known_args.config_file elif not os.path.exists('.bumpversion.cfg') and \ os.path.exists('setup.cfg'): config_file = 'setup.cfg' else: config_file = '.bumpversion.cfg' config_file_exists = os.path.exists(config_file) part_configs = {} files = [] if config_file_exists: logger.info("Reading config file {}:".format(config_file)) logger.info(io.open(config_file, 'rt', encoding='utf-8').read()) config.readfp(io.open(config_file, 'rt', encoding='utf-8')) log_config = StringIO() config.write(log_config) if 'files' in dict(config.items("bumpversion")): warnings.warn( "'files =' configuration is will be deprecated, please use [bumpversion:file:...]", PendingDeprecationWarning ) defaults.update(dict(config.items("bumpversion"))) for listvaluename in ("serialize",): try: value = config.get("bumpversion", listvaluename) defaults[listvaluename] = list(filter(None, (x.strip() for x in value.splitlines()))) except NoOptionError: pass # no default value then ;) for boolvaluename in ("commit", "tag", "dry_run"): try: defaults[boolvaluename] = config.getboolean( "bumpversion", boolvaluename) except NoOptionError: pass # no default value then ;) for section_name in config.sections(): section_name_match = re.compile("^bumpversion:(file|part):(.+)").match(section_name) if not section_name_match: continue section_prefix, section_value = section_name_match.groups() section_config = dict(config.items(section_name)) if section_prefix == "part": ThisVersionPartConfiguration = NumericVersionPartConfiguration if 'values' in section_config: section_config['values'] = list(filter(None, (x.strip() for x in section_config['values'].splitlines()))) ThisVersionPartConfiguration = ConfiguredVersionPartConfiguration part_configs[section_value] = ThisVersionPartConfiguration(**section_config) elif section_prefix == "file": filename = section_value if 'serialize' in section_config: section_config['serialize'] = list(filter(None, (x.strip() for x in section_config['serialize'].splitlines()))) section_config['part_configs'] = part_configs if not 'parse' in section_config: section_config['parse'] = defaults.get("parse", '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)') if not 'serialize' in section_config: section_config['serialize'] = defaults.get('serialize', [str('{major}.{minor}.{patch}')]) if not 'search' in section_config: section_config['search'] = defaults.get("search", '{current_version}') if not 'replace' in section_config: section_config['replace'] = defaults.get("replace", '{new_version}') files.append(ConfiguredFile(filename, VersionConfig(**section_config))) else: message = "Could not read config file at {}".format(config_file) if explicit_config: raise argparse.ArgumentTypeError(message) else: logger.info(message) parser2 = argparse.ArgumentParser(prog='bumpversion', add_help=False, parents=[parser1]) parser2.set_defaults(**defaults) parser2.add_argument('--current-version', metavar='VERSION', help='Version that needs to be updated', required=False) parser2.add_argument('--parse', metavar='REGEX', help='Regex parsing the version string', default=defaults.get("parse", '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)')) parser2.add_argument('--serialize', metavar='FORMAT', action=DiscardDefaultIfSpecifiedAppendAction, help='How to format what is parsed back to a version', default=defaults.get("serialize", [str('{major}.{minor}.{patch}')])) parser2.add_argument('--search', metavar='SEARCH', help='Template for complete string to search', default=defaults.get("search", '{current_version}')) parser2.add_argument('--replace', metavar='REPLACE', help='Template for complete string to replace', default=defaults.get("replace", '{new_version}')) known_args, remaining_argv = parser2.parse_known_args(args) defaults.update(vars(known_args)) assert type(known_args.serialize) == list context = dict(list(time_context.items()) + list(prefixed_environ().items()) + list(vcs_info.items())) try: vc = VersionConfig( parse=known_args.parse, serialize=known_args.serialize, search=known_args.search, replace=known_args.replace, part_configs=part_configs, ) except sre_constants.error as e: sys.exit(1) current_version = vc.parse(known_args.current_version) if known_args.current_version else None new_version = None if not 'new_version' in defaults and known_args.current_version: try: if current_version and len(positionals) > 0: logger.info("Attempting to increment part '{}'".format(positionals[0])) new_version = current_version.bump(positionals[0], vc.order()) logger.info("Values are now: " + keyvaluestring(new_version._values)) defaults['new_version'] = vc.serialize(new_version, context) except MissingValueForSerializationException as e: logger.info("Opportunistic finding of new_version failed: " + e.message) except IncompleteVersionRepresenationException as e: logger.info("Opportunistic finding of new_version failed: " + e.message) except KeyError as e: logger.info("Opportunistic finding of new_version failed") parser3 = argparse.ArgumentParser( prog='bumpversion', description=DESCRIPTION, formatter_class=argparse.ArgumentDefaultsHelpFormatter, conflict_handler='resolve', parents=[parser2], ) parser3.set_defaults(**defaults) parser3.add_argument('--current-version', metavar='VERSION', help='Version that needs to be updated', required=not 'current_version' in defaults) parser3.add_argument('--dry-run', '-n', action='store_true', default=False, help="Don't write any files, just pretend.") parser3.add_argument('--new-version', metavar='VERSION', help='New version that should be in the files', required=not 'new_version' in defaults) commitgroup = parser3.add_mutually_exclusive_group() commitgroup.add_argument('--commit', action='store_true', dest="commit", help='Commit to version control', default=defaults.get("commit", False)) commitgroup.add_argument('--no-commit', action='store_false', dest="commit", help='Do not commit to version control', default=argparse.SUPPRESS) taggroup = parser3.add_mutually_exclusive_group() taggroup.add_argument('--tag', action='store_true', dest="tag", default=defaults.get("tag", False), help='Create a tag in version control') taggroup.add_argument('--no-tag', action='store_false', dest="tag", help='Do not create a tag in version control', default=argparse.SUPPRESS) parser3.add_argument('--tag-name', metavar='TAG_NAME', help='Tag name (only works with --tag)', default=defaults.get('tag_name', 'v{new_version}')) parser3.add_argument('--message', '-m', metavar='COMMIT_MSG', help='Commit message', default=defaults.get('message', 'Bump version: {current_version} → {new_version}')) file_names = [] if 'files' in defaults: assert defaults['files'] != None file_names = defaults['files'].split(' ') parser3.add_argument('part', help='Part of the version to be bumped.') parser3.add_argument('files', metavar='file', nargs='*', help='Files to change', default=file_names) args = parser3.parse_args(remaining_argv + positionals) if args.dry_run: logger.info("Dry run active, won't touch any files.") if args.new_version: new_version = vc.parse(args.new_version) logger.info("New version will be '{}'".format(args.new_version)) file_names = file_names or positionals[1:] for file_name in file_names: files.append(ConfiguredFile(file_name, vc)) for vcs in VCS: if vcs.is_usable(): try: vcs.assert_nondirty() except WorkingDirectoryIsDirtyException as e: if not defaults['allow_dirty']: logger.warn( "{}\n\nUse --allow-dirty to override this if you know what you're doing.".format(e.message)) raise break else: vcs = None # make sure files exist and contain version string logger.info("Asserting files {} contain the version string:".format(", ".join([str(f) for f in files]))) for f in files: f.should_contain_version(current_version, context) # change version string in files for f in files: f.replace(current_version, new_version, context, args.dry_run) commit_files = [f.path for f in files] config.set('bumpversion', 'new_version', args.new_version) for key, value in config.items('bumpversion'): logger_list.info("{}={}".format(key, value)) config.remove_option('bumpversion', 'new_version') config.set('bumpversion', 'current_version', args.new_version) new_config = StringIO() try: write_to_config_file = (not args.dry_run) and config_file_exists logger.info("{} to config file {}:".format( "Would write" if not write_to_config_file else "Writing", config_file, )) config.write(new_config) logger.info(new_config.getvalue()) if write_to_config_file: with io.open(config_file, 'wb') as f: f.write(new_config.getvalue().encode('utf-8')) except UnicodeEncodeError: warnings.warn( "Unable to write UTF-8 to config file, because of an old configparser version. " "Update with `pip install --upgrade configparser`." ) if config_file_exists: commit_files.append(config_file) if not vcs: return assert vcs.is_usable(), "Did find '{}' unusable, unable to commit.".format(vcs.__name__) do_commit = (not args.dry_run) and args.commit do_tag = (not args.dry_run) and args.tag logger.info("{} {} commit".format( "Would prepare" if not do_commit else "Preparing", vcs.__name__, )) for path in commit_files: logger.info("{} changes in file '{}' to {}".format( "Would add" if not do_commit else "Adding", path, vcs.__name__, )) if do_commit: vcs.add_path(path) vcs_context = { "current_version": args.current_version, "new_version": args.new_version, } vcs_context.update(time_context) vcs_context.update(prefixed_environ()) commit_message = args.message.format(**vcs_context) logger.info("{} to {} with message '{}'".format( "Would commit" if not do_commit else "Committing", vcs.__name__, commit_message, )) if do_commit: vcs.commit(message=commit_message) tag_name = args.tag_name.format(**vcs_context) logger.info("{} '{}' in {}".format( "Would tag" if not do_tag else "Tagging", tag_name, vcs.__name__ )) if do_tag: vcs.tag(tag_name)
class AcsooConfig(object): # list of callables returning dictionaries to update default_map default_map_readers = [] def __init__(self, filename): self.__cfg = RawConfigParser() if not filename and os.path.isfile(DEFAULT_CONFIG_FILE): filename = DEFAULT_CONFIG_FILE if filename: if not os.path.isfile(filename): raise click.ClickException( "Configuration file {} not found.".format(filename) ) self.__cfgfile = filename self.__cfg.read(filename) @staticmethod def add_default_map_reader(reader): AcsooConfig.default_map_readers.append(reader) def get_default_map(self): default_map = {} for reader in self.default_map_readers: default_map.update(reader(self)) return default_map @property def series(self): r = self.__cfg.get(SECTION, "series") if not r: raise click.ClickException("Missing series in {}.".format(self.__cfgfile)) if r not in ("8.0", "9.0", "10.0", "11.0", "12.0"): raise click.ClickException( "Unsupported series {} in {}.".format(r, self.__cfgfile) ) return r @property def version(self): r = self.__cfg.get(SECTION, "version") if not r: raise click.ClickException("Missing version in {}.".format(self.__cfgfile)) return r @property def trigram(self): r = self.__cfg.get(SECTION, "trigram") if not r: raise click.ClickException("Missing trigram in {}.".format(self.__cfgfile)) return r @property def pushable(self): r = self.getlist(SECTION, "pushable") if not r: return ["github.com:acsone"] else: return r def get(self, section, option, default=None, flatten=False): try: r = self.__cfg.get(section, option) if flatten: r = "".join(_split_multiline(r)) return r except (NoOptionError, NoSectionError): return default def getboolean(self, section, option, default=None): try: return self.__cfg.getboolean(section, option) except (NoOptionError, NoSectionError): return default def getlist(self, section, option, default=None): try: return _split_multiline(self.__cfg.get(section, option)) except (NoOptionError, NoSectionError): return default or []
}, } } # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = config.get('secrets', 'SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config.getboolean('debug', 'DEBUG') ALLOWED_HOSTS = [host for host in config.get('debug', 'ALLOWED_HOSTS').split(',')] FILE_CHARSET = 'UTF-8' # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
class Config(object): """A wrapper around RawConfigParser. Provides a ``defaults`` attribute of the same type which can be used to set default values. """ def __init__(self, version=None, _defaults=True): """Use read() to read in an existing config file. version should be an int starting with 0 that gets incremented if you want to register a new upgrade function. If None, upgrade is disabled. """ self._config = ConfigParser(dict_type=_sorted_dict) self.defaults = None if _defaults: self.defaults = Config(_defaults=False) self._version = version self._loaded_version = None self._upgrade_funcs = [] def _do_upgrade(self, func): assert self._loaded_version is not None assert self._version is not None old_version = self._loaded_version new_version = self._version if old_version != new_version: print_d("Config upgrade: %d->%d (%r)" % ( old_version, new_version, func)) func(self, old_version, new_version) def get_version(self): """Get the version of the loaded config file (for testing only) Raises Error if no file was loaded or versioning is disabled. """ if self._version is None: raise Error("Versioning disabled") if self._loaded_version is None: raise Error("No file loaded") return self._loaded_version def register_upgrade_function(self, function): """Register an upgrade function that gets called at each read() if the current config version and the loaded version don't match. Can also be registered after read was called. function(config, old_version: int, new_version: int) -> None """ if self._version is None: raise Error("Versioning disabled") self._upgrade_funcs.append(function) # after read(), so upgrade now if self._loaded_version is not None: self._do_upgrade(function) return function def reset(self, section, option): """Reset the value to the default state""" assert self.defaults is not None try: self._config.remove_option(section, option) except NoSectionError: pass def options(self, section): """Returns a list of options available in the specified section.""" try: options = self._config.options(section) except NoSectionError: if self.defaults: return self.defaults.options(section) raise else: if self.defaults: try: options.extend(self.defaults.options(section)) options = list_unique(options) except NoSectionError: pass return options def get(self, section, option, default=_DEFAULT): """get(section, option[, default]) -> str If default is not given or set, raises Error in case of an error """ try: return self._config.get(section, option) except Error: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.get(section, option) except Error: pass raise return default def gettext(self, *args, **kwargs): value = self.get(*args, **kwargs) # make sure there are no surrogates value.encode("utf-8") return value def getbytes(self, section, option, default=_DEFAULT): try: value = self._config.get(section, option) value = value.encode("utf-8", "surrogateescape") return value except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getbytes(section, option) except Error: pass raise Error(e) return default def getboolean(self, section, option, default=_DEFAULT): """getboolean(section, option[, default]) -> bool If default is not given or set, raises Error in case of an error """ try: return self._config.getboolean(section, option) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getboolean(section, option) except Error: pass raise Error(e) return default def getint(self, section, option, default=_DEFAULT): """getint(section, option[, default]) -> int If default is not give or set, raises Error in case of an error """ try: return int(self._config.getfloat(section, option)) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getint(section, option) except Error: pass raise Error(e) return default def getfloat(self, section, option, default=_DEFAULT): """getfloat(section, option[, default]) -> float If default is not give or set, raises Error in case of an error """ try: return self._config.getfloat(section, option) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getfloat(section, option) except Error: pass raise Error(e) return default def getstringlist(self, section, option, default=_DEFAULT): """getstringlist(section, option[, default]) -> list If default is not given or set, raises Error in case of an error. Gets a list of strings, using CSV to parse and delimit. """ try: value = self._config.get(section, option) parser = csv.reader( [value], lineterminator='\n', quoting=csv.QUOTE_MINIMAL) try: vals = next(parser) except (csv.Error, ValueError) as e: raise Error(e) return vals except Error as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getstringlist(section, option) except Error: pass raise Error(e) return default def setstringlist(self, section, option, values): """Saves a list of unicode strings using the csv module""" sw = StringIO() values = [str(v) for v in values] writer = csv.writer(sw, lineterminator='\n', quoting=csv.QUOTE_MINIMAL) writer.writerow(values) self.set(section, option, sw.getvalue()) def setlist(self, section, option, values, sep=","): """Saves a list of str using ',' as a separator and \\ for escaping""" values = [str(v) for v in values] joined = join_escape(values, sep) self.set(section, option, joined) def getlist(self, section, option, default=_DEFAULT, sep=","): """Returns a str list saved with setlist()""" try: value = self._config.get(section, option) return split_escape(value, sep) except (Error, ValueError) as e: if default is _DEFAULT: if self.defaults is not None: try: return self.defaults.getlist(section, option, sep=sep) except Error: pass raise Error(e) return default def set(self, section, option, value): """Saves the string representation for the passed value Don't pass unicode, encode first. """ if isinstance(value, bytes): raise TypeError("use setbytes") # RawConfigParser only allows string values but doesn't # scream if they are not (and it only fails before the # first config save..) if not isinstance(value, str): value = str(value) try: self._config.set(section, option, value) except NoSectionError: if self.defaults and self.defaults.has_section(section): self._config.add_section(section) self._config.set(section, option, value) else: raise def settext(self, section, option, value): value = str(value) # make sure there are no surrogates value.encode("utf-8") self.set(section, option, value) def setbytes(self, section, option, value): assert isinstance(value, bytes) value = value.decode("utf-8", "surrogateescape") self.set(section, option, value) def write(self, filename): """Write config to filename. Can raise EnvironmentError """ assert isinstance(filename, fsnative) mkdir(os.path.dirname(filename)) # temporary set the new version for saving if self._version is not None: self.add_section("__config__") self.set("__config__", "version", self._version) try: with atomic_save(filename, "wb") as fileobj: temp = StringIO() self._config.write(temp) data = temp.getvalue().encode("utf-8", "surrogateescape") fileobj.write(data) finally: if self._loaded_version is not None: self.set("__config__", "version", self._loaded_version) def clear(self): """Remove all sections.""" for section in self._config.sections(): self._config.remove_section(section) def is_empty(self): """Whether the config has any sections""" return not self._config.sections() def read(self, filename): """Reads the config from `filename` if the file exists, otherwise does nothing Can raise EnvironmentError, Error. """ try: with open(filename, "rb") as fileobj: fileobj = StringIO( fileobj.read().decode("utf-8", "surrogateescape")) self._config.readfp(fileobj, filename) except (IOError, OSError): return # don't upgrade if we just created a new config if self._version is not None: self._loaded_version = self.getint("__config__", "version", -1) for func in self._upgrade_funcs: self._do_upgrade(func) def has_option(self, section, option): """If the given section exists, and contains the given option""" return self._config.has_option(section, option) or ( self.defaults and self.defaults.has_option(section, option)) def has_section(self, section): """If the given section exists""" return self._config.has_section(section) or ( self.defaults and self.defaults.has_section(section)) def remove_option(self, section, option): """Remove the specified option from the specified section Can raise Error. """ return self._config.remove_option(section, option) def add_section(self, section): """Add a section named section to the instance if it not already exists.""" if not self._config.has_section(section): self._config.add_section(section)
config.readfp(open(argv[1])) except: try: config.readfp(open('regexbot.ini')) except Exception: print "Syntax:" print " %s [config]" % argv[0] print "" print "If no configuration file is specified or there was an error, it will default to `regexbot.ini'." print "If there was a failure reading the configuration, it will display this message." exit(1) # read config SERVER = config.get('regexbot', 'server') PORT = config.getint('regexbot', 'port') IPV6 = config.getboolean('regexbot', 'ipv6') NICK = str(config.get('regexbot', 'nick')) CHANNELS = str(config.get('regexbot', 'channels')).split() VERSION = str(config.get('regexbot', 'version')) + '; %s' try: VERSION = VERSION % Popen(["git","branch","-v","--contains"], stdout=PIPE).communicate()[0].strip() except: VERSION = VERSION % 'unknown' del Popen, PIPE TRANSLATE_ENABLED = config.getboolean('regexbot','translate_enabled') RECONNECT_TO_SERVER = config.getboolean('regexbot', 'reconnect_to_server') CHANNEL_FLOOD_COOLDOWN = timedelta(seconds=config.getint('regexbot', 'channel_flood_cooldown')) GLOBAL_FLOOD_COOLDOWN = timedelta(seconds=config.getint('regexbot', 'global_flood_cooldown')) MAX_MESSAGES = config.getint('regexbot', 'max_messages') MAX_MESSAGE_SIZE = config.getint('regexbot', 'max_message_size') try: NICKSERV_PASS = str(config.get('regexbot', 'nickserv_pass')) except: NICKSERV_PASS = None
parser.read(OAUTH_CONFIG) trait_parser = RawConfigParser() trait_parser.read(TRAIT_FILE) GENOTYPE_PROVIDERS = {} providers = parser.sections() for provider in providers: provider_dict = {} for option in parser.options(provider): provider_dict[option] = parser.get(provider,option) provider_dict['has_oauth'] = _can_do_oauth(provider_dict) GENOTYPE_PROVIDERS[provider] = provider_dict TRAITS = [] traits = trait_parser.sections() for trait in traits: trait_dict = {'name':trait} for option in trait_parser.options(trait): if option == 'quantitive': trait_dict[option] = trait_parser.getboolean(trait,option) else: trait_dict[option] = trait_parser.get(trait,option) trait_dict['histogram'] = _get_trait_histogram(trait) trait_dict['meanRisk'] = float(np.mean(np.asarray(trait_dict['histogram'][1:]))) TRAITS.append(trait_dict)
def create_app(args): # load in the configuration file config = ConfigParser({ 'certfile': None, 'keyfile': None, 'ca_certs': None, 'ssl': False, 'port': '3001', 'host': '0.0.0.0', 'icons': 'link', 'corpus_link': None, 'doc_title_format': '{0}', 'doc_url_format': '', 'raw_corpus': None, 'label_module': None, 'fulltext': 'false', 'topics': None, 'cluster': None, 'corpus_desc' : None, 'home_link' : '/', 'lang': None}) with open(args.config, encoding='utf8') as configfile: config.read_file(configfile) # path variables context_type = config.get('main', 'context_type') corpus_file = config.get('main', 'corpus_file') model_pattern = config.get('main', 'model_pattern') cluster_path = config.get('main', 'cluster') # language customization lang = config.get('main', 'lang') # set topic_range if config.get('main', 'topics'): topic_range = eval(config.get('main', 'topics')) # get icons_list config_icons = config.get('www', 'icons').split(",") if args.fulltext or config.getboolean('www', 'fulltext'): if not any('fulltext' in icon for icon in config_icons): config_icons.insert(0, 'fulltext-inline') # Create application object corpus_name = config.get('www', 'corpus_name') corpus_link = config.get('www', 'corpus_link') doc_title_format = config.get('www', 'doc_title_format') doc_url_format = config.get('www', 'doc_url_format') home_link = config.get('www', 'home_link') label_module = config.get('main', 'label_module') corpus_path = config.get('main', 'raw_corpus') corpus_desc = config.get('main', 'corpus_desc') fulltext = args.fulltext or config.getboolean('www', 'fulltext') app = Application(corpus_file=corpus_file, model_pattern=model_pattern, topic_range=topic_range, context_type=context_type, label_module=label_module, config_file=args.config, corpus_path=corpus_path, fulltext=fulltext, lang=lang, icons=config_icons, corpus_name=corpus_name, corpus_link=corpus_link, doc_title_format=doc_title_format, doc_url_format=doc_url_format, cluster_path=cluster_path, corpus_desc=corpus_desc, home_link=home_link) """ host, port = get_host_port(args) """ # app.run(host='0.0.0.0', port=8081) return app
def main(original_args=None): positionals, args = split_args_in_optional_and_positional( sys.argv[1:] if original_args is None else original_args ) parser1 = argparse.ArgumentParser(add_help=False) parser1.add_argument( '--config-file', default='.bumpversion.cfg', metavar='FILE', help='Config file to read most of the variables from', required=False) parser1.add_argument( '--verbose', action='count', default=0, help='Print verbose logging to stderr', required=False) known_args, remaining_argv = parser1.parse_known_args(args) if len(logger.handlers) == 0: ch = logging.StreamHandler(sys.stderr) logformatter = logging.Formatter('%(message)s') ch.setFormatter(logformatter) logger.addHandler(ch) log_level = { 0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG, }.get(known_args.verbose, logging.DEBUG) logger.setLevel(log_level) logger.debug("Starting {}".format(DESCRIPTION)) defaults = {} vcs_info = {} for vcs in VCS: if vcs.is_usable(): vcs_info.update(vcs.latest_tag_info()) if 'current_version' in vcs_info: defaults['current_version'] = vcs_info['current_version'] config = None if os.path.exists(known_args.config_file): config = RawConfigParser() config.readfp(io.open(known_args.config_file, 'rt', encoding='utf-8')) log_config = StringIO() config.write(log_config) logger.info("Reading config file {}:".format(known_args.config_file)) logger.info(log_config.getvalue()) defaults.update(dict(config.items("bumpversion"))) for listvaluename in ("serialize",): try: value = config.get("bumpversion", listvaluename) defaults[listvaluename] = list(filter(None, (x.strip() for x in value.splitlines()))) except NoOptionError: pass # no default value then ;) for boolvaluename in ("commit", "tag", "dry_run"): try: defaults[boolvaluename] = config.getboolean( "bumpversion", boolvaluename) except NoOptionError: pass # no default value then ;) else: message = "Could not read config file at {}".format(known_args.config_file) if known_args.config_file != parser1.get_default('config_file'): raise argparse.ArgumentTypeError(message) else: logger.info(message) parser2 = argparse.ArgumentParser(add_help=False, parents=[parser1]) parser2.set_defaults(**defaults) parser2.add_argument('--current-version', metavar='VERSION', help='Version that needs to be updated', required=False) parser2.add_argument('--parse', metavar='REGEX', help='Regex parsing the version string', default=defaults.get("parse", '(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)')) parser2.add_argument('--serialize', metavar='FORMAT', action=DiscardDefaultIfSpecifiedAppendAction, help='How to format what is parsed back to a version', default=defaults.get("serialize", [str('{major}.{minor}.{patch}')])) known_args, remaining_argv = parser2.parse_known_args(args) defaults.update(vars(known_args)) assert type(known_args.serialize) == list time_context = { 'now': datetime.now(), 'utcnow': datetime.utcnow(), } try: v = Version( known_args.parse, known_args.serialize, context=dict(list(time_context.items()) + list(prefixed_environ().items()) + list(vcs_info.items())) ) except sre_constants.error as e: sys.exit(1) if not 'new_version' in defaults and known_args.current_version: v.parse(known_args.current_version) if len(positionals) > 0: v.bump(positionals[0]) try: defaults['new_version'] = v.serialize() except MissingValueForSerializationException as e: logger.info("Opportunistic finding of new_version failed: " + e.message) except IncompleteVersionRepresenationException as e: logger.info("Opportunistic finding of new_version failed: " + e.message) except KeyError as e: logger.info("Opportunistic finding of new_version failed") parser3 = argparse.ArgumentParser( description=DESCRIPTION, formatter_class=argparse.ArgumentDefaultsHelpFormatter, conflict_handler='resolve', parents=[parser2], ) parser3.set_defaults(**defaults) parser3.add_argument('--current-version', metavar='VERSION', help='Version that needs to be updated', required=not 'current_version' in defaults) parser3.add_argument('--dry-run', '-n', action='store_true', default=False, help="Don't write any files, just pretend.") parser3.add_argument('--new-version', metavar='VERSION', help='New version that should be in the files', required=not 'new_version' in defaults) commitgroup = parser3.add_mutually_exclusive_group() commitgroup.add_argument('--commit', action='store_true', dest="commit", help='Commit to version control', default=defaults.get("commit", False)) commitgroup.add_argument('--no-commit', action='store_false', dest="commit", help='Do not commit to version control', default=argparse.SUPPRESS) taggroup = parser3.add_mutually_exclusive_group() taggroup.add_argument('--tag', action='store_true', dest="tag", default=defaults.get("tag", False), help='Create a tag in version control') taggroup.add_argument('--no-tag', action='store_false', dest="tag", help='Do not create a tag in version control', default=argparse.SUPPRESS) parser3.add_argument('--tag-name', metavar='TAG_NAME', help='Tag name (only works with --tag)', default=defaults.get('tag_name', 'v{new_version}')) parser3.add_argument('--message', '-m', metavar='COMMIT_MSG', help='Commit message', default=defaults.get('message', 'Bump version: {current_version} → {new_version}')) files = [] if 'files' in defaults: assert defaults['files'] != None files = defaults['files'].split(' ') parser3.add_argument('part', help='Part of the version to be bumped.') parser3.add_argument('files', metavar='file', nargs='*', help='Files to change', default=files) args = parser3.parse_args(remaining_argv + positionals) if args.dry_run: logger.info("Dry run active, won't touch any files.") logger.info("New version will be '{}'".format(args.new_version)) files = files or positionals[1:] for vcs in VCS: if vcs.is_usable(): vcs.assert_nondirty() break else: vcs = None # make sure files exist and contain version string logger.info("Asserting files {} contain string '{}':".format(", ".join(files), args.current_version)) for path in files: with io.open(path, 'rb') as f: found_before = False for lineno, line in enumerate(f.readlines()): if args.current_version in line.decode('utf-8'): found_before = True logger.info("Found '{}' in {} at line {}: {}".format(args.current_version, path, lineno, line.decode('utf-8').rstrip())) assert found_before, 'Did not find string {} in file {}'.format( args.current_version, path) # change version string in files for path in files: with io.open(path, 'rb') as f: before = f.read().decode('utf-8') after = before.replace(args.current_version, args.new_version) logger.info("{} file {}:".format( "Would change" if args.dry_run else "Changing", path, )) logger.info("\n".join(list(unified_diff(before.splitlines(), after.splitlines(), lineterm="", fromfile="a/"+path, tofile="b/"+path)))) if not args.dry_run: with io.open(path, 'wt', encoding='utf-8') as f: f.write(after) commit_files = files if config: config.remove_option('bumpversion', 'new_version') config.set('bumpversion', 'current_version', args.new_version) s = StringIO() try: config.write(s) logger.info("{} to config file {}:".format( "Would write" if args.dry_run else "Writing", known_args.config_file, )) logger.info(log_config.getvalue()) if not args.dry_run: with io.open(known_args.config_file, 'wb') as f: f.write(s.getvalue().encode('utf-8')) except UnicodeEncodeError: warnings.warn( "Unable to write UTF-8 to config file, because of an old configparser version. " "Update with `pip install --upgrade configparser`." ) commit_files.append(known_args.config_file) if not vcs: return assert vcs.is_usable(), "Did find '{}' unusable, unable to commit.".format(vcs.__name__) do_commit = (not args.dry_run) and args.commit do_tag = (not args.dry_run) and args.tag logger.info("{} {} commit".format( "Would prepare" if not do_commit else "Preparing", vcs.__name__, )) for path in commit_files: logger.info("{} changes in file '{}' to {}".format( "Would add" if not do_commit else "Adding", path, vcs.__name__, )) if do_commit: vcs.add_path(path) vcs_context = { "current_version": args.current_version, "new_version": args.new_version, } vcs_context.update(time_context) vcs_context.update(prefixed_environ()) commit_message = args.message.format(**vcs_context) logger.info("{} to {} with message '{}'".format( "Would commit" if not do_commit else "Committing", vcs.__name__, commit_message, )) if do_commit: vcs.commit(message=commit_message) tag_name = args.tag_name.format(**vcs_context) logger.info("{} '{}' in {}".format( "Would tag" if not do_tag else "Tagging", tag_name, vcs.__name__ )) if do_tag: vcs.tag(tag_name)
def run(arg): # Some info from the plugin dispatcher. environ = arg['environ'] plugin_config = arg['config'] config = RawConfigParser(defaults=plugin_config) config.add_section('iptables') config._sections['iptables'] = plugin_config # Setup plugin logging l = getLogger('plugin_iptables') l.addHandler(logHandler) if config.getboolean('iptables', 'debug'): l.setLevel(DEBUG) l.debug('debug logging enabled') # Get client IP from webapp, try HTTP_X_FORWARDED_FOR and fallback on # REMOTE_ADDR. client_ip = environ.get( 'HTTP_X_FORWARDED_FOR', environ.get('REMOTE_ADDR') ) client_mac = None error_msg = None iptables_failed = False # Verify client IP try: socket.inet_aton(client_ip) except socket.error: l.error('Client ip:{ip} is invalid'.format( ip=repr(client_ip) )) return { 'error': str(e), 'failed': True } # Attempt to get client HW address with arping if use_arping: try: client_mac = mac_from_ip( l, config.get('iptables', 'arping'), client_ip ) except Exception as e: l.info('Failed to get client HW address: {error}'.format( error=str(e) )) error_msg = str(e) pass if client_ip: iptables_cmd = config.get('iptables', 'iptables_cmd').format( ip_address=client_ip, mac_address=client_mac ) output = BytesIO() error = BytesIO() try: # The two arguments must not contain spaces of course. rc = sudo(tuple(iptables_cmd.split(' ')), _out=output, _err=error) except ErrorReturnCode: error.seek(0) error_msg = error.read() l.warn('{cmd}: exited badly: {error}'.format( cmd=('iptables', iptables_cmd), error=error_msg )) iptables_failed = True raise except Exception as e: l.warn('{cmd}: failed: {error}'.format( cmd=('iptables', iptables_cmd), error=str(e) )) error_msg = str(e) iptables_failed = True raise if rc.exit_code == 0: l.debug('Created iptables rule for client:{ip}'.format( ip=client_ip )) # If all else fails, error! This will be shown to end users. return { 'error': error_msg, 'failed': iptables_failed }