One .po file for each .py script - python-3.x

In my package, I would like to use one .po file for each .py script it contains.
Here is my file tree :
foo
mainscript.py
commands/
commandOne.py
locales/fr/LC_MESSAGES/
mainscript_fr.po
commandOne_fr.po
In the mainscript.py, I got the following line to apply gettext to the strings :
if "fr" in os.environ['LANG']:
traduction = gettext.translation('mainscript_fr', localedir='./locales', languages=['fr'])
traduction.install()
else:
gettext.install('')
Until now, it is working as expected. But now I would like to add another .po file to translates the strings in commandOne.py.
I tried the following code :
if "fr" in os.environ['LANG']:
traduction = gettext.translation('commandOne_fr', localedir='../locales', languages=['fr'])
traduction.install()
else:
gettext.install('')
But I get a "FileNotFoundError: [Errno 2] No translation file found for domain: 'commandOne_fr' "
How can I use multiple file like that ? The package being a cli, there is many strings in a single file because of the help man and verbose mode...etc and this is not acceptable to have a single .po file with hundreds of strings.
Note : The mainscript.py calls a function from commandOne.py, which is itself inherited from an abstract class that contains other strings to translate... so I hope if any solution exists that it will also be applicable to the abstract class file.
Thank you

Translations are retrieved from .mo files, not .po files, see https://docs.python.org/3/library/gettext.html#gettext.translation. Most probably you have to compile CommandOne_fr.po with the program msgfmt into CommandOne_fr.mo.
Two more hints:
What you are doing looks like a premature optimization. You won't have any performance problem until the number of translations gets really big. Rather wait for that to happen.
Why the _fr in the name of the translation files? The language code fr is already a path component.

Related

why an exact copy with "open" doesn't produce a valid binary file?

I noticed that an exact copy of an *.exe file does not quite work as expected opening the file as binary:
with open("input.exe", "rb") as f1:
bs = f1.read()
with open("output.exe", "wb") as f2:
# Copy byte by byte
f2.write(bs)
The resulting "output.exe" crashes at startup.
I know that I'm being quite vague here, but from a theoretical point of view, am I missing something? I even checked and the md5 checksum of both files is the same, but still "input.exe" runs whereas "output.exe" does not.
P.S. at the beginning I wanted to encode "input.exe" as a txt file and the decoding it back to a binary file, but in the end the issue boils down to the code above.
P.S That same exact code works for other files type, like images (*.jpg)
I found the problem, the output file name should have had a particular name.

Get a top level from Path object of pathlib

I use pathlib to match all files recursively to filter the files based on their content. Then I would like to find what is the top level of the folder of this file. Assume the following. I have a file in the folder:
a/b/c/file.log
I do the search from the level a:
for f in path_data.glob("**/*"):
if something inside file f:
# I would like to get in what folder this file is, i.e. 'b'
I now that I can get all parents levels using:
f.parents would give me b/c
f.parent would give me c
f.name would give me file.log
But how could I get b?
Just to precise: the number of levels where the file is stored is not known.
UPD: I know I could do it with split, but I would like to know if there is a proper API to do that. I couldn't find it.
The question was asked a while ago, but didn't quite get the attention. Nevertheless, I still would publish the answer:
f.parts[0]

Output other than .txt

