genstrings does not work with macro for NSLocalizedString - iphone

I would like to shorten "NSLocalizedString" to "_" so I'm using macro
_(x) NSLocalizedString(#x, #__FILE__)
.
But now, when I want to generate strings for localization with
find . -name \*.m | xargs genstrings
it generates nothing.
Any help?

You can tell genstrings to look for a different function by using the '-s' argument:
genstring -s MyFunctionName ....
However, MyFunctionName must follow the same naming and argument conventions as one of the built in NSLocalizeString macros.
In your case, you can not just specify the string key, you must also specify the documentation string. In fact, you should never generate a strings file without both the string and documentation. There are many languages where the actual phrase or word will depend on context. German is a great example where a car is "das auto" and more than one is "die autos". There are many more examples that include changes for gender, number, time, question versus statement, and yes versus no. The documentation string helps your translator figure out what translation to use.
In addition, the best practice is to use a key that is different from the native language word. That says use NSLocalizedStringWithDefaultValue(key, table, bundle, val, comment).
You can specify nil for the table and [NSBundle mainBundle] for the bundle argument.
You can wrap this in a shorthand, but you still have to follow the StringWithDefaultValue name and the arguments for genstrings to work.
I strongly recommend you look at the WWDC 2012 session on Localization Tips and Tricks.
Maurice

You can use the -s option of genstrings. From the man page :
-s routine
Substitutes routine for NSLocalizedString. For example, -s MyLocalString will catch calls to MyLocalString and MyLocalStringFromTable.
So I think you could try :
genstrings -s _

I had the same problem when my NSLocalizedString macro was taking 1 argument instead of 2 like genstrings expects, so i wrote i python script that does the job.
the first argument for the script is the macro name and the second is the path to your project.
import fnmatch
import os
from xml.dom import minidom
function = sys.argv[1]
rootdir = sys.argv[2]
# Generate strings from .m files
files = []
for root, dirnames, filenames in os.walk(rootdir):
for filename in fnmatch.filter(filenames, '*.m'):
files.append(os.path.join(root, filename))
strings = []
for file in files:
lineNumber = 0
for line in open(file):
lineNumber += 1
index = line.find(function)
if (index != -1):
callStr = line[index:]
index = callStr.find('#')
if (index == -1):
print 'call with a variable/macro. file: ' + file + ' line: %d' % lineNumber
else:
callStr = callStr[index+1:]
index = callStr.find('")')
callStr = callStr[:index+1]
if callStr not in strings:
strings.append(callStr)
# Write strings to file
f = open('Localizable.strings', 'w+')
for string in strings:
f.write(string + ' = ' + string + ';\n\n')
f.close()

I have improved Or Arbel's script to include the cases where there's multiple macro-calls on a single line:
import fnmatch
import os
from xml.dom import minidom
import sys
function = sys.argv[1]
rootdir = sys.argv[2]
# Generate strings from .m files
files = []
for root, dirnames, filenames in os.walk(rootdir):
for filename in fnmatch.filter(filenames, '*.m'):
files.append(os.path.join(root, filename))
strings = []
for file in files:
lineNumber = 0
for line in open(file):
lineNumber += 1
index = line.find(function)
startIndex = 0
while (index != -1):
startIndex = index+1
callStr = line[index:]
index = callStr.find('#')
if (index == -1):
print 'call with a variable/macro. file: ' + file + ' line: %d' % lineNumber
else:
callStr = callStr[index+1:]
index = callStr.find('")')
callStr = callStr[:index+1]
if callStr not in strings:
strings.append(callStr)
index = line.find(function, startIndex)
# Write strings to file
f = open('Localizable.strings', 'w+')
for string in strings:
f.write(string + ' = ' + string + ';\n\n')
f.close()

Related

How to handle .doc corruption and password protected .doc files in python

I have a folder with 20,000 .doc/docx files in it. I needed to convert all of these to .pdf. I decided to go to python to achieve this and was able to put together a quick and dirty code to achieve the output. However I had to babysit the process as I would occasionally come across a corrupt .doc or .docx file or a password protected file. In these instances I would just like to skip over these files and continue on. I ended up having to find the file in question and remove from the folder and continue on. I have dug around in the win32com documentation but was unable to find anything. Code below
from os import chdir, getcwd, listdir, path
from time import strftime
from win32com import client
def count_files(filetype):
''' (str) -> int
Returns the number of files given a specified file type.
>>> count_files(".docx")
11
'''
count_files = 0
for files in listdir(folder):
if files.endswith(filetype):
count_files += 1
return count_files
# Function "check_path" is used to check whether the path the user provided does
# actually exist. The user is prompted for a path until the existence of the
# provided path has been verified.
def check_path(prompt):
''' (str) -> str
Verifies if the provided absolute path does exist.
'''
abs_path = raw_input(prompt)
while path.exists(abs_path) != True:
print ("\nThe specified path does not exist.")
abs_path = raw_input(prompt)
return abs_path
print ("\n")
folder = "My Absolute Folder Path Here"
# Change the directory.
chdir(folder)
# Count the number of docx and doc files in the specified folder.
num_docx = count_files(".docx")
num_doc = count_files(".doc")
# Check if the number of docx or doc files is equal to 0 (= there are no files
# to convert) and if so stop executing the script.
if num_docx + num_doc == 0:
print ("\nThe specified folder does not contain docx or docs files.")
print (strftime("%H:%M:%S"), "There are no files to convert. BYE, BYE!.")
exit()
else:
print ("\nNumber of doc and docx files: ", num_docx + num_doc, "")
print (strftime("%H:%M:%S"), "Starting to convert files ...")
# Try to open win32com instance. If unsuccessful return an error message.
try:
word = client.DispatchEx("Word.Application")
for files in listdir(getcwd()):
if files.endswith(".docx"):
new_name = files.replace(".docx", r".pdf")
in_file = path.abspath(folder + "\\" + files)
new_file = path.abspath(folder + "\\" + new_name)
doc = word.Documents.Open(in_file)
print (strftime("%H:%M:%S"), " docx -> pdf ", path.relpath(new_file))
doc.SaveAs(new_file, FileFormat = 17)
doc.Close()
if files.endswith(".doc"):
new_name = files.replace(".doc", r".pdf")
in_file = path.abspath(folder + "\\" + files)
new_file = path.abspath(folder + "\\" + new_name)
doc = word.Documents.Open(in_file)
print (strftime("%H:%M:%S"), " doc -> pdf ", path.relpath(new_file))
doc.SaveAs(new_file, FileFormat = 17)
doc.Close()
except Exception as e:
print (e)
finally:
word.Quit()
print ("\n", strftime("%H:%M:%S"), "Finished converting files.")
# Count the number of pdf files.
num_pdf = count_files(".pdf")
print ("\nNumber of pdf files: ", num_pdf)
# Check if the number of docx and doc file is equal to the number of files.
if num_docx + num_doc == num_pdf:
print ("\nNumber of doc and docx files is equal to number of pdf files.")
else:
print ("\nNumber of doc and docx files is not equal to number of pdf files.")
The following code is what I use (Excel VBA) to create PDF. It's the best I can do to help you. Hope it helps.
Sub WordtoPDF()
'OPEN IN EXCEL
'takes files from a location of particular file type, uses WORD to save them as PDFs to new location
Dim strOldFileName As String
Dim strOldPath As String
Dim strNewFileName As String
Dim strNewPath As String
Dim OldType As String
Dim NewType As String
Dim AraryFileNames() As String
Dim Path as String
Dim coll As Collection
Set coll = New Collection
'Allows to be used on any folder
Do While strOldPath = "" Or strOldPath = "False"
strOldPath = Application.InputBox("FolderPath containing Original files", "FolderPath eg C:\temp", "C:\temp", Type:=2)
If Dir(strOldPath, vbDirectory) = "" Then
MsgBox ("Directory doesn't exist")
Exit Sub
Else
End If
Loop
Do While OldType = "" Or OldType = "False" Or InStr(OldType, ".") <> 0
OldType = Application.InputBox("Original file's filetype", "FolderPath eg docx", "docx", Type:=2)
Loop
Do While strNewPath = "" Or strOldPath = "False"
strNewPath = Application.InputBox("location of NEW files.", "FolderPath eg C:\temp", strOldPath, Type:=2)
If Dir(strNewPath, vbDirectory) = "" Then
MsgBox ("Directory doesn't exist")
Exit Sub
Else
End If
Loop
Do While NewType = "" Or NewType = "False" Or InStr(NewType, ".") <> 0
NewType = Application.InputBox("file type to convert files to", "FolderPath eg docx becomes pdf", "pdf", Type:=2)
Loop
'AAAAA
'Counts how many files there are in the folder with the ".docx"(OldType) ending and makes a collection of their names
'creates a collection of with only "OldType" filetype.
Path = strOldPath & "\*." & OldType
fileName = Dir(Path)
coll.Add fileName
Do While fileName <> ""
Count = Count + 1
fileName = Dir()
coll.Add fileName
Loop
Dim item As Variant
On Error GoTo line1:
For Each item In coll
For i = 1 To coll.Count
If UCase(Right(coll(i), Len(OldType))) <> UCase(OldType) Then
coll.Remove (i)
i = 0
Else
End If
Next i
Next item
line1:
On Error GoTo 0
'AAAAA
'BBBBB
'Checks new location to make sure that files won't be saving over existing files
'collection file names with NewType extension checks to see if unique in new location.
On Error GoTo Error:
For i = 1 To coll.Count
Path2 = strNewPath & "\*." & NewType
fileName2 = Dir(Path2)
Do While fileName2 <> ""
If UCase(Left(coll(i), Len(coll(i)) - Len(OldType)) & NewType) = UCase(fileName2) Then
MsgBox (Left(coll(i), Len(coll(i)) - Len(OldType)) & NewType & " already exists in " & strNewPath)
Exit Sub
Else
End If
fileName2 = Dir()
Loop
Next i
Error:
On Error GoTo 0
'BBBBB
'CCCCC
'Opens each Old Type file in the original location using word, and saves as PDF with the same name in the new location
Set appWD = CreateObject("Word.Application")
For i = 1 To coll.Count
TempString = strOldPath & "\" & Left(coll(i), Len(coll(i)) - Len(OldType) - 1) & "." & OldType
Set objDoc = appWD.Documents.Open(fileName:=TempString)
TempString2 = strNewPath & "\" & Left(coll(i), Len(coll(i)) - Len(OldType) - 1) & "." & NewType
objDoc.ExportAsFixedFormat OutputFileName:=TempString2, ExportFormat:=17 '17 = wdExportFormatPDF
Next i
'CCCCC
If Not appWD Is Nothing Then
appWD.Quit
Set appWD = Nothing
End If
End Sub

Matlab Load from relative path

function []= read_c3d_feat(output_list_relative)
dir_list = importdata(output_list_relative);
dim_feat = 512;
for i = 1 : size(dir_list, 1)
dir_str = char(dir_list(i));
feat_files = dir([dir_str, '/*.res5b']);
num_feat = length(feat_files);
feat = zeros(num_feat, dim_feat);
for j = 1 : num_feat
feat_path = strcat(dir_str, '/', feat_files(j).name);
...............
....................so on
Give me error like
Error using dir
Invalid path. The path must not contain a null character.
Error in read_c3d_feat (line 12)
feat_files = dir([dir_str, '/*.res5b']);
Your dir_list variable must have strings which contain null characters, as the error tells you. If you try using hard-coded strings you will see it works:
function read_c3d_feat(output_list_relative)
dir_list = {'21';'45';'18'};
for i = 1:size(dir_list, 1)
dir_str = dir_list{i}; % Loops through '21','45','18'
% The dir function now works because we know dir_str is a valid string
feat_files = dir([dir_str, '/*.res5b']);
end
end
This means you need to debug your code and find out what this line is actually assigning to dir_list:
dir_list = importdata(output_list_relative);
Note that if dir_list is a cell of text entries, you should be indexing it with curly braces as above. If instead it is a matrix (because all of the entries seem to be numerical anyway) then you should be using num2str when passing to dir:
function read_c3d_feat(output_list_relative)
dir_list = importdata(output_list_relative);
dim_feat = 512;
for i = 1:size(dir_list, 1)
feat_files = dir([num2str(dir_list(i)), '/*.res5b']);
% ...

How do you order annotations by offset in brat?

When using the rapid annotator tool brat, it appears that the created annotations file will present the annotation in the order that the annotations were performed by the user. If you start at the beginning of a document and go the end performing annotation, then the annotations will naturally be in the correct offset order. However, if you need to go earlier in the document and add another annotation, the offset order of the annotations in the output .ann file will be out of order.
How then can you rearrange the .ann file such that the annotations are in offset order when you are done? Is there some option within brat that allows you to do this or is it something that one has to write their own script to perform?
Hearing nothing, I did write a python script to accomplish what I had set out to do. First, I reorder all annotations by begin index. Secondly, I resequence the label numbers so that they are once again in ascending order.
import optparse, sys
splitchar1 = '\t'
splitchar2 = ' '
# for brat, overlapped is not permitted (or at least a warning is generated)
# we could use this simplification in sorting by simply sorting on begin. it is
# probably a good idea anyway.
class AnnotationRecord:
label = 'T0'
type = ''
begin = -1
end = -1
text = ''
def __repr__(self):
return self.label + splitchar1
+ self.type + splitchar2
+ str(self.begin) + splitchar2
+ str(self.end) + splitchar1 + self.text
def create_record(parts):
record = AnnotationRecord()
record.label = parts[0]
middle_parts = parts[1].split(splitchar2)
record.type = middle_parts[0]
record.begin = middle_parts[1]
record.end = middle_parts[2]
record.text = parts[2]
return record
def main(filename, out_filename):
fo = open(filename, 'r')
lines = fo.readlines()
fo.close()
annotation_records = []
for line in lines:
parts = line.split(splitchar1)
annotation_records.append(create_record(parts))
# sort based upon begin
sorted_records = sorted(annotation_records, key=lambda a: int(a.begin))
# now relabel based upon the sorted order
label_value = 1
for sorted_record in sorted_records:
sorted_record.label = 'T' + str(label_value)
label_value += 1
# now write the resulting file to disk
fo = open(out_filename, 'w')
for sorted_record in sorted_records:
fo.write(sorted_record.__repr__())
fo.close()
#format of .ann file is T# Type Start End Text
#args are input file, output file
if __name__ == '__main__':
parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(),
usage=globals()['__doc__'],
version='$Id$')
parser.add_option ('-v', '--verbose', action='store_true',
default=False, help='verbose output')
(options, args) = parser.parse_args()
if len(args) < 2:
parser.error ('missing argument')
main(args[0], args[1])
sys.exit(0)

