def propsTranslator(separator, inputfile, languages):
    global CURR_LANGUAGE
    for curr in languages:
        CURR_LANGUAGE = curr
        props = Properties()
        with open(inputfile, 'rb') as f:
            props.load(f, encoding='utf-8')
            newProps = Properties()
            for k, v in props.items():
                print("translate: {} to language: {}".format(
                    v.data, CURR_LANGUAGE))
                response = client.translate_text(
                    parent=GOOGLE_PARENT_PROJECT,
                    mime_type="text/plain",
                    target_language_code=CURR_LANGUAGE,
                    contents=[v.data])
                trans = response.translations
                print("result: {} \n".format(trans[0].translated_text))
                newProps[k] = trans[0].translated_text

            base = os.path.basename(inputfile)
            filename = os.path.splitext(base)[0]
            with open(filename + separator + CURR_LANGUAGE + '.properties',
                      'wb') as newF:
                newProps.store(newF, encoding='utf-8')
Example #2
0
def test_setmeta_bytes():
    p = Properties()
    p["a key"] = "the value", {b"metakey": b"metaval", b"__internal": b"foo"}

    out = BytesIO()
    p.store(out, strip_meta=False, timestamp=False)

    out.seek(0)
    assert out.read() == b"#: metakey=metaval\na\\ key=the value\n"
Example #3
0
def test_setmeta_int():
    p = Properties()
    p["a key"] = "the value", {"metakey": 42}

    out = BytesIO()
    p.store(out, strip_meta=False, timestamp=False)

    out.seek(0)
    assert out.read() == b"#: metakey=42\na\\ key=the value\n"
Example #4
0
def test_setmeta_bytes():
    p = Properties()
    p["a key"] = "the value", {b"metakey": b"metaval"}

    out = StringIO()
    p.store(out, strip_meta=False, timestamp=False)

    out.seek(0)
    assert out.read() == b"#: metakey=metaval\na\\ key=the value\n"
Example #5
0
def test_setmeta_unicode():
    p = Properties()
    p["a key"] = "the value", {u"metakey": u"ünicode metävalue!"}

    out = StringIO()
    p.store(out, encoding="utf-8", strip_meta=False, timestamp=False)

    out.seek(0)
    assert out.read() == b"#: metakey=ünicode metävalue\\!\na\\ key=the value\n"
def test_setmeta_int():
    p = Properties()
    p["a key"] = "the value", {"metakey": 42}

    out = StringIO()
    p.store(out, strip_meta=False, timestamp=False)

    out.seek(0)
    assert out.read() == "#: metakey=42\na\\ key=the value\n"
Example #7
0
def test_setmeta_unicode():
    p = Properties()
    p["a key"] = "the value", {u"metakey": u"ünicode metävalue!"}

    out = StringIO()
    p.store(out, encoding="utf-8", strip_meta=False, timestamp=False)

    out.seek(0)
    assert out.read(
    ) == b"#: metakey=ünicode metävalue\\!\na\\ key=the value\n"
Example #8
0
    def _generate_properties_file(self):
        properties_file = os.path.join(CONFIG_PATH,
                                       "cassandra-rackdc.properties")

        cassandra_properties = Properties()
        cassandra_properties["dc"] = "dc1"
        cassandra_properties["rack"] = "rack1"

        with open(properties_file, "wb") as f:
            cassandra_properties.store(f, encoding="utf-8")
def test_load_write_file(datadir, tmpdir):
    p = Properties()

    with datadir["simple.utf8.prop"].open("rb") as fh:
        p.load(fh, "utf-8")

    with tmpdir.join("dump.prop").open("w+b") as out:
        p.store(out, encoding="utf-8", timestamp=False)

        out.seek(0)
        assert out.read() == b"tästkey=This is the value\nanotherkey=Not mäny välues in this file\n"
Example #10
0
def write_properties(configs, file, path=None):
    
    if path is None:
        path = '/opt'
    pfile = Properties()
    d_file = path + '/' + file + '.properties'
    if configs is not None:
        for k, v in configs.items():
            pfile[k] = v
        with open(d_file, 'wb') as f:
            pfile.store(f, encoding='utf-8')
def test_load_write_file(datadir, tmpdir):
    p = Properties()

    with datadir["simple.utf8.prop"].open("rb") as fh:
        p.load(fh, "utf-8")

    with tmpdir.join("dump.prop").open("w+b") as out:
        p.store(out, encoding="utf-8", timestamp=False)

        out.seek(0)
        assert out.read(
        ) == b"tästkey=This is the value\nanotherkey=Not mäny välues in this file\n"
