reload translation file in pyramid web framework - translation

I develop a translation helper page in my pyramid based application.
A new translation is saved in .po and .mo files via this:
po = polib.pofile(join(root, 'locale', lang, 'LC_MESSAGES', 'myapp.po'))
.....
po.save(join(root, 'locale', lang, 'LC_MESSAGES', 'myapp.po'))
po.save_as_mofile(join(root, 'locale', lang, 'LC_MESSAGES', 'myapp.mo'))
But the new translation does not take effect until I restart the application.
I need to reload the translation file right after the new translation file is saved without restarting the wsgi application.

Translations are cached for performance in Pyramid but you can setup pserve to automatically restart the wsgi server when the files change. Just add watch_files directive to your ini file. The watcher uses python glob module and so what is supported depends on if you're on python2 or python3 but you can experiment to find a regex that works. (For example, ** is not supported on python 2).
[pserve]
watch_files = myapp/locales/**/*

Related

What is the different between PATH=:$PATH and PATH="$PATH:" and other export lines

I have a question about following lines related to adding PATH to enviroment.
export PATH=/usr/loca/cuda/bin:$PATH
export PATH=/usr/local/cuda-9.1/bin${PATH:+:${PATH}}
export PATH="/home/ics_vr/anaconda3/bin:$PATH"
export PATH="$PATH:/home/user/anaconda3/bin"
Regardless the content of path in each export lines, my first question is how do I distinguish thoes lines starting with export PATH=? e.g. grammer and its functions, regardless the variable I used in thoes lines.
Secondly, I see many people use # to comment on/off to switch those path,but this is not convenient. Is there any union way to realize all, without commenting the export line every time?
This is convenient because people want to use system python for example as default, but if the path is settled not properly, anaconda python interpreter will be settled by default. We need a way that default is the system python interpreter, and when I need anaconda, I will use
source activate ENV_I_BUILD
Thank you for your time and help. I am very appreciate on that.
The environment variable PATH is a list of colon separated folder paths where to find executables.
The order in which folder paths are places in this variable is very important. Indeed, if you call a program from the command line, the executable will first be searched in the first folder path, then if it's not there the second and so on...
Anaconda ships with a python installation (either 2.x or 3.x).
If you export:
export PATH="/home/ics_vr/anaconda3/bin:$PATH"
then the python in "/home/ics_vr/anaconda3/bin/anaconda3" will be used preferentially. Thus, if you want to keep the system python by default, you might want to use:
export PATH="$PATH:/path/to/whatever/conda"
The source activate ... will prepend the environment bin folder in your PATH anyway. So if you activate an environment, the system python will be superseeded by the python of the conda env.
As for the two lines:
export PATH=/usr/loca/cuda/bin:$PATH
export PATH=/usr/local/cuda-9.1/bin${PATH:+:${PATH}}
you will have to decide what executables you want first in your PATH variable.
For information, you can set multiple folders in your PATH in one line:
export PATH="$PATH:/usr/loca/cuda/bin:/home/ics_vr/anaconda3/bin:/my/personal/bin"
Do not forget to add what was already in your PATH variable when exporting a new PATH if you do not want to loose basic commands listed in, for example, "/usr/bin" or "/usr/local/bin".

Lua: relative import fails from different working directory

