Easiest way to specify alternate transmogrifier _path? - plone

I'm doing a content migration with collective.transmogrifier and I'm reading files off the file system with transmogrify.filesystem. Instead of importing the files "as is", I'd like to import them to a sub directory in Plone. What is the easiest way to modify the _path?
For example, if the following exists:
/var/www/html/bar/index.html
I'd like to import to:
/Plone/foo/bar/index.html
In other words, import the contents of "baz" to a subdirectory "foo". I see two options:
Use some blueprint in collective.transmogrifier to mangle _path.
Write some blueprint to mangle _path.
Am I missing anything easier?

Use the standard inserter blueprint to generate the paths; it accepts python expressions and can replace keys in-place:
[manglepath]
blueprint = collective.transmogrifier.sections.inserter
key = string:_path
value = python:item['_path'].replace('/var/www/html', '/Plone/foo')
This thus takes the output of the value python expression (which uses the item _path and stores it back under the same key.

Related

How to do file over-rides in hydra?

I have a main config file, let's say config.yaml:
num_layers: 4
embedding_size: 512
learning_rate: 0.2
max_steps: 200000
I'd like to be able to override this, on the command-line, with another file, like say big_model.yaml, which I'd use conceptually like:
python my_script.py --override big_model.yaml
and big_model.yaml might look like:
num_layers: 8
embedding_size: 1024
I'd like to be able to override with an arbitrary number of such files, each one taking priority over the last. Let's say I also have fast_learn.yaml
learning_rate: 2.0
And so I'd then want to conceptually do something like:
python my_script.py --override big_model.yaml --override fast_learn.yaml
What is the easiest/most standard way to do this in hydra? (or potentially in omegaconf perhaps?)
(note that I'd like these override files to ideally just be standard yaml files, that override the earlier yaml files, ideally; though if I have to write using override DSL instead, I can do that, if that's the easiest/best/most standard way)
It sounds like package override might be the a good solution for you.
The documentation can be found here: https://hydra.cc/docs/next/advanced/overriding_packages
an example application can be found here:
https://github.com/facebookresearch/hydra/tree/master/examples/advanced/package_overrides
using the example application as an example, you can achieve the override by doing something like
$ python simple.py db=postgresql db.pass=helloworld
db:
driver: postgresql
user: postgre_user
pass: helloworld
timeout: 10
Refer to the basic tutorial and read about config groups.
You can create arbitrary config groups, and select one option from each (As of Hydra 1.0, config groups options are mutually exclusive), you will need two config groups here:
one can be model, with a normal, small and big model, and another can trainer, with maybe normal and fast options.
Config groups can also override things in other config groups.
You can also always append to the defaults list from the command line - so you can also add additional config groups that are only used in the command line.
an example for that can an 'experiment' config group. You can use it as:
$ python train.py +experiment=exp1
In such config groups that are overriding things across the entire config you should use the global package (read more about packages in the docs).
# #package _global_
num_layers: 8
embedding_size: 1024
learning_rate: 2.0

Find a given file recursively inside a directory

Find a given file recursively inside a dir. The code I tried is not showing any output, though I have a file C:\Users\anaveed\test\hoax\a.txt
Below the code
import glob
import os
os.chdir(r'C:\Users\anaveed\test')
for f in glob.iglob('a.txt', recursive=True):
print(f)
No output
Looks like you need.
import glob
for f in glob.iglob(r'C:\Users\anaveed\test\**\a.txt', recursive=True):
print(f)
This is another way of doing it:
import os
path = r'C:\Users\anaveed\test'
filename = 'a.txt'
for root, dirs, files in os.walk(path):
for name in files:
if name == filename:
print(os.path.join(root, name))
A couple of comments:
you do not need to use glob if you are not specifying wildcards, just use os.walk()
you do not need to move to a specific path to look for files therein, just save the path into a variable.
it would be even better to wrap this into a function (perhaps using a list comprehension).
the glob solution is typically faster.

How to specify output name for qt5_add_translation?

I want to generate a plenty *.qm for plenty *.ts files for different languages using qt5_add_translation. All the *.ts files are named using *.de_DE.ts/*.fr_FR.ts/etc convention. But qt5_add_translation produce output, using only basename until first ., not the last one.
There is no possibility to pass options to lrelease using qt5_add_translation(QM_FILES "${PROJECT_NAME}.de_DE.ts" OPTIONS -qm "${PROJECT_NAME}.de_DE.qm") syntax.
Also setting OUTPUT_NAME property for source *.ts file is not working:
set_source_files_properties(
"${PROJECT_NAME}.de_DE.ts" PROPERTIES
OUTPUT_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_NAME "${PROJECT_NAME}.de_DE.qm"
)
Producing filename in the case is still "${PROJECT_NAME}.qm", not "${PROJECT_NAME}.de_DE.qm"
How to override producing name for resulting *.qm file?
Surely I can make custom command and use it for my purposes, but I prefer to use ready qt5_add_translation.
EDIT:
Looking at /usr/local/Qt-5.9.2/lib/cmake/Qt5LinguistTools/Qt5LinguistToolsMacros.cmake I conclude, that there is no way to achieve desired using ready to use qt5_add_translation, because of using get_filename_component(qm ${_abs_FILE} NAME_WE) to get filename:
NAME_WE = File name without directory or longest extension
For my purposes there is need to use combination of ABSOLUTE (to get filename w/ full suffix), then to apply multiple times EXT in combination with NAME_WE to extract filename w/o shortest extension.
I ended up with the below custom function add_translation to replace qt5_add_translation:
function(ADD_TRANSLATION _qm_files)
foreach(_basename ${ARGN})
set(qm "${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.qm")
add_custom_command(
OUTPUT "${qm}"
COMMAND "${Qt5_LRELEASE_EXECUTABLE}"
ARGS -markuntranslated "Not translated!" -nounfinished -removeidentical -compress "${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.ts" -qm "${qm}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${_basename}.ts" VERBATIM
)
list(APPEND ${_qm_files} "${qm}")
endforeach()
set(${_qm_files} ${${_qm_files}} PARENT_SCOPE)
endfunction()
It accepts basenames of *.ts files and produces list of resulting *.qm files: both in current source directory.
Please upgrade to Qt 5.9.4 or newer. The handling of .ts files with dots in the name has been fixed there, see also https://bugreports.qt.io/browse/QTBUG-64317 .

Does sbt have something like gradle's processResources task with ReplaceTokens support?

We are moving into Scala/SBT from a Java/Gradle stack. Our gradle builds were leveraging a task called processResources and some Ant filter thing named ReplaceTokens to dynamically replace tokens in a checked-in .properties file without actually changing the .properties file (just changing the output). The gradle task looks like:
processResources {
def whoami = System.getProperty( 'user.name' );
def hostname = InetAddress.getLocalHost().getHostName()
def buildTimestamp = new Date().format('yyyy-MM-dd HH:mm:ss z')
filter ReplaceTokens, tokens: [
"buildsig.version" : project.version,
"buildsig.classifier" : project.classifier,
"buildsig.timestamp" : buildTimestamp,
"buildsig.user" : whoami,
"buildsig.system" : hostname,
"buildsig.tag" : buildTag
]
}
This task locates all the template files in the src/main/resources directory, performs the requisite substitutions and outputs the results at build/resources/main. In other words it transforms src/main/resources/buildsig.properties from...
buildsig.version=#buildsig.version#
buildsig.classifier=#buildsig.classifier#
buildsig.timestamp=#buildsig.timestamp#
buildsig.user=#buildsig.user#
buildsig.system=#buildsig.system#
buildsig.tag=#buildsig.tag#
...to build/resources/main/buildsig.properties...
buildsig.version=1.6.5
buildsig.classifier=RELEASE
buildsig.timestamp=2013-05-06 09:46:52 PDT
buildsig.user=jenkins
buildsig.system=bobk-mbp.local
buildsig.tag=dev
Which, ultimately, finds its way into the WAR file at WEB-INF/classes/buildsig.properties. This works like a champ to record build specific information in a Properties file which gets loaded from the classpath at runtime.
What do I do in SBT to get something like this done? I'm new to Scala / SBT so please forgive me if this seems a stupid question. At the end of the day what I need is a means of pulling some information from the environment on which I build and placing that information into a properties file that is classpath loadable at runtime. Any insights you can give to help me get this done are greatly appreciated.
The sbt-buildinfo is a good option. The README shows an example of how to define custom mappings and mappings that should run on each compile. In addition to the straightforward addition of normal settings like version shown there, you want a section like this:
buildInfoKeys ++= Seq[BuildInfoKey](
"hostname" -> java.net.InetAddress.getLocalHost().getHostName(),
"whoami" -> System.getProperty("user.name"),
BuildInfoKey.action("buildTimestamp") {
java.text.DateFormat.getDateTimeInstance.format(new java.util.Date())
}
)
Would the following be what you're looking for:
sbt-editsource: An SBT plugin for editing files
sbt-editsource is a text substitution plugin for SBT 0.11.x and
greater. In a way, it’s a poor man’s sed(1), for SBT. It provides the
ability to apply line-by-line substitutions to a source text file,
producing an edited output file. It supports two kinds of edits:
Variable substitution, where ${var} is replaced by a value. sed-like
regular expression substitution.
This is from Community Plugins.

Powershell command for IIS 7 for replacing one string with another

I have a configuration setting in web. config file as follows:
<param name="url" value="http://localhost/services.msc">
I want to write a batch file using powershell to replace value="http://localhost/services.msc"
with port number as value="http://localhost:808/services.msc"
Is it possible to replace one text with another?
There are several ways to do this. My favorite way to modify any XML file if I know the value I'm trying to modify is to cast the content as XML, find the setting right where I know it to be, to modify it, and then to set the content of the config file to what I now have in powershell. An example is below. To find the exact value I usually take the item above it and pipe it to | fl until I get exactly what I'm looking at. You can read the XML and get the info probably alot easier.
#Make sure you set the webconfigpath variable.
#Make sure you know your keyname and where to find it in the XML doc.
$webConfig = [xml](Get-Content -path $webConfigPath)
$setting = $webConfig.configuration.appSettings.add | ?{$_.key -eq "KeyName"}
$setting.Value = "NewValue"
$webConfig.save($webConfigPath)
This may be something that can be changed with the Set-WebConfigurationProperty cmdlet, but I'm not sure and haven't used it personally.

Resources