Require Regex blocks in Separate File - coffeescript

How can I write coffee script regex blocks in a separate file and require that file where needed? I have a lot and it would be nice to keep them in a separate location.
EDIT:
I would like to keep things like:
texInputsPattern = ///
(.*[A-Za-z0-9_]\/\/:$) # Ensure last three characters are '//:'
///
In a separate file(patterns.coffee). I have tried:
module.exports=
texInputsPattern = ///
(.*[A-Za-z0-9_]\/\/:$) # Ensure last three characters are '//:'
///
anotherPattern1 = ///
... #
///
anotherPattern2 = ///
... #
///
anotherPattern3 = ///
... #
///
...
Then in main.coffee I tried:
_ = require 'underscore-plus'
module.exports =
_.clone(require('./patterns))
...
I also tried:
_ = require 'underscore-plus'
_.clone(require('./patterns))
module.exports =
...
I get a complaint about unexpected /. My question is how can I keep a file like patterns.coffee and include the patterns in main.coffee?

You look to simply be failing to import the objects containing the regexes. Are you running on node or browsers with browserify/webpack?
This should work:
regexes.coffee
module.exports =
a: /\d/
main.coffee
regexes = require('./regexes')
console.log(regexes.a.test(1)) # True
console.log(regexes.a.test('a')) # False

Related

Nested object in TOML

I have the following JSON:
{
"profile": {
"ci": {
"fuzz": {
"runs": 1000
}
}
}
}
Which I know I can write in TOML like this:
[profile.ci.fuzz]
runs = 1000
The problem is that I have multiple profiles, and writing profile.NAME.fuzz for all of them is rather repetitive.
I would like to ideally write the TOML like this:
[profile.ci]
fuzz = {
runs = 1000
}
However, that didn't work. I got this syntax error:
expected a table key, found a newline at line 2 column 9
How can I define nested objects in TOML?
TOML calls inline tables the objects defined within curly braces. Newlines are allowed for strings and arrays, but not for inline tables, from the specs:
Inline tables are intended to appear on a single line. A terminating comma (also called trailing comma) is not permitted after the last key/value pair in an inline table. No newlines are allowed between the curly braces unless they are valid within a value. Even so, it is strongly discouraged to break an inline table onto multiples lines. If you find yourself gripped with this desire, it means you should be using standard tables.
Regarding your example, this works:
[profile.ci]
fuzz = { runs = 1000 }
Something like this would also be allowed:
profiles = [
{ name = "foo", runs = 100 },
{ name = "bar", runs = 200 }
]

Can bitbake include/require a file based on a variable?

With bitbake, I'd like to be able to include/require a different file based on a variable, i.e.
require ${somevar}
From everything I've read this doesn't work directly, but maybe there's a solution for my specific use case. I'm using OECore and would like to have one image recipe that includes a group of settings located in another file based on something like the MACHINE. So rather than having to do this:
SETTING_A_machine1 = "..."
SETTING_B_machine1 = "..."
SETTING_C_machine1 = "..."
SETTING_D_machine1 = "..."
...
SETTING_A_machine2 = "..."
SETTING_B_machine2 = "..."
SETTING_C_machine2 = "..."
SETTING_D_machine2 = "..."
...
I'd like to be able to do something equivalent to this:
require include/${MACHINE}.inc
include/machine1.inc
SETTING_A = "..."
SETTING_B = "..."
SETTING_C = "..."
SETTING_D = "..."
include/machine2.inc
SETTING_A = "..."
SETTING_B = "..."
SETTING_C = "..."
SETTING_D = "..."
Any ideas?
Yocto supports machine override, could you give the following a try:
your-image-recipe.bb
include/machine1/common.inc
include/machine2/common.inc
Then in your-image-recipe.bb, you have:
require include/common.inc
Have you actually tried this?
require include/${MACHINE}.inc
should just work. If you look at bitbake.conf, this is how the machine configuration files are included in the first place! You just need to be aware that this implies immediate variable expansion so if you reference ${FOO} here, then later in the file change FOO, it won't be reflected in the file which is included.

Is there an easy way to add/remove/modify query parameters of a URL in Tritium?

I saw a very manual way of doing this in another post: How do I add a query parameter to a URL?
This doesn't seem very intuitive, but someone there mentioned an easier way to accomplish this using the upcoming "URL scope". Is this feature out yet, and how would I use it?
If you're using the stdlib mixer, you should be able to use the URL scope which provides helper functions for adding, viewing, editing, and removing URL params. Here's a quick example:
$original_url = "http://cuteoverload.com/2013/08/01/buttless-monkey-jams?hi=there"
$new_url = url($original_url) {
log(param("hi"))
param("hello", "world")
remove_param("hi")
}
log($new_url)
Tritium Tester example here: http://tester.tritium.io/9fcda48fa81b6e0b8700ccdda9f85612a5d7442f
Almost forgot, link to docs: http://tritium.io/current (You'll want to click on the URL category).
AFAIK, there's no built-in way of doing so.
I'll post here how I did to append a query param, making sure that it does not get duplicated if already on the url:
Inside your functions/main.ts file, you can declare:
# Adds a query parameter to the URL string in scope.
# The parameter is added as the last parameter in
# the query string.
#
# Sample use:
# $("//a[#id='my_link]") {
# attribute("href") {
# value() {
# appendQueryParameter('MVWomen', '1')
# }
# }
# }
#
# That will add MVwomen=1 to the end of the query string,
# but before any hash arguments.
# It also takes care of deciding if a ? or a #
# should be used.
#func Text.appendQueryParameter(Text %param_name, Text %param_value) {
# this beautiful regex is divided in three parts:
# 1. Get anything until a ? or # is found (or we reach the end)
# 2. Get anything until a # is found (or we reach the end - can be empty)
# 3. Get the remainder (can be empty)
replace(/^([^#\?]*)(\?[^#]*)?(#.*)?$/) {
var('query_symbol', '?')
match(%2, /^\?/) {
$query_symbol = '&'
}
# first, it checks if the %param_name with this %param_value already exists
# if so, we don't do anything
match_not(%2, concat(%param_name, '=', %param_value)) {
# We concatenate the URL until ? or # (%1),
# then the query string (%2), which can be empty or not,
# then the query symbol (either ? or &),
# then the name of the parameter we are appending,
# then an equals sign,
# then the value of the parameter we are appending
# and finally the hash fragment, which can be empty or not
set(concat(%1, %2, $query_symbol, %param_name, '=', %param_value, %3))
}
}
}
The other features you want (remove, modify) can be achieved similarly (by creating a function inside functions/main.ts and leveraging some regex magic).
Hope it helps.

Output '{{$NEXT}}' with Text::Template

The NextRelease plugin for Dist::Zilla looks for {{$NEXT}} in the Changes file to put the release datetime information. However, I can't get this to be generated using my profile.ini. Here's what I have:
[GenerateFile / Generate-Changes ]
filename = Changes
is_template = 1
content = Revision history for {{$dist->name}}
content =
;todo: how can we get this to print correctly with a template?
content = {{$NEXT}}
content = initial release
{{$dist->name}} is correctly replaced with my distribution name, but {{$NEXT}} is, as-is, replaced with nothing (since it's not escaped and there is no $NEXT variable in). I've tried different combinations of slashes to escape the braces, but it either results in nothing or an error during generation with dzil new. How can I properly escape this string so that after dzil processes it with Text::Template it outputs {{$NEXT}}?
In the content, {{$NEXT}} is being interpreted as a template block and, as you say, wants to fill itself in as the contents of the missing $NEXT.
Instead, try:
content = {{'{{$NEXT}}'}}
Example program:
use 5.14.0;
use Text::Template 'fill_in_string';
my $result = fill_in_string(
q<{{'{{$NEXT}}'}}>,
DELIMITERS => [ '{{', '}}' ],
BROKEN => sub { die },
);
say $result;

Sed and awk application

I've read a little about sed and awk, and understand that both are text manipulators.
I plan to use one of these to edit groups of files (code in some programming language, js, python etc.) to make similar changes to large sets of files.
Primarily editing function definitions (parameters passed) and variable names for now, but the more I can do the better.
I'd like to know if someone's attempted something similar, and those who have, are there any obvious pitfalls that one should look out for? And which of sed and awk would be preferable/more suitable for such an application. (Or maybe something entirely else? )
Input
function(paramOne){
//Some code here
var variableOne = new ObjectType;
array[1] = "Some String";
instanceObj = new Something.something;
}
Output
function(ParamterOne){
//Some code here
var PartOfSomething.variableOne = new ObjectType;
sArray[1] = "Some String";
var instanceObj = new Something.something
}
Here's a GNU awk (for "gensub()" function) script that will transform your sample input file into your desired output file:
$ cat tst.awk
BEGIN{ sym = "[[:alnum:]_]+" }
{
$0 = gensub("^(" sym ")[(](" sym ")[)](.*)","\\1(ParameterOne)\\3","")
$0 = gensub("^(var )(" sym ")(.*)","\\1PartOfSomething.\\2\\3","")
$0 = gensub("^a(rray.*)","sA\\1","")
$0 = gensub("^(" sym " =.*)","var \\1","")
print
}
$ cat file
function(paramOne){
//Some code here
var variableOne = new ObjectType;
array[1] = "Some String";
instanceObj = new Something.something;
}
$ gawk -f tst.awk file
function(ParameterOne){
//Some code here
var PartOfSomething.variableOne = new ObjectType;
sArray[1] = "Some String";
var instanceObj = new Something.something;
}
BUT think about how your real input could vary from that - you could have more/less/different spacing between symbols. You could have assignments starting on one line and finishing on the next. You could have comments that contain similar-looking lines to the code that you don't want changed. You could have multiple statements on one line. etc., etc.
You can address every issue one at a time but it could take you a lot longer than just updating your files and chances are you still will not be able to get it completely right.
If your code is EXCEEDINGLY well structured and RIGOROUSLY follows a specific, highly restrictive coding format then you might be able to do what you want with a scripting language but your best bets are either:
change the files by hand if there's less than, say, 10,000 of them or
get a hold of a parser (e.g. the compiler) for the language your files are written in and modify that to spit out your updated code.
As soon as it starts to get slightly more complicated you will switch to a script language anyway. So why not start with python in the first place?
Walking directories:
walking along and processing files in directory in python
Replacing text in a file:
replacing text in a file with Python
Python regex howto:
http://docs.python.org/dev/howto/regex.html
I also recommend to install Eclipse + PyDev as this will make debugging a lot easier.
Here is an example of a simple automatic replacer
import os;
import sys;
import re;
import itertools;
folder = r"C:\Workspaces\Test\";
skip_extensions = ['.gif', '.png', '.jpg', '.mp4', ''];
substitutions = [("Test.Alpha.", "test.alpha."),
("Test.Beta.", "test.beta."),
("Test.Gamma.", "test.gamma.")];
for root, dirs, files in os.walk(folder):
for name in files:
(base, ext) = os.path.splitext(name);
file_path = os.path.join(root, name);
if ext in skip_extensions:
print "skipping", file_path;
else:
print "processing", file_path;
with open(file_path) as f:
s = f.read();
before = [[s[found.start()-5:found.end()+5] for found in re.finditer(old, s)] for old, new in substitutions];
for old, new in substitutions:
s = s.replace(old, new);
after = [[s[found.start()-5:found.end()+5] for found in re.finditer(new, s)] for old, new in substitutions];
for b, a in zip(itertools.chain(*before), itertools.chain(*after)):
print b, "-->", a;
with open(file_path, "w") as f:
f.write(s);