def main(path):
    properties = Properties()

    with open(path, "rb") as f:
        properties.load(f, "utf-8")

    for key in properties.iterkeys():
        if os.environ.get(key.replace('.', '_')):
            properties[key] = os.environ[key.replace('.', '_')]

    with open(path, "wb") as f:
        f.truncate(0)
        properties.store(f, encoding="utf-8")
Example #13
0
def test_setmeta_unicode():
    p = Properties()
    p["a key"] = "the value", {
        u"metakey": u"ünicode metävalue!",
        u"__internal": u"foo"
    }

    out = BytesIO()
    p.store(out, encoding="utf-8", strip_meta=False, timestamp=False)

    out.seek(0)
    text = u"#: metakey=ünicode metävalue\\!\na\\ key=the value\n".encode(
        "utf-8")
    assert out.read() == text
Example #14
0
def _save_session_to(dir_path, session, filename, file_extension):
    fig_dir_ = session['figures.directory']
    if fig_dir_:
        session['figures.directory'] = os.path.join(car_figures_dir, fig_dir_)

    p = Properties()
    for key in session:
        p[key] = session[key]
    session_file_path = os.path.join(dir_path, filename + file_extension)
    with open(session_file_path, "w") as f:
        p.store(f, encoding="utf-8")
    msg = 'Session successfully stored as "' + filename + '"'
    _log_info(msg)
    return msg
def generateFromManualConfig(configPath):
    with open(configPath, 'rb') as manCfg:
        configList = json.load(manCfg)
        dotEnv = DotEnv()
        for config in configList:
            tmplCfg = PropertyTemplate(config['file'] + '.template')
            tmplCfg.updateExistingProperties(config['properties'])
            dotEnv.updateProperties(config['properties'])
            tmplCfg.generatePropertiesFile()
        print('Random MariaDB root password:'******'MARIADB_ROOT_PASSWORD'])
        p = Properties()
        p.properties.update(dotEnv.envs)
        with open(os.path.dirname(configPath) + os.path.sep + '.env', 'wb') as f:
            p.store(f, encoding='utf-8')
def change_config(config_remote, config_file, fn):
    from jproperties import Properties
    read_props = Properties()
    write_props = Properties()

    if config_file is None:
        return
    with open(config_file, "r") as file:
        read_props.load(file)
    write_props = copy_exist(write_props, read_props)
    write_props = fn(write_props, read_props, config_remote)
    with open(config_file, "w") as wfile:
        write_props.reset()
        write_props.store(wfile)
def _save_session_to(dir_path, session, filename, file_extension):
    fig_dir_ = session['figures.directory']
    if fig_dir_:
        session['figures.directory'] = os.path.join(car_figures_dir, fig_dir_)

    p = Properties()
    for key in session:
        p[key] = session[key]
    session_file_path = os.path.join(dir_path, filename + file_extension)
    with open(session_file_path, "w") as f:
        p.store(f, encoding="utf-8")
    msg = 'Session successfully stored as "' + filename + '"'
    _log_info(msg)
    return msg
def test_surrogate_roundtrip_utf8():
    p = Properties()
    p["surrogate"] = u"Muuusic \U0001D160"

    out = StringIO()
    p.store(out, encoding="utf-8", timestamp=None)

    out.seek(0)
    dumped = out.read()
    assert dumped == b"surrogate=Muuusic \xF0\x9D\x85\xA0\n"

    p2 = Properties()
    p2.load(dumped, "utf-8")

    assert p2["surrogate"] == (u"Muuusic \U0001D160", {})
def test_surrogate_roundtrip(out_encoding):
    p = Properties()
    p["surrogate"] = u"Muuusic \U0001D160"

    out = StringIO()
    p.store(out, encoding=out_encoding, timestamp=None)

    out.seek(0)
    dumped = out.read()
    assert dumped == b"surrogate=Muuusic \\ud834\\udd60\n"

    p2 = Properties()
    p2.load(dumped, out_encoding)

    assert p2["surrogate"] == (u"Muuusic \U0001D160", {})
Example #20
0
class PropertiesService:
    def __init__(self, path, encoding="utf-8"):
        self._properties = Properties()
        self._path = path
        self._encoding = encoding
        with open(path, "rb") as f:
            self._properties.load(f, encoding)

    def get_property(self, key):
        prop, meta = self._properties[key]
        return prop

    def save_property(self, key, value):
        self._properties[key] = value
        with open(self._path, "wb") as f:
            self._properties.store(f, self._encoding)
Example #21
0
def property_parser(schema, source, path, value):
    with open(source, 'rb') as file:
        content = Properties()
        content.load(file, 'utf-8')
        k = '.'.join(path)
        try:
            old_value, _ = content[k]
        except Exception as e:
            Log.get('property-parser').exception(f'Parsing property {k}', e)
            old_value = None
        if old_value != value:
            content[k] = value
            with open(source, 'wb') as file:
                content.store(file, encoding='utf-8')
                return {'value': {'new': value, 'old': old_value}}
        else:
            return {'note': NO_CHANGE_NEEDED}
