Commits

Jesse London committed d066945

util tweaks

  • Participants
  • Parent commits dd8290f

Comments (0)

Files changed (3)

parser/convert.py

 import argparse
 
 
+TARGET_ALL = r'.*'
 TARGET_PATTERNS = r'^\d((f.+)|(text\d+))\.wp[5d]$',
 IGNORED_PATTERNS = (
     r'^0fheads\.wpd$',
         description="Convert all WPD files under target directory to plain text"
     )
     parser.add_argument('-t', '--target', metavar='DIR', default=os.path.curdir)
-    parser.add_argument('--include', default=TARGET_PATTERNS, metavar="REGEX")
-    parser.add_argument('--ignore', default=IGNORED_PATTERNS, metavar="REGEX")
+    parser.add_argument('--include', action='append', metavar="REGEX")
+    parser.add_argument('--ignore', action='append', metavar="REGEX")
+    parser.add_argument('--clear-include-defaults', action='store_true')
+    parser.add_argument('--clear-ignore-defaults', action='store_true')
     parser.add_argument('destination', metavar='DIR')
     args = parser.parse_args()
-    main(args.target, args.destination, args.include, args.ignore)
+    ignores = args.ignore or []
+    if not args.clear_ignore_defaults:
+        ignores.extend(IGNORED_PATTERNS)
+    includes = args.include or []
+    if args.clear_include_defaults:
+        includes.append(TARGET_ALL)
+    else:
+        includes.extend(TARGET_PATTERNS)
+    main(args.target, args.destination, includes, ignores)
 
 if __name__ == '__main__':
     console()
 from datetime import date
 
 
-def main(target, out, version=None):
+def main(target, out, title, version=None):
     data = csv.reader(open(target))
     version = version or date.strftime(date.today(), '%m.%d.%Y')
     with open(out, 'w') as outfile:
+        outfile.write('var defaultDataTitle = "%s";\n' % title)
         outfile.write('var defaultDataVersion = "%s";\n' % version)
         outfile.write('var DefaultData = ')
         json.dump(list(data), outfile, indent=2)
     parser.add_argument('infile')
     parser.add_argument('outfile')
     parser.add_argument('-v', '--version')
+    parser.add_argument('-t', '--title', default='Article Index')
     args = parser.parse_args()
-    main(args.infile, args.outfile, args.version)
+    main(args.infile, args.outfile, args.title, args.version)
     issue = os.path.basename(dirpath)
     with open(os.path.join(dirpath, filename)) as handler:
         contents = handler.xreadlines()
-        title = contents.next().strip()
+        title = ''
+        while not title:
+            title = contents.next().strip()
         body = ''.join(contents).strip()
     return issue, title, body
 
 def get_original_path(target, originals, dirpath, filename):
     basename = os.path.splitext(filename)[0]
     basedir = dirpath.replace(target, '', 1).strip(os.sep)
-    for ext in ('wpd', 'wp5'):
-        path = os.path.join(originals, basedir, basename)
-        full = "%s.%s" % (path, ext)
-        if os.path.exists(full):
-            return full
+    original_path = os.path.join(originals, basedir)
+    for candidate in os.listdir(original_path):
+        if os.path.splitext(candidate)[0] == basename:
+            return os.path.join(original_path, candidate)
 
 
 def parse_files(target, originals, include_body=False):