Lua ranme files before extension - lua

Im having issues on renaming a file with a tag before the extension
here is the filename sample i want to rename:
AudiA6.bin
My current code name tag is "---Tuned"
And returning it like this:
AudiA6.bin---Tuned
I want it to handle the file like this:
AudiA6---Tuned.bin
here is my smaple:
if (OpenProjectVersion (olsnaam, 1versienummer)) then -- "1" => cast to integer
if (projectExport (output_path..inputfile.."---Tuned", eFiletypeBinary)) then
OUTPUT:write ("done"); --File exported successfully
projectClose();
gevonden = "1";
if (OpenProjectVersion (olsnaam, 1versienummer)) then -- "1" => cast to integer
if (projectExport (output_path..inputfile.."---Tuned", eFiletypeBinary)) then
OUTPUT:write ("done"); --File exported successfully
projectClose();
gevonden = "1";

local fileName = "AudiA6.bin"
local tag = "---Test"
local newFileName = fileName:gsub("(.*)(%.%w+)", "%1".. tag .. "%2")
print(newFileName)
Please read the Lua manual

Related

How do I create a file in the nix store?

I'd like to define/manage a config file within a nix file and have it end up in the nix store, how can I do this?
Currently I'm managing this (outside of the nix store) manually by having the file within a path like ~/example/config.json, and used like the below:
systemd.services = {
example = {
description = "abcxyz";
serviceConfig = {
WorkingDirectory = "%h/example/";
Type = "simple";
ExecStart = "${example-pkg}/bin/app -c config.json";
Restart = "always";
RestartSec = 60;
};
wantedBy = [ "default.target" ];
};
For example Nixos defines a postgresql service that has a config file located at /nix/store/pcp1r7f8mylwvri381an01r64knj1wwb-postgresql.conf/postgresql.conf - how could I replicate this functionality in my own service above?
Taking a look at https://github.com/NixOS/nixpkgs/blob/226ff16bd6fbe6d47e714379eeba8598cd61a90a/nixos/modules/services/databases/postgresql.nix#L21
configFile = pkgs.writeTextDir "postgresql.conf" (concatStringsSep "\n" (mapAttrsToList (n: v: "${n} = ${toStr v}") cfg.settings));
It's not clear to me what configFile is? Will it be the path to the postgresql.conf file?
Either way pkgs.writeTextDir "config.ini" seems to create a directory instead, should I be using pkgs.writeText instead?
The above seems to work:
configFile = pkgs.writeText "config.json"
''
example config file bla bla
'';
And then you can use configFile via string interpolation:
ExecStart = "${example-pkg}/bin/app -c ${configFile}";

Command line formatting in LUA

I'm getting stuck on the following. Probably pretty easy for you all, but I'm relatively new to LUA scripting.
return function ()
local getuservar = gma.user.getvar
local cmd = function(syntax, ...)
gma.cmd(syntax:format(...))
end
local Effect_Attribute = getuservar('Effect_Attribute')
local Effect_Group = getuservar('Effect_Group')
local Form = getuservar('Effect_Dim_Gr1')
cmd('copy image "B Form %s Active" at "B Effect Form %s %s" /m',Form, Effect_Attribute, Effect_Group)
end
That works perfectly.
What I would like to do is to create a string formatting for the getuservar, that looks something like:
local Effect_Attribute = getuservar('Effect_Attribute')
local Effect_Group = getuservar('Effect_Group')
local Form = getuservar('Effect_%s_%s', Effect_Attribute, Effect_Group)
How should I change the part of
local cmd = function(syntax, ...)
gma.cmd(syntax:format(...))
end
So, that I could use this style of formatting for my getuservar?

Reading File Path to a Variable

I want to use the variable that I passed to a function which contains a file path. However, I don't get it working.
For example, I have a path like "/samba-test/log_gen/log_gen/log_generator" and when I read this path to a variable it doesn't work as expected. Please refer to my explaination in the code. My comments
are tagged with the string "VENK" . Any help would be appreciated.
/* caller */
config_path = "/samba-test/log_gen/log_gen/log_generator"
ReadWrite_Config(config_path)
/*definition*/
def ReadWrite_Timeline(lp_readpath, lp_filterlist):
current_parent_path = lp_readpath
current_search_list = lp_filterlist
print(current_parent_path) >>>>>> VENK - PATH prints fine here as expected <<<<<<<<.
strings_1 = ("2e88422c-4b61-41d7-9cf9-4650edaa4e56", "2017-11-27 16:1")
for index in range(0,3):
print (current_search_list[index])
files=None
filext=[".txt",".log"]
#outputfile = open(wrsReportFileName, "a")
for ext in filext:
print("Current_Parent_Path",current_parent_path ) <<<<<<VENK - Prints as Expected ""
#VENK - The above line prints as ('Current_Parent_Path', '/samba-test/log_gen/log_gen/log_generator') which is expected
#The actual files are inside the 'varlog' where the 'varlog' folder is inside '/samba-test/log_gen/log_gen/log_generator'
#possible problematic line below.
varlogpath = "(current_parent_path/varlog)/*"+ext >>>>>>>>>>> VENK- Unable to find the files if given in this format <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
print("varlogpath",varlogpath) >>>>>>>>>>>> VENK- varlogpath doesn't print as expected <<<<<<<<<<<<<<<<<<<<
#VENK - The above line prints as ('varlogpath', 'current_parent_path/varlog/*.txt') which I feel is problematic.
#VENK - If I give the absolute path as below it works fine
#varlogpath = "/samba-test/log_gen/log_gen/log_generator/varlog/*"+ext
files = glob.glob(varlogpath)
for file in files:
fname_varlog = open(file, 'r')
outputfile.write("\n")
outputfile.write(file)
outputfile.write("\n")
for line in fname_varlog:
#if any(s in line for s in strings):
"""
#s1 searches the mandatory arguments
#s2 searches the optional arguments
"""
if all(s1 in line for s1 in strings_1):
#if all(s1 in line for s1 in strings_1) or all(s2 in line for s2 in strings_2):
#print (file, end="")
outputfile.write(line)
fname_varlog.close()
outputfile.write("\n")
outputfile.write("10.[##### Summary of the Issue #####] -- Enter Data Manually \n")
outputfile.write("\n")
outputfile.close()
#print (ext)
A path join to the variable 'current_parent_path' helped to resolve the problem (like below).
varlogpath = os.path.join(current_parent_path, "*"+ext)

loading all lua files in a directory

I am trying to "import" every file in the specified directory to a table but when i look at the output it only shows one imported file.
with "import" I mean require(dir.."/"..name) this will return a value witch will be put in the $table at position "name" so this looks like $table[name]
I have made the following code but it does not work for me.. I hope someone can tell me what I am doing wrong..
(the code below is simplified but does include all the files, structures and code associated with this function, also i have inserted print() and printTable() for debugging purposes)
I work on a Debian Jessie machine
file structure
~/x/main.lua
~/x/folder/file1.lua
~/x/folder/file2.lua
~/x/folder/file3.lua
~/x/folder/file4.lua
~/x/folder/file5.lua
./main.lua (printTable script)
local printTable = require "printTable"
-- scan dir and add the file contents (name ,function(if .lua), filepath from script) to a table
function appendDir(functiontable,dir)
for filename in io.popen('dir "'..dir..'" -1'):lines() do
print(filename)
local path = dir.."/"..filename
local name = filename:sub(1,filename:len()-4)
print(name)
local tempValue = false
if filename:sub(filename:len()-3,filename:len()) == ".lua" then tempValue = require(dir.."/"..name) end
functiontable[name]={["value"]=tempValue,["path"]=path}
printTable(functiontable)
end
print("---")
printTable(functiontable)
end
local table1 = {}
appendDir(table1,"./folder")
print("")
print("table1:")
printTable(table1)
for key,value in pairs(table1) do
value.value()
end
./folder/file*.lua all the files are build in the same way only the function returns a different string.
function func()
return "$string"
end
return func
the following strings are for the different files and are inserted in the $string position
~/x/folder/file1.lua $string=string of file 1
~/x/folder/file2.lua $string=string of file 2
~/x/folder/file3.lua $string=string of file 3
~/x/folder/file4.lua $string=string of file 4
~/x/folder/file5.lua $string=string of file 5
now when i execute the main.lua script i get:
~/x $ lua -v
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
~/x $dir folder -1
file1.lua
file2.lua
file3.lua
file4.lua
file5.lua
~/x $ lua main.lua
file1.lua
file1
[file1][value] -> TYPE == function
[file1][path] -> VALUE == ./folder/file1.lua
file2.lua
file2
[file1][value] -> TYPE == function
[file1][path] -> VALUE == ./folder/file1.lua
file3.lua
file3
[file3][value] -> TYPE == function
[file3][path] -> VALUE == ./folder/file3.lua
file4.lua
file4
[file3][value] -> TYPE == function
[file3][path] -> VALUE == ./folder/file3.lua
file5.lua
file5
[file4][value] -> TYPE == function
[file4][path] -> VALUE == ./folder/file4.lua
---
[file4][value] -> TYPE == function
[file4][path] -> VALUE == ./folder/file4.lua
table1:
[file4][value] -> TYPE == function
[file4][path] -> VALUE == ./folder/file4.lua
as #EgorSkriptunoff pointed out in the comment section of the original post it was a bug in the printTable function where the function returned to the for loop instead of the internal function. this has been resolved and committed to github!
Thank you for your help!

vlc lua: how do I get the full path of the current playing item?

I'm not a programmer so this is difficult for me. I want to make an extension to send the full path to the clipboard in the full format. Example:
D:\MyFolder\music\audio.mp3
I recently found and butchered this extension which sends the running time to the clipboard. Is it possible to modify it so it gets the full path instead of the running time?
I'm using VLC media player 2.0.5 Twoflower 32 bits.
Windows 7 professional 32bits SP1
Here's the content of the .lua file I'm using and want to modify:
-- Time2Clip.lua -- VLC extension --
--[[
INSTALLATION:
Put the file in the VLC subdir /lua/extensions, by default:
* Windows (all users): %ProgramFiles%\VideoLAN\VLC\lua\extensions\
Restart the VLC.
Then you simply use the extension by going to the "View" menu and selecting it.
--]]
function descriptor()
return {
title = "Time2Clip";
version = "1.0";
author = "valuex";
url = 'https://forum.videolan.org/viewtopic.php?f=29&t=101114';
shortdesc = "Time2Clip";
description = "<div style=\"background-color:lightgreen;\"><b>just a simple VLC extension </b></div>";
capabilities = {"input-listener"}
}
end
function activate()
create_dialog()
end
function close()
vlc.deactivate()
end
function create_dialog()
w = vlc.dialog("Time2Clip")
--w2 = w:add_button("Save_to_Clip", click_SAVE,2,1,1,1)
click_SAVE()
end
function click_SAVE()
local input = vlc.object.input()
if input then
local curtime=vlc.path()
-- local curtime=vlc.var.get(input, "time")
-- w2:set_text( curtime )
save_to_clipboard(curtime)
end
end
function save_to_clipboard(var)
strCmd = 'echo '..var..' |clip'
os.execute(strCmd)
vlc.deactivate()
end
I read LUA's README.TXT file and found this but I don't know how to use it. Please help me. Thanks in advance.
input.item(): Get the current input item. Input item methods are:
:uri(): Get item's URI.
:name(): Get item's name.
How about:
function descriptor()
return {
title = "URI2Clip";
version = "1.0";
author = "";
url = '';
shortdesc = "URI2Clip";
description = "<div><b>Copy the media URI to the Windows clipboard</b></div>";
}
end
function activate()
local item = vlc.input.item()
local uri = item:uri()
uri = string.gsub(uri, '^file:///', '')
uri = string.gsub(uri, '/', '\\')
strCmd = 'echo '..uri..' |clip'
os.execute(strCmd)
end
URI returns something like file:///c:/users/username/Documents/song.mp3 so I convert that to c:\users\username... format instead. NB. This is only going to work for saved files, it will mangle web addresses.

Resources