Example #22
0
def set_entry_dict(contents: Dict[str, str], file_path: str):
    """Sets the property file to the key-value pairs of the contents dictionary.

    Args:
        contents (Dict[str, str]): The dictionary whose contents will be the key value pairs of the properties file.
        file_path (str): The path to the properties file to create.
    """

    props = Properties()
    for key, val in contents.items():
        props[key] = val

    parent_dir, file = os.path.split(file_path)
    if not os.path.exists(parent_dir):
        os.makedirs(parent_dir)

    with open(file_path, "wb") as f:
        props.store(f)
    def getId(self, properties):
        """Function to get a new ID each time a file is created
		
		Returns:
			[integer] -- ID
		"""
        # Read last availaible id
        p = Properties()
        with open(properties, 'rb') as f:
            p.load(f, 'utf-8')
        id = str(int(p["video_id"].data))
        # Increment the id in the file
        p["video_id"] = str(int(p["video_id"].data) + 1)
        with open(properties, 'wb') as f:
            p.store(f, encoding="utf-8")

        # Return availaible id
        return id
def test_euro(out_encoding):
    p = Properties()

    # The euro symbol is not present in ASCII/ISO-8859-1,
    # so it should be encoded as "\u20ac".
    p["test"] = u"Euro: €"

    out = StringIO()
    p.store(out, encoding=out_encoding, timestamp=None)

    out.seek(0)
    assert out.read() == b"test=Euro\\: \\u20ac\n"

    # Read it back again:
    out.seek(0)
    p2 = Properties()
    p2.load(out)

    assert p2.properties == {u"test": u"Euro: €"}
Example #25
0
def test_euro(out_encoding):
    p = Properties()

    # The euro symbol is not present in ASCII/ISO-8859-1,
    # so it should be encoded as "\u20ac".
    p["test"] = u"Euro: €"

    out = StringIO()
    p.store(out, encoding=out_encoding, timestamp=None)

    out.seek(0)
    assert out.read() == b"test=Euro\\: \\u20ac\n"

    # Read it back again:
    out.seek(0)
    p2 = Properties()
    p2.load(out)

    assert p2.properties == {u"test": u"Euro: €"}
Example #26
0
def load_world_and_config():
    # Get current properties
    props = Properties()
    with open(properties_path, "rb") as f:
        props.load(f, "utf-8")

    # Select world
    world_list = [x.name for x in Path(worlds_path).iterdir() if x.is_dir()]
    level_name = inquirer.prompt([
        inquirer.List("level-name",
                      message="Select a world",
                      choices=world_list,
                      default=props["level-name"].data),
    ])["level-name"]

    # Load world's configuration
    world_props_path = Path(worlds_path, level_name + ".properties")
    world_props = Properties()
    if world_props_path.is_file():
        with open(world_props_path, "rb") as f:
            world_props.load(f, "utf-8")
    else:
        print("No configuration found for this world\n")
    world_props["level-name"] = level_name
    props.update(world_props)

    print("Writing following config to server.properties:")
    pprint.pprint(world_props.properties)
    print()
    with open(properties_path, "wb") as f:
        props.store(f, encoding="utf-8")

    # Prompt for restart/start
    action = inquirer.prompt([
        inquirer.Confirm(
            "action",
            message=f"{'Restart' if server_is_running() else 'Start'} server?",
            default=True),
    ])["action"]
    if action:
        restart_server() if server_is_running() else start_server()
Example #27
0
def property_parser(schema, source, path, value):
    with open(source, 'rb') as file:
        content = Properties()
        content.load(file, 'utf-8')
        k = '.'.join(path)
        try:
            old_value, _ = content[k]
        except Exception as e:
            Log.get('property-parser').exception(e)
            old_value = None
        if old_value != value:
            content[k] = value
            with open(source, 'wb') as file:
                content.store(file, encoding='utf-8')
                return dict(schema=schema,
                            source=source,
                            path=path,
                            value=dict(new=value, old=old_value))
        else:
            return dict(schema=schema,
                        source=source,
                        path=path,
                        value=value,
                        note=NO_CHANGE_NEEDED)
