I need to print the keys of Localizable.strings in my App, instead of their values (for a debugging purpose). Is there a fast way to override the NSLocalizedString() method or redefine the macro, something like:
#define NSLocalizedString(key, comment) NSLocalizedString(key, key)
One option would be to export your app for localizations via Product Menu > Export Localizations within Xcode, then save the xcloc file to your Desktop.
After which you could use a python script to parse the inner xliff (xml) to find the file elements with original attributes which contain Localizable.strings and print the trans-unit's source element text within the body of them. Here's an example of a python script which should do it localizationKeys.py:
import sys
import os.path
from xml.etree import ElementTree as et
import argparse as ap
import re
if __name__ == '__main__':
parser = ap.ArgumentParser()
# filename argument ex: de.xliff
parser.add_argument('filename', help="filename of the xliff to find keys ex:de.xliff")
# verbose flag
parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Show all the output')
args = parser.parse_args()
if (os.path.isfile(args.filename)):
tree = et.parse(args.filename)
root = tree.getroot()
match = re.match(r'\{.*\}', root.tag)
ns = match.group(0) if match else ''
files = root.findall(ns + 'file')
for file in files:
originalAttr = file.attrib['original']
# find all files which contain Localizable.strings
if originalAttr != None and 'Localizable.strings' in originalAttr:
if args.verbose == True:
print("----- Localizations for file: " + originalAttr + " -----")
# grab the body element
bodyElement = file.find(ns + 'body')
# get all the trans-units
transUnits = bodyElement.findall(ns + 'trans-unit')
for transUnit in transUnits:
# print all the source values (keys)
print(transUnit.find(ns + 'source').text)
else:
print("No file found with the specified name: " + args.filename)
Which you could then use as follows:
python3 localizationKeys.py en.xcloc/Localized\ Contents/en.xliff
Or if you'd prefer to print to to a file instead
python3 localizationKeys.py en.xcloc/Localized\ Contents/en.xliff > output.txt
This could almost definitely be more concise using xpath instead, but this is just what I came up with quickly.
Ok, this is how I obtained what I needed
// Overriding NSLocalizedString to print keys instead of values
#ifdef NSLocalizedString
#undef NSLocalizedString
#endif
#define NSLocalizedString(key, comment) key
In this way the App use the keys instead of the values
I am using CarrierWave and find myself doing this all the time:
fin = File.open(payload_file_name,'rb')
self.payload = fin
fin.close
where payload is my CarrierWave field. I considered doing:
self.payload = File.open(payload_file_name,'rb')
but I was concerned it would leave the file open and my app server would eventually run out of file pointers. Is there a Rubyism that will let me assign the return value of a simple block to a variable?
This is possible using a block with open:
File.open(payload_file_name,'rb') do |fin|
self.payload = fin.read
end
You can reduce that further, to a single line, but it won't accomplish anything additional:
File.open(payload_file_name,'rb') { |fin| self.payload = fin.read }
You could also use read:
self.payload = File.read(payload_file_name, :mode => 'rb')
But you knew all that after reading the documentation, right?
You can pass a block to File.open() and it will close automatically after the block finishes execution. Something like(untested):
File.open('payload_file_name', 'rb') do | file |
# do stuff with file
end
Or the one-liner version.
File.open('payload_file_name', 'rb') {|file| # do stuff }
Using IO::read:
self.payload = File.read(payload_file_name, mode: 'rb')
I'm trying to upload file but get this error message :
move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: The second argument to copy() function cannot be a directory
I think there's something problem with this file but I've no idea to solve it..
<?php
class FileUploadController extends CController {
public function actionUpload() {
$model = new FileUpload();
$form = new CForm('application.views.fileUpload.uploadForm', $model);
if ($form->submitted('submit') && $form->validate()) {
$form->model->image = CUploadedFile::getInstance($form->model, 'image');
if($model->validate())
{
$model->image->saveAs('/opt/lampp/htdocs/upl/images');
Yii::app()->user->setFlash('success', 'File Uploaded');
$this->redirect(array('upload'));
}
}
$this->render('upload', array('form' => $form));
}
}
?>
You either need to check all files existed at '/opt/lampp/htdocs/upl/images' and check if the same named file is available or not, if available then just rename the file with extra "_1" every time, or you can always upload the file by renaming the file into some machine name sort of thing see the code below,
$name = rand(1000,9999) . time(); // rand(1000,9999) optional
$name = md5($name); //optional
$model->image->saveAs('/opt/lampp/htdocs/upl/images/' . $name . '.jpg');
This is what I usually do with file uploads, provided that you're saving the files references into the database or in any text file.
EDIT
Get Extension.
In case if you're required to get extension of the file rather then of hard-coded, you can use $model->image->getExtensionName(); it will get you the extension of the uploaded file without . (dot)
Finally, I solved it by myself:
The problem was located in line
$model->image->saveAs('/opt/lampp/htdocs/upl/images');
It should be :
$model->image->saveAs('/opt/lampp/htdocs/upl/images/images.jpg');
Now, there's another problem: when I upload 'new image', it will be replace the old file, I want the file(s) being uploaded not replaced the old file or I need something like rename as new file. Does anyone knows?
goto cuploadedfile.php .
over write this function with this
if($this->_error==UPLOAD_ERR_OK)
{
if($deleteTempFile)
return move_uploaded_file($this->_tempName,$file."/".$this->getName());
elseif(is_uploaded_file($this->_tempName))
return copy($this->_tempName, $file."/".$this->getName());
else
return false;
}
thank u.........
What is the easiest method for joining/merging all files in a folder (tab delimited) into a single file? They all share a unique column (primary key). Actually, I only need to combine a certain column and link on this primary key, so the output file would contain a new column for each file. Ex:
KEY# Ratio1 Ratio2 Ratio3
1 5.1 4.4 3.3
2 1.2 2.3 3.2
etc....
There are many other columns in each file that I don't need to combine in the output file, I just need these "ratio" columns linked by the unique key column.
I am running OS X Snow Leopard but have access to a few Linux machines.
use the join(1) utility
I actually spent some time learning Perl and solved the issue on my own. I figured I'd share the source code if anyone has a similar problem to solve.
#!/usr/bin/perl -w
#File: combine_all.pl
#Description: This program will combine the rates from all "gff" files in the current directory.
use Cwd; #provides current working directory related functions
my(#handles);
print "Process starting... Please wait this may take a few minutes...\n";
unlink"_combined.out"; #this will remove the file if it exists
for(<./*.gff>){
#file = split("_",$_);
push(#files, substr($file[0], 2));
open($handles[#handles],$_);
}
open(OUTFILE,">_combined.out");
foreach (#files){
print OUTFILE"$_" . "\t";
}
#print OUTFILE"\n";
my$continue=1;
while($continue){
$continue=0;
for my$op(#handles){
if($_=readline($op)){
my#col=split;
if($col[8]) {
$gibberish=0;
$col[3]+=0;
$key = $col[3];
$col[5]+=0; #otherwise you print nothing
$col[5] = sprintf("%.2f", $col[5]);
print OUTFILE"$col[5]\t";
$continue=1;
} else {
$key = "\t";
$continue=1;
$gibberish=1;
}
}else{
#do nothing
}
}
if($continue != 0 && $gibberish != 1) {
print OUTFILE"$key\n";
} else {
print OUTFILE"\n";
}
}
undef#handles; #closes all files
close(OUTFILE);
print "Process Complete! The output file is located in the current directory with the filename: _combined.out\n";
How do I check if a directory exists in lua, preferably without using the LuaFileSystem module if possible?
Trying to do something like this python line:
os.path.isdir(path)
This is a way that works on both Unix and Windows, without any external dependencies:
--- Check if a file or directory exists in this path
function exists(file)
local ok, err, code = os.rename(file, file)
if not ok then
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
--- Check if a directory exists in this path
function isdir(path)
-- "/" works on both Unix and Windows
return exists(path.."/")
end
The problem is that the stock Lua distribution (nearly) only includes features that are specified in standard C. Standard C makes no presumptions about there actually being a file system of any specific sort out there (or even an operating system, for that matter), so the os and io modules don't provide access information not available from the standard C library.
If you were attempting to code in pure standard C, you would have the same issue.
There is a chance that you can learn whether the folder exists implicitly from an attempt to use it. If you expect it to exist and be writable to you, then create a temporary file there and if the that succeeds, the folder exists. If it fails, you might not be able to distinguish a non-existent folder from insufficient permissions, of course.
By far the lightest-weight answer to getting a specific answer would be a thin binding to just those OS-specific function calls that provide the information you need. If you can accept the lua alien module, then you can like do the binding in otherwise pure Lua.
Simpler, but slightly heavier, is to accept Lua File System. It provides a portable module that supports most things one might want to learn about files and the file system.
If you're specifically interested in avoiding the LFS library, the Lua Posix library has an interface to stat().
require 'posix'
function isdir(fn)
return (posix.stat(fn, "type") == 'directory')
end
Well, the 5.1 reference manual doesn't have anything in the os table, but if you use Nixstaller, you get os.fileexists for precisely what you've explained.
If you can afford to fiddle around a bit, or if you know what OS you'll be running on, you might get away with the standard os library's os.execute with some system call that will identify if the file exists.
Even better than os.execute might be os.rename:
os.rename(oldname, newname)
Renames file named oldname to newname.
If this function fails, it returns
nil, plus a string describing the
error.
You could try setting oldname and newname the same -- you might not have write permissions, though, so it might fail because you can't write, even though you can read. In that event, you'd have to parse the returned error string and deduce whether you could write, or you'd have to just try executing your function that needs an existing file, and wrap it in a pcall.
You can also use the 'paths' package. Here's the link to the package
Then in Lua do:
require 'paths'
if paths.dirp('your_desired_directory') then
print 'it exists'
else
print 'it does not exist'
end
This is tested for the windows platform. It is quite easy actually:
local function directory_exists( sPath )
if type( sPath ) ~= "string" then return false end
local response = os.execute( "cd " .. sPath )
if response == 0 then
return true
end
return false
end
Obviously, this may not work on other OS's. But for windows users, this can be a solution :)
here is a simple way to check if a folder exists WITHOUT ANY EXTERNAL LIBRARY DEPENDENCIES :)
function directory_exists(path)
local f = io.popen("cd " .. path)
local ff = f:read("*all")
if (ff:find("ItemNotFoundException")) then
return false
else
return true
end
end
print(directory_exists("C:\\Users"))
print(directory_exists("C:\\ThisFolder\\IsNotHere"))
If you copy and paste the above into Lua you should see
false
true
good luck :)
I use these (but i actually check for the error):
require("lfs")
-- no function checks for errors.
-- you should check for them
function isFile(name)
if type(name)~="string" then return false end
if not isDir(name) then
return os.rename(name,name) and true or false
-- note that the short evaluation is to
-- return false instead of a possible nil
end
return false
end
function isFileOrDir(name)
if type(name)~="string" then return false end
return os.rename(name, name) and true or false
end
function isDir(name)
if type(name)~="string" then return false end
local cd = lfs.currentdir()
local is = lfs.chdir(name) and true or false
lfs.chdir(cd)
return is
end
os.rename(name1, name2) will rename name1 to name2. Use the same name and nothing should change (except there is a badass error). If everything worked out good it returns true, else it returns nil and the errormessage. You said you dont want to use lfs. If you dont you cant differentiate between files and directorys without trying to open the file (which is a bit slow but ok).
So without LuaFileSystem
-- no require("lfs")
function exists(name)
if type(name)~="string" then return false end
return os.rename(name,name) and true or false
end
function isFile(name)
if type(name)~="string" then return false end
if not exists(name) then return false end
local f = io.open(name)
if f then
f:close()
return true
end
return false
end
function isDir(name)
return (exists(name) and not isFile(name))
end
It looks shorter, but takes longer...
Also open a file is a it risky, because of that you should use lfs.
If you don't care about performance (and errorhandling -.-) you can just use it.
Have fun coding!
My preferred way of doing this in linux is
if os.execute '[ -e "/home" ]' then
io.write "it exists"
if os.execute '[ -d "/home" ]' then
io.write " and is a directory"
end
io.write "\n"
end
or, to put this into a function:
function is_dir(path)
return os.execute(('[ -d "%s" ]'):format(path))
end -- note that this implementation will return some more values
This feels so simple that I feel like there's a reason nobody else has done this.
But it works on my machine (Ubuntu focal), for files and directories.
It does however say that existent-yet-forbidden files don't exist.
For quick scripts and/or few files:
function exists(path)
return (io.open(path,"r") ~= nil)
end
Good practice:
function exists(path)
local file = io.open(path,"r")
if (file ~= nil) then
io.close(file)
return true
else
return false
end
end
For Linux users:
function dir_exists( path )
if type( path ) ~= 'string' then
error('input error')
return false
end
local response = os.execute( 'cd ' .. path )
if response == nil then
return false
end
return response
end
local function directory_exist(dir_path)
local f = io.popen('[ -d "' .. dir_path .. '" ] && echo -n y')
local result = f:read(1)
f:close()
return result == "y"
end
try this
nixio.fs
a package widely used in openwrt,
has lots of useful functions, and easy to use
docs:
http://openwrt.github.io/luci/api/modules/nixio.fs.html#nixio.fs.stat
fs.stat, lstat
> fs = require'nixio.fs'
> st = fs.stat('/tmp')
> = st.type=='dir'
true
> = fs.lstat'/var'.type=='lnk'
true
> = fs.stat'/bin/sh'.type=='reg'
true
tests:
> = pj(fs.stat'/var')
{
"dev": 17,
"type": "dir",
"modedec": 755,
"rdev": 0,
"nlink": 28,
"atime": 1617348683,
"blocks": 0,
"modestr": "rwxr-xr-x",
"ino": 1136,
"mtime": 1666474113,
"gid": 0,
"blksize": 4096,
"ctime": 1666474113,
"uid": 0,
"size": 1960
}
> = pj(fs.lstat'/var')
{
"dev": 19,
"type": "lnk",
"modedec": 777,
"rdev": 0,
"nlink": 1,
"atime": 1613402557,
"blocks": 0,
"modestr": "rwxrwxrwx",
"ino": 1003,
"mtime": 1613402557,
"gid": 0,
"blksize": 1024,
"ctime": 1613402557,
"uid": 0,
"size": 3
}
about stat(), lstat() syscall
shell test operator
[ -e path ]
[ -f path ]
[ -d path ]
[ -L path ]
low level both rely on this syscall.
$ strace test -e /var |& grep stat | tail -1
stat("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
$ strace test -f /var |& grep stat | tail -1
stat("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
$ strace test -d /var |& grep stat | tail -1
stat("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
$ strace test -L /var |& grep stat | tail -1
lstat("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0