Read with order files in subfolders in matlab

I've got a folder which contains subfolders with text files. I want to read those file with the same order as they are in the subfolders. I've got a problem with that. I use the following matlab code:
outNames = {};
k=1;
feature = zeros(619,85);
fileN = cell(619,1);
for i=1:length(nameFolds)
dirList = dir(strcat(path, num2str(cell2mat(nameFolds(i,1)))));
names = {dirList.name};
outNames = {};
for j=1:numel(names)
name = names{j};
if ~isequal(name,'.') && ~isequal(name,'..')
[~,name] = fileparts(names{j});
outNames{end+1} = name;
fileName = strcat(path, num2str(cell2mat(nameFolds(i,1))), '\', name, '.descr' );
feature(k,:) = textread(fileName);
fileN{k} = [fileName num2str(k)];
k= k+1;
end
end
end
In one subfolder I've got the above text file names:
AnimalPrint_tiger_test_01.descr
AnimalPrint_tiger_test_02.descr
AnimalPrint_tiger_test_03.descr
AnimalPrint_tiger_test_04.descr
AnimalPrint_tiger_test_05.descr
AnimalPrint_tiger_test_06.descr
AnimalPrint_tiger_test_07.descr
AnimalPrint_tiger_test_08.descr
AnimalPrint_tiger_test_09.descr
AnimalPrint_tiger_test_10.descr
AnimalPrint_tiger_test_11.descr
AnimalPrint_tiger_test_12.descr
AnimalPrint_tiger_test_13.descr
AnimalPrint_tiger_test_14.descr
AnimalPrint_tiger_test_15.descr
AnimalPrint_zebra_test_1.descr
AnimalPrint_zebra_test_2.descr
AnimalPrint_zebra_test_3.descr
AnimalPrint_zebra_test_4.descr
AnimalPrint_zebra_test_5.descr
AnimalPrint_zebra_test_12.descr
But it seems that it reads first the AnimalPrint_zebra_test_12.descr and after the AnimalPrint_zebra_test_1.descr and the rest. Any idea why this happens?
dir sorts the files according to their names, for instance
test_1
test_12 % 1 followed by 2
test_2
test_3
You may want to build your own order with ['test_' num2str(variable) '.descr'] that concatenates test_ with an incrementing variable.

Renaming a Word document and saving its filename with its first 10 letters

I have recovered some Word documents from a corrupted hard drive using a piece of software called photorec. The problem is that the documents' names can't be recovered; they are all renamed by a sequence of numbers. There are over 2000 documents to sort through and I was wondering if I could rename them using some automated process.
Is there a script I could use to find the first 10 letters in the document and rename it with that? It would have to be able to cope with multiple documents having the same first 10 letters and so not write over documents with the same name. Also, it would have to avoid renaming the document with illegal characters (such as '?', '*', '/', etc.)
I only have a little bit of experience with Python, C, and even less with bash programming in Linux, so bear with me if I don't know exactly what I'm doing if I have to write a new script.
How about VBScript? Here is a sketch:
FolderName = "C:\Docs\"
Set fs = CreateObject("Scripting.FileSystemObject")
Set fldr = fs.GetFolder(Foldername)
Set ws = CreateObject("Word.Application")
For Each f In fldr.Files
If Left(f.name,2)<>"~$" Then
If InStr(f.Type, "Microsoft Word") Then
MsgBox f.Name
Set doc = ws.Documents.Open(Foldername & f.Name)
s = vbNullString
i = 1
Do While Trim(s) = vbNullString And i <= doc.Paragraphs.Count
s = doc.Paragraphs(i)
s = CleanString(Left(s, 10))
i = i + 1
Loop
doc.Close False
If s = "" Then s = "NoParas"
s1 = s
i = 1
Do While fs.FileExists(s1)
s1 = s & i
i = i + 1
Loop
MsgBox "Name " & Foldername & f.Name & " As " & Foldername & s1 _
& Right(f.Name, InStrRev(f.Name, "."))
'' This uses copy, because it seems safer
f.Copy Foldername & s1 & Right(f.Name, InStrRev(f.Name, ".")), False
'' MoveFile will copy the file:
'' fs.MoveFile Foldername & f.Name, Foldername & s1 _
'' & Right(f.Name, InStrRev(f.Name, "."))
End If
End If
Next
msgbox "Done"
ws.Quit
Set ws = Nothing
Set fs = Nothing
Function CleanString(StringToClean)
''http://msdn.microsoft.com/en-us/library/ms974570.aspx
Dim objRegEx
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True
objRegEx.Global = True
''Find anything not a-z, 0-9
objRegEx.Pattern = "[^a-z0-9]"
CleanString = objRegEx.Replace(StringToClean, "")
End Function
Word documents are stored in a custom format which places a load of binary cruft on the beginning of the file.
The simplest thing would be to knock something up in Python that searched for the first line beginning with ASCII chars. Here you go:
#!/usr/bin/python
import glob
import os
for file in glob.glob("*.doc"):
f = open(file, "rb")
new_name = ""
chars = 0
char = f.read(1)
while char != "":
if 0 < ord(char) < 128:
if ord("a") <= ord(char) <= ord("z") or ord("A") <= ord(char) <= ord("Z") or ord("0") <= ord(char) <= ord("9"):
new_name += char
else:
new_name += "_"
chars += 1
if chars == 100:
new_name = new_name[:20] + ".doc"
print "renaming " + file + " to " + new_name
f.close()
break;
else:
new_name = ""
chars = 0
char = f.read(1)
if new_name != "":
os.rename(file, new_name)
NOTE: if you want to glob multiple directories you'll need to change the glob line accordingly. Also this takes no account of whether the file you're trying to rename to already exists, so if you have multiple docs with the same first few chars then you'll need to handle that.
I found the first chunk of 100 ASCII chars in a row (if you look for less than that you end up picking up doc keywords and such) and then used the first 20 of these to make the new name, replacing anything that's not a-z A-Z or 0-9 with underscores to avoid file name issues.