Example #28
0
def processJavaParameters(params, fh):
    """
    Take parameters, get all of their keys and values, and write them to the
    output file handle as Java property values.

    Args:
        params (Iterator): generator for the parameters.
        fh (file-like): An open, writeable file handle to output the Java properties values.
    """
    props = Properties()
    for param, basePath in params:
        paramPath = param['Name'][len(basePath):]
        m = JAVA_PARAMETER_NAME_RE.search(paramPath)
        if not m:
            logger.warning(
                '%(Name)s: skipping parameter because of invalid Java property name',
                param)
            continue
        name = m.group('name')
        value = param['Value']

        if param['Type'] == 'SecureString':
            logger.debug('%(path)s: %(name)s = ********', {
                'path': param['Name'],
                'name': name
            })
        else:
            logger.debug('%(path)s: %(name)s = %(value)s', {
                'path': param['Name'],
                'name': name,
                'value': value
            })

        props[name.replace('/', '.')] = value

    props.store(fh, encoding="utf-8")
Example #29
0
                        type=str,
                        help='Kafka Server Template')
    parser.add_argument('--server_count',
                        '-n',
                        default=5,
                        type=int,
                        help='Kafka Number of Server')
    parser.add_argument('--logs_dir',
                        '-cd',
                        default='/tmp/kafka-logs',
                        help='hbase logs dir')
    args = parser.parse_args()

    p = Properties(process_escapes_in_values=False)

    p.load(open(args.config_template, 'rb'), 'utf-8')

    for i in range(0, args.server_count):
        # p['broker.id'] = str(int(p['broker.id'].data) + 1)
        p['broker.id'] = str(i)
        p['port'] = str(9092 + i)
        p['log.dirs'] = '{}/{}'.format('/tmp/kafka-logs', i)
        # p['zookeeper.connect'] = 'localhost:2181'
        # p['advertised.host.name'] = 'localhost'
        # p['listeners'] = 'PLAINTEXT://localhost:{}'.format(9092 + i)
        # p['delete.topic.enable'] = 'true'

        with open('./configs/server-{}.properties'.format(i), 'wb') as f:
            p.store(f, strip_meta=False)
            f.close()
Example #30
0
test = os.environ['bamboo_planRepository_repositoryUrl']

print("API BUILD NUMBER IS ===========> " + build)
print("API REPO NAME IS ===========> " + test)

# maven clean package to create war file
subprocess.call(["mvn", "clean", "package"], shell=True)

#upload war file into artifactory location
url = os.environ[
    'bamboo_artifactory_url'] + "com/services/{0}/{1}-0.0.1-SNAPSHOT.war".format(
        build, planName)
file_name = "target/{0}-0.0.1-SNAPSHOT.war".format(planName)
auth = (os.environ['bamboo_artifactory_username'],
        os.environ['bamboo_artifactory_password'])
with open(file_name, 'rb') as fobj:
    res = requests.put(url, auth=auth, data=fobj)
    print(res.text)
    print(res.status_code)

#Update build number and repo name in the properties file.
p = Properties()
p["BUILD_NUMMBER"] = build
p["REPO_NAME"] = planName
with open("bambo-test.properties", "wb") as f:
    p.store(f, encoding="utf-8")

print("==================API Build Ends ====================")

#curl -u admin:Welcome@123 -X PUT -F "data=@target/employeservice-0.0.1-SNAPSHOT.war" http://localhost:8082/artifactory/DEMO/com/services/build/employeservice-0.0.1-SNAPSHOT.war
Example #31
0
def writeproperties(path, **kwargs):
    p = Properties()
    for k, v in kwargs.items():
        p[k] = v
    with path.open('wb') as f:
        p.store(f)
    def save_properties(self, p: Properties):

        with open(self.get_properties_path(), 'wb') as f:
            p.store(f, encoding='utf-8')

        return
Example #33
0
from ec2_metadata import ec2_metadata
client = boto3.client('secretsmanager', region_name=ec2_metadata.region)

if (len(sys.argv) == 1):
    print("No secrets to be mounted, exiting")
    sys.exit(0)

secret_names = sys.argv[1].split(",")
out_directory = sys.argv[2]
file_type = sys.argv[3]

values = {
    secret["Name"].replace('/', '.'): secret["SecretString"]
    for secret in map(lambda name: client.get_secret_value(SecretId=name),
                      secret_names)
}
file_name = "%s/secrets.%s" % (out_directory, file_type)

if file_type == "yaml":
    yaml.dump(unflatten(values), open(file_name, 'w'), explicit_start=True)
elif file_type == "json":
    json.dump(unflatten(values), open(file_name, 'w'))
elif file_type == "toml":
    toml.dump(unflatten(values), open(file_name, 'w'))
else:
    properties = Properties()
    properties.properties = values

    with open(file_name, "wb") as out_file:
        properties.store(out_file, strict=True)
 def generatePropertiesFile(self):
     p = Properties()
     p.properties.update(self.existingProps)
     with open(self.destPath, 'wb') as f:
         p.store(f, encoding='utf-8')