def ipo_china(): cld = CalendarChina() output = cld.get_output_path() text = read_file(output) if expired_for_seconds(cld.name, 60 * 60 * 3) or text is None: cld.gen_ics() return read_file(output) or "FILE NOT FOUND"
def ipo_tiger_rss(): rss = get_rss() cld = CalendarTiger() cld.name += rss output = cld.get_output_path(rss) text = read_file(output) if expired_for_seconds(cld.name, 60 * 60 * 3) or text is None: cld.gen_ics(rss) return read_file(output) or "FILE NOT FOUND"
def ipo_tiger_mainland(): cld = CalendarTiger(filter_fn=lambda x: x["market"] in ["SZ", "SH"]) cld.name = "ipo-tiger-mainland" cld.cal_name = "IPO (tiger) (SH/SZ)" output = cld.get_output_path() text = read_file(output) if expired_for_seconds(cld.name, 60 * 60 * 3) or text is None: cld.gen_ics() return read_file(output) or "FILE NOT FOUND"
def ipo_tiger_hk(): cld = CalendarTiger(filter_fn=lambda x: x["market"] == "HK") cld.name = "ipo-tiger-hk" cld.cal_name = "IPO (tiger) (HK)" output = cld.get_output_path() text = read_file(output) if expired_for_seconds(cld.name, 60 * 60 * 3) or text is None: cld.gen_ics() return read_file(output) or "FILE NOT FOUND"
def ipo_tiger_rss_us(): ext = get_rss() cld = CalendarTiger(filter_fn=lambda x: x["market"] == "US") cld.name = "ipo-tiger-us" + "-" + ext cld.cal_name = "IPO (tiger) (US)" output = cld.get_output_path(ext) text = read_file(output) if expired_for_seconds(cld.name, 60 * 60 * 3) or text is None: cld.gen_ics(ext) return read_file(output) or "FILE NOT FOUND"
def do_open(self, arg): """Load text file list of artifacts Command will detect each line items artifact type, create the artifact, and add it to the current session if there is one. Usage: open <path/to/file.txt> """ if not os.path.exists(arg): warning('Cannot find file on disk (%s)' % arg) return artifacts = read_file(arg, True) for artifact in artifacts: new_artifact = create_artifact(artifact) if not self.db.exists(new_artifact.type, {'name': new_artifact.name}): doc_id = self.db.insert_one(new_artifact.type, new_artifact) if doc_id is not None: success('Created new artifact (%s - %s)' % (artifact.name, artifact.type)) if self.session is None: self.session = RedisCache(config) self.session.set(1, arg) success('Opened new session') print('Artifact ID: 1') else: count = 0 for key in self.session.db.scan_iter(): count += 1 _id = count + 1 self.session.set(_id, arg) print('Artifact ID: %s' % _id) success('Finished loading artifact list')
def configure_sshd(): """ Configures SSHD to allow root logins :return: Whether the operation was successful """ sshd_config = read_file(FILE_SSHD) re.sub(r"^#?\s*PermitRootLogin\s+(no|prohibit-password)$", "PermitRootLogin yes", sshd_config, flags=re.MULTILINE) return write_file(FILE_SSHD, sshd_config)
def change_rootpw(passwd): """ Changes the system root password :param passwd: New root password :return: Whether the operation was successful """ shadow_contents = read_file(FILE_PASSWD) if shadow_contents is not None: shadow_contents = re.sub(r"(?<=^root:)\*(?=:)", crypt.crypt(passwd, salt=crypt.METHOD_SHA256), shadow_contents) return write_file(FILE_PASSWD, shadow_contents)
def configure_networking(): """ Configures networking in the given chroot using systemd-networkd and systemd-resolved :param chroot: Filesystem root :return: Whether the operation was successful """ success = systemd_enable("dbus") if success: logging.info("Configuring systemd-networkd...") success = (write_file(FILE_NETWORKD_DEFAULT, CONFIG_DHCP) and success and systemd_enable("systemd-networkd")) if success: logging.info("Configuring systemd-resolved...") try: os.makedirs(DIR_RESOLVCONF, mode=0o755, exist_ok=True) success = (run_cmd("cp {} {}".format(FILE_RESOLVCONF_ETC, FILE_RESOLVCONF_RUN)) and unmount_device(FILE_RESOLVCONF_ETC)) os.remove(FILE_RESOLVCONF_ETC) except Exception as e: logging.error("Error copying systemd-resolved files: {}".format(e)) success = False if success: try: os.symlink(FILE_RESOLVCONF_RUN, FILE_RESOLVCONF_ETC) except Exception as e: logging.error("Error symlinking resolvconf: {}".format(e)) success = False if success: success = systemd_enable("systemd-resolved") if success: ssh_config = read_file(FILE_SSHD) try: ssh_config = re.sub( r"# +(?=PermitRootLogin\s+yes|no|prohibit-password)$", "", ssh_config, re.MULTILINE) except Exception as e: logging.error("Error formatting ssh config: {}".format(e)) success = False if success: success = (write_file(FILE_SSHD, ssh_config) and systemd_enable("ssh")) return success
def create_query(fn, columns, table_name): data = read_file(fn) if data: values = [] for row in data: v = '(' for col in columns: x = row[col] if col == "age.max" and x == "NA": x = "NULL" else: x = "'" + x + "'" v += x + "," v += str(year) +")" values.append(v) fields = map(make_field_name, columns) query = 'INSERT INTO '+ table_name +' ("' + '","'.join(fields) +'", "year") VALUES ' +"\n" + ",\n".join(values) + ";" return query return None
def create_query(fn, columns, table_name): data = read_file(fn) if data: values = [] for row in data: v = '(' for col in columns: x = row[col] if col == "age.max" and x == "NA": x = "NULL" else: x = "'" + x + "'" v += x + "," v += str(year) + ")" values.append(v) fields = map(make_field_name, columns) query = 'INSERT INTO ' + table_name + ' ("' + '","'.join( fields) + '", "year") VALUES ' + "\n" + ",\n".join(values) + ";" return query return None
def new_with_example(id): example_md = read_file(example_md_path) mdir.save_md(id, example_md) return example_md
''' from lib.common import geo_levels, read_file, create_insert_query, get_geo_field_name, get_upper_levels, log import argparse parser = argparse.ArgumentParser(description='Generate SQL to import zip codes (zip geographic level)') parser.add_argument('file', nargs=1, help='file to import') args = parser.parse_args() fn = str(args.file[0]) log("Importing file " + fn) data = read_file('data/zip/' + fn) def create_levels(level, value): d = {} if level == 'nuts3': d['code_nuts3'] = value d['code_nuts2'] = value[0:4] if level == 'nuts2': d['code_nuts2'] = value n2 = d['code_nuts2'] d['code_nuts1'] = n2[0:3] d['country'] = n2[0:2] return d fields = None zips = []