Boost.Locale translation - Preventing user modifications to dictionaries / embed dictionaries in executable - c++

I use Boost.Locale with ICU backend (internally using GNU gettext) to do the translations. This translation uses dictionaries stored on disk. Search paths are provided through boost's generator class like so:
boost::locale:generator gen;
gen.add_messages_path("path");
By inspecting the boost's source code, this internally later passes the paths to localzation backend through localization_backend::set_option. In case of ICU localization backend implementation that I use, this finally makes the paths to be set in gnu_gettext::messages_info (paths field).
Now as for my question - my requirement is to make sure that the user will not change the texts e.g. by modifying the .mo dictionary file on disk. The reason I use Boost.Locale is its codepage translations support, multiple languages support etc. and I want to keep that, but I don't want the ability for the user to freely define the texts later used in the application. My initial thought was to use the dictionaries "in memory" in some way, e.g. by storing .mo file contents inside executable and pass already read data into the localization_backend somehow. However, after checking how it works internally (described above) it seems that the only supported option is to have the dictionaries read in "real time" as I do the translations, which may include any changes to those files done by the user. It's either that or maybe I'm missing something?
What are my options?

You can use the callback field on gnu_gettext::messages_info to provide a function that will be called instead of loading messages files from disk. From Custom Filesystem Support:
namespace blg = boost::locale::gnu_gettext;
blg::messages_info info;
info.language = "he";
info.country = "IL";
info.encoding="UTF-8";
info.paths.push_back(""); // You need some even empty path
info.domains.push_back(blg::messages_info::domain("my_app"));
info.callback = some_file_loader; // Provide a callback
The callback signature is std::vector<char>(std::string const &file_name, std::string const &encoding). There's an example in the tests; this actually loads from disk, but you can adapt it to return hard-coded data instead.

Related

GIMP script-fu, can "file-glob" return only files with particular extentions?

I'm trying to use python-fu in GIMP. I would like pdb.file_glob to return an array of image files in the format I specify. I tried:
myGlob = "*.png|*.PNG|*.jpg|*.JPG|*.gif|*.GIF|*.xcf|*.XCF"
globpath = os.path.join(patternDir, myGlob)
num_files, files = pdb.file_glob(globpath, 0)
But the files array is always empty, I assume because the glob syntax is invalid.
Note that if I use myGlob="*", I get the graphics files I want, but I also get files such as "fake.txt", which I want to exclude.
The doc of all PDB functions can be found via the Python-fu console. Hit the Browse... button and then enter your search in the filter bar at the top of the left pane. This documentation is dynamic, it includes the documentation of any callable plugin/script (as long as authors have written some of course)
The PDB functions for Python are a direct mapping of the script-fu API. In this specific case file_glob() was very recently added to the script-fu API because there is nothing in the base TinyScheme language to do it. In Python, you are better off using the standard Python API, os.walk() or glob.glob()/glob.iglob().
In any case such functions only do simple pattern matching, if you want several extensions you want something like this:
sorted([filename for ext in ['XCF','xcf','jpg','JPG','jpeg','gif','GIF'] for filename in glob.glob('*.'+ext)])
Edit: this is a "comprehension", more or less a loop with the inner instruction outside. You can read it as:
files=[]
for ext in ['XCF','xcf','jpg','JPG','jpeg','gif','GIF']:
for filename in glob.glob('*.'+ext):
list.append(filename)

How to get a list of members out of an IBM i file?

New to using IBM i here, and haven't been able to find the solution to this problem in the Knowledge Base.
I have a library. Inside that library I have a file. Inside that file I have several members. Every member is a .c file.
What I want to do is write a .cpp that looks inside that file at the members, get each member's name, and then run CL stuff with the member.
Using qp0z1170.h I am confident I know how to run the CL commands, but I can not seem to figure out how to iterate through the list of members and get their names.
Any help would be greatly appreciated.
You could do a DSPFD to get the member list to an outfile...
DSPFD FILE(LIB/FILE) TYPE(*MBRLIST) OUTPUT(*OUTFILE) OUTFILE(LIB/TARGET)
Sounds like you're dealing with a source physical file (PF-SRC)...
Luckily, a PF-SRC is still a database table..
Just use the List Database File Members (QUSLMBR) API
Note that it returns data via a user space...so you're going to need to use the *USRSPC API's to process the data.
Create User Space (QUSCRTUS) API
Change User Space Attributes (QUSCUSAT) API
Retrieve Pointer to User Space (QUSPTRUS) API
Delete User Space (QUSDLTUS) API
You can find include files for the IBM API's in the H file in the QSYSINC library.
I'd post some code, but I've only ever used the APIs from RPG.

How do I provide default YAML configuration values in a static library?

