0

I am trying to extend a bit the features-set available to manage groups in BibDesk and I want to manipulate from a program the bibtex comments in which BibDesk writes down information for Static Groups.

To do this I need a systematic and robust way to get all that is inside this comment part of the bibtex file.

@comment{BibDesk Static Groups{
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>group name</key>
        <string>MyGroupName</string>
        <key>keys</key>
        <string>BitexRefId1,BitexRefId2</string>
    </dict>
</array>
</plist>
}}

Once I put my hands on the XML array I think I know what to do with it, but the first part, getting the @comment{BibDesk Static Groups{ is a bit tricky to me. I would know how to it with sed using sed -e '/@comment{BibDesk Static Groups{/,/}/!d' test.bib, but what would be the pythonic way to do that? My best thing was essentially a home-grown parser

file = open(file_name,"r")
for line in file:
    if  static_groups_group:
        if "}" in line:
            static_groups_group=False
            print "ending static group block"
    if  static_groups_group:
        xml_groups.append(line)
    if "@comment{BibDesk Static Groups{" in line:
        print line," found"
        static_groups_group=True
Rho Phi
  • 1,182
  • 1
  • 12
  • 21

1 Answers1

0

This is a quick and dirty translation of your sed command. I wouldn't necessarily recommend this approach though, as it isn't particularly robust.

import re

with open(file_name) as fp:
    text = fp.read()

groups = re.findall(r'\@comment\{BibDesk Static Groups\{(.*?)\}\}', text, re.DOTALL)