I'm looking to build a simple program that will simply modify existing output files from an other program so I don't have to open the program and enter a bunch of data the long way. This program is very specific to my domain and has an extension named .wcc. However, when I change the extension of one of these output files to .txt, I get half gibberish :
ÿÿ WPointÿÿ WPolygonÿÿ  WQuadrilateralÿÿ  WMemberDataÿÿ
WLoadÿÿ WLStandardMembersÿÿ WLSavedDesignSettingsÿÿ WLSavedFormatSettingsÿÿ  WLSavedViewSettingsÿÿ WLSavedProjectSettingsÿÿ  WLSavedSettingsÿÿ  WLSavedLoadSettingsÿÿ WLSavedDefaultSettingsÿÿ WLineÿÿ WProductÿÿ WBeamDataÿÿ  WColumnDataÿÿ
WJoistDataÿÿ
WWallStudDataÿÿ WSupportingMemberDataÿÿ WSavedAnalysisSettingsÿÿ WSavedGravityDesignSettingsÿÿ WSavedPreferencesSettingsÿÿ WNotchÿÿ WIJoistÿÿ WFloorCWC37 ÀAE LumberS-P-F No.1/No.2 # À# lumwall.cww ÿÿÿÿ1.2.3.1.Mur_1_EX-D ÿÿÿÿÿÿ B Cÿÿ B C €? 4C 4C   Neige #F #F ÈC ÿÿÿ
WLStandardMembersÿÿ "
There are also musical notes and perpendicular signs which I can't copy paste here. I can sorta read the text, but still not enough to make modifications via txt file. What type of file could this be? Is it even possible to do what I'm trying to do? Thanks!
I am surprised that you are trying to open a .wcc file as a text file (it's contents - as you will see - don't lend themselves to being converted to such a file type); however, the attempt to open the file as a .txt file seems to be specific to your domain.
I noticed part of your question is as follows: "What type of file could this be?"
You are right in thinking that the .wcc file is a rather obscure file type - we don't think about that file type a lot (or are not conscious of it existing). A .wcc file is a WinCam 2000 Cache file that allows WinCam 2000 movies to be previewed in the slide browser - these were often generated by older WinCam 2000 screen recording and editing programs.
Again, the file extension is very rare these days (a Google search only returns ~700 results). But, it appears you have a program that is producing the file, which - as you are saying - "is quite specific to your domain". You may be out of luck with regard to opening them for modification purposes.
Supposedly, you can covert .wac files to .wav files, which are much more relevant to today's technology (and definitely alterable from code); however, without knowing the purpose of the file, e.g. what you are trying to do with the file domain-side, I can't say that this will suit your needs.
Also, the above comments are "correct": changing a file extension will not convert the file to the file extension type. Typically, converters - like a simple software - are needed to convert files.

echo of var to file in SConscript

In an SConscript file I have a env variable BUILDID_STR
that contains a C string that I wish to output to a file.
bstr = env['BUILDID_STR']
print(bstr)
which when printed, print(bstr) correctly shows this:
//this file is automatically generated
static char* build_str="0|0.1.0|2014-05-29_16:16:51";
However, I can't get the var expanded/exported correctly, just the literal string is output instead of the above text:
cat src/log/src/version.c
env[BUILDID_STR]
Here's the pertinent part of my SConscript file
env.Command(target='#/src/log/src/version.c',
source=libSrcfiles,
action="echo env['BUILDID_STR'] > $TARGET")
env.SharedLibrary('log', [libSrcfiles, '#/src/log/src/version.c'])
I've also tried the code in a function and also passing to a shell script, all with the same result.
The reason I have .../version.c in the SharedLibrary is that my goal is to have the .c file generated only when on of the libSrcfiles is built, thereby version.c is compiled-in.
The "textfile" Tool offers two Builders Textfile() and Substfile() for cases like this. You probably want to use the first:
env = Environment(tools=['textfile'])
env['BUILDID_STR'] = 'A test'
env.Textfile('test.txt', ['$BUILDID_STR'])
As you have seen, the action is not expanding the env variable. In the action, you can refer to env variables with the $VAR syntax (just like referring to the $SOURCE and $TARGET variables provided by SCons):
env.Command(target='#/src/log/src/version.c',
source=libSrcfiles,
action="echo $BUILDID_STR > $TARGET")
This solution may not handle a BUILDID_STR containing multiple lines.
You may want to investigate a system that builds your source file from a template (instead of constructing the file contents entirely within a string). The Substfile Builder referenced in this previous question might be a good starting point -- you can provide a list of (key, value) pairs to be substituted in an input file. The Substfile Builder is built into recent versions of SCons.

Jenkins - Dynamic Choice Parameter - Removing File extension from list

I am having a bit of trouble getting my groovy code to work properly in Jenkins using the Dynamic choice parameter. We currently have a folder that contains a lot of properties files for various environments. The following groovy code returns a list of all the file names correctly, however it is appending the file extension which is unneeded.
Arrays.asList(new File("path").list())
How would I change that to only list .xml files and not append the file extension in the list. I've found some examples of this while searching, but for some reason when I try some of these examples it isn't populating the list.
You mean like:
new File( 'path' ).list()
.findAll { it.endsWith( '.xml' ) }
.collect { it[ 0..-5 ] }
That gets the list of files (as Strings), keeps those that end with .xml, then removes the .xml off the end

Resources