I have a configuration file system written in C++ which uses the yaml-cpp library to parse and write to YAML files. I have this as part of my static library.
I would like the ability to return a default value for a field that is requested by a user of the library (calling from their code), but which has not been defined in the user's YAML file.
For example say the user wants to use the field foo from their custom config.yaml file:
int bar = config_reader.read<int>( "config.yaml", "foo" );
If they have foo: 10 in their config.yaml then bar will be set to 10. However I would also like to provide a default value (for example 4) in the case where foo is omitted from config.yaml.
There are two possibilities I have thought of:
Have a set of static maps between field names and default values in a cpp file which gets compiled into the static library, however I will need to have different maps for different types and I feel this could get messy with type checking and maybe requiring template specialization methods.
Have a YAML file which contains all of the default values for expected fields, which the configuration system falls back on if it cannot find the field in the user's config file. I think this would be the preferred solution for me, but I cannot think of a neat way of packaging this YAML file. I would rather the user didn't have to copy or point to this file each time they set up a new project linking the static library.
I would provide the defaults in a YAML file in a global (i.e. non-user specific place) and allow to override the values with user-specific ones.
Consider just throwing an error if the global defaults are missing an entry, this will not happen by accident.
The global defaults you can put in /etc/default/YOUBLIBNAME.yaml. The user's configuration nowadays mostly follows the XDG base directory specification. For that use $XDG_CONFIG_HOME/YOURLIBNAME/config.yaml if XDG_CONFIG_HOME is set in the environment, if not set use $HOME/.config/YOURLIBNAME/config.yaml.
If your library has to work under Windows, I would put the user specific data under %APPDATA% in a subdir YOURBLINAME.

Changing between two .properties with a same template in FreeMarker

I am new to FreeMarker, and what I would like to accomplish is switch between different properties using the same template file, in order to send e-mails in different languages.
My default properties file is messages.properties, and the second file (non-default) I would like to use is messages_fr.properties.
A template file is template.ftl
The code I have is:
Map<String, Object> content = new HashMap<String, Object>();
content.put(I18N, i18nService);
content.put("someText", "a text";
content.put("language", language);
String setBodyTextMessage = commonService.getProcessedTemplate("template.ftl", content);
What I am struggling to find out is how to do the same thing as in the code I provided, but by using the messages_fr.properties without changing the configuration file (I would like to make the switch within the Java code). Furthermore, setting a different locale will not work either.
You have a data-model there, called content. You put into that whatever you want, with Java code. Each template processing can use its own data-model. So I'm not sure where do you stuck.
Or, you can create a ResourceBundle and put that into the data-model (even as shared variable on the Configuration-level), and then set the locale setting for that single request (not in the shared Configuration object). To do that, instead of Configuration.getTemplate use Template.createProcessingEnvironment. In the returned freemarker.core.Environment you can set the locale (and other settings), and then call Environment.process to generate the output.

How to use Matplotlib images in Django templates?

Background: I'm starting off with Django, and have limited experience with Python, so please bear with me. I've written a Python script that runs periodically (in a cron job) to store data into a SQLite3 database, from which I'd like to read from and generate images with Matplotlib (more specifically, with Basemap). This started off as an interest in learning Python and building an "interesting" enough project. I'm picking the Django framework because it seems reasonably-well documented, although I was pleasantly surprised by web.py because of its "lightweightness" in its requirements (but web.py's sparse documentation makes it a bit harder to start off with); but at the moment, I'm not entirely dead-set on a framework.
The example in question 1874642 is almost what I'm looking for, with an image being generated on-the-fly without requiring having to write it to disk (and thusly having to deal with periodically cleaning up the generated files).
However, what is not clear to me is how the generated image can be incorporated in a template, instead of having the browser simply showing the image. From the tutorial material, I'm guessing that it should be possible to represent the variables incorporated in some django.template.Context into the django.http.HttpResponse, but the referenced example shortcuts it by responding directly with a Mime object instead of building it with a Context.
So what I'm asking is:
Is it necessary to invoke a print_png on the generated Matplotlib FigureCanvas object? Or is the FigureCanvas copyied "unprinted" to the Context, so that in the Django template I explicity write the HTML img tag and put by hand the tag's attributes?
I'm under the impression that I have to write the Canvas to disk (i.e. do a canvas.print_figure("image.png")), so that the HTML img tag sees it in the Django template. But I want to be sure that there isn't a "more manageable way" -- i.e. passing the image in the Context and having the template "magically" generate it. If it's really necessary to write to disk, I suppose I could use Django's filesystem caching facility to write the generated images in some way (checking whether an image was already written for a given input parameter set, of course). I welcome your suggestions on this regard, since it's not yet clear at this time the size and number of the images that will be generated, and thusly I'm looking to avoid spending disk space and instead prefer waiting for an image to be generated (even if it takes a few seconds).
Thank you in advance.
you can pass a StringIO object to pyplot.savefig(), and get the PNG file content by StringIO.getvalue().
This view would serve a PNG image. Just bind it to some URL like "img.png" and use that in an img tag.
def create_fig(request):
# MPL stuff
response = HttpResponse(content_type='image/png')
fig.savefig(response)
return response
Of course, that assumes that you can generate the image independent of the main view. You can pass arguments to the image, like (in urls.py):
url(r'^img(?P<nr>\d+).png$', create_fig),
which passes a (string repr. of) a number nr to create_fig.