I'm trying to repackage into a Docker container some Lua library that is made of the main module and some helper modules. The helper modules are kept inside a subfolder of the library so that imports from the main file are done as
require 'helpers/SomeHelper'
The problem is: because of the way I want the Docker container to work, it would be extremely helpful if I can invoke this library from a different working folder. That is, my call to the main program would be something like
th /app/main.lua
regardless of the actual working directory I'm standing. Unfortunately, relative imports seem to fail when the working directory is different from the directory where the main file is located.
Is there any way I can configure LUA_PATH or any other mechanism to make these imports work correctly? Note that changing the code of the library itself would be a poor solution, as it wasn't developed by me and I would like to be able to update it to newer versions easily.
If you don't care about the working directory, you can just load lfs / LuaFileSytem and use lfs.chdir( src_dir ) to change to the source directory (potentially saving the current working directory with lfs.currentdir( ) first.)
You can also extend the search path of Lua so that it will search those extra directories. The search is driven by package.searchpath. To add a directory /foo/bar/ to the search in a way that supports all normally supported library layouts, add
/foo/bar/?.lua;/foo/bar/?/init.lua to package.path
/foo/bar/?.so (or .dylib or .dll on other OSen) to package.cpath
You can use several ways to extend the path.
One option that works well is to set the LUA_PATH / LUA_CPATH environment variables. (A ;; sequence in one of them will expand to the full default path.) This can be done from .profile or other setup scripts via an earlier export LUA_PATH="..." or (if started from a wrapper script) inline by setting variables just for that call LUA_PATH="..." lua /foo/bar.lua. (Note that if you export this variable in too broad a scope, other Lua scripts will also get their path extended and may find potentially incompatible Lua libraries.)
(You can also manually modify package.(c)path from LUA_INIT. That way, you won't be able to independently disable LUA_INIT or LUA_PATH, but you can use all of Lua to generate the path dynamically.)
A third option (this may be best in your specific case) is to put the extension of package.path at the top of your main script, as in
do
local dir = (arg[0]:match "^(.*)/$")
if dir then -- else cwd is . which works by default
package.path = dir.."/?.lua;"..dir.."/?/init.lua;"..package.path
package.cpath = dir.."/?.so;"..package.cpath
end
end
-- rest of your program goes here
When running a script with the Lua interpreter, arg[0] is the script. So this extends the path to include the program's directory no matter where it is located, and it will only affect the search path of this particular script / program.
You should not forget about that not all modules are loaded from FS directly.
E.g. to improve perfomance it is possible read/compile file to memory and then
use preload table to provide way to load module from memory.
Basic example
--- preload code. It can be done by host application.
local FooUtils = function()
return {
print = function(...)
print("foo", ...)
end
}
end
local Foo = function()
local Utils = require "foo.utils"
return {
foo = function()
Utils.print"hello"
end
}
end
package.preload['foo.utils'] = FooUtils
package.preload['foo'] = Foo
--- main application
require "foo".foo()
In this example assume that FooUtils and Foo just example of compiled modules.
E.g. it can be like FooUtils = loadstring('path/to/utils.lua) and it can be done
even in separate Lua state and then used in any other.
It is important to remember that Foo module have no idea about how host application does lookup of foo.utils.
So there no standart way to provide original file path or relevant paths.
So if you write some module wich relays on relative paths then this module
may be not working in some environments.
So I just suggest use full namespace like require 'foo.utils' instead of require 'utils'

How to load the resource file in Erlang

What is a usual way of storing and loading resource file in Erlang. I need to create a certain human-readable dictionary and load it at application initialization. For example, in Java I would put the data in a .property file, then put it somewhere in the classpath and finally load it with help of code like this:
new Properties().load(Class.getResourceAsStream("/file.properties"))
So, I have the following questions:
where I can (must) keep the resource file?
how to determine in runtime the path to the resource file
how to load it (for example file:consult(Filename))
In Erlang properties are in *.config file, that usually is (but doesn't have to be) in the root directory of your project. For example:
Chicago Boss has boss.config
RabbitMQ has rabbitmq.config
Zotonic has different configs for different sites stored in priv/sitename/config
You can provide config file by running
erl -config myconfig
WARNING: the file should be named "myconfig.config" and you should omit the extension.
The config file should be structured this way:
[{Application1, [{Par11,Val11},...]},
...,
{ApplicationN, [{ParN1,ValN1},...]}].
for example:
[{kernel, [
{my_key, "value"}
]}].
Than in erlang shell, you can type:
application:get_env(kernel, my_key).
{ok,"value"}
I used kernel application, because it is always loaded and application:get_env/2 returns undefined, if the application is not loaded. You should put any configs in your own application and make sure, that it is loaded before invoking get_env/2.
Also, configs are hierarchical, you can put the defaults in *.app file, that user usually doesn't have to modify. You can overwrite them in config file and finally, you can provide the key value pairs in command line (they will overwrite things, that are in config file).
You can read more about configuration here:
http://www.erlang.org/doc/design_principles/applications.html#id74398
You can also make config file more user friendly by using comments, example:
https://github.com/ChicagoBoss/ChicagoBoss/blob/master/skel/boss.config
I found the answer myself. The prefered path to store resource files is a priv directory. code:priv_dir/1 returns the path to the priv directory in an application.
Here is a code snippet to load JSON from the file:
File = filename:join([code:priv_dir(application), "resource.json"]),
{ok, Text} = file:read_file(File),
%% parse json

Using custom Environment Variables in JetBrains products for File watcher Arguments

I am trying to use node-sass as File Watcher in WebStorm.
I created a local variable named STYLE with main stylesheet name inside to add it as variable to File Watcher settings everywhere it needed.
But if I add $STYLE$ in a Path I get an error:
/Users/grawl/Sites/sitename/node_modules/node-sass/bin/node-sass app/styles/$STYLE$.scss public/$STYLE$.css
error reading file "app/styles/$STYLE$.scss"
Process finished with exit code 1
IDE just don't interprets my variable.
Also I tried to use %STYLE% but with no luck.
Please do not blame me for direct mapping filenames in File Watcher without using built-in variables like $FileName$ or $FileNameWithoutExtension$ because even WebStorm 9 EAP does not support preprocessor's dependencies except of built-in preprocessors like Sass and Jade.
This case is not only case to use local variables.
For example I want to put into variables my public/ path (that can be dest/ in other projects) and app/ (can be source/). And so on.
So let's figure out this feature.

In Ruby on Rails, what is good way for a site specific config / yaml file?

If a website is to be "cloned" as a different website, so that only the title, header, banner logo, and the products of the site are different, it probably will be good to move all the site-specific data to a config or yaml file.
(probably better if not using environment.rb so that the data is separate from other data, to keep it more self-contained, and easier for Program Management to edit the data)
In Rails 3, what is a good method to do this? I think the data probably will be mostly text, plus some data such as what product data to fetch from the main DB. The color theme / sprite offsets probably will still be best to keep in a separate .sass file.
I doing the following
Created a my_config.yaml under config folder with development, test and production settings.
Then used a script under initializers to load those settings
Contents of config/my_config.yaml
# This file contains all the Application Configuration
# config/my_config.yml
development:
greet: Hello Developer
test:
greet: Hello Tester
production:
greet: Hello World
Contents of config/initializers/load_config.rb
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/my_config.yml")[RAILS_ENV]
And now I can use APP_CONFIG['greet'] in my app
If you common setting across all environment then you can do some thing like the following
Contents of config/my_setting.yaml
page_title: Hello World
page_header: This is a test
Contents of config/initializers/load_settings.rb
APP_SETTING = YAML.load_file("#{RAILS_ROOT}/config/my_setting.